dolibarr  7.0.0-beta
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 {
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 
46  function __construct()
47  {
48  global $conf, $langs;
49 
50  $this->url = DOL_URL_ROOT.'/admin/modules.php?mode=marketplace';
51  $this->shop_url = 'https://www.dolistore.com/index.php?controller=product&id_product=';
52  $this->vat_rate = 1.2; // 20% de TVA
53  $this->debug_api = false;
54 
55  $langtmp = explode('_', $langs->defaultlang);
56  $lang = $langtmp[0];
57  $lang_array = array('en'=>0, 'fr'=>1, 'es'=>2, 'it'=>3, 'de'=>4); // Into table ps_lang of Prestashop - 1
58  if (! in_array($lang, array_keys($lang_array))) $lang = 'en';
59  $this->lang = $lang_array[$lang];
60  }
61 
69  function getRemoteData($options = array('start' => 0, 'end' => 10, 'per_page' => 50, 'categorie' => 0))
70  {
71  global $conf, $langs;
72 
73  $this->start = $options['start'];
74  $this->end = $options['end'];
75  $this->per_page = $options['per_page'];
76  $this->categorie = $options['categorie'];
77  $this->search = $options['search'];
78 
79  if ($this->end == 0) {
80  $this->end = $this->per_page;
81  }
82 
83  try {
84  $this->api = new PrestaShopWebservice($conf->global->MAIN_MODULE_DOLISTORE_API_SRV,
85  $conf->global->MAIN_MODULE_DOLISTORE_API_KEY, $this->debug_api);
86 
87  // Here we set the option array for the Webservice : we want products resources
88  $opt = array();
89  $opt['resource'] = 'products';
90 
91  // make a search to limit the id returned.
92  if ($this->search != '') {
93  $opt2 = array();
94  $opt2['url'] = $conf->global->MAIN_MODULE_DOLISTORE_API_SRV.'/api/search?query='.$this->search.'&language='.$this->lang;
95  // Call
96  $xml = $this->api->get($opt2);
97  $products = array();
98  foreach ($xml->products->children() as $product) {
99  $products[] = (int) $product['id'];
100  }
101  $opt['filter[id]'] = '['.implode('|', $products).']';
102  } elseif ($this->categorie != 0) {
103  $opt2 = array();
104  $opt2['resource'] = 'categories';
105  $opt2['id'] = $this->categorie;
106  // Call
107  $xml = $this->api->get($opt2);
108  $products = array();
109  foreach ($xml->category->associations->products->children() as $product) {
110  $products[] = (int) $product->id;
111  }
112  $opt['filter[id]'] = '['.implode('|', $products).']';
113  }
114  $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]';
115  $opt['sort'] = 'id_desc';
116  $opt['filter[active]'] = '[1]';
117  $opt['limit'] = "$this->start,$this->end";
118  // $opt['filter[id]'] contais list of product id that are result of search
119 
120  // Call API to get the detail
121  $xml = $this->api->get($opt);
122  $this->products = $xml->products->children();
123 
124 
125  // Here we set the option array for the Webservice : we want categories resources
126  $opt = array();
127  $opt['resource'] = 'categories';
128  $opt['display'] = '[id,id_parent,nb_products_recursive,active,is_root_category,name,description]';
129  $opt['sort'] = 'id_asc';
130  // Call
131  $xml = $this->api->get($opt);
132  $this->categories = $xml->categories->children();
133  } catch (PrestaShopWebserviceException $e) {
134  // Here we are dealing with errors
135  $trace = $e->getTrace();
136  if ($trace[0]['args'][0] == 404) die('Bad ID');
137  else if ($trace[0]['args'][0] == 401) die('Bad auth key');
138  else die('Can not access to '.$conf->global->MAIN_MODULE_DOLISTORE_API_SRV);
139  }
140  }
141 
148  function get_categories($parent = 0)
149  {
150  if (!isset($this->categories)) die('not possible');
151  if ($parent != 0) {
152  $html = '<ul>';
153  } else {
154  $html = '';
155  }
156 
157  $nbofcateg = count($this->categories);
158  for ($i = 0; $i < $nbofcateg; $i++)
159  {
160  $cat = $this->categories[$i];
161  if ($cat->is_root_category == 1 && $parent == 0) {
162  $html .= '<li class="root"><h3 class="nomargesupinf"><a class="nomargesupinf link2cat" href="?mode=marketplace&categorie='.$cat->id.'" '
163  .'title="'.dol_escape_htmltag(strip_tags($cat->description->language[$this->lang])).'"'
164  .'>'.$cat->name->language[$this->lang].' <sup>'.$cat->nb_products_recursive.'</sup></a></h3>';
165  $html .= self::get_categories($cat->id);
166  $html .= "</li>\n";
167  } elseif (trim($cat->id_parent) == $parent && $cat->active == 1 && trim($cat->id_parent) != 0) { // si cat est de ce niveau
168  $select = ($cat->id == $this->categorie) ? ' selected' : '';
169  $html .= '<li><a class="link2cat'.$select.'" href="?mode=marketplace&categorie='.$cat->id.'"'
170  .' title="'.dol_escape_htmltag(strip_tags($cat->description->language[$this->lang])).'" '
171  .'>'.$cat->name->language[$this->lang].' <sup>'.$cat->nb_products_recursive.'</sup></a>';
172  $html .= self::get_categories($cat->id);
173  $html .= "</li>\n";
174  } else {
175 
176  }
177  }
178 
179  if ($html == '<ul>') {
180  return '';
181  }
182  if ($parent != 0) {
183  return $html.'</ul>';
184  } else {
185  return $html;
186  }
187  }
188 
194  function get_products()
195  {
196  global $langs, $conf;
197  $html = "";
198  $parity = "pair";
199  $last_month = time() - (30 * 24 * 60 * 60);
200  foreach ($this->products as $product) {
201  $parity = ($parity == "impair") ? 'pair' : 'impair';
202 
203  // check new product ?
204  $newapp = '';
205  if ($last_month < strtotime($product->date_add)) {
206  $newapp .= '<span class="newApp">'.$langs->trans('New').'</span> ';
207  }
208 
209  // check updated ?
210  if ($last_month < strtotime($product->date_upd) && $newapp == '') {
211  $newapp .= '<span class="updatedApp">'.$langs->trans('Updated').'</span> ';
212  }
213 
214  // add image or default ?
215  if ($product->id_default_image != '') {
216  $image_url = DOL_URL_ROOT.'/admin/dolistore/ajax/image.php?id_product='.$product->id.'&id_image='.$product->id_default_image;
217  $images = '<a href="'.$image_url.'" class="fancybox" rel="gallery'.$product->id.'" title="'.$product->name->language[$this->lang].', '.$langs->trans('Version').' '.$product->module_version.'">'.
218  '<img src="'.$image_url.'&quality=home_default" style="max-height:250px;max-width: 210px;" alt="" /></a>';
219  } else {
220  $images = '<img src="'.DOL_URL_ROOT.'/admin/dolistore/img/NoImageAvailable.png" />';
221  }
222 
223  // free or pay ?
224  if ($product->price > 0) {
225  $price = '<h3>'.price(round((float) $product->price * $this->vat_rate, 2)).'&nbsp;&euro;</h3>';
226  $download_link = '<a target="_blank" href="'.$this->shop_url.$product->id.'"><img width="32" src="'.DOL_URL_ROOT.'/admin/dolistore/img/follow.png" /></a>';
227  } else {
228  $price = '<h3>'.$langs->trans('Free').'</h3>';
229  $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>';
230  $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>';
231  }
232 
233  //checking versions
234  if ($this->version_compare($product->dolibarr_min, DOL_VERSION) <= 0) {
235  if ($this->version_compare($product->dolibarr_max, DOL_VERSION) >= 0) {
236  //compatible
237  $version = '<span class="compatible">'.$langs->trans('CompatibleUpTo', $product->dolibarr_max,
238  $product->dolibarr_min, $product->dolibarr_max).'</span>';
239  $compatible = '';
240  } else {
241  //never compatible, module expired
242  $version = '<span class="notcompatible">'.$langs->trans('NotCompatible', DOL_VERSION,
243  $product->dolibarr_min, $product->dolibarr_max).'</span>';
244  $compatible = 'NotCompatible';
245  }
246  } else {
247  //need update
248  $version = '<span class="compatibleafterupdate">'.$langs->trans('CompatibleAfterUpdate', DOL_VERSION,
249  $product->dolibarr_min, $product->dolibarr_max).'</span>';
250  $compatible = 'NotCompatible';
251  }
252 
253  //.'<br><a class="inline-block valignmiddle" target="_blank" href="'.$this->shop_url.$product->id.'"><span class="details button">'.$langs->trans("SeeInMarkerPlace").'</span></a>
254 
255  //output template
256  $html .= '<tr class="app '.$parity.' '.$compatible.'">
257  <td align="center" width="210"><div class="newAppParent">'.$newapp.$images.'</div></td>
258  <td class="margeCote"><h2 class="appTitle">'.$product->name->language[$this->lang]
259  .'<br/><small>'.$version.'</small></h2>
260  <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>
261  <td style="display:none;" class="long_description">'.$product->description->language[$this->lang].'</td>
262  <td class="margeCote" align="center">'.$price.'
263  </td>
264  <td class="margeCote">'.$download_link.'</td>
265  </tr>';
266  }
267  return $html;
268  }
269 
270  function get_previous_link($text = '<<')
271  {
272  return '<a href="'.$this->get_previous_url().'" class="button">'.$text.'</a>';
273  }
274 
275  function get_next_link($text = '>>')
276  {
277  return '<a href="'.$this->get_next_url().'" class="button">'.$text.'</a>';
278  }
279 
280  function get_previous_url()
281  {
282  $param_array = array();
283  if ($this->start < $this->per_page) {
284  $sub = 0;
285  } else {
286  $sub = $this->per_page;
287  }
288  $param_array['start'] = $this->start - $sub;
289  $param_array['end'] = $this->end - $sub;
290  if ($this->categorie != 0) {
291  $param_array['categorie'] = $this->categorie;
292  }
293  $param = http_build_query($param_array);
294  return $this->url."&".$param;
295  }
296 
297  function get_next_url()
298  {
299  $param_array = array();
300  if (count($this->products) < $this->per_page) {
301  $add = 0;
302  } else {
303  $add = $this->per_page;
304  }
305  $param_array['start'] = $this->start + $add;
306  $param_array['end'] = $this->end + $add;
307  if ($this->categorie != 0) {
308  $param_array['categorie'] = $this->categorie;
309  }
310  $param = http_build_query($param_array);
311  return $this->url."&".$param;
312  }
313 
314  function version_compare($v1, $v2)
315  {
316  $v1 = explode('.', $v1);
317  $v2 = explode('.', $v2);
318  $ret = 0;
319  $level = 0;
320  $count1 = count($v1);
321  $count2 = count($v2);
322  $maxcount = max($count1, $count2);
323  while ($level < $maxcount) {
324  $operande1 = isset($v1[$level]) ? $v1[$level] : 'x';
325  $operande2 = isset($v2[$level]) ? $v2[$level] : 'x';
326  $level++;
327  if (strtoupper($operande1) == 'X' || strtoupper($operande2) == 'X' || $operande1 == '*' || $operande2 == '*') {
328  break;
329  }
330  if ($operande1 < $operande2) {
331  $ret = -$level;
332  break;
333  }
334  if ($operande1 > $operande2) {
335  $ret = $level;
336  break;
337  }
338  }
339  //print join('.',$versionarray1).'('.count($versionarray1).') / '.join('.',$versionarray2).'('.count($versionarray2).') => '.$ret.'<br>'."\n";
340  return $ret;
341  }
342 }
343 
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:286
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...
__construct()
Constructor.
get_products()
Return list of product formated for output.
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.