CommonDBTM::update PHP Method

update() public method

Update some elements of an item in the database.
public update ( array $input, $history = 1, $options = [] ) : boolean
$input array array the _POST vars returned by the item form when press update
$history boolean do history log ? (default 1)
return boolean : true on success
    function update(array $input, $history = 1, $options = array())
    {
        global $DB, $CFG_GLPI;
        if ($DB->isSlave()) {
            return false;
        }
        if (!$this->getFromDB($input[static::getIndexName()])) {
            return false;
        }
        // Store input in the object to be available in all sub-method / hook
        $this->input = $input;
        // Manage the _no_history
        if (!isset($this->input['_no_history'])) {
            $this->input['_no_history'] = !$history;
        }
        // Plugin hook - $this->input can be altered
        Plugin::doHook("pre_item_update", $this);
        if ($this->input && is_array($this->input)) {
            $this->input = $this->prepareInputForUpdate($this->input);
            if (isset($this->input['update'])) {
                $this->input['_update'] = $this->input['update'];
                unset($this->input['update']);
            }
            $this->filterValues(!isCommandLine());
        }
        // Valid input for update
        if ($this->checkUnicity(false, $options)) {
            if ($this->input && is_array($this->input)) {
                // Fill the update-array with changes
                $x = 0;
                $this->updates = array();
                $this->oldvalues = array();
                foreach ($this->input as $key => $val) {
                    if (array_key_exists($key, $this->fields)) {
                        // Prevent history for date statement (for date for example)
                        if (is_null($this->fields[$key]) && $this->input[$key] == 'NULL') {
                            $this->fields[$key] = 'NULL';
                        }
                        // Compare item
                        $ischanged = true;
                        $searchopt = $this->getSearchOptionByField('field', $key, $this->getTable());
                        if (isset($searchopt['datatype'])) {
                            switch ($searchopt['datatype']) {
                                case 'string':
                                case 'text':
                                    $ischanged = strcmp($DB->escape($this->fields[$key]), $this->input[$key]) != 0;
                                    break;
                                case 'itemlink':
                                    if ($key == 'name') {
                                        $ischanged = strcmp($DB->escape($this->fields[$key]), $this->input[$key]) != 0;
                                        break;
                                    }
                                    // else default
                                // else default
                                default:
                                    $ischanged = $DB->escape($this->fields[$key]) != $this->input[$key];
                                    break;
                            }
                        } else {
                            // No searchoption case
                            $ischanged = $DB->escape($this->fields[$key]) != $this->input[$key];
                        }
                        if ($ischanged) {
                            if ($key != "id") {
                                // Store old values
                                if (!in_array($key, $this->history_blacklist)) {
                                    $this->oldvalues[$key] = $this->fields[$key];
                                }
                                $this->fields[$key] = $this->input[$key];
                                $this->updates[$x] = $key;
                                $x++;
                            }
                        }
                    }
                }
                if (count($this->updates)) {
                    if (array_key_exists('date_mod', $this->fields)) {
                        // is a non blacklist field exists
                        if (count(array_diff($this->updates, $this->history_blacklist)) > 0) {
                            $this->fields['date_mod'] = $_SESSION["glpi_currenttime"];
                            $this->updates[$x++] = 'date_mod';
                        }
                    }
                    $this->pre_updateInDB();
                    if (count($this->updates)) {
                        if ($this->updateInDB($this->updates, $this->dohistory && $history ? $this->oldvalues : array())) {
                            $this->addMessageOnUpdateAction();
                            Plugin::doHook("item_update", $this);
                            //Fill forward_entity_to array with itemtypes coming from plugins
                            if (isset(self::$plugins_forward_entity[$this->getType()])) {
                                foreach (self::$plugins_forward_entity[$this->getType()] as $itemtype) {
                                    static::$forward_entity_to[] = $itemtype;
                                }
                            }
                            // forward entity information if needed
                            if (count(static::$forward_entity_to) && (in_array("entities_id", $this->updates) || in_array("is_recursive", $this->updates))) {
                                $this->forwardEntityInformations();
                            }
                            // If itemtype is in infocomtype and if states_id field is filled
                            // and item not a template
                            if (InfoCom::canApplyOn($this) && in_array('states_id', $this->updates) && $this->getField('is_template') != NOT_AVAILABLE) {
                                //Check if we have to automatical fill dates
                                Infocom::manageDateOnStatusChange($this, false);
                            }
                        }
                    }
                }
                $this->post_updateItem($history);
                if ($this->mailqueueonaction) {
                    QueuedMail::forceSendFor($this->getType(), $this->fields['id']);
                }
                return true;
            }
        }
        return false;
    }

Usage Example

 /**
  * @since version 0.90
  *
  * @see CommonDBTM::processMassiveActionsForOneItemtype()
  **/
 static function processMassiveActionsForOneItemtype(MassiveAction $ma, CommonDBTM $item, array $ids)
 {
     switch ($ma->getAction()) {
         case 'merge':
             foreach ($ids as $key) {
                 if ($item->can($key, UPDATE)) {
                     if ($item->getEntityID() == $_SESSION['glpiactive_entity']) {
                         if ($item->update(array('id' => $key, 'is_recursive' => 1))) {
                             $ma->itemDone($item->getType(), $key, MassiveAction::ACTION_OK);
                         } else {
                             $ma->itemDone($item->getType(), $key, MassiveAction::ACTION_KO);
                             $ma->addMessage($item->getErrorMessage(ERROR_ON_ACTION));
                         }
                     } else {
                         $input2 = $item->fields;
                         // Change entity
                         $input2['entities_id'] = $_SESSION['glpiactive_entity'];
                         $input2['is_recursive'] = 1;
                         $input2 = Toolbox::addslashes_deep($input2);
                         if (!$item->import($input2)) {
                             $ma->itemDone($item->getType(), $key, MassiveAction::ACTION_KO);
                             $ma->addMessage($item->getErrorMessage(ERROR_ON_ACTION));
                         } else {
                             $ma->itemDone($item->getType(), $key, MassiveAction::ACTION_OK);
                         }
                     }
                 } else {
                     $ma->itemDone($item->getType(), $key, MassiveAction::ACTION_NORIGHT);
                     $ma->addMessage($item->getErrorMessage(ERROR_RIGHT));
                 }
             }
             return;
     }
     parent::processMassiveActionsForOneItemtype($ma, $item, $ids);
 }
All Usage Examples Of CommonDBTM::update
CommonDBTM