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

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