From 61dc305512a76efa269cb459981b0dfdc2e29451 Mon Sep 17 00:00:00 2001 From: Floddy Date: Thu, 21 Jun 2018 14:54:30 +0200 Subject: [PATCH 01/36] Missing payload causes js console errors --- js/algoliasearch/internals/frontend/common.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/algoliasearch/internals/frontend/common.js b/js/algoliasearch/internals/frontend/common.js index b64c4458..b3893a4f 100644 --- a/js/algoliasearch/internals/frontend/common.js +++ b/js/algoliasearch/internals/frontend/common.js @@ -279,7 +279,7 @@ document.addEventListener("DOMContentLoaded", function (e) { displayKey: 'query', name: section.name, templates: { - suggestion: function (hit) { + suggestion: function (hit, payload) { if (hit.facet) { hit.category = hit.facet.value; } From d0bf26a814016035cb8691f666b575f3e3d760a0 Mon Sep 17 00:00:00 2001 From: Damien Couchez Date: Thu, 28 Jun 2018 16:05:06 +0200 Subject: [PATCH 02/36] Handle manual indexing mode for cms pages and products (#1041) * Handle manuel indexing mode for cms pages and products * Refactoring --- .../Algolia/Algoliasearch/Model/Observer.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/app/code/community/Algolia/Algoliasearch/Model/Observer.php b/app/code/community/Algolia/Algoliasearch/Model/Observer.php index 5013f903..48d06917 100644 --- a/app/code/community/Algolia/Algoliasearch/Model/Observer.php +++ b/app/code/community/Algolia/Algoliasearch/Model/Observer.php @@ -96,6 +96,10 @@ public function useAlgoliaSearchPopup(Varien_Event_Observer $observer) public function saveProduct(Varien_Event_Observer $observer) { + if ($this->isIndexerInManualMode('algolia_search_indexer')) { + return; + } + $product = $observer->getDataObject(); $product = Mage::getModel('catalog/product')->load($product->getId()); @@ -104,7 +108,9 @@ public function saveProduct(Varien_Event_Observer $observer) public function savePage(Varien_Event_Observer $observer) { - if (!$this->config->getApplicationID() || !$this->config->getAPIKey()) { + if (!$this->config->getApplicationID() + || !$this->config->getAPIKey() + || $this->isIndexerInManualMode('algolia_search_indexer_pages')) { return; } @@ -318,4 +324,15 @@ private function loadAnalyticsHandle(Varien_Event_Observer $observer) $observer->getData('layout')->getUpdate()->addHandle('algolia_search_handle_click_conversion_analytics'); } + + private function isIndexerInManualMode($indexerCode) + { + /** @var $process Mage_Index_Model_Process */ + $process = Mage::getModel('index/process')->load($indexerCode, 'indexer_code'); + if (!is_null($process) && $process->getMode() == Mage_Index_Model_Process::MODE_MANUAL) { + return true; + } + + return false; + } } From 72482394662676578d16ca322adfba05569439d8 Mon Sep 17 00:00:00 2001 From: Damien Couchez Date: Mon, 13 Aug 2018 16:53:31 +0200 Subject: [PATCH 03/36] Format price on slider facets (#1051) --- .../template/algoliasearch/internals/configuration.phtml | 1 + js/algoliasearch/instantsearch.js | 4 +++- skin/frontend/base/default/algoliasearch/algoliasearch.css | 1 + 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/app/design/frontend/base/default/template/algoliasearch/internals/configuration.phtml b/app/design/frontend/base/default/template/algoliasearch/internals/configuration.phtml index 8946509c..add7d9b1 100644 --- a/app/design/frontend/base/default/template/algoliasearch/internals/configuration.phtml +++ b/app/design/frontend/base/default/template/algoliasearch/internals/configuration.phtml @@ -259,6 +259,7 @@ $algoliaJsConfig = array( 'priceKey' => $priceKey, 'currencyCode' => $currencyCode, 'currencySymbol' => $currencySymbol, + 'priceFormat' => Mage::app()->getLocale()->getJsPriceFormat(), 'maxValuesPerFacet' => (int) $config->getMaxValuesPerFacet(), 'autofocus' => true, 'ccAnalytics' => array( diff --git a/js/algoliasearch/instantsearch.js b/js/algoliasearch/instantsearch.js index 8357d97d..ab146457 100644 --- a/js/algoliasearch/instantsearch.js +++ b/js/algoliasearch/instantsearch.js @@ -461,7 +461,9 @@ document.addEventListener("DOMContentLoaded", function (event) { }, tooltips: { format: function (formattedValue) { - return parseInt(formattedValue); + return facet.attribute.match(/price/) === null ? + parseInt(formattedValue) : + formatCurrency(formattedValue, algoliaConfig.priceFormat); } } }]; diff --git a/skin/frontend/base/default/algoliasearch/algoliasearch.css b/skin/frontend/base/default/algoliasearch/algoliasearch.css index b3a80488..0d8259c8 100644 --- a/skin/frontend/base/default/algoliasearch/algoliasearch.css +++ b/skin/frontend/base/default/algoliasearch/algoliasearch.css @@ -318,6 +318,7 @@ a.ais-current-refined-values--link:hover position: absolute; background: #FFFFFF; top: -2em; + left: -50%; min-width: 20px; text-align: center; font-size: .8em; From 81bcce43948ecce3a2f0b589e0ffce5e3074fd27 Mon Sep 17 00:00:00 2001 From: Damien Couchez Date: Mon, 13 Aug 2018 16:58:16 +0200 Subject: [PATCH 04/36] Add non castable textfield attributes advanced feature (#1035) --- .../Config/Form/Field/ProductAttributes.php | 38 +++++++++++++++++++ .../Algolia/Algoliasearch/Helper/Config.php | 17 +++++++++ .../Algoliasearch/Helper/Entity/Helper.php | 13 +++++-- .../Algolia/Algoliasearch/etc/system.xml | 14 +++++++ 4 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 app/code/community/Algolia/Algoliasearch/Block/System/Config/Form/Field/ProductAttributes.php diff --git a/app/code/community/Algolia/Algoliasearch/Block/System/Config/Form/Field/ProductAttributes.php b/app/code/community/Algolia/Algoliasearch/Block/System/Config/Form/Field/ProductAttributes.php new file mode 100644 index 00000000..d76e0427 --- /dev/null +++ b/app/code/community/Algolia/Algoliasearch/Block/System/Config/Form/Field/ProductAttributes.php @@ -0,0 +1,38 @@ +settings = array( + 'columns' => array( + 'attribute' => array( + 'label' => 'Attribute', + 'options' => function () { + $options = array(); + + /** @var Algolia_Algoliasearch_Helper_Entity_Producthelper $product_helper */ + $product_helper = Mage::helper('algoliasearch/entity_producthelper'); + $attributes = $product_helper->getAllAttributes(); + foreach ($attributes as $key => $label) { + $options[$key] = $key ?: $label; + } + + $options['custom_attribute'] = '[use custom attribute]'; + + return $options; + }, + 'rowMethod' => 'getAttribute', + 'width' => 150, + ), + ), + 'buttonLabel' => 'Add Attribute', + 'addAfter' => false, + ); + + parent::__construct(); + } +} diff --git a/app/code/community/Algolia/Algoliasearch/Helper/Config.php b/app/code/community/Algolia/Algoliasearch/Helper/Config.php index e6bbee9b..90db0f1f 100644 --- a/app/code/community/Algolia/Algoliasearch/Helper/Config.php +++ b/app/code/community/Algolia/Algoliasearch/Helper/Config.php @@ -87,6 +87,7 @@ class Algolia_Algoliasearch_Helper_Config extends Mage_Core_Helper_Abstract const PREVENT_BACKEND_RENDERING = 'algoliasearch/advanced/prevent_backend_rendering'; const PREVENT_BACKEND_RENDERING_DISPLAY_MODE = 'algoliasearch/advanced/prevent_backend_rendering_display_mode'; const BACKEND_RENDERING_ALLOWED_USER_AGENTS = 'algoliasearch/advanced/backend_rendering_allowed_user_agents'; + const NON_CASTABLE_ATTRIBUTES = 'algoliasearch/advanced/non_castable_attributes'; const SHOW_OUT_OF_STOCK = 'cataloginventory/options/show_out_of_stock'; const LOGGING_ENABLED = 'algoliasearch/credentials/debug'; @@ -716,6 +717,22 @@ public function getBackendRenderingDisplayMode($storeId = null) return Mage::getStoreConfig(self::PREVENT_BACKEND_RENDERING_DISPLAY_MODE, $storeId); } + public function getNonCastableAttributes($storeId = null) + { + $nonCastableAttributes = array(); + $config = unserialize(Mage::getStoreConfig(self::NON_CASTABLE_ATTRIBUTES, $storeId)); + + if (is_array($config)) { + foreach ($config as $attributeData) { + if (isset($attributeData['attribute'])) { + $nonCastableAttributes[] = $attributeData['attribute']; + } + } + } + + return $nonCastableAttributes; + } + private function getCustomRanking($configName, $storeId = null) { $attrs = unserialize(Mage::getStoreConfig($configName, $storeId)); diff --git a/app/code/community/Algolia/Algoliasearch/Helper/Entity/Helper.php b/app/code/community/Algolia/Algoliasearch/Helper/Entity/Helper.php index 65c3923e..d061ad2e 100644 --- a/app/code/community/Algolia/Algoliasearch/Helper/Entity/Helper.php +++ b/app/code/community/Algolia/Algoliasearch/Helper/Entity/Helper.php @@ -14,6 +14,9 @@ abstract class Algolia_Algoliasearch_Helper_Entity_Helper protected static $_activeCategories; protected static $_categoryNames; + /** @var array */ + private $nonCastableAttributes = array('sku', 'name', 'description'); + abstract protected function getIndexNameSuffix(); public function __construct() @@ -21,6 +24,12 @@ public function __construct() $this->config = Mage::helper('algoliasearch/config'); $this->algolia_helper = Mage::helper('algoliasearch/algoliahelper'); $this->logger = Mage::helper('algoliasearch/logger'); + + // Merge non castable attributes set in config + $this->nonCastableAttributes = array_merge( + $this->nonCastableAttributes, + $this->config->getNonCastableAttributes() + ); } public function getBaseIndexName($storeId = null) @@ -54,10 +63,8 @@ protected function try_cast($value) protected function castProductObject(&$productData) { - $nonCastableAttributes = array('sku', 'name', 'description'); - foreach ($productData as $key => &$data) { - if (in_array($key, $nonCastableAttributes, true) === true) { + if (in_array($key, $this->nonCastableAttributes, true) === true) { continue; } diff --git a/app/code/community/Algolia/Algoliasearch/etc/system.xml b/app/code/community/Algolia/Algoliasearch/etc/system.xml index 0eb73280..70983f25 100644 --- a/app/code/community/Algolia/Algoliasearch/etc/system.xml +++ b/app/code/community/Algolia/Algoliasearch/etc/system.xml @@ -1310,6 +1310,20 @@ 1 + + + algoliasearch/system_config_form_field_productAttributes + algoliasearch/system_config_backend_serialized_array + 120 + 1 + 1 + 1 + + + + From 3640f7633df514fb792fc3b9f480751de890242c Mon Sep 17 00:00:00 2001 From: Betty Suravech Date: Wed, 3 Oct 2018 18:27:52 +0200 Subject: [PATCH 05/36] Move admin notification logic to the toHtml() function so that it is out of the templates --- .../Block/Adminhtml/Notifications.php | 19 +++++++++++++++++++ .../algoliasearch/notifications.phtml | 11 ----------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/Notifications.php b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/Notifications.php index 677c6dfb..c456c40e 100644 --- a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/Notifications.php +++ b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/Notifications.php @@ -47,4 +47,23 @@ public function getQueueInfo() return $queueInfo; } + + /** + * Show notification based on condition + * + * @return bool + */ + protected function _toHtml() + { + $queueInfo = $this->getQueueInfo(); + if ($this->showNotification() + && $queueInfo['isEnabled'] === true + && $queueInfo['currentSize'] > 0) { + + return parent::_toHtml(); + } + + return ''; + } + } diff --git a/app/design/adminhtml/default/default/template/algoliasearch/notifications.phtml b/app/design/adminhtml/default/default/template/algoliasearch/notifications.phtml index 3a432862..4451146c 100644 --- a/app/design/adminhtml/default/default/template/algoliasearch/notifications.phtml +++ b/app/design/adminhtml/default/default/template/algoliasearch/notifications.phtml @@ -1,17 +1,6 @@ showNotification() === false) { - return; -} - $queueInfo = $this->getQueueInfo(); - -if ($queueInfo['isEnabled'] === true && $queueInfo['currentSize'] === 0) { - return; -} - ?>
From daba99d07328cdfd12a519c7acc0fe13a4b564d6 Mon Sep 17 00:00:00 2001 From: Betty Suravech Date: Tue, 9 Oct 2018 10:41:43 +0200 Subject: [PATCH 06/36] reindex sku feature preliminary module with new exception classes --- .../Block/Adminhtml/ReindexSku/Edit.php | 39 +++++ .../Block/Adminhtml/ReindexSku/Edit/Form.php | 47 ++++++ .../Algolia/Algoliasearch/Helper/Data.php | 49 ++++++- .../Helper/Entity/Producthelper.php | 38 +++++ .../Exception/ProductDeletedException.php | 6 + .../Exception/ProductDisabledException.php | 6 + .../Exception/ProductNotVisibleException.php | 6 + .../Exception/ProductOutOfStockException.php | 6 + .../Exception/ProductReindexException.php | 60 ++++++++ .../Exception/ProductUnknownSkuException.php | 6 + .../Algolia/ReindexSkuController.php | 137 ++++++++++++++++++ .../Algolia/Algoliasearch/etc/adminhtml.xml | 26 ++++ .../default/default/layout/algoliasearch.xml | 7 + .../default/algoliasearch/algoliasearch.css | 5 + 14 files changed, 437 insertions(+), 1 deletion(-) create mode 100644 app/code/community/Algolia/Algoliasearch/Block/Adminhtml/ReindexSku/Edit.php create mode 100644 app/code/community/Algolia/Algoliasearch/Block/Adminhtml/ReindexSku/Edit/Form.php create mode 100644 app/code/community/Algolia/Algoliasearch/Model/Exception/ProductDeletedException.php create mode 100644 app/code/community/Algolia/Algoliasearch/Model/Exception/ProductDisabledException.php create mode 100644 app/code/community/Algolia/Algoliasearch/Model/Exception/ProductNotVisibleException.php create mode 100644 app/code/community/Algolia/Algoliasearch/Model/Exception/ProductOutOfStockException.php create mode 100644 app/code/community/Algolia/Algoliasearch/Model/Exception/ProductReindexException.php create mode 100644 app/code/community/Algolia/Algoliasearch/Model/Exception/ProductUnknownSkuException.php create mode 100644 app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algolia/ReindexSkuController.php diff --git a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/ReindexSku/Edit.php b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/ReindexSku/Edit.php new file mode 100644 index 00000000..66ca614e --- /dev/null +++ b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/ReindexSku/Edit.php @@ -0,0 +1,39 @@ +_objectId = 'sku'; + $this->_blockGroup = 'algoliasearch'; + $this->_controller = 'adminhtml_reindexsku'; + } + + /** + * Get header text + * + * @return string + */ + public function getHeaderText() + { + return Mage::helper('algoliasearch')->__('Algolia Search - Reindex SKU(s)'); + } + + /** + * Set custom Algolia icon class + * + * @return string + */ + public function getHeaderCssClass() + { + return 'icon-head algoliasearch-head-icon'; + } + +} \ No newline at end of file diff --git a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/ReindexSku/Edit/Form.php b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/ReindexSku/Edit/Form.php new file mode 100644 index 00000000..91e5d8ba --- /dev/null +++ b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/ReindexSku/Edit/Form.php @@ -0,0 +1,47 @@ + 'edit_form', + 'action' => $this->getUrl('*/*/reindexPost'), + 'method' => 'post', + // 'enctype' => 'multipart/form-data' + )); + + $fieldset = $form->addFieldset('base_fieldset', array()); + + $html = '

'; + $html .= '

' . __('Enter here the SKU(s) you want to reindex separated by commas or carriage returns.') . '

'; + $html .= '

' . __('You will be notified if there is any reason why your product can\'t be reindexed.') . '

'; + $html .= '

' . __('It can be :') . '

'; + $html .= '
    '; + $html .= '
  • ' . __('Product is disabled.') . '
  • '; + $html .= '
  • ' . __('Product is deleted.') . '
  • '; + $html .= '
  • ' . __('Product is out of stock.') . '
  • '; + $html .= '
  • ' . __('Product is not visible.') . '
  • '; + $html .= '
  • ' . __('Product is not related to the store.') . '
  • '; + $html .= '
'; + $html .= '

' . __('You can reindex up to 10 SKUs at once.') . '

'; + + $fieldset->addField('skus', 'textarea', array( + 'name' => 'skus', + 'label' => Mage::helper('algoliasearch')->__('Product SKU(s)'), + 'title' => Mage::helper('algoliasearch')->__('Product SKU(s)'), + 'required' => true, + 'style' => 'width:100%', + 'after_element_html' => $html + )); + + $form->setUseContainer(true); + $this->setForm($form); + + return parent::_prepareForm(); + } + +} \ No newline at end of file diff --git a/app/code/community/Algolia/Algoliasearch/Helper/Data.php b/app/code/community/Algolia/Algoliasearch/Helper/Data.php index d06cbe58..8b07e342 100644 --- a/app/code/community/Algolia/Algoliasearch/Helper/Data.php +++ b/app/code/community/Algolia/Algoliasearch/Helper/Data.php @@ -437,6 +437,54 @@ public function rebuildStoreCategoryIndexPage($storeId, $collectionDefault, $pag } } + /** + * Check if product can be index on Algolia + * + * @param Mage_Catalog_Model_Product $product + * @param int $storeId + * + * @return bool + * + */ + public function canProductBeReindexed(Mage_Catalog_Model_Product $product, $storeId) + { + + if ($product->isDeleted() === true) { + throw (new Algolia_Algoliasearch_Model_Exception_ProductDeletedException()) + ->withProduct($product) + ->withStoreId($storeId); + } + + if ($product->getStatus() == Mage_Catalog_Model_Product_Status::STATUS_DISABLED) { + throw (new Algolia_Algoliasearch_Model_Exception_ProductDisabledException()) + ->withProduct($product) + ->withStoreId($storeId); + } + + if (!in_array($product->getVisibility(), [ + Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH, + Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_SEARCH, + Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG, + ])) { + throw (new Algolia_Algoliasearch_Model_Exception_ProductNotVisibleException()) + ->withProduct($product) + ->withStoreId($storeId); + } + + if (!$this->config->getShowOutOfStock($storeId)) { + if (!$product->isSalable() || !$product->getStockItem()->getIsInStock()) { + + Mage::log($product->getData()); + throw (new Algolia_Algoliasearch_Model_Exception_ProductOutOfStockException()) + ->withProduct($product) + ->withStoreId($storeId); + } + } + + return true; + } + + protected function getProductsRecords($storeId, $collection, $potentiallyDeletedProductsIds = array()) { $productsToIndex = array(); @@ -511,7 +559,6 @@ protected function getProductsRecords($storeId, $collection, $potentiallyDeleted 'toRemove' => array_unique($productsToRemove), ); } - public function rebuildStoreProductIndexPage($storeId, $collectionDefault, $page, $pageSize, $emulationInfo = null, $productIds = null, $useTmpIndex = false) { if ($this->config->isEnabledBackend($storeId) === false) { diff --git a/app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php b/app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php index e77042fd..5ccb3696 100644 --- a/app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php +++ b/app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php @@ -1041,6 +1041,44 @@ public function getObject(Mage_Catalog_Model_Product $product) return $customData; } + /** + * Returns all parent product IDs, e.g. when simple product is part of configurable or bundle + * + * @param array $productIds + * @return array + */ + public function getParentProductIds(array $productIds) + { + $parentIds = []; + foreach ($this->getCompositeTypes() as $typeInstance) { + $parentIds = array_merge($parentIds, $typeInstance->getParentIdsByChild($productIds)); + } + return $parentIds; + } + + /** + * Returns composite product type instances + * + * @return Mage_Catalog_Model_Product_Type[] + * + * @see Mage_Catalog_Model_Resource_Product_Flat_Indexer::getProductTypeInstances() + */ + private function getCompositeTypes() + { + if ($this->compositeTypes === null) { + /** @var Mage_Catalog_Model_Product $productEmulator */ + $productEmulator = Mage::getModel('catalog/product'); + /** @var Mage_Catalog_Model_Product_Type $productType */ + $productType = Mage::getModel('catalog/product_type'); + foreach ($productType->getCompositeTypes() as $typeId) { + $productEmulator->setTypeId($typeId); + $this->compositeTypes[$typeId] = $productType->factory($productEmulator); + } + } + + return $this->compositeTypes; + } + public function getAllProductIds($storeId) { $products = Mage::getModel('catalog/product')->getCollection(); diff --git a/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductDeletedException.php b/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductDeletedException.php new file mode 100644 index 00000000..f84bd22c --- /dev/null +++ b/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductDeletedException.php @@ -0,0 +1,6 @@ +product = $product; + + return $this; + } + + /** + * Add related store ID + * + * @param int $storeId + * + * @return $this + */ + public function withStoreId($storeId) + { + $this->storeId = $storeId; + + return $this; + } + + /** + * Get related product + * + * @return Mage_Catalog_Model_Product + */ + public function getProduct() + { + return $this->product; + } + + /** + * Get related store ID + * + * @return int + */ + public function getStoreId() + { + return $this->storeId; + } + +} \ No newline at end of file diff --git a/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductUnknownSkuException.php b/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductUnknownSkuException.php new file mode 100644 index 00000000..7abad38f --- /dev/null +++ b/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductUnknownSkuException.php @@ -0,0 +1,6 @@ +_title($this->__('System')) + ->_title($this->__('Algolia Search')) + ->_title($this->__('Reindex SKU(s)')); + + $this->loadLayout(); + $this->_setActiveMenu('system/algolia/reindexsku'); + $this->renderLayout(); + + // Mage::helper('algoliasearch/entity_producthelper')->getParentProductIds(); + } + + /** + * rebuildStoreProductIndex + */ + public function reindexPostAction() + { + if ($this->getRequest()->getParam('skus')) { + $skus = array_map('trim', preg_split("/(,|\r\n|\n|\r)/", $this->getRequest()->getParam('skus'))); + $stores = Mage::app()->getStores(); + $session = Mage::getSingleton('adminhtml/session'); + + if (count($skus) > self::MAX_SKUS) { + $session->addError($this->__('The maximal number of SKU(s) is %s. Could you please remove some SKU(s) to fit into the limit?', + self::MAX_SKUS)); + $this->_redirect('*/*/'); + return; + } + + // Load the collection instead of loading every one individually + $collection = Mage::getResourceModel('catalog/product_collection') + ->addAttributeToSelect('*') + ->addAttributeToFilter('sku', ['in' => $skus]) + ->setFlag('require_stock_items', true); + + foreach ($skus as $sku) { + + try { + $product = $collection->getItemByColumnValue('sku', $sku); + if (!$product) { + throw new Algolia_AlgoliaSearch_Model_Exception_ProductUnknownSkuException($this->__('Product with SKU "%s" was not found.', $sku)); + } + $this->checkAndReindex($product, $stores); + + } catch (Algolia_AlgoliaSearch_Model_Exception_ProductUnknownSkuException $e) { + $session->addError($e->getMessage()); + } catch (Algolia_AlgoliaSearch_Model_Exception_ProductDisabledException $e) { + $session->addError( + $this->__('The product "%s" (%s) is disabled in store "%s".', $e->getProduct()->getName(), $e->getProduct()->getSku(), $stores[$e->getStoreId()]->getName()) + ); + } catch (Algolia_AlgoliaSearch_Model_Exception_ProductDeletedException $e) { + $session->addError( + $this->__('The product "%s" (%s) is deleted from store "%s".', $e->getProduct()->getName(), $e->getProduct()->getSku(), $stores[$e->getStoreId()]->getName()) + ); + } catch (Algolia_AlgoliaSearch_Model_Exception_ProductNotVisibleException $e) { + $session->addError( + $this->__('The product "%s" (%s) is not visible in store "%s".', $e->getProduct()->getName(), $e->getProduct()->getSku(), $stores[$e->getStoreId()]->getName()) + ); + } catch (Algolia_Algoliasearch_Model_Exception_ProductOutOfStockException $e) { + $session->addError( + $this->__('The product "%s" (%s) is out of stock in store "%s".', $e->getProduct()->getName(), $e->getProduct()->getSku(), $stores[$e->getStoreId()]->getName()) + ); + } catch (Exception $e) { + $session->addError($e->getMessage()); + } + + } + } + + $this->_redirect('*/*/'); + + } + + /** + * @param Mage_Catalog_Model_Product $product + * @param array $stores + */ + protected function checkAndReindex($product, array $stores) + { + + $session = Mage::getSingleton('adminhtml/session'); + foreach ($stores as $storeId => $store) { + + if (!in_array($storeId, $product->getStoreIds())) { + $session->addNotice($this->__('The product "%s" (%s) is not associated with store "%s".', + $product->getName(), $product->getSku(), $store->getName())); + continue; + } + + try { + Mage::helper('algoliasearch')->canProductBeReindexed($product, $storeId); + } catch (Algolia_AlgoliaSearch_Model_Exception_ProductNotVisibleException $e) { + // If it's a simple product that is not visible, try to index its parent if it exists + if ($e->getProduct()->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_SIMPLE) { + $parentId = Mage::helper('algoliasearch/entity_producthelper')->getParentProductIds([$e->getProduct()->getId()]); + if (isset($parentId[0])) { + $parentProduct = Mage::getModel('catalog/product')->load($parentId[0]); + $session->addError( + $this->__('The product "%s" (%s) is not visible but it has a parent product "%s" (%s) for store "%s".', + $e->getProduct()->getName(), $e->getProduct()->getSku(), $parentProduct->getName(), $parentProduct->getSku(), $stores[$e->getStoreId()]->getName())); + $this->checkAndReindex($parentProduct, [$stores[$e->getStoreId()]]); + continue; + } + } + } + + $productIds = [$product->getId()]; + $productIds = array_merge($productIds, Mage::helper('algoliasearch/entity_producthelper')->getParentProductIds($productIds)); + + Mage::helper('algoliasearch')->rebuildStoreProductIndex($storeId, $productIds); + + $session->addSuccess($this->__('The product "%s" (%s) has been reindexed for store "%s".', + $product->getName(), $product->getSku(), $store->getName())); + + } + + } + + + /** + * Check ACL permissions + * + * @return bool + */ + protected function _isAllowed() + { + return Mage::getSingleton('admin/session')->isAllowed('system/algoliasearch/reindexsku'); + } + +} \ No newline at end of file diff --git a/app/code/community/Algolia/Algoliasearch/etc/adminhtml.xml b/app/code/community/Algolia/Algoliasearch/etc/adminhtml.xml index 45ca42e9..838ab4b3 100644 --- a/app/code/community/Algolia/Algoliasearch/etc/adminhtml.xml +++ b/app/code/community/Algolia/Algoliasearch/etc/adminhtml.xml @@ -1,5 +1,22 @@ + + + + + Algolia Search + 93 + + + Reindex SKU(s) + adminhtml/algolia_reindexsku + 10 + + + + + + @@ -14,6 +31,15 @@ + + Algolia Search + + + Reindex SKU(s) + 100 + + + diff --git a/app/design/adminhtml/default/default/layout/algoliasearch.xml b/app/design/adminhtml/default/default/layout/algoliasearch.xml index 9397aee3..b514005f 100644 --- a/app/design/adminhtml/default/default/layout/algoliasearch.xml +++ b/app/design/adminhtml/default/default/layout/algoliasearch.xml @@ -20,4 +20,11 @@ + + + + + + + diff --git a/skin/adminhtml/base/default/algoliasearch/algoliasearch.css b/skin/adminhtml/base/default/algoliasearch/algoliasearch.css index 2d7145ea..f5c2b95d 100644 --- a/skin/adminhtml/base/default/algoliasearch/algoliasearch.css +++ b/skin/adminhtml/base/default/algoliasearch/algoliasearch.css @@ -3,3 +3,8 @@ background-size: 16px 16px; border-color: #bee1ee; } + +.algoliasearch-head-icon { + background: url(/skin/frontend/base/default/algoliasearch/algolia-admin-menu.svg) no-repeat; + background-size: 16px 16px; +} \ No newline at end of file From 07a618a47fbf8453ee1460903214b4af1bbf3210 Mon Sep 17 00:00:00 2001 From: Betty Suravech Date: Tue, 9 Oct 2018 11:57:07 +0200 Subject: [PATCH 07/36] update outofstock condition to match productsToRemove conditions --- .../Algolia/Algoliasearch/Helper/Data.php | 28 +++++++------------ .../Helper/Entity/Producthelper.php | 3 ++ 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/app/code/community/Algolia/Algoliasearch/Helper/Data.php b/app/code/community/Algolia/Algoliasearch/Helper/Data.php index 8b07e342..df24db49 100644 --- a/app/code/community/Algolia/Algoliasearch/Helper/Data.php +++ b/app/code/community/Algolia/Algoliasearch/Helper/Data.php @@ -461,24 +461,17 @@ public function canProductBeReindexed(Mage_Catalog_Model_Product $product, $stor ->withStoreId($storeId); } - if (!in_array($product->getVisibility(), [ - Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH, - Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_SEARCH, - Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG, - ])) { + if ($this->product_helper->shouldIndexProductByItsVisibility($product, $storeId) === false) { throw (new Algolia_Algoliasearch_Model_Exception_ProductNotVisibleException()) ->withProduct($product) ->withStoreId($storeId); } - if (!$this->config->getShowOutOfStock($storeId)) { - if (!$product->isSalable() || !$product->getStockItem()->getIsInStock()) { - - Mage::log($product->getData()); - throw (new Algolia_Algoliasearch_Model_Exception_ProductOutOfStockException()) - ->withProduct($product) - ->withStoreId($storeId); - } + if (!$this->config->getShowOutOfStock($storeId) + && !$product->getStockItem()->getIsInStock()) { + throw (new Algolia_Algoliasearch_Model_Exception_ProductOutOfStockException()) + ->withProduct($product) + ->withStoreId($storeId); } return true; @@ -538,15 +531,14 @@ protected function getProductsRecords($storeId, $collection, $potentiallyDeleted continue; } - if ($product->isDeleted() === true - || $product->getStatus() == Mage_Catalog_Model_Product_Status::STATUS_DISABLED - || $this->product_helper->shouldIndexProductByItsVisibility($product, $storeId) === false - || ($product->getStockItem()->is_in_stock == 0 && !$this->config->getShowOutOfStock($storeId)) - ) { + try { + $this->canProductBeReindexed($product, $storeId); + } catch (Algolia_Algoliasearch_Model_Exception_ProductReindexException $e) { $productsToRemove[$productId] = $productId; continue; } + $productsToIndex[$productId] = $this->product_helper->getObject($product); } diff --git a/app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php b/app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php index 5ccb3696..460d1580 100644 --- a/app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php +++ b/app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php @@ -2,6 +2,9 @@ class Algolia_Algoliasearch_Helper_Entity_Producthelper extends Algolia_Algoliasearch_Helper_Entity_Helper { + + private $compositeTypes; + protected static $_productAttributes; protected static $_currencies; From 39556cc16fe67fadaed3fb25432af69d88ca76d2 Mon Sep 17 00:00:00 2001 From: Betty Suravech Date: Tue, 9 Oct 2018 15:40:19 +0200 Subject: [PATCH 08/36] update filepath name for adminhtml from algolia to algoliasearch --- .../Algoliasearch/Block/Adminhtml/ReindexSku/Edit/Form.php | 3 +-- app/code/community/Algolia/Algoliasearch/Helper/Data.php | 1 - .../{Algolia => Algoliasearch}/ReindexSkuController.php | 2 +- app/code/community/Algolia/Algoliasearch/etc/adminhtml.xml | 2 +- .../adminhtml/default/default/layout/algoliasearch.xml | 5 ++--- 5 files changed, 5 insertions(+), 8 deletions(-) rename app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/{Algolia => Algoliasearch}/ReindexSkuController.php (98%) diff --git a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/ReindexSku/Edit/Form.php b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/ReindexSku/Edit/Form.php index 91e5d8ba..9573f1ec 100644 --- a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/ReindexSku/Edit/Form.php +++ b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/ReindexSku/Edit/Form.php @@ -10,8 +10,7 @@ protected function _prepareForm() $form = new Varien_Data_Form(array( 'id' => 'edit_form', 'action' => $this->getUrl('*/*/reindexPost'), - 'method' => 'post', - // 'enctype' => 'multipart/form-data' + 'method' => 'post' )); $fieldset = $form->addFieldset('base_fieldset', array()); diff --git a/app/code/community/Algolia/Algoliasearch/Helper/Data.php b/app/code/community/Algolia/Algoliasearch/Helper/Data.php index df24db49..9c7bc0be 100644 --- a/app/code/community/Algolia/Algoliasearch/Helper/Data.php +++ b/app/code/community/Algolia/Algoliasearch/Helper/Data.php @@ -538,7 +538,6 @@ protected function getProductsRecords($storeId, $collection, $potentiallyDeleted continue; } - $productsToIndex[$productId] = $this->product_helper->getObject($product); } diff --git a/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algolia/ReindexSkuController.php b/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/ReindexSkuController.php similarity index 98% rename from app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algolia/ReindexSkuController.php rename to app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/ReindexSkuController.php index 3bf21959..386dd954 100644 --- a/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algolia/ReindexSkuController.php +++ b/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/ReindexSkuController.php @@ -1,6 +1,6 @@ Reindex SKU(s) - adminhtml/algolia_reindexsku + adminhtml/algoliasearch_reindexsku 10 diff --git a/app/design/adminhtml/default/default/layout/algoliasearch.xml b/app/design/adminhtml/default/default/layout/algoliasearch.xml index b514005f..22c42e13 100644 --- a/app/design/adminhtml/default/default/layout/algoliasearch.xml +++ b/app/design/adminhtml/default/default/layout/algoliasearch.xml @@ -21,10 +21,9 @@ - - + - + From 4aab86549ea39232febc9c0df778bb011c2ab3a1 Mon Sep 17 00:00:00 2001 From: Betty Suravech Date: Wed, 10 Oct 2018 11:24:24 +0200 Subject: [PATCH 09/36] update QueueController directory and controller path usages --- .../QueueController.php} | 2 +- js/algoliasearch/internals/adminhtml/admin_scripts.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) rename app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/{AlgoliaQueueController.php => Algoliasearch/QueueController.php} (94%) diff --git a/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/AlgoliaQueueController.php b/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/QueueController.php similarity index 94% rename from app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/AlgoliaQueueController.php rename to app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/QueueController.php index ced6b9fc..02fd8dc6 100644 --- a/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/AlgoliaQueueController.php +++ b/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/QueueController.php @@ -1,6 +1,6 @@ ⚠ ' + 'Indexing queue is not enabled
' + 'It\'s highly recommended to enable it, especially if you are on production environment. ' + @@ -80,7 +80,7 @@ algoliaAdminBundle.$(function($) { } }, 200); - $.getJSON(baseUrl + '/algoliaqueue/truncate', function(payload) { + $.getJSON(baseUrl + '/algoliasearch_queue/truncate', function(payload) { window.clearInterval(dots); if (payload.status === 'ok') { From 2ef2f49012539a17807a518a5e6c24aa5f79bce5 Mon Sep 17 00:00:00 2001 From: Betty Suravech Date: Wed, 10 Oct 2018 11:24:52 +0200 Subject: [PATCH 10/36] add configuration to the menu item for System > Algolia Search --- app/code/community/Algolia/Algoliasearch/etc/adminhtml.xml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/code/community/Algolia/Algoliasearch/etc/adminhtml.xml b/app/code/community/Algolia/Algoliasearch/etc/adminhtml.xml index 16fb0175..ec24a12e 100644 --- a/app/code/community/Algolia/Algoliasearch/etc/adminhtml.xml +++ b/app/code/community/Algolia/Algoliasearch/etc/adminhtml.xml @@ -7,6 +7,11 @@ Algolia Search 93 + + Configurations + adminhtml/system_config/edit/section/algoliasearch + 0 + Reindex SKU(s) adminhtml/algoliasearch_reindexsku From ede73fc682faf2e3878b4d53f8f00be9e69de871 Mon Sep 17 00:00:00 2001 From: Betty Suravech Date: Wed, 10 Oct 2018 11:25:26 +0200 Subject: [PATCH 11/36] correct casing for exception classes and add array_filter to skus to not process empties --- .../Exception/ProductUnknownSkuException.php | 2 +- .../Algoliasearch/ReindexSkuController.php | 15 +++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductUnknownSkuException.php b/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductUnknownSkuException.php index 7abad38f..6671ee2b 100644 --- a/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductUnknownSkuException.php +++ b/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductUnknownSkuException.php @@ -1,6 +1,6 @@ loadLayout(); $this->_setActiveMenu('system/algolia/reindexsku'); $this->renderLayout(); - - // Mage::helper('algoliasearch/entity_producthelper')->getParentProductIds(); } /** @@ -23,7 +21,7 @@ public function indexAction() public function reindexPostAction() { if ($this->getRequest()->getParam('skus')) { - $skus = array_map('trim', preg_split("/(,|\r\n|\n|\r)/", $this->getRequest()->getParam('skus'))); + $skus = array_filter(array_map('trim', preg_split("/(,|\r\n|\n|\r)/", $this->getRequest()->getParam('skus')))); $stores = Mage::app()->getStores(); $session = Mage::getSingleton('adminhtml/session'); @@ -43,23 +41,24 @@ public function reindexPostAction() foreach ($skus as $sku) { try { + $product = $collection->getItemByColumnValue('sku', $sku); if (!$product) { - throw new Algolia_AlgoliaSearch_Model_Exception_ProductUnknownSkuException($this->__('Product with SKU "%s" was not found.', $sku)); + throw new Algolia_Algoliasearch_Model_Exception_ProductUnknownSkuException($this->__('Product with SKU "%s" was not found.', $sku)); } $this->checkAndReindex($product, $stores); - } catch (Algolia_AlgoliaSearch_Model_Exception_ProductUnknownSkuException $e) { + } catch (Algolia_Algoliasearch_Model_Exception_ProductUnknownSkuException $e) { $session->addError($e->getMessage()); - } catch (Algolia_AlgoliaSearch_Model_Exception_ProductDisabledException $e) { + } catch (Algolia_Algoliasearch_Model_Exception_ProductDisabledException $e) { $session->addError( $this->__('The product "%s" (%s) is disabled in store "%s".', $e->getProduct()->getName(), $e->getProduct()->getSku(), $stores[$e->getStoreId()]->getName()) ); - } catch (Algolia_AlgoliaSearch_Model_Exception_ProductDeletedException $e) { + } catch (Algolia_Algoliasearch_Model_Exception_ProductDeletedException $e) { $session->addError( $this->__('The product "%s" (%s) is deleted from store "%s".', $e->getProduct()->getName(), $e->getProduct()->getSku(), $stores[$e->getStoreId()]->getName()) ); - } catch (Algolia_AlgoliaSearch_Model_Exception_ProductNotVisibleException $e) { + } catch (Algolia_Algoliasearch_Model_Exception_ProductNotVisibleException $e) { $session->addError( $this->__('The product "%s" (%s) is not visible in store "%s".', $e->getProduct()->getName(), $e->getProduct()->getSku(), $stores[$e->getStoreId()]->getName()) ); From 6de9cd0b1ea770ef6119c42d257617e8eb980bb5 Mon Sep 17 00:00:00 2001 From: Betty Suravech Date: Thu, 11 Oct 2018 10:28:51 +0200 Subject: [PATCH 12/36] cache queueInfo to prevent second db call to queue table --- .../Block/Adminhtml/Notifications.php | 49 +++++++++++-------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/Notifications.php b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/Notifications.php index c456c40e..a1b6ddd9 100644 --- a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/Notifications.php +++ b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/Notifications.php @@ -2,6 +2,9 @@ class Algolia_Algoliasearch_Block_Adminhtml_Notifications extends Mage_Adminhtml_Block_Template { + + protected $_queueInfo; + public function getConfigurationUrl() { return $this->getUrl('adminhtml/system_config/edit/section/algoliasearch'); @@ -17,35 +20,39 @@ public function showNotification() public function getQueueInfo() { - /** @var Algolia_Algoliasearch_Helper_Config $config */ - $config = Mage::helper('algoliasearch/config'); + if (!$this->_queueInfo) { + /** @var Algolia_Algoliasearch_Helper_Config $config */ + $config = Mage::helper('algoliasearch/config'); - /** @var Mage_Core_Model_Resource $resource */ - $resource = Mage::getSingleton('core/resource'); - $tableName = $resource->getTableName('algoliasearch/queue'); + /** @var Mage_Core_Model_Resource $resource */ + $resource = Mage::getSingleton('core/resource'); + $tableName = $resource->getTableName('algoliasearch/queue'); - $readConnection = $resource->getConnection('core_read'); + $readConnection = $resource->getConnection('core_read'); - $size = (int) $readConnection->query('SELECT COUNT(*) as total_count FROM '.$tableName)->fetchColumn(0); - $maxJobsPerSingleRun = $config->getNumberOfJobToRun(); + $size = (int)$readConnection->query('SELECT COUNT(*) as total_count FROM ' . $tableName)->fetchColumn(0); + $maxJobsPerSingleRun = $config->getNumberOfJobToRun(); - $etaMinutes = ceil($size / $maxJobsPerSingleRun) * 5; // 5 - assuming the queue runner runs every 5 minutes + $etaMinutes = ceil($size / $maxJobsPerSingleRun) * 5; // 5 - assuming the queue runner runs every 5 minutes - $eta = $etaMinutes.' minutes'; - if ($etaMinutes > 60) { - $hours = floor($etaMinutes / 60); - $restMinutes = $etaMinutes % 60; + $eta = $etaMinutes . ' minutes'; + if ($etaMinutes > 60) { + $hours = floor($etaMinutes / 60); + $restMinutes = $etaMinutes % 60; - $eta = $hours.' hours '.$restMinutes.' minutes'; - } + $eta = $hours . ' hours ' . $restMinutes . ' minutes'; + } + + $queueInfo = array( + 'isEnabled' => $config->isQueueActive(), + 'currentSize' => $size, + 'eta' => $eta, + ); - $queueInfo = array( - 'isEnabled' => $config->isQueueActive(), - 'currentSize' => $size, - 'eta' => $eta, - ); + $this->_queueInfo = $queueInfo; + } - return $queueInfo; + return $this->_queueInfo; } /** From 4b8411f3e12def17105a6bce767e96e13b26d665 Mon Sep 17 00:00:00 2001 From: Betty Suravech Date: Thu, 11 Oct 2018 10:52:11 +0200 Subject: [PATCH 13/36] styleci updates to remove returns --- .../Algolia/Algoliasearch/Block/Adminhtml/Notifications.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/Notifications.php b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/Notifications.php index a1b6ddd9..bf5610d9 100644 --- a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/Notifications.php +++ b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/Notifications.php @@ -2,7 +2,6 @@ class Algolia_Algoliasearch_Block_Adminhtml_Notifications extends Mage_Adminhtml_Block_Template { - protected $_queueInfo; public function getConfigurationUrl() @@ -66,11 +65,9 @@ protected function _toHtml() if ($this->showNotification() && $queueInfo['isEnabled'] === true && $queueInfo['currentSize'] > 0) { - return parent::_toHtml(); } return ''; } - } From 79789caef45fc780e68fa253b9662ed92d7304a8 Mon Sep 17 00:00:00 2001 From: Betty Suravech Date: Thu, 11 Oct 2018 11:37:59 +0200 Subject: [PATCH 14/36] PSR2 remove spacing and use long form array casting --- .../Block/Adminhtml/ReindexSku/Edit.php | 5 ++--- .../Block/Adminhtml/ReindexSku/Edit/Form.php | 5 ++--- .../Algolia/Algoliasearch/Helper/Data.php | 1 - .../Helper/Entity/Producthelper.php | 2 +- .../Model/Exception/ProductDeletedException.php | 1 - .../Exception/ProductDisabledException.php | 1 - .../Exception/ProductNotVisibleException.php | 1 - .../Exception/ProductOutOfStockException.php | 1 - .../Model/Exception/ProductReindexException.php | 2 -- .../Exception/ProductUnknownSkuException.php | 1 - .../Algoliasearch/ReindexSkuController.php | 17 ++++------------- 11 files changed, 9 insertions(+), 28 deletions(-) diff --git a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/ReindexSku/Edit.php b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/ReindexSku/Edit.php index 66ca614e..2f1a01d7 100644 --- a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/ReindexSku/Edit.php +++ b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/ReindexSku/Edit.php @@ -1,7 +1,7 @@ isDeleted() === true) { throw (new Algolia_Algoliasearch_Model_Exception_ProductDeletedException()) ->withProduct($product) diff --git a/app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php b/app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php index 460d1580..1e344e3f 100644 --- a/app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php +++ b/app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php @@ -1052,7 +1052,7 @@ public function getObject(Mage_Catalog_Model_Product $product) */ public function getParentProductIds(array $productIds) { - $parentIds = []; + $parentIds = array(); foreach ($this->getCompositeTypes() as $typeInstance) { $parentIds = array_merge($parentIds, $typeInstance->getParentIdsByChild($productIds)); } diff --git a/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductDeletedException.php b/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductDeletedException.php index f84bd22c..090084ae 100644 --- a/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductDeletedException.php +++ b/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductDeletedException.php @@ -2,5 +2,4 @@ class Algolia_Algoliasearch_Model_Exception_ProductDeletedException extends Algolia_Algoliasearch_Model_Exception_ProductReindexException { - } \ No newline at end of file diff --git a/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductDisabledException.php b/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductDisabledException.php index 0306ded5..f605c42e 100644 --- a/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductDisabledException.php +++ b/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductDisabledException.php @@ -2,5 +2,4 @@ class Algolia_Algoliasearch_Model_Exception_ProductDisabledException extends Algolia_Algoliasearch_Model_Exception_ProductReindexException { - } \ No newline at end of file diff --git a/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductNotVisibleException.php b/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductNotVisibleException.php index 842d1c2b..13f22ff5 100644 --- a/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductNotVisibleException.php +++ b/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductNotVisibleException.php @@ -2,5 +2,4 @@ class Algolia_Algoliasearch_Model_Exception_ProductNotVisibleException extends Algolia_Algoliasearch_Model_Exception_ProductReindexException { - } \ No newline at end of file diff --git a/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductOutOfStockException.php b/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductOutOfStockException.php index 8436e38d..58dc3c12 100644 --- a/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductOutOfStockException.php +++ b/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductOutOfStockException.php @@ -2,5 +2,4 @@ class Algolia_Algoliasearch_Model_Exception_ProductOutOfStockException extends Algolia_Algoliasearch_Model_Exception_ProductReindexException { - } \ No newline at end of file diff --git a/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductReindexException.php b/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductReindexException.php index 00a85346..d99929c7 100644 --- a/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductReindexException.php +++ b/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductReindexException.php @@ -2,7 +2,6 @@ class Algolia_Algoliasearch_Model_Exception_ProductReindexException extends RuntimeException { - /** @var Mage_Catalog_Model_Product */ protected $product; @@ -56,5 +55,4 @@ public function getStoreId() { return $this->storeId; } - } \ No newline at end of file diff --git a/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductUnknownSkuException.php b/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductUnknownSkuException.php index 6671ee2b..ecd85088 100644 --- a/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductUnknownSkuException.php +++ b/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductUnknownSkuException.php @@ -2,5 +2,4 @@ class Algolia_Algoliasearch_Model_Exception_ProductUnknownSkuException extends Algolia_Algoliasearch_Model_Exception_ProductReindexException { - } \ No newline at end of file diff --git a/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/ReindexSkuController.php b/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/ReindexSkuController.php index ab84dbe8..c9078e41 100644 --- a/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/ReindexSkuController.php +++ b/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/ReindexSkuController.php @@ -35,11 +35,10 @@ public function reindexPostAction() // Load the collection instead of loading every one individually $collection = Mage::getResourceModel('catalog/product_collection') ->addAttributeToSelect('*') - ->addAttributeToFilter('sku', ['in' => $skus]) + ->addAttributeToFilter('sku', array('in' => $skus)) ->setFlag('require_stock_items', true); foreach ($skus as $sku) { - try { $product = $collection->getItemByColumnValue('sku', $sku); @@ -69,7 +68,6 @@ public function reindexPostAction() } catch (Exception $e) { $session->addError($e->getMessage()); } - } } @@ -83,43 +81,37 @@ public function reindexPostAction() */ protected function checkAndReindex($product, array $stores) { - $session = Mage::getSingleton('adminhtml/session'); foreach ($stores as $storeId => $store) { - if (!in_array($storeId, $product->getStoreIds())) { $session->addNotice($this->__('The product "%s" (%s) is not associated with store "%s".', $product->getName(), $product->getSku(), $store->getName())); continue; } - try { Mage::helper('algoliasearch')->canProductBeReindexed($product, $storeId); } catch (Algolia_AlgoliaSearch_Model_Exception_ProductNotVisibleException $e) { // If it's a simple product that is not visible, try to index its parent if it exists if ($e->getProduct()->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_SIMPLE) { - $parentId = Mage::helper('algoliasearch/entity_producthelper')->getParentProductIds([$e->getProduct()->getId()]); + $parentId = Mage::helper('algoliasearch/entity_producthelper')->getParentProductIds(array($e->getProduct()->getId())); if (isset($parentId[0])) { $parentProduct = Mage::getModel('catalog/product')->load($parentId[0]); $session->addError( $this->__('The product "%s" (%s) is not visible but it has a parent product "%s" (%s) for store "%s".', $e->getProduct()->getName(), $e->getProduct()->getSku(), $parentProduct->getName(), $parentProduct->getSku(), $stores[$e->getStoreId()]->getName())); - $this->checkAndReindex($parentProduct, [$stores[$e->getStoreId()]]); + $this->checkAndReindex($parentProduct, array($stores[$e->getStoreId()])); continue; } } } - - $productIds = [$product->getId()]; + $productIds = array($product->getId()); $productIds = array_merge($productIds, Mage::helper('algoliasearch/entity_producthelper')->getParentProductIds($productIds)); Mage::helper('algoliasearch')->rebuildStoreProductIndex($storeId, $productIds); $session->addSuccess($this->__('The product "%s" (%s) has been reindexed for store "%s".', $product->getName(), $product->getSku(), $store->getName())); - } - } @@ -132,5 +124,4 @@ protected function _isAllowed() { return Mage::getSingleton('admin/session')->isAllowed('system/algoliasearch/reindexsku'); } - } \ No newline at end of file From 4e4fb45f3cf040d8d97271a6e26e929e37521f4a Mon Sep 17 00:00:00 2001 From: Betty Suravech Date: Fri, 12 Oct 2018 10:54:52 +0200 Subject: [PATCH 15/36] new files update by php-cs-fixer for ci --- .../Block/Adminhtml/ReindexSku/Edit.php | 12 +++--- .../Block/Adminhtml/ReindexSku/Edit/Form.php | 38 +++++++++---------- .../Exception/ProductDeletedException.php | 2 +- .../Exception/ProductDisabledException.php | 2 +- .../Exception/ProductNotVisibleException.php | 2 +- .../Exception/ProductOutOfStockException.php | 2 +- .../Exception/ProductReindexException.php | 10 ++--- .../Exception/ProductUnknownSkuException.php | 2 +- 8 files changed, 34 insertions(+), 36 deletions(-) diff --git a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/ReindexSku/Edit.php b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/ReindexSku/Edit.php index 2f1a01d7..c8aafd35 100644 --- a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/ReindexSku/Edit.php +++ b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/ReindexSku/Edit.php @@ -3,21 +3,19 @@ class Algolia_Algoliasearch_Block_Adminhtml_ReindexSku_Edit extends Mage_Adminhtml_Block_Widget_Form_Container { /** - * Internal constructor - * - * @return void + * Internal constructor. */ protected function _construct() { parent::_construct(); - $this->_objectId = 'sku'; + $this->_objectId = 'sku'; $this->_blockGroup = 'algoliasearch'; $this->_controller = 'adminhtml_reindexsku'; } /** - * Get header text + * Get header text. * * @return string */ @@ -27,7 +25,7 @@ public function getHeaderText() } /** - * Set custom Algolia icon class + * Set custom Algolia icon class. * * @return string */ @@ -35,4 +33,4 @@ public function getHeaderCssClass() { return 'icon-head algoliasearch-head-icon'; } -} \ No newline at end of file +} diff --git a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/ReindexSku/Edit/Form.php b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/ReindexSku/Edit/Form.php index 09e077a6..1eed734c 100644 --- a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/ReindexSku/Edit/Form.php +++ b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/ReindexSku/Edit/Form.php @@ -8,33 +8,33 @@ class Algolia_Algoliasearch_Block_Adminhtml_ReindexSku_Edit_Form extends Mage_Ad protected function _prepareForm() { $form = new Varien_Data_Form(array( - 'id' => 'edit_form', - 'action' => $this->getUrl('*/*/reindexPost'), - 'method' => 'post' + 'id' => 'edit_form', + 'action' => $this->getUrl('*/*/reindexPost'), + 'method' => 'post', )); $fieldset = $form->addFieldset('base_fieldset', array()); $html = '

'; - $html .= '

' . __('Enter here the SKU(s) you want to reindex separated by commas or carriage returns.') . '

'; - $html .= '

' . __('You will be notified if there is any reason why your product can\'t be reindexed.') . '

'; - $html .= '

' . __('It can be :') . '

'; + $html .= '

'.__('Enter here the SKU(s) you want to reindex separated by commas or carriage returns.').'

'; + $html .= '

'.__('You will be notified if there is any reason why your product can\'t be reindexed.').'

'; + $html .= '

'.__('It can be :').'

'; $html .= '
    '; - $html .= '
  • ' . __('Product is disabled.') . '
  • '; - $html .= '
  • ' . __('Product is deleted.') . '
  • '; - $html .= '
  • ' . __('Product is out of stock.') . '
  • '; - $html .= '
  • ' . __('Product is not visible.') . '
  • '; - $html .= '
  • ' . __('Product is not related to the store.') . '
  • '; + $html .= '
  • '.__('Product is disabled.').'
  • '; + $html .= '
  • '.__('Product is deleted.').'
  • '; + $html .= '
  • '.__('Product is out of stock.').'
  • '; + $html .= '
  • '.__('Product is not visible.').'
  • '; + $html .= '
  • '.__('Product is not related to the store.').'
  • '; $html .= '
'; - $html .= '

' . __('You can reindex up to 10 SKUs at once.') . '

'; + $html .= '

'.__('You can reindex up to 10 SKUs at once.').'

'; $fieldset->addField('skus', 'textarea', array( - 'name' => 'skus', - 'label' => Mage::helper('algoliasearch')->__('Product SKU(s)'), - 'title' => Mage::helper('algoliasearch')->__('Product SKU(s)'), - 'required' => true, - 'style' => 'width:100%', - 'after_element_html' => $html + 'name' => 'skus', + 'label' => Mage::helper('algoliasearch')->__('Product SKU(s)'), + 'title' => Mage::helper('algoliasearch')->__('Product SKU(s)'), + 'required' => true, + 'style' => 'width:100%', + 'after_element_html' => $html, )); $form->setUseContainer(true); @@ -42,4 +42,4 @@ protected function _prepareForm() return parent::_prepareForm(); } -} \ No newline at end of file +} diff --git a/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductDeletedException.php b/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductDeletedException.php index 090084ae..f33e05f3 100644 --- a/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductDeletedException.php +++ b/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductDeletedException.php @@ -2,4 +2,4 @@ class Algolia_Algoliasearch_Model_Exception_ProductDeletedException extends Algolia_Algoliasearch_Model_Exception_ProductReindexException { -} \ No newline at end of file +} diff --git a/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductDisabledException.php b/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductDisabledException.php index f605c42e..71879e0e 100644 --- a/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductDisabledException.php +++ b/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductDisabledException.php @@ -2,4 +2,4 @@ class Algolia_Algoliasearch_Model_Exception_ProductDisabledException extends Algolia_Algoliasearch_Model_Exception_ProductReindexException { -} \ No newline at end of file +} diff --git a/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductNotVisibleException.php b/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductNotVisibleException.php index 13f22ff5..bd042656 100644 --- a/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductNotVisibleException.php +++ b/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductNotVisibleException.php @@ -2,4 +2,4 @@ class Algolia_Algoliasearch_Model_Exception_ProductNotVisibleException extends Algolia_Algoliasearch_Model_Exception_ProductReindexException { -} \ No newline at end of file +} diff --git a/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductOutOfStockException.php b/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductOutOfStockException.php index 58dc3c12..3140348c 100644 --- a/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductOutOfStockException.php +++ b/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductOutOfStockException.php @@ -2,4 +2,4 @@ class Algolia_Algoliasearch_Model_Exception_ProductOutOfStockException extends Algolia_Algoliasearch_Model_Exception_ProductReindexException { -} \ No newline at end of file +} diff --git a/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductReindexException.php b/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductReindexException.php index d99929c7..c426485d 100644 --- a/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductReindexException.php +++ b/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductReindexException.php @@ -9,7 +9,7 @@ class Algolia_Algoliasearch_Model_Exception_ProductReindexException extends Runt protected $storeId; /** - * Add related product + * Add related product. * * @param Mage_Catalog_Model_Product $product * @@ -23,7 +23,7 @@ public function withProduct($product) } /** - * Add related store ID + * Add related store ID. * * @param int $storeId * @@ -37,7 +37,7 @@ public function withStoreId($storeId) } /** - * Get related product + * Get related product. * * @return Mage_Catalog_Model_Product */ @@ -47,7 +47,7 @@ public function getProduct() } /** - * Get related store ID + * Get related store ID. * * @return int */ @@ -55,4 +55,4 @@ public function getStoreId() { return $this->storeId; } -} \ No newline at end of file +} diff --git a/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductUnknownSkuException.php b/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductUnknownSkuException.php index ecd85088..2287f89b 100644 --- a/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductUnknownSkuException.php +++ b/app/code/community/Algolia/Algoliasearch/Model/Exception/ProductUnknownSkuException.php @@ -2,4 +2,4 @@ class Algolia_Algoliasearch_Model_Exception_ProductUnknownSkuException extends Algolia_Algoliasearch_Model_Exception_ProductReindexException { -} \ No newline at end of file +} From c83995fb6a79fbcd5659abf8270371be52b60bab Mon Sep 17 00:00:00 2001 From: Betty Suravech Date: Fri, 12 Oct 2018 10:58:16 +0200 Subject: [PATCH 16/36] more fixer updates on changes --- .../Helper/Entity/Producthelper.php | 1 - .../Algoliasearch/ReindexSkuController.php | 18 ++++++------------ 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php b/app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php index 1e344e3f..02090b67 100644 --- a/app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php +++ b/app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php @@ -2,7 +2,6 @@ class Algolia_Algoliasearch_Helper_Entity_Producthelper extends Algolia_Algoliasearch_Helper_Entity_Helper { - private $compositeTypes; protected static $_productAttributes; diff --git a/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/ReindexSkuController.php b/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/ReindexSkuController.php index c9078e41..02082201 100644 --- a/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/ReindexSkuController.php +++ b/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/ReindexSkuController.php @@ -1,7 +1,7 @@ renderLayout(); } - /** - * rebuildStoreProductIndex - */ public function reindexPostAction() { if ($this->getRequest()->getParam('skus')) { @@ -29,6 +26,7 @@ public function reindexPostAction() $session->addError($this->__('The maximal number of SKU(s) is %s. Could you please remove some SKU(s) to fit into the limit?', self::MAX_SKUS)); $this->_redirect('*/*/'); + return; } @@ -40,13 +38,11 @@ public function reindexPostAction() foreach ($skus as $sku) { try { - $product = $collection->getItemByColumnValue('sku', $sku); if (!$product) { throw new Algolia_Algoliasearch_Model_Exception_ProductUnknownSkuException($this->__('Product with SKU "%s" was not found.', $sku)); } $this->checkAndReindex($product, $stores); - } catch (Algolia_Algoliasearch_Model_Exception_ProductUnknownSkuException $e) { $session->addError($e->getMessage()); } catch (Algolia_Algoliasearch_Model_Exception_ProductDisabledException $e) { @@ -72,12 +68,11 @@ public function reindexPostAction() } $this->_redirect('*/*/'); - } /** * @param Mage_Catalog_Model_Product $product - * @param array $stores + * @param array $stores */ protected function checkAndReindex($product, array $stores) { @@ -114,9 +109,8 @@ protected function checkAndReindex($product, array $stores) } } - /** - * Check ACL permissions + * Check ACL permissions. * * @return bool */ @@ -124,4 +118,4 @@ protected function _isAllowed() { return Mage::getSingleton('admin/session')->isAllowed('system/algoliasearch/reindexsku'); } -} \ No newline at end of file +} From 33c74d98c19a5c0c5c823372f84c84f2435d45ec Mon Sep 17 00:00:00 2001 From: Betty Suravech Date: Fri, 12 Oct 2018 11:53:47 +0200 Subject: [PATCH 17/36] add isEnabledBackend store filtering to prevent indexing on disabled indexing stores --- .../Adminhtml/Algoliasearch/ReindexSkuController.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/ReindexSkuController.php b/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/ReindexSkuController.php index 02082201..2d1bcfc1 100644 --- a/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/ReindexSkuController.php +++ b/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/ReindexSkuController.php @@ -19,8 +19,15 @@ public function reindexPostAction() { if ($this->getRequest()->getParam('skus')) { $skus = array_filter(array_map('trim', preg_split("/(,|\r\n|\n|\r)/", $this->getRequest()->getParam('skus')))); - $stores = Mage::app()->getStores(); $session = Mage::getSingleton('adminhtml/session'); + $stores = Mage::app()->getStores(); + $config = Mage::helper('algoliasearch/config'); + + foreach ($stores as $storeId => $store) { + if ($config->isEnabledBackend($storeId) === false) { + unset($stores[$storeId]); + } + } if (count($skus) > self::MAX_SKUS) { $session->addError($this->__('The maximal number of SKU(s) is %s. Could you please remove some SKU(s) to fit into the limit?', From 9b0623f2273398d8bb4a9532f1137d383d57bbab Mon Sep 17 00:00:00 2001 From: Betty Suravech Date: Mon, 15 Oct 2018 08:59:45 -0700 Subject: [PATCH 18/36] update changes for readability and moving method to scoped helper --- .../Algolia/Algoliasearch/Helper/Data.php | 42 +------------------ .../Helper/Entity/Producthelper.php | 41 ++++++++++++++++++ .../Algoliasearch/ReindexSkuController.php | 12 ++++-- 3 files changed, 50 insertions(+), 45 deletions(-) diff --git a/app/code/community/Algolia/Algoliasearch/Helper/Data.php b/app/code/community/Algolia/Algoliasearch/Helper/Data.php index 01fcb569..d0518e4b 100644 --- a/app/code/community/Algolia/Algoliasearch/Helper/Data.php +++ b/app/code/community/Algolia/Algoliasearch/Helper/Data.php @@ -437,46 +437,6 @@ public function rebuildStoreCategoryIndexPage($storeId, $collectionDefault, $pag } } - /** - * Check if product can be index on Algolia - * - * @param Mage_Catalog_Model_Product $product - * @param int $storeId - * - * @return bool - * - */ - public function canProductBeReindexed(Mage_Catalog_Model_Product $product, $storeId) - { - if ($product->isDeleted() === true) { - throw (new Algolia_Algoliasearch_Model_Exception_ProductDeletedException()) - ->withProduct($product) - ->withStoreId($storeId); - } - - if ($product->getStatus() == Mage_Catalog_Model_Product_Status::STATUS_DISABLED) { - throw (new Algolia_Algoliasearch_Model_Exception_ProductDisabledException()) - ->withProduct($product) - ->withStoreId($storeId); - } - - if ($this->product_helper->shouldIndexProductByItsVisibility($product, $storeId) === false) { - throw (new Algolia_Algoliasearch_Model_Exception_ProductNotVisibleException()) - ->withProduct($product) - ->withStoreId($storeId); - } - - if (!$this->config->getShowOutOfStock($storeId) - && !$product->getStockItem()->getIsInStock()) { - throw (new Algolia_Algoliasearch_Model_Exception_ProductOutOfStockException()) - ->withProduct($product) - ->withStoreId($storeId); - } - - return true; - } - - protected function getProductsRecords($storeId, $collection, $potentiallyDeletedProductsIds = array()) { $productsToIndex = array(); @@ -531,7 +491,7 @@ protected function getProductsRecords($storeId, $collection, $potentiallyDeleted } try { - $this->canProductBeReindexed($product, $storeId); + $this->product_helper->canProductBeReindexed($product, $storeId); } catch (Algolia_Algoliasearch_Model_Exception_ProductReindexException $e) { $productsToRemove[$productId] = $productId; continue; diff --git a/app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php b/app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php index 02090b67..010351e0 100644 --- a/app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php +++ b/app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php @@ -1055,6 +1055,7 @@ public function getParentProductIds(array $productIds) foreach ($this->getCompositeTypes() as $typeInstance) { $parentIds = array_merge($parentIds, $typeInstance->getParentIdsByChild($productIds)); } + return $parentIds; } @@ -1070,6 +1071,7 @@ private function getCompositeTypes() if ($this->compositeTypes === null) { /** @var Mage_Catalog_Model_Product $productEmulator */ $productEmulator = Mage::getModel('catalog/product'); + /** @var Mage_Catalog_Model_Product_Type $productType */ $productType = Mage::getModel('catalog/product_type'); foreach ($productType->getCompositeTypes() as $typeId) { @@ -1124,6 +1126,45 @@ private function getVisibilityAttributeValues($storeId) return $catalog_productVisibility->{$visibilityMethod}(); } + /** + * Check if product can be index on Algolia + * + * @param Mage_Catalog_Model_Product $product + * @param int $storeId + * + * @return bool + * + */ + public function canProductBeReindexed(Mage_Catalog_Model_Product $product, $storeId) + { + if ($product->isDeleted() === true) { + throw (new Algolia_Algoliasearch_Model_Exception_ProductDeletedException()) + ->withProduct($product) + ->withStoreId($storeId); + } + + if ($product->getStatus() === Mage_Catalog_Model_Product_Status::STATUS_DISABLED) { + throw (new Algolia_Algoliasearch_Model_Exception_ProductDisabledException()) + ->withProduct($product) + ->withStoreId($storeId); + } + + if ($this->shouldIndexProductByItsVisibility($product, $storeId) === false) { + throw (new Algolia_Algoliasearch_Model_Exception_ProductNotVisibleException()) + ->withProduct($product) + ->withStoreId($storeId); + } + + if (!$this->config->getShowOutOfStock($storeId) + && !$product->getStockItem()->getIsInStock()) { + throw (new Algolia_Algoliasearch_Model_Exception_ProductOutOfStockException()) + ->withProduct($product) + ->withStoreId($storeId); + } + + return true; + } + private function explodeSynomyms($synonyms) { return array_map('trim', explode(',', $synonyms)); diff --git a/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/ReindexSkuController.php b/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/ReindexSkuController.php index 2d1bcfc1..56334943 100644 --- a/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/ReindexSkuController.php +++ b/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/ReindexSkuController.php @@ -83,7 +83,10 @@ public function reindexPostAction() */ protected function checkAndReindex($product, array $stores) { + /** @var Algolia_Algoliasearch_Helper_Entity_Producthelper $productHelper */ + $productHelper = Mage::helper('algoliasearch/entity_producthelper'); $session = Mage::getSingleton('adminhtml/session'); + foreach ($stores as $storeId => $store) { if (!in_array($storeId, $product->getStoreIds())) { $session->addNotice($this->__('The product "%s" (%s) is not associated with store "%s".', @@ -91,23 +94,24 @@ protected function checkAndReindex($product, array $stores) continue; } try { - Mage::helper('algoliasearch')->canProductBeReindexed($product, $storeId); + $productHelper->canProductBeReindexed($product, $storeId); } catch (Algolia_AlgoliaSearch_Model_Exception_ProductNotVisibleException $e) { // If it's a simple product that is not visible, try to index its parent if it exists if ($e->getProduct()->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_SIMPLE) { - $parentId = Mage::helper('algoliasearch/entity_producthelper')->getParentProductIds(array($e->getProduct()->getId())); + $parentId = $productHelper->getParentProductIds(array($e->getProduct()->getId())); if (isset($parentId[0])) { $parentProduct = Mage::getModel('catalog/product')->load($parentId[0]); $session->addError( $this->__('The product "%s" (%s) is not visible but it has a parent product "%s" (%s) for store "%s".', - $e->getProduct()->getName(), $e->getProduct()->getSku(), $parentProduct->getName(), $parentProduct->getSku(), $stores[$e->getStoreId()]->getName())); + $e->getProduct()->getName(), $e->getProduct()->getSku(), $parentProduct->getName(), + $parentProduct->getSku(), $stores[$e->getStoreId()]->getName())); $this->checkAndReindex($parentProduct, array($stores[$e->getStoreId()])); continue; } } } $productIds = array($product->getId()); - $productIds = array_merge($productIds, Mage::helper('algoliasearch/entity_producthelper')->getParentProductIds($productIds)); + $productIds = array_merge($productIds, $productHelper->getParentProductIds($productIds)); Mage::helper('algoliasearch')->rebuildStoreProductIndex($storeId, $productIds); From df8bc6cf6896558a3696368bb87b8169c3e916e3 Mon Sep 17 00:00:00 2001 From: Betty Suravech Date: Mon, 15 Oct 2018 10:13:43 -0700 Subject: [PATCH 19/36] undo strict type as product status is not always an int as tested by travis --- .../Algolia/Algoliasearch/Helper/Entity/Producthelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php b/app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php index 010351e0..54ef262a 100644 --- a/app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php +++ b/app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php @@ -1143,7 +1143,7 @@ public function canProductBeReindexed(Mage_Catalog_Model_Product $product, $stor ->withStoreId($storeId); } - if ($product->getStatus() === Mage_Catalog_Model_Product_Status::STATUS_DISABLED) { + if ($product->getStatus() == Mage_Catalog_Model_Product_Status::STATUS_DISABLED) { throw (new Algolia_Algoliasearch_Model_Exception_ProductDisabledException()) ->withProduct($product) ->withStoreId($storeId); From 9ff3911891b5c8a56bd7d83b72f29804e35806e8 Mon Sep 17 00:00:00 2001 From: Betty Suravech Date: Mon, 15 Oct 2018 10:36:41 -0700 Subject: [PATCH 20/36] update getQueueInfo method for readable structure --- .../Block/Adminhtml/Notifications.php | 48 ++++++++++--------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/Notifications.php b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/Notifications.php index bf5610d9..21db5df4 100644 --- a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/Notifications.php +++ b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/Notifications.php @@ -19,38 +19,40 @@ public function showNotification() public function getQueueInfo() { - if (!$this->_queueInfo) { - /** @var Algolia_Algoliasearch_Helper_Config $config */ - $config = Mage::helper('algoliasearch/config'); - - /** @var Mage_Core_Model_Resource $resource */ - $resource = Mage::getSingleton('core/resource'); - $tableName = $resource->getTableName('algoliasearch/queue'); + if (isset($this->_queueInfo)) { + return $this->_queueInfo; + } - $readConnection = $resource->getConnection('core_read'); + /** @var Algolia_Algoliasearch_Helper_Config $config */ + $config = Mage::helper('algoliasearch/config'); - $size = (int)$readConnection->query('SELECT COUNT(*) as total_count FROM ' . $tableName)->fetchColumn(0); - $maxJobsPerSingleRun = $config->getNumberOfJobToRun(); + /** @var Mage_Core_Model_Resource $resource */ + $resource = Mage::getSingleton('core/resource'); + $tableName = $resource->getTableName('algoliasearch/queue'); - $etaMinutes = ceil($size / $maxJobsPerSingleRun) * 5; // 5 - assuming the queue runner runs every 5 minutes + $readConnection = $resource->getConnection('core_read'); - $eta = $etaMinutes . ' minutes'; - if ($etaMinutes > 60) { - $hours = floor($etaMinutes / 60); - $restMinutes = $etaMinutes % 60; + $size = (int)$readConnection->query('SELECT COUNT(*) as total_count FROM ' . $tableName)->fetchColumn(0); + $maxJobsPerSingleRun = $config->getNumberOfJobToRun(); - $eta = $hours . ' hours ' . $restMinutes . ' minutes'; - } + $etaMinutes = ceil($size / $maxJobsPerSingleRun) * 5; // 5 - assuming the queue runner runs every 5 minutes - $queueInfo = array( - 'isEnabled' => $config->isQueueActive(), - 'currentSize' => $size, - 'eta' => $eta, - ); + $eta = $etaMinutes . ' minutes'; + if ($etaMinutes > 60) { + $hours = floor($etaMinutes / 60); + $restMinutes = $etaMinutes % 60; - $this->_queueInfo = $queueInfo; + $eta = $hours . ' hours ' . $restMinutes . ' minutes'; } + $queueInfo = array( + 'isEnabled' => $config->isQueueActive(), + 'currentSize' => $size, + 'eta' => $eta, + ); + + $this->_queueInfo = $queueInfo; + return $this->_queueInfo; } From 5b2021566942b494dc6685acf486be7a32661d65 Mon Sep 17 00:00:00 2001 From: Betty Suravech Date: Tue, 16 Oct 2018 12:08:54 -0700 Subject: [PATCH 21/36] preliminary indexing queue controller, block, entity --- .../Block/Adminhtml/IndexingQueue.php | 42 +++++++++ .../Block/Adminhtml/IndexingQueue/Grid.php | 93 +++++++++++++++++++ .../Algolia/Algoliasearch/Model/Job.php | 36 +++++++ .../Algoliasearch/Model/Resource/Job.php | 12 +++ .../Model/Resource/Job/Collection.php | 12 +++ .../Algoliasearch/IndexingQueueController.php | 55 +++++++++++ .../Algolia/Algoliasearch/etc/adminhtml.xml | 13 ++- .../Algolia/Algoliasearch/etc/config.xml | 3 + .../default/default/layout/algoliasearch.xml | 6 ++ 9 files changed, 270 insertions(+), 2 deletions(-) create mode 100644 app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue.php create mode 100644 app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Grid.php create mode 100644 app/code/community/Algolia/Algoliasearch/Model/Job.php create mode 100644 app/code/community/Algolia/Algoliasearch/Model/Resource/Job.php create mode 100644 app/code/community/Algolia/Algoliasearch/Model/Resource/Job/Collection.php create mode 100644 app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/IndexingQueueController.php diff --git a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue.php b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue.php new file mode 100644 index 00000000..437c35fc --- /dev/null +++ b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue.php @@ -0,0 +1,42 @@ +_blockGroup = 'algoliasearch'; + $this->_controller = 'adminhtml_indexingqueue'; + + parent::__construct(); + + $this->_removeButton('add'); + $this->_addButton('clear_queue', array( + 'label' => Mage::helper('algoliasearch')->__('Clear Queue'), + 'onclick' => "location.href='" . $this->getUrl('*/*/clear') . "'", + 'class' => 'cancel', + )); + } + + /** + * Get header text. + * + * @return string + */ + public function getHeaderText() + { + return Mage::helper('algoliasearch')->__('Algolia Search - Indexing Queue'); + } + + /** + * Set custom Algolia icon class. + * + * @return string + */ + public function getHeaderCssClass() + { + return 'icon-head algoliasearch-head-icon'; + } +} \ No newline at end of file diff --git a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Grid.php b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Grid.php new file mode 100644 index 00000000..4d03318c --- /dev/null +++ b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Grid.php @@ -0,0 +1,93 @@ +setId('job_id'); + $this->setDefaultSort('query_id'); + $this->setDefaultDir('desc'); + } + + /** + * Prepare Search Report collection for grid + * + * @return Mage_Adminhtml_Block_Report_Search_Grid + */ + protected function _prepareCollection() + { + $collection = Mage::getResourceModel('algoliasearch/job_collection'); + $this->setCollection($collection); + + return parent::_prepareCollection(); + } + + /** + * Prepare Grid columns + * + * @return Mage_Adminhtml_Block_Report_Search_Grid + */ + protected function _prepareColumns() + { + $this->addColumn('job_id', array( + 'header' => Mage::helper('algoliasearch')->__('Job ID'), + 'width' => '50px', + 'filter' => false, + 'index' => 'job_id', + 'type' => 'number' + )); + + $this->addColumn('created', array( + 'header' => Mage::helper('algoliasearch')->__('Created'), + 'index' => 'created', + 'type' => 'datetime', + )); + + $this->addColumn('method', array( + 'header' => Mage::helper('algoliasearch')->__('Method'), + 'index' => 'method', + 'type' => 'options', + 'options' => Mage::getModel('algoliasearch/job')->getMethodOptionArray(), + )); + + // to format by renderer + $this->addColumn('data', array( + 'header' => Mage::helper('algoliasearch')->__('Data'), + 'index' => 'data', + )); + + $this->addColumn('max_retries', array( + 'header' => Mage::helper('algoliasearch')->__('Max Retries'), + 'width' => '50px', + 'filter' => false, + 'index' => 'max_retries', + 'type' => 'number' + )); + + $this->addColumn('retries', array( + 'header' => Mage::helper('algoliasearch')->__('Retries'), + 'width' => '50px', + 'filter' => false, + 'index' => 'retries', + 'type' => 'number' + )); + + return parent::_prepareColumns(); + } + + /** + * Retrieve Row Click callback URL + * + * @return string + */ + public function getRowUrl($row) + { + return $this->getUrl('*/*/view', array('id' => $row->getJobId())); + } +} \ No newline at end of file diff --git a/app/code/community/Algolia/Algoliasearch/Model/Job.php b/app/code/community/Algolia/Algoliasearch/Model/Job.php new file mode 100644 index 00000000..86e69940 --- /dev/null +++ b/app/code/community/Algolia/Algoliasearch/Model/Job.php @@ -0,0 +1,36 @@ +_init('algoliasearch/job'); + } + + /** + * @return array + */ + public function getMethodOptionArray() + { + return array( + 'saveConfigurationToAlgolia' => 'Save Configuration', + 'moveIndex' => 'Move Index', + 'deleteObjects' => 'Object deletion', + 'rebuildStoreCategoryIndex' => 'Store Category Reindex', + 'rebuildCategoryIndex' => 'Category Reindex', + 'rebuildStoreProductIndex' => 'Store Product Reindex', + 'rebuildProductIndex' => 'Product Reindex', + 'rebuildStoreAdditionalSectionsIndex' => 'Additional Section Reindex', + 'rebuildStoreSuggestionIndex' => 'Suggestion Reindex', + 'rebuildStorePageIndex' => 'Page Reindex', + ); + } +} \ No newline at end of file diff --git a/app/code/community/Algolia/Algoliasearch/Model/Resource/Job.php b/app/code/community/Algolia/Algoliasearch/Model/Resource/Job.php new file mode 100644 index 00000000..70255f6d --- /dev/null +++ b/app/code/community/Algolia/Algoliasearch/Model/Resource/Job.php @@ -0,0 +1,12 @@ +_init('algoliasearch/job', 'job_id'); + } +} \ No newline at end of file diff --git a/app/code/community/Algolia/Algoliasearch/Model/Resource/Job/Collection.php b/app/code/community/Algolia/Algoliasearch/Model/Resource/Job/Collection.php new file mode 100644 index 00000000..4e1d9a69 --- /dev/null +++ b/app/code/community/Algolia/Algoliasearch/Model/Resource/Job/Collection.php @@ -0,0 +1,12 @@ +_init('algoliasearch/job'); + } +} \ No newline at end of file diff --git a/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/IndexingQueueController.php b/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/IndexingQueueController.php new file mode 100644 index 00000000..14f292bd --- /dev/null +++ b/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/IndexingQueueController.php @@ -0,0 +1,55 @@ +_checkQueueIsActivated(); + return parent::preDispatch(); + } + + public function indexAction() + { + $this->_title($this->__('System')) + ->_title($this->__('Algolia Search')) + ->_title($this->__('Indexing Queue')); + + $this->loadLayout(); + $this->_setActiveMenu('system/algolia/indexing_queue'); + $this->renderLayout(); + } + + public function viewAction() + { + + } + + public function clearAction() + { + + } + + protected function _checkQueueIsActivated() + { + if (!Mage::helper('algoliasearch/config')->isQueueActive()) { + Mage::getSingleton('adminhtml/session')->addWarning( + $this->__('The indexing queue is not activated. Please activate it in the Algolia configuration.', + $this->getUrl('adminhtml/system_config/edit/section/algoliasearch'))); + } + } + + /** + * Check ACL permissions. + * + * @return bool + */ + protected function _isAllowed() + { + return Mage::getSingleton('admin/session')->isAllowed('system/algoliasearch/indexing_queue'); + } +} \ No newline at end of file diff --git a/app/code/community/Algolia/Algoliasearch/etc/adminhtml.xml b/app/code/community/Algolia/Algoliasearch/etc/adminhtml.xml index ec24a12e..feee0492 100644 --- a/app/code/community/Algolia/Algoliasearch/etc/adminhtml.xml +++ b/app/code/community/Algolia/Algoliasearch/etc/adminhtml.xml @@ -12,10 +12,15 @@ adminhtml/system_config/edit/section/algoliasearch 0 + + Indexing Queue + adminhtml/algoliasearch_indexingqueue + 10 + Reindex SKU(s) adminhtml/algoliasearch_reindexsku - 10 + 20
@@ -39,9 +44,13 @@ Algolia Search + + Indexing Queue + 100 + Reindex SKU(s) - 100 + 110 diff --git a/app/code/community/Algolia/Algoliasearch/etc/config.xml b/app/code/community/Algolia/Algoliasearch/etc/config.xml index 1ae66ed9..eb67f1b6 100644 --- a/app/code/community/Algolia/Algoliasearch/etc/config.xml +++ b/app/code/community/Algolia/Algoliasearch/etc/config.xml @@ -96,6 +96,9 @@ algoliasearch_queue
+ + algoliasearch_queue
+
diff --git a/app/design/adminhtml/default/default/layout/algoliasearch.xml b/app/design/adminhtml/default/default/layout/algoliasearch.xml index 22c42e13..fbc344c1 100644 --- a/app/design/adminhtml/default/default/layout/algoliasearch.xml +++ b/app/design/adminhtml/default/default/layout/algoliasearch.xml @@ -21,6 +21,12 @@ + + + + + + From 49df8e8fbe8484167f0050095aa25031245ffcd3 Mon Sep 17 00:00:00 2001 From: Betty Suravech Date: Tue, 16 Oct 2018 14:46:10 -0700 Subject: [PATCH 22/36] add clearQueue method to Queue model and add preliminary viewAction for queue/job item view --- .../Block/Adminhtml/IndexingQueue/Grid.php | 3 +- .../Block/Adminhtml/IndexingQueue/View.php | 38 +++++++++++++++++++ .../Algolia/Algoliasearch/Model/Queue.php | 8 ++++ .../Algoliasearch/IndexingQueueController.php | 34 +++++++++++++++++ .../Algoliasearch/QueueController.php | 9 ++--- .../default/default/layout/algoliasearch.xml | 5 +++ 6 files changed, 89 insertions(+), 8 deletions(-) create mode 100644 app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/View.php diff --git a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Grid.php b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Grid.php index 4d03318c..b41dbaaf 100644 --- a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Grid.php +++ b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Grid.php @@ -2,7 +2,6 @@ class Algolia_Algoliasearch_Block_Adminhtml_IndexingQueue_Grid extends Mage_Adminhtml_Block_Widget_Grid { - /** * Initialize Grid Properties * @@ -11,7 +10,7 @@ public function __construct() { parent::__construct(); $this->setId('job_id'); - $this->setDefaultSort('query_id'); + $this->setDefaultSort('job_id'); $this->setDefaultDir('desc'); } diff --git a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/View.php b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/View.php new file mode 100644 index 00000000..cf5197bb --- /dev/null +++ b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/View.php @@ -0,0 +1,38 @@ +_blockGroup = 'algoliasearch'; + $this->_controller = 'adminhtml_indexingqueue'; + + $this->_removeButton('edit'); + } + + /** + * Get header text. + * + * @return string + */ + public function getHeaderText() + { + return Mage::helper('algoliasearch')->__('Algolia Search - Indexing Queue Job #%s', + Mage::registry('algoliasearch_indexingqueue_job')->getId()); + } + + /** + * Set custom Algolia icon class. + * + * @return string + */ + public function getHeaderCssClass() + { + return 'icon-head algoliasearch-head-icon'; + } +} \ No newline at end of file diff --git a/app/code/community/Algolia/Algoliasearch/Model/Queue.php b/app/code/community/Algolia/Algoliasearch/Model/Queue.php index abe524fa..aeaaffb3 100644 --- a/app/code/community/Algolia/Algoliasearch/Model/Queue.php +++ b/app/code/community/Algolia/Algoliasearch/Model/Queue.php @@ -416,4 +416,12 @@ private function clearOldLogRecords() $this->db->query("DELETE FROM {$this->logTable} WHERE id IN (" . implode(", ", $idsToDelete) . ")"); } } + + public function clearQueue($canClear = false) + { + if ($canClear) { + $this->db->truncateTable($this->table); + $this->logger->log("{$this->table} table has been truncated."); + } + } } diff --git a/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/IndexingQueueController.php b/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/IndexingQueueController.php index 14f292bd..d7108b87 100644 --- a/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/IndexingQueueController.php +++ b/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/IndexingQueueController.php @@ -26,12 +26,46 @@ public function indexAction() public function viewAction() { + $this->_title($this->__('System')) + ->_title($this->__('Algolia Search')) + ->_title($this->__('Indexing Queue')); + + $id = $this->getRequest()->getParam('id'); + if (!$id) { + Mage::getSingleton('adminhtml/session')->addError( + Mage::helper('algoliasearch')->__('Indexing Queue Job ID is not set.')); + $this->_redirect('*/*/'); + return; + } + + $job = Mage::getModel('algoliasearch/job')->load($id); + if (!$job->getId()) { + Mage::getSingleton('adminhtml/session')->addError( + Mage::helper('algoliasearch')->__('This indexing queue job no longer exists.')); + $this->_redirect('*/*/'); + return; + } + Mage::register('algoliasearch_indexingqueue_job', $job); + + $this->loadLayout(); + $this->_setActiveMenu('system/algolia/indexing_queue'); + $this->renderLayout(); } public function clearAction() { + try { + /** @var Algolia_Algoliasearch_Model_Queue $queue */ + $queue = Mage::getModel('algoliasearch/queue'); + $queue->clearQueue(true); + + Mage::getSingleton('adminhtml/session')->addSuccess('Indexing Queue has been cleared.'); + } catch (Exception $e) { + Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); + } + $this->_redirect('*/*/'); } protected function _checkQueueIsActivated() diff --git a/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/QueueController.php b/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/QueueController.php index 02fd8dc6..704fd054 100644 --- a/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/QueueController.php +++ b/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/QueueController.php @@ -42,13 +42,10 @@ public function indexAction() public function truncateAction() { - /** @var Mage_Core_Model_Resource $resource */ - $resource = Mage::getSingleton('core/resource'); - $tableName = $resource->getTableName('algoliasearch/queue'); - try { - $writeConnection = $resource->getConnection('core_write'); - $writeConnection->query('TRUNCATE TABLE '.$tableName); + /** @var Algolia_Algoliasearch_Model_Queue $queue */ + $queue = Mage::getModel('algoliasearch/queue'); + $queue->clearQueue(true); $status = array('status' => 'ok'); } catch (\Exception $e) { diff --git a/app/design/adminhtml/default/default/layout/algoliasearch.xml b/app/design/adminhtml/default/default/layout/algoliasearch.xml index fbc344c1..19dd3c59 100644 --- a/app/design/adminhtml/default/default/layout/algoliasearch.xml +++ b/app/design/adminhtml/default/default/layout/algoliasearch.xml @@ -26,6 +26,11 @@ + + + + + From 9cc593ae4e105303ef4d615de283d17448f81769 Mon Sep 17 00:00:00 2001 From: Betty Suravech Date: Wed, 17 Oct 2018 08:42:07 -0700 Subject: [PATCH 23/36] update to indexing queue job view --- .../Block/Adminhtml/IndexingQueue.php | 5 +- .../IndexingQueue/{View.php => Edit.php} | 15 +-- .../Adminhtml/IndexingQueue/Edit/Form.php | 96 +++++++++++++++++++ .../Block/Adminhtml/IndexingQueue/Grid.php | 2 +- .../Algoliasearch/IndexingQueueController.php | 2 +- .../default/default/layout/algoliasearch.xml | 2 +- 6 files changed, 111 insertions(+), 11 deletions(-) rename app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/{View.php => Edit.php} (67%) create mode 100644 app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Edit/Form.php diff --git a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue.php b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue.php index 437c35fc..36b5b7b1 100644 --- a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue.php +++ b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue.php @@ -15,7 +15,8 @@ public function __construct() $this->_removeButton('add'); $this->_addButton('clear_queue', array( 'label' => Mage::helper('algoliasearch')->__('Clear Queue'), - 'onclick' => "location.href='" . $this->getUrl('*/*/clear') . "'", + 'onclick' => "if (confirm('Are you sure you want to clear the queue? This operation cannot be reverted.')) { + location.href='" . $this->getUrl('*/*/clear') . "' };", 'class' => 'cancel', )); } @@ -39,4 +40,4 @@ public function getHeaderCssClass() { return 'icon-head algoliasearch-head-icon'; } -} \ No newline at end of file +} diff --git a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/View.php b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Edit.php similarity index 67% rename from app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/View.php rename to app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Edit.php index cf5197bb..ccdb9513 100644 --- a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/View.php +++ b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Edit.php @@ -1,18 +1,20 @@ _objectId = 'job_id'; $this->_blockGroup = 'algoliasearch'; $this->_controller = 'adminhtml_indexingqueue'; - $this->_removeButton('edit'); + $this->_removeButton('save'); + $this->_removeButton('reset'); } /** @@ -23,7 +25,7 @@ protected function _construct() public function getHeaderText() { return Mage::helper('algoliasearch')->__('Algolia Search - Indexing Queue Job #%s', - Mage::registry('algoliasearch_indexingqueue_job')->getId()); + Mage::registry('algoliasearch_indexingqueue_job')->getJobId()); } /** @@ -35,4 +37,5 @@ public function getHeaderCssClass() { return 'icon-head algoliasearch-head-icon'; } -} \ No newline at end of file +} + diff --git a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Edit/Form.php b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Edit/Form.php new file mode 100644 index 00000000..fce2fc87 --- /dev/null +++ b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Edit/Form.php @@ -0,0 +1,96 @@ + 'edit_form', + 'action' => $this->getUrl('*/*/updatePost'), + 'method' => 'post', + )); + + $fieldset = $form->addFieldset('base_fieldset', array()); + + $fieldset->addField('job_id', 'text', array( + 'name' => 'job_id', + 'label' => Mage::helper('algoliasearch')->__('Job ID'), + 'title' => Mage::helper('algoliasearch')->__('Job ID'), + 'readonly' => true, + )); + + $fieldset->addField('created', 'text', array( + 'name' => 'created', + 'label' => Mage::helper('algoliasearch')->__('Created'), + 'title' => Mage::helper('algoliasearch')->__('Created'), + 'readonly' => true, + )); + + $fieldset->addField('pid', 'text', array( + 'name' => 'pid', + 'label' => Mage::helper('algoliasearch')->__('PID'), + 'title' => Mage::helper('algoliasearch')->__('PID'), + 'readonly' => true, + )); + + $fieldset->addField('class', 'text', array( + 'name' => 'class', + 'label' => Mage::helper('algoliasearch')->__('Class'), + 'title' => Mage::helper('algoliasearch')->__('Class'), + 'readonly' => true, + )); + + $fieldset->addField('method', 'text', array( + 'name' => 'method', + 'label' => Mage::helper('algoliasearch')->__('Method'), + 'title' => Mage::helper('algoliasearch')->__('Method'), + 'readonly' => true, + )); + + $fieldset->addField('data', 'textarea', array( + 'name' => 'data', + 'label' => Mage::helper('algoliasearch')->__('Data'), + 'title' => Mage::helper('algoliasearch')->__('Data'), + 'readonly' => true, + )); + + $fieldset->addField('max_retries', 'text', array( + 'name' => 'max_retries', + 'label' => Mage::helper('algoliasearch')->__('Max Retries'), + 'title' => Mage::helper('algoliasearch')->__('Max Retries'), + 'readonly' => true, + )); + + $fieldset->addField('retries', 'text', array( + 'name' => 'retries', + 'label' => Mage::helper('algoliasearch')->__('Retries'), + 'title' => Mage::helper('algoliasearch')->__('Retries'), + 'readonly' => true, + )); + + $fieldset->addField('error_log', 'textarea', array( + 'name' => 'error_log', + 'label' => Mage::helper('algoliasearch')->__('Error Log'), + 'title' => Mage::helper('algoliasearch')->__('Error Log'), + 'readonly' => true, + )); + + $fieldset->addField('data_size', 'text', array( + 'name' => 'data_size', + 'label' => Mage::helper('algoliasearch')->__('Data Size'), + 'title' => Mage::helper('algoliasearch')->__('Data Size'), + 'readonly' => true, + )); + + $form->setValues($model->getData()); + $form->setUseContainer(true); + $this->setForm($form); + + return parent::_prepareForm(); + } +} diff --git a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Grid.php b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Grid.php index b41dbaaf..1414c338 100644 --- a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Grid.php +++ b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Grid.php @@ -89,4 +89,4 @@ public function getRowUrl($row) { return $this->getUrl('*/*/view', array('id' => $row->getJobId())); } -} \ No newline at end of file +} diff --git a/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/IndexingQueueController.php b/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/IndexingQueueController.php index d7108b87..55d1da75 100644 --- a/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/IndexingQueueController.php +++ b/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/IndexingQueueController.php @@ -86,4 +86,4 @@ protected function _isAllowed() { return Mage::getSingleton('admin/session')->isAllowed('system/algoliasearch/indexing_queue'); } -} \ No newline at end of file +} diff --git a/app/design/adminhtml/default/default/layout/algoliasearch.xml b/app/design/adminhtml/default/default/layout/algoliasearch.xml index 19dd3c59..b9690e55 100644 --- a/app/design/adminhtml/default/default/layout/algoliasearch.xml +++ b/app/design/adminhtml/default/default/layout/algoliasearch.xml @@ -28,7 +28,7 @@ - + From 1d0ac829ba4a46b3ded38a406c43f14f22957852 Mon Sep 17 00:00:00 2001 From: Betty Suravech Date: Wed, 17 Oct 2018 16:01:09 -0700 Subject: [PATCH 24/36] Indexing Queue Status preliminary --- .../Block/Adminhtml/IndexingQueue/Edit.php | 1 - .../Adminhtml/IndexingQueue/Edit/Form.php | 10 +++ .../Block/Adminhtml/IndexingQueue/Grid.php | 10 ++- .../Block/Adminhtml/IndexingQueue/Status.php | 87 +++++++++++++++++++ .../Model/Indexer/Algoliaqueuerunner.php | 1 + .../Algolia/Algoliasearch/Model/Job.php | 40 +++++---- .../Algoliasearch/Model/Resource/Job.php | 2 +- .../Model/Resource/Job/Collection.php | 2 +- .../Algoliasearch/Model/Source/JobMethods.php | 40 +++++++++ .../Model/Source/JobStatuses.php | 39 +++++++++ .../default/default/layout/algoliasearch.xml | 4 +- 11 files changed, 216 insertions(+), 20 deletions(-) create mode 100644 app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Status.php create mode 100644 app/code/community/Algolia/Algoliasearch/Model/Source/JobMethods.php create mode 100644 app/code/community/Algolia/Algoliasearch/Model/Source/JobStatuses.php diff --git a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Edit.php b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Edit.php index ccdb9513..437fff38 100644 --- a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Edit.php +++ b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Edit.php @@ -38,4 +38,3 @@ public function getHeaderCssClass() return 'icon-head algoliasearch-head-icon'; } } - diff --git a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Edit/Form.php b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Edit/Form.php index fce2fc87..4ba6c4d9 100644 --- a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Edit/Form.php +++ b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Edit/Form.php @@ -31,6 +31,13 @@ protected function _prepareForm() 'readonly' => true, )); + $fieldset->addField('status', 'text', array( + 'name' => 'status', + 'label' => Mage::helper('algoliasearch')->__('Status'), + 'title' => Mage::helper('algoliasearch')->__('Status'), + 'readonly' => true + )); + $fieldset->addField('pid', 'text', array( 'name' => 'pid', 'label' => Mage::helper('algoliasearch')->__('PID'), @@ -88,6 +95,9 @@ protected function _prepareForm() )); $form->setValues($model->getData()); + $form->addValues(array( + 'status' => $model->getStatusLabel() + )); $form->setUseContainer(true); $this->setForm($form); diff --git a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Grid.php b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Grid.php index 1414c338..9de13169 100644 --- a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Grid.php +++ b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Grid.php @@ -22,6 +22,7 @@ public function __construct() protected function _prepareCollection() { $collection = Mage::getResourceModel('algoliasearch/job_collection'); + $this->setCollection($collection); return parent::_prepareCollection(); @@ -48,11 +49,18 @@ protected function _prepareColumns() 'type' => 'datetime', )); + $this->addColumn('status', array( + 'header' => Mage::helper('algoliasearch')->__('Status'), + 'index' => 'status', + 'getter' => 'getStatusLabel', + 'filter' => false, + )); + $this->addColumn('method', array( 'header' => Mage::helper('algoliasearch')->__('Method'), 'index' => 'method', 'type' => 'options', - 'options' => Mage::getModel('algoliasearch/job')->getMethodOptionArray(), + 'options' => Mage::getModel('algoliasearch/source_jobMethods')->getMethods(), )); // to format by renderer diff --git a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Status.php b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Status.php new file mode 100644 index 00000000..e6c7f58c --- /dev/null +++ b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Status.php @@ -0,0 +1,87 @@ +config = Mage::helper('algoliasearch/config'); + $this->queueRunnerIndexer = Mage::getModel('index/indexer') + ->getProcessByCode(Algolia_Algoliasearch_Model_Indexer_Algoliaqueuerunner::INDEXER_ID); + + print_r($this->queueRunnerIndexer->getData()); + } + + /** + * @return mixed + */ + public function isQueueActive() + { + return $this->configHelper->isQueueActive(); + } + + /** + * @return string + */ + public function getQueueRunnerStatus() + { + $status = 'Unknown'; + + /** @var Mage_Index_Model_Process $process */ + $process = Mage::getModel('index/process'); + $statuses = $process->getStatusesOptions(); + + if ($this->queueRunnerIndexer->getStatus() + && isset($statuses[$this->queueRunnerIndexer->getStatus()])) { + $status = $statuses[$this->queueRunnerIndexer->getStatus()]; + } + + return $status; + } + + public function getLastQueueUpdate() + { + return $this->queueRunnerIndexer->getEndedAt(); + } + + /** + * Check if the average processing time of the queue is fast + * + * @return bool + */ + private function isQueueFast() + { + $averageProcessingTime = $this->queue->getAverageProcessingTime(); + + return !is_null($averageProcessingTime) && $averageProcessingTime < self::QUEUE_FAST_LIMIT; + } + + /** + * Prepare html output + * + * @return string + */ + protected function _toHtml() + { + if ($this->isQueueActive()) { + return parent::_toHtml(); + } + + return ''; + } +} diff --git a/app/code/community/Algolia/Algoliasearch/Model/Indexer/Algoliaqueuerunner.php b/app/code/community/Algolia/Algoliasearch/Model/Indexer/Algoliaqueuerunner.php index 1c4ae6a3..0d13e32a 100644 --- a/app/code/community/Algolia/Algoliasearch/Model/Indexer/Algoliaqueuerunner.php +++ b/app/code/community/Algolia/Algoliasearch/Model/Indexer/Algoliaqueuerunner.php @@ -2,6 +2,7 @@ class Algolia_Algoliasearch_Model_Indexer_Algoliaqueuerunner extends Mage_Index_Model_Indexer_Abstract { + const INDEXER_ID = 'algolia_queue_runner'; const EVENT_MATCH_RESULT_KEY = 'algoliasearch_match_result'; /** @var Algolia_Algoliasearch_Helper_Config */ diff --git a/app/code/community/Algolia/Algoliasearch/Model/Job.php b/app/code/community/Algolia/Algoliasearch/Model/Job.php index 86e69940..965f238a 100644 --- a/app/code/community/Algolia/Algoliasearch/Model/Job.php +++ b/app/code/community/Algolia/Algoliasearch/Model/Job.php @@ -16,21 +16,31 @@ protected function _construct() } /** - * @return array + * @return string */ - public function getMethodOptionArray() + public function getStatus() { - return array( - 'saveConfigurationToAlgolia' => 'Save Configuration', - 'moveIndex' => 'Move Index', - 'deleteObjects' => 'Object deletion', - 'rebuildStoreCategoryIndex' => 'Store Category Reindex', - 'rebuildCategoryIndex' => 'Category Reindex', - 'rebuildStoreProductIndex' => 'Store Product Reindex', - 'rebuildProductIndex' => 'Product Reindex', - 'rebuildStoreAdditionalSectionsIndex' => 'Additional Section Reindex', - 'rebuildStoreSuggestionIndex' => 'Suggestion Reindex', - 'rebuildStorePageIndex' => 'Page Reindex', - ); + $status = Algolia_Algoliasearch_Model_Source_JobStatuses::STATUS_PROCESSING; + + if (is_null($this->getPid())) { + $status = Algolia_Algoliasearch_Model_Source_JobStatuses::STATUS_NEW; + } + + if ((int) $this->getRetries() >= $this->getMaxRetries()) { + $status = Algolia_Algoliasearch_Model_Source_JobStatuses::STATUS_ERROR; + } + + return $status; + } + + /** + * @return string + */ + public function getStatusLabel() + { + $status = $this->getStatus(); + $labels = Mage::getModel('algoliasearch/source_jobStatuses')->getStatuses(); + + return isset($labels[$status]) ? $labels[$status] : $status; } -} \ No newline at end of file +} diff --git a/app/code/community/Algolia/Algoliasearch/Model/Resource/Job.php b/app/code/community/Algolia/Algoliasearch/Model/Resource/Job.php index 70255f6d..3b098614 100644 --- a/app/code/community/Algolia/Algoliasearch/Model/Resource/Job.php +++ b/app/code/community/Algolia/Algoliasearch/Model/Resource/Job.php @@ -9,4 +9,4 @@ protected function _construct() { $this->_init('algoliasearch/job', 'job_id'); } -} \ No newline at end of file +} diff --git a/app/code/community/Algolia/Algoliasearch/Model/Resource/Job/Collection.php b/app/code/community/Algolia/Algoliasearch/Model/Resource/Job/Collection.php index 4e1d9a69..457c600c 100644 --- a/app/code/community/Algolia/Algoliasearch/Model/Resource/Job/Collection.php +++ b/app/code/community/Algolia/Algoliasearch/Model/Resource/Job/Collection.php @@ -9,4 +9,4 @@ protected function _construct() { $this->_init('algoliasearch/job'); } -} \ No newline at end of file +} diff --git a/app/code/community/Algolia/Algoliasearch/Model/Source/JobMethods.php b/app/code/community/Algolia/Algoliasearch/Model/Source/JobMethods.php new file mode 100644 index 00000000..9cb592e6 --- /dev/null +++ b/app/code/community/Algolia/Algoliasearch/Model/Source/JobMethods.php @@ -0,0 +1,40 @@ + 'Save Configuration', + 'moveIndex' => 'Move Index', + 'deleteObjects' => 'Object Deletion', + 'rebuildStoreCategoryIndex' => 'Store Category Reindex', + 'rebuildCategoryIndex' => 'Category Reindex', + 'rebuildStoreProductIndex' => 'Store Product Reindex', + 'rebuildProductIndex' => 'Product Reindex', + 'rebuildStoreAdditionalSectionsIndex' => 'Additional Section Reindex', + 'rebuildStoreSuggestionIndex' => 'Suggestion Reindex', + 'rebuildStorePageIndex' => 'Page Reindex', + ); + + /** + * @return array + */ + public function getMethods() + { + return $this->_methods; + } + + /** + * @return array + */ + public function toOptionArray() + { + $options = array(); + foreach ($this->_methods as $method => $label) { + $option[] = array( + 'value' => $method, + 'label' => $label, + ); + } + return $options; + } +} diff --git a/app/code/community/Algolia/Algoliasearch/Model/Source/JobStatuses.php b/app/code/community/Algolia/Algoliasearch/Model/Source/JobStatuses.php new file mode 100644 index 00000000..5a243843 --- /dev/null +++ b/app/code/community/Algolia/Algoliasearch/Model/Source/JobStatuses.php @@ -0,0 +1,39 @@ + 'New', + self::STATUS_ERROR => 'Error', + self::STATUS_PROCESSING => 'Processing', + self::STATUS_COMPLETE => 'Complete' + ); + + /** + * @return array + */ + public function getStatuses() + { + return $this->_statuses; + } + + /** + * @return array + */ + public function toOptionArray() + { + $options = array(); + foreach ($this->_methods as $method => $label) { + $option[] = array( + 'value' => $method, + 'label' => $label, + ); + } + return $options; + } +} diff --git a/app/design/adminhtml/default/default/layout/algoliasearch.xml b/app/design/adminhtml/default/default/layout/algoliasearch.xml index b9690e55..8352b41b 100644 --- a/app/design/adminhtml/default/default/layout/algoliasearch.xml +++ b/app/design/adminhtml/default/default/layout/algoliasearch.xml @@ -23,7 +23,9 @@ - + + + From 14f6b8db09b03883cb7746ac4a4d26f9a0e27a7d Mon Sep 17 00:00:00 2001 From: Betty Suravech Date: Fri, 19 Oct 2018 10:57:09 -0700 Subject: [PATCH 25/36] indexing queue statuses notification --- .../Block/Adminhtml/IndexingQueue/Grid.php | 6 +- .../IndexingQueue/Grid/Renderer/Json.php | 21 +++++ .../Block/Adminhtml/IndexingQueue/Status.php | 93 ++++++++++++++++++- .../Algolia/Algoliasearch/Model/Queue.php | 22 +++++ .../Algoliasearch/Model/Source/JobMethods.php | 9 +- .../Algoliasearch/IndexingQueueController.php | 16 ++++ .../default/default/layout/algoliasearch.xml | 5 +- .../template/algoliasearch/queue/status.phtml | 11 +++ .../default/algoliasearch/algoliasearch.css | 11 +++ 9 files changed, 183 insertions(+), 11 deletions(-) create mode 100644 app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Grid/Renderer/Json.php create mode 100644 app/design/adminhtml/default/default/template/algoliasearch/queue/status.phtml diff --git a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Grid.php b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Grid.php index 9de13169..6c6438dd 100644 --- a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Grid.php +++ b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Grid.php @@ -63,15 +63,15 @@ protected function _prepareColumns() 'options' => Mage::getModel('algoliasearch/source_jobMethods')->getMethods(), )); - // to format by renderer $this->addColumn('data', array( 'header' => Mage::helper('algoliasearch')->__('Data'), 'index' => 'data', + 'renderer' => 'Algolia_Algoliasearch_Block_Adminhtml_IndexingQueue_Grid_Renderer_Json' )); $this->addColumn('max_retries', array( 'header' => Mage::helper('algoliasearch')->__('Max Retries'), - 'width' => '50px', + 'width' => '40px', 'filter' => false, 'index' => 'max_retries', 'type' => 'number' @@ -79,7 +79,7 @@ protected function _prepareColumns() $this->addColumn('retries', array( 'header' => Mage::helper('algoliasearch')->__('Retries'), - 'width' => '50px', + 'width' => '40px', 'filter' => false, 'index' => 'retries', 'type' => 'number' diff --git a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Grid/Renderer/Json.php b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Grid/Renderer/Json.php new file mode 100644 index 00000000..7a6b913c --- /dev/null +++ b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Grid/Renderer/Json.php @@ -0,0 +1,21 @@ +getData('data')) { + $json = json_decode($json, true); + + foreach ($json as $var => $value) { + $html .= $var . ': ' . (is_array($value) ? implode(',', $value) : $value) . '
'; + } + } + return $html; + } +} diff --git a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Status.php b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Status.php index e6c7f58c..25068c1c 100644 --- a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Status.php +++ b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Status.php @@ -11,6 +11,12 @@ class Algolia_Algoliasearch_Block_Adminhtml_IndexingQueue_Status extends Mage_Ad /** @var Algolia_Algoliasearch_Helper_Config */ protected $config; + /** @var Mage_Core_Model_Date */ + protected $dateTime; + + /** @var Algolia_Algoliasearch_Model_Queue */ + protected $queue; + /** @var Mage_Index_Model_Process */ protected $queueRunnerIndexer; @@ -21,10 +27,13 @@ protected function _construct() { parent::_construct(); $this->config = Mage::helper('algoliasearch/config'); + $this->dateTime = Mage::getModel('core/date'); + $this->queue = Mage::getModel('algoliasearch/queue'); + $this->queueRunnerIndexer = Mage::getModel('index/indexer') ->getProcessByCode(Algolia_Algoliasearch_Model_Indexer_Algoliaqueuerunner::INDEXER_ID); - print_r($this->queueRunnerIndexer->getData()); + $this->setTemplate('algoliasearch/queue/status.phtml'); } /** @@ -32,7 +41,7 @@ protected function _construct() */ public function isQueueActive() { - return $this->configHelper->isQueueActive(); + return $this->config->isQueueActive(); } /** @@ -59,6 +68,74 @@ public function getLastQueueUpdate() return $this->queueRunnerIndexer->getEndedAt(); } + /** + * @return mixed + */ + public function getResetQueueUrl() + { + return $this->getUrl('*/*/reset'); + } + + /** + * @return array + */ + public function getNotices() + { + $notices = array(); + + if ($this->isQueueStuck()) { + $notices[] = ' ' . $this->__('Reset Queue') . ''; + } + + if ($this->isQueueNotProcessed()) { + $notices[] = $this->__( + 'Queue has not been processed for one hour and indexing might be stuck or your cron is not set up properly.' + ); + $notices[] = $this->__( + 'To help you, please read our documentation.', + 'https://community.algolia.com/magento/doc/m1/indexing-queue/' + ); + } + + if ($this->isQueueFast()) { + $notices[] = $this->__('The average processing time of the queue has been performed under 3 minutes.'); + $notices[] = $this->__( + 'Adding more jobs in the Indexing Queue configuration would increase the indexing speed.', + $this->getUrl('adminhtml/system_config/edit/section/algoliasearch/') + ); + } + + return $notices; + } + + /** + * If the queue status is not "ready" and it is running for more than 5 minutes, we consider that the queue is stuck + * + * @return bool + */ + private function isQueueStuck() + { + if ($this->queueRunnerIndexer->getStatus() == Mage_Index_Model_Process::STATUS_PENDING) { + return false; + } + + if ($this->getTimeSinceLastIndexerUpdate() > self::CRON_QUEUE_FREQUENCY) { + return true; + } + + return false; + } + + /** + * Check if the queue indexer has not been processed for more than 1 hour + * + * @return bool + */ + private function isQueueNotProcessed() + { + return $this->getTimeSinceLastIndexerUpdate() > self::QUEUE_NOT_PROCESSED_LIMIT; + } + /** * Check if the average processing time of the queue is fast * @@ -71,6 +148,18 @@ private function isQueueFast() return !is_null($averageProcessingTime) && $averageProcessingTime < self::QUEUE_FAST_LIMIT; } + /** @return int */ + private function getIndexerLastUpdateTimestamp() + { + return $this->dateTime->gmtTimestamp($this->queueRunnerIndexer->getLatestUpdated()); + } + + /** @return int */ + private function getTimeSinceLastIndexerUpdate() + { + return $this->dateTime->gmtTimestamp('now') - $this->getIndexerLastUpdateTimestamp(); + } + /** * Prepare html output * diff --git a/app/code/community/Algolia/Algoliasearch/Model/Queue.php b/app/code/community/Algolia/Algoliasearch/Model/Queue.php index aeaaffb3..c37d5a84 100644 --- a/app/code/community/Algolia/Algoliasearch/Model/Queue.php +++ b/app/code/community/Algolia/Algoliasearch/Model/Queue.php @@ -61,6 +61,28 @@ public function add($class, $method, $data, $data_size) )); } + /** + * Return the average processing time for the 2 last two days + * (null if there was less than 100 runs with processed jobs) + * + * @throws \Zend_Db_Statement_Exception + * + * @return float|null + */ + public function getAverageProcessingTime() + { + $data = $this->db->query( + $this->db->select() + ->from($this->logTable, ['number_of_runs' => 'COUNT(duration)', 'average_time' => 'AVG(duration)']) + ->where('processed_jobs > 0 AND with_empty_queue = 0 AND started >= (CURDATE() - INTERVAL 2 DAY)') + ); + $result = $data->fetch(); + + return (int) $result['number_of_runs'] >= 100 && isset($result['average_time']) ? + (float) $result['average_time'] : + null; + } + public function runCron($nbJobs = null, $force = false) { if (!$this->config->isQueueActive() && $force === false) { diff --git a/app/code/community/Algolia/Algoliasearch/Model/Source/JobMethods.php b/app/code/community/Algolia/Algoliasearch/Model/Source/JobMethods.php index 9cb592e6..bef1ac1a 100644 --- a/app/code/community/Algolia/Algoliasearch/Model/Source/JobMethods.php +++ b/app/code/community/Algolia/Algoliasearch/Model/Source/JobMethods.php @@ -10,9 +10,12 @@ class Algolia_Algoliasearch_Model_Source_JobMethods 'rebuildCategoryIndex' => 'Category Reindex', 'rebuildStoreProductIndex' => 'Store Product Reindex', 'rebuildProductIndex' => 'Product Reindex', - 'rebuildStoreAdditionalSectionsIndex' => 'Additional Section Reindex', - 'rebuildStoreSuggestionIndex' => 'Suggestion Reindex', - 'rebuildStorePageIndex' => 'Page Reindex', + 'rebuildStoreAdditionalSectionsIndex' => 'Store Additional Section Reindex', + 'rebuildAdditionalSectionsIndex' => 'Additional Section Reindex', + 'rebuildStoreSuggestionIndex' => 'Store Suggestion Reindex', + 'rebuildSuggestionIndex' => 'Sugesstion Reindex', + 'rebuildStorePageIndex' => 'Store Page Reindex', + 'rebuildPageIndex' => 'Page Reindex', ); /** diff --git a/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/IndexingQueueController.php b/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/IndexingQueueController.php index 55d1da75..a2e28f2d 100644 --- a/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/IndexingQueueController.php +++ b/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/IndexingQueueController.php @@ -68,6 +68,22 @@ public function clearAction() $this->_redirect('*/*/'); } + public function resetAction() + { + try { + $queueRunnerIndexer = Mage::getModel('index/indexer') + ->getProcessByCode(Algolia_Algoliasearch_Model_Indexer_Algoliaqueuerunner::INDEXER_ID); + $queueRunnerIndexer->setStatus(Mage_Index_Model_Process::STATUS_PENDING); + $queueRunnerIndexer->save(); + + Mage::getSingleton('adminhtml/session')->addSuccess('Indexing Queue has been reset.'); + } catch (Exception $e) { + Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); + } + + $this->_redirect('*/*/'); + } + protected function _checkQueueIsActivated() { if (!Mage::helper('algoliasearch/config')->isQueueActive()) { diff --git a/app/design/adminhtml/default/default/layout/algoliasearch.xml b/app/design/adminhtml/default/default/layout/algoliasearch.xml index 8352b41b..5ef89b17 100644 --- a/app/design/adminhtml/default/default/layout/algoliasearch.xml +++ b/app/design/adminhtml/default/default/layout/algoliasearch.xml @@ -23,9 +23,8 @@ - - - + + diff --git a/app/design/adminhtml/default/default/template/algoliasearch/queue/status.phtml b/app/design/adminhtml/default/default/template/algoliasearch/queue/status.phtml new file mode 100644 index 00000000..d5d43022 --- /dev/null +++ b/app/design/adminhtml/default/default/template/algoliasearch/queue/status.phtml @@ -0,0 +1,11 @@ + +
+
+

__('Status of the queue : %s.', $this->getQueueRunnerStatus()); ?>
+ __('Last Update : %s.', $this->getLastQueueUpdate()); ?>

+ getNotices() ?> + +

+ +
+
\ No newline at end of file diff --git a/skin/adminhtml/base/default/algoliasearch/algoliasearch.css b/skin/adminhtml/base/default/algoliasearch/algoliasearch.css index f5c2b95d..4ff2e11f 100644 --- a/skin/adminhtml/base/default/algoliasearch/algoliasearch.css +++ b/skin/adminhtml/base/default/algoliasearch/algoliasearch.css @@ -7,4 +7,15 @@ .algoliasearch-head-icon { background: url(/skin/frontend/base/default/algoliasearch/algolia-admin-menu.svg) no-repeat; background-size: 16px 16px; +} + +.algolia-notice { + border: 1px solid #dfdfdf; + background-color: #e9f3ff; + background-position: 10px 10px; + padding-left: 35px; + padding-top: 10px; + margin-bottom: 1rem; + color: #333; + font-size: 13px; } \ No newline at end of file From ca8214363bc9ea364f80782929620ea578298404 Mon Sep 17 00:00:00 2001 From: Betty Suravech Date: Fri, 19 Oct 2018 14:04:01 -0700 Subject: [PATCH 26/36] long form array update --- .../Algoliasearch/Block/Adminhtml/IndexingQueue/Grid.php | 1 - app/code/community/Algolia/Algoliasearch/Model/Queue.php | 2 +- skin/adminhtml/base/default/algoliasearch/algoliasearch.css | 1 - 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Grid.php b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Grid.php index 6c6438dd..90dc16c8 100644 --- a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Grid.php +++ b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Grid.php @@ -22,7 +22,6 @@ public function __construct() protected function _prepareCollection() { $collection = Mage::getResourceModel('algoliasearch/job_collection'); - $this->setCollection($collection); return parent::_prepareCollection(); diff --git a/app/code/community/Algolia/Algoliasearch/Model/Queue.php b/app/code/community/Algolia/Algoliasearch/Model/Queue.php index c37d5a84..7ada1ba9 100644 --- a/app/code/community/Algolia/Algoliasearch/Model/Queue.php +++ b/app/code/community/Algolia/Algoliasearch/Model/Queue.php @@ -73,7 +73,7 @@ public function getAverageProcessingTime() { $data = $this->db->query( $this->db->select() - ->from($this->logTable, ['number_of_runs' => 'COUNT(duration)', 'average_time' => 'AVG(duration)']) + ->from($this->logTable, array('number_of_runs' => 'COUNT(duration)', 'average_time' => 'AVG(duration)')) ->where('processed_jobs > 0 AND with_empty_queue = 0 AND started >= (CURDATE() - INTERVAL 2 DAY)') ); $result = $data->fetch(); diff --git a/skin/adminhtml/base/default/algoliasearch/algoliasearch.css b/skin/adminhtml/base/default/algoliasearch/algoliasearch.css index 4ff2e11f..ae2d0448 100644 --- a/skin/adminhtml/base/default/algoliasearch/algoliasearch.css +++ b/skin/adminhtml/base/default/algoliasearch/algoliasearch.css @@ -11,7 +11,6 @@ .algolia-notice { border: 1px solid #dfdfdf; - background-color: #e9f3ff; background-position: 10px 10px; padding-left: 35px; padding-top: 10px; From 9a1ed6762cbc844ca45d2116080d6941c7415283 Mon Sep 17 00:00:00 2001 From: Betty Suravech Date: Mon, 22 Oct 2018 15:52:33 -0700 Subject: [PATCH 27/36] feedback fixes and add archive table --- .../Block/Adminhtml/IndexingQueue/Edit.php | 1 + .../Algolia/Algoliasearch/Model/Job.php | 14 +++++++++++++ .../Algolia/Algoliasearch/Model/Queue.php | 16 ++++++++++++++- .../Algolia/Algoliasearch/etc/config.xml | 8 +++++++- .../mysql4-upgrade-1.14.0-1.15.0.php | 20 +++++++++++++++++++ .../template/algoliasearch/queue/status.phtml | 10 +++++++--- 6 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 app/code/community/Algolia/Algoliasearch/sql/algoliasearch_setup/mysql4-upgrade-1.14.0-1.15.0.php diff --git a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Edit.php b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Edit.php index 437fff38..cddca755 100644 --- a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Edit.php +++ b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Edit.php @@ -15,6 +15,7 @@ public function __construct() $this->_removeButton('save'); $this->_removeButton('reset'); + $this->_removeButton('delete'); } /** diff --git a/app/code/community/Algolia/Algoliasearch/Model/Job.php b/app/code/community/Algolia/Algoliasearch/Model/Job.php index 965f238a..9f58fa15 100644 --- a/app/code/community/Algolia/Algoliasearch/Model/Job.php +++ b/app/code/community/Algolia/Algoliasearch/Model/Job.php @@ -43,4 +43,18 @@ public function getStatusLabel() return isset($labels[$status]) ? $labels[$status] : $status; } + + /** + * @param Exception $e + * + * @return Job + */ + public function saveError(Exception $e) + { + $this->setErrorLog($e->getMessage()); + $this->save($this); + + return $this; + } + } diff --git a/app/code/community/Algolia/Algoliasearch/Model/Queue.php b/app/code/community/Algolia/Algoliasearch/Model/Queue.php index 7ada1ba9..b260bb4d 100644 --- a/app/code/community/Algolia/Algoliasearch/Model/Queue.php +++ b/app/code/community/Algolia/Algoliasearch/Model/Queue.php @@ -7,6 +7,7 @@ class Algolia_Algoliasearch_Model_Queue protected $table; protected $logTable; + protected $archiveTable; /** @var Magento_Db_Adapter_Pdo_Mysql */ protected $db; @@ -38,7 +39,8 @@ public function __construct() $coreResource = Mage::getSingleton('core/resource'); $this->table = $coreResource->getTableName('algoliasearch/queue'); - $this->logTable = $this->table.'_log'; + $this->logTable = $coreResource->getTableName('algoliasearch/queue_log'); + $this->archiveTable = $coreResource->getTableName('algoliasearch/queue_archive'); $this->db = $coreResource->getConnection('core_write'); @@ -170,14 +172,26 @@ public function run($maxJobs) } } + private function archiveFailedJobs($whereClause) + { + $this->db->query( + "INSERT INTO {$this->archiveTable} (pid, class, method, data, error_log, data_size, created_at) + SELECT pid, class, method, data, error_log, data_size, NOW() + FROM {$this->table} + WHERE " . $whereClause + ); + } + private function getJobs($maxJobs, $pid) { // Clear jobs with crossed max retries count $retryLimit = $this->config->getRetryLimit(); if ($retryLimit > 0) { $where = $this->db->quoteInto('retries >= ?', $retryLimit); + $this->archiveFailedJobs($where); $this->db->delete($this->table, $where); } else { + $this->archiveFailedJobs('retries > max_retries'); $this->db->delete($this->table, 'retries > max_retries'); } diff --git a/app/code/community/Algolia/Algoliasearch/etc/config.xml b/app/code/community/Algolia/Algoliasearch/etc/config.xml index eb67f1b6..80d61dae 100644 --- a/app/code/community/Algolia/Algoliasearch/etc/config.xml +++ b/app/code/community/Algolia/Algoliasearch/etc/config.xml @@ -2,7 +2,7 @@ - 1.14.0 + 1.15.0 @@ -99,6 +99,12 @@ algoliasearch_queue
+ + algoliasearch_queue_log
+
+ + algoliasearch_queue_archive
+
diff --git a/app/code/community/Algolia/Algoliasearch/sql/algoliasearch_setup/mysql4-upgrade-1.14.0-1.15.0.php b/app/code/community/Algolia/Algoliasearch/sql/algoliasearch_setup/mysql4-upgrade-1.14.0-1.15.0.php new file mode 100644 index 00000000..07d561aa --- /dev/null +++ b/app/code/community/Algolia/Algoliasearch/sql/algoliasearch_setup/mysql4-upgrade-1.14.0-1.15.0.php @@ -0,0 +1,20 @@ +startSetup(); + +$tableName = $installer->getTable('algoliasearch/queue_archive'); + +$installer->run(" +CREATE TABLE IF NOT EXISTS `{$tableName}` ( + `pid` int(11) DEFAULT NULL COMMENT 'Pid', + `class` varchar(50) NOT NULL COMMENT 'Class', + `method` varchar(50) NOT NULL COMMENT 'Method', + `data` text NOT NULL COMMENT 'Data', + `error_log` text NOT NULL COMMENT 'Error_log', + `data_size` int(11) DEFAULT NULL COMMENT 'Data_size', + `created_at` datetime NOT NULL COMMENT 'Created_at' +) ENGINE=InnoDB DEFAULT CHARSET=utf8; +"); + +$installer->endSetup(); diff --git a/app/design/adminhtml/default/default/template/algoliasearch/queue/status.phtml b/app/design/adminhtml/default/default/template/algoliasearch/queue/status.phtml index d5d43022..e29dc4c9 100644 --- a/app/design/adminhtml/default/default/template/algoliasearch/queue/status.phtml +++ b/app/design/adminhtml/default/default/template/algoliasearch/queue/status.phtml @@ -1,8 +1,12 @@ -
+
-

__('Status of the queue : %s.', $this->getQueueRunnerStatus()); ?>
- __('Last Update : %s.', $this->getLastQueueUpdate()); ?>

+

+ __('Status of the queue: %s', $this->getQueueRunnerStatus()); ?>
+ getLastQueueUpdate()): ?> + __('Last update: %s', $this->getLastQueueUpdate()); ?> + +

getNotices() ?>

From 7bd7cc14773ab22f081f0055e169ba84bb30473a Mon Sep 17 00:00:00 2001 From: Betty Suravech Date: Tue, 23 Oct 2018 16:36:29 -0700 Subject: [PATCH 28/36] match updated logging in queue --- .../Block/Adminhtml/IndexingQueue/Edit.php | 2 +- .../Adminhtml/IndexingQueue/Edit/Form.php | 28 ++++++++---- .../Algolia/Algoliasearch/Model/Job.php | 4 +- .../Algolia/Algoliasearch/Model/Queue.php | 45 ++++++++++++------- .../Algoliasearch/IndexingQueueController.php | 4 +- .../mysql4-upgrade-1.14.0-1.15.0.php | 6 +-- 6 files changed, 56 insertions(+), 33 deletions(-) diff --git a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Edit.php b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Edit.php index cddca755..473e18a5 100644 --- a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Edit.php +++ b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Edit.php @@ -26,7 +26,7 @@ public function __construct() public function getHeaderText() { return Mage::helper('algoliasearch')->__('Algolia Search - Indexing Queue Job #%s', - Mage::registry('algoliasearch_indexingqueue_job')->getJobId()); + Mage::registry('algoliasearch_current_job')->getJobId()); } /** diff --git a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Edit/Form.php b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Edit/Form.php index 4ba6c4d9..29d405df 100644 --- a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Edit/Form.php +++ b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Edit/Form.php @@ -7,7 +7,7 @@ class Algolia_Algoliasearch_Block_Adminhtml_IndexingQueue_Edit_Form extends Mage */ protected function _prepareForm() { - $model = Mage::registry('algoliasearch_indexingqueue_job'); + $model = Mage::registry('algoliasearch_current_job'); $form = new Varien_Data_Form(array( 'id' => 'edit_form', @@ -16,12 +16,14 @@ protected function _prepareForm() )); $fieldset = $form->addFieldset('base_fieldset', array()); + $readOnlyStyle = 'border: 0; background: none;'; $fieldset->addField('job_id', 'text', array( 'name' => 'job_id', 'label' => Mage::helper('algoliasearch')->__('Job ID'), 'title' => Mage::helper('algoliasearch')->__('Job ID'), 'readonly' => true, + 'style' => $readOnlyStyle, )); $fieldset->addField('created', 'text', array( @@ -29,13 +31,15 @@ protected function _prepareForm() 'label' => Mage::helper('algoliasearch')->__('Created'), 'title' => Mage::helper('algoliasearch')->__('Created'), 'readonly' => true, + 'style' => $readOnlyStyle, )); $fieldset->addField('status', 'text', array( 'name' => 'status', 'label' => Mage::helper('algoliasearch')->__('Status'), 'title' => Mage::helper('algoliasearch')->__('Status'), - 'readonly' => true + 'readonly' => true, + 'style' => $readOnlyStyle, )); $fieldset->addField('pid', 'text', array( @@ -43,6 +47,7 @@ protected function _prepareForm() 'label' => Mage::helper('algoliasearch')->__('PID'), 'title' => Mage::helper('algoliasearch')->__('PID'), 'readonly' => true, + 'style' => $readOnlyStyle, )); $fieldset->addField('class', 'text', array( @@ -50,6 +55,7 @@ protected function _prepareForm() 'label' => Mage::helper('algoliasearch')->__('Class'), 'title' => Mage::helper('algoliasearch')->__('Class'), 'readonly' => true, + 'style' => $readOnlyStyle, )); $fieldset->addField('method', 'text', array( @@ -57,6 +63,7 @@ protected function _prepareForm() 'label' => Mage::helper('algoliasearch')->__('Method'), 'title' => Mage::helper('algoliasearch')->__('Method'), 'readonly' => true, + 'style' => $readOnlyStyle, )); $fieldset->addField('data', 'textarea', array( @@ -71,6 +78,7 @@ protected function _prepareForm() 'label' => Mage::helper('algoliasearch')->__('Max Retries'), 'title' => Mage::helper('algoliasearch')->__('Max Retries'), 'readonly' => true, + 'style' => $readOnlyStyle, )); $fieldset->addField('retries', 'text', array( @@ -78,6 +86,15 @@ protected function _prepareForm() 'label' => Mage::helper('algoliasearch')->__('Retries'), 'title' => Mage::helper('algoliasearch')->__('Retries'), 'readonly' => true, + 'style' => $readOnlyStyle, + )); + + $fieldset->addField('data_size', 'text', array( + 'name' => 'data_size', + 'label' => Mage::helper('algoliasearch')->__('Data Size'), + 'title' => Mage::helper('algoliasearch')->__('Data Size'), + 'readonly' => true, + 'style' => $readOnlyStyle, )); $fieldset->addField('error_log', 'textarea', array( @@ -87,18 +104,13 @@ protected function _prepareForm() 'readonly' => true, )); - $fieldset->addField('data_size', 'text', array( - 'name' => 'data_size', - 'label' => Mage::helper('algoliasearch')->__('Data Size'), - 'title' => Mage::helper('algoliasearch')->__('Data Size'), - 'readonly' => true, - )); $form->setValues($model->getData()); $form->addValues(array( 'status' => $model->getStatusLabel() )); $form->setUseContainer(true); + $this->setForm($form); return parent::_prepareForm(); diff --git a/app/code/community/Algolia/Algoliasearch/Model/Job.php b/app/code/community/Algolia/Algoliasearch/Model/Job.php index 9f58fa15..bd912812 100644 --- a/app/code/community/Algolia/Algoliasearch/Model/Job.php +++ b/app/code/community/Algolia/Algoliasearch/Model/Job.php @@ -3,6 +3,7 @@ class Algolia_Algoliasearch_Model_Job extends Mage_Core_Model_Abstract { const CACHE_TAG = 'algoliasearch_queue_job'; + protected $_cacheTag = 'algoliasearch_queue_job'; protected $_eventPrefix = 'algoliasearch_queue_job'; protected $_eventObject = 'queue_job'; @@ -47,7 +48,7 @@ public function getStatusLabel() /** * @param Exception $e * - * @return Job + * @return Algolia_Algoliasearch_Model_Job */ public function saveError(Exception $e) { @@ -56,5 +57,4 @@ public function saveError(Exception $e) return $this; } - } diff --git a/app/code/community/Algolia/Algoliasearch/Model/Queue.php b/app/code/community/Algolia/Algoliasearch/Model/Queue.php index b260bb4d..eccb3c0e 100644 --- a/app/code/community/Algolia/Algoliasearch/Model/Queue.php +++ b/app/code/community/Algolia/Algoliasearch/Model/Queue.php @@ -146,24 +146,34 @@ public function run($maxJobs) $method = $job['method']; $model->{$method}(new Varien_Object($job['data'])); + // Delete one by one + $where = $this->db->quoteInto('job_id IN (?)', $job['merged_ids']); + $this->db->delete($this->table, $where); + $this->logRecord['processed_jobs'] += count($job['merged_ids']); } catch (\Exception $e) { $this->noOfFailedJobs++; + // Log error information + $logMessage = 'Queue processing ' . $job['pid'] . ' [KO]: + Class: ' . $job['class'] . ', + Method: ' . $job['method'] . ', + Parameters: ' . json_encode($job['data']); + $this->logger->log($logMessage); + + $logMessage = date('c') . ' ERROR: ' . get_class($e) . ': + ' . $e->getMessage() . ' in ' . $e->getFile() . ':' . $e->getLine() . + "\nStack trace:\n" . $e->getTraceAsString(); + $this->logger->log($logMessage); + // Increment retries, set the job ID back to NULL - $updateQuery = "UPDATE {$this->db->quoteIdentifier($this->table, true)} SET pid = NULL, retries = retries + 1 WHERE job_id IN (".implode(', ', (array) $job['merged_ids']).")"; + $updateQuery = "UPDATE {$this->db->quoteIdentifier($this->table, true)} + SET pid = NULL, retries = retries + 1 , error_log = '" . addslashes($logMessage) . "' + WHERE job_id IN (".implode(', ', (array) $job['merged_ids']).")"; $this->db->query($updateQuery); - - // log error information - $this->logger->log("Queue processing {$job['pid']} [KO]: Mage::getSingleton({$job['class']})->{$job['method']}(".json_encode($job['data']).')'); - $this->logger->log(date('c').' ERROR: '.get_class($e).": '{$e->getMessage()}' in {$e->getFile()}:{$e->getLine()}\n"."Stack trace:\n".$e->getTraceAsString()); } } - // Delete only when finished to be able to debug the queue if needed - $where = $this->db->quoteInto('pid = ?', $pid); - $this->db->delete($this->table, $where); - $isFullReindex = ($maxJobs === -1); if ($isFullReindex) { $this->run(-1); @@ -252,6 +262,15 @@ private function getJobs($maxJobs, $pid) } } + if (isset($firstJobId)) { + $lastJobId = $this->maxValueInArray($jobs, 'job_id'); + + // Reserve all new jobs since last run + $this->db->query("UPDATE {$this->db->quoteIdentifier($this->table, true)} + SET pid = " . $pid . ' + WHERE job_id >= ' . $firstJobId . " AND job_id <= $lastJobId"); + } + $this->db->commit(); } catch (\Exception $e) { $this->db->rollBack(); @@ -260,14 +279,6 @@ private function getJobs($maxJobs, $pid) throw $e; } - - if (isset($firstJobId)) { - $lastJobId = $this->maxValueInArray($jobs, 'job_id'); - - // Reserve all new jobs since last run - $this->db->query("UPDATE {$this->db->quoteIdentifier($this->table, true)} SET pid = ".$pid.' WHERE job_id >= '.$firstJobId." AND job_id <= $lastJobId"); - } - return $jobs; } diff --git a/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/IndexingQueueController.php b/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/IndexingQueueController.php index a2e28f2d..03a724f3 100644 --- a/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/IndexingQueueController.php +++ b/app/code/community/Algolia/Algoliasearch/controllers/Adminhtml/Algoliasearch/IndexingQueueController.php @@ -46,7 +46,7 @@ public function viewAction() return; } - Mage::register('algoliasearch_indexingqueue_job', $job); + Mage::register('algoliasearch_current_job', $job); $this->loadLayout(); $this->_setActiveMenu('system/algolia/indexing_queue'); @@ -88,7 +88,7 @@ protected function _checkQueueIsActivated() { if (!Mage::helper('algoliasearch/config')->isQueueActive()) { Mage::getSingleton('adminhtml/session')->addWarning( - $this->__('The indexing queue is not activated. Please activate it in the Algolia configuration.', + $this->__('The indexing queue is not enabled. Please activate it in your Algolia configuration.', $this->getUrl('adminhtml/system_config/edit/section/algoliasearch'))); } } diff --git a/app/code/community/Algolia/Algoliasearch/sql/algoliasearch_setup/mysql4-upgrade-1.14.0-1.15.0.php b/app/code/community/Algolia/Algoliasearch/sql/algoliasearch_setup/mysql4-upgrade-1.14.0-1.15.0.php index 07d561aa..d278b403 100644 --- a/app/code/community/Algolia/Algoliasearch/sql/algoliasearch_setup/mysql4-upgrade-1.14.0-1.15.0.php +++ b/app/code/community/Algolia/Algoliasearch/sql/algoliasearch_setup/mysql4-upgrade-1.14.0-1.15.0.php @@ -11,9 +11,9 @@ `class` varchar(50) NOT NULL COMMENT 'Class', `method` varchar(50) NOT NULL COMMENT 'Method', `data` text NOT NULL COMMENT 'Data', - `error_log` text NOT NULL COMMENT 'Error_log', - `data_size` int(11) DEFAULT NULL COMMENT 'Data_size', - `created_at` datetime NOT NULL COMMENT 'Created_at' + `error_log` text NOT NULL COMMENT 'Error Log', + `data_size` int(11) DEFAULT NULL COMMENT 'Data Size', + `created_at` datetime NOT NULL COMMENT 'Created At' ) ENGINE=InnoDB DEFAULT CHARSET=utf8; "); From 3f88d788ab207b3e3484b712cd7ca9e3f7abdcfc Mon Sep 17 00:00:00 2001 From: Betty Suravech Date: Fri, 26 Oct 2018 11:43:09 -0700 Subject: [PATCH 29/36] PR updates - add view action link for indexing queue grid and add additional job methods to the list --- .../Block/Adminhtml/IndexingQueue/Grid.php | 17 +++++++++++++++++ .../Algoliasearch/Model/Source/JobMethods.php | 2 ++ 2 files changed, 19 insertions(+) diff --git a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Grid.php b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Grid.php index 90dc16c8..93999db9 100644 --- a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Grid.php +++ b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Grid.php @@ -84,6 +84,23 @@ protected function _prepareColumns() 'type' => 'number' )); + $this->addColumn('action', + array( + 'header' => Mage::helper('algoliasearch')->__('Action'), + 'width' => '50px', + 'type' => 'action', + 'getter' => 'getJobId', + 'actions' => array( + array( + 'caption' => Mage::helper('algoliasearch')->__('View'), + 'url' => array('base'=>'*/*/view'), + 'field' => 'id' + ) + ), + 'filter' => false, + 'sortable' => false, + )); + return parent::_prepareColumns(); } diff --git a/app/code/community/Algolia/Algoliasearch/Model/Source/JobMethods.php b/app/code/community/Algolia/Algoliasearch/Model/Source/JobMethods.php index bef1ac1a..01107a98 100644 --- a/app/code/community/Algolia/Algoliasearch/Model/Source/JobMethods.php +++ b/app/code/community/Algolia/Algoliasearch/Model/Source/JobMethods.php @@ -3,8 +3,10 @@ class Algolia_Algoliasearch_Model_Source_JobMethods { protected $_methods = array( + 'saveSettings' => 'Save Settings', 'saveConfigurationToAlgolia' => 'Save Configuration', 'moveIndex' => 'Move Index', + 'moveProductsTmpIndex' => 'Move Products Temp Index', 'deleteObjects' => 'Object Deletion', 'rebuildStoreCategoryIndex' => 'Store Category Reindex', 'rebuildCategoryIndex' => 'Category Reindex', From 2b6493119d2950146550e481a3d6edf8efe19211 Mon Sep 17 00:00:00 2001 From: Jan Petr Date: Mon, 5 Nov 2018 14:14:46 +0100 Subject: [PATCH 30/36] Improve name of "Remove inactive products" indexer (#1054) The name is currently misleading and gives users a feeling that it'll delete inactive products from Magento DB completely. It only removed inactive products from Algolia so its' not appear in search. This commit explicitly mentions that it will remove it only from Algolia and not DB. --- .../Algoliasearch/Model/Indexer/Algoliadeleteproducts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/community/Algolia/Algoliasearch/Model/Indexer/Algoliadeleteproducts.php b/app/code/community/Algolia/Algoliasearch/Model/Indexer/Algoliadeleteproducts.php index 6e70ae19..1da8478a 100644 --- a/app/code/community/Algolia/Algoliasearch/Model/Indexer/Algoliadeleteproducts.php +++ b/app/code/community/Algolia/Algoliasearch/Model/Indexer/Algoliadeleteproducts.php @@ -18,7 +18,7 @@ public function __construct() public function getName() { - return Mage::helper('algoliasearch')->__('Algolia Search - Delete inactive products'); + return Mage::helper('algoliasearch')->__('Algolia Search - Remove inactive products from Algolia'); } public function getDescription() From 5962cc8a0ea5afa3bcae7a1ac5bd004802c82594 Mon Sep 17 00:00:00 2001 From: Hrvoje Novosel Date: Mon, 5 Nov 2018 14:49:30 +0100 Subject: [PATCH 31/36] Exclude non-salable subproducts from price calculation (#1075) --- .../Algoliasearch/Helper/Entity/Producthelper.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php b/app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php index 54ef262a..595382ad 100644 --- a/app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php +++ b/app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php @@ -600,15 +600,21 @@ protected function handlePrice(Mage_Catalog_Model_Product &$product, $sub_produc } else { if (count($sub_products) > 0) { foreach ($sub_products as $sub_product) { + if (!$sub_product->isSalable()) { + continue; + } + $price = (double) $taxHelper->getPrice($product, $sub_product->getFinalPrice(), $with_tax, null, null, null, $product->getStore(), null); $min = min($min, $price); $max = max($max, $price); } - } else { + } + + if ($min > $max) { $min = $max; - } // avoid to have PHP_INT_MAX in case of no subproducts (Corner case of visibility and stock options) + } // avoid to have PHP_INT_MAX in case of no salable subproducts (Corner case of visibility and stock options) } if ($min != $max) { From cb7dabfafddb30ba4d6d0499b8c1df862623d616 Mon Sep 17 00:00:00 2001 From: Betty Suravech Date: Wed, 7 Nov 2018 15:05:26 -0800 Subject: [PATCH 32/36] update grid default direction asc on jobId --- .../Algoliasearch/Block/Adminhtml/IndexingQueue/Grid.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Grid.php b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Grid.php index 93999db9..2b317b39 100644 --- a/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Grid.php +++ b/app/code/community/Algolia/Algoliasearch/Block/Adminhtml/IndexingQueue/Grid.php @@ -11,7 +11,7 @@ public function __construct() parent::__construct(); $this->setId('job_id'); $this->setDefaultSort('job_id'); - $this->setDefaultDir('desc'); + $this->setDefaultDir('acs'); } /** From af85ce2eb54093270ec4db47ed922483111f97bc Mon Sep 17 00:00:00 2001 From: Betty Suravech Date: Wed, 21 Nov 2018 13:02:02 -0800 Subject: [PATCH 33/36] update version number on mysql upgrade file --- app/code/community/Algolia/Algoliasearch/etc/config.xml | 2 +- ...grade-1.14.0-1.15.0.php => mysql4-upgrade-1.14.1-1.15.0.php} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename app/code/community/Algolia/Algoliasearch/sql/algoliasearch_setup/{mysql4-upgrade-1.14.0-1.15.0.php => mysql4-upgrade-1.14.1-1.15.0.php} (100%) diff --git a/app/code/community/Algolia/Algoliasearch/etc/config.xml b/app/code/community/Algolia/Algoliasearch/etc/config.xml index b2ca9c82..304c49e7 100644 --- a/app/code/community/Algolia/Algoliasearch/etc/config.xml +++ b/app/code/community/Algolia/Algoliasearch/etc/config.xml @@ -2,7 +2,7 @@ - 1.14.1 + 1.15.0 diff --git a/app/code/community/Algolia/Algoliasearch/sql/algoliasearch_setup/mysql4-upgrade-1.14.0-1.15.0.php b/app/code/community/Algolia/Algoliasearch/sql/algoliasearch_setup/mysql4-upgrade-1.14.1-1.15.0.php similarity index 100% rename from app/code/community/Algolia/Algoliasearch/sql/algoliasearch_setup/mysql4-upgrade-1.14.0-1.15.0.php rename to app/code/community/Algolia/Algoliasearch/sql/algoliasearch_setup/mysql4-upgrade-1.14.1-1.15.0.php From 8f4fd56c9f7fbda0b9eeaa4e70bc4a599c8cd432 Mon Sep 17 00:00:00 2001 From: Damien Couchez Date: Tue, 11 Dec 2018 18:26:56 +0100 Subject: [PATCH 34/36] Fix autocomplete bottom links (#1066) --- js/algoliasearch/internals/frontend/common.js | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/js/algoliasearch/internals/frontend/common.js b/js/algoliasearch/internals/frontend/common.js index b3893a4f..d380e074 100644 --- a/js/algoliasearch/internals/frontend/common.js +++ b/js/algoliasearch/internals/frontend/common.js @@ -321,13 +321,17 @@ document.addEventListener("DOMContentLoaded", function (e) { if (section.name === 'products') { source.templates.footer = function (query, content) { var keys = []; - for (var key in content.facets['categories.level0']) { - var url = algoliaConfig.baseUrl + '/catalogsearch/result/?q=' + encodeURIComponent(query.query) + '#q=' + encodeURIComponent(query.query) + '&hFR[categories.level0][0]=' + encodeURIComponent(key) + '&idx=' + algoliaConfig.indexName + '_products'; - keys.push({ - key: key, - value: content.facets['categories.level0'][key], - url: url - }); + for (var i = 0; i 0) { - ors += '' + keys[0].key + ''; - } - - if (keys.length > 1) { - ors += ', ' + keys[1].key + ''; + var orsTab = []; + for (var i = 0; i < keys.length && i < 2; i++) { + orsTab.push('' + keys[i].key + ''); + } + ors = orsTab.join(', '); } var allUrl = algoliaConfig.baseUrl + '/catalogsearch/result/?q=' + encodeURIComponent(query.query); From 5505a50d34e8fc8a34c688f7b1826387cec66404 Mon Sep 17 00:00:00 2001 From: Damien Couchez Date: Tue, 11 Dec 2018 18:29:11 +0100 Subject: [PATCH 35/36] Fix special prices when customer groups are enabled (#1071) --- .../Helper/Entity/Producthelper.php | 28 ++++++++++++------- .../algoliasearch/autocomplete/product.phtml | 2 +- .../instantsearch/hit-item.phtml | 2 +- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php b/app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php index 595382ad..36707ede 100644 --- a/app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php +++ b/app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php @@ -570,21 +570,29 @@ protected function handlePrice(Mage_Catalog_Model_Product &$product, $sub_produc $group_id = (int) $group->getData('customer_group_id'); if ($special_price && $special_price < $customData[$field][$currency_code]['group_'.$group_id]) { - $customData[$field][$currency_code]['group_'.$group_id.'_original_formated'] = $customData[$field][$currency_code]['default_formated']; + $customData[$field][$currency_code]['group_'.$group_id.'_original_formated'] = + $customData[$field][$currency_code]['default_formated']; $customData[$field][$currency_code]['group_'.$group_id] = $special_price; - $customData[$field][$currency_code]['group_'.$group_id.'_formated'] = $this->formatPrice($special_price, - false, $currency_code); + $customData[$field][$currency_code]['group_'.$group_id.'_formated'] = $this->formatPrice( + $special_price, + false, + $currency_code + ); } } - } else { - if ($special_price && $special_price < $customData[$field][$currency_code]['default']) { - $customData[$field][$currency_code]['default_original_formated'] = $customData[$field][$currency_code]['default_formated']; + } - $customData[$field][$currency_code]['default'] = $special_price; - $customData[$field][$currency_code]['default_formated'] = $this->formatPrice($special_price, - false, $currency_code); - } + if ($special_price && $special_price < $customData[$field][$currency_code]['default']) { + $customData[$field][$currency_code]['default_original_formated'] = + $customData[$field][$currency_code]['default_formated']; + + $customData[$field][$currency_code]['default'] = $special_price; + $customData[$field][$currency_code]['default_formated'] = $this->formatPrice( + $special_price, + false, + $currency_code + ); } if ($type == 'grouped' || $type == 'bundle' || $type == 'configurable') { diff --git a/app/design/frontend/base/default/template/algoliasearch/autocomplete/product.phtml b/app/design/frontend/base/default/template/algoliasearch/autocomplete/product.phtml index af59601b..5ddaef4a 100644 --- a/app/design/frontend/base/default/template/algoliasearch/autocomplete/product.phtml +++ b/app/design/frontend/base/default/template/algoliasearch/autocomplete/product.phtml @@ -11,7 +11,7 @@ $storeId = Mage::app()->getStore()->getStoreId(); $currencyCode = Mage::app()->getStore()->getCurrentCurrencyCode(); $priceKey = '.'.$currencyCode.'.default'; -if ($config->isCustomerGroupsEnabled($storeId) && $customerGroupId !== 0) { +if ($config->isCustomerGroupsEnabled($storeId)) { $priceKey = '.'.$currencyCode.'.group_'.$customerGroupId; } diff --git a/app/design/frontend/base/default/template/algoliasearch/instantsearch/hit-item.phtml b/app/design/frontend/base/default/template/algoliasearch/instantsearch/hit-item.phtml index ba11267b..a3bedf8f 100644 --- a/app/design/frontend/base/default/template/algoliasearch/instantsearch/hit-item.phtml +++ b/app/design/frontend/base/default/template/algoliasearch/instantsearch/hit-item.phtml @@ -11,7 +11,7 @@ $storeId = Mage::app()->getStore()->getStoreId(); $currencyCode = Mage::app()->getStore()->getCurrentCurrencyCode(); $priceKey = '.'.$currencyCode.'.default'; -if ($config->isCustomerGroupsEnabled($storeId) && $customerGroupId !== 0) { +if ($config->isCustomerGroupsEnabled($storeId)) { $priceKey = '.'.$currencyCode.'.group_'.$customerGroupId; } From 8a13298d7a2a439bce9f3c9f24f7c8a835fd41a5 Mon Sep 17 00:00:00 2001 From: Jan Petr Date: Tue, 11 Dec 2018 18:31:11 +0100 Subject: [PATCH 36/36] Fix error with configuration save while query rules are disabled in Algolia engine (#1077) --- .../Algoliasearch/Helper/Algoliahelper.php | 8 ++- .../Helper/Entity/Producthelper.php | 51 +++++++++++++------ 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/app/code/community/Algolia/Algoliasearch/Helper/Algoliahelper.php b/app/code/community/Algolia/Algoliasearch/Helper/Algoliahelper.php index bea47ea3..ee1fdf52 100644 --- a/app/code/community/Algolia/Algoliasearch/Helper/Algoliahelper.php +++ b/app/code/community/Algolia/Algoliasearch/Helper/Algoliahelper.php @@ -236,7 +236,13 @@ public function copySynonyms($fromIndexName, $toIndexName) $this->lastUsedIndexName = $toIndex; $this->lastTaskId = $res['taskID']; } - + + /** + * @param $fromIndexName + * @param $toIndexName + * + * @throws \AlgoliaSearch\AlgoliaException + */ public function copyQueryRules($fromIndexName, $toIndexName) { $fromIndex = $this->getIndex($fromIndexName); diff --git a/app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php b/app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php index 36707ede..37d4ddf5 100644 --- a/app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php +++ b/app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php @@ -1,5 +1,7 @@ algolia_helper->copyQueryRules($this->getIndexName($storeId), $this->getIndexName($storeId, $saveToTmpIndicesToo)); + $indexName = $this->getIndexName($storeId); + $indexNameTmp = $this->getIndexName($storeId, $saveToTmpIndicesToo); + + try { + $this->algolia_helper->copyQueryRules($indexName, $indexNameTmp); + } catch (AlgoliaException $e) { + // Fail silently if query rules are disabled on the app + // If QRs are disabled, nothing will happen and the extension will work as expected + if ($e->getMessage() !== 'Query Rules are not enabled on this application') { + throw $e; + } + } } } @@ -1273,22 +1286,28 @@ private function setFacetsQueryRules($indexName) private function clearFacetsQueryRules($index) { - $hitsPerPage = 100; - $page = 0; - do { - $fetchedQueryRules = $index->searchRules( - array( - 'context' => 'magento_filters', - 'page' => $page, - 'hitsPerPage' => $hitsPerPage, - ) - ); + try { + $hitsPerPage = 100; + $page = 0; + do { + $fetchedQueryRules = $index->searchRules(array( + 'context' => 'magento_filters', + 'page' => $page, + 'hitsPerPage' => $hitsPerPage, + )); + + foreach ($fetchedQueryRules['hits'] as $hit) { + $index->deleteRule($hit['objectID'], true); + } - foreach ($fetchedQueryRules['hits'] as $hit) { - $index->deleteRule($hit['objectID'], true); + $page++; + } while (($page * $hitsPerPage) < $fetchedQueryRules['nbHits']); + } catch (AlgoliaException $e) { + // Fail silently if query rules are disabled on the app + // If QRs are disabled, nothing will happen and the extension will work as expected + if ($e->getMessage() !== 'Query Rules are not enabled on this application') { + throw $e; } - - $page++; - } while (($page * $hitsPerPage) < $fetchedQueryRules['nbHits']); + } } }