26 $namespace = @strtoupper( @str_replace(
'\\' ,
'_' , __NAMESPACE__ ) ) .
'_';
27 if ( !defined(
'_PTCFORM_' . $namespace ) )
29 @define(
'_PTCFORM_' . $namespace , get_called_class( ) );
31 $this->_options = ( !empty( $options ) ) ?
33 $this->_options = ( !empty( $this->_options ) ) ?
35 if ( method_exists( get_called_class( ) ,
'boot' ) ){ $this->boot( ); }
36 if ( $this->_options[
'keep_values' ] )
39 if ( !empty( $method ) )
41 foreach ( @$method as $k => $v )
43 if (
false !== @strpos( $k ,
'_ptcgen' ) )
45 $this->_hiddenValues[ $key = substr( $k , 0 , -12 ) ] = $v;
51 if ( method_exists( get_called_class( ) ,
'formFields' ) )
55 foreach ( $method as $k => $v )
57 if ( in_array( $k , $this->_submit ) )
59 $this->
_fireEvent(
'submit' , array( $k , $this ) );
60 unset( $this->_submit[ array_search( $k, $this->_submit ) ] );
64 if ( !defined(
'_PTC_RANDID_' ) )
66 @$_SESSION[
'_PTC_RANDID_' ] = 0;
67 @define(
'_PTC_RANDID_' ,
'PhpToolCase Random Id Generator' );
76 if ( !@array_key_exists(
'name' , $params ) )
78 trigger_error( __CLASS__ .
'::' . __FUNCTION__ .
'( ) missing "name" for defined field!' ,
79 $this->_options[
'err_msg_level' ] );
82 if ( !@array_key_exists(
'type' , $params ) ){ $params[
'type' ] =
'text'; }
83 $field_name = $params[
'name' ];
84 $field_type = $params[
'type' ];
85 $this->_fields[ $field_name ] = array(
'type' => $params[
'type' ] );
86 unset( $params[
'name' ] );
87 unset( $params[
'type' ] );
88 if ( array_key_exists(
'values' , $params ) )
90 $params = array_merge( array(
'values' => $params[
'values' ] ) , $params );
92 if ( array_key_exists(
'value' , $params ) )
94 $params = array_merge( array(
'value' => $params[
'value' ] ) , $params );
96 if ( array_key_exists(
'attributes' , $params ) )
98 $params = array_merge( array(
'attributes' => $params[
'attributes' ] ) , $params );
100 $this->
_fireEvent(
'adding' , array( $field_name , $field_type , &$params , $this ) );
101 foreach( $params as $k => $v )
103 $v = ( is_callable( $v ) ) ? call_user_func( $v , $this ) : $v;
104 if ( $match = preg_match(
'/\[.*?\]/' , $k , $matches ) )
107 if($field_type!=
'radiogroup' && $field_type!=
'checkboxgroup' && $field_type!=
'select')
109 trigger_error(__CLASS__.
'::'.__FUNCTION__.
'() brackets[] not supported for "'.
110 $field_type.
'" field',$this->_options[
'err_msg_level']);
114 if ( $err = $this->
_checkErrors( $field_name , 2 , __FUNCTION__ .
'()' ) ){
break; }
115 $type = explode(
'[' , trim( $k ) );
117 if ( $err = $this->
_checkErrors( $type[ 0 ] , 3 , __FUNCTION__ .
'( )' ) ){
continue; }
119 if ( $field_type ==
'select' && $type[ 0 ] !=
'attributes' )
121 trigger_error( __CLASS__ .
'::' . __FUNCTION__ .
'( )"' . $type[ 0 ] .
122 '[]" parameter is not supported for "select" field!' ,
123 $this->_options[
'err_msg_level' ] );
126 if ( $type[ 1 ] ===
']' )
128 foreach ( $this->_fields[ $field_name ][
'values' ] as $key => $arr )
136 $type[ 1 ] = str_replace(
']' ,
'' , $type[ 1 ] );
137 $this->
_addFieldParams( $field_name .
'=>' . $type[ 1 ] , $type[ 0 ] , $v );
141 if ( $k ===
'values' || $k ===
'value' ){ $this->
_addFieldValues( $field_name , $v );
continue; }
142 if ( $k===
'events' && ( $field_type ===
'radiogroup' || $field_type ===
'checkboxgroup'))
145 if ( $err = $this->
_checkErrors( $field_name , 2 , __FUNCTION__ .
'( )' ) ){
continue; }
146 foreach ( $this->_fields[ $field_name ][
'values' ] as $key => $arr )
152 if ( $err = $this->
_checkErrors( $k , 3 , __FUNCTION__ .
'( )' ) ){
continue; }
155 $this->_fields[ $field_name ] = $this->
_addDefaultValues( $this->_fields[ $field_name ] );
156 if (
'submit' === $field_type ){ $this->_submit[ ] = $field_name; }
157 $this->
_fireEvent(
'added' , array( $field_name , &$this->_fields[ $field_name ] , $this ) );
166 $spacer_height = ( $spacerVal ) ? $spacerVal : $this->_options[
'spacer_height' ];
167 $spacer_el = str_replace(
'{id}',
'id="ptc-gen' . $this->
_randomId( ) .
'"' , $this->_htmlTpls[
'spacer' ] ) .
"\n";
168 return $this->_options[
'start_tab' ] . $spacer_el = str_replace(
'{spacerVal}' , $spacer_height , $spacer_el );
176 public function render( $attributes = array( ) , $events = array( ) )
178 if ( $err = $this->
_checkErrors(
'' , 5 , __FUNCTION__ .
'( )' ) ){
return; }
180 foreach ( $method as $k => $v )
182 if ( in_array( $k , $this->_submit ) ){ $this->
_fireEvent(
'submit' , array( $k , $this ) ); }
184 $main_container = str_replace(
'{form_width}' , $this->_options[
'form_width' ] ,
185 "\n" . $this->_options[
'start_tab' ] . $this->_htmlTpls[
'main_container' ] );
186 $start_tab = $this->_options[
'start_tab' ];
187 $this->_options[
'start_tab' ] = $this->_options[
'start_tab' ] .
"\t";
188 $container =
"\n" . $this->_options[
'start_tab' ] . $this->_htmlTpls[
'form' ];
189 $container = str_replace(
'{action}' ,
'action="' . $this->_options[
'form_action' ] .
'"' , $container );
190 $form_method = strtolower( $this->_options[
'form_method' ] );
191 $container = str_replace(
'{method}' ,
'method="' . $form_method .
'"' , $container );
192 $this->_options[
'start_tab' ] = $this->_options[
'start_tab' ] .
"\t";
195 foreach ( $events as $k => $v ){ $js .= $k .
'="' . $v .
'" '; }
196 $container = str_replace(
'{events}' , $js_clean = @substr( $js , 0 , -1 ) , $container );
197 if ( !array_key_exists(
'id' , $attributes ) ){ $attributes[
'id' ] =
'ptc-gen' . $this->
_randomId( ); }
198 foreach ( $attributes as $k => $v )
200 $container = str_replace(
'{' . $k .
'}' , $k .
'="' . $v .
'"' , $container );
202 $container = preg_replace(
'# {.*?}|{.*?}^fields#i' ,
'' , $container );
203 $container = str_replace(
' >{fields}' ,
'>{fields}' , $container );
205 foreach ( $this->_fields as $k => $arrV )
207 $this->
_fireEvent(
'building' , array( $k , &$this->_fields[ $k ] , $this ) );
209 $this->
_fireEvent(
'built' , array( $k , &$fields , $this ) );
211 $container = str_replace(
'{fields}' ,
"\n" . $fields . $start_tab .
"\t" , $container );
212 $container = str_replace(
'{form}' , $container .
"\n" . $start_tab , $main_container );
214 self::_debug( $this->_options , $attributes[
'id' ] .
' form options' , $this->_options[
'debug_category' ] );
215 self::_debug( $this->_fields , $attributes[
'id' ] .
' form fields' , $this->_options[
'debug_category' ] );
216 if ( @$this->_validate[
'fields' ] )
218 self::_debug( $this->_validate[
'fields' ] , $attributes[
'id' ] .
' validator config' ,
219 $this->_options[
'debug_category' ] );
221 self::_debug( $this,$attributes[
'id' ] .
' form object' , $this->_options[
'debug_category' ] );
223 $container = (
$this->_errMsg ) ? $this->_errMsg . $container : $container;
224 $this->
_fireEvent(
'rendering' , array( &$container , $this ) );
225 if ( $this->_options[
'print_form' ] ){ print $container .
"\n"; }
226 else{
return $container .
"\n"; }
227 $this->
_fireEvent(
'rendered' , array( &$container , $this ) );
235 $signature = __CLASS__ .
'::' . __FUNCTION__;
236 if ( is_array( $this->_validate ) )
241 $validate = array(
'errors' =>
false ,
'isValid' =>
true ,
'fields' => $this->_validate );
242 foreach ( $validate[
'fields' ] as $k => $arr )
244 foreach ( $arr as $key => $val )
246 $this->
_fireEvent(
'validating' , array( $key , &$k , &$method ) );
252 @$errs[ $key ][ $k ] = 1;
258 @$errs[ $key ][ $k ] = 1;
260 $this->
_fireEvent(
'validated' , array( $k , $method , &$errs ) );
265 @$errs[ $key ][ $k ] = 1;
271 @$errs[ $key ][ $k ] = 1;
277 @$errs[ $key ][ $k ] = 1;
282 if ( method_exists( $this , $val ) )
284 if ( !$is_valid = @call_user_func( array( $this , $val ) , $k ) )
286 @$errs[ $val ][ $k ] = 1;
289 else if ( is_callable( $val ) )
291 if ( !$is_valid = @call_user_func( $val , $k ) )
293 @$errs[ $val ][ $k ] = 1;
298 trigger_error( $signature .
' could not run validator "'.
299 $val .
'"!' , $this->_options[
'err_msg_level' ] );
302 $this->
_fireEvent(
'validated' , array( $key , $k , &$method , &$errs ) );
305 $this->_validate = $validate;
306 if ( !empty( $errs ) )
308 $this->_validate[
'errors' ] = $errs;
309 $this->_validate[
'isValid' ] =
false;
310 $this->
_fireEvent(
'error' , array( &$this->_validate , &$this->_errMsg , $this ) );
312 else{ $this->
_fireEvent(
'valid' , array( &$this->_validate , &$this->_errMsg , $this ) ); }
313 self::_debug( $this->_validate ,
'validator result' , $this->_options[
'debug_category' ] );
316 trigger_error( $signature .
' no fields to validate found, quitting now!' , $this->_options[
'err_msg_level' ] );
327 return ( @strlen( $array[ $fieldName ] ) > 0 ) ?
true :
false;
340 $pattern =
"^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$";
341 return @eregi( $pattern , @$array[ $fieldName ] ) ?
true :
false;
355 return ( @is_numeric( @$array[ $fieldName ] ) ) ?
true :
false;
370 return ( @$array[ $fieldName ] == @$array[ $matchField ] ) ?
true :
false;
385 return @eregi( $pattern , @$array[ $fieldName ] ) ?
true :
false;
399 $this->_htmlTpls=array_merge( $this->_htmlTpls , $templates );
411 $attributes = ( @is_array( $attributes ) ) ? $attributes : array( $attributes );
412 $this->_elAttributes = array_merge( $this->_elAttributes , $attributes );
422 if ( !$type ) { $type = $this->_options[
'labels_align' ]; }
423 $this->_labelStyles[ $num ][ $type ] = array_merge(
424 $this->_labelStyles[ $num ][ $type ] , $labelStyle );
425 foreach ( $this->_labelStyles[ $num ][ $type ] as $k => $v )
427 if (
false === $v ){ unset($this->_labelStyles[ $num ][ $type ][ $k ]); }
437 $this->_inputStyles[ $type ] = array_merge( $this->_inputStyles[ $type ] , $style );
438 foreach ( $this->_inputStyles[ $type ] as $k => $v )
440 if (
false === $v ){ unset($this->_inputStyles[ $type ][ $k ]); }
451 trigger_error( $event_class .
' NOT FOUND!' , E_USER_ERROR );
454 $class = ( $class ) ? $class : get_called_class( );
455 $methods = get_class_methods( $class );
456 foreach ( $this->_events as $event )
458 if ( in_array( $event , $methods ) )
460 $cls = strtolower( $class );
461 $event_class::listen( $cls .
'.' . $event , $class .
'::' . $event );
462 $this->_observers[ $cls .
'.' . $event ] = $event;
492 'form_method' =>
'post' ,
493 'form_action' =>
'#' ,
494 'form_width' =>
'500px' ,
495 'add_class_validator' =>
false ,
496 'labels_align' =>
'left' ,
497 'labels_width' =>
'40%' ,
498 'style_elements' =>
true ,
499 'style_labels' =>
true ,
500 'style_tables' =>
true ,
501 'spacer_height' =>
'3px' ,
502 'keep_values' =>
true ,
503 'print_form' =>
true ,
504 'start_tab' =>
"\t" ,
505 'err_msg_level' => E_USER_WARNING ,
506 'debug_category' =>
'PtcForm' ,
507 'event_class' =>
'\PtcEvent'
514 'select_option' =>
'<option {attributes}>{label}</option>',
515 'select' =>
'<select name="{name}" {attributes}>{options}</select>',
516 'textarea' =>
'<textarea name="{name}" {attributes}>{value}</textarea>',
517 'input' =>
'<input type="{type}" name="{name}" {attributes}>',
518 'fieldset' =>
'<fieldset {attributes}>{label}{data} {start_tab}</fieldset>',
519 'form' =>
'<form {method} {action} {attributes}>{fields}</form>',
520 'spacer' =>
'<div style="clear:both;height:{spacerVal}" {id}><!-- --></div>',
521 'label' =>
'<label {for} {attributes}>{label}</label>',
522 'legend' =>
'<legend {attributes}>{label}</legend>',
523 'span' =>
'<span {attributes}>{label}</span>',
524 'td' =>
'<td align="left" valign="top">',
525 'table' =>
'<table {attributes}>',
526 'field_container' =>
'<div {attributes}>{label}{field} {start_tab}</div>',
527 'main_container' =>
'<div style="width:{form_width}">{form}</div>'
535 1 => array(
'left' => array(
'float' =>
'left' ,
'margin' =>
'2px 0 0 0' ,
'width' =>
'{label_width}%' ) ,
536 'right' => array(
'float' =>
'left' ,
'margin' =>
'1px 3px 0 0' ,
537 'text-align' =>
'right' ,
'width' =>
'{label_width}%' ) ,
538 'top' => array( ) ) ,
540 2 => array(
'left' => array(
'vertical-align' =>
'middle' ,
'border' =>
'none' ) ,
541 'right' => array(
'vertical-align' =>
'middle' ,
'border' =>
'none' ) ,
542 'top' => array( ) ) ,
544 3 => array(
'left' => array(
'float' =>
'left' ,
'margin' =>
'3px 0 0 0' ,
'width' =>
'{label_width}%' ) ,
545 'right' => array(
'float' =>
'left' ,
'margin' =>
'1px 3px 0 0' ,
546 'text-align' =>
'right' ,
'width' =>
'{label_width}%' ) ,
554 'radio' => array(
'padding' =>
'0px' ,
'margin' =>
'0px' ,
'vertical-align' =>
'middle' ,
'width' =>
'14px' ) ,
555 'input' => array(
'margin' =>
'0px' ,
'padding' =>
'3px' ,
'border' =>
'1px inset' ) ,
556 'button' => array(
'margin' =>
'0px' )
563 'class' ,
'id' ,
'style' ,
'value' ,
'maxlength' ,
'minlength' ,
'cellpadding' ,
564 'cellspacing' ,
'size' ,
'disabled' ,
'checked' ,
'target' ,
'align' ,
'events' ,
565 'title' ,
'selected' ,
'cols' ,
'rows' ,
'equalTo' ,
'border' ,
'pattern'
570 protected $_storageKeys = array(
'events' ,
'attributes' ,
'validate' ,
'label' ,
'labelOptions' ,
'parentEl' );
596 'rendering' ,
'rendered' ,
'validating' ,
'validated' ,
597 'building' ,
'built' ,
'error' ,
'valid' ,
'submit' ,
'adding' ,
'added'
610 if ( $err = $this->
_checkErrors( $fieldName , 1 , __FUNCTION__ .
"( )" ) ){
return; }
611 if ( !is_array( $options ) )
613 $options = array(
'value' => $options );
617 switch( $this->_fields[ $fieldName ][
'type' ] )
621 case 'checkboxgroup' :
622 foreach ( $options as $k => $v )
624 if ( !is_array( $v ) )
626 $field_type = str_replace(
'group' ,
'' , $this->_fields[ $fieldName ][
'type' ] );
627 $v = array(
'type' => $field_type ,
'label' => array( $v ) ,
628 'attributes' => array(
'value' => $k ) );
635 $this->_fields[ $fieldName ][
'values' ] = $options;
646 protected function _removeField( $fieldName ){ unset( $this->_fields[ $fieldName ] ); }
654 protected function _buildContainer( $fieldName , $fieldHtml , $labelHtml =
'' , $switch =
false )
656 $main_container =
'';
658 $main_container .= $this->_options[
'start_tab' ] . $this->_htmlTpls[
'field_container' ] .
"\n";
659 $main_container = $this->
_buildAttributes( $fieldName , $main_container ,
'parentEl' );
662 $main_container = str_replace(
' {start_tab}' , $this->_options[
'start_tab' ] , $main_container );
664 if ( $switch ){ $container = str_replace(
'{label}{field}' , $fieldHtml . $labelHtml , $main_container ); }
665 else{ $container = str_replace(
'{label}{field}' , $labelHtml . $fieldHtml , $main_container ); }
675 foreach ( $this->_fields[ $fieldName ][
'values' ] as $k => $v )
677 if ( $err = $this->
_checkErrors( $v , 4 , __FUNCTION__ .
'( )' ) )
679 unset( $this->_fields[ $fieldName ][
'values' ][ $k ] );
682 $this->_fields[ $fieldName ][
'values' ][ $v ] = $this->_fields[ $v ];
684 unset( $this->_fields[ $fieldName ][
'values' ][ $k ] );
693 foreach ( $this->_storageKeys as $k => $v )
695 if ( !@array_key_exists( $v , $array ) ){ $array[ $v ] = 0; }
706 switch ( $this->_fields[ $fieldName ][
'type' ] )
709 case 'checkboxgroup' :
711 $label_container = $this->_htmlTpls[
'span' ];
714 $label_container =
"\n" . $this->_options[
'start_tab' ] .
"\t" .
715 $this->_htmlTpls[
'legend' ];
717 default:$label_container = $this->_htmlTpls[
'label' ];
720 $label_html = str_replace(
'{label}' , $labelText , $label_container );
721 return $label_html = $this->
_buildAttributes( $fieldName , $label_html ,
'labelOptions' );
729 if($err=$this->
_checkErrors($fieldName,1,__FUNCTION__.
"()")){
return; }
734 if(@$this->_fields[$fieldName][
'label'][0])
736 $align_label=@array_key_exists(
'align',$this->_fields[$fieldName][
'labelOptions']) ?
737 $this->_fields[$fieldName][
'labelOptions'][
'align'] : $this->_options[
'labels_align'];
738 $label_width=@array_key_exists(
'width',$this->_fields[$fieldName][
'labelOptions']) ?
739 $this->_fields[$fieldName][
'labelOptions'][
'width'] : $this->_options[
'labels_width'];
740 $label_html=$this->
_switchLabelEl($fieldName,$this->_fields[$fieldName][
'label'][0]);
742 $spacer_height=@array_key_exists(
'spacer_height',$this->_fields[$fieldName][
'attributes']) ?
743 $this->_fields[$fieldName][
'attributes'][
'spacer_height'] : $this->_options[
'spacer_height'];
744 switch($field_type=$this->_fields[$fieldName][
'type'])
749 foreach( $this->_inputStyles[
'radio' ] as $k => $v ){ $dyn_style .= $k .
':' . $v .
';'; }
751 $label=$this->
_buildLabel(2,$align_label,$label_width,$label_html);
753 $label_container=$this->
_addLabelStyle($fieldName,$label[
'container'],$label[
'style']);
754 $field=str_replace(
'{inputField}',$this->
_buildHtml($fieldName),$label[
'input_container']);
755 $container=$this->
_buildContainer($fieldName,$field,$label_container,$label[
'switch']);
758 $container=$this->_options[
'start_tab'].$this->_fields[$fieldName][
'attributes'][
'value'].
"\n";
762 $input_container=
"\n".$this->_options[
'start_tab'].
"\t".
'{inputField}'.
"\n";
764 foreach($this->_inputStyles[
'button'] as $k=>$v){ $dyn_style.=$k.
":".$v.
";"; }
767 $field=str_replace(
'{inputField}',$this->
_buildHtml($fieldName),$input_container);
772 $ori_tab=$this->_options[
'start_tab'];
773 $this->_options[
'start_tab']=$this->_options[
'start_tab'].
"\t";
774 foreach($this->_fields[$fieldName][
'values'] as $k=>$arr)
776 $this->_fields[$k]=$arr;
778 unset($this->_fields[$k]);
780 $this->_options[
'start_tab']=$ori_tab;
781 $container=str_replace(
'{data}',$data,$this->_options[
'start_tab'].$this->_htmlTpls[
'fieldset']);
783 $container=str_replace(
'{label}',$label_html,$container);
784 $container=str_replace(
' {start_tab}',$this->_options[
'start_tab'],$container.
"\n");
786 case 'checkboxgroup':
788 $cols=!@array_key_exists(
'cols',$this->_fields[$fieldName][
'attributes']) ? 1 :
789 $this->_fields[$fieldName][
'attributes'][
'cols'];
791 $cols=$cols>
sizeof(@$this->_fields[$fieldName][
'values']) ?
792 sizeof(@$this->_fields[$fieldName][
'values']) : $cols;
793 $this->_fields[
'group_now']=$this->_fields[$fieldName];
794 unset($this->_fields[$fieldName]);
795 $label=$this->
_buildLabel(3,$align_label,$label_width,$label_html);
797 $table_container=$this->
_buildAttributes(
'group_now',$this->_htmlTpls[
'table'],
"attributes");
799 @$this->_fields[
'group_now'][
'values'],$table_container);
800 $this->_fields[$fieldName]=$this->_fields[
'group_now'];
801 unset($this->_fields[
'group_now']);
802 $field=
"\n".$this->_options[
'start_tab'].
"\t<div>{table}\t".$this->_options[
'start_tab'].
"</div>\n";
803 $field=str_replace(
'{table}',$table,$field);
805 $label_container=$this->
_addLabelStyle($fieldName,$label[
'container'],$label[
'style']);
809 $field_attributes=$this->_fields[$fieldName][
'attributes'];
810 $cols=!@array_key_exists(
'cols',$field_attributes) ?
811 sizeof($this->_fields[$fieldName][
'values']) : $field_attributes[
'cols'];
813 $cols=$cols>
sizeof($this->_fields[$fieldName][
'values']) ?
814 sizeof($this->_fields[$fieldName][
'values']) : $cols;
815 $label=$this->
_buildLabel(3,$align_label,$label_width,$label_html);
817 $table_container=$this->
_buildAttributes($fieldName,$this->_htmlTpls[
'table'],
'attributes');
819 $this->_fields[$fieldName][
'values'],$table_container);
820 $field=
"\n".$this->_options[
'start_tab'].
"\t<div>{table}\t".
821 $this->_options[
'start_tab'].
"</div>\n";
822 $field=str_replace(
'{table}',$table,$field);
824 $label_container=$this->
_addLabelStyle($fieldName,$label[
'container'],$label[
'style']);
830 foreach($this->_inputStyles[
'input'] as $k=>$v){ $dyn_style.=$k.
":".$v.
";"; }
832 $label=$this->
_buildLabel(1,$align_label,$label_width,$label_html);
834 $label_container=$this->
_addLabelStyle($fieldName,$label[
'container'],$label[
'style']);
835 $field=str_replace(
'{inputField}',$this->
_buildHtml($fieldName),$label[
'input_container']);
837 if($this->_fields[$fieldName][
'type']==
'select')
839 $field=str_replace(
'</div>',$this->_options[
'start_tab'].
"\t</div>",$field);
844 $container.=str_replace(
'{spacerVal}',$spacer_height,$this->
addSpacer());
845 $container=preg_replace(
'# {.*?}|{.*?}#i',
'',$container);
858 if(!$data){
return; }
859 $table=
"\n".$this->_options[
'start_tab'].
"\t\t".$container.
"\n";
860 $table.=$this->_options[
'start_tab'].
"\t\t\t".
'<tr>';
864 foreach($data as $k=>$arr)
866 $opts_spacer_height=$this->_options[
'spacer_height'];
867 $key=($type==1) ? $fieldName : $k;
868 $this->_fields[$key]=$arr;
869 $opts_start_tab=$this->_options[
'start_tab'];
870 $this->_options[
'start_tab']=$this->_options[
'start_tab'].
"\t\t\t\t\t";
871 $this->_options[
'spacer_height']=
'0px';
872 if($b>1 && $type==1){ $this->_buildHidden=
false; }
874 if($this->_options[
'keep_values'] && !empty($this->_hiddenValues) && !empty($method))
876 if(isset($method[str_replace(
'[]',
'',$fieldName)]))
878 $keep_vals=$method[str_replace(
'[]',
'',$fieldName)];
879 if(@is_array($keep_vals))
881 foreach($keep_vals as $k=>$v)
883 if(@$arr[
'attributes'][
'value']==$v)
886 $new_method_arr=$method;
887 $new_method_arr[str_replace(
'[]',
'',$fieldName)]=$v;
897 $this->_options[
'start_tab']=$opts_start_tab;
898 $this->_options[
'spacer_height']=$opts_spacer_height;
899 $table.=
"\n".$this->_options[
'start_tab'].
"\t\t\t\t".$this->_htmlTpls[
'td'];
900 $table.=
"\n".$td_content.$this->_options[
'start_tab'].
"\t\t\t\t".
'</td>';
903 $table.=
"\n".$this->_options[
'start_tab'].
"\t\t\t".
'</tr>';
905 if($b<
sizeof($data)){ $table.=
"\n".$this->_options[
'start_tab'].
"\t\t\t".
'<tr>'; }
909 unset($this->_fields[$key]);
911 $final=($cols*ceil(
sizeof($data)/$cols));
912 if(
sizeof($data)<$final)
914 for($z=
sizeof($data);$z<$final;$z++)
916 $table.=
"\n".$this->_options[
'start_tab'].
"\t\t\t\t".$this->_htmlTpls[
'td'].
" </td>";
918 $table.=
"\n".$this->_options[
'start_tab'].
"\t\t\t".
'</tr>';
920 $this->_buildHidden=$ori_build_hidden;
921 return $table.=
"\n".$this->_options[
'start_tab'].
"\t\t".
'</table>'.
"\n";
931 $options=is_array($options) ? $options : array($options);
932 $name=explode(
'=>',$fieldName);
934 $exclude_types=array(
'composite',
'fieldset');
938 if(!@$this->_fields[$fieldName])
940 trigger_error(__CLASS__.
'::'.__FUNCTION__.
' could not add '.$type.
941 ' for field '.$fieldName,E_USER_WARNING);
return;
943 if($type==
'validate')
945 if(in_array(@$this->_fields[$name[0]][
'type'],$exclude_types))
947 trigger_error(__CLASS__.
'::'.__FUNCTION__.
' could not add validator to field '.$fieldName.
948 ', '.@$this->_fields[$name[0]][
'type'].
' type not supported!',E_USER_WARNING);
954 if ( @array_key_exists(
'class' , $this->_fields[ $name[ 0 ] ][
'attributes' ] ) &&
955 $type ==
'attributes' && @array_key_exists(
'class' , $options ) )
957 $class=$this->_fields[$name[0]][
'attributes'][
'class'].
' '.$options[
'class'];
958 $this->_fields[$name[0]][
'attributes'][
'class']=trim($class);
961 if(!@is_array($this->_fields[$fieldName][$type])){ $this->_fields[$fieldName][$type]=$options; }
962 else{ $this->_fields[$fieldName][$type]=array_merge($this->_fields[$fieldName][$type],$options); }
965 if ( !@array_key_exists( $name[ 1 ] , $this->_fields[ $name[ 0 ] ][
'values' ] ) )
967 trigger_error(__CLASS__.
'::'.__FUNCTION__.
' could not find fieldname '.$fieldName,
971 if ( $type ==
'validate' )
973 if ( in_array( @$this->_fields[ $name[ 0 ] ][
'values' ][ $name[ 1 ] ][
'type' ] , $exclude_types ) )
975 trigger_error(__CLASS__.
'::'.__FUNCTION__.
' could not add validator to field '.$fieldName.
976 ', field type '.@$this->_fields[$name[0]][
'values'][$name[1]][
'type'].
' not supported!',
982 if ( $this->_fields[$name[0]][
'type' ] ==
'checkboxgroup' ||
983 $this->_fields[$name[0]][
'type' ] ==
'radiogroup'
991 if ( $this->_fields[$name[0]][
'values'][$name[1]][
'type']==
'radiogroup' ||
992 $this->_fields[$name[0]][
'values'][$name[1]][
'type']==
'checkboxgroup')
994 foreach($this->_fields[$name[0]][
'values'][$name[1]][
'values'] as $k=> $v)
996 $this->_fields[$k.
'_temp']=$v;
998 $this->_fields[$name[0]][
'values'][$name[1]][
'values'][$k]=$this->_fields[$k.
'_temp'];
999 unset($this->_fields[$k.
'_temp']);
1004 if(@array_key_exists(
'class',$this->_fields[$name[0]][
'values'][$name[1]][
'attributes']) &&
1005 $type==
'attributes' && @array_key_exists(
'class',$options))
1007 $class=$this->_fields[$name[0]][
'values'][$name[1]][
'attributes'][
'class'].
' '.$options[
'class'];
1008 $this->_fields[$name[0]][
'values'][$name[1]][
'attributes'][
'class']=trim($class);
1011 if(!@is_array($this->_fields[$name[0]][
'values'][$name[1]][$type]))
1013 $this->_fields[$name[0]][
'values'][$name[1]][$type]=$options;
1017 $options=array_merge($this->_fields[$name[0]][
'values'][$name[1]][$type],$options);
1018 $this->_fields[$name[0]][
'values'][$name[1]][$type]=$options;
1022 return $this->_fields[$name[0]];
1031 $options = is_array( $options ) ? $options : array( $options );
1032 foreach ( $options as $k => $v )
1034 if ( is_numeric( $k ) )
1036 $options[ $v ] = $v;
1037 unset( $options[ $k ] );
1040 if ( !@array_key_exists( $fieldName , $this->_validate ) )
1042 $this->_validate[ str_replace(
'[]' ,
'' , $fieldName ) ] = $options;
1044 else{ array_merge( $this->_validate[ $fieldName ] , $options ); }
1054 if($this->_options[
'add_class_validator'])
1056 foreach($options as $k=>$v)
1064 if($fieldType==
'radiogroup' || $fieldType==
'checkboxgroup')
1066 if(!@$this->_fields[$fieldName][
'values']){
break; }
1067 foreach($this->_fields[$fieldName][
'values'] as $key=>$arr)
1069 $this->
_addFieldParams($fieldName.
'=>'.$key,
'attributes',array(
'class'=>$v));
1072 else{ $this->
_addFieldParams( $fieldName ,
'attributes' , array(
'class' => $v ) ); }
1083 if ( $this->_options[
'style_elements' ] )
1085 if ( @array_key_exists(
'style' , $this->_fields[ $fieldName ][
'attributes' ] ) ){
return; }
1086 if ( !is_array( $this->_fields[ $fieldName ][
'attributes' ] ) )
1088 $this->_fields[ $fieldName ][
'attributes' ] = array( );
1090 $this->_fields[ $fieldName ][
'attributes' ][
'style' ] = $fieldStyle;
1102 if ( !@array_key_exists(
'style' , $this->_fields[ $fieldName ][
'labelOptions' ] ) &&
1103 $this->_options[
'style_labels' ] )
1105 $label_style = $style;
1107 $label_html = str_replace(
' {label_style}' , $label_style .
1108 ' id="ptc-gen' . $this->
_randomId( ) .
'"' , $labelContainer );
1110 return $label_html = str_replace(
'{for}' ,
'for="' .
1111 $this->_fields[ $fieldName ][
'attributes' ][
'id' ] .
'"' , $label_html );
1120 if ( $this->_options[
'style_tables' ] )
1122 if ( !@is_array( $this->_fields[ $fieldName ][
'attributes' ] ) )
1124 $this->_fields[ $fieldName ][
'attributes' ] = array( );
1126 if ( !@array_key_exists(
'style' , $this->_fields[ $fieldName ][
'attributes' ] ) )
1128 $this->_fields[ $fieldName ][
'attributes' ][
'style' ] = $tableStyle;
1130 if ( !@array_key_exists(
'cellpadding' , $this->_fields[ $fieldName ][
'attributes' ] ) )
1132 $this->_fields[ $fieldName ][
'attributes' ][
'cellpadding' ] = 0;
1134 if ( !@array_key_exists(
'cellspacing' , $this->_fields[ $fieldName ][
'attributes' ] ) )
1136 $this->_fields[ $fieldName ][
'attributes' ][
'cellspacing' ] = 0;
1146 $chain_attributes =
'';
1147 foreach ( $this->_elAttributes as $k => $v ){ $chain_attributes .=
'{' . $v .
'} '; }
1148 return $container = str_replace(
'{attributes}' ,
1149 substr( $chain_attributes , 0 , -1 ) , $container );
1160 if ( $arrKey ==
'parentEl' || $arrKey ==
'attributes')
1164 if ( @is_array( $this->_fields[ $fieldName ][ $arrKey ] ) )
1166 if ( $arrKey ==
'events' )
1169 foreach ( $this->_fields[ $fieldName ][ $arrKey ] as $k => $v )
1171 $events .= $k .
'="' . $v .
'" ';
1173 $events = substr( $events , 0 , -1 );
1174 return $container = str_replace(
'{events}' , $events , $container );
1176 $this->_fields[ $fieldName ][ $arrKey ] =
1177 array_filter( $this->_fields[ $fieldName ][ $arrKey ] ,
'strlen' );
1178 foreach ( $this->_fields[ $fieldName ][ $arrKey ] as $k => $v )
1180 $container = str_replace(
'{' . $k .
'}' , $k .
'="' . $v .
'"' , $container );
1192 if ( !@array_key_exists(
'id' , $this->_fields[ $fieldName ][ $arrKey ] ) )
1194 if ( !@is_array( $this->_fields[ $fieldName ][ $arrKey ] ) )
1196 $this->_fields[ $fieldName ][ $arrKey ] = array( );
1198 $this->_fields[ $fieldName ][ $arrKey ][
'id' ] =
'ptc-gen' . $this->
_randomId( );
1208 if ( @$this->_fields[ $fieldName ][
'values' ] )
1211 foreach ( @$this->_fields[ $fieldName ][
'values' ] as $k => $arrV )
1214 if ( @$arrV[
'label' ][ 0 ] )
1216 $option = str_replace(
'{label}' , $arrV[
'label' ][ 0 ] , $option );
1218 foreach ( @$arrV[
'attributes' ] as $k => $v )
1220 $option = str_replace(
'{' . $k .
'}' , $k .
'="' . $v .
'"' , $option );
1222 $options .= preg_replace(
'# {.*?}|{.*?}#i' ,
'' ,
"\n" .
1223 $this->_options[
'start_tab' ] .
"\t\t\t" . $option );
1225 $select_field = str_replace(
'{options}' , $options .
"\n" , $this->_htmlTpls[
'select' ] );
1226 $select_field =
"\n" . $this->_options[
'start_tab' ] .
"\t\t" . $select_field .
"\n";
1227 return $select_field = str_replace(
'</select>' ,
1228 $this->_options[
'start_tab' ] .
"\t\t</select>" , $select_field );
1238 switch( $this->_fields[ $fieldName ][
'type' ] )
1241 $html_field = $this->_htmlTpls[
'textarea' ];
1242 if ( isset( $this->_fields[ $fieldName ][
'attributes' ][
'value' ] ) )
1244 $html_field = str_replace(
'{value}' ,
1245 $this->_fields[ $fieldName ][
'attributes' ][
'value' ] , $html_field );
1248 case 'select':$html_field = $this->
_buildList( $fieldName );
1252 if ( $this->_options[
'keep_values' ] && $this->_buildHidden )
1254 $hidden_field = str_replace(
'{type}' ,
'hidden' , $this->_htmlTpls[
'input' ] ) .
"\n" .
1255 $this->_options[
'start_tab' ] .
"\t";
1256 $html_field = str_replace(
'{name}' , str_replace(
'[]' ,
'' , $fieldName ) .
'_' .
1257 mt_rand( 1000 , 9999 ) .
'_ptcgen' , $hidden_field );
1258 $html_field = preg_replace(
'# {.*?}|{.*?}#i' ,
'' , $html_field );
1260 @$html_field .= str_replace(
'{type}' ,
1261 $this->_fields[ $fieldName ][
'type' ] , $this->_htmlTpls[
'input' ] );
1263 default:$html_field = str_replace(
'{type}' ,
1264 $this->_fields[ $fieldName ][
'type' ] , $this->_htmlTpls[
'input' ] );
1267 $html_field = str_replace(
'{name}' , $fieldName , $html_field );
1268 $html_field = $this->
_buildAttributes( $fieldName , $html_field ,
'attributes' );
1269 return $html_field = $this->
_buildAttributes( $fieldName , $html_field ,
'events' );
1277 if ( !@array_key_exists(
'noAutoValue' , @$this->_fields[ $fieldName ][
'attributes' ] ) )
1280 if ( $this->_options[
'keep_values' ] && !empty( $method ) )
1282 switch ( $this->_fields[ $fieldName ][
'type' ] )
1286 if ( !@$this->_fields[ $fieldName ][
'attributes' ][
'value' ] )
1288 $this->_fields[ $fieldName ][
'attributes' ][
'value' ] =
'on';
1290 unset( $this->_fields[ $fieldName ][
'attributes' ][
'checked' ] );
1291 foreach ( $this->_hiddenValues as $k => $v )
1293 if ( @array_key_exists( $k , $method ) &&
1294 $k == str_replace(
'[]' ,
'' , $fieldName ) &&
1295 @$method[ $k ] == $this->_fields[ $fieldName ][
'attributes' ][
'value' ] )
1297 $this->
_addFieldParams( $fieldName ,
'attributes' , array(
'checked' => 1 ) );
1302 unset( $this->_fields[ $fieldName ][
'attributes' ][
'value' ] );
1303 if ( @strlen( $method[ $fieldName ] ) > 0 )
1309 foreach ( @$this->_fields[ $fieldName ][
'values' ] as $k => $arrV )
1311 unset( $this->_fields[ $fieldName ][
'values' ][ $k ][
'attributes' ][
'selected' ] );
1312 if ( @$method[ $fieldName ] == $k )
1315 'attributes' , array(
'selected' => 1 ) );
1320 unset( $this->_fields[ $fieldName ][
'attributes' ][
'value' ] );
1321 if ( @strlen( $method[ $fieldName ] ) > 0 )
1336 protected function _buildLabel( $case , $alignLabel , $labelWidth , $labelHtml )
1338 $label_width = str_replace(
'%' ,
'' , $labelWidth );
1339 $table_width = ( 99 - $label_width );
1343 switch ( $alignLabel )
1346 $label[
'container' ] =
"\n" . $this->_options[
'start_tab' ] .
"\t" .
1347 '<div {label_style}>' . $labelHtml .
'</div>';
1348 $label[
'input_container' ] =
"\n" . $this->_options[
'start_tab' ] .
"\t" .
1349 '<div {id}>{inputField}</div>' .
"\n";
1352 $label[
'container' ] =
"\n" . $this->_options[
'start_tab' ] .
"\t" .
1353 '<div {label_style}>' . $labelHtml .
'</div>';
1354 $label[
'input_container' ] =
"\n" . $this->_options[
'start_tab' ] .
"\t" .
1355 '<div {id}>{inputField}</div>'.
"\n";
1358 $label[
'container' ] =
"\n" . $this->_options[
'start_tab' ] .
"\t" .
1359 '<div {label_style}>' . $labelHtml .
'</div>';
1360 $label[
'input_container' ] =
"\n" . $this->_options[
'start_tab' ] .
"\t" .
1361 '<div {id}>{inputField}</div>'.
"\n";
1364 $label[
'container' ] =
'';
1365 $label[
'input_container' ] =
"\n" . $this->_options[
'start_tab' ] .
"\t" .
1366 '<div {id}>{inputField}</div>' .
"\n";
1369 $label[
'input_container' ] = str_replace(
"{id}" ,
'id="ptc-gen' .
1370 $this->
_randomId( ) .
'"' , $label[
'input_container' ] );
1372 else if ( $case == 2 )
1374 switch ( $alignLabel )
1377 $label[
'container' ] =
"\n" . $this->_options[
'start_tab' ] .
"\t" .
1378 '<span {label_style}>' . $labelHtml .
'</span>';
1379 $label[
'input_container' ] =
"\n" . $this->_options[
'start_tab' ] .
1380 "\t" .
'{inputField}' .
"\n";
1381 $label[
'switch' ] =
false;
1384 $label[
'container' ] =
"\n" . $this->_options[
'start_tab' ] .
"\t" .
1385 '<div {label_style}>' . $labelHtml .
'</div>';
1386 $label[
'input_container' ] =
"\n" . $this->_options[
'start_tab' ] .
"\t" .
1387 '<div {id}>{inputField}</div>' .
"\n";
1388 $label[
'switch' ] =
false;
1391 $label[
'container' ] =
"\n" . $this->_options[
'start_tab' ] .
"\t" .
1392 '<span {label_style}>' . $labelHtml .
'</span>' .
"\n";
1393 $label[
'input_container' ] =
"\n" . $this->_options[
'start_tab' ] .
"\t" .
'{inputField}';
1394 $label[
'switch' ] =
true;
1397 $label[
'container' ] =
'';
1398 $label[
'input_container' ] =
"\n" . $this->_options[
'start_tab' ] .
1399 "\t" .
'{inputField}' .
"\n";
1400 $label[
'switch' ] =
false;
1403 $label[
'input_container' ] = str_replace(
"{id}" ,
'id="ptc-gen' .
1404 $this->
_randomId( ) .
'"' , $label[
'input_container' ] );
1406 else if ( $case == 3 )
1408 switch ( $alignLabel )
1411 $label[
'container' ] =
"\n" . $this->_options[
'start_tab' ] .
"\t" .
1412 '<div {label_style}>' . $labelHtml .
'</div>';
1413 $label[
'table_style' ] =
"width:" . $table_width .
"%;";
1416 $label[
'container' ] =
"\n" . $this->_options[
'start_tab' ] .
"\t" .
1417 '<div {label_style}>' . $labelHtml .
'</div>';
1418 $label[
'table_style' ] =
"width:100%;";
1421 $label[
'container' ] =
"\n" . $this->_options[
'start_tab' ] .
"\t" .
1422 '<div {label_style}>' . $labelHtml .
'</div>';
1423 $label[
'table_style' ] =
"width:" . ( $table_width - 1 ) .
"%;";
1426 $label[
'container' ] =
'';
1427 $label[
'table_style' ] =
"width:100%;";
1431 if ( !@array_key_exists(
'style' , $label ) && @$this->_labelStyles[ $case ][ $alignLabel ] )
1433 $label[
'style' ] =
' style="';
1434 foreach ( $this->_labelStyles[ $case ][ $alignLabel ] as $k => $v )
1436 $label[
'style' ] .= $k .
':' . $v .
';';
1438 $label[
'style' ] = str_replace(
'{label_width}' , $label_width , $label[
'style' ] ) .
'"';
1440 else{ $label[
'style' ] =
''; }
1450 protected function _checkErrors( $fieldName , $type , $function = null , $errType = null )
1452 $errType = ( !$errType ) ? $this->_options[
'err_msg_level' ] : $errType;
1453 $signature = ( !$function ) ? __CLASS__ .
' ' : __CLASS__ .
'::' . $function .
' ';
1458 if ( !@$this->_fields[ $fieldName ] )
1460 $debug_msg =
'could not find field ' . $fieldName;
1461 trigger_error( $signature . $debug_msg , $errType );
1466 if ( !@$this->_fields[ $fieldName ][
'values' ] )
1468 $debug_msg =
'could not find values for ' . $fieldName;
1469 trigger_error( $signature . $debug_msg , $errType );
1474 if ( !in_array( $fieldName , $this->_storageKeys ) )
1476 $debug_msg =
'"' . $fieldName .
'" is not a valid type';
1477 trigger_error( $signature . $debug_msg , $errType );
1482 if ( !@$this->_fields[ $fieldName ] )
1484 $debug_msg =
'could not find field ' . $fieldName .
' to add to composite';
1485 trigger_error( $signature . $debug_msg , $errType );
1490 if ( empty( $this->_fields ) )
1492 $debug_msg =
'no fields defined, quitting now!';
1493 trigger_error( $signature . $debug_msg , $errType );
1505 switch ( strtolower( $this->_options[
'form_method' ] ) )
1507 case 'get':
return $_GET;
break;
1508 default:
return $_POST;
break;
1517 switch ( strtolower( $this->_options[
'form_method' ] ) )
1519 case 'get' : $_GET = $array;
break;
1520 default: $_POST = $array;
break;
1528 return @$_SESSION[
'_PTC_RANDID_' ] = ( @$_SESSION[
'_PTC_RANDID_' ] + 1 );
1537 $event = ( is_array( $event ) ) ? $event : array( $event );
1539 if ( !empty( $this->_observers ) )
1541 foreach ( $this->_observers as $k => $v )
1543 foreach ( $event as $ev )
1545 if ( $v === $ev ){ $event_class::fire( $k , $data ); }
1555 return __NAMESPACE__ . $this->_options[
'event_class' ];
1563 protected static function _debug( $string , $statement = null , $category = null )
1565 if ( !defined(
'_PTCDEBUG_NAMESPACE_' ) ) {
return false; }
1566 return @call_user_func_array( array(
'\\' . _PTCDEBUG_NAMESPACE_ ,
'bufferLog' ) ,
1567 array( $string , $statement , $category ) );