Files
le-matelot/local/modules/GiftOffered/Controller/GiftOfferedBackController.php
2020-01-27 08:56:08 +01:00

83 lines
2.4 KiB
PHP

<?php
namespace GiftOffered\Controller;
use GiftOffered\GiftOffered;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Thelia\Controller\Admin\BaseAdminController;
use Thelia\Core\Security\AccessManager;
use Thelia\Core\Security\Resource\AdminResources;
use Thelia\Form\Exception\FormValidationException;
use Thelia\Model\ConfigQuery;
use Thelia\Tools\URL;
/**
* Class Configuration
* @package GiftOffered\Form
* @author Frederic Faou <fredodefrance@gmail.com>
*/
class GiftOfferedBackController extends BaseAdminController
{
protected $request;
public function updateAction()
{
if (null !== $response = $this->checkAuth(AdminResources::MODULE, [GiftOffered::DOMAIN_NAME], AccessManager::UPDATE)) {
return $response;
}
$request = $this->getRequest();
// Create the form from the request
$form = $this->createForm("giftoffered.configuration.form");
// Initialize the potential exception
$ex = null;
try {
// Check the form against constraints violations
$validateForm = $this->validateForm($form);
// Get the form field values
$data = $validateForm->getData();
ConfigQuery::write('gifts_category', $data['category'], false, true);
ConfigQuery::write('gift_amount', $data['amount'], false, true);
// Redirect to the configuration page if everything is OK
return $this->redirectToConfigurationPage();
} catch (FormValidationException $ex) {
// Form cannot be validated. Create the error message using
// the BaseAdminController helper method.
$error_message = $this->createStandardFormValidationErrorMessage($ex);
}
catch (\Exception $ex) {
// Any other error
$error_message = $ex->getMessage();
}
if (null !== $ex) {
$this->setupFormErrorContext(
'GiftOffered configuration',
$error_message,
$form
);
$response = $this->render("module-configure", ['module_code' => 'GiftOffered']);
}
return $response;
}
/**
* Redirect to the configuration page
*/
protected function redirectToConfigurationPage()
{
return RedirectResponse::create(URL::getInstance()->absoluteUrl('/admin/module/GiftOffered'));
}
}