ddurieux / glpi_physicalinv

Plugin GLPI used to manage the physical inventory of devices
GNU Affero General Public License v3.0
6 stars 4 forks source link

when using with Generic Object plugin - PHP fatal error #3

Open Rafit77 opened 7 years ago

Rafit77 commented 7 years ago

when try to search anything, nothing shows, and apache log error:

PHP Fatal error: Call to a member function fetch_array() on boolean in /var/www/html/inc/dbmysql.class.php on line 282, referer: http ://xx.xx.xx.xx/plugins/physicalinv/front/inventory.php

problem: file: /plugins/physicalinv/inc/inventory.class.php

code:

         $result = $DB->query($query);
         while ($data = $DB->fetch_array($result)) {
            if ($item->canEdit($data['id'])) {
               $id_list[$itemtype][$data['id']] = $data['id'];
            }
         }

fixed code:

         $result = $DB->query($query);
         if ($DB->numrows($result)>0) {
             while ($data = $DB->fetch_array($result)) {
                if ($item->canEdit($data['id'])) {
                   $id_list[$itemtype][$data['id']] = $data['id'];
                }
             }
         }

I hope this helps (it worked for me).

EDIT: I investigated further, using Generic Object plugin caused error. To make both plugins work together few more changes need to be done. Reason: not all object need to have dustbin and template fields.

   /**
    * Search and get list of devices have the 'number' in serial number or
    * inventory number
    *
    * @param string $number
    * @return array
    */
   function searchItemWithNumber($number) {
      global $DB, $CFG_GLPI;

      $id_list = array();

      // search in inventory have serial number or inventory number
      foreach($CFG_GLPI["asset_types"] as $itemtype) {
         $where_fields = array();
         $table = getTableForItemType($itemtype);
         $item = new $itemtype();

         if (FieldExists($table, 'serial')) {
            $where_fields[] = 'serial';
         }
         if (FieldExists($table, 'otherserial')) {
            $where_fields[] = 'otherserial';
         }
         if (count($where_fields) == 0) {
            continue;
         }
         $query = "SELECT *
                   FROM `".$table."` WHERE (";
         $first = True;
         foreach ($where_fields as $field) {
            if (!$first) {
               $query .= " OR ";
            }
            $query .= " `$field`='$number'";
            $first = False;
         }
         $query .= ")";
         if (FieldExists($table, 'is_deleted')) { // only if dust bin enabled
            $query .= " AND `is_deleted`='0'";
         }
         if (FieldExists($table, 'is_template')) { // only if templates are in use
            $query .= " AND `is_template`='0'";
         }

         $result = $DB->query($query);
         if ($DB->numrows($result)>0) {
             while ($data = $DB->fetch_array($result)) {
                if ($item->canEdit($data['id'])) {
                   $id_list[$itemtype][$data['id']] = $data['id'];
                }
             }
         }
      }
      return $id_list;
   }

Regards Rafal

Rafit77 commented 5 years ago

The problem should be resolved with provided pull request.