├── .gitattributes ├── NestedSetsTreeBehavior.php ├── README.md └── composer.json /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /NestedSetsTreeBehavior.php: -------------------------------------------------------------------------------- 1 | labelOutAttribute => $node[$this->labelAttribute], 59 | $this->outKey => $node[$this->key], 60 | ]; 61 | if (is_callable($makeLink = $this->makeLinkCallable)) { 62 | $newData += [ 63 | $this->hrefOutAttribute => $makeLink($node), 64 | ]; 65 | } 66 | return array_merge($node, $newData); 67 | }; 68 | 69 | // Trees mapped 70 | $trees = array(); 71 | $collection = $this->owner->find()->orderBy($this->leftAttribute)->asArray()->all(); 72 | 73 | if (count($collection) > 0) { 74 | foreach ($collection as &$col) $col = $makeNode($col); 75 | 76 | // Node Stack. Used to help building the hierarchy 77 | $stack = array(); 78 | 79 | foreach ($collection as $node) { 80 | $item = $node; 81 | $item[$this->childrenOutAttribute] = array(); 82 | 83 | // Number of stack items 84 | $l = count($stack); 85 | 86 | // Check if we're dealing with different levels 87 | while ($l > 0 && $stack[$l - 1][$this->depthAttribute] >= $item[$this->depthAttribute]) { 88 | array_pop($stack); 89 | $l--; 90 | } 91 | 92 | // Stack is empty (we are inspecting the root) 93 | if ($l == 0) { 94 | // Assigning the root node 95 | $i = count($trees); 96 | $trees[$i] = $item; 97 | $stack[] = &$trees[$i]; 98 | } else { 99 | // Add node to parent 100 | $i = count($stack[$l - 1][$this->childrenOutAttribute]); 101 | $stack[$l - 1][$this->hasChildrenOutAttribute] = true; 102 | $stack[$l - 1][$this->childrenOutAttribute][$i] = $item; 103 | $stack[] = &$stack[$l - 1][$this->childrenOutAttribute][$i]; 104 | } 105 | } 106 | } 107 | 108 | return $trees; 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | NestedSetsTreeBehavior 2 | ====================== 3 | NestedSetsTreeBehavior this is the connector between 4 | https://github.com/creocoder/yii2-nested-sets 5 | and 6 | https://github.com/wbraganca/yii2-fancytree-widget 7 | 8 | 9 | Installation 10 | ------------ 11 | 12 | The preferred way to install this extension is through [composer](http://getcomposer.org/download/). 13 | 14 | Either run 15 | 16 | ``` 17 | php composer.phar require --prefer-dist wokster/yii2-nested-sets-tree-behavior "*" 18 | ``` 19 | 20 | or add 21 | 22 | ``` 23 | "wokster/yii2-nested-sets-tree-behavior": "*" 24 | ``` 25 | 26 | to the require section of your `composer.json` file. 27 | 28 | 29 | Usage 30 | ----- 31 | 32 | Once the extension is installed, simply use it in your model : 33 | 34 | ```php 35 | 'htmlTree'=>[ 36 | 'class' => \wokster\treebehavior\NestedSetsTreeBehavior::className() 37 | ] 38 | ``` 39 | use 40 | ```php 41 | SomeModel::findOne($id)->tree() 42 | ``` 43 | to get array for fancytree-widget 44 | 45 | 46 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "wokster/yii2-nested-sets-tree-behavior", 3 | "description": "NestedSetsTreeBehavior for build array from creocoder nested sets behavior and fancytree widget", 4 | "type": "yii2-extension", 5 | "keywords": ["yii2","extension","creocoder","behavior","nested sets","array","fancytree"], 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "Maksym Tymofeiev", 10 | "email": "wokster@list.ru" 11 | } 12 | ], 13 | "require": { 14 | "yiisoft/yii2": "*", 15 | "creocoder/yii2-nested-sets": "^0.9.0" 16 | }, 17 | "autoload": { 18 | "psr-4": { 19 | "wokster\\treebehavior\\": "" 20 | } 21 | } 22 | } 23 | --------------------------------------------------------------------------------