Advertisement
lukicdarkoo

Masterpiece, highly optimised :)

Jul 18th, 2013
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.35 KB | None | 0 0
  1. <?php
  2. //TODO: Add PHPDoc
  3. //By Darko Lukic
  4. class Category extends CActiveRecord
  5. {
  6.     const TYPE_MAIN = 1;
  7.     const TYPE_CATEGORY = 2;
  8.     const TYPE_TEST = 3;
  9.     const TYPE_CUSTOM_TEST = 4;
  10.    
  11.     const VISIBILITY_HIDDEN = 0;
  12.     const VISIBILITY_VISIBLE = 1;
  13.     const VISIBILITY_HIGHLIGHTED = 2;
  14.    
  15.     private $parents;
  16.     private $childs;
  17.    
  18.     public static function model($className = __CLASS__) {
  19.         return parent::model($className);
  20.     }
  21.    
  22.     public function tableName() {
  23.         return '{{categories}}';
  24.     }
  25.    
  26.     public function rules() {
  27.         return array(
  28.             'parent_category' => array(self::BELONGS_TO, 'Category', 'category_id'),
  29.             'child_categories' => array(self::HAS_MANY, 'Category', 'category_id'),
  30.             'questions' => array(self::HAS_MANY, 'Question', 'category_id'),
  31.         );
  32.     }
  33.    
  34.    
  35.     public function attributeLabels() {
  36.         return array(
  37.            
  38.         );
  39.     }
  40.    
  41.    
  42.     public function getParents()
  43.     {
  44.         if ($this->parents != NULL)
  45.             return $this->parents;
  46.        
  47.        
  48.         $this->parents = array();
  49.         $parentModel = $this->getParent();
  50.        
  51.         if ($parentModel)
  52.             $this->parents[] = $parentModel;
  53.         else return false;
  54.        
  55.        
  56.         while($parentModel)
  57.         {
  58.             $parentModel = $this->getParent($parentModel->category_id);
  59.            
  60.             if ($parentModel)
  61.                 $this->parents[] = $parentModel;
  62.         }
  63.        
  64.         return array_reverse($this->parents);
  65.     }
  66.    
  67.     public function getParent($category_id = NULL)
  68.     {        
  69.         if ($category_id == NULL)
  70.         {
  71.             if (isset($this->parents[0]))
  72.                 return $this->parents[0];
  73.  
  74.             return self::model()->findByAttributes(array('id' => $this->category_id));
  75.         }
  76.        
  77.         if ($this->parents != NULL)
  78.             foreach($this->parents as $parent)
  79.                 if ($parent->id == $category_id)
  80.                     return $parent;
  81.            
  82.         return self::model()->findByAttributes(array('id' => $category_id));
  83.     }
  84.    
  85.     public static function getParentById($category_id)
  86.     {
  87.         return self::model()->findByAttributes(array('id' => $category_id));
  88.     }
  89.  
  90.  
  91.     public function getChilds()
  92.     {
  93.         if ($this->childs != NULL)
  94.             return $this->childs;
  95.            
  96.         $this->childs = self::model()->findAllByAttributes(array('category_id' => $this->id));
  97.         return $this->childs;
  98.     }
  99.    
  100.     public function getTimeLimit()
  101.     {
  102.         if (empty($this->time_limit))
  103.         {
  104.             $parentModel = $this->getParent();
  105.            
  106.             if (!$parentModel)
  107.                 return 0;
  108.            
  109.             while (empty($parentModel->time_limit) && $parentModel->type != self::TYPE_MAIN)
  110.                 $parentModel = $this->getParent();
  111.            
  112.             return $parentModel->time_limit;
  113.         }
  114.        
  115.         return $this->time_limit;
  116.     }
  117.    
  118.     public function getImageUrl()
  119.     {
  120.         $imagesPath = Yii::app()->basePath.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'categories'.DIRECTORY_SEPARATOR;
  121.        
  122.         if (!file_exists($imagesPath.$this->id.'.png'))
  123.         {
  124.             $parentModel = $this->getParent();
  125.             while (!file_exists($imagesPath.$parentModel->id.'.png'))
  126.                 $parentModel = $this->getParent();
  127.            
  128.             return Yii::app()->createUrl('images/categories/'.$parentModel->id.'.png');
  129.         }
  130.        
  131.         return Yii::app()->createUrl('images/categories/'.$this->id.'.png');
  132.     }
  133.    
  134.     public static function createBreadcrumbs($categoryParents, $category)
  135.     {
  136.         $breadcrumbs = array();
  137.        
  138.         if ($categoryParents)
  139.             foreach ($categoryParents as $categoryParent)
  140.                 $breadcrumbs[$categoryParent->title] = Yii::app()->createUrl('category/view/', array('id' => $categoryParent->id));
  141.        
  142.         $breadcrumbs[] = $category->title;
  143.        
  144.         return $breadcrumbs;        
  145.     }
  146.    
  147.     public static function isTest($type)
  148.     {
  149.         if ($type == self::TYPE_TEST || $type == self::TYPE_CUSTOM_TEST)
  150.             return true;
  151.         else return false;
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement