CommonDBTM::delete PHP Method

delete() public method

Delete an item in the database.
public delete ( array $input, $force, $history = 1 ) : boolean
$input array array the _POST vars returned by the item form when press delete
$force boolean force deletion (default 0)
$history boolean do history log ? (default 1)
return boolean : true on success
    function delete(array $input, $force = 0, $history = 1)
    {
        global $DB;
        if ($DB->isSlave()) {
            return false;
        }
        if (!$this->getFromDB($input[static::getIndexName()])) {
            return false;
        }
        // Force purge for templates / may not to be deleted / not dynamic lockable items
        if ($this->isTemplate() || !$this->maybeDeleted() || $this->useDeletedToLockIfDynamic() && !$this->isDynamic()) {
            $force = 1;
        }
        // Store input in the object to be available in all sub-method / hook
        $this->input = $input;
        if (isset($this->input['purge'])) {
            $this->input['_purge'] = $this->input['purge'];
            unset($this->input['purge']);
        }
        if (isset($this->input['delete'])) {
            $this->input['_delete'] = $this->input['delete'];
            unset($this->input['delete']);
        }
        if (!isset($this->input['_no_history'])) {
            $this->input['_no_history'] = !$history;
        }
        // Purge
        if ($force) {
            Plugin::doHook("pre_item_purge", $this);
        } else {
            Plugin::doHook("pre_item_delete", $this);
        }
        if (!is_array($this->input)) {
            // $input clear by a hook to cancel delete
            return false;
        }
        if ($this->pre_deleteItem()) {
            if ($this->deleteFromDB($force)) {
                if ($force) {
                    $this->addMessageOnPurgeAction();
                    $this->post_purgeItem();
                    Plugin::doHook("item_purge", $this);
                } else {
                    $this->addMessageOnDeleteAction();
                    if ($this->dohistory && $history) {
                        $changes[0] = 0;
                        $changes[1] = $changes[2] = "";
                        $logaction = Log::HISTORY_DELETE_ITEM;
                        if ($this->useDeletedToLockIfDynamic() && $this->isDynamic()) {
                            $logaction = Log::HISTORY_LOCK_ITEM;
                        }
                        Log::history($this->fields["id"], $this->getType(), $changes, 0, $logaction);
                    }
                    $this->post_deleteItem();
                    Plugin::doHook("item_delete", $this);
                }
                if ($this->mailqueueonaction) {
                    QueuedMail::forceSendFor($this->getType(), $this->fields['id']);
                }
                return true;
            }
        }
        return false;
    }

Usage Example

 /**
  * @since version 0.85
  *
  * @see CommonDBTM::processMassiveActionsForOneItemtype()
  **/
 static function processMassiveActionsForOneItemtype(MassiveAction $ma, CommonDBTM $item, array $ids)
 {
     global $DB;
     switch ($ma->getAction()) {
         case 'transform_to':
             $input = $ma->getInput();
             if (isset($input["transform_to"]) && !empty($input["transform_to"])) {
                 $networkport = new NetworkPort();
                 foreach ($ids as $id) {
                     if ($networkport->canEdit($id) && $item->can($id, DELETE)) {
                         if (empty($networkport->fields['instantiation_type'])) {
                             if ($networkport->switchInstantiationType($input['transform_to']) !== false) {
                                 $instantiation = $networkport->getInstantiation();
                                 $input2 = $item->fields;
                                 $input2['networkports_id'] = $input2['id'];
                                 unset($input2['id']);
                                 if ($instantiation->add($input2)) {
                                     $item->delete(array('id' => $id));
                                     $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_OK);
                                 } else {
                                     $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO);
                                     $ma->addMessage($networkport->getErrorMessage(ERROR_ON_ACTION));
                                 }
                             } else {
                                 $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO);
                                 $ma->addMessage($networkport->getErrorMessage(ERROR_ON_ACTION));
                             }
                         } else {
                             $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO);
                             $ma->addMessage($networkport->getErrorMessage(ERROR_ON_ACTION));
                         }
                     } else {
                         $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_NORIGHT);
                         $ma->addMessage($networkport->getErrorMessage(ERROR_RIGHT));
                     }
                 }
             } else {
                 $ma->itemDone($item->getType(), $ids, MassiveAction::ACTION_KO);
             }
             return;
     }
     parent::processMassiveActionsForOneItemtype($ma, $item, $ids);
 }
All Usage Examples Of CommonDBTM::delete
CommonDBTM