From 696a2be8ba2a99c36a83f621dc5f72c12edf8185 Mon Sep 17 00:00:00 2001 From: franck Date: Mon, 16 Sep 2013 12:52:35 +0200 Subject: [PATCH] Completed template management --- core/lib/Thelia/Action/Template.php | 63 +++++++++++ .../Thelia/Config/Resources/routing/admin.xml | 24 ++++ .../Controller/Admin/TemplateController.php | 106 ++++++++++++++++++ .../Core/Event/TemplateAddAttributeEvent.php | 51 +++++++++ .../Core/Event/TemplateAddFeatureEvent.php | 51 +++++++++ .../Event/TemplateDeleteAttributeEvent.php | 51 +++++++++ .../Core/Event/TemplateDeleteFeatureEvent.php | 51 +++++++++ core/lib/Thelia/Core/Event/TheliaEvents.php | 6 + .../Thelia/Core/Template/Loop/Attribute.php | 15 ++- .../lib/Thelia/Core/Template/Loop/Feature.php | 37 ++++-- .../default/ajax/template-attribute-list.html | 101 +++++++++++++++-- .../default/ajax/template-feature-list.html | 102 +++++++++++++++++ templates/admin/default/template-edit.html | 4 +- 13 files changed, 639 insertions(+), 23 deletions(-) create mode 100644 core/lib/Thelia/Core/Event/TemplateAddAttributeEvent.php create mode 100644 core/lib/Thelia/Core/Event/TemplateAddFeatureEvent.php create mode 100644 core/lib/Thelia/Core/Event/TemplateDeleteAttributeEvent.php create mode 100644 core/lib/Thelia/Core/Event/TemplateDeleteFeatureEvent.php create mode 100644 templates/admin/default/ajax/template-feature-list.html diff --git a/core/lib/Thelia/Action/Template.php b/core/lib/Thelia/Action/Template.php index a06e10430..18174dd26 100644 --- a/core/lib/Thelia/Action/Template.php +++ b/core/lib/Thelia/Action/Template.php @@ -42,6 +42,14 @@ use Thelia\Core\Event\TemplateEvent; use Thelia\Model\TemplateTemplate; use Thelia\Model\TemplateTemplateQuery; use Thelia\Model\ProductQuery; +use Thelia\Core\Event\TemplateAddAttributeEvent; +use Thelia\Core\Event\TemplateDeleteAttributeEvent; +use Thelia\Model\AttributeTemplateQuery; +use Thelia\Model\AttributeTemplate; +use Thelia\Core\Event\TemplateDeleteFeatureEvent; +use Thelia\Core\Event\TemplateAddFeatureEvent; +use Thelia\Model\FeatureTemplateQuery; +use Thelia\Model\FeatureTemplate; class Template extends BaseAction implements EventSubscriberInterface { @@ -113,6 +121,54 @@ class Template extends BaseAction implements EventSubscriberInterface } } + public function addAttribute(TemplateAddAttributeEvent $event) { + + if (null === AttributeTemplateQuery::create()->filterByAttributeId($event->getAttributeId())->filterByTemplate($event->getTemplate())->findOne()) { + + $attribute_template = new AttributeTemplate(); + + $attribute_template + ->setAttributeId($event->getAttributeId()) + ->setTemplate($event->getTemplate()) + ->save() + ; + } + } + + public function deleteAttribute(TemplateDeleteAttributeEvent $event) { + + $attribute_template = AttributeTemplateQuery::create() + ->filterByAttributeId($event->getAttributeId()) + ->filterByTemplate($event->getTemplate())->findOne() + ; + + if ($attribute_template !== null) $attribute_template->delete(); + } + + public function addFeature(TemplateAddFeatureEvent $event) { + + if (null === FeatureTemplateQuery::create()->filterByFeatureId($event->getFeatureId())->filterByTemplate($event->getTemplate())->findOne()) { + + $feature_template = new FeatureTemplate(); + + $feature_template + ->setFeatureId($event->getFeatureId()) + ->setTemplate($event->getTemplate()) + ->save() + ; + } + } + + public function deleteFeature(TemplateDeleteFeatureEvent $event) { + + $feature_template = FeatureTemplateQuery::create() + ->filterByFeatureId($event->getFeatureId()) + ->filterByTemplate($event->getTemplate())->findOne() + ; + + if ($feature_template !== null) $feature_template->delete(); + } + /** * {@inheritDoc} */ @@ -122,6 +178,13 @@ class Template extends BaseAction implements EventSubscriberInterface TheliaEvents::TEMPLATE_CREATE => array("create", 128), TheliaEvents::TEMPLATE_UPDATE => array("update", 128), TheliaEvents::TEMPLATE_DELETE => array("delete", 128), + + TheliaEvents::TEMPLATE_ADD_ATTRIBUTE => array("addAttribute", 128), + TheliaEvents::TEMPLATE_DELETE_ATTRIBUTE => array("deleteAttribute", 128), + + TheliaEvents::TEMPLATE_ADD_FEATURE => array("addFeature", 128), + TheliaEvents::TEMPLATE_DELETE_FEATURE => array("deleteFeature", 128), + ); } } \ No newline at end of file diff --git a/core/lib/Thelia/Config/Resources/routing/admin.xml b/core/lib/Thelia/Config/Resources/routing/admin.xml index 14ab8d4de..059ded5d6 100755 --- a/core/lib/Thelia/Config/Resources/routing/admin.xml +++ b/core/lib/Thelia/Config/Resources/routing/admin.xml @@ -234,6 +234,30 @@ Thelia\Controller\Admin\TemplateController::deleteAction + + Thelia\Controller\Admin\TemplateController::getAjaxFeaturesAction + + + + Thelia\Controller\Admin\TemplateController::addFeatureAction + + + + Thelia\Controller\Admin\TemplateController::deleteFeatureAction + + + + Thelia\Controller\Admin\TemplateController::getAjaxAttributesAction + + + + Thelia\Controller\Admin\TemplateController::addAttributeAction + + + + Thelia\Controller\Admin\TemplateController::deleteAttributeAction + + diff --git a/core/lib/Thelia/Controller/Admin/TemplateController.php b/core/lib/Thelia/Controller/Admin/TemplateController.php index aafc56e4b..3685a359a 100644 --- a/core/lib/Thelia/Controller/Admin/TemplateController.php +++ b/core/lib/Thelia/Controller/Admin/TemplateController.php @@ -35,6 +35,10 @@ use Thelia\Model\TemplateAv; use Thelia\Model\TemplateAvQuery; use Thelia\Core\Event\TemplateAvUpdateEvent; use Thelia\Core\Event\TemplateEvent; +use Thelia\Core\Event\TemplateDeleteAttributeEvent; +use Thelia\Core\Event\TemplateAddAttributeEvent; +use Thelia\Core\Event\TemplateAddFeatureEvent; +use Thelia\Core\Event\TemplateDeleteFeatureEvent; /** * Manages templates sent by mail @@ -193,4 +197,106 @@ class TemplateController extends AbstractCrudController return null; } + public function getAjaxFeaturesAction() { + return $this->render( + 'ajax/template-feature-list', + array('template_id' => $this->getRequest()->get('template_id')) + ); + } + + public function getAjaxAttributesAction() { + return $this->render( + 'ajax/template-attribute-list', + array('template_id' => $this->getRequest()->get('template_id')) + ); + } + + public function addAttributeAction() { + + // Check current user authorization + if (null !== $response = $this->checkAuth("admin.configuration.template.attribute.add")) return $response; + + $attribute_id = intval($this->getRequest()->get('attribute_id')); + + if ($attribute_id > 0) { + $event = new TemplateAddAttributeEvent( + $this->getExistingObject(), + $attribute_id + ); + + try { + $this->dispatch(TheliaEvents::TEMPLATE_ADD_ATTRIBUTE, $event); + } catch (\Exception $ex) { + // Any error + return $this->errorPage($ex); + } + } + + $this->redirectToEditionTemplate(); + } + + public function deleteAttributeAction() { + + // Check current user authorization + if (null !== $response = $this->checkAuth("admin.configuration.template.attribute.delete")) return $response; + + $event = new TemplateDeleteAttributeEvent( + $this->getExistingObject(), + intval($this->getRequest()->get('attribute_id')) + ); + + try { + $this->dispatch(TheliaEvents::TEMPLATE_DELETE_ATTRIBUTE, $event); + } catch (\Exception $ex) { + // Any error + return $this->errorPage($ex); + } + + $this->redirectToEditionTemplate(); + } + + public function addFeatureAction() { + + // Check current user authorization + if (null !== $response = $this->checkAuth("admin.configuration.template.feature.add")) return $response; + + $feature_id = intval($this->getRequest()->get('feature_id')); + + if ($feature_id > 0) { + $event = new TemplateAddFeatureEvent( + $this->getExistingObject(), + $feature_id + ); + + try { + $this->dispatch(TheliaEvents::TEMPLATE_ADD_FEATURE, $event); + } catch (\Exception $ex) { + // Any error + return $this->errorPage($ex); + } + } + + $this->redirectToEditionTemplate(); + } + + public function deleteFeatureAction() { + + // Check current user authorization + if (null !== $response = $this->checkAuth("admin.configuration.template.feature.delete")) return $response; + + $event = new TemplateDeleteFeatureEvent( + $this->getExistingObject(), + intval($this->getRequest()->get('feature_id')) + ); + + try { + $this->dispatch(TheliaEvents::TEMPLATE_DELETE_FEATURE, $event); + } catch (\Exception $ex) { + // Any error + return $this->errorPage($ex); + } + + $this->redirectToEditionTemplate(); + } + } \ No newline at end of file diff --git a/core/lib/Thelia/Core/Event/TemplateAddAttributeEvent.php b/core/lib/Thelia/Core/Event/TemplateAddAttributeEvent.php new file mode 100644 index 000000000..6adebf080 --- /dev/null +++ b/core/lib/Thelia/Core/Event/TemplateAddAttributeEvent.php @@ -0,0 +1,51 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Core\Event; + +use Thelia\Model\Template; +class TemplateAddAttributeEvent extends TemplateEvent +{ + protected $attribute_id; + + public function __construct(Template $template, $attribute_id) + { + + parent::__construct($template); + + $this->attribute_id = $attribute_id; + } + + public function getAttributeId() + { + return $this->attribute_id; + } + + public function setAttributeId($attribute_id) + { + $this->attribute_id = $attribute_id; + + return $this; + } + +} diff --git a/core/lib/Thelia/Core/Event/TemplateAddFeatureEvent.php b/core/lib/Thelia/Core/Event/TemplateAddFeatureEvent.php new file mode 100644 index 000000000..ccfecb95a --- /dev/null +++ b/core/lib/Thelia/Core/Event/TemplateAddFeatureEvent.php @@ -0,0 +1,51 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Core\Event; + +use Thelia\Model\Template; +class TemplateAddFeatureEvent extends TemplateEvent +{ + protected $feature_id; + + public function __construct(Template $template, $feature_id) + { + + parent::__construct($template); + + $this->feature_id = $feature_id; + } + + public function getFeatureId() + { + return $this->feature_id; + } + + public function setFeatureId($feature_id) + { + $this->feature_id = $feature_id; + + return $this; + } + +} diff --git a/core/lib/Thelia/Core/Event/TemplateDeleteAttributeEvent.php b/core/lib/Thelia/Core/Event/TemplateDeleteAttributeEvent.php new file mode 100644 index 000000000..3df83422d --- /dev/null +++ b/core/lib/Thelia/Core/Event/TemplateDeleteAttributeEvent.php @@ -0,0 +1,51 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Core\Event; + +use Thelia\Model\Template; + +class TemplateDeleteAttributeEvent extends TemplateEvent +{ + protected $attribute_id; + + public function __construct(Template $template, $attribute_id) + { + + parent::__construct($template); + + $this->attribute_id = $attribute_id; + } + + public function getAttributeId() + { + return $this->attribute_id; + } + + public function setAttributeId($attribute_id) + { + $this->attribute_id = $attribute_id; + + return $this; + } +} diff --git a/core/lib/Thelia/Core/Event/TemplateDeleteFeatureEvent.php b/core/lib/Thelia/Core/Event/TemplateDeleteFeatureEvent.php new file mode 100644 index 000000000..83f73d923 --- /dev/null +++ b/core/lib/Thelia/Core/Event/TemplateDeleteFeatureEvent.php @@ -0,0 +1,51 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Core\Event; + +use Thelia\Model\Template; + +class TemplateDeleteFeatureEvent extends TemplateEvent +{ + protected $feature_id; + + public function __construct(Template $template, $feature_id) + { + + parent::__construct($template); + + $this->feature_id = $feature_id; + } + + public function getFeatureId() + { + return $this->feature_id; + } + + public function setFeatureId($feature_id) + { + $this->feature_id = $feature_id; + + return $this; + } +} diff --git a/core/lib/Thelia/Core/Event/TheliaEvents.php b/core/lib/Thelia/Core/Event/TheliaEvents.php index 6708449b5..220b4bf0d 100755 --- a/core/lib/Thelia/Core/Event/TheliaEvents.php +++ b/core/lib/Thelia/Core/Event/TheliaEvents.php @@ -343,6 +343,12 @@ final class TheliaEvents const TEMPLATE_UPDATE = "action.updateTemplate"; const TEMPLATE_DELETE = "action.deleteTemplate"; + const TEMPLATE_ADD_ATTRIBUTE = "action.templateAddAttribute"; + const TEMPLATE_DELETE_ATTRIBUTE = "action.templateDeleteAttribute"; + + const TEMPLATE_ADD_FEATURE = "action.templateAddFeature"; + const TEMPLATE_DELETE_FEATURE = "action.templateDeleteFeature"; + const BEFORE_CREATETEMPLATE = "action.before_createTemplate"; const AFTER_CREATETEMPLATE = "action.after_createTemplate"; diff --git a/core/lib/Thelia/Core/Template/Loop/Attribute.php b/core/lib/Thelia/Core/Template/Loop/Attribute.php index db9eb9b8f..e2ea7cf0f 100755 --- a/core/lib/Thelia/Core/Template/Loop/Attribute.php +++ b/core/lib/Thelia/Core/Template/Loop/Attribute.php @@ -64,6 +64,7 @@ class Attribute extends BaseI18nLoop Argument::createIntListTypeArgument('id'), Argument::createIntListTypeArgument('product'), Argument::createIntListTypeArgument('template'), + Argument::createIntListTypeArgument('exclude_template'), Argument::createIntListTypeArgument('exclude'), new Argument( 'order', @@ -115,15 +116,25 @@ class Attribute extends BaseI18nLoop $template = $productObj->getTemplate(); } - // If we have to filter by template, find all attributes assigned to this template, and filter by found IDs if (null !== $template) { $search->filterById( - AttributeTemplateQuery::create()->filterByTemplateId($template)->select('id')->find(), + AttributeTemplateQuery::create()->filterByTemplateId($template)->select('attribute_id')->find(), Criteria::IN ); } + $exclude_template = $this->getExcludeTemplate(); + + // If we have to filter by template, find all attributes assigned to this template, and filter by found IDs + if (null !== $exclude_template) { + // Exclure tous les attribut qui sont attachés aux templates indiqués + $search->filterById( + AttributeTemplateQuery::create()->filterByTemplateId($exclude_template)->select('attribute_id')->find(), + Criteria::NOT_IN + ); + } + $orders = $this->getOrder(); foreach ($orders as $order) { diff --git a/core/lib/Thelia/Core/Template/Loop/Feature.php b/core/lib/Thelia/Core/Template/Loop/Feature.php index 7f412e4b0..f5c66d2e6 100755 --- a/core/lib/Thelia/Core/Template/Loop/Feature.php +++ b/core/lib/Thelia/Core/Template/Loop/Feature.php @@ -38,6 +38,7 @@ use Thelia\Model\Map\ProductCategoryTableMap; use Thelia\Type\TypeCollection; use Thelia\Type; use Thelia\Type\BooleanOrBothType; +use Thelia\Model\FeatureTemplateQuery; /** * @@ -60,7 +61,8 @@ class Feature extends BaseI18nLoop return new ArgumentCollection( Argument::createIntListTypeArgument('id'), Argument::createIntListTypeArgument('product'), - Argument::createIntListTypeArgument('category'), + Argument::createIntListTypeArgument('template'), + Argument::createIntListTypeArgument('exclude_template'), Argument::createBooleanOrBothTypeArgument('visible', 1), Argument::createIntListTypeArgument('exclude'), new Argument( @@ -102,22 +104,33 @@ class Feature extends BaseI18nLoop if ($visible != BooleanOrBothType::ANY) $search->filterByVisible($visible); $product = $this->getProduct(); - $category = $this->getCategory(); + $template = $this->getTemplate(); if (null !== $product) { - $productCategories = ProductCategoryQuery::create()->select(array(ProductCategoryTableMap::CATEGORY_ID))->filterByProductId($product, Criteria::IN)->find()->getData(); + // Find the template assigned to the product. + $productObj = ProductQuery::create()->findPk($product); - if (null === $category) { - $category = $productCategories; - } else { - $category = array_merge($category, $productCategories); - } + // Ignore if the product cannot be found. + if ($productObj !== null) + $template = $productObj->getTemplate(); + } + + // If we have to filter by template, find all features assigned to this template, and filter by found IDs + if (null !== $template) { + $search->filterById( + FeatureTemplateQuery::create()->filterByTemplateId($template)->select('feature_id')->find(), + Criteria::IN + ); } - if (null !== $category) { - $search->filterByCategory( - CategoryQuery::create()->filterById($category)->find(), - Criteria::IN + $exclude_template = $this->getExcludeTemplate(); + + // If we have to filter by template, find all features assigned to this template, and filter by found IDs + if (null !== $exclude_template) { + // Exclure tous les attribut qui sont attachés aux templates indiqués + $search->filterById( + FeatureTemplateQuery::create()->filterByTemplateId($exclude_template)->select('feature_id')->find(), + Criteria::NOT_IN ); } diff --git a/templates/admin/default/ajax/template-attribute-list.html b/templates/admin/default/ajax/template-attribute-list.html index b6379672a..772ed5883 100644 --- a/templates/admin/default/ajax/template-attribute-list.html +++ b/templates/admin/default/ajax/template-attribute-list.html @@ -1,15 +1,102 @@
{ifloop rel="free_attributes"} - - {intl l='Select an attribute and click (+) to add it to this template'} +
+ + + +
+ + + + +
+ + {intl l='Select an attribute and click (+) to add it to this template'} +
+ {/ifloop} {elseloop rel="free_attributes"}
There is currently no available attributes.
{/elseloop}
+ + + + + + + + {module_include location='template_attributes_table_header'} + + + + + + + {loop name="assigned_attributes" type="attribute" template="$template_id" backend_context="1" lang="$edit_language_id"} + + + + + + {module_include location='template_attributes_table_row'} + + + + {/loop} + + {elseloop rel="assigned_attributes"} + + + + {/elseloop} + +
{intl l='ID'}{intl l='Attribute title'}{intl l="Actions"}
{$ID} + {$TITLE} + +
+ {loop type="auth" name="can_create" roles="ADMIN" permissions="admin.configuration.template.attribute.delete"} + + + + {/loop} +
+
+
+ {intl l="This template contains no attributes"} +
+
+ +{* Delete value confirmation dialog *} + +{capture "delete_attribute_dialog"} + + +{/capture} + +{include + file = "includes/generic-confirm-dialog.html" + + dialog_id = "delete_attribute_dialog" + dialog_title = {intl l="Remove attribute"} + dialog_message = {intl l="Do you really want to remove this attribute from the template ?"} + + form_action = {url path='/admin/configuration/templates/attributes/delete'} + form_content = {$smarty.capture.delete_attribute_dialog nofilter} +} + + diff --git a/templates/admin/default/ajax/template-feature-list.html b/templates/admin/default/ajax/template-feature-list.html new file mode 100644 index 000000000..a20ff7125 --- /dev/null +++ b/templates/admin/default/ajax/template-feature-list.html @@ -0,0 +1,102 @@ +
+ {ifloop rel="free_features"} +
+ + + +
+ + + + +
+ + {intl l='Select an feature and click (+) to add it to this template'} +
+ + {/ifloop} + {elseloop rel="free_features"} +
There is currently no available features.
+ {/elseloop} +
+ + + + + + + + + {module_include location='template_features_table_header'} + + + + + + + {loop name="assigned_features" type="feature" template="$template_id" backend_context="1" lang="$edit_language_id"} + + + + + + {module_include location='template_features_table_row'} + + + + {/loop} + + {elseloop rel="assigned_features"} + + + + {/elseloop} + +
{intl l='ID'}{intl l='Feature title'}{intl l="Actions"}
{$ID} + {$TITLE} + +
+ {loop type="auth" name="can_create" roles="ADMIN" permissions="admin.configuration.template.feature.delete"} + + + + {/loop} +
+
+
+ {intl l="This template contains no features"} +
+
+ +{* Delete value confirmation dialog *} + +{capture "delete_feature_dialog"} + + +{/capture} + +{include + file = "includes/generic-confirm-dialog.html" + + dialog_id = "delete_feature_dialog" + dialog_title = {intl l="Remove feature"} + dialog_message = {intl l="Do you really want to remove this feature from the template ?"} + + form_action = {url path='/admin/configuration/templates/features/delete'} + form_content = {$smarty.capture.delete_feature_dialog nofilter} +} + + diff --git a/templates/admin/default/template-edit.html b/templates/admin/default/template-edit.html index 0a073b719..7b9ddd4aa 100644 --- a/templates/admin/default/template-edit.html +++ b/templates/admin/default/template-edit.html @@ -108,8 +108,8 @@ {/block} \ No newline at end of file