From 6ea61ff68c79ea706c4038ebfaca924de5b1593c Mon Sep 17 00:00:00 2001 From: Etienne Roudeix Date: Thu, 24 Oct 2013 19:50:09 +0200 Subject: [PATCH] searchLoopInterface + use in OrderLoop --- .../Thelia/Core/Template/Element/BaseLoop.php | 70 +++++++++++- .../Element/Exception/SearchLoopException.php | 40 +++++++ .../Template/Element/SearchLoopInterface.php | 44 ++++++++ core/lib/Thelia/Core/Template/Loop/Order.php | 60 +++++++++- core/lib/Thelia/Model/OrderQuery.php | 2 + templates/admin/default/search.html | 103 ++++++++++++++++++ 6 files changed, 314 insertions(+), 5 deletions(-) create mode 100644 core/lib/Thelia/Core/Template/Element/Exception/SearchLoopException.php create mode 100644 core/lib/Thelia/Core/Template/Element/SearchLoopInterface.php create mode 100644 templates/admin/default/search.html diff --git a/core/lib/Thelia/Core/Template/Element/BaseLoop.php b/core/lib/Thelia/Core/Template/Element/BaseLoop.php index 7e803c43a..7ec77efbd 100755 --- a/core/lib/Thelia/Core/Template/Element/BaseLoop.php +++ b/core/lib/Thelia/Core/Template/Element/BaseLoop.php @@ -23,12 +23,18 @@ namespace Thelia\Core\Template\Element; +use Propel\Runtime\ActiveQuery\Criteria; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Thelia\Core\Template\Element\Exception\SearchLoopException; use Thelia\Core\Template\Loop\Argument\Argument; use Propel\Runtime\ActiveQuery\ModelCriteria; use Thelia\Core\Security\SecurityContext; +use Thelia\Type\AlphaNumStringListType; +use Thelia\Type\EnumListType; +use Thelia\Type\EnumType; +use Thelia\Type\TypeCollection; /** * @@ -84,13 +90,43 @@ abstract class BaseLoop */ protected function getDefaultArgs() { - return array( - Argument::createIntTypeArgument('offset', 0), - Argument::createIntTypeArgument('page'), - Argument::createIntTypeArgument('limit', PHP_INT_MAX), + $defaultArgs = array( Argument::createBooleanTypeArgument('backend_context', false), Argument::createBooleanTypeArgument('force_return', false), ); + + if(true === $this->countable) { + $defaultArgs = array_merge($defaultArgs, array( + Argument::createIntTypeArgument('offset', 0), + Argument::createIntTypeArgument('page'), + Argument::createIntTypeArgument('limit', PHP_INT_MAX), + )); + } + + if($this instanceof SearchLoopInterface) { + $defaultArgs = array_merge($defaultArgs, array( + Argument::createAnyTypeArgument('search_term'), + new Argument( + 'search_in', + new TypeCollection( + new EnumListType($this->getSearchIn()) + ) + ), + new Argument( + 'search_mode', + new TypeCollection( + new EnumType(array( + SearchLoopInterface::MODE_ANY_WORD, + SearchLoopInterface::MODE_SENTENCE, + SearchLoopInterface::MODE_STRICT_SENTENCE, + )) + ), + SearchLoopInterface::MODE_STRICT_SENTENCE + ) + )); + } + + return $defaultArgs; } /** @@ -205,6 +241,32 @@ abstract class BaseLoop */ protected function search(ModelCriteria $search, &$pagination = null) { + if($this instanceof SearchLoopInterface) { + $searchTerm = $this->getSearch_term(); + $searchIn = $this->getSearch_in(); + $searchMode = $this->getSearch_mode(); + if(null !== $searchTerm && null !== $searchIn) { + + switch($searchMode) { + case SearchLoopInterface::MODE_ANY_WORD: + $searchCriteria = Criteria::IN; + $searchTerm = explode(' ', $searchTerm); + break; + case SearchLoopInterface::MODE_SENTENCE: + $searchCriteria = Criteria::EQUAL; + $searchTerm = '%' . $searchTerm . '%'; + break; + case SearchLoopInterface::MODE_STRICT_SENTENCE: + $searchCriteria = Criteria::EQUAL; + break; + } + + $this->doSearch($search, $searchTerm, $searchIn, $searchCriteria); + + $in = 'true'; + } + } + if ($this->getArgValue('page') !== null) { return $this->searchWithPagination($search, $pagination); } else { diff --git a/core/lib/Thelia/Core/Template/Element/Exception/SearchLoopException.php b/core/lib/Thelia/Core/Template/Element/Exception/SearchLoopException.php new file mode 100644 index 000000000..f1ce6657f --- /dev/null +++ b/core/lib/Thelia/Core/Template/Element/Exception/SearchLoopException.php @@ -0,0 +1,40 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Core\Template\Element\Exception; + +class SearchLoopException extends \RuntimeException +{ + const UNKNOWN_EXCEPTION = 0; + + public function __construct($message, $code = null, $arguments = array(), $previous = null) + { + if (is_array($arguments)) { + $this->arguments = $arguments; + } + if ($code === null) { + $code = self::UNKNOWN_EXCEPTION; + } + parent::__construct($message, $code, $previous); + } +} diff --git a/core/lib/Thelia/Core/Template/Element/SearchLoopInterface.php b/core/lib/Thelia/Core/Template/Element/SearchLoopInterface.php new file mode 100644 index 000000000..e180105a6 --- /dev/null +++ b/core/lib/Thelia/Core/Template/Element/SearchLoopInterface.php @@ -0,0 +1,44 @@ +. */ +/* */ +/*************************************************************************************/ +namespace Thelia\Core\Template\Element; + +use Symfony\Component\Validator\ExecutionContextInterface; + +/** + * + * @author Etienne Roudeix + * + */ +interface SearchLoopInterface +{ + const MODE_ANY_WORD = 1; + const MODE_SENTENCE = 2; + const MODE_STRICT_SENTENCE = 3; + + /** + * @return array of available field to search in + */ + public function getSearchIn(); + + public function doSearch(&$search, $searchTerm, $searchIn, $searchCriteria); +} diff --git a/core/lib/Thelia/Core/Template/Loop/Order.php b/core/lib/Thelia/Core/Template/Loop/Order.php index 5b1e97cc1..140a64fc3 100755 --- a/core/lib/Thelia/Core/Template/Loop/Order.php +++ b/core/lib/Thelia/Core/Template/Loop/Order.php @@ -28,8 +28,12 @@ use Thelia\Core\Template\Element\BaseLoop; use Thelia\Core\Template\Element\LoopResult; use Thelia\Core\Template\Element\LoopResultRow; +use Thelia\Core\Template\Element\SearchLoopInterface; use Thelia\Core\Template\Loop\Argument\ArgumentCollection; use Thelia\Core\Template\Loop\Argument\Argument; +use Thelia\Model\CustomerQuery; +use Thelia\Model\OrderAddress; +use Thelia\Model\OrderAddressQuery; use Thelia\Model\OrderQuery; use Thelia\Type\TypeCollection; use Thelia\Type; @@ -38,8 +42,9 @@ use Thelia\Type; * @package Thelia\Core\Template\Loop * * @author Franck Allimant + * @author Etienne Roudeix */ -class Order extends BaseLoop +class Order extends BaseLoop implements SearchLoopInterface { public $countable = true; public $timestampable = true; @@ -74,6 +79,59 @@ class Order extends BaseLoop ); } + public function getSearchIn() + { + return array( + "ref", + "customer_ref", + "customer_firstname", + "customer_lastname", + "customer_email", + ); + } + + /** + * @param OrderQuery $search + * @param $searchTerm + * @param $searchIn + * @param $searchCriteria + */ + public function doSearch(&$search, $searchTerm, $searchIn, $searchCriteria) + { + + $search->_and(); + foreach($searchIn as $index => $searchInElement) { + if($index > 0) { + $search->_or(); + } + switch($searchInElement) { + case "ref": + $search->filterByRef($searchTerm, $searchCriteria); + break; + case "customer_ref": + $search->filterByCustomer( + CustomerQuery::create()->filterByRef($searchTerm, $searchCriteria)->find() + ); + break; + case "customer_firstname": + $search->filterByOrderAddressRelatedByInvoiceOrderAddressId( + OrderAddressQuery::create()->filterByFirstname($searchTerm, $searchCriteria)->find() + ); + break; + case "customer_lastname": + $search->filterByOrderAddressRelatedByInvoiceOrderAddressId( + OrderAddressQuery::create()->filterByLastname($searchTerm, $searchCriteria)->find() + ); + break; + case "customer_email": + $search->filterByCustomer( + CustomerQuery::create()->filterByEmail($searchTerm, $searchCriteria)->find() + ); + break; + } + } + } + /** * @param $pagination * diff --git a/core/lib/Thelia/Model/OrderQuery.php b/core/lib/Thelia/Model/OrderQuery.php index 8d5e4c085..0fff5dba1 100755 --- a/core/lib/Thelia/Model/OrderQuery.php +++ b/core/lib/Thelia/Model/OrderQuery.php @@ -2,6 +2,7 @@ namespace Thelia\Model; +use Propel\Runtime\ActiveQuery\Criteria; use Propel\Runtime\Exception\PropelException; use Propel\Runtime\Propel; use Thelia\Model\Base\OrderQuery as BaseOrderQuery; @@ -52,4 +53,5 @@ class OrderQuery extends BaseOrderQuery return $obj; } + } // OrderQuery diff --git a/templates/admin/default/search.html b/templates/admin/default/search.html new file mode 100644 index 000000000..026e52400 --- /dev/null +++ b/templates/admin/default/search.html @@ -0,0 +1,103 @@ +{extends file="admin-layout.tpl"} + +{block name="page-title"}{intl l='Modules'}{/block} + +{block name="check-resource"}admin.configuration.search{/block} +{block name="check-access"}view{/block} + +{block name="main-content"} +
+ +
+ + + + {module_include location='modules_top'} + +
+
+ + {* order search *} +
+
+ + + + + + + + + + + + + + + + + {loop type="order" name="order-search" backend_context=1 customer="*" search_term=$smarty.get.search_term search_in="ref,customer_ref,customer_firstname,customer_lastname,customer_email"} + {loop type="order_address" name="order-invoice-address" id=$INVOICE_ADDRESS} + {assign "orderInvoiceFirstName" $FIRSTNAME} + {assign "orderInvoiceLastName" $LASTNAME} + {assign "orderInvoiceCompany" $COMPANY} + {/loop} + + {loop type="order-status" name="order-status" id=$STATUS} + {assign "orderStatus" $TITLE} + {assign "orderStatusLabel" "order_$CODE"} + {/loop} + + + + + + + + + + + {module_include location='orders_table_row'} + + + + {/loop} + +
+ {intl l='Orders'} +
{intl l="Order n°"}{intl l="Date & Hour"}{intl l="Company"}{intl l="Name"}{intl l="Amount"}{intl l="Status"}{intl l="Actions"}
{$REF}{format_date date=$CREATE_DATE}{$orderInvoiceCompany}{$orderInvoiceFirstName|ucwords} {$orderInvoiceLastName|upper}{$TOTAL_TAXED_AMOUNT}{$orderStatus} +
+ + {loop type="auth" name="can_change" role="ADMIN" resource="admin.order" access="UPDATE"} + + {if $STATUS !== 5} + + {/if} + {/loop} +
+
+
+
+ {* end order search *} + +
+
+ + {module_include location='modules_bottom'} + +
+
+ +{/block} + +{block name="javascript-initialization"} + + + +{/block} \ No newline at end of file