dolibarr  9.0.0
dolistore.class.php
1 <?php
2 /*
3  * Copyright (C) 2017 Oscss-Shop <support@oscss-shop.fr>.
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 
26 class Dolistore
27 {
33  public $start;
38  public $end;
39 
40  public $per_page; // pagination: display per page
41  public $categorie; // the current categorie
42  public $search; // the search keywords
43 
44  // setups
45  public $url; // the url of this page
46  public $shop_url; // the url of the shop
47  public $vat_rate; // the vat rate used in the shop (prices are provided without vat)
48  public $lang; // the integer representing the lang in the store
49  public $debug_api; // usefull if no dialog
50 
51 
57  function __construct($debug=false)
58  {
59  global $conf, $langs;
60 
61  $this->url = DOL_URL_ROOT.'/admin/modules.php?mode=marketplace';
62  $this->shop_url = 'https://www.dolistore.com/index.php?controller=product&id_product=';
63  $this->vat_rate = 1.2; // 20% de TVA
64  $this->debug_api = $debug;
65 
66  $langtmp = explode('_', $langs->defaultlang);
67  $lang = $langtmp[0];
68  $lang_array = array('en'=>0, 'fr'=>1, 'es'=>2, 'it'=>3, 'de'=>4); // Into table ps_lang of Prestashop - 1
69  if (! in_array($lang, array_keys($lang_array))) $lang = 'en';
70  $this->lang = $lang_array[$lang];
71  }
72 
80  function getRemoteData($options = array('start' => 0, 'end' => 10, 'per_page' => 50, 'categorie' => 0))
81  {
82  global $conf, $langs;
83 
84  $this->start = $options['start'];
85  $this->end = $options['end'];
86  $this->per_page = $options['per_page'];
87  $this->categorie = $options['categorie'];
88  $this->search = $options['search'];
89 
90  if ($this->end == 0) {
91  $this->end = $this->per_page;
92  }
93 
94  try {
95  $this->api = new PrestaShopWebservice($conf->global->MAIN_MODULE_DOLISTORE_API_SRV, $conf->global->MAIN_MODULE_DOLISTORE_API_KEY, $this->debug_api);
96  dol_syslog("Call API with MAIN_MODULE_DOLISTORE_API_SRV = ".$conf->global->MAIN_MODULE_DOLISTORE_API_SRV);
97  // $conf->global->MAIN_MODULE_DOLISTORE_API_KEY is for the login of basic auth. There is no password as it is public data.
98 
99  // Here we set the option array for the Webservice : we want products resources
100  $opt = array();
101  $opt['resource'] = 'products';
102  $opt2 = array();
103 
104  // make a search to limit the id returned.
105  if ($this->search != '') {
106  $opt2['url'] = $conf->global->MAIN_MODULE_DOLISTORE_API_SRV.'/api/search?query='.$this->search.'&language='.$this->lang;
107 
108  // Call
109  //var_dump($this->api);
110 
111  dol_syslog("Call API with opt = ".var_export($opt, true));
112  dol_syslog("Call API with opt2 = ".var_export($opt2, true));
113 
114  $xml = $this->api->get($opt2);
115  $products = array();
116  foreach ($xml->products->children() as $product) {
117  $products[] = (int) $product['id'];
118  }
119  $opt['filter[id]'] = '['.implode('|', $products).']';
120  } elseif ($this->categorie != 0) {
121  $opt2['resource'] = 'categories';
122  $opt2['id'] = $this->categorie;
123  // Call
124  $xml = $this->api->get($opt2);
125  $products = array();
126  foreach ($xml->category->associations->products->children() as $product) {
127  $products[] = (int) $product->id;
128  }
129  $opt['filter[id]'] = '['.implode('|', $products).']';
130  }
131  $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]';
132  $opt['sort'] = 'id_desc';
133  $opt['filter[active]'] = '[1]';
134  $opt['limit'] = "$this->start,$this->end";
135  // $opt['filter[id]'] contais list of product id that are result of search
136 
137 
138  // Call API to get the detail
139  dol_syslog("Call API with opt = ".var_export($opt, true));
140  dol_syslog("Call API with opt2 = ".var_export($opt2, true));
141  $xml = $this->api->get($opt);
142  $this->products = $xml->products->children();
143 
144 
145  // Here we set the option array for the Webservice : we want categories resources
146  $opt = array();
147  $opt['resource'] = 'categories';
148  $opt['display'] = '[id,id_parent,nb_products_recursive,active,is_root_category,name,description]';
149  $opt['sort'] = 'id_asc';
150  // Call
151  $xml = $this->api->get($opt);
152  $this->categories = $xml->categories->children();
153  } catch (PrestaShopWebserviceException $e) {
154  // Here we are dealing with errors
155  $trace = $e->getTrace();
156  if ($trace[0]['args'][0] == 404) die('Bad ID');
157  else if ($trace[0]['args'][0] == 401) die('Bad auth key');
158  else
159  {
160  print 'Can not access to '.$conf->global->MAIN_MODULE_DOLISTORE_API_SRV.'<br>';
161  print $e->getMessage();
162  }
163  }
164  }
165 
166  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
173  function get_categories($parent = 0)
174  {
175  // phpcs:enable
176  if (!isset($this->categories)) die('not possible');
177  if ($parent != 0) {
178  $html = '<ul>';
179  } else {
180  $html = '';
181  }
182 
183  $nbofcateg = count($this->categories);
184  for ($i = 0; $i < $nbofcateg; $i++)
185  {
186  $cat = $this->categories[$i];
187  if ($cat->is_root_category == 1 && $parent == 0) {
188  $html .= '<li class="root"><h3 class="nomargesupinf"><a class="nomargesupinf link2cat" href="?mode=marketplace&categorie='.$cat->id.'" '
189  .'title="'.dol_escape_htmltag(strip_tags($cat->description->language[$this->lang])).'"'
190  .'>'.$cat->name->language[$this->lang].' <sup>'.$cat->nb_products_recursive.'</sup></a></h3>';
191  $html .= self::get_categories($cat->id);
192  $html .= "</li>\n";
193  } elseif (trim($cat->id_parent) == $parent && $cat->active == 1 && trim($cat->id_parent) != 0) { // si cat est de ce niveau
194  $select = ($cat->id == $this->categorie) ? ' selected' : '';
195  $html .= '<li><a class="link2cat'.$select.'" href="?mode=marketplace&categorie='.$cat->id.'"'
196  .' title="'.dol_escape_htmltag(strip_tags($cat->description->language[$this->lang])).'" '
197  .'>'.$cat->name->language[$this->lang].' <sup>'.$cat->nb_products_recursive.'</sup></a>';
198  $html .= self::get_categories($cat->id);
199  $html .= "</li>\n";
200  } else {
201 
202  }
203  }
204 
205  if ($html == '<ul>') {
206  return '';
207  }
208  if ($parent != 0) {
209  return $html.'</ul>';
210  } else {
211  return $html;
212  }
213  }
214 
215  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
221  function get_products()
222  {
223  // phpcs:enable
224  global $langs, $conf;
225  $html = "";
226  $parity = "pair";
227  $last_month = time() - (30 * 24 * 60 * 60);
228  foreach ($this->products as $product) {
229  $parity = ($parity == "impair") ? 'pair' : 'impair';
230 
231  // check new product ?
232  $newapp = '';
233  if ($last_month < strtotime($product->date_add)) {
234  $newapp .= '<span class="newApp">'.$langs->trans('New').'</span> ';
235  }
236 
237  // check updated ?
238  if ($last_month < strtotime($product->date_upd) && $newapp == '') {
239  $newapp .= '<span class="updatedApp">'.$langs->trans('Updated').'</span> ';
240  }
241 
242  // add image or default ?
243  if ($product->id_default_image != '') {
244  $image_url = DOL_URL_ROOT.'/admin/dolistore/ajax/image.php?id_product='.$product->id.'&id_image='.$product->id_default_image;
245  $images = '<a href="'.$image_url.'" class="fancybox" rel="gallery'.$product->id.'" title="'.$product->name->language[$this->lang].', '.$langs->trans('Version').' '.$product->module_version.'">'.
246  '<img src="'.$image_url.'&quality=home_default" style="max-height:250px;max-width: 210px;" alt="" /></a>';
247  } else {
248  $images = '<img src="'.DOL_URL_ROOT.'/admin/dolistore/img/NoImageAvailable.png" />';
249  }
250 
251  // free or pay ?
252  if ($product->price > 0) {
253  $price = '<h3>'.price(round((float) $product->price * $this->vat_rate, 2)).'&nbsp;&euro;</h3>';
254  $download_link = '<a target="_blank" href="'.$this->shop_url.$product->id.'"><img width="32" src="'.DOL_URL_ROOT.'/admin/dolistore/img/follow.png" /></a>';
255  } else {
256  $price = '<h3>'.$langs->trans('Free').'</h3>';
257  $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>';
258  $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>';
259  }
260 
261  //checking versions
262  if ($this->version_compare($product->dolibarr_min, DOL_VERSION) <= 0) {
263  if ($this->version_compare($product->dolibarr_max, DOL_VERSION) >= 0) {
264  //compatible
265  $version = '<span class="compatible">'.$langs->trans('CompatibleUpTo', $product->dolibarr_max,
266  $product->dolibarr_min, $product->dolibarr_max).'</span>';
267  $compatible = '';
268  } else {
269  //never compatible, module expired
270  $version = '<span class="notcompatible">'.$langs->trans('NotCompatible', DOL_VERSION,
271  $product->dolibarr_min, $product->dolibarr_max).'</span>';
272  $compatible = 'NotCompatible';
273  }
274  } else {
275  //need update
276  $version = '<span class="compatibleafterupdate">'.$langs->trans('CompatibleAfterUpdate', DOL_VERSION,
277  $product->dolibarr_min, $product->dolibarr_max).'</span>';
278  $compatible = 'NotCompatible';
279  }
280 
281  //.'<br><a class="inline-block valignmiddle" target="_blank" href="'.$this->shop_url.$product->id.'"><span class="details button">'.$langs->trans("SeeInMarkerPlace").'</span></a>
282 
283  //output template
284  $html .= '<tr class="app '.$parity.' '.$compatible.'">
285  <td align="center" width="210"><div class="newAppParent">'.$newapp.$images.'</div></td>
286  <td class="margeCote"><h2 class="appTitle">'.$product->name->language[$this->lang]
287  .'<br/><small>'.$version.'</small></h2>
288  <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>
289  <td style="display:none;" class="long_description">'.$product->description->language[$this->lang].'</td>
290  <td class="margeCote" align="center">'.$price.'
291  </td>
292  <td class="margeCote">'.$download_link.'</td>
293  </tr>';
294  }
295  return $html;
296  }
297 
298  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
305  function get_previous_link($text = '<<')
306  {
307  // phpcs:enable
308  return '<a href="'.$this->get_previous_url().'" class="button">'.$text.'</a>';
309  }
310 
311  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
318  function get_next_link($text = '>>')
319  {
320  // phpcs:enable
321  return '<a href="'.$this->get_next_url().'" class="button">'.$text.'</a>';
322  }
323 
324  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
330  function get_previous_url()
331  {
332  // phpcs:enable
333  $param_array = array();
334  if ($this->start < $this->per_page) {
335  $sub = 0;
336  } else {
337  $sub = $this->per_page;
338  }
339  $param_array['start'] = $this->start - $sub;
340  $param_array['end'] = $this->end - $sub;
341  if ($this->categorie != 0) {
342  $param_array['categorie'] = $this->categorie;
343  }
344  $param = http_build_query($param_array);
345  return $this->url."&".$param;
346  }
347 
348  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
354  function get_next_url()
355  {
356  // phpcs:enable
357  $param_array = array();
358  if (count($this->products) < $this->per_page) {
359  $add = 0;
360  } else {
361  $add = $this->per_page;
362  }
363  $param_array['start'] = $this->start + $add;
364  $param_array['end'] = $this->end + $add;
365  if ($this->categorie != 0) {
366  $param_array['categorie'] = $this->categorie;
367  }
368  $param = http_build_query($param_array);
369  return $this->url."&".$param;
370  }
371 
372  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
380  function version_compare($v1, $v2)
381  {
382  // phpcs:enable
383  $v1 = explode('.', $v1);
384  $v2 = explode('.', $v2);
385  $ret = 0;
386  $level = 0;
387  $count1 = count($v1);
388  $count2 = count($v2);
389  $maxcount = max($count1, $count2);
390  while ($level < $maxcount) {
391  $operande1 = isset($v1[$level]) ? $v1[$level] : 'x';
392  $operande2 = isset($v2[$level]) ? $v2[$level] : 'x';
393  $level++;
394  if (strtoupper($operande1) == 'X' || strtoupper($operande2) == 'X' || $operande1 == '*' || $operande2 == '*') {
395  break;
396  }
397  if ($operande1 < $operande2) {
398  $ret = -$level;
399  break;
400  }
401  if ($operande1 > $operande2) {
402  $ret = $level;
403  break;
404  }
405  }
406  //print join('.',$versionarray1).'('.count($versionarray1).') / '.join('.',$versionarray2).'('.count($versionarray2).') => '.$ret.'<br>'."\n";
407  return $ret;
408  }
409 }
get_next_link($text='>>')
get next link
__construct($debug=false)
Constructor.
print
Draft customers invoices.
Definition: index.php:91
get_previous_url()
get previous url
getRemoteData($options=array('start'=> 0, 'end'=> 10, 'per_page'=> 50, 'categorie'=> 0))
Load data from remote Dolistore market place.
dol_stringtotime($string, $gm=1)
Convert a string date into a GM Timestamps date Warning: YYYY-MM-DDTHH:MM:SS+02:00 (RFC3339) is not s...
Definition: date.lib.php:295
version_compare($v1, $v2)
version compare
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
get_products()
Return list of product formated for output.
get_next_url()
get next url
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Class Dolistore.
get_categories($parent=0)
Return tree of Dolistore categories.
get_previous_link($text='<<')
get previous link