dolistore.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 <https://www.gnu.org/licenses/>.
  16. * or see https://www.gnu.org/
  17. */
  18. include_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  19. if (!class_exists('PrestaShopWebservice')) // We keep this because some modules add this lib too into a different path. This is to avoid "Cannot declare class PrestaShopWebservice" errors.
  20. {
  21. include_once DOL_DOCUMENT_ROOT.'/admin/dolistore/class/PSWebServiceLibrary.class.php';
  22. }
  23. /**
  24. * Class Dolistore
  25. */
  26. class Dolistore
  27. {
  28. /**
  29. * beginning of pagination
  30. * @var int
  31. */
  32. public $start;
  33. /**
  34. * end of pagination
  35. * @var int
  36. */
  37. public $end;
  38. public $per_page; // pagination: display per page
  39. public $categorie; // the current categorie
  40. public $search; // the search keywords
  41. // setups
  42. public $url; // the url of this page
  43. public $shop_url; // the url of the shop
  44. public $lang; // the integer representing the lang in the store
  45. public $debug_api; // usefull if no dialog
  46. /**
  47. * Constructor
  48. *
  49. * @param boolean $debug Enable debug of request on screen
  50. */
  51. public function __construct($debug = false)
  52. {
  53. global $conf, $langs;
  54. $this->url = DOL_URL_ROOT.'/admin/modules.php?mode=marketplace';
  55. $this->shop_url = 'https://www.dolistore.com/index.php?controller=product&id_product=';
  56. $this->debug_api = $debug;
  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. * Load data from remote Dolistore market place.
  65. * This fills ->categories
  66. *
  67. * @return void
  68. */
  69. public function getRemoteCategories()
  70. {
  71. global $conf;
  72. try {
  73. $this->api = new PrestaShopWebservice($conf->global->MAIN_MODULE_DOLISTORE_API_SRV, $conf->global->MAIN_MODULE_DOLISTORE_API_KEY, $this->debug_api);
  74. dol_syslog("Call API with MAIN_MODULE_DOLISTORE_API_SRV = ".$conf->global->MAIN_MODULE_DOLISTORE_API_SRV);
  75. // $conf->global->MAIN_MODULE_DOLISTORE_API_KEY is for the login of basic auth. There is no password as it is public data.
  76. // Here we set the option array for the Webservice : we want categories resources
  77. $opt = array();
  78. $opt['resource'] = 'categories';
  79. $opt['display'] = '[id,id_parent,nb_products_recursive,active,is_root_category,name,description]';
  80. $opt['sort'] = 'id_asc';
  81. // Call
  82. dol_syslog("Call API with opt = ".var_export($opt, true));
  83. $xml = $this->api->get($opt);
  84. $this->categories = $xml->categories->children();
  85. } catch (PrestaShopWebserviceException $e) {
  86. // Here we are dealing with errors
  87. $trace = $e->getTrace();
  88. if ($trace[0]['args'][0] == 404) die('Bad ID');
  89. elseif ($trace[0]['args'][0] == 401) die('Bad auth key');
  90. else
  91. {
  92. print 'Can not access to '.$conf->global->MAIN_MODULE_DOLISTORE_API_SRV.'<br>';
  93. print $e->getMessage();
  94. }
  95. }
  96. }
  97. /**
  98. * Load data from remote Dolistore market place.
  99. * This fills ->products
  100. *
  101. * @param array $options Options. If 'categorie' is defined, we filter products on this category id
  102. * @return void
  103. */
  104. public function getRemoteProducts($options = array('start' => 0, 'end' => 10, 'per_page' => 50, 'categorie' => 0, 'search' => ''))
  105. {
  106. global $conf;
  107. $this->start = $options['start'];
  108. $this->end = $options['end'];
  109. $this->per_page = $options['per_page'];
  110. $this->categorie = $options['categorie'];
  111. $this->search = $options['search'];
  112. if ($this->end == 0) {
  113. $this->end = $this->per_page;
  114. }
  115. try {
  116. $this->api = new PrestaShopWebservice($conf->global->MAIN_MODULE_DOLISTORE_API_SRV, $conf->global->MAIN_MODULE_DOLISTORE_API_KEY, $this->debug_api);
  117. dol_syslog("Call API with MAIN_MODULE_DOLISTORE_API_SRV = ".$conf->global->MAIN_MODULE_DOLISTORE_API_SRV);
  118. // $conf->global->MAIN_MODULE_DOLISTORE_API_KEY is for the login of basic auth. There is no password as it is public data.
  119. // Here we set the option array for the Webservice : we want products resources
  120. $opt = array();
  121. $opt['resource'] = 'products';
  122. $opt2 = array();
  123. // make a search to limit the id returned.
  124. if ($this->search != '') {
  125. $opt2['url'] = $conf->global->MAIN_MODULE_DOLISTORE_API_SRV.'/api/search?query='.$this->search.'&language='.$this->lang; // It seems for search, key start with
  126. // Call
  127. dol_syslog("Call API with opt2 = ".var_export($opt2, true));
  128. $xml = $this->api->get($opt2);
  129. $products = array();
  130. foreach ($xml->products->children() as $product) {
  131. $products[] = (int) $product['id'];
  132. }
  133. $opt['filter[id]'] = '['.implode('|', $products).']';
  134. } elseif ($this->categorie != 0) { // We filter on category, so we first get list of product id in this category
  135. // $opt2['url'] is set by default to $this->url.'/api/'.$options['resource'];
  136. $opt2['resource'] = 'categories';
  137. $opt2['id'] = $this->categorie;
  138. // Call
  139. dol_syslog("Call API with opt2 = ".var_export($opt2, true));
  140. $xml = $this->api->get($opt2);
  141. $products = array();
  142. foreach ($xml->category->associations->products->children() as $product) {
  143. $products[] = (int) $product->id;
  144. }
  145. $opt['filter[id]'] = '['.implode('|', $products).']';
  146. }
  147. $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]';
  148. $opt['sort'] = 'id_desc';
  149. $opt['filter[active]'] = '[1]';
  150. $opt['limit'] = "$this->start,$this->end";
  151. // $opt['filter[id]'] contais list of product id that are result of search
  152. // Call API to get the detail
  153. dol_syslog("Call API with opt = ".var_export($opt, true));
  154. $xml = $this->api->get($opt);
  155. $this->products = $xml->products->children();
  156. } catch (PrestaShopWebserviceException $e) {
  157. // Here we are dealing with errors
  158. $trace = $e->getTrace();
  159. if ($trace[0]['args'][0] == 404) die('Bad ID');
  160. elseif ($trace[0]['args'][0] == 401) die('Bad auth key');
  161. else
  162. {
  163. print 'Can not access to '.$conf->global->MAIN_MODULE_DOLISTORE_API_SRV.'<br>';
  164. print $e->getMessage();
  165. }
  166. }
  167. }
  168. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  169. /**
  170. * Return tree of Dolistore categories. $this->categories must have been loaded before.
  171. *
  172. * @param int $parent Id of parent category
  173. * @return string
  174. */
  175. public function get_categories($parent = 0)
  176. {
  177. // phpcs:enable
  178. if (!isset($this->categories)) die('not possible');
  179. if ($parent != 0) {
  180. $html = '<ul>';
  181. } else {
  182. $html = '';
  183. }
  184. $nbofcateg = count($this->categories);
  185. for ($i = 0; $i < $nbofcateg; $i++)
  186. {
  187. $cat = $this->categories[$i];
  188. if ($cat->is_root_category == 1 && $parent == 0) {
  189. $html .= '<li class="root"><h3 class="nomargesupinf"><a class="nomargesupinf link2cat" href="?mode=marketplace&categorie='.$cat->id.'" ';
  190. $html .= 'title="'.dol_escape_htmltag(strip_tags($cat->description->language[$this->lang - 1])).'">'.$cat->name->language[$this->lang - 1].' <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. $html .= ' title="'.dol_escape_htmltag(strip_tags($cat->description->language[$this->lang - 1])).'" ';
  197. $html .= '>'.$cat->name->language[$this->lang - 1].' <sup>'.$cat->nb_products_recursive.'</sup></a>';
  198. $html .= self::get_categories($cat->id);
  199. $html .= "</li>\n";
  200. }
  201. }
  202. if ($html == '<ul>') {
  203. return '';
  204. }
  205. if ($parent != 0) {
  206. return $html.'</ul>';
  207. } else {
  208. return $html;
  209. }
  210. }
  211. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  212. /**
  213. * Return list of product formated for output
  214. *
  215. * @return string HTML output
  216. */
  217. public function get_products()
  218. {
  219. // phpcs:enable
  220. global $langs, $conf;
  221. $html = "";
  222. $parity = "pair";
  223. $last_month = time() - (30 * 24 * 60 * 60);
  224. foreach ($this->products as $product) {
  225. $parity = ($parity == "impair") ? 'pair' : 'impair';
  226. // check new product ?
  227. $newapp = '';
  228. if ($last_month < strtotime($product->date_add)) {
  229. $newapp .= '<span class="newApp">'.$langs->trans('New').'</span> ';
  230. }
  231. // check updated ?
  232. if ($last_month < strtotime($product->date_upd) && $newapp == '') {
  233. $newapp .= '<span class="updatedApp">'.$langs->trans('Updated').'</span> ';
  234. }
  235. // add image or default ?
  236. if ($product->id_default_image != '') {
  237. $image_url = DOL_URL_ROOT.'/admin/dolistore/ajax/image.php?id_product='.$product->id.'&id_image='.$product->id_default_image;
  238. $images = '<a href="'.$image_url.'" class="fancybox" rel="gallery'.$product->id.'" title="'.$product->name->language[$this->lang - 1].', '.$langs->trans('Version').' '.$product->module_version.'">'.
  239. '<img src="'.$image_url.'&quality=home_default" style="max-height:250px;max-width: 210px;" alt="" /></a>';
  240. } else {
  241. $images = '<img src="'.DOL_URL_ROOT.'/admin/dolistore/img/NoImageAvailable.png" />';
  242. }
  243. // free or pay ?
  244. if ($product->price > 0) {
  245. $price = '<h3>'.price(price2num($product->price, 'MT'), 0, $langs, 1, -1, -1, 'EUR').' '.$langs->trans("HT").'</h3>';
  246. $download_link = '<a target="_blank" href="'.$this->shop_url.$product->id.'"><img width="32" src="'.DOL_URL_ROOT.'/admin/dolistore/img/follow.png" /></a>';
  247. } else {
  248. $price = '<h3>'.$langs->trans('Free').'</h3>';
  249. $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>';
  250. $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>';
  251. }
  252. //checking versions
  253. if ($this->version_compare($product->dolibarr_min, DOL_VERSION) <= 0) {
  254. if ($this->version_compare($product->dolibarr_max, DOL_VERSION) >= 0) {
  255. //compatible
  256. $version = '<span class="compatible">'.$langs->trans('CompatibleUpTo', $product->dolibarr_max,
  257. $product->dolibarr_min, $product->dolibarr_max).'</span>';
  258. $compatible = '';
  259. } else {
  260. //never compatible, module expired
  261. $version = '<span class="notcompatible">'.$langs->trans('NotCompatible', DOL_VERSION,
  262. $product->dolibarr_min, $product->dolibarr_max).'</span>';
  263. $compatible = 'NotCompatible';
  264. }
  265. } else {
  266. //need update
  267. $version = '<span class="compatibleafterupdate">'.$langs->trans('CompatibleAfterUpdate', DOL_VERSION,
  268. $product->dolibarr_min, $product->dolibarr_max).'</span>';
  269. $compatible = 'NotCompatible';
  270. }
  271. //.'<br><a class="inline-block valignmiddle" target="_blank" href="'.$this->shop_url.$product->id.'"><span class="details button">'.$langs->trans("SeeInMarkerPlace").'</span></a>
  272. //output template
  273. $html .= '<tr class="app '.$parity.' '.$compatible.'">
  274. <td class="center" width="210"><div class="newAppParent">'.$newapp.$images.'</div></td>
  275. <td class="margeCote"><h2 class="appTitle">'.$product->name->language[$this->lang - 1]
  276. .'<br/><small>'.$version.'</small></h2>
  277. <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 - 1].'</td>
  278. <td style="display:none;" class="long_description">'.$product->description->language[$this->lang - 1].'</td>
  279. <td class="margeCote center">'.$price.'
  280. </td>
  281. <td class="margeCote">'.$download_link.'</td>
  282. </tr>';
  283. }
  284. return $html;
  285. }
  286. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  287. /**
  288. * get previous link
  289. *
  290. * @param string $text symbol previous
  291. * @return string html previous link
  292. */
  293. public function get_previous_link($text = '<<')
  294. {
  295. // phpcs:enable
  296. return '<a href="'.$this->get_previous_url().'" class="button">'.$text.'</a>';
  297. }
  298. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  299. /**
  300. * get next link
  301. *
  302. * @param string $text symbol next
  303. * @return string html next link
  304. */
  305. public function get_next_link($text = '>>')
  306. {
  307. // phpcs:enable
  308. return '<a href="'.$this->get_next_url().'" class="button">'.$text.'</a>';
  309. }
  310. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  311. /**
  312. * get previous url
  313. *
  314. * @return string previous url
  315. */
  316. public function get_previous_url()
  317. {
  318. // phpcs:enable
  319. $param_array = array();
  320. if ($this->start < $this->per_page) {
  321. $sub = 0;
  322. } else {
  323. $sub = $this->per_page;
  324. }
  325. $param_array['start'] = $this->start - $sub;
  326. $param_array['end'] = $this->end - $sub;
  327. if ($this->categorie != 0) {
  328. $param_array['categorie'] = $this->categorie;
  329. }
  330. $param = http_build_query($param_array);
  331. return $this->url."&".$param;
  332. }
  333. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  334. /**
  335. * get next url
  336. *
  337. * @return string next url
  338. */
  339. public function get_next_url()
  340. {
  341. // phpcs:enable
  342. $param_array = array();
  343. if (count($this->products) < $this->per_page) {
  344. $add = 0;
  345. } else {
  346. $add = $this->per_page;
  347. }
  348. $param_array['start'] = $this->start + $add;
  349. $param_array['end'] = $this->end + $add;
  350. if ($this->categorie != 0) {
  351. $param_array['categorie'] = $this->categorie;
  352. }
  353. $param = http_build_query($param_array);
  354. return $this->url."&".$param;
  355. }
  356. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  357. /**
  358. * version compare
  359. *
  360. * @param string $v1 version 1
  361. * @param string $v2 version 2
  362. * @return int result of compare
  363. */
  364. public function version_compare($v1, $v2)
  365. {
  366. // phpcs:enable
  367. $v1 = explode('.', $v1);
  368. $v2 = explode('.', $v2);
  369. $ret = 0;
  370. $level = 0;
  371. $count1 = count($v1);
  372. $count2 = count($v2);
  373. $maxcount = max($count1, $count2);
  374. while ($level < $maxcount) {
  375. $operande1 = isset($v1[$level]) ? $v1[$level] : 'x';
  376. $operande2 = isset($v2[$level]) ? $v2[$level] : 'x';
  377. $level++;
  378. if (strtoupper($operande1) == 'X' || strtoupper($operande2) == 'X' || $operande1 == '*' || $operande2 == '*') {
  379. break;
  380. }
  381. if ($operande1 < $operande2) {
  382. $ret = -$level;
  383. break;
  384. }
  385. if ($operande1 > $operande2) {
  386. $ret = $level;
  387. break;
  388. }
  389. }
  390. //print join('.',$versionarray1).'('.count($versionarray1).') / '.join('.',$versionarray2).'('.count($versionarray2).') => '.$ret.'<br>'."\n";
  391. return $ret;
  392. }
  393. }