CommonDBTM::cleanRelationData PHP Method

cleanRelationData() public method

Clean data in the tables which have linked the deleted item Clear 1/N Relation
public cleanRelationData ( ) : nothing
return nothing
    function cleanRelationData()
    {
        global $DB, $CFG_GLPI;
        $RELATION = getDbRelations();
        if (isset($RELATION[$this->getTable()])) {
            $newval = isset($this->input['_replace_by']) ? $this->input['_replace_by'] : 0;
            foreach ($RELATION[$this->getTable()] as $tablename => $field) {
                if ($tablename[0] != '_') {
                    $itemtype = getItemTypeForTable($tablename);
                    // Code factorization : we transform the singleton to an array
                    if (!is_array($field)) {
                        $field = array($field);
                    }
                    foreach ($field as $f) {
                        foreach ($DB->request($tablename, array($f => $this->getID())) as $data) {
                            // Be carefull : we must use getIndexName because self::update rely on that !
                            if ($object = getItemForItemtype($itemtype)) {
                                $idName = $object->getIndexName();
                                // And we must ensure that the index name is not the same as the field
                                // we try to modify. Otherwise we will loose this element because all
                                // will be set to $newval ...
                                if ($idName != $f) {
                                    $object->update(array($idName => $data[$idName], $f => $newval, '_disablenotif' => true));
                                    // Disable notifs
                                }
                            }
                        }
                    }
                }
            }
        }
        // Clean ticket open against the item
        if (in_array($this->getType(), $CFG_GLPI["ticket_types"])) {
            $job = new Ticket();
            $itemsticket = new Item_Ticket();
            $query = "SELECT *\n                   FROM `glpi_items_tickets`\n                   WHERE `items_id` = '" . $this->fields['id'] . "'\n                         AND `itemtype`='" . $this->getType() . "'";
            $result = $DB->query($query);
            if ($DB->numrows($result)) {
                while ($data = $DB->fetch_assoc($result)) {
                    $cnt = countElementsInTable('glpi_items_tickets', ['tickets_id' => $data['tickets_id']]);
                    $job->getFromDB($data['tickets_id']);
                    if ($cnt == 1) {
                        if ($CFG_GLPI["keep_tickets_on_delete"] == 1) {
                            $itemsticket->delete(array("id" => $data["id"]));
                        } else {
                            $job->delete(array("id" => $data["tickets_id"]));
                        }
                    } else {
                        $itemsticket->delete(array("id" => $data["id"]));
                    }
                }
            }
        }
    }
CommonDBTM