Skip to content

Commit 3ec8829

Browse files
authored
Add facet query rules management + dashboard warnings (#844)
* Add custom logic on Facet QR management * Add dashboard confusion warnings
1 parent ef6d2cb commit 3ec8829

File tree

8 files changed

+204
-5
lines changed

8 files changed

+204
-5
lines changed

Helper/Image.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ protected function initBaseFile()
7373
$model->setBaseFile($this->getProductImage());
7474
}
7575
}
76+
7677
return $this;
7778
}
7879

Model/Backend/Facets.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Model\Backend;
4+
5+
use Magento\Config\Model\Config\Backend\Serialized;
6+
7+
/**
8+
* Algolia custom facet backend model
9+
*/
10+
class Facets extends Serialized
11+
{
12+
public function beforeSave()
13+
{
14+
$values = $this->getValue();
15+
if (is_array($values)) {
16+
unset($values['__empty']);
17+
}
18+
19+
// Adding query rule config (set to "no") in case the select doesn't appear in the form
20+
foreach ($values as &$facet) {
21+
if (!isset($facet['create_rule'])) {
22+
$facet['create_rule'] = '2';
23+
}
24+
}
25+
26+
$this->setValue($values);
27+
28+
return parent::beforeSave();
29+
}
30+
}

Model/Source/Facets.php

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,34 @@
22

33
namespace Algolia\AlgoliaSearch\Model\Source;
44

5+
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
6+
use Algolia\AlgoliaSearch\Helper\Entity\CategoryHelper;
7+
use Algolia\AlgoliaSearch\Helper\Entity\ProductHelper;
8+
use Algolia\AlgoliaSearch\Helper\ProxyHelper;
9+
use Magento\Backend\Block\Template\Context;
10+
511
class Facets extends AbstractTable
612
{
13+
protected $proxyHelper;
14+
15+
public function __construct(
16+
Context $context,
17+
ProductHelper $producthelper,
18+
CategoryHelper $categoryHelper,
19+
ConfigHelper $configHelper,
20+
ProxyHelper $proxyHelper,
21+
array $data = []
22+
) {
23+
$this->proxyHelper = $proxyHelper;
24+
25+
parent::__construct($context, $producthelper, $categoryHelper, $configHelper, $data);
26+
}
27+
728
protected function getTableData()
829
{
930
$productHelper = $this->productHelper;
1031

11-
return [
32+
$config = [
1233
'attribute' => [
1334
'label' => 'Attribute',
1435
'values' => function () use ($productHelper) {
@@ -39,10 +60,28 @@ protected function getTableData()
3960
'label' => 'Searchable?',
4061
'values' => ['1' => 'Yes', '2' => 'No'],
4162
],
42-
'create_rule' => [
63+
];
64+
65+
if ($this->isQueryRulesEnabled()) {
66+
$config['create_rule'] = [
4367
'label' => 'Create Query rule?',
4468
'values' => ['1' => 'Yes', '2' => 'No'],
45-
],
46-
];
69+
];
70+
}
71+
72+
return $config;
73+
}
74+
75+
private function isQueryRulesEnabled()
76+
{
77+
$info = $this->proxyHelper->getInfo(\Algolia\AlgoliaSearch\Helper\ProxyHelper::INFO_TYPE_QUERY_RULES);
78+
79+
// In case the call to API proxy fails,
80+
// be "nice" and return true
81+
if ($info && array_key_exists('query_rules', $info)) {
82+
return $info['query_rules'];
83+
}
84+
85+
return true;
4786
}
4887
}

etc/adminhtml/system.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@
285285
<field id="facets" translate="label comment" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
286286
<label>Facets</label>
287287
<frontend_model>Algolia\AlgoliaSearch\Model\Source\Facets</frontend_model>
288-
<backend_model>Magento\Config\Model\Config\Backend\Serialized\ArraySerialized</backend_model>
288+
<backend_model>Algolia\AlgoliaSearch\Model\Backend\Facets</backend_model>
289289
<comment>
290290
<![CDATA[
291291
Specify the filtering options you want to offer on a search results page.<br>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0"?>
2+
3+
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
4+
<head>
5+
<link src="Algolia_AlgoliaSearch::js/config.js"/>
6+
</head>
7+
</page>

view/adminhtml/web/css/common.css

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,3 +418,18 @@
418418
.algolia-clearfix {
419419
clear: both;
420420
}
421+
422+
/* Warning blocks on configuration */
423+
.algolia_dashboard_warning {
424+
background-image: url("../images/icon-magento-settings.svg");
425+
background-repeat: no-repeat;
426+
background-position: 25px 20px;
427+
background-color : #FDFAF5;
428+
border : 1px solid #F8E5C4;
429+
padding : 20px 20px 10px 90px;
430+
margin-bottom : 20px;
431+
}
432+
433+
.algolia_dashboard_warning_page {
434+
margin-bottom : 0;
435+
}
Lines changed: 19 additions & 0 deletions
Loading

view/adminhtml/web/js/config.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
require(
2+
[
3+
'jquery',
4+
'mage/translate',
5+
],
6+
function ($) {
7+
8+
addDashboardWarnings();
9+
10+
function addDashboardWarnings() {
11+
// rows
12+
var rowIds = [
13+
'#row_algoliasearch_instant_instant_facets',
14+
'#row_algoliasearch_instant_instant_max_values_per_facet'
15+
];
16+
17+
var rowWarning = '<div class="algolia_dashboard_warning">';
18+
rowWarning += '<p>This setting is also available in the Algolia Dashboard. We advise you to manage it from this page, because saving Magento settings will override the Algolia settings.</p>';
19+
rowWarning += '</div>';
20+
21+
for (var i=0; i < rowIds.length; i++) {
22+
var element = $(rowIds[i]);
23+
if (element.length > 0) {
24+
element.find('.value').prepend(rowWarning);
25+
}
26+
}
27+
28+
// pages
29+
var pageIds = [
30+
'#algoliasearch_products_products',
31+
'#algoliasearch_categories_categories',
32+
'#algoliasearch_synonyms_synonyms_group',
33+
'#algoliasearch_extra_settings_extra_settings'
34+
];
35+
36+
var pageWarning = '<div class="algolia_dashboard_warning algolia_dashboard_warning_page">';
37+
pageWarning += '<p>These settings are also available in the Algolia Dashboard. We advise you to manage it from this page, because saving Magento settings will override the Algolia settings.</p>';
38+
pageWarning += '</div>';
39+
40+
for (var i=0; i < pageIds.length; i++) {
41+
var element = $(pageIds[i]);
42+
if (element.length > 0) {
43+
element.find('.comment').append(pageWarning);
44+
}
45+
}
46+
}
47+
48+
if ($('#algoliasearch_instant_instant_facets').length > 0) {
49+
var addButton = $('#algoliasearch_instant_instant_facets tfoot .action-add');
50+
addButton.on('click', function(){
51+
handleFacetQueryRules();
52+
});
53+
54+
handleFacetQueryRules();
55+
}
56+
57+
function handleFacetQueryRules() {
58+
var facets = $('#algoliasearch_instant_instant_facets tbody tr');
59+
60+
for (var i=0; i < facets.length; i++) {
61+
var rowId = $(facets[i]).attr('id');
62+
var searchableSelect = $('select[name="groups[instant][fields][facets][value][' + rowId + '][searchable]"]');
63+
64+
searchableSelect.on('change', function(){
65+
configQrFromSearchableSelect($(this));
66+
});
67+
68+
configQrFromSearchableSelect(searchableSelect);
69+
}
70+
}
71+
72+
function configQrFromSearchableSelect(searchableSelect) {
73+
var rowId = searchableSelect.parent().parent().attr('id');
74+
var qrSelect = $('select[name="groups[instant][fields][facets][value][' + rowId + '][create_rule]"]');
75+
if (qrSelect.length > 0) {
76+
if (searchableSelect.val() == "2") {
77+
qrSelect.val('2');
78+
qrSelect.attr('disabled','disabled');
79+
} else {
80+
qrSelect.removeAttr('disabled');
81+
}
82+
} else {
83+
$('#row_algoliasearch_instant_instant_facets .algolia_block').hide();
84+
}
85+
}
86+
87+
}
88+
);

0 commit comments

Comments
 (0)