Merge branch 'master' of github.com:thelia/thelia
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
namespace Thelia\Model;
|
||||
|
||||
use Thelia\Model\Base\AttributeAv as BaseAttributeAv;
|
||||
use Thelia\Core\Event\AttributeValueEvent;
|
||||
use Thelia\Core\Event\AttributeAvEvent;
|
||||
use Propel\Runtime\Connection\ConnectionInterface;
|
||||
use Thelia\Core\Event\TheliaEvents;
|
||||
use Propel\Runtime\ActiveQuery\Criteria;
|
||||
@@ -11,21 +11,14 @@ use Propel\Runtime\ActiveQuery\Criteria;
|
||||
class AttributeAv extends BaseAttributeAv {
|
||||
|
||||
use \Thelia\Model\Tools\ModelEventDispatcherTrait;
|
||||
|
||||
use \Thelia\Model\Tools\PositionManagementTrait;
|
||||
|
||||
/**
|
||||
* Get the position of the next inserted object
|
||||
* when dealing with position, be sure to work insite the current attribute.
|
||||
*/
|
||||
public function getNextPosition($parent = null) {
|
||||
|
||||
$last = $this->createQuery()
|
||||
->filterByAttributeId($this->getAttributeId())
|
||||
->orderByPosition(Criteria::DESC)
|
||||
->limit(1)
|
||||
->findOne()
|
||||
;
|
||||
|
||||
return $last != null ? $last->getPosition() + 1 : 1;
|
||||
protected function addCriteriaToPositionQuery($query) {
|
||||
$query->filterByAttributeId($this->getAttributeId());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -33,11 +26,11 @@ class AttributeAv extends BaseAttributeAv {
|
||||
*/
|
||||
public function preInsert(ConnectionInterface $con = null)
|
||||
{
|
||||
$this->dispatchEvent(TheliaEvents::BEFORE_CREATEATTRIBUTE_VALUE, new AttributeValueEvent($this));
|
||||
|
||||
// Set the current position for the new object
|
||||
$this->setPosition($this->getNextPosition());
|
||||
|
||||
$this->dispatchEvent(TheliaEvents::BEFORE_CREATEATTRIBUTE_AV, new AttributeAvEvent($this));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -46,7 +39,7 @@ class AttributeAv extends BaseAttributeAv {
|
||||
*/
|
||||
public function postInsert(ConnectionInterface $con = null)
|
||||
{
|
||||
$this->dispatchEvent(TheliaEvents::AFTER_CREATEATTRIBUTE_VALUE, new AttributeValueEvent($this));
|
||||
$this->dispatchEvent(TheliaEvents::AFTER_CREATEATTRIBUTE_AV, new AttributeAvEvent($this));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -54,7 +47,7 @@ class AttributeAv extends BaseAttributeAv {
|
||||
*/
|
||||
public function preUpdate(ConnectionInterface $con = null)
|
||||
{
|
||||
$this->dispatchEvent(TheliaEvents::BEFORE_UPDATEATTRIBUTE_VALUE, new AttributeValueEvent($this));
|
||||
$this->dispatchEvent(TheliaEvents::BEFORE_UPDATEATTRIBUTE_AV, new AttributeAvEvent($this));
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -64,7 +57,7 @@ class AttributeAv extends BaseAttributeAv {
|
||||
*/
|
||||
public function postUpdate(ConnectionInterface $con = null)
|
||||
{
|
||||
$this->dispatchEvent(TheliaEvents::AFTER_UPDATEATTRIBUTE_VALUE, new AttributeValueEvent($this));
|
||||
$this->dispatchEvent(TheliaEvents::AFTER_UPDATEATTRIBUTE_AV, new AttributeAvEvent($this));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -72,7 +65,7 @@ class AttributeAv extends BaseAttributeAv {
|
||||
*/
|
||||
public function preDelete(ConnectionInterface $con = null)
|
||||
{
|
||||
$this->dispatchEvent(TheliaEvents::BEFORE_DELETEATTRIBUTE_VALUE, new AttributeValueEvent($this));
|
||||
$this->dispatchEvent(TheliaEvents::BEFORE_DELETEATTRIBUTE_AV, new AttributeAvEvent($this));
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -82,6 +75,6 @@ class AttributeAv extends BaseAttributeAv {
|
||||
*/
|
||||
public function postDelete(ConnectionInterface $con = null)
|
||||
{
|
||||
$this->dispatchEvent(TheliaEvents::AFTER_DELETEATTRIBUTE_VALUE, new AttributeValueEvent($this));
|
||||
$this->dispatchEvent(TheliaEvents::AFTER_DELETEATTRIBUTE_AV, new AttributeAvEvent($this));
|
||||
}
|
||||
}
|
||||
@@ -45,29 +45,34 @@ trait PositionManagementTrait {
|
||||
return $class->getConstant('DATABASE_NAME');
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementors may add some search criteria (e.g., parent id) to the queries
|
||||
* used to change/get position by overloading this method.
|
||||
*/
|
||||
protected function addCriteriaToPositionQuery($query) {
|
||||
// Add required criteria here...
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the position of the next inserted object
|
||||
*/
|
||||
public function getNextPosition($parent = null) {
|
||||
public function getNextPosition() {
|
||||
|
||||
$query = $this->createQuery()
|
||||
->orderByPosition(Criteria::DESC)
|
||||
->limit(1);
|
||||
|
||||
if ($parent !== null) $query->filterByParent($parent);
|
||||
$this->addCriteriaToPositionQuery($query);
|
||||
|
||||
$last = $query->findOne()
|
||||
;
|
||||
$last = $query->findOne();
|
||||
|
||||
return $last != null ? $last->getPosition() + 1 : 1;
|
||||
return $last != null ? $last->getPosition() + 1 : 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Move up a object
|
||||
*/
|
||||
public function movePositionUp() {
|
||||
echo "move up !";
|
||||
$this->movePositionUpOrDown(true);
|
||||
}
|
||||
|
||||
@@ -91,7 +96,7 @@ trait PositionManagementTrait {
|
||||
// Find object to exchange position with
|
||||
$search = $this->createQuery();
|
||||
|
||||
if (method_exists($this, 'getParent')) $search->filterByParent($this->getParent());
|
||||
$this->addCriteriaToPositionQuery($search);
|
||||
|
||||
// Up or down ?
|
||||
if ($up === true) {
|
||||
@@ -152,7 +157,7 @@ trait PositionManagementTrait {
|
||||
// Find categories to offset
|
||||
$search = $this->createQuery();
|
||||
|
||||
if (method_exists($this, 'getParent')) $search->filterByParent($this->getParent());
|
||||
$this->addCriteriaToPositionQuery($search);
|
||||
|
||||
if ($newPosition > $current_position) {
|
||||
// The new position is after the current position -> we will offset + 1 all categories located between us and the new position
|
||||
|
||||
Reference in New Issue
Block a user