diff --git a/core/lib/Thelia/Core/Template/Loop/Category.php b/core/lib/Thelia/Core/Template/Loop/Category.php index faba2f288..962acc794 100644 --- a/core/lib/Thelia/Core/Template/Loop/Category.php +++ b/core/lib/Thelia/Core/Template/Loop/Category.php @@ -61,7 +61,12 @@ class Category extends BaseLoop { ); } - + /** + * + * + * @param \Thelia\Tpex\Element\Loop\text $text + * @return mixed|string + */ public function exec($text) { $search = CategoryQuery::create(); @@ -91,8 +96,10 @@ class Category extends BaseLoop { if($this->limit > -1) { $search->limit($this->limit); } + $search->filterByVisible($this->visible); $search->offset($this->offset); + switch($this->order) { case "alpha": $search->addAscendingOrderByColumn(\Thelia\Model\CategoryI18nPeer::TITLE); @@ -112,16 +119,31 @@ class Category extends BaseLoop { $search->clearOrderByColumns(); $search->addAscendingOrderByColumn('RAND()'); } - $search->joinWithI18n('en_US'); + + /** + * \Criteria::INNER_JOIN in second parameter for joinWithI18n exclude query without translation. + * + * @todo : verify here if we want results for row without translations. + */ + $search->joinWithI18n($this->request->getSession()->get('locale', 'en_US'), \Criteria::INNER_JOIN); $categories = $search->find(); $res = ""; foreach ($categories as $category) { + + if ($this->not_empty && $category->countAllProducts() == 0) continue; + $temp = str_replace("#TITLE", $category->getTitle(), $text); $temp = str_replace("#CHAPO", $category->getChapo(), $temp); + $temp = str_replace("#DESCRIPTION", $category->getDescription(), $temp); + $temp = str_replace("#POSTSCRIPTUM", $category->getPostscriptum(), $temp); + $temp = str_replace("#PARENT", $category->getParent(), $temp); $temp = str_replace("#ID", $category->getId(), $temp); + $temp = str_replace("#URL", $category->getUrl(), $temp); + $temp = str_replace("#LINK", $category->getLink(), $temp); + $temp = str_replace("#NB_CHILD", $category->countChild(), $temp); $res .= $temp; } diff --git a/core/lib/Thelia/Core/TheliaHttpKernel.php b/core/lib/Thelia/Core/TheliaHttpKernel.php index 00b038d08..57ff21e54 100644 --- a/core/lib/Thelia/Core/TheliaHttpKernel.php +++ b/core/lib/Thelia/Core/TheliaHttpKernel.php @@ -62,6 +62,7 @@ class TheliaHttpKernel extends HttpKernel * @throws \Exception When an Exception occurs during processing * * @api + * */ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true) { @@ -82,6 +83,36 @@ class TheliaHttpKernel extends HttpKernel return $response; } + + /* + + * @TODO define here all needed parameters like locale, currency, etc : + * + * + * LOCALE : + * define locale with this order : + * 1) locale define into plugins + * 2) locale paramter in request + * 3) current locale <-> locale + * 4) locale in session + * 5) default locale + * + * $lang déjà défini par un plugin + * 2) paramètre 'lang' dans l'URL courante + * 3) Correspondance URL courante <-> langue + * 4) Langue précédemment stockée en session + * 5) Defaut + * put this locale into session with keyname "locale" + * + * + * + * + * + */ + protected function initParam(Request $request) + { + + } protected function initSession(Request $request) { diff --git a/core/lib/Thelia/Model/Category.php b/core/lib/Thelia/Model/Category.php index 928e87465..f0b345dbb 100644 --- a/core/lib/Thelia/Model/Category.php +++ b/core/lib/Thelia/Model/Category.php @@ -3,6 +3,7 @@ namespace Thelia\Model; use Thelia\Model\om\BaseCategory; +use Thelia\Model\CategoryQuery; /** @@ -18,4 +19,42 @@ use Thelia\Model\om\BaseCategory; */ class Category extends BaseCategory { + + /** + * @return int number of child for the current category + */ + public function countChild() + { + return CategoryQuery::countChild($this->getId()); + } + + public function getUrl() + { + + } + + /** + * + * count all products for current category and sub categories + * + * @return int + */ + public function countAllProducts() + { + $children = CategoryQuery::findAllChild($this->getId()); + array_push($children, $this); + + $countProduct = 0; + + foreach($children as $child) + { + $countProduct += ProductQuery::create() + ->filterByCategory($child) + ->count(); + } + + return $countProduct; + + } + } diff --git a/core/lib/Thelia/Model/CategoryQuery.php b/core/lib/Thelia/Model/CategoryQuery.php index 3a79b3d3a..61ff52dfb 100644 --- a/core/lib/Thelia/Model/CategoryQuery.php +++ b/core/lib/Thelia/Model/CategoryQuery.php @@ -18,4 +18,44 @@ use Thelia\Model\om\BaseCategoryQuery; */ class CategoryQuery extends BaseCategoryQuery { + + /** + * + * count how many direct children have a category + * + * @param int $parent category parent id + * @return int + */ + public static function countChild($parent) + { + return self::create()->filterByParent($parent)->count(); + } + + /** + * + * find all category children for a given category. an array of \Thelia\Model\Category is return + * + * @param $categoryId the category id + * @param int $depth max depth you want to search + * @param int $currentPos don't change this param, it is used for recursion + * @return \Thelia\Model\Category[] + */ + public static function findAllChild($categoryId, $depth = 0, $currentPos = 0) + { + $result = array(); + $currentPos++; + + if($depth == $currentPos && $depth != 0) return; + + $categories = self::create() + ->filterByParent($categoryId) + ->find(); + + foreach ($categories as $category) { + array_push($result, $category); + $result = array_merge($result, (array) self::findAllChild($category->getId(), $depth, $currentPos)); + } + + return $result; + } } diff --git a/install/faker.php b/install/faker.php index 28cdb5da1..de6f6fdfb 100644 --- a/install/faker.php +++ b/install/faker.php @@ -37,6 +37,16 @@ try { $jeans->save(); + //third category + $other = new Thelia\Model\Category(); + $other->setParent($jeans->getId()); + $other->setVisible(1); + $other->setPosition(3); + $other->setDescription($faker->text(255)); + $other->setTitle($faker->bs); + + $other->save(); + for ($i=1; $i <= 5; $i++) { $product = new \Thelia\Model\Product(); $product->addCategory($sweet); diff --git a/templates/default/index.html b/templates/default/index.html index 6a844d33f..76a107aba 100644 --- a/templates/default/index.html +++ b/templates/default/index.html @@ -7,8 +7,9 @@
TODO write content
- + #__COUNT__ - #TITLE
+ nb child : #NB_CHILD