diff --git a/core/lib/Thelia/Action/Currency.php b/core/lib/Thelia/Action/Currency.php index 4e3bb45d4..58257c012 100644 --- a/core/lib/Thelia/Action/Currency.php +++ b/core/lib/Thelia/Action/Currency.php @@ -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,7 +55,7 @@ class Currency extends BaseAction implements EventSubscriberInterface ->setName($event->getCurrencyName()) ->setSymbol($event->getSymbol()) ->setRate($event->getRate()) - ->setCode($event->getCode()) + ->setCode(strtoupper($event->getCode())) ->save() ; @@ -79,7 +81,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 +89,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 +134,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), + ); } } diff --git a/core/lib/Thelia/Config/Resources/routing/admin.xml b/core/lib/Thelia/Config/Resources/routing/admin.xml index 627fa9bf3..58645d531 100755 --- a/core/lib/Thelia/Config/Resources/routing/admin.xml +++ b/core/lib/Thelia/Config/Resources/routing/admin.xml @@ -101,6 +101,14 @@ Thelia\Controller\Admin\CurrencyController::saveChangeAction + + Thelia\Controller\Admin\CurrencyController::setDefaultAction + + + + Thelia\Controller\Admin\CurrencyController::updateRatesAction + + Thelia\Controller\Admin\CurrencyController::deleteAction diff --git a/core/lib/Thelia/Config/Resources/routing/front.xml b/core/lib/Thelia/Config/Resources/routing/front.xml index 9329b22ad..c10549d6d 100755 --- a/core/lib/Thelia/Config/Resources/routing/front.xml +++ b/core/lib/Thelia/Config/Resources/routing/front.xml @@ -40,4 +40,8 @@ Thelia\Controller\Front\CartController::changeItem cart + + + Thelia\Controller\Front\UrlRewritingController::check + diff --git a/core/lib/Thelia/Controller/Admin/BaseAdminController.php b/core/lib/Thelia/Controller/Admin/BaseAdminController.php index b4b10e9be..949344685 100755 --- a/core/lib/Thelia/Controller/Admin/BaseAdminController.php +++ b/core/lib/Thelia/Controller/Admin/BaseAdminController.php @@ -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 */ diff --git a/core/lib/Thelia/Controller/Admin/ConfigController.php b/core/lib/Thelia/Controller/Admin/ConfigController.php index c6fab6b1b..f7e62ff19 100644 --- a/core/lib/Thelia/Controller/Admin/ConfigController.php +++ b/core/lib/Thelia/Controller/Admin/ConfigController.php @@ -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'); } } \ No newline at end of file diff --git a/core/lib/Thelia/Controller/Admin/CurrencyController.php b/core/lib/Thelia/Controller/Admin/CurrencyController.php index b9ca98083..654a92bcf 100644 --- a/core/lib/Thelia/Controller/Admin/CurrencyController.php +++ b/core/lib/Thelia/Controller/Admin/CurrencyController.php @@ -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,28 @@ 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()); + + var_dump($ex); + + exit; } - 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 +173,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 +197,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 +234,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 +245,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 +270,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 +326,6 @@ class CurrencyController extends BaseAdminController $this->dispatch(TheliaEvents::CURRENCY_DELETE, $event); - $this->redirect(URL::adminViewUrl('currencies')); + $this->redirectToRoute('admin.configuration.currencies.default'); } } \ No newline at end of file diff --git a/core/lib/Thelia/Controller/Admin/MessageController.php b/core/lib/Thelia/Controller/Admin/MessageController.php index 5a53fd28a..29ee83df4 100644 --- a/core/lib/Thelia/Controller/Admin/MessageController.php +++ b/core/lib/Thelia/Controller/Admin/MessageController.php @@ -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 diff --git a/core/lib/Thelia/Controller/Admin/SessionController.php b/core/lib/Thelia/Controller/Admin/SessionController.php index 552fea8eb..a377df208 100755 --- a/core/lib/Thelia/Controller/Admin/SessionController.php +++ b/core/lib/Thelia/Controller/Admin/SessionController.php @@ -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() diff --git a/core/lib/Thelia/Controller/BaseController.php b/core/lib/Thelia/Controller/BaseController.php index 49d65f22f..b39c65b5e 100755 --- a/core/lib/Thelia/Controller/BaseController.php +++ b/core/lib/Thelia/Controller/BaseController.php @@ -37,6 +37,7 @@ 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; /** * @@ -178,9 +179,9 @@ class BaseController extends ContainerAware * * @param string $url */ - public function redirect($url) + public function redirect($url, $status = 302) { - Redirect::exec($url); + Redirect::exec($url, $status); } /** @@ -217,4 +218,13 @@ class BaseController extends ContainerAware return $route->getPath(); } -} + + /** + * Return a 404 error + * + * @return \Symfony\Component\HttpFoundation\Response + */ + protected function pageNotFound() + { + } +} \ No newline at end of file diff --git a/core/lib/Thelia/Controller/Front/BaseFrontController.php b/core/lib/Thelia/Controller/Front/BaseFrontController.php index c466ab0c6..b6d4a6f93 100755 --- a/core/lib/Thelia/Controller/Front/BaseFrontController.php +++ b/core/lib/Thelia/Controller/Front/BaseFrontController.php @@ -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)); + } } diff --git a/core/lib/Thelia/Controller/Front/DefaultController.php b/core/lib/Thelia/Controller/Front/DefaultController.php index 235e1bb64..d9c5a681a 100755 --- a/core/lib/Thelia/Controller/Front/DefaultController.php +++ b/core/lib/Thelia/Controller/Front/DefaultController.php @@ -23,6 +23,9 @@ namespace Thelia\Controller\Front; use Symfony\Component\HttpFoundation\Request; +use Thelia\Model\ConfigQuery; +use Thelia\Tools\Redirect; +use Thelia\Tools\URL; /** * @@ -43,6 +46,16 @@ class DefaultController extends BaseFrontController */ public function noAction(Request $request) { + if(ConfigQuery::isRewritingEnable()) { + + /* Does the query GET parameters match a rewritten URL ? */ + $rewrittenUrl = URL::init()->retrieveCurrent($request); + if($rewrittenUrl->rewrittenUrl !== null) { + /* 301 redirection to rewritten URL */ + $this->redirect($rewrittenUrl->rewrittenUrl, 301); + } + } + if (! $view = $request->query->get('view')) { $view = "index"; if ($request->request->has('view')) { diff --git a/core/lib/Thelia/Controller/Front/UrlRewritingController.php b/core/lib/Thelia/Controller/Front/UrlRewritingController.php new file mode 100755 index 000000000..df2479969 --- /dev/null +++ b/core/lib/Thelia/Controller/Front/UrlRewritingController.php @@ -0,0 +1,81 @@ +. */ +/* */ +/*************************************************************************************/ +namespace Thelia\Controller\Front; + +use Thelia\Core\HttpFoundation\Request; +use Thelia\Exception\UrlRewritingException; +use Thelia\Model\ConfigQuery; +use Thelia\Tools\URL; + +class UrlRewritingController extends BaseFrontController +{ + public function check(Request $request, $rewritten_url) + { + if(ConfigQuery::isRewritingEnable()) { + try { + $rewrittenUrlData = URL::init()->resolve($rewritten_url); + } catch(UrlRewritingException $e) { + switch($e->getCode()) { + case UrlRewritingException::URL_NOT_FOUND : + return $this->pageNotFound(); + break; + default: + throw $e; + } + } + + /* is the URL redirected ? */ + + if(null !== $rewrittenUrlData->redirectedToUrl) { + $this->redirect($rewrittenUrlData->redirectedToUrl, 301); + } + + /* define GET arguments in request */ + + if(null !== $rewrittenUrlData->view) { + $request->query->set('view', $rewrittenUrlData->view); + if(null !== $rewrittenUrlData->viewId) { + $request->query->set($rewrittenUrlData->view . '_id', $rewrittenUrlData->viewId); + } + } + if(null !== $rewrittenUrlData->locale) { + $request->query->set('locale', $rewrittenUrlData->locale); + } + + foreach($rewrittenUrlData->otherParameters as $parameter => $value) { + $request->query->set($parameter, $value); + } + } + + if (! $view = $request->query->get('view')) { + $view = "index"; + if ($request->request->has('view')) { + $view = $request->request->get('view'); + } + } + + $request->attributes->set('_view', $view); + + } + +} diff --git a/core/lib/Thelia/Core/Event/CurrencyChangeEvent.php b/core/lib/Thelia/Core/Event/CurrencyChangeEvent.php index f94d71b88..1e7677fee 100644 --- a/core/lib/Thelia/Core/Event/CurrencyChangeEvent.php +++ b/core/lib/Thelia/Core/Event/CurrencyChangeEvent.php @@ -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; } -} \ No newline at end of file + + public function getIsDefault() + { + return $this->is_default; + } + + public function setIsDefault($is_default) + { + $this->is_default = $is_default; + + return $this; + } +} diff --git a/core/lib/Thelia/Core/Event/CurrencyCreateEvent.php b/core/lib/Thelia/Core/Event/CurrencyCreateEvent.php index fba23a09b..d9d345ddd 100644 --- a/core/lib/Thelia/Core/Event/CurrencyCreateEvent.php +++ b/core/lib/Thelia/Core/Event/CurrencyCreateEvent.php @@ -65,6 +65,8 @@ class CurrencyCreateEvent extends CurrencyEvent public function setSymbol($symbol) { $this->symbol = $symbol; + + return $this; } public function getCode() diff --git a/core/lib/Thelia/Core/Event/TheliaEvents.php b/core/lib/Thelia/Core/Event/TheliaEvents.php index 8bca60bb4..ba7f46b8e 100755 --- a/core/lib/Thelia/Core/Event/TheliaEvents.php +++ b/core/lib/Thelia/Core/Event/TheliaEvents.php @@ -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"; diff --git a/core/lib/Thelia/Core/Template/Loop/Address.php b/core/lib/Thelia/Core/Template/Loop/Address.php index eb837eb4b..43bbbaefb 100755 --- a/core/lib/Thelia/Core/Template/Loop/Address.php +++ b/core/lib/Thelia/Core/Template/Loop/Address.php @@ -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); } diff --git a/core/lib/Thelia/Core/Template/Loop/CategoryTree.php b/core/lib/Thelia/Core/Template/Loop/CategoryTree.php index 63f1e9cfb..afcd0410e 100755 --- a/core/lib/Thelia/Core/Template/Loop/CategoryTree.php +++ b/core/lib/Thelia/Core/Template/Loop/CategoryTree.php @@ -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); diff --git a/core/lib/Thelia/Core/Template/Loop/Config.php b/core/lib/Thelia/Core/Template/Loop/Config.php index 684803385..f8819afef 100644 --- a/core/lib/Thelia/Core/Template/Loop/Config.php +++ b/core/lib/Thelia/Core/Template/Loop/Config.php @@ -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); } diff --git a/core/lib/Thelia/Core/Template/Loop/Currency.php b/core/lib/Thelia/Core/Template/Loop/Currency.php index b6756fbe6..6db6c6223 100755 --- a/core/lib/Thelia/Core/Template/Loop/Currency.php +++ b/core/lib/Thelia/Core/Template/Loop/Currency.php @@ -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); } diff --git a/core/lib/Thelia/Core/Template/Loop/Product.php b/core/lib/Thelia/Core/Template/Loop/Product.php index a35ff90b3..17ecd3212 100755 --- a/core/lib/Thelia/Core/Template/Loop/Product.php +++ b/core/lib/Thelia/Core/Template/Loop/Product.php @@ -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); diff --git a/core/lib/Thelia/Core/Template/Smarty/Plugins/UrlGenerator.php b/core/lib/Thelia/Core/Template/Smarty/Plugins/UrlGenerator.php index 33e699c43..635c03b8c 100755 --- a/core/lib/Thelia/Core/Template/Smarty/Plugins/UrlGenerator.php +++ b/core/lib/Thelia/Core/Template/Smarty/Plugins/UrlGenerator.php @@ -147,7 +147,7 @@ class UrlGenerator extends AbstractSmartyPlugin { return array( "current" => "getCurrentUrl", - "return_to" => "getReturnToUrl", + "return_to" => "getReturnToUrl", "index" => "getIndexUrl", ); } @@ -169,7 +169,9 @@ class UrlGenerator extends AbstractSmartyPlugin protected function getCurrentUrl() { - return URL::retrieveCurrent($this->request); + $retriever = URL::init()->retrieveCurrent($this->request); + + return $retriever->rewrittenUrl === null ? $retriever->url : $retriever->rewrittenUrl ; } protected function getReturnToUrl() diff --git a/core/lib/Thelia/Exception/UrlRewritingException.php b/core/lib/Thelia/Exception/UrlRewritingException.php new file mode 100755 index 000000000..0df566a6b --- /dev/null +++ b/core/lib/Thelia/Exception/UrlRewritingException.php @@ -0,0 +1,42 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Exception; + +use Thelia\Log\Tlog; + +class UrlRewritingException extends \Exception +{ + const UNKNOWN_EXCEPTION = 0; + + const URL_NOT_FOUND = 404; + + const RESOLVER_NULL_SEARCH = 800; + + public function __construct($message, $code = null, $previous = null) { + if($code === null) { + $code = self::UNKNOWN_EXCEPTION; + } + parent::__construct($message, $code, $previous); + } +} diff --git a/core/lib/Thelia/Form/CurrencyCreationForm.php b/core/lib/Thelia/Form/CurrencyCreationForm.php index 11a496582..6150947b8 100644 --- a/core/lib/Thelia/Form/CurrencyCreationForm.php +++ b/core/lib/Thelia/Form/CurrencyCreationForm.php @@ -40,8 +40,8 @@ class CurrencyCreationForm extends BaseForm } $this->formBuilder - ->add("name" , "text" , array("constraints" => array($name_constraints))) - ->add("locale" , "text" , array()) + ->add("name" , "text" , array("constraints" => $name_constraints)) + ->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()))) diff --git a/core/lib/Thelia/Form/CurrencyModificationForm.php b/core/lib/Thelia/Form/CurrencyModificationForm.php index 6a4279d1b..3f0b1f152 100644 --- a/core/lib/Thelia/Form/CurrencyModificationForm.php +++ b/core/lib/Thelia/Form/CurrencyModificationForm.php @@ -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() diff --git a/core/lib/Thelia/Model/Category.php b/core/lib/Thelia/Model/Category.php index ac3b2fff6..26343142e 100755 --- a/core/lib/Thelia/Model/Category.php +++ b/core/lib/Thelia/Model/Category.php @@ -22,7 +22,7 @@ class Category extends BaseCategory public function getUrl($locale) { - return URL::retrieve('category', $this->getId(), $locale); + return URL::init()->retrieve('category', $this->getId(), $locale); } /** diff --git a/core/lib/Thelia/Model/Content.php b/core/lib/Thelia/Model/Content.php index 1a797be89..f51915776 100755 --- a/core/lib/Thelia/Model/Content.php +++ b/core/lib/Thelia/Model/Content.php @@ -9,6 +9,6 @@ class Content extends BaseContent { public function getUrl($locale) { - return URL::retrieve('content', $this->getId(), $locale); + return URL::init()->retrieve('content', $this->getId(), $locale); } } diff --git a/core/lib/Thelia/Model/Folder.php b/core/lib/Thelia/Model/Folder.php index 05838e939..2672c17de 100755 --- a/core/lib/Thelia/Model/Folder.php +++ b/core/lib/Thelia/Model/Folder.php @@ -17,7 +17,7 @@ class Folder extends BaseFolder public function getUrl($locale) { - return URL::retrieve('folder', $this->getId(), $locale); + return URL::init()->retrieve('folder', $this->getId(), $locale); } /** diff --git a/core/lib/Thelia/Model/Product.php b/core/lib/Thelia/Model/Product.php index e7d0586cc..b4b036896 100755 --- a/core/lib/Thelia/Model/Product.php +++ b/core/lib/Thelia/Model/Product.php @@ -9,6 +9,6 @@ class Product extends BaseProduct { public function getUrl($locale) { - return URL::retrieve('product', $this->getId(), $locale); + return URL::init()->retrieve('product', $this->getId(), $locale); } } diff --git a/core/lib/Thelia/Model/RewritingUrlQuery.php b/core/lib/Thelia/Model/RewritingUrlQuery.php index c5b73c1e5..d1ab8fcdf 100644 --- a/core/lib/Thelia/Model/RewritingUrlQuery.php +++ b/core/lib/Thelia/Model/RewritingUrlQuery.php @@ -2,8 +2,10 @@ namespace Thelia\Model; +use Propel\Runtime\ActiveQuery\Criteria; +use Propel\Runtime\ActiveQuery\Join; use Thelia\Model\Base\RewritingUrlQuery as BaseRewritingUrlQuery; - +use Thelia\Model\Map\RewritingUrlTableMap; /** * Skeleton subclass for performing query and update operations on the 'rewriting_url' table. @@ -15,6 +17,96 @@ use Thelia\Model\Base\RewritingUrlQuery as BaseRewritingUrlQuery; * long as it does not already exist in the output directory. * */ -class RewritingUrlQuery extends BaseRewritingUrlQuery { +class RewritingUrlQuery extends BaseRewritingUrlQuery +{ + /** + * @param $rewrittenUrl + * + * @return array|mixed|\Propel\Runtime\Collection\ObjectCollection + */ + public function getResolverSearch($rewrittenUrl) + { + $redirectedJoin = new Join(); + $redirectedJoin->addExplicitCondition(RewritingUrlTableMap::TABLE_NAME, 'REDIRECTED', 'ru', RewritingUrlTableMap::TABLE_NAME, 'ID', 'is_redirected'); + $redirectedJoin->setJoinType(Criteria::LEFT_JOIN); + $search = RewritingArgumentQuery::create() + ->joinRewritingUrl('ru', Criteria::RIGHT_JOIN) + ->addJoinObject($redirectedJoin) + ->where('`ru`.URL = ?', $rewrittenUrl, \PDO::PARAM_STR) + ->withColumn('`ru`.URL', 'ru_url') + ->withColumn('`ru`.VIEW', 'ru_view') + ->withColumn('`ru`.VIEW_LOCALE', 'ru_locale') + ->withColumn('`ru`.VIEW_ID', 'ru_viewId') + ->withColumn('`is_redirected`.URL', 'ru_redirected_to_url') + ->find(); + + return $search; + } + + /** + * @param $view + * @param $viewId + * @param $viewLocale + * + * @return null|RewritingUrl + */ + public function getViewUrlQuery($view, $viewLocale, $viewId) + { + return RewritingUrlQuery::create() + ->joinRewritingArgument('ra', Criteria::LEFT_JOIN) + ->where('ISNULL(`ra`.REWRITING_URL_ID)') + ->filterByView($view) + ->filterByViewLocale($viewLocale) + ->filterByViewId($viewId) + ->filterByRedirected(null) + ->orderByUpdatedAt(Criteria::DESC) + ->findOne(); + } + + /** + * @param $view + * @param $viewLocale + * @param $viewId + * @param $viewOtherParameters + * + * @return null|RewritingUrl + */ + public function getSpecificUrlQuery($view, $viewLocale, $viewId, $viewOtherParameters) + { + $urlQuery = RewritingUrlQuery::create() + ->joinRewritingArgument('ra', Criteria::LEFT_JOIN) + ->withColumn('`ra`.REWRITING_URL_ID', 'ra_REWRITING_URL_ID') + ->filterByView($view) + ->filterByViewLocale($viewLocale) + ->filterByViewId($viewId) + ->filterByRedirected(null) + ->orderByUpdatedAt(Criteria::DESC); + + $otherParametersCount = count($viewOtherParameters); + if($otherParametersCount > 0) { + $parameterConditions = array(); + + foreach($viewOtherParameters as $parameter => $value) { + $conditionName = 'other_parameter_condition_' . count($parameterConditions); + $urlQuery->condition('parameter_condition', '`ra`.PARAMETER= ?', $parameter, \PDO::PARAM_STR) + ->condition('value_condition', '`ra`.VALUE = ?', $value, \PDO::PARAM_STR) + ->combine(array('parameter_condition', 'value_condition'), Criteria::LOGICAL_AND, $conditionName); + $parameterConditions[] = $conditionName; + } + + $urlQuery->where($parameterConditions, Criteria::LOGICAL_OR); + + $urlQuery->groupBy(RewritingUrlTableMap::ID); + + $urlQuery->condition('count_condition_1', 'COUNT(' . RewritingUrlTableMap::ID . ') = ?', $otherParametersCount, \PDO::PARAM_INT) // ensure we got all the asked parameters (provided by the query) + ->condition('count_condition_2', 'COUNT(' . RewritingUrlTableMap::ID . ') = (SELECT COUNT(*) FROM rewriting_argument WHERE rewriting_argument.REWRITING_URL_ID = ra_REWRITING_URL_ID)'); // ensure we don't miss any parameters (needed to match the rewritten url) + + $urlQuery->having(array('count_condition_1', 'count_condition_2'), Criteria::LOGICAL_AND); + } else { + $urlQuery->where('ISNULL(`ra`.REWRITING_URL_ID)'); + } + + return $urlQuery->findOne(); + } } // RewritingUrlQuery diff --git a/core/lib/Thelia/Rewriting/RewritingResolver.php b/core/lib/Thelia/Rewriting/RewritingResolver.php index 818dbec32..900442db9 100755 --- a/core/lib/Thelia/Rewriting/RewritingResolver.php +++ b/core/lib/Thelia/Rewriting/RewritingResolver.php @@ -22,6 +22,13 @@ /*************************************************************************************/ namespace Thelia\Rewriting; +use Propel\Runtime\ActiveQuery\Criteria; +use Propel\Runtime\ActiveQuery\Join; +use Thelia\Exception\RewritingUrlException; +use Thelia\Exception\UrlRewritingException; +use Thelia\Model\RewritingUrlQuery; +use Thelia\Model\Map\RewritingUrlTableMap; + /** * Class RewritingResolver * @package Thelia\Rewriting @@ -31,5 +38,58 @@ namespace Thelia\Rewriting; */ class RewritingResolver { + protected $search = null; + protected $rewritingUrlQuery = null; + + public $view; + public $viewId; + public $locale; + public $otherParameters; + public $redirectedToUrl; + + public function __construct($url = null) + { + $this->rewritingUrlQuery = new RewritingUrlQuery(); + + if($url !== null) { + $this->load($url); + } + } + + public function load($rewrittenUrl) + { + $this->search = $this->rewritingUrlQuery->getResolverSearch($rewrittenUrl); + + if($this->search->count() == 0) { + throw new UrlRewritingException('URL NOT FOUND', UrlRewritingException::URL_NOT_FOUND); + } + + $this->view = $this->search->getFirst()->getVirtualColumn('ru_view'); + $this->viewId = $this->search->getFirst()->getVirtualColumn('ru_viewId'); + $this->locale = $this->search->getFirst()->getVirtualColumn('ru_locale'); + $this->redirectedToUrl = $this->search->getFirst()->getVirtualColumn('ru_redirected_to_url'); + + $this->otherParameters = $this->getOtherParameters(); + } + + protected function getOtherParameters() + { + if($this->search === null) { + throw new UrlRewritingException('RESOLVER NULL SEARCH', UrlRewritingException::RESOLVER_NULL_SEARCH); + } + + $otherParameters = array(); + foreach($this->search as $result) { + $parameter = $result->getParameter(); + $value = $result->getValue(); + + if(null !== $parameter) { + $otherParameters[$parameter] = $value; + } + } + + return $otherParameters; + } + } diff --git a/core/lib/Thelia/Rewriting/RewritingRetriever.php b/core/lib/Thelia/Rewriting/RewritingRetriever.php index 0deb7de45..7958af012 100755 --- a/core/lib/Thelia/Rewriting/RewritingRetriever.php +++ b/core/lib/Thelia/Rewriting/RewritingRetriever.php @@ -23,8 +23,9 @@ namespace Thelia\Rewriting; use Propel\Runtime\ActiveQuery\Criteria; -use Thelia\Model\Base\RewritingUrlQuery; +use Thelia\Model\RewritingUrlQuery; use Thelia\Model\Map\RewritingUrlTableMap; +use Thelia\Tools\URL; /** * Class RewritingRetriever @@ -35,38 +36,40 @@ use Thelia\Model\Map\RewritingUrlTableMap; */ class RewritingRetriever { - /** - * @param $view - * @param $viewLocale - * @param $viewId - * - * @return null|$url - */ - public function getViewUrl($view, $viewLocale, $viewId) - { - $url = $this->getViewUrlQuery($view, $viewId, $viewLocale); + protected $search = null; + protected $rewritingUrlQuery = null; - return $url === null ? null : $url->getUrl(); + public $url; + public $rewrittenUrl; + + public function __construct($view = null, $viewLocale = null, $viewId = null) + { + $this->rewritingUrlQuery = new RewritingUrlQuery(); + + if($view !== null && $viewLocale !== null) { + $this->load($view, $viewLocale, $viewId); + } } /** - * @param $view - * @param $viewId - * @param $viewLocale - * - * @return null|RewritingUrl + * @param $view + * @param $viewLocale + * @param null $viewId */ - protected function getViewUrlQuery($view, $viewId, $viewLocale) + public function loadViewUrl($view, $viewLocale, $viewId = null) { - return RewritingUrlQuery::create() - ->joinRewritingArgument('ra', Criteria::LEFT_JOIN) - ->where('ISNULL(`ra`.REWRITING_URL_ID)') - ->filterByView($view) - ->filterByViewLocale($viewLocale) - ->filterByViewId($viewId) - ->filterByRedirected(null) - ->orderByUpdatedAt(Criteria::DESC) - ->findOne(); + $this->search = $this->rewritingUrlQuery->getViewUrlQuery($view, $viewLocale, $viewId); + + $allParametersWithoutView = array(); + $allParametersWithoutView['locale'] = $viewLocale; + if(null !== $viewId) { + $allParametersWithoutView[$view . '_id'] = $viewId; + } + + $this->url = URL::viewUrl($view, $allParametersWithoutView); + if($this->search !== null) { + $this->rewrittenUrl = $this->search->getUrl(); + } } /** @@ -74,46 +77,25 @@ class RewritingRetriever * @param $viewLocale * @param null $viewId * @param array $viewOtherParameters - * - * @return null|$url */ - public function getSpecificUrl($view, $viewLocale, $viewId = null, $viewOtherParameters = array()) + public function loadSpecificUrl($view, $viewLocale, $viewId = null, $viewOtherParameters = array()) { - $urlQuery = RewritingUrlQuery::create() - ->joinRewritingArgument('ra', Criteria::LEFT_JOIN) - ->withColumn('`ra`.REWRITING_URL_ID', 'ra_REWRITING_URL_ID') - ->filterByView($view) - ->filterByViewLocale($viewLocale) - ->filterByViewId($viewId) - ->filterByRedirected(null) - ->orderByUpdatedAt(Criteria::DESC); - - $otherParametersCount = count($viewOtherParameters); - if($otherParametersCount > 0) { - $parameterConditions = array(); - - foreach($viewOtherParameters as $parameter => $value) { - $conditionName = 'other_parameter_condition_' . count($parameterConditions); - $urlQuery->condition('parameter_condition', '`ra`.PARAMETER= ?', $parameter, \PDO::PARAM_STR) - ->condition('value_condition', '`ra`.VALUE = ?', $value, \PDO::PARAM_STR) - ->combine(array('parameter_condition', 'value_condition'), Criteria::LOGICAL_AND, $conditionName); - $parameterConditions[] = $conditionName; - } - - $urlQuery->where($parameterConditions, Criteria::LOGICAL_OR); - - $urlQuery->groupBy(RewritingUrlTableMap::ID); - - $urlQuery->condition('count_condition_1', 'COUNT(' . RewritingUrlTableMap::ID . ') = ?', $otherParametersCount, \PDO::PARAM_INT) // ensure we got all the asked parameters (provided by the query) - ->condition('count_condition_2', 'COUNT(' . RewritingUrlTableMap::ID . ') = (SELECT COUNT(*) FROM rewriting_argument WHERE rewriting_argument.REWRITING_URL_ID = ra_REWRITING_URL_ID)'); // ensure we don't miss any parameters (needed to match the rewritten url) - - $urlQuery->having(array('count_condition_1', 'count_condition_2'), Criteria::LOGICAL_AND); - } else { - $urlQuery->where('ISNULL(`ra`.REWRITING_URL_ID)'); + if(empty($viewOtherParameters)) { + $this->loadViewUrl($view, $viewLocale, $viewId); + return; } - $url = $urlQuery->findOne(); + $this->search = $this->rewritingUrlQuery->getSpecificUrlQuery($view, $viewLocale, $viewId, $viewOtherParameters); - return $url === null ? null : $url->getUrl(); + $allParametersWithoutView = $viewOtherParameters; + $allParametersWithoutView['locale'] = $viewLocale; + if(null !== $viewId) { + $allParametersWithoutView[$view . '_id'] = $viewId; + } + + $this->url = URL::viewUrl($view, $allParametersWithoutView); + if($this->search !== null) { + $this->rewrittenUrl = $this->search->getUrl(); + } } } diff --git a/core/lib/Thelia/Tests/Rewriting/RewritingResolverTest.php b/core/lib/Thelia/Tests/Rewriting/RewritingResolverTest.php new file mode 100755 index 000000000..2ebce8e77 --- /dev/null +++ b/core/lib/Thelia/Tests/Rewriting/RewritingResolverTest.php @@ -0,0 +1,119 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Tests\Rewriting; + +use Thelia\Model\RewritingArgument; +use Thelia\Rewriting\RewritingResolver; +use Propel\Runtime\Collection\ObjectCollection; + +/** + * + * @author Etienne Roudeix + * + */ +class RewritingResolverTest extends \PHPUnit_Framework_TestCase +{ + protected function getMethod($name) + { + $class = new \ReflectionClass('\Thelia\Rewriting\RewritingResolver'); + $method = $class->getMethod($name); + $method->setAccessible(true); + + return $method; + } + + protected function getProperty($name) + { + $class = new \ReflectionClass('\Thelia\Rewriting\RewritingResolver'); + $property = $class->getProperty($name); + $property->setAccessible(true); + + return $property; + } + + public function testGetOtherParameters() + { + $rewritingArguments = array( + array('Parameter' => 'foo0', 'Value' => 'bar0'), + array('Parameter' => 'foo1', 'Value' => 'bar1'), + array('Parameter' => 'foo2', 'Value' => 'bar2'), + ); + $searchResult = new ObjectCollection(); + $searchResult->setModel('\Thelia\Model\RewritingArgument'); + $searchResult->fromArray($rewritingArguments); + + $resolver = new RewritingResolver(); + + $search = $this->getProperty('search'); + $search->setValue($resolver, $searchResult); + + $method = $this->getMethod('getOtherParameters'); + $actual = $method->invoke($resolver); + + $expected = array( + 'foo0' => 'bar0', + 'foo1' => 'bar1', + 'foo2' => 'bar2', + ); + + $this->assertEquals($expected, $actual); + } + + public function testLoad() + { + $collection = new ObjectCollection(); + $collection->setModel('\Thelia\Model\RewritingArgument'); + + for($i=0; $i<3; $i++) { + $ra = new RewritingArgument(); + $ra->setParameter('foo' . $i); + $ra->setValue('bar' . $i); + $ra->setVirtualColumn('ru_view', 'view'); + $ra->setVirtualColumn('ru_viewId', 'viewId'); + $ra->setVirtualColumn('ru_locale', 'locale'); + $ra->setVirtualColumn('ru_redirected_to_url', null); + + $collection->append($ra); + } + + + $resolverQuery = $this->getMock('\Thelia\Model\RewritingUrlQuery', array('getResolverSearch')); + $resolverQuery->expects($this->any()) + ->method('getResolverSearch') + ->with('foo.html') + ->will($this->returnValue($collection)); + + $resolver = new RewritingResolver(); + + $rewritingUrlQuery = $this->getProperty('rewritingUrlQuery'); + $rewritingUrlQuery->setValue($resolver, $resolverQuery); + + $resolver->load('foo.html'); + + $this->assertEquals('view', $resolver->view); + $this->assertEquals('viewId', $resolver->viewId); + $this->assertEquals('locale', $resolver->locale); + $this->assertEquals(array('foo0' => 'bar0', 'foo1' => 'bar1', 'foo2' => 'bar2'), $resolver->otherParameters); + } +} diff --git a/core/lib/Thelia/Tests/Rewriting/RewritingRetrieverTest.php b/core/lib/Thelia/Tests/Rewriting/RewritingRetrieverTest.php index 741c22b4e..9a5036adc 100755 --- a/core/lib/Thelia/Tests/Rewriting/RewritingRetrieverTest.php +++ b/core/lib/Thelia/Tests/Rewriting/RewritingRetrieverTest.php @@ -23,7 +23,9 @@ namespace Thelia\Tests\Rewriting; +use Thelia\Model\RewritingUrl; use Thelia\Rewriting\RewritingRetriever; +use Thelia\Tools\URL; /** * @@ -32,13 +34,65 @@ use Thelia\Rewriting\RewritingRetriever; */ class RewritingRetrieverTest extends \PHPUnit_Framework_TestCase { - public function testGetViewUrl() + protected function getMethod($name) { + $class = new \ReflectionClass('\Thelia\Rewriting\RewritingRetriever'); + $method = $class->getMethod($name); + $method->setAccessible(true); + return $method; } - public function testGetSpecificUrl() + protected function getProperty($name) { + $class = new \ReflectionClass('\Thelia\Rewriting\RewritingRetriever'); + $property = $class->getProperty($name); + $property->setAccessible(true); + return $property; + } + + public function testLoadViewUrl() + { + $searchResult = new RewritingUrl(); + $searchResult->setUrl('foo.html'); + + $retrieverQuery = $this->getMock('\Thelia\Model\RewritingUrlQuery', array('getViewUrlQuery')); + $retrieverQuery->expects($this->any()) + ->method('getViewUrlQuery') + ->with('view', 'fr_FR', 1) + ->will($this->returnValue($searchResult)); + + $retriever = new RewritingRetriever(); + + $rewritingUrlQuery = $this->getProperty('rewritingUrlQuery'); + $rewritingUrlQuery->setValue($retriever, $retrieverQuery); + + $retriever->loadViewUrl('view', 'fr_FR', 1); + + $this->assertEquals('foo.html', $retriever->rewrittenUrl); + $this->assertEquals(URL::viewUrl('view', array('locale' => 'fr_FR', 'view_id' => 1)), $retriever->url); + } + + public function testLoadSpecificUrl() + { + $searchResult = new RewritingUrl(); + $searchResult->setUrl('foo.html'); + + $retrieverQuery = $this->getMock('\Thelia\Model\RewritingUrlQuery', array('getSpecificUrlQuery')); + $retrieverQuery->expects($this->any()) + ->method('getSpecificUrlQuery') + ->with('view', 'fr_FR', 1, array('foo0' => 'bar0', 'foo1' => 'bar1')) + ->will($this->returnValue($searchResult)); + + $retriever = new RewritingRetriever(); + + $rewritingUrlQuery = $this->getProperty('rewritingUrlQuery'); + $rewritingUrlQuery->setValue($retriever, $retrieverQuery); + + $retriever->loadSpecificUrl('view', 'fr_FR', 1, array('foo0' => 'bar0', 'foo1' => 'bar1')); + + $this->assertEquals('foo.html', $retriever->rewrittenUrl); + $this->assertEquals(URL::viewUrl('view', array('foo0' => 'bar0', 'foo1' => 'bar1', 'locale' => 'fr_FR', 'view_id' => 1)), $retriever->url); } } diff --git a/core/lib/Thelia/Tools/URL.php b/core/lib/Thelia/Tools/URL.php index b84fdca73..a527746ce 100755 --- a/core/lib/Thelia/Tools/URL.php +++ b/core/lib/Thelia/Tools/URL.php @@ -25,18 +25,33 @@ namespace Thelia\Tools; use Symfony\Component\HttpFoundation\Request; use Thelia\Model\ConfigQuery; +use Thelia\Rewriting\RewritingResolver; use Thelia\Rewriting\RewritingRetriever; class URL { + protected $resolver = null; + protected $retriever = null; + const PATH_TO_FILE = true; const WITH_INDEX_PAGE = false; + public function __construct() + { + $this->retriever = new RewritingRetriever(); + $this->resolver = new RewritingResolver(); + } + public static function getIndexPage() { return ConfigQuery::read('base_url', '/') . "index_dev.php"; // FIXME ! } + public static function init() + { + return new URL(); + } + /** * Returns the Absolute URL for a given path relative to web root. By default, * the index.php (or index_dev.php) script name is added to the URL, use @@ -103,7 +118,7 @@ class URL */ public static function viewUrl($viewName, array $parameters = array()) { - $path = sprintf("%s?view=%s", self::getIndexPage(), $viewName); + $path = sprintf("?view=%s", $viewName); return self::absoluteUrl($path, $parameters); } @@ -115,18 +130,17 @@ class URL * * @return null|string */ - public static function retrieve($view, $viewId, $viewLocale) + public function retrieve($view, $viewId, $viewLocale) { $rewrittenUrl = null; if(ConfigQuery::isRewritingEnable()) { - $retriever = new RewritingRetriever(); - $rewrittenUrl = $retriever->getViewUrl($view, $viewLocale, $viewId); + $rewrittenUrl = $this->retriever->loadViewUrl($view, $viewLocale, $viewId); } return $rewrittenUrl === null ? self::viewUrl($view, array($view . '_id' => $viewId, 'locale' => $viewLocale)) : $rewrittenUrl; } - public static function retrieveCurrent(Request $request) + public function retrieveCurrent(Request $request) { $rewrittenUrl = null; if(ConfigQuery::isRewritingEnable()) { @@ -134,12 +148,10 @@ class URL $viewLocale = $request->query->get('locale', null); $viewId = $view === null ? null : $request->query->get($view . '_id', null); - $allParameters = $request->query->all(); - $allParametersWithoutView = $allParameters; + $allOtherParameters = $request->query->all(); if($view !== null) { - unset($allParametersWithoutView['view']); + unset($allOtherParameters['view']); } - $allOtherParameters = $allParametersWithoutView; if($viewLocale !== null) { unset($allOtherParameters['locale']); } @@ -147,10 +159,15 @@ class URL unset($allOtherParameters[$view . '_id']); } - $retriever = new RewritingRetriever(); - $rewrittenUrl = $retriever->getSpecificUrl($view, $viewLocale, $viewId, $allOtherParameters); + $this->retriever->loadSpecificUrl($view, $viewLocale, $viewId, $allOtherParameters); } - return $rewrittenUrl === null ? self::viewUrl($view, $allParametersWithoutView) : $rewrittenUrl; + return $this->retriever; + } + + public function resolve($url) + { + $this->resolver->load($url); + return $this->resolver; } } diff --git a/install/insert.sql b/install/insert.sql index 41c63e353..761704362 100755 --- a/install/insert.sql +++ b/install/insert.sql @@ -6,14 +6,15 @@ INSERT INTO `lang`(`id`,`title`,`code`,`locale`,`url`,`by_default`,`created_at`, INSERT INTO `config` (`name`, `value`, `secured`, `hidden`, `created_at`, `updated_at`) VALUES ('session_config.default', '1', 1, 1, NOW(), NOW()), -('verifyStock', '1', 1, 0, NOW(), NOW()), -('default_lang_without_translation', '1', 1, 0, NOW(), NOW()), -('rewriting_enable', '0', 1, 0, NOW(), NOW()), -('imagine_graphic_driver', 'gd', 1, 0, NOW(), NOW()), -('default_images_quality_percent', '75', 1, 0, NOW(), NOW()), -('original_image_delivery_mode', 'symlink', 1, 0, NOW(), NOW()), -('images_library_path', 'local/media/images', 1, 0, NOW(), NOW()), -('image_cache_dir_from_web_root', 'cache/images', 1, 0, NOW(), NOW()); +('verifyStock', '1', 0, 0, NOW(), NOW()), +('default_lang_without_translation', '1', 0, 0, NOW(), NOW()), +('rewriting_enable', '0', 0, 0, NOW(), NOW()), +('imagine_graphic_driver', 'gd', 0, 0, NOW(), NOW()), +('default_images_quality_percent', '75', 0, 0, NOW(), NOW()), +('original_image_delivery_mode', 'symlink', 0, 0, NOW(), NOW()), +('images_library_path', 'local/media/images', 0, 0, NOW(), NOW()), +('image_cache_dir_from_web_root', 'cache/images', 0, 0, NOW(), NOW()), +('currency_rate_update_url', 'http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml', 0, 0, NOW(), NOW()); INSERT INTO `module` (`code`, `type`, `activate`, `position`, `created_at`, `updated_at`) VALUES ('test', '1', '1', '1', NOW(), NOW()); diff --git a/templates/admin/default/currencies.html b/templates/admin/default/currencies.html index 6671c131f..a693214d7 100644 --- a/templates/admin/default/currencies.html +++ b/templates/admin/default/currencies.html @@ -26,7 +26,7 @@
-
+
@@ -117,25 +118,24 @@ {loop name="currencies" type="currency" backend_context="1" lang=$lang_id order=$order} + - + - + - + - + - - - + - + {module_include location='currencies_table_row'} @@ -208,7 +210,7 @@
@@ -35,6 +35,7 @@ + {/loop}
{$ID}{$ID} + {loop type="auth" name="can_change" roles="ADMIN" permissions="admin.configuration.currencies.change"} + {$NAME} + {/loop} + {elseloop rel="can_change"} + {$NAME} + {/elseloop} + - {loop type="auth" name="can_change" roles="ADMIN" permissions="admin.configuration.currencies.change"} - {$NAME} - {/loop} - {elseloop rel="can_change"} - {$NAME} - {/elseloop} - {$ISOCODE}{$ISOCODE}{$SYMBOL}{$SYMBOL}{format_number number="$RATE" decimals="4"}{$RATE|string_format:"%.4f"} + {admin_position_block permission="admin.currencies.edit" path="/admin/configuration/currencies" @@ -144,9 +144,11 @@ position="$POSITION" id="$ID" } - + +