if ($edited_Userfield->load_from_Request()) { // We could load data from form without errors: // While inserting into DB, ID property of Userfield object will be set to autogenerated ID // So far as we set ID manualy, we need to preserve this value // When assignment of wrong value will be fixed, we can skip this $entered_userfield_id = $edited_Userfield->ID; // Insert in DB: $DB->begin(); // because of manual assigning ID, // member function Userfield::dbexists() is overloaded for proper functionality $q = $edited_Userfield->dbexists(); if ($q) { // We have a duplicate entry: param_error('ufdf_ID', sprintf(T_('This user field already exists. Do you want to <a %s>edit the existing user field</a>?'), 'href="?ctrl=userfields&action=edit&ufdf_ID=' . $q . '"')); } else { $edited_Userfield->dbinsert(); $Messages->add(T_('New User field created.'), 'success'); } $DB->commit(); if (empty($q)) { // What next? switch ($action) { case 'create_copy': // Redirect so that a reload doesn't write to the DB twice: header_redirect('?ctrl=userfields&action=new&ufdf_ID=' . $edited_Userfield->ID, 303); // Will EXIT // We have EXITed already at this point!! break; case 'create_new': // Redirect so that a reload doesn't write to the DB twice: header_redirect('?ctrl=userfields&action=new', 303);
/** * Get user field group ID by name AND Try to create new if it doesn't exist yet * * @param string Field code * @param string Field group name * @param string Field name * @param string Field type: 'text', 'word', 'email', 'number', 'phone', 'url' * @return integer Field ID */ function userfield_get_by_code($field_code, $field_group_name, $field_name, $field_type) { if (is_null($this->userfields)) { // Load all user fields in cache on first time request: global $DB; $SQL = new SQL(); $SQL->SELECT('ufdf_ID, ufdf_code'); $SQL->FROM('T_users__fielddefs'); $this->userfields = $DB->get_assoc($SQL->get(), 'Load all user fields in cache array of LDAP plugin'); // Convert all user field codes to lowercase: $this->userfields = array_map('utf8_strtolower', $this->userfields); } // Code MUST be lowercase ASCII only: $field_code = utf8_strtolower($field_code); // Check if requested user field code already exists in DB: $field_ID = array_search($field_code, $this->userfields); if ($field_ID === false) { // No user field in DB, Try to create new: $field_group_ID = $this->userfield_get_group_by_name($field_group_name); if (!$field_group_ID) { // Some error on creating new user field group, We cannot create new user field without group: // Exit here: $this->debug_log(sprintf('Skip a creating of new user field "%s" because new group "%s" cannot be created.', $field_name, $field_group_name)); return; } // Load Userfield class: load_class('users/model/_userfield.class.php', 'Userfield'); $Userfield = new Userfield(); $Userfield->set('code', $field_code); $Userfield->set('ufgp_ID', $field_group_ID); $Userfield->set('name', $field_name); $Userfield->set('type', $field_type); $Userfield->set('order', $Userfield->get_last_order($field_group_ID)); $Userfield->set('duplicated', 'forbidden'); if ($Userfield->dbinsert()) { // New user field has been created $field_ID = $Userfield->ID; // Add new user field code in cache array: $this->userfields[$field_ID] = $field_code; $this->debug_log(sprintf('New user field "%s" has been created in system', $field_name)); } } return $field_ID; }