From 3627d96175c1424916d353c5a06c25883a338703 Mon Sep 17 00:00:00 2001 From: gmorel Date: Wed, 18 Sep 2013 10:07:57 +0200 Subject: [PATCH] WIP : Upload management --- core/lib/Thelia/Config/Resources/config.xml | 1 + .../Thelia/Config/Resources/routing/admin.xml | 4 + .../Controller/Admin/CategoryController.php | 60 +++++++++++ .../Form/CategoryPictureCreationForm.php | 100 ++++++++++++++++++ core/lib/Thelia/Model/CategoryImage.php | 44 ++++++++ templates/admin/default/category-edit.html | 25 +++++ 6 files changed, 234 insertions(+) create mode 100644 core/lib/Thelia/Form/CategoryPictureCreationForm.php diff --git a/core/lib/Thelia/Config/Resources/config.xml b/core/lib/Thelia/Config/Resources/config.xml index 41e49d7d7..db2b1db82 100755 --- a/core/lib/Thelia/Config/Resources/config.xml +++ b/core/lib/Thelia/Config/Resources/config.xml @@ -54,6 +54,7 @@
+ diff --git a/core/lib/Thelia/Config/Resources/routing/admin.xml b/core/lib/Thelia/Config/Resources/routing/admin.xml index af7c950af..8f2e54f86 100755 --- a/core/lib/Thelia/Config/Resources/routing/admin.xml +++ b/core/lib/Thelia/Config/Resources/routing/admin.xml @@ -100,6 +100,10 @@ Thelia\Controller\Admin\CategoryController::addRelatedContentAction + + Thelia\Controller\Admin\CategoryController::addRelatedPictureAction + + Thelia\Controller\Admin\CategoryController::deleteRelatedContentAction diff --git a/core/lib/Thelia/Controller/Admin/CategoryController.php b/core/lib/Thelia/Controller/Admin/CategoryController.php index 8c74c31ec..1c7854416 100755 --- a/core/lib/Thelia/Controller/Admin/CategoryController.php +++ b/core/lib/Thelia/Controller/Admin/CategoryController.php @@ -27,6 +27,7 @@ use Thelia\Core\Event\CategoryDeleteEvent; use Thelia\Core\Event\TheliaEvents; use Thelia\Core\Event\CategoryUpdateEvent; use Thelia\Core\Event\CategoryCreateEvent; +use Thelia\Form\CategoryPictureCreationForm; use Thelia\Model\CategoryQuery; use Thelia\Form\CategoryModificationForm; use Thelia\Form\CategoryCreationForm; @@ -306,6 +307,39 @@ class CategoryController extends AbstractCrudController $this->redirectToEditionTemplate(); } + /** + * Add category pictures + * + * @return \Symfony\Component\HttpFoundation\Response + */ + public function addRelatedPictureAction() + { + // 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 @@ -331,4 +365,30 @@ class CategoryController extends AbstractCrudController $this->redirectToEditionTemplate(); } + + /** + * @todo remove + * @return Symfony\Component\HttpFoundation\Response|void + */ + public function updateAction() + { + var_dump('updateAction'); + if ($this->getRequest()->isMethod('POST')) { + var_dump('getRequest', $this->getRequest()->files); + // Create the form from the request + $creationForm = new CategoryPictureCreationForm($this->getRequest()); + + // Check the form against constraints violations + $form = $this->validateForm($creationForm, 'POST'); + var_dump('$form', $form); + + // Get the form field values + $data = $form->getData(); + var_dump('$data', $data); + } + + + return parent::updateAction(); + } + } diff --git a/core/lib/Thelia/Form/CategoryPictureCreationForm.php b/core/lib/Thelia/Form/CategoryPictureCreationForm.php new file mode 100644 index 000000000..2dffc61c3 --- /dev/null +++ b/core/lib/Thelia/Form/CategoryPictureCreationForm.php @@ -0,0 +1,100 @@ +. */ +/* */ +/*************************************************************************************/ +namespace Thelia\Form; + +use Symfony\Component\Validator\Constraints\Image; +use Symfony\Component\Validator\Constraints\NotBlank; +use Thelia\Core\Translation\Translator; + +class CategoryPictureCreationForm extends BaseForm +{ + protected function buildForm() + { + $this->formBuilder +// ->add('alt') + ->add('file', 'file', array( + 'constraints' => array( + new NotBlank(), + new Image( + array( + 'minWidth' => 200, + 'maxWidth' => 400, + 'minHeight' => 200, + 'maxHeight' => 400, + ) + ) + ))) +// ->add('category_id', 'model', array( +// 'disabled' => false, +// 'class' => 'Thelia\Model\ProductImage' +// )) +// ->add('position', 'integer', array( +// 'constraints' => array( +// new NotBlank() +// ) +// )) + ->add('title', 'text', array( + 'constraints' => array( +// new NotBlank() + ), +// 'label' => Translator::getInstance()->trans('Category picture title *'), +// 'label_attr' => array( +// 'for' => 'title' +// ) + )) +// ->add('description', 'text', array( +// 'constraints' => array( +// new NotBlank() +// ), +// 'label' => Translator::getInstance()->trans('Category picture description *'), +// 'label_attr' => array( +// 'for' => 'description' +// ) +// )) +// ->add('chapo', 'text', array( +// 'constraints' => array( +// new NotBlank() +// ), +// 'label' => Translator::getInstance()->trans('Category picture chapo *'), +// 'label_attr' => array( +// 'for' => 'chapo' +// ) +// )) +// ->add('postscriptum', 'text', array( +// 'constraints' => array( +// new NotBlank() +// ), +// 'label' => Translator::getInstance()->trans('Category picture postscriptum *'), +// 'label_attr' => array( +// 'for' => 'postscriptum' +// ) +// )) + + ; + } + + public function getName() + { + return 'thelia_category_picture_creation'; + } +} diff --git a/core/lib/Thelia/Model/CategoryImage.php b/core/lib/Thelia/Model/CategoryImage.php index 5bf964e10..aca77615a 100755 --- a/core/lib/Thelia/Model/CategoryImage.php +++ b/core/lib/Thelia/Model/CategoryImage.php @@ -25,4 +25,48 @@ class CategoryImage extends BaseCategoryImage return true; } + + /** + * Get picture absolute path + * + * @return null|string + */ + public function getAbsolutePath() + { + return null === $this->file + ? null + : $this->getUploadRootDir().'/'.$this->file; + } + + /** + * Get picture web path + * + * @return null|string + */ + public function getWebPath() + { + return null === $this->file + ? null + : $this->getUploadDir().'/'.$this->file; + } + + /** + * The absolute directory path where uploaded + * documents should be saved + * @return string + */ + protected function getUploadRootDir() + { + return __DIR__.'/../../../../../'.$this->getUploadDir(); + } + + /** + * Get rid of the __DIR__ so it doesn't screw up + * when displaying uploaded doc/image in the view. + * @return string + */ + protected function getUploadDir() + { + return 'local/media/images/category'; + } } diff --git a/templates/admin/default/category-edit.html b/templates/admin/default/category-edit.html index 723cf8242..6d1c4a9c2 100755 --- a/templates/admin/default/category-edit.html +++ b/templates/admin/default/category-edit.html @@ -246,6 +246,31 @@
+ image + {form name="thelia.admin.category.picture.creation"} + + {form_hidden_fields form=$form} + + + {form_field form=$form field='title'} +
+ + + {if $error}{$message}{/if} +
+ {/form_field} + + {form_field form=$form field='file'} +
+ + + {if $error}{$message}{/if} +
+ {/form_field} + + + + {/form}