Jump to content

Accessories bug & patch


Recommended Posts

Hi,

I just found a bug in the product informations admin page. The script that populates the accessories' select is useless on large catalogs. I'm opening a ticket about this right now, but I need a place to propose a solution, so I'm posting here too ;)

Edit: details here : http://www.prestashop.com/bug_tracker/view/3194/

/admin/tabs/AdminProducts.php - line 1913

$(function() {
                 var filter = $("input[name=filter]");
                 var select = $("#selectAccessories");
                 filter.change(function(event) {
                   var value = filter.val();
                   if (value.length > 2)
                   {
                     $.post("'.dirname($currentIndex).'/ajax.php", { ajaxProductAccessories: 1, id_lang: '.intval($cookie->id_lang).', id_product: '.($obj->id ? intval($obj->id) : 0).', search: value },
                     function (data) {
                     alert(data.length);
                       select.children("option").remove();

                       if (data.length == 0)
                       {
                         select.append($("'.$this->l('No match').'"));
                       }
                       else
                       {
                         for (var i = 0; i < data.length; i++)
                         {
                           select.append($(""+data[i].text+""));
                         }
                       }
                     }, "json")
                   }
                 })
               })



/admin/ajax.php - line 34

if (isset($_POST['ajaxProductAccessories']))
{
   $currentIndex = 'index.php?tab=AdminCatalog';
   $jsonArray = array();
 $search = mysql_real_escape_string($_POST['search']);

   $products = Db::getInstance()->ExecuteS('
   SELECT p.`id_product`, pl.`name`
   FROM `'._DB_PREFIX_.'product` p
   LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (pl.id_product = p.id_product AND pl.name LIKE "%'.$search.'%")
   WHERE pl.`id_lang` = '.intval(Tools::getValue('id_lang')).'
   AND p.`id_product` != '.intval(Tools::getValue('id_product')).'
   AND p.`id_product` NOT IN (
       SELECT a.`id_product_2`
       FROM `'._DB_PREFIX_.'accessory` a
       WHERE a.`id_product_1` = '.intval(Tools::getValue('id_product')).')');
   foreach ($products AS $accessory)
       $jsonArray[] = '{value: \''.intval($accessory['id_product']).'-'.addslashes($accessory['name']).'\', text:\''.intval($accessory['id_product']).' - '.addslashes($accessory['name']).'\'}';
   die('['.implode(',', $jsonArray).']');
}

Link to comment
Share on other sites

  • 3 weeks later...
  • 4 months later...

Bon j'ai pas réussi à faire fonctionner le code ci-dessus. Par contre il m'a été bien utile pour débeuguer le code initial avec les lignes suivantes (en gras dans le code quoté) :
Dans ajax.php remplacer entre les lignes 34 et 55 environ par :

if (isset($_GET['ajaxProductAccessories']))
{
$currentIndex = 'index.php?tab=AdminCatalog';
$jsonArray = array();
$search = mysql_real_escape_string($_GET['search']);

$products = Db::getInstance()->ExecuteS('
SELECT p.`id_product`, pl.`name`
FROM `'._DB_PREFIX_.'product` p
NATURAL LEFT JOIN `'._DB_PREFIX_.'product_lang` pl
WHERE pl.`id_lang` = '.intval(Tools::getValue('id_lang')).'
AND p.`id_product` != '.intval(Tools::getValue('id_product')).'
AND pl.name LIKE "%'.$search.'%"
AND p.`id_product` NOT IN (
SELECT a.`id_product_2`
FROM `'._DB_PREFIX_.'accessory` a
WHERE a.`id_product_1` = '.intval(Tools::getValue('id_product')).')');

foreach ($products AS $accessory)
$jsonArray[] = '{value: \''.intval($accessory['id_product']).'-'.addslashes($accessory['name']).'\', text:\''.intval($accessory['id_product']).' - '.addslashes($accessory['name']).'\'}';
die('['.implode(',', $jsonArray).']');
}

et sur AdminProducts.php entre les lignes 1917 et 1953 environ par :
function fillAccessories()
{
var filter = $("input[name=filter]");
var value = filter.val();

$.getJSON("'.dirname($currentIndex).'/ajax.php",{ajaxProductAccessories:1,id_lang:'.intval($cookie->id_lang).',id_product:'.($obj->id ? intval($obj->id) : 0).', search: value},
function(j)
{
for (var i = 0; i < j.length; i++)
accessories = new Array(j.value, j.text);

formProduct = document.layers ? document.forms.product : document.product;
formProduct.selectAccessories.length = accessories.length + 1;
for (i = 0, j = 1; i < accessories.length; i++)
{
if (formProduct.filter.value)
if (accessories[1].toLowerCase().indexOf(formProduct.filter.value.toLowerCase()) == -1)
continue;
formProduct.selectAccessories.options[j].value = accessories[0];
formProduct.selectAccessories.options[j].text = accessories[1];
j++;
}
if (j == 1)
{
formProduct.selectAccessories.length = 2;
formProduct.selectAccessories.options[1].value = -1;
formProduct.selectAccessories.options[1].text = \''.$this->l('No match found').'\';
formProduct.selectAccessories.options.selectedIndex = 1;
}
else
{
formProduct.selectAccessories.length = j;
formProduct.selectAccessories.options.selectedIndex = (formProduct.filter.value == \'\' ? 0 : 1);
}
}
);

}
Link to comment
Share on other sites

the same in code style :

if (isset($_GET['ajaxProductAccessories']))
{
   $currentIndex = 'index.php?tab=AdminCatalog';
   $jsonArray = array();
   $search = mysql_real_escape_string($_GET['search']);

   $products = Db::getInstance()->ExecuteS('
   SELECT p.`id_product`, pl.`name`
   FROM `'._DB_PREFIX_.'product` p
   NATURAL LEFT JOIN `'._DB_PREFIX_.'product_lang` pl
   WHERE pl.`id_lang` = '.intval(Tools::getValue('id_lang')).'
   AND p.`id_product` != '.intval(Tools::getValue('id_product')).'
   AND pl.name LIKE "%'.$search.'%" 
   AND p.`id_product` NOT IN (
       SELECT a.`id_product_2`
       FROM `'._DB_PREFIX_.'accessory` a
       WHERE a.`id_product_1` = '.intval(Tools::getValue('id_product')).')');

   foreach ($products AS $accessory)
       $jsonArray[] = '{value: \''.intval($accessory['id_product']).'-'.addslashes($accessory['name']).'\', text:\''.intval($accessory['id_product']).' - '.addslashes($accessory['name']).'\'}';
   die('['.implode(',', $jsonArray).']');
} 


function fillAccessories()
                               {
                                   var filter = $("input[name=filter]");
                                   var value = filter.val();

                                   $.getJSON("'.dirname($currentIndex).'/ajax.php",{ajaxProductAccessories:1,id_lang:'.intval($cookie->id_lang).',id_product:'.($obj->id ? intval($obj->id) : 0).', search: value},
                                       function(j)
                                       {
                                           for (var i = 0; i < j.length; i++)
                                               accessories[i] = new Array(j[i].value, j[i].text);

                                           formProduct = document.layers ? document.forms.product : document.product;
                                           formProduct.selectAccessories.length = accessories.length + 1;
                                           for (i = 0, j = 1; i < accessories.length; i++)
                                           {
                                               if (formProduct.filter.value)
                                                   if (accessories[i][1].toLowerCase().indexOf(formProduct.filter.value.toLowerCase()) == -1)
                                                       continue;
                                               formProduct.selectAccessories.options[j].value = accessories[i][0];
                                               formProduct.selectAccessories.options[j].text = accessories[i][1];
                                               j++;
                                           }
                                           if (j == 1)
                                           {
                                               formProduct.selectAccessories.length = 2;
                                               formProduct.selectAccessories.options[1].value = -1;
                                               formProduct.selectAccessories.options[1].text = \''.$this->l('No match found').'\';
                                               formProduct.selectAccessories.options.selectedIndex = 1;
                                           }
                                           else
                                           {
                                               formProduct.selectAccessories.length = j;
                                               formProduct.selectAccessories.options.selectedIndex = (formProduct.filter.value == \'\' ? 0 : 1);
                                           }
                                       }
                                   );

                               }

Link to comment
Share on other sites

×
×
  • Create New...