This commit is contained in:
mespeche
2013-09-18 15:39:03 +02:00
13 changed files with 392 additions and 89 deletions

View File

@@ -0,0 +1,60 @@
<?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;
/**
* Class GenerateRewrittenUrlEvent
* @package Thelia\Core\Event
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
class GenerateRewrittenUrlEvent extends ActionEvent {
protected $object;
protected $locale;
protected $url;
public function __construct($object, $locale)
{
$this->object;
$this->locale;
}
public function setUrl($url)
{
$this->url = $url;
}
public function isRewritten()
{
return null !== $this->url;
}
public function getUrl()
{
return $this->url;
}
}

View File

@@ -441,4 +441,9 @@ final class TheliaEvents
*/
const MAILTRANSPORTER_CONFIG = 'action.mailertransporter.config';
/**
* sent when Thelia try to generate a rewriten url
*/
const GENERATE_REWRITTENURL = 'action.generate_rewritenurl';
}

View File

@@ -27,6 +27,7 @@ use Thelia\Core\Template\Smarty\SmartyPluginDescriptor;
use Thelia\Core\Template\Smarty\AbstractSmartyPlugin;
use Thelia\Tools\URL;
use Thelia\Core\HttpFoundation\Request;
use Thelia\Core\Translation\Translator;
class UrlGenerator extends AbstractSmartyPlugin
{
@@ -47,11 +48,27 @@ class UrlGenerator extends AbstractSmartyPlugin
public function generateUrlFunction($params, &$smarty)
{
// the path to process
$path = $this->getParam($params, 'path');
$path = $this->getParam($params, 'path', null);
$file = $this->getParam($params, 'file', null);
if ($file !== null) {
$path = $file;
$mode = URL::PATH_TO_FILE;
}
else if ($path !== null) {
$mode = URL::WITH_INDEX_PAGE;
}
else {
throw \InvalidArgumentException(Translator::getInstance()->trans("Please specify either 'path' or 'file' parameter in {url} function."));
}
$target = $this->getParam($params, 'target', null);
$url = URL::getInstance()->absoluteUrl($path, $this->getArgsFromParam($params, array('path', 'target')));
$url = URL::getInstance()->absoluteUrl(
$path,
$this->getArgsFromParam($params, array('path', 'file', 'target')),
$mode
);
if ($target != null) $url .= '#'.$target;

View File

@@ -56,9 +56,15 @@ class ConfigQuery extends BaseConfigQuery {
public static function getPageNotFoundView()
{
return self::read("page_not_found_view", '404.html');
return self::read("page_not_found_view", '404');
}
public static function getPassedUrlView()
{
return self::read('passed_url_view', 'passed-url');
}
public static function getActiveTemplate()
{
return self::read('active-template', 'default');

View File

@@ -200,6 +200,12 @@ class Product extends BaseProduct
*/
public function postDelete(ConnectionInterface $con = null)
{
RewritingUrlQuery::create()
->filterByView($this->getRewrittenUrlViewName())
->filterByViewId($this->getId())
->update(array(
"View" => ConfigQuery::getPassedUrlView()
));
$this->dispatchEvent(TheliaEvents::AFTER_DELETEPRODUCT, new ProductEvent($this));
}
}

View File

@@ -23,6 +23,8 @@
namespace Thelia\Model\Tools;
use Thelia\Core\Event\GenerateRewrittenUrlEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Exception\UrlRewritingException;
use Thelia\Model\RewritingUrlQuery;
use Thelia\Model\RewritingUrl;
@@ -61,7 +63,21 @@ trait UrlRewritingTrait {
$this->setLocale($locale);
$title = $this->getTitle() ?: $this->getRef();
$generateEvent = new GenerateRewrittenUrlEvent($this, $locale);
$this->dispatchEvent(TheliaEvents::GENERATE_REWRITTENURL, $generateEvent);
if($generateEvent->isRewritten())
{
return $generateEvent->getUrl();
}
$title = $this->getTitle();
if(null == $title) {
throw new \RuntimeException('Impossible to create an url if title is null');
}
// Replace all weird characters with dashes
$string = preg_replace('/[^\w\-~_\.]+/u', '-', $title);
@@ -104,7 +120,7 @@ trait UrlRewritingTrait {
->filterByViewLocale($locale)
->filterByView($this->getRewrittenUrlViewName())
->filterByViewId($this->getId())
->filterByRedirected(0)
->filterByRedirected(null)
->findOne()
;

View File

@@ -0,0 +1,108 @@
<?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\Tests\Rewriting;
/**
* Class BaseRewritingObject
* @package Thelia\Tests\Rewriting
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
abstract class BaseRewritingObject extends \PHPUnit_Framework_TestCase
{
/**
* @return mixed an instance of Product, Folder, Content or Category Model
*/
abstract function getObject();
/**
* @covers Thelia\Model\Tools\UrlRewritingTrait::generateRewrittenUrl
*/
public function testSimpleFrenchRewrittenUrl()
{
$object = $this->getObject();
$object->setVisible(1)
->setPosition(1)
->setLocale('fr_FR')
->setTitle('Mon super titre en français')
->save();
$this->assertRegExp('/^mon-super-titre-en-français(-[0-9]+)?\.html$/', $object->getRewrittenUrl('fr_FR'));
$rewrittenUrl = $object->generateRewrittenUrl('fr_FR');
$this->assertNotNull($rewrittenUrl, "rewritten url can not be null");
$this->assertRegExp('/^mon-super-titre-en-français(-[0-9]+)?\.html$/', $rewrittenUrl);
//mon-super-titre-en-français-2.html
$object->delete();
}
/**
* @covers Thelia\Model\Tools\UrlRewritingTrait::generateRewrittenUrl
*/
public function testSimpleEnglishRewrittenUrl()
{
$object = $this->getObject();
$object->setVisible(1)
->setPosition(1)
->setLocale('en_US')
->setTitle('My english super Title')
->save();
$this->assertRegExp('/^my-english-super-title(-[0-9]+)?\.html$/', $object->getRewrittenUrl('en_US'));
$rewrittenUrl = $object->generateRewrittenUrl('en_US');
$this->assertNotNull($rewrittenUrl, "rewritten url can not be null");
$this->assertRegExp('/^my-english-super-title(-[0-9]+)?\.html$/', $rewrittenUrl);
$object->delete();
}
/**
* @covers Thelia\Model\Tools\UrlRewritingTrait::generateRewrittenUrl
* @expectedException \RuntimeException
* @expectedExceptionMessage Impossible to create an url if title is null
*/
public function testRewrittenWithoutTitle()
{
$object = $this->getObject();
$object->setVisible(1)
->setPosition(1)
->setLocale('en_US')
->setDescription('My english super Description')
->save();
}
/**
* @covers Thelia\Model\Tools\UrlRewritingTrait::generateRewrittenUrl
* @expectedException \RuntimeException
*/
public function testOnNotSavedObject()
{
$object = $this->getObject();
$object->generateRewrittenUrl('fr_FR');
}
}

View File

@@ -0,0 +1,43 @@
<?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\Tests\Rewriting;
use Thelia\Model\Category;
/**
* Class CategoryRewritingTest
* @package Thelia\Tests\Rewriting
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
class CategoryRewritingTest extends BaseRewritingObject
{
/**
* @return \Thelia\Model\Category
*/
function getObject()
{
return new Category();
}
}

View File

@@ -0,0 +1,43 @@
<?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\Tests\Rewriting;
use Thelia\Model\Content;
/**
* Class ContentRewritingTest
* @package Thelia\Tests\Rewriting
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
class ContentRewritingTest extends BaseRewritingObject
{
/**
* @return \Thelia\Model\Content
*/
function getObject()
{
return new Content();
}
}

View File

@@ -0,0 +1,43 @@
<?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\Tests\Rewriting;
use Thelia\Model\Folder;
/**
* Class FolderRewritingTest
* @package Thelia\Tests\Rewriting
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
class FolderRewritingTest extends BaseRewritingObject
{
/**
* @return mixed an instance of Product, Folder, Content or Category Model
*/
function getObject()
{
return new Folder();
}
}

View File

@@ -31,59 +31,14 @@ use Thelia\Model\ProductQuery;
* @package Thelia\Tests\Rewriting
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
class ProductRewriteTest extends \PHPUnit_Framework_TestCase
class ProductRewriteTest extends BaseRewritingObject
{
protected static $productId;
public static function setUpBeforeClass()
{
$product = new Product();
$product->setRef(sprintf("TestRewrittenProduct%s",uniqid()))
->setPosition(1)
->setVisible(1)
->setLocale('en_US')
->setTitle('My english super Title')
->setLocale('fr_FR')
->setTitle('Mon super titre en français')
->save();
self::$productId = $product->getId();
}
/**
* @covers Thelia\Model\Tools\UrlRewritingTrait::generateRewrittenUrl
* @return mixed an instance of Product, Folder, Content or Category Model
*/
public function testFrenchRewrittenUrl()
function getObject()
{
$product = ProductQuery::create()->findPk(self::$productId);
$rewrittenUrl = $product->generateRewrittenUrl('fr_FR');
$this->assertNotNull($rewrittenUrl, "rewritten url can not be null");
$this->assertRegExp('/^mon-super-titre-en-français(-[0-9]+)?\.html$/', $rewrittenUrl);
//mon-super-titre-en-français-2.html
}
/**
* @covers Thelia\Model\Tools\UrlRewritingTrait::generateRewrittenUrl
*/
public function testEnglishRewrittenUrl()
{
$product = ProductQuery::create()->findPk(self::$productId);
$rewrittenUrl = $product->generateRewrittenUrl('en_US');
$this->assertNotNull($rewrittenUrl, "rewritten url can not be null");
$this->assertRegExp('/^my-english-super-title(-[0-9]+)?\.html$/', $rewrittenUrl);
}
/**
* @covers Thelia\Model\Tools\UrlRewritingTrait::generateRewrittenUrl
* @expectedException \RuntimeException
* @expectedExceptionMessage Object product must be saved before generating url
*/
public function testOnNotSavedProduct()
{
$product = new Product();
$product->generateRewrittenUrl('fr_FR');
return new Product();
}
}

View File

@@ -19,7 +19,8 @@ INSERT INTO `config` (`name`, `value`, `secured`, `hidden`, `created_at`, `updat
('image_cache_dir_from_web_root', 'cache/images', 0, 0, NOW(), NOW()),
('document_cache_dir_from_web_root', 'cache/documents', 0, 0, NOW(), NOW()),
('currency_rate_update_url', 'http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml', 0, 0, NOW(), NOW()),
('page_not_found_view', '404.html', 0, 0, NOW(), NOW()),
('page_not_found_view', '404', 0, 0, NOW(), NOW()),
('passed_url_view', 'passed-url', 0, 0, NOW(), NOW()),
('use_tax_free_amounts', 0, 0, 0, NOW(), NOW()),
('process_assets', '1', 0, 0, NOW(), NOW()),
('thelia_admin_remember_me_cookie_name', 'tarmcn', 0, 0, NOW(), NOW()),

View File

@@ -264,7 +264,7 @@
var $elem = $('#jqplot');
var url = '/web/test_to_remove/admin-stats.json',
var url = "{url file='/test_to_remove/admin-stats.json'}",
series = [],
seriesColors = [],
ticks = [],