Computer_Item::prepareInputForAdd PHP Method

prepareInputForAdd() public method

Overloaded to check is Disconnect needed (during OCS sync) and to manage autoupdate feature
public prepareInputForAdd ( $input ) : the
$input array of datas used to add the item
return the modified $input array
    function prepareInputForAdd($input)
    {
        global $DB, $CFG_GLPI;
        $item = static::getItemFromArray(static::$itemtype_2, static::$items_id_2, $input);
        if (!$item instanceof CommonDBTM || $item->getField('is_global') == 0 && $this->countForItem($item) > 0) {
            return false;
        }
        $comp = static::getItemFromArray(static::$itemtype_1, static::$items_id_1, $input);
        if (!$item instanceof CommonDBTM || self::countForAll($comp, $item) > 0) {
            // no duplicates
            return false;
        }
        if (!$item->getField('is_global')) {
            // Autoupdate some fields - should be in post_addItem (here to avoid more DB access)
            $updates = array();
            if ($CFG_GLPI["is_location_autoupdate"] && $comp->fields['locations_id'] != $item->getField('locations_id')) {
                $updates['locations_id'] = addslashes($comp->fields['locations_id']);
                Session::addMessageAfterRedirect(__('Location updated. The connected items have been moved in the same location.'), true);
            }
            if ($CFG_GLPI["is_user_autoupdate"] && $comp->fields['users_id'] != $item->getField('users_id') || $CFG_GLPI["is_group_autoupdate"] && $comp->fields['groups_id'] != $item->getField('groups_id')) {
                if ($CFG_GLPI["is_user_autoupdate"]) {
                    $updates['users_id'] = $comp->fields['users_id'];
                }
                if ($CFG_GLPI["is_group_autoupdate"]) {
                    $updates['groups_id'] = $comp->fields['groups_id'];
                }
                Session::addMessageAfterRedirect(__('User or group updated. The connected items have been moved in the same values.'), true);
            }
            if ($CFG_GLPI["is_contact_autoupdate"] && ($comp->fields['contact'] != $item->getField('contact') || $comp->fields['contact_num'] != $item->getField('contact_num'))) {
                $updates['contact'] = addslashes($comp->fields['contact']);
                $updates['contact_num'] = addslashes($comp->fields['contact_num']);
                Session::addMessageAfterRedirect(__('Alternate username updated. The connected items have been updated using this alternate username.'), true);
            }
            if ($CFG_GLPI["state_autoupdate_mode"] < 0 && $comp->fields['states_id'] != $item->getField('states_id')) {
                $updates['states_id'] = $comp->fields['states_id'];
                Session::addMessageAfterRedirect(__('Status updated. The connected items have been updated using this status.'), true);
            }
            if ($CFG_GLPI["state_autoupdate_mode"] > 0 && $item->getField('states_id') != $CFG_GLPI["state_autoupdate_mode"]) {
                $updates['states_id'] = $CFG_GLPI["state_autoupdate_mode"];
            }
            if (count($updates)) {
                $updates['id'] = $input['items_id'];
                $history = true;
                if (isset($input['_no_history']) && $input['_no_history']) {
                    $history = false;
                }
                $item->update($updates, $history);
            }
        }
        return parent::prepareInputForAdd($input);
    }