From 60d09dc76e86c96f7690815850f7db727a848aaa Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Tue, 17 Sep 2013 19:11:44 +0200 Subject: [PATCH] refactor rewrriten method name --- core/lib/Thelia/Model/Category.php | 4 +- core/lib/Thelia/Model/Content.php | 4 +- core/lib/Thelia/Model/Folder.php | 4 +- core/lib/Thelia/Model/Product.php | 4 +- core/lib/Thelia/Model/RewritingUrl.php | 17 +++++ .../Thelia/Model/Tools/UrlRewritingTrait.php | 63 +++++++++++++++---- 6 files changed, 76 insertions(+), 20 deletions(-) diff --git a/core/lib/Thelia/Model/Category.php b/core/lib/Thelia/Model/Category.php index 042864de0..91b32eec7 100755 --- a/core/lib/Thelia/Model/Category.php +++ b/core/lib/Thelia/Model/Category.php @@ -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)); diff --git a/core/lib/Thelia/Model/Content.php b/core/lib/Thelia/Model/Content.php index 79660f93a..d626bfb10 100755 --- a/core/lib/Thelia/Model/Content.php +++ b/core/lib/Thelia/Model/Content.php @@ -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; } diff --git a/core/lib/Thelia/Model/Folder.php b/core/lib/Thelia/Model/Folder.php index 1e73e7e23..318edb7ac 100755 --- a/core/lib/Thelia/Model/Folder.php +++ b/core/lib/Thelia/Model/Folder.php @@ -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; } diff --git a/core/lib/Thelia/Model/Product.php b/core/lib/Thelia/Model/Product.php index 87b414dd9..d227cc9a2 100755 --- a/core/lib/Thelia/Model/Product.php +++ b/core/lib/Thelia/Model/Product.php @@ -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)); diff --git a/core/lib/Thelia/Model/RewritingUrl.php b/core/lib/Thelia/Model/RewritingUrl.php index 58dde9388..9dd826d01 100644 --- a/core/lib/Thelia/Model/RewritingUrl.php +++ b/core/lib/Thelia/Model/RewritingUrl.php @@ -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; + } } diff --git a/core/lib/Thelia/Model/Tools/UrlRewritingTrait.php b/core/lib/Thelia/Model/Tools/UrlRewritingTrait.php index f50da56d5..c9154a0db 100644 --- a/core/lib/Thelia/Model/Tools/UrlRewritingTrait.php +++ b/core/lib/Thelia/Model/Tools/UrlRewritingTrait.php @@ -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 !