Blame view

common/components/artboxtree/ArtboxTreeQueryTrait.php 3.56 KB
a8370482   Alexander Karnovsky   init project
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
  <?php
  
  namespace common\components\artboxtree;
  
  use common\components\artboxtree\ArtboxTreeHelper;
  
  trait ArtboxTreeQueryTrait {
  
      static public $cache_tree = [];
  
      /** @var \yii\db\ActiveQuery $this */
      static $model;
  
      /*
       * @return \yii\db\ActiveQuery
       */
      private function getModel()
      {
          if (empty(self::$model)) {
              $class = $this->modelClass;
              self::$model = new $class;
          }
          return self::$model;
      }
  
14eadb86   Karnovsky A   Eager loading for...
26
      public function getTree($group = null, $with = null) {
a8370482   Alexander Karnovsky   init project
27
          $model = $this->getModel();
b519af22   Karnovsky A   Base-product func...
28
          if ($group !== null) {
14eadb86   Karnovsky A   Eager loading for...
29
              $this->andWhere([$model->keyNameGroup => $group]);
b519af22   Karnovsky A   Base-product func...
30
          }
14eadb86   Karnovsky A   Eager loading for...
31
32
33
          if ($with) {
              $this->with($with);
          }
e136edc8   Karnovsky A   ---
34
35
36
37
38
          $this->orderBy([$model->keyNameDepth => SORT_ASC]);
          $data = [];
          foreach($this->all() as $item) {
              $data[$item->{$model->keyNameId}] = $item;
          }
a8370482   Alexander Karnovsky   init project
39
40
41
          if (empty($data))
              return [];
  
b519af22   Karnovsky A   Base-product func...
42
          return $this->buildTree($data);
a8370482   Alexander Karnovsky   init project
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
      }
  
      private function _recursiveRebuild($tree, $parentPath = null, $depth = 0) {
          $model = $this->getModel();
  
          foreach ($tree as $row) {
              $path = (is_null($parentPath) ? '' : $parentPath . $model->delimiter) . $row['item']->getAttribute($model->keyNameId);
              $row['item']->setAttribute($model->keyNamePath, $path);
              $row['item']->setAttribute($model->keyNameDepth, $depth);
              $row['item']->save();
              if (!empty($row['children'])) {
                  $this->_recursiveRebuild($row['children'], $path, $depth+1);
              }
          }
      }
  
      /**
       * @param int $group
       */
14eadb86   Karnovsky A   Eager loading for...
62
63
      public function rebuildMP($group, $with = null) {
          $tree = $this->getTree($group, $with);
a8370482   Alexander Karnovsky   init project
64
65
66
67
  
          $this->_recursiveRebuild($tree);
      }
  
e136edc8   Karnovsky A   ---
68
      /*protected function buildTree(array $data, $parentId = 0) {
a8370482   Alexander Karnovsky   init project
69
70
71
72
73
74
75
76
77
78
79
80
81
          $model = $this->getModel();
  
          $result = [];
          foreach ($data as $element) {
              if ($element[$model->keyNameParentId] == $parentId) {
                  $children = $this->buildTree($data, $element[$model->keyNameId]);
                  $result[] = [
                      'item' => $element,
                      'children' => $children
                  ];
              }
          }
          return $result;
e136edc8   Karnovsky A   ---
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
      }*/
  
      protected function buildTree(array $data, $parentId = 0) {
          $model = $this->getModel();
  
          $result = [];
          foreach ($data as $element) {
  
              if ($element->{$model->keyNameDepth} == 0) {
                  $result[$element->{$model->keyNameId}]['item'] = $element;
                  $result[$element->{$model->keyNameId}]['children'] = [];
                  continue;
              }
              $parents = explode(",", trim($element->{$model->keyNamePath}, '{}'));
              $node = &$result[$parents[0]];
              for($i=1;$i<$element->{$model->keyNameDepth};$i++) {
                  $node = &$node['children'][$parents[$i]];
              }
  
              $node['children'][$element->{$model->keyNameId}]['item'] = $element;
          }
          return $result;
a8370482   Alexander Karnovsky   init project
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
      }
  
      public function normalizeTreeData(array $data, $parentId = null)
      {
          $model = $this->getModel();
  
          $result = [];
          foreach ($data as $element) {
              if ($element[$model->keyNameParentId] == $parentId) {
                  $result[] = $element;
                  $children = $this->normalizeTreeData($data, $element[$model->keyNameId]);
                  if ($children) {
                      $result = array_merge($result, $children);
                  }
              }
          }
          return $result;
      }
  }