Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if the update on save is apt #233

Merged
81 changes: 57 additions & 24 deletions doofinder.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* NOTICE OF LICENSE
*
Expand Down Expand Up @@ -32,15 +33,15 @@ class Doofinder extends Module
const DOOMANAGER_URL = 'https://admin.doofinder.com';
const GS_SHORT_DESCRIPTION = 1;
const GS_LONG_DESCRIPTION = 2;
const VERSION = '4.8.6';
const VERSION = '4.8.7';
const YES = 1;
const NO = 0;

public function __construct()
{
$this->name = 'doofinder';
$this->tab = 'search_filter';
$this->version = '4.8.6';
$this->version = '4.8.7';
$this->author = 'Doofinder (http://www.doofinder.com)';
$this->ps_versions_compliancy = ['min' => '1.5', 'max' => _PS_VERSION_];
$this->module_key = 'd1504fe6432199c7f56829be4bd16347';
Expand Down Expand Up @@ -373,13 +374,15 @@ protected function getConfigFormSearchLayer()
'name' => 'DF_SHOW_LAYER',
'is_bool' => true,
'values' => $this->getBooleanFormValue(),
], [
],
[
'type' => (version_compare(_PS_VERSION_, '1.6.0', '>=') ? 'switch' : 'radio'),
'label' => $this->l('Doofinder search layer in mobile version'),
'name' => 'DF_SHOW_LAYER_MOBILE',
'is_bool' => true,
'values' => $this->getBooleanFormValue(),
], [
],
[
'type' => 'text',
'label' => $this->l('Doofinder Store ID'),
'name' => 'DF_INSTALLATION_ID',
Expand Down Expand Up @@ -451,7 +454,8 @@ protected function renderFormDataFeed($adv = false)
];

if (!$this->showNewShopForm(Context::getContext()->shop)) {
$html .= $helper->generateForm([$this->getConfigFormDataFeed()]);
$apt_update_on_save = $this->apt_update_on_save();
$html .= $helper->generateForm([$this->getConfigFormDataFeed($apt_update_on_save)]);
// Search layer form
$helper->tpl_vars['fields_value'] = $this->getConfigFormValuesSearchLayer();
$html .= $helper->generateForm([$this->getConfigFormSearchLayer()]);
Expand All @@ -468,8 +472,28 @@ protected function renderFormDataFeed($adv = false)
*
* @return array
*/
protected function getConfigFormDataFeed()
{
protected function getConfigFormDataFeed($apt_update_on_save = false)
{
if ($apt_update_on_save) {
$disabled = false;
$query = [
5 => ['id' => 5, 'name' => sprintf($this->l('Each %s minutes'), '5')],
15 => ['id' => 15, 'name' => sprintf($this->l('Each %s minutes'), '15')],
30 => ['id' => 30, 'name' => sprintf($this->l('Each %s minutes'), '30')],
60 => ['id' => 60, 'name' => $this->l('Each hour')],
120 => ['id' => 120, 'name' => sprintf($this->l('Each %s hours'), '2')],
360 => ['id' => 360, 'name' => sprintf($this->l('Each %s hours'), '6')],
720 => ['id' => 720, 'name' => sprintf($this->l('Each %s hours'), '12')],
1440 => ['id' => 1440, 'name' => $this->l('Once a day')],
0 => ['id' => 0, 'name' => $this->l('Disabled')],
];
} else {
$disabled = true;
$query = [
0 => ['id' => 0, 'name' => $this->l('Disabled')],
];
}

return [
'form' => [
'legend' => [
Expand Down Expand Up @@ -551,20 +575,11 @@ protected function getConfigFormDataFeed()
[
'type' => 'select',
'label' => $this->l('Automatically process modified products'),
'desc' => $this->l('Configure how often changes will be sent to Doofinder. It will only be executed if there are changes.'),
'desc' => $this->l('This action will only be executed if there are changes. If you see the field disabled, it is because you are making a usage in the indexes that is not supported by the automatic processing of modified products.'),
'name' => 'DF_UPDATE_ON_SAVE_DELAY',
'disabled' => $disabled,
'options' => [
'query' => [
5 => ['id' => 5, 'name' => sprintf($this->l('Each %s minutes'), '5')],
15 => ['id' => 15, 'name' => sprintf($this->l('Each %s minutes'), '15')],
30 => ['id' => 30, 'name' => sprintf($this->l('Each %s minutes'), '30')],
60 => ['id' => 60, 'name' => $this->l('Each hour')],
120 => ['id' => 120, 'name' => sprintf($this->l('Each %s hours'), '2')],
360 => ['id' => 360, 'name' => sprintf($this->l('Each %s hours'), '6')],
720 => ['id' => 720, 'name' => sprintf($this->l('Each %s hours'), '12')],
1440 => ['id' => 1440, 'name' => $this->l('Once a day')],
0 => ['id' => 0, 'name' => $this->l('Disabled')],
],
'query' => $query,
'id' => 'id',
'name' => 'name',
],
Expand Down Expand Up @@ -1280,6 +1295,23 @@ private function indexApiInvokeReindexing()
Configuration::updateValue('DF_FEED_INDEXED', false);
}

private function apt_update_on_save()
{
require_once 'lib/doofinder_api_search_engine.php';

$region = Configuration::get('DF_REGION');
$api_key = Configuration::get('DF_API_KEY');
$api = new DoofinderApiSearchEngine($api_key, $region);
$response = $api->apt_update_on_save(Configuration::get('DF_INSTALLATION_ID'));
if (empty($response) || $response['status'] !== 200) {
$this->debug('Error checking search engines: ' . json_encode($response));

return false;
}

return @$response['apt?'];
}

/**
* Perform an API connection test
*
Expand Down Expand Up @@ -1389,7 +1421,8 @@ public function searchOnApi(
'timeout' => $timeout,
'types' => [
'product',
], 'transformer' => 'basic',
],
'transformer' => 'basic',
];
if ($query_name) {
$queryParams['query_name'] = $query_name;
Expand Down Expand Up @@ -1421,7 +1454,7 @@ public function searchOnApi(
}
$id_product = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(
'SELECT id_product FROM ' . _DB_PREFIX_ . 'product_attribute'
. ' WHERE id_product_attribute = ' . (int) pSQL($id_product_attribute)
. ' WHERE id_product_attribute = ' . (int) pSQL($id_product_attribute)
);
$product_pool_ids[] = ((!empty($id_product)) ? (int) pSQL($id_product) : 0);
}
Expand Down Expand Up @@ -1467,10 +1500,10 @@ public function searchOnApi(
DATE_SUB(
NOW(),
INTERVAL ' . (Validate::isUnsignedInt(Configuration::get('PS_NB_DAYS_NEW_PRODUCT'))
? Configuration::get('PS_NB_DAYS_NEW_PRODUCT') : 20) . ' DAY
? Configuration::get('PS_NB_DAYS_NEW_PRODUCT') : 20) . ' DAY
)
) > 0 new' . (Combination::isFeatureActive() ?
', MAX(product_attribute_shop.minimal_quantity) AS product_attribute_minimal_quantity' : '') . '
', MAX(product_attribute_shop.minimal_quantity) AS product_attribute_minimal_quantity' : '') . '
FROM ' . _DB_PREFIX_ . 'product p
' . Shop::addSqlAssociation('product', 'p') . '
INNER JOIN `' . _DB_PREFIX_ . 'product_lang` pl ON (
Expand All @@ -1479,7 +1512,7 @@ public function searchOnApi(
. (Combination::isFeatureActive() ? ' LEFT JOIN `' . _DB_PREFIX_ . 'product_attribute` pa
ON (p.`id_product` = pa.`id_product`)
' . Shop::addSqlAssociation('product_attribute', 'pa', false, ($show_variations) ? '' :
' product_attribute_shop.default_on = 1') . '
' product_attribute_shop.default_on = 1') . '
' . Product::sqlStock('p', 'product_attribute_shop', false, $context->shop) :
Product::sqlStock('p', 'product', false, Context::getContext()->shop)) . '
LEFT JOIN `' . _DB_PREFIX_ . 'manufacturer` m ON m.`id_manufacturer` = p.`id_manufacturer`
Expand Down
62 changes: 62 additions & 0 deletions lib/doofinder_api_search_engine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* NOTICE OF LICENSE
*
* This file is licenced under the Software License Agreement.
* With the purchase or the installation of the software in your application
* you accept the licence agreement.
*
* You must not modify, adapt or create derivative works of this source code
*
* @author Doofinder
* @copyright Doofinder
* @license GPLv3
*/
require_once _PS_MODULE_DIR_ . 'doofinder/lib/EasyREST.php';

if (!defined('_PS_VERSION_')) {
exit;
}

const API_URL = 'https://{region}-plugins.doofinder.com';

class DoofinderApiSearchEngine
eduardogomez97 marked this conversation as resolved.
Show resolved Hide resolved
{
private $api_key;
private $api_url;

public function __construct($api_key, $region)
{
$this->api_key = $api_key;
$this->api_url = str_replace('{region}', $region, API_URL);
}

/**
* Make a request to the plugins API to to check that the update on save is apt
*
* @param string $installation_id
* @param string $callback_url
*/
public function apt_update_on_save($installation_id)
eduardogomez97 marked this conversation as resolved.
Show resolved Hide resolved
{
$api_endpoint = $this->api_url . '/' . $installation_id . '/apt-update-on-save';

return $this->get($api_endpoint);
eduardogomez97 marked this conversation as resolved.
Show resolved Hide resolved
}

private function get($url)
{
$client = new EasyREST();

$response = $client->get(
$url,
null,
false,
false,
'application/json',
['Authorization: Token ' . $this->api_key]
);

return json_decode($response->response, true);
}
}
2 changes: 1 addition & 1 deletion translations/en.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
$_MODULE['<{doofinder}prestashop>doofinder_ebe75aa799359b32559a09eae8594835'] = 'Select features will be shown in feed';
$_MODULE['<{doofinder}prestashop>doofinder_90dce1b992114b5c7ab37ee99ea195ec'] = 'Product Image Size';
$_MODULE['<{doofinder}prestashop>doofinder_ae29d32626c4da07fe58def585bbc283'] = 'Automatically process modified products';
$_MODULE['<{doofinder}prestashop>doofinder_c820ca1511d196b41e53253c03ab742f'] = 'Configure how often changes will be sent to Doofinder. It will only be executed if there are changes.';
$_MODULE['<{doofinder}prestashop>doofinder_c820ca1511d196b41e53253c03ab742f'] = 'This action will only be executed if there are changes. If you see the field disabled, it is because you are making a usage in the indexes that is not supported by the automatic processing of modified products.';
$_MODULE['<{doofinder}prestashop>doofinder_9150cc930978b400710ef1c218af6478'] = 'Each %s minutes';
$_MODULE['<{doofinder}prestashop>doofinder_f826cc3c38088c39b0a05cf87435e08f'] = 'Each hour';
$_MODULE['<{doofinder}prestashop>doofinder_c5638b8b3adb3469c89b8fc43040a55e'] = 'Each %s hours';
Expand Down
2 changes: 1 addition & 1 deletion translations/es.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
$_MODULE['<{doofinder}prestashop>doofinder_ebe75aa799359b32559a09eae8594835'] = 'Seleccionar las características que serán mostradas en el feed';
$_MODULE['<{doofinder}prestashop>doofinder_90dce1b992114b5c7ab37ee99ea195ec'] = 'Tamaño de la imagen de producto';
$_MODULE['<{doofinder}prestashop>doofinder_ae29d32626c4da07fe58def585bbc283'] = 'Procesar automáticamente los productos modificados';
$_MODULE['<{doofinder}prestashop>doofinder_c820ca1511d196b41e53253c03ab742f'] = 'Configura con qué frecuencia se enviarán cambios a Doofinder. Solo se ejecutará si hay cambios.';
$_MODULE['<{doofinder}prestashop>doofinder_c820ca1511d196b41e53253c03ab742f'] = 'Esta acción solo se ejecutará si hay cambios. Si le aparece el campo deshabilitado, es debido a que está haciendo un uso en los índices que no está soportado por el procesamiento automático de productos modificados.';
$_MODULE['<{doofinder}prestashop>doofinder_9150cc930978b400710ef1c218af6478'] = 'Cada %s minutos';
$_MODULE['<{doofinder}prestashop>doofinder_f826cc3c38088c39b0a05cf87435e08f'] = 'Cada hora';
$_MODULE['<{doofinder}prestashop>doofinder_c5638b8b3adb3469c89b8fc43040a55e'] = 'Cada %s horas';
Expand Down
2 changes: 1 addition & 1 deletion translations/fr.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
$_MODULE['<{doofinder}prestashop>doofinder_ebe75aa799359b32559a09eae8594835'] = 'Sélectionner les caractéristiques à afficher dans le flux';
$_MODULE['<{doofinder}prestashop>doofinder_90dce1b992114b5c7ab37ee99ea195ec'] = 'Taille de l’image du produit';
$_MODULE['<{doofinder}prestashop>doofinder_ae29d32626c4da07fe58def585bbc283'] = 'Traiter automatiquement les produits modifiés';
$_MODULE['<{doofinder}prestashop>doofinder_c820ca1511d196b41e53253c03ab742f'] = 'Configurer la fréquence à laquelle les modifications seront envoyées à Doofinder. Cela ne sera exécuté que s’il y a des changements';
$_MODULE['<{doofinder}prestashop>doofinder_c820ca1511d196b41e53253c03ab742f'] = 'Cette action ne sera exécutée que s’il y a des changements. Si vous voyez le champ désactivé, c’est que vous faites une utilisation dans les index qui n’est pas prise en charge par le traitement automatique des produits modifiés.';
$_MODULE['<{doofinder}prestashop>doofinder_9150cc930978b400710ef1c218af6478'] = 'Toutes les %s minutes';
$_MODULE['<{doofinder}prestashop>doofinder_f826cc3c38088c39b0a05cf87435e08f'] = 'Toutes les heures';
$_MODULE['<{doofinder}prestashop>doofinder_c5638b8b3adb3469c89b8fc43040a55e'] = 'Toutes les %s heures';
Expand Down
Loading