parent
ecfbe6420a
commit
c87c20b383
@ -0,0 +1,175 @@ |
||||
<?php |
||||
|
||||
/* For licensing terms, see /license.txt */ |
||||
/** |
||||
* @package chamilo.admin |
||||
*/ |
||||
// name of the language file that needs to be included |
||||
$language_file = array('admin', 'registration'); |
||||
|
||||
// resetting the course information |
||||
$cidReset = true; |
||||
|
||||
// including the global library |
||||
require '../inc/global.inc.php'; |
||||
|
||||
// section for the tabs |
||||
$this_section = SECTION_PLATFORM_ADMIN; |
||||
|
||||
// user permissions |
||||
api_protect_admin_script(); |
||||
|
||||
// breadcrumbs |
||||
$interbreadcrumb[] = array('url' => '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 .= '<a href="' . api_get_self() . '?action=moveup&option_id=' . $option_id . '&field_id=' . Security::remove_XSS($_GET['field_id']) . '&sec_token=' . $_SESSION['sec_token'] . '">' . Display::return_icon('up.gif', get_lang('Up')) . '</a>'; |
||||
} 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 .= '<a href="' . api_get_self() . '?action=movedown&option_id=' . $option_id . '&field_id=' . Security::remove_XSS($_GET['field_id']) . '&sec_token=' . $_SESSION['sec_token'] . '">' . Display::return_icon('down.gif', get_lang('Down')) . '</a>'; |
||||
} |
||||
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 <patrick.cool@UGent.be>, 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; |
||||
} |
@ -0,0 +1,494 @@ |
||||
<?php |
||||
|
||||
class ExtraField 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'); |
||||
|
||||
CONST FIELD_TYPE_TEXT = 1; |
||||
CONST FIELD_TYPE_TEXTAREA = 2; |
||||
CONST FIELD_TYPE_RADIO = 3; |
||||
CONST FIELD_TYPE_SELECT = 4; |
||||
CONST FIELD_TYPE_SELECT_MULTIPLE = 5; |
||||
CONST FIELD_TYPE_DATE = 6; |
||||
CONST FIELD_TYPE_DATETIME = 7; |
||||
CONST FIELD_TYPE_DOUBLE_SELECT = 8; |
||||
CONST FIELD_TYPE_DIVIDER = 9; |
||||
CONST FIELD_TYPE_TAG = 10; |
||||
CONST FIELD_TYPE_TIMEZONE = 11; |
||||
CONST FIELD_TYPE_SOCIAL_PROFILE = 12; |
||||
|
||||
public $type = 'user'; //or session |
||||
public $handler_id = 'user_id'; |
||||
|
||||
function __construct($type) { |
||||
$this->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'].'<br />',$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($("<option/>", { |
||||
value: index, |
||||
text: value |
||||
})); |
||||
}); |
||||
}, |
||||
}); |
||||
});'; |
||||
|
||||
$first_id = null; |
||||
$second_id = null; |
||||
|
||||
if (!empty($extra_data)) { |
||||
$first_id = $extra_data['extra_'.$field_details['field_variable']]['extra_'.$field_details['field_variable']]; |
||||
$second_id = $extra_data['extra_'.$field_details['field_variable']]['extra_'.$field_details['field_variable'].'_second']; |
||||
} |
||||
|
||||
$options = ExtraField::extra_field_double_select_convert_array_to_ordered_array($field_details['options']); |
||||
$values = array(); |
||||
$second_values = array(); |
||||
if (!empty($options)) { |
||||
foreach ($options as $option) { |
||||
foreach ($option as $sub_option) { |
||||
if ($sub_option['option_value'] == '0') { |
||||
$values[$sub_option['id']] = $sub_option['option_display_text']; |
||||
} else { |
||||
if ($first_id === $sub_option['option_value']) { |
||||
$second_values[$sub_option['id']] = $sub_option['option_display_text']; |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
$group = array(); |
||||
$group[] = $form->createElement('select', 'extra_'.$field_details['field_variable'], null, $values, array('id' => $first_select_id)); |
||||
$group[] = $form->createElement('select', 'extra_'.$field_details['field_variable'].'_second', null, $second_values, array('id'=>'second_extra_'.$field_details['field_variable'])); |
||||
$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_DIVIDER: |
||||
$form->addElement('static', $field_details['field_variable'], '<br /><strong>'.$field_details['field_display_text'].'</strong>'); |
||||
break; |
||||
case ExtraField::FIELD_TYPE_TAG: |
||||
//the magic should be here |
||||
$user_tags = UserManager::get_user_tags($user_id, $field_details['id']); |
||||
|
||||
$tag_list = ''; |
||||
if (is_array($user_tags) && count($user_tags) > 0) { |
||||
foreach ($user_tags as $tag) { |
||||
$tag_list .= '<option value="'.$tag['tag'].'" class="selected">'.$tag['tag'].'</option>'; |
||||
} |
||||
} |
||||
|
||||
$multi_select = '<select id="extra_'.$field_details['field_variable'].'" name="extra_'.$field_details['field_variable'].'"> |
||||
'.$tag_list.' |
||||
</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 .= <<<EOF |
||||
$("#extra_$field_variable").fcbkcomplete({ |
||||
json_url: "$url?a=search_tags&field_id=$field_id", |
||||
cache: false, |
||||
filter_case: true, |
||||
filter_hide: true, |
||||
complete_text:"$complete_text", |
||||
firstselected: true, |
||||
//onremove: "testme", |
||||
//onselect: "testme", |
||||
filter_selected: true, |
||||
newel: true |
||||
}); |
||||
EOF; |
||||
break; |
||||
case ExtraField::FIELD_TYPE_TIMEZONE: |
||||
$form->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; |
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,244 @@ |
||||
<?php |
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
class ExtraFieldOption extends Model { |
||||
public $columns = array('id', 'field_id', 'option_value', 'option_display_text', 'option_order', 'tms'); |
||||
|
||||
public function __construct($type) { |
||||
$this->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); |
||||
} |
||||
} |
@ -0,0 +1,183 @@ |
||||
<?php |
||||
|
||||
class ExtraFieldValue extends Model { |
||||
public $type = null; |
||||
public $columns = array('id', 'field_id', 'field_value', 'tms'); |
||||
public $handler_id = null; |
||||
|
||||
public function __construct($type) { |
||||
$this->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); |
||||
} |
||||
} |
@ -1,237 +1,8 @@ |
||||
<?php |
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
class SessionFieldOption extends Model { |
||||
public $columns = array('id', 'field_id', 'option_value', 'option_display_text', 'option_order', 'tms'); |
||||
|
||||
public function __construct() { |
||||
$this->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'); |
||||
} |
||||
} |
@ -1,175 +1,6 @@ |
||||
<?php |
||||
|
||||
class SessionFieldValue extends Model { |
||||
public $columns = array('id', 'session_id', 'field_id', 'field_value', 'tms'); |
||||
|
||||
public function __construct() { |
||||
$this->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'); |
||||
} |
||||
} |
Loading…
Reference in new issue