accessory loop

product loop
order fix
This commit is contained in:
Etienne Roudeix
2013-07-19 13:59:25 +02:00
parent 56719a4c46
commit c28ac536aa
8 changed files with 217 additions and 80 deletions

View File

@@ -5,6 +5,7 @@
xsi:schemaLocation="http://thelia.net/schema/dic/config http://thelia.net/schema/dic/config/thelia-1.0.xsd">
<loops>
<loop class="Thelia\Core\Template\Loop\Accessory" name="accessory"/>
<loop class="Thelia\Core\Template\Loop\Category" name="category"/>
<loop class="Thelia\Core\Template\Loop\Product" name="product"/>
<loop class="Thelia\Core\Template\Loop\Feed" name="feed"/>

View File

@@ -51,7 +51,7 @@ abstract class BaseLoop
protected $securityContext;
private $args;
protected $args;
/**
* Create a new Loop

View File

@@ -0,0 +1,119 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Core\Template\Loop;
use Thelia\Core\Template\Loop\Product;
use Propel\Runtime\ActiveQuery\Criteria;
use Propel\Runtime\ActiveQuery\Join;
use Thelia\Core\Template\Element\BaseLoop;
use Thelia\Core\Template\Element\LoopResult;
use Thelia\Core\Template\Element\LoopResultRow;
use Thelia\Core\Template\Loop\Argument\ArgumentCollection;
use Thelia\Core\Template\Loop\Argument\Argument;
use Thelia\Log\Tlog;
use Thelia\Model\AccessoryQuery;
use Thelia\Model\ProductQuery;
use Thelia\Model\ConfigQuery;
use Thelia\Type\TypeCollection;
use Thelia\Type;
/**
*
* Accessory loop
*
*
* Class Accessory
* @package Thelia\Core\Template\Loop
* @author Etienne Roudeix <eroudeix@openstudio.fr>
*/
class Accessory extends Product
{
/**
* @return ArgumentCollection
*/
protected function getArgDefinitions()
{
$argumentCollection = parent::getArgDefinitions();
$argumentCollection->addArgument(
Argument::createIntListTypeArgument('product', null, true)
);
$argumentCollection->get('order')->default = "accessory";
$argumentCollection->get('order')->type->getKey(0)->addValue('accessory');
$argumentCollection->get('order')->type->getKey(0)->addValue('accessory_reverse');
return $argumentCollection;
}
/**
* @param $pagination
*
* @return \Thelia\Core\Template\Element\LoopResult
*/
public function exec(&$pagination)
{
$search = AccessoryQuery::create();
$product = $this->getProduct();
$search->filterByProductId($product, Criteria::IN);
$order = $this->getOrder();
$orderByAccessory = array_search('accessory', $order);
$orderByAccessoryReverse = array_search('accessory_reverse', $order);
if($orderByAccessory !== false) {
$search->orderByPosition(Criteria::ASC);
$order[$orderByAccessory] = 'given_id';
$this->args->get('order')->setValue( implode(',', $order) );
}
if($orderByAccessoryReverse !== false) {
$search->orderByPosition(Criteria::DESC);
$order[$orderByAccessoryReverse] = 'given_id';
$this->args->get('order')->setValue( implode(',', $order) );
}
$accessories = $this->search($search);
$accessoryIdList = array(0);
foreach ($accessories as $accessory) {
array_push($accessoryIdList, $accessory->getAccessory());
}
$receivedIdList = $this->getId();
/* if an Id list is receive, loop will only match accessories from this list */
if($receivedIdList === null) {
$this->args->get('id')->setValue( implode(',', $accessoryIdList) );
} else {
$this->args->get('id')->setValue( implode(',', array_intersect($receivedIdList, $accessoryIdList)) );
}
return parent::exec($pagination);
}
}

View File

@@ -83,8 +83,9 @@ class Category extends BaseLoop
new Argument(
'order',
new TypeCollection(
new Type\EnumListType('alpha', 'alpha_reverse', 'reverse')
)
new Type\EnumListType(array('alpha', 'alpha_reverse', 'manual', 'manual-reverse', 'random'))
),
'manual'
),
Argument::createBooleanTypeArgument('random', 0),
Argument::createIntListTypeArgument('exclude')
@@ -139,34 +140,28 @@ class Category extends BaseLoop
$orders = $this->getOrder();
if(null === $orders) {
$search->orderByPosition();
} else {
foreach($orders as $order) {
switch ($order) {
case "alpha":
$search->addAscendingOrderByColumn(\Thelia\Model\CategoryI18nPeer::TITLE);
break;
case "alpha_reverse":
$search->addDescendingOrderByColumn(\Thelia\Model\CategoryI18nPeer::TITLE);
break;
case "reverse":
$search->orderByPosition(\Criteria::DESC);
break;
default:
$search->orderByPosition();
break;
}
foreach($orders as $order) {
switch ($order) {
case "alpha":
$search->addAscendingOrderByColumn(\Thelia\Model\Map\CategoryI18nTableMap::TITLE);
break;
case "alpha_reverse":
$search->addDescendingOrderByColumn(\Thelia\Model\Map\CategoryI18nTableMap::TITLE);
break;
case "manual-reverse":
$search->orderByPosition(Criteria::DESC);
break;
case "manual":
$search->orderByPosition(Criteria::ASC);
break;
case "random":
$search->clearOrderByColumns();
$search->addAscendingOrderByColumn('RAND()');
break(2);
break;
}
}
$random = $this->getRandom();
if ($random === true) {
$search->clearOrderByColumns();
$search->addAscendingOrderByColumn('RAND()');
}
/**
* \Criteria::INNER_JOIN in second parameter for joinWithI18n exclude query without translation.
*

View File

@@ -84,8 +84,9 @@ class Product extends BaseLoop
new Argument(
'order',
new TypeCollection(
new Type\EnumListType(array('alpha', 'alpha_reverse', 'reverse', 'min_price', 'max_price', 'manual', 'manual_reverse', 'ref', 'promo', 'new', 'random'))
)
new Type\EnumListType(array('alpha', 'alpha_reverse', 'reverse', 'min_price', 'max_price', 'manual', 'manual_reverse', 'ref', 'promo', 'new', 'random', 'given_id'))
),
'manual'
),
Argument::createIntListTypeArgument('exclude'),
Argument::createIntListTypeArgument('exclude_category'),
@@ -254,54 +255,56 @@ class Product extends BaseLoop
$orders = $this->getOrder();
if(null === $orders) {
$search->orderByPosition();
} else {
foreach($orders as $order) {
switch ($order) {
case "alpha":
$search->addAscendingOrderByColumn(\Thelia\Model\Map\CategoryI18nTableMap::TITLE);
$search->addAscendingOrderByColumn(\Thelia\Model\Map\CategoryI18nTableMap::TITLE);
break;
case "alpha_reverse":
$search->addDescendingOrderByColumn(\Thelia\Model\Map\CategoryI18nTableMap::TITLE);
break;
case "reverse":
$search->orderByPosition(Criteria::DESC);
break;
case "min_price":
$search->orderBy('real_price', Criteria::ASC);
break;
case "max_price":
$search->orderBy('real_price', Criteria::DESC);
break;
case "manual":
if(null === $this->category || count($this->category) != 1)
throw new \InvalidArgumentException('Manual order cannot be set without single category argument');
$search->addAscendingOrderByColumn(ProductTableMap::POSITION);
break;
case "manual_reverse":
if(null === $this->category || count($this->category) != 1)
throw new \InvalidArgumentException('Manual order cannot be set without single category argument');
$search->addDescendingOrderByColumn(ProductTableMap::POSITION);
break;
case "ref":
$search->addAscendingOrderByColumn(ProductTableMap::REF);
break;
case "promo":
$search->addDescendingOrderByColumn(ProductTableMap::PROMO);
break;
case "new":
$search->addDescendingOrderByColumn(ProductTableMap::NEWNESS);
break;
case "random":
$search->clearOrderByColumns();
$search->addAscendingOrderByColumn('RAND()');
break(2);
default:
$search->orderByPosition();
break;
}
foreach($orders as $order) {
switch ($order) {
case "alpha":
$search->addAscendingOrderByColumn(\Thelia\Model\Map\ProductI18nTableMap::TITLE);
break;
case "alpha_reverse":
$search->addDescendingOrderByColumn(\Thelia\Model\Map\ProductI18nTableMap::TITLE);
break;
case "reverse":
$search->orderByPosition(Criteria::DESC);
break;
case "min_price":
$search->orderBy('real_price', Criteria::ASC);
break;
case "max_price":
$search->orderBy('real_price', Criteria::DESC);
break;
case "manual":
if(null === $this->category || count($this->category) != 1)
throw new \InvalidArgumentException('Manual order cannot be set without single category argument');
$search->orderByPosition(Criteria::ASC);
break;
case "manual_reverse":
if(null === $this->category || count($this->category) != 1)
throw new \InvalidArgumentException('Manual order cannot be set without single category argument');
$search->orderByPosition(Criteria::DESC);
break;
case "ref":
$search->orderByRef(Criteria::ASC);
break;
case "promo":
$search->orderByPromo(Criteria::DESC);
break;
case "new":
$search->orderByNewness(Criteria::DESC);
break;
case "given_id":
if (!is_null($id)) {
foreach($id as $singleId) {
$givenIdMatched = 'given_id_matched_' . $singleId;
$search->withColumn(ProductTableMap::ID . "='$singleId'", $givenIdMatched);
$search->orderBy($givenIdMatched, Criteria::DESC);
}
}
break;
case "random":
$search->clearOrderByColumns();
$search->addAscendingOrderByColumn('RAND()');
break(2);
}
}

View File

@@ -38,6 +38,12 @@ class EnumListType implements TypeInterface
$this->values = $values;
}
public function addValue($value)
{
if(!in_array($value, $this->values))
$this->values[] = $value;
}
public function getType()
{
return 'Enum list type';

View File

@@ -142,4 +142,9 @@ class TypeCollection implements \Iterator
return null;
}
public function getKey($key)
{
return isset($this->types[$key]) ? $this->types[$key] : null;
}
}