Blame view

common/models/Specialization.php 5.33 KB
71f66dca   Administrator   add Vitaliy's wid...
1
2
  <?php
  
9a26e63d   Yarik   Commit
3
      namespace common\models;
71f66dca   Administrator   add Vitaliy's wid...
4
  
9a26e63d   Yarik   Commit
5
6
7
      use Yii;
      use yii\db\ActiveQuery;
      use yii\helpers\ArrayHelper;
71f66dca   Administrator   add Vitaliy's wid...
8
9
  
      /**
9a26e63d   Yarik   Commit
10
11
12
13
14
15
       * This is the model class for table "specialization".
       * @property integer $specialization_id
       * @property integer $specialization_pid
       * @property string  $specialization_name
       * @property string  $image
       * @property string  $background
71f66dca   Administrator   add Vitaliy's wid...
16
       */
9a26e63d   Yarik   Commit
17
      class Specialization extends \yii\db\ActiveRecord
71f66dca   Administrator   add Vitaliy's wid...
18
      {
71f66dca   Administrator   add Vitaliy's wid...
19
  
9a26e63d   Yarik   Commit
20
21
22
23
24
25
26
          /**
           * @inheritdoc
           */
          public static function tableName()
          {
              return 'specialization';
          }
dafc9daf   Administrator   add yii jquery
27
  
9a26e63d   Yarik   Commit
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
          /**
           * @inheritdoc
           */
          public function rules()
          {
              return [
                  [
                      [
                          'specialization_pid',
                          'status',
                      ],
                      'integer',
                  ],
                  [
                      [ 'specialization_pid' ],
                      'default',
                      'value' => '0',
                  ],
                  [
                      [ 'specialization_name' ],
                      'required',
                  ],
                  [
                      [
                          'specialization_name',
                          'image',
                          'background',
                      ],
                      'string',
                      'max' => 255,
                  ],
              ];
          }
dafc9daf   Administrator   add yii jquery
61
  
9a26e63d   Yarik   Commit
62
63
64
65
66
67
68
69
70
71
72
73
74
75
          /**
           * @inheritdoc
           */
          public function attributeLabels()
          {
              return [
                  'specialization_id'          => Yii::t('app', 'specialization_id'),
                  'specialization_pid'         => Yii::t('app', 'specialization_pid'),
                  'specialization_name'        => Yii::t('app', 'specialization_name'),
                  'specialization_parent_name' => Yii::t('app', 'specialization_parent_name'),
                  'image'                      => Yii::t('app', 'image'),
                  'background'                 => Yii::t('app', 'background'),
                  'status'                     => Yii::t('app', 'status'),
              ];
dafc9daf   Administrator   add yii jquery
76
77
          }
  
9a26e63d   Yarik   Commit
78
79
          public static function specializationsList()
          {
dafc9daf   Administrator   add yii jquery
80
  
9a26e63d   Yarik   Commit
81
              $specializationArray = [ ];
dafc9daf   Administrator   add yii jquery
82
  
9a26e63d   Yarik   Commit
83
84
85
86
87
88
89
90
              $specialization = Specialization::find()
                                              ->where([
                                                  'specialization_id' => Specialization::find()
                                                                                       ->select('specialization_id')
                                                                                       ->andWhere('specialization_pid != 0')
                                                                                       ->column(),
                                              ])
                                              ->all();
dafc9daf   Administrator   add yii jquery
91
  
9a26e63d   Yarik   Commit
92
93
94
95
96
97
              foreach(ArrayHelper::index($specialization, 'specialization_id') as $spec) {
                  $array = $spec->hasChildrenInArray($specialization);
                  if($array) {
                      $specializationArray[ $spec->specialization_name ] = $array;
                  }
              }
71f66dca   Administrator   add Vitaliy's wid...
98
  
9a26e63d   Yarik   Commit
99
              return $specializationArray;
71f66dca   Administrator   add Vitaliy's wid...
100
  
9a26e63d   Yarik   Commit
101
          }
ea8c5991   Administrator   add Vitaliy's wid...
102
  
9a26e63d   Yarik   Commit
103
104
105
106
          public function getParent()
          {
              return $this->hasOne(self::className(), [ 'specialization_id' => 'specialization_pid' ]);
          }
35b03e57   Administrator   add yii jquery
107
  
9a26e63d   Yarik   Commit
108
109
110
          public function getChildren()
          {
              return $this->hasMany(self::className(), [ 'specialization_pid' => 'specialization_id' ]);
35b03e57   Administrator   add yii jquery
111
112
          }
  
9a26e63d   Yarik   Commit
113
114
115
          public function hasChildrenInArray($array)
          {
              $array = ArrayHelper::map($array, 'specialization_id', 'specialization_name', 'specialization_pid');
35b03e57   Administrator   add yii jquery
116
  
9a26e63d   Yarik   Commit
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
              if(isset( $array[ $this->specialization_id ] )) {
                  return $array[ $this->specialization_id ];
              } else {
                  return false;
              }
  
          }
  
          public function getStack()
          {
              $stack[] = $this->specialization_id;
              if(!empty( $this->children )) {
                  foreach($this->children as $child) {
                      $stack[] = $child->specialization_id;
                      if(!empty( $child->children )) {
                          $stack = array_merge($stack, ArrayHelper::getColumn($child->children, 'specialization_id', false));
                      }
004b2298   Yarik   test
134
135
                  }
              }
9a26e63d   Yarik   Commit
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
              return $stack;
          }
  
          /**
           * @return ActiveQuery
           */
          public static function getSorted()
          {
              return self::find()
                         ->with([
                             'children' => function($query) {
                                 /**
                                  * @var ActiveQuery $query
                                  */
                                 $query->orderBy('specialization_name');
                             },
                         ])
                         ->with([
                             'children.children' => function($query) {
                                 /**
                                  * @var ActiveQuery $query
                                  */
                                 $query->orderBy('specialization_name');
                             },
                         ])
                         ->where([ 'specialization_pid' => 0 ])
                         ->orderBy('specialization_name');
004b2298   Yarik   test
163
          }
004b2298   Yarik   test
164
  
9a26e63d   Yarik   Commit
165
      }