From c87c20b3839c2a15816dbb4cee11f4d46adc7bae Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Thu, 13 Sep 2012 14:11:35 +0200 Subject: [PATCH] Reordering extrafields classes --- main/admin/configure_inscription.php | 2 +- main/admin/fields_options.php | 175 ++++++++ main/admin/session_list.php | 7 +- main/admin/user_add.php | 2 +- main/auth/inscription.php | 2 +- main/auth/profile.php | 4 +- main/inc/ajax/extra_field.ajax.php | 10 +- main/inc/ajax/model.ajax.php | 2 +- main/inc/lib/autoload.class.php | 7 +- main/inc/lib/extra_field.lib.php | 494 ++++++++++++++++++++++ main/inc/lib/extra_field_option.lib.php | 244 +++++++++++ main/inc/lib/extra_field_value.lib.php | 183 ++++++++ main/inc/lib/session_field.lib.php | 141 +----- main/inc/lib/session_field_option.lib.php | 235 +--------- main/inc/lib/session_field_value.lib.php | 177 +------- main/inc/lib/sessionmanager.lib.php | 4 +- main/inc/lib/usermanager.lib.php | 361 +--------------- main/survey/fillsurvey.php | 2 +- main/survey/survey.lib.php | 18 +- main/user/subscribe_user.php | 10 +- 20 files changed, 1169 insertions(+), 911 deletions(-) create mode 100644 main/admin/fields_options.php create mode 100644 main/inc/lib/extra_field.lib.php create mode 100644 main/inc/lib/extra_field_option.lib.php create mode 100644 main/inc/lib/extra_field_value.lib.php diff --git a/main/admin/configure_inscription.php b/main/admin/configure_inscription.php index d205e64e94..e1c954bd4d 100644 --- a/main/admin/configure_inscription.php +++ b/main/admin/configure_inscription.php @@ -277,7 +277,7 @@ if ($display_all_form) { } } $extra_data = UserManager::get_extra_user_data(api_get_user_id(), true); - UserManager::set_extra_fields_in_form($form, $extra_data, 'registration', false, null, 'user'); + ExtraField::set_extra_fields_in_form($form, $extra_data, 'registration', false, null, 'user'); } // Terms and conditions diff --git a/main/admin/fields_options.php b/main/admin/fields_options.php new file mode 100644 index 0000000000..841feadd79 --- /dev/null +++ b/main/admin/fields_options.php @@ -0,0 +1,175 @@ + 'index.php', 'name' => get_lang('PlatformAdmin')); +$interbreadcrumb[] = array('url' => 'user_fields.php', 'name' => get_lang('UserFields')); +$interbreadcrumb[] = array('url' => 'user_fields_add.php?action=edit&field_id=' . Security::remove_XSS($_GET['field_id']) . '&sec_token=' . $_SESSION['sec_token'], 'name' => get_lang('EditUserFields')); + +// name of the tools +$tool_name = get_lang('UserFieldsSortOptions'); + +// display header +Display::display_header($tool_name); + +if (isset($_GET['action'])) { + $check = Security::check_token('get'); + if ($check) { + switch ($_GET['action']) { + case 'moveup' : + if (api_is_platform_admin() && !empty($_GET['option_id'])) { + if (move_user_field_option('moveup', $_GET['option_id'])) { + Display :: display_confirmation_message(get_lang('FieldOptionMovedUp')); + } else { + Display :: display_error_message(get_lang('CannotMoveFieldOption')); + } + } + break; + case 'movedown' : + if (api_is_platform_admin() && !empty($_GET['option_id'])) { + if (move_user_field_option('movedown', $_GET['option_id'])) { + Display :: display_confirmation_message(get_lang('FieldOptionMovedDown')); + } else { + Display :: display_error_message(get_lang('CannotMoveFieldOption')); + } + } + break; + } + } +} + +// getting all the information of the field +$field_info = UserManager::get_extra_field_information($_GET['field_id']); +echo Display::page_header($field_info['3']); + +// the total number of options (used in the actions_filter function but declared here for performance reasons) +$number_of_options = get_number_of_options(); + +// displaying the sortable table +$parameters['sec_token'] = Security::get_token(); +$parameters['field_id'] = Security::remove_XSS($_GET['field_id']); +$table = new SortableTable('options', 'get_number_of_options', 'get_options_data', 2); +$table->set_additional_parameters($parameters); +$table->set_header(0, get_lang('DisplayOrder'), false); +$table->set_header(1, get_lang('OptionText'), false); +$table->set_header(2, get_lang('Actions'), false); +$table->set_column_filter(2, 'actions_filter'); +$table->display(); + +// display footer +Display::display_footer(); + +function get_options_data($from, $number_of_items, $column, $direction) { + // Database table definition + $table_userfields_options = Database :: get_main_table(TABLE_MAIN_USER_FIELD_OPTIONS); + + // The sql statement + $sql = "SELECT + option_order AS col0, + option_display_text AS col1, + id AS col2 + FROM $table_userfields_options WHERE field_id='" . Database::escape_string($_GET['field_id']) . "' ORDER BY option_order ASC"; + $sql .= " LIMIT $from,$number_of_items"; + $res = Database::query($sql); + $return = array(); + while ($option = Database::fetch_row($res)) { + $return[] = $option; + } + return $return; +} + +function get_number_of_options($from = null, $number_of_items = null, $column = null, $direction = null) { + // Database table definition + $table_userfields_options = Database :: get_main_table(TABLE_MAIN_USER_FIELD_OPTIONS); + + // The sql statement + $sql = "SELECT count(id) as total FROM $table_userfields_options WHERE field_id='" . Database::escape_string($_GET['field_id']) . "' "; + $res = Database::query($sql); + $row = Database::fetch_row($res); + return $row[0]; +} + +function actions_filter($option_id, $url_params, $row) { + global $number_of_options; + + if ($row[0] <> 1) { + $return .= '' . Display::return_icon('up.gif', get_lang('Up')) . ''; + } else { + $return .= Display::return_icon('blank.gif', '', array('width' => '21px')); + } + + // the down icon only has to appear when the row can be moved down (all but the last row) + if ($row[0] <> $number_of_options) { + $return .= '' . Display::return_icon('down.gif', get_lang('Down')) . ''; + } + return $return; +} + +/** + * Move a user defined field option up or down + * + * @param string $direction the direction we have to move the field to (up or down) + * @param unknown_type $field_id + * + * @author Patrick Cool , Ghent University, Belgium + * @version July 2008 + * @since Dokeos 1.8.6 + */ +function move_user_field_option($direction, $option_id) { + // Database table definition + $table_userfields_options = Database :: get_main_table(TABLE_MAIN_USER_FIELD_OPTIONS); + + // check the parameters + if (!in_array($direction, array('moveup', 'movedown')) OR !is_numeric($option_id)) { + return false; + } + + // determine the SQL sort direction + if ($direction == 'moveup') { + $sortdirection = 'DESC'; + } else { + $sortdirection = 'ASC'; + } + + $found = false; + + $sql = "SELECT id, option_order FROM $table_userfields_options WHERE field_id='" . Database::escape_string($_GET['field_id']) . "' ORDER BY option_order $sortdirection"; + $result = Database::query($sql); + while ($row = Database::fetch_array($result)) { + if ($found) { + $next_id = $row['id']; + $next_order = $row['option_order']; + break; + } + + if ($option_id == $row['id']) { + $this_id = $row['id']; + $this_order = $row['option_order']; + $found = true; + } + } + + $sql1 = "UPDATE " . $table_userfields_options . " SET option_order = '" . Database::escape_string($next_order) . "' WHERE id = '" . Database::escape_string($this_id) . "'"; + $sql2 = "UPDATE " . $table_userfields_options . " SET option_order = '" . Database::escape_string($this_order) . "' WHERE id = '" . Database::escape_string($next_id) . "'"; + Database::query($sql1); + Database::query($sql2); + return true; +} \ No newline at end of file diff --git a/main/admin/session_list.php b/main/admin/session_list.php index 9bb5fa2008..5d5fc97ea1 100644 --- a/main/admin/session_list.php +++ b/main/admin/session_list.php @@ -112,11 +112,10 @@ $rules[] = array( "field" => "display_end_date", "op" => "le", "data" => api_get //$rules[] = array( "field" => "course_code", "op" => "cn", "data" => ''); if (!empty($fields)) { - foreach ($fields as $field) { - $search_options = array(); - + foreach ($fields as $field) { + $search_options = array(); $type = 'text'; - if ($field['field_type'] == UserManager::USER_FIELD_TYPE_SELECT) { + if ($field['field_type'] == ExtraField::FIELD_TYPE_SELECT) { $type = 'select'; $search_options['sopt'] = array('eq', 'ne'); //equal not equal } else { diff --git a/main/admin/user_add.php b/main/admin/user_add.php index 74e97e40da..5923c4be24 100644 --- a/main/admin/user_add.php +++ b/main/admin/user_add.php @@ -207,7 +207,7 @@ $form->addElement('radio', 'active', '', get_lang('Inactive'), 0); $extra_data = UserManager::get_extra_user_data(0, true); -$return_params = UserManager::set_extra_fields_in_form($form, $extra_data, 'user_add', false, null, 'user'); +$return_params = ExtraField::set_extra_fields_in_form($form, $extra_data, 'user_add', false, null, 'user'); $jquery_ready_content = $return_params['jquery_ready_content']; // the $jquery_ready_content variable collects all functions that will be load in the $(document).ready javascript function diff --git a/main/auth/inscription.php b/main/auth/inscription.php index ed3fc67b7f..d2f9f53398 100644 --- a/main/auth/inscription.php +++ b/main/auth/inscription.php @@ -155,7 +155,7 @@ if ($user_already_registered_show_terms == false) { } // EXTRA FIELDS $extra_data = UserManager::get_extra_user_data(api_get_user_id(), true); - UserManager::set_extra_fields_in_form($form, $extra_data, 'registration', false, null, 'user'); + ExtraField::set_extra_fields_in_form($form, $extra_data, 'registration', false, null, 'user'); } if (isset($_SESSION['user_language_choice']) && $_SESSION['user_language_choice'] != '') { diff --git a/main/auth/profile.php b/main/auth/profile.php index c800eb9b6a..b2b77fa2eb 100644 --- a/main/auth/profile.php +++ b/main/auth/profile.php @@ -280,7 +280,7 @@ if (is_platform_authentication() && is_profile_editable() && api_get_setting('pr // EXTRA FIELDS $extra_data = UserManager::get_extra_user_data(api_get_user_id(), true); -$return_params = UserManager::set_extra_fields_in_form($form, $extra_data, 'profile', api_get_user_id(), 'user'); +$return_params = ExtraField::set_extra_fields_in_form($form, $extra_data, 'profile', api_get_user_id(), 'user'); $jquery_ready_content = $return_params['jquery_ready_content']; // the $jquery_ready_content variable collects all functions that will be load in the $(document).ready javascript function @@ -612,7 +612,7 @@ if ($form->validate()) { // User tag process //1. Deleting all user tags - $list_extra_field_type_tag = UserManager::get_all_extra_field_by_type(UserManager::USER_FIELD_TYPE_TAG); + $list_extra_field_type_tag = UserManager::get_all_extra_field_by_type(ExtraField::FIELD_TYPE_TAG); if (is_array($list_extra_field_type_tag) && count($list_extra_field_type_tag)>0) { foreach ($list_extra_field_type_tag as $id) { diff --git a/main/inc/ajax/extra_field.ajax.php b/main/inc/ajax/extra_field.ajax.php index d4dde072ca..42ff016b49 100644 --- a/main/inc/ajax/extra_field.ajax.php +++ b/main/inc/ajax/extra_field.ajax.php @@ -11,14 +11,8 @@ switch ($action) { $option_value_id = isset($_REQUEST['option_value_id']) ? $_REQUEST['option_value_id'] : null; if (!empty($type) && !empty($field_id) && !empty($option_value_id)) { - switch($type) { - case 'session': - $session_field_options = new SessionFieldOption(); - echo $session_field_options->get_second_select_field_options_by_field($field_id, $option_value_id, true); - break; - case 'user': - break; - } + $field_options = new ExtraFieldOption($type); + echo $field_options->get_second_select_field_options_by_field($field_id, $option_value_id, true); } break; default: diff --git a/main/inc/ajax/model.ajax.php b/main/inc/ajax/model.ajax.php index 597bd16cec..4f254a5c24 100644 --- a/main/inc/ajax/model.ajax.php +++ b/main/inc/ajax/model.ajax.php @@ -87,7 +87,7 @@ if ($_REQUEST['_search'] == 'true') { $where_condition .= get_where_clause($field, $rule->op, $rule->data); } else { $original_field = str_replace('extra_', '', $rule->field); - $session_field_option = $session_field->get_session_field_info_by_field_variable($original_field); + $session_field_option = $session_field->get_handler_field_info_by_field_variable($original_field); $extra_fields[] = array( 'field' => $rule->field, diff --git a/main/inc/lib/autoload.class.php b/main/inc/lib/autoload.class.php index 04e63b18ff..87a6856f9e 100644 --- a/main/inc/lib/autoload.class.php +++ b/main/inc/lib/autoload.class.php @@ -492,7 +492,7 @@ class Autoload $result['Entity\SearchEngineRef'] = '/main/inc/entity/search_engine_ref.class.php'; $result['Entity\Session'] = '/main/inc/entity/session.class.php'; $result['Entity\SessionCategory'] = '/main/inc/entity/session_category.class.php'; - $result['Entity\SessionField'] = '/main/inc/entity/session_field.class.php'; + $result['Entity\SessionField'] = '/main/inc/entity/session_field.class.php'; $result['Entity\SessionFieldValues'] = '/main/inc/entity/session_field_values.class.php'; $result['Entity\SessionRelCourse'] = '/main/inc/entity/session_rel_course.class.php'; $result['Entity\SessionRelCourseRelUser'] = '/main/inc/entity/session_rel_course_rel_user.class.php'; @@ -584,6 +584,11 @@ class Autoload $result['EventsMail'] = '/main/inc/lib/events_email.class.php'; $result['Exercise'] = '/main/exercice/exercise.class.php'; $result['ExerciseLink'] = '/main/gradebook/lib/be/exerciselink.class.php'; + + $result['ExtraField'] = '/main/inc/lib/extra_field.lib.php'; + $result['ExtraFieldOption'] = '/main/inc/lib/extra_field_option.lib.php'; + $result['ExtraFieldValue'] = '/main/inc/lib/extra_field_value.lib.php'; + $result['Testcategory'] = '/main/exercice/testcategory.class.php'; $result['ExerciseResult'] = '/main/exercice/exercise_result.class.php'; $result['ExerciseShowFunctions'] = '/main/inc/lib/exercise_show_functions.lib.php'; diff --git a/main/inc/lib/extra_field.lib.php b/main/inc/lib/extra_field.lib.php new file mode 100644 index 0000000000..abe1dcb5c4 --- /dev/null +++ b/main/inc/lib/extra_field.lib.php @@ -0,0 +1,494 @@ +type = $type; + switch ($this->type) { + case 'user': + //$this->table_field = Database::get_main_table(TABLE_MAIN_USER_FIELD); + $this->table_field_options = Database::get_main_table(TABLE_MAIN_USER_FIELD_OPTIONS); + $this->table_field_values = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES); + + //Used for the model + $this->table = Database::get_main_table(TABLE_MAIN_USER_FIELD); + $this->handler_id = 'user_id'; + break; + case 'session': + //$this->table_field = Database::get_main_table(TABLE_MAIN_SESSION_FIELD); + $this->table_field_options = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_OPTIONS); + $this->table_field_values = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_VALUES); + $this->handler_id = 'session_id'; + + $this->table = Database::get_main_table(TABLE_MAIN_SESSION_FIELD); + break; + } + } + + public function get_count() { + $row = Database::select('count(*) as count', $this->table, array(), 'first'); + return $row['count']; + } + + + public function get_all($where_conditions = array()) { + $options = Database::select('*', $this->table, array('where'=>$where_conditions,'order' =>'field_display_text ASC')); + $sesion_field_option = new SessionFieldOption(); + if (!empty($options)) { + foreach ($options as &$option) { + $option['options'] = $sesion_field_option->get_field_options_by_field($option['id']); + } + } + return $options; + } + + + public function get_handler_field_info_by_field_variable($field_variable) { + $field_variable = Database::escape_string($field_variable); + $sql_field = "SELECT * FROM {$this->table} WHERE field_variable = '$field_variable'"; + $result = Database::query($sql_field); + if (Database::num_rows($result)) { + $r_field = Database::fetch_array($result, 'ASSOC'); + return $r_field; + } else { + return false; + } + } + + public function get_max_field_order() { + $sql = "SELECT MAX(field_order) FROM {$this->table}"; + $res = Database::query($sql); + + $order = 0; + if (Database::num_rows($res)>0) { + $row = Database::fetch_row($res); + $order = $row[0]+1; + } + return $order; + } + + public static function get_extra_fields_by_handler($handler) { + $types = array(); + $types[self::FIELD_TYPE_TEXT] = get_lang('FieldTypeText'); + $types[self::FIELD_TYPE_TEXTAREA] = get_lang('FieldTypeTextarea'); + $types[self::FIELD_TYPE_RADIO] = get_lang('FieldTypeRadio'); + $types[self::FIELD_TYPE_SELECT] = get_lang('FieldTypeSelect'); + $types[self::FIELD_TYPE_SELECT_MULTIPLE] = get_lang('FieldTypeSelectMultiple'); + $types[self::FIELD_TYPE_DATE] = get_lang('FieldTypeDate'); + $types[self::FIELD_TYPE_DATETIME] = get_lang('FieldTypeDatetime'); + $types[self::FIELD_TYPE_DOUBLE_SELECT] = get_lang('FieldTypeDoubleSelect'); + $types[self::FIELD_TYPE_DIVIDER] = get_lang('FieldTypeDivider'); + $types[self::FIELD_TYPE_TAG] = get_lang('FieldTypeTag'); + $types[self::FIELD_TYPE_TIMEZONE] = get_lang('FieldTypeTimezone'); + $types[self::FIELD_TYPE_SOCIAL_PROFILE] = get_lang('FieldTypeSocialProfile'); + + switch ($handler) { + case 'session': + unset($types[self::FIELD_TYPE_TAG]); + unset($types[self::FIELD_TYPE_SOCIAL_PROFILE]); + break; + case 'user': + break; + } + return $types; + } + + public static function get_all_extra_field_by_type($field_type) { + // all the information of the field + $sql = "SELECT * FROM {$this->table} WHERE field_type='".Database::escape_string($field_type)."'"; + $result = Database::query($sql); + $return = array(); + while ($row = Database::fetch_array($result)) { + $return[] = $row['id']; + } + return $return; + } + + + public function get_field_types() { + return self::get_extra_fields_by_handler($this->type); + } + + public function get_field_type_by_id($id) { + $types = self::get_field_types(); + if (isset($types[$id])) { + return $types[$id]; + } + return null; + } + + + /** + * Converts a string like this: + * France:Paris;Bretagne;Marseilles;Lyon|Belgique:Bruxelles;Namur;Liège;Bruges|Peru:Lima;Piura; + * into + * array('France' => array('Paris', 'Bregtane', 'Marseilles'), 'Belgique' => array('Namur', 'Liège', etc + * @param string $string + * @return array + */ + static function extra_field_double_select_convert_string_to_array($string) { + $options = explode('|', $string); + $options_parsed = array(); + $id = 0; + if (!empty($options)) { + foreach ($options as $sub_options) { + $options = explode(':', $sub_options); + $sub_sub_options = explode(';', $options[1]); + $options_parsed[$id] = array('label' => $options[0], 'options' => $sub_sub_options); + $id++; + } + } + return $options_parsed; + } + + static function extra_field_double_select_convert_array_to_ordered_array($options) { + $options_parsed = array(); + if (!empty($options)) { + foreach ($options as $option) { + if ($option['option_value'] == 0 ) { + $options_parsed[$option['id']][] = $option; + } else { + $options_parsed[$option['option_value']][] = $option; + } + } + } + return $options_parsed; + } + + /** + + * @param array options the result of the get_field_options_by_field() array + */ + static function extra_field_double_select_convert_array_to_string($options) { + $string = null; + //var_dump($options); + $options_parsed = self::extra_field_double_select_convert_array_to_ordered_array($options); + + if (!empty($options_parsed)) { + foreach ($options_parsed as $option) { + foreach ($option as $key => $item) { + $string .= $item['option_display_text']; + if ($key == 0) { + $string .= ':'; + } else { + if (isset($option[$key+1])) { + $string .= ';'; + } + } + } + $string .= '|'; + } + } + + if (!empty($string)) { + $string = substr($string, 0, strlen($string)-1); + } + return $string; + } + + + function clean_parameters($params) { + if (!isset($params['field_variable']) || empty($params['field_variable'])) { + $params['field_variable'] = trim(strtolower(str_replace(" ","_", $params['field_display_text']))); + } + + if (!isset($params['field_order'])) { + $max_order = self::get_max_field_order(); + $params['field_order'] = $max_order; + } + return $params; + } + + public function save($params, $show_query = false) { + $session_field_info = self::get_handler_field_info_by_field_variable($params['field_variable']); + $params = self::clean_parameters($params); + if ($session_field_info) { + return $session_field_info['id']; + } else { + if (!isset($params['tms'])) { + $params['tms'] = api_get_utc_datetime(); + } + $id = parent::save($params, $show_query); + if ($id) { + $session_field_option = new SessionFieldOption(); + $params['field_id'] = $id; + $session_field_option->save($params); + } + return $id; + } + } + + + public function update($params) { + $params = self::clean_parameters($params); + if (isset($params['id'])) { + $field_option = new ExtraFieldOption($this->type); + $params['field_id'] = $params['id']; + $field_option->save($params); + } + parent::update($params); + } + + public function delete($id) { + parent::delete($id); + $field_option = new ExtraFieldOption($this->type); + $field_option->delete_all_options_by_field_id($id); + + $session_field_values = new ExtraFieldValue($this->type); + $session_field_values->delete_all_values_by_field_id($id); + } + + static function set_extra_fields_in_form($form, $extra_data, $form_name, $admin_permissions = false, $user_id = null, $type = 'user', $extra = null) { + $user_id = intval($user_id); + + // User extra fields + if ($type == 'user') { + $extra = UserManager::get_extra_fields(0, 50, 5, 'ASC', true, null, true); + } + + $jquery_ready_content = null; + + if (!empty($extra)) + foreach ($extra as $field_details) { + if (!$admin_permissions) { + if ($field_details['field_visible'] == 0) { + continue; + } + } + switch ($field_details['field_type']) { + case ExtraField::FIELD_TYPE_TEXT: + $form->addElement('text', 'extra_'.$field_details['field_variable'], $field_details['field_display_text'], array('class' => 'span4')); + $form->applyFilter('extra_'.$field_details['field_variable'], 'stripslashes'); + $form->applyFilter('extra_'.$field_details['field_variable'], 'trim'); + if (!$admin_permissions) { + if ($field_details['field_visible'] == 0) $form->freeze('extra_'.$field_details['field_variable']); + } + break; + case ExtraField::FIELD_TYPE_TEXTAREA: + $form->add_html_editor('extra_'.$field_details['field_variable'], $field_details['field_display_text'], false, false, array('ToolbarSet' => 'Profile', 'Width' => '100%', 'Height' => '130')); + //$form->addElement('textarea', 'extra_'.$field_details['field_variable'], $field_details['field_display_text'], array('size' => 80)); + $form->applyFilter('extra_'.$field_details['field_variable'], 'stripslashes'); + $form->applyFilter('extra_'.$field_details['field_variable'], 'trim'); + if (!$admin_permissions) { + if ($field_details['field_visible'] == 0) $form->freeze('extra_'.$field_details['field_variable']); + } + break; + case ExtraField::FIELD_TYPE_RADIO: + $group = array(); + foreach ($field_details['options'] as $option_id => $option_details) { + $options[$option_details['option_value']] = $option_details['option_display_text']; + $group[] = $form->createElement('radio', 'extra_'.$field_details['field_variable'], $option_details['option_value'],$option_details['option_display_text'].'
',$option_details['option_value']); + } + $form->addGroup($group, 'extra_'.$field_details['field_variable'], $field_details['field_display_text'], ''); + if (!$admin_permissions) { + if ($field_details['field_visible'] == 0) $form->freeze('extra_'.$field_details['field_variable']); + } + break; + case ExtraField::FIELD_TYPE_SELECT: + $get_lang_variables = false; + if (in_array($field_details['field_variable'], array('mail_notify_message','mail_notify_invitation', 'mail_notify_group_message'))) { + $get_lang_variables = true; + } + $options = array(); + + foreach ($field_details['options'] as $option_id => $option_details) { + //$options[$option_details['option_value']] = $option_details['option_display_text']; + if ($get_lang_variables) { + $options[$option_details['option_value']] = get_lang($option_details['option_display_text']); + } else { + $options[$option_details['option_value']] = $option_details['option_display_text']; + } + } + if ($get_lang_variables) { + $field_details['field_display_text'] = get_lang($field_details['field_display_text']); + } + //chzn-select doesn't work for sessions?? + $form->addElement('select','extra_'.$field_details['field_variable'], $field_details['field_display_text'], $options, array('class'=>'', 'id'=>'extra_'.$field_details['field_variable'])); + if (!$admin_permissions) { + if ($field_details['field_visible'] == 0) { + $form->freeze('extra_'.$field_details['field_variable']); + } + } + break; + case ExtraField::FIELD_TYPE_SELECT_MULTIPLE: + $options = array(); + foreach ($field_details['options'] as $option_id => $option_details) { + $options[$option_details['option_value']] = $option_details['option_display_text']; + } + $form->addElement('select','extra_'.$field_details['field_variable'], $field_details['field_display_text'], $options, array('multiple' => 'multiple')); + if (!$admin_permissions) { + if ($field_details['field_visible'] == 0) { + $form->freeze('extra_'.$field_details['field_variable']); + } + } + break; + case ExtraField::FIELD_TYPE_DATE: + $form->addElement('datepickerdate', 'extra_'.$field_details['field_variable'], $field_details['field_display_text'], array('form_name' => $form_name)); + $form->_elements[$form->_elementIndex['extra_'.$field_details['field_variable']]]->setLocalOption('minYear', 1900); + $defaults['extra_'.$field_details['field_variable']] = date('Y-m-d 12:00:00'); + $form -> setDefaults($defaults); + if (!$admin_permissions) { + if ($field_details['field_visible'] == 0) { + $form->freeze('extra_'.$field_details['field_variable']); + } + } + $form->applyFilter('theme', 'trim'); + break; + case ExtraField::FIELD_TYPE_DATETIME: + $form->addElement('datepicker', 'extra_'.$field_details['field_variable'], $field_details['field_display_text'], array('form_name' => $form_name)); + $form->_elements[$form->_elementIndex['extra_'.$field_details['field_variable']]]->setLocalOption('minYear', 1900); + $defaults['extra_'.$field_details['field_variable']] = date('Y-m-d 12:00:00'); + $form -> setDefaults($defaults); + if (!$admin_permissions) { + if ($field_details['field_visible'] == 0) { + $form->freeze('extra_'.$field_details['field_variable']); + } + } + $form->applyFilter('theme', 'trim'); + break; + case ExtraField::FIELD_TYPE_DOUBLE_SELECT: + $first_select_id = 'first_extra_'.$field_details['field_variable']; + + $url = api_get_path(WEB_AJAX_PATH).'extra_field.ajax.php?1=1'; + + $jquery_ready_content .= ' + $("#'.$first_select_id.'").on("change", function() { + var id = $(this).val(); + $.ajax({ + url: "'.$url.'&a=get_second_select_options", + dataType: "json", + data: "type='.$type.'&field_id='.$field_details['id'].'&option_value_id="+id, + success: function(data) { + $("#second_extra_'.$field_details['field_variable'].'").empty(); + $.each(data, function(index, value) { + $("#second_extra_'.$field_details['field_variable'].'").append($("'; + } + } + + $multi_select = ''; + + $form->addElement('label',$field_details['field_display_text'], $multi_select); + $url = api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php'; + $complete_text = get_lang('StartToType'); + //if cache is set to true the jquery will be called 1 time + $field_variable = $field_details['field_variable']; + $field_id = $field_details['id']; + $jquery_ready_content .= <<addElement('select', 'extra_'.$field_details['field_variable'], $field_details['field_display_text'], api_get_timezones(), ''); + if ($field_details['field_visible'] == 0) $form->freeze('extra_'.$field_details['field_variable']); + break; + case ExtraField::FIELD_TYPE_SOCIAL_PROFILE: + // get the social network's favicon + $icon_path = UserManager::get_favicon_from_url($extra_data['extra_'.$field_details['field_variable']], $field_details['field_default_value']); + // special hack for hi5 + $leftpad = '1.7'; + $top = '0.4'; + $domain = parse_url($icon_path, PHP_URL_HOST); + if ($domain == 'www.hi5.com' or $domain == 'hi5.com') { + $leftpad = '3'; $top = '0'; + } + // print the input field + $form->addElement('text', 'extra_'.$field_details['field_variable'], $field_details['field_display_text'], array('size' => 60, 'style' => 'background-image: url(\''.$icon_path.'\'); background-repeat: no-repeat; background-position: 0.4em '.$top.'em; padding-left: '.$leftpad.'em; ')); + $form->applyFilter('extra_'.$field_details['field_variable'], 'stripslashes'); + $form->applyFilter('extra_'.$field_details['field_variable'], 'trim'); + if ($field_details['field_visible'] == 0) { + $form->freeze('extra_'.$field_details['field_variable']); + } + break; + } + } + $return = array(); + $return['jquery_ready_content'] = $jquery_ready_content; + return $return; + } + + +} \ No newline at end of file diff --git a/main/inc/lib/extra_field_option.lib.php b/main/inc/lib/extra_field_option.lib.php new file mode 100644 index 0000000000..3a85368934 --- /dev/null +++ b/main/inc/lib/extra_field_option.lib.php @@ -0,0 +1,244 @@ +type = $type; + switch ($this->type) { + case 'user': + $this->table = Database::get_main_table(TABLE_MAIN_USER_FIELD_OPTIONS); + break; + case 'session': + $this->table = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_OPTIONS); + break; + } + } + + public function get_count() { + $row = Database::select('count(*) as count', $this->table, array(), 'first'); + return $row['count']; + } + + public function get_field_options_to_string($field_id) { + $options = self::get_field_options_by_field($field_id); + $new_options = array(); + if (!empty($options)) { + foreach ($options as $option) { + $new_options[] = $option['option_value'].':'.$option['option_value']; + } + $string = implode(';', $new_options); + return $string; + } + } + + public function delete_all_options_by_field_id($field_id) { + $field_id = intval($field_id); + $sql = "DELETE FROM {$this->table} WHERE field_id = $field_id"; + Database::query($sql); + } + + public function save($params, $show_query = false) { + $field_id = intval($params['field_id']); + + if (empty($field_id)) { + return false; + } + + $time = api_get_utc_datetime(); + if (!empty($params['field_options']) && + in_array($params['field_type'], array( + ExtraField::FIELD_TYPE_RADIO, + ExtraField::FIELD_TYPE_SELECT, + ExtraField::FIELD_TYPE_SELECT_MULTIPLE, + ExtraField::FIELD_TYPE_DOUBLE_SELECT)) + ) { + if ($params['field_type'] == ExtraField::FIELD_TYPE_DOUBLE_SELECT) { + //$params['field_options'] = France:Paris;Bretagne;Marseilles;Lyon|Belgique:Bruxelles;Namur;Liège;Bruges|Peru:Lima;Piura; + $options_parsed = ExtraField::extra_field_double_select_convert_string_to_array($params['field_options']); + + if (!empty($options_parsed)) { + foreach ($options_parsed as $key => $option) { + $sub_options = $option['options']; + + $new_params = array( + 'field_id' => $field_id, + 'option_value' => 0, + 'option_display_text' => $option['label'], + 'option_order' => 0, + 'tms' => $time, + ); + //Looking if option already exists: + $option_info = self::get_field_option_by_field_id_and_option_display_text($field_id, $option['label']); + + if (empty($option_info)) { + $sub_id = parent::save($new_params, $show_query); + } else { + $sub_id = $option_info['id']; + $new_params['id'] = $sub_id; + parent::update($new_params, $show_query); + } + foreach ($sub_options as $sub_option) { + if (!empty($sub_option)) { + $new_params = array( + 'field_id' => $field_id, + 'option_value' => $sub_id, + 'option_display_text' => $sub_option, + 'option_order' => 0, + 'tms' => $time, + ); + $option_info = self::get_field_option_by_field_id_and_option_display_text_and_option_value($field_id, $sub_option, $sub_id); + if (empty($option_info)) { + parent::save($new_params, $show_query); + } else { + $new_params['id'] = $option_info['id']; + parent::update($new_params, $show_query); + } + } + + } + } + } + $list = array(); + } else { + $list = explode(';', $params['field_options']); + } + + if (!empty($list)) { + foreach ($list as $option) { + $option_info = self::get_field_option_by_field_and_option($field_id, $option); + + if ($option_info == false) { + $order = self::get_max_order($field_id); + $new_params = array( + 'field_id' => $field_id, + 'option_value' => $option, + 'option_display_text' => $option, + 'option_order' => $order, + 'tms' => $time, + ); + parent::save($new_params, $show_query); + } + } + } + } + return true; + } + + public function get_field_option_by_field_and_option($field_id, $option_value) { + $field_id = intval($field_id); + $option_value = Database::escape_string($option_value); + + $sql = "SELECT * FROM {$this->table} WHERE field_id = $field_id AND option_value = '".$option_value."'"; + $result = Database::query($sql); + if (Database::num_rows($result) > 0) { + return Database::store_result($result, 'ASSOC'); + } + return false; + } + + public function get_field_option_by_field_id_and_option_display_text($field_id, $option_display_text) { + $field_id = intval($field_id); + $option_display_text = Database::escape_string($option_display_text); + + $sql = "SELECT * FROM {$this->table} WHERE field_id = $field_id AND option_display_text = '".$option_display_text."'"; + $result = Database::query($sql); + if (Database::num_rows($result) > 0) { + return Database::fetch_array($result, 'ASSOC'); + } + return false; + } + + public function get_field_option_by_field_id_and_option_display_text_and_option_value($field_id, $option_display_text, $option_value) { + $field_id = intval($field_id); + $option_display_text = Database::escape_string($option_display_text); + $option_value = Database::escape_string($option_value); + + $sql = "SELECT * FROM {$this->table} WHERE field_id = $field_id AND option_display_text = '".$option_display_text."' AND option_value = '$option_value'"; + $result = Database::query($sql); + if (Database::num_rows($result) > 0) { + return Database::fetch_array($result, 'ASSOC'); + } + return false; + } + + public function get_field_options_by_field($field_id) { + $field_id = intval($field_id); + $option_value = Database::escape_string($option_value); + + $sql = "SELECT * FROM {$this->table} WHERE field_id = $field_id "; + $result = Database::query($sql); + if (Database::num_rows($result) > 0) { + return Database::store_result($result, 'ASSOC'); + } + return false; + } + + public function get_second_select_field_options_by_field($field_id, $option_value_id, $to_json = false) { + $field_id = intval($field_id); + $option_value_id = intval($option_value_id); + $options = array(); + $sql = "SELECT * FROM {$this->table} WHERE field_id = $field_id AND option_value = $option_value_id "; + $result = Database::query($sql); + if (Database::num_rows($result) > 0) { + $options = Database::store_result($result, 'ASSOC'); + } + + if ($to_json) { + $string = null; + if (!empty($options)) { + $array = array(); + foreach ($options as $option) { + $array[$option['id']] = $option['option_display_text']; + } + $string = json_encode($array); + } + return $string; + + } + return $options; + } + + public function get_field_options_by_field_to_string($field_id) { + $field = new ExtraField($this->type); + $field_info = $field->get($field_id); + + $options = self::get_field_options_by_field($field_id); + + $elements = array(); + if (!empty($options)) { + switch ($field_info['field_type']) { + case ExtraField::FIELD_TYPE_DOUBLE_SELECT: + $html = ExtraField::extra_field_double_select_convert_array_to_string($options); + break; + default: + foreach ($options as $option) { + $elements[]= $option['option_value']; + } + $html = implode(';', $elements); + break; + } + + return $html; + } + return null; + + } + + public function get_max_order($field_id) { + $field_id = intval($field_id); + $sql = "SELECT MAX(option_order) FROM {$this->table} WHERE field_id = $field_id"; + $res = Database::query($sql); + $max = 1; + if (Database::num_rows($res) > 0) { + $row = Database::fetch_array($res); + $max = $row[0] + 1; + } + return $max; + } + + public function update($params) { + parent::update($params); + } +} \ No newline at end of file diff --git a/main/inc/lib/extra_field_value.lib.php b/main/inc/lib/extra_field_value.lib.php new file mode 100644 index 0000000000..1021e0741c --- /dev/null +++ b/main/inc/lib/extra_field_value.lib.php @@ -0,0 +1,183 @@ +type = $type; + $extra_field = ExtraField($this->type); + $this->handler_id = $extra_field->handler_id; + switch ($this->type) { + case 'user': + $this->table = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES); + $this->table_session_field = Database::get_main_table(TABLE_MAIN_USER_FIELD); + break; + case 'session': + $this->table = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_VALUES); + $this->table_session_field = Database::get_main_table(TABLE_MAIN_SESSION_FIELD); + break; + } + $this->columns[] = $this->handler_id; + } + + public function get_count() { + $row = Database::select('count(*) as count', $this->table, array(), 'first'); + return $row['count']; + } + + public function save_field_values($params) { + $session_field = new SessionField(); + if (empty($params['session_id'])) { + return false; + } + + //Parse params + foreach ($params as $key => $value) { + if (substr($key, 0, 6) == 'extra_') { //an extra field + $field_variable = substr($key, 6); + $session_field_info = $session_field->get_handler_field_info_by_field_variable($field_variable); + if ($session_field_info) { + $new_params = array( + 'session_id' => $params['session_id'], + 'field_id' => $session_field_info['id'], + 'field_value' => $value + ); + self::save($new_params); + } + } + } + } + + public function save($params, $show_query = false) { + $session_field = new SessionField(); + //$session_field_option = new SessionFieldOption(); + + //Setting value to insert + $value = $params['field_value']; + + $value_to_insert = null; + if (is_array($value)) { + $value_to_insert = implode(';', $value); + } else { + $value_to_insert = Database::escape_string($value); + } + $params['field_value'] = $value_to_insert; + + //If field id exists + $session_field_info = $session_field->get($params['field_id']); + + if ($session_field_info) { + switch ($session_field_info['field_type']) { + case ExtraField::FIELD_TYPE_TAG : + break; + case ExtraField::FIELD_TYPE_RADIO: + case ExtraField::FIELD_TYPE_SELECT: + case ExtraField::FIELD_TYPE_SELECT_MULTIPLE: + //$field_options = $session_field_option->get_field_options_by_field($params['field_id']); + //$params['field_value'] = split(';', $value_to_insert); + /* + if ($field_options) { + $check = false; + foreach ($field_options as $option) { + if (in_array($option['option_value'], $values)) { + $check = true; + break; + } + } + if (!$check) { + return false; //option value not found + } + } else { + return false; //enumerated type but no option found + }*/ + break; + case ExtraField::FIELD_TYPE_TEXT: + case ExtraField::FIELD_TYPE_TEXTAREA: + break; + case ExtraField::FIELD_TYPE_DOUBLE_SELECT: + if (is_array($value)) { + $value_to_insert = $value['extra_'.$session_field_info['field_variable']].'::'.$value['extra_'.$session_field_info['field_variable'].'_second']; + } + default: + break; + } + $session_field_values = self::get_values_by_handler_and_field_id($params['session_id'], $params['field_id']); + if ($session_field_values) { + self::delete_values_by_handler_and_field_id($params['session_id'], $params['field_id']); + } + $params['field_value'] = $value_to_insert; + $params['tms'] = api_get_utc_datetime(); + parent::save($params, $show_query); + } + } + + /** + * + * @param int $session_id + * @param int $field_id + * @param bool transform the result to a human readable strings + * @return boolean + */ + public function get_values_by_handler_and_field_id($item_id, $field_id, $transform = false) { + $field_id = intval($field_id); + $item_id = intval($item_id); + + $sql = "SELECT s.*, field_type FROM {$this->table} s INNER JOIN {$this->table_session_field} sf ON (s.field_id = sf.id) + WHERE {$this->handler_id} = '$item_id' AND field_id = '".$field_id."' ORDER BY id"; + $result = Database::query($sql); + if (Database::num_rows($result)) { + $result = Database::fetch_array($result, 'ASSOC'); + if ($transform) { + if ($result['field_type'] == ExtraField::FIELD_TYPE_DOUBLE_SELECT) { + if (!empty($result['field_value'])) { + $session_field_option = new SessionFieldOption(); + $options = explode('::', $result['field_value']); + // only available for PHP 5.4 :( $result['field_value'] = $session_field_option->get($options[0])['id'].' -> '; + $result = $session_field_option->get($options[0]); + $result_second = $session_field_option->get($options[1]); + $result['field_value'] = $result['option_display_text'].' -> '; + $result['field_value'] .= $result_second['option_display_text']; + } + } + } + return $result; + } else { + return false; + } + /*$field = Database::escape_string($field); + $sql_field = "SELECT id FROM {$this->table} WHERE field_variable = '$field'"; + $result = Database::query($sql_field); + if (Database::num_rows($result)) { + $r_field = Database::fetch_row($result); + return $r_field; + } else { + return false; + }*/ + } + + /* Get all values by field id */ + public function get_values_by_field_id($field_id) { + $sql = "SELECT s.*, field_type FROM {$this->table} s INNER JOIN {$this->table_session_field} sf ON (s.field_id = sf.id) + WHERE field_id = '".$field_id."' ORDER BY id"; + $result = Database::query($sql); + if (Database::num_rows($result)) { + return Database::store_result($result, 'ASSOC'); + } + return false; + } + + public function delete_all_values_by_field_id($field_id) { + $field_id = intval($field_id); + $sql = "DELETE FROM {$this->table} WHERE field_id = $field_id"; + Database::query($sql); + } + + public function delete_values_by_handler_and_field_id($item_id, $field_id) { + $field_id = intval($field_id); + $item_id = intval($item_id); + $sql = "DELETE FROM {$this->table} WHERE {$this->handler_id} = '$item_id' AND field_id = '".$field_id."' "; + Database::query($sql); + } +} \ No newline at end of file diff --git a/main/inc/lib/session_field.lib.php b/main/inc/lib/session_field.lib.php index e213e3bf6c..127d4a175e 100644 --- a/main/inc/lib/session_field.lib.php +++ b/main/inc/lib/session_field.lib.php @@ -2,11 +2,10 @@ /* For licensing terms, see /license.txt */ -class SessionField extends Model { - public $columns = array('id', 'field_type', 'field_variable', 'field_display_text', 'field_default_value', 'field_order', 'field_visible', 'field_changeable', 'field_filter', 'tms'); - +class SessionField extends ExtraField { + public function __construct() { - $this->table = Database::get_main_table(TABLE_MAIN_SESSION_FIELD); + parent::__construct('session'); } public function add_elements($form, $session_id = null) { @@ -21,50 +20,10 @@ class SessionField extends Model { } } $extra_fields = self::get_all(); - $extra = UserManager::set_extra_fields_in_form($form, $extra_data, 'session_field', false, false, 'session', $extra_fields); + $extra = ExtraField::set_extra_fields_in_form($form, $extra_data, 'session_field', false, false, 'session', $extra_fields); return $extra; } - public function get_count() { - $row = Database::select('count(*) as count', $this->table, array(), 'first'); - return $row['count']; - } - - public function get_max_field_order() { - $sql = "SELECT MAX(field_order) FROM {$this->table}"; - $res = Database::query($sql); - - $order = 0; - if (Database::num_rows($res)>0) { - $row = Database::fetch_row($res); - $order = $row[0]+1; - } - return $order; - } - - public function get_session_field_info_by_field_variable($field_variable) { - $field_variable = Database::escape_string($field_variable); - $sql_field = "SELECT * FROM {$this->table} WHERE field_variable = '$field_variable'"; - $result = Database::query($sql_field); - if (Database::num_rows($result)) { - $r_field = Database::fetch_array($result, 'ASSOC'); - return $r_field; - } else { - return false; - } - } - - public function get_all($where_conditions = array()) { - $options = Database::select('*', $this->table, array('where'=>$where_conditions,'order' =>'field_display_text ASC')); - $sesion_field_option = new SessionFieldOption(); - if (!empty($options)) { - foreach ($options as &$option) { - $option['options'] = $sesion_field_option->get_field_options_by_field($option['id']); - } - } - return $options; - } - public function get_session_extra_data($session_id) { if (empty($session_id)) { return array(); @@ -80,14 +39,14 @@ class SessionField extends Model { $field_value = $field_value['field_value']; switch ($session_field['field_type']) { - case UserManager::USER_FIELD_TYPE_DOUBLE_SELECT: + case ExtraField::FIELD_TYPE_DOUBLE_SELECT: $selected_options = explode('::', $field_value); $extra_data['extra_'.$session_field['field_variable']]['extra_'.$session_field['field_variable']] = $selected_options[0]; $extra_data['extra_'.$session_field['field_variable']]['extra_'.$session_field['field_variable'].'_second'] = $selected_options[1]; break; - case UserManager::USER_FIELD_TYPE_SELECT_MULTIPLE: + case ExtraField::FIELD_TYPE_SELECT_MULTIPLE: $field_value = explode(';', $field_value); - case UserManager::USER_FIELD_TYPE_RADIO: + case ExtraField::FIELD_TYPE_RADIO: $extra_data['extra_'.$session_field['field_variable']]['extra_'.$session_field['field_variable']] = $field_value; break; default: @@ -99,21 +58,7 @@ class SessionField extends Model { } return $extra_data; } - - public function get_field_types() { - $types = UserManager::get_user_field_types(); - unset($types[UserManager::USER_FIELD_TYPE_TAG]); - unset($types[UserManager::USER_FIELD_TYPE_SOCIAL_PROFILE]); - return $types; - } - - public function get_field_type_by_id($id) { - $types = self::get_field_types(); - if (isset($types[$id])) { - return $types[$id]; - } - return null; - } + function display() { // action links @@ -124,38 +69,7 @@ class SessionField extends Model { echo ''; echo Display::grid_html('session_fields'); } - - function clean_parameters($params) { - if (!isset($params['field_variable']) || empty($params['field_variable'])) { - $params['field_variable'] = trim(strtolower(str_replace(" ","_", $params['field_display_text']))); - } - - if (!isset($params['field_order'])) { - $max_order = self::get_max_field_order(); - $params['field_order'] = $max_order; - } - return $params; - } - - public function save($params, $show_query = false) { - $session_field_info = self::get_session_field_info_by_field_variable($params['field_variable']); - $params = self::clean_parameters($params); - if ($session_field_info) { - return $session_field_info['id']; - } else { - if (!isset($params['tms'])) { - $params['tms'] = api_get_utc_datetime(); - } - $id = parent::save($params, $show_query); - if ($id) { - $session_field_option = new SessionFieldOption(); - $params['field_id'] = $id; - $session_field_option->save($params); - } - return $id; - } - } - + public function return_form($url, $action) { $form = new FormValidator('session_field', 'post', $url); @@ -172,7 +86,7 @@ class SessionField extends Model { $form->addElement('text', 'field_display_text', get_lang('Name'), array('class' => 'span5')); // Field type - $types = self::get_field_types(); + $types = self::get_field_types(); $form->addElement('select', 'field_type', get_lang('FieldType'), $types, array('id' => 'field_type', 'class' => 'chzn-select', 'data-placeholder' => get_lang('Select'))); $form->addElement('label', get_lang('Example'), '
-
'); @@ -181,7 +95,7 @@ class SessionField extends Model { //$form->addElement('html',''); - /*$status_list = $this->get_status_list(); $form->addElement('select', 'status', get_lang('Status'), $status_list);*/ - if ($action == 'edit') { - //$form->freeze('created_at'); - } - $defaults = array(); if ($action == 'edit') { // Setting the defaults $defaults = $this->get($id); - $option = new SessionFieldOption(); + $option = new SessionFieldOption('session'); + if ($defaults['field_type'] == ExtraField::FIELD_TYPE_DOUBLE_SELECT) { + $form->freeze('field_options'); + } $defaults['field_options'] = $option->get_field_options_by_field_to_string($id); + $form->addElement('button', 'submit', get_lang('Modify'), 'class="save"'); } else { $defaults['field_visible'] = 0; @@ -238,24 +150,5 @@ class SessionField extends Model { $form->addRule('field_type', get_lang('ThisFieldIsRequired'), 'required'); return $form; - } - - public function update($params) { - $params = self::clean_parameters($params); - if (isset($params['id'])) { - $session_field_option = new SessionFieldOption(); - $params['field_id'] = $params['id']; - $session_field_option->save($params); - } - parent::update($params); - } - - public function delete($id) { - parent::delete($id); - $session_field_option = new SessionFieldOption(); - $session_field_option->delete_all_options_by_field_id($id); - - $session_field_values = new SessionFieldValue(); - $session_field_values->delete_all_values_by_field_id($id); - } + } } \ No newline at end of file diff --git a/main/inc/lib/session_field_option.lib.php b/main/inc/lib/session_field_option.lib.php index c80cf1dab0..899d129d9e 100644 --- a/main/inc/lib/session_field_option.lib.php +++ b/main/inc/lib/session_field_option.lib.php @@ -1,237 +1,8 @@ table = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_OPTIONS); - } - - public function get_count() { - $row = Database::select('count(*) as count', $this->table, array(), 'first'); - return $row['count']; - } - - public function get_field_options_to_string($field_id) { - $options = self::get_field_options_by_field($field_id); - $new_options = array(); - if (!empty($options)) { - foreach ($options as $option) { - $new_options[] = $option['option_value'].':'.$option['option_value']; - } - $string = implode(';', $new_options); - return $string; - } - } - - public function delete_all_options_by_field_id($field_id) { - $field_id = intval($field_id); - $sql = "DELETE FROM {$this->table} WHERE field_id = $field_id"; - Database::query($sql); - } - - public function save($params, $show_query = false) { - $field_id = intval($params['field_id']); - - if (empty($field_id)) { - return false; - } - - $time = api_get_utc_datetime(); - if (!empty($params['field_options']) && - in_array($params['field_type'], array( - UserManager::USER_FIELD_TYPE_RADIO, - UserManager::USER_FIELD_TYPE_SELECT, - UserManager::USER_FIELD_TYPE_SELECT_MULTIPLE, - UserManager::USER_FIELD_TYPE_DOUBLE_SELECT)) - ) { - if ($params['field_type'] == UserManager::USER_FIELD_TYPE_DOUBLE_SELECT) { - //$params['field_options'] = France:Paris;Bretagne;Marseilles;Lyon|Belgique:Bruxelles;Namur;Liège;Bruges|Peru:Lima;Piura; - $options_parsed = UserManager::extra_field_double_select_convert_string_to_array($params['field_options']); - - if (!empty($options_parsed)) { - foreach ($options_parsed as $key => $option) { - $sub_options = $option['options']; - - $new_params = array( - 'field_id' => $field_id, - 'option_value' => 0, - 'option_display_text' => $option['label'], - 'option_order' => 0, - 'tms' => $time, - ); - //Looking if option already exists: - $option_info = self::get_field_option_by_field_id_and_option_display_text($field_id, $option['label']); - - if (empty($option_info)) { - $sub_id = parent::save($new_params, $show_query); - } else { - $sub_id = $option_info['id']; - $new_params['id'] = $sub_id; - parent::update($new_params, $show_query); - } - foreach ($sub_options as $sub_option) { - if (!empty($sub_option)) { - $new_params = array( - 'field_id' => $field_id, - 'option_value' => $sub_id, - 'option_display_text' => $sub_option, - 'option_order' => 0, - 'tms' => $time, - ); - $option_info = self::get_field_option_by_field_id_and_option_display_text_and_option_value($field_id, $sub_option, $sub_id); - if (empty($option_info)) { - parent::save($new_params, $show_query); - } else { - $new_params['id'] = $option_info['id']; - parent::update($new_params, $show_query); - } - } - - } - } - } - $list = array(); - } else { - $list = explode(';', $params['field_options']); - } - - if (!empty($list)) { - foreach ($list as $option) { - $option_info = self::get_field_option_by_field_and_option($field_id, $option); - - if ($option_info == false) { - $order = self::get_max_order($field_id); - $new_params = array( - 'field_id' => $field_id, - 'option_value' => $option, - 'option_display_text' => $option, - 'option_order' => $order, - 'tms' => $time, - ); - parent::save($new_params, $show_query); - } - } - } - } - return true; - } - - public function get_field_option_by_field_and_option($field_id, $option_value) { - $field_id = intval($field_id); - $option_value = Database::escape_string($option_value); - - $sql = "SELECT * FROM {$this->table} WHERE field_id = $field_id AND option_value = '".$option_value."'"; - $result = Database::query($sql); - if (Database::num_rows($result) > 0) { - return Database::store_result($result, 'ASSOC'); - } - return false; - } - - public function get_field_option_by_field_id_and_option_display_text($field_id, $option_display_text) { - $field_id = intval($field_id); - $option_display_text = Database::escape_string($option_display_text); - - $sql = "SELECT * FROM {$this->table} WHERE field_id = $field_id AND option_display_text = '".$option_display_text."'"; - $result = Database::query($sql); - if (Database::num_rows($result) > 0) { - return Database::fetch_array($result, 'ASSOC'); - } - return false; - } - - public function get_field_option_by_field_id_and_option_display_text_and_option_value($field_id, $option_display_text, $option_value) { - $field_id = intval($field_id); - $option_display_text = Database::escape_string($option_display_text); - $option_value = Database::escape_string($option_value); - - $sql = "SELECT * FROM {$this->table} WHERE field_id = $field_id AND option_display_text = '".$option_display_text."' AND option_value = '$option_value'"; - $result = Database::query($sql); - if (Database::num_rows($result) > 0) { - return Database::fetch_array($result, 'ASSOC'); - } - return false; - } - - - - public function get_field_options_by_field($field_id) { - $field_id = intval($field_id); - $option_value = Database::escape_string($option_value); - - $sql = "SELECT * FROM {$this->table} WHERE field_id = $field_id "; - $result = Database::query($sql); - if (Database::num_rows($result) > 0) { - return Database::store_result($result, 'ASSOC'); - } - return false; - } - - public function get_second_select_field_options_by_field($field_id, $option_value_id, $to_json = false) { - $field_id = intval($field_id); - $option_value_id = intval($option_value_id); - $options = array(); - $sql = "SELECT * FROM {$this->table} WHERE field_id = $field_id AND option_value = $option_value_id "; - $result = Database::query($sql); - if (Database::num_rows($result) > 0) { - $options = Database::store_result($result, 'ASSOC'); - } - - if ($to_json) { - $string = null; - if (!empty($options)) { - $array = array(); - foreach ($options as $option) { - $array[$option['id']] = $option['option_display_text']; - } - $string = json_encode($array); - } - return $string; - - } - return $options; - } - - public function get_field_options_by_field_to_string($field_id) { - $session_field = new SessionField(); - $field_info = $session_field->get($field_id); - - $options = self::get_field_options_by_field($field_id); - $elements = array(); - if (!empty($options)) { - switch ($field_info['field_type']) { - case UserManager::USER_FIELD_TYPE_DOUBLE_SELECT: - $html = UserManager::extra_field_double_select_convert_array_to_string($options); - break; - default: - foreach ($options as $option) { - $elements[]= $option['option_value']; - } - $html = implode(';', $elements); - break; - } - - return $html; - } - return null; - - } - - public function get_max_order($field_id) { - $field_id = intval($field_id); - $sql = "SELECT MAX(option_order) FROM {$this->table} WHERE field_id = $field_id"; - $res = Database::query($sql); - $max = 1; - if (Database::num_rows($res) > 0) { - $row = Database::fetch_array($res); - $max = $row[0] + 1; - } - return $max; - } - - public function update($params) { - parent::update($params); +class SessionFieldOption extends ExtraFieldOption { + public function __construct() { + parent::__construct('session'); } } \ No newline at end of file diff --git a/main/inc/lib/session_field_value.lib.php b/main/inc/lib/session_field_value.lib.php index 7a73787ac1..c4b34b486d 100644 --- a/main/inc/lib/session_field_value.lib.php +++ b/main/inc/lib/session_field_value.lib.php @@ -1,175 +1,6 @@ table = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_VALUES); - $this->table_session_field = Database::get_main_table(TABLE_MAIN_SESSION_FIELD); - } - - public function get_count() { - $row = Database::select('count(*) as count', $this->table, array(), 'first'); - return $row['count']; - } - - public function save_session_field_values($params) { - $session_field = new SessionField(); - if (empty($params['session_id'])) { - return false; - } - - //Parse params - foreach ($params as $key => $value) { - if (substr($key, 0, 6) == 'extra_') { //an extra field - $field_variable = substr($key, 6); - $session_field_info = $session_field->get_session_field_info_by_field_variable($field_variable); - if ($session_field_info) { - $new_params = array( - 'session_id' => $params['session_id'], - 'field_id' => $session_field_info['id'], - 'field_value' => $value - ); - self::save($new_params); - } - } - } - } - - public function save($params, $show_query = false) { - $session_field = new SessionField(); - //$session_field_option = new SessionFieldOption(); - - //Setting value to insert - $value = $params['field_value']; - - $value_to_insert = null; - if (is_array($value)) { - $value_to_insert = implode(';', $value); - } else { - $value_to_insert = Database::escape_string($value); - } - $params['field_value'] = $value_to_insert; - - //If field id exists - $session_field_info = $session_field->get($params['field_id']); - - if ($session_field_info) { - switch ($session_field_info['field_type']) { - case UserManager::USER_FIELD_TYPE_TAG : - break; - case UserManager::USER_FIELD_TYPE_RADIO: - case UserManager::USER_FIELD_TYPE_SELECT: - case UserManager::USER_FIELD_TYPE_SELECT_MULTIPLE: - //$field_options = $session_field_option->get_field_options_by_field($params['field_id']); - //$params['field_value'] = split(';', $value_to_insert); - - /* - if ($field_options) { - $check = false; - foreach ($field_options as $option) { - if (in_array($option['option_value'], $values)) { - $check = true; - break; - } - } - if (!$check) { - return false; //option value not found - } - } else { - return false; //enumerated type but no option found - }*/ - break; - case UserManager::USER_FIELD_TYPE_TEXT: - case UserManager::USER_FIELD_TYPE_TEXTAREA: - break; - case UserManager::USER_FIELD_TYPE_DOUBLE_SELECT: - if (is_array($value)) { - $value_to_insert = $value['extra_'.$session_field_info['field_variable']].'::'.$value['extra_'.$session_field_info['field_variable'].'_second']; - } - default: - break; - } - $session_field_values = self::get_values_by_session_and_field_id($params['session_id'], $params['field_id']); - if ($session_field_values) { - self::delete_values_by_session_and_field_id($params['session_id'], $params['field_id']); - } - $params['field_value'] = $value_to_insert; - $params['tms'] = api_get_utc_datetime(); - parent::save($params, $show_query); - } - } - - /** - * - * @param int $session_id - * @param int $field_id - * @param bool transform the result to a human readable strings - * @return boolean - */ - public function get_values_by_session_and_field_id($session_id, $field_id, $transform = false) { - $field_id = intval($field_id); - $session_id = intval($session_id); - - $sql = "SELECT s.*, field_type FROM {$this->table} s INNER JOIN {$this->table_session_field} sf ON (s.field_id = sf.id) - WHERE session_id = '$session_id' AND field_id = '".$field_id."' ORDER BY id"; - $result = Database::query($sql); - if (Database::num_rows($result)) { - $result = Database::fetch_array($result, 'ASSOC'); - if ($transform) { - if ($result['field_type'] == UserManager::USER_FIELD_TYPE_DOUBLE_SELECT) { - if (!empty($result['field_value'])) { - $session_field_option = new SessionFieldOption(); - $options = explode('::', $result['field_value']); - // only available for PHP 5.4 :( $result['field_value'] = $session_field_option->get($options[0])['id'].' -> '; - $result = $session_field_option->get($options[0]); - $result_second = $session_field_option->get($options[1]); - $result['field_value'] = $result['option_display_text'].' -> '; - $result['field_value'] .= $result_second['option_display_text']; - } - } - } - return $result; - } else { - return false; - } - /*$field = Database::escape_string($field); - $sql_field = "SELECT id FROM {$this->table} WHERE field_variable = '$field'"; - $result = Database::query($sql_field); - if (Database::num_rows($result)) { - $r_field = Database::fetch_row($result); - return $r_field; - } else { - return false; - }*/ - } - - /* Get all values by field id */ - public function get_values_by_field_id($field_id) { - $sql = "SELECT s.*, field_type FROM {$this->table} s INNER JOIN {$this->table_session_field} sf ON (s.field_id = sf.id) - WHERE field_id = '".$field_id."' ORDER BY id"; - $result = Database::query($sql); - if (Database::num_rows($result)) { - return Database::store_result($result, 'ASSOC'); - } - return false; - } - - public function delete_all_values_by_field_id($field_id) { - $field_id = intval($field_id); - $sql = "DELETE FROM {$this->table} WHERE field_id = $field_id"; - Database::query($sql); - } - - public function delete_values_by_session_and_field_id($session_id, $field_id) { - $field_id = intval($field_id); - $session_id = intval($session_id); - $sql = "DELETE FROM {$this->table} WHERE session_id = '$session_id' AND field_id = '".$field_id."' "; - Database::query($sql); - } - - public function update($params) { - - } - +class SessionFieldValue extends ExtraFieldValue { + public function __construct() { + parent::__construct('session'); + } } \ No newline at end of file diff --git a/main/inc/lib/sessionmanager.lib.php b/main/inc/lib/sessionmanager.lib.php index ee32174077..4b4b54acd6 100644 --- a/main/inc/lib/sessionmanager.lib.php +++ b/main/inc/lib/sessionmanager.lib.php @@ -78,7 +78,7 @@ class SessionManager { //Saving extra fields $session_field_value = new SessionFieldValue(); $params['session_id'] = $session_id; - $session_field_value->save_session_field_values($params); + $session_field_value->save_field_values($params); //Adding to the correct URL $access_url_id = api_get_current_access_url_id(); @@ -99,7 +99,7 @@ class SessionManager { $session_field_value = new SessionFieldValue(); $params['session_id'] = $params['id']; unset($params['id']); - $session_field_value->save_session_field_values($params); + $session_field_value->save_field_values($params); } } diff --git a/main/inc/lib/usermanager.lib.php b/main/inc/lib/usermanager.lib.php index d82374fe39..026cd3afc2 100644 --- a/main/inc/lib/usermanager.lib.php +++ b/main/inc/lib/usermanager.lib.php @@ -15,21 +15,6 @@ * @package chamilo.include.user */ class UserManager { - - // Constants for user extra field types. - CONST USER_FIELD_TYPE_TEXT = 1; - CONST USER_FIELD_TYPE_TEXTAREA = 2; - CONST USER_FIELD_TYPE_RADIO = 3; - CONST USER_FIELD_TYPE_SELECT = 4; - CONST USER_FIELD_TYPE_SELECT_MULTIPLE = 5; - CONST USER_FIELD_TYPE_DATE = 6; - CONST USER_FIELD_TYPE_DATETIME = 7; - CONST USER_FIELD_TYPE_DOUBLE_SELECT = 8; - CONST USER_FIELD_TYPE_DIVIDER = 9; - CONST USER_FIELD_TYPE_TAG = 10; - CONST USER_FIELD_TYPE_TIMEZONE = 11; - CONST USER_FIELD_TYPE_SOCIAL_PROFILE = 12; - private function __construct () { } @@ -1281,14 +1266,14 @@ class UserManager { // Check if enumerated field, if the option is available $rowuf = Database::fetch_array($resuf); switch ($rowuf['field_type']) { - case self::USER_FIELD_TYPE_TAG : + case ExtraField::FIELD_TYPE_TAG : //4. Tags are process here comes from main/auth/profile.php UserManager::process_tags(explode(';', $fvalues), $user_id, $rowuf['id']); return true; break; - case self::USER_FIELD_TYPE_RADIO: - case self::USER_FIELD_TYPE_SELECT: - case self::USER_FIELD_TYPE_SELECT_MULTIPLE: + case ExtraField::FIELD_TYPE_RADIO: + case ExtraField::FIELD_TYPE_SELECT: + case ExtraField::FIELD_TYPE_SELECT_MULTIPLE: $sqluo = "SELECT * FROM $t_ufo WHERE field_id = ".$rowuf['id']; $resuo = Database::query($sqluo); $values = split(';',$fvalues); @@ -1538,8 +1523,8 @@ class UserManager { return false; } - if (!empty($fieldoptions) && in_array($fieldtype, array(self::USER_FIELD_TYPE_RADIO, self::USER_FIELD_TYPE_SELECT, self::USER_FIELD_TYPE_SELECT_MULTIPLE, self::USER_FIELD_TYPE_DOUBLE_SELECT))) { - if ($fieldtype == self::USER_FIELD_TYPE_DOUBLE_SELECT) { + if (!empty($fieldoptions) && in_array($fieldtype, array(ExtraField::FIELD_TYPE_RADIO, ExtraField::FIELD_TYPE_SELECT, ExtraField::FIELD_TYPE_SELECT_MULTIPLE, ExtraField::FIELD_TYPE_DOUBLE_SELECT))) { + if ($fieldtype == ExtraField::FIELD_TYPE_DOUBLE_SELECT) { $twolist = explode('|', $fieldoptions); $counter = 0; foreach ($twolist as $individual_list) { @@ -1631,7 +1616,7 @@ class UserManager { $result = Database::query($sql); // we create an array with all the options (will be used later in the script) - if ($fieldtype == self::USER_FIELD_TYPE_DOUBLE_SELECT) { + if ($fieldtype == ExtraField::FIELD_TYPE_DOUBLE_SELECT) { $twolist = explode('|', $fieldoptions); $counter = 0; foreach ($twolist as $individual_list) { @@ -1747,7 +1732,7 @@ class UserManager { $res = Database::query($sql); if (Database::num_rows($res) > 0) { while ($row = Database::fetch_array($res)) { - if ($row['type'] == self::USER_FIELD_TYPE_DOUBLE_SELECT) { + if ($row['type'] == ExtraField::FIELD_TYPE_DOUBLE_SELECT) { $field_options = self::get_extra_field_options($row['fvar']); $field_details['options'] = $field_options; @@ -1780,7 +1765,7 @@ class UserManager { } } - } elseif ($row['type'] == self::USER_FIELD_TYPE_TAG) { + } elseif ($row['type'] == ExtraField::FIELD_TYPE_TAG) { $tags = self::get_user_tags_to_string($user_id, $row['id'], false); $extra_data['extra_'.$row['fvar']] = $tags; } else { @@ -1794,7 +1779,7 @@ class UserManager { if (Database::num_rows($resu) > 0) { $rowu = Database::fetch_array($resu); $fval = $rowu['fval']; - if ($row['type'] == self::USER_FIELD_TYPE_SELECT_MULTIPLE) { + if ($row['type'] == ExtraField::FIELD_TYPE_SELECT_MULTIPLE) { $fval = split(';', $rowu['fval']); } } else { @@ -1803,13 +1788,13 @@ class UserManager { } // We get here (and fill the $extra_data array) even if there is no user with data (we fill it with default values) if ($prefix) { - if ($row['type'] == self::USER_FIELD_TYPE_RADIO) { + if ($row['type'] == ExtraField::FIELD_TYPE_RADIO) { $extra_data['extra_'.$row['fvar']]['extra_'.$row['fvar']] = $fval; } else { $extra_data['extra_'.$row['fvar']] = $fval; } } else { - if ($row['type'] == self::USER_FIELD_TYPE_RADIO) { + if ($row['type'] == ExtraField::FIELD_TYPE_RADIO) { $extra_data['extra_'.$row['fvar']]['extra_'.$row['fvar']] = $fval; } else { $extra_data[$row['fvar']] = $fval; @@ -1859,7 +1844,7 @@ class UserManager { if (Database::num_rows($resu) > 0) { $rowu = Database::fetch_array($resu); $fval = $rowu['fval']; - if ($row['type'] == self::USER_FIELD_TYPE_SELECT_MULTIPLE) { + if ($row['type'] == ExtraField::FIELD_TYPE_SELECT_MULTIPLE) { $fval = split(';',$rowu['fval']); } } @@ -3609,257 +3594,9 @@ class UserManager { } return false; } - - static function set_extra_fields_in_form($form, $extra_data, $form_name, $admin_permissions = false, $user_id = null, $type = 'user', $extra = null) { - $user_id = intval($user_id); - - // User extra fields - if ($type == 'user') { - $extra = self::get_extra_fields(0, 50, 5, 'ASC', true, null, true); - } - - $jquery_ready_content = null; - - if (!empty($extra)) - foreach ($extra as $field_details) { - if (!$admin_permissions) { - if ($field_details['field_visible'] == 0) { - continue; - } - } - switch ($field_details['field_type']) { - case self::USER_FIELD_TYPE_TEXT: - $form->addElement('text', 'extra_'.$field_details['field_variable'], $field_details['field_display_text'], array('class' => 'span4')); - $form->applyFilter('extra_'.$field_details['field_variable'], 'stripslashes'); - $form->applyFilter('extra_'.$field_details['field_variable'], 'trim'); - if (!$admin_permissions) { - if ($field_details['field_visible'] == 0) $form->freeze('extra_'.$field_details['field_variable']); - } - break; - case self::USER_FIELD_TYPE_TEXTAREA: - $form->add_html_editor('extra_'.$field_details['field_variable'], $field_details['field_display_text'], false, false, array('ToolbarSet' => 'Profile', 'Width' => '100%', 'Height' => '130')); - //$form->addElement('textarea', 'extra_'.$field_details['field_variable'], $field_details['field_display_text'], array('size' => 80)); - $form->applyFilter('extra_'.$field_details['field_variable'], 'stripslashes'); - $form->applyFilter('extra_'.$field_details['field_variable'], 'trim'); - if (!$admin_permissions) { - if ($field_details['field_visible'] == 0) $form->freeze('extra_'.$field_details['field_variable']); - } - break; - case self::USER_FIELD_TYPE_RADIO: - $group = array(); - foreach ($field_details['options'] as $option_id => $option_details) { - $options[$option_details['option_value']] = $option_details['option_display_text']; - $group[] = $form->createElement('radio', 'extra_'.$field_details['field_variable'], $option_details['option_value'],$option_details['option_display_text'].'
',$option_details['option_value']); - } - $form->addGroup($group, 'extra_'.$field_details['field_variable'], $field_details['field_display_text'], ''); - if (!$admin_permissions) { - if ($field_details['field_visible'] == 0) $form->freeze('extra_'.$field_details['field_variable']); - } - break; - case self::USER_FIELD_TYPE_SELECT: - $get_lang_variables = false; - if (in_array($field_details['field_variable'], array('mail_notify_message','mail_notify_invitation', 'mail_notify_group_message'))) { - $get_lang_variables = true; - } - $options = array(); - - foreach ($field_details['options'] as $option_id => $option_details) { - //$options[$option_details['option_value']] = $option_details['option_display_text']; - if ($get_lang_variables) { - $options[$option_details['option_value']] = get_lang($option_details['option_display_text']); - } else { - $options[$option_details['option_value']] = $option_details['option_display_text']; - } - } - if ($get_lang_variables) { - $field_details['field_display_text'] = get_lang($field_details['field_display_text']); - } - //chzn-select doesn't work for sessions?? - $form->addElement('select','extra_'.$field_details['field_variable'], $field_details['field_display_text'], $options, array('class'=>'', 'id'=>'extra_'.$field_details['field_variable'])); - if (!$admin_permissions) { - if ($field_details['field_visible'] == 0) { - $form->freeze('extra_'.$field_details['field_variable']); - } - } - break; - case self::USER_FIELD_TYPE_SELECT_MULTIPLE: - $options = array(); - foreach ($field_details['options'] as $option_id => $option_details) { - $options[$option_details['option_value']] = $option_details['option_display_text']; - } - $form->addElement('select','extra_'.$field_details['field_variable'], $field_details['field_display_text'], $options, array('multiple' => 'multiple')); - if (!$admin_permissions) { - if ($field_details['field_visible'] == 0) { - $form->freeze('extra_'.$field_details['field_variable']); - } - } - break; - case self::USER_FIELD_TYPE_DATE: - $form->addElement('datepickerdate', 'extra_'.$field_details['field_variable'], $field_details['field_display_text'], array('form_name' => $form_name)); - $form->_elements[$form->_elementIndex['extra_'.$field_details['field_variable']]]->setLocalOption('minYear', 1900); - $defaults['extra_'.$field_details['field_variable']] = date('Y-m-d 12:00:00'); - $form -> setDefaults($defaults); - if (!$admin_permissions) { - if ($field_details['field_visible'] == 0) { - $form->freeze('extra_'.$field_details['field_variable']); - } - } - $form->applyFilter('theme', 'trim'); - break; - case self::USER_FIELD_TYPE_DATETIME: - $form->addElement('datepicker', 'extra_'.$field_details['field_variable'], $field_details['field_display_text'], array('form_name' => $form_name)); - $form->_elements[$form->_elementIndex['extra_'.$field_details['field_variable']]]->setLocalOption('minYear', 1900); - $defaults['extra_'.$field_details['field_variable']] = date('Y-m-d 12:00:00'); - $form -> setDefaults($defaults); - if (!$admin_permissions) { - if ($field_details['field_visible'] == 0) { - $form->freeze('extra_'.$field_details['field_variable']); - } - } - $form->applyFilter('theme', 'trim'); - break; - case self::USER_FIELD_TYPE_DOUBLE_SELECT: - $first_select_id = 'first_extra_'.$field_details['field_variable']; - - $url = api_get_path(WEB_AJAX_PATH).'extra_field.ajax.php?1=1'; - - $jquery_ready_content .= ' - $("#'.$first_select_id.'").on("change", function() { - var id = $(this).val(); - $.ajax({ - url: "'.$url.'&a=get_second_select_options", - dataType: "json", - data: "type='.$type.'&field_id='.$field_details['id'].'&option_value_id="+id, - success: function(data) { - $("#second_extra_'.$field_details['field_variable'].'").empty(); - $.each(data, function(index, value) { - $("#second_extra_'.$field_details['field_variable'].'").append($("'; - } - } - - $multi_select = ''; - - $form->addElement('label',$field_details['field_display_text'], $multi_select); - $url = api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php'; - $complete_text = get_lang('StartToType'); - //if cache is set to true the jquery will be called 1 time - $field_variable = $field_details['field_variable']; - $field_id = $field_details['id']; - $jquery_ready_content .= <<addElement('select', 'extra_'.$field_details['field_variable'], $field_details['field_display_text'], api_get_timezones(), ''); - if ($field_details['field_visible'] == 0) $form->freeze('extra_'.$field_details['field_variable']); - break; - case self::USER_FIELD_TYPE_SOCIAL_PROFILE: - // get the social network's favicon - $icon_path = UserManager::get_favicon_from_url($extra_data['extra_'.$field_details['field_variable']], $field_details['field_default_value']); - // special hack for hi5 - $leftpad = '1.7'; - $top = '0.4'; - $domain = parse_url($icon_path, PHP_URL_HOST); - if ($domain == 'www.hi5.com' or $domain == 'hi5.com') { - $leftpad = '3'; $top = '0'; - } - // print the input field - $form->addElement('text', 'extra_'.$field_details['field_variable'], $field_details['field_display_text'], array('size' => 60, 'style' => 'background-image: url(\''.$icon_path.'\'); background-repeat: no-repeat; background-position: 0.4em '.$top.'em; padding-left: '.$leftpad.'em; ')); - $form->applyFilter('extra_'.$field_details['field_variable'], 'stripslashes'); - $form->applyFilter('extra_'.$field_details['field_variable'], 'trim'); - if ($field_details['field_visible'] == 0) { - $form->freeze('extra_'.$field_details['field_variable']); - } - break; - } - } - $return = array(); - $return['jquery_ready_content'] = $jquery_ready_content; - return $return; - } - + static function get_user_field_types() { - $types = array(); - $types[self::USER_FIELD_TYPE_TEXT] = get_lang('FieldTypeText'); - $types[self::USER_FIELD_TYPE_TEXTAREA] = get_lang('FieldTypeTextarea'); - $types[self::USER_FIELD_TYPE_RADIO] = get_lang('FieldTypeRadio'); - $types[self::USER_FIELD_TYPE_SELECT] = get_lang('FieldTypeSelect'); - $types[self::USER_FIELD_TYPE_SELECT_MULTIPLE] = get_lang('FieldTypeSelectMultiple'); - $types[self::USER_FIELD_TYPE_DATE] = get_lang('FieldTypeDate'); - $types[self::USER_FIELD_TYPE_DATETIME] = get_lang('FieldTypeDatetime'); - $types[self::USER_FIELD_TYPE_DOUBLE_SELECT] = get_lang('FieldTypeDoubleSelect'); - $types[self::USER_FIELD_TYPE_DIVIDER] = get_lang('FieldTypeDivider'); - $types[self::USER_FIELD_TYPE_TAG] = get_lang('FieldTypeTag'); - $types[self::USER_FIELD_TYPE_TIMEZONE] = get_lang('FieldTypeTimezone'); - $types[self::USER_FIELD_TYPE_SOCIAL_PROFILE] = get_lang('FieldTypeSocialProfile'); - return $types; + return ExtraField::get_extra_fields_by_handler('user'); } static function add_user_as_admin($user_id) { @@ -3880,72 +3617,4 @@ EOF; Database::query($sql); } } - - /** - * Converts a string like this: - * France:Paris;Bretagne;Marseilles;Lyon|Belgique:Bruxelles;Namur;Liège;Bruges|Peru:Lima;Piura; - * into - * array('France' => array('Paris', 'Bregtane', 'Marseilles'), 'Belgique' => array('Namur', 'Liège', etc - * @param string $string - * @return array - */ - static function extra_field_double_select_convert_string_to_array($string) { - $options = explode('|', $string); - $options_parsed = array(); - $id = 0; - if (!empty($options)) { - foreach ($options as $sub_options) { - $options = explode(':', $sub_options); - $sub_sub_options = explode(';', $options[1]); - $options_parsed[$id] = array('label' => $options[0], 'options' => $sub_sub_options); - $id++; - } - } - return $options_parsed; - } - - static function extra_field_double_select_convert_array_to_ordered_array($options) { - $options_parsed = array(); - if (!empty($options)) { - foreach ($options as $option) { - if ($option['option_value'] == 0 ) { - $options_parsed[$option['id']][] = $option; - } else { - $options_parsed[$option['option_value']][] = $option; - } - } - } - return $options_parsed; - } - - /** - - * @param array options the result of the get_field_options_by_field() array - */ - static function extra_field_double_select_convert_array_to_string($options) { - $string = null; - //var_dump($options); - $options_parsed = self::extra_field_double_select_convert_array_to_ordered_array($options); - - if (!empty($options_parsed)) { - foreach ($options_parsed as $option) { - foreach ($option as $key => $item) { - $string .= $item['option_display_text']; - if ($key == 0) { - $string .= ':'; - } else { - if (isset($option[$key+1])) { - $string .= ';'; - } - } - } - $string .= '|'; - } - } - - if (!empty($string)) { - $string = substr($string, 0, strlen($string)-1); - } - return $string; - } } \ No newline at end of file diff --git a/main/survey/fillsurvey.php b/main/survey/fillsurvey.php index 3c0b5e782f..db92aee63b 100644 --- a/main/survey/fillsurvey.php +++ b/main/survey/fillsurvey.php @@ -389,7 +389,7 @@ if ($survey_data['form_fields']!='' && $survey_data['anonymous'] == 0 && is_arra // EXTRA FIELDS $extra_data = UserManager :: get_extra_user_data($user_id, true); - UserManager::set_extra_fields_in_form($form, $extra_data, 'profile', false, null, 'user'); + ExtraField::set_extra_fields_in_form($form, $extra_data, 'profile', false, null, 'user'); $form->addElement('style_submit_button', '', get_lang('Next'), array('class' => 'next')); $user_data = array_merge($user_data, $extra_data); diff --git a/main/survey/survey.lib.php b/main/survey/survey.lib.php index aa608db448..e324701946 100644 --- a/main/survey/survey.lib.php +++ b/main/survey/survey.lib.php @@ -4552,7 +4552,7 @@ class SurveyUtil { continue; } switch ($field_details[2]) { - case UserManager::USER_FIELD_TYPE_TEXT: + case ExtraField::FIELD_TYPE_TEXT: $field_list_array['extra_'.$field_details[1]]['name'] = $field_details[3]; if ($field_details[7] == 0) { $field_list_array['extra_'.$field_details[1]]['visibility'] = 0; @@ -4560,7 +4560,7 @@ class SurveyUtil { $field_list_array['extra_'.$field_details[1]]['visibility'] = 1; } break; - case UserManager::USER_FIELD_TYPE_TEXTAREA: + case ExtraField::FIELD_TYPE_TEXTAREA: $field_list_array['extra_'.$field_details[1]]['name'] = $field_details[3]; if ($field_details[7] == 0) { $field_list_array['extra_'.$field_details[1]]['visibility'] = 0; @@ -4568,7 +4568,7 @@ class SurveyUtil { $field_list_array['extra_'.$field_details[1]]['visibility'] = 1; } break; - case UserManager::USER_FIELD_TYPE_RADIO: + case ExtraField::FIELD_TYPE_RADIO: $field_list_array['extra_'.$field_details[1]]['name'] = $field_details[3]; if ($field_details[7] == 0) { @@ -4578,7 +4578,7 @@ class SurveyUtil { } break; - case UserManager::USER_FIELD_TYPE_SELECT: + case ExtraField::FIELD_TYPE_SELECT: $field_list_array['extra_'.$field_details[1]]['name'] = $field_details[3]; if ($field_details[7] == 0) { $field_list_array['extra_'.$field_details[1]]['visibility'] = 0; @@ -4587,7 +4587,7 @@ class SurveyUtil { } break; - case UserManager::USER_FIELD_TYPE_SELECT_MULTIPLE: + case ExtraField::FIELD_TYPE_SELECT_MULTIPLE: $field_list_array['extra_'.$field_details[1]]['name'] = $field_details[3]; if ($field_details[7] == 0) { $field_list_array['extra_'.$field_details[1]]['visibility'] = 0; @@ -4596,7 +4596,7 @@ class SurveyUtil { } break; - case UserManager::USER_FIELD_TYPE_DATE: + case ExtraField::FIELD_TYPE_DATE: $field_list_array['extra_'.$field_details[1]]['name'] = $field_details[3]; if ($field_details[7] == 0) { $field_list_array['extra_'.$field_details[1]]['visibility'] = 0; @@ -4605,7 +4605,7 @@ class SurveyUtil { } break; - case UserManager::USER_FIELD_TYPE_DATETIME: + case ExtraField::FIELD_TYPE_DATETIME: $field_list_array['extra_'.$field_details[1]]['name'] = $field_details[3]; if ($field_details[7] == 0) { $field_list_array['extra_'.$field_details[1]]['visibility'] = 0; @@ -4614,7 +4614,7 @@ class SurveyUtil { } break; - case UserManager::USER_FIELD_TYPE_DOUBLE_SELECT: + case ExtraField::FIELD_TYPE_DOUBLE_SELECT: $field_list_array['extra_'.$field_details[1]]['name'] = $field_details[3]; if ($field_details[7] == 0) { $field_list_array['extra_'.$field_details[1]]['visibility'] = 0; @@ -4652,7 +4652,7 @@ class SurveyUtil { } }*/ break; - case UserManager::USER_FIELD_TYPE_DIVIDER: + case ExtraField::FIELD_TYPE_DIVIDER: //$form->addElement('static',$field_details[1], '
'.$field_details[3].''); break; } diff --git a/main/user/subscribe_user.php b/main/user/subscribe_user.php index e215de5cee..7b38d6e8ad 100644 --- a/main/user/subscribe_user.php +++ b/main/user/subscribe_user.php @@ -685,14 +685,14 @@ function display_extra_profile_fields_filter() { // $field_details[2] contains the type of the additional user profile field switch($field_details[2]) { // text fields cannot be used as a filter - case UserManager::USER_FIELD_TYPE_TEXT: + case ExtraField::FIELD_TYPE_TEXT: break; // text area fields cannot be used as a filter - case UserManager::USER_FIELD_TYPE_TEXTAREA: + case ExtraField::FIELD_TYPE_TEXTAREA: break; - case UserManager::USER_FIELD_TYPE_RADIO: - case UserManager::USER_FIELD_TYPE_SELECT: - case UserManager::USER_FIELD_TYPE_SELECT_MULTIPLE: + case ExtraField::FIELD_TYPE_RADIO: + case ExtraField::FIELD_TYPE_SELECT: + case ExtraField::FIELD_TYPE_SELECT_MULTIPLE: $return .= ''; foreach($field_details[9] as $option_id => $option_details) { if ($_GET['subscribe_user_filter_value'] == $field_details[0].'*'.$option_details[1]) {