| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace devgroup\JsTreeWidget\actions\AdjacencyList; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use DevGroup\TagDependencyHelper\NamingHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Yii; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use yii\base\Action; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use yii\base\InvalidConfigException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use yii\caching\TagDependency; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use yii\db\ActiveRecord; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use yii\web\BadRequestHttpException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * Helper action to change sort_order attribute via JsTree Drag&Drop | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * Example usage in controller: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * ``` php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * public function actions() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  *     return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  *         'reorder' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  *             'class' => TreeNodesReorderAction::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  *             'class_name' => Category::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  *         ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  *         ... | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  *     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * ``` | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 28 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  | class TreeNodesReorderAction extends Action | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     public $className = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     public $modelSortOrderField = 'sort_order'; | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     public $sortOrder = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     public function init() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         if (!isset($this->className)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             throw new InvalidConfigException("Model name should be set in controller actions"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         if (!class_exists($this->className)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             throw new InvalidConfigException("Model class does not exists"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $this->sortOrder = Yii::$app->request->post('order'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         if (empty($this->sortOrder)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             throw new BadRequestHttpException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     public function run() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         /** @var ActiveRecord $class */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $class = $this->className; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $sortOrderField = Yii::$app->db->quoteColumnName($this->modelSortOrderField); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $case = 'CASE `id`'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $newSortOrders = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         foreach ($this->sortOrder as $id => $sort_order) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             if ($sort_order === '' || $sort_order === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             $case .= ' when "' . $id . '" then "' . $sort_order . '"'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             $newSortOrders[$id] = $sort_order; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $case .= ' END'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $sql = "UPDATE " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             . $class::tableName() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             . " SET " . $sortOrderField . " = " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             . $case | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             . " WHERE `id` IN(" . implode(', ', array_keys($newSortOrders)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             . ")"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         TagDependency::invalidate( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             Yii::$app->cache, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             NamingHelper::getCommonTag($class) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         return 0 !== Yii::$app->db->createCommand($sql)->execute(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 76 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |  |