. * Copyright (C) 2024 MDW * * This program is free software; you can redistribute it and/or modifyion 2.0 (the "License"); * it under the terms of the GNU General Public License as published bypliance with the License. * the Free Software Foundation; either version 3 of the License, or * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * or see https://www.gnu.org/ */ include_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php'; 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. include_once DOL_DOCUMENT_ROOT.'/admin/remotestore/class/PSWebServiceLibrary.class.php'; } /** * Class Dolistore */ class Dolistore { /** * beginning of pagination * @var int */ public $start; /** * end of pagination * @var int */ public $end; /** * @var int Pagination: display per page */ public $per_page; /** * @var int The current categorie */ public $categorie; /** * @var ?SimpleXMLElement */ public $categories; // an array of categories /** * @var string The search keywords */ public $search; // setups /** * @var string */ public $url; // the url of this page /** * @var string */ public $shop_url; // the url of the shop /** * @var int */ public $lang; // the integer representing the lang in the store /** * @var bool */ public $debug_api; // useful if no dialog /** * @var PrestaShopWebservice */ public $api; /** * @var ?SimpleXMLElement */ public $products; /** * Constructor * * @param boolean $debug Enable debug of request on screen */ public function __construct($debug = false) { global $langs; $this->url = DOL_URL_ROOT.'/admin/modules.php?mode=marketplace'; $this->shop_url = 'https://www.dolistore.com/index.php?controller=product&id_product='; $this->debug_api = $debug; $langtmp = explode('_', $langs->defaultlang); $lang = $langtmp[0]; $lang_array = array('en' => 1, 'fr' => 2, 'es' => 3, 'it' => 4, 'de' => 5); // Into table ps_lang of Prestashop - 1 if (!in_array($lang, array_keys($lang_array))) { $lang = 'en'; } $this->lang = $lang_array[$lang]; } /** * Load data from remote Dolistore market place. * This fills ->categories * * @return void */ public function getRemoteCategories() { global $conf; try { $this->api = new PrestaShopWebservice(getDolGlobalString('MAIN_MODULE_DOLISTORE_API_SRV'), getDolGlobalString('MAIN_MODULE_DOLISTORE_API_KEY'), $this->debug_api); dol_syslog("Call API with MAIN_MODULE_DOLISTORE_API_SRV = ".getDolGlobalString('MAIN_MODULE_DOLISTORE_API_SRV')); // conf MAIN_MODULE_DOLISTORE_API_KEY is for the login of basic auth. There is no password as it is public data. // Here we set the option array for the Webservice : we want categories resources $opt = array(); $opt['resource'] = 'categories'; $opt['display'] = '[id,id_parent,nb_products_recursive,active,is_root_category,name,description]'; $opt['sort'] = 'id_asc'; // Call dol_syslog("Call API with opt = ".var_export($opt, true)); $xml = $this->api->get($opt); // @phan-suppress-next-line PhanPluginUnknownObjectMethodCall $this->categories = $xml->categories->children(); } catch (PrestaShopWebserviceException $e) { // Here we are dealing with errors $trace = $e->getTrace(); if ($trace[0]['args'][0] == 404) { die('Bad ID'); } elseif ($trace[0]['args'][0] == 401) { die('Bad auth key'); } else { print 'Can not access to ' . getDolGlobalString('MAIN_MODULE_DOLISTORE_API_SRV').'
'; print $e->getMessage(); } } } /** * Load data from remote Dolistore market place. * This fills ->products * * @param array{start:int,end:int,per_page:int,categorie:int,search:string} $options Options. If 'categorie' is defined, we filter products on this category id * @return void */ public function getRemoteProducts($options = array('start' => 0, 'end' => 10, 'per_page' => 50, 'categorie' => 0, 'search' => '')) { global $conf; $this->start = $options['start']; $this->end = $options['end']; $this->per_page = $options['per_page']; $this->categorie = $options['categorie']; $this->search = $options['search']; if ($this->end == 0) { $this->end = $this->per_page; } try { $this->api = new PrestaShopWebservice(getDolGlobalString('MAIN_MODULE_DOLISTORE_API_SRV'), getDolGlobalString('MAIN_MODULE_DOLISTORE_API_KEY'), $this->debug_api); dol_syslog("Call API with MAIN_MODULE_DOLISTORE_API_SRV = ".getDolGlobalString('MAIN_MODULE_DOLISTORE_API_SRV')); // conf MAIN_MODULE_DOLISTORE_API_KEY is for the login of basic auth. There is no password as it is public data. // Here we set the option array for the Webservice : we want products resources $opt = array(); $opt['resource'] = 'products'; $opt2 = array(); // make a search to limit the id returned. if ($this->search != '') { $opt2['url'] = getDolGlobalString('MAIN_MODULE_DOLISTORE_API_SRV') . '/api/search?query='.$this->search.'&language='.$this->lang; // It seems for search, key start with // Call dol_syslog("Call API with opt2 = ".var_export($opt2, true)); $xml = $this->api->get($opt2); $products = array(); // @phan-suppress-next-line PhanPluginUnknownObjectMethodCall foreach ($xml->products->children() as $product) { $products[] = (int) $product['id']; } $opt['filter[id]'] = '['.implode('|', $products).']'; } elseif ($this->categorie != 0) { // We filter on category, so we first get list of product id in this category // $opt2['url'] is set by default to $this->url.'/api/'.$options['resource']; $opt2['resource'] = 'categories'; $opt2['id'] = $this->categorie; // Call dol_syslog("Call API with opt2 = ".var_export($opt2, true)); $xml = $this->api->get($opt2); $products = array(); // @phan-suppress-next-line PhanPluginUnknownObjectMethodCall foreach ($xml->category->associations->products->children() as $product) { $products[] = (int) $product->id; } $opt['filter[id]'] = '['.implode('|', $products).']'; } $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]'; $opt['sort'] = 'id_desc'; $opt['filter[active]'] = '[1]'; $opt['limit'] = "$this->start,$this->end"; // $opt['filter[id]'] contains list of product id that are result of search // Call API to get the detail dol_syslog("Call API with opt = ".var_export($opt, true)); $xml = $this->api->get($opt); // @phan-suppress-next-line PhanPluginUnknownObjectMethodCall $this->products = $xml->products->children(); } catch (PrestaShopWebserviceException $e) { // Here we are dealing with errors $trace = $e->getTrace(); if ($trace[0]['args'][0] == 404) { die('Bad ID'); } elseif ($trace[0]['args'][0] == 401) { die('Bad auth key'); } else { print 'Can not access to ' . getDolGlobalString('MAIN_MODULE_DOLISTORE_API_SRV').'
'; print $e->getMessage(); } } } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * Return tree of Dolistore categories. $this->categories must have been loaded before. * * @param int $parent Id of parent category * @return string */ public function get_categories($parent = 0) { // phpcs:enable if (!isset($this->categories)) { die('not possible'); } if ($parent != 0) { $html = '