Design.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace app\models;
  3. use app\models\base\NewsTopic;
  4. use app\models\base\Story;
  5. use Yii;
  6. /**
  7. * This is the model class for table "design".
  8. *
  9. * @property int $id id
  10. * @property int $parent_id ресурсы
  11. * @property string $type тип ресурса
  12. * @property int $design вариант отображения
  13. * @property string|null $title заголовок
  14. * @property int|null $sort сортировка
  15. * @property string|null $url URL
  16. * @property string $active спрятать
  17. * @property NewsTopic|Story $parent
  18. */
  19. class Design extends \yii\db\ActiveRecord
  20. {
  21. const WIDE_WIDGET = 1;
  22. const TEXT_NEWS_WIDGET = 2;
  23. const TOPIC_WIDGET = 3;
  24. const IMAGINE_NEWS_WIDGET = 4;
  25. const COLORFUL_WIDGET = 5;
  26. const MAY9 = 6;
  27. public static array $id_to_view_map = [
  28. self::WIDE_WIDGET=>'index/wide_widget',
  29. self::TEXT_NEWS_WIDGET=>'index/text_news_widget',
  30. self::TOPIC_WIDGET=>'index/topic_widget',
  31. self::IMAGINE_NEWS_WIDGET=>'index/partners_news',
  32. self::COLORFUL_WIDGET=>'index/colorful_widget',
  33. self::MAY9=>'index/wide_widget_9may',
  34. ];
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public static function tableName()
  39. {
  40. return 'design';
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function rules()
  46. {
  47. return [
  48. [['parent_id','active','type','design'], 'required'],
  49. [['parent_id', 'design', 'order'], 'integer'],
  50. [['active'], 'string'],
  51. [['type'], 'string', 'max' => 64],
  52. [['title', 'url'], 'string', 'max' => 255],
  53. ];
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function attributeLabels()
  59. {
  60. return [
  61. 'id' => 'id',
  62. 'parent_id' => 'ресурсы',
  63. 'type' => 'тип ресурса',
  64. 'design' => 'вариант отображения',
  65. 'title' => 'заголовок',
  66. 'order' => 'сортировка',
  67. 'url' => 'URL',
  68. 'active' => 'спрятать',
  69. ];
  70. }
  71. /**
  72. * {@inheritdoc}
  73. * @return status.
  74. */
  75. public function Active($id, $state = -1)
  76. {
  77. if( $state < 0 ){
  78. $this->active = ($this->active == 'Y')?'N':'Y';
  79. }else{
  80. $this->active = $state;
  81. }
  82. return $this->save();
  83. }
  84. private $_parent = false;
  85. /**
  86. * @return NewsTopic|Story|NULL
  87. */
  88. public function getParent()
  89. {
  90. if(!$this->_parent){
  91. switch ($this->type){
  92. case "topic":
  93. $this->_parent = NewsTopic::find()->alias("t")->andWhere(["t.id"=>$this->parent_id])->one();
  94. break;
  95. default:
  96. $this->_parent = Story::find()->alias("s")->andWhere(['s.id'=>$this->parent_id])->one();
  97. }
  98. }
  99. return $this->_parent;
  100. }
  101. }