diff --git a/htdocs/admin/dolistore/class/dolistore.class.php b/htdocs/admin/dolistore/class/dolistore.class.php index 26b9bcc8918..daa8620b783 100644 --- a/htdocs/admin/dolistore/class/dolistore.class.php +++ b/htdocs/admin/dolistore/class/dolistore.class.php @@ -63,7 +63,7 @@ class Dolistore $langtmp = explode('_', $langs->defaultlang); $lang = $langtmp[0]; - $lang_array = array('en'=>0, 'fr'=>1, 'es'=>2, 'it'=>3, 'de'=>4); // Into table ps_lang of Prestashop - 1 + $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]; } @@ -72,10 +72,48 @@ class Dolistore * Load data from remote Dolistore market place. * This fills ->categories * - * @param array $options Options * @return void */ - public function getRemoteData($options = array('start' => 0, 'end' => 10, 'per_page' => 50, 'categorie' => 0)) + public function getRemoteCategories() + { + global $conf; + + try { + $this->api = new PrestaShopWebservice($conf->global->MAIN_MODULE_DOLISTORE_API_SRV, $conf->global->MAIN_MODULE_DOLISTORE_API_KEY, $this->debug_api); + dol_syslog("Call API with MAIN_MODULE_DOLISTORE_API_SRV = ".$conf->global->MAIN_MODULE_DOLISTORE_API_SRV); + // $conf->global->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); + $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 '.$conf->global->MAIN_MODULE_DOLISTORE_API_SRV.'
'; + print $e->getMessage(); + } + } + } + + /** + * Load data from remote Dolistore market place. + * This fills ->products + * + * @param array $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, $langs; @@ -101,25 +139,26 @@ class Dolistore // make a search to limit the id returned. if ($this->search != '') { - $opt2['url'] = $conf->global->MAIN_MODULE_DOLISTORE_API_SRV.'/api/search?query='.$this->search.'&language='.$this->lang; + $opt2['url'] = $conf->global->MAIN_MODULE_DOLISTORE_API_SRV.'/api/search?query='.$this->search.'&language='.($this->lang); // It seems for search, key start with // Call - //var_dump($this->api); - - dol_syslog("Call API with opt = ".var_export($opt, true)); dol_syslog("Call API with opt2 = ".var_export($opt2, true)); - $xml = $this->api->get($opt2); + $products = array(); foreach ($xml->products->children() as $product) { $products[] = (int) $product['id']; } $opt['filter[id]'] = '['.implode('|', $products).']'; - } elseif ($this->categorie != 0) { + } 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(); foreach ($xml->category->associations->products->children() as $product) { $products[] = (int) $product->id; @@ -132,22 +171,10 @@ class Dolistore $opt['limit'] = "$this->start,$this->end"; // $opt['filter[id]'] contais 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)); - dol_syslog("Call API with opt2 = ".var_export($opt2, true)); $xml = $this->api->get($opt); $this->products = $xml->products->children(); - - - // 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 - $xml = $this->api->get($opt); - $this->categories = $xml->categories->children(); } catch (PrestaShopWebserviceException $e) { // Here we are dealing with errors $trace = $e->getTrace(); @@ -184,15 +211,15 @@ class Dolistore $cat = $this->categories[$i]; if ($cat->is_root_category == 1 && $parent == 0) { $html .= '
  • '.$cat->name->language[$this->lang].' '.$cat->nb_products_recursive.'

    '; + .'title="'.dol_escape_htmltag(strip_tags($cat->description->language[$this->lang - 1])).'"' + .'>'.$cat->name->language[$this->lang - 1].' '.$cat->nb_products_recursive.''; $html .= self::get_categories($cat->id); $html .= "
  • \n"; } elseif (trim($cat->id_parent) == $parent && $cat->active == 1 && trim($cat->id_parent) != 0) { // si cat est de ce niveau $select = ($cat->id == $this->categorie) ? ' selected' : ''; $html .= '
  • '.$cat->name->language[$this->lang].' '.$cat->nb_products_recursive.''; + .' title="'.dol_escape_htmltag(strip_tags($cat->description->language[$this->lang - 1])).'" ' + .'>'.$cat->name->language[$this->lang - 1].' '.$cat->nb_products_recursive.''; $html .= self::get_categories($cat->id); $html .= "
  • \n"; } else { @@ -240,7 +267,7 @@ class Dolistore // add image or default ? if ($product->id_default_image != '') { $image_url = DOL_URL_ROOT.'/admin/dolistore/ajax/image.php?id_product='.$product->id.'&id_image='.$product->id_default_image; - $images = ''. + $images = ''. ''; } else { $images = ''; @@ -281,10 +308,10 @@ class Dolistore //output template $html .= '
    '.$newapp.$images.'
    -

    '.$product->name->language[$this->lang] +

    '.$product->name->language[$this->lang - 1] .'
    '.$version.'

    - '.dol_print_date(dol_stringtotime($product->date_upd), 'dayhour').' - '.$langs->trans('Ref').': '.$product->reference.' - '.$langs->trans('Id').': '.$product->id.'

    '.$product->description_short->language[$this->lang].' - '.$product->description->language[$this->lang].' + '.dol_print_date(dol_stringtotime($product->date_upd), 'dayhour').' - '.$langs->trans('Ref').': '.$product->reference.' - '.$langs->trans('Id').': '.$product->id.'

    '.$product->description_short->language[$this->lang - 1].' + '.$product->description->language[$this->lang - 1].' '.$price.' '.$download_link.' diff --git a/htdocs/admin/modules.php b/htdocs/admin/modules.php index c735362d87f..d06f5d393d9 100644 --- a/htdocs/admin/modules.php +++ b/htdocs/admin/modules.php @@ -888,7 +888,8 @@ if ($mode == 'marketplace') { // $options is array with filter criterias //var_dump($options); - $dolistore->getRemoteData($options); + $dolistore->getRemoteCategories(); + $dolistore->getRemoteProducts($options); print ''.$langs->trans('DOLISTOREdescriptionLong').'

    ';