Skip to content

Commit

Permalink
+ prevent, force posibility
Browse files Browse the repository at this point in the history
  • Loading branch information
svobik7 committed Mar 7, 2017
1 parent 46b402c commit 3ac3e33
Showing 1 changed file with 66 additions and 57 deletions.
123 changes: 66 additions & 57 deletions components/Behavior.php
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/
*/

Expand All @@ -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
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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);
Expand Down Expand Up @@ -108,8 +140,7 @@ public function getSortColumn()
*/
public function getSortOrder($reversed = false)
{
if ($reversed)
{
if ($reversed) {
$restrictions = [];

$this->_pullRestrictions($this->owner, $restrictions);
Expand All @@ -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);
Expand All @@ -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.');
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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};
}

Expand All @@ -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 = [])
{
Expand All @@ -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);
}
}
Expand All @@ -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]);
}
}
Expand Down Expand Up @@ -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);
}

Expand All @@ -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};
}
}
Expand All @@ -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]);
}
}
Expand All @@ -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();
Expand All @@ -449,4 +457,5 @@ private function _fixSortGaps($restrictions = [])
$transaction->commit();
}
}

?>

0 comments on commit 3ac3e33

Please sign in to comment.