/** * Update or create profile field. Also can create fields matching. * * @param array $field_data Array of profile field data * @param int $field_id Profile field id to be updated. If empty - new field will be created * @param strging $lang_code 2-letters language code * * @return int $field_id New or updated field id */ function fn_update_profile_field($field_data, $field_id, $lang_code = DESCR_SL) { if (empty($field_id)) { $add_match = false; $field_name = $field_data['field_name']; if ($field_data['section'] == 'BS') { $field_data['section'] = 'B'; $field_data['field_name'] = !empty($field_name) ? 'b_' . $field_name : ''; $add_match = true; } // Insert main data $field_id = db_query("INSERT INTO ?:profile_fields ?e", $field_data); // Insert descriptions $_data = array('object_id' => $field_id, 'object_type' => 'F', 'description' => $field_data['description']); foreach (fn_get_translation_languages() as $_data['lang_code'] => $_v) { db_query("INSERT INTO ?:profile_field_descriptions ?e", $_data); } if (substr_count('SR', $field_data['field_type']) && is_array($field_data['add_values']) && $add_match == false) { fn_add_field_values($field_data['add_values'], $field_id); } if ($add_match == true) { $field_data['section'] = 'S'; $field_data['field_name'] = !empty($field_name) ? 's_' . $field_name : ''; $field_data['matching_id'] = $field_id; // Update match for the billing field $s_field_id = fn_update_profile_field($field_data, 0, $lang_code); if (!empty($s_field_id)) { db_query('UPDATE ?:profile_fields SET matching_id = ?i WHERE field_id = ?i', $s_field_id, $field_id); } } } else { db_query("UPDATE ?:profile_fields SET ?u WHERE field_id = ?i", $field_data, $field_id); if (!empty($field_data['matching_id']) && $field_data['section'] == 'S') { db_query('UPDATE ?:profile_fields SET field_type = ?s WHERE field_id = ?i', $field_data['field_type'], $field_data['matching_id']); } db_query("UPDATE ?:profile_field_descriptions SET ?u WHERE object_id = ?i AND object_type = 'F' AND lang_code = ?s", $field_data, $field_id, $lang_code); if (!empty($field_data['field_type'])) { if (strpos('SR', $field_data['field_type']) !== false) { if (!empty($field_data['values'])) { foreach ($field_data['values'] as $value_id => $vdata) { db_query("UPDATE ?:profile_field_values SET ?u WHERE value_id = ?i", $vdata, $value_id); db_query("UPDATE ?:profile_field_descriptions SET ?u WHERE object_id = ?i AND object_type = 'V' AND lang_code = ?s", $vdata, $value_id, $lang_code); } // Completely delete removed values $existing_ids = db_get_fields("SELECT value_id FROM ?:profile_field_values WHERE field_id = ?i", $field_id); $val_ids = array_diff($existing_ids, array_keys($field_data['values'])); if (!empty($val_ids)) { fn_delete_field_values($field_id, $val_ids); } } else { if (isset($field_data['add_values'])) { fn_delete_field_values($field_id); } } if (!empty($field_data['add_values']) && is_array($field_data['add_values'])) { fn_add_field_values($field_data['add_values'], $field_id); } } else { fn_delete_field_values($field_id); } } } return $field_id; }
function fn_add_profile_fields($fields) { if (empty($fields)) { return false; } foreach ($fields as $v) { $add_match = false; if (empty($v['description'])) { continue; } if ($v['section'] == 'BS') { $v['section'] = 'B'; $add_match = true; } // Insert main data $_data = fn_check_table_fields($v, 'profile_fields'); $field_id = db_query("INSERT INTO ?:profile_fields ?e", $_data); // Insert descriptions $_data = array('object_id' => $field_id, 'object_type' => 'F', 'description' => $v['description']); foreach ((array) Registry::get('languages') as $_data['lang_code'] => $_v) { db_query("INSERT INTO ?:profile_field_descriptions ?e", $_data); } if (substr_count('SR', $v['field_type']) && is_array($v['values']) && $add_match == false) { fn_add_field_values($v['values'], $field_id); } if ($add_match == true) { $v['section'] = 'S'; $v['matching_id'] = $field_id; fn_add_profile_fields(array($v)); } } }