API::updateItems PHP Method

updateItems() protected method

update an object to GLPI
protected updateItems ( $itemtype, $params = [] ) : array
$itemtype string itemtype (class) of object
$params array with theses options : - 'input' : Array of objects with fields of itemtype to be updated. Mandatory. You must provide in each object a key named 'id' to identify item to update.
return array of boolean
    protected function updateItems($itemtype, $params = array())
    {
        $this->initEndpoint();
        $input = isset($params['input']) ? $params["input"] : null;
        $item = new $itemtype();
        $response = "";
        if (is_object($input)) {
            $input = array($input);
            $isMultiple = false;
        }
        $isMultiple = true;
        if (is_array($input)) {
            $idCollection = array();
            $failed = 0;
            foreach ($input as $object) {
                if (isset($object->id)) {
                    if (!$item->getFromDB($object->id)) {
                        $failed++;
                        $idCollection[] = array($object->id => false, 'message' => __("Item not found"));
                        continue;
                    }
                    //check rights
                    if (!$item->can($object->id, UPDATE)) {
                        $failed++;
                        $idCollection[] = array($object->id => false, 'message' => __("You don't have permission to perform this action."));
                    } else {
                        //update item
                        $object = Toolbox::sanitize((array) $object);
                        $update_return = $item->update($object);
                        if ($update_return === false) {
                            $failed++;
                        }
                        $idCollection[] = array($item->fields["id"] => $update_return, 'message' => $this->getGlpiLastMessage());
                    }
                }
            }
            if ($isMultiple) {
                if ($failed == count($input)) {
                    $this->returnError($idCollection, 400, "ERROR_GLPI_UPDATE", false);
                } else {
                    if ($failed > 0) {
                        $this->returnError($idCollection, 207, "ERROR_GLPI_PARTIAL_UPDATE", false);
                    }
                }
            } else {
                if ($failed > 0) {
                    $this->returnError($idCollection[0]['message'], 400, "ERROR_GLPI_UPDATE", false);
                } else {
                    return $idCollection;
                    // Return collection, even if the request affects a single item
                }
            }
            return $idCollection;
        } else {
            $this->messageBadArrayError();
        }
    }