Example #1
0
 public function autoComplete()
 {
     $f = new ARSelectFilter();
     $f->setLimit(20);
     $resp = array();
     $field = $this->request->get('field');
     if (in_array($field, array('sku', 'URL', 'keywords'))) {
         $c = new LikeCond(new ARFieldHandle('Product', $field), $this->request->get($field) . '%');
         $f->setCondition($c);
         $f->setOrder(new ARFieldHandle('Product', $field), 'ASC');
         $query = new ARSelectQueryBuilder();
         $query->setFilter($f);
         $query->includeTable('Product');
         $query->addField('DISTINCT(Product.' . $field . ')');
         $results = ActiveRecordModel::getDataBySQL($query->createString());
         foreach ($results as $value) {
             $resp[] = $value[$field];
         }
     } else {
         if ('name' == $field) {
             $c = new LikeCond(new ARFieldHandle('Product', $field), '%:"' . $this->request->get($field) . '%');
             $f->setCondition($c);
             $locale = $this->locale->getLocaleCode();
             $langCond = new LikeCond(Product::getLangSearchHandle(new ARFieldHandle('Product', 'name'), $locale), $this->request->get($field) . '%');
             $c->addAND($langCond);
             $f->setOrder(Product::getLangSearchHandle(new ARFieldHandle('Product', 'name'), $locale), 'ASC');
             $results = ActiveRecordModel::getRecordSet('Product', $f);
             foreach ($results as $value) {
                 $resp[$value->getValueByLang('name', $locale, Product::NO_DEFAULT_VALUE)] = true;
             }
             $resp = array_keys($resp);
         } else {
             if ('specField_' == substr($field, 0, 10)) {
                 list($foo, $id) = explode('_', $field);
                 $handle = new ARFieldHandle('SpecificationStringValue', 'value');
                 $locale = $this->locale->getLocaleCode();
                 $searchHandle = MultiLingualObject::getLangSearchHandle($handle, $locale);
                 $f->setCondition(new EqualsCond(new ARFieldHandle('SpecificationStringValue', 'specFieldID'), $id));
                 $f->mergeCondition(new LikeCond($handle, '%:"' . $this->request->get($field) . '%'));
                 $f->mergeCondition(new LikeCond($searchHandle, $this->request->get($field) . '%'));
                 $f->setOrder($searchHandle, 'ASC');
                 $results = ActiveRecordModel::getRecordSet('SpecificationStringValue', $f);
                 foreach ($results as $value) {
                     $resp[$value->getValueByLang('value', $locale, Product::NO_DEFAULT_VALUE)] = true;
                 }
                 $resp = array_keys($resp);
             }
         }
     }
     return new AutoCompleteResponse($resp);
 }