diff --git a/core/lib/Thelia/Action/Category.php b/core/lib/Thelia/Action/Category.php index a34f4a85e..64254d734 100755 --- a/core/lib/Thelia/Action/Category.php +++ b/core/lib/Thelia/Action/Category.php @@ -36,6 +36,10 @@ use Thelia\Core\Event\CategoryDeleteEvent; use Thelia\Model\ConfigQuery; use Thelia\Core\Event\UpdatePositionEvent; use Thelia\Core\Event\CategoryToggleVisibilityEvent; +use Thelia\Core\Event\CategoryAddContentEvent; +use Thelia\Core\Event\CategoryDeleteContentEvent; +use Thelia\Model\CategoryAssociatedContent; +use Thelia\Model\CategoryAssociatedContentQuery; class Category extends BaseAction implements EventSubscriberInterface { @@ -147,6 +151,33 @@ class Category extends BaseAction implements EventSubscriberInterface } } + public function addContent(CategoryAddContentEvent $event) { + + if (CategoryAssociatedContentQuery::create() + ->filterByContentId($event->getContentId()) + ->filterByCategory($event->getCategory())->count() <= 0) { + + $content = new CategoryAssociatedContent(); + + $content + ->setCategory($event->getCategory()) + ->setContentId($event->getContentId()) + ->save() + ; + } + } + + public function removeContent(CategoryDeleteContentEvent $event) { + + $content = CategoryAssociatedContentQuery::create() + ->filterByContentId($event->getContentId()) + ->filterByCategory($event->getCategory())->findOne() + ; + + if ($content !== null) $content->delete(); + } + + /** * {@inheritDoc} */ @@ -157,7 +188,12 @@ class Category extends BaseAction implements EventSubscriberInterface TheliaEvents::CATEGORY_UPDATE => array("update", 128), TheliaEvents::CATEGORY_DELETE => array("delete", 128), TheliaEvents::CATEGORY_TOGGLE_VISIBILITY => array("toggleVisibility", 128), - TheliaEvents::CATEGORY_UPDATE_POSITION => array("updatePosition", 128) + + TheliaEvents::CATEGORY_UPDATE_POSITION => array("updatePosition", 128), + + TheliaEvents::CATEGORY_ADD_CONTENT => array("addContent", 128), + TheliaEvents::CATEGORY_REMOVE_CONTENT => array("removeContent", 128), + ); } } diff --git a/core/lib/Thelia/Config/Resources/routing/admin.xml b/core/lib/Thelia/Config/Resources/routing/admin.xml index 53c137988..ca76db188 100755 --- a/core/lib/Thelia/Config/Resources/routing/admin.xml +++ b/core/lib/Thelia/Config/Resources/routing/admin.xml @@ -95,12 +95,25 @@ Thelia\Controller\Admin\CategoryController::updatePositionAction + + + Thelia\Controller\Admin\CategoryController::addRelatedContentAction + + + + Thelia\Controller\Admin\CategoryController::deleteRelatedContentAction + + + + Thelia\Controller\Admin\CategoryController::getAvailableRelatedContentAction + xml|json + + Thelia\Controller\Admin\CategoryController::getByParentIdAction xml|json - diff --git a/core/lib/Thelia/Controller/Admin/AbstractCrudController.php b/core/lib/Thelia/Controller/Admin/AbstractCrudController.php index 5b7f8b60a..7b9550610 100644 --- a/core/lib/Thelia/Controller/Admin/AbstractCrudController.php +++ b/core/lib/Thelia/Controller/Admin/AbstractCrudController.php @@ -224,7 +224,7 @@ abstract class AbstractCrudController extends BaseAdminController * @param unknown $updateEvent the update event * @return Response a response, or null to continue normal processing */ - protected function performAdditionalUpdateAction($updateeEvent) + protected function performAdditionalUpdateAction($updateEvent) { return null; } diff --git a/core/lib/Thelia/Controller/Admin/CategoryController.php b/core/lib/Thelia/Controller/Admin/CategoryController.php index 83d90c86e..a9f5ef4d4 100755 --- a/core/lib/Thelia/Controller/Admin/CategoryController.php +++ b/core/lib/Thelia/Controller/Admin/CategoryController.php @@ -32,6 +32,13 @@ use Thelia\Form\CategoryModificationForm; use Thelia\Form\CategoryCreationForm; use Thelia\Core\Event\UpdatePositionEvent; use Thelia\Core\Event\CategoryToggleVisibilityEvent; +use Thelia\Core\Event\CategoryDeleteContentEvent; +use Thelia\Core\Event\CategoryAddContentEvent; +use Thelia\Model\CategoryAssociatedContent; +use Thelia\Model\FolderQuery; +use Thelia\Model\ContentQuery; +use Propel\Runtime\ActiveQuery\Criteria; +use Thelia\Model\CategoryAssociatedContentQuery; /** * Manages categories @@ -162,14 +169,23 @@ class CategoryController extends AbstractCrudController } protected function renderEditionTemplate() { - return $this->render('category-edit', array('category_id' => $this->getRequest()->get('category_id', 0))); + + return $this->render('category-edit', + array( + 'category_id' => $this->getRequest()->get('category_id', 0), + 'folder_id' => $this->getRequest()->get('folder_id', 0), + 'current_tab' => $this->getRequest()->get('current_tab', 'general') + )); } protected function redirectToEditionTemplate() { $this->redirectToRoute( "admin.categories.update", - array('category_id' => $this->getRequest()->get('category_id', 0)) - ); + array( + 'category_id' => $this->getRequest()->get('category_id', 0), + 'folder_id' => $this->getRequest()->get('folder_id', 0), + 'current_tab' => $this->getRequest()->get('current_tab', 'general') + )); } protected function redirectToListTemplate() { @@ -209,6 +225,18 @@ class CategoryController extends AbstractCrudController ); } + protected function performAdditionalUpdateAction($updateEvent) + { + if ($this->getRequest()->get('save_mode') != 'stay') { + + // Redirect to parent category list + $this->redirectToRoute( + 'admin.categories.default', + array('category_id' => $updateEvent->getCategory()->getParent()) + ); + } + } + protected function performAdditionalUpdatePositionAction($event) { @@ -224,4 +252,80 @@ class CategoryController extends AbstractCrudController return null; } + + public function getAvailableRelatedContentAction($categoryId, $folderId) { + + $result = array(); + + $folders = FolderQuery::create()->filterById($folderId)->find(); + + if ($folders !== null) { + + $list = ContentQuery::create() + ->filterByFolder($folders, Criteria::IN) + ->filterById(CategoryAssociatedContentQuery::create()->select('content_id')->findByCategoryId($categoryId), Criteria::NOT_IN) + ->find(); + ; + + if ($list !== null) { + foreach($list as $item) { + $result[] = array('id' => $item->getId(), 'title' => $item->getTitle()); + } + } + } + + return $this->jsonResponse(json_encode($result)); + } + + public function addRelatedContentAction() { + + // Check current user authorization + if (null !== $response = $this->checkAuth("admin.categories.update")) return $response; + + $content_id = intval($this->getRequest()->get('content_id')); + + if ($content_id > 0) { + + $event = new CategoryAddContentEvent( + $this->getExistingObject(), + $content_id + ); + + try { + $this->dispatch(TheliaEvents::CATEGORY_ADD_CONTENT, $event); + } + catch (\Exception $ex) { + // Any error + return $this->errorPage($ex); + } + } + + $this->redirectToEditionTemplate(); + } + + public function deleteRelatedContentAction() { + + // Check current user authorization + if (null !== $response = $this->checkAuth("admin.categories.update")) return $response; + + $content_id = intval($this->getRequest()->get('content_id')); + + if ($content_id > 0) { + + $event = new CategoryDeleteContentEvent( + $this->getExistingObject(), + $content_id + ); + + try { + $this->dispatch(TheliaEvents::CATEGORY_REMOVE_CONTENT, $event); + } + catch (\Exception $ex) { + // Any error + return $this->errorPage($ex); + } + } + + $this->redirectToEditionTemplate(); + } } diff --git a/core/lib/Thelia/Controller/BaseController.php b/core/lib/Thelia/Controller/BaseController.php index 3c4c0a5bc..1c7140211 100755 --- a/core/lib/Thelia/Controller/BaseController.php +++ b/core/lib/Thelia/Controller/BaseController.php @@ -62,6 +62,14 @@ class BaseController extends ContainerAware return new Response(); } + /** + * Return a JSON response + */ + protected function jsonResponse($json_data) + { + return new Response($json_data, 200, array('content-type' => 'application/json')); + } + /** * Dispatch a Thelia event * diff --git a/core/lib/Thelia/Core/Event/CategoryAddContentEvent.php b/core/lib/Thelia/Core/Event/CategoryAddContentEvent.php new file mode 100644 index 000000000..3ca5b2ae5 --- /dev/null +++ b/core/lib/Thelia/Core/Event/CategoryAddContentEvent.php @@ -0,0 +1,48 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Core\Event; + +use Thelia\Model\Category; + +class CategoryAddContentEvent extends CategoryEvent +{ + protected $content_id; + + public function __construct(Category $category, $content_id) + { + parent::__construct($category); + + $this->content_id = $content_id; + } + + public function getContentId() + { + return $this->content_id; + } + + public function setContentId($content_id) + { + $this->content_id = $content_id; + } +} diff --git a/core/lib/Thelia/Core/Event/CategoryDeleteContentEvent.php b/core/lib/Thelia/Core/Event/CategoryDeleteContentEvent.php new file mode 100644 index 000000000..bb28beb21 --- /dev/null +++ b/core/lib/Thelia/Core/Event/CategoryDeleteContentEvent.php @@ -0,0 +1,48 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Core\Event; + +use Thelia\Model\Category; + +class CategoryDeleteContentEvent extends CategoryEvent +{ + protected $content_id; + + public function __construct(Category $category, $content_id) + { + parent::__construct($category); + + $this->content_id = $content_id; + } + + public function getContentId() + { + return $this->content_id; + } + + public function setContentId($content_id) + { + $this->content_id = $content_id; + } +} diff --git a/core/lib/Thelia/Core/Event/TheliaEvents.php b/core/lib/Thelia/Core/Event/TheliaEvents.php index 1fc91dacb..e7eb41203 100755 --- a/core/lib/Thelia/Core/Event/TheliaEvents.php +++ b/core/lib/Thelia/Core/Event/TheliaEvents.php @@ -153,6 +153,9 @@ final class TheliaEvents const CATEGORY_TOGGLE_VISIBILITY = "action.toggleCategoryVisibility"; const CATEGORY_UPDATE_POSITION = "action.updateCategoryPosition"; + const CATEGORY_ADD_CONTENT = "action.categoryAddContent"; + const CATEGORY_REMOVE_CONTENT = "action.categoryRemoveContent"; + const BEFORE_CREATECATEGORY = "action.before_createcategory"; const AFTER_CREATECATEGORY = "action.after_createcategory"; diff --git a/core/lib/Thelia/Core/Template/Loop/AssociatedContent.php b/core/lib/Thelia/Core/Template/Loop/AssociatedContent.php index 253c48da7..d85f75fa6 100755 --- a/core/lib/Thelia/Core/Template/Loop/AssociatedContent.php +++ b/core/lib/Thelia/Core/Template/Loop/AssociatedContent.php @@ -53,8 +53,12 @@ class AssociatedContent extends Content { $argumentCollection = parent::getArgDefinitions(); - $argumentCollection->addArgument(Argument::createIntTypeArgument('product')) - ->addArgument(Argument::createIntTypeArgument('category')); + $argumentCollection + ->addArgument(Argument::createIntTypeArgument('product')) + ->addArgument(Argument::createIntTypeArgument('category')) + ->addArgument(Argument::createIntTypeArgument('exclude_product')) + ->addArgument(Argument::createIntTypeArgument('exclude_category')) + ; $argumentCollection->get('order')->default = "associated_content"; @@ -91,6 +95,28 @@ class AssociatedContent extends Content $search->filterByCategoryId($category, Criteria::EQUAL); } + $exclude_product = $this->getExcludeProduct(); + + // If we have to filter by template, find all attributes assigned to this template, and filter by found IDs + if (null !== $exclude_product) { + // Exclure tous les attribut qui sont attachés aux templates indiqués + $search->filterById( + ProductAssociatedContentQuery::create()->filterByProductId($exclude_product)->select('product_id')->find(), + Criteria::NOT_IN + ); + } + + $exclude_category = $this->getExcludeCategory(); + + // If we have to filter by template, find all attributes assigned to this template, and filter by found IDs + if (null !== $exclude_category) { + // Exclure tous les attribut qui sont attachés aux templates indiqués + $search->filterById( + CategoryAssociatedContentQuery::create()->filterByProductId($exclude_category)->select('category_id')->find(), + Criteria::NOT_IN + ); + } + $order = $this->getOrder(); $orderByAssociatedContent = array_search('associated_content', $order); $orderByAssociatedContentReverse = array_search('associated_content_reverse', $order); diff --git a/core/lib/Thelia/Form/CategoryCreationForm.php b/core/lib/Thelia/Form/CategoryCreationForm.php index 172b09ee6..5da76c8ba 100755 --- a/core/lib/Thelia/Form/CategoryCreationForm.php +++ b/core/lib/Thelia/Form/CategoryCreationForm.php @@ -43,15 +43,18 @@ class CategoryCreationForm extends BaseForm "label" => Translator::getInstance()->trans("Parent category *"), "constraints" => array( new NotBlank() - ) + ), + "label_attr" => array("for" => "parent_create") )) ->add("locale", "text", array( "constraints" => array( new NotBlank() - ) + ), + "label_attr" => array("for" => "locale_create") )) ->add("visible", "integer", array( - "label" => Translator::getInstance()->trans("This category is online on the front office.") + "label" => Translator::getInstance()->trans("This category is online on the front office."), + "label_attr" => array("for" => "visible_create") )) ; } diff --git a/core/lib/Thelia/Form/CategoryModificationForm.php b/core/lib/Thelia/Form/CategoryModificationForm.php index 42b5893c1..943c0d941 100644 --- a/core/lib/Thelia/Form/CategoryModificationForm.php +++ b/core/lib/Thelia/Form/CategoryModificationForm.php @@ -39,7 +39,8 @@ class CategoryModificationForm extends CategoryCreationForm ->add("url", "text", array( "label" => Translator::getInstance()->trans("Rewriten URL *"), - "constraints" => array(new NotBlank()) + "constraints" => array(new NotBlank()), + "label_attr" => array("for" => "rewriten_url") )) ; diff --git a/core/lib/Thelia/Form/StandardDescriptionFieldsTrait.php b/core/lib/Thelia/Form/StandardDescriptionFieldsTrait.php index 9d1851252..b35aad12f 100644 --- a/core/lib/Thelia/Form/StandardDescriptionFieldsTrait.php +++ b/core/lib/Thelia/Form/StandardDescriptionFieldsTrait.php @@ -58,7 +58,8 @@ trait StandardDescriptionFieldsTrait "label" => Translator::getInstance()->trans("Title"), "label_attr" => array( "for" => "title" - ) + ), + "label_attr" => array("for" => "title_field") ) ); @@ -67,7 +68,7 @@ trait StandardDescriptionFieldsTrait ->add("chapo", "text", array( "label" => Translator::getInstance()->trans("Summary"), "label_attr" => array( - "for" => "summary" + "for" => "summary_field" ) )); @@ -76,7 +77,7 @@ trait StandardDescriptionFieldsTrait ->add("description", "text", array( "label" => Translator::getInstance()->trans("Detailed description"), "label_attr" => array( - "for" => "detailed_description" + "for" => "detailed_description_field" ) )); @@ -85,7 +86,7 @@ trait StandardDescriptionFieldsTrait ->add("postscriptum", "text", array( "label" => Translator::getInstance()->trans("Conclusion"), "label_attr" => array( - "for" => "conclusion" + "for" => "conclusion_field" ) )); } diff --git a/core/lib/Thelia/Model/CategoryDocument.php b/core/lib/Thelia/Model/CategoryDocument.php index 73d99f591..5724e2df1 100755 --- a/core/lib/Thelia/Model/CategoryDocument.php +++ b/core/lib/Thelia/Model/CategoryDocument.php @@ -25,4 +25,4 @@ class CategoryDocument extends BaseCategoryDocument return true; } -} +} \ No newline at end of file diff --git a/templates/admin/default/category-edit.html b/templates/admin/default/category-edit.html index 626114d2a..723cf8242 100755 --- a/templates/admin/default/category-edit.html +++ b/templates/admin/default/category-edit.html @@ -39,8 +39,8 @@
-