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);
}
}