From 8ad0dc7b210cd76bb00b1b80f36c297221062e96 Mon Sep 17 00:00:00 2001 From: franck Date: Thu, 12 Sep 2013 15:43:37 +0200 Subject: [PATCH] AttributesAv management --- core/lib/Thelia/Action/AttributeAv.php | 6 +- core/lib/Thelia/Config/Resources/config.xml | 3 +- .../Admin/AttributeAvController.php | 180 ++++++++++++++++++ ...onForm.php => AttributeAvCreationForm.php} | 4 +- .../Thelia/Form/AttributeModificationForm.php | 4 + core/lib/Thelia/Model/AttributeAv.php | 31 ++- .../Model/Tools/PositionManagementTrait.php | 21 +- templates/admin/default/attribute-edit.html | 23 ++- 8 files changed, 228 insertions(+), 44 deletions(-) create mode 100644 core/lib/Thelia/Controller/Admin/AttributeAvController.php rename core/lib/Thelia/Form/{AttributeValueCreationForm.php => AttributeAvCreationForm.php} (96%) diff --git a/core/lib/Thelia/Action/AttributeAv.php b/core/lib/Thelia/Action/AttributeAv.php index 53df7aa72..a6b442fa2 100644 --- a/core/lib/Thelia/Action/AttributeAv.php +++ b/core/lib/Thelia/Action/AttributeAv.php @@ -50,6 +50,7 @@ class AttributeAv extends BaseAction implements EventSubscriberInterface $attribute ->setDispatcher($this->getDispatcher()) + ->setAttributeId($event->getAttributeId()) ->setLocale($event->getLocale()) ->setTitle($event->getTitle()) @@ -57,11 +58,6 @@ class AttributeAv extends BaseAction implements EventSubscriberInterface ; $event->setAttributeAv($attribute); - - // Add atribute to all product templates if required - if ($event->getAddToAllTemplates() != 0) { - // TODO: add to all product template - } } /** diff --git a/core/lib/Thelia/Config/Resources/config.xml b/core/lib/Thelia/Config/Resources/config.xml index 31f013c88..ddb32cf65 100755 --- a/core/lib/Thelia/Config/Resources/config.xml +++ b/core/lib/Thelia/Config/Resources/config.xml @@ -70,7 +70,8 @@
- + + diff --git a/core/lib/Thelia/Controller/Admin/AttributeAvController.php b/core/lib/Thelia/Controller/Admin/AttributeAvController.php new file mode 100644 index 000000000..b7118b53a --- /dev/null +++ b/core/lib/Thelia/Controller/Admin/AttributeAvController.php @@ -0,0 +1,180 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Controller\Admin; + +use Thelia\Core\Event\AttributeAvDeleteEvent; +use Thelia\Core\Event\TheliaEvents; +use Thelia\Core\Event\AttributeAvUpdateEvent; +use Thelia\Core\Event\AttributeAvCreateEvent; +use Thelia\Model\AttributeAvQuery; +use Thelia\Form\AttributeAvModificationForm; +use Thelia\Form\AttributeAvCreationForm; +use Thelia\Core\Event\UpdatePositionEvent; + +/** + * Manages attributes-av sent by mail + * + * @author Franck Allimant + */ +class AttributeAvController extends AbstractCrudController +{ + public function __construct() { + parent::__construct( + 'attribute', + 'manual', + + 'admin.configuration.attributes-av.view', + 'admin.configuration.attributes-av.create', + 'admin.configuration.attributes-av.update', + 'admin.configuration.attributes-av.delete', + + TheliaEvents::ATTRIBUTE_AV_CREATE, + TheliaEvents::ATTRIBUTE_AV_UPDATE, + TheliaEvents::ATTRIBUTE_AV_DELETE, + null, // No visibility toggle + TheliaEvents::ATTRIBUTE_AV_UPDATE_POSITION + ); + } + + protected function getCreationForm() { + return new AttributeAvCreationForm($this->getRequest()); + } + + protected function getUpdateForm() { + return new AttributeAvModificationForm($this->getRequest()); + } + + protected function getCreationEvent($formData) { + $createEvent = new AttributeAvCreateEvent(); + + $createEvent + ->setAttributeId($formData['attribute_id']) + ->setTitle($formData['title']) + ->setLocale($formData["locale"]) + ; + + return $createEvent; + } + + protected function getUpdateEvent($formData) { + + $changeEvent = new AttributeAvUpdateEvent($formData['id']); + + // Create and dispatch the change event + $changeEvent + ->setLocale($formData["locale"]) + ->setTitle($formData['title']) + ->setChapo($formData['chapo']) + ->setDescription($formData['description']) + ->setPostscriptum($formData['postscriptum']) + ; + + return $changeEvent; + } + + protected function createUpdatePositionEvent($positionChangeMode, $positionValue) { + + return new UpdatePositionEvent( + $this->getRequest()->get('attributeav_id', null), + $positionChangeMode, + $positionValue + ); + } + + protected function getDeleteEvent() { + return new AttributeAvDeleteEvent($this->getRequest()->get('attributeav_id')); + } + + protected function eventContainsObject($event) { + return $event->hasAttributeAv(); + } + + protected function hydrateObjectForm($object) { + + $data = array( + 'id' => $object->getId(), + 'locale' => $object->getLocale(), + 'title' => $object->getTitle(), + 'chapo' => $object->getChapo(), + 'description' => $object->getDescription(), + 'postscriptum' => $object->getPostscriptum() + ); + + // Setup the object form + return new AttributeAvModificationForm($this->getRequest(), "form", $data); + } + + protected function getObjectFromEvent($event) { + return $event->hasAttributeAv() ? $event->getAttributeAv() : null; + } + + protected function getExistingObject() { + return AttributeAvQuery::create() + ->joinWithI18n($this->getCurrentEditionLocale()) + ->findOneById($this->getRequest()->get('attributeav_id')); + } + + protected function getObjectLabel($object) { + return $object->getTitle(); + } + + protected function getObjectId($object) { + return $object->getId(); + } + + protected function getViewArguments() { + return array( + 'attribute_id' => $this->getRequest()->get('attribute_id'), + 'order' => $this->getCurrentListOrder() + ); + } + + protected function renderListTemplate($currentOrder) { + // We always return to the attribute edition form + return $this->render( + 'attribute-edit', + $this->getViewArguments() + ); + } + + protected function renderEditionTemplate() { + // We always return to the attribute edition form + return $this->render('attribute-edit', $this->getViewArguments()); + } + + protected function redirectToEditionTemplate() { + // We always return to the attribute edition form + $this->redirectToRoute( + "admin.configuration.attributes.update", + $this->getViewArguments() + ); + } + + protected function redirectToListTemplate() { + $this->redirectToRoute( + "admin.configuration.attributes.update", + $this->getViewArguments() + ); + } +} \ No newline at end of file diff --git a/core/lib/Thelia/Form/AttributeValueCreationForm.php b/core/lib/Thelia/Form/AttributeAvCreationForm.php similarity index 96% rename from core/lib/Thelia/Form/AttributeValueCreationForm.php rename to core/lib/Thelia/Form/AttributeAvCreationForm.php index dc9de681d..2ad202e1d 100644 --- a/core/lib/Thelia/Form/AttributeValueCreationForm.php +++ b/core/lib/Thelia/Form/AttributeAvCreationForm.php @@ -28,7 +28,7 @@ use Symfony\Component\Validator\ExecutionContextInterface; use Symfony\Component\Validator\Constraints\NotBlank; use Thelia\Core\Translation\Translator; -class AttributeValueCreationForm extends BaseForm +class AttributeAvCreationForm extends BaseForm { protected function buildForm() { @@ -57,6 +57,6 @@ class AttributeValueCreationForm extends BaseForm public function getName() { - return "thelia_attribute_value_creation"; + return "thelia_attributeav_creation"; } } \ No newline at end of file diff --git a/core/lib/Thelia/Form/AttributeModificationForm.php b/core/lib/Thelia/Form/AttributeModificationForm.php index 922ec489b..62b0b707a 100644 --- a/core/lib/Thelia/Form/AttributeModificationForm.php +++ b/core/lib/Thelia/Form/AttributeModificationForm.php @@ -43,6 +43,10 @@ class AttributeModificationForm extends AttributeCreationForm ) ) )) + ->add('attribute_values', 'collection', array( + 'type' => 'text', + 'options' => array('required' => false) + )) ; // Add standard description fields diff --git a/core/lib/Thelia/Model/AttributeAv.php b/core/lib/Thelia/Model/AttributeAv.php index 2b70881d7..d45b16192 100755 --- a/core/lib/Thelia/Model/AttributeAv.php +++ b/core/lib/Thelia/Model/AttributeAv.php @@ -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)); } } \ No newline at end of file diff --git a/core/lib/Thelia/Model/Tools/PositionManagementTrait.php b/core/lib/Thelia/Model/Tools/PositionManagementTrait.php index d5cc4ea63..70da830ac 100644 --- a/core/lib/Thelia/Model/Tools/PositionManagementTrait.php +++ b/core/lib/Thelia/Model/Tools/PositionManagementTrait.php @@ -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 diff --git a/templates/admin/default/attribute-edit.html b/templates/admin/default/attribute-edit.html index 58933cc9a..1b290f222 100644 --- a/templates/admin/default/attribute-edit.html +++ b/templates/admin/default/attribute-edit.html @@ -61,7 +61,7 @@ {intl l='Attribute values'} - {loop type="auth" name="can_create" roles="ADMIN" permissions="admin.configuration.attribute-values.create"} + {loop type="auth" name="can_create" roles="ADMIN" permissions="admin.configuration.attribute-av.create"} @@ -125,8 +125,8 @@ {admin_position_block permission="admin.attributes.edit" - path="/admin/configuration/attributes/update-value-position" - url_parameter="attribute_id" + path={url path='/admin/configuration/attributes-av/update-position' attribute_id=$attribute_id} + url_parameter="attributeav_id" in_place_edit_class="positionChange" position="$POSITION" id="$ID" @@ -137,7 +137,11 @@
- + {loop type="auth" name="can_create" roles="ADMIN" permissions="admin.configuration.attribute-av.delete"} + + + + {/loop}
@@ -181,7 +185,7 @@ {* Adding a new attribute *} -{form name="thelia.admin.attribute-value.creation"} +{form name="thelia.admin.attributeav.creation"} {* Capture the dialog body, to pass it to the generic dialog *} @@ -232,7 +236,7 @@ dialog_ok_label = {intl l="Create this value"} - form_action = {url path='/admin/configuration/attributes-av'} + form_action = {url path='/admin/configuration/attributes-av/create'} form_enctype = {form_enctype form=$form} form_error_message = $form_error_message } @@ -241,7 +245,8 @@ {* Delete value confirmation dialog *} {capture "delete_dialog"} - + + {/capture} {include @@ -275,7 +280,7 @@ {include file = "includes/generic-js-dialog.html" dialog_id = "creation_dialog" - form_name = "thelia.admin.attribute-value.creation" + form_name = "thelia.admin.attributeav.creation" } {* Inline editing of object position using bootstrap-editable *} @@ -288,7 +293,7 @@ placement : 'left', success : function(response, newValue) { // The URL template - var url = "{url path='/admin/configuration/attributes/update-value-position' attribute_value_id='__ID__' position='__POS__'}"; + var url = "{url path='/admin/configuration/attributes-av/update-position' attributeav_id='__ID__' position='__POS__' attribute_id=$attribute_id}"; // Perform subtitutions url = url.replace('__ID__', $(this).data('id')).replace('__POS__', newValue);