-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
66 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
<?php | ||
/** | ||
* @link http://www.digitaldeals.cz/ | ||
* @copyright Copyright (c) 2014 Digital Deals s.r.o. | ||
* @copyright Copyright (c) 2014 Digital Deals s.r.o. | ||
* @license http://www.digitaldeals.cz/license/ | ||
*/ | ||
|
||
|
@@ -24,7 +24,8 @@ | |
* | ||
* @author Jiri Svoboda <[email protected]> | ||
*/ | ||
class Behavior extends \yii\base\Behavior { | ||
class Behavior extends \yii\base\Behavior | ||
{ | ||
|
||
/** | ||
* @var string name of attr to be used as key | ||
|
@@ -46,6 +47,16 @@ class Behavior extends \yii\base\Behavior { | |
*/ | ||
public $index = 'sortItems'; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $_prevent = false; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $_force = false; | ||
|
||
/** | ||
* @return array events | ||
*/ | ||
|
@@ -57,6 +68,22 @@ public function events() | |
]; | ||
} | ||
|
||
/** | ||
* Prevent handling order | ||
*/ | ||
public function __srtblPrevent() | ||
{ | ||
$this->_prevent = true; | ||
} | ||
|
||
/** | ||
* Force handling order | ||
*/ | ||
public function __srtblForce() | ||
{ | ||
$this->_force = true; | ||
} | ||
|
||
/** | ||
* Before save | ||
*/ | ||
|
@@ -65,11 +92,16 @@ public function __srtblHandleBeforeInsert() | |
/** @var ActiveRecord $model */ | ||
$model = $this->owner; | ||
|
||
if (!$model->hasAttribute($this->column)) | ||
{ | ||
if (!$model->hasAttribute($this->column)) { | ||
throw new InvalidConfigException("Invalid sortable column `{$this->column}`."); | ||
} | ||
|
||
if ($this->_prevent && !$this->_force) { | ||
|
||
$model->{$this->column} = 0; | ||
return false; | ||
} | ||
|
||
$restrictions = []; | ||
|
||
$this->_pullRestrictions($this->owner, $restrictions); | ||
|
@@ -108,8 +140,7 @@ public function getSortColumn() | |
*/ | ||
public function getSortOrder($reversed = false) | ||
{ | ||
if ($reversed) | ||
{ | ||
if ($reversed) { | ||
$restrictions = []; | ||
|
||
$this->_pullRestrictions($this->owner, $restrictions); | ||
|
@@ -129,23 +160,20 @@ public function setSortOrder($sort) | |
|
||
$this->_parseKeys($itemKeys); | ||
|
||
if ($itemKeys && is_array($itemKeys)) | ||
{ | ||
if ($itemKeys && is_array($itemKeys)) { | ||
$transaction = \Yii::$app->db->beginTransaction(); | ||
|
||
$maxSortOrder = $this->getMaxSortOrder($itemKeys); | ||
|
||
if (!is_numeric($maxSortOrder) || $maxSortOrder == 0) | ||
{ | ||
if (!is_numeric($maxSortOrder) || $maxSortOrder == 0) { | ||
$this->resetSortOrder($itemKeys); | ||
} | ||
|
||
$affectedModels = []; | ||
$restrictions = []; | ||
|
||
// find all affected models and pull all restrictions | ||
for ($i = 0; $i < count($itemKeys); $i++) | ||
{ | ||
for ($i = 0; $i < count($itemKeys); $i++) { | ||
$affectedModels[$i] = $this->owner->findOne($itemKeys[$i]); | ||
|
||
$this->_pullRestrictions($affectedModels[$i], $restrictions); | ||
|
@@ -155,16 +183,12 @@ public function setSortOrder($sort) | |
$currentModels = $this->_getCurrentModels($itemKeys, $sort, $restrictions); | ||
|
||
// upadte & save all affected models | ||
for ($i = 0; $i < count($itemKeys); $i++) | ||
{ | ||
if (isset($affectedModels[$i])) | ||
{ | ||
if ($affectedModels[$i]->{$this->column} != $currentModels[$i]->{$this->column}) | ||
{ | ||
for ($i = 0; $i < count($itemKeys); $i++) { | ||
if (isset($affectedModels[$i])) { | ||
if ($affectedModels[$i]->{$this->column} != $currentModels[$i]->{$this->column}) { | ||
$affectedModels[$i]->{$this->column} = $currentModels[$i]->{$this->column}; | ||
|
||
if (!$affectedModels[$i]->save()) | ||
{ | ||
if (!$affectedModels[$i]->save()) { | ||
$transaction->rollback(); | ||
|
||
throw new \ErrorException('Cannot set model sort order.'); | ||
|
@@ -190,7 +214,7 @@ public function isFirst($sort = SORT_ASC) | |
|
||
$last = array_shift($sorted); | ||
|
||
return (null !== $last && $this->getOwnerKey() === (int) $last); | ||
return (null !== $last && $this->getOwnerKey() === (int)$last); | ||
} | ||
|
||
/** | ||
|
@@ -204,7 +228,7 @@ public function isLast($sort = SORT_ASC) | |
|
||
$last = array_pop($sorted); | ||
|
||
return (null !== $last && $this->getOwnerKey() === (int) $last); | ||
return (null !== $last && $this->getOwnerKey() === (int)$last); | ||
} | ||
|
||
/** | ||
|
@@ -218,8 +242,7 @@ public function prev($sort = SORT_ASC) | |
|
||
$current = array_search($this->getOwnerKey(), $sorted); | ||
|
||
if (false !== $current) | ||
{ | ||
if (false !== $current) { | ||
$next = ArrayHelper::getValue($sorted, --$current, null); | ||
|
||
return $next; | ||
|
@@ -239,8 +262,7 @@ public function next($sort = SORT_ASC) | |
|
||
$current = array_search($this->getOwnerKey(), $sorted); | ||
|
||
if (false !== $current) | ||
{ | ||
if (false !== $current) { | ||
$next = ArrayHelper::getValue($sorted, ++$current, null); | ||
|
||
return $next; | ||
|
@@ -255,8 +277,7 @@ public function next($sort = SORT_ASC) | |
*/ | ||
protected function getOwnerKey() | ||
{ | ||
if (isset($this->key)) | ||
{ | ||
if (isset($this->key)) { | ||
return $this->owner->{$this->key}; | ||
} | ||
|
||
|
@@ -269,35 +290,33 @@ protected function getOwnerKey() | |
*/ | ||
protected function getOwnerKeyAttr() | ||
{ | ||
if (isset($this->key)) | ||
{ | ||
if (isset($this->key)) { | ||
return $this->key; | ||
} | ||
|
||
if (is_array($this->owner->tableSchema->primaryKey)) | ||
{ | ||
if (is_array($this->owner->tableSchema->primaryKey)) { | ||
return ArrayHelper::getValue($this->owner->tableSchema->primaryKey, 0); | ||
} | ||
|
||
return $this->owner->tableSchema->primaryKey; | ||
} | ||
|
||
/** | ||
* Retrieves maximum sortOrder value for | ||
* Retrieves maximum sortOrder value for | ||
* @param $items items which will be included | ||
* @return int max sortOrder | ||
*/ | ||
protected function getMaxSortOrder($items = [], $restrictions = []) | ||
{ | ||
$query = $this->_getQuery($items, $restrictions); | ||
|
||
return (int) $query->max($this->column); | ||
return (int)$query->max($this->column); | ||
} | ||
|
||
/** | ||
* Resets sortOrder and sets it to model ID value | ||
* @param array $items defines which models should be included, if is empty it includes all models | ||
* @return int | ||
* @return int | ||
*/ | ||
protected function resetSortOrder($items = []) | ||
{ | ||
|
@@ -316,8 +335,7 @@ protected function resetSortOrder($items = []) | |
*/ | ||
private function _parseKeys(&$keys) | ||
{ | ||
foreach ($keys as $id => $key) | ||
{ | ||
foreach ($keys as $id => $key) { | ||
$keys[$id] = \yii\helpers\Json::decode($key); | ||
} | ||
} | ||
|
@@ -332,15 +350,12 @@ private function _getQuery($items = [], $restrictions = []) | |
{ | ||
$query = (new Query())->from($this->owner->tableName()); | ||
|
||
if (!empty($items)) | ||
{ | ||
if (!empty($items)) { | ||
$query->where(['in', $this->getOwnerKeyAttr(), $items]); | ||
} | ||
|
||
if (!empty($restrictions)) | ||
{ | ||
foreach ($restrictions as $column => $values) | ||
{ | ||
if (!empty($restrictions)) { | ||
foreach ($restrictions as $column => $values) { | ||
$query->andWhere(['in', $column, $values]); | ||
} | ||
} | ||
|
@@ -376,8 +391,7 @@ private function _getCurrentModels($items = [], $sort = SORT_DESC, $restrictions | |
->where([$this->getOwnerKeyAttr() => $items]) | ||
->orderBy([$this->column => $sort]); | ||
|
||
if ($restrictions) | ||
{ | ||
if ($restrictions) { | ||
$query->andWhere($restrictions); | ||
} | ||
|
||
|
@@ -391,10 +405,8 @@ private function _getCurrentModels($items = [], $sort = SORT_DESC, $restrictions | |
*/ | ||
private function _pullRestrictions(ActiveRecord $model, &$restrictions) | ||
{ | ||
foreach ($this->restrictions as $attr) | ||
{ | ||
if (isset($model->{$attr}) && (!isset($restrictions[$attr]) || !in_array($model->{$attr}, $restrictions[$attr]))) | ||
{ | ||
foreach ($this->restrictions as $attr) { | ||
if (isset($model->{$attr}) && (!isset($restrictions[$attr]) || !in_array($model->{$attr}, $restrictions[$attr]))) { | ||
$restrictions[$attr][] = $model->{$attr}; | ||
} | ||
} | ||
|
@@ -409,10 +421,8 @@ private function _pullRestrictions(ActiveRecord $model, &$restrictions) | |
*/ | ||
private function _assignRestrictions($restrictions, &$query) | ||
{ | ||
if (!empty($restrictions)) | ||
{ | ||
foreach ($restrictions as $column => $values) | ||
{ | ||
if (!empty($restrictions)) { | ||
foreach ($restrictions as $column => $values) { | ||
$query->andWhere([$column => $values]); | ||
} | ||
} | ||
|
@@ -434,10 +444,8 @@ private function _fixSortGaps($restrictions = []) | |
|
||
$sortOrder = 1; | ||
|
||
foreach ($models as $model) | ||
{ | ||
if ($model->{$this->column} != $sortOrder) | ||
{ | ||
foreach ($models as $model) { | ||
if ($model->{$this->column} != $sortOrder) { | ||
$model->{$this->column} = $sortOrder; | ||
|
||
$model->save(); | ||
|
@@ -449,4 +457,5 @@ private function _fixSortGaps($restrictions = []) | |
$transaction->commit(); | ||
} | ||
} | ||
|
||
?> |