getCommonLeftJoin() static public method

static public getCommonLeftJoin ( )
Example #1
0
 static function pdfForItem(PluginPdfSimplePDF $pdf, CommonDBTM $item, $tree = false)
 {
     global $DB, $CFG_GLPI;
     $ID = $item->getField('id');
     $type = $item->getType();
     if (!Session::haveRight("show_all_ticket", "1")) {
         return;
     }
     switch ($item->getType()) {
         case 'User':
             $restrict = "(`glpi_tickets_users`.`users_id` = '" . $item->getID() . "'\n                            AND `glpi_tickets_users`.`type` = " . CommonITILActor::REQUESTER . ")";
             $order = '`glpi_tickets`.`date_mod` DESC';
             break;
         case 'SLA':
             $restrict = "(`slas_id` = '" . $item->getID() . "')";
             $order = '`glpi_tickets`.`due_date` DESC';
             break;
         case 'Supplier':
             $restrict = "(`glpi_suppliers_tickets`.`suppliers_id` = '" . $item->getID() . "'\n                           AND `glpi_suppliers_tickets`.`type` = " . CommonITILActor::ASSIGN . ")";
             $order = '`glpi_tickets`.`date_mod` DESC';
             break;
         case 'Group':
             if ($tree) {
                 $restrict = "IN (" . implode(',', getSonsOf('glpi_groups', $item->getID())) . ")";
             } else {
                 $restrict = "='" . $item->getID() . "'";
             }
             $restrict = "(`glpi_groups_tickets`.`groups_id` {$restrict}\n                            AND `glpi_groups_tickets`.`type` = " . CommonITILActor::REQUESTER . ")";
             $order = '`glpi_tickets`.`date_mod` DESC';
             break;
         default:
             $restrict = "(`items_id` = '" . $item->getID() . "'  AND `itemtype` = '{$type}')";
             $order = '`glpi_tickets`.`date_mod` DESC';
     }
     $query = "SELECT " . Ticket::getCommonSelect() . "\n                FROM glpi_tickets " . Ticket::getCommonLeftJoin() . "\n                WHERE {$restrict} " . getEntitiesRestrictRequest("AND", "glpi_tickets") . "\n                ORDER BY {$order}\n                LIMIT " . intval($_SESSION['glpilist_limit']);
     $result = $DB->query($query);
     $number = $DB->numrows($result);
     $pdf->setColumnsSize(100);
     if (!$number) {
         $pdf->displayTitle('<b>' . __('Last tickets') . '</b>');
     } else {
         $pdf->displayTitle("<b>" . sprintf(__('Last %d ticket') . "</b>", $number));
         $job = new Ticket();
         while ($data = $DB->fetch_assoc($result)) {
             if (!$job->getFromDB($data["id"])) {
                 continue;
             }
             $pdf->setColumnsAlign('center');
             $col = '<b><i>ID ' . $job->fields["id"] . '</i></b>, ' . sprintf(__('%1$s: %2$s'), __('Status'), Ticket::getStatus($job->fields["status"]));
             if (count($_SESSION["glpiactiveentities"]) > 1) {
                 if ($job->fields['entities_id'] == 0) {
                     $col = sprintf(__('%1$s (%2$s)'), $col, __('Root entity'));
                 } else {
                     $col = sprintf(__('%1$s (%2$s)'), $col, Dropdown::getDropdownName("glpi_entities", $job->fields['entities_id']));
                 }
             }
             $pdf->displayLine($col);
             $pdf->setColumnsAlign('left');
             $col = '<b><i>' . sprintf(__('Opened on %s') . '</i></b>', Html::convDateTime($job->fields['date']));
             if ($job->fields['begin_waiting_date']) {
                 $col = sprintf(__('%1$s, %2$s'), $col, '<b><i>' . sprintf(__('Put on hold on %s') . '</i></b>', Html::convDateTime($job->fields['begin_waiting_date'])));
             }
             if (in_array($job->fields["status"], $job->getSolvedStatusArray()) || in_array($job->fields["status"], $job->getClosedStatusArray())) {
                 $col = sprintf(__('%1$s, %2$s'), $col, '<b><i>' . sprintf(__('Solved on %s') . '</i></b>', Html::convDateTime($job->fields['solvedate'])));
             }
             if (in_array($job->fields["status"], $job->getClosedStatusArray())) {
                 $col = sprintf(__('%1$s, %2$s'), $col, '<b><i>' . sprintf(__('Closed on %s') . '</i></b>', Html::convDateTime($job->fields['closedate'])));
             }
             if ($job->fields['due_date']) {
                 $col = sprintf(__('%1$s, %2$s'), $col, '<b><i>' . sprintf(__('%1$s: %2$s') . '</i></b>', __('Due date'), Html::convDateTime($job->fields['due_date'])));
             }
             $pdf->displayLine($col);
             $col = '<b><i>' . sprintf(__('%1$s: %2$s'), __('Priority') . '</i></b>', Ticket::getPriorityName($job->fields["priority"]));
             if ($job->fields["itilcategories_id"]) {
                 $col = sprintf(__('%1$s - %2$s'), $col, '<b><i>' . sprintf(__('%1$s: %2$s') . '</i></b>', __('Category'), Dropdown::getDropdownName('glpi_itilcategories', $job->fields["itilcategories_id"])));
             }
             $pdf->displayLine($col);
             $col = '';
             $users = $job->getUsers(CommonITILActor::REQUESTER);
             if (count($users)) {
                 foreach ($users as $d) {
                     if (empty($col)) {
                         $col = getUserName($d['users_id']);
                     } else {
                         $col = sprintf(__('%1$s, %2$s'), $col, getUserName($d['users_id']));
                     }
                 }
             }
             $grps = $job->getGroups(CommonITILActor::REQUESTER);
             if (count($grps)) {
                 if (empty($col)) {
                     $col = sprintf(__('%1$s %2$s'), $col, _n('Group', 'Groups', 2) . ' </i></b>');
                 } else {
                     $col = sprintf(__('%1$s - %2$s'), $col, _n('Group', 'Groups', 2) . ' </i></b>');
                 }
                 $first = true;
                 foreach ($grps as $d) {
                     if ($first) {
                         $col = sprintf(__('%1$s  %2$s'), $col, Dropdown::getDropdownName("glpi_groups", $d['groups_id']));
                     } else {
                         $col = sprintf(__('%1$s, %2$s'), $col, Dropdown::getDropdownName("glpi_groups", $d['groups_id']));
                     }
                     $first = false;
                 }
             }
             if ($col) {
                 $texte = '<b><i>' . sprintf(__('%1$s: %2$s'), __('Requester') . '</i></b>', '');
                 $pdf->displayText($texte, $col, 1);
             }
             $col = '';
             $users = $job->getUsers(CommonITILActor::ASSIGN);
             if (count($users)) {
                 foreach ($users as $d) {
                     if (empty($col)) {
                         $col = getUserName($d['users_id']);
                     } else {
                         $col = sprintf(__('%1$s, %2$s'), $col, getUserName($d['users_id']));
                     }
                 }
             }
             $grps = $job->getGroups(CommonITILActor::ASSIGN);
             if (count($grps)) {
                 if (empty($col)) {
                     $col = sprintf(__('%1$s %2$s'), $col, _n('Group', 'Groups', 2) . ' </i></b>');
                 } else {
                     $col = sprintf(__('%1$s - %2$s'), $col, _n('Group', 'Groups', 2) . ' </i></b>');
                 }
                 $first = true;
                 foreach ($grps as $d) {
                     if ($first) {
                         $col = sprintf(__('%1$s  %2$s'), $col, Dropdown::getDropdownName("glpi_groups", $d['groups_id']));
                     } else {
                         $col = sprintf(__('%1$s, %2$s'), $col, Dropdown::getDropdownName("glpi_groups", $d['groups_id']));
                     }
                     $first = false;
                 }
             }
             if ($col) {
                 $texte = '<b><i>' . sprintf(__('%1$s: %2$s') . '</i></b>', 'Assigned to', '');
                 $pdf->displayText($texte, $col, 1);
             }
             $texte = '<b><i>' . sprintf(__('%1$s: %2$s') . '</i></b>', 'Title', '');
             $pdf->displayText($texte, $job->fields["name"], 1);
         }
     }
     $pdf->displaySpace();
 }
 static function getTickets($protocol, $params = array())
 {
     global $DB, $WEBSERVICE_LINKED_OBJECTS;
     $item = new $params['options']['itemtype']();
     $resp = array();
     if ($item->can($params['data']['id'], 'r')) {
         $query = "SELECT " . Ticket::getCommonSelect() . "\n                    FROM `glpi_tickets` " . Ticket::getCommonLeftJoin() . "\n                    WHERE (`items_id` = '" . $params['data']['id'] . "'\n                          AND `itemtype` = '" . $params['options']['itemtype'] . "') " . getEntitiesRestrictRequest("AND", "glpi_tickets") . "\n                    ORDER BY `glpi_tickets`.`date_mod` DESC";
         $output = array();
         $ticket = new Ticket();
         foreach ($DB->request($query) as $data) {
             $params = array('data' => $data, 'searchOptions' => $ticket->getSearchOptions(), 'options' => $params['options']);
             parent::formatDataForOutput($params, $output);
         }
     }
     return $output;
 }
Example #3
0
 /**
  * Return the instance fields of itemtype identified by id
  *
  * @param $itemtype    string  itemtype (class) of object
  * @param $id          integer identifier of object
  * @param $params      array   with theses options :
  *    - 'expand_dropdowns': Show dropdown's names instead of id. default: false. Optionnal
  *    - 'get_hateoas':      Show relation of current item in a links attribute. default: true. Optionnal
  *    - 'get_sha1':         Get a sha1 signature instead of the full answer. default: false. Optionnal
  *    - 'with_components':  Only for [Computer, NetworkEquipment, Peripheral, Phone, Printer], Optionnal.
  *    - 'with_disks':       Only for Computer, retrieve the associated filesystems. Optionnal.
  *    - 'with_softwares':   Only for Computer, retrieve the associated softwares installations. Optionnal.
  *    - 'with_connections': Only for Computer, retrieve the associated direct connections (like peripherals and printers) .Optionnal.
  *    - 'with_networkports':Retrieve all network connections and advanced network informations. Optionnal.
  *    - 'with_infocoms':    Retrieve financial and administrative informations. Optionnal.
  *    - 'with_contracts':   Retrieve associated contracts. Optionnal.
  *    - 'with_documents':   Retrieve associated external documents. Optionnal.
  *    - 'with_tickets':     Retrieve associated itil tickets. Optionnal.
  *    - 'with_problems':    Retrieve associated itil problems. Optionnal.
  *    - 'with_changes':     Retrieve associated itil changes. Optionnal.
  *    - 'with_notes':       Retrieve Notes (if exists, not all itemtypes have notes). Optionnal.
  *    - 'with_logs':        Retrieve historical. Optionnal.
  *
  * @return     array    fields of found object
  **/
 protected function getItem($itemtype, $id, $params = array())
 {
     global $CFG_GLPI, $DB;
     $this->initEndpoint();
     // default params
     $default = array('expand_dropdowns' => false, 'get_hateoas' => true, 'get_sha1' => false, 'with_components' => false, 'with_disks' => false, 'with_softwares' => false, 'with_connections' => false, 'with_networkports' => false, 'with_infocoms' => false, 'with_contracts' => false, 'with_documents' => false, 'with_tickets' => false, 'with_problems' => false, 'with_changes' => false, 'with_notes' => false, 'with_logs' => false);
     $params = array_merge($default, $params);
     $item = new $itemtype();
     if (!$item->getFromDB($id)) {
         return $this->messageNotfoundError();
     }
     if (!$item->can($id, READ)) {
         return $this->messageRightError();
     }
     $fields = $item->fields;
     // avoid disclosure of critical fields
     $item::unsetUndisclosedFields($fields);
     // retrieve devices
     if (isset($params['with_devices']) && $params['with_devices'] && in_array($itemtype, Item_Devices::getConcernedItems())) {
         $all_devices = array();
         foreach (Item_Devices::getItemAffinities($item->getType()) as $device_type) {
             $found_devices = getAllDatasFromTable($device_type::getTable(), "`items_id` = '" . $item->getID() . "'\n                                                   AND `itemtype` = '" . $item->getType() . "'\n                                                   AND `is_deleted` = '0'", true);
             foreach ($found_devices as $devices_id => &$device) {
                 unset($device['items_id']);
                 unset($device['itemtype']);
                 unset($device['is_deleted']);
             }
             if (!empty($found_devices)) {
                 $all_devices[$device_type] = $found_devices;
             }
         }
         $fields['_devices'] = $all_devices;
     }
     // retrieve computer disks
     if (isset($params['with_disks']) && $params['with_disks'] && $itemtype == "Computer") {
         // build query to retrive filesystems
         $query = "SELECT `glpi_filesystems`.`name` AS fsname,\n                          `glpi_computerdisks`.*\n                   FROM `glpi_computerdisks`\n                   LEFT JOIN `glpi_filesystems`\n                             ON (`glpi_computerdisks`.`filesystems_id` = `glpi_filesystems`.`id`)\n                   WHERE `computers_id` = '{$id}'\n                         AND `is_deleted` = '0'";
         $fields['_disks'] = array();
         if ($result = $DB->query($query)) {
             while ($data = $DB->fetch_assoc($result)) {
                 unset($data['computers_id']);
                 unset($data['is_deleted']);
                 $fields['_disks'][] = array('name' => $data);
             }
         }
     }
     // retrieve computer softwares
     if (isset($params['with_softwares']) && $params['with_softwares'] && $itemtype == "Computer") {
         $fields['_softwares'] = array();
         if (!Software::canView()) {
             $fields['_softwares'] = self::arrayRightError();
         } else {
             $query = "SELECT `glpi_softwares`.`softwarecategories_id`,\n                             `glpi_softwares`.`id` AS softwares_id,\n                             `glpi_softwareversions`.`id` AS softwareversions_id,\n                             `glpi_computers_softwareversions`.`is_dynamic`,\n                             `glpi_softwareversions`.`states_id`,\n                             `glpi_softwares`.`is_valid`\n                      FROM `glpi_computers_softwareversions`\n                      LEFT JOIN `glpi_softwareversions`\n                           ON (`glpi_computers_softwareversions`.`softwareversions_id`\n                                 = `glpi_softwareversions`.`id`)\n                      LEFT JOIN `glpi_softwares`\n                           ON (`glpi_softwareversions`.`softwares_id` = `glpi_softwares`.`id`)\n                      WHERE `glpi_computers_softwareversions`.`computers_id` = '{$id}'\n                            AND `glpi_computers_softwareversions`.`is_deleted` = '0'\n                      ORDER BY `glpi_softwares`.`name`, `glpi_softwareversions`.`name`";
             if ($result = $DB->query($query)) {
                 while ($data = $DB->fetch_assoc($result)) {
                     $fields['_softwares'][] = $data;
                 }
             }
         }
     }
     // retrieve item connections
     if (isset($params['with_connections']) && $params['with_connections'] && $itemtype == "Computer") {
         $fields['_connections'] = array();
         foreach ($CFG_GLPI["directconnect_types"] as $connect_type) {
             $connect_item = new $connect_type();
             if ($connect_item->canView()) {
                 $query = "SELECT `glpi_computers_items`.`id` AS assoc_id,\n                         `glpi_computers_items`.`computers_id` AS assoc_computers_id,\n                         `glpi_computers_items`.`itemtype` AS assoc_itemtype,\n                         `glpi_computers_items`.`items_id` AS assoc_items_id,\n                         `glpi_computers_items`.`is_dynamic` AS assoc_is_dynamic,\n                         " . getTableForItemType($connect_type) . ".*\n                         FROM `glpi_computers_items`\n                         LEFT JOIN `" . getTableForItemType($connect_type) . "`\n                           ON (`" . getTableForItemType($connect_type) . "`.`id`\n                                 = `glpi_computers_items`.`items_id`)\n                         WHERE `computers_id` = '{$id}'\n                               AND `itemtype` = '" . $connect_type . "'\n                               AND `glpi_computers_items`.`is_deleted` = '0'";
                 if ($result = $DB->query($query)) {
                     while ($data = $DB->fetch_assoc($result)) {
                         $fields['_connections'][$connect_type][] = $data;
                     }
                 }
             }
         }
     }
     // retrieve item networkports
     if (isset($params['with_networkports']) && $params['with_networkports']) {
         $fields['_networkports'] = array();
         if (!NetworkEquipment::canView()) {
             $fields['_networkports'] = self::arrayRightError();
         } else {
             foreach (NetworkPort::getNetworkPortInstantiations() as $networkport_type) {
                 $netport_table = $networkport_type::getTable();
                 $query = "SELECT\n                           netp.`id` as netport_id,\n                           netp.`entities_id`,\n                           netp.`is_recursive`,\n                           netp.`logical_number`,\n                           netp.`name`,\n                           netp.`mac`,\n                           netp.`comment`,\n                           netp.`is_dynamic`,\n                           netp_subtable.*\n                         FROM glpi_networkports AS netp\n                         LEFT JOIN `{$netport_table}` AS netp_subtable\n                           ON netp_subtable.`networkports_id` = netp.`id`\n                         WHERE netp.`instantiation_type` = '{$networkport_type}'\n                           AND netp.`items_id` = '{$id}'\n                           AND netp.`itemtype` = '{$itemtype}'\n                           AND netp.`is_deleted` = '0'";
                 if ($result = $DB->query($query)) {
                     while ($data = $DB->fetch_assoc($result)) {
                         if (isset($data['netport_id'])) {
                             // append network name
                             $query_netn = "SELECT\n                              GROUP_CONCAT(CONCAT(ipadr.`id`, '" . Search::SHORTSEP . "' , ipadr.`name`)\n                                           SEPARATOR '" . Search::LONGSEP . "') as ipadresses,\n                              netn.`id` as networknames_id,\n                              netn.`name` as networkname,\n                              netn.`fqdns_id`,\n                              fqdn.`name` as fqdn_name,\n                              fqdn.`fqdn`\n                           FROM `glpi_networknames` AS netn\n                           LEFT JOIN `glpi_ipaddresses` AS ipadr\n                              ON ipadr.`itemtype` = 'NetworkName' AND ipadr.`items_id` = netn.`id`\n                           LEFT JOIN `glpi_fqdns` AS fqdn\n                              ON fqdn.`id` = netn.`fqdns_id`\n                           LEFT JOIN `glpi_ipaddresses_ipnetworks` ipadnet\n                              ON ipadnet.`ipaddresses_id` = ipadr.`id`\n                           LEFT JOIN `glpi_ipnetworks` `ipnet`\n                              ON ipnet.`id` = ipadnet.`ipnetworks_id`\n                           WHERE netn.`itemtype` = 'NetworkPort'\n                             AND netn.`items_id` = " . $data['netport_id'] . "\n                           GROUP BY netn.`id`, netn.`name`, netn.fqdns_id, fqdn.name, fqdn.fqdn";
                             if ($result_netn = $DB->query($query_netn)) {
                                 $data_netn = $DB->fetch_assoc($result_netn);
                                 $raw_ipadresses = explode(Search::LONGSEP, $data_netn['ipadresses']);
                                 $ipadresses = array();
                                 foreach ($raw_ipadresses as $ipadress) {
                                     $ipadress = explode(Search::SHORTSEP, $ipadress);
                                     //find ip network attached to these ip
                                     $ipnetworks = array();
                                     $query_ipnet = "SELECT\n                                    ipnet.`id`,\n                                    ipnet.`completename`,\n                                    ipnet.`name`,\n                                    ipnet.`address`,\n                                    ipnet.`netmask`,\n                                    ipnet.`gateway`,\n                                    ipnet.`ipnetworks_id`,\n                                    ipnet.`comment`\n                                 FROM `glpi_ipnetworks` ipnet\n                                 INNER JOIN `glpi_ipaddresses_ipnetworks` ipadnet\n                                    ON ipnet.`id` = ipadnet.`ipnetworks_id`\n                                    AND ipadnet.`ipaddresses_id` = " . $ipadress[0];
                                     if ($result_ipnet = $DB->query($query_ipnet)) {
                                         while ($data_ipnet = $DB->fetch_assoc($result_ipnet)) {
                                             $ipnetworks[] = $data_ipnet;
                                         }
                                     }
                                     $ipadresses[] = array('id' => $ipadress[0], 'name' => $ipadress[1], 'IPNetwork' => $ipnetworks);
                                 }
                                 $data['NetworkName'] = array('id' => $data_netn['networknames_id'], 'name' => $data_netn['networkname'], 'fqdns_id' => $data_netn['fqdns_id'], 'FQDN' => array('id' => $data_netn['fqdns_id'], 'name' => $data_netn['fqdn_name'], 'fqdn' => $data_netn['fqdn']), 'IPAddress' => $ipadresses);
                             }
                         }
                         $fields['_networkports'][$networkport_type][] = $data;
                     }
                 }
             }
         }
     }
     // retrieve item infocoms
     if (isset($params['with_infocoms']) && $params['with_infocoms']) {
         $fields['_infocoms'] = array();
         if (!Infocom::canView()) {
             $fields['_infocoms'] = self::arrayRightError();
         } else {
             $ic = new Infocom();
             if ($ic->getFromDBforDevice($itemtype, $id)) {
                 $fields['_infocoms'] = $ic->fields;
             }
         }
     }
     // retrieve item contracts
     if (isset($params['with_contracts']) && $params['with_contracts']) {
         $fields['_contracts'] = array();
         if (!Contract::canView()) {
             $fields['_contracts'] = self::arrayRightError();
         } else {
             $query = "SELECT `glpi_contracts_items`.*\n                     FROM `glpi_contracts_items`,\n                          `glpi_contracts`\n                     LEFT JOIN `glpi_entities` ON (`glpi_contracts`.`entities_id`=`glpi_entities`.`id`)\n                     WHERE `glpi_contracts`.`id`=`glpi_contracts_items`.`contracts_id`\n                           AND `glpi_contracts_items`.`items_id` = '{$id}'\n                           AND `glpi_contracts_items`.`itemtype` = '{$itemtype}'" . getEntitiesRestrictRequest(" AND", "glpi_contracts", '', '', true) . "\n                     ORDER BY `glpi_contracts`.`name`";
             if ($result = $DB->query($query)) {
                 while ($data = $DB->fetch_assoc($result)) {
                     $fields['_contracts'][] = $data;
                 }
             }
         }
     }
     // retrieve item contracts
     if (isset($params['with_documents']) && $params['with_documents']) {
         $fields['_documents'] = array();
         if (!$itemtype != 'Ticket' && $itemtype != 'KnowbaseItem' && $itemtype != 'Reminder' && !Document::canView()) {
             $fields['_documents'] = self::arrayRightError();
         } else {
             $query = "SELECT `glpi_documents_items`.`id` AS assocID,\n                             `glpi_documents_items`.`date_mod` AS assocdate,\n                             `glpi_entities`.`id` AS entityID,\n                             `glpi_entities`.`completename` AS entity,\n                             `glpi_documentcategories`.`completename` AS headings,\n                             `glpi_documents`.*\n                      FROM `glpi_documents_items`\n                      LEFT JOIN `glpi_documents`\n                                ON (`glpi_documents_items`.`documents_id`=`glpi_documents`.`id`)\n                      LEFT JOIN `glpi_entities` ON (`glpi_documents`.`entities_id`=`glpi_entities`.`id`)\n                      LEFT JOIN `glpi_documentcategories`\n                              ON (`glpi_documents`.`documentcategories_id`=`glpi_documentcategories`.`id`)\n                      WHERE `glpi_documents_items`.`items_id` = '{$id}'\n                            AND `glpi_documents_items`.`itemtype` = '{$itemtype}' ";
             if ($result = $DB->query($query)) {
                 while ($data = $DB->fetch_assoc($result)) {
                     $fields['_documents'][] = $data;
                 }
             }
         }
     }
     // retrieve item tickets
     if (isset($params['with_tickets']) && $params['with_tickets']) {
         $fields['_tickets'] = array();
         if (!Ticket::canView()) {
             $fields['_tickets'] = self::arrayRightError();
         } else {
             $query = "SELECT " . Ticket::getCommonSelect() . "\n                      FROM `glpi_tickets` " . Ticket::getCommonLeftJoin() . "\n                      WHERE `glpi_items_tickets`.`items_id` = '{$id}'\n                             AND `glpi_items_tickets`.`itemtype` = '{$itemtype}' " . getEntitiesRestrictRequest("AND", "glpi_tickets") . "\n                      ORDER BY `glpi_tickets`.`date_mod` DESC";
             if ($result = $DB->query($query)) {
                 while ($data = $DB->fetch_assoc($result)) {
                     $fields['_tickets'][] = $data;
                 }
             }
         }
     }
     // retrieve item problems
     if (isset($params['with_problems']) && $params['with_problems']) {
         $fields['_problems'] = array();
         if (!Problem::canView()) {
             $fields['_problems'] = self::arrayRightError();
         } else {
             $query = "SELECT " . Problem::getCommonSelect() . "\n                            FROM `glpi_problems`\n                            LEFT JOIN `glpi_items_problems`\n                              ON (`glpi_problems`.`id` = `glpi_items_problems`.`problems_id`) " . Problem::getCommonLeftJoin() . "\n                            WHERE `items_id` = '{$id}'\n                                  AND `itemtype` = '{$itemtype}' " . getEntitiesRestrictRequest("AND", "glpi_problems") . "\n                            ORDER BY `glpi_problems`.`date_mod` DESC";
             if ($result = $DB->query($query)) {
                 while ($data = $DB->fetch_assoc($result)) {
                     $fields['_problems'][] = $data;
                 }
             }
         }
     }
     // retrieve item changes
     if (isset($params['with_changes']) && $params['with_changes']) {
         $fields['_changes'] = array();
         if (!Change::canView()) {
             $fields['_changes'] = self::arrayRightError();
         } else {
             $query = "SELECT " . Change::getCommonSelect() . "\n                            FROM `glpi_changes`\n                            LEFT JOIN `glpi_changes_items`\n                              ON (`glpi_changes`.`id` = `glpi_changes_items`.`problems_id`) " . Change::getCommonLeftJoin() . "\n                            WHERE `items_id` = '{$id}'\n                                  AND `itemtype` = '{$itemtype}' " . getEntitiesRestrictRequest("AND", "glpi_changes") . "\n                            ORDER BY `glpi_changes`.`date_mod` DESC";
             if ($result = $DB->query($query)) {
                 while ($data = $DB->fetch_assoc($result)) {
                     $fields['_changes'][] = $data;
                 }
             }
         }
     }
     // retrieve item notes
     if (isset($params['with_notes']) && $params['with_notes']) {
         $fields['_notes'] = array();
         if (!Session::haveRight($itemtype::$rightname, READNOTE)) {
             $fields['_notes'] = self::arrayRightError();
         } else {
             $fields['_notes'] = Notepad::getAllForItem($item);
         }
     }
     // retrieve item logs
     if (isset($params['with_logs']) && $params['with_logs']) {
         $fields['_logs'] = array();
         if (!Session::haveRight($itemtype::$rightname, READNOTE)) {
             $fields['_logs'] = self::arrayRightError();
         } else {
             $fields['_logs'] = getAllDatasFromTable("glpi_logs", "`items_id` = '" . $item->getID() . "'\n                                                    AND `itemtype` = '" . $item->getType() . "'");
         }
     }
     // expand dropdown (retrieve name of dropdowns) and get hateoas from foreign keys
     $fields = self::parseDropdowns($fields, $params);
     // get hateoas from children
     if ($params['get_hateoas']) {
         $hclasses = self::getHatoasClasses($itemtype);
         foreach ($hclasses as $hclass) {
             $fields['links'][] = array('rel' => $hclass, 'href' => self::$api_url . "/{$itemtype}/" . $item->getID() . "/{$hclass}/");
         }
     }
     // get sha1 footprint if needed
     if ($params['get_sha1']) {
         $fields = sha1(json_encode($fields, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK));
     }
     return $fields;
 }
Example #4
0
 /**
  * Return the instance fields of itemtype identified by id
  *
  * @since version 9.1
  *
  * @param      string   $itemtype  itemtype (class) of object
  * @param      integer  $id        identifier of object
  * @param      array   $params   array with theses options :
  *    - 'expand_dropdowns' : show dropdown's names instead of id. default: false. Optionnal
  *    - 'get_hateoas' :      show relation of current item in a links attribute. default: true. Optionnal
  *    - 'with_components' :  Only for [Computer, NetworkEquipment, Peripheral, Phone, Printer], Optionnal.
  *    - 'with_disks' :       Only for Computer, retrieve the associated filesystems. Optionnal.
  *    - 'with_softwares' :   Only for Computer, retrieve the associated softwares installations. Optionnal.
  *    - 'with_connections' : Only for Computer, retrieve the associated direct connections (like peripherals and printers) .Optionnal.
  *    - 'with_networkports' :Retrieve all network connections and advanced network informations. Optionnal.
  *    - 'with_infocoms' :    Retrieve financial and administrative informations. Optionnal.
  *    - 'with_contracts' :   Retrieve associated contracts. Optionnal.
  *    - 'with_documents' :   Retrieve associated external documents. Optionnal.
  *    - 'with_tickets' :     Retrieve associated itil tickets. Optionnal.
  *    - 'with_problems' :    Retrieve associated itil problems. Optionnal.
  *    - 'with_changes' :     Retrieve associated itil changes. Optionnal.
  *    - 'with_notes' :       Retrieve Notes (if exists, not all itemtypes have notes). Optionnal.
  *    - 'with_logs' :        Retrieve historical. Optionnal.
  *
  * @return     array    fields of found object
  */
 protected function getItem($itemtype, $id, $params = array())
 {
     global $CFG_GLPI, $DB;
     $this->initEndpoint();
     // default params
     $default = array('expand_dropdowns' => false, 'get_hateoas' => true, 'with_components' => false, 'with_disks' => false, 'with_softwares' => false, 'with_connections' => false, 'with_networkports' => false, 'with_infocoms' => false, 'with_contracts' => false, 'with_documents' => false, 'with_tickets' => false, 'with_problems' => false, 'with_changes' => false, 'with_notes' => false, 'with_logs' => false);
     $params = array_merge($default, $params);
     $item = new $itemtype();
     if (!$item->getFromDB($id)) {
         return $this->messageNotfoundError();
     }
     if (!$item->can($id, READ)) {
         return $this->messageRightError();
     }
     $fields = $item->fields;
     // retrieve devices
     if (isset($params['with_devices']) && $params['with_devices'] && in_array($itemtype, Item_Devices::getConcernedItems())) {
         $all_devices = array();
         foreach (Item_Devices::getItemAffinities($item->getType()) as $device_type) {
             $found_devices = getAllDatasFromTable($device_type::getTable(), "`items_id` = '" . $item->getID() . "'\n                                                   AND `itemtype` = '" . $item->getType() . "'\n                                                   AND `is_deleted` = '0'", true);
             foreach ($found_devices as $devices_id => &$device) {
                 unset($device['items_id']);
                 unset($device['itemtype']);
                 unset($device['is_deleted']);
             }
             if (!empty($found_devices)) {
                 $all_devices[$device_type] = $found_devices;
             }
         }
         $fields['_devices'] = $all_devices;
     }
     // retrieve computer disks
     if (isset($params['with_disks']) && $params['with_disks'] && $itemtype == "Computer") {
         // build query to retrive filesystems
         $query = "SELECT `glpi_filesystems`.`name` AS fsname,\n                          `glpi_computerdisks`.*\n                   FROM `glpi_computerdisks`\n                   LEFT JOIN `glpi_filesystems`\n                             ON (`glpi_computerdisks`.`filesystems_id` = `glpi_filesystems`.`id`)\n                   WHERE `computers_id` = '{$id}'\n                         AND `is_deleted` = '0'";
         if ($result = $DB->query($query)) {
             while ($data = $DB->fetch_assoc($result)) {
                 unset($data['computers_id']);
                 unset($data['is_deleted']);
                 $fields['_disks'][] = array('name' => $data);
             }
         }
     }
     // retrieve computer softwares
     if (isset($params['with_softwares']) && $params['with_softwares'] && $itemtype == "Computer") {
         $fields['_softwares'] = array();
         if (!Software::canView()) {
             $fields['_softwares'] = self::arrayRightError();
         } else {
             $query = "SELECT `glpi_softwares`.`softwarecategories_id`,\n                             `glpi_softwares`.`id` AS softwares_id,\n                             `glpi_softwareversions`.`id` AS softwareversions_id,\n                             `glpi_computers_softwareversions`.`is_dynamic`,\n                             `glpi_softwareversions`.`states_id`,\n                             `glpi_softwares`.`is_valid`\n                      FROM `glpi_computers_softwareversions`\n                      LEFT JOIN `glpi_softwareversions`\n                           ON (`glpi_computers_softwareversions`.`softwareversions_id`\n                                 = `glpi_softwareversions`.`id`)\n                      LEFT JOIN `glpi_softwares`\n                           ON (`glpi_softwareversions`.`softwares_id` = `glpi_softwares`.`id`)\n                      WHERE `glpi_computers_softwareversions`.`computers_id` = '{$id}'\n                            AND `glpi_computers_softwareversions`.`is_deleted` = '0'\n                      ORDER BY `glpi_softwares`.`name`, `glpi_softwareversions`.`name`";
             if ($result = $DB->query($query)) {
                 while ($data = $DB->fetch_assoc($result)) {
                     $fields['_softwares'][] = $data;
                 }
             }
         }
     }
     // retrieve item connections
     if (isset($params['with_connections']) && $params['with_connections'] && $itemtype == "Computer") {
         $fields['_connections'] = array();
         foreach ($CFG_GLPI["directconnect_types"] as $connect_type) {
             $connect_item = new $connect_type();
             if ($connect_item->canView()) {
                 $query = "SELECT `glpi_computers_items`.`id` AS assoc_id,\n                         `glpi_computers_items`.`computers_id` AS assoc_computers_id,\n                         `glpi_computers_items`.`itemtype` AS assoc_itemtype,\n                         `glpi_computers_items`.`items_id` AS assoc_items_id,\n                         `glpi_computers_items`.`is_dynamic` AS assoc_is_dynamic,\n                         " . getTableForItemType($connect_type) . ".*\n                         FROM `glpi_computers_items`\n                         LEFT JOIN `" . getTableForItemType($connect_type) . "`\n                           ON (`" . getTableForItemType($connect_type) . "`.`id`\n                                 = `glpi_computers_items`.`items_id`)\n                         WHERE `computers_id` = '{$id}'\n                               AND `itemtype` = '" . $connect_type . "'\n                               AND `glpi_computers_items`.`is_deleted` = '0'";
                 if ($result = $DB->query($query)) {
                     while ($data = $DB->fetch_assoc($result)) {
                         $fields['_connections'][$connect_type][] = $data;
                     }
                 }
             }
         }
     }
     // retrieve item networkports
     if (isset($params['with_networkports']) && $params['with_networkports']) {
         $fields['_networkports'] = array();
         if (!NetworkEquipment::canView()) {
             $fields['_networkports'] = self::arrayRightError();
         } else {
             foreach (NetworkPort::getNetworkPortInstantiations() as $networkport_type) {
                 $query = "SELECT\n                           netp.`entities_id`,\n                           netp.`is_recursive`,\n                           netp.`logical_number`,\n                           netp.`name`,\n                           netp.`mac`,\n                           netp.`comment`,\n                           netp.`is_dynamic`,\n                           netp_subtable.*\n                         FROM glpi_networkports AS netp\n                         LEFT JOIN " . $networkport_type::getTable() . " AS netp_subtable\n                           ON netp_subtable.`networkports_id` = netp.`id`\n                         WHERE netp.`instantiation_type` = '{$networkport_type}'\n                               AND netp.`items_id` = '{$id}'\n                               AND netp.`itemtype` = '{$itemtype}'\n                               AND netp.`is_deleted` = '0'";
                 if ($result = $DB->query($query)) {
                     while ($data = $DB->fetch_assoc($result)) {
                         $fields['_networkports'][$networkport_type][] = $data;
                     }
                 }
             }
         }
     }
     // retrieve item infocoms
     if (isset($params['with_infocoms']) && $params['with_infocoms']) {
         $fields['_infocoms'] = array();
         if (!Infocom::canView()) {
             $fields['_infocoms'] = self::arrayRightError();
         } else {
             $ic = new Infocom();
             if ($ic->getFromDBforDevice($itemtype, $id)) {
                 $fields['_infocoms'] = $ic->fields;
             }
         }
     }
     // retrieve item contracts
     if (isset($params['with_contracts']) && $params['with_contracts']) {
         $fields['_contracts'] = array();
         if (!Contract::canView()) {
             $fields['_contracts'] = self::arrayRightError();
         } else {
             $query = "SELECT `glpi_contracts_items`.*\n                     FROM `glpi_contracts_items`,\n                          `glpi_contracts`\n                     LEFT JOIN `glpi_entities` ON (`glpi_contracts`.`entities_id`=`glpi_entities`.`id`)\n                     WHERE `glpi_contracts`.`id`=`glpi_contracts_items`.`contracts_id`\n                           AND `glpi_contracts_items`.`items_id` = '{$id}'\n                           AND `glpi_contracts_items`.`itemtype` = '{$itemtype}'" . getEntitiesRestrictRequest(" AND", "glpi_contracts", '', '', true) . "\n                     ORDER BY `glpi_contracts`.`name`";
             if ($result = $DB->query($query)) {
                 while ($data = $DB->fetch_assoc($result)) {
                     $fields['_contracts'][] = $data;
                 }
             }
         }
     }
     // retrieve item contracts
     if (isset($params['with_documents']) && $params['with_documents']) {
         $fields['_documents'] = array();
         if (!$itemtype != 'Ticket' && $itemtype != 'KnowbaseItem' && $itemtype != 'Reminder' && !Document::canView()) {
             $fields['_documents'] = self::arrayRightError();
         } else {
             $query = "SELECT `glpi_documents_items`.`id` AS assocID,\n                             `glpi_documents_items`.`date_mod` AS assocdate,\n                             `glpi_entities`.`id` AS entityID,\n                             `glpi_entities`.`completename` AS entity,\n                             `glpi_documentcategories`.`completename` AS headings,\n                             `glpi_documents`.*\n                      FROM `glpi_documents_items`\n                      LEFT JOIN `glpi_documents`\n                                ON (`glpi_documents_items`.`documents_id`=`glpi_documents`.`id`)\n                      LEFT JOIN `glpi_entities` ON (`glpi_documents`.`entities_id`=`glpi_entities`.`id`)\n                      LEFT JOIN `glpi_documentcategories`\n                              ON (`glpi_documents`.`documentcategories_id`=`glpi_documentcategories`.`id`)\n                      WHERE `glpi_documents_items`.`items_id` = '{$id}'\n                            AND `glpi_documents_items`.`itemtype` = '{$itemtype}' ";
             if ($result = $DB->query($query)) {
                 while ($data = $DB->fetch_assoc($result)) {
                     $fields['_documents'][] = $data;
                 }
             }
         }
     }
     // retrieve item tickets
     if (isset($params['with_tickets']) && $params['with_tickets']) {
         $fields['_tickets'] = array();
         if (!Ticket::canView()) {
             $fields['_tickets'] = self::arrayRightError();
         } else {
             $query = "SELECT " . Ticket::getCommonSelect() . "\n                      FROM `glpi_tickets` " . Ticket::getCommonLeftJoin() . "\n                      WHERE `glpi_items_tickets`.`items_id` = '{$id}'\n                             AND `glpi_items_tickets`.`itemtype` = '{$itemtype}' " . getEntitiesRestrictRequest("AND", "glpi_tickets") . "\n                      ORDER BY `glpi_tickets`.`date_mod` DESC";
             if ($result = $DB->query($query)) {
                 while ($data = $DB->fetch_assoc($result)) {
                     $fields['_tickets'][] = $data;
                 }
             }
         }
     }
     // retrieve item problems
     if (isset($params['with_problems']) && $params['with_problems']) {
         $fields['_problems'] = array();
         if (!Problem::canView()) {
             $fields['_problems'] = self::arrayRightError();
         } else {
             $query = "SELECT " . Problem::getCommonSelect() . "\n                            FROM `glpi_problems`\n                            LEFT JOIN `glpi_items_problems`\n                              ON (`glpi_problems`.`id` = `glpi_items_problems`.`problems_id`) " . Problem::getCommonLeftJoin() . "\n                            WHERE `items_id` = '{$id}'\n                                  AND `itemtype` = '{$itemtype}' " . getEntitiesRestrictRequest("AND", "glpi_problems") . "\n                            ORDER BY `glpi_problems`.`date_mod` DESC";
             if ($result = $DB->query($query)) {
                 while ($data = $DB->fetch_assoc($result)) {
                     $fields['_problems'][] = $data;
                 }
             }
         }
     }
     // retrieve item changes
     if (isset($params['with_changes']) && $params['with_changes']) {
         $fields['_changes'] = array();
         if (!Change::canView()) {
             $fields['_changes'] = self::arrayRightError();
         } else {
             $query = "SELECT " . Change::getCommonSelect() . "\n                            FROM `glpi_changes`\n                            LEFT JOIN `glpi_changes_items`\n                              ON (`glpi_changes`.`id` = `glpi_changes_items`.`problems_id`) " . Change::getCommonLeftJoin() . "\n                            WHERE `items_id` = '{$id}'\n                                  AND `itemtype` = '{$itemtype}' " . getEntitiesRestrictRequest("AND", "glpi_changes") . "\n                            ORDER BY `glpi_changes`.`date_mod` DESC";
             if ($result = $DB->query($query)) {
                 while ($data = $DB->fetch_assoc($result)) {
                     $fields['_changes'][] = $data;
                 }
             }
         }
     }
     // retrieve item notes
     if (isset($params['with_notes']) && $params['with_notes']) {
         $fields['_notes'] = array();
         if (!Session::haveRight($itemtype::$rightname, READNOTE)) {
             $fields['_notes'] = self::arrayRightError();
         } else {
             $fields['_notes'] = Notepad::getAllForItem($itemtype);
         }
     }
     // retrieve item logs
     if (isset($params['with_logs']) && $params['with_logs']) {
         $fields['_logs'] = array();
         if (!Session::haveRight($itemtype::$rightname, READNOTE)) {
             $fields['_logs'] = self::arrayRightError();
         } else {
             $fields['_logs'] = getAllDatasFromTable("glpi_logs", "`items_id` = '" . $item->getID() . "'\n                                                    AND `itemtype` = '" . $item->getType() . "'");
         }
     }
     // expand dropdown (retrieve name of dropdowns) and get hateoas from foreign keys
     $fields = self::parseDropdowns($fields, $params);
     // get hateoas from children
     if ($params['get_hateoas']) {
         $hclasses = self::getHatoasClasses($itemtype);
         foreach ($hclasses as $hclass) {
             $fields['links'][] = array('rel' => $hclass, 'href' => self::$api_url . "/{$itemtype}/" . $item->getID() . "/{$hclass}/");
         }
     }
     return $fields;
 }