Blame view

common/modules/partners/models/Item.php 2.87 KB
d1f8bd40   Alexey Boroda   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
  <?php
  
  namespace common\modules\partners\models;
  
  use Yii;
  use thread\app\base\models\ActiveRecord;
  use thread\app\base\module\abstracts\Module;
  use common\modules\partners\Partners as MainModule;
  
  
  /**
   * Class Item
   *
   * @property integer id
   * @property string image_link
   * @property string site_link
   * @property integer position
   * @property integer created_at
   * @property integer updated_at
   * @property boolean published
   * @property boolean deleted
   * @property \yii\db\ActiveQuery $lang
   * @property null|string $imageLink
   *
   * @author Alla Kuzmenko
   * @package common\modules\partners\models
   */
  class Item extends ActiveRecord
  {
  
      const moduleName = 'partners';
  
      /**
       * @return null|object|string
       */
      public static function getDb()
      {
          return MainModule::getDb();
      }
  
      /**
       * @return string
       */
      public static function tableName()
      {
          return '{{%partners_item}}';
      }
  
      /**
       * @return array
       */
      public function rules()
      {
          return [
              [['created_at', 'updated_at', 'position'], 'integer'],
              [['published', 'deleted'], 'in', 'range' => array_keys(static::statusKeyRange())],
              [['image_link', 'site_link'], 'string', 'max' => 255],
          ];
      }
  
      /**
       * @return array
       */
      public function scenarios()
      {
          return [
              'published' => ['published'],
              'deleted' => ['deleted'],
              'backend' => [
                  'image_link',
                  'site_link',
                  'position',
                  'published',
                  'deleted'
              ]
          ];
      }
  
      /**
       * @return array
       */
      public function attributeLabels()
      {
          return [
              'id' => Yii::t('app', 'ID'),
              'image_link' => Yii::t('app', 'Image'),
              'site_link' => Yii::t(self::moduleName, 'site link'),
              'position' => Yii::t('app', 'Position'),
              'created_at' => Yii::t('app', 'Create time'),
              'updated_at' => Yii::t('app', 'Update time'),
              'published' => Yii::t('app', 'Published'),
              'deleted' => Yii::t('app', 'Deleted'),
          ];
      }
  
      /**
       * @return \yii\db\ActiveQuery
       */
      public function getLang()
      {
          return $this->hasOne(ItemLang::class, ['rid' => 'id']);
      }
  
      /**
       * @return null|string
       */
      public function getImageLink()
      {
          $module = $this->getModule();
          $path = $module->getItemUploadPath();
          $url = $module->getItemUploadUrl();
  
          return (!empty($this->image_link) && is_file($path . '/' . $this->image_link))
              ? $url . '/' . $this->image_link
              : null;
      }
  
      /**
       * @return Module|null the module instance, `null` if the module does not exist.
       */
      public static function getModule()
      {
          return Yii::$app->getModule(self::moduleName);
      }
  }