CommonDBTM::add PHP Method

add() public method

Add an item in the database with all it's items.
public add ( array $input, $options = [], $history = true ) : integer
$input array array the _POST vars returned by the item form when press add
$history boolean do history log ? (true by default)
return integer the new ID of the added item (or false if fail)
    function add(array $input, $options = array(), $history = true)
    {
        global $DB, $CFG_GLPI;
        if ($DB->isSlave()) {
            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;
        }
        if (isset($this->input['add'])) {
            // Input from the interface
            // Save this data to be available if add fail
            $this->saveInput();
        }
        // Call the plugin hook - $this->input can be altered
        // This hook get the data from the form, not yet altered
        Plugin::doHook("pre_item_add", $this);
        if ($this->input && is_array($this->input)) {
            if (isset($this->input['add'])) {
                $this->input['_add'] = $this->input['add'];
                unset($this->input['add']);
            }
            $this->input = $this->prepareInputForAdd($this->input);
        }
        if ($this->input && is_array($this->input)) {
            // Call the plugin hook - $this->input can be altered
            // This hook get the data altered by the object method
            Plugin::doHook("post_prepareadd", $this);
        }
        if ($this->input && is_array($this->input)) {
            //Check values to inject
            $this->filterValues(!isCommandLine());
        }
        if ($this->input && is_array($this->input)) {
            $this->fields = array();
            $table_fields = $DB->list_fields($this->getTable());
            // fill array for add
            foreach ($this->input as $key => $val) {
                if ($key[0] != '_' && isset($table_fields[$key])) {
                    $this->fields[$key] = $this->input[$key];
                }
            }
            // Auto set date_creation if exsist
            if (isset($table_fields['date_creation'])) {
                $this->fields['date_creation'] = $_SESSION["glpi_currenttime"];
            }
            // Auto set date_mod if exsist
            if (isset($table_fields['date_mod'])) {
                $this->fields['date_mod'] = $_SESSION["glpi_currenttime"];
            }
            if ($this->checkUnicity(true, $options)) {
                if ($this->addToDB()) {
                    $this->post_addItem();
                    $this->addMessageOnAddAction();
                    if ($this->dohistory && $history) {
                        $changes[0] = 0;
                        $changes[1] = $changes[2] = "";
                        Log::history($this->fields["id"], $this->getType(), $changes, 0, Log::HISTORY_CREATE_ITEM);
                    }
                    // Auto create infocoms
                    if (isset($CFG_GLPI["auto_create_infocoms"]) && $CFG_GLPI["auto_create_infocoms"] && Infocom::canApplyOn($this)) {
                        $ic = new Infocom();
                        if (!$ic->getFromDBforDevice($this->getType(), $this->fields['id'])) {
                            $ic->add(array('itemtype' => $this->getType(), 'items_id' => $this->fields['id']));
                        }
                    }
                    // If itemtype is in infocomtype and if states_id field is filled
                    // and item is not a template
                    if (InfoCom::canApplyOn($this) && isset($this->input['states_id']) && (!isset($this->input['is_template']) || !$this->input['is_template'])) {
                        //Check if we have to automatical fill dates
                        Infocom::manageDateOnStatusChange($this);
                    }
                    Plugin::doHook("item_add", $this);
                    // As add have suceed, clean the old input value
                    if (isset($this->input['_add'])) {
                        $this->clearSavedInput();
                    }
                    if ($this->mailqueueonaction) {
                        QueuedMail::forceSendFor($this->getType(), $this->fields['id']);
                    }
                    // For unit test (workaround for MyIsam without transaction)
                    if (isset($DB->objcreated) && is_array($DB->objcreated)) {
                        $DB->objcreated[$this->getTable()][] = $this->fields['id'];
                    }
                    return $this->fields['id'];
                }
            }
        }
        $this->last_status = self::NOTHING_TO_DO;
        return false;
    }

Usage Example

 /**
  * Add a new item with the instance values
  *
  *@param $p_force=FALSE Force add even if no updates where done
  *@return nothing
  **/
 function addCommon($p_force = FALSE)
 {
     if (count($this->ptcdUpdates) or $p_force) {
         $itemID = parent::add($this->ptcdUpdates);
         $this->load($itemID);
     }
 }
CommonDBTM