diff --git a/behaviors/SortableDateController.php b/behaviors/SortableDateController.php new file mode 100644 index 0000000..0d681f5 --- /dev/null +++ b/behaviors/SortableDateController.php @@ -0,0 +1,53 @@ +model; + $success = ''; + if(($model = $modelClass::findOne($id))){ + if($direction === 'up'){ + $eq = '>'; + $orderDir = 'ASC'; + } else { + $eq = '<'; + $orderDir = 'DESC'; + } + + $query = $modelClass::find()->orderBy('time '.$orderDir)->limit(1); + + $where = [$eq, 'time', $model->time]; + if(count($condition)){ + $where = ['and', $where]; + foreach($condition as $key => $value){ + $where[] = [$key => $value]; + } + } + $modelSwap = $query->where($where)->one(); + + if(!empty($modelSwap)) + { + $newOrderNum = $modelSwap->time; + + $modelSwap->time = $model->time; + $modelSwap->update(); + + $model->time = $newOrderNum; + $model->update(); + + $success = ['swap_id' => $modelSwap->primaryKey]; + } + } + else{ + $this->owner->error = Yii::t('easyii', 'Not found'); + } + + return $this->owner->formatResponse($success); + } +} \ No newline at end of file diff --git a/components/ActiveQuery.php b/components/ActiveQuery.php index 369703c..5ac869d 100644 --- a/components/ActiveQuery.php +++ b/components/ActiveQuery.php @@ -28,4 +28,10 @@ public function sort() $this->orderBy('order_num DESC'); return $this; } + + public function sortDate() + { + $this->orderBy('time DESC'); + return $this; + } } \ No newline at end of file