Advertisement
tabvn

BuilderBase.php

Aug 16th, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.39 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @file
  4.  * Provides Drupal\builder\BuilderBase.
  5.  */
  6.  
  7. namespace Drupal\builder;
  8.  
  9.  
  10. use \Drupal\Core\Database\StatementInterface;
  11. use Drupal\Core\Cache\Cache;
  12. use \Drupal\Core\Cache\CacheBackendInterface;
  13.  
  14.  
  15. class BuilderBase
  16. {
  17.  
  18.  
  19.     protected $cache_id;
  20.  
  21.  
  22.     public function __construct()
  23.     {
  24.  
  25.         if (empty($this->cache_id)) {
  26.             $cache_id = $this->generateCacheId();
  27.             $this->cache_id = $cache_id;
  28.         }
  29.     }
  30.  
  31.  
  32.     public static function generateCacheId()
  33.     {
  34.  
  35.         return 'builder:' . uniqid();
  36.     }
  37.  
  38.     public static function createBid()
  39.     {
  40.  
  41.         $user = \Drupal::currentUser();
  42.  
  43.         $fields = array('uid' => $user->id(), 'created' => REQUEST_TIME, 'status' => 0);
  44.         $bid = db_insert('builder_data')
  45.             ->fields($fields)
  46.             ->execute();
  47.  
  48.  
  49.         return $bid;
  50.  
  51.     }
  52.  
  53.     public static function getCacheId($bid)
  54.     {
  55.         $language_id = \Drupal::languageManager()->getCurrentLanguage()->getId();
  56.         $cid = "builder:$language_id:$bid";
  57.         return $cid;
  58.     }
  59.  
  60.     public static function cache_get($cid)
  61.     {
  62.  
  63.         $cache = \Drupal::cache()->get($cid);
  64.  
  65.         if (isset($cache->data)) {
  66.             return $cache->data;
  67.         }
  68.         return $cache;
  69.  
  70.     }
  71.  
  72.     public static function cache_set($cid, $data)
  73.     {
  74.         $tags = array('module:builder');
  75.         \Drupal::cache()->set($cid, $data, CacheBackendInterface::CACHE_PERMANENT, $tags);
  76.  
  77.     }
  78.  
  79.     public static function getData($bid)
  80.     {
  81.         $data = db_select('builder_data', 'b')
  82.             ->fields('b', array('data'))
  83.             ->condition('b.bid', $bid)
  84.             ->execute()
  85.             ->fetchField();
  86.  
  87.         if (!empty($data)) {
  88.             $data = @unserialize($data);
  89.         } else {
  90.             $data = new \stdClass();
  91.             $data->bid = $bid;
  92.         }
  93.  
  94.         return $data;
  95.  
  96.     }
  97.  
  98.     public static function objToArray($obj, &$arr)
  99.     {
  100.  
  101.         if (!is_object($obj) && !is_array($obj)) {
  102.             $arr = $obj;
  103.             return $arr;
  104.         }
  105.  
  106.         foreach ($obj as $key => $value) {
  107.             if (!empty($value)) {
  108.                 $arr[$key] = array();
  109.                 self::objToArray($value, $arr[$key]);
  110.             } else {
  111.                 $arr[$key] = $value;
  112.             }
  113.         }
  114.         return $arr;
  115.  
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement