|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Copyright (C) 2017 Oscss-Shop <[email protected]>. |
|
4
|
|
|
* |
|
5
|
|
|
* This program is free software; you can redistribute it and/or modifyion 2.0 (the "License"); |
|
6
|
|
|
* it under the terms of the GNU General Public License as published bypliance with the License. |
|
7
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
|
8
|
|
|
* |
|
9
|
|
|
* This program is distributed in the hope that it will be useful, |
|
10
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12
|
|
|
* GNU General Public License for more details. |
|
13
|
|
|
* |
|
14
|
|
|
* You should have received a copy of the GNU General Public License |
|
15
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16
|
|
|
* or see http://www.gnu.org/ |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
include_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php'; |
|
20
|
|
|
include_once DOL_DOCUMENT_ROOT.'/admin/dolistore/class/PSWebServiceLibrary.class.php'; |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class Dolistore |
|
25
|
|
|
*/ |
|
26
|
|
|
class Dolistore |
|
27
|
|
|
{ |
|
28
|
|
|
// params |
|
29
|
|
|
public $start; // beginning of pagination |
|
30
|
|
|
public $end; // end of pagination |
|
31
|
|
|
public $per_page; // pagination: display per page |
|
32
|
|
|
public $categorie; // the current categorie |
|
33
|
|
|
public $search; // the search keywords |
|
34
|
|
|
|
|
35
|
|
|
// setups |
|
36
|
|
|
public $url; // the url of this page |
|
37
|
|
|
public $shop_url; // the url of the shop |
|
38
|
|
|
public $vat_rate; // the vat rate used in the shop (prices are provided without vat) |
|
39
|
|
|
public $lang; // the integer representing the lang in the store |
|
40
|
|
|
public $debug_api; // usefull if no dialog |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Constructor |
|
45
|
|
|
* |
|
46
|
|
|
* @param boolean $debug Enable debug of request on screen |
|
47
|
|
|
*/ |
|
48
|
|
|
function __construct($debug=false) |
|
49
|
|
|
{ |
|
50
|
|
|
global $conf, $langs; |
|
51
|
|
|
|
|
52
|
|
|
$this->url = DOL_URL_ROOT.'/admin/modules.php?mode=marketplace'; |
|
53
|
|
|
$this->shop_url = 'https://www.dolistore.com/index.php?controller=product&id_product='; |
|
54
|
|
|
$this->vat_rate = 1.2; // 20% de TVA |
|
55
|
|
|
$this->debug_api = $debug; |
|
56
|
|
|
|
|
57
|
|
|
$langtmp = explode('_', $langs->defaultlang); |
|
58
|
|
|
$lang = $langtmp[0]; |
|
59
|
|
|
$lang_array = array('en'=>1, 'fr'=>2, 'es'=>3, 'it'=>4, 'de'=>5); // Into table ps_lang of Prestashop - 1 |
|
60
|
|
|
if (! in_array($lang, array_keys($lang_array))) $lang = 'en'; |
|
61
|
|
|
$this->lang = $lang_array[$lang]; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Load data from remote Dolistore market place. |
|
66
|
|
|
* This fills ->categories |
|
67
|
|
|
* |
|
68
|
|
|
* @param array $options Options |
|
69
|
|
|
* @return void |
|
70
|
|
|
*/ |
|
71
|
|
|
function getRemoteData($options = array('start' => 0, 'end' => 10, 'per_page' => 50, 'categorie' => 0)) |
|
72
|
|
|
{ |
|
73
|
|
|
global $conf, $langs; |
|
74
|
|
|
|
|
75
|
|
|
$this->start = $options['start']; |
|
76
|
|
|
$this->end = $options['end']; |
|
77
|
|
|
$this->per_page = $options['per_page']; |
|
78
|
|
|
$this->categorie = $options['categorie']; |
|
79
|
|
|
$this->search = $options['search']; |
|
80
|
|
|
|
|
81
|
|
|
if ($this->end == 0) { |
|
82
|
|
|
$this->end = $this->per_page; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
try { |
|
86
|
|
|
$this->api = new PrestaShopWebservice($conf->global->MAIN_MODULE_DOLISTORE_API_SRV, $conf->global->MAIN_MODULE_DOLISTORE_API_KEY, $this->debug_api); |
|
87
|
|
|
dol_syslog("Call API with MAIN_MODULE_DOLISTORE_API_SRV = ".$conf->global->MAIN_MODULE_DOLISTORE_API_SRV); |
|
88
|
|
|
// $conf->global->MAIN_MODULE_DOLISTORE_API_KEY is for the login of basic auth. There is no password as it is public data. |
|
89
|
|
|
|
|
90
|
|
|
// Here we set the option array for the Webservice : we want products resources |
|
91
|
|
|
$opt = array(); |
|
92
|
|
|
$opt['resource'] = 'products'; |
|
93
|
|
|
|
|
94
|
|
|
// make a search to limit the id returned. |
|
95
|
|
|
if ($this->search != '') { |
|
96
|
|
|
$opt2 = array(); |
|
97
|
|
|
$opt2['url'] = $conf->global->MAIN_MODULE_DOLISTORE_API_SRV.'/api/search?query='.$this->search.'&language='.$this->lang; |
|
98
|
|
|
|
|
99
|
|
|
// Call |
|
100
|
|
|
//var_dump($this->api); |
|
101
|
|
|
|
|
102
|
|
|
dol_syslog("Call API with opt = ".var_export($opt, true)); |
|
103
|
|
|
dol_syslog("Call API with opt2 = ".var_export($opt2, true)); |
|
104
|
|
|
|
|
105
|
|
|
$xml = $this->api->get($opt2); |
|
106
|
|
|
$products = array(); |
|
107
|
|
|
foreach ($xml->products->children() as $product) { |
|
108
|
|
|
$products[] = (int) $product['id']; |
|
109
|
|
|
} |
|
110
|
|
|
$opt['filter[id]'] = '['.implode('|', $products).']'; |
|
111
|
|
|
} elseif ($this->categorie != 0) { |
|
112
|
|
|
$opt2 = array(); |
|
113
|
|
|
$opt2['resource'] = 'categories'; |
|
114
|
|
|
$opt2['id'] = $this->categorie; |
|
115
|
|
|
// Call |
|
116
|
|
|
$xml = $this->api->get($opt2); |
|
117
|
|
|
$products = array(); |
|
118
|
|
|
foreach ($xml->category->associations->products->children() as $product) { |
|
119
|
|
|
$products[] = (int) $product->id; |
|
120
|
|
|
} |
|
121
|
|
|
$opt['filter[id]'] = '['.implode('|', $products).']'; |
|
122
|
|
|
} |
|
123
|
|
|
$opt['display'] = '[id,name,id_default_image,id_category_default,reference,price,condition,show_price,date_add,date_upd,description_short,description,module_version,dolibarr_min,dolibarr_max]'; |
|
124
|
|
|
$opt['sort'] = 'id_desc'; |
|
125
|
|
|
$opt['filter[active]'] = '[1]'; |
|
126
|
|
|
$opt['limit'] = "$this->start,$this->end"; |
|
127
|
|
|
// $opt['filter[id]'] contais list of product id that are result of search |
|
128
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
// Call API to get the detail |
|
131
|
|
|
dol_syslog("Call API with opt = ".var_export($opt, true)); |
|
132
|
|
|
dol_syslog("Call API with opt2 = ".var_export($opt2, true)); |
|
|
|
|
|
|
133
|
|
|
$xml = $this->api->get($opt); |
|
134
|
|
|
$this->products = $xml->products->children(); |
|
135
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
// Here we set the option array for the Webservice : we want categories resources |
|
138
|
|
|
$opt = array(); |
|
139
|
|
|
$opt['resource'] = 'categories'; |
|
140
|
|
|
$opt['display'] = '[id,id_parent,nb_products_recursive,active,is_root_category,name,description]'; |
|
141
|
|
|
$opt['sort'] = 'id_asc'; |
|
142
|
|
|
// Call |
|
143
|
|
|
$xml = $this->api->get($opt); |
|
144
|
|
|
$this->categories = $xml->categories->children(); |
|
145
|
|
|
} catch (PrestaShopWebserviceException $e) { |
|
146
|
|
|
// Here we are dealing with errors |
|
147
|
|
|
$trace = $e->getTrace(); |
|
148
|
|
|
if ($trace[0]['args'][0] == 404) die('Bad ID'); |
|
149
|
|
|
else if ($trace[0]['args'][0] == 401) die('Bad auth key'); |
|
150
|
|
|
else |
|
151
|
|
|
{ |
|
152
|
|
|
print 'Can not access to '.$conf->global->MAIN_MODULE_DOLISTORE_API_SRV.'<br>'; |
|
153
|
|
|
print $e->getMessage(); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Return tree of Dolistore categories. $this->categories must have been loaded before. |
|
160
|
|
|
* |
|
161
|
|
|
* @param int $parent Id of parent category |
|
162
|
|
|
* @return string |
|
163
|
|
|
*/ |
|
164
|
|
|
function get_categories($parent = 0) |
|
165
|
|
|
{ |
|
166
|
|
|
if (!isset($this->categories)) die('not possible'); |
|
167
|
|
|
if ($parent != 0) { |
|
168
|
|
|
$html = '<ul>'; |
|
169
|
|
|
} else { |
|
170
|
|
|
$html = ''; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
$nbofcateg = count($this->categories); |
|
174
|
|
|
for ($i = 0; $i < $nbofcateg; $i++) |
|
175
|
|
|
{ |
|
176
|
|
|
$cat = $this->categories[$i]; |
|
177
|
|
|
if ($cat->is_root_category == 1 && $parent == 0) { |
|
178
|
|
|
$html .= '<li class="root"><h3 class="nomargesupinf"><a class="nomargesupinf link2cat" href="?mode=marketplace&categorie='.$cat->id.'" ' |
|
179
|
|
|
.'title="'.dol_escape_htmltag(strip_tags($cat->description->language[$this->lang])).'"' |
|
180
|
|
|
.'>'.$cat->name->language[$this->lang].' <sup>'.$cat->nb_products_recursive.'</sup></a></h3>'; |
|
181
|
|
|
$html .= self::get_categories($cat->id); |
|
182
|
|
|
$html .= "</li>\n"; |
|
183
|
|
|
} elseif (trim($cat->id_parent) == $parent && $cat->active == 1 && trim($cat->id_parent) != 0) { // si cat est de ce niveau |
|
184
|
|
|
$select = ($cat->id == $this->categorie) ? ' selected' : ''; |
|
185
|
|
|
$html .= '<li><a class="link2cat'.$select.'" href="?mode=marketplace&categorie='.$cat->id.'"' |
|
186
|
|
|
.' title="'.dol_escape_htmltag(strip_tags($cat->description->language[$this->lang])).'" ' |
|
187
|
|
|
.'>'.$cat->name->language[$this->lang].' <sup>'.$cat->nb_products_recursive.'</sup></a>'; |
|
188
|
|
|
$html .= self::get_categories($cat->id); |
|
189
|
|
|
$html .= "</li>\n"; |
|
190
|
|
|
} else { |
|
191
|
|
|
|
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
if ($html == '<ul>') { |
|
196
|
|
|
return ''; |
|
197
|
|
|
} |
|
198
|
|
|
if ($parent != 0) { |
|
199
|
|
|
return $html.'</ul>'; |
|
200
|
|
|
} else { |
|
201
|
|
|
return $html; |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Return list of product formated for output |
|
207
|
|
|
* |
|
208
|
|
|
* @return string HTML output |
|
209
|
|
|
*/ |
|
210
|
|
|
function get_products() |
|
211
|
|
|
{ |
|
212
|
|
|
global $langs, $conf; |
|
213
|
|
|
$html = ""; |
|
214
|
|
|
$parity = "pair"; |
|
215
|
|
|
$last_month = time() - (30 * 24 * 60 * 60); |
|
216
|
|
|
foreach ($this->products as $product) { |
|
217
|
|
|
$parity = ($parity == "impair") ? 'pair' : 'impair'; |
|
218
|
|
|
|
|
219
|
|
|
// check new product ? |
|
220
|
|
|
$newapp = ''; |
|
221
|
|
|
if ($last_month < strtotime($product->date_add)) { |
|
222
|
|
|
$newapp .= '<span class="newApp">'.$langs->trans('New').'</span> '; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
// check updated ? |
|
226
|
|
|
if ($last_month < strtotime($product->date_upd) && $newapp == '') { |
|
227
|
|
|
$newapp .= '<span class="updatedApp">'.$langs->trans('Updated').'</span> '; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
// add image or default ? |
|
231
|
|
|
if ($product->id_default_image != '') { |
|
232
|
|
|
$image_url = DOL_URL_ROOT.'/admin/dolistore/ajax/image.php?id_product='.$product->id.'&id_image='.$product->id_default_image; |
|
233
|
|
|
$images = '<a href="'.$image_url.'" class="fancybox" rel="gallery'.$product->id.'" title="'.$product->name->language[$this->lang].', '.$langs->trans('Version').' '.$product->module_version.'">'. |
|
234
|
|
|
'<img src="'.$image_url.'&quality=home_default" style="max-height:250px;max-width: 210px;" alt="" /></a>'; |
|
235
|
|
|
} else { |
|
236
|
|
|
$images = '<img src="'.DOL_URL_ROOT.'/admin/dolistore/img/NoImageAvailable.png" />'; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
// free or pay ? |
|
240
|
|
|
if ($product->price > 0) { |
|
241
|
|
|
$price = '<h3>'.price(round((float) $product->price * $this->vat_rate, 2)).' €</h3>'; |
|
242
|
|
|
$download_link = '<a target="_blank" href="'.$this->shop_url.$product->id.'"><img width="32" src="'.DOL_URL_ROOT.'/admin/dolistore/img/follow.png" /></a>'; |
|
243
|
|
|
} else { |
|
244
|
|
|
$price = '<h3>'.$langs->trans('Free').'</h3>'; |
|
245
|
|
|
$download_link = '<a target="_blank" href="'.$this->shop_url.$product->id.'"><img width="32" src="'.DOL_URL_ROOT.'/admin/dolistore/img/Download-128.png" /></a>'; |
|
246
|
|
|
$download_link.= '<br><br><a target="_blank" href="'.$this->shop_url.$product->id.'"><img width="32" src="'.DOL_URL_ROOT.'/admin/dolistore/img/follow.png" /></a>'; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
//checking versions |
|
250
|
|
|
if ($this->version_compare($product->dolibarr_min, DOL_VERSION) <= 0) { |
|
251
|
|
|
if ($this->version_compare($product->dolibarr_max, DOL_VERSION) >= 0) { |
|
252
|
|
|
//compatible |
|
253
|
|
|
$version = '<span class="compatible">'.$langs->trans('CompatibleUpTo', $product->dolibarr_max, |
|
254
|
|
|
$product->dolibarr_min, $product->dolibarr_max).'</span>'; |
|
255
|
|
|
$compatible = ''; |
|
256
|
|
|
} else { |
|
257
|
|
|
//never compatible, module expired |
|
258
|
|
|
$version = '<span class="notcompatible">'.$langs->trans('NotCompatible', DOL_VERSION, |
|
259
|
|
|
$product->dolibarr_min, $product->dolibarr_max).'</span>'; |
|
260
|
|
|
$compatible = 'NotCompatible'; |
|
261
|
|
|
} |
|
262
|
|
|
} else { |
|
263
|
|
|
//need update |
|
264
|
|
|
$version = '<span class="compatibleafterupdate">'.$langs->trans('CompatibleAfterUpdate', DOL_VERSION, |
|
265
|
|
|
$product->dolibarr_min, $product->dolibarr_max).'</span>'; |
|
266
|
|
|
$compatible = 'NotCompatible'; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
//.'<br><a class="inline-block valignmiddle" target="_blank" href="'.$this->shop_url.$product->id.'"><span class="details button">'.$langs->trans("SeeInMarkerPlace").'</span></a> |
|
270
|
|
|
|
|
271
|
|
|
//output template |
|
272
|
|
|
$html .= '<tr class="app '.$parity.' '.$compatible.'"> |
|
273
|
|
|
<td align="center" width="210"><div class="newAppParent">'.$newapp.$images.'</div></td> |
|
274
|
|
|
<td class="margeCote"><h2 class="appTitle">'.$product->name->language[$this->lang] |
|
275
|
|
|
.'<br/><small>'.$version.'</small></h2> |
|
276
|
|
|
<small> '.dol_print_date(dol_stringtotime($product->date_upd), 'dayhour').' - '.$langs->trans('Ref').': '.$product->reference.' - '.$langs->trans('Id').': '.$product->id.'</small><br><br>'.$product->description_short->language[$this->lang].'</td> |
|
277
|
|
|
<td style="display:none;" class="long_description">'.$product->description->language[$this->lang].'</td> |
|
278
|
|
|
<td class="margeCote" align="center">'.$price.' |
|
279
|
|
|
</td> |
|
280
|
|
|
<td class="margeCote">'.$download_link.'</td> |
|
281
|
|
|
</tr>'; |
|
282
|
|
|
} |
|
283
|
|
|
return $html; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
function get_previous_link($text = '<<') |
|
287
|
|
|
{ |
|
288
|
|
|
return '<a href="'.$this->get_previous_url().'" class="button">'.$text.'</a>'; |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
function get_next_link($text = '>>') |
|
292
|
|
|
{ |
|
293
|
|
|
return '<a href="'.$this->get_next_url().'" class="button">'.$text.'</a>'; |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
function get_previous_url() |
|
297
|
|
|
{ |
|
298
|
|
|
$param_array = array(); |
|
299
|
|
|
if ($this->start < $this->per_page) { |
|
300
|
|
|
$sub = 0; |
|
301
|
|
|
} else { |
|
302
|
|
|
$sub = $this->per_page; |
|
303
|
|
|
} |
|
304
|
|
|
$param_array['start'] = $this->start - $sub; |
|
305
|
|
|
$param_array['end'] = $this->end - $sub; |
|
306
|
|
|
if ($this->categorie != 0) { |
|
307
|
|
|
$param_array['categorie'] = $this->categorie; |
|
308
|
|
|
} |
|
309
|
|
|
$param = http_build_query($param_array); |
|
310
|
|
|
return $this->url."&".$param; |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
function get_next_url() |
|
314
|
|
|
{ |
|
315
|
|
|
$param_array = array(); |
|
316
|
|
|
if (count($this->products) < $this->per_page) { |
|
317
|
|
|
$add = 0; |
|
318
|
|
|
} else { |
|
319
|
|
|
$add = $this->per_page; |
|
320
|
|
|
} |
|
321
|
|
|
$param_array['start'] = $this->start + $add; |
|
322
|
|
|
$param_array['end'] = $this->end + $add; |
|
323
|
|
|
if ($this->categorie != 0) { |
|
324
|
|
|
$param_array['categorie'] = $this->categorie; |
|
325
|
|
|
} |
|
326
|
|
|
$param = http_build_query($param_array); |
|
327
|
|
|
return $this->url."&".$param; |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
|
function version_compare($v1, $v2) |
|
331
|
|
|
{ |
|
332
|
|
|
$v1 = explode('.', $v1); |
|
333
|
|
|
$v2 = explode('.', $v2); |
|
334
|
|
|
$ret = 0; |
|
335
|
|
|
$level = 0; |
|
336
|
|
|
$count1 = count($v1); |
|
337
|
|
|
$count2 = count($v2); |
|
338
|
|
|
$maxcount = max($count1, $count2); |
|
339
|
|
|
while ($level < $maxcount) { |
|
340
|
|
|
$operande1 = isset($v1[$level]) ? $v1[$level] : 'x'; |
|
341
|
|
|
$operande2 = isset($v2[$level]) ? $v2[$level] : 'x'; |
|
342
|
|
|
$level++; |
|
343
|
|
|
if (strtoupper($operande1) == 'X' || strtoupper($operande2) == 'X' || $operande1 == '*' || $operande2 == '*') { |
|
344
|
|
|
break; |
|
345
|
|
|
} |
|
346
|
|
|
if ($operande1 < $operande2) { |
|
347
|
|
|
$ret = -$level; |
|
348
|
|
|
break; |
|
349
|
|
|
} |
|
350
|
|
|
if ($operande1 > $operande2) { |
|
351
|
|
|
$ret = $level; |
|
352
|
|
|
break; |
|
353
|
|
|
} |
|
354
|
|
|
} |
|
355
|
|
|
//print join('.',$versionarray1).'('.count($versionarray1).') / '.join('.',$versionarray2).'('.count($versionarray2).') => '.$ret.'<br>'."\n"; |
|
356
|
|
|
return $ret; |
|
357
|
|
|
} |
|
358
|
|
|
} |
|
359
|
|
|
|
|
360
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: