-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathphalgoliaplaces.php
364 lines (311 loc) · 12 KB
/
phalgoliaplaces.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<?php
/**
* 2007-2015 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2015 PrestaShop SA
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
/**
* @author Krystian Podemski <krystian@prestahome.com>
* @copyright (c) 2016 Krystian Podemski - www.podemski.info / www.PrestaHome.com
* @license You only can use module, nothing more!
*/
if (!defined('_PS_VERSION_')) {
exit;
}
class PhAlgoliaPlaces extends Module
{
private $options_prefix;
private $fields_form = array();
private $hooksToInstall = array(
'displayHeader',
);
public function __construct()
{
$this->name = 'phalgoliaplaces';
$this->tab = 'front_office_features';
$this->version = '0.4.0';
$this->author = 'PrestaHome';
$this->need_instance = 0;
$this->is_configurable = 0;
$this->ps_versions_compliancy['min'] = '1.6';
$this->ps_versions_compliancy['max'] = _PS_VERSION_;
$this->secure_key = Tools::encrypt($this->name);
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('Integration with Algolia Places');
$this->description = $this->l('Algolia Places provides a fast, distributed and easy way to use address search autocomplete JavaScript library on your website. ');
$this->options_prefix = $this->name.'_';
}
public function install()
{
$this->_clearCache('*');
$this->renderConfigurationForm();
$this->batchUpdateConfigs();
// Hooks & Install
return (parent::install()
&& $this->prepareModuleSettings()
&& $this->registerHook($this->hooksToInstall));
}
public function prepareModuleSettings()
{
$sql = array();
$sql_file = _PS_MODULE_DIR_.$this->name.'/init/install_sql.php';
if (file_exists($sql_file)) {
include($sql_file);
foreach ($sql as $s) {
if (!Db::getInstance()->Execute($s)) {
die('Error while creating DB');
}
}
}
if (file_exists(_PS_MODULE_DIR_.$this->name.'/init/my-install.php')) {
include_once _PS_MODULE_DIR_.$this->name.'/init/my-install.php';
}
return true;
}
public function uninstall()
{
$this->renderConfigurationForm();
$this->deleteConfigs();
$this->_clearCache('*');
if (!parent::uninstall()) {
return false;
}
// Database
$sql = array();
$sql_file = _PS_MODULE_DIR_.$this->name.'/init/uninstall_sql.php';
if (file_exists($sql_file)) {
include($sql_file);
foreach ($sql as $s) {
if (!Db::getInstance()->Execute($s)) {
die('Error while creating DB');
}
}
}
if (file_exists(_PS_MODULE_DIR_.$this->name.'/init/my-uninstall.php')) {
include_once _PS_MODULE_DIR_.$this->name.'/init/my-uninstall.php';
}
return true;
}
public function getContent()
{
$this->context->controller->addjQueryPlugin(array(
'fancybox'
));
if (file_exists(_PS_MODULE_DIR_.$this->name.'/views/js/admin.js')) {
$this->context->controller->addJS(array(
_MODULE_DIR_.$this->name.'/views/js/admin.js',
));
}
if (file_exists(_PS_MODULE_DIR_.$this->name.'/views/css/admin.css')) {
$this->context->controller->addCSS(array(
_MODULE_DIR_.$this->name.'/views/css/admin.css',
));
}
$this->_html = '<h2>'.$this->displayName.'</h2>';
if (Tools::isSubmit('save'.$this->name)) {
$this->renderConfigurationForm();
$this->batchUpdateConfigs();
$this->_clearCache('*');
$this->_html .= $this->displayConfirmation($this->l('Settings updated successfully.'));
}
return $this->_html . $this->renderForm();
}
protected function renderForm()
{
$default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
$this->renderConfigurationForm();
$helper = new HelperForm();
$helper->module = $this;
$helper->name_controller = $this->name;
$helper->identifier = $this->identifier;
$helper->token = Tools::getAdminTokenLite('AdminModules');
foreach (Language::getLanguages(false) as $lang) {
$helper->languages[] = array(
'id_lang' => $lang['id_lang'],
'iso_code' => $lang['iso_code'],
'name' => $lang['name'],
'is_default' => ($default_lang == $lang['id_lang'] ? 1 : 0)
);
}
$helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
$helper->default_form_language = $default_lang;
$helper->allow_employee_form_lang = $default_lang;
$helper->toolbar_scroll = true;
$helper->title = $this->displayName;
$helper->submit_action = 'save'.$this->name;
$helper->toolbar_btn = array(
'save' =>
array(
'desc' => $this->l('Save'),
'href' => AdminController::$currentIndex.'&configure='.$this->name.'&save'.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules'),
)
);
$helper->tpl_vars = array(
'fields_value' => $this->getConfigFieldsValues(),
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id,
'link' => $this->context->link,
);
return $helper->generateForm($this->fields_form);
}
public function renderConfigurationForm()
{
if ($this->fields_form) {
return;
}
$fields_form = array(
'form' => array(
'legend' => array(
'title' => $this->l('Settings'),
'icon' => 'icon-cogs'
),
'input' => array(
array(
'type' => 'text',
'label' => $this->l('App Id:'),
'name' => $this->options_prefix.'appid',
'default' => '',
'lang' => false,
),
array(
'type' => 'text',
'label' => $this->l('API key:'),
'name' => $this->options_prefix.'apikey',
'default' => '',
'lang' => false,
),
),
'submit' => array(
'title' => $this->l('Save'),
'class' => 'btn btn-default'
)
),
);
$this->fields_form[] = $fields_form;
}
public function getConfigFieldsValues()
{
$id_shop = Shop::getContextShopID(true);
$id_shop_group = Shop::getContextShopGroupID(true);
$fields_values = array();
foreach ($this->fields_form as $k => $f) {
foreach ($f['form']['input'] as $i => $input) {
if (isset($input['ignore']) && $input['ignore'] == true) {
continue;
}
if (isset($input['lang']) && $input['lang'] == true) {
foreach (Language::getLanguages(false) as $lang) {
$values = Tools::getValue($input['name'].'_'.$lang['id_lang'], (Configuration::hasKey($input['name'], $lang['id_lang']) ? Configuration::get($input['name'], $lang['id_lang'], (int)$id_shop_group, (int)$id_shop) : $input['default']));
$fields_values[$input['name']][$lang['id_lang']] = $values;
}
} else {
$values = Tools::getValue($input['name'], (Configuration::hasKey($input['name'], null, (int)$id_shop_group, (int)$id_shop) ? Configuration::get($input['name']) : $input['default']));
$fields_values[$input['name']] = $values;
}
}
}
$this->assignCustomConfigs($fields_values);
return $fields_values;
}
public function batchUpdateConfigs()
{
foreach ($this->fields_form as $k => $f) {
foreach ($f['form']['input'] as $i => $input) {
if (isset($input['ignore']) && $input['ignore'] == true) {
continue;
}
if (isset($input['lang']) && $input['lang'] == true) {
$data = array();
foreach (Language::getLanguages(false) as $lang) {
$val = Tools::getValue($input['name'].'_'.$lang['id_lang'], $input['default']);
$data[$lang['id_lang']] = $val;
}
Configuration::updateValue(trim($input['name']), $data, true);
} else {
$val = Tools::getValue($input['name'], $input['default']);
Configuration::updateValue($input['name'], $val, true);
}
}
}
$this->batchUpdateCustomConfigs();
}
public function deleteConfigs()
{
foreach ($this->fields_form as $k => $f) {
foreach ($f['form']['input'] as $i => $input) {
if (isset($input['ignore']) && $input['ignore'] == true) {
continue;
}
Configuration::deleteByName($input['name']);
}
}
$this->deleteCustomConfigs();
return true;
}
public function assignCustomConfigs(&$fields_values)
{
return array();
}
public function batchUpdateCustomConfigs()
{
return true;
}
public function deleteCustomConfigs()
{
return true;
}
/**
* Return value with every available language
* @param string Value
* @return array
*/
public static function prepareValueForLangs($value)
{
$output = array();
foreach (Language::getLanguages(false) as $lang) {
$output[$lang['id_lang']] = $value;
}
return $output;
}
public function hookDisplayHeader($params)
{
$pages = array('authentication', 'address', 'order-opc');
if (isset($this->context->controller->php_self) && in_array($this->context->controller->php_self, $pages)) {
Media::addJsDef(
array(
$this->name.'_appId' => Configuration::get($this->options_prefix.'appid'),
$this->name.'_apiKey' => Configuration::get($this->options_prefix.'appkey'),
$this->name.'_isoLang' => $this->context->language->iso_code,
)
);
$this->context->controller->addCSS(array(
_PS_MODULE_DIR_.$this->name.'/views/css/front.css',
));
$this->context->controller->addJS(array(
'https://cdn.jsdelivr.net/algoliasearch/3/algoliasearch.min.js',
'https://cdn.jsdelivr.net/places.js/1/places.min.js',
_PS_MODULE_DIR_.$this->name.'/views/js/front.js',
));
}
}
}