2007-06-13 22:03:01 +00:00
< ? php
2006-12-02 04:36:16 +00:00
/*
+ ----------------------------------------------------------------------------+
| e107 website system
|
2007-03-04 13:53:37 +00:00
| Steve Dunstan 2001 - 2002
2006-12-02 04:36:16 +00:00
| http :// e107 . org
| jalist @ e107 . org
|
| Released under the terms and conditions of the
| GNU General Public License ( http :// gnu . org ) .
|
| $Source : / cvs_backup / e107_0 . 8 / e107_handlers / user_extended_class . php , v $
2009-08-06 20:29:58 +00:00
| $Revision : 1.24 $
| $Date : 2009 - 08 - 06 20 : 29 : 58 $
| $Author : e107coders $
2006-12-02 04:36:16 +00:00
+----------------------------------------------------------------------------+
*/
if ( ! defined ( 'e107_INIT' )) { exit ; }
/*
2009-01-11 21:06:52 +00:00
Code uses two tables :
user_extended_struct - individual field definitions , one record per field
user_extended - actual field data , one record per user
//TODO: Should user_extended_validate_entry() ckech DB for DB-type fields?
2006-12-02 04:36:16 +00:00
*/
2009-01-11 21:06:52 +00:00
include_lan ( e_LANGUAGEDIR . e_LANGUAGE . '/lan_user_extended.php' );
2006-12-02 04:36:16 +00:00
class e107_user_extended
{
var $user_extended_types ;
var $extended_xml ;
var $typeArray ;
var $reserved_names ;
2009-01-11 21:06:52 +00:00
var $fieldDefinitions ; // Array initialised from DB by constructor
var $nameIndex ; // Array for field name lookup - initialised by constructor
2006-12-02 04:36:16 +00:00
function e107_user_extended ()
{
2009-01-11 21:06:52 +00:00
define ( 'EUF_TEXT' , 1 );
define ( 'EUF_RADIO' , 2 );
define ( 'EUF_DROPDOWN' , 3 );
define ( 'EUF_DB_FIELD' , 4 );
define ( 'EUF_TEXTAREA' , 5 );
define ( 'EUF_INTEGER' , 6 );
define ( 'EUF_DATE' , 7 );
define ( 'EUF_LANGUAGE' , 8 );
define ( 'EUF_PREDEFINED' , 9 );
2009-08-06 20:29:58 +00:00
define ( 'EUF_CHECKBOX' , 10 );
2009-01-11 21:06:52 +00:00
$this -> typeArray = array (
2006-12-02 04:36:16 +00:00
'text' => 1 ,
'radio' => 2 ,
'dropdown' => 3 ,
'db field' => 4 ,
'textarea' => 5 ,
'integer' => 6 ,
'date' => 7 ,
2008-01-15 21:57:53 +00:00
'language' => 8 ,
2009-08-06 20:29:58 +00:00
'list' => 9 ,
'checkbox' => 10
2009-01-11 21:06:52 +00:00
);
$this -> user_extended_types = array (
1 => UE_LAN_1 ,
2 => UE_LAN_2 ,
3 => UE_LAN_3 ,
4 => UE_LAN_4 ,
5 => UE_LAN_5 ,
6 => UE_LAN_6 ,
7 => UE_LAN_7 ,
8 => UE_LAN_8 ,
2009-08-06 20:29:58 +00:00
9 => UE_LAN_9 ,
10 => UE_LAN_10
2009-01-11 21:06:52 +00:00
);
2006-12-02 04:36:16 +00:00
//load array with field names from main user table, so we can disallow these
2008-01-15 21:57:53 +00:00
// user_new, user_timezone deleted for 0.8
2006-12-02 04:36:16 +00:00
$this -> reserved_names = array (
'id' , 'name' , 'loginname' , 'customtitle' , 'password' ,
2008-01-15 21:57:53 +00:00
'sess' , 'email' , 'signature' , 'image' , 'hideemail' ,
2008-12-21 11:07:58 +00:00
'join' , 'lastvisit' , 'currentvisit' , 'chats' ,
2008-01-15 21:57:53 +00:00
'comments' , 'forums' , 'ip' , 'ban' , 'prefs' , 'viewed' ,
2008-12-28 22:37:43 +00:00
'visits' , 'admin' , 'login' , 'class' , 'baseclasslist' , 'perms' , 'pwchange' ,
2006-12-02 04:36:16 +00:00
'xup'
);
2009-01-11 21:06:52 +00:00
$this -> fieldDefinitions = $this -> user_extended_get_fieldList (); // Assume that we'll need these if an object has been instantiated
$this -> nameIndex = array ();
foreach ( $this -> fieldDefinitions as $k => $v )
{
$this -> nameIndex [ 'user_' . $v [ 'user_extended_struct_name' ]] = $k ; // Create name to ID index
}
2006-12-02 04:36:16 +00:00
}
function user_extended_reserved ( $name )
{
2008-01-15 21:57:53 +00:00
return ( in_array ( $name , $this -> reserved_names ));
2006-12-02 04:36:16 +00:00
}
2008-01-15 21:57:53 +00:00
2009-01-11 21:06:52 +00:00
// Adds the _FIELD_TYPES array to the data, ready for saving in the DB.
function addFieldTypes ( & $target )
{
$target [ '_FIELD_TYPES' ] = array (); // We should always want to recreate the array, even if it exists
foreach ( $target [ 'data' ] as $k => $v )
{
if ( isset ( $this -> nameIndex [ $k ]))
{
switch ( $this -> fieldDefinitions [ $this -> nameIndex [ $k ]][ 'user_extended_struct_type' ])
{
case EUF_TEXT :
case EUF_DB_FIELD :
case EUF_TEXTAREA :
case EUF_DROPDOWN :
case EUF_DATE :
case EUF_LANGUAGE :
case EUF_PREDEFINED :
2009-08-06 20:29:58 +00:00
case EUF_CHECKBOX :
2009-01-11 21:06:52 +00:00
$target [ '_FIELD_TYPES' ][ $k ] = 'todb' ;
break ;
case EUF_RADIO :
case EUF_INTEGER :
$target [ '_FIELD_TYPES' ][ $k ] = 'int' ;
break ;
}
}
}
}
// For all UEFs not in the target array, adds the default value
// Also updates the _FIELD_TYPES array, so call this last thing before writing to the DB
function addDefaultFields ( & $target )
{
$target [ '_FIELD_TYPES' ] = array (); // We should always want to recreate the array, even if it exists
foreach ( $this -> fieldDefinitions as $k => $defs )
{
$f = 'user_' . $defs [ 'user_extended_struct_name' ];
if ( ! isset ( $target [ 'data' ][ $f ]))
{
switch ( $this -> fieldDefinitions [ $k ][ 'user_extended_struct_type' ])
{
case EUF_TEXT :
case EUF_DB_FIELD :
case EUF_TEXTAREA :
case EUF_DROPDOWN :
case EUF_DATE :
case EUF_LANGUAGE :
case EUF_PREDEFINED :
2009-08-06 20:29:58 +00:00
2009-01-11 21:06:52 +00:00
$target [ 'data' ][ $f ] = $this -> fieldDefinitions [ $k ][ 'user_extended_struct_default' ];
$target [ '_FIELD_TYPES' ][ $f ] = 'todb' ;
break ;
case EUF_RADIO :
case EUF_INTEGER :
$target [ 'data' ][ $f ] = $this -> fieldDefinitions [ $k ][ 'user_extended_struct_default' ];
$target [ '_FIELD_TYPES' ][ $f ] = 'int' ;
break ;
2009-08-06 20:29:58 +00:00
case EUF_CHECKBOX :
$target [ 'data' ][ $f ] = $this -> fieldDefinitions [ $k ][ 'user_extended_struct_default' ];
$target [ '_FIELD_TYPES' ][ $f ] = 'array' ;
break ;
2009-01-11 21:06:52 +00:00
}
}
}
}
2008-12-28 22:37:43 +00:00
// Validate a single extended user field
2008-06-13 20:20:23 +00:00
// $val is whatever the user entered.
// $params is the field definition
// Return FALSE if acceptable, TRUE if fail , error message on regex fail if the message is defined
function user_extended_validate_entry ( $val , $params )
{
2008-12-28 22:37:43 +00:00
global $tp ;
2009-01-11 21:06:52 +00:00
$parms = explode ( '^,^' , $params [ 'user_extended_struct_parms' ]);
2008-12-28 22:37:43 +00:00
$requiredField = $params [ 'user_extended_struct_required' ] == 1 ;
$regex = $tp -> toText ( $parms [ 1 ]);
$regexfail = $tp -> toText ( $parms [ 2 ]);
if ( defined ( $regexfail )) { $regexfail = constant ( $regexfail ); }
if ( $val == '' && $requiredField ) return TRUE ;
switch ( $type )
{
case EUF_DATE :
if ( $requiredField && ( $val == '0000-00-00' )) return TRUE ;
break ;
}
if ( $regex != " " && $val != " " )
{
if ( ! preg_match ( $regex , $val )) return $regexfail ? $regexfail : TRUE ;
}
return FALSE ; // Pass by default here
2008-06-13 20:20:23 +00:00
}
2008-12-28 22:37:43 +00:00
// Validate all user-modifable extended user fields which are presented.
// $inArray is the input data (usually from $_POST or $_POST['ue'], although doesn't have to be) - may have 'surplus' values
// $hideArray is a set of possible 'hide' flags
2009-04-23 19:13:18 +00:00
// $isSignup TRUE causes required fields to be specifically checked, else only data passed is checked
function userExtendedValidateAll ( $inArray , $hideArray , $isSignup = FALSE )
2008-12-28 22:37:43 +00:00
{
global $tp ;
$eufVals = array (); // 'Answer' array
$hideFlags = array ();
2009-01-11 21:06:52 +00:00
foreach ( $this -> fieldDefinitions as $k => $defs )
2008-12-28 22:37:43 +00:00
{
$f = 'user_' . $defs [ 'user_extended_struct_name' ];
2009-04-23 19:13:18 +00:00
if ( isset ( $inArray [ $f ]) || ( $isSignup && ( $defs [ 'user_extended_struct_required' ] == 1 )))
2008-12-28 22:37:43 +00:00
{ // Only allow valid keys
2009-04-23 19:13:18 +00:00
$val = varset ( $inArray [ $f ], FALSE );
2009-08-06 20:29:58 +00:00
$err = $this -> user_extended_validate_entry ( $val , $defs );
2008-12-28 22:37:43 +00:00
if ( $err === true )
{ // General error - usually empty field; could be unacceptable value, or regex fail and no error message defined
$eufVals [ 'errortext' ][ $f ] = str_replace ( '--SOMETHING--' , $tp -> toHtml ( $defs [ 'user_extended_struct_text' ], FALSE , 'defs' ), LAN_USER_75 );
$eufVals [ 'errors' ][ $f ] = ERR_GENERIC ;
}
elseif ( $err )
{ // Specific error message returned - usually regex fail
2009-08-06 20:29:58 +00:00
$eufVals [ 'errortext' ][ $f ] = $err ;
$eufVals [ 'errors' ][ $f ] = ERR_GENERIC ;
2008-12-28 22:37:43 +00:00
}
elseif ( ! $err )
{
2009-01-11 21:06:52 +00:00
$eufVals [ 'data' ][ $f ] = $tp -> toDB ( $val );
2008-12-28 22:37:43 +00:00
}
if ( isset ( $hideArray [ $f ]))
{
$hideFlags [] = $f ;
}
}
}
$hidden_fields = implode ( " ^ " , $hideFlags );
if ( $hidden_fields != " " )
{
$hidden_fields = " ^ " . $hidden_fields . " ^ " ;
}
2009-01-11 21:06:52 +00:00
$eufVals [ 'data' ][ 'user_hidden_fields' ] = $hidden_fields ;
2009-08-06 20:29:58 +00:00
2008-12-28 22:37:43 +00:00
return $eufVals ;
}
2008-06-13 20:20:23 +00:00
2006-12-02 04:36:16 +00:00
function user_extended_get_categories ( $byID = TRUE )
{
2007-11-13 07:45:12 +00:00
$ret = array ();
2006-12-02 04:36:16 +00:00
global $sql ;
if ( $sql -> db_Select ( " user_extended_struct " , " * " , " user_extended_struct_type = 0 ORDER BY user_extended_struct_order ASC " ))
{
if ( $byID == TRUE )
{
while ( $row = $sql -> db_Fetch ())
{
$ret [ $row [ 'user_extended_struct_id' ]][] = $row ;
}
}
else
{
$ret = $sql -> db_getList ();
}
}
return $ret ;
}
2007-04-14 16:50:50 +00:00
// Get the definition of all fields, or those in a specific category, grouped by category ID
2006-12-02 04:36:16 +00:00
function user_extended_get_fields ( $cat = " " )
{
global $sql ;
2008-04-06 21:38:02 +00:00
$ret = array ();
2006-12-02 04:36:16 +00:00
$more = ( $cat ) ? " AND user_extended_struct_parent = " . intval ( $cat ) . " " : " " ;
2007-01-13 05:04:12 +00:00
if ( $sql -> db_Select ( " user_extended_struct " , " * " , " user_extended_struct_type > 0 AND user_extended_struct_text != '_system_' { $more } ORDER BY user_extended_struct_order ASC " ))
2006-12-02 04:36:16 +00:00
{
2008-12-28 22:37:43 +00:00
while ( $row = $sql -> db_Fetch ( MYSQL_ASSOC ))
2006-12-02 04:36:16 +00:00
{
$ret [ $row [ 'user_extended_struct_parent' ]][] = $row ;
}
}
return $ret ;
}
2009-01-11 21:06:52 +00:00
// Get the definition of all fields, or those in a specific category, indexed by field ID (or some other field by specifying $indexField)
2008-12-02 20:23:32 +00:00
function user_extended_get_fieldList ( $cat = " " , $indexField = 'user_extended_struct_id' )
2006-12-02 04:36:16 +00:00
{
global $sql ;
2007-07-30 20:25:36 +00:00
$more = ( $cat != '' ) ? " AND user_extended_struct_parent = " . intval ( $cat ) . " " : " " ;
2008-12-02 20:23:32 +00:00
if ( $sql -> db_Select ( " user_extended_struct " , " * " , " user_extended_struct_type > 0 AND user_extended_struct_text != '_system_' { $more } ORDER BY user_extended_struct_order ASC " ))
2006-12-02 04:36:16 +00:00
{
2008-12-28 22:37:43 +00:00
while ( $row = $sql -> db_Fetch ( MYSQL_ASSOC ))
2006-12-02 04:36:16 +00:00
{
2008-12-02 20:23:32 +00:00
$ret [ $row [ $indexField ]] = $row ;
2006-12-02 04:36:16 +00:00
}
}
return $ret ;
}
2008-01-15 21:57:53 +00:00
2009-01-11 21:06:52 +00:00
// Return the field creation text for a definition
2006-12-02 04:36:16 +00:00
function user_extended_type_text ( $type , $default )
{
2008-01-15 21:57:53 +00:00
global $tp ;
switch ( $type )
{
case EUF_INTEGER :
$db_type = 'INT(11)' ;
break ;
case EUF_DATE :
$db_type = 'DATE NOT NULL' ;
break ;
case EUF_TEXTAREA :
$db_type = 'TEXT' ;
break ;
case EUF_TEXT :
case EUF_RADIO :
case EUF_DROPDOWN :
case EUF_DB_FIELD :
case EUF_LANGUAGE :
case EUF_PREDEFINED :
2009-08-06 20:29:58 +00:00
case EUF_CHECKBOX :
2008-01-15 21:57:53 +00:00
$db_type = 'VARCHAR(255)' ;
break ;
}
2008-12-10 13:14:51 +00:00
if ( $type != EUF_DB_FIELD && $type != EUF_TEXTAREA && $default != '' )
2008-01-15 21:57:53 +00:00
{
$default_text = " DEFAULT ' " . $tp -> toDB ( $default , true ) . " ' " ;
}
else
{
$default_text = '' ;
}
return $db_type . $default_text ;
2006-12-02 04:36:16 +00:00
}
2008-01-15 21:57:53 +00:00
2006-12-02 04:36:16 +00:00
function user_extended_field_exist ( $name )
{
2008-01-15 21:57:53 +00:00
global $sql , $tp ;
return $sql -> db_Count ( 'user_extended_struct' , '(*)' , " WHERE user_extended_struct_name = ' " . $tp -> toDB ( $name , true ) . " ' " );
2006-12-02 04:36:16 +00:00
}
2008-11-30 23:15:15 +00:00
function clear_cache ()
{
$e107 = e107 :: getInstance ();
2008-12-10 13:14:51 +00:00
$e107 -> ecache -> clear_sys ( 'nomd5_extended_struct' );
2008-11-30 23:15:15 +00:00
}
2008-01-15 21:57:53 +00:00
2007-01-13 05:04:12 +00:00
// For use by plugins to add extended user fields and won't be visible anywhere else
2008-11-24 00:36:50 +00:00
function user_extended_add_system ( $name , $type , $default = '' , $source = '_system_' )
2007-01-13 05:04:12 +00:00
{
2008-11-24 00:36:50 +00:00
return $this -> user_extended_add ( $name , '_system_' , $type , $source , '' , $default , 0 , 255 , 255 , 255 , 0 , 0 );
2007-01-13 05:04:12 +00:00
}
2008-01-15 21:57:53 +00:00
2006-12-02 04:36:16 +00:00
function user_extended_add ( $name , $text , $type , $parms , $values , $default , $required , $read , $write , $applicable , $order = '' , $parent )
{
2008-01-15 21:57:53 +00:00
global $sql , $tp ;
2008-11-30 23:15:15 +00:00
$this -> clear_cache ();
2008-01-15 21:57:53 +00:00
if ( is_array ( $name ))
{
extract ( $name );
}
if ( ! is_numeric ( $type ))
{
$type = $this -> typeArray [ $type ];
}
if ( ! $this -> user_extended_field_exist ( $name ) && ! $this -> user_extended_reserved ( $name ))
{
$field_info = $this -> user_extended_type_text ( $type , $default );
if ( $order === '' )
2006-12-02 04:36:16 +00:00
{
2008-11-30 23:15:15 +00:00
if ( $sql -> db_Select ( 'user_extended_struct' , 'MAX(user_extended_struct_order) as maxorder' , '1' ))
2008-01-15 21:57:53 +00:00
{
$row = $sql -> db_Fetch ();
if ( is_numeric ( $row [ 'maxorder' ]))
2006-12-02 04:36:16 +00:00
{
2008-01-15 21:57:53 +00:00
$order = $row [ 'maxorder' ] + 1 ;
2006-12-02 04:36:16 +00:00
}
2008-01-15 21:57:53 +00:00
}
2006-12-02 04:36:16 +00:00
}
2008-12-10 13:14:51 +00:00
$sql -> db_Select_gen ( 'ALTER TABLE #user_extended ADD user_' . $tp -> toDB ( $name , true ) . ' ' . $field_info );
2008-11-30 23:15:15 +00:00
$sql -> db_Insert ( 'user_extended_struct' , " 0,' " . $tp -> toDB ( $name , true ) . " ',' " . $tp -> toDB ( $text , true ) . " ',' " . intval ( $type ) . " ',' " . $tp -> toDB ( $parms , true ) . " ',' " . $tp -> toDB ( $values , true ) . " ', ' " . $tp -> toDB ( $default , true ) . " ', ' " . intval ( $read ) . " ', ' " . intval ( $write ) . " ', ' " . intval ( $required ) . " ', '0', ' " . intval ( $applicable ) . " ', ' " . intval ( $order ) . " ', ' " . intval ( $parent ) . " ' " );
2008-01-15 21:57:53 +00:00
if ( $this -> user_extended_field_exist ( $name ))
{
return TRUE ;
}
}
return FALSE ;
2006-12-02 04:36:16 +00:00
}
2008-01-15 21:57:53 +00:00
2006-12-02 04:36:16 +00:00
function user_extended_modify ( $id , $name , $text , $type , $parms , $values , $default , $required , $read , $write , $applicable , $parent )
{
global $sql , $tp ;
if ( $this -> user_extended_field_exist ( $name ))
{
$field_info = $this -> user_extended_type_text ( $type , $default );
$sql -> db_Select_gen ( " ALTER TABLE #user_extended MODIFY user_ " . $tp -> toDB ( $name , true ) . " " . $field_info );
$newfield_info = "
user_extended_struct_text = '".$tp -> toDB($text, true)."' ,
user_extended_struct_type = '".intval($type)."' ,
user_extended_struct_parms = '".$tp -> toDB($parms, true)."' ,
user_extended_struct_values = '".$tp -> toDB($values, true)."' ,
user_extended_struct_default = '".$tp -> toDB($default, true)."' ,
user_extended_struct_required = '".intval($required)."' ,
user_extended_struct_read = '".intval($read)."' ,
user_extended_struct_write = '".intval($write)."' ,
user_extended_struct_applicable = '".intval($applicable)."' ,
user_extended_struct_parent = '".intval($parent)."'
WHERE user_extended_struct_id = '".intval($id)."'
" ;
return $sql -> db_Update ( " user_extended_struct " , $newfield_info );
}
}
function user_extended_remove ( $id , $name )
{
global $sql , $tp ;
2008-11-30 23:15:15 +00:00
$this -> clear_cache ();
2006-12-02 04:36:16 +00:00
if ( $this -> user_extended_field_exist ( $name ))
{
$sql -> db_Select_gen ( " ALTER TABLE #user_extended DROP user_ " . $tp -> toDB ( $name , true ));
if ( is_numeric ( $id ))
{
$sql -> db_Delete ( " user_extended_struct " , " user_extended_struct_id = ' " . intval ( $id ) . " ' " );
}
else
{
$sql -> db_Delete ( " user_extended_struct " , " user_extended_struct_name = ' " . $tp -> toDB ( $id , true ) . " ' " );
}
return ! ( $this -> user_extended_field_exist ( $name ));
}
}
function user_extended_hide ( $struct , $curval )
{
$chk = ( $curval ) ? " checked='checked' " : " " ;
$name = " hide[user_ " . $struct [ 'user_extended_struct_name' ] . " ] " ;
return " <input type='checkbox' { $chk } value='1' name=' { $name } ' /> " . UE_LAN_HIDE ;
}
2008-01-15 21:57:53 +00:00
function user_extended_edit ( $struct , $curval )
{
global $cal , $tp ;
if ( trim ( $curval ) == " " && $struct [ 'user_extended_struct_default' ] != " " )
{
$curval = $struct [ 'user_extended_struct_default' ];
}
$choices = explode ( " , " , $struct [ 'user_extended_struct_values' ]);
foreach ( $choices as $k => $v )
{
$choices [ $k ] = str_replace ( " [E_COMMA] " , " , " , $choices [ $k ]);
}
$parms = explode ( " ^,^ " , $struct [ 'user_extended_struct_parms' ]);
$include = preg_replace ( " / \n / " , " " , $tp -> toHtml ( $parms [ 0 ]));
$regex = $tp -> toText ( $parms [ 1 ]);
$regexfail = $tp -> toText ( $parms [ 2 ]);
$fname = " ue[user_ " . $struct [ 'user_extended_struct_name' ] . " ] " ;
2008-12-10 13:14:51 +00:00
if ( strpos ( $include , 'class' ) === FALSE )
2008-01-15 21:57:53 +00:00
{
$include .= " class='tbox' " ;
}
2006-12-02 04:36:16 +00:00
2008-01-15 21:57:53 +00:00
switch ( $struct [ 'user_extended_struct_type' ])
{
case EUF_TEXT : //textbox
case EUF_INTEGER : //integer
$ret = " <input name=' { $fname } ' value=' { $curval } ' { $include } /> " ;
return $ret ;
break ;
case EUF_RADIO : //radio
foreach ( $choices as $choice )
{
$choice = trim ( $choice );
$chk = ( $curval == $choice ) ? " checked='checked' " : " " ;
$ret .= " <input { $include } type='radio' name=' { $fname } ' value=' { $choice } ' { $chk } /> { $choice } " ;
}
return $ret ;
break ;
2009-08-06 20:29:58 +00:00
case EUF_CHECKBOX : //checkboxes
foreach ( $choices as $choice )
{
$choice = trim ( $choice );
if ( strpos ( $choice , " | " ) !== FALSE )
{
list ( $val , $label ) = explode ( " | " , $choice );
}
else
{
$val = $choice ;
$label = $choice ;
}
$chk = ( $curval == $val ) ? " checked='checked' " : " " ;
$ret .= " <input { $include } type='checkbox' name=' { $fname } []' value=' { $val } ' { $chk } /> { $label } <br /> " ;
}
return $ret ;
break ;
2008-01-15 21:57:53 +00:00
case EUF_DROPDOWN : //dropdown
$ret = " <select { $include } name=' { $fname } '> \n " ;
$ret .= " <option value=''> </option> \n " ; // ensures that the user chose it.
foreach ( $choices as $choice )
{
$choice = trim ( $choice );
$sel = ( $curval == $choice ) ? " selected='selected' " : " " ;
$ret .= " <option value=' { $choice } ' { $sel } > { $choice } </option> \n " ;
}
$ret .= " </select> \n " ;
return $ret ;
break ;
case EUF_PREDEFINED : // predefined list, shown in dropdown
$filename = e_ADMIN . 'sql/extended_' . trim ( $struct [ 'user_extended_struct_values' ]) . '.php' ;
if ( ! is_readable ( $filename )) return 'No file: ' . $filename ;
require ( $filename );
$list_name = $struct [ 'user_extended_struct_values' ] . '_list' ;
$display_func = $struct [ 'user_extended_struct_values' ] . '_value' ;
if ( ! function_exists ( $display_func )) $display_func = '' ;
$source_data = $$list_name ;
$ret = " <select { $include } name=' { $fname } '> \n " ;
$ret .= " <option value=''> </option> \n " ; // ensures that the user chose it.
foreach ( $source_data as $v )
{
$val = $v [ 0 ];
$choice = trim ( $v [ 1 ]);
if ( $display_func ) $choice = $display_func ( $val , $choice );
$sel = ( $curval == $val ) ? " selected='selected' " : " " ;
$ret .= " <option value=' { $val } ' { $sel } > { $choice } </option> \n " ;
}
$ret .= " </select> \n " ;
return $ret ;
break ;
case EUF_DB_FIELD : //db_field
2006-12-02 04:36:16 +00:00
global $sql ;
$order = ( $choices [ 3 ]) ? " ORDER BY " . $tp -> toDB ( $choices [ 3 ], true ) : " " ;
if ( $sql -> db_Select ( $tp -> toDB ( $choices [ 0 ], true ), $tp -> toDB ( $choices [ 1 ], true ) . " , " . $tp -> toDB ( $choices [ 2 ], true ), " 1 $order " )){
$choiceList = $sql -> db_getList ( 'ALL' , FALSE );
$ret = " <select { $include } name=' { $fname } ' > \n " ;
$ret .= " <option value=''> </option> \n " ; // ensures that the user chose it.
foreach ( $choiceList as $cArray )
{
$cID = trim ( $cArray [ $choices [ 1 ]]);
$cText = trim ( $cArray [ $choices [ 2 ]]);
$sel = ( $curval == $cID ) ? " selected='selected' " : " " ;
$ret .= " <option value=' { $cID } ' { $sel } > { $cText } </option> \n " ;
}
$ret .= " </select> \n " ;
return $ret ;
} else {
return " " ;
}
break ;
2008-01-15 21:57:53 +00:00
case EUF_TEXTAREA : //textarea
2006-12-02 04:36:16 +00:00
return " <textarea { $include } name=' { $fname } ' > { $curval } </textarea> " ;
break ;
2008-01-15 21:57:53 +00:00
case EUF_DATE : //date
2006-12-02 04:36:16 +00:00
return $cal -> make_input_field (
array (
'ifFormat' => '%Y-%m-%d'
),
array (
'class' => 'tbox' ,
'name' => $fname ,
'value' => $curval
)
);
break ;
2008-01-15 21:57:53 +00:00
case EUF_LANGUAGE : // language
2006-12-02 04:36:16 +00:00
require_once ( e_HANDLER . " file_class.php " );
$fl = new e_file ;
$lanlist = $fl -> get_dirs ( e_LANGUAGEDIR );
sort ( $lanlist );
$ret = " <select { $include } name=' { $fname } '> \n " ;
$ret .= " <option value=''> </option> \n " ; // ensures that the user chose it.
foreach ( $lanlist as $choice )
{
$choice = trim ( $choice );
$sel = ( $curval == $choice || ( ! USER && $choice == e_LANGUAGE )) ? " selected='selected' " : " " ;
$ret .= " <option value=' { $choice } ' { $sel } > { $choice } </option> \n " ;
}
$ret .= " </select> \n " ;
break ;
}
return $ret ;
}
function user_extended_getStruct ( $orderby = " user_extended_struct_order " )
{
if ( $ueStruct = getcachedvars ( 'ue_struct' ))
{
return $ueStruct ;
}
2008-05-25 15:32:06 +00:00
global $tp ;
2006-12-02 04:36:16 +00:00
$ret = array ();
$parms = " " ;
if ( $orderby != " " )
{
$parms = " 1 ORDER BY " . $tp -> toDB ( $orderby , true );
}
2008-05-25 15:32:06 +00:00
$sql_ue = new db ; // Use our own db to avoid interference with other objects
if ( $sql_ue -> db_Select ( 'user_extended_struct' , '*' , $parms ))
2006-12-02 04:36:16 +00:00
{
2008-05-25 15:32:06 +00:00
while ( $row = $sql_ue -> db_Fetch ())
2006-12-02 04:36:16 +00:00
{
$ret [ 'user_' . $row [ 'user_extended_struct_name' ]] = $row ;
}
}
cachevars ( 'ue_struct' , $ret );
return $ret ;
}
2008-01-15 21:57:53 +00:00
2006-12-02 04:36:16 +00:00
function parse_extended_xml ( $contents , $no_cache = FALSE )
{
if ( $no_cache == FALSE && $this -> extended_xml )
{
return $this -> extended_xml ;
}
require_once ( e_HANDLER . " xml_class.php " );
2008-01-20 04:46:35 +00:00
$xml = new xmlClass ;
$data = $xml -> loadXMLfile ( e_FILE . " cache/user_extended.xml " , true );
$ret [ 'version' ] = $data [ '@attributes' ][ 'version' ];
2006-12-02 04:36:16 +00:00
unset ( $info );
2008-01-20 04:46:35 +00:00
foreach ( $data [ 'item' ] as $item )
2006-12-02 04:36:16 +00:00
{
2008-01-20 04:46:35 +00:00
if ( is_array ( $item [ 'include_text' ]) && ! count ( $item [ 'include_text' ]))
{
$item [ 'include_text' ] = '' ;
}
2006-12-02 04:36:16 +00:00
$info = array (
2009-01-11 21:06:52 +00:00
" name " => $item [ '@attributes' ][ 'name' ],
" text " => " UE_LAN_ " . strtoupper ( $item [ '@attributes' ][ 'name' ]),
" type " => $item [ 'type' ],
" values " => $item [ 'values' ],
" default " => $item [ 'default' ],
" required " => $item [ 'required' ],
" read " => $item [ 'read' ],
" write " => $item [ 'write' ],
" applicable " => $item [ 'applicable' ],
" include_text " => $item [ 'include_text' ],
" parms " => $item [ 'include_text' ],
" regex " => $item [ 'regex' ]
);
2008-01-20 04:46:35 +00:00
if ( is_array ( $item [ 'default' ]) && $item [ 'default' ] == '' )
2006-12-02 04:36:16 +00:00
{
$info [ 'default' ] = 0 ;
}
2008-01-20 04:46:35 +00:00
if ( $item [ 'regex' ])
2006-12-02 04:36:16 +00:00
{
2008-01-20 04:46:35 +00:00
$info [ 'parms' ] .= $item [ 'include_text' ] . " ^,^ " . $item [ 'regex' ] . " ^,^LAN_UE_FAIL_ " . strtoupper ( $item [ '@attributes' ][ 'name' ]);
2006-12-02 04:36:16 +00:00
}
2008-01-20 04:46:35 +00:00
$ret [ $item [ '@attributes' ][ 'name' ]] = $info ;
2006-12-02 04:36:16 +00:00
}
$this -> extended_xml = $ret ;
return $this -> extended_xml ;
}
2008-12-10 13:14:51 +00:00
2007-01-16 01:43:23 +00:00
/**
* Set the value of an extended field
*
* $ue = new e107_user_extended ;
2008-12-18 15:28:59 +00:00
* $result = $ue -> user_extended_setvalue ( 1 , 'location' , 'Pittsburgh' );
2008-12-10 13:14:51 +00:00
*
*
2007-01-16 01:43:23 +00:00
*/
2008-12-18 15:28:59 +00:00
function user_extended_setvalue ( $uid , $field_name , $newvalue , $fieldType = 'todb' )
2007-01-16 01:43:23 +00:00
{
2008-12-18 15:28:59 +00:00
$e107 = e107 :: getInstance ();
$uid = ( int ) $uid ;
switch ( $fieldType )
{
case 'int' :
$newvalue = ( int ) $newvalue ;
break ;
case 'escape' :
$newvalue = " ' " . mysql_real_escape_string ( $newvalue ) . " ' " ;
break ;
default :
$newvalue = " ' " . $e107 -> tp -> toDB ( $newvalue ) . " ' " ;
break ;
}
2007-01-16 01:43:23 +00:00
if ( substr ( $field_name , 0 , 5 ) != 'user_' )
{
$field_name = 'user_' . $field_name ;
}
2008-12-18 15:28:59 +00:00
$qry = "
INSERT INTO `#user_extended` ( user_extended_id , { $field_name })
VALUES ({ $uid }, { $newvalue })
ON DUPLICATE KEY UPDATE { $field_name } = { $newvalue }
" ;
return $e107 -> sql -> db_Select_gen ( $qry );
2007-01-16 01:43:23 +00:00
}
/**
* Retrieve the value of an extended field
*
* $ue = new e107_user_extended ;
* $value = $ue -> user_extended_getvalue ( 2 , 'location' );
2008-12-10 13:14:51 +00:00
*
2007-01-16 01:43:23 +00:00
*/
function user_extended_getvalue ( $uid , $field_name , $ifnotset = false )
{
$uid = intval ( $uid );
if ( substr ( $field_name , 0 , 5 ) != 'user_' )
{
$field_name = 'user_' . $field_name ;
}
$uinfo = get_user_data ( $uid );
2008-01-15 21:57:53 +00:00
if ( ! isset ( $uinfo [ $field_name ])) return $ifnotset ;
return $uinfo [ $field_name ];
2007-01-16 01:43:23 +00:00
}
2008-01-15 21:57:53 +00:00
// Given a predefined list field, returns the display text corresponding to the passed value
function user_extended_display_text ( $table , $value )
{
$filename = e_ADMIN . 'sql/extended_' . $table . '.php' ;
if ( ! is_readable ( $filename )) return 'No file: ' . $filename ;
require_once ( $filename );
$list_name = $table . '_list' ;
$display_func = $table . '_value' ;
if ( ! function_exists ( $display_func )) $display_func = '' ;
$source_data = $$list_name ;
foreach ( $source_data as $v )
{
if ( $value == $v [ 0 ])
{
if ( $display_func ) return $display_func ( $v [ 0 ], $v [ 1 ]);
return $v [ 1 ];
}
}
return '????' ;
}
2006-12-02 04:36:16 +00:00
}
2007-03-04 13:53:37 +00:00
?>