DevGroup-ru /
dotplant2
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace app\behaviors; |
||
| 4 | |||
| 5 | use app\modules\shop\models\Category; |
||
| 6 | use Yii; |
||
| 7 | use yii\helpers\ArrayHelper; |
||
| 8 | use yii\base\Behavior; |
||
| 9 | use yii\caching\TagDependency; |
||
| 10 | use yii\db\ActiveRecord; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Class Tree |
||
| 14 | * @package app\behaviors |
||
| 15 | * @property \yii\db\ActiveRecord $owner |
||
| 16 | * @property \yii\db\ActiveRecord[] $children |
||
| 17 | * @property \yii\db\ActiveRecord $parent |
||
| 18 | */ |
||
| 19 | class Tree extends Behavior |
||
| 20 | { |
||
| 21 | public $idAttribute = 'id'; |
||
| 22 | public $parentIdAttribute = 'parent_id'; |
||
| 23 | public $sortOrder = ['id' => SORT_ASC]; |
||
| 24 | public $cascadeDeleting = false; // @todo Set default value equals true and check all models that use Tree behavior |
||
| 25 | public $activeAttribute = 'active'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @return ActiveRecord |
||
| 29 | */ |
||
| 30 | public function getParent() |
||
| 31 | { |
||
| 32 | $cacheKey = 'TreeParent:' . $this->owner->className() . ':' . $this->owner->getAttribute($this->idAttribute); |
||
|
0 ignored issues
–
show
|
|||
| 33 | /** @var $parent ActiveRecord */ |
||
| 34 | $parent = Yii::$app->cache->get($cacheKey); |
||
| 35 | if ($parent === false) { |
||
| 36 | $className = $this->owner->className(); |
||
|
0 ignored issues
–
show
The method
yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.
This method has been deprecated. The supplier of the class has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead. Loading history...
|
|||
| 37 | $parent = new $className; |
||
| 38 | $parent_id = $this->owner->getAttribute($this->parentIdAttribute); |
||
| 39 | if ($parent_id < 1) { |
||
| 40 | return null; |
||
| 41 | } |
||
| 42 | /* findById does not have a calling standard and uses different parameters. For example isActive = 1. |
||
| 43 | * But we must get a parent model by primary id only. |
||
| 44 | * Uncomment this code if you unify findById method. |
||
| 45 | */ |
||
| 46 | // if ($parent->hasMethod('findById')) { |
||
| 47 | // $parent = $parent->findById($parent_id); |
||
| 48 | // } else { |
||
| 49 | if ($parent instanceof Category) { |
||
| 50 | $parent = Category::findById($parent_id, null); |
||
| 51 | } else { |
||
| 52 | |||
| 53 | $parent = $parent->findOne($parent_id); |
||
| 54 | } |
||
| 55 | Yii::$app->cache->set( |
||
| 56 | $cacheKey, |
||
| 57 | $parent, |
||
| 58 | 86400, |
||
| 59 | new TagDependency( |
||
| 60 | [ |
||
| 61 | 'tags' => [ |
||
| 62 | \devgroup\TagDependencyHelper\ActiveRecordHelper::getCommonTag($className), |
||
| 63 | ] |
||
| 64 | ] |
||
| 65 | ) |
||
| 66 | ); |
||
| 67 | |||
| 68 | } |
||
| 69 | return $parent; |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @return ActiveRecord[] |
||
| 74 | */ |
||
| 75 | public function getChildren() |
||
| 76 | { |
||
| 77 | $cacheKey = 'TreeChildren:' . $this->owner->className() . ':' . $this->owner->{$this->idAttribute}; |
||
|
0 ignored issues
–
show
The method
yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.
This method has been deprecated. The supplier of the class has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead. Loading history...
|
|||
| 78 | $children = Yii::$app->cache->get($cacheKey); |
||
| 79 | if ($children === false) { |
||
| 80 | /** @var $className ActiveRecord */ |
||
| 81 | $className = $this->owner->className(); |
||
|
0 ignored issues
–
show
The method
yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.
This method has been deprecated. The supplier of the class has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead. Loading history...
|
|||
| 82 | $children = $className::find() |
||
| 83 | ->where([$this->parentIdAttribute => $this->owner->{$this->idAttribute}]) |
||
| 84 | ->orderBy($this->sortOrder); |
||
| 85 | if ($this->activeAttribute !== false) { |
||
| 86 | $children = $children->andWhere([$this->activeAttribute => 1]); |
||
| 87 | } |
||
| 88 | $children = $children->all(); |
||
| 89 | Yii::$app->cache->set( |
||
| 90 | $cacheKey, |
||
| 91 | $children, |
||
| 92 | 86400, |
||
| 93 | new TagDependency( |
||
| 94 | [ |
||
| 95 | 'tags' => [ |
||
| 96 | \devgroup\TagDependencyHelper\ActiveRecordHelper::getCommonTag($className), |
||
| 97 | ] |
||
| 98 | ] |
||
| 99 | ) |
||
| 100 | ); |
||
| 101 | } |
||
| 102 | return $children; |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Helper function - converts 2D-array of rows from db to tree hierarchy for use in Menu widget sorted by parent_id ASC. |
||
| 107 | * |
||
| 108 | * Attributes needed for use with \yii\widgets\Menu: |
||
| 109 | * - name |
||
| 110 | * - route or url - if empty, then url attribute of menu item will be unset! |
||
| 111 | * - rbac_check _optional_ - will be used to determine if this menu item is allowed to user in rbac |
||
| 112 | * - parent_id, id - for hierarchy |
||
| 113 | * |
||
| 114 | * Optional attributes needed for use with \app\backend\widgets\Menu: |
||
| 115 | * - icon |
||
| 116 | * - class or css_class |
||
| 117 | * - translation_category - if exists and is set then the name will be translated with `Yii::t($item['translation_category'], $item['name'])` |
||
| 118 | * |
||
| 119 | * For example use see \app\backend\models\BackendMenu::getAllMenu() |
||
| 120 | * |
||
| 121 | * @param array $rows Array of rows. Example query: `$rows = static::find()->orderBy('parent_id ASC, sort_order ASC')->asArray()->all();` |
||
| 122 | * @param integer $start_index Start index of array to go through |
||
| 123 | * @param integer $current_parent_id ID of current parent |
||
| 124 | * @param boolean $native_menu_mode Use output for \yii\widgets\Menu |
||
| 125 | * @return array Tree suitable for 'items' attribute in Menu widget |
||
| 126 | */ |
||
| 127 | public static function rowsArrayToMenuTree($rows, $start_index = 0, $current_parent_id = 0, $native_menu_mode = true) |
||
| 128 | { |
||
| 129 | $index = $start_index; |
||
| 130 | $tree = []; |
||
| 131 | |||
| 132 | while (isset($rows[$index]) === true && $rows[$index]['parent_id'] <= $current_parent_id) { |
||
| 133 | if ($rows[$index]['parent_id'] != $current_parent_id) { |
||
| 134 | $index++; |
||
| 135 | continue; |
||
| 136 | } |
||
| 137 | $item = $rows[$index]; |
||
| 138 | |||
| 139 | $url = isset($item['route']) ? $item['route'] : $item['url']; |
||
| 140 | |||
| 141 | $tree_item = [ |
||
| 142 | 'label' => $item['name'], |
||
| 143 | 'url' => preg_match("#^(/|https?://)#Usi", $url) ? $url : ['/'.$url], |
||
| 144 | ]; |
||
| 145 | if (empty($url)) { |
||
| 146 | unset($tree_item['url']); |
||
| 147 | } |
||
| 148 | |||
| 149 | if (array_key_exists('rbac_check', $item)) { |
||
| 150 | $tree_item['visible'] = Yii::$app->user->can($item['rbac_check']); |
||
| 151 | } |
||
| 152 | |||
| 153 | if ($native_menu_mode === false) { |
||
| 154 | $attributes_to_check = ['icon', 'class']; |
||
| 155 | foreach ($attributes_to_check as $attribute) { |
||
| 156 | if (array_key_exists($attribute, $item)) { |
||
| 157 | $tree_item[$attribute] = $item[$attribute]; |
||
| 158 | } |
||
| 159 | } |
||
| 160 | if (array_key_exists('css_class', $item)) { |
||
| 161 | $tree_item['class'] = $item['css_class']; |
||
| 162 | } |
||
| 163 | |||
| 164 | // translate if translation_category is available |
||
| 165 | if (array_key_exists('translation_category', $item)) { |
||
| 166 | $tree_item['label'] = Yii::t($item['translation_category'], $item['name']); |
||
| 167 | } |
||
| 168 | } |
||
| 169 | $index++; |
||
| 170 | $tree_item['items'] = static::rowsArrayToMenuTree($rows, $index, $item['id'], $native_menu_mode); |
||
| 171 | |||
| 172 | if (empty($tree_item['visible']) && in_array(true, ArrayHelper::getColumn($tree_item['items'], 'visible'))) { |
||
| 173 | $tree_item['visible'] = true; |
||
| 174 | } |
||
| 175 | $tree[] = $tree_item; |
||
| 176 | } |
||
| 177 | return $tree; |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @inheritdoc |
||
| 182 | */ |
||
| 183 | public function events() |
||
| 184 | { |
||
| 185 | return [ |
||
| 186 | ActiveRecord::EVENT_AFTER_DELETE => 'afterDelete', |
||
| 187 | ]; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * After delete event. |
||
| 192 | * It deletes children models. |
||
| 193 | * @param $event |
||
| 194 | * @throws \Exception |
||
| 195 | */ |
||
| 196 | public function afterDelete($event) |
||
|
0 ignored issues
–
show
|
|||
| 197 | { |
||
| 198 | if ($this->cascadeDeleting) { |
||
| 199 | foreach ($this->children as $child) { |
||
| 200 | $child->delete(); |
||
| 201 | } |
||
| 202 | } |
||
| 203 | } |
||
| 204 | } |
||
| 205 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.