Design.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. const INQUIRER_3 = 7;
  28. const INQUIRER_WIDE = 8;
  29. const VIDEO_WIDE = 9;
  30. public static array $id_to_view_map = [
  31. self::WIDE_WIDGET=>'index/wide_widget',
  32. self::TEXT_NEWS_WIDGET=>'index/text_news_widget',
  33. self::TOPIC_WIDGET=>'index/topic_widget',
  34. self::IMAGINE_NEWS_WIDGET=>'index/partners_news',
  35. self::COLORFUL_WIDGET=>'index/colorful_widget',
  36. self::MAY9=>'index/wide_widget_9may',
  37. self::INQUIRER_3=>'index/inquirer3',
  38. self::INQUIRER_WIDE=>'index/inquirer_wide',
  39. self::VIDEO_WIDE=>'index/wide_widget_video',
  40. ];
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public static function tableName()
  45. {
  46. return 'design';
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function rules()
  52. {
  53. return [
  54. [['parent_id','active','type','design'], 'required'],
  55. [['parent_id', 'design', 'order'], 'integer'],
  56. [['active'], 'string'],
  57. [['type'], 'string', 'max' => 64],
  58. [['title', 'url'], 'string', 'max' => 255],
  59. ];
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function attributeLabels()
  65. {
  66. return [
  67. 'id' => 'id',
  68. 'parent_id' => 'ресурсы',
  69. 'type' => 'тип ресурса',
  70. 'design' => 'вариант отображения',
  71. 'title' => 'заголовок',
  72. 'order' => 'сортировка',
  73. 'url' => 'URL',
  74. 'active' => 'спрятать',
  75. ];
  76. }
  77. /**
  78. * {@inheritdoc}
  79. * @return status.
  80. */
  81. public function Active($id, $state = -1)
  82. {
  83. if( $state < 0 ){
  84. $this->active = ($this->active == 'Y')?'N':'Y';
  85. }else{
  86. $this->active = $state;
  87. }
  88. return $this->save();
  89. }
  90. private $_parent = false;
  91. /**
  92. * @return NewsTopic|Story|NULL
  93. */
  94. public function getParent()
  95. {
  96. if(!$this->_parent){
  97. switch ($this->type){
  98. case "topic":
  99. $this->_parent = NewsTopic::find()->alias("t")->andWhere(["t.id"=>$this->parent_id])->one();
  100. break;
  101. default:
  102. $this->_parent = Story::find()->alias("s")->andWhere(['s.id'=>$this->parent_id])->one();
  103. }
  104. }
  105. return $this->_parent;
  106. }
  107. }