dolistore.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. .'title="'.dol_escape_htmltag(strip_tags($cat->description->language[$this->lang - 1])).'"'
  191. .'>'.$cat->name->language[$this->lang - 1].' <sup>'.$cat->nb_products_recursive.'</sup></a></h3>';
  192. $html .= self::get_categories($cat->id);
  193. $html .= "</li>\n";
  194. } elseif (trim($cat->id_parent) == $parent && $cat->active == 1 && trim($cat->id_parent) != 0) { // si cat est de ce niveau
  195. $select = ($cat->id == $this->categorie) ? ' selected' : '';
  196. $html .= '<li><a class="link2cat'.$select.'" href="?mode=marketplace&categorie='.$cat->id.'"'
  197. .' title="'.dol_escape_htmltag(strip_tags($cat->description->language[$this->lang - 1])).'" '
  198. .'>'.$cat->name->language[$this->lang - 1].' <sup>'.$cat->nb_products_recursive.'</sup></a>';
  199. $html .= self::get_categories($cat->id);
  200. $html .= "</li>\n";
  201. } else {
  202. }
  203. }
  204. if ($html == '<ul>') {
  205. return '';
  206. }
  207. if ($parent != 0) {
  208. return $html.'</ul>';
  209. } else {
  210. return $html;
  211. }
  212. }
  213. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  214. /**
  215. * Return list of product formated for output
  216. *
  217. * @return string HTML output
  218. */
  219. public function get_products()
  220. {
  221. // phpcs:enable
  222. global $langs, $conf;
  223. $html = "";
  224. $parity = "pair";
  225. $last_month = time() - (30 * 24 * 60 * 60);
  226. foreach ($this->products as $product) {
  227. $parity = ($parity == "impair") ? 'pair' : 'impair';
  228. // check new product ?
  229. $newapp = '';
  230. if ($last_month < strtotime($product->date_add)) {
  231. $newapp .= '<span class="newApp">'.$langs->trans('New').'</span> ';
  232. }
  233. // check updated ?
  234. if ($last_month < strtotime($product->date_upd) && $newapp == '') {
  235. $newapp .= '<span class="updatedApp">'.$langs->trans('Updated').'</span> ';
  236. }
  237. // add image or default ?
  238. if ($product->id_default_image != '') {
  239. $image_url = DOL_URL_ROOT.'/admin/dolistore/ajax/image.php?id_product='.$product->id.'&id_image='.$product->id_default_image;
  240. $images = '<a href="'.$image_url.'" class="fancybox" rel="gallery'.$product->id.'" title="'.$product->name->language[$this->lang - 1].', '.$langs->trans('Version').' '.$product->module_version.'">'.
  241. '<img src="'.$image_url.'&quality=home_default" style="max-height:250px;max-width: 210px;" alt="" /></a>';
  242. } else {
  243. $images = '<img src="'.DOL_URL_ROOT.'/admin/dolistore/img/NoImageAvailable.png" />';
  244. }
  245. // free or pay ?
  246. if ($product->price > 0) {
  247. $price = '<h3>'.price(price2num($product->price, 'MT'), 0, $langs, 1, -1, -1, 'EUR').' '.$langs->trans("HT").'</h3>';
  248. $download_link = '<a target="_blank" href="'.$this->shop_url.$product->id.'"><img width="32" src="'.DOL_URL_ROOT.'/admin/dolistore/img/follow.png" /></a>';
  249. } else {
  250. $price = '<h3>'.$langs->trans('Free').'</h3>';
  251. $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>';
  252. $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>';
  253. }
  254. //checking versions
  255. if ($this->version_compare($product->dolibarr_min, DOL_VERSION) <= 0) {
  256. if ($this->version_compare($product->dolibarr_max, DOL_VERSION) >= 0) {
  257. //compatible
  258. $version = '<span class="compatible">'.$langs->trans('CompatibleUpTo', $product->dolibarr_max,
  259. $product->dolibarr_min, $product->dolibarr_max).'</span>';
  260. $compatible = '';
  261. } else {
  262. //never compatible, module expired
  263. $version = '<span class="notcompatible">'.$langs->trans('NotCompatible', DOL_VERSION,
  264. $product->dolibarr_min, $product->dolibarr_max).'</span>';
  265. $compatible = 'NotCompatible';
  266. }
  267. } else {
  268. //need update
  269. $version = '<span class="compatibleafterupdate">'.$langs->trans('CompatibleAfterUpdate', DOL_VERSION,
  270. $product->dolibarr_min, $product->dolibarr_max).'</span>';
  271. $compatible = 'NotCompatible';
  272. }
  273. //.'<br><a class="inline-block valignmiddle" target="_blank" href="'.$this->shop_url.$product->id.'"><span class="details button">'.$langs->trans("SeeInMarkerPlace").'</span></a>
  274. //output template
  275. $html .= '<tr class="app '.$parity.' '.$compatible.'">
  276. <td class="center" width="210"><div class="newAppParent">'.$newapp.$images.'</div></td>
  277. <td class="margeCote"><h2 class="appTitle">'.$product->name->language[$this->lang - 1]
  278. .'<br/><small>'.$version.'</small></h2>
  279. <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>
  280. <td style="display:none;" class="long_description">'.$product->description->language[$this->lang - 1].'</td>
  281. <td class="margeCote center">'.$price.'
  282. </td>
  283. <td class="margeCote">'.$download_link.'</td>
  284. </tr>';
  285. }
  286. return $html;
  287. }
  288. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  289. /**
  290. * get previous link
  291. *
  292. * @param string $text symbol previous
  293. * @return string html previous link
  294. */
  295. public function get_previous_link($text = '<<')
  296. {
  297. // phpcs:enable
  298. return '<a href="'.$this->get_previous_url().'" class="button">'.$text.'</a>';
  299. }
  300. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  301. /**
  302. * get next link
  303. *
  304. * @param string $text symbol next
  305. * @return string html next link
  306. */
  307. public function get_next_link($text = '>>')
  308. {
  309. // phpcs:enable
  310. return '<a href="'.$this->get_next_url().'" class="button">'.$text.'</a>';
  311. }
  312. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  313. /**
  314. * get previous url
  315. *
  316. * @return string previous url
  317. */
  318. public function get_previous_url()
  319. {
  320. // phpcs:enable
  321. $param_array = array();
  322. if ($this->start < $this->per_page) {
  323. $sub = 0;
  324. } else {
  325. $sub = $this->per_page;
  326. }
  327. $param_array['start'] = $this->start - $sub;
  328. $param_array['end'] = $this->end - $sub;
  329. if ($this->categorie != 0) {
  330. $param_array['categorie'] = $this->categorie;
  331. }
  332. $param = http_build_query($param_array);
  333. return $this->url."&".$param;
  334. }
  335. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  336. /**
  337. * get next url
  338. *
  339. * @return string next url
  340. */
  341. public function get_next_url()
  342. {
  343. // phpcs:enable
  344. $param_array = array();
  345. if (count($this->products) < $this->per_page) {
  346. $add = 0;
  347. } else {
  348. $add = $this->per_page;
  349. }
  350. $param_array['start'] = $this->start + $add;
  351. $param_array['end'] = $this->end + $add;
  352. if ($this->categorie != 0) {
  353. $param_array['categorie'] = $this->categorie;
  354. }
  355. $param = http_build_query($param_array);
  356. return $this->url."&".$param;
  357. }
  358. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  359. /**
  360. * version compare
  361. *
  362. * @param string $v1 version 1
  363. * @param string $v2 version 2
  364. * @return int result of compare
  365. */
  366. public function version_compare($v1, $v2)
  367. {
  368. // phpcs:enable
  369. $v1 = explode('.', $v1);
  370. $v2 = explode('.', $v2);
  371. $ret = 0;
  372. $level = 0;
  373. $count1 = count($v1);
  374. $count2 = count($v2);
  375. $maxcount = max($count1, $count2);
  376. while ($level < $maxcount) {
  377. $operande1 = isset($v1[$level]) ? $v1[$level] : 'x';
  378. $operande2 = isset($v2[$level]) ? $v2[$level] : 'x';
  379. $level++;
  380. if (strtoupper($operande1) == 'X' || strtoupper($operande2) == 'X' || $operande1 == '*' || $operande2 == '*') {
  381. break;
  382. }
  383. if ($operande1 < $operande2) {
  384. $ret = -$level;
  385. break;
  386. }
  387. if ($operande1 > $operande2) {
  388. $ret = $level;
  389. break;
  390. }
  391. }
  392. //print join('.',$versionarray1).'('.count($versionarray1).') / '.join('.',$versionarray2).'('.count($versionarray2).') => '.$ret.'<br>'."\n";
  393. return $ret;
  394. }
  395. }