Merge branch 'master' into rewrite

Conflicts:
	core/lib/Thelia/Controller/BaseController.php
	install/insert.sql
This commit is contained in:
Etienne Roudeix
2013-09-04 10:06:22 +02:00
27 changed files with 472 additions and 112 deletions

View File

@@ -34,6 +34,8 @@ use Thelia\Core\Event\TheliaEvents;
use Thelia\Core\Event\CurrencyChangeEvent;
use Thelia\Core\Event\CurrencyCreateEvent;
use Thelia\Core\Event\CurrencyDeleteEvent;
use Thelia\Model\Map\CurrencyTableMap;
use Thelia\Model\ConfigQuery;
class Currency extends BaseAction implements EventSubscriberInterface
{
@@ -53,11 +55,12 @@ class Currency extends BaseAction implements EventSubscriberInterface
->setName($event->getCurrencyName())
->setSymbol($event->getSymbol())
->setRate($event->getRate())
->setCode($event->getCode())
->setCode(strtoupper($event->getCode()))
->save()
;
$event->setCurrency($currency);
}
@@ -79,7 +82,7 @@ class Currency extends BaseAction implements EventSubscriberInterface
->setName($event->getCurrencyName())
->setSymbol($event->getSymbol())
->setRate($event->getRate())
->setCode($event->getCode())
->setCode(strtoupper($event->getCode()))
->save();
@@ -87,6 +90,32 @@ class Currency extends BaseAction implements EventSubscriberInterface
}
}
/**
* Set the default currency
*
* @param CurrencyChangeEvent $event
*/
public function setDefault(CurrencyChangeEvent $event)
{
$search = CurrencyQuery::create();
if (null !== $currency = CurrencyQuery::create()->findOneById($event->getCurrencyId())) {
if ($currency->getByDefault() != $event->getIsDefault()) {
// Reset default status
CurrencyQuery::create()->filterByByDefault(true)->update(array('ByDefault' => false));
$currency
->setByDefault($event->getIsDefault())
->save()
;
}
$event->setCurrency($currency);
}
}
/**
* Delete a currencyuration entry
*
@@ -106,15 +135,41 @@ class Currency extends BaseAction implements EventSubscriberInterface
}
}
public function updateRates() {
$rates_url = ConfigQuery::read('currency_rate_update_url', 'http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml');
$rate_data = file_get_contents($rates_url);
if ($rate_data && $sxe = new \SimpleXMLElement($rate_data)) {
foreach ($sxe->Cube[0]->Cube[0]->Cube as $last)
{
$code = strtoupper($last["currency"]);
$rate = floatval($last['rate']);
if (null !== $currency = CurrencyQuery::create()->findOneByCode($code)) {
$currency->setRate($rate)->save();
}
}
}
else {
throw new \RuntimeException(sprintf("Failed to get currency rates data from URL %s", $url));
}
}
/**
* {@inheritDoc}
*/
public static function getSubscribedEvents()
{
return array(
TheliaEvents::CURRENCY_CREATE => array("create", 128),
TheliaEvents::CURRENCY_MODIFY => array("modify", 128),
TheliaEvents::CURRENCY_DELETE => array("delete", 128),
TheliaEvents::CURRENCY_CREATE => array("create", 128),
TheliaEvents::CURRENCY_MODIFY => array("modify", 128),
TheliaEvents::CURRENCY_DELETE => array("delete", 128),
TheliaEvents::CURRENCY_SET_DEFAULT => array("setDefault", 128),
TheliaEvents::CURRENCY_UPDATE_RATES => array("updateRates", 128),
);
}
}

View File

@@ -101,6 +101,14 @@
<default key="_controller">Thelia\Controller\Admin\CurrencyController::saveChangeAction</default>
</route>
<route id="admin.configuration.currencies.set-default" path="/admin/configuration/currencies/set-default">
<default key="_controller">Thelia\Controller\Admin\CurrencyController::setDefaultAction</default>
</route>
<route id="admin.configuration.currencies.update-rates" path="/admin/configuration/currencies/update-rates">
<default key="_controller">Thelia\Controller\Admin\CurrencyController::updateRatesAction</default>
</route>
<route id="admin.configuration.currencies.delete" path="/admin/configuration/currencies/delete">
<default key="_controller">Thelia\Controller\Admin\CurrencyController::deleteAction</default>
</route>

View File

@@ -150,6 +150,27 @@ class BaseAdminController extends BaseController
return $this->container->get('http_kernel')->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
}
/**
* Return the route path defined for the givent route ID
*
* @param string $routeId a route ID, as defines in Config/Resources/routing/admin.xml
*
* @see \Thelia\Controller\BaseController::getRouteFromRouter()
*/
protected function getRoute($routeId) {
return $this->getRouteFromRouter('router.admin', $routeId);
}
/**
* Redirect to à route ID related URL
*
* @param unknown $routeId the route ID, as found in Config/Resources/routing/admin.xml
* @param unknown $urlParameters the URL parametrs, as a var/value pair array
*/
public function redirectToRoute($routeId, $urlParameters = array()) {
$this->redirect(URL::absoluteUrl($this->getRoute($routeId), $urlParameters));
}
/**
* Get the current edition lang ID, checking if a change was requested in the current request
*/

View File

@@ -239,10 +239,11 @@ class ConfigController extends BaseAdminController
// If we have to stay on the same page, do not redirect to the succesUrl,
// just redirect to the edit page again.
if ($this->getRequest()->get('save_mode') == 'stay') {
$this->redirect(URL::absoluteUrl(
"admin/configuration/variables/change",
$this->redirectToRoute(
"admin.configuration.variables.change",
array('variable_id' => $variable_id)
));
);
}
// Redirect to the success URL
@@ -295,7 +296,7 @@ class ConfigController extends BaseAdminController
$this->dispatch(TheliaEvents::CONFIG_SETVALUE, $event);
}
$this->redirect(URL::absoluteUrl('/admin/configuration/variables'));
$this->redirectToRoute('admin.configuration.variables.default');
}
/**
@@ -313,6 +314,6 @@ class ConfigController extends BaseAdminController
$this->dispatch(TheliaEvents::CONFIG_DELETE, $event);
$this->redirect(URL::absoluteUrl('/admin/configuration/variables'));
$this->redirectToRoute('admin.configuration.variables.default');
}
}

View File

@@ -83,7 +83,7 @@ class CurrencyController extends BaseAdminController
// Check current user authorization
if (null !== $response = $this->checkAuth("admin.configuration.currencies.create")) return $response;
$currency = false;
$error_msg = false;
// Create the Creation Form
$creationForm = new CurrencyCreationForm($this->getRequest());
@@ -120,24 +120,24 @@ class CurrencyController extends BaseAdminController
}
catch (FormValidationException $ex) {
// Form cannot be validated
$currency = sprintf("Please check your input: %s", $ex->getCurrency());
$error_msg = sprintf("Please check your input: %s", $ex->getMessage());
}
catch (\Exception $ex) {
// Any other error
$currency = sprintf("Sorry, an error occured: %s", $ex->getCurrency());
$error_msg = sprintf("Sorry, an error occured: %s", $ex->getMessage());
}
if ($currency !== false) {
if ($error_msg !== false) {
// An error has been detected: log it
Tlog::getInstance()->error(sprintf("Error during currency creation process : %s. Exception was %s", $currency, $ex->getCurrency()));
Tlog::getInstance()->error(sprintf("Error during currency creation process : %s. Exception was %s", $error_msg, $ex->getMessage()));
// Mark the form as errored
$creationForm->setErrorCurrency($currency);
$creationForm->setErrorMessage($error_msg);
// Pass it to the parser, along with the error currency
$this->getParserContext()
->addForm($creationForm)
->setGeneralError($currency)
->setGeneralError($error_msg)
;
}
@@ -169,7 +169,7 @@ class CurrencyController extends BaseAdminController
'locale' => $currency->getLocale(),
'code' => $currency->getCode(),
'symbol' => $currency->getSymbol(),
'rate' => $currency->getSubject()
'rate' => $currency->getRate()
);
// Setup the object form
@@ -193,7 +193,7 @@ class CurrencyController extends BaseAdminController
// Check current user authorization
if (null !== $response = $this->checkAuth("admin.configuration.currencies.change")) return $response;
$currency = false;
$error_msg = false;
// Create the form from the request
$changeForm = new CurrencyModificationForm($this->getRequest());
@@ -230,10 +230,10 @@ class CurrencyController extends BaseAdminController
// If we have to stay on the same page, do not redirect to the succesUrl,
// just redirect to the edit page again.
if ($this->getRequest()->get('save_mode') == 'stay') {
$this->redirect(URL::absoluteUrl(
"admin/configuration/currencies/change",
$this->redirectToRoute(
"admin.configuration.currencies.change",
array('currency_id' => $currency_id)
));
);
}
// Redirect to the success URL
@@ -241,24 +241,24 @@ class CurrencyController extends BaseAdminController
}
catch (FormValidationException $ex) {
// Invalid data entered
$currency = sprintf("Please check your input: %s", $ex->getCurrency());
$error_msg = sprintf("Please check your input: %s", $ex->getMessage());
}
catch (\Exception $ex) {
// Any other error
$currency = sprintf("Sorry, an error occured: %s", $ex->getCurrency());
$error_msg = sprintf("Sorry, an error occured: %s", $ex->getMessage());
}
if ($currency !== false) {
if ($error_msg !== false) {
// Log error currency
Tlog::getInstance()->error(sprintf("Error during currency modification process : %s. Exception was %s", $currency, $ex->getCurrency()));
Tlog::getInstance()->error(sprintf("Error during currency modification process : %s. Exception was %s", $error_msg, $ex->getMessage()));
// Mark the form as errored
$changeForm->setErrorCurrency($currency);
$changeForm->setErrorMessage($error_msg);
// Pas the form and the error to the parser
$this->getParserContext()
->addForm($changeForm)
->setGeneralError($currency)
->setGeneralError($error_msg)
;
}
@@ -266,6 +266,47 @@ class CurrencyController extends BaseAdminController
return $this->render('currency-edit', array('currency_id' => $currency_id));
}
/**
* Sets the default currency
*/
public function setDefaultAction() {
// Check current user authorization
if (null !== $response = $this->checkAuth("admin.configuration.currencies.change")) return $response;
$changeEvent = new CurrencyChangeEvent($this->getRequest()->get('currency_id', 0));
// Create and dispatch the change event
$changeEvent->setIsDefault(true);
try {
$this->dispatch(TheliaEvents::CURRENCY_SET_DEFAULT, $changeEvent);
}
catch (\Exception $ex) {
// Any error
return $this->errorPage(sprintf("Sorry, an error occured: %s", $ex->getMessage()));
}
$this->redirectToRoute('admin.configuration.currencies.default');
}
/**
* Update currencies rates
*/
public function updateRatesAction() {
// Check current user authorization
if (null !== $response = $this->checkAuth("admin.configuration.currencies.change")) return $response;
try {
$this->dispatch(TheliaEvents::CURRENCY_UPDATE_RATES);
}
catch (\Exception $ex) {
// Any error
return $this->errorPage(sprintf("Sorry, an error occured: %s", $ex->getMessage()));
}
$this->redirectToRoute('admin.configuration.currencies.default');
}
/**
* Delete a currency object
*
@@ -281,6 +322,6 @@ class CurrencyController extends BaseAdminController
$this->dispatch(TheliaEvents::CURRENCY_DELETE, $event);
$this->redirect(URL::adminViewUrl('currencies'));
$this->redirectToRoute('admin.configuration.currencies.default');
}
}

View File

@@ -214,10 +214,10 @@ class MessageController extends BaseAdminController
// If we have to stay on the same page, do not redirect to the succesUrl,
// just redirect to the edit page again.
if ($this->getRequest()->get('save_mode') == 'stay') {
$this->redirect(URL::absoluteUrl(
"admin/configuration/messages/change",
$this->redirectToRoute(
"admin.configuration.messages.change",
array('message_id' => $message_id)
));
);
}
// Redirect to the success URL

View File

@@ -46,7 +46,7 @@ class SessionController extends BaseAdminController
$this->getSecurityContext()->clearAdminUser();
// Go back to login page.
return Redirect::exec(URL::absoluteUrl('/admin/login')); // FIXME - should be a parameter
$this->redirectToRoute('admin.login');
}
public function checkLoginAction()

View File

@@ -31,14 +31,13 @@ use Thelia\Tools\Redirect;
use Thelia\Core\Template\ParserContext;
use Thelia\Core\Event\ActionEvent;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Thelia\Core\Factory\ActionEventFactory;
use Thelia\Form\BaseForm;
use Thelia\Form\Exception\FormValidationException;
use Symfony\Component\EventDispatcher\Event;
use Thelia\Core\Event\DefaultActionEvent;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
*
* The defaut administration controller. Basically, display the login form if
@@ -203,6 +202,25 @@ class BaseController extends ContainerAware
if (null !== $url) $this->redirect($url);
}
/**
* Get a route path from the route id.
*
* @param $routerName
* @param $routeId
*
* @return mixed
* @throws InvalidArgumentException
*/
protected function getRouteFromRouter($routerName, $routeId) {
$route = $this->container->get($routerName)->getRouteCollection()->get($routeId);
if ($route == null) {
throw new InvalidArgumentException(sprintf("Route ID '%s' does not exists.", $routeId));
}
return $route->getPath();
}
/**
* Return a 404 error
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
@@ -211,4 +229,4 @@ class BaseController extends ContainerAware
{
throw new NotFoundHttpException();
}
}
}

View File

@@ -26,4 +26,24 @@ use Thelia\Controller\BaseController;
class BaseFrontController extends BaseController
{
/**
* Return the route path defined for the givent route ID
*
* @param string $routeId a route ID, as defines in Config/Resources/routing/front.xml
*
* @see \Thelia\Controller\BaseController::getRouteFromRouter()
*/
protected function getRoute($routeId) {
return $this->getRouteFromRouter('router.front', $routeId);
}
/**
* Redirect to à route ID related URL
*
* @param unknown $routeId the route ID, as found in Config/Resources/routing/admin.xml
* @param unknown $urlParameters the URL parametrs, as a var/value pair array
*/
public function redirectToRoute($routeId, $urlParameters = array()) {
$this->redirect(URL::absoluteUrl($this->getRoute($routeId), $urlParameters));
}
}

View File

@@ -27,6 +27,7 @@ use Thelia\Model\Currency;
class CurrencyChangeEvent extends CurrencyCreateEvent
{
protected $currency_id;
protected $is_default;
public function __construct($currency_id)
{
@@ -44,4 +45,16 @@ class CurrencyChangeEvent extends CurrencyCreateEvent
return $this;
}
}
public function getIsDefault()
{
return $this->is_default;
}
public function setIsDefault($is_default)
{
$this->is_default = $is_default;
return $this;
}
}

View File

@@ -65,6 +65,8 @@ class CurrencyCreateEvent extends CurrencyEvent
public function setSymbol($symbol)
{
$this->symbol = $symbol;
return $this;
}
public function getCode()

View File

@@ -211,9 +211,11 @@ final class TheliaEvents
// -- Currencies management ---------------------------------------------
const CURRENCY_CREATE = "action.createCurrency";
const CURRENCY_MODIFY = "action.changeCurrency";
const CURRENCY_DELETE = "action.deleteCurrency";
const CURRENCY_CREATE = "action.createCurrency";
const CURRENCY_MODIFY = "action.changeCurrency";
const CURRENCY_DELETE = "action.deleteCurrency";
const CURRENCY_SET_DEFAULT = "action.setDefaultCurrency";
const CURRENCY_UPDATE_RATES = "action.updateCurrencyRates";
const BEFORE_CREATECURRENCY = "action.before_createCurrency";
const AFTER_CREATECURRENCY = "action.after_createCurrency";

View File

@@ -114,22 +114,24 @@ class Address extends BaseLoop
foreach ($addresses as $address) {
$loopResultRow = new LoopResultRow();
$loopResultRow->set("ID", $address->getId());
$loopResultRow->set("NAME", $address->getName());
$loopResultRow->set("CUSTOMER", $address->getCustomerId());
$loopResultRow->set("TITLE", $address->getTitleId());
$loopResultRow->set("COMPANY", $address->getCompany());
$loopResultRow->set("FIRSTNAME", $address->getFirstname());
$loopResultRow->set("LASTNAME", $address->getLastname());
$loopResultRow->set("ADDRESS1", $address->getAddress1());
$loopResultRow->set("ADDRESS2", $address->getAddress2());
$loopResultRow->set("ADDRESS3", $address->getAddress3());
$loopResultRow->set("ZIPCODE", $address->getZipcode());
$loopResultRow->set("CITY", $address->getCity());
$loopResultRow->set("COUNTRY", $address->getCountryId());
$loopResultRow->set("PHONE", $address->getPhone());
$loopResultRow->set("CELLPHONE", $address->getCellphone());
$loopResultRow->set("DEFAULT", $address->getIsDefault());
$loopResultRow
->set("ID", $address->getId())
->set("NAME", $address->getName())
->set("CUSTOMER", $address->getCustomerId())
->set("TITLE", $address->getTitleId())
->set("COMPANY", $address->getCompany())
->set("FIRSTNAME", $address->getFirstname())
->set("LASTNAME", $address->getLastname())
->set("ADDRESS1", $address->getAddress1())
->set("ADDRESS2", $address->getAddress2())
->set("ADDRESS3", $address->getAddress3())
->set("ZIPCODE", $address->getZipcode())
->set("CITY", $address->getCity())
->set("COUNTRY", $address->getCountryId())
->set("PHONE", $address->getPhone())
->set("CELLPHONE", $address->getCellphone())
->set("DEFAULT", $address->getIsDefault())
;
$loopResult->addRow($loopResultRow);
}

View File

@@ -107,8 +107,6 @@ class CategoryTree extends BaseI18nLoop
$visible = $this->getVisible();
$exclude = $this->getExclude();
//echo "exclude=".print_r($exclude);
$loopResult = new LoopResult();
$this->buildCategoryTree($id, $visible, 0, $depth, $exclude, $loopResult);

View File

@@ -164,9 +164,13 @@ class Config extends BaseI18nLoop
->set("POSTSCRIPTUM" , $result->getVirtualColumn('i18n_POSTSCRIPTUM'))
->set("HIDDEN" , $result->getHidden())
->set("SECURED" , $result->getSecured())
->set("CREATE_DATE" , $result->getCreatedAt())
->set("UPDATE_DATE" , $result->getUpdatedAt())
;
->set("CREATE_DATE" , $result->getCreatedAt())
->set("UPDATE_DATE" , $result->getUpdatedAt())
->set("VERSION" , $result->getVersion())
->set("VERSION_DATE" , $result->getVersionCreatedAt())
->set("VERSION_AUTHOR" , $result->getVersionCreatedBy())
;
$loopResult->addRow($loopResultRow);
}

View File

@@ -177,7 +177,11 @@ class Currency extends BaseI18nLoop
->set("SYMBOL" , $currency->getSymbol())
->set("RATE" , $currency->getRate())
->set("POSITION" , $currency->getPosition())
->set("IS_DEFAULT" , $currency->getByDefault());
->set("IS_DEFAULT" , $currency->getByDefault())
->set("CREATE_DATE" , $currency->getCreatedAt())
->set("UPDATE_DATE" , $currency->getUpdatedAt())
;
$loopResult->addRow($loopResultRow);
}

View File

@@ -519,6 +519,12 @@ class Product extends BaseI18nLoop
->set("IS_PROMO", $product->getVirtualColumn('main_product_is_promo'))
->set("IS_NEW", $product->getVirtualColumn('main_product_is_new'))
->set("POSITION", $product->getPosition())
->set("CREATE_DATE", $category->getCreatedAt())
->set("UPDATE_DATE", $category->getUpdatedAt())
->set("VERSION", $category->getVersion())
->set("VERSION_DATE", $category->getVersionCreatedAt())
->set("VERSION_AUTHOR", $category->getVersionCreatedBy())
;
$loopResult->addRow($loopResultRow);

View File

@@ -31,20 +31,20 @@ class CurrencyCreationForm extends BaseForm
{
protected function buildForm($change_mode = false)
{
$name_constraints = array(new Constraints\NotBlank());
$code_constraints = array(new Constraints\NotBlank());
if (!$change_mode) {
$name_constraints[] = new Constraints\Callback(array(
"methods" => array(array($this, "checkDuplicateName"))
$code_constraints[] = new Constraints\Callback(array(
"methods" => array(array($this, "checkDuplicateCode"))
));
}
$this->formBuilder
->add("name" , "text" , array("constraints" => array($name_constraints)))
->add("locale" , "text" , array())
->add("name" , "text" , array("constraints" => array(new NotBlank())))
->add("locale" , "text" , array("constraints" => array(new NotBlank())))
->add("symbol" , "text" , array("constraints" => array(new NotBlank())))
->add("rate" , "text" , array("constraints" => array(new NotBlank())))
->add("code" , "text" , array("constraints" => array(new NotBlank())))
->add("code" , "text" , array("constraints" => $code_constraints))
;
}
@@ -53,12 +53,12 @@ class CurrencyCreationForm extends BaseForm
return "thelia_currency_creation";
}
public function checkDuplicateName($value, ExecutionContextInterface $context)
public function checkDuplicateCode($value, ExecutionContextInterface $context)
{
$currency = CurrencyQuery::create()->findOneByName($value);
$currency = CurrencyQuery::create()->findOneByCode($value);
if ($currency) {
$context->addViolation(sprintf("A currency with name \"%s\" already exists.", $value));
$context->addViolation(sprintf("A currency with code \"%s\" already exists.", $value));
}
}

View File

@@ -34,8 +34,8 @@ class CurrencyModificationForm extends CurrencyCreationForm
parent::buildForm(true);
$this->formBuilder
->add("id" , "hidden", array("constraints" => array(new GreaterThan(array('value' => 0)))))
;
->add("id", "hidden", array("constraints" => array(new GreaterThan(array('value' => 0)))))
;
}
public function getName()

View File

@@ -2,6 +2,7 @@
namespace Thelia\Model;
use Thelia\Core\Event\CategoryEvent;
use Thelia\Model\Base\Category as BaseCategory;
use Propel\Runtime\ActiveQuery\Criteria;
use Thelia\Tools\URL;