loop product

This commit is contained in:
Etienne Roudeix
2013-07-02 16:33:43 +02:00
parent 23aaa4951b
commit 40c0da5553
8 changed files with 164 additions and 60 deletions

View File

@@ -6,6 +6,7 @@
<loops>
<loop class="Thelia\Core\Template\Loop\Category" name="category"/>
<loop class="Thelia\Core\Template\Loop\Product" name="product"/>
</loops>

View File

@@ -72,12 +72,25 @@ class Argument
);
}
public static function createFloatTypeArgument($name, $default=null, $mandatory=false, $empty=true)
{
return new Argument(
$name,
new TypeCollection(
new Type\FloatType()
),
$default,
$mandatory,
$empty
);
}
public static function createBooleanTypeArgument($name, $default=null, $mandatory=false, $empty=true)
{
return new Argument(
$name,
new TypeCollection(
new Type\BoolType()
new Type\BooleanType()
),
$default,
$mandatory,

View File

@@ -84,9 +84,9 @@ class Category extends BaseLoop
return new ArgumentCollection(
Argument::createIntListTypeArgument('id'),
Argument::createIntTypeArgument('parent'),
Argument::createIntTypeArgument('current'),
Argument::createIntTypeArgument('not_empty', 0),
Argument::createIntTypeArgument('visible', 1),
Argument::createBooleanTypeArgument('current'),
Argument::createBooleanTypeArgument('not_empty', 0),
Argument::createBooleanTypeArgument('visible', 1),
Argument::createAnyTypeArgument('link'),
new Argument(
'order',
@@ -94,7 +94,7 @@ class Category extends BaseLoop
new Type\EnumType('alpha', 'alpha_reverse', 'reverse')
)
),
Argument::createIntTypeArgument('random', 0),
Argument::createBooleanTypeArgument('random', 0),
Argument::createIntListTypeArgument('exclude')
);
}
@@ -109,28 +109,28 @@ class Category extends BaseLoop
$search = CategoryQuery::create();
if (!is_null($this->id)) {
$search->filterById(explode(',', $this->id), \Criteria::IN);
$search->filterById($this->id, \Criteria::IN);
}
if (!is_null($this->parent)) {
$search->filterByParent($this->parent);
}
if ($this->current == 1) {
if ($this->current === true) {
$search->filterById($this->request->get("category_id"));
} elseif (null !== $this->current && $this->current == 0) {
} elseif ($this->current === false) {
$search->filterById($this->request->get("category_id"), \Criteria::NOT_IN);
}
if (!is_null($this->exclude)) {
$search->filterById(explode(",", $this->exclude), \Criteria::NOT_IN);
$search->filterById($this->exclude, \Criteria::NOT_IN);
}
if (!is_null($this->link)) {
$search->filterByLink($this->link);
}
$search->filterByVisible($this->visible);
$search->filterByVisible($this->visible ? 1 : 0);
switch ($this->order) {
case "alpha":
@@ -147,7 +147,7 @@ class Category extends BaseLoop
break;
}
if ($this->random == 1) {
if ($this->random === true) {
$search->clearOrderByColumns();
$search->addAscendingOrderByColumn('RAND()');
}

View File

@@ -31,6 +31,8 @@ use Thelia\Core\Template\Loop\Argument\ArgumentCollection;
use Thelia\Core\Template\Loop\Argument\Argument;
use Thelia\Log\Tlog;
use Thelia\Model\CategoryQuery;
use Thelia\Model\ProductCategoryQuery;
use Thelia\Model\ProductQuery;
use Thelia\Type\TypeCollection;
use Thelia\Type;
@@ -68,13 +70,17 @@ class Product extends BaseLoop
public $id;
public $ref;
public $category;
public $price;
public $price2;
public $new;
public $promo;
public $newness;
public $visible;
public $weight;
public $min_price;
public $max_price;
public $min_stock;
public $min_weight;
public $max_weight;
public $current;
public $current_category;
public $depth;
public $visible;
public $order;
public $random;
public $exclude;
@@ -94,25 +100,23 @@ class Product extends BaseLoop
),
Argument::createIntListTypeArgument('category'),
Argument::createBooleanTypeArgument('new'),
Argument::createIntTypeArgument('promo'),
Argument::createIntTypeArgument('max_prix'),
Argument::createIntTypeArgument('min_price'),
Argument::createBooleanTypeArgument('promo'),
Argument::createFloatTypeArgument('min_price'),
Argument::createFloatTypeArgument('max_prix'),
Argument::createIntTypeArgument('min_stock'),
Argument::createIntTypeArgument('min_weight'),
Argument::createIntTypeArgument('max_weight'),
Argument::createIntTypeArgument('current'),
Argument::createIntTypeArgument('current_category'),
Argument::createFloatTypeArgument('min_weight'),
Argument::createFloatTypeArgument('max_weight'),
Argument::createBooleanTypeArgument('current'),
Argument::createBooleanTypeArgument('current_category'),
Argument::createIntTypeArgument('depth'),
Argument::createIntTypeArgument('not_empty', 0),
Argument::createIntTypeArgument('visible', 1),
Argument::createAnyTypeArgument('link'),
Argument::createBooleanTypeArgument('visible', 1),
new Argument(
'order',
new TypeCollection(
new Type\EnumType('title_alpha', 'title_alpha_reverse', 'reverse', 'min_price', 'max_price', 'category', 'manual', 'ref', 'promo')
new Type\EnumType('alpha', 'alpha_reverse', 'reverse', 'min_price', 'max_price', 'category', 'manual', 'manual_reverse', 'ref', 'promo', 'new')
)
),
Argument::createIntTypeArgument('random', 0),
Argument::createBooleanTypeArgument('random', 0),
Argument::createIntListTypeArgument('exclude')
);
}
@@ -124,28 +128,65 @@ class Product extends BaseLoop
*/
public function exec(&$pagination)
{
$search = CategoryQuery::create();
$search = ProductQuery::create();
if (!is_null($this->id)) {
$search->filterById(explode(',', $this->id), \Criteria::IN);
$search->filterById($this->id, \Criteria::IN);
}
if (!is_null($this->parent)) {
$search->filterByParent($this->parent);
if (!is_null($this->ref)) {
$search->filterByRef($this->ref, \Criteria::IN);
}
if ($this->current == 1) {
$search->filterById($this->request->get("category_id"));
} elseif (null !== $this->current && $this->current == 0) {
$search->filterById($this->request->get("category_id"), \Criteria::NOT_IN);
if (!is_null($this->category)) {
$search->filterByCategory(
CategoryQuery::create()->filterById($this->category, \Criteria::IN)->find(),
\Criteria::IN
);
}
if (!is_null($this->exclude)) {
$search->filterById(explode(",", $this->exclude), \Criteria::NOT_IN);
if ($this->new === true) {
$search->filterByNewness(1, \Criteria::EQUAL);
} else if($this->new === false) {
$search->filterByNewness(0, \Criteria::EQUAL);
}
if (!is_null($this->link)) {
$search->filterByLink($this->link);
if ($this->promo === true) {
$search->filterByPromo(1, \Criteria::EQUAL);
} else if($this->promo === false) {
$search->filterByNewness(0, \Criteria::EQUAL);
}
$search->filterByPriceDependingOnPromo($this->min_price, $this->max_price); //@todo review
if ($this->current === true) {
$search->filterById($this->request->get("product_id"));
} elseif($this->current === false) {
$search->filterById($this->request->get("product_id"), \Criteria::NOT_IN);
}
if ($this->current_category === true) {
$search->filterByCategory(
CategoryQuery::create()->filterByProduct(
ProductCategoryQuery::create()->filterByProductId(
$this->request->get("product_id"),
\Criteria::EQUAL
)->find(),
\Criteria::IN
)->find(),
\Criteria::IN
);
} elseif($this->current_category === false) {
$search->filterByCategory(
CategoryQuery::create()->filterByProduct(
ProductCategoryQuery::create()->filterByProductId(
$this->request->get("product_id"),
\Criteria::EQUAL
)->find(),
\Criteria::IN
)->find(),
\Criteria::NOT_IN
);
}
$search->filterByVisible($this->visible);
@@ -160,16 +201,44 @@ class Product extends BaseLoop
case "reverse":
$search->orderByPosition(\Criteria::DESC);
break;
/*case "min_price":
$search->orderByPosition(\Criteria::DESC);
break;
case "max_price":
$search->orderByPosition(\Criteria::DESC);
break;
case "category":
$search->orderByPosition(\Criteria::DESC);
break;*/
case "manual":
$search->addAscendingOrderByColumn(\Thelia\Model\ProductPeer::POSITION);
break;
case "manual_reverse":
$search->addDescendingOrderByColumn(\Thelia\Model\ProductPeer::POSITION);
break;
case "ref":
$search->addAscendingOrderByColumn(\Thelia\Model\ProductPeer::REF);
break;
case "promo":
$search->addDescendingOrderByColumn(\Thelia\Model\ProductPeer::PROMO);
break;
case "new":
$search->addDescendingOrderByColumn(\Thelia\Model\ProductPeer::NEWNESS);
break;
default:
$search->orderByPosition();
break;
}
if ($this->random == 1) {
if ($this->random === true) {
$search->clearOrderByColumns();
$search->addAscendingOrderByColumn('RAND()');
}
if (!is_null($this->exclude)) {
$search->filterById($this->exclude, \Criteria::NOT_IN);
}
/**
* \Criteria::INNER_JOIN in second parameter for joinWithI18n exclude query without translation.
*
@@ -177,24 +246,21 @@ class Product extends BaseLoop
*/
$search->joinWithI18n($this->request->getSession()->get('locale', 'en_US'), \Criteria::INNER_JOIN);
$categories = $this->search($search, $pagination);
$products = $this->search($search, $pagination);
$loopResult = new LoopResult();
foreach ($categories as $category) {
if ($this->not_empty && $category->countAllProducts() == 0) continue;
foreach ($products as $product) {
$loopResultRow = new LoopResultRow();
$loopResultRow->set("TITLE",$category->getTitle());
$loopResultRow->set("CHAPO", $category->getChapo());
$loopResultRow->set("DESCRIPTION", $category->getDescription());
$loopResultRow->set("POSTSCRIPTUM", $category->getPostscriptum());
$loopResultRow->set("PARENT", $category->getParent());
$loopResultRow->set("ID", $category->getId());
$loopResultRow->set("URL", $category->getUrl());
$loopResultRow->set("LINK", $category->getLink());
$loopResultRow->set("NB_CHILD", $category->countChild());
$loopResultRow->set("ID", $product->getId());
$loopResultRow->set("REF",$product->getRef());
$loopResultRow->set("TITLE",$product->getTitle());
$loopResultRow->set("CHAPO", $product->getChapo());
$loopResultRow->set("DESCRIPTION", $product->getDescription());
$loopResultRow->set("POSTSCRIPTUM", $product->getPostscriptum());
//$loopResultRow->set("CATEGORY", $product->getCategory());
//$loopResultRow->set("URL", $product->getUrl());
$loopResult->addRow($loopResultRow);
}

View File

@@ -78,7 +78,6 @@ class TheliaLoop implements SmartyPluginInterface
if (empty($params['name']))
throw new \InvalidArgumentException("Missing 'name' parameter in loop arguments");
if (empty($params['type']))
throw new \InvalidArgumentException("Missing 'type' parameter in loop arguments");
@@ -279,7 +278,7 @@ class TheliaLoop implements SmartyPluginInterface
$argumentsCollection = $loop->getArgs();
foreach( $argumentsCollection as $argument ) {
$value = isset($smartyParam[$argument->name]) ? $smartyParam[$argument->name] : null;
$value = isset($smartyParam[$argument->name]) ? (string)$smartyParam[$argument->name] : null;
/* check if mandatory */
if($value === null && $argument->mandatory) {
@@ -304,11 +303,13 @@ class TheliaLoop implements SmartyPluginInterface
/* set default */
/* did it as last checking for we consider default value is acceptable no matter type or empty restriction */
if($value === null) {
$value = $argument->default;
if($value === null && $argument->default !== null) {
$value = (string)$argument->default;
}
$loop->{$argument->name} = $value === null ? null : $argument->type->getFormatedValue($value);
$test = $argument->type->getFormatedValue($value);
$loop->{$argument->name} = $value === null ? null : $test;
}
if (!empty($faultActor)) {

View File

@@ -18,4 +18,8 @@ use Thelia\Model\om\BaseProductPeer;
*/
class ProductPeer extends BaseProductPeer
{
public static function getPriceDependingOnPromoExpression()
{
return 'IF(' . self::PROMO . '=1, ' . self::PRICE2 . ', ' . self::PRICE . ')';
}
}

View File

@@ -18,4 +18,15 @@ use Thelia\Model\om\BaseProductQuery;
*/
class ProductQuery extends BaseProductQuery
{
public function filterByPriceDependingOnPromo($minPrice = null, $maxPrice = null)
{
if ($minPrice !== null) {
$this->where(ProductPeer::getPriceDependingOnPromoExpression() . ' ' . \Criteria::GREATER_EQUAL . ' ?', $minPrice);
}
if ($maxPrice !== null) {
$this->where(ProductPeer::getPriceDependingOnPromoExpression() . ' ' . \Criteria::LESS_EQUAL . ' ?', $maxPrice);
}
return $this;
}
}

View File

@@ -0,0 +1,8 @@
{loop name="category0" type="category" parent="0"}
<h2>1 - CATEGORY : #TITLE</h2>
<hr /><hr />
{loop name="category1" type="category" parent="#ID"}
<h3>2 - SUBCATEGORY : #TITLE</h3>
{/loop}
<h2>1bis - CATEGORY : #TITLE</h2>
{/loop}