2005-07-21 04:02:32 +00:00
< ? php
2005-08-21 06:49:02 +00:00
/* SVN FILE: $Id$ */
2005-07-21 04:02:32 +00:00
/**
2005-09-07 01:52:45 +00:00
* Automatic generation of HTML FORMs from given data .
2005-12-23 21:57:26 +00:00
*
2005-09-07 01:52:45 +00:00
* Used for scaffolding .
2005-08-21 06:49:02 +00:00
*
* PHP versions 4 and 5
*
2007-02-02 10:39:45 +00:00
* CakePHP ( tm ) : Rapid Development Framework < http :// www . cakephp . org />
* Copyright 2005 - 2007 , Cake Software Foundation , Inc .
2006-05-26 05:29:17 +00:00
* 1785 E . Sahara Avenue , Suite 490 - 204
* Las Vegas , Nevada 89104
2005-12-27 03:33:44 +00:00
*
2005-12-23 21:57:26 +00:00
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice .
2005-08-21 06:49:02 +00:00
*
2005-12-23 21:57:26 +00:00
* @ filesource
2007-02-02 10:39:45 +00:00
* @ copyright Copyright 2005 - 2007 , Cake Software Foundation , Inc .
* @ link http :// www . cakefoundation . org / projects / info / cakephp CakePHP ( tm ) Project
2006-05-26 05:29:17 +00:00
* @ package cake
* @ subpackage cake . cake . libs . view . helpers
2007-02-02 10:39:45 +00:00
* @ since CakePHP ( tm ) v 0.10 . 0.1076
2006-05-26 05:29:17 +00:00
* @ version $Revision $
* @ modifiedby $LastChangedBy $
* @ lastmodified $Date $
* @ license http :// www . opensource . org / licenses / mit - license . php The MIT License
2005-07-21 04:02:32 +00:00
*/
/**
* Form helper library .
2005-12-23 21:57:26 +00:00
*
2005-09-07 01:52:45 +00:00
* Automatic generation of HTML FORMs from given data .
2005-07-21 04:02:32 +00:00
*
2006-05-26 05:29:17 +00:00
* @ package cake
* @ subpackage cake . cake . libs . view . helpers
2005-07-21 04:02:32 +00:00
*/
2006-10-27 21:40:34 +00:00
class FormHelper extends AppHelper {
2007-05-02 08:07:19 +00:00
/**
2007-10-09 20:44:28 +00:00
* Other helpers used by FormHelper
2007-05-02 08:07:19 +00:00
*
2007-10-14 23:41:05 +00:00
* @ var array
* @ access public
2007-05-02 08:07:19 +00:00
*/
2006-05-26 05:29:17 +00:00
var $helpers = array ( 'Html' );
2007-05-02 08:07:19 +00:00
/**
2007-10-09 20:44:28 +00:00
* Holds the fields array ( 'field_name' => 'type' ), sizes array ( 'field_name' => 'size' ),
2007-05-02 08:07:19 +00:00
* primaryKey and validates array ( 'field_name' )
*
* @ access public
*/
2007-10-09 20:44:28 +00:00
var $fieldset = array ( 'fields' => array (), 'sizes' => array (), 'key' => 'id' , 'validates' => array ());
2007-05-02 08:07:19 +00:00
/**
* Enter description here ...
*
2007-10-14 23:41:05 +00:00
* @ var array
2007-05-02 08:07:19 +00:00
*/
2007-09-16 18:32:02 +00:00
var $__options = array (
'day' => array (), 'minute' => array (), 'hour' => array (),
'month' => array (), 'year' => array (), 'meridian' => array ()
);
2007-05-02 08:07:19 +00:00
/**
* Enter description here ...
*
2007-10-09 20:44:28 +00:00
* @ var array
2007-10-14 23:41:05 +00:00
* @ access public
2007-05-02 08:07:19 +00:00
*/
2007-05-01 08:56:02 +00:00
var $fields = array ();
2007-10-09 20:44:28 +00:00
/**
* Defines the type of form being created . Set by FormHelper :: create () .
*
* @ var string
2007-10-14 23:41:05 +00:00
* @ access public
2007-10-09 20:44:28 +00:00
*/
var $requestType = null ;
2006-08-04 08:03:39 +00:00
/**
* Returns an HTML FORM element .
*
2006-11-29 07:58:35 +00:00
* @ access public
2006-11-26 20:43:54 +00:00
* @ param string $model The model object which the form is being defined for
2007-05-24 23:14:14 +00:00
* @ param array $options
2006-08-04 08:03:39 +00:00
* @ return string An formatted opening FORM tag .
*/
2006-11-26 20:43:54 +00:00
function create ( $model = null , $options = array ()) {
2007-03-16 04:47:10 +00:00
$defaultModel = null ;
2007-06-08 01:17:40 +00:00
$data = $this -> fieldset ;
2007-04-01 04:55:56 +00:00
$view =& ClassRegistry :: getObject ( 'view' );
2007-03-16 04:47:10 +00:00
2006-12-22 05:23:07 +00:00
if ( is_array ( $model ) && empty ( $options )) {
$options = $model ;
2007-01-22 08:32:00 +00:00
$model = null ;
2006-12-22 05:23:07 +00:00
}
2007-01-16 07:07:49 +00:00
2007-03-16 04:47:10 +00:00
if ( empty ( $model ) && $model !== false && ! empty ( $this -> params [ 'models' ])) {
2006-12-22 05:23:07 +00:00
$model = $this -> params [ 'models' ][ 0 ];
2007-03-16 04:47:10 +00:00
$defaultModel = $this -> params [ 'models' ][ 0 ];
} elseif ( empty ( $model ) && empty ( $this -> params [ 'models' ])) {
$model = false ;
2007-06-20 06:07:11 +00:00
} elseif ( is_string ( $model ) && ( strpos ( $model , '/' ) !== false || strpos ( $model , '.' ) !== false )) {
2007-03-16 04:47:10 +00:00
$path = preg_split ( '/\/|\./' , $model );
$model = $path [ count ( $path ) - 1 ];
2006-11-26 20:43:54 +00:00
}
2007-01-16 07:07:49 +00:00
2006-11-26 20:43:54 +00:00
if ( ClassRegistry :: isKeySet ( $model )) {
$object =& ClassRegistry :: getObject ( $model );
2007-04-27 20:16:38 +00:00
}
2007-04-17 02:06:15 +00:00
2007-04-30 09:42:39 +00:00
$models = ClassRegistry :: keys ();
2007-06-20 06:07:11 +00:00
foreach ( $models as $currentModel ) {
2007-04-27 20:16:38 +00:00
if ( ClassRegistry :: isKeySet ( $currentModel )) {
$currentObject =& ClassRegistry :: getObject ( $currentModel );
2007-06-20 06:07:11 +00:00
if ( is_a ( $currentObject , 'Model' ) && ! empty ( $currentObject -> validationErrors )) {
2007-04-27 20:16:38 +00:00
$this -> validationErrors [ Inflector :: camelize ( $currentModel )] =& $currentObject -> validationErrors ;
2007-04-06 19:05:06 +00:00
}
}
2006-11-26 20:43:54 +00:00
}
2007-03-17 07:58:22 +00:00
$this -> setFormTag ( $model . '.' );
2006-11-26 20:43:54 +00:00
$append = '' ;
2007-03-16 23:54:24 +00:00
$created = $id = false ;
2007-01-03 17:36:14 +00:00
2007-06-20 06:07:11 +00:00
if ( isset ( $object )) {
2007-01-01 19:39:07 +00:00
$fields = $object -> loadInfo ();
2007-05-05 17:36:30 +00:00
$fieldNames = $fields -> extract ( '{n}.name' );
$fieldTypes = $fields -> extract ( '{n}.type' );
$fieldLengths = $fields -> extract ( '{n}.length' );
if ( ! count ( $fieldNames ) || ! count ( $fieldTypes )) {
2007-05-24 23:14:14 +00:00
trigger_error ( __ ( '(FormHelper::create) Unable to use model field data. If you are using a model without a database table, try implementing loadInfo()' , true ), E_USER_WARNING );
2007-05-05 17:36:30 +00:00
}
if ( ! count ( $fieldNames ) || ! count ( $fieldLengths ) || ( count ( $fieldNames ) != count ( $fieldTypes ))) {
2007-05-24 23:14:14 +00:00
trigger_error ( __ ( '(FormHelper::create) Unable to use model field data. If you are using a model without a database table, try implementing loadInfo()' , true ), E_USER_WARNING );
2007-05-05 17:36:30 +00:00
}
2007-01-01 19:39:07 +00:00
$data = array (
2007-05-05 17:36:30 +00:00
'fields' => array_combine ( $fieldNames , $fieldTypes ),
'sizes' => array_combine ( $fieldNames , $fieldLengths ),
2007-01-01 19:39:07 +00:00
'key' => $object -> primaryKey ,
2007-01-31 22:43:32 +00:00
'validates' => ( ife ( empty ( $object -> validate ), array (), array_keys ( $object -> validate )))
2007-01-01 19:39:07 +00:00
);
2007-06-08 01:17:40 +00:00
$habtm = array ();
2007-06-20 06:07:11 +00:00
if ( ! empty ( $object -> hasAndBelongsToMany )) {
2007-06-08 01:17:40 +00:00
$habtm = array_combine ( array_keys ( $object -> hasAndBelongsToMany ), array_keys ( $object -> hasAndBelongsToMany ));
}
2007-10-22 23:41:43 +00:00
$data [ 'fields' ] = am ( $data [ 'fields' ], $habtm );
2007-04-04 20:38:26 +00:00
$this -> fieldset = $data ;
2007-01-01 19:39:07 +00:00
}
2006-11-30 18:07:08 +00:00
2006-11-27 19:56:06 +00:00
if ( isset ( $this -> data [ $model ]) && isset ( $this -> data [ $model ][ $data [ 'key' ]]) && ! empty ( $this -> data [ $model ][ $data [ 'key' ]])) {
2006-11-26 20:43:54 +00:00
$created = true ;
2007-03-16 23:54:24 +00:00
$id = $this -> data [ $model ][ $data [ 'key' ]];
2006-11-26 20:43:54 +00:00
}
2006-12-21 06:10:35 +00:00
$options = am ( array (
'type' => ( $created && empty ( $options [ 'action' ])) ? 'put' : 'post' ,
2007-03-16 04:47:10 +00:00
'action' => null ,
'url' => null ,
2006-12-21 06:10:35 +00:00
'default' => true ),
$options );
2006-11-26 20:43:54 +00:00
2007-03-16 04:47:10 +00:00
if ( empty ( $options [ 'url' ]) || is_array ( $options [ 'url' ])) {
2006-12-01 19:04:16 +00:00
$options = ( array ) $options ;
2007-10-23 02:36:07 +00:00
if ( ! empty ( $model ) && $model != $defaultModel ) {
2007-03-16 04:47:10 +00:00
$controller = Inflector :: underscore ( Inflector :: pluralize ( $model ));
} else {
$controller = Inflector :: underscore ( $this -> params [ 'controller' ]);
}
if ( empty ( $options [ 'action' ])) {
$options [ 'action' ] = ife ( $created , 'edit' , 'add' );
}
2006-11-26 20:43:54 +00:00
$actionDefaults = array (
2007-05-21 04:27:42 +00:00
'plugin' => $this -> plugin ,
2007-03-16 04:47:10 +00:00
'controller' => $controller ,
'action' => $options [ 'action' ],
2007-03-16 23:54:24 +00:00
'id' => $id
2006-11-26 20:43:54 +00:00
);
2007-06-20 06:07:11 +00:00
if ( ! empty ( $options [ 'action' ]) && ! isset ( $options [ 'id' ])) {
2007-04-17 02:06:15 +00:00
$options [ 'id' ] = $model . Inflector :: camelize ( $options [ 'action' ]) . 'Form' ;
}
2007-03-16 04:47:10 +00:00
$options [ 'action' ] = am ( $actionDefaults , ( array ) $options [ 'url' ]);
2007-03-16 23:54:24 +00:00
} elseif ( is_string ( $options [ 'url' ])) {
$options [ 'action' ] = $options [ 'url' ];
2006-11-26 20:43:54 +00:00
}
2007-03-16 23:54:24 +00:00
unset ( $options [ 'url' ]);
2006-11-26 20:43:54 +00:00
switch ( low ( $options [ 'type' ])) {
case 'get' :
2006-12-01 19:04:16 +00:00
$htmlAttributes [ 'method' ] = 'get' ;
2006-11-26 20:43:54 +00:00
break ;
2006-12-01 19:04:16 +00:00
case 'file' :
$htmlAttributes [ 'enctype' ] = 'multipart/form-data' ;
2007-01-31 22:43:32 +00:00
$options [ 'type' ] = ife ( $created , 'put' , 'post' );
2006-12-01 19:04:16 +00:00
case 'post' :
2006-11-26 20:43:54 +00:00
case 'put' :
case 'delete' :
2007-03-16 23:54:24 +00:00
//$append .= $this->hidden('_method', array('name' => '_method', 'value' => up($options['type']), 'id' => $options['id'] . 'Method'));
2006-11-26 20:43:54 +00:00
default :
2006-12-01 19:04:16 +00:00
$htmlAttributes [ 'method' ] = 'post' ;
2006-11-26 20:43:54 +00:00
break ;
2006-08-04 08:03:39 +00:00
}
2007-10-09 20:44:28 +00:00
$this -> requestType = low ( $options [ 'type' ]);
2006-12-21 04:46:52 +00:00
2006-11-26 20:43:54 +00:00
$htmlAttributes [ 'action' ] = $this -> url ( $options [ 'action' ]);
unset ( $options [ 'type' ], $options [ 'action' ]);
2006-12-21 06:10:35 +00:00
if ( $options [ 'default' ] == false ) {
if ( isset ( $htmlAttributes [ 'onSubmit' ])) {
2007-10-19 07:46:19 +00:00
$htmlAttributes [ 'onSubmit' ] .= ' event.returnValue = false; return false;' ;
2006-12-21 06:10:35 +00:00
} else {
2007-10-19 07:46:19 +00:00
$htmlAttributes [ 'onSubmit' ] = 'event.returnValue = false; return false;' ;
2006-12-21 06:10:35 +00:00
}
}
unset ( $options [ 'default' ]);
2006-11-26 20:43:54 +00:00
$htmlAttributes = am ( $options , $htmlAttributes );
2006-08-04 08:03:39 +00:00
2007-05-01 15:17:27 +00:00
if ( isset ( $this -> params [ '_Token' ]) && ! empty ( $this -> params [ '_Token' ])) {
2007-10-15 07:47:13 +00:00
$append .= '<p style="display: none;">' ;
2007-10-04 19:43:32 +00:00
$append .= $this -> hidden ( '_Token/key' , array ( 'value' => $this -> params [ '_Token' ][ 'key' ], 'id' => 'Token' . mt_rand ()));
2007-05-01 15:17:27 +00:00
$append .= '</p>' ;
}
2007-05-02 08:07:19 +00:00
return $this -> output ( sprintf ( $this -> Html -> tags [ 'form' ], $this -> Html -> _parseAttributes ( $htmlAttributes , null , '' ))) . $append ;
2006-11-26 20:43:54 +00:00
}
/**
2007-10-09 20:44:28 +00:00
* Closes an HTML form , cleans up values set by FormHelper :: create (), and writes hidden
* input fields where appropriate .
2006-11-26 20:43:54 +00:00
*
* @ return string A closing FORM tag .
2007-10-09 20:44:28 +00:00
* @ access public
2006-11-26 20:43:54 +00:00
*/
2007-05-01 15:17:27 +00:00
function end ( $options = null ) {
if ( ! empty ( $this -> params [ 'models' ])) {
$models = $this -> params [ 'models' ][ 0 ];
}
2007-04-17 02:06:15 +00:00
2007-05-01 15:17:27 +00:00
$submitOptions = true ;
2007-06-20 06:07:11 +00:00
if ( ! is_array ( $options )) {
2007-04-17 02:06:15 +00:00
$submitOptions = $options ;
2007-06-20 06:07:11 +00:00
} elseif ( isset ( $options [ 'submit' ])) {
2007-04-17 02:06:15 +00:00
$submitOptions = $options [ 'submit' ];
unset ( $options [ 'submit' ]);
2007-06-20 06:07:11 +00:00
if ( isset ( $options [ 'label' ])) {
2007-10-22 17:30:14 +00:00
$submit = $options [ 'label' ];
2007-05-02 12:56:51 +00:00
unset ( $options [ 'label' ]);
2007-04-17 02:06:15 +00:00
}
}
2007-04-17 06:48:16 +00:00
2007-06-20 06:07:11 +00:00
if ( $submitOptions === true ) {
2007-10-20 07:12:54 +00:00
$submit = __ ( 'Submit' , true );
2007-06-20 06:07:11 +00:00
} elseif ( is_string ( $submitOptions )) {
2007-04-17 02:06:15 +00:00
$submit = $submitOptions ;
}
2007-06-20 06:07:11 +00:00
if ( ! is_array ( $submitOptions )) {
2007-04-17 02:06:15 +00:00
$submitOptions = array ();
}
2007-05-01 13:35:33 +00:00
$out = null ;
2007-04-17 02:06:15 +00:00
2007-06-20 06:07:11 +00:00
if ( isset ( $submit )) {
2007-04-17 02:06:15 +00:00
$out .= $this -> submit ( $submit , $submitOptions );
2007-10-06 04:37:58 +00:00
} elseif ( isset ( $this -> params [ '_Token' ]) && ! empty ( $this -> params [ '_Token' ])) {
2007-06-06 18:54:45 +00:00
$out .= $this -> secure ( $this -> fields );
$this -> fields = array ();
2007-04-17 02:06:15 +00:00
}
2007-06-08 01:17:40 +00:00
$this -> setFormTag ( null );
2007-05-01 15:17:27 +00:00
$out .= $this -> Html -> tags [ 'formend' ];
return $this -> output ( $out );
2006-08-04 08:03:39 +00:00
}
2007-10-09 20:44:28 +00:00
/**
* Generates a hidden field with a security hash based on the fields used in the form .
*
* @ param array $fields The list of fields to use when generating the hash
* @ return string A hidden input field with a security hash
* @ access public
*/
2007-05-01 08:56:02 +00:00
function secure ( $fields ) {
2007-10-06 04:37:58 +00:00
if ( ! empty ( $fields )) {
2007-10-15 07:47:13 +00:00
$append = '<p style="display: none;">' ;
2007-10-06 04:37:58 +00:00
foreach ( $fields as $key => $value ) {
2007-10-15 07:47:13 +00:00
if ( strpos ( $key , '_' ) !== 0 ) {
2007-10-06 04:37:58 +00:00
sort ( $fields [ $key ]);
}
2007-10-04 18:27:35 +00:00
}
2007-10-06 04:37:58 +00:00
ksort ( $fields );
2007-10-16 09:05:25 +00:00
$append .= $this -> hidden ( '_Token.fields' , array ( 'value' => urlencode ( Security :: hash ( serialize ( $fields ) . Configure :: read ( 'Security.salt' ))), 'id' => 'TokenFields' . mt_rand ()));
2007-10-06 04:37:58 +00:00
$append .= '</p>' ;
return $append ;
2007-10-04 18:27:35 +00:00
}
2007-10-06 04:37:58 +00:00
return null ;
2007-05-01 08:56:02 +00:00
}
2007-10-09 20:44:28 +00:00
/**
* Generates a field list
*
* @ param string $model Model name
* @ param array $options Options
* @ access private
*/
2007-05-02 02:31:06 +00:00
function __secure ( $model = null , $options = null ) {
2007-06-20 06:07:11 +00:00
if ( ! $model ) {
2007-05-02 02:31:06 +00:00
$model = $this -> model ();
}
2007-06-20 06:07:11 +00:00
if ( isset ( $this -> params [ '_Token' ]) && ! empty ( $this -> params [ '_Token' ])) {
if ( ! empty ( $this -> params [ '_Token' ][ 'disabledFields' ])) {
2007-05-02 02:31:06 +00:00
foreach ( $this -> params [ '_Token' ][ 'disabledFields' ] as $value ) {
$parts = preg_split ( '/\/|\./' , $value );
if ( count ( $parts ) == 1 ) {
2007-06-20 06:07:11 +00:00
if ( $parts [ 0 ] === $this -> field ()) {
2007-05-02 02:31:06 +00:00
return ;
}
} elseif ( count ( $parts ) == 2 ) {
2007-06-20 06:07:11 +00:00
if ( $parts [ 0 ] === $this -> model () && $parts [ 1 ] === $this -> field ()) {
2007-05-02 02:31:06 +00:00
return ;
}
}
}
2007-06-20 06:07:11 +00:00
if ( ! is_null ( $options )) {
2007-05-02 02:31:06 +00:00
$this -> fields [ $model ][ $this -> field ()] = $options ;
return ;
}
$this -> fields [ $model ][] = $this -> field ();
return ;
}
2007-06-20 06:07:11 +00:00
if ( ! is_null ( $options )) {
2007-05-02 02:31:06 +00:00
$this -> fields [ $model ][ $this -> field ()] = $options ;
return ;
}
$this -> fields [ $model ][] = $this -> field ();
return ;
}
}
2006-02-18 23:42:21 +00:00
/**
2006-12-31 08:28:55 +00:00
* Returns true if there is an error for the given field , otherwise false
2005-12-27 03:33:44 +00:00
*
2007-03-17 07:58:22 +00:00
* @ param string $field This should be " Modelname.fieldname " , " Modelname/fieldname " is deprecated
2007-10-22 16:09:35 +00:00
* @ return boolean If there are errors this method returns true , else false .
2007-10-09 20:44:28 +00:00
* @ access public
2005-12-27 03:33:44 +00:00
*/
2006-05-26 05:29:17 +00:00
function isFieldError ( $field ) {
2006-12-31 08:28:55 +00:00
$this -> setFormTag ( $field );
return ( bool ) $this -> tagIsInvalid ();
}
/**
* Returns a formatted error message for given FORM field , NULL if no errors .
*
2007-03-17 07:58:22 +00:00
* @ param string $field A field name , like " Modelname.fieldname " , " Modelname/fieldname " is deprecated
2006-12-31 08:28:55 +00:00
* @ param string $text Error message
* @ param array $options Rendering options for < div /> wrapper tag
* @ return string If there are errors this method returns an error message , otherwise null .
2007-10-09 20:44:28 +00:00
* @ access public
2006-12-31 08:28:55 +00:00
*/
function error ( $field , $text = null , $options = array ()) {
2006-08-30 16:23:24 +00:00
$this -> setFormTag ( $field );
2007-03-23 16:06:48 +00:00
$options = am ( array ( 'wrap' => true , 'class' => 'error-message' , 'escape' => true ), $options );
2006-05-26 05:29:17 +00:00
2007-01-17 20:17:25 +00:00
if ( $error = $this -> tagIsInvalid ()) {
2007-05-14 10:37:23 +00:00
if ( is_array ( $text ) && is_numeric ( $error ) && $error > 0 ) {
$error -- ;
}
2007-04-30 12:10:57 +00:00
if ( is_array ( $text ) && isset ( $text [ $error ])) {
$text = $text [ $error ];
2007-06-20 06:07:11 +00:00
} elseif ( is_array ( $text )) {
2007-04-30 12:10:57 +00:00
$text = null ;
}
2006-12-31 08:28:55 +00:00
if ( $text != null ) {
$error = $text ;
} elseif ( is_numeric ( $error )) {
$error = 'Error in field ' . Inflector :: humanize ( $this -> field ());
}
2007-03-23 16:06:48 +00:00
if ( $options [ 'escape' ]) {
$error = h ( $error );
}
if ( $options [ 'wrap' ] === true ) {
return $this -> Html -> div ( $options [ 'class' ], $error );
} else {
return $error ;
}
2006-05-26 05:29:17 +00:00
} else {
2006-12-31 08:28:55 +00:00
return null ;
2006-05-26 05:29:17 +00:00
}
}
2006-02-18 23:42:21 +00:00
/**
2005-12-27 03:33:44 +00:00
* Returns a formatted LABEL element for HTML FORMs .
*
2007-05-25 03:16:47 +00:00
* @ param string $fieldName This should be " Modelname.fieldname " , " Modelname/fieldname " is deprecated
2006-05-26 05:29:17 +00:00
* @ param string $text Text that will appear in the label field .
* @ return string The formatted LABEL element
2005-12-27 03:33:44 +00:00
*/
2007-05-25 03:16:47 +00:00
function label ( $fieldName = null , $text = null , $attributes = array ()) {
if ( empty ( $fieldName )) {
$fieldName = implode ( '.' , array_filter ( array ( $this -> model (), $this -> field ())));
2006-12-08 08:45:35 +00:00
}
2007-04-28 08:57:34 +00:00
if ( $text === null ) {
2007-05-25 03:16:47 +00:00
if ( strpos ( $fieldName , '/' ) !== false || strpos ( $fieldName , '.' ) !== false ) {
list ( , $text ) = preg_split ( '/[\/\.]+/' , $fieldName );
2006-08-30 16:23:24 +00:00
} else {
2007-05-25 03:16:47 +00:00
$text = $fieldName ;
2006-08-30 16:23:24 +00:00
}
2006-12-08 08:45:35 +00:00
if ( substr ( $text , - 3 ) == '_id' ) {
$text = substr ( $text , 0 , strlen ( $text ) - 3 );
}
2007-10-14 23:41:05 +00:00
$text = Inflector :: humanize ( Inflector :: underscore ( $text ));
2006-08-30 16:23:24 +00:00
}
2007-05-21 05:15:26 +00:00
2007-05-13 12:07:23 +00:00
if ( isset ( $attributes [ 'for' ])) {
$labelFor = $attributes [ 'for' ];
unset ( $attributes [ 'for' ]);
} else {
2007-05-25 03:16:47 +00:00
$labelFor = $this -> domId ( $fieldName );
2007-05-13 12:07:23 +00:00
}
2007-10-15 07:47:13 +00:00
if ( ! empty ( $text )) {
2007-09-07 01:22:03 +00:00
$text = __ ( $text , true );
}
2007-05-13 12:07:23 +00:00
return $this -> output ( sprintf ( $this -> Html -> tags [ 'label' ], $labelFor , $this -> _parseAttributes ( $attributes ), $text ));
2006-08-30 16:23:24 +00:00
}
2006-12-08 08:45:35 +00:00
/**
2007-05-25 03:16:47 +00:00
* Will display all the fields passed in an array expects fieldName as an array key
2006-11-27 01:04:19 +00:00
* replaces generateFields
*
2006-11-29 07:58:35 +00:00
* @ access public
2007-04-04 20:38:26 +00:00
* @ param array $fields works well with Controller :: generateFields () or on its own ;
* @ param array $blacklist a simple array of fields to skip
2007-05-01 15:17:27 +00:00
* @ return output
2006-11-30 18:07:08 +00:00
*/
2007-10-15 07:47:13 +00:00
function inputs ( $fields = null , $blacklist = null ) {
2007-10-16 04:41:00 +00:00
$fieldset = $legend = true ;
2007-10-14 23:41:05 +00:00
if ( is_array ( $fields )) {
if ( array_key_exists ( 'legend' , $fields )) {
$legend = $fields [ 'legend' ];
unset ( $fields [ 'legend' ]);
}
if ( isset ( $fields [ 'fieldset' ])) {
2007-10-16 04:41:00 +00:00
$fieldset = $fields [ 'fieldset' ];
2007-10-14 23:41:05 +00:00
unset ( $fields [ 'fieldset' ]);
}
2007-10-15 07:47:13 +00:00
} elseif ( $fields !== null ) {
2007-10-14 23:41:05 +00:00
$legend = $fields ;
unset ( $fields );
2007-04-17 02:06:15 +00:00
}
2007-10-14 23:41:05 +00:00
if ( empty ( $fields )) {
$fields = array_keys ( $this -> fieldset [ 'fields' ]);
}
if ( $legend === true ) {
2007-10-22 23:41:43 +00:00
$actionName = __ ( 'New' , true );
if ( strpos ( $this -> action , 'update' ) !== false || strpos ( $this -> action , 'edit' ) !== false ) {
$actionName = __ ( 'Edit' , true );
2007-10-14 23:41:05 +00:00
}
2007-10-22 23:41:43 +00:00
$modelName = Inflector :: humanize ( Inflector :: underscore ( $this -> model ()));
$legend = $actionName . ' ' . __ ( $modelName , true );
2007-04-05 21:20:08 +00:00
}
2006-11-27 01:04:19 +00:00
$out = null ;
2007-06-20 06:07:11 +00:00
foreach ( $fields as $name => $options ) {
2006-12-08 08:45:35 +00:00
if ( is_numeric ( $name ) && ! is_array ( $options )) {
2007-10-22 23:41:43 +00:00
$name = $options ;
$options = array ();
2006-12-08 08:45:35 +00:00
}
2007-06-21 18:02:56 +00:00
if ( is_array ( $blacklist ) && in_array ( $name , $blacklist )) {
continue ;
}
2006-12-08 08:45:35 +00:00
$out .= $this -> input ( $name , $options );
2006-11-27 01:04:19 +00:00
}
2007-10-16 04:41:00 +00:00
if ( $fieldset ) {
2007-04-17 02:06:15 +00:00
return sprintf ( $this -> Html -> tags [ 'fieldset' ], $legend , $out );
} else {
return $out ;
}
2006-11-27 01:04:19 +00:00
}
2006-08-30 16:23:24 +00:00
/**
* Generates a form input element complete with label and wrapper div
*
2007-05-25 03:16:47 +00:00
* @ param string $fieldName This should be " Modelname.fieldname " , " Modelname/fieldname " is deprecated
2006-08-30 16:23:24 +00:00
* @ param array $options
* @ return string
*/
2007-05-25 03:16:47 +00:00
function input ( $fieldName , $options = array ()) {
$this -> setFormTag ( $fieldName );
2007-01-16 12:54:55 +00:00
$options = am (
array (
'before' => null ,
'between' => null ,
'after' => null
),
$options );
2006-09-29 01:32:09 +00:00
2006-08-30 16:23:24 +00:00
if ( ! isset ( $options [ 'type' ])) {
2006-12-08 08:45:35 +00:00
$options [ 'type' ] = 'text' ;
2006-08-30 16:23:24 +00:00
if ( isset ( $options [ 'options' ])) {
$options [ 'type' ] = 'select' ;
2007-10-15 07:47:13 +00:00
} elseif ( in_array ( $this -> field (), array ( 'psword' , 'passwd' , 'password' ))) {
2006-09-29 01:32:09 +00:00
$options [ 'type' ] = 'password' ;
2007-06-20 06:07:11 +00:00
} elseif ( isset ( $this -> fieldset [ 'fields' ][ $this -> field ()])) {
2007-04-04 20:38:26 +00:00
$type = $this -> fieldset [ 'fields' ][ $this -> field ()];
$primaryKey = $this -> fieldset [ 'key' ];
2007-06-20 06:07:11 +00:00
} elseif ( ClassRegistry :: isKeySet ( $this -> model ())) {
2006-12-08 08:45:35 +00:00
$model =& ClassRegistry :: getObject ( $this -> model ());
$type = $model -> getColumnType ( $this -> field ());
2007-04-05 12:11:23 +00:00
$primaryKey = $model -> primaryKey ;
2007-04-04 20:38:26 +00:00
}
2007-06-20 06:07:11 +00:00
if ( isset ( $type )) {
2006-12-08 08:45:35 +00:00
$map = array (
2006-12-12 22:43:01 +00:00
'string' => 'text' , 'datetime' => 'datetime' ,
2007-05-24 23:14:14 +00:00
'boolean' => 'checkbox' , 'timestamp' => 'datetime' ,
2006-12-08 08:45:35 +00:00
'text' => 'textarea' , 'time' => 'time' ,
'date' => 'date'
);
if ( isset ( $map [ $type ])) {
$options [ 'type' ] = $map [ $type ];
2007-10-22 23:41:43 +00:00
} elseif ( $type === $this -> field ()) {
$this -> setFormTag ( $this -> field () . '.' . $this -> field ());
$fieldName = $this -> field () . '.' . $this -> field ();
2006-12-08 08:45:35 +00:00
}
2007-06-20 06:07:11 +00:00
if ( $this -> field () == $primaryKey ) {
2006-12-12 22:43:01 +00:00
$options [ 'type' ] = 'hidden' ;
}
2007-06-08 01:17:40 +00:00
}
}
2007-04-04 20:38:26 +00:00
2007-10-15 07:47:13 +00:00
if ( $this -> model () === $this -> field ()) {
$options [ 'type' ] = 'select' ;
$options [ 'multiple' ] = 'multiple' ;
}
if ( ! isset ( $options [ 'options' ]) && in_array ( $options [ 'type' ], array ( 'text' , 'radio' , 'select' ))) {
$view =& ClassRegistry :: getObject ( 'view' );
$varName = Inflector :: variable ( Inflector :: pluralize ( preg_replace ( '/_id$/' , '' , $this -> field ())));
$varOptions = $view -> getVar ( $varName );
if ( is_array ( $varOptions )) {
if ( $options [ 'type' ] === 'text' ) {
$options [ 'type' ] = 'select' ;
2007-06-08 01:17:40 +00:00
}
2007-10-15 07:47:13 +00:00
$options [ 'options' ] = $varOptions ;
2007-04-04 20:38:26 +00:00
}
}
2007-06-20 06:07:11 +00:00
if ( ! array_key_exists ( 'maxlength' , $options ) && $options [ 'type' ] == 'text' ) {
if ( isset ( $this -> fieldset [ 'sizes' ][ $this -> field ()])) {
2007-04-04 20:38:26 +00:00
$options [ 'maxlength' ] = $this -> fieldset [ 'sizes' ][ $this -> field ()];
}
}
2006-12-22 16:26:54 +00:00
$out = '' ;
2006-12-08 08:45:35 +00:00
$div = true ;
2007-04-04 20:38:26 +00:00
if ( array_key_exists ( 'div' , $options )) {
2006-12-08 08:45:35 +00:00
$div = $options [ 'div' ];
unset ( $options [ 'div' ]);
2006-08-30 16:23:24 +00:00
}
2007-04-05 21:20:08 +00:00
2007-06-20 06:07:11 +00:00
if ( ! empty ( $div )) {
2007-08-13 20:20:01 +00:00
$divOptions = array ( 'class' => 'input' );
2007-04-04 20:38:26 +00:00
if ( is_string ( $div )) {
$divOptions [ 'class' ] = $div ;
} elseif ( is_array ( $div )) {
$divOptions = am ( $divOptions , $div );
}
2007-04-05 02:11:20 +00:00
if ( in_array ( $this -> field (), $this -> fieldset [ 'validates' ])) {
$divOptions = $this -> addClass ( $divOptions , 'required' );
2007-04-05 21:20:08 +00:00
}
2006-08-30 16:23:24 +00:00
}
2007-04-05 21:20:08 +00:00
2006-08-30 16:23:24 +00:00
$label = null ;
2007-10-15 07:47:13 +00:00
if ( isset ( $options [ 'label' ]) && $options [ 'type' ] !== 'radio' ) {
2006-08-30 16:23:24 +00:00
$label = $options [ 'label' ];
unset ( $options [ 'label' ]);
}
2007-05-21 05:15:26 +00:00
2007-10-15 07:47:13 +00:00
if ( $options [ 'type' ] === 'radio' ) {
$label = false ;
if ( isset ( $options [ 'options' ])) {
if ( is_array ( $options [ 'options' ])) {
$radioOptions = $options [ 'options' ];
} else {
$radioOptions = array ( $options [ 'options' ]);
}
unset ( $options [ 'options' ]);
}
}
2007-05-13 12:07:23 +00:00
if ( $label !== false ) {
$labelAttributes = array ();
2007-05-21 05:15:26 +00:00
2007-05-13 12:07:23 +00:00
if ( in_array ( $options [ 'type' ], array ( 'date' , 'datetime' ))) {
$labelFor = $this -> domId ( implode ( '.' , array_filter ( array ( $this -> model (), $this -> field ()))));
2007-08-13 20:20:01 +00:00
$labelAttributes = array ( 'for' => $labelFor . 'Month' );
2007-05-13 12:07:23 +00:00
}
2007-05-21 05:15:26 +00:00
2007-05-13 12:07:23 +00:00
if ( is_array ( $label )) {
$labelText = null ;
if ( isset ( $label [ 'text' ])) {
$labelText = $label [ 'text' ];
unset ( $label [ 'text' ]);
}
2007-08-27 03:15:08 +00:00
2007-05-13 12:07:23 +00:00
$labelAttributes = am ( $labelAttributes , $label );
} else {
$labelText = $label ;
2006-12-22 05:23:07 +00:00
}
2007-05-21 05:15:26 +00:00
2007-10-15 07:47:13 +00:00
$out = $this -> label ( null , $labelText , $labelAttributes );
2006-12-22 05:23:07 +00:00
}
2006-08-30 16:23:24 +00:00
$error = null ;
if ( isset ( $options [ 'error' ])) {
$error = $options [ 'error' ];
unset ( $options [ 'error' ]);
2006-11-27 19:56:06 +00:00
}
2006-11-30 18:07:08 +00:00
2006-11-27 01:04:19 +00:00
$selected = null ;
2006-11-29 10:21:22 +00:00
if ( array_key_exists ( 'selected' , $options )) {
2006-11-27 01:04:19 +00:00
$selected = $options [ 'selected' ];
unset ( $options [ 'selected' ]);
2006-11-30 18:07:08 +00:00
}
2007-06-20 06:07:11 +00:00
if ( isset ( $options [ 'rows' ]) || isset ( $options [ 'cols' ])) {
2007-01-01 19:39:07 +00:00
$options [ 'type' ] = 'textarea' ;
}
2007-01-14 05:46:03 +00:00
2007-01-11 00:11:55 +00:00
$empty = false ;
2007-06-20 06:07:11 +00:00
if ( isset ( $options [ 'empty' ])) {
2007-01-11 00:11:55 +00:00
$empty = $options [ 'empty' ];
unset ( $options [ 'empty' ]);
}
2006-12-24 19:25:04 +00:00
2007-08-11 14:47:53 +00:00
$timeFormat = 12 ;
if ( isset ( $options [ 'timeFormat' ])) {
$timeFormat = $options [ 'timeFormat' ];
unset ( $options [ 'timeFormat' ]);
}
2007-05-24 23:14:14 +00:00
$type = $options [ 'type' ];
$before = $options [ 'before' ];
2007-01-16 12:54:55 +00:00
$between = $options [ 'between' ];
2007-05-24 23:14:14 +00:00
$after = $options [ 'after' ];
2007-01-16 12:54:55 +00:00
unset ( $options [ 'type' ], $options [ 'before' ], $options [ 'between' ], $options [ 'after' ]);
2006-12-31 08:28:55 +00:00
switch ( $type ) {
2006-11-27 01:04:19 +00:00
case 'hidden' :
2007-05-25 03:16:47 +00:00
$out = $this -> hidden ( $fieldName , $options );
2006-12-08 08:45:35 +00:00
unset ( $divOptions );
2006-11-27 01:04:19 +00:00
break ;
case 'checkbox' :
2007-05-25 03:16:47 +00:00
$out = $before . $this -> checkbox ( $fieldName , $options ) . $between . $out ;
2006-11-27 01:04:19 +00:00
break ;
2007-06-23 04:25:37 +00:00
case 'radio' :
2007-10-11 16:45:23 +00:00
$out = $before . $out . $this -> radio ( $fieldName , $radioOptions , $options ) . $between ;
2007-06-23 04:25:37 +00:00
break ;
2006-08-30 16:23:24 +00:00
case 'text' :
2006-09-29 01:32:09 +00:00
case 'password' :
2007-05-25 03:16:47 +00:00
$out = $before . $out . $between . $this -> { $type }( $fieldName , $options );
2006-09-29 01:32:09 +00:00
break ;
2006-08-30 16:23:24 +00:00
case 'file' :
2007-05-25 03:16:47 +00:00
$out = $before . $out . $between . $this -> file ( $fieldName , $options );
2006-08-30 16:23:24 +00:00
break ;
case 'select' :
2007-01-17 20:07:35 +00:00
$options = am ( array ( 'options' => array ()), $options );
2007-01-16 12:54:55 +00:00
$list = $options [ 'options' ];
unset ( $options [ 'options' ]);
2007-05-25 03:16:47 +00:00
$out = $before . $out . $between . $this -> select ( $fieldName , $list , $selected , $options , $empty );
2006-08-30 16:23:24 +00:00
break ;
2006-11-27 01:04:19 +00:00
case 'time' :
2007-08-11 14:47:53 +00:00
$out = $before . $out . $between . $this -> dateTime ( $fieldName , null , $timeFormat , $selected , $options , $empty );
2006-11-27 01:04:19 +00:00
break ;
case 'date' :
2007-05-25 03:16:47 +00:00
$out = $before . $out . $between . $this -> dateTime ( $fieldName , 'MDY' , null , $selected , $options , $empty );
2006-11-27 01:04:19 +00:00
break ;
case 'datetime' :
2007-08-11 14:47:53 +00:00
$out = $before . $out . $between . $this -> dateTime ( $fieldName , 'MDY' , $timeFormat , $selected , $options , $empty );
2006-11-27 01:04:19 +00:00
break ;
2006-09-29 01:32:09 +00:00
case 'textarea' :
default :
2007-05-25 03:16:47 +00:00
$out = $before . $out . $between . $this -> textarea ( $fieldName , am ( array ( 'cols' => '30' , 'rows' => '6' ), $options ));
2006-09-29 01:32:09 +00:00
break ;
2006-08-30 16:23:24 +00:00
}
2007-07-07 23:30:40 +00:00
if ( $type != 'hidden' ) {
2007-01-16 12:54:55 +00:00
$out .= $after ;
2007-07-07 23:30:40 +00:00
if ( $error !== false ) {
$out .= $this -> error ( $fieldName , $error );
}
2007-01-16 12:54:55 +00:00
}
2006-12-08 08:45:35 +00:00
if ( isset ( $divOptions )) {
$out = $this -> Html -> div ( $divOptions [ 'class' ], $out , $divOptions );
2006-08-30 16:23:24 +00:00
}
2006-12-08 08:45:35 +00:00
return $out ;
2006-05-26 05:29:17 +00:00
}
2007-05-24 21:44:05 +00:00
/**
* Creates a checkbox input widget .
*
* @ param string $fieldNamem Name of a field , like this " Modelname.fieldname " , " Modelname/fieldname " is deprecated
* @ param array $options Array of HTML attributes .
* @ return string An HTML text input element
*/
function checkbox ( $fieldName , $options = array ()) {
$value = 1 ;
2007-06-20 06:07:11 +00:00
if ( isset ( $options [ 'value' ])) {
2007-05-24 21:44:05 +00:00
$value = $options [ 'value' ];
unset ( $options [ 'value' ]);
}
2007-05-24 23:14:14 +00:00
2007-07-26 14:23:38 +00:00
$options = $this -> __initInputField ( $fieldName , $options );
2007-05-30 00:08:25 +00:00
$this -> __secure ();
2007-05-24 23:14:14 +00:00
2007-05-24 21:44:05 +00:00
$model = $this -> model ();
if ( ClassRegistry :: isKeySet ( $model )) {
$object =& ClassRegistry :: getObject ( $model );
}
2007-05-24 23:14:14 +00:00
2007-05-24 21:44:05 +00:00
$output = null ;
2007-06-20 06:07:11 +00:00
if ( isset ( $object ) && isset ( $options [ 'value' ]) && ( $options [ 'value' ] == 0 || $options [ 'value' ] == 1 )) {
2007-05-24 21:44:05 +00:00
$db =& ConnectionManager :: getDataSource ( $object -> useDbConfig );
2007-06-27 15:37:51 +00:00
$value = $db -> boolean ( $options [ 'value' ], false );
2007-05-24 21:44:05 +00:00
$options [ 'value' ] = 1 ;
}
2007-06-20 04:30:45 +00:00
$output = $this -> hidden ( $fieldName , array ( 'value' => '0' , 'id' => $options [ 'id' ] . '_' ), true );
2007-05-24 23:14:14 +00:00
2007-06-20 06:07:11 +00:00
if ( isset ( $options [ 'value' ]) && $value == $options [ 'value' ]) {
2007-05-24 21:44:05 +00:00
$options [ 'checked' ] = 'checked' ;
2007-06-20 06:07:11 +00:00
} elseif ( ! empty ( $value )) {
2007-05-24 21:44:05 +00:00
$options [ 'value' ] = $value ;
}
2007-05-24 23:14:14 +00:00
2007-10-09 20:44:28 +00:00
$output .= sprintf ( $this -> Html -> tags [ 'checkbox' ], $options [ 'name' ], $this -> _parseAttributes ( $options , array ( 'name' ), null , ' ' ));
2007-05-24 21:44:05 +00:00
return $this -> output ( $output );
2007-05-24 23:14:14 +00:00
}
2007-06-22 04:13:29 +00:00
/**
* Creates a set of radio widgets .
*
2007-10-09 20:44:28 +00:00
* @ param string $fieldName Name of a field , like this " Modelname.fieldname "
* @ param array $options Radio button options array .
* @ param array $attributes Array of HTML attributes . Use the 'separator' key to
* define the string in between the radio buttons
2007-06-22 04:13:29 +00:00
* @ return string
*/
2007-10-09 20:44:28 +00:00
function radio ( $fieldName , $options = array (), $attributes = array ()) {
$attributes = $this -> __initInputField ( $fieldName , $attributes );
2007-06-22 04:13:29 +00:00
$this -> __secure ();
2007-06-22 06:10:27 +00:00
2007-10-15 07:47:13 +00:00
$legend = false ;
if ( isset ( $attributes [ 'legend' ])) {
$legend = $attributes [ 'legend' ];
unset ( $attributes [ 'legend' ]);
} elseif ( count ( $options ) > 1 ) {
$legend = Inflector :: humanize ( $this -> field ());
2007-06-22 04:13:29 +00:00
}
2007-06-22 06:10:27 +00:00
2007-10-15 07:47:13 +00:00
$label = true ;
2007-08-27 00:11:36 +00:00
if ( isset ( $attributes [ 'label' ])) {
$label = $attributes [ 'label' ];
unset ( $attributes [ 'label' ]);
}
2007-08-17 08:50:16 +00:00
2007-10-15 07:47:13 +00:00
$inbetween = null ;
if ( isset ( $attributes [ 'separator' ])) {
$inbetween = $attributes [ 'separator' ];
unset ( $attributes [ 'separator' ]);
}
2007-06-22 06:10:27 +00:00
2007-10-15 07:47:13 +00:00
if ( isset ( $attributes [ 'value' ])) {
$value = $attributes [ 'value' ];
} else {
$value = $this -> value ( $fieldName );
}
$out = array ();
2007-06-22 04:13:29 +00:00
foreach ( $options as $optValue => $optTitle ) {
$optionsHere = array ( 'value' => $optValue );
2007-09-07 01:22:03 +00:00
if ( isset ( $value ) && $optValue == $value ) {
2007-10-09 20:44:28 +00:00
$optionsHere [ 'checked' ] = 'checked' ;
}
$parsedOptions = $this -> _parseAttributes ( array_merge ( $attributes , $optionsHere ), array ( 'name' , 'type' , 'id' ), '' , ' ' );
2007-10-15 07:47:13 +00:00
$tagName = Inflector :: camelize ( $this -> field () . '_' . Inflector :: underscore ( $optValue ));
if ( $label ) {
2007-08-27 03:15:08 +00:00
$optTitle = sprintf ( $this -> Html -> tags [ 'label' ], $tagName , null , $optTitle );
2007-08-27 00:11:36 +00:00
}
2007-10-09 20:44:28 +00:00
$out [] = sprintf ( $this -> Html -> tags [ 'radio' ], $attributes [ 'name' ], $tagName , $parsedOptions , $optTitle );
2007-06-22 04:13:29 +00:00
}
2007-10-15 07:47:13 +00:00
$hidden = null ;
if ( ! isset ( $value )) {
$hidden = $this -> hidden ( $fieldName , array ( 'value' => '' , 'id' => $attributes [ 'id' ] . '_' ), true );
}
$out = $hidden . join ( $inbetween , $out );
if ( $legend ) {
$out = sprintf ( $this -> Html -> tags [ 'fieldset' ], $legend , $out );
}
2007-08-27 00:11:36 +00:00
return $this -> output ( $out );
2007-06-22 04:13:29 +00:00
}
2006-07-22 14:13:07 +00:00
/**
* Creates a text input widget .
*
2007-03-17 07:58:22 +00:00
* @ param string $fieldNamem Name of a field , like this " Modelname.fieldname " , " Modelname/fieldname " is deprecated
2007-04-01 04:55:56 +00:00
* @ param array $options Array of HTML attributes .
2006-07-22 14:13:07 +00:00
* @ return string An HTML text input element
*/
2007-04-01 04:55:56 +00:00
function text ( $fieldName , $options = array ()) {
$options = $this -> __initInputField ( $fieldName , am ( array ( 'type' => 'text' ), $options ));
2007-05-30 00:08:25 +00:00
$this -> __secure ();
2007-10-09 20:44:28 +00:00
return $this -> output ( sprintf ( $this -> Html -> tags [ 'input' ], $options [ 'name' ], $this -> _parseAttributes ( $options , array ( 'name' ), null , ' ' )));
2006-07-22 14:13:07 +00:00
}
2006-09-29 01:32:09 +00:00
/**
* Creates a password input widget .
*
2007-03-17 07:58:22 +00:00
* @ param string $fieldName Name of a field , like this " Modelname.fieldname " , " Modelname/fieldname " is deprecated
2007-04-01 04:55:56 +00:00
* @ param array $options Array of HTML attributes .
2006-09-29 01:32:09 +00:00
* @ return string
*/
2007-04-01 04:55:56 +00:00
function password ( $fieldName , $options = array ()) {
$options = $this -> __initInputField ( $fieldName , $options );
2007-05-30 00:08:25 +00:00
$this -> __secure ();
2007-10-09 20:44:28 +00:00
return $this -> output ( sprintf ( $this -> Html -> tags [ 'password' ], $options [ 'name' ], $this -> _parseAttributes ( $options , array ( 'name' ), null , ' ' )));
2006-09-29 01:32:09 +00:00
}
2006-08-22 19:35:08 +00:00
/**
* Creates a textarea widget .
*
2007-03-17 07:58:22 +00:00
* @ param string $fieldNamem Name of a field , like this " Modelname.fieldname " , " Modelname/fieldname " is deprecated
2007-04-01 04:55:56 +00:00
* @ param array $options Array of HTML attributes .
2006-08-22 19:35:08 +00:00
* @ return string An HTML text input element
*/
2007-04-01 04:55:56 +00:00
function textarea ( $fieldName , $options = array ()) {
$options = $this -> __initInputField ( $fieldName , $options );
2007-05-30 00:08:25 +00:00
$this -> __secure ();
2006-11-27 01:04:19 +00:00
$value = null ;
2007-04-01 04:55:56 +00:00
if ( array_key_exists ( 'value' , $options )) {
$value = $options [ 'value' ];
2007-10-17 17:08:34 +00:00
if ( ! array_key_exists ( 'escape' , $options ) || $options [ 'escape' ] !== false ) {
$value = h ( $value );
}
2007-04-01 04:55:56 +00:00
unset ( $options [ 'value' ]);
2006-08-22 19:35:08 +00:00
}
2007-10-17 17:08:34 +00:00
return $this -> output ( sprintf ( $this -> Html -> tags [ 'textarea' ], $options [ 'name' ], $this -> _parseAttributes ( $options , array ( 'type' , 'name' ), null , ' ' ), $value ));
2006-08-22 19:35:08 +00:00
}
2006-11-26 20:43:54 +00:00
/**
* Creates a hidden input field .
*
2007-03-17 07:58:22 +00:00
* @ param string $fieldName Name of a field , like this " Modelname.fieldname " , " Modelname/fieldname " is deprecated
2007-04-01 04:55:56 +00:00
* @ param array $options Array of HTML attributes .
2006-11-26 20:43:54 +00:00
* @ return string
2006-12-24 19:25:04 +00:00
* @ access public
2006-11-26 20:43:54 +00:00
*/
2007-04-01 04:55:56 +00:00
function hidden ( $fieldName , $options = array ()) {
$options = $this -> __initInputField ( $fieldName , $options );
2007-01-22 08:12:06 +00:00
$model = $this -> model ();
2007-10-09 20:44:28 +00:00
$value = '' ;
2007-10-16 09:05:25 +00:00
$key = '_' . $model ;
2007-05-21 05:15:26 +00:00
2007-06-20 06:07:11 +00:00
if ( isset ( $this -> params [ '_Token' ]) && ! empty ( $this -> params [ '_Token' ])) {
2007-10-16 09:05:25 +00:00
$options [ 'name' ] = str_replace ( $model , $key , $options [ 'name' ]);
2007-05-01 15:17:27 +00:00
}
2007-10-09 20:44:28 +00:00
2007-06-04 06:11:48 +00:00
if ( ! empty ( $options [ 'value' ]) || $options [ 'value' ] === '0' ) {
$value = $options [ 'value' ];
}
2007-10-16 09:05:25 +00:00
$this -> __secure ( $key , $value );
2007-05-01 08:56:02 +00:00
2007-10-09 20:44:28 +00:00
/* if ( in_array ( $fieldName , array ( '_method' , '_fields' ))) {
2007-01-22 08:12:06 +00:00
$model = null ;
2007-10-09 20:44:28 +00:00
} */
return $this -> output ( sprintf ( $this -> Html -> tags [ 'hidden' ], $options [ 'name' ], $this -> _parseAttributes ( $options , array ( 'name' , 'class' ), '' , ' ' )));
2006-11-26 20:43:54 +00:00
}
2006-12-24 19:25:04 +00:00
/**
* Creates file input widget .
*
2007-03-17 07:58:22 +00:00
* @ param string $fieldName Name of a field , like this " Modelname.fieldname " , " Modelname/fieldname " is deprecated
2007-04-01 04:55:56 +00:00
* @ param array $options Array of HTML attributes .
2006-12-24 19:25:04 +00:00
* @ return string
* @ access public
*/
2007-04-01 04:55:56 +00:00
function file ( $fieldName , $options = array ()) {
$options = $this -> __initInputField ( $fieldName , $options );
2007-05-30 00:08:25 +00:00
$this -> __secure ();
2007-10-09 20:44:28 +00:00
return $this -> output ( sprintf ( $this -> Html -> tags [ 'file' ], $options [ 'name' ], $this -> _parseAttributes ( $options , array ( 'name' ), '' , ' ' )));
2006-12-24 19:25:04 +00:00
}
2006-07-22 14:13:07 +00:00
/**
* Creates a button tag .
*
* @ param mixed $params Array of params [ content , type , options ] or the
2007-05-24 23:14:14 +00:00
* content of the button .
* @ param string $type Type of the button ( button , submit or reset ) .
2006-07-22 14:13:07 +00:00
* @ param array $options Array of options .
* @ return string A HTML button tag .
2006-12-24 19:25:04 +00:00
* @ access public
2006-07-22 14:13:07 +00:00
*/
function button ( $params , $type = 'button' , $options = array ()) {
2006-12-22 22:49:47 +00:00
trigger_error ( __ ( " Don't use me yet " ), E_USER_ERROR );
2006-07-22 14:13:07 +00:00
if ( isset ( $options [ 'name' ])) {
2007-05-09 02:09:56 +00:00
if ( strpos ( $options [ 'name' ], " / " ) !== false || strpos ( $options [ 'name' ], " . " ) !== false ) {
2007-05-27 19:54:36 +00:00
if ( $this -> value ( $options [ 'name' ])) {
2006-07-22 14:13:07 +00:00
$options [ 'checked' ] = 'checked' ;
}
$this -> setFieldName ( $options [ 'name' ]);
2006-12-08 08:45:35 +00:00
$options [ 'name' ] = 'data[' . $this -> model () . '][' . $this -> field () . ']' ;
2006-07-22 14:13:07 +00:00
}
}
$options [ 'type' ] = $type ;
$values = array (
'options' => $this -> _parseOptions ( $options ),
'tagValue' => $content
);
return $this -> _assign ( 'button' , $values );
}
2006-12-08 08:45:35 +00:00
/**
* Creates a submit button element .
*
2007-05-24 23:14:14 +00:00
* @ param string $caption The label appearing on the button
2006-12-08 08:45:35 +00:00
* @ param array $options
* @ return string A HTML submit button
*/
2007-10-20 05:55:37 +00:00
function submit ( $caption = null , $options = array ()) {
if ( ! $caption ) {
2007-10-20 07:10:46 +00:00
$caption = __ ( 'Submit' , true );
2007-10-20 05:55:37 +00:00
}
2006-12-08 08:45:35 +00:00
$options [ 'value' ] = $caption ;
2007-05-01 13:35:33 +00:00
$secured = null ;
2007-10-20 05:55:37 +00:00
2007-06-20 06:07:11 +00:00
if ( isset ( $this -> params [ '_Token' ]) && ! empty ( $this -> params [ '_Token' ])) {
2007-05-01 13:35:33 +00:00
$secured = $this -> secure ( $this -> fields );
2007-05-02 09:11:42 +00:00
$this -> fields = array ();
2007-05-01 13:35:33 +00:00
}
2006-12-24 19:25:04 +00:00
$div = true ;
2007-06-22 06:10:27 +00:00
2006-12-24 19:25:04 +00:00
if ( isset ( $options [ 'div' ])) {
$div = $options [ 'div' ];
unset ( $options [ 'div' ]);
}
$divOptions = array ();
2007-06-22 06:10:27 +00:00
2006-12-24 19:25:04 +00:00
if ( $div === true ) {
$divOptions [ 'class' ] = 'submit' ;
} elseif ( $div === false ) {
unset ( $divOptions );
} elseif ( is_string ( $div )) {
$divOptions [ 'class' ] = $div ;
} elseif ( is_array ( $div )) {
$divOptions = am ( array ( 'class' => 'submit' ), $div );
}
2007-06-22 06:10:27 +00:00
$out = $secured . $this -> output ( sprintf ( $this -> Html -> tags [ 'submit' ], $this -> _parseAttributes ( $options , null , '' , ' ' )));
2007-01-01 19:39:07 +00:00
2006-12-24 19:25:04 +00:00
if ( isset ( $divOptions )) {
2007-06-22 06:10:27 +00:00
$out = $this -> Html -> div ( $divOptions [ 'class' ], $out , $divOptions );
2006-12-24 19:25:04 +00:00
}
return $out ;
2006-12-08 08:45:35 +00:00
}
2006-07-22 14:13:07 +00:00
/**
* Creates an image input widget .
*
2007-05-24 23:14:14 +00:00
* @ param string $path Path to the image file , relative to the webroot / img / directory .
2007-04-01 04:55:56 +00:00
* @ param array $options Array of HTML attributes .
2006-07-22 14:13:07 +00:00
* @ return string HTML submit image element
*/
2007-04-01 04:55:56 +00:00
function submitImage ( $path , $options = array ()) {
2006-07-22 14:13:07 +00:00
if ( strpos ( $path , '://' )) {
$url = $path ;
} else {
2007-02-08 07:42:23 +00:00
$url = $this -> webroot ( IMAGES_URL . $path );
2006-07-22 14:13:07 +00:00
}
2007-04-01 04:55:56 +00:00
return $this -> output ( sprintf ( $this -> Html -> tags [ 'submitimage' ], $url , $this -> _parseAttributes ( $options , null , '' , ' ' )));
2006-07-22 14:13:07 +00:00
}
2006-07-30 12:19:48 +00:00
/**
* Returns a formatted SELECT element .
*
* @ param string $fieldName Name attribute of the SELECT
2006-10-05 08:50:04 +00:00
* @ param array $options Array of the OPTION elements ( as 'value' => 'Text' pairs ) to be used in the SELECT element
2006-11-26 20:43:54 +00:00
* @ param mixed $selected The option selected by default . If null , the default value
2007-05-24 23:14:14 +00:00
* from POST data will be used when available .
* @ param array $attributes The HTML attributes of the select element . If
* 'showParents' is included in the array and set to true ,
* an additional option element will be added for the parent
* of each option group .
2006-11-26 20:43:54 +00:00
* @ param mixed $showEmpty If true , the empty select option is shown . If a string ,
2007-05-24 23:14:14 +00:00
* that string is displayed as the empty element .
2006-07-30 12:19:48 +00:00
* @ return string Formatted SELECT element
*/
2006-11-26 20:43:54 +00:00
function select ( $fieldName , $options = array (), $selected = null , $attributes = array (), $showEmpty = '' ) {
$showParents = false ;
2007-05-05 00:34:56 +00:00
$escapeOptions = true ;
2007-05-21 05:15:26 +00:00
2007-05-05 00:34:56 +00:00
if ( isset ( $attributes [ 'escape' ])) {
$escapeOptions = $attributes [ 'escape' ];
unset ( $attributes [ 'escape' ]);
}
2007-10-09 20:44:28 +00:00
$attributes = $this -> __initInputField ( $fieldName , $attributes );
2007-05-21 05:15:26 +00:00
2006-12-22 05:23:07 +00:00
if ( is_string ( $options ) && isset ( $this -> __options [ $options ])) {
$options = $this -> __generateOptions ( $options );
2007-06-20 06:07:11 +00:00
} elseif ( ! is_array ( $options )) {
2006-11-29 07:58:35 +00:00
$options = array ();
}
2006-11-29 10:21:22 +00:00
if ( isset ( $attributes [ 'type' ])) {
unset ( $attributes [ 'type' ]);
}
2007-01-01 19:39:07 +00:00
if ( in_array ( 'showParents' , $attributes )) {
2006-11-26 20:43:54 +00:00
$showParents = true ;
2007-02-17 16:56:25 +00:00
unset ( $attributes [ 'showParents' ]);
2006-07-30 12:19:48 +00:00
}
2006-11-30 18:07:08 +00:00
2006-07-30 12:19:48 +00:00
if ( ! isset ( $selected )) {
2007-10-09 20:44:28 +00:00
$selected = $attributes [ 'value' ];
2006-07-30 12:19:48 +00:00
}
2006-11-30 18:07:08 +00:00
2007-01-17 20:07:35 +00:00
if ( isset ( $attributes ) && array_key_exists ( 'multiple' , $attributes )) {
2006-11-22 03:51:21 +00:00
$tag = $this -> Html -> tags [ 'selectmultiplestart' ];
2006-07-30 12:19:48 +00:00
} else {
2006-11-22 03:51:21 +00:00
$tag = $this -> Html -> tags [ 'selectstart' ];
2007-05-21 19:51:40 +00:00
$this -> __secure ();
2006-07-30 12:19:48 +00:00
}
2007-10-09 20:44:28 +00:00
$select [] = sprintf ( $tag , $attributes [ 'name' ], $this -> _parseAttributes ( $attributes , array ( 'name' , 'value' )));
2006-07-30 12:19:48 +00:00
2007-08-13 20:20:01 +00:00
if ( $showEmpty !== null && $showEmpty !== false && ! ( empty ( $showEmpty ) && ( isset ( $attributes ) && array_key_exists ( 'multiple' , $attributes )))) {
2007-06-20 06:07:11 +00:00
if ( $showEmpty === true ) {
2006-09-26 19:53:22 +00:00
$showEmpty = '' ;
}
2006-12-04 18:00:36 +00:00
$options = array_reverse ( $options , true );
$options [ '' ] = $showEmpty ;
2006-12-22 22:49:47 +00:00
$options = array_reverse ( $options , true );
2006-07-30 12:19:48 +00:00
}
2007-05-05 00:34:56 +00:00
$select = am ( $select , $this -> __selectOptions ( array_reverse ( $options , true ), $selected , array (), $showParents , array ( 'escape' => $escapeOptions )));
2006-11-22 03:51:21 +00:00
$select [] = sprintf ( $this -> Html -> tags [ 'selectend' ]);
2006-07-30 12:19:48 +00:00
return $this -> output ( implode ( " \n " , $select ));
}
2007-03-04 12:14:33 +00:00
/**
* Returns a SELECT element for days .
*
2007-04-01 04:55:56 +00:00
* @ param string $fieldName Prefix name for the SELECT element
2007-03-04 12:14:33 +00:00
* @ param string $selected Option which is selected .
2007-05-24 23:14:14 +00:00
* @ param array $attributes HTML attributes for the select element
2007-04-01 04:55:56 +00:00
* @ param mixed $showEmpty Show / hide the empty select option
2007-03-04 12:14:33 +00:00
* @ return string
*/
2007-04-01 04:55:56 +00:00
function day ( $fieldName , $selected = null , $attributes = array (), $showEmpty = true ) {
2007-05-27 19:54:36 +00:00
$value = $this -> value ( $fieldName );
2007-06-20 06:07:11 +00:00
2007-05-25 03:16:47 +00:00
if ( empty ( $value )) {
2007-06-20 06:07:11 +00:00
if ( ! $showEmpty && ! $selected ) {
2007-05-25 03:16:47 +00:00
$value = 'now' ;
2007-06-20 06:07:11 +00:00
} elseif ( strlen ( $selected ) > 2 ) {
2007-05-25 03:16:47 +00:00
$value = $selected ;
2007-06-20 06:07:11 +00:00
} elseif ( $selected === false ) {
2007-05-25 03:16:47 +00:00
$selected = null ;
}
2007-03-04 12:14:33 +00:00
}
2007-06-20 06:07:11 +00:00
if ( ! empty ( $value )) {
2007-05-25 03:16:47 +00:00
$selected = date ( 'd' , strtotime ( $value ));
2007-04-01 04:55:56 +00:00
}
return $this -> select ( $fieldName . " _day " , $this -> __generateOptions ( 'day' ), $selected , $attributes , $showEmpty );
2007-03-04 12:14:33 +00:00
}
/**
* Returns a SELECT element for years
*
2007-04-01 04:55:56 +00:00
* @ param string $fieldName Prefix name for the SELECT element
2007-10-22 16:11:12 +00:00
* @ param integer $minYear First year in sequence
* @ param integer $maxYear Last year in sequence
2007-03-04 12:14:33 +00:00
* @ param string $selected Option which is selected .
2007-04-01 04:55:56 +00:00
* @ param array $attributes Attribute array for the select elements .
2007-10-22 16:54:36 +00:00
* @ param boolean $showEmpty Show / hide the empty select option
2007-03-04 12:14:33 +00:00
* @ return string
*/
2007-04-01 04:55:56 +00:00
function year ( $fieldName , $minYear = null , $maxYear = null , $selected = null , $attributes = array (), $showEmpty = true ) {
2007-05-27 19:54:36 +00:00
$value = $this -> value ( $fieldName );
2007-06-20 06:07:11 +00:00
if ( empty ( $value )) {
if ( ! $showEmpty && ! $maxYear && ! $selected ) {
2007-05-25 03:16:47 +00:00
$value = 'now' ;
2007-06-20 06:07:11 +00:00
} elseif ( ! $showEmpty && $maxYear && ! $selected ) {
2007-05-25 03:16:47 +00:00
$selected = $maxYear ;
2007-06-20 06:07:11 +00:00
} elseif ( strlen ( $selected ) > 4 ) {
2007-05-25 03:16:47 +00:00
$value = $selected ;
2007-06-20 06:07:11 +00:00
} elseif ( $selected === false ) {
2007-05-25 03:16:47 +00:00
$selected = null ;
}
2007-03-04 12:14:33 +00:00
}
2007-06-08 01:17:40 +00:00
2007-06-20 06:07:11 +00:00
if ( ! empty ( $value )) {
$selected = date ( 'Y' , strtotime ( $value ));
}
2007-04-01 14:31:23 +00:00
return $this -> select ( $fieldName . " _year " , $this -> __generateOptions ( 'year' , $minYear , $maxYear ), $selected , $attributes , $showEmpty );
2007-03-04 12:14:33 +00:00
}
/**
* Returns a SELECT element for months .
*
2007-05-25 03:16:47 +00:00
* @ param string $fieldName Prefix name for the SELECT element
2007-03-04 12:14:33 +00:00
* @ param string $selected Option which is selected .
2007-10-22 16:54:36 +00:00
* @ param boolean $showEmpty Show / hide the empty select option
2007-03-04 12:14:33 +00:00
* @ return string
*/
2007-05-25 03:16:47 +00:00
function month ( $fieldName , $selected = null , $attributes = array (), $showEmpty = true ) {
2007-05-27 19:54:36 +00:00
$value = $this -> value ( $fieldName );
2007-06-20 06:07:11 +00:00
2007-05-25 03:16:47 +00:00
if ( empty ( $value )) {
2007-06-20 06:07:11 +00:00
if ( ! $showEmpty && ! $selected ) {
2007-05-25 03:16:47 +00:00
$value = 'now' ;
2007-06-20 06:07:11 +00:00
} elseif ( strlen ( $selected ) > 2 ) {
2007-05-25 03:16:47 +00:00
$value = $selected ;
2007-06-20 06:07:11 +00:00
} elseif ( $selected === false ) {
2007-05-25 03:16:47 +00:00
$selected = null ;
}
}
2007-06-20 06:07:11 +00:00
if ( ! empty ( $value )) {
2007-03-04 12:14:33 +00:00
$selected = date ( 'm' , strtotime ( $value ));
}
2007-05-25 03:16:47 +00:00
return $this -> select ( $fieldName . " _month " , $this -> __generateOptions ( 'month' ), $selected , $attributes , $showEmpty );
2007-03-04 12:14:33 +00:00
}
/**
* Returns a SELECT element for hours .
*
2007-05-25 03:16:47 +00:00
* @ param string $fieldName Prefix name for the SELECT element
2007-10-22 16:54:36 +00:00
* @ param boolean $format24Hours True for 24 hours format
2007-03-04 12:14:33 +00:00
* @ param string $selected Option which is selected .
2007-04-01 04:55:56 +00:00
* @ param array $attributes List of HTML attributes
* @ param mixed $showEmpty True to show an empty element , or a string to provide default empty element text
2007-03-04 12:14:33 +00:00
* @ return string
*/
2007-05-25 03:16:47 +00:00
function hour ( $fieldName , $format24Hours = false , $selected = null , $attributes = array (), $showEmpty = true ) {
2007-05-27 19:54:36 +00:00
$value = $this -> value ( $fieldName );
2007-06-20 06:07:11 +00:00
2007-05-25 03:16:47 +00:00
if ( empty ( $value )) {
2007-06-20 06:07:11 +00:00
if ( ! $showEmpty && ! $selected ) {
2007-05-25 03:16:47 +00:00
$value = 'now' ;
2007-06-20 06:07:11 +00:00
} elseif ( strlen ( $selected ) > 2 ) {
2007-05-25 03:16:47 +00:00
$value = $selected ;
2007-06-20 06:07:11 +00:00
} elseif ( $selected === false ) {
2007-05-25 03:16:47 +00:00
$selected = null ;
2007-03-04 12:14:33 +00:00
}
}
2007-06-20 06:07:11 +00:00
if ( ! empty ( $value ) && $format24Hours ) {
2007-05-25 03:16:47 +00:00
$selected = date ( 'H' , strtotime ( $value ));
2007-06-20 06:07:11 +00:00
} elseif ( ! empty ( $value ) && ! $format24Hours ) {
2007-05-25 03:16:47 +00:00
$selected = date ( 'g' , strtotime ( $value ));
2007-03-04 12:14:33 +00:00
}
2007-05-25 03:16:47 +00:00
return $this -> select ( $fieldName . " _hour " , $this -> __generateOptions ( $format24Hours ? 'hour24' : 'hour' ), $selected , $attributes , $showEmpty );
2007-03-04 12:14:33 +00:00
}
/**
* Returns a SELECT element for minutes .
*
2007-05-25 03:16:47 +00:00
* @ param string $fieldName Prefix name for the SELECT element
2007-03-04 12:14:33 +00:00
* @ param string $selected Option which is selected .
* @ return string
*/
2007-05-25 03:16:47 +00:00
function minute ( $fieldName , $selected = null , $attributes = array (), $showEmpty = true ) {
2007-05-27 19:54:36 +00:00
$value = $this -> value ( $fieldName );
2007-06-20 06:07:11 +00:00
2007-05-25 03:16:47 +00:00
if ( empty ( $value )) {
2007-06-20 06:07:11 +00:00
if ( ! $showEmpty && ! $selected ) {
2007-05-25 03:16:47 +00:00
$value = 'now' ;
2007-06-20 06:07:11 +00:00
} elseif ( strlen ( $selected ) > 2 ) {
2007-05-25 03:16:47 +00:00
$value = $selected ;
2007-06-20 06:07:11 +00:00
} elseif ( $selected === false ) {
2007-05-25 03:16:47 +00:00
$selected = null ;
}
}
2007-06-20 06:07:11 +00:00
if ( ! empty ( $value )) {
2007-03-04 12:14:33 +00:00
$selected = date ( 'i' , strtotime ( $value ));
}
2007-05-25 03:16:47 +00:00
return $this -> select ( $fieldName . " _min " , $this -> __generateOptions ( 'minute' ), $selected , $attributes , $showEmpty );
2007-03-04 12:14:33 +00:00
}
/**
* Returns a SELECT element for AM or PM .
*
2007-05-25 03:16:47 +00:00
* @ param string $fieldName Prefix name for the SELECT element
2007-03-04 12:14:33 +00:00
* @ param string $selected Option which is selected .
* @ return string
*/
2007-05-25 03:16:47 +00:00
function meridian ( $fieldName , $selected = null , $attributes = array (), $showEmpty = true ) {
2007-05-27 19:54:36 +00:00
if ( empty ( $selected ) && $value = $this -> value ( $fieldName )) {
2007-03-04 12:14:33 +00:00
$selected = date ( 'a' , strtotime ( $value ));
}
2007-04-01 04:55:56 +00:00
$selected = empty ( $selected ) ? ( $showEmpty ? null : date ( 'a' )) : $selected ;
2007-05-25 03:16:47 +00:00
return $this -> select ( $fieldName . " _meridian " , $this -> __generateOptions ( 'meridian' ), $selected , $attributes , $showEmpty );
2007-03-04 12:14:33 +00:00
}
/**
* Returns a set of SELECT elements for a full datetime setup : day , month and year , and then time .
*
2007-05-25 03:16:47 +00:00
* @ param string $fieldName Prefix name for the SELECT element
2007-03-04 12:14:33 +00:00
* @ param string $dateFormat DMY , MDY , YMD or NONE .
* @ param string $timeFormat 12 , 24 , NONE
* @ param string $selected Option which is selected .
* @ return string The HTML formatted OPTION element
*/
2007-05-25 03:16:47 +00:00
function dateTime ( $fieldName , $dateFormat = 'DMY' , $timeFormat = '12' , $selected = null , $attributes = array (), $showEmpty = true ) {
2007-05-24 23:14:14 +00:00
$day = null ;
$month = null ;
$year = null ;
$hour = null ;
$min = null ;
2007-03-04 12:14:33 +00:00
$meridian = null ;
if ( empty ( $selected )) {
2007-05-27 19:54:36 +00:00
$selected = $this -> value ( $fieldName );
2007-03-04 12:14:33 +00:00
}
if ( ! empty ( $selected )) {
if ( is_int ( $selected )) {
$selected = strftime ( '%Y-%m-%d %H:%M:%S' , $selected );
}
$meridian = 'am' ;
$pos = strpos ( $selected , '-' );
2007-06-20 07:51:52 +00:00
if ( $pos !== false ) {
2007-03-04 12:14:33 +00:00
$date = explode ( '-' , $selected );
$days = explode ( ' ' , $date [ 2 ]);
$day = $days [ 0 ];
$month = $date [ 1 ];
$year = $date [ 0 ];
} else {
$days [ 1 ] = $selected ;
}
if ( $timeFormat != 'NONE' && ! empty ( $timeFormat )) {
$time = explode ( ':' , $days [ 1 ]);
2007-03-04 15:28:08 +00:00
$check = str_replace ( ':' , '' , $days [ 1 ]);
2007-03-04 12:14:33 +00:00
2007-03-04 15:28:08 +00:00
if (( $check > 115959 ) && $timeFormat == '12' ) {
2007-03-04 12:14:33 +00:00
$time [ 0 ] = $time [ 0 ] - 12 ;
$meridian = 'pm' ;
2007-06-20 06:07:11 +00:00
} elseif ( $time [ 0 ] > 12 ) {
2007-03-04 12:14:33 +00:00
$meridian = 'pm' ;
}
$hour = $time [ 0 ];
$min = $time [ 1 ];
}
}
$elements = array ( 'Day' , 'Month' , 'Year' , 'Hour' , 'Minute' , 'Meridian' );
2007-04-01 04:55:56 +00:00
if ( isset ( $attributes [ 'id' ])) {
if ( is_string ( $attributes [ 'id' ])) {
2007-03-04 12:14:33 +00:00
// build out an array version
foreach ( $elements as $element ) {
$selectAttrName = 'select' . $element . 'Attr' ;
${$selectAttrName} = $selectAttr ;
2007-04-01 04:55:56 +00:00
${$selectAttrName} [ 'id' ] = $attributes [ 'id' ] . $element ;
2007-03-04 12:14:33 +00:00
}
2007-04-01 04:55:56 +00:00
} elseif ( is_array ( $attributes [ 'id' ])) {
2007-03-04 12:14:33 +00:00
// check for missing ones and build selectAttr for each element
foreach ( $elements as $element ) {
$selectAttrName = 'select' . $element . 'Attr' ;
2007-04-01 04:55:56 +00:00
${$selectAttrName} = $attributes ;
${$selectAttrName} [ 'id' ] = $attributes [ 'id' ][ strtolower ( $element )];
2007-03-04 12:14:33 +00:00
}
}
} else {
// build the selectAttrName with empty id's to pass
foreach ( $elements as $element ) {
$selectAttrName = 'select' . $element . 'Attr' ;
2007-04-01 04:55:56 +00:00
${$selectAttrName} = $attributes ;
2007-03-04 12:14:33 +00:00
}
}
2007-04-17 02:06:15 +00:00
2007-04-11 15:20:37 +00:00
$attributes = am ( array ( 'minYear' => null , 'maxYear' => null ), $attributes );
2007-04-17 02:06:15 +00:00
2007-03-04 12:14:33 +00:00
switch ( $dateFormat ) {
case 'DMY' : // so uses the new selex
2007-05-25 03:16:47 +00:00
$opt = $this -> day ( $fieldName , $day , $selectDayAttr , $showEmpty ) . '-' .
$this -> month ( $fieldName , $month , $selectMonthAttr , $showEmpty ) . '-' . $this -> year ( $fieldName , $attributes [ 'minYear' ], $attributes [ 'maxYear' ], $year , $selectYearAttr , $showEmpty );
2007-03-04 12:14:33 +00:00
break ;
case 'MDY' :
2007-05-25 03:16:47 +00:00
$opt = $this -> month ( $fieldName , $month , $selectMonthAttr , $showEmpty ) . '-' .
$this -> day ( $fieldName , $day , $selectDayAttr , $showEmpty ) . '-' . $this -> year ( $fieldName , $attributes [ 'minYear' ], $attributes [ 'maxYear' ], $year , $selectYearAttr , $showEmpty );
2007-03-04 12:14:33 +00:00
break ;
case 'YMD' :
2007-05-25 03:16:47 +00:00
$opt = $this -> year ( $fieldName , $attributes [ 'minYear' ], $attributes [ 'maxYear' ], $year , $selectYearAttr , $showEmpty ) . '-' .
$this -> month ( $fieldName , $month , $selectMonthAttr , $showEmpty ) . '-' .
$this -> day ( $fieldName , $day , $selectDayAttr , $showEmpty );
2007-03-04 12:14:33 +00:00
break ;
case 'Y' :
2007-05-25 03:16:47 +00:00
$opt = $this -> year ( $fieldName , $attributes [ 'minYear' ], $attributes [ 'maxYear' ], $selected , $selectYearAttr , $showEmpty );
2007-03-04 12:14:33 +00:00
break ;
case 'NONE' :
default :
$opt = '' ;
break ;
}
switch ( $timeFormat ) {
case '24' :
2007-05-25 03:16:47 +00:00
$opt .= $this -> hour ( $fieldName , true , $hour , $selectHourAttr , $showEmpty ) . ':' .
$this -> minute ( $fieldName , $min , $selectMinuteAttr , $showEmpty );
2007-03-04 12:14:33 +00:00
break ;
case '12' :
2007-05-25 03:16:47 +00:00
$opt .= $this -> hour ( $fieldName , false , $hour , $selectHourAttr , $showEmpty ) . ':' .
$this -> minute ( $fieldName , $min , $selectMinuteAttr , $showEmpty ) . ' ' .
$this -> meridian ( $fieldName , $meridian , $selectMeridianAttr , $showEmpty );
2007-03-04 12:14:33 +00:00
break ;
case 'NONE' :
default :
$opt .= '' ;
break ;
}
return $opt ;
}
2007-10-09 20:44:28 +00:00
/**
* Gets the input field name for the current tag
*
* @ param array $options
* @ param string $key
* @ return array
*/
function __name ( $options = array (), $field = null , $key = 'name' ) {
if ( $this -> requestType == 'get' ) {
if ( $options === null ) {
$options = array ();
} elseif ( is_string ( $options )) {
$field = $options ;
$options = 0 ;
}
if ( ! empty ( $field )) {
$this -> setFormTag ( $field );
}
if ( is_array ( $options ) && isset ( $options [ $key ])) {
return $options ;
}
$name = $this -> field ();
if ( is_array ( $options )) {
$options [ $key ] = $name ;
return $options ;
} else {
return $name ;
}
}
return parent :: __name ( $options , $field , $key );
}
2006-07-30 12:19:48 +00:00
/**
* Returns an array of formatted OPTION / OPTGROUP elements
2006-08-04 08:03:39 +00:00
*
2006-07-30 12:19:48 +00:00
* @ return array
*/
2007-05-05 00:34:56 +00:00
function __selectOptions ( $elements = array (), $selected = null , $parents = array (), $showParents = null , $attributes = array ()) {
2007-05-21 05:15:26 +00:00
2006-07-30 12:19:48 +00:00
$select = array ();
2007-08-13 20:20:01 +00:00
$attributes = am ( array ( 'escape' => true ), $attributes );
$selectedIsEmpty = ( $selected === '' || $selected === null );
$selectedIsArray = is_array ( $selected );
2007-06-20 06:07:11 +00:00
foreach ( $elements as $name => $title ) {
2006-07-30 12:19:48 +00:00
$htmlOptions = array ();
if ( is_array ( $title ) && ( ! isset ( $title [ 'name' ]) || ! isset ( $title [ 'value' ]))) {
2006-08-07 00:48:25 +00:00
if ( ! empty ( $name )) {
2006-11-22 03:51:21 +00:00
$select [] = $this -> Html -> tags [ 'optiongroupend' ];
2006-09-26 19:53:22 +00:00
$parents [] = $name ;
2006-08-07 00:48:25 +00:00
}
2007-05-05 00:34:56 +00:00
$select = am ( $select , $this -> __selectOptions ( $title , $selected , $parents , $showParents , $attributes ));
2006-08-07 00:48:25 +00:00
if ( ! empty ( $name )) {
2006-11-22 03:51:21 +00:00
$select [] = sprintf ( $this -> Html -> tags [ 'optiongroup' ], $name , '' );
2006-08-07 00:48:25 +00:00
}
2006-07-30 12:19:48 +00:00
$name = null ;
} elseif ( is_array ( $title )) {
$htmlOptions = $title ;
$name = $title [ 'value' ];
$title = $title [ 'name' ];
unset ( $htmlOptions [ 'name' ], $htmlOptions [ 'value' ]);
}
if ( $name !== null ) {
2007-08-13 20:20:01 +00:00
if (( ! $selectedIsEmpty && ( $selected == $name )) || ( $selectedIsArray && in_array ( $name , $selected ))) {
2006-07-30 12:19:48 +00:00
$htmlOptions [ 'selected' ] = 'selected' ;
}
2007-06-20 06:07:11 +00:00
if ( $showParents || ( ! in_array ( $title , $parents ))) {
2007-05-05 00:34:56 +00:00
$title = ife ( $attributes [ 'escape' ], h ( $title ), $title );
$select [] = sprintf ( $this -> Html -> tags [ 'selectoption' ], $name , $this -> Html -> _parseAttributes ( $htmlOptions ), $title );
2006-09-26 19:53:22 +00:00
}
2006-07-30 12:19:48 +00:00
}
}
2006-11-22 03:32:58 +00:00
2006-09-28 02:57:29 +00:00
return array_reverse ( $select , true );
2006-07-30 12:19:48 +00:00
}
2006-12-22 05:23:07 +00:00
/**
* Generates option lists for common < select /> menus
*
*/
2007-04-01 04:55:56 +00:00
function __generateOptions ( $name , $min = null , $max = null ) {
2006-12-22 05:23:07 +00:00
if ( ! empty ( $this -> options [ $name ])) {
return $this -> options [ $name ];
}
$data = array ();
switch ( $name ) {
case 'minute' :
2007-06-20 06:15:35 +00:00
for ( $i = 0 ; $i < 60 ; $i ++ ) {
2006-12-22 05:23:07 +00:00
$data [ $i ] = sprintf ( '%02d' , $i );
}
break ;
case 'hour' :
2007-06-20 06:15:35 +00:00
for ( $i = 1 ; $i <= 12 ; $i ++ ) {
2007-04-01 04:55:56 +00:00
$data [ sprintf ( '%02d' , $i )] = $i ;
}
2007-04-01 14:47:39 +00:00
break ;
2007-04-01 04:55:56 +00:00
case 'hour24' :
2007-06-20 06:15:35 +00:00
for ( $i = 0 ; $i <= 23 ; $i ++ ) {
2006-12-22 05:23:07 +00:00
$data [ sprintf ( '%02d' , $i )] = $i ;
}
break ;
case 'meridian' :
$data = array ( 'am' => 'am' , 'pm' => 'pm' );
break ;
case 'day' :
2007-04-01 04:55:56 +00:00
if ( empty ( $min )) {
$min = 1 ;
}
if ( empty ( $max )) {
$max = 31 ;
}
2007-06-20 06:15:35 +00:00
for ( $i = $min ; $i <= $max ; $i ++ ) {
2006-12-22 05:23:07 +00:00
$data [ sprintf ( '%02d' , $i )] = $i ;
}
break ;
case 'month' :
2007-06-20 06:15:35 +00:00
for ( $i = 1 ; $i <= 12 ; $i ++ ) {
2007-05-24 23:27:30 +00:00
$data [ sprintf ( " %02s " , $i )] = strftime ( " %B " , mktime ( 1 , 1 , 1 , $i , 1 , 1999 ));
}
2006-12-22 05:23:07 +00:00
break ;
case 'year' :
$current = intval ( date ( 'Y' ));
2007-04-01 04:55:56 +00:00
if ( empty ( $min )) {
$min = $current - 20 ;
}
if ( empty ( $max )) {
$max = $current + 20 ;
}
if ( $min > $max ) {
list ( $min , $max ) = array ( $max , $min );
}
for ( $i = $min ; $i <= $max ; $i ++ ) {
2006-12-22 05:23:07 +00:00
$data [ $i ] = $i ;
}
break ;
}
$this -> __options [ $name ] = $data ;
return $this -> __options [ $name ];
}
2005-07-21 04:02:32 +00:00
}
2007-10-22 23:41:43 +00:00
?>