update area postage

This commit is contained in:
Manuel Raynaud
2013-10-14 11:12:17 +02:00
parent 8d4fa3b29b
commit 52b68df4bc
4 changed files with 59 additions and 6 deletions

View File

@@ -25,6 +25,7 @@ namespace Thelia\Action;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Thelia\Core\Event\Area\AreaAddCountryEvent;
use Thelia\Core\Event\Area\AreaRemoveCountryEvent;
use Thelia\Core\Event\Area\AreaUpdatePostageEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Model\AreaQuery;
use Thelia\Model\CountryQuery;
@@ -57,6 +58,17 @@ class Area extends BaseAction implements EventSubscriberInterface
}
}
public function updatePostage(AreaUpdatePostageEvent $event)
{
if (null !== $area = AreaQuery::create()->findPk($event->getAreaId())) {
$area
->setPostage($event->getPostage())
->save();
$event->setArea($area);
}
}
/**
* Returns an array of event names this subscriber wants to listen to.
@@ -82,7 +94,8 @@ class Area extends BaseAction implements EventSubscriberInterface
{
return array(
TheliaEvents::AREA_ADD_COUNTRY => array('addCountry', 128),
TheliaEvents::AREA_REMOVE_COUNTRY => array('removeCountry', 128)
TheliaEvents::AREA_REMOVE_COUNTRY => array('removeCountry', 128),
TheliaEvents::AREA_POSTAGE_UPDATE => array('updatePostage', 128)
);
}
}

View File

@@ -28,10 +28,12 @@ use Thelia\Core\Event\Area\AreaCreateEvent;
use Thelia\Core\Event\Area\AreaDeleteEvent;
use Thelia\Core\Event\Area\AreaRemoveCountryEvent;
use Thelia\Core\Event\Area\AreaUpdateEvent;
use Thelia\Core\Event\Area\AreaUpdatePostageEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Form\Area\AreaCountryForm;
use Thelia\Form\Area\AreaCreateForm;
use Thelia\Form\Area\AreaModificationForm;
use Thelia\Form\Area\AreaPostageForm;
use Thelia\Form\Exception\FormValidationException;
use Thelia\Model\AreaQuery;
@@ -251,10 +253,6 @@ class AreaController extends AbstractCrudController
$this->adminLogAppend(sprintf("%s %s (ID %s) modified", ucfirst($this->objectName), $this->getObjectLabel($changedObject), $this->getObjectId($changedObject)));
}
if ($this->getRequest()->get('save_mode') == 'stay') {
$this->redirectToEditionTemplate($this->getRequest());
}
// Redirect to the success URL
$this->redirect($areaCountryForm->getSuccessUrl());
@@ -284,4 +282,45 @@ class AreaController extends AbstractCrudController
$this->redirectToEditionTemplate();
}
public function updatePostageAction()
{
if (null !== $response = $this->checkAuth($this->updatePermissionIdentifier)) return $response;
$areaUpdateForm = new AreaPostageForm($this->getRequest());
$error_msg = null;
try {
$form = $this->validateForm($areaUpdateForm);
$event = new AreaUpdatePostageEvent($form->get('area_id')->getData());
$event->setPostage($form->get('postage')->getData());
$this->dispatch(TheliaEvents::AREA_POSTAGE_UPDATE, $event);
if (! $this->eventContainsObject($event))
throw new \LogicException(
$this->getTranslator()->trans("No %obj was updated.", array('%obj', $this->objectName)));
// Log object modification
if (null !== $changedObject = $this->getObjectFromEvent($event)) {
$this->adminLogAppend(sprintf("%s %s (ID %s) modified", ucfirst($this->objectName), $this->getObjectLabel($changedObject), $this->getObjectId($changedObject)));
}
// Redirect to the success URL
$this->redirect($areaUpdateForm->getSuccessUrl());
} catch (FormValidationException $ex) {
// Form cannot be validated
$error_msg = $this->createStandardFormValidationErrorMessage($ex);
} catch (\Exception $ex) {
// Any other error
$error_msg = $ex->getMessage();
}
$this->setupFormErrorContext(
$this->getTranslator()->trans("%obj modification", array('%obj' => $this->objectName)), $error_msg, $areaUpdateForm);
// At this point, the form has errors, and should be redisplayed.
return $this->renderEditionTemplate();
}
}

View File

@@ -240,6 +240,7 @@ final class TheliaEvents
const AREA_ADD_COUNTRY = 'action.area.addCountry';
const AREA_REMOVE_COUNTRY = 'action.area.removeCountry';
const AREA_POSTAGE_UPDATE = 'action.area.postageUpdate';
// -- Categories Associated Content ----------------------------------------

View File

@@ -95,7 +95,7 @@
{form_field form=$form field='postage'}
<label for="{$label_attr.for}" class="control-label">{$label}</label>
<div class="input-group">
<input type="text" class="form-control" name="{$name}" value="{$value}" id="{$label_attr.for}" placeholder="Postage">
<input type="text" class="form-control" name="{$name}" value="{$POSTAGE}" id="{$label_attr.for}" placeholder="Postage">
<span class="input-group-addon">&euro;</span>
<span class="input-group-btn">
<button type="submit" class="btn btn-default btn-primary"><span class="glyphicon glyphicon-check"></span></button>