From c8baa38fed2dafeddc2adcbe1da761793ddcda4c Mon Sep 17 00:00:00 2001 From: franck Date: Tue, 17 Sep 2013 17:31:22 +0200 Subject: [PATCH] Added missing files... --- core/lib/Thelia/Action/Product.php | 199 ++++++++++ .../Controller/Admin/ProductController.php | 350 ++++++++++++++++++ .../Core/Event/ProductAddContentEvent.php | 48 +++ .../Thelia/Core/Event/ProductCreateEvent.php | 80 ++++ .../Core/Event/ProductDeleteContentEvent.php | 48 +++ .../Thelia/Core/Event/ProductDeleteEvent.php | 44 +++ core/lib/Thelia/Core/Event/ProductEvent.php | 54 +++ .../Event/ProductToggleVisibilityEvent.php | 28 ++ .../Thelia/Core/Event/ProductUpdateEvent.php | 113 ++++++ .../Thelia/Form/ProductModificationForm.php | 64 ++++ core/lib/Thelia/Model/Product.php | 21 +- 11 files changed, 1042 insertions(+), 7 deletions(-) create mode 100644 core/lib/Thelia/Action/Product.php create mode 100644 core/lib/Thelia/Controller/Admin/ProductController.php create mode 100644 core/lib/Thelia/Core/Event/ProductAddContentEvent.php create mode 100644 core/lib/Thelia/Core/Event/ProductCreateEvent.php create mode 100644 core/lib/Thelia/Core/Event/ProductDeleteContentEvent.php create mode 100644 core/lib/Thelia/Core/Event/ProductDeleteEvent.php create mode 100644 core/lib/Thelia/Core/Event/ProductEvent.php create mode 100644 core/lib/Thelia/Core/Event/ProductToggleVisibilityEvent.php create mode 100644 core/lib/Thelia/Core/Event/ProductUpdateEvent.php create mode 100644 core/lib/Thelia/Form/ProductModificationForm.php diff --git a/core/lib/Thelia/Action/Product.php b/core/lib/Thelia/Action/Product.php new file mode 100644 index 000000000..d2deb7688 --- /dev/null +++ b/core/lib/Thelia/Action/Product.php @@ -0,0 +1,199 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Action; + +use Symfony\Component\EventDispatcher\EventSubscriberInterface; + +use Thelia\Model\ProductQuery; +use Thelia\Model\Product as ProductModel; + +use Thelia\Core\Event\TheliaEvents; + +use Thelia\Core\Event\ProductUpdateEvent; +use Thelia\Core\Event\ProductCreateEvent; +use Thelia\Core\Event\ProductDeleteEvent; +use Thelia\Model\ConfigQuery; +use Thelia\Core\Event\UpdatePositionEvent; +use Thelia\Core\Event\ProductToggleVisibilityEvent; +use Thelia\Core\Event\ProductAddContentEvent; +use Thelia\Core\Event\ProductDeleteContentEvent; +use Thelia\Model\ProductAssociatedContent; +use Thelia\Model\ProductAssociatedContentQuery; + +class Product extends BaseAction implements EventSubscriberInterface +{ + /** + * Create a new product entry + * + * @param ProductCreateEvent $event + */ + public function create(ProductCreateEvent $event) + { + $product = new ProductModel(); + + $product + ->setDispatcher($this->getDispatcher()) + + ->setLocale($event->getLocale()) + ->setTitle($event->getTitle()) + ->setParent($event->getParent()) + ->setVisible($event->getVisible()) + + ->save() + ; + + $event->setProduct($product); + } + + /** + * Change a product + * + * @param ProductUpdateEvent $event + */ + public function update(ProductUpdateEvent $event) + { + $search = ProductQuery::create(); + + if (null !== $product = ProductQuery::create()->findPk($event->getProductId())) { + + $product + ->setDispatcher($this->getDispatcher()) + + ->setLocale($event->getLocale()) + ->setTitle($event->getTitle()) + ->setDescription($event->getDescription()) + ->setChapo($event->getChapo()) + ->setPostscriptum($event->getPostscriptum()) + + ->setParent($event->getParent()) + ->setVisible($event->getVisible()) + + ->save(); + + $event->setProduct($product); + } + } + + /** + * Delete a product entry + * + * @param ProductDeleteEvent $event + */ + public function delete(ProductDeleteEvent $event) + { + if (null !== $product = ProductQuery::create()->findPk($event->getProductId())) { + + $product + ->setDispatcher($this->getDispatcher()) + ->delete() + ; + + $event->setProduct($product); + } + } + + /** + * Toggle product visibility. No form used here + * + * @param ActionEvent $event + */ + public function toggleVisibility(ProductToggleVisibilityEvent $event) + { + $product = $event->getProduct(); + + $product + ->setDispatcher($this->getDispatcher()) + ->setVisible($product->getVisible() ? false : true) + ->save() + ; + } + + /** + * Changes position, selecting absolute ou relative change. + * + * @param ProductChangePositionEvent $event + */ + public function updatePosition(UpdatePositionEvent $event) + { + if (null !== $product = ProductQuery::create()->findPk($event->getObjectId())) { + + $product->setDispatcher($this->getDispatcher()); + + $mode = $event->getMode(); + + if ($mode == UpdatePositionEvent::POSITION_ABSOLUTE) + return $product->changeAbsolutePosition($event->getPosition()); + else if ($mode == UpdatePositionEvent::POSITION_UP) + return $product->movePositionUp(); + else if ($mode == UpdatePositionEvent::POSITION_DOWN) + return $product->movePositionDown(); + } + } + + public function addContent(ProductAddContentEvent $event) { + + if (ProductAssociatedContentQuery::create() + ->filterByContentId($event->getContentId()) + ->filterByProduct($event->getProduct())->count() <= 0) { + + $content = new ProductAssociatedContent(); + + $content + ->setProduct($event->getProduct()) + ->setContentId($event->getContentId()) + ->save() + ; + } + } + + public function removeContent(ProductDeleteContentEvent $event) { + + $content = ProductAssociatedContentQuery::create() + ->filterByContentId($event->getContentId()) + ->filterByProduct($event->getProduct())->findOne() + ; + + if ($content !== null) $content->delete(); + } + + + /** + * {@inheritDoc} + */ + public static function getSubscribedEvents() + { + return array( + TheliaEvents::PRODUCT_CREATE => array("create", 128), + TheliaEvents::PRODUCT_UPDATE => array("update", 128), + TheliaEvents::PRODUCT_DELETE => array("delete", 128), + TheliaEvents::PRODUCT_TOGGLE_VISIBILITY => array("toggleVisibility", 128), + + TheliaEvents::PRODUCT_UPDATE_POSITION => array("updatePosition", 128), + + TheliaEvents::PRODUCT_ADD_CONTENT => array("addContent", 128), + TheliaEvents::PRODUCT_REMOVE_CONTENT => array("removeContent", 128), + + ); + } +} diff --git a/core/lib/Thelia/Controller/Admin/ProductController.php b/core/lib/Thelia/Controller/Admin/ProductController.php new file mode 100644 index 000000000..e1b651e60 --- /dev/null +++ b/core/lib/Thelia/Controller/Admin/ProductController.php @@ -0,0 +1,350 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Controller\Admin; + +use Thelia\Core\Event\ProductDeleteEvent; +use Thelia\Core\Event\TheliaEvents; +use Thelia\Core\Event\ProductUpdateEvent; +use Thelia\Core\Event\ProductCreateEvent; +use Thelia\Model\ProductQuery; +use Thelia\Form\ProductModificationForm; +use Thelia\Form\ProductCreationForm; +use Thelia\Core\Event\UpdatePositionEvent; +use Thelia\Core\Event\ProductToggleVisibilityEvent; +use Thelia\Core\Event\ProductDeleteContentEvent; +use Thelia\Core\Event\ProductAddContentEvent; +use Thelia\Model\ProductAssociatedContent; +use Thelia\Model\FolderQuery; +use Thelia\Model\ContentQuery; +use Propel\Runtime\ActiveQuery\Criteria; +use Thelia\Model\ProductAssociatedContentQuery; + +/** + * Manages products + * + * @author Franck Allimant + */ +class ProductController extends AbstractCrudController +{ + public function __construct() + { + parent::__construct( + 'product', + 'manual', + 'product_order', + + 'admin.products.default', + 'admin.products.create', + 'admin.products.update', + 'admin.products.delete', + + TheliaEvents::PRODUCT_CREATE, + TheliaEvents::PRODUCT_UPDATE, + TheliaEvents::PRODUCT_DELETE, + + TheliaEvents::PRODUCT_TOGGLE_VISIBILITY, + TheliaEvents::PRODUCT_UPDATE_POSITION + ); + } + + protected function getCreationForm() + { + return new ProductCreationForm($this->getRequest()); + } + + protected function getUpdateForm() + { + return new ProductModificationForm($this->getRequest()); + } + + protected function getCreationEvent($formData) + { + $createEvent = new ProductCreateEvent(); + + $createEvent + ->setTitle($formData['title']) + ->setLocale($formData["locale"]) + ->setParent($formData['parent']) + ->setVisible($formData['visible']) + ; + + return $createEvent; + } + + protected function getUpdateEvent($formData) + { + $changeEvent = new ProductUpdateEvent($formData['id']); + + // Create and dispatch the change event + $changeEvent + ->setLocale($formData['locale']) + ->setTitle($formData['title']) + ->setChapo($formData['chapo']) + ->setDescription($formData['description']) + ->setPostscriptum($formData['postscriptum']) + ->setVisible($formData['visible']) + ->setUrl($formData['url']) + ->setParent($formData['parent']) + ; + + return $changeEvent; + } + + protected function createUpdatePositionEvent($positionChangeMode, $positionValue) + { + return new UpdatePositionEvent( + $this->getRequest()->get('product_id', null), + $positionChangeMode, + $positionValue + ); + } + + protected function getDeleteEvent() + { + return new ProductDeleteEvent($this->getRequest()->get('product_id', 0)); + } + + protected function eventContainsObject($event) + { + return $event->hasProduct(); + } + + protected function hydrateObjectForm($object) + { + // Prepare the data that will hydrate the form + $data = array( + 'id' => $object->getId(), + 'locale' => $object->getLocale(), + 'title' => $object->getTitle(), + 'chapo' => $object->getChapo(), + 'description' => $object->getDescription(), + 'postscriptum' => $object->getPostscriptum(), + 'visible' => $object->getVisible(), + 'url' => $object->getRewritenUrl($this->getCurrentEditionLocale()), + 'parent' => $object->getParent() + ); + + // Setup the object form + return new ProductModificationForm($this->getRequest(), "form", $data); + } + + protected function getObjectFromEvent($event) + { + return $event->hasProduct() ? $event->getProduct() : null; + } + + protected function getExistingObject() + { + return ProductQuery::create() + ->joinWithI18n($this->getCurrentEditionLocale()) + ->findOneById($this->getRequest()->get('product_id', 0)); + } + + protected function getObjectLabel($object) + { + return $object->getTitle(); + } + + protected function getObjectId($object) + { + return $object->getId(); + } + + protected function getEditionArguments() + { + return array( + 'product_id' => $this->getRequest()->get('product_id', 0), + 'folder_id' => $this->getRequest()->get('folder_id', 0), + 'current_tab' => $this->getRequest()->get('current_tab', 'general') + ); + } + + protected function renderListTemplate($currentOrder) + { + $this->getListOrderFromSession('product', 'product_order', 'manual'); + + return $this->render('categories', + array( + 'product_order' => $currentOrder, + 'product_id' => $this->getRequest()->get('product_id', 0) + )); + } + + protected function redirectToListTemplate() + { + // Redirect to the product default category list + $product = $this->getExistingObject(); + + $this->redirectToRoute( + 'admin.products.default', + array('category_id' => $product != null ? $product->getDefaultCategory() : 0) + ); + } + + protected function renderEditionTemplate() + { + return $this->render('product-edit', $this->getEditionArguments()); + } + + protected function redirectToEditionTemplate() + { + $this->redirectToRoute("admin.products.update", $this->getEditionArguments()); + } + + /** + * Online status toggle product + */ + public function setToggleVisibilityAction() + { + // Check current user authorization + if (null !== $response = $this->checkAuth("admin.products.update")) return $response; + + $event = new ProductToggleVisibilityEvent($this->getExistingObject()); + + try { + $this->dispatch(TheliaEvents::PRODUCT_TOGGLE_VISIBILITY, $event); + } catch (\Exception $ex) { + // Any error + return $this->errorPage($ex); + } + + // Ajax response -> no action + return $this->nullResponse(); + } + + protected function performAdditionalDeleteAction($deleteEvent) + { + // Redirect to parent product list + $this->redirectToRoute( + 'admin.products.default', + array('category_id' => $deleteEvent->getProduct()->getDefaultCategory()) + ); + } + + protected function performAdditionalUpdateAction($updateEvent) + { + if ($this->getRequest()->get('save_mode') != 'stay') { + + // Redirect to parent product list + $this->redirectToRoute( + 'admin.categories.default', + array('category_id' => $product->getDefaultCategory()) + ); + } + } + + protected function performAdditionalUpdatePositionAction($event) + { + $product = ProductQuery::create()->findPk($event->getObjectId()); + + if ($product != null) { + // Redirect to parent product list + $this->redirectToRoute( + 'admin.categories.default', + array('category_id' => $product->getDefaultCategory()) + ); + } + + return null; + } + + public function getAvailableRelatedContentAction($productId, $folderId) + { + $result = array(); + + $folders = FolderQuery::create()->filterById($folderId)->find(); + + if ($folders !== null) { + + $list = ContentQuery::create() + ->joinWithI18n($this->getCurrentEditionLocale()) + ->filterByFolder($folders, Criteria::IN) + ->filterById(ProductAssociatedContentQuery::create()->select('content_id')->findByProductId($productId), 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.products.update")) return $response; + + $content_id = intval($this->getRequest()->get('content_id')); + + if ($content_id > 0) { + + $event = new ProductAddContentEvent( + $this->getExistingObject(), + $content_id + ); + + try { + $this->dispatch(TheliaEvents::PRODUCT_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.products.update")) return $response; + + $content_id = intval($this->getRequest()->get('content_id')); + + if ($content_id > 0) { + + $event = new ProductDeleteContentEvent( + $this->getExistingObject(), + $content_id + ); + + try { + $this->dispatch(TheliaEvents::PRODUCT_REMOVE_CONTENT, $event); + } + catch (\Exception $ex) { + // Any error + return $this->errorPage($ex); + } + } + + $this->redirectToEditionTemplate(); + } +} diff --git a/core/lib/Thelia/Core/Event/ProductAddContentEvent.php b/core/lib/Thelia/Core/Event/ProductAddContentEvent.php new file mode 100644 index 000000000..8cd648753 --- /dev/null +++ b/core/lib/Thelia/Core/Event/ProductAddContentEvent.php @@ -0,0 +1,48 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Core\Event; + +use Thelia\Model\Product; + +class ProductAddContentEvent extends ProductEvent +{ + protected $content_id; + + public function __construct(Product $product, $content_id) + { + parent::__construct($product); + + $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/ProductCreateEvent.php b/core/lib/Thelia/Core/Event/ProductCreateEvent.php new file mode 100644 index 000000000..51be86e25 --- /dev/null +++ b/core/lib/Thelia/Core/Event/ProductCreateEvent.php @@ -0,0 +1,80 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Core\Event; + +class ProductCreateEvent extends ProductEvent +{ + protected $title; + protected $parent; + protected $locale; + protected $visible; + + public function getTitle() + { + return $this->title; + } + + public function setTitle($title) + { + $this->title = $title; + + return $this; + } + + public function getParent() + { + return $this->parent; + } + + public function setParent($parent) + { + $this->parent = $parent; + + return $this; + } + + public function getLocale() + { + return $this->locale; + } + + public function setLocale($locale) + { + $this->locale = $locale; + + return $this; + } + + public function getVisible() + { + return $this->visible; + } + + public function setVisible($visible) + { + $this->visible = $visible; + + return $this; + } +} diff --git a/core/lib/Thelia/Core/Event/ProductDeleteContentEvent.php b/core/lib/Thelia/Core/Event/ProductDeleteContentEvent.php new file mode 100644 index 000000000..f3cdbda1f --- /dev/null +++ b/core/lib/Thelia/Core/Event/ProductDeleteContentEvent.php @@ -0,0 +1,48 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Core\Event; + +use Thelia\Model\Product; + +class ProductDeleteContentEvent extends ProductEvent +{ + protected $content_id; + + public function __construct(Product $product, $content_id) + { + parent::__construct($product); + + $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/ProductDeleteEvent.php b/core/lib/Thelia/Core/Event/ProductDeleteEvent.php new file mode 100644 index 000000000..43273887c --- /dev/null +++ b/core/lib/Thelia/Core/Event/ProductDeleteEvent.php @@ -0,0 +1,44 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Core\Event; + +class ProductDeleteEvent extends ProductEvent +{ + public function __construct($product_id) + { + $this->product_id = $product_id; + } + + public function getProductId() + { + return $this->product_id; + } + + public function setProductId($product_id) + { + $this->product_id = $product_id; + + return $this; + } +} diff --git a/core/lib/Thelia/Core/Event/ProductEvent.php b/core/lib/Thelia/Core/Event/ProductEvent.php new file mode 100644 index 000000000..6decd8101 --- /dev/null +++ b/core/lib/Thelia/Core/Event/ProductEvent.php @@ -0,0 +1,54 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Core\Event; + +use Thelia\Model\Product; +use Thelia\Core\Event\ActionEvent; + +class ProductEvent extends ActionEvent +{ + public $product = null; + + public function __construct(Product $product = null) + { + $this->product = $product; + } + + public function hasProduct() + { + return ! is_null($this->product); + } + + public function getProduct() + { + return $this->product; + } + + public function setProduct(Product $product) + { + $this->product = $product; + + return $this; + } +} diff --git a/core/lib/Thelia/Core/Event/ProductToggleVisibilityEvent.php b/core/lib/Thelia/Core/Event/ProductToggleVisibilityEvent.php new file mode 100644 index 000000000..732cfac76 --- /dev/null +++ b/core/lib/Thelia/Core/Event/ProductToggleVisibilityEvent.php @@ -0,0 +1,28 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Core\Event; + +class ProductToggleVisibilityEvent extends ProductEvent +{ +} diff --git a/core/lib/Thelia/Core/Event/ProductUpdateEvent.php b/core/lib/Thelia/Core/Event/ProductUpdateEvent.php new file mode 100644 index 000000000..8ade33c74 --- /dev/null +++ b/core/lib/Thelia/Core/Event/ProductUpdateEvent.php @@ -0,0 +1,113 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Core\Event; + +class ProductUpdateEvent extends ProductCreateEvent +{ + protected $product_id; + + protected $chapo; + protected $description; + protected $postscriptum; + + protected $url; + protected $parent; + + public function __construct($product_id) + { + $this->product_id = $product_id; + } + + public function getProductId() + { + return $this->product_id; + } + + public function setProductId($product_id) + { + $this->product_id = $product_id; + + return $this; + } + + public function getChapo() + { + return $this->chapo; + } + + public function setChapo($chapo) + { + $this->chapo = $chapo; + + return $this; + } + + public function getDescription() + { + return $this->description; + } + + public function setDescription($description) + { + $this->description = $description; + + return $this; + } + + public function getPostscriptum() + { + return $this->postscriptum; + } + + public function setPostscriptum($postscriptum) + { + $this->postscriptum = $postscriptum; + + return $this; + } + + public function getUrl() + { + return $this->url; + } + + public function setUrl($url) + { + $this->url = $url; + + return $this; + } + + public function getParent() + { + return $this->parent; + } + + public function setParent($parent) + { + $this->parent = $parent; + + return $this; + } +} diff --git a/core/lib/Thelia/Form/ProductModificationForm.php b/core/lib/Thelia/Form/ProductModificationForm.php new file mode 100644 index 000000000..dbda15eaa --- /dev/null +++ b/core/lib/Thelia/Form/ProductModificationForm.php @@ -0,0 +1,64 @@ +. */ +/* */ +/*************************************************************************************/ +namespace Thelia\Form; + +use Symfony\Component\Validator\Constraints\GreaterThan; +use Thelia\Core\Translation\Translator; +use Symfony\Component\Validator\Constraints\NotBlank; + +class ProductModificationForm extends ProductCreationForm +{ + use StandardDescriptionFieldsTrait; + + protected function buildForm() + { + parent::buildForm(true); + + $this->formBuilder + ->add("id", "integer", array( + "label" => Translator::getInstance()->trans("Prodcut ID *"), + "label_attr" => array("for" => "product_id_field"), + "constraints" => array(new GreaterThan(array('value' => 0))) + + )) + ->add("template_id", "integer", array( + "label" => Translator::getInstance()->trans("Product template"), + "label_attr" => array("for" => "product_template_field") + + )) + ->add("url", "text", array( + "label" => Translator::getInstance()->trans("Rewriten URL *"), + "constraints" => array(new NotBlank()), + "label_attr" => array("for" => "rewriten_url_field") + )) + ; + + // Add standard description fields, excluding title and locale, which a re defined in parent class + $this->addStandardDescFields(array('title', 'locale')); + } + + public function getName() + { + return "thelia_product_modification"; + } +} \ No newline at end of file diff --git a/core/lib/Thelia/Model/Product.php b/core/lib/Thelia/Model/Product.php index c770379b9..87b414dd9 100755 --- a/core/lib/Thelia/Model/Product.php +++ b/core/lib/Thelia/Model/Product.php @@ -46,7 +46,8 @@ class Product extends BaseProduct /** * @return the current default category for this product */ - public function getDefaultCategory() { + public function getDefaultCategory() + { // Find default category $default_category = ProductCategoryQuery::create() ->filterByProductId($this->getId()) @@ -61,8 +62,8 @@ class Product extends BaseProduct * * @param integer $categoryId the new default category id */ - public function setDefaultCategory($categoryId) { - + public function setDefaultCategory($categoryId) + { // Unset previous category ProductCategoryQuery::create() ->filterByProductId($this->getId()) @@ -85,11 +86,17 @@ class Product extends BaseProduct /** * Calculate next position relative to our default category */ - protected function addCriteriaToPositionQuery($query) { - - // TODO: Find the default category for this product, - // and generate the position relative to this category + protected function addCriteriaToPositionQuery($query) + { + // Retrourver les produits qui ont la même categorie par defaut + $produits = ProductCategoryQuery::create() + ->filterByCategory($this->getDefaultCategory()) + ->filterByDefaultCategory(true) + ->select('product_id') + ->find(); + // Filtrer la requete sur ces produits + if ($produits != null) $query->filterById($produits); } /**