API::deleteItems PHP Method

deleteItems() protected method

delete one or more objects in GLPI
protected deleteItems ( $itemtype, $params = [] ) : boolean
$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 delete.* - 'force_purge' : boolean, if itemtype have a dustbin, you can force purge (delete finally). Optionnal. - 'history' : boolean, default true, false to disable saving of deletion in global history. Optionnal.
return boolean or array of boolean
    protected function deleteItems($itemtype, $params = array())
    {
        $this->initEndpoint();
        $default = array('force_purge' => false, 'history' => true);
        $params = array_merge($default, $params);
        $input = $params['input'];
        $item = new $itemtype();
        $response = "";
        if (is_object($input)) {
            $input = array($input);
            $isMultiple = false;
        } else {
            $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;
                    }
                    // Force purge for templates / may not to be deleted / not dynamic lockable items
                    // see CommonDBTM::delete()
                    // Needs factorization
                    if ($item->isTemplate() || !$item->maybeDeleted() || $item->useDeletedToLockIfDynamic() && !$item->isDynamic()) {
                        $params['force_purge'] = 1;
                    } else {
                        $params['force_purge'] = filter_var($params['force_purge'], FILTER_VALIDATE_BOOLEAN);
                    }
                    //check rights
                    if ($params['force_purge'] && !$item->can($object->id, PURGE) || !$params['force_purge'] && !$item->can($object->id, DELETE)) {
                        $failed++;
                        $idCollection[] = array($object->id => false, 'message' => __("You don't have permission to perform this action."));
                    } else {
                        //delete item
                        $delete_return = $item->delete((array) $object, $params['force_purge'], $params['history']);
                        if ($delete_return === false) {
                            $failed++;
                        }
                        $idCollection[] = array($object->id => $delete_return, 'message' => $this->getGlpiLastMessage());
                    }
                }
            }
            if ($isMultiple) {
                if ($failed == count($input)) {
                    $this->returnError($idCollection, 400, "ERROR_GLPI_DELETE", false);
                } else {
                    if ($failed > 0) {
                        $this->returnError($idCollection, 207, "ERROR_GLPI_PARTIAL_DELETE", false);
                    }
                }
            } else {
                if ($failed > 0) {
                    $this->returnError($idCollection[0]['message'], 400, "ERROR_GLPI_DELETE", false);
                } else {
                    return $idCollection;
                    // Return collection, even if the request affects a single item
                }
            }
            return $idCollection;
        } else {
            $this->messageBadArrayError();
        }
    }