Blame view

common/components/artboxtree/ArtboxTreeWidget.php 3.92 KB
d8c1a2e0   Yarik   Big commit artbox
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
  <?php

  

  namespace common\components\artboxtree;

  

  use Yii;

  use yii\base\Widget;

  use yii\i18n\Formatter;

  use yii\base\InvalidConfigException;

  

  class ArtboxTreeWidget extends Widget

  {

  

      /**

       * @var \yii\data\DataProviderInterface the data provider for the view. This property is required.

       */

      public $dataProvider;

  

      /**

       * @var string

       */

      public $keyNameId = 'id';

  

      /**

       * @var string

       */

      public $keyNameParentId = 'parent_id';

  

      /**

       * @var integer or null

       */

      public $maxLevel = null;

  

      /**

       * @var integer

       */

      public $rootParentId = 0;

  

      /**

       * @var string

       */

      public $emptyResult;

  

      /**

       * @var boolean include the CSS and JS files. Default is true.

       * If this is set false, you are responsible to explicitly include the necessary CSS and JS files in your page.

       */

      public $assetBundle;

  

      /**

       * @var array|Formatter the formatter used to format model attribute values into displayable texts.

       * This can be either an instance of [[Formatter]] or an configuration array for creating the [[Formatter]]

       * instance. If this property is not set, the "formatter" application component will be used.

       */

      public $formatter;

  

      /**

       * Init the widget object.

       */

      public function init()

      {

          parent::init();

          if ($this->dataProvider === null) {

              throw new InvalidConfigException('The "dataProvider" property must be set.');

          }

          if ($this->keyNameId === null) {

              throw new InvalidConfigException('The "keyNameId" property must be set.');

          }

          if ($this->formatter == null) {

              $this->formatter = Yii::$app->getFormatter();

          } elseif (is_array($this->formatter)) {

              $this->formatter = Yii::createObject($this->formatter);

          }

          if (!$this->formatter instanceof Formatter) {

              throw new InvalidConfigException('The "formatter" property must be either a Format object or a configuration array.');

          }

      }

  

      /**

       * Runs the widget.

       */

      public function run()

      {

          if (!empty($this->assetBundle) && class_exists($this->assetBundle)) {

              $view = $this->getView();

              $assetBundle = $this->assetBundle;

              $assetBundle::register($view);

          }

          if ($this->dataProvider->getCount() == 0) {

              return $this->renderEmptyResult();

          }

  

          parent::run();

      }

  

      protected function renderEmptyResult() {

          return empty($this->emptyResult) ? Yii::t('artbox', 'TreeViewEmptyResult') : Yii::t('artbox', $this->emptyResult);

      }

  

      /**

       * Normalize tree data

       * @param array $data

       * @param string $parentId

       * @return array

       */

8af13427   Yarik   For leha commit.
105
      protected function normalizeTreeData(array $data, $parentId = null) {

d8c1a2e0   Yarik   Big commit artbox
106
107
108
109
          $result = [];

          foreach ($data as $element) {

              if ($element[$this->keyNameParentId] == $parentId) {

                  $result[] = $element;

8af13427   Yarik   For leha commit.
110
                  $children = $this->normalizeTreeData($data, $element[$this->keyNameId]);

d8c1a2e0   Yarik   Big commit artbox
111
112
113
114
115
116
117
118
119
120
121
122
123
124
                  if ($children) {

                      $result = array_merge($result, $children);

                  }

              }

          }

          return $result;

      }

  

      /**

       * Hierarchy tree data

       * @param array $data

       * @param string $parentId

       * @return array

       */

8af13427   Yarik   For leha commit.
125
      protected function hierarchyTreeData(array $data, $parentId = null) {

d8c1a2e0   Yarik   Big commit artbox
126
127
128
          $result = [];

          foreach ($data as $element) {

              if ($element[$this->keyNameParentId] == $parentId) {

8af13427   Yarik   For leha commit.
129
                  $children = $this->hierarchyTreeData($data, $element[$this->keyNameId]);

d8c1a2e0   Yarik   Big commit artbox
130
131
132
133
134
135
136
137
138
                  $result[] = [

                      'item' => $element,

                      'children' => $children

                  ];

              }

          }

          return $result;

      }

  }