1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-05 06:07:32 +02:00

Restructure use of predefined lists in extended user classes - get rid of global, more general interface which can be used with a DB or similar as well.

This commit is contained in:
SteveD
2012-12-21 22:33:02 +00:00
parent 5768214ca8
commit 918c2bfc34
2 changed files with 1036 additions and 982 deletions

View File

@@ -2,7 +2,7 @@
/* /*
* e107 website system * e107 website system
* *
* Copyright (C) 2008-2009 e107 Inc (e107.org) * Copyright (C) 2008-2013 e107 Inc (e107.org)
* Released under the terms and conditions of the * Released under the terms and conditions of the
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
* *
@@ -17,62 +17,118 @@
if (!defined('e107_INIT')) { exit; } if (!defined('e107_INIT')) { exit; }
/* /*
This file is used with the extended user field 'predefined list' type. Its invoked when the value field is 'timezones'. This file is used with the extended user field 'predefined list' type. It is invoked when the value field is 'timezones'.
It is an example of an extended user field which access a predetermined list of key-pair values. In this example all the data is loaded
into memory; for other applications the data may be read from a database, possibly with caching.
The objective is to provide a uniform interface to such data.
The class name must be the same as the file name - i.e. the list name prefixed with 'extended_'.
The variable name must be 'timezones_list', and is an array of possible values, each of which is a value => text pair The variable name must be 'timezones_list', and is an array of possible values, each of which is a value => text pair
The text is displayed in a drop-down; the value is returned. The text is displayed in a drop-down; the value is returned.
If function timezones_value() exists, it is called to create the displayed text If function timezones_value() exists, it is called to create the displayed text
*/ */
//FIXME - remove globals. class extended_timezones
global $timezones_list;
if (!is_array($timezones_list))
{ {
$timezones_list = array(
array('-12', "International DateLine West"), private $timezonesList = array(
array('-11', "Samoa"), '-12' => "International DateLine West",
array('-10', "Hawaii"), '-11' => "Samoa",
array( '-9', "Alaska"), '-10' => "Hawaii",
array( '-8', "Pacific Time (US and Canada)"), '-9' => "Alaska",
array( '-7', "Mountain Time (US and Canada)"), '-8' => "Pacific Time (US and Canada)",
array( '-6', "Central Time (US and Canada), Central America"), '-7' => "Mountain Time (US and Canada)",
array( '-5', "Eastern Time (US and Canada)"), '-6' => "Central Time (US and Canada), Central America",
array( '-4', "Atlantic Time (Canada)"), '-5' => "Eastern Time (US and Canada)",
array( '-3.30', 'Newfoundland'), '-4' => "Atlantic Time (Canada)",
array( '-3', "Greenland, Brasilia, Buenos Aires, Georgetown"), '-3.30' => 'Newfoundland',
array( '-2', "Mid-Atlantic"), '-3' => "Greenland, Brasilia, Buenos Aires, Georgetown",
array( '-1', "Azores, Cape Verde Islands"), '-2' => "Mid-Atlantic",
array( '+0', "UK, Ireland, Lisbon"), '-1' => "Azores, Cape Verde Islands",
array( '+1', "West Central Africa, Western Europe"), '+0' => "UK, Ireland, Lisbon",
array( '+2', "Greece, Egypt, parts of Africa"), '+1' => "West Central Africa, Western Europe",
array( '+3', "Russia, Baghdad, Kuwait, Nairobi"), '+2' => "Greece, Egypt, parts of Africa",
array( '+3.30', 'Tehran, Iran'), '+3' => "Russia, Baghdad, Kuwait, Nairobi",
array( '+4', "Abu Dhabi, Kabul"), '+3.30' => 'Tehran, Iran',
array( '+4.30', 'Afghanistan'), '+4' => "Abu Dhabi, Kabul",
array( '+5', "Islamabad, Karachi"), '+4.30' => 'Afghanistan',
array( '+5.30', "Mumbai, Delhi, Calcutta"), '+5' => "Islamabad, Karachi",
array( '+5.45', 'Kathmandu'), '+5.30' => "Mumbai, Delhi, Calcutta",
array( '+6', "Astana, Dhaka"), '+5.45' => 'Kathmandu',
array( '+7', "Bangkok, Rangoon"), '+6' => "Astana, Dhaka",
array( '+8', "Hong Kong, Singapore, Perth, Beijing"), '+7' => "Bangkok, Rangoon",
array( '+9', "Tokyo, Seoul"), '+8' => "Hong Kong, Singapore, Perth, Beijing",
array( '+9.30', 'Darwin, Adelaide'), '+9' => "Tokyo, Seoul",
array('+10', "Brisbane, Canberra, Sydney, Melbourne"), '+9.30' => 'Darwin, Adelaide',
array('+10.30', 'Lord Howe Island'), '+10' => "Brisbane, Canberra, Sydney, Melbourne",
array('+11', "Soloman Islands"), '+10.30' => 'Lord Howe Island',
array('+11.30', 'Norfolk Island'), '+11' => "Soloman Islands",
array('+12', "New Zealand, Fiji, Marshall Islands"), '+11.30' => 'Norfolk Island',
array('+13', "Tonga, Nuku'alofa, Rawaki Islands"), '+12' => "New Zealand, Fiji, Marshall Islands",
array('+13.45', 'Chatham Island'), '+13' => "Tonga, Nuku'alofa, Rawaki Islands",
array('+14', 'Kiribati: Line Islands') '+13.45' => 'Chatham Island',
); '+14' => 'Kiribati: Line Islands'
);
private $isEOF = FALSE; // True if at last element of list
private $bufferValid = FALSE;
/**
* Call before using the 'next' format option, to ensure the array is indexed from the beginning
*/
public function pointerReset()
{
$this->isEOF = (FALSE === reset($this->timezonesList));
$this->bufferValid = TRUE;
}
/**
* Return a formatted timezone value
*
* @param mixed $key - the key value to select
* @param string $formatSpec - defines format of return value
*
* @return mixed (according to $formatSpec). FALSE if no value available
* 'array' - a single-element array; key as passed, and value to match key
* 'next' - as 'array', but ignores the passed $key and moves to next value.
* default - a string usable for display
*/
public function getValue($key, $formatSpec = '')
{
if ($formatSpec == 'next')
{
if (!$this->bufferValid) $this->pointerReset; // Make sure buffer is defined
if ($this->isEOF) return FALSE;
$key = key($this->timezonesList);
$val = current($this->timezonesList);
if (FALSE === $val)
{
$this->isEOF = TRUE;
return FALSE;
}
$this->isEOF = (FALSE === next($this->timezonesList));
return array($key => $val);
}
$exists = isset($this->timezonesList[$key]);
if (!$exists) return FALSE;
$val = $this->timezonesList[$key];
if ($formatSpec == 'array')
{
return array($key => $val);
}
// Default (as per earlier implementations) - can be specified with 'display' format
return 'GMT'.$key.' - '.$val;
}
} }
if (!function_exists('timezones_value'))
{
function timezones_value($key, $value)
{
return 'GMT'.$key.' - '.$value;
}
}
?> ?>

View File

@@ -648,26 +648,27 @@ class e107_user_extended
break; break;
case EUF_PREDEFINED : // predefined list, shown in dropdown case EUF_PREDEFINED : // predefined list, shown in dropdown
$filename = e_ADMIN.'sql/extended_'.trim($struct['user_extended_struct_values']).'.php'; $listRoot = trim($struct['user_extended_struct_values']); // Base list name
if (!is_readable($filename)) return 'No file: '.$filename; $filename = e_ADMIN.'sql/extended_'.$listRoot.'.php';
require($filename); if (!is_readable($filename)) return 'No file: '.$filename;
$list_name = $struct['user_extended_struct_values'].'_list'; require_once($filename);
$display_func = $struct['user_extended_struct_values'].'_value'; $className = 'extended_'.$listRoot;
if (!function_exists($display_func)) $display_func = ''; if (!class_exists($className)) return '?????';
$source_data = $$list_name; $temp = new $className();
$ret = "<select {$include} name='{$fname}'>\n"; if (!method_exists($className, 'getValue')) return '???-???';
$ret .= "<option value=''>&nbsp;</option>\n"; // ensures that the user chose it. $temp->pointerReset();
foreach($source_data as $v)
{ $ret = "<select {$include} name='{$fname}'>\n";
$val = $v[0]; $ret .= "<option value=''>&nbsp;</option>\n"; // ensures that the user chooses it.
$choice = trim($v[1]); while (FALSE !== ($row = $temp->getValue(0, 'next')))
if ($display_func) $choice = $display_func($val,$choice); {
$sel = ($curval == $val) ? " selected='selected' " : ""; $val = key($row);
$ret .= "<option value='{$val}' {$sel}>{$choice}</option>\n"; $choice = $temp->getValue($val, 'display');
} $sel = ($curval == $val) ? " selected='selected' " : '';
$ret .= "</select>\n"; $ret .= "<option value='{$val}' {$sel}>{$choice}</option>\n";
return $ret; }
break; $ret .= "</select>\n";
return $ret;
case EUF_DB_FIELD : //db_field case EUF_DB_FIELD : //db_field
global $sql; global $sql;
@@ -881,25 +882,22 @@ class e107_user_extended
return $uinfo[$field_name]; return $uinfo[$field_name];
} }
// Given a predefined list field, returns the display text corresponding to the passed value
function user_extended_display_text($table,$value) /**
* Given a predefined list field, returns the display text corresponding to the passed value
*
* @TODO: consider whether to cache the class object
*/
function user_extended_display_text($table, $value)
{ {
$filename = e_ADMIN.'sql/extended_'.$table.'.php'; $filename = e_ADMIN.'sql/extended_'.$table.'.php';
if (!is_readable($filename)) return 'No file: '.$filename; if (!is_readable($filename)) return 'No file: '.$filename;
require_once($filename); require_once($filename);
$list_name = $table.'_list'; $className = 'extended_'.$table;
$display_func = $table.'_value'; if (!class_exists($className)) return '?????';
if (!function_exists($display_func)) $display_func = ''; $temp = new $className();
$source_data = $$list_name; if (!method_exists($className, 'getValue')) return '???-???';
foreach($source_data as $v) return $temp->getValue($value);
{
if ($value == $v[0])
{
if ($display_func) return $display_func($v[0],$v[1]);
return $v[1];
}
}
return '????';
} }
} }