Skip to content

Commit

Permalink
Merge pull request #972 from algolia/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Jan Petr committed Nov 22, 2017
2 parents c91305c + 48f22bc commit bf5aa0d
Show file tree
Hide file tree
Showing 30 changed files with 542 additions and 272 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ install:
- composer install --no-scripts

script:
- dev/runTests.sh -a $ALGOLIA_APPLICATION_ID -k $ALGOLIA_API_KEY -s $ALGOLIA_SEARCH_API_KEY -b http://magento.local -p magento1_tests_ -v 193 --xdebug
- dev/runTests.sh -a $ALGOLIA_APPLICATION_ID -k $ALGOLIA_API_KEY -s $ALGOLIA_SEARCH_API_KEY -b http://magento.local -p magento1_tests_ -v 193
178 changes: 102 additions & 76 deletions CHANGELOG.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Algolia Search for Magento 1.6+
==================

![Latest version](https://img.shields.io/badge/latest-1.12.0-green.svg)
![Latest version](https://img.shields.io/badge/latest-1.13.0-green.svg)

[![Build Status](https://travis-ci.org/algolia/algoliasearch-magento.svg?branch=master)](https://travis-ci.org/algolia/algoliasearch-magento)
![PHP >= 5.3](https://img.shields.io/badge/php-%3E=5.3-green.svg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function getQueueInfo()

$readConnection = $resource->getConnection('core_read');

$size = (int)$readConnection->query('SELECT COUNT(*) as total_count FROM '.$tableName)->fetchColumn(0);
$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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public function __construct()
\AlgoliaSearch\Version::addPrefixUserAgentSegment('Magento integration', $version);
\AlgoliaSearch\Version::addSuffixUserAgentSegment('PHP', phpversion());
\AlgoliaSearch\Version::addSuffixUserAgentSegment('Magento', Mage::getVersion());

if (method_exists('Mage', 'getEdition')) {
\AlgoliaSearch\Version::addSuffixUserAgentSegment('Edition', Mage::getEdition());
}
}

public function resetCredentialsFromConfig()
Expand Down
14 changes: 14 additions & 0 deletions app/code/community/Algolia/Algoliasearch/Helper/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Algolia_Algoliasearch_Helper_Config extends Mage_Core_Helper_Abstract
const MAX_VALUES_PER_FACET = 'algoliasearch/instant/max_values_per_facet';
const SORTING_INDICES = 'algoliasearch/instant/sorts';
const XML_ADD_TO_CART_ENABLE = 'algoliasearch/instant/add_to_cart_enable';
const INFINITE_SCROLL_ENABLE = 'algoliasearch/instant/infinite_scroll_enable';

const NB_OF_PRODUCTS_SUGGESTIONS = 'algoliasearch/autocomplete/nb_of_products_suggestions';
const NB_OF_CATEGORIES_SUGGESTIONS = 'algoliasearch/autocomplete/nb_of_categories_suggestions';
Expand Down Expand Up @@ -78,6 +79,7 @@ class Algolia_Algoliasearch_Helper_Config extends Mage_Core_Helper_Abstract
const INDEX_PRODUCT_ON_CATEGORY_PRODUCTS_UPDATE = 'algoliasearch/advanced/index_product_on_category_products_update';
const INDEX_ALL_CATEGORY_PRODUCTS_ON_CATEGORY_UPDATE = 'algoliasearch/advanced/index_all_category_product_on_category_update';
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 SHOW_OUT_OF_STOCK = 'cataloginventory/options/show_out_of_stock';
Expand Down Expand Up @@ -242,6 +244,12 @@ public function isAddToCartEnable($storeId = null)
return Mage::getStoreConfigFlag(self::XML_ADD_TO_CART_ENABLE, $storeId);
}

public function isInfiniteScrollEnabled($storeId = null)
{
return $this->isInstantEnabled($storeId)
&& Mage::getStoreConfigFlag(self::INFINITE_SCROLL_ENABLE, $storeId);
}

public function isRemoveBranding($storeId = null)
{
return Mage::getStoreConfigFlag(self::REMOVE_BRANDING, $storeId);
Expand Down Expand Up @@ -445,6 +453,7 @@ public function getAttributesToRetrieve($group_id)
'image_url',
'in_stock',
'type_id',
'value', // for additional sections
));

/** @var Mage_Directory_Model_Currency $currencyDirectory */
Expand Down Expand Up @@ -670,6 +679,11 @@ public function preventBackendRendering($storeId = null)
return true;
}

public function getBackendRenderingDisplayMode($storeId = null)
{
return Mage::getStoreConfig(self::PREVENT_BACKEND_RENDERING_DISPLAY_MODE, $storeId);
}

private function getCustomRanking($configName, $storeId = null)
{
$attrs = unserialize(Mage::getStoreConfig($configName, $storeId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ protected function strip($s, $completeRemoveTags = array())
{
if (!empty($completeRemoveTags)) {
$dom = new DOMDocument();
if (@$dom->loadHTML($s)) {
if (@$dom->loadHTML('<?xml encoding="utf-8" ?>' . $s)) {
$toRemove = array();
foreach ($completeRemoveTags as $tag) {
$removeTags = $dom->getElementsByTagName($tag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public function getProductCollectionQuery($storeId, $productIds = null, $only_vi

$products = $products->setStoreId($storeId);
$products = $products->addStoreFilter($storeId);
$products = $products->distinct(true);

if ($productIds && count($productIds) > 0) {
$products = $products->addAttributeToFilter('entity_id', array('in' => $productIds));
Expand All @@ -171,6 +172,7 @@ public function getProductCollectionQuery($storeId, $productIds = null, $only_vi
}

$products = $products
->addAttributeToSelect('special_price')
->addAttributeToSelect('special_from_date')
->addAttributeToSelect('special_to_date')
->addAttributeToSelect('visibility')
Expand Down Expand Up @@ -897,7 +899,7 @@ public function getObject(Mage_Catalog_Model_Product $product)

foreach ($additionalAttributes as $attribute) {
$attribute_name = $attribute['attribute'];
if (isset($customData[$attribute_name])) {
if (array_key_exists($attribute_name, $customData)) {
continue;
}

Expand Down
95 changes: 77 additions & 18 deletions app/code/community/Algolia/Algoliasearch/Model/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ public function __construct()
}

/**
* On config save.
* On configuration save
*
* @param Varien_Event_Observer $observer
*/
public function configSaved(Varien_Event_Observer $observer)
{
Expand All @@ -57,33 +59,36 @@ public function addBundleToAdmin(Varien_Event_Observer $observer)
$req = Mage::app()->getRequest();

if (strpos($req->getPathInfo(), 'system_config/edit/section/algoliasearch') !== false) {
$observer->getLayout()->getUpdate()->addHandle('algolia_bundle_handle');
$observer->getData('layout')->getUpdate()->addHandle('algolia_bundle_handle');
}
}

/**
* Call algoliasearch.xml To load js / css / phtml.
* Call algoliasearch.xml to load JS / CSS / PHTMLs
*
* @param Varien_Event_Observer $observer
* @return $this
*/
public function useAlgoliaSearchPopup(Varien_Event_Observer $observer)
{
if ($this->config->isEnabledFrontEnd()) {
if ($this->config->getApplicationID() && $this->config->getAPIKey()) {
if ($this->config->isPopupEnabled() || $this->config->isInstantEnabled()) {
$observer->getLayout()->getUpdate()->addHandle('algolia_search_handle');

if ($this->config->isDefaultSelector()) {
$observer->getLayout()->getUpdate()->addHandle('algolia_search_handle_with_topsearch');
} else {
$observer->getLayout()->getUpdate()->addHandle('algolia_search_handle_no_topsearch');
}
if (!$this->config->isEnabledFrontEnd()) {
return $this;
}

if ($this->config->preventBackendRendering() === true) {
$observer->getLayout()->getUpdate()->addHandle('algolia_search_handle_prevent_backend_rendering');
}
}
}
if (!$this->config->getApplicationID() || !$this->config->getAPIKey()) {
return $this;
}

$this->loadAlgoliasearchHandle($observer);

$this->loadSearchFormHandle($observer);

$this->loadInstantSearchHandle($observer);

$this->loadAutocompleteHandle($observer);

$this->loadPreventBackendRenderingHandle($observer);

return $this;
}

Expand Down Expand Up @@ -244,4 +249,58 @@ public function moveProductsTmpIndex(Varien_Object $event)

$this->helper->moveProductsIndex($storeId);
}

private function loadAlgoliasearchHandle(Varien_Event_Observer $observer)
{
if (!$this->config->isPopupEnabled() && !$this->config->isInstantEnabled()) {
return;
}

$observer->getData('layout')->getUpdate()->addHandle('algolia_search_handle');
}

private function loadSearchFormHandle(Varien_Event_Observer $observer)
{
if (!$this->config->isDefaultSelector()) {
return;
}

$observer->getData('layout')->getUpdate()->addHandle('algolia_search_handle_with_topsearch');
}

private function loadInstantSearchHandle(Varien_Event_Observer $observer)
{
if (!$this->config->isInstantEnabled()) {
return;
}

$category = Mage::registry('current_category');
if ($this->config->replaceCategories() && $category && $category->getDisplayMode() === 'PAGE') {
return;
}

$observer->getData('layout')->getUpdate()->addHandle('algolia_search_handle_instantsearch');
}

private function loadAutocompleteHandle(Varien_Event_Observer $observer)
{
if ($this->config->isPopupEnabled()) {
$observer->getData('layout')->getUpdate()->addHandle('algolia_search_handle_autocomplete');
}
}

private function loadPreventBackendRenderingHandle(Varien_Event_Observer $observer)
{
if (!$this->config->preventBackendRendering()) {
return;
}

$category = Mage::registry('current_category');
$backendRenderingDisplayMode = $this->config->getBackendRenderingDisplayMode();
if ($category && $backendRenderingDisplayMode === 'only_products' && $category->getDisplayMode() === 'PAGE') {
return;
}

$observer->getData('layout')->getUpdate() ->addHandle('algolia_search_handle_prevent_backend_rendering');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function prepareResult($object, $queryText, $query)

protected function _saveProductIndexes($storeId, $productIndexes)
{
if ($this->config->isEnabledBackend(Mage::app()->getStore()->getId()) === false) {
if ($this->config->isEnabledBackend($storeId) === false) {
return parent::_saveProductIndexes($storeId, $productIndexes);
}

Expand All @@ -40,32 +40,56 @@ protected function _saveProductIndexes($storeId, $productIndexes)

/**
* Only used when reindexing everything. Otherwise Model/Indexer/Algolia will take care of the rest.
*
* @param int|null $storeId
* @param array|null $productIds
*
* @return $this|Mage_CatalogSearch_Model_Resource_Fulltext
*/
public function rebuildIndex($storeId = null, $productIds = null)
{
if ($this->config->isEnabledBackend(Mage::app()->getStore()->getId()) === false) {
if ($this->config->isModuleOutputEnabled() === false) {
return parent::rebuildIndex($storeId, $productIds);
}

if ($this->config->isModuleOutputEnabled() === false) {
if ($storeId !== null) {
$this->reindex($storeId, $productIds);

return $this;
}

/** @var Mage_Core_Model_Store $store */
foreach (Mage::app()->getStores() as $store) {
$this->reindex($store->getId(), $productIds);
}

return $this;
}

private function reindex($storeId, $productIds)
{
if ($this->config->isEnabledBackend($storeId) === false) {
return parent::rebuildIndex($storeId, $productIds);
}

if (!$this->config->getApplicationID() || !$this->config->getAPIKey() || !$this->config->getSearchOnlyAPIKey()) {
return $this->reindexAlgolia($storeId, $productIds);
}

private function reindexAlgolia($storeId, $productIds)
{
if (!$this->config->getApplicationID($storeId) || !$this->config->getAPIKey($storeId) || !$this->config->getSearchOnlyAPIKey($storeId)) {
/** @var Mage_Adminhtml_Model_Session $session */
$session = Mage::getSingleton('adminhtml/session');
$session->addError('Algolia reindexing failed: You need to configure your Algolia credentials in System > Configuration > Algolia Search.');

return null;
return;
}

/* Avoid Indexing twice */
if (is_array($productIds) && $productIds > 0) {
return $this;
return;
}

$this->engine->rebuildProducts($storeId);

return $this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

class Algolia_Algoliasearch_Model_System_BackendRenderingDisplayMode
{
public function toOptionArray()
{
return array(
array('value' => 'all', 'label' => Mage::helper('algoliasearch')->__('All categories')),
array('value' => 'only_products', 'label' => Mage::helper('algoliasearch')->__('Categories without static blocks')),
);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class Algolia_Algoliasearch_Adminhtml_QueueController extends Mage_Adminhtml_Controller_Action
class Algolia_Algoliasearch_Adminhtml_AlgoliaQueueController extends Mage_Adminhtml_Controller_Action
{
public function _isAllowed()
{
Expand Down
6 changes: 4 additions & 2 deletions app/code/community/Algolia/Algoliasearch/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<config>
<modules>
<Algolia_Algoliasearch>
<version>1.12.0</version>
<version>1.13.0</version>
</Algolia_Algoliasearch>
</modules>
<frontend>
Expand Down Expand Up @@ -182,7 +182,7 @@
<index_prefix>magento_</index_prefix>
<is_popup_enabled>1</is_popup_enabled>
<is_instant_enabled>0</is_instant_enabled>
<use_image_adaptive>0</use_image_adaptive>
<use_adaptive_image>0</use_adaptive_image>
</credentials>
<products>
<number_product_results>9</number_product_results>
Expand All @@ -201,6 +201,7 @@
<max_values_per_facet>10</max_values_per_facet>
<sorts>a:3:{s:18:"_1432908018844_844";a:3:{s:9:"attribute";s:5:"price";s:4:"sort";s:3:"asc";s:5:"label";s:12:"Lowest price";}s:18:"_1432908022539_539";a:3:{s:9:"attribute";s:5:"price";s:4:"sort";s:4:"desc";s:5:"label";s:13:"Highest price";}s:18:"_1433768597454_454";a:3:{s:9:"attribute";s:10:"created_at";s:4:"sort";s:4:"desc";s:5:"label";s:12:"Newest first";}}</sorts>
<add_to_cart_enable>1</add_to_cart_enable>
<infinite_scroll_enable>0</infinite_scroll_enable>
</instant>
<autocomplete>
<nb_of_products_suggestions>6</nb_of_products_suggestions>
Expand Down Expand Up @@ -251,6 +252,7 @@
<index_product_on_category_products_update>1</index_product_on_category_products_update>
<index_all_category_product_on_category_update>0</index_all_category_product_on_category_update>
<prevent_backend_rendering>0</prevent_backend_rendering>
<prevent_backend_rendering_display_mode>all</prevent_backend_rendering_display_mode>
<backend_rendering_allowed_user_agents>Googlebot
Bingbot</backend_rendering_allowed_user_agents>
</advanced>
Expand Down
Loading

0 comments on commit bf5aa0d

Please sign in to comment.