Merge branch 'master' into tax

This commit is contained in:
Etienne Roudeix
2013-10-11 11:13:41 +02:00
32 changed files with 932 additions and 131 deletions

View File

@@ -22,31 +22,249 @@
/*************************************************************************************/
namespace Thelia\Controller\Admin;
use Thelia\Core\Event\Country\CountryCreateEvent;
use Thelia\Core\Event\Country\CountryDeleteEvent;
use Thelia\Core\Event\Country\CountryToggleDefaultEvent;
use Thelia\Core\Event\Country\CountryUpdateEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Form\CountryCreationForm;
use Thelia\Form\CountryModificationForm;
use Thelia\Model\CountryQuery;
/**
* Class CustomerController
* @package Thelia\Controller\Admin
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
class CountryController extends BaseAdminController
class CountryController extends AbstractCrudController
{
public function indexAction()
/**
* @param string $objectName the lower case object name. Example. "message"
*
* @param string $defaultListOrder the default object list order, or null if list is not sortable. Example: manual
* @param string $orderRequestParameterName Name of the request parameter that set the list order (null if list is not sortable)
*
* @param string $viewPermissionIdentifier the 'view' permission identifier. Example: "admin.configuration.message.view"
* @param string $createPermissionIdentifier the 'create' permission identifier. Example: "admin.configuration.message.create"
* @param string $updatePermissionIdentifier the 'update' permission identifier. Example: "admin.configuration.message.update"
* @param string $deletePermissionIdentifier the 'delete' permission identifier. Example: "admin.configuration.message.delete"
*
* @param string $createEventIdentifier the dispatched create TheliaEvent identifier. Example: TheliaEvents::MESSAGE_CREATE
* @param string $updateEventIdentifier the dispatched update TheliaEvent identifier. Example: TheliaEvents::MESSAGE_UPDATE
* @param string $deleteEventIdentifier the dispatched delete TheliaEvent identifier. Example: TheliaEvents::MESSAGE_DELETE
*
* @param string $visibilityToggleEventIdentifier the dispatched visibility toggle TheliaEvent identifier, or null if the object has no visible options. Example: TheliaEvents::MESSAGE_TOGGLE_VISIBILITY
* @param string $changePositionEventIdentifier the dispatched position change TheliaEvent identifier, or null if the object has no position. Example: TheliaEvents::MESSAGE_UPDATE_POSITION
*/
public function __construct()
{
parent::__construct(
'country',
'manual',
'country_order',
'admin.country.default',
'admin.country.create',
'admin.country.update',
'admin.country.delete',
TheliaEvents::COUNTRY_CREATE,
TheliaEvents::COUNTRY_UPDATE,
TheliaEvents::COUNTRY_DELETE
);
}
/**
* Return the creation form for this object
*/
protected function getCreationForm()
{
return new CountryCreationForm($this->getRequest());
}
/**
* Return the update form for this object
*/
protected function getUpdateForm()
{
return new CountryModificationForm($this->getRequest());
}
/**
* Hydrate the update form for this object, before passing it to the update template
*
* @param \Thelia\Model\Country $object
*/
protected function hydrateObjectForm($object)
{
$data = array(
'id' => $object->getId(),
'locale' => $object->getLocale(),
'title' => $object->getTitle(),
'isocode' => $object->getIsocode(),
'isoalpha2' => $object->getIsoalpha2(),
'isoalpha3' => $object->getIsoalpha3(),
);
return new CountryModificationForm($this->getRequest(), 'form', $data);
}
/**
* Creates the creation event with the provided form data
*
* @param unknown $formData
*/
protected function getCreationEvent($formData)
{
$event = new CountryCreateEvent();
return $this->hydrateEvent($event, $formData);
}
/**
* Creates the update event with the provided form data
*
* @param unknown $formData
*/
protected function getUpdateEvent($formData)
{
$event = new CountryUpdateEvent();
return $this->hydrateEvent($event, $formData);
}
protected function hydrateEvent($event, $formData)
{
$event
->setLocale($formData['locale'])
->setTitle($formData['title'])
->setIsocode($formData['isocode'])
->setIsoAlpha2($formData['isoalpha2'])
->setIsoAlpha3($formData['isoalpha3'])
->setArea($formData['area'])
;
return $event;
}
/**
* Creates the delete event with the provided form data
*/
protected function getDeleteEvent()
{
return new CountryDeleteEvent($this->getRequest()->get('country_id'));
}
/**
* Return true if the event contains the object, e.g. the action has updated the object in the event.
*
* @param unknown $event
*/
protected function eventContainsObject($event)
{
return $event->hasCountry();
}
/**
* Get the created object from an event.
*
* @param unknown $createEvent
*/
protected function getObjectFromEvent($event)
{
return $event->getCountry();
}
/**
* Load an existing object from the database
*/
protected function getExistingObject()
{
return CountryQuery::create()
->joinWithI18n($this->getCurrentEditionLocale())
->findPk($this->getRequest()->get('country_id', 0));
}
/**
* Returns the object label form the object event (name, title, etc.)
*
* @param \Thelia\Model\Country $object
*/
protected function getObjectLabel($object)
{
return $object->getTitle();
}
/**
* Returns the object ID from the object
*
* @param \Thelia\Model\Country $object
*/
protected function getObjectId($object)
{
return $object->getId();
}
/**
* Render the main list template
*
* @param unknown $currentOrder, if any, null otherwise.
*/
protected function renderListTemplate($currentOrder)
{
if (null !== $response = $this->checkAuth("admin.country.view")) return $response;
return $this->render("countries", array("display_country" => 20));
}
/**
* update country action
*
* @param $country_id
* @return mixed|\Symfony\Component\HttpFoundation\Response
* Render the edition template
*/
public function updateAction($country_id)
protected function renderEditionTemplate()
{
return $this->render("country-edit", array(
"country_id" => $country_id
));
return $this->render('country-edit', $this->getEditionArgument());
}
protected function getEditionArgument()
{
return array(
'country_id' => $this->getRequest()->get('country_id', 0)
);
}
/**
* Redirect to the edition template
*/
protected function redirectToEditionTemplate()
{
$this->redirectToRoute('admin.configuration.countries.update', array(), $this->getRequest()->get('country_id', 0));
}
/**
* Redirect to the list template
*/
protected function redirectToListTemplate()
{
$this->redirectToRoute('admin.configuration.countries.default');
}
public function toggleDefaultAction()
{
if (null !== $response = $this->checkAuth($this->updatePermissionIdentifier)) return $response;
$content = null;
if (null !== $country_id = $this->getRequest()->get('country_id')) {
$toogleDefaultEvent = new CountryToggleDefaultEvent($country_id);
try {
$this->dispatch(TheliaEvents::COUNTRY_TOGGLE_DEFAULT, $toogleDefaultEvent);
if($toogleDefaultEvent->hasCountry()) {
return $this->nullResponse();
}
} catch (\Exception $ex) {
$content = $ex->getMessage();
}
}
return $this->nullResponse($content, 500);
}
}

View File

@@ -58,9 +58,9 @@ class BaseController extends ContainerAware
/**
* Return an empty response (after an ajax request, for example)
*/
protected function nullResponse()
protected function nullResponse($status = 200)
{
return new Response();
return new Response(null, $status);
}
/**