How to use minihaku (4)

Date 2006-09-03 06:23:15 | Category: XOOPS

in englishin japanese
How to write the logic.
There are four part in include/config.php

(A) Prefereces Part
There are three parameters in the current version.
read (1) and (2).

// preferences
$auto_belong_groups = array( XOOPS_GROUP_USERS ) ; // default (2)
$allow_blank_email = false ;
$allow_blank_vpass = false ;


(B) Extra Field Definition Part

$extra_fields = array(
	'sex' => array(
		'initval' => -1 ,
		'options' => array( 0 => 'male' , 1 => 'female' ) ,
		) ,
	'birth' => array(
		'initval' => '1950-01-01' ,
		) ,
	) ;

define extra fields as an array "$extra_fields".
The key of the array should be field name of users table.
(In this sample, it should be "sex" and "birth")
All values of the array are used only in this file.

(C) Initialization and Query Part

if( empty( $minihaku_uid4whr ) ) {
	// for registering
	foreach( $extra_fields as $key => $attribs ) {
		$allowed_requests[$key] = $attribs['initval'] ;
	}
} else {
	// for editing or minihaku's smarty plugin
	// a query for getting values in extra fields
	$db =& Database::getInstance() ;
	list( $allowed_requests['sex'] , $allowed_requests['birth'] ) = $db->fetchRow( $db->query( "SELECT sex,birth FROM ".$db->prefix("users")." WHERE uid=$minihaku_uid4whr" ) ) ;

	// It is important to filter intval() here
	$allowed_requests['sex'] = intval( $allowed_requests['sex'] ) ;

	// prepare string for display in minihaku's smarty plugin
	$fields4html['sex'] = $extra_fields['sex']['options'][ $allowed_requests['sex'] ] ;
	$fields4html['birth'] = str_replace( '-' , '/' , $allowed_requests['birth'] ) ;
}

The array "$allowed_requests" stores fields can be overwritten by $_POST.
Any field is not in $allowed_requests won't be changed.
(On registering user, DEFAULT in the field of DB will be effected.)

Keys of the array are field names.
Values of the array are current/initial values.
And type of values are the field's types. (Important!)

In this sample, "sex" is type of integer.
Thus, "sex" is convert into integer just after getting from DB.

The array "$fields4html" will be used in minihaku_userinfo as a smarty's plugin.

(D) Valid Check Part

if( isset( $_POST['sex'] ) ) {
	if( $_POST['sex'] < 0 || $_POST['sex'] > 1 ) {
		die( "invalid sex value" ) ;
	}
}
if( ! empty( $_POST['Date_Year'] ) ) {
	$_POST['birth'] = intval( $_POST['Date_Year'] ) . '-' . intval( $_POST['Date_Month'] ) . '-' . intval( $_POST['Date_Day'] ) ;
}

Do valid checks.
Since "sex" should be 0 or 1, exit if the other value is requested.
You can also modify requests here.
$_POST['birth'] is built from three requests.




You can read more news at PEAK XOOPS.
http://xoops.peak.ne.jp

The URL for this story is:
http://xoops.peak.ne.jp/md/news/index.php?page=article&storyid=357