/**
  * @inheritdoc
  */
 public function moveToPosition($position)
 {
     if (parent::moveToPosition($position)) {
         return;
     }
     $currentPosition = $this->getSortablePosition();
     if ($position < $currentPosition) {
         // Moving forward
         $oldSortFrom = $position;
         $oldSortTo = $currentPosition - 1;
         $addedValue = 1;
     } else {
         // Moving back
         $oldSortFrom = $currentPosition + 1;
         $oldSortTo = $position;
         $addedValue = -1;
     }
     $models = $this->query->andWhere(['>=', $this->sortAttribute, $oldSortFrom])->andWhere(['<=', $this->sortAttribute, $oldSortTo])->andWhere(['<>', $this->sortAttribute, $currentPosition])->all();
     foreach ($models as $model) {
         $sort = $model->getSort() + $addedValue;
         $model->updateAttributes([$this->sortAttribute => $sort]);
     }
     $this->model->updateAttributes([$this->sortAttribute => $position]);
 }
Exemplo n.º 2
0
 protected function isVisible()
 {
     if (!$this->_model->isSortableByCurrentUser()) {
         return false;
     }
     if ($this->grid->filterModel) {
         $scopeAttributes = array_keys($this->_model->getSortableScopeCondition());
         $sortableAttributes = array_keys($this->_model->sortableCondition);
         $formData = Yii::$app->request->get($this->grid->filterModel->formName(), []);
         foreach ($scopeAttributes as $attribute) {
             if (!ArrayHelper::getValue($formData, $attribute) && !Yii::$app->request->get($attribute)) {
                 return false;
             }
         }
         foreach ($sortableAttributes as $attribute => $value) {
             $formValue = ArrayHelper::getValue($formData, $attribute);
             if ($formValue && $formValue != $value) {
                 return false;
             }
         }
     }
     $sort = $this->grid->dataProvider->getSort();
     return $sort->orders == [$this->_model->sortAttribute => SORT_ASC];
 }
 /**
  * @return \yii\db\ActiveQuery
  */
 protected function getQuery()
 {
     return parent::getQuery()->select(null);
 }