Added order parameter, improved request
This commit is contained in:
@@ -25,6 +25,9 @@ use Thelia\Core\Template\Loop\Argument\ArgumentCollection;
|
||||
use Thelia\Coupon\Type\CouponInterface;
|
||||
use Thelia\Model\Coupon as MCoupon;
|
||||
use Thelia\Model\CouponQuery;
|
||||
use Thelia\Model\Map\CouponTableMap;
|
||||
use Thelia\Type\EnumListType;
|
||||
use Thelia\Type\TypeCollection;
|
||||
|
||||
/**
|
||||
* Coupon Loop
|
||||
@@ -44,7 +47,23 @@ class Coupon extends BaseI18nLoop implements PropelSearchLoopInterface
|
||||
{
|
||||
return new ArgumentCollection(
|
||||
Argument::createIntListTypeArgument('id'),
|
||||
Argument::createBooleanOrBothTypeArgument('is_enabled')
|
||||
Argument::createBooleanOrBothTypeArgument('is_enabled'),
|
||||
new Argument(
|
||||
'order',
|
||||
new TypeCollection(
|
||||
new EnumListType(array(
|
||||
'id', 'id-reverse',
|
||||
'code', 'code-reverse',
|
||||
'title', 'title-reverse',
|
||||
'enabled', 'enabled-reverse',
|
||||
'expiration-date', 'expiration-date-reverse',
|
||||
'days-left', 'days-left-reverse',
|
||||
'usages-left', 'usages-left-reverse'
|
||||
)
|
||||
)
|
||||
),
|
||||
'code'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -66,6 +85,63 @@ class Coupon extends BaseI18nLoop implements PropelSearchLoopInterface
|
||||
$search->filterByIsEnabled($isEnabled ? true : false);
|
||||
}
|
||||
|
||||
$search->addAsColumn('days_left', 'DATEDIFF('.CouponTableMap::EXPIRATION_DATE.', CURDATE()) - 1');
|
||||
|
||||
$orders = $this->getOrder();
|
||||
|
||||
foreach ($orders as $order) {
|
||||
switch ($order) {
|
||||
case 'id':
|
||||
$search->orderById(Criteria::ASC);
|
||||
break;
|
||||
case 'id-reverse':
|
||||
$search->orderById(Criteria::DESC);
|
||||
break;
|
||||
|
||||
case 'code':
|
||||
$search->orderByCode(Criteria::ASC);
|
||||
break;
|
||||
case 'code-reverse':
|
||||
$search->orderByCode(Criteria::DESC);
|
||||
break;
|
||||
|
||||
case 'title':
|
||||
$search->addAscendingOrderByColumn('i18n_TITLE');
|
||||
break;
|
||||
case 'title-reverse':
|
||||
$search->addDescendingOrderByColumn('i18n_TITLE');
|
||||
break;
|
||||
|
||||
case 'enabled':
|
||||
$search->orderByIsEnabled(Criteria::ASC);
|
||||
break;
|
||||
case 'enabled-reverse':
|
||||
$search->orderByIsEnabled(Criteria::DESC);
|
||||
break;
|
||||
|
||||
case 'expiration-date':
|
||||
$search->orderByExpirationDate(Criteria::ASC);
|
||||
break;
|
||||
case 'expiration-date-reverse':
|
||||
$search->orderByExpirationDate(Criteria::DESC);
|
||||
break;
|
||||
|
||||
case 'usages-left':
|
||||
$search->orderByMaxUsage(Criteria::ASC);
|
||||
break;
|
||||
case 'usages-left-reverse':
|
||||
$search->orderByMaxUsage(Criteria::DESC);
|
||||
break;
|
||||
|
||||
case 'days-left':
|
||||
$search->addAscendingOrderByColumn('days_left');
|
||||
break;
|
||||
case 'days-left-reverse':
|
||||
$search->addDescendingOrderByColumn('days_left');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $search;
|
||||
}
|
||||
|
||||
@@ -81,7 +157,9 @@ class Coupon extends BaseI18nLoop implements PropelSearchLoopInterface
|
||||
|
||||
/** @var MCoupon $coupon */
|
||||
foreach ($loopResult->getResultDataCollection() as $coupon) {
|
||||
|
||||
$loopResultRow = new LoopResultRow($coupon);
|
||||
|
||||
$conditions = $conditionFactory->unserializeConditionCollection(
|
||||
$coupon->getSerializedConditions()
|
||||
);
|
||||
@@ -103,10 +181,6 @@ class Coupon extends BaseI18nLoop implements PropelSearchLoopInterface
|
||||
$coupon->getExpirationDate()
|
||||
);
|
||||
|
||||
$now = time();
|
||||
$datediff = $coupon->getExpirationDate()->getTimestamp() - $now;
|
||||
$daysLeftBeforeExpiration = floor($datediff/(60*60*24));
|
||||
|
||||
$cleanedConditions = array();
|
||||
/** @var ConditionInterface $condition */
|
||||
foreach ($conditions as $condition) {
|
||||
@@ -116,14 +190,16 @@ class Coupon extends BaseI18nLoop implements PropelSearchLoopInterface
|
||||
);
|
||||
$cleanedConditions[] = $temp;
|
||||
}
|
||||
$loopResultRow->set("ID", $coupon->getId())
|
||||
|
||||
$loopResultRow
|
||||
->set("ID", $coupon->getId())
|
||||
->set("IS_TRANSLATED", $coupon->getVirtualColumn('IS_TRANSLATED'))
|
||||
->set("LOCALE", $this->locale)
|
||||
->set("CODE", $coupon->getCode())
|
||||
->set("TITLE", $coupon->getVirtualColumn('i18n_TITLE'))
|
||||
->set("SHORT_DESCRIPTION", $coupon->getVirtualColumn('i18n_SHORT_DESCRIPTION'))
|
||||
->set("DESCRIPTION", $coupon->getVirtualColumn('i18n_DESCRIPTION'))
|
||||
->set("EXPIRATION_DATE", $coupon->getExpirationDate($lang->getDateFormat()))
|
||||
->set("EXPIRATION_DATE", $coupon->getExpirationDate())
|
||||
->set("USAGE_LEFT", $coupon->getMaxUsage())
|
||||
->set("IS_CUMULATIVE", $coupon->getIsCumulative())
|
||||
->set("IS_REMOVING_POSTAGE", $coupon->getIsRemovingPostage())
|
||||
@@ -132,7 +208,7 @@ class Coupon extends BaseI18nLoop implements PropelSearchLoopInterface
|
||||
->set("AMOUNT", $coupon->getAmount())
|
||||
->set("APPLICATION_CONDITIONS", $cleanedConditions)
|
||||
->set("TOOLTIP", $couponManager->getToolTip())
|
||||
->set("DAY_LEFT_BEFORE_EXPIRATION", $daysLeftBeforeExpiration)
|
||||
->set("DAY_LEFT_BEFORE_EXPIRATION", max(0, $coupon->getVirtualColumn('days_left')))
|
||||
->set("SERVICE_ID", $couponManager->getServiceId());
|
||||
$loopResult->addRow($loopResultRow);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user