Algolia_Algoliasearch_Helper_Entity_Producthelper::setSettings PHP Метод

setSettings() публичный Метод

public setSettings ( $storeId, $saveToTmpIndicesToo = false )
    public function setSettings($storeId, $saveToTmpIndicesToo = false)
    {
        $attributesToIndex = array();
        $unretrievableAttributes = array();
        $attributesForFaceting = array();
        foreach ($this->config->getProductAdditionalAttributes($storeId) as $attribute) {
            if ($attribute['searchable'] == '1') {
                if ($attribute['order'] == 'ordered') {
                    $attributesToIndex[] = $attribute['attribute'];
                } else {
                    $attributesToIndex[] = 'unordered(' . $attribute['attribute'] . ')';
                }
            }
            if ($attribute['retrievable'] != '1') {
                $unretrievableAttributes[] = $attribute['attribute'];
            }
            if ($attribute['attribute'] == 'categories' && $attribute['searchable'] == '1') {
                $attributesToIndex[] = $attribute['order'] == 'ordered' ? 'categories_without_path' : 'unordered(categories_without_path)';
            }
        }
        $customRankings = $this->config->getProductCustomRanking($storeId);
        $customRankingsArr = array();
        $facets = $this->config->getFacets();
        /** @var Mage_Directory_Model_Currency $directoryCurrency */
        $directoryCurrency = Mage::getModel('directory/currency');
        $currencies = $directoryCurrency->getConfigAllowCurrencies();
        foreach ($facets as $facet) {
            if ($facet['attribute'] === 'price') {
                foreach ($currencies as $currency_code) {
                    $facet['attribute'] = 'price.' . $currency_code . '.default';
                    if ($this->config->isCustomerGroupsEnabled($storeId)) {
                        /** @var Mage_Customer_Model_Group $group */
                        foreach ($groups = Mage::getModel('customer/group')->getCollection() as $group) {
                            $group_id = (int) $group->getData('customer_group_id');
                            $attributesForFaceting[] = 'price.' . $currency_code . '.group_' . $group_id;
                        }
                    }
                    $attributesForFaceting[] = $facet['attribute'];
                }
            } else {
                $attributesForFaceting[] = $facet['attribute'];
            }
        }
        foreach ($customRankings as $ranking) {
            $customRankingsArr[] = $ranking['order'] . '(' . $ranking['attribute'] . ')';
        }
        $indexSettings = array('attributesToIndex' => array_values(array_unique($attributesToIndex)), 'customRanking' => $customRankingsArr, 'unretrievableAttributes' => $unretrievableAttributes, 'attributesForFaceting' => $attributesForFaceting, 'maxValuesPerFacet' => (int) $this->config->getMaxValuesPerFacet($storeId), 'removeWordsIfNoResults' => $this->config->getRemoveWordsIfNoResult($storeId));
        // Additional index settings from event observer
        $transport = new Varien_Object($indexSettings);
        Mage::dispatchEvent('algolia_index_settings_prepare', array('store_id' => $storeId, 'index_settings' => $transport));
        $indexSettings = $transport->getData();
        $indexName = $this->getIndexName($storeId);
        $mergeSettings = $this->algolia_helper->mergeSettings($indexName, $indexSettings);
        $this->algolia_helper->setSettings($indexName, $mergeSettings);
        if ($saveToTmpIndicesToo === true) {
            $this->algolia_helper->setSettings($indexName . '_tmp', $mergeSettings);
        }
        /*
         * Handle Slaves
         */
        $sorting_indices = $this->config->getSortingIndices();
        if (count($sorting_indices) > 0) {
            $slaves = array();
            foreach ($sorting_indices as $values) {
                if ($this->config->isCustomerGroupsEnabled($storeId) && $values['attribute'] === 'price') {
                    foreach ($groups = Mage::getModel('customer/group')->getCollection() as $group) {
                        $group_id = (int) $group->getData('customer_group_id');
                        $suffix_index_name = 'group_' . $group_id;
                        $slaves[] = $this->getIndexName($storeId) . '_' . $values['attribute'] . '_' . $suffix_index_name . '_' . $values['sort'];
                    }
                } else {
                    if ($values['attribute'] === 'price') {
                        $slaves[] = $this->getIndexName($storeId) . '_' . $values['attribute'] . '_default_' . $values['sort'];
                    } else {
                        $slaves[] = $this->getIndexName($storeId) . '_' . $values['attribute'] . '_' . $values['sort'];
                    }
                }
            }
            $this->algolia_helper->setSettings($this->getIndexName($storeId), array('slaves' => $slaves));
            foreach ($sorting_indices as $values) {
                if ($this->config->isCustomerGroupsEnabled($storeId) && $values['attribute'] === 'price') {
                    foreach ($groups = Mage::getModel('customer/group')->getCollection() as $group) {
                        $group_id = (int) $group->getData('customer_group_id');
                        $suffix_index_name = 'group_' . $group_id;
                        $sort_attribute = $values['attribute'] === 'price' ? $values['attribute'] . '.' . $currencies[0] . '.' . $suffix_index_name : $values['attribute'];
                        $mergeSettings['ranking'] = array($values['sort'] . '(' . $sort_attribute . ')', 'typo', 'geo', 'words', 'proximity', 'attribute', 'exact', 'custom');
                        $this->algolia_helper->setSettings($this->getIndexName($storeId) . '_' . $values['attribute'] . '_' . $suffix_index_name . '_' . $values['sort'], $mergeSettings);
                    }
                } else {
                    $sort_attribute = $values['attribute'] === 'price' ? $values['attribute'] . '.' . $currencies[0] . '.' . 'default' : $values['attribute'];
                    $mergeSettings['ranking'] = array($values['sort'] . '(' . $sort_attribute . ')', 'typo', 'geo', 'words', 'proximity', 'attribute', 'exact', 'custom');
                    if ($values['attribute'] === 'price') {
                        $this->algolia_helper->setSettings($this->getIndexName($storeId) . '_' . $values['attribute'] . '_default_' . $values['sort'], $mergeSettings);
                    } else {
                        $this->algolia_helper->setSettings($this->getIndexName($storeId) . '_' . $values['attribute'] . '_' . $values['sort'], $mergeSettings);
                    }
                }
            }
        }
        if ($this->config->isEnabledSynonyms($storeId) === true) {
            if ($synonymsFile = $this->config->getSynonymsFile($storeId)) {
                $synonymsToSet = json_decode(file_get_contents($synonymsFile));
            } else {
                $synonymsToSet = array();
                $synonyms = $this->config->getSynonyms($storeId);
                foreach ($synonyms as $objectID => $synonym) {
                    if (!trim($synonym['synonyms'])) {
                        continue;
                    }
                    $synonymsToSet[] = array('objectID' => $objectID, 'type' => 'synonym', 'synonyms' => $this->explodeSynomyms($synonym['synonyms']));
                }
                $onewaySynonyms = $this->config->getOnewaySynonyms($storeId);
                foreach ($onewaySynonyms as $objectID => $onewaySynonym) {
                    if (!trim($onewaySynonym['input']) || !trim($onewaySynonym['synonyms'])) {
                        continue;
                    }
                    $synonymsToSet[] = array('objectID' => $objectID, 'type' => 'oneWaySynonym', 'input' => $onewaySynonym['input'], 'synonyms' => $this->explodeSynomyms($onewaySynonym['synonyms']));
                }
            }
            $this->algolia_helper->setSynonyms($this->getIndexName($storeId, $saveToTmpIndicesToo), $synonymsToSet);
        }
    }

Usage Example

Пример #1
0
 public function saveConfigurationToAlgolia($storeId, $saveToTmpIndicesToo = false)
 {
     $this->algolia_helper->resetCredentialsFromConfig();
     if (!($this->config->getApplicationID() && $this->config->getAPIKey())) {
         return;
     }
     if ($this->config->isEnabledBackend($storeId) === false) {
         $this->logger->log('INDEXING IS DISABLED FOR ' . $this->logger->getStoreName($storeId));
         return;
     }
     $this->algolia_helper->setSettings($this->category_helper->getIndexName($storeId), $this->category_helper->getIndexSettings($storeId));
     $this->algolia_helper->setSettings($this->page_helper->getIndexName($storeId), $this->page_helper->getIndexSettings($storeId));
     $this->algolia_helper->setSettings($this->suggestion_helper->getIndexName($storeId), $this->suggestion_helper->getIndexSettings($storeId));
     foreach ($this->config->getAutocompleteSections() as $section) {
         if ($section['name'] === 'products' || $section['name'] === 'categories' || $section['name'] === 'pages' || $section['name'] === 'suggestions') {
             continue;
         }
         $this->algolia_helper->setSettings($this->additionalsections_helper->getIndexName($storeId) . '_' . $section['name'], $this->additionalsections_helper->getIndexSettings($storeId));
     }
     $this->product_helper->setSettings($storeId, $saveToTmpIndicesToo);
 }