refactor rewrriten method name

This commit is contained in:
Manuel Raynaud
2013-09-17 19:11:44 +02:00
parent 232ad70494
commit 60d09dc76e
6 changed files with 76 additions and 20 deletions

View File

@@ -28,7 +28,7 @@ class Category extends BaseCategory
/**
* {@inheritDoc}
*/
protected function getRewritenUrlViewName() {
protected function getRewrittenUrlViewName() {
return 'category';
}
@@ -69,7 +69,7 @@ class Category extends BaseCategory
{
$this->setPosition($this->getNextPosition());
$this->generateRewritenUrl($this->getLocale());
$this->generateRewrittenUrl($this->getLocale());
$this->dispatchEvent(TheliaEvents::BEFORE_CREATECATEGORY, new CategoryEvent($this));

View File

@@ -17,7 +17,7 @@ class Content extends BaseContent
/**
* {@inheritDoc}
*/
protected function getRewritenUrlViewName() {
protected function getRewrittenUrlViewName() {
return 'content';
}
@@ -37,7 +37,7 @@ class Content extends BaseContent
{
$this->setPosition($this->getNextPosition());
$this->generateRewritenUrl($this->getLocale());
$this->generateRewrittenUrl($this->getLocale());
return true;
}

View File

@@ -17,7 +17,7 @@ class Folder extends BaseFolder
/**
* {@inheritDoc}
*/
protected function getRewritenUrlViewName() {
protected function getRewrittenUrlViewName() {
return 'folder';
}
@@ -67,7 +67,7 @@ class Folder extends BaseFolder
{
$this->setPosition($this->getNextPosition());
$this->generateRewritenUrl($this->getLocale());
$this->generateRewrittenUrl($this->getLocale());
return true;
}

View File

@@ -21,7 +21,7 @@ class Product extends BaseProduct
/**
* {@inheritDoc}
*/
protected function getRewritenUrlViewName() {
protected function getRewrittenUrlViewName() {
return 'product';
}
@@ -106,7 +106,7 @@ class Product extends BaseProduct
{
$this->setPosition($this->getNextPosition());
$this->generateRewritenUrl($this->getLocale());
$this->generateRewrittenUrl($this->getLocale());
$this->dispatchEvent(TheliaEvents::BEFORE_CREATEPRODUCT, new ProductEvent($this));

View File

@@ -2,8 +2,25 @@
namespace Thelia\Model;
use Propel\Runtime\Connection\ConnectionInterface;
use Thelia\Model\Base\RewritingUrl as BaseRewritingUrl;
use Thelia\Model\RewritingUrlQuery;
class RewritingUrl extends BaseRewritingUrl {
public function preSave(ConnectionInterface $con = null)
{
if($this->getRedirected() == 0) {
//check if rewriting url alredy exists and put redirect to 1
RewritingUrlQuery::create()
->filterByView($this->getView())
->filterByViewId($this->getViewId())
->filterByViewLocale($this->getViewLocale())
->update(array(
"redirect" => 1
));
}
return true;
}
}

View File

@@ -24,7 +24,8 @@
namespace Thelia\Model\Tools;
use Thelia\Exception\UrlRewritingException;
use Thelia\Model\Rewriting;
use Thelia\Model\RewritingUrlQuery;
use Thelia\Model\RewritingUrl;
use Thelia\Tools\URL;
/**
* A trait for managing Rewriten URLs from model classes
@@ -34,7 +35,7 @@ trait UrlRewritingTrait {
/**
* @returns string the view name of the rewriten object (e.g., 'category', 'product')
*/
protected abstract function getRewritenUrlViewName();
protected abstract function getRewrittenUrlViewName();
/**
* Get the object URL for the given locale, rewriten if rewriting is enabled.
@@ -43,7 +44,7 @@ trait UrlRewritingTrait {
*/
public function getUrl($locale)
{
return URL::getInstance()->retrieve($this->getRewritenUrlViewName(), $this->getId(), $locale)->toString();
return URL::getInstance()->retrieve($this->getRewrittenUrlViewName(), $this->getId(), $locale)->toString();
}
/**
@@ -51,13 +52,22 @@ trait UrlRewritingTrait {
*
* @param string $locale a valid locale (e.g. en_US)
*/
public function generateRewritenUrl($locale)
public function generateRewrittenUrl($locale)
{
if ($this->isNew()) {
throw new \RuntimeException(sprintf('Object %s must be save before generating url', $this->getRewrittenUrlViewName()));
}
// Borrowed from http://stackoverflow.com/questions/2668854/sanitizing-strings-to-make-them-url-and-filename-safe
$this->setLocale($locale);
$title = $this->getTitle();
if (null === $title) {
throw new \RuntimeException(sprintf('Impossible to generate url if title does not exists for the locale %s', $locale));
}
// Replace all weird characters with dashes
$string = preg_replace('/[^\w\-~_\.]+/u', '-', $this->getTitle());
$string = preg_replace('/[^\w\-~_\.]+/u', '-', $title);
// Only allow one dash separator at a time (and make string lowercase)
$cleanString = mb_strtolower(preg_replace('/--+/u', '-', $string), 'UTF-8');
@@ -67,30 +77,59 @@ trait UrlRewritingTrait {
// TODO :
// check if URL url already exists, and add a numeric suffix, or the like
try{
URL::getInstance()->resolve($urlFilePart);
$i=0;
while(URL::getInstance()->resolve($urlFilePart)) {
$i++;
$urlFilePart = sprintf("%s-%d.html",$cleanString, $i);
}
} catch (UrlRewritingException $e) {
$rewritingUrl = new RewritingUrl();
$rewritingUrl->setUrl($urlFilePart)
->setView($this->getRewrittenUrlViewName())
->setViewId($this->getId())
->setViewLocale($locale)
->setRedirected(0)
->save()
;
}
// insert the URL in the rewriting table
//URL::getInstance()->generateRewritenUrl($this->getRewritenUrlViewName(), $this->getId(), $locale, $this->getTitle());
return $urlFilePart;
}
/**
* return the rewriten URL for the given locale
*
* @param string $locale a valid locale (e.g. en_US)
* @return null
*/
public function getRewritenUrl($locale)
public function getRewrittenUrl($locale)
{
return "fake url - TODO";
$rewritingUrl = RewritingUrlQuery::create()
->filterByViewLocale($locale)
->filterByView($this->getRewrittenUrlViewName())
->filterByViewId($this->getId())
->filterByRedirected(0)
->findOne()
;
if($rewritingUrl) {
$url = $rewritingUrl->getUrl();
} else {
$url = null;
}
return $url;
}
/**
* Set the rewriten URL for the given locale
*
* @param string $locale a valid locale (e.g. en_US)
* @param $url the wanted url
* @return $this
*/
public function setRewritenUrl($locale, $url)
public function setRewrittenUrl($locale, $url)
{
// TODO - code me !