| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace betadog\yii1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Класс для автоматического отслеживания и порядка сортировки элементов | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * TIP: отслежтваемую колонку не надо добавлять в rules() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * Usage: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | <pre> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | public function behaviors() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         'orderingAttribute' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |             'class'      => 'betadog\\yii1\\OrderingValueBehavior', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |             // колонка по которой осуществляется сортировка | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |             'columnName' => 'ordering', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |             // на какое место ставится новый элемент | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |             'position'   => \betadog\yii1\OrderingValueBehavior::POSITION_END | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | </pre> | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 26 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  | class OrderingValueBehavior extends \CActiveRecordBehavior | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     const POSITION_START = 0; | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     const POSITION_END   = 1; | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     /** @var string */ | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     public $columnName; | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     /** @var int */ | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     public $position = self::POSITION_START; | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     public function attach($owner) | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         $validator = \CValidator::createValidator('numerical', $owner, $this->columnName, [ | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |             'integerOnly' => true, | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |             'allowEmpty'  => false, | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         $owner->validatorList->add($validator); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         parent::attach($owner); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     public function beforeValidate($event) | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         /** @var \CActiveRecord $owner */ | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         $owner = $this->getOwner(); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         if ($owner->getIsNewRecord()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |             $fieldName = \Yii::app()->db->quoteColumnName($this->columnName); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |             $position  = \Yii::app()->db->createCommand() | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |                 ->select("MAX($fieldName)") | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |                 ->from($owner->tableName()) | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |                 ->queryScalar(); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |             $owner->{$this->columnName} = $position + 1; | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     public function afterSave($event) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         /** @var \CActiveRecord $owner */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $owner = $this->getOwner(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |          * self::POSITION_END set in validator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         if ($owner->getIsNewRecord() && self::POSITION_START == $this->position) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $fieldName   = \Yii::app()->db->quoteColumnName($this->columnName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             $minPosition = \Yii::app()->db->createCommand() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 ->select("MIN($fieldName)") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 ->from($owner->tableName()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 ->queryScalar(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             \Yii::app()->db->createCommand()->update($owner->tableName(), [$this->columnName => new \CDbExpression("{$fieldName} + 1")]); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 80 |  |  |             \Yii::app()->db->createCommand()->update($owner->tableName(), [$this->columnName => $minPosition], 'id = :id', [':id' => $owner->id]); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 83 |  |  | } |