Merge branch 'master' into tax

This commit is contained in:
Etienne Roudeix
2013-10-23 16:32:10 +02:00
25 changed files with 1469 additions and 128 deletions

View File

@@ -0,0 +1,135 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Action;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Thelia\Core\Event\Lang\LangCreateEvent;
use Thelia\Core\Event\Lang\LangDefaultBehaviorEvent;
use Thelia\Core\Event\Lang\LangDeleteEvent;
use Thelia\Core\Event\Lang\LangToggleDefaultEvent;
use Thelia\Core\Event\Lang\LangUpdateEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Model\ConfigQuery;
use Thelia\Model\LangQuery;
use Thelia\Model\Lang as LangModel;
/**
* Class Lang
* @package Thelia\Action
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
class Lang extends BaseAction implements EventSubscriberInterface
{
public function update(LangUpdateEvent $event)
{
if (null !== $lang = LangQuery::create()->findPk($event->getId())) {
$lang->setDispatcher($this->getDispatcher());
$lang->setTitle($event->getTitle())
->setLocale($event->getLocale())
->setCode($event->getCode())
->setDateFormat($event->getDateFormat())
->setTimeFormat($event->getTimeFormat())
->save();
$event->setLang($lang);
}
}
public function toggleDefault(LangToggleDefaultEvent $event)
{
if (null !== $lang = LangQuery::create()->findPk($event->getLangId())) {
$lang->setDispatcher($this->getDispatcher());
$lang->toggleDefault();
$event->setLang($lang);
}
}
public function create(LangCreateEvent $event)
{
$lang = new LangModel();
$lang
->setDispatcher($this->getDispatcher())
->setTitle($event->getTitle())
->setCode($event->getCode())
->setLocale($event->getLocale())
->setDateFormat($event->getDateFormat())
->setTimeFormat($event->getTimeFormat())
->save();
$event->setLang($lang);
}
public function delete(LangDeleteEvent $event)
{
if (null !== $lang = LangQuery::create()->findPk($event->getLangId())) {
$lang->setDispatcher($this->getDispatcher())
->delete();
$event->setLang($lang);
}
}
public function defaultBehavior(LangDefaultBehaviorEvent $event)
{
ConfigQuery::create()
->filterByName('default_lang_without_translation')
->update(array('Value' => $event->getDefaultBehavior()));
}
/**
* Returns an array of event names this subscriber wants to listen to.
*
* The array keys are event names and the value can be:
*
* * The method name to call (priority defaults to 0)
* * An array composed of the method name to call and the priority
* * An array of arrays composed of the method names to call and respective
* priorities, or 0 if unset
*
* For instance:
*
* * array('eventName' => 'methodName')
* * array('eventName' => array('methodName', $priority))
* * array('eventName' => array(array('methodName1', $priority), array('methodName2'))
*
* @return array The event names to listen to
*
* @api
*/
public static function getSubscribedEvents()
{
return array(
TheliaEvents::LANG_UPDATE => array('update', 128),
TheliaEvents::LANG_TOGGLEDEFAULT => array('toggleDefault', 128),
TheliaEvents::LANG_CREATE => array('create', 128),
TheliaEvents::LANG_DELETE => array('delete', 128),
TheliaEvents::LANG_DEFAULTBEHAVIOR => array('defaultBehavior', 128)
);
}
}

View File

@@ -165,6 +165,11 @@
<argument type="service" id="service_container"/>
<tag name="kernel.event_subscriber"/>
</service>
<service id="thelia.action.lang" class="Thelia\Action\Lang">
<argument type="service" id="service_container"/>
<tag name="kernel.event_subscriber"/>
</service>
</services>
</config>

View File

@@ -141,8 +141,6 @@
<form name="thelia.admin.country.creation" class="Thelia\Form\CountryCreationForm"/>
<form name="thelia.admin.country.modification" class="Thelia\Form\CountryModificationForm"/>
<form name="thelia.admin.language.creation" class="Thelia\Form\LanguageCreationForm"/>
<form name="thelia.admin.area.create" class="Thelia\Form\Area\AreaCreateForm"/>
<form name="thelia.admin.area.modification" class="Thelia\Form\Area\AreaModificationForm"/>
@@ -154,6 +152,10 @@
<form name="thelia.contact" class="Thelia\Form\ContactForm"/>
<form name="thelia.newsletter" class="Thelia\Form\NewsletterForm"/>
<form name="thelia.lang.update" class="Thelia\Form\Lang\LangUpdateForm"/>
<form name="thelia.lang.create" class="Thelia\Form\Lang\LangCreateForm"/>
<form name="thelia.lang.defaultBehavior" class="Thelia\Form\Lang\LangDefaultBehaviorForm"/>
</forms>

View File

@@ -933,6 +933,39 @@
<!-- end tax rules management -->
<!-- language management -->
<route id="admin.configuration.languages" path="/admin/configuration/languages">
<default key="_controller">Thelia\Controller\Admin\LangController::defaultAction</default>
</route>
<route id="admin.configuration.languages.update" path="/admin/configuration/languages/update/{lang_id}">
<default key="_controller">Thelia\Controller\Admin\LangController::updateAction</default>
<requirement key="lang_id">\d+</requirement>
</route>
<route id="admin.configuration.languages.update.process" path="/admin/configuration/languages/save/{lang_id}">
<default key="_controller">Thelia\Controller\Admin\LangController::processUpdateAction</default>
<requirement key="lang_id">\d+</requirement>
</route>
<route id="admin.configuration.languages.toggleDefault" path="/admin/configuration/languages/toggleDefault/{lang_id}">
<default key="_controller">Thelia\Controller\Admin\LangController::toggleDefaultAction</default>
<requirement key="lang_id">\d+</requirement>
</route>
<route id="admin.configuration.languages.add" path="/admin/configuration/languages/add">
<default key="_controller">Thelia\Controller\Admin\LangController::addAction</default>
</route>
<route id="admin.configuration.languages.delete" path="/admin/configuration/languages/delete">
<default key="_controller">Thelia\Controller\Admin\LangController::deleteAction</default>
</route>
<route id="admin.configuration.languages.defaultBehavior" path="/admin/configuration/languages/defaultBehavior">
<default key="_controller">Thelia\Controller\Admin\LangController::defaultBehaviorAction</default>
</route>
<!-- The default route, to display a template -->

View File

@@ -97,14 +97,17 @@ class BaseAdminController extends BaseController
*
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function errorPage($message)
protected function errorPage($message, $status = 500)
{
if ($message instanceof \Exception) {
$message = $this->getTranslator()->trans("Sorry, an error occured: %msg", array('%msg' => $message->getMessage()));
}
return $this->render('general_error', array(
"error_message" => $message)
return $this->render('general_error',
array(
"error_message" => $message
),
$status
);
}
@@ -133,7 +136,7 @@ class BaseAdminController extends BaseController
// Generate the proper response
$response = new Response();
return $this->errorPage($this->getTranslator()->trans("Sorry, you're not allowed to perform this action"));
return $this->errorPage($this->getTranslator()->trans("Sorry, you're not allowed to perform this action"), 403);
}
/*
@@ -366,14 +369,13 @@ class BaseAdminController extends BaseController
* Render the given template, and returns the result as an Http Response.
*
* @param $templateName the complete template name, with extension
* @param array $args the template arguments
* @param array $args the template arguments
* @param int $status http code status
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function render($templateName, $args = array())
protected function render($templateName, $args = array(), $status = 200)
{
$response = new Response();
return $response->setContent($this->renderRaw($templateName, $args));
return Response::create($this->renderRaw($templateName, $args), $status);
}
/**
@@ -430,7 +432,7 @@ class BaseAdminController extends BaseController
Redirect::exec(URL::getInstance()->absoluteUrl($ex->getLoginTemplate()));
} catch (AuthorizationException $ex) {
// User is not allowed to perform the required action. Return the error page instead of the requested page.
return $this->errorPage($this->getTranslator()->trans("Sorry, you are not allowed to perform this action."));
return $this->errorPage($this->getTranslator()->trans("Sorry, you are not allowed to perform this action."), 403);
}
}
}

View File

@@ -251,6 +251,6 @@ class CountryController extends AbstractCrudController
}
return $this->nullResponse($content, 500);
return $this->nullResponse(500);
}
}

View File

@@ -0,0 +1,261 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Controller\Admin;
use Symfony\Component\Form\Form;
use Thelia\Core\Event\Lang\LangCreateEvent;
use Thelia\Core\Event\Lang\LangDefaultBehaviorEvent;
use Thelia\Core\Event\Lang\LangDeleteEvent;
use Thelia\Core\Event\Lang\LangToggleDefaultEvent;
use Thelia\Core\Event\Lang\LangUpdateEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Core\Security\AccessManager;
use Thelia\Core\Security\Resource\AdminResources;
use Thelia\Form\Exception\FormValidationException;
use Thelia\Form\Lang\LangCreateForm;
use Thelia\Form\Lang\LangDefaultBehaviorForm;
use Thelia\Form\Lang\LangUpdateForm;
use Thelia\Model\ConfigQuery;
use Thelia\Model\LangQuery;
/**
* Class LangController
* @package Thelia\Controller\Admin
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
class LangController extends BaseAdminController
{
public function defaultAction()
{
if (null !== $response = $this->checkAuth(AdminResources::LANGUAGE, AccessManager::VIEW)) return $response;
return $this->renderDefault();
}
public function renderDefault(array $param = array())
{
return $this->render('languages', array_merge($param, array(
'lang_without_translation' => ConfigQuery::getDefaultLangWhenNoTranslationAvailable(),
'one_domain_per_lang' => ConfigQuery::read("one_domain_foreach_lang", false)
)));
}
public function updateAction($lang_id)
{
if (null !== $response = $this->checkAuth(AdminResources::LANGUAGE, AccessManager::UPDATE)) return $response;
$this->checkXmlHttpRequest();
$lang = LangQuery::create()->findPk($lang_id);
$langForm = new LangUpdateForm($this->getRequest(), 'form', array(
'id' => $lang->getId(),
'title' => $lang->getTitle(),
'code' => $lang->getCode(),
'locale' => $lang->getLocale(),
'date_format' => $lang->getDateFormat(),
'time_format' => $lang->getTimeFormat()
));
$this->getParserContext()->addForm($langForm);
return $this->render('ajax/language-update-modal', array(
'lang_id' => $lang_id
));
}
public function processUpdateAction($lang_id)
{
if (null !== $response = $this->checkAuth(AdminResources::LANGUAGE, AccessManager::UPDATE)) return $response;
$error_msg = false;
$langForm = new LangUpdateForm($this->getRequest());
try {
$form = $this->validateForm($langForm);
$event = new LangUpdateEvent($form->get('id')->getData());
$event = $this->hydrateEvent($event, $form);
$this->dispatch(TheliaEvents::LANG_UPDATE, $event);
if (false === $event->hasLang()) {
throw new \LogicException(
$this->getTranslator()->trans("No %obj was updated.", array('%obj', 'Lang')));
}
$changedObject = $event->getLang();
$this->adminLogAppend(sprintf("%s %s (ID %s) modified", 'Lang', $changedObject->getTitle(), $changedObject->getId()));
$this->redirectToRoute('/admin/configuration/languages');
} catch(\Exception $e) {
$error_msg = $e->getMessage();
}
return $this->renderDefault();
}
protected function hydrateEvent($event,Form $form)
{
return $event
->setTitle($form->get('title')->getData())
->setCode($form->get('code')->getData())
->setLocale($form->get('locale')->getData())
->setDateFormat($form->get('date_format')->getData())
->setTimeFormat($form->get('time_format')->getData())
;
}
public function toggleDefaultAction($lang_id)
{
if (null !== $response = $this->checkAuth(AdminResources::LANGUAGE, AccessManager::UPDATE)) return $response;
$this->checkXmlHttpRequest();
$error = false;
try {
$event = new LangToggleDefaultEvent($lang_id);
$this->dispatch(TheliaEvents::LANG_TOGGLEDEFAULT, $event);
if (false === $event->hasLang()) {
throw new \LogicException(
$this->getTranslator()->trans("No %obj was updated.", array('%obj', 'Lang')));
}
$changedObject = $event->getLang();
$this->adminLogAppend(sprintf("%s %s (ID %s) modified", 'Lang', $changedObject->getTitle(), $changedObject->getId()));
} catch (\Exception $e) {
\Thelia\Log\Tlog::getInstance()->error(sprintf("Error on changing default languages with message : %s", $e->getMessage()));
$error = $e->getMessage();
}
if($error) {
return $this->nullResponse(500);
} else {
return $this->nullResponse();
}
}
public function addAction()
{
if (null !== $response = $this->checkAuth(AdminResources::LANGUAGE, AccessManager::CREATE)) return $response;
$createForm = new LangCreateForm($this->getRequest());
$error_msg = false;
try {
$form = $this->validateForm($createForm);
$createEvent = new LangCreateEvent();
$createEvent = $this->hydrateEvent($createEvent, $form);
$this->dispatch(TheliaEvents::LANG_CREATE, $createEvent);
if (false === $createEvent->hasLang()) {
throw new \LogicException(
$this->getTranslator()->trans("No %obj was updated.", array('%obj', 'Lang')));
}
$createdObject = $createEvent->getLang();
$this->adminLogAppend(sprintf("%s %s (ID %s) created", 'Lang', $createdObject->getTitle(), $createdObject->getId()));
$this->redirectToRoute('admin.configuration.languages');
} 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 creation", array('%obj' => 'Lang')), $error_msg, $createForm, $ex);
// At this point, the form has error, and should be redisplayed.
return $this->renderDefault();
}
public function deleteAction()
{
if (null !== $response = $this->checkAuth(AdminResources::LANGUAGE, AccessManager::DELETE)) return $response;
$error_msg = false;
try {
$deleteEvent = new LangDeleteEvent($this->getRequest()->get('language_id', 0));
$this->dispatch(TheliaEvents::LANG_DELETE, $deleteEvent);
$this->redirectToRoute('admin.configuration.languages');
} catch (\Exception $ex) {
\Thelia\Log\Tlog::getInstance()->error(sprintf("error during language removal with message : %s", $ex->getMessage()));
$error_msg = $ex->getMessage();
}
return $this->renderDefault(array(
'error_delete_message' => $error_msg
));
}
public function defaultBehaviorAction()
{
if (null !== $response = $this->checkAuth(AdminResources::LANGUAGE, AccessManager::UPDATE)) return $response;
$error_msg = false;
$exception = false;
$behaviorForm = new LangDefaultBehaviorForm($this->getRequest());
try {
$form = $this->validateForm($behaviorForm);
$event = new LangDefaultBehaviorEvent($form->get('behavior')->getData());
$this->dispatch(TheliaEvents::LANG_DEFAULTBEHAVIOR, $event);
$this->redirectToRoute('admin.configuration.languages');
} 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 creation", array('%obj' => 'Lang')), $error_msg, $behaviorForm, $ex);
// At this point, the form has error, and should be redisplayed.
return $this->renderDefault();
}
}

View File

@@ -57,8 +57,10 @@ class BaseController extends ContainerAware
/**
* Return an empty response (after an ajax request, for example)
* @param int $status
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function nullResponse($content = null, $status = 200)
protected function nullResponse($status = 200)
{
return new Response(null, $status);
}

View File

@@ -0,0 +1,141 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Core\Event\Lang;
/**
* Class LangCreateEvent
* @package Thelia\Core\Event\Lang
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
class LangCreateEvent extends LangEvent
{
protected $title;
protected $code;
protected $locale;
protected $date_format;
protected $time_format;
/**
* @param mixed $code
*
* @return $this
*/
public function setCode($code)
{
$this->code = $code;
return $this;
}
/**
* @return mixed
*/
public function getCode()
{
return $this->code;
}
/**
* @param mixed $date_format
*
* @return $this
*/
public function setDateFormat($date_format)
{
$this->date_format = $date_format;
return $this;
}
/**
* @return mixed
*/
public function getDateFormat()
{
return $this->date_format;
}
/**
* @param mixed $locale
*
* @return $this
*/
public function setLocale($locale)
{
$this->locale = $locale;
return $this;
}
/**
* @return mixed
*/
public function getLocale()
{
return $this->locale;
}
/**
* @param mixed $time_format
*
* @return $this
*/
public function setTimeFormat($time_format)
{
$this->time_format = $time_format;
return $this;
}
/**
* @return mixed
*/
public function getTimeFormat()
{
return $this->time_format;
}
/**
* @param mixed $title
*
* @return $this
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* @return mixed
*/
public function getTitle()
{
return $this->title;
}
}

View File

@@ -0,0 +1,64 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Core\Event\Lang;
use Thelia\Core\Event\ActionEvent;
/**
* Class LangDefaultBehaviorEvent
* @package Thelia\Core\Event\Lang
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
class LangDefaultBehaviorEvent extends ActionEvent
{
/**
* @var int default behavior status
*/
protected $defaultBehavior;
function __construct($defaultBehavior)
{
$this->defaultBehavior = $defaultBehavior;
}
/**
* @param int $defaultBehavior
*/
public function setDefaultBehavior($defaultBehavior)
{
$this->defaultBehavior = $defaultBehavior;
}
/**
* @return int
*/
public function getDefaultBehavior()
{
return $this->defaultBehavior;
}
}

View File

@@ -0,0 +1,67 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Core\Event\Lang;
/**
* Class LangDeleteEvent
* @package Thelia\Core\Event\Lang
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
class LangDeleteEvent extends LangEvent
{
/**
* @var int
*/
protected $lang_id;
/**
* @param int $lang_id
*/
function __construct($lang_id)
{
$this->lang_id = $lang_id;
}
/**
* @param int $lang_id
*
* @return $this
*/
public function setLangId($lang_id)
{
$this->lang_id = $lang_id;
return $this;
}
/**
* @return int
*/
public function getLangId()
{
return $this->lang_id;
}
}

View File

@@ -0,0 +1,76 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Core\Event\Lang;
use Thelia\Core\Event\ActionEvent;
use Thelia\Model\Lang;
/**
* Class LangEvent
* @package Thelia\Core\Event\Lang
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
class LangEvent extends ActionEvent
{
/**
* @var \Thelia\Model\Lang
*/
protected $lang;
function __construct(Lang $lang = null)
{
$this->lang = $lang;
}
/**
* @param \Thelia\Model\Lang $lang
*/
public function setLang(Lang $lang)
{
$this->lang = $lang;
}
/**
* @return \Thelia\Model\Lang
*/
public function getLang()
{
return $this->lang;
}
/**
*
* check if lang object is present
*
* @return bool
*/
public function hasLang()
{
return null !== $this->lang;
}
}

View File

@@ -0,0 +1,69 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Core\Event\Lang;
/**
* Class LangToggleDefaultEvent
* @package Thelia\Core\Event\Lang
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
class LangToggleDefaultEvent extends LangEvent
{
/**
* @var int
*/
protected $lang_id;
/**
* @param int $lang_id
*/
function __construct($lang_id)
{
$this->lang_id = $lang_id;
}
/**
* @param int $lang_id
*
* @return $this
*/
public function setLangId($lang_id)
{
$this->lang_id = $lang_id;
return $this;
}
/**
* @return int
*/
public function getLangId()
{
return $this->lang_id;
}
}

View File

@@ -0,0 +1,69 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Core\Event\Lang;
/**
* Class LangUpdateEvent
* @package Thelia\Core\Event\Lang
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
class LangUpdateEvent extends LangCreateEvent
{
/**
* @var int lang id
*/
protected $id;
/**
* @param int $id
*/
function __construct($id)
{
$this->id = $id;
}
/**
* @param int $id
*
* @return $this
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* @return int
*/
public function getId()
{
return $this->id;
}
}

View File

@@ -694,4 +694,23 @@ final class TheliaEvents
* sent for subscribing to the newsletter
*/
const NEWSLETTER_SUBSCRIBE = 'thelia.newsletter.subscribe';
/************ LANG MANAGEMENT ****************************/
const LANG_UPDATE = 'action.lang.update';
const LANG_CREATE = 'action.lang.create';
const LANG_DELETE = 'action.lang.delete';
const LANG_DEFAULTBEHAVIOR = 'action.lang.defaultBehavior';
const LANG_TOGGLEDEFAULT = 'action.lang.toggleDefault';
const BEFORE_UPDATELANG = 'action.lang.beforeUpdate';
const AFTER_UPDATELANG = 'action.lang.afterUpdate';
const BEFORE_CREATELANG = 'action.lang.beforeCreate';
const AFTER_CREATELANG = 'action.lang.afterCreate';
const BEFORE_DELETELANG = 'action.lang.beforeDelete';
const AFTER_DELETELANG = 'action.lang.afterDelete';
}

View File

@@ -99,7 +99,8 @@ class Lang extends BaseLoop
->set("LOCALE", $result->getLocale())
->set("URL", $result->getUrl())
->set("IS_DEFAULT", $result->getByDefault())
->set("URL", $result->getUrl())
->set("DATE_FORMAT", $result->getDateFormat())
->set("TIME_FORMAT", $result->getTimeFormat())
->set("POSITION", $result->getPosition())
;

View File

@@ -0,0 +1,116 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Form\Lang;
use Symfony\Component\Validator\Constraints\NotBlank;
use Thelia\Form\BaseForm;
use Thelia\Core\Translation\Translator;
/**
* Class LangCreateForm
* @package Thelia\Form\Lang
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
class LangCreateForm extends BaseForm
{
/**
*
* in this function you add all the fields you need for your Form.
* Form this you have to call add method on $this->formBuilder attribute :
*
* $this->formBuilder->add("name", "text")
* ->add("email", "email", array(
* "attr" => array(
* "class" => "field"
* ),
* "label" => "email",
* "constraints" => array(
* new \Symfony\Component\Validator\Constraints\NotBlank()
* )
* )
* )
* ->add('age', 'integer');
*
* @return null
*/
protected function buildForm()
{
$this->formBuilder
->add('title', 'text', array(
'constraints' => array(
new NotBlank()
),
'label' => Translator::getInstance()->trans('Language name'),
'label_attr' => array(
'for' => 'title_lang'
)
))
->add('code', 'text', array(
'constraints' => array(
new NotBlank()
),
'label' => Translator::getInstance()->trans('ISO 639 Code'),
'label_attr' => array(
'for' => 'code_lang'
)
))
->add('locale', 'text', array(
'constraints' => array(
new NotBlank()
),
'label' => Translator::getInstance()->trans('language locale'),
'label_attr' => array(
'for' => 'locale_lang'
)
))
->add('date_format', 'text', array(
'constraints' => array(
new NotBlank()
),
'label' => Translator::getInstance()->trans('date format'),
'label_attr' => array(
'for' => 'date_lang'
)
))
->add('time_format', 'text', array(
'constraints' => array(
new NotBlank()
),
'label' => Translator::getInstance()->trans('time format'),
'label_attr' => array(
'for' => 'time_lang'
)
))
;
}
/**
* @return string the name of you form. This name must be unique
*/
public function getName()
{
return 'thelia_language_create';
}
}

View File

@@ -0,0 +1,85 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Form\Lang;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Range;
use Thelia\Form\BaseForm;
use Thelia\Core\Translation\Translator;
/**
* Class LangDefaultBehaviorForm
* @package Thelia\Form\Lang
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
class LangDefaultBehaviorForm extends BaseForm
{
/**
*
* in this function you add all the fields you need for your Form.
* Form this you have to call add method on $this->formBuilder attribute :
*
* $this->formBuilder->add("name", "text")
* ->add("email", "email", array(
* "attr" => array(
* "class" => "field"
* ),
* "label" => "email",
* "constraints" => array(
* new \Symfony\Component\Validator\Constraints\NotBlank()
* )
* )
* )
* ->add('age', 'integer');
*
* @return null
*/
protected function buildForm()
{
$this->formBuilder
->add('behavior', 'choice', array(
'choices' => array(
0 => Translator::getInstance()->trans("Strictly use the requested language"),
1 => Translator::getInstance()->trans("Replace by the default language"),
),
'constraints' => array(
new NotBlank()
),
'label' => Translator::getInstance()->trans("If a translation is missing or incomplete :"),
'label_attr' => array(
'for' => 'defaultBehavior-form'
)
));
}
/**
* @return string the name of you form. This name must be unique
*/
public function getName()
{
return 'thelia_lang_defaultBehavior';
}
}

View File

@@ -20,39 +20,35 @@
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Form;
namespace Thelia\Form\Lang;
use Symfony\Component\Validator\Constraints\GreaterThan;
use Symfony\Component\Validator\Constraints\NotBlank;
use Thelia\Core\Translation\Translator;
class LanguageCreationForm extends BaseForm
/**
* Class LangUpdateForm
* @package Thelia\Form\Lang
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
class LangUpdateForm extends LangCreateForm
{
protected function buildForm()
public function buildForm()
{
parent::buildForm();
$this->formBuilder
->add("title", "text", array(
"constraints" => array(
new NotBlank()
),
"label" => Translator::getInstance()->trans("Language title *"),
"label_attr" => array(
"for" => "title"
->add('id', 'hidden', array(
'constraints' => array(
new NotBlank(),
new GreaterThan(array('value' => 0))
)
))
->add("isocode", "text", array(
"constraints" => array(
new NotBlank()
),
"label" => Translator::getInstance()->trans("ISO Code *"),
"label_attr" => array(
"for" => "isocode"
)
))
;
));
}
public function getName()
{
return "thelia_language_creation";
return 'thelia_lang_update';
}
}
}

View File

@@ -3,9 +3,12 @@
namespace Thelia\Model;
use Propel\Runtime\Connection\ConnectionInterface;
use Propel\Runtime\Exception\PropelException;
use Propel\Runtime\Propel;
use Thelia\Core\Event\Country\CountryEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Model\Base\Country as BaseCountry;
use Thelia\Model\Map\CountryTableMap;
class Country extends BaseCountry
{
@@ -16,13 +19,25 @@ class Country extends BaseCountry
if($this->getId() === null) {
throw new \RuntimeException("impossible to just uncheck default country, choose a new one");
}
CountryQuery::create()
->filterByByDefault(1)
->update(array('ByDefault' => 0));
$this
->setByDefault(1)
->save();
$con = Propel::getWriteConnection(CountryTableMap::DATABASE_NAME);
$con->beginTransaction();
try {
CountryQuery::create()
->filterByByDefault(1)
->update(array('ByDefault' => 0), $con);
$this
->setByDefault(1)
->save($con);
$con->commit();
} catch(PropelException $e) {
$con->rollBack();
throw $e;
}
}
public function preInsert(ConnectionInterface $con = null)

View File

@@ -2,10 +2,18 @@
namespace Thelia\Model;
use Propel\Runtime\Connection\ConnectionInterface;
use Propel\Runtime\Exception\PropelException;
use Propel\Runtime\Propel;
use Thelia\Core\Event\Lang\LangEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Model\Base\Lang as BaseLang;
use Thelia\Model\Base\LangQuery;
use Thelia\Model\Map\LangTableMap;
class Lang extends BaseLang {
use \Thelia\Model\Tools\ModelEventDispatcherTrait;
/**
* Return the default language object, using a local variable to cache it.
*
@@ -20,4 +28,65 @@ class Lang extends BaseLang {
return $default_lang;
}
public function toggleDefault()
{
if($this->getId() === null) {
throw new \RuntimeException("impossible to just uncheck default language, choose a new one");
}
$con = Propel::getWriteConnection(LangTableMap::DATABASE_NAME);
$con->beginTransaction();
try {
LangQuery::create()
->filterByByDefault(1)
->update(array('ByDefault' => 0), $con);
$this
->setByDefault(1)
->save($con);
$con->commit();
} catch(PropelException $e) {
$con->rollBack();
throw $e;
}
}
public function preInsert(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::BEFORE_CREATELANG, new LangEvent($this));
return true;
}
public function postInsert(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::AFTER_CREATELANG, new LangEvent($this));
}
public function preUpdate(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::BEFORE_UPDATELANG, new LangEvent($this));
return true;
}
public function postUpdate(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::AFTER_UPDATELANG, new LangEvent($this));
}
public function preDelete(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::BEFORE_DELETELANG, new LangEvent($this));
return true;
}
public function postDelete(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::AFTER_DELETELANG, new LangEvent($this));
}
}