Merge branch 'master' of github.com:thelia/thelia

This commit is contained in:
Manuel Raynaud
2013-07-09 09:52:38 +02:00
23 changed files with 275 additions and 127 deletions

View File

@@ -56,6 +56,19 @@ abstract class BaseLoop
);
}
public function __call($name, $arguments) {
if (substr($name, 0, 3) == 'get') {
$argName = strtolower(substr($name, 3));
return $this->getArg($argName)->getValue();
}
throw new \InvalidArgumentException(sprintf("Unsupported magic method %s. only getArgname() is supported.", $name));
}
/**
* Create a new Loop
*
@@ -83,6 +96,7 @@ abstract class BaseLoop
$faultDetails = array();
while (($argument = $this->args->current()) !== false) {
$this->args->next();
$value = isset($nameValuePairs[$argument->name]) ? $nameValuePairs[$argument->name] : null;
@@ -114,8 +128,6 @@ abstract class BaseLoop
}
$argument->setValue($value);
$this->args->next();
}
if (!empty($faultActor)) {

View File

@@ -52,11 +52,11 @@ class Argument
}
public function getValue() {
return $this->value;
return $this->type->getFormattedValue($this->value);
}
public function setValue($value) {
$this->value = $value;
$this->value = $value === null ? null : (string)$value;
}
public static function createAnyTypeArgument($name, $default=null, $mandatory=false, $empty=true)

View File

@@ -83,7 +83,7 @@ class Category extends BaseLoop
new Argument(
'order',
new TypeCollection(
new Type\EnumType('alpha', 'alpha_reverse', 'reverse')
new Type\EnumListType('alpha', 'alpha_reverse', 'reverse')
)
),
Argument::createBooleanTypeArgument('random', 0),
@@ -100,21 +100,20 @@ class Category extends BaseLoop
{
$search = CategoryQuery::create();
$id = $this->getArgValue('id');
$id = $this->getId();
if (!is_null($id)) {
$search->filterById($id, Criteria::IN);
}
$parent = $this->getArgValue('parent');
$parent = $this->getParent();
if (!is_null($parent)) {
$search->filterByParent($parent);
}
$current = $this->getArgValue('current');
$current = $this->getCurrent();
if ($current === true) {
$search->filterById($this->request->get("category_id"));
@@ -123,22 +122,28 @@ class Category extends BaseLoop
}
$exclude = $this->getArgValue('exclude');
$exclude = $this->getExclude();
if (!is_null($exclude)) {
$search->filterById($exclude, Criteria::NOT_IN);
}
$link = $this->getArgValue('link');
$link = $this->getLink();
if (!is_null($link)) {
$search->filterByLink($link);
}
$search->filterByVisible($this->getArgValue('visible') ? 1 : 0);
$search->filterByVisible($this->getVisible() ? 1 : 0);
switch ($this->getArgValue('order')) {
$orders = $this->getOrder();
if(null === $orders) {
$search->orderByPosition();
} else {
foreach($orders as $order) {
switch ($order) {
case "alpha":
$search->addAscendingOrderByColumn(\Thelia\Model\CategoryI18nPeer::TITLE);
break;
@@ -152,9 +157,12 @@ class Category extends BaseLoop
$search->orderByPosition();
break;
}
}
}
$random = $this->getRandom();
if ($this->getArgValue('random') === true) {
if ($random === true) {
$search->clearOrderByColumns();
$search->addAscendingOrderByColumn('RAND()');
}

View File

@@ -44,17 +44,8 @@ class Feed extends BaseLoop
public function getArgDefinitions()
{
return new ArgumentCollection(
new Argument(
'url',
new TypeCollection(new Type\AnyType())
),
new Argument(
'timeout',
new TypeCollection(
new Type\IntType()
),
10
)
Argument::createAnyTypeArgument('url', null, true),
Argument::createIntTypeArgument('timeout', 10)
);
}
@@ -73,17 +64,17 @@ class Feed extends BaseLoop
}
}
$feed = new \SimplePie($this->getArgValue('url'), THELIA_ROOT . 'cache/feeds');
$feed = new \SimplePie($this->getUrl(), THELIA_ROOT . 'cache/feeds');
$feed->init();
$feed->handle_content_type();
$feed->set_timeout($this->getArgValue('timeout'));
$feed->set_timeout($this->getTimeout());
$items = $feed->get_items();
$limit = min(count($items), $this->getArgValue('limit'));
$limit = min(count($items), $this->getLimit());
$loopResult = new LoopResult();

View File

@@ -24,7 +24,6 @@
namespace Thelia\Core\Template\Loop;
use Propel\Runtime\ActiveQuery\Criteria;
use Thelia\Core\Template\Element\BaseLoop;
use Thelia\Core\Template\Element\LoopResult;
use Thelia\Core\Template\Element\LoopResultRow;
@@ -34,8 +33,8 @@ use Thelia\Core\Template\Loop\Argument\Argument;
use Thelia\Log\Tlog;
use Thelia\Model\CategoryQuery;
use Thelia\Model\Map\ProductTableMap;
use Thelia\Model\ProductCategoryQuery;
use Thelia\Model\ProductPeer;
use Thelia\Model\ProductQuery;
use Thelia\Model\ConfigQuery;
use Thelia\Type\TypeCollection;
@@ -80,7 +79,7 @@ class Product extends BaseLoop
new Argument(
'order',
new TypeCollection(
new Type\EnumType(array('alpha', 'alpha_reverse', 'reverse', 'min_price', 'max_price', 'category', 'manual', 'manual_reverse', 'ref', 'promo', 'new'))
new Type\EnumListType(array('alpha', 'alpha_reverse', 'reverse', 'min_price', 'max_price', 'manual', 'manual_reverse', 'ref', 'promo', 'new'))
)
),
Argument::createBooleanTypeArgument('random', 0),
@@ -97,24 +96,26 @@ class Product extends BaseLoop
{
$search = ProductQuery::create();
$id = $this->getArgValue('id');
$search->withColumn('CASE WHEN ' . ProductTableMap::PROMO . '=1 THEN ' . ProductTableMap::PRICE2 . ' ELSE ' . ProductTableMap::PRICE . ' END', 'real_price');
$id = $this->getId();
if (!is_null($id)) {
$search->filterById($id, Criteria::IN);
}
$ref = $this->getArgValue('ref');
$ref = $this->getRef();
if (!is_null($ref)) {
$search->filterByRef($ref, Criteria::IN);
}
$category = $this->getArgValue('category');
$category = $this->getCategory();
if (!is_null($category)) {
$categories = CategoryQuery::create()->filterById($category, Criteria::IN)->find();
$depth = $this->getArgValue('depth');
$depth = $this->getDepth();
if(null !== $depth) {
foreach(CategoryQuery::findAllChild($category, $depth) as $subCategory) {
@@ -128,7 +129,7 @@ class Product extends BaseLoop
);
}
$new = $this->getArgValue('new');
$new = $this->getNew();
if ($new === true) {
$search->filterByNewness(1, Criteria::EQUAL);
@@ -136,7 +137,7 @@ class Product extends BaseLoop
$search->filterByNewness(0, Criteria::EQUAL);
}
$promo = $this->getArgValue('promo');
$promo = $this->getPromo();
if ($promo === true) {
$search->filterByPromo(1, Criteria::EQUAL);
@@ -144,49 +145,59 @@ class Product extends BaseLoop
$search->filterByNewness(0, Criteria::EQUAL);
}
$min_stock = $this->getArgValue('min_stock');
$min_stock = $this->getMin_stock();
if (null != $min_stock) {
$search->filterByQuantity($min_stock, Criteria::GREATER_EQUAL);
}
$min_price = $this->getArgValue('min_price');
$min_price = $this->getMin_price();
if(null !== $min_price) {
$search->condition('in_promo', ProductPeer::PROMO . Criteria::EQUAL . '1')
->condition('not_in_promo', ProductPeer::PROMO . Criteria::NOT_EQUAL . '1')
->condition('min_price2', ProductPeer::PRICE2 . Criteria::GREATER_EQUAL . '?', $min_price)
->condition('min_price', ProductPeer::PRICE . Criteria::GREATER_EQUAL . '?', $min_price)
/**
* Following should work but does not :
*
* $search->filterBy('real_price', $max_price, Criteria::GREATER_EQUAL);
*/
$search->condition('in_promo', ProductTableMap::PROMO . Criteria::EQUAL . '1')
->condition('not_in_promo', ProductTableMap::PROMO . Criteria::NOT_EQUAL . '1')
->condition('min_price2', ProductTableMap::PRICE2 . Criteria::GREATER_EQUAL . '?', $min_price)
->condition('min_price', ProductTableMap::PRICE . Criteria::GREATER_EQUAL . '?', $min_price)
->combine(array('in_promo', 'min_price2'), Criteria::LOGICAL_AND, 'in_promo_min_price')
->combine(array('not_in_promo', 'min_price'), Criteria::LOGICAL_AND, 'not_in_promo_min_price')
->where(array('not_in_promo_min_price', 'in_promo_min_price'), Criteria::LOGICAL_OR);
}
$max_price = $this->getArgValue('max_price');
$max_price = $this->getMax_price();
if(null !== $max_price) {
$search->condition('in_promo', ProductPeer::PROMO . Criteria::EQUAL . '1')
->condition('not_in_promo', ProductPeer::PROMO . Criteria::NOT_EQUAL . '1')
->condition('max_price2', ProductPeer::PRICE2 . Criteria::LESS_EQUAL . '?', $max_price)
->condition('max_price', ProductPeer::PRICE . Criteria::LESS_EQUAL . '?', $max_price)
/**
* Following should work but does not :
*
* $search->filterBy('real_price', $max_price, Criteria::LESS_EQUAL);
*/
$search->condition('in_promo', ProductTableMap::PROMO . Criteria::EQUAL . '1')
->condition('not_in_promo', ProductTableMap::PROMO . Criteria::NOT_EQUAL . '1')
->condition('max_price2', ProductTableMap::PRICE2 . Criteria::LESS_EQUAL . '?', $max_price)
->condition('max_price', ProductTableMap::PRICE . Criteria::LESS_EQUAL . '?', $max_price)
->combine(array('in_promo', 'max_price2'), Criteria::LOGICAL_AND, 'in_promo_max_price')
->combine(array('not_in_promo', 'max_price'), Criteria::LOGICAL_AND, 'not_in_promo_max_price')
->where(array('not_in_promo_max_price', 'in_promo_max_price'), Criteria::LOGICAL_OR);
}
$min_weight = $this->getArgValue('min_weight');
$min_weight = $this->getMin_weight();
if(null !== $min_weight) {
$search->filterByWeight($min_weight, Criteria::GREATER_EQUAL);
}
$max_weight = $this->getArgValue('max_weight');
$max_weight = $this->getMax_weight();
if(null !== $max_weight) {
$search->filterByWeight($max_weight, Criteria::LESS_EQUAL);
}
$current = $this->getArgValue('current');
$current = $this->getCurrent();
if ($current === true) {
$search->filterById($this->request->get("product_id"));
@@ -194,7 +205,7 @@ class Product extends BaseLoop
$search->filterById($this->request->get("product_id"), Criteria::NOT_IN);
}
$current_category = $this->getArgValue('current_category');
$current_category = $this->getCurrent_category();
if ($current_category === true) {
$search->filterByCategory(
@@ -220,54 +231,65 @@ class Product extends BaseLoop
);
}
$search->filterByVisible($this->getArgValue('visible'));
$search->filterByVisible($this->getVisible());
switch ($this->getArgValue('order')) {
$orders = $this->getOrder();
if(null === $orders) {
$search->orderByPosition();
} else {
foreach($orders as $order) {
switch ($order) {
case "alpha":
$search->addAscendingOrderByColumn(\Thelia\Model\CategoryI18nPeer::TITLE);
$search->addAscendingOrderByColumn(\Thelia\Model\Map\CategoryI18nTableMap::TITLE);
$search->addAscendingOrderByColumn(\Thelia\Model\Map\CategoryI18nTableMap::TITLE);
break;
case "alpha_reverse":
$search->addDescendingOrderByColumn(\Thelia\Model\CategoryI18nPeer::TITLE);
$search->addDescendingOrderByColumn(\Thelia\Model\Map\CategoryI18nTableMap::TITLE);
break;
case "reverse":
$search->orderByPosition(Criteria::DESC);
break;
case "min_price":
//$search->order
//$search->orderByPosition(Criteria::DESC);
$search->orderBy('real_price', Criteria::ASC);
break;
/*case "max_price":
$search->orderByPosition(Criteria::DESC);
case "max_price":
$search->orderBy('real_price', Criteria::DESC);
break;
case "category":
$search->orderByPosition(Criteria::DESC);
break;*/
case "manual":
$search->addAscendingOrderByColumn(\Thelia\Model\ProductPeer::POSITION);
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":
$search->addDescendingOrderByColumn(\Thelia\Model\ProductPeer::POSITION);
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(\Thelia\Model\ProductPeer::REF);
$search->addAscendingOrderByColumn(ProductTableMap::REF);
break;
case "promo":
$search->addDescendingOrderByColumn(\Thelia\Model\ProductPeer::PROMO);
$search->addDescendingOrderByColumn(ProductTableMap::PROMO);
break;
case "new":
$search->addDescendingOrderByColumn(\Thelia\Model\ProductPeer::NEWNESS);
$search->addDescendingOrderByColumn(ProductTableMap::NEWNESS);
break;
default:
$search->orderByPosition();
break;
}
}
}
if ($this->getArgValue('random') === true) {
$random = $this->getRandom();
if ($random === true) {
$search->clearOrderByColumns();
$search->addAscendingOrderByColumn('RAND()');
}
$exclude = $this->getArgValue('exclude');
$exclude = $this->getExclude();
if (!is_null($exclude)) {
$search->filterById($exclude, Criteria::NOT_IN);

View File

@@ -42,7 +42,7 @@ class AlphaNumStringListTypeTest extends \PHPUnit_Framework_TestCase
public function testFormatAlphaNumStringListType()
{
$type = new AlphaNumStringListType();
$this->assertTrue(is_array($type->getFormatedValue('FOO1,FOO_2,FOO-3')));
$this->assertNull($type->getFormatedValue('5€'));
$this->assertTrue(is_array($type->getFormattedValue('FOO1,FOO_2,FOO-3')));
$this->assertNull($type->getFormattedValue('5€'));
}
}

View File

@@ -48,8 +48,8 @@ class BooleanTypeTest extends \PHPUnit_Framework_TestCase
public function testFormatBooleanType()
{
$booleanType = new BooleanType();
$this->assertTrue($booleanType->getFormatedValue('yes'));
$this->assertFalse($booleanType->getFormatedValue('no'));
$this->assertNull($booleanType->getFormatedValue('foo'));
$this->assertTrue($booleanType->getFormattedValue('yes'));
$this->assertFalse($booleanType->getFormattedValue('no'));
$this->assertNull($booleanType->getFormattedValue('foo'));
}
}

View File

@@ -0,0 +1,50 @@
<?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\Tests\Type;
use Thelia\Type\EnumListType;
/**
*
* @author Etienne Roudeix <eroudeix@openstudio.fr>
*
*/
class EnumListTypeTest extends \PHPUnit_Framework_TestCase
{
public function testEnumListType()
{
$enumListType = new EnumListType(array("cat", "dog", "frog"));
$this->assertTrue($enumListType->isValid('cat'));
$this->assertTrue($enumListType->isValid('cat,dog'));
$this->assertFalse($enumListType->isValid('potato'));
$this->assertFalse($enumListType->isValid('cat,monkey'));
}
public function testFormatEnumListType()
{
$enumListType = new EnumListType(array("cat", "dog", "frog"));
$this->assertTrue(is_array($enumListType->getFormattedValue('cat,dog')));
$this->assertNull($enumListType->getFormattedValue('cat,monkey'));
}
}

View File

@@ -43,7 +43,7 @@ class IntListTypeTest extends \PHPUnit_Framework_TestCase
public function testFormatIntListType()
{
$intListType = new IntListType();
$this->assertTrue(is_array($intListType->getFormatedValue('1,2,3')));
$this->assertNull($intListType->getFormatedValue('foo'));
$this->assertTrue(is_array($intListType->getFormattedValue('1,2,3')));
$this->assertNull($intListType->getFormattedValue('foo'));
}
}

View File

@@ -42,7 +42,7 @@ class JsonTypeTest extends \PHPUnit_Framework_TestCase
public function testFormatJsonType()
{
$jsonType = new JsonType();
$this->assertTrue(is_array($jsonType->getFormatedValue('{"k0":"v0","k1":"v1","k2":"v2"}')));
$this->assertNull($jsonType->getFormatedValue('foo'));
$this->assertTrue(is_array($jsonType->getFormattedValue('{"k0":"v0","k1":"v1","k2":"v2"}')));
$this->assertNull($jsonType->getFormattedValue('foo'));
}
}

View File

@@ -45,7 +45,7 @@ class AlphaNumStringListType implements TypeInterface
return true;
}
public function getFormatedValue($values)
public function getFormattedValue($values)
{
return $this->isValid($values) ? explode(',', $values) : null;
}

View File

@@ -40,7 +40,7 @@ class AlphaNumStringType implements TypeInterface
return preg_match('#^[a-zA-Z0-9\-_]+$#', $value) ? true : false;
}
public function getFormatedValue($value)
public function getFormattedValue($value)
{
return $this->isValid($value) ? $value : null;
}

View File

@@ -40,7 +40,7 @@ class AnyType implements TypeInterface
return true;
}
public function getFormatedValue($value)
public function getFormattedValue($value)
{
return $value;
}

View File

@@ -53,7 +53,7 @@ class BooleanType implements TypeInterface
return in_array($value, $this->trueValuesArray) || in_array($value, $this->falseValuesArray);
}
public function getFormatedValue($value)
public function getFormattedValue($value)
{
return $this->isValid($value) ? ( in_array($value, $this->trueValuesArray) ) : null;
}

View File

@@ -0,0 +1,65 @@
<?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\Type;
/**
*
* @author Etienne Roudeix <eroudeix@openstudio.fr>
*
*/
class EnumListType implements TypeInterface
{
protected $values = array();
public function __construct($values = array())
{
if(is_array($values))
$this->values = $values;
}
public function getType()
{
return 'Enum list type';
}
public function isValid($values)
{
foreach(explode(',', $values) as $value) {
if(!$this->isSingleValueValid($value))
return false;
}
return true;
}
public function getFormattedValue($values)
{
return $this->isValid($values) ? explode(',', $values) : null;
}
public function isSingleValueValid($value)
{
return in_array($value, $this->values);
}
}

View File

@@ -48,7 +48,7 @@ class EnumType implements TypeInterface
return in_array($value, $this->values);
}
public function getFormatedValue($value)
public function getFogetFormattedValuermatedValue($value)
{
return $this->isValid($value) ? $value : null;
}

View File

@@ -40,7 +40,7 @@ class FloatType implements TypeInterface
return filter_var($value, FILTER_VALIDATE_FLOAT) === false ? false : true;
}
public function getFormatedValue($value)
public function getFormattedValue($value)
{
return $this->isValid($value) ? $value : null;
}

View File

@@ -45,7 +45,7 @@ class IntListType implements TypeInterface
return true;
}
public function getFormatedValue($values)
public function getFormattedValue($values)
{
return $this->isValid($values) ? explode(',', $values) : null;
}

View File

@@ -40,7 +40,7 @@ class IntType implements TypeInterface
return filter_var($value, FILTER_VALIDATE_INT) === false ? false : true;
}
public function getFormatedValue($value)
public function getFormattedValue($value)
{
return $this->isValid($value) ? $value : null;
}

View File

@@ -41,7 +41,7 @@ class JsonType implements TypeInterface
return (json_last_error() == JSON_ERROR_NONE);
}
public function getFormatedValue($value)
public function getFormattedValue($value)
{
return $this->isValid($value) ? json_decode($value, true) : null;
}

View File

@@ -133,10 +133,10 @@ class TypeCollection implements \Iterator
return false;
}
public function getFormatedValue($value) {
public function getFormattedValue($value) {
foreach($this as $type) {
if($type->isValid($value)) {
return $type->getFormatedValue($value);
return $type->getFormattedValue($value);
}
}

View File

@@ -34,5 +34,5 @@ interface TypeInterface
public function isValid($value);
public function getFormatedValue($value);
public function getFormattedValue($value);
}

View File

@@ -19,7 +19,7 @@
<hr />
{/loop*}
<h2>PRODUCTS</h2>
{loop name="product" type="product" order="min_price"}
{loop name="product" type="product" order="promo,min_price"}
<h3>PRODUCT : #REF / #TITLE</h3>
price : #PRICE €<br />
promo price : #PROMO_PRICE €<br />