├── .gitignore ├── src ├── interfaces │ ├── TreeQueryInterface.php │ └── TreeInterface.php ├── forms │ └── MoveNodeForm.php ├── widgets │ └── nestable │ │ ├── NestableAsset.php │ │ ├── assets │ │ ├── jquery.nestable.css │ │ └── jquery.nestable.js │ │ └── Nestable.php └── actions │ ├── DeleteNodeAction.php │ ├── CreateNodeAction.php │ ├── UpdateNodeAction.php │ ├── BaseAction.php │ └── MoveNodeAction.php ├── LICENSE ├── composer.json ├── README.md └── patch.diff /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | vendor 3 | composer.lock -------------------------------------------------------------------------------- /src/interfaces/TreeQueryInterface.php: -------------------------------------------------------------------------------- 1 | findModel($id); 27 | 28 | return $model->deleteWithChildren(); 29 | } 30 | } -------------------------------------------------------------------------------- /src/actions/CreateNodeAction.php: -------------------------------------------------------------------------------- 1 | modelClass); 24 | 25 | $params = Yii::$app->getRequest()->getBodyParams(); 26 | $model->load($params); 27 | 28 | if (!$model->validate()) { 29 | return $model; 30 | } 31 | 32 | $roots = $model::find()->roots()->all(); 33 | 34 | if (isset($roots[0])) { 35 | return $model->appendTo($roots[0])->save(); 36 | } else { 37 | return $model->makeRoot()->save(); 38 | } 39 | } 40 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Vitaly 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /src/actions/UpdateNodeAction.php: -------------------------------------------------------------------------------- 1 | findModel($id); 35 | 36 | $name = Yii::$app->request->post('name'); 37 | $model->setAttribute($this->nameAttribute, $name); 38 | 39 | if (!$model->validate()) { 40 | return $model; 41 | } 42 | 43 | return $model->update(true, [$this->nameAttribute]); 44 | } 45 | } -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "voskobovich/yii2-tree-manager", 3 | "description": "Tree Manager using jquery.nestable plugin for Yii 2", 4 | "keywords": [ 5 | "widget", 6 | "nested sets", 7 | "yii2", 8 | "nestable", 9 | "editor" 10 | ], 11 | "homepage": "https://github.com/voskobovich/yii2-tree-manager", 12 | "type": "yii2-widget", 13 | "license": "MIT", 14 | "support": { 15 | "issues": "https://github.com/voskobovich/yii2-tree-manager/issues", 16 | "source": "https://github.com/voskobovich/yii2-tree-manager" 17 | }, 18 | "authors": [ 19 | { 20 | "name": "Vitaly Voskobovich", 21 | "email": "vitaly@voskobovich.com", 22 | "homepage": "http://voskobovich.com" 23 | } 24 | ], 25 | "require": { 26 | "php": ">=5.4.0", 27 | "yiisoft/yii2": "^2.0.0", 28 | "yiisoft/yii2-bootstrap": "^2.0.0" 29 | }, 30 | "autoload": { 31 | "psr-4": { 32 | "voskobovich\\tree\\manager\\actions\\": "src/actions", 33 | "voskobovich\\tree\\manager\\behaviors\\": "src/behaviors", 34 | "voskobovich\\tree\\manager\\forms\\": "src/forms", 35 | "voskobovich\\tree\\manager\\interfaces\\": "src/interfaces", 36 | "voskobovich\\tree\\manager\\widgets\\nestable\\": "src/widgets/nestable" 37 | } 38 | } 39 | } -------------------------------------------------------------------------------- /src/actions/BaseAction.php: -------------------------------------------------------------------------------- 1 | modelClass) { 30 | throw new InvalidConfigException('Param "modelClass" must be contain model name with namespace.'); 31 | } 32 | } 33 | 34 | /** 35 | * @param $id 36 | * @return ActiveRecord|TreeInterface 37 | * @throws NotFoundHttpException 38 | */ 39 | public function findModel($id) 40 | { 41 | /** @var ActiveRecord $model */ 42 | $model = $this->modelClass; 43 | /** @var ActiveRecord|TreeInterface $model */ 44 | $model = $model::findOne($id); 45 | 46 | if ($model == null) { 47 | throw new NotFoundHttpException(); 48 | } 49 | 50 | return $model; 51 | } 52 | } -------------------------------------------------------------------------------- /src/actions/MoveNodeAction.php: -------------------------------------------------------------------------------- 1 | findModel($id); 29 | 30 | $form = new MoveNodeForm(); 31 | 32 | $params = Yii::$app->getRequest()->getBodyParams(); 33 | $form->load($params, ''); 34 | 35 | if (!$form->validate()) { 36 | return $form; 37 | } 38 | 39 | try { 40 | if ($form->prev_id > 0) { 41 | $parentModel = $this->findModel($form->prev_id); 42 | if ($parentModel->isRoot()) { 43 | return $model->appendTo($parentModel)->save(); 44 | } else { 45 | return $model->insertAfter($parentModel)->save(); 46 | } 47 | } elseif ($form->next_id > 0) { 48 | $parentModel = $this->findModel($form->next_id); 49 | return $model->insertBefore($parentModel)->save(); 50 | } elseif ($form->parent_id > 0) { 51 | $parentModel = $this->findModel($form->parent_id); 52 | return $model->appendTo($parentModel)->save(); 53 | } 54 | } catch (Exception $ex) { 55 | } 56 | 57 | return false; 58 | } 59 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Tree Manager for Yii2 2 | 3 | Виджет для управления деревом. 4 | 5 | Внимание! 6 | ----- 7 | Виджет рассчитан на работу с поведениями Павла Зимакова: 8 | 9 | [Yii2 Adjacency List Behavior](https://github.com/paulzi/yii2-adjacency-list) 10 | [Yii2 Nested Sets Behavior](https://github.com/paulzi/yii2-nested-sets) 11 | [Yii2 Nested Intervals Behavior](https://github.com/paulzi/yii2-nested-intervals) 12 | [Yii2 Materialized Path Behavior](https://github.com/paulzi/yii2-materialized-path) 13 | 14 | Отличная статья на [Хабре](http://habrahabr.ru/post/266155/). 15 | 16 | 17 | Installation 18 | ------------- 19 | 20 | The preferred way to install this extension is through [composer](http://getcomposer.org/download/). 21 | 22 | Either run 23 | 24 | ``` 25 | php composer.phar require --prefer-dist voskobovich/yii2-tree-manager "~1.0" 26 | ``` 27 | 28 | or add 29 | 30 | ``` 31 | "voskobovich/yii2-tree-manager": "~1.0" 32 | ``` 33 | 34 | to the require section of your `composer.json` file. 35 | 36 | 37 | Usage 38 | ----- 39 | 40 | 1. Подключите к вашей модели любое из указанных выше поведений 41 | 42 | 2. Подключите в контроллер дополнительные actions 43 | 44 | ``` 45 | public function actions() 46 | { 47 | $modelClass = 'namespace\ModelName'; 48 | 49 | return [ 50 | 'moveNode' => [ 51 | 'class' => 'voskobovich\tree\manager\actions\MoveNodeAction', 52 | 'modelClass' => $modelClass, 53 | ], 54 | 'deleteNode' => [ 55 | 'class' => 'voskobovich\tree\manager\actions\DeleteNodeAction', 56 | 'modelClass' => $modelClass, 57 | ], 58 | 'updateNode' => [ 59 | 'class' => 'voskobovich\tree\manager\actions\UpdateNodeAction', 60 | 'modelClass' => $modelClass, 61 | ], 62 | 'createNode' => [ 63 | 'class' => 'voskobovich\tree\manager\actions\CreateNodeAction', 64 | 'modelClass' => $modelClass, 65 | ], 66 | ]; 67 | } 68 | ``` 69 | 70 | 3. Выведите виджет в удобном месте 71 | 72 | ``` 73 | use \voskobovich\tree\manager\widgets\nestable\Nestable; 74 | 75 | 'models\ModelName', 77 | ]) ?> 78 | ``` 79 | 80 | Пример того, как выглядит виджет: 81 | ------------- 82 | 83 | ![Screenshot](http://s019.radikal.ru/i644/1708/64/5f8e8e986d3c.png) 84 | -------------------------------------------------------------------------------- /patch.diff: -------------------------------------------------------------------------------- 1 | From b51a022602ec7d36f12ab8b160f9f476995e13e7 Mon Sep 17 00:00:00 2001 2 | From: =?UTF-8?q?=D0=95=D0=B2=D0=B3=D0=B5=D0=BD=D0=B8=D0=B9?= 3 | <89125230101@mail.ru> 4 | Date: Tue, 27 Dec 2016 09:52:02 +0500 5 | Subject: [PATCH] fix Labels in modal box 6 | 7 | --- 8 | src/widgets/nestable/Nestable.php | 11 +++++++---- 9 | 1 file changed, 7 insertions(+), 4 deletions(-) 10 | 11 | diff --git a/src/widgets/nestable/Nestable.php b/src/widgets/nestable/Nestable.php 12 | index 3df1c1f..24fa465 100644 13 | --- a/src/widgets/nestable/Nestable.php 14 | +++ b/src/widgets/nestable/Nestable.php 15 | @@ -119,7 +119,7 @@ class Nestable extends Widget 16 | } 17 | 18 | /** @var ActiveRecord|TreeInterface $model */ 19 | - $model = $this->modelClass; 20 | + $model = new $this->modelClass; 21 | 22 | /** @var ActiveRecord[]|TreeInterface[] $rootNodes */ 23 | $rootNodes = $model::find()->roots()->all(); 24 | @@ -348,6 +348,9 @@ class Nestable extends Widget 25 | { 26 | /** @var ActiveRecord $model */ 27 | $model = new $this->modelClass; 28 | + $newNodeString = Yii::t('vendor/voskobovich/yii2-tree-manager/widgets/nestable','New node'); 29 | + $closeButtonString = Yii::t('vendor/voskobovich/yii2-tree-manager/widgets/nestable','Close'); 30 | + $createNodeString = Yii::t('vendor/voskobovich/yii2-tree-manager/widgets/nestable','Create node'); 31 | 32 | echo << 34 | @@ -362,7 +365,7 @@ HTML; 35 | echo << 37 | 38 | - 39 | + 40 | 41 |