Fix search in dolistore

This commit is contained in:
Laurent Destailleur 2019-03-31 13:03:38 +02:00
parent 9f0d712957
commit b10682e5db
2 changed files with 58 additions and 30 deletions

View File

@ -63,7 +63,7 @@ class Dolistore
$langtmp = explode('_', $langs->defaultlang); $langtmp = explode('_', $langs->defaultlang);
$lang = $langtmp[0]; $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'; if (! in_array($lang, array_keys($lang_array))) $lang = 'en';
$this->lang = $lang_array[$lang]; $this->lang = $lang_array[$lang];
} }
@ -72,10 +72,48 @@ class Dolistore
* Load data from remote Dolistore market place. * Load data from remote Dolistore market place.
* This fills ->categories * This fills ->categories
* *
* @param array $options Options
* @return void * @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.'<br>';
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; global $conf, $langs;
@ -101,25 +139,26 @@ class Dolistore
// make a search to limit the id returned. // make a search to limit the id returned.
if ($this->search != '') { 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 // 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)); dol_syslog("Call API with opt2 = ".var_export($opt2, true));
$xml = $this->api->get($opt2); $xml = $this->api->get($opt2);
$products = array(); $products = array();
foreach ($xml->products->children() as $product) { foreach ($xml->products->children() as $product) {
$products[] = (int) $product['id']; $products[] = (int) $product['id'];
} }
$opt['filter[id]'] = '['.implode('|', $products).']'; $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['resource'] = 'categories';
$opt2['id'] = $this->categorie; $opt2['id'] = $this->categorie;
// Call // Call
dol_syslog("Call API with opt2 = ".var_export($opt2, true));
$xml = $this->api->get($opt2); $xml = $this->api->get($opt2);
$products = array(); $products = array();
foreach ($xml->category->associations->products->children() as $product) { foreach ($xml->category->associations->products->children() as $product) {
$products[] = (int) $product->id; $products[] = (int) $product->id;
@ -132,22 +171,10 @@ class Dolistore
$opt['limit'] = "$this->start,$this->end"; $opt['limit'] = "$this->start,$this->end";
// $opt['filter[id]'] contais list of product id that are result of search // $opt['filter[id]'] contais list of product id that are result of search
// Call API to get the detail // Call API to get the detail
dol_syslog("Call API with opt = ".var_export($opt, true)); 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); $xml = $this->api->get($opt);
$this->products = $xml->products->children(); $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) { } catch (PrestaShopWebserviceException $e) {
// Here we are dealing with errors // Here we are dealing with errors
$trace = $e->getTrace(); $trace = $e->getTrace();
@ -184,15 +211,15 @@ class Dolistore
$cat = $this->categories[$i]; $cat = $this->categories[$i];
if ($cat->is_root_category == 1 && $parent == 0) { if ($cat->is_root_category == 1 && $parent == 0) {
$html .= '<li class="root"><h3 class="nomargesupinf"><a class="nomargesupinf link2cat" href="?mode=marketplace&categorie='.$cat->id.'" ' $html .= '<li class="root"><h3 class="nomargesupinf"><a class="nomargesupinf link2cat" href="?mode=marketplace&categorie='.$cat->id.'" '
.'title="'.dol_escape_htmltag(strip_tags($cat->description->language[$this->lang])).'"' .'title="'.dol_escape_htmltag(strip_tags($cat->description->language[$this->lang - 1])).'"'
.'>'.$cat->name->language[$this->lang].' <sup>'.$cat->nb_products_recursive.'</sup></a></h3>'; .'>'.$cat->name->language[$this->lang - 1].' <sup>'.$cat->nb_products_recursive.'</sup></a></h3>';
$html .= self::get_categories($cat->id); $html .= self::get_categories($cat->id);
$html .= "</li>\n"; $html .= "</li>\n";
} elseif (trim($cat->id_parent) == $parent && $cat->active == 1 && trim($cat->id_parent) != 0) { // si cat est de ce niveau } 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' : ''; $select = ($cat->id == $this->categorie) ? ' selected' : '';
$html .= '<li><a class="link2cat'.$select.'" href="?mode=marketplace&categorie='.$cat->id.'"' $html .= '<li><a class="link2cat'.$select.'" href="?mode=marketplace&categorie='.$cat->id.'"'
.' title="'.dol_escape_htmltag(strip_tags($cat->description->language[$this->lang])).'" ' .' title="'.dol_escape_htmltag(strip_tags($cat->description->language[$this->lang - 1])).'" '
.'>'.$cat->name->language[$this->lang].' <sup>'.$cat->nb_products_recursive.'</sup></a>'; .'>'.$cat->name->language[$this->lang - 1].' <sup>'.$cat->nb_products_recursive.'</sup></a>';
$html .= self::get_categories($cat->id); $html .= self::get_categories($cat->id);
$html .= "</li>\n"; $html .= "</li>\n";
} else { } else {
@ -240,7 +267,7 @@ class Dolistore
// add image or default ? // add image or default ?
if ($product->id_default_image != '') { 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; $image_url = DOL_URL_ROOT.'/admin/dolistore/ajax/image.php?id_product='.$product->id.'&id_image='.$product->id_default_image;
$images = '<a href="'.$image_url.'" class="fancybox" rel="gallery'.$product->id.'" title="'.$product->name->language[$this->lang].', '.$langs->trans('Version').' '.$product->module_version.'">'. $images = '<a href="'.$image_url.'" class="fancybox" rel="gallery'.$product->id.'" title="'.$product->name->language[$this->lang - 1].', '.$langs->trans('Version').' '.$product->module_version.'">'.
'<img src="'.$image_url.'&quality=home_default" style="max-height:250px;max-width: 210px;" alt="" /></a>'; '<img src="'.$image_url.'&quality=home_default" style="max-height:250px;max-width: 210px;" alt="" /></a>';
} else { } else {
$images = '<img src="'.DOL_URL_ROOT.'/admin/dolistore/img/NoImageAvailable.png" />'; $images = '<img src="'.DOL_URL_ROOT.'/admin/dolistore/img/NoImageAvailable.png" />';
@ -281,10 +308,10 @@ class Dolistore
//output template //output template
$html .= '<tr class="app '.$parity.' '.$compatible.'"> $html .= '<tr class="app '.$parity.' '.$compatible.'">
<td class="center" width="210"><div class="newAppParent">'.$newapp.$images.'</div></td> <td class="center" width="210"><div class="newAppParent">'.$newapp.$images.'</div></td>
<td class="margeCote"><h2 class="appTitle">'.$product->name->language[$this->lang] <td class="margeCote"><h2 class="appTitle">'.$product->name->language[$this->lang - 1]
.'<br/><small>'.$version.'</small></h2> .'<br/><small>'.$version.'</small></h2>
<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> <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>
<td style="display:none;" class="long_description">'.$product->description->language[$this->lang].'</td> <td style="display:none;" class="long_description">'.$product->description->language[$this->lang - 1].'</td>
<td class="margeCote center">'.$price.' <td class="margeCote center">'.$price.'
</td> </td>
<td class="margeCote">'.$download_link.'</td> <td class="margeCote">'.$download_link.'</td>

View File

@ -888,7 +888,8 @@ if ($mode == 'marketplace')
{ {
// $options is array with filter criterias // $options is array with filter criterias
//var_dump($options); //var_dump($options);
$dolistore->getRemoteData($options); $dolistore->getRemoteCategories();
$dolistore->getRemoteProducts($options);
print '<span class="opacitymedium">'.$langs->trans('DOLISTOREdescriptionLong').'</span><br><br>'; print '<span class="opacitymedium">'.$langs->trans('DOLISTOREdescriptionLong').'</span><br><br>';