Example #1
0
 protected function getInstance($record, CsvImportProfile $profile)
 {
     $fields = $profile->getSortedFields();
     // get delivery zone
     if (isset($fields['DeliveryZone']['ID'])) {
         try {
             $zone = DeliveryZone::getInstanceByID($record[$fields['DeliveryZone']['ID']], true);
         } catch (ARNotFoundException $e) {
             $zone = DeliveryZone::getDefaultZoneInstance();
         }
     } else {
         $zone = DeliveryZone::getDefaultZoneInstance();
     }
     // get shipping service
     $f = select(new EqualsCond(MultiLingualObject::getLangSearchHandle(new ARFieldHandle('ShippingService', 'name'), $this->application->getDefaultLanguageCode()), $record[$fields['ShippingService']['name']]));
     if ($zone->isDefault()) {
         $f->mergeCondition(new IsNullCond(f('ShippingService.deliveryZoneID')));
     } else {
         $f->mergeCondition(eq(f('ShippingService.deliveryZoneID'), $zone->getID()));
     }
     $services = ActiveRecordModel::getRecordSet('ShippingService', $f);
     if ($services->get(0)) {
         $service = $services->get(0);
         // temporary
         $service->deleteRelatedRecordSet('ShippingRate');
     } else {
         $service = ShippingService::getNewInstance($zone, '', 0);
         $service->rangeType->set(ShippingService::SUBTOTAL_BASED);
     }
     $this->importInstance($record, $profile, $service);
     $this->setLastImportedRecordName($service->getValueByLang('name'));
     // get rate instance
     $rate = ShippingRate::getNewInstance($service, 0, 1000000);
     $rate->subtotalRangeStart->set(0);
     $rate->subtotalRangeEnd->set(1000000);
     return $rate;
 }
Example #2
0
 private function getFieldHandle($field, $handleType)
 {
     // aliases
     if (!strpos($field, '.')) {
         return f($field);
     }
     list($schemaName, $fieldName) = explode('.', $field);
     if ($this->getEavTableAlias() == $schemaName) {
         $fieldID = $fieldName;
         $fieldName = 'value';
         $specField = call_user_func(array($this->getEavFieldClass(), 'getInstanceByID'), $fieldID);
         if ($specField->isSelector()) {
             if ($specField->isMultiValue->get()) {
                 return $specField;
             } else {
                 if (self::FILTER_HANDLE == $handleType) {
                     $fieldName = 'ID';
                 }
             }
         }
     }
     $handle = null;
     if ($schemaName) {
         $schema = $this->getSchemaInstance($field);
         if ($field = $schema->getField($fieldName)) {
             $handle = $schema->getHandle($fieldName);
             if ($this->getEavTableAlias() == $schemaName) {
                 $handle->setTable('specField_' . $fieldID . ($schema->getName() == $this->getEavValueClass() ? '_value' : ''));
             }
             // language fields
             if ($field->getDataType() instanceof ARArray) {
                 if (self::SORT_HANDLE == $handleType) {
                     $handle = MultiLingualObject::getLangOrderHandle($handle);
                 } else {
                     $handleres = array();
                     $defLang = $this->application->getDefaultLanguageCode();
                     $locale = $this->application->getLocaleCode();
                     $handleres[] = MultiLingualObject::getLangSearchHandle($handle, $locale);
                     if ($locale != $defLang) {
                         $handleres[] = MultiLingualObject::getLangSearchHandle($handle, $defLang);
                     }
                     $handle = $handleres;
                 }
             }
         }
     } else {
         $handle = new ARExpressionHandle($fieldName);
     }
     return $handle;
 }
Example #3
0
 public static function findByName($name)
 {
     $f = select(new EqualsCond(MultiLingualObject::getLangSearchHandle(new ARFieldHandle('TaxClass', 'name'), self::getApplication()->getDefaultLanguageCode()), $name));
     return ActiveRecordModel::getRecordSet('TaxClass', $f)->get(0);
 }
Example #4
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);
 }
Example #5
0
 private function importProductVariationValue(Product $product, $index, $name)
 {
     $parent = $product->parent->get();
     $type = $this->getVariationTypeByIndex($parent, $index);
     if (!$type->getID()) {
         $type = $this->importVariationType($parent, $index, '');
     }
     $f = new ARSelectFilter();
     $f->mergeCondition(new EqualsCond(MultiLingualObject::getLangSearchHandle(new ARFieldHandle('ProductVariation', 'name'), $this->application->getDefaultLanguageCode()), $name));
     $values = $type->getRelatedRecordSet('ProductVariation', $f);
     if ($values->size()) {
         $variation = $values->get(0);
     } else {
         $variation = ProductVariation::getNewInstance($type);
         $variation->setValueByLang('name', null, $name);
         $variation->save();
     }
     if (!$product->getID()) {
         $product->save();
     }
     $f = new ARDeleteFilter(new EqualsCond(new ARFieldHandle('ProductVariation', 'typeID'), $type->getID()));
     $product->deleteRelatedRecordSet('ProductVariationValue', $f, array('ProductVariation'));
     ProductVariationValue::getNewInstance($product, $variation)->save();
 }
Example #6
0
 public function index()
 {
     ClassLoader::import('application.model.presentation.CategoryPresentation');
     $this->getAppliedFilters();
     // presentation
     if ($theme = CategoryPresentation::getThemeByCategory($this->getCategory())) {
         if ($theme->getTheme()) {
             $this->application->setTheme($theme->getTheme());
         }
         if ($layout = $theme->listStyle->get()) {
             $this->request->set('layout', strtolower($layout));
             $this->config->set('LIST_LAYOUT', $layout);
             $this->config->set('ALLOW_SWITCH_LAYOUT', false);
         }
     }
     // pagination
     $currentPage = $this->request->get('page', 1);
     $listLayout = $this->getListLayout();
     $perPage = $this->getProductLimitCount($listLayout);
     $offsetStart = ($currentPage - 1) * $perPage + 1;
     $offsetEnd = $currentPage * $perPage;
     $selectFilter = new ARSelectFilter();
     $selectFilter->setLimit($perPage, $offsetStart - 1);
     $this->application->processInstancePlugins('productFilter', $selectFilter);
     // create new search filter
     $query = $this->request->get('q');
     if ($query) {
         $searchFilter = new SearchFilter($query);
         $this->filters[] = $searchFilter;
         // search by category names
         $f = new ARSelectFilter();
         foreach (array($this->locale->getLocaleCode(), $this->application->getDefaultLanguageCode()) as $handle) {
             $langHandle = MultiLingualObject::getLangSearchHandle(new ARFieldHandle('Category', 'name'), $handle);
             $f->mergeCondition(new LikeCond($langHandle, '%' . $query . '%'));
         }
         $foundCategories = ActiveRecordModel::getRecordSet('Category', $f, Category::LOAD_REFERENCES);
         foreach ($foundCategories as $category) {
             $category->getPathNodeSet();
         }
         $cleanedQuery = $searchFilter->getCleanedQuery($query);
         $this->logSearchQuery($cleanedQuery);
     }
     // root category?
     if ($this->getCategory()->isRoot() && !$this->filters && !$this instanceof IndexController && !$this->request->get('includeSub') && $currentPage > 1) {
         return new ActionRedirectResponse('index', 'index');
     }
     // sorting
     $sort = array();
     $opts = $this->config->get('ALLOWED_SORT_ORDER');
     if ($opts) {
         foreach ($opts as $opt => $status) {
             $sort[strtolower($opt)] = $this->translate($opt);
         }
     }
     foreach ($this->getCategory()->getSpecificationFieldArray() as $field) {
         if ($field['isSortable']) {
             $sortName = $field['dataType'] == SpecField::DATATYPE_NUMBERS ? '_sort_num' : '_sort_text';
             $sort[$field['ID'] . '-' . $field['handle'] . '_asc'] = $this->maketext($sortName . '_asc', $field['name_lang']);
             $sort[$field['ID'] . '-' . $field['handle'] . '_desc'] = $this->maketext($sortName . '_desc', $field['name_lang']);
         }
     }
     $order = $this->request->get('sort');
     $defOrder = strtolower($this->config->get('SORT_ORDER'));
     if (!$order) {
         $order = $defOrder;
     }
     $this->applySortOrder($selectFilter, $order);
     // setup ProductFilter
     $productFilter = new ProductFilter($this->getCategory(), $selectFilter);
     if ($this->config->get('INCLUDE_SUBCATEGORY_PRODUCTS')) {
         $productFilter->includeSubcategories();
     }
     $this->productFilter = $productFilter;
     foreach ($this->filters as $filter) {
         $productFilter->applyFilter($filter);
         if ($filter instanceof SearchFilter) {
             $productFilter->includeSubcategories();
             $searchQuery = $filter->getKeywords();
         }
     }
     if ($this->getCategory()->isRoot() && $this->filters || $this->filters || $this->request->get('includeSub')) {
         $productFilter->includeSubcategories();
     }
     $products = $this->getProductsArray($productFilter);
     $this->hasProducts = count($products) > 0;
     // pagination
     $count = new ProductCount($this->productFilter, $this->application);
     $totalCount = $count->getCategoryProductCount($productFilter);
     $offsetEnd = min($totalCount, $offsetEnd);
     $this->totalCount = $totalCount;
     // narrow by subcategories
     $subCategories = $this->getCategory()->getSubCategoryArray(Category::LOAD_REFERENCES);
     $categoryNarrow = array();
     if ((!empty($searchQuery) || $this->getCategory()->isRoot() || $this->filters) && $products) {
         $categoryNarrow = $this->getSubCategoriesBySearchQuery($productFilter->getSelectFilter(), $subCategories);
     }
     $categoryArray = $this->getCategory()->toArray();
     if (!$this->getCategory()->isRoot()) {
         $this->redirect301($this->request->get('cathandle'), createHandleString($categoryArray['name_lang']));
     }
     // if all the results come from one category, redirect to this category
     if (count($categoryNarrow) == 1 && count($this->filters) == 1 && empty($foundCategories)) {
         $canNarrow = true;
         foreach ($products as $product) {
             if ($product['Category']['ID'] == $this->getCategoryId()) {
                 $canNarrow = false;
             }
         }
         if ($canNarrow) {
             while (count($categoryNarrow) == 1) {
                 if ($categoryNarrow[0]['searchCount'] != $totalCount) {
                     break;
                 }
                 $this->category = Category::getInstanceByID($categoryNarrow[0]['ID'], Category::LOAD_DATA);
                 $subCategories = $this->getCategory()->getSubCategoryArray(Category::LOAD_REFERENCES);
                 if ($subCategories) {
                     $subCategories[] = $categoryArray;
                 }
                 $categoryNarrow = $this->getSubCategoriesBySearchQuery($selectFilter, $subCategories);
             }
             include_once ClassLoader::getRealPath('application.helper.smarty') . '/function.categoryUrl.php';
             if (!$this->getCategory()->isRoot()) {
                 return new RedirectResponse(createCategoryUrl(array('data' => $this->getCategory()->toArray(), 'filters' => $this->filters), $this->application));
             }
         }
     }
     // get subcategory-subcategories
     if ($subCategories && $this->config->get('CAT_MENU_SUBS')) {
         $this->getSubSubCategories($subCategories);
     }
     // get subcategory featured products
     $subCatFeatured = array();
     if ($subCategories && !$products || $this instanceof IndexController) {
         $subCatFeatured = $this->getSubCatFeaturedProducts();
     }
     // if there were no products found, include subcategories in filter counts... except home page
     if (!$products || $this->getCategory()->isRoot()) {
         $selectFilter->removeCondition(new EqualsCond(new ARFieldHandle('Product', 'categoryID'), $this->getCategory()->getID()));
         $this->productFilter->includeSubcategories();
     }
     // search redirects
     // no products found, but found one category name - redirect to this category
     if (isset($foundCategories) && 1 == $foundCategories->size() && !$products) {
         include_once ClassLoader::getRealPath('application.helper.smarty') . '/function.categoryUrl.php';
         return new RedirectResponse(createCategoryUrl(array('data' => $foundCategories->get(0)->toArray()), $this->application));
     }
     $filterArray = array();
     foreach ($this->filters as $filter) {
         $filterArray[] = $filter->toArray();
     }
     if ($this->config->get('DISPLAY_CATEGORY_FEATURED')) {
         $this->getFeaturedMainCategoryProducts($subCategories);
         $this->getFeaturedMainCategoryProducts($categoryNarrow);
     }
     $response = new ActionResponse();
     $response->set('id', $this->getCategoryId());
     $response->set('products', $products);
     $response->set('count', $totalCount);
     $response->set('offsetStart', $offsetStart);
     $response->set('offsetEnd', $offsetEnd);
     $response->set('perPage', $perPage);
     $response->set('currentPage', $currentPage);
     $response->set('category', $categoryArray);
     $response->set('subCategories', $subCategories);
     $response->set('currency', $this->getRequestCurrency());
     $response->set('sortOptions', $sort);
     $response->set('sortForm', $this->buildSortForm($order));
     $response->set('sortField', $order);
     $response->set('categoryNarrow', $categoryNarrow);
     $response->set('subCatFeatured', $subCatFeatured);
     //$response->set('allFilters', $filters);
     //$response->set('showAll', $showAll);
     $response->set('appliedFilters', $filterArray);
     $response->set('layout', $listLayout);
     $response->set('listAttributes', $this->getListAttributes());
     $filterChainHandle = $this->setUpBreadCrumbAndReturnFilterChainHandle($currentPage);
     $response->set('url', $this->getCategoryPageUrl(array('page' => '_000_', 'filters' => $filterChainHandle)));
     $response->set('layoutUrl', $this->getCategoryPageUrl(array('filters' => $filterChainHandle, 'query' => array('layout' => ''))));
     $response->set('filterChainHandle', $filterChainHandle);
     if (isset($searchQuery)) {
         $response->set('searchQuery', $searchQuery);
     }
     if (isset($foundCategories)) {
         $response->set('foundCategories', $foundCategories->toArray());
     }
     // look for manufacturer filter
     foreach ($this->filters as $filter) {
         if ($filter instanceof ManufacturerFilter) {
             $response->set('manufacturerFilter', $filter->toArray());
         }
     }
     if (1 == $currentPage && $query) {
         $searchCon = new SearchController($this->application);
         $response->set('modelSearch', $searchCon->searchAll($cleanedQuery));
     }
     return $response;
 }