add all substitution for loop category

This commit is contained in:
Manuel Raynaud
2013-05-06 15:22:33 +02:00
parent 046f0e0f94
commit 78dd8514d1
6 changed files with 146 additions and 3 deletions

View File

@@ -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;
}

View File

@@ -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)
{
@@ -83,6 +84,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)
{

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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);

View File

@@ -7,8 +7,9 @@
<body>
<div>TODO write content<br />
<THELIA_cat type="category" order="alpha_reverse">
<THELIA_cat type="category" not_empty="1">
#__COUNT__ - #TITLE <br />
nb child : #NB_CHILD <br /><br />
</THELIA_cat>
<TEST_equal test="equal" variable="3" value="1">