Skip to content

Commit

Permalink
+ sortDate
Browse files Browse the repository at this point in the history
  • Loading branch information
noumo committed May 16, 2015
1 parent 655f901 commit 8aa207d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
53 changes: 53 additions & 0 deletions behaviors/SortableDateController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
namespace yii\easyii\behaviors;

use Yii;

class SortableDateController extends \yii\base\Behavior
{
public $model;

public function move($id, $direction, $condition = [])
{
$modelClass = $this->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);
}
}
6 changes: 6 additions & 0 deletions components/ActiveQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ public function sort()
$this->orderBy('order_num DESC');
return $this;
}

public function sortDate()
{
$this->orderBy('time DESC');
return $this;
}
}

0 comments on commit 8aa207d

Please sign in to comment.