Skip to content

Instantly share code, notes, and snippets.

@ineersa
Last active August 29, 2015 14:02
Show Gist options
  • Save ineersa/85e245242ffd180e110e to your computer and use it in GitHub Desktop.
Save ineersa/85e245242ffd180e110e to your computer and use it in GitHub Desktop.
<?php
public static function setSort($book)
{
$model=BooksSort::model()->findByPk($book->id_book);
if ($model===NULL){
$model=new BooksSort;
$max=Yii::app()->db->createCommand("SELECT MAX(sort) FROM {$model->tableName()} WHERE app_id={$book->app_id};")->queryScalar();
$model->sort=$book->sort;
$model->app_id=$book->app_id;
if ($model->validate()){
if ($model->sort<(int)$max){
Yii::app()->db->createCommand(
"UPDATE {$model->tableName()} SET sort=sort+1
WHERE sort>=:sort AND app_id=:app"
)->bindParam(":sort",$model->sort,PDO::PARAM_INT)
->bindParam(":app",$book->app_id,PDO::PARAM_INT)
->execute();
$model->id_book=$book->id_book;
$model->timestamp=date("Y-m-d H:i:s");
$model->save(false);
} else {
$model->sort=$max+1;
$model->id_book=$book->id_book;
$model->app_id=$book->app_id;
$model->timestamp=date("Y-m-d H:i:s");
$model->save(false);
}
} else {
$model->sort=20;
if ($model->sort<(int)$max){
Yii::app()->db->createCommand(
"UPDATE {$model->tableName()} SET sort=sort+1
WHERE sort>=:sort AND app_id=:app"
)->bindParam(":sort",$model->sort,PDO::PARAM_INT)
->bindParam(":app",$book->app_id,PDO::PARAM_INT)
->execute();
$model->id_book=$book->id_book;
$model->timestamp=date("Y-m-d H:i:s");
$model->save(false);
} else {
$model->sort=$max+1;
$model->id_book=$book->id_book;
$model->timestamp=date("Y-m-d H:i:s");
$model->app_id=$book->app_id;
$model->save(false);
}
}
} else {
$max=Yii::app()->db->createCommand("SELECT MAX(sort) FROM {$model->tableName()} WHERE app_id={$book->app_id};")->queryScalar();
$old=$model->sort;
$new=$book->sort;
$model->sort=$new;
if ($model->validate()){
if ($new>$max) $new=$max;
} else {
if ($max>=20) $new=20;
else $new=$max;
}
if ($old>$new){
Yii::app()->db->createCommand("UPDATE {$model->tableName()} SET sort=sort+1
WHERE sort>=:new AND sort<:old AND app_id=:app;")
->bindParam(":new",$new,PDO::PARAM_INT)
->bindParam(":app",$book->app_id,PDO::PARAM_INT)
->bindParam(":old",$old,PDO::PARAM_INT)
->execute();
Yii::app()->db->createCommand("UPDATE {$model->tableName()} SET sort=:new WHERE id_book=:id;")
->bindParam(":new",$new,PDO::PARAM_INT)
->bindParam(":id",$book->id_book,PDO::PARAM_INT)
->execute();
} elseif ($old<$new){
Yii::app()->db->createCommand("UPDATE {$model->tableName()} SET sort=sort-1
WHERE sort<=:new AND sort>:old AND app_id=:app;")
->bindParam(":new",$new,PDO::PARAM_INT)
->bindParam(":app",$book->app_id,PDO::PARAM_INT)
->bindParam(":old",$old,PDO::PARAM_INT)
->execute();
Yii::app()->db->createCommand("UPDATE {$model->tableName()} SET sort=:new WHERE id_book=:id;")
->bindParam(":new",$new,PDO::PARAM_INT)
->bindParam(":id",$book->id_book,PDO::PARAM_INT)
->execute();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment