Skip to content

Commit

Permalink
sort order arrange fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jirisvoboda committed Jun 13, 2016
1 parent af79481 commit 41ca264
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
16 changes: 11 additions & 5 deletions GalleryBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ public function addImage($fileName, $attrs = [])
* @param array $order
* @return type
*/
public function arrange($order)
public function arrange($order, $sort = SORT_ASC)
{
$orders = [];
$i = 0;
Expand All @@ -431,13 +431,19 @@ public function arrange($order)
$orders[] = $order[$k];
$i++;
}
sort($orders);

if (SORT_ASC == $sort)
{
sort($orders);
}
else
{
rsort($orders);
}

$i = 0;
$res = [];
foreach ($order as $k => $v)
{
$res[$k] = $orders[$i];

\Yii::$app->db->createCommand()
->update($this->_galleryTable, ['rank' => $orders[$i]], ['id' => $k])
->execute();
Expand Down
4 changes: 2 additions & 2 deletions GalleryManagerAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class GalleryManagerAction extends Action
* @see GalleryManagerAction::run
*/
public $types = [];

public $sort = SORT_ASC;

protected $type;
protected $behaviorName;
Expand Down Expand Up @@ -129,7 +129,7 @@ public function actionOrder($order)
if (count($order) == 0) {
throw new HttpException(400, 'No data, to save');
}
$res = $this->behavior->arrange($order);
$res = $this->behavior->arrange($order, $this->sort);

return Json::encode($res);

Expand Down
5 changes: 3 additions & 2 deletions assets/jquery.galleryManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@
photos[id] = photo;
photo.data('id', id);
photo.data('rank', rank);

photo.attr('data-key', id);
photo.attr('data-rank', rank);

$('img', photo).attr('src', src);
if (opts.hasName) {
$('.caption h5', photo).text(name);
Expand All @@ -106,7 +108,6 @@
$('.caption p', photo).text(description);
}

console.log(prepend);
if (prepend) {
$images.prepend(photo);
}
Expand Down

0 comments on commit 41ca264

Please sign in to comment.