CommonDBTM::checkUnicity PHP Method

checkUnicity() public method

Check field unicity before insert or update
public checkUnicity ( $add = false, $options = [] ) : true
$options array
return true if item can be written in DB, false if not
    function checkUnicity($add = false, $options = array())
    {
        global $DB, $CFG_GLPI;
        $p['unicity_error_message'] = true;
        $p['add_event_on_duplicate'] = true;
        $p['disable_unicity_check'] = false;
        if (is_array($options) && count($options)) {
            foreach ($options as $key => $value) {
                $p[$key] = $value;
            }
        }
        // Do not check for template
        if (isset($this->input['is_template']) && $this->input['is_template']) {
            return true;
        }
        $result = true;
        //Do not check unicity when creating infocoms or if checking is expliclty disabled
        if ($p['disable_unicity_check']) {
            return $result;
        }
        //Get all checks for this itemtype and this entity
        if (in_array(get_class($this), $CFG_GLPI["unicity_types"])) {
            // Get input entities if set / else get object one
            if (isset($this->input['entities_id'])) {
                $entities_id = $this->input['entities_id'];
            } else {
                $entities_id = $this->fields['entities_id'];
            }
            $all_fields = FieldUnicity::getUnicityFieldsConfig(get_class($this), $entities_id);
            foreach ($all_fields as $key => $fields) {
                //If there's fields to check
                if (!empty($fields) && !empty($fields['fields'])) {
                    $where = "";
                    $continue = true;
                    foreach (explode(',', $fields['fields']) as $field) {
                        if (isset($this->input[$field]) && (getTableNameForForeignKeyField($field) == '' && $this->input[$field] != '' || getTableNameForForeignKeyField($field) != '' && $this->input[$field] > 0) && !Fieldblacklist::isFieldBlacklisted(get_class($this), $entities_id, $field, $this->input[$field])) {
                            $where .= " AND `" . $this->getTable() . "`.`{$field}` = '" . $this->input[$field] . "'";
                        } else {
                            $continue = false;
                        }
                    }
                    if ($continue && $where != '') {
                        $entities = $fields['entities_id'];
                        if ($fields['is_recursive']) {
                            $entities = getSonsOf('glpi_entities', $fields['entities_id']);
                        }
                        $where_global = getEntitiesRestrictRequest(" AND", $this->getTable(), '', $entities);
                        $tmp = clone $this;
                        if ($tmp->maybeTemplate()) {
                            $where_global .= " AND NOT `is_template`";
                        }
                        //If update, exclude ID of the current object
                        if (!$add) {
                            $where .= " AND `" . $this->getTable() . "`.`id` NOT IN (" . $this->input['id'] . ") ";
                        }
                        if (countElementsInTable($this->getTable(), "1 {$where} {$where_global}") > 0) {
                            if ($p['unicity_error_message'] || $p['add_event_on_duplicate']) {
                                $message = array();
                                foreach (explode(',', $fields['fields']) as $field) {
                                    $message[$field] = $this->input[$field];
                                }
                                $doubles = getAllDatasFromTable($this->gettable(), "1 {$where} {$where_global}");
                                $message_text = $this->getUnicityErrorMessage($message, $fields, $doubles);
                                if ($p['unicity_error_message']) {
                                    if (!$fields['action_refuse']) {
                                        $show_other_messages = $fields['action_refuse'] ? true : false;
                                    } else {
                                        $show_other_messages = true;
                                    }
                                    Session::addMessageAfterRedirect($message_text, true, $show_other_messages, $show_other_messages);
                                }
                                if ($p['add_event_on_duplicate']) {
                                    Event::log(!$add ? $this->fields['id'] : 0, get_class($this), 4, 'inventory', sprintf(__('%1$s trying to add an item that already exists: %2$s'), $_SESSION["glpiname"], $message_text));
                                }
                            }
                            if ($fields['action_refuse']) {
                                $result = false;
                            }
                            if ($fields['action_notify']) {
                                $params = array('action_type' => $add, 'action_user' => getUserName(Session::getLoginUserID()), 'entities_id' => $entities_id, 'itemtype' => get_class($this), 'date' => $_SESSION['glpi_currenttime'], 'refuse' => $fields['action_refuse'], 'label' => $message, 'field' => $fields, 'double' => $doubles);
                                NotificationEvent::raiseEvent('refuse', new FieldUnicity(), $params);
                            }
                        }
                    }
                }
            }
        }
        return $result;
    }
CommonDBTM