/** * Retrieve last history Data for an item * * @param $item CommonDBTM object * @param $start integer first line to retrieve (default 0) * @param $limit integer max number of line to retrive (0 for all) (default 0) * @param $sqlfilter string to add an SQL filter (default '') * * @return array of localized log entry (TEXT only, no HTML) **/ static function getHistoryData(CommonDBTM $item, $start = 0, $limit = 0, $sqlfilter = '') { global $DB; $itemtype = $item->getType(); $items_id = $item->getField('id'); $itemtable = $item->getTable(); $SEARCHOPTION = Search::getOptions($itemtype); $query = "SELECT *\n FROM `glpi_logs`\n WHERE `items_id` = '{$items_id}'\n AND `itemtype` = '{$itemtype}' "; if ($sqlfilter) { $query .= "AND ({$sqlfilter}) "; } $query .= "ORDER BY `id` DESC"; if ($limit) { $query .= " LIMIT " . intval($start) . "," . intval($limit); } $changes = array(); foreach ($DB->request($query) as $data) { $tmp = array(); $tmp['display_history'] = true; $tmp['id'] = $data["id"]; $tmp['date_mod'] = Html::convDateTime($data["date_mod"]); $tmp['user_name'] = $data["user_name"]; $tmp['field'] = ""; $tmp['change'] = ""; $tmp['datatype'] = ""; // This is an internal device ? if ($data["linked_action"]) { // Yes it is an internal device switch ($data["linked_action"]) { case self::HISTORY_CREATE_ITEM: $tmp['change'] = __('Add the item'); break; case self::HISTORY_DELETE_ITEM: $tmp['change'] = __('Delete the item'); break; case self::HISTORY_LOCK_ITEM: $tmp['change'] = __('Lock the item'); break; case self::HISTORY_UNLOCK_ITEM: $tmp['change'] = __('Unlock the item'); break; case self::HISTORY_RESTORE_ITEM: $tmp['change'] = __('Restore the item'); break; case self::HISTORY_ADD_DEVICE: $tmp['field'] = NOT_AVAILABLE; if ($item2 = getItemForItemtype($data["itemtype_link"])) { $tmp['field'] = $item2->getTypeName(1); } //TRANS: %s is the component name $tmp['change'] = sprintf(__('%1$s: %2$s'), __('Add the component'), $data["new_value"]); break; case self::HISTORY_UPDATE_DEVICE: $tmp['field'] = NOT_AVAILABLE; $change = ''; $linktype_field = explode('#', $data["itemtype_link"]); $linktype = $linktype_field[0]; $field = $linktype_field[1]; $devicetype = $linktype::getDeviceType(); $tmp['field'] = $devicetype; $specif_fields = $linktype::getSpecificities(); if (isset($specif_fields[$field]['short name'])) { $tmp['field'] = $devicetype; $tmp['field'] .= " (" . $specif_fields[$field]['short name'] . ")"; } //TRANS: %1$s is the old_value, %2$s is the new_value $tmp['change'] = sprintf(__('Change the component %1$s: %2$s'), $tmp['field'], sprintf(__('%1$s by %2$s'), $data["old_value"], $data["new_value"])); break; case self::HISTORY_DELETE_DEVICE: $tmp['field'] = NOT_AVAILABLE; if ($item2 = getItemForItemtype($data["itemtype_link"])) { $tmp['field'] = $item2->getTypeName(1); } //TRANS: %s is the component name $tmp['change'] = sprintf(__('%1$s: %2$s'), __('Delete the component'), $data["old_value"]); break; case self::HISTORY_LOCK_DEVICE: $tmp['field'] = NOT_AVAILABLE; if ($item2 = getItemForItemtype($data["itemtype_link"])) { $tmp['field'] = $item2->getTypeName(1); } //TRANS: %s is the component name $tmp['change'] = sprintf(__('%1$s: %2$s'), __('Lock the component'), $data["old_value"]); break; case self::HISTORY_UNLOCK_DEVICE: $tmp['field'] = NOT_AVAILABLE; if ($item2 = getItemForItemtype($data["itemtype_link"])) { $tmp['field'] = $item2->getTypeName(1); } //TRANS: %s is the component name $tmp['change'] = sprintf(__('%1$s: %2$s'), __('Unlock the component'), $data["new_value"]); break; case self::HISTORY_INSTALL_SOFTWARE: $tmp['field'] = _n('Software', 'Software', 1); //TRANS: %s is the software name $tmp['change'] = sprintf(__('%1$s: %2$s'), __('Install the software'), $data["new_value"]); break; case self::HISTORY_UNINSTALL_SOFTWARE: $tmp['field'] = _n('Software', 'Software', 1); //TRANS: %s is the software name $tmp['change'] = sprintf(__('%1$s: %2$s'), __('Uninstall the software'), $data["old_value"]); break; case self::HISTORY_DISCONNECT_DEVICE: $tmp['field'] = NOT_AVAILABLE; if ($item2 = getItemForItemtype($data["itemtype_link"])) { $tmp['field'] = $item2->getTypeName(1); } //TRANS: %s is the item name $tmp['change'] = sprintf(__('%1$s: %2$s'), __('Disconnect the item'), $data["old_value"]); break; case self::HISTORY_CONNECT_DEVICE: $tmp['field'] = NOT_AVAILABLE; if ($item2 = getItemForItemtype($data["itemtype_link"])) { $tmp['field'] = $item2->getTypeName(1); } //TRANS: %s is the item name $tmp['change'] = sprintf(__('%1$s: %2$s'), __('Connect the item'), $data["new_value"]); break; case self::HISTORY_LOG_SIMPLE_MESSAGE: $tmp['field'] = ""; $tmp['change'] = $data["new_value"]; break; case self::HISTORY_ADD_RELATION: $tmp['field'] = NOT_AVAILABLE; if ($item2 = getItemForItemtype($data["itemtype_link"])) { $tmp['field'] = $item2->getTypeName(1); } $tmp['change'] = sprintf(__('%1$s: %2$s'), __('Add a link with an item'), $data["new_value"]); break; case self::HISTORY_DEL_RELATION: $tmp['field'] = NOT_AVAILABLE; if ($item2 = getItemForItemtype($data["itemtype_link"])) { $tmp['field'] = $item2->getTypeName(1); } $tmp['change'] = sprintf(__('%1$s: %2$s'), __('Delete a link with an item'), $data["old_value"]); break; case self::HISTORY_LOCK_RELATION: $tmp['field'] = NOT_AVAILABLE; if ($item2 = getItemForItemtype($data["itemtype_link"])) { $tmp['field'] = $item2->getTypeName(1); } $tmp['change'] = sprintf(__('%1$s: %2$s'), __('Lock a link with an item'), $data["old_value"]); break; case self::HISTORY_UNLOCK_RELATION: $tmp['field'] = NOT_AVAILABLE; if ($item2 = getItemForItemtype($data["itemtype_link"])) { $tmp['field'] = $item2->getTypeName(1); } $tmp['change'] = sprintf(__('%1$s: %2$s'), __('Unlock a link with an item'), $data["new_value"]); break; case self::HISTORY_ADD_SUBITEM: $tmp['field'] = ''; if ($item2 = getItemForItemtype($data["itemtype_link"])) { $tmp['field'] = $item2->getTypeName(1); } $tmp['change'] = sprintf(__('%1$s: %2$s'), __('Add the item'), sprintf(__('%1$s (%2$s)'), $tmp['field'], $data["new_value"])); break; case self::HISTORY_UPDATE_SUBITEM: $tmp['field'] = ''; if ($item2 = getItemForItemtype($data["itemtype_link"])) { $tmp['field'] = $item2->getTypeName(1); } $tmp['change'] = sprintf(__('%1$s: %2$s'), __('Update the item'), sprintf(__('%1$s (%2$s)'), $tmp['field'], $data["new_value"])); break; case self::HISTORY_DELETE_SUBITEM: $tmp['field'] = ''; if ($item2 = getItemForItemtype($data["itemtype_link"])) { $tmp['field'] = $item2->getTypeName(1); } $tmp['change'] = sprintf(__('%1$s: %2$s'), __('Delete the item'), sprintf(__('%1$s (%2$s)'), $tmp['field'], $data["old_value"])); break; case self::HISTORY_LOCK_SUBITEM: $tmp['field'] = ''; if ($item2 = getItemForItemtype($data["itemtype_link"])) { $tmp['field'] = $item2->getTypeName(1); } $tmp['change'] = sprintf(__('%1$s: %2$s'), __('Lock an item'), sprintf(__('%1$s (%2$s)'), $tmp['field'], $data["old_value"])); break; case self::HISTORY_UNLOCK_SUBITEM: $tmp['field'] = ''; if ($item2 = getItemForItemtype($data["itemtype_link"])) { $tmp['field'] = $item2->getTypeName(1); } $tmp['change'] = sprintf(__('%1$s: %2$s'), __('Unlock an item'), sprintf(__('%1$s (%2$s)'), $tmp['field'], $data["new_value"])); break; default: $fct = array($data['itemtype_link'], 'getHistoryEntry'); if ($data['linked_action'] >= self::HISTORY_PLUGIN && $data['itemtype_link'] && is_callable($fct)) { $tmp['field'] = $data['itemtype_link']::getTypeName(1); $tmp['change'] = call_user_func($fct, $data); } $tmp['display_history'] = !empty($tmp['change']); } } else { $fieldname = ""; $searchopt = array(); $tablename = ''; // It's not an internal device foreach ($SEARCHOPTION as $key2 => $val2) { if ($key2 == $data["id_search_option"]) { $tmp['field'] = $val2["name"]; $tablename = $val2["table"]; $fieldname = $val2["field"]; $searchopt = $val2; if (isset($val2['datatype'])) { $tmp['datatype'] = $val2["datatype"]; } break; } } if ($itemtable == $tablename) { switch ($tmp['datatype']) { // specific case for text field case 'text': $tmp['change'] = __('Update of the field'); break; default: $data["old_value"] = $item->getValueToDisplay($searchopt, $data["old_value"]); $data["new_value"] = $item->getValueToDisplay($searchopt, $data["new_value"]); break; } } if (empty($tmp['change'])) { $tmp['change'] = sprintf(__('Change %1$s by %2$s'), $data["old_value"], $data["new_value"]); } } $changes[] = $tmp; } return $changes; }
/** * @since version 0.85 * * @see CommonDBTM::processMassiveActionsForOneItemtype() **/ static function processMassiveActionsForOneItemtype(MassiveAction $ma, CommonDBTM $item, array $ids) { $input = $ma->getInput(); switch ($ma->getAction()) { case 'move_under': if (isset($input['parent'])) { $fk = $item->getForeignKeyField(); $parent = clone $item; if (!$parent->getFromDB($input['parent'])) { $ma->itemDone($item->getType(), $ids, MassiveAction::ACTION_KO); $ma->addMessage($parent->getErrorMessage(ERROR_NOT_FOUND)); return; } foreach ($ids as $id) { if ($item->can($id, UPDATE)) { // Check if parent is not a child of the original one if (!in_array($parent->getID(), getSonsOf($item->getTable(), $item->getID()))) { if ($item->update(array('id' => $id, $fk => $parent->getID()))) { $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_OK); } else { $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO); $ma->addMessage($item->getErrorMessage(ERROR_ON_ACTION)); } } else { $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO); $ma->addMessage($item->getErrorMessage(ERROR_COMPAT)); } } else { $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_NORIGHT); $ma->addMessage($item->getErrorMessage(ERROR_RIGHT)); } } } else { $ma->itemDone($item->getType(), $ids, MassiveAction::ACTION_KO); $ma->addMessage($parent->getErrorMessage(ERROR_COMPAT)); } return; } parent::processMassiveActionsForOneItemtype($ma, $item, $ids); }
static function getSoftwareVersionsOrLicenses($protocol, $params = array(), CommonDBTM $item) { global $DB; $software = new Software(); $resp = array(); if ($software->can($params['data']['id'], READ)) { $query = "SELECT `gsv`.*\n FROM `" . Toolbox::addslashes_deep($item->getTable()) . "` AS gsv,\n `glpi_softwares` AS gs\n WHERE `gsv`.`softwares_id` = `gs`.`id`\n AND `gsv`.`softwares_id`\n = '" . Toolbox::addslashes_deep($params['data']['id']) . "'\n GROUP BY `gsv`.`softwares_id`\n ORDER BY `gsv`.`softwares_id` ASC"; $toformat = array('searchOptions' => Search::getOptions(get_class($item)), 'options' => $params['options']); foreach ($DB->request($query) as $version_or_license) { $toformat['data'] = $version_or_license; $result = array(); parent::formatDataForOutput($toformat, $result); $resp[$version_or_license['id']] = $result; } } return $resp; }
/** * Construct history for an item * * @param $item CommonDBTM object * @param $oldvalues array of old values updated * @param $values array of all values of the item * * @return boolean for success (at least 1 log entry added) **/ static function constructHistory(CommonDBTM $item, &$oldvalues, &$values) { global $LANG; if (!count($oldvalues)) { return false; } // needed to have $SEARCHOPTION if ($item->getType() == 'TicketSatisfaction') { $real_type = 'Ticket'; $real_id = $item->fields['tickets_id']; } else { if ($item->getType() == 'Infocom') { $real_type = $item->fields['itemtype']; $real_id = $item->fields['items_id']; } else { $real_type = $item->getType(); $real_id = $item->fields['id']; } } $searchopt = Search::getOptions($real_type); if (!is_array($searchopt)) { return false; } $result = 0; foreach ($oldvalues as $key => $oldval) { $changes = array(); if ($real_type == 'Infocom') { // Parsing $SEARCHOPTION to find infocom foreach ($searchopt as $key2 => $val2) { if ($val2["field"] == $key && strpos($val2['table'], 'infocoms') || $key == 'budgets_id' && $val2['table'] == 'glpi_budgets' || $key == 'suppliers_id' && $val2['table'] == 'glpi_suppliers') { $id_search_option = $key2; // Give ID of the $SEARCHOPTION if ($val2["table"] == "glpi_infocoms") { // 1st case : text field -> keep datas $changes = array($id_search_option, addslashes($oldval), $values[$key]); } else { if ($val2["table"] == "glpi_suppliers") { // 2nd case ; link field -> get data from glpi_suppliers $changes = array($id_search_option, addslashes(Dropdown::getDropdownName("glpi_suppliers", $oldval)), addslashes(Dropdown::getDropdownName("glpi_suppliers", $values[$key]))); } else { // 3rd case ; link field -> get data from dropdown (budget) $changes = array($id_search_option, addslashes(Dropdown::getDropdownName($val2["table"], $oldval)), addslashes(Dropdown::getDropdownName($val2["table"], $values[$key]))); } } break; // foreach exit } } } else { // Not an Infocom // Parsing $SEARCHOPTION to find changed field foreach ($searchopt as $key2 => $val2) { // Linkfield or standard field not massive action enable if ($val2["linkfield"] == $key || $key == $val2["field"] && $val2["table"] == $item->getTable()) { $id_search_option = $key2; // Give ID of the $SEARCHOPTION // 1st case : Ticket specific dropdown case (without table) if ($real_type == 'Ticket' && in_array($key, array('status', 'urgency', 'impact', 'priority', 'global_validation'))) { switch ($key) { case 'global_validation': $changes = array($id_search_option, addslashes(TicketValidation::getStatus($oldval)), addslashes(TicketValidation::getStatus($values[$key]))); break; case 'status': $changes = array($id_search_option, addslashes(Ticket::getStatus($oldval)), addslashes(Ticket::getStatus($values[$key]))); break; case 'urgency': $changes = array($id_search_option, addslashes(Ticket::getUrgencyName($oldval)), addslashes(Ticket::getUrgencyName($values[$key]))); break; case 'impact': $changes = array($id_search_option, addslashes(Ticket::getImpactName($oldval)), addslashes(Ticket::getImpactName($values[$key]))); break; case 'priority': $changes = array($id_search_option, addslashes(Ticket::getPriorityName($oldval)), addslashes(Ticket::getPriorityName($values[$key]))); break; } } else { if ($val2["table"] == $item->getTable()) { // 2nd case : text field -> keep datas $changes = array($id_search_option, addslashes($oldval), $values[$key]); } else { // if ($val2['table'] == 'glpi_users_validation') { // $val2['table'] = 'glpi_users'; // } // other cases ; link field -> get data from dropdown if ($val2["table"] != 'glpi_complete_entities') { $changes = array($id_search_option, addslashes(Dropdown::getDropdownName($val2["table"], $oldval)), addslashes(Dropdown::getDropdownName($val2["table"], $values[$key]))); } } } break; } } } if (count($changes)) { $result = self::history($real_id, $real_type, $changes); } } return $result; }