Exemple #1
0
 public function getCondition()
 {
     $from = new EqualsOrMoreCond(new ARFieldHandle('ProductPrice', 'price'), $this->priceFrom);
     $to = new EqualsOrLessCond(new ARFieldHandle('ProductPrice', 'price'), $this->priceTo);
     $from->addAND($to);
     return $from;
 }
Exemple #2
0
 public function __construct(LiveCart $application, ARSelectFilter $filter, $modelClass = false, $columnTypes = array())
 {
     $this->application = $application;
     $this->modelClass = $modelClass;
     $this->filter = $filter;
     $this->columnTypes = $columnTypes;
     $request = $this->application->getRequest();
     // set recordset boundaries (limits)
     $filter->setLimit($request->get('page_size', 10), $request->get('offset', 0));
     // set order
     if ($request->isValueSet('sort_col')) {
         $handle = $this->getFieldHandle($request->get('sort_col'), self::SORT_HANDLE);
         if ($handle) {
             $filter->setOrder($handle, $request->get('sort_dir'));
         }
     }
     // apply filters
     $filters = $request->get('filters');
     if (!is_array($filters)) {
         $filters = (array) json_decode($request->get('filters'));
     }
     $conds = array();
     if ($filter->getCondition()) {
         $conds[] = $filter->getCondition();
     }
     foreach ($filters as $field => $value) {
         if (!strlen($value)) {
             continue;
         }
         $value = urldecode($value);
         $handle = $this->getFieldHandle($field, self::FILTER_HANDLE);
         if ($handle instanceof EavFieldCommon) {
             continue;
         } else {
             if (!is_array($handle) && !is_null($handle) && !$handle instanceof ARExpressionHandle) {
                 $fieldInst = $this->getFieldInstance($field);
                 if ($fieldInst && ($fieldInst->getDataType() instanceof ARNumeric || $handle->getField()->getDataType() instanceof ARNumeric)) {
                     $value = preg_replace('/[ ]{2,}/', ' ', $value);
                     $constraints = $fieldInst->getDataType() instanceof ARNumeric ? explode(' ', $value) : array($value);
                     foreach ($constraints as $c) {
                         list($operator, $value) = $this->parseOperatorAndValue($c);
                         if (!is_numeric($value) && $fieldInst->getDataType() instanceof ARNumeric) {
                             continue;
                         }
                         $conds[] = new OperatorCond($handle, $value, $operator);
                     }
                 } else {
                     if ($fieldInst && $fieldInst->getDataType() instanceof ARPeriod) {
                         if (substr($value, 0, 10) == 'daterange ') {
                             $value = str_replace('daterange ', '', $value);
                             list($from, $to) = explode('|', $value);
                             $from = trim($from);
                             $to = trim($to);
                             // convert
                             // 2010-9-1 to 2010-09-01 ( +first or last minute of day)
                             // unset dates to 'inf' (meaning ingnore condition)
                             if ($from == '') {
                                 $from = 'inf';
                             } else {
                                 list($y, $m, $d) = explode('-', $from);
                                 $from = $y . '-' . str_pad($m, 2, '0', STR_PAD_LEFT) . '-' . str_pad($d, 2, '0', STR_PAD_LEFT) . ' 00:00:00';
                             }
                             if ($to == '') {
                                 $to = 'inf';
                             } else {
                                 list($y, $m, $d) = explode('-', $to);
                                 $to = $y . '-' . str_pad($m, 2, '0', STR_PAD_LEFT) . '-' . str_pad($d, 2, '0', STR_PAD_LEFT) . ' 23:59:59';
                             }
                         } else {
                             list($from, $to) = explode(' | ', $value);
                         }
                         $cond = null;
                         // from condition
                         if ('inf' != $from) {
                             $cond = new EqualsOrMoreCond($handle, getDateFromString($from));
                         }
                         // to condition
                         if ('now' != $to && 'inf' != $to) {
                             $condTo = new EqualsOrLessCond($handle, getDateFromString($to));
                             if ($cond) {
                                 $cond->addAnd($condTo);
                             } else {
                                 $cond = $condTo;
                             }
                         }
                         if ($cond) {
                             $conds[] = $cond;
                         }
                     } else {
                         $conds[] = new LikeCond($handle, '%' . $value . '%');
                     }
                 }
             } else {
                 if (is_array($handle)) {
                     $cond = null;
                     foreach ($handle as $h) {
                         $c = new LikeCond($h, '%' . $value . '%');
                         if (!$cond) {
                             $cond = $c;
                         } else {
                             $cond->addOR($c);
                         }
                     }
                     $conds[] = $cond;
                 } else {
                     if (array_key_exists($field, $this->columnTypes)) {
                         $type = $this->columnTypes[$field]['type'];
                     } else {
                         $type = null;
                     }
                     $value = preg_replace('/[ ]{2,}/', ' ', $value);
                     switch ($type) {
                         case 'numeric':
                             $constraints = explode(' ', $value);
                             foreach ($constraints as $c) {
                                 list($operator, $value) = $this->parseOperatorAndValue($c);
                                 if (!is_numeric($value)) {
                                     continue;
                                 }
                                 $having[] = new OperatorCond($handle, $value, $operator);
                             }
                             break;
                         default:
                             $having[] = eq(new ARExpressionHandle($field), $value);
                     }
                 }
             }
         }
     }
     // apply IDs to filter
     if ($request->get('selectedIDs') || $request->get('isInverse')) {
         $selectedIDs = json_decode($request->get('selectedIDs'));
         if ($selectedIDs) {
             if ((bool) $request->get('isInverse')) {
                 $idcond = new NotINCond(new ARFieldHandle($modelClass, 'ID'), $selectedIDs);
             } else {
                 $idcond = new INCond(new ARFieldHandle($modelClass, 'ID'), $selectedIDs);
             }
             $conds[] = $idcond;
         } else {
             if (!(bool) $request->get('isInverse')) {
                 $idcond = new EqualsCond(new ARExpressionHandle(1), 2);
                 $conds[] = $idcond;
             }
         }
     }
     if ($conds) {
         $filter->setCondition(new AndChainCondition($conds));
     }
     if (!empty($having)) {
         $filter->setHavingCondition(new AndChainCondition($having));
     }
 }
Exemple #3
0
 public function getProductCondition($includeSubcategories = false)
 {
     if ($includeSubcategories) {
         if (!$this->isRoot()) {
             $cond = new EqualsOrMoreCond(new ARFieldHandle('Category', 'lft'), $this->lft->get());
             $cond->addAND(new EqualsOrLessCond(new ARFieldHandle('Category', 'rgt'), $this->rgt->get()));
             if ($this->hasProductsAsSecondaryCategory()) {
                 $cond->addOr(new INCond(new ARFieldHandle('Product', 'ID'), 'SELECT ProductCategory.productID FROM ProductCategory LEFT JOIN Category ON ProductCategory.categoryID=Category.ID WHERE Category.lft>=' . $this->lft->get() . ' AND Category.rgt<=' . $this->rgt->get()));
             }
         } else {
             $cond = new IsNotNullCond(new ARFieldHandle('Product', 'categoryID'));
         }
     } else {
         $cond = new EqualsCond(new ARFieldHandle('Product', 'categoryID'), $this->getID());
         if ($this->hasProductsAsSecondaryCategory()) {
             $cond->addOr(new INCond(new ARFieldHandle('Product', 'ID'), 'SELECT ProductCategory.productID FROM ProductCategory WHERE ProductCategory.categoryID=' . $this->getID()));
         }
     }
     return $cond;
 }
 private function getTabCounts($categoryId)
 {
     ClassLoader::import('application.model.category.*');
     ClassLoader::import('application.model.filter.*');
     ClassLoader::import('application.model.product.*');
     $category = Category::getInstanceByID($categoryId, Category::LOAD_DATA);
     $reviewCond = new EqualsOrMoreCond(new ARFieldHandle('Category', 'lft'), $category->lft->get());
     $reviewCond->addAND(new EqualsOrLessCond(new ARFieldHandle('Category', 'rgt'), $category->rgt->get()));
     return array('tabProducts' => $category->totalProductCount->get(), 'tabFilters' => $this->getFilterCount($category), 'tabFields' => $this->getSpecFieldCount($category), 'tabImages' => $this->getCategoryImageCount($category), 'tabOptions' => $category->getOptions()->getTotalRecordCount(), 'tabRatingCategories' => ProductRatingType::getCategoryRatingTypes($category)->size(), 'tabReviews' => ActiveRecordModel::getRecordCount('ProductReview', new ARSelectFilter($reviewCond), array('Category', 'Product')), 'tabProductLists' => $category->getRelatedRecordCount('ProductList'));
 }
 /**
  *  Narrow search results by categories
  */
 private function getSubcategoriesBySearchQuery(ARSelectFilter $selectFilter, $subCategories)
 {
     if (count($subCategories) > 0) {
         $case = new ARCaseHandle();
         $index = array();
         foreach ($subCategories as $key => $cat) {
             if (Category::ROOT_ID == $cat['ID']) {
                 continue;
             }
             $cond = new EqualsOrMoreCond(new ARFieldHandle('Category', 'lft'), $cat['lft']);
             $cond->addAND(new EqualsOrLessCond(new ARFieldHandle('Category', 'rgt'), $cat['rgt']));
             $case->addCondition($cond, new ARExpressionHandle($cat['ID']));
             $index[$cat['ID']] = $key;
         }
         $query = new ARSelectQueryBuilder();
         $query->includeTable('Product');
         $filter = clone $selectFilter;
         $filter->setLimit(0);
         $filter->resetOrder();
         $filter->setOrder(new ARExpressionHandle('cnt'), 'DESC');
         $filter->setGrouping(new ARExpressionHandle('ID'));
         foreach ($this->filters as $f) {
             $f->defineJoin($filter);
         }
         $query->setFilter($filter);
         $query->addField($case->toString(), null, 'ID');
         $query->addField('COUNT(*)', null, 'cnt');
         $query->joinTable('Category', 'Product', 'ID', 'categoryID');
         $query->joinTable('ProductPrice', 'Product', 'productID AND (ProductPrice.currencyID = "' . $this->application->getDefaultCurrencyCode() . '")', 'ID');
         $count = $query->getPreparedStatement(ActiveRecord::getDBConnection())->executeQuery();
         $categoryNarrow = array();
         foreach ($count as $cat) {
             if (!isset($index[$cat['ID']])) {
                 continue;
             }
             $data = $subCategories[$index[$cat['ID']]];
             $data['searchCount'] = $cat['cnt'];
             $categoryNarrow[] = $data;
         }
         return $categoryNarrow;
     }
 }
Exemple #6
0
 protected function getOrderCount($from, $to)
 {
     $cond = new EqualsOrMoreCond(new ARFieldHandle('CustomerOrder', 'dateCompleted'), getDateFromString($from));
     if ('now' != $to) {
         $cond->addAnd(new EqualsOrLessCond(new ARFieldHandle('CustomerOrder', 'dateCompleted'), getDateFromString($to)));
     }
     $f = new ARSelectFilter($cond);
     $f->mergeCondition(new EqualsCond(new ARFieldHandle('CustomerOrder', 'isFinalized'), true));
     $f->mergeCondition(new EqualsCond(new ARFieldHandle('CustomerOrder', 'isCancelled'), false));
     return ActiveRecordModel::getRecordCount('CustomerOrder', $f);
 }
Exemple #7
0
 protected function getSelectFilter()
 {
     $id = $this->getID();
     if ($this->isCategory()) {
         $owner = Category::getInstanceByID($id, Category::LOAD_DATA);
         $cond = new EqualsOrMoreCond(new ARFieldHandle('Category', 'lft'), $owner->lft->get());
         $cond->addAND(new EqualsOrLessCond(new ARFieldHandle('Category', 'rgt'), $owner->rgt->get()));
     } else {
         $cond = new EqualsCond(new ARFieldHandle('ProductReview', 'productID'), $id);
     }
     return new ARSelectFilter($cond);
 }