Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //TODO: Add PHPDoc
- //By Darko Lukic
- class Category extends CActiveRecord
- {
- const TYPE_MAIN = 1;
- const TYPE_CATEGORY = 2;
- const TYPE_TEST = 3;
- const TYPE_CUSTOM_TEST = 4;
- const VISIBILITY_HIDDEN = 0;
- const VISIBILITY_VISIBLE = 1;
- const VISIBILITY_HIGHLIGHTED = 2;
- private $parents;
- private $childs;
- public static function model($className = __CLASS__) {
- return parent::model($className);
- }
- public function tableName() {
- return '{{categories}}';
- }
- public function rules() {
- return array(
- 'parent_category' => array(self::BELONGS_TO, 'Category', 'category_id'),
- 'child_categories' => array(self::HAS_MANY, 'Category', 'category_id'),
- 'questions' => array(self::HAS_MANY, 'Question', 'category_id'),
- );
- }
- public function attributeLabels() {
- return array(
- );
- }
- public function getParents()
- {
- if ($this->parents != NULL)
- return $this->parents;
- $this->parents = array();
- $parentModel = $this->getParent();
- if ($parentModel)
- $this->parents[] = $parentModel;
- else return false;
- while($parentModel)
- {
- $parentModel = $this->getParent($parentModel->category_id);
- if ($parentModel)
- $this->parents[] = $parentModel;
- }
- return array_reverse($this->parents);
- }
- public function getParent($category_id = NULL)
- {
- if ($category_id == NULL)
- {
- if (isset($this->parents[0]))
- return $this->parents[0];
- return self::model()->findByAttributes(array('id' => $this->category_id));
- }
- if ($this->parents != NULL)
- foreach($this->parents as $parent)
- if ($parent->id == $category_id)
- return $parent;
- return self::model()->findByAttributes(array('id' => $category_id));
- }
- public static function getParentById($category_id)
- {
- return self::model()->findByAttributes(array('id' => $category_id));
- }
- public function getChilds()
- {
- if ($this->childs != NULL)
- return $this->childs;
- $this->childs = self::model()->findAllByAttributes(array('category_id' => $this->id));
- return $this->childs;
- }
- public function getTimeLimit()
- {
- if (empty($this->time_limit))
- {
- $parentModel = $this->getParent();
- if (!$parentModel)
- return 0;
- while (empty($parentModel->time_limit) && $parentModel->type != self::TYPE_MAIN)
- $parentModel = $this->getParent();
- return $parentModel->time_limit;
- }
- return $this->time_limit;
- }
- public function getImageUrl()
- {
- $imagesPath = Yii::app()->basePath.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'categories'.DIRECTORY_SEPARATOR;
- if (!file_exists($imagesPath.$this->id.'.png'))
- {
- $parentModel = $this->getParent();
- while (!file_exists($imagesPath.$parentModel->id.'.png'))
- $parentModel = $this->getParent();
- return Yii::app()->createUrl('images/categories/'.$parentModel->id.'.png');
- }
- return Yii::app()->createUrl('images/categories/'.$this->id.'.png');
- }
- public static function createBreadcrumbs($categoryParents, $category)
- {
- $breadcrumbs = array();
- if ($categoryParents)
- foreach ($categoryParents as $categoryParent)
- $breadcrumbs[$categoryParent->title] = Yii::app()->createUrl('category/view/', array('id' => $categoryParent->id));
- $breadcrumbs[] = $category->title;
- return $breadcrumbs;
- }
- public static function isTest($type)
- {
- if ($type == self::TYPE_TEST || $type == self::TYPE_CUSTOM_TEST)
- return true;
- else return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement