PageRenderTime 110ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/protected/modules/contentblock/models/ContentBlock.php

https://gitlab.com/RonLab1987/YupePlusClear
PHP | 240 lines | 132 code | 22 blank | 86 comment | 1 complexity | 99a62204806060604acd2f44b3ba1061 MD5 | raw file
  1. <?php
  2. /**
  3. * Модель ContentBlock
  4. *
  5. * @category YupeMigration
  6. * @package yupe.modules.contentblock.models
  7. * @author YupeTeam <team@yupe.ru>
  8. * @license BSD https://raw.github.com/yupe/yupe/master/LICENSE
  9. * @link http://yupe.ru
  10. **/
  11. /**
  12. * This is the model class for table "ContentBlock".
  13. *
  14. * The followings are the available columns in table 'ContentBlock':
  15. * @property string $id
  16. * @property string $name
  17. * @property integer $type
  18. * @property string $content
  19. * @property string $description
  20. * @property string $code
  21. * @property integer $category_id
  22. * @property integer $status
  23. *
  24. * @property Category|null $category
  25. *
  26. * @method ContentBlock active()
  27. */
  28. class ContentBlock extends yupe\models\YModel
  29. {
  30. /**
  31. *
  32. */
  33. const SIMPLE_TEXT = 1;
  34. /**
  35. *
  36. */
  37. const HTML_TEXT = 3;
  38. /**
  39. *
  40. */
  41. const RAW_TEXT = 4;
  42. /**
  43. *
  44. */
  45. const STATUS_NOT_ACTIVE = 0;
  46. /**
  47. *
  48. */
  49. const STATUS_ACTIVE = 1;
  50. /**
  51. * Returns the static model of the specified AR class.
  52. * @param string $className
  53. * @return ContentBlock the static model class
  54. */
  55. public static function model($className = __CLASS__)
  56. {
  57. return parent::model($className);
  58. }
  59. /**
  60. * @return string the associated database table name
  61. */
  62. public function tableName()
  63. {
  64. return '{{contentblock_content_block}}';
  65. }
  66. /**
  67. * @return array validation rules for model attributes.
  68. */
  69. public function rules()
  70. {
  71. return [
  72. ['name, code, content, type', 'filter', 'filter' => 'trim'],
  73. ['name, code', 'filter', 'filter' => [new CHtmlPurifier(), 'purify']],
  74. ['name, code, type, status', 'required'],
  75. ['type, category_id, status', 'numerical', 'integerOnly' => true],
  76. ['type', 'length', 'max' => 11],
  77. ['type', 'in', 'range' => array_keys($this->types)],
  78. ['name', 'length', 'max' => 250],
  79. ['code', 'length', 'max' => 100],
  80. ['description', 'length', 'max' => 255],
  81. [
  82. 'code',
  83. 'yupe\components\validators\YSLugValidator',
  84. 'message' => Yii::t(
  85. 'ContentBlockModule.contentblock',
  86. 'Unknown field format "{attribute}" only alphas, digits and _, from 2 to 50 characters'
  87. )
  88. ],
  89. ['code', 'unique'],
  90. ['id, name, code, type, content, description, category_id, status', 'safe', 'on' => 'search'],
  91. ];
  92. }
  93. /**
  94. * @return array
  95. */
  96. public function scopes()
  97. {
  98. return [
  99. 'active' => [
  100. 'condition' => $this->tableAlias . '.status = :status',
  101. 'params' => [':status' => self::STATUS_ACTIVE],
  102. ],
  103. ];
  104. }
  105. /**
  106. * @return array relational rules.
  107. */
  108. public function relations()
  109. {
  110. return [
  111. 'category' => [self::BELONGS_TO, 'Category', 'category_id']
  112. ];
  113. }
  114. /**
  115. * @return array customized attribute labels (name=>label)
  116. */
  117. public function attributeLabels()
  118. {
  119. return [
  120. 'id' => Yii::t('ContentBlockModule.contentblock', 'id'),
  121. 'name' => Yii::t('ContentBlockModule.contentblock', 'Title'),
  122. 'code' => Yii::t('ContentBlockModule.contentblock', 'Code'),
  123. 'type' => Yii::t('ContentBlockModule.contentblock', 'Type'),
  124. 'content' => Yii::t('ContentBlockModule.contentblock', 'Content'),
  125. 'description' => Yii::t('ContentBlockModule.contentblock', 'Description'),
  126. 'category_id' => Yii::t('ContentBlockModule.contentblock', 'Category'),
  127. 'status' => Yii::t('ContentBlockModule.contentblock', 'Status'),
  128. ];
  129. }
  130. /**
  131. * Retrieves a list of models based on the current search/filter conditions.
  132. * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
  133. */
  134. public function search()
  135. {
  136. $criteria = new CDbCriteria();
  137. $criteria->compare($this->tableAlias . '.id', $this->id);
  138. $criteria->compare($this->tableAlias . '.name', $this->name, true);
  139. $criteria->compare($this->tableAlias . '.code', $this->code, true);
  140. $criteria->compare($this->tableAlias . '.type', $this->type);
  141. $criteria->compare($this->tableAlias . '.content', $this->content, true);
  142. $criteria->compare($this->tableAlias . '.description', $this->description, true);
  143. $criteria->compare($this->tableAlias . '.category_id', $this->category_id);
  144. $criteria->compare($this->tableAlias . '.status', $this->status);
  145. return new CActiveDataProvider(get_class($this), ['criteria' => $criteria]);
  146. }
  147. /**
  148. * @return array
  149. */
  150. public function getTypes()
  151. {
  152. return [
  153. self::SIMPLE_TEXT => Yii::t('ContentBlockModule.contentblock', 'Simple text'),
  154. self::HTML_TEXT => Yii::t('ContentBlockModule.contentblock', 'HTML code'),
  155. self::RAW_TEXT => Yii::t('ContentBlockModule.contentblock', 'Raw text'),
  156. ];
  157. }
  158. /**
  159. * @return mixed|string
  160. */
  161. public function getType()
  162. {
  163. $data = $this->getTypes();
  164. return isset($data[$this->type]) ? $data[$this->type] : Yii::t(
  165. 'ContentBlockModule.contentblock',
  166. '*unknown type*'
  167. );
  168. }
  169. /**
  170. * @return string
  171. */
  172. public function getContent()
  173. {
  174. $content = '';
  175. switch ($this->type) {
  176. case ContentBlock::SIMPLE_TEXT:
  177. $content = CHtml::encode($this->content);
  178. break;
  179. case ContentBlock::HTML_TEXT:
  180. case ContentBlock::RAW_TEXT:
  181. $content = $this->content;
  182. break;
  183. }
  184. return $content;
  185. }
  186. /**
  187. * @return bool
  188. */
  189. protected function beforeSave()
  190. {
  191. Yii::app()->getCache()->delete("ContentBlock{$this->code}" . Yii::app()->language);
  192. return parent::beforeSave();
  193. }
  194. /**
  195. * @return string
  196. */
  197. public function getCategoryName()
  198. {
  199. return empty($this->category) ? Yii::t('ContentBlockModule.contentblock', '--not selected--') : $this->category->name;
  200. }
  201. /**
  202. * @return string
  203. */
  204. public function getCategoryAlias()
  205. {
  206. return empty($this->category) ? '<code_category>' : $this->category->slug;
  207. }
  208. /**
  209. * @return array
  210. */
  211. public function getStatusList()
  212. {
  213. return [
  214. self::STATUS_NOT_ACTIVE => Yii::t('ContentBlockModule.contentblock', 'Disabled'),
  215. self::STATUS_ACTIVE => Yii::t('ContentBlockModule.contentblock', 'Enabled'),
  216. ];
  217. }
  218. }