Пример #1
0
 /**
  * Check field unicity before insert or update
  *
  * @param add true for insert, false for update
  * @param $options array
  *
  * @return true if item can be written in DB, false if not
  **/
 function checkUnicity($add = false, $options = array())
 {
     global $LANG, $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;
         }
     }
     $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);
                     //If update, exclude ID of the current object
                     if (!$add) {
                         $where .= " AND `" . $this->getTable() . "`.`id` NOT IN (" . $this->input['id'] . ") ";
                     }
                     if (countElementsInTable($this->table, "1 {$where} {$where_global}") > 0) {
                         if ($p['unicity_error_message'] || $p['add_event_on_duplicate']) {
                             $message = array();
                             foreach (explode(',', $fields['fields']) as $field) {
                                 $table = getTableNameForForeignKeyField($field);
                                 if ($table != '') {
                                     $searchOption = $this->getSearchOptionByField('field', 'name', $table);
                                 } else {
                                     $searchOption = $this->getSearchOptionByField('field', $field);
                                 }
                                 $message[] = $searchOption['name'] . '=' . $this->input[$field];
                             }
                             $doubles = getAllDatasFromTable($this->table, "1 {$where} {$where_global}");
                             $message_text = $this->getUncityErrorMessage($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;
                                 }
                                 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', $_SESSION["glpiname"] . " " . $LANG['log'][123] . ' : ' . $message_text);
                             }
                         }
                         if ($fields['action_refuse']) {
                             $result = false;
                         }
                         if ($fields['action_notify']) {
                             $params = array('message' => html_clean($message_text), 'action_type' => $add, 'action_user' => getUserName(getLoginUserID()), 'entities_id' => $entities_id, 'itemtype' => get_class($this), 'date' => $_SESSION['glpi_currenttime'], 'refuse' => $fields['action_refuse']);
                             NotificationEvent::raiseEvent('refuse', new FieldUnicity(), $params);
                         }
                     }
                 }
             }
         }
     }
     return $result;
 }