/usr/local/miolo2/classes/ui/controls/mform.class

Go to the documentation of this file.
00001 <?
00002 define ('FORM_SUBMIT_BTN_NAME', 'submit_button');
00003 
00008 class MForm extends MControl
00009 {
00013     var $title;
00014 
00018     var $action;
00019 
00023     var $method;
00024 
00028     var $buttons;
00029 
00033     var $fields=array();
00034 
00038     var $return;
00039 
00043     var $reset;
00044 
00048     var $styles;
00049 
00053     var $help;
00054 
00058     var $footer;
00059 
00063     var $width;
00064 
00068     var $showHints = true;
00069 
00073     var $enctype;
00074 
00078     var $validations;
00079 
00083     var $defaultButton;
00084 
00088     var $errors;
00089 
00093     var $infos;
00094 
00098     var $box;
00099 
00100     static $fieldNum = 0;
00104     var $layout;
00105 
00109     var $cssForm = false;
00110     var $cssButtons;
00114     var $zebra = false;
00118     var $labelWidth = NULL;
00119 
00120     var $bgColor = NULL;
00121     var $align = NULL;
00122     
00132     function __construct($title='',$action='',$close='',$icon='', $help='')
00133     {
00134         parent::__construct();
00135         $this->AddStyleFile('m_forms.css'); 
00136         $this->name          = $this->page->name;
00137         $this->box           = new MBox($title,$close,$icon,$help);
00138         $this->title         = $title;
00139         $this->action        = $action;
00140         $this->method        = 'post';
00141         $this->return        = false;
00142         $this->width         = '95%';
00143         $this->defaultButton = true;
00144         $this->fields        = array();
00145         $this->validations   = array();
00146         $this->CreateFields();
00147         if ($this->IsSubmitted())
00148         { 
00149             $this->GetFormFields(); // set the fields array with form post/get values
00150         }
00151         $this->onLoad();
00152     }
00153 
00154     
00165     function __set($name, $value)
00166     {
00167         $this->AddControl($value);
00168         $this->fields[$name] = $value;
00169     }
00170 
00171     
00181     function __get($name)
00182     {
00183         return $this->fields[$name];
00184     }
00185 
00186     
00194     function OnLoad()
00195     {
00196     }
00197 
00198     
00206     function CreateFields()
00207     {
00208     }
00209 
00210     
00220     function AddValidator($validator)
00221     {
00222         if ( ! $validator->label )
00223         {
00224             $field = $this->{$validator->field};
00225             $validator->label = ($validator->label == '') ? $field->label : $validator->label;
00226         }
00227 
00228         $name = '_validator_' . $this->name . '_' . $validator->id . '_' . $validator->field;
00229         $validator->name     = $name;
00230         $validator->form     = $this->name;
00231         $validator->max      = $validator->max ? $validator->max : 0;
00232         $this->validations[] = $validator;
00233         return $name;
00234     }
00235 
00236     
00246     function SetValidators($validators)
00247     {
00248        if (is_array($validators))
00249        {
00250           foreach($validators as $v)
00251           {
00252              $this->AddValidator($v);
00253           }
00254        }
00255        elseif (is_subclass_of( $validators, 'validator'))
00256        {
00257              $this->AddValidator($validators);
00258        }
00259     }
00260 
00261     
00274     function IsSubmitted()
00275     {   
00276         $isSubmitted = $this->defaultButton && MForm::getFormValue(FORM_SUBMIT_BTN_NAME);
00277         if (isset($this->buttons))
00278         {
00279             foreach($this->buttons as $b)
00280             {
00281                 $isSubmitted = $isSubmitted || MForm::GetFormValue($b->name);
00282             }
00283         }
00284         return $isSubmitted;
00285     }
00286     
00287     
00302     function GetTitle()
00303     {
00304         return $this->title;
00305     }
00306     
00307     
00320     function SetTitle($title)
00321     {
00322         $this->title = $title;
00323         $this->caption = $title;
00324         $this->box->SetCaption($title);
00325     }
00326 
00339     function SetClose($action)
00340     {
00341         if ($this->box->boxTitle != NULL)
00342         {
00343            $this->box->boxTitle->SetClose($action);
00344         }
00345     }
00346     
00347         
00360     public function SetIcon($icon)
00361     {
00362         if ($this->box->boxTitle != NULL)
00363         {
00364             $this->box->boxTitle->SetIcon($icon);
00365         }
00366     }
00367 
00368     function SetAlternate($color0, $color1)
00369     {
00370         $this->zebra = array($color0, $color1);
00371     }
00372 
00373     
00388     function GetFooter()
00389     {
00390         return $this->footer;
00391     }
00392 
00400     function SetFooter($footer)
00401     {
00402         $this->footer = $footer;
00403     }
00404 
00405     
00417     function GetFormFields()
00418     {   
00419        $this->_GetFormFieldValue($this->fields);
00420     }
00421 
00422     
00432     function _GetFormFieldValue($field)
00433     {   
00434         if ( is_array($field) )
00435         {
00436             foreach($field as $f)
00437             {
00438                 $this->_GetFormFieldValue($f);
00439             }
00440         }
00441         else
00442         {
00443             if ($field instanceof MFormControl)
00444             {
00445                 if ( $field->name )
00446                 {
00447                     $defvalue = $field->GetValue();
00448                     $value = $this->page->Request($field->name);
00449                     if ( ($field instanceof MCheckBox) || ($field instanceof MRadioButton) )
00450                     {
00451                        $field->checked = (isset($value) ? ($value == $field->value) : $field->checked);
00452                     }
00453                     else
00454                     {  
00455                        $field->SetValue(isset($value) ? $value : $defvalue);
00456                     }
00457                 }
00458             }
00459         }
00460     }
00461     
00462     
00478     function GetFormValue($name,$value=NULL)
00479     { 
00480         $result = '';
00481         if ( ($name != '') && ((strpos($name,'[')) === false))
00482         {
00483            $result = MIOLO::_REQUEST($name, 'ALL');
00484         }
00485         if (! isset($result))
00486         {
00487             $result = $value;
00488         }
00489         return $result;
00490     }
00491 
00492         
00508     function SetFormValue($name,$value)
00509     {   
00510         $value = MForm::EscapeValue($value);
00511         if (isset($this->$name))
00512         {
00513             $this->$name->SetValue($value);
00514         }
00515         $_REQUEST[$name] = $value;
00516     }
00517     
00518     
00528     private function /* PRIVATE */ EscapeValue($value)
00529     {
00530         if ( is_array($value) )
00531         {
00532             for ( $i=0, $n = count($value); $i < $n; $i++ )
00533             {
00534                 $value[$i] = $this->EscapeValue($value[$i]);
00535             }
00536         }
00537         else
00538         {
00539             $value = str_replace('\"','&quot;',$value);
00540             $value = str_replace('"','&quot;',$value);
00541         }
00542         return $value;
00543     }
00544 
00545     
00565     function OnSubmit($jscode)
00566     {
00567         $this->page->OnSubmit($jscode);
00568     }
00569 
00570     
00580     function AddJsCode($jscode)
00581     {
00582         $this->page->AddJsCode($jscode);
00583     }
00584     
00585     
00600     function SetAction($action)
00601     {
00602         $this->action = $action;
00603     }
00604     
00605     
00620     function setHelp($help, $module=null, $action=null)
00621     {
00622         if ($this->box->boxTitle != NULL)
00623         {
00624             if ( $module != null &&
00625                  $action != null )
00626             {
00627                 $class = $help;
00628                 $content = "MIOLO_getHelp( '$class', '$module', '$action');";
00629             }
00630             else
00631             {
00632                 $content = $help;
00633             }
00634 
00635             $this->box->boxTitle->setHelp($content);
00636         }
00637         else
00638         {
00639             $this->help = $help;
00640         }
00641     }
00642 
00652     private function /*PRIVATE*/ GetFields()
00653     {
00654         return $this->fields;
00655     }
00656     
00657     
00670     function SetFields(&$fields)
00671     {
00672 //        $this->fields = $fields; // inicializa a lista de campos
00673 //        $this->_RegisterField($this->fields);
00674         $this->fields = array();
00675         if (!is_array($fields)) $fields = array($fields);
00676         $this->layout = $fields;
00677         $this->_RegisterField($fields);
00678     }
00679     
00680     
00696     private function /*PRIVATE*/ _RegisterField($field)
00697     {   
00698         if ( is_array($field) )
00699         {
00700             for ( $i=0, $n = count($field); $i < $n; $i++ )
00701             {
00702                 $this->_RegisterField($field[$i]);
00703             }
00704         }
00705         elseif ($field instanceof MDiv)
00706         {
00707             $namefield = $field->id;
00708             $this->$namefield = $field;
00709 
00710             $field = $field->getInner();
00711 
00712             if(is_array($field) || is_object($field) )
00713             {
00714                 return $this->_registerField($field);
00715             }
00716         }
00717         else
00718         {
00719             $field->form = $this;
00720             $className = $field->className;
00721             if ($field instanceof MFileField)
00722             {
00723                 $this->enctype='multipart/form-data';
00724                 $this->page->setEnctype($this->enctype);
00725             }
00726             if ($field->name == $field->id)
00727             {
00728                 $namefield = $field->name;
00729             }
00730             else
00731             {
00732                 $namefield = $field->id;
00733             }
00734             $this->manager->Assert(!isset($this->$namefield), "Err: property [$namefield] already in use in the form [$this->title]! Choose another name to [$namefield].");
00735             $this->$namefield = $field;
00736 
00737 
00738             if ($field instanceof MFormControl)
00739             {
00740                 $value = $this->page->Request($field->name);
00741                 if ( ($field instanceof MCheckBox) || ($field instanceof MRadioButton) )
00742                 {
00743                     // set checked flag of checkbox or radiobutton if the value matches
00744                     $field->checked = $this->page->isPostBack() ? (isset($value) ? ($value == $field->value) : $field->checked) : $field->checked;
00745                 }
00746                 elseif ( ($field instanceof MIndexedControl) )
00747                 {
00748                     $this->AddFields($field->controls);
00749                     $field->SetValue($value);
00750                 }
00751                 elseif (($field instanceof MInputGrid))
00752                 {
00753                     $field->SetValue($value);
00754                 }
00755                 elseif ( $field->value == '' )
00756                 {
00757                     $field->SetValue($value);
00758                     if ($field instanceof MContainer)
00759                     {
00760                         $this->_RegisterField($field->GetControls());
00761                     }
00762                     else
00763                     {
00764                         $field->SetValue($this->EscapeValue($field->value));
00765                     }
00766                 }
00767             }
00768          }
00769     }
00770     
00771     
00791     function AddField($field,$hint=false)
00792     {
00793         if ( $hint )
00794         {
00795             $field->SetHint($hint);
00796         }
00797         $this->_RegisterField($field);
00798         $this->layout[] = $field;
00799 //        $this->fields[] = $field;
00800     }
00801 
00802     
00812     function AddFields($fields)
00813     {
00814         if (is_array($fields))
00815         {
00816            foreach($fields as $f)
00817               $this->AddField($f);
00818         }
00819     }
00820     
00821     
00843     function AddButton($btn)
00844     {
00845         if (strtoupper($btn->action == 'REPORT'))
00846         {
00847            $this->page->hasReport = true;
00848         }
00849         $this->buttons[] = $this->{$btn->GetId()} = $btn;
00850     }
00851 
00852     
00862     function SetButtons($btn)
00863     {
00864         if ( is_array($btn) )
00865         {
00866             for ( $i=0, $n = count($btn); $i < $n; $i++ )
00867             {
00868                 $this->SetButtons($btn[$i]);
00869             }
00870         }
00871         else
00872         {
00873            $this->AddButton($btn);
00874         }
00875     }
00876 
00885     public function setButtonLabel( $index, $label )
00886     {
00887         $this->buttons[$index]->label = $label;
00888     }
00889 
00893     public function showReturn( $state )
00894     {
00895         $this->setShowReturnButton( $state );
00896     }
00897 
00904     public function setShowReturnButton( $state )
00905     {
00906         $this->return = $state;
00907     }
00908 
00915     public function setShowPostButton( $state )
00916     {
00917         $this->defaultButton = $state;
00918     }
00919 
00923     public function showReset( $state )
00924     {
00925         $this->setShowResetButton( $state );
00926     }
00927 
00934     public function setShowResetButton( $state )
00935     {
00936         $this->reset = $state;
00937     }
00938 
00945     public function getShowHints()
00946     {
00947         return $this->showHints;
00948     }
00949 
00959     public function setShowHints( $state )
00960     {
00961         $this->showHints = $state;
00962     }
00963 
00967     public function showHints( $state )
00968     {
00969         $this->setShowHints ( $state );
00970     }
00971 
00972 
00986     function GetFieldList()
00987     {
00988         return $this->_GetFieldList($this->fields);
00989     }
00990 
01000     private function _GetFieldList($allfields)
01001     {
01002         $fields = array();
01003         foreach ($allfields as $f )
01004         {
01005             if ( is_array($f) )
01006             {
01007                 foreach ( $f as $a )
01008                 {
01009                     if (is_a($a,'BaseLabel')) continue;
01010                     $fields[] = $a;
01011                 }
01012             }
01013             else
01014             {
01015                 if ( is_a($f,'BaseLabel') || is_null($f->value) ) continue;
01016                 $fields[] = $f;
01017             }
01018         }
01019         return $fields;
01020     }
01021 
01022     function ClearFields()
01023     {
01024         $this->fields = NULL;
01025         $this->layout = NULL;
01026     }
01027 
01028     function ClearButtons()
01029     {
01030         $this->buttons = NULL;
01031     }
01032 
01045     function validateAll( $assert=true )
01046     {
01047         foreach ( $this->getFieldList() as $f )
01048         {
01049             if ( $f->name )
01050             {
01051                 $required[] = $f->name;
01052             }
01053         }
01054         return $this->validate($required, $assert);
01055     }
01056 
01066     function validateRequiredFields( $assert=true )
01067     {
01068         foreach ( $this->getFieldList() as $f )
01069         {
01070             if ( $f->required )
01071             {
01072                 $required[] = $f->name;
01073             }
01074         }
01075         return $this->Validate( $required, $assert );
01076     }
01077 
01091     function validate( $required, $assert=true )
01092     {   global $MIOLO,$HTTP_POST_VARS;
01093 
01094         $this->errors = array();
01095         // collect fields by label
01096         foreach ( $this->getFieldList() as $f )
01097         {
01098             $fields[$f->name] = $f->name;
01099         }
01100         foreach ( $required as $r )
01101         {
01102             $name  = $r;
01103             $label = $fields[$name];
01104             $MIOLO->Assert( $label,
01105                             "ERROR: Required field [<b><font color=red>$name</font></b>] is not defined in form!" );
01106 
01107             $value = $this->getFormValue( $name );
01108             if ( $value === '' || (is_null($value)) )
01109             {
01110                 $this->errors[] = "<b>$label</b> " . _M("was not informed!");
01111             }
01112         }
01113         if ( $assert && count($this->errors) )
01114         {
01115             $theme =& $MIOLO->getTheme();
01116             $theme->setContent( $this );
01117             $theme->generate();
01118         }
01119         return count( $this->errors ) == 0;
01120     }
01121 
01131     function Error( $err )
01132     {   global $MIOLO;
01133 
01134         $MIOLO->logMessage('[DEPRECATED] Call method Form::Error() is deprecated -- use Form::AddError() instead!');
01135         $this->addError( $err );
01136     }
01137 
01150     function AddError($err)
01151     {
01152         if ( $err )
01153         {
01154             if ( is_array($err) )
01155             {
01156                 if ( $this->errors )
01157                 {
01158                     $this->errors = array_merge($this->errors,$err);
01159                 }
01160                 
01161                 else
01162                 {
01163                     $this->errors = $err;
01164                 }
01165             }
01166             else
01167             {
01168                 $this->errors[] = $err;
01169             }
01170         }
01171     }
01172 
01173     
01184     function HasErrors()
01185     {
01186         return count($this->errors);
01187     }
01188     
01189     
01202     function AddInfo($info)
01203     {
01204         if ( $info )
01205         {
01206             if ( is_array($info) )
01207             {
01208                 if ( $this->infos )
01209                 {
01210                     $this->infos = array_merge($this->infos,$info);
01211                 }
01212                 
01213                 else
01214                 {
01215                     $this->infos = $info;
01216                 }
01217             }
01218             else
01219             {
01220                 $this->infos[] = $info;
01221             }
01222         }
01223     }
01224 
01225     
01236     function HasInfos()
01237     {
01238         return count($this->infos);
01239     }
01240     
01241     
01254     function CollectInput($data)
01255     {           
01256         foreach ( $this->GetFieldList() as $f )
01257         {
01258             $field = $f->name;
01259             if ( $field != '' )
01260             {
01261                   $value        = $this->GetFormValue($field); 
01262                   $data->$field = $value;
01263             }
01264         }
01265         return $data;
01266     }
01267 
01275     function getData()
01276     {
01277         return $this->collectInput( new FormData() );
01278     }
01279 
01294     function SetData($data)
01295     {
01296         $this->_SetData($data);
01297     }
01298     
01299     
01321     private function _SetData($data)
01322     {  
01323         foreach( $this->fields as $i=>$field) // ( $i=0, $n = count($this->fields); $i < $n; $i++ )
01324         {
01325             $name = $this->fields[$i]->name;
01326             if ( $name )
01327             {
01328                 if ( ($this->fields[$i] instanceof MRadioButton) || 
01329                      ($this->fields[$i] instanceof MCheckBox) )
01330                 {
01331                     $this->fields[$i]->checked = ( $data->$name == $this->fields[$i]->value );
01332                 }
01333                 else
01334                 { 
01335                     $value = $data->$name;
01336 
01337                     if ( method_exists($this->fields[$i], 'setValue') )
01338                     {
01339                         $this->fields[$i]->setValue($value);
01340                     }
01341 
01342                     $_POST[$name] = $value;
01343                 }
01344             }
01345         }
01346     }
01347     
01348     
01362     function GetFieldValue($name,$value=false)
01363     {   
01364         $field = $this->fields[$name];
01365         return ($field ? $field->GetValue() : NULL);
01366     }
01367     
01368     
01382     function SetFieldValue($name, $value)
01383     {   
01384         $field = $this->fields[$name];
01385 
01386         if( method_exists($field, 'setValue') )
01387         {
01388             $field->setValue( $value );
01389         }
01390     }
01391     
01392     
01406     function SetFieldValidator($name,$value)
01407     {   
01408         for ( $i=0, $n = count($field); $i < $n; $i++ )
01409         {
01410             if ( $name == $this->fields[$i]->name )
01411             {
01412                 $this->fields[$i]->validator = $value;
01413                 break;
01414             }
01415         }
01416     }
01417     
01418     
01422     function & GetField($name)
01423     {
01424         return $this->fields[$name];
01425     }
01426     
01427     
01431     function & GetButton($name)
01432     {
01433         for ( $i=0, $n = count($this->buttons); $i < $n; $i++ )
01434         {
01435             if ( $name == $this->buttons[$i]->name )
01436             {
01437                 return $this->buttons[$i];
01438             }
01439         }
01440     }
01441     
01442     
01446     function & GetPage()
01447     {
01448         return $this->page;
01449     }
01450     
01451     
01464     function SetPage(&$page)
01465     {
01466         $this->page = &$page;
01467     }
01468     
01469     
01484     function SetFieldAttr($name,$attr,$value)
01485     {   
01486 //      $field = $this->$name;
01487 //      $field->$attr = $value;
01488         $this->fields[$name]->$attr = $value; 
01489     }
01490     
01491     
01506     function GetFieldAttr($name,$attr, $index=NULL)
01507     {   
01508         $field = $this->fields[$name];
01509         if ( is_array($field->$attr) )
01510         {
01511             $a = $field->$attr;
01512             $value = $a[$index];
01513         }
01514         else
01515         {
01516           $value = $field->$attr;
01517         }
01518         return $value;
01519     }
01520     
01521     
01536     function SetButtonAttr($name,$attr,$value)
01537     {   
01538         $button = &$this->GetButton($name);
01539         $button->$attr = $value;
01540     }
01541     
01542     
01559     function SetFieldCSS($name,$top,$left,$width=NULL, $position='absolute')
01560     {   
01561         $field = $this->$name;
01562 //        $top = $this->box->top + $top;
01563 //        $left = $this->box->left + $left;
01564         if ($width) 
01565         {
01566             $field->width = "{$width}px";
01567         }
01568         $field->top      = "{$top}px";
01569         $field->left     = "{$left}px";
01570         if ($position) $field->position = $position;
01571         $field->formMode = 2;
01572     }
01573 
01574     
01575     
01590     function SetFormCSS($height=0, $width=0, $top=0, $left=0, $buttons=0, $position='absolute')
01591     {
01592         if ($height)   $this->box->AddStyle('height',"{$height}px");
01593         if ($width)    $this->box->AddStyle('width',"{$width}px");
01594         if ($top)      $this->box->AddStyle('top',"{$top}px");
01595         if ($left)     $this->box->AddStyle('left',"{$left}px");
01596         if ($position) $this->box->AddStyle('position',$position);
01597         $this->cssButtons = "{$buttons}px";
01598         $this->cssForm    = true;
01599     }
01600     
01601     function SetBackgroundColor($bgcolor)
01602     {
01603         $this->bgColor = $bgcolor; 
01604     }
01605 
01606     function SetAlign($value)
01607     {
01608         $this->align = $value;
01609     }
01610     
01611     function SetWidth($width=NULL)
01612     { 
01613         if ($width) $this->box->AddStyle('width',"{$width}");
01614     }
01615     
01616     function SetHeight($height=NULL)
01617     { 
01618         if ($height) $this->box->AddStyle('height',"{$height}");
01619     }
01632    function SetLabelWidth($width)
01633    {
01634        $this->labelWidth = $width; 
01635    } 
01636    
01644    function GenerateErrors()
01645    {    
01646         $prompt = Prompt::Error($this->errors,'NONE','Erros');
01647         return $prompt;
01648    }
01649    
01650    
01658    function GenerateInfos()
01659    {    
01660         $prompt = Prompt::Information($this->infos,'NONE','Informações');
01661         return $prompt;
01662    }
01663    
01664     
01672    function GenerateBody()
01673    {  
01674         global $MIOLO;
01675 
01676         $row = 0;
01677         $t = array();
01678         // optionally generate errors
01679         if ( $this->HasErrors() )
01680         {
01681             $t[] = $this->GenerateErrors();
01682         }
01683         if ( $this->HasInfos() )
01684         {
01685             $t[] = $this->GenerateInfos();
01686         }
01687         $hidden = null;
01688         $t = array_merge($t, $this->GenerateLayoutFields($hidden));
01689         
01690         if( method_exists($this->page,'getLayout') )
01691         {
01692             $layout = $this->page->theme->GetLayout();
01693         }
01694         else
01695         {
01696             $layout = $this->manager->theme->GetLayout();
01697         }
01698         
01699         if ( $layout != 'print')
01700         {
01701            $buttons = $this->GenerateButtons();
01702            if ($buttons)
01703            {
01704               $t = array_merge($t,array($buttons));
01705            }
01706         }
01707         if ( $hidden )
01708         {
01709            $t = array_merge($t,$this->GenerateHiddenFields($hidden));
01710         }
01711         $t = array_merge($t,$this->GenerateScript());
01712 //        $t[] = new MDiv('','&nbsp;','m-spacer');
01713         $body = new MDiv('',$t);
01714         return $body;
01715    }
01716    
01717     
01725    function GenerateFooter()
01726    {
01727    }
01728    
01729     
01739    function GenerateLayoutFields(&$hidden)
01740    {  
01741        $line = 0;  
01742        $zebra = is_array($this->zebra);
01743        $t = array();
01744        if (is_array($this->layout))
01745        {
01746            foreach ( $this->layout as $f )
01747            {
01748                if ( $f->validator != NULL)
01749                {
01750                     if ($f->validator instanceof mValidator)
01751                     {
01752                         $this->AddValidator($f->validator);
01753                     }
01754                }
01755                elseif ( $f instanceof mformcontrol )
01756                {
01757                     $f->validator = $this->getFieldValidator($f->name);
01758                }
01759                $row = $t[] = $this->GenerateLayoutField($f, $hidden);
01760                if ($zebra)
01761                {
01762                    $row->AddStyle('backgroundColor', $this->zebra[($line++) % 2]);
01763                }
01764            }
01765        }
01766        return $t;
01767    }
01768 
01769     public function getFieldValidator($name)
01770     {
01771         foreach($this->validations as $validator)
01772         {
01773             if( $validator && $validator->field == $name )
01774             {
01775                 return $validator;
01776             }
01777         }
01778         return false;
01779     }
01780 
01781    
01792    function GenerateLayoutField($f, &$hidden)
01793    {  
01794        if ( is_array($f) )
01795        {
01796           $c = array();
01797           foreach($f as $fld)
01798           {
01799              if ($fld->visible) $c[] = $fld;
01800           }
01801           $f = new MHContainer('',$c);
01802           $f->showLabel = true;
01803        }
01804        if ( ! $f->visible ) return;
01805        $rowContent = NULL;
01806        $label = $f->label;
01807        if ( ( ( ($f->className == 'textfield') || ($f->className == 'mtextfield'))  && ($f->size==0) ) || ($f instanceof  MHiddenField) )
01808        {
01809           $hidden[] = $f;
01810           return;
01811        }
01812        if ( $f->maintainstate )
01813        {
01814           $hidden[] = $f;
01815        }
01816        if ( $f->cssp )
01817        {
01818           return $f;
01819        }
01820    
01821 //       if ( ($f->formMode == 0) || (($f->formMode == 1) && (! $label)))
01822        if (($f->formMode != 1) || (($f->formMode == 1) && (! $label)))
01823        {
01824           $rowContent = $f;
01825        }
01826        else
01827        {
01828           if ( $label != '' && $label != '&nbsp;' )
01829           {
01830               $label .= ':';
01831           }
01832           $tf = array();
01833           if ($label != '') 
01834           { 
01835              if ($f->id != '')
01836              { 
01837                 $lbl = new MFieldLabel($f->id,$label);
01838                 $lbl->setClass('m-caption');
01839              }
01840              else
01841              {
01842                 $lbl = new MSpan('',$label,'m-caption');
01843              }
01844              if($f->validator && $f->validator->type == 'required')
01845              {
01846                 $lbl->setClass('m-caption-required');
01847              }
01848 
01849              $slbl = $rowContent[] = new MSpan('',$lbl,'label');
01850              if ($this->labelWidth != NULL)
01851              {
01852                  //$slbl->_AddStyle('width',$this->labelWidth.'%');
01853                  $slbl->_AddStyle('width',$this->labelWidth.'px');
01854              }
01855           }
01856           if ( $this->showHints && $f->hint )
01857           {
01858              $hint = new MSpan('',$f->hint,'m-hint');
01859              $sfld = $rowContent[] = new MSpan('',array($f,'&nbsp;',$hint),'field');
01860           }
01861           else
01862           {
01863              $sfld = new MSpan('',$f,'field');
01864              $rowContent[] = $sfld;
01865           }
01866           if ($this->labelWidth != NULL)
01867           {
01868              //$sfld->_AddStyle('width', (95 - $this->labelWidth).'%');
01869           }
01870        }
01871        return new MDiv('',$rowContent, "m-form-row");
01872     }
01873 
01874     
01882     function GenerateButtons()
01883     {
01884            $ul = new MUnorderedList();
01885            if (isset($this->buttons) )
01886            {
01887               $ul->AddOption(new MHr);
01888               foreach ( $this->buttons as $b )
01889               {
01890                  if ($b->visible) $ul->AddOption($b);
01891               }
01892            }
01893            if ( $this->reset )
01894            {
01895               $ul->AddOption(new MButton('_reset','Limpar','RESET'));
01896            }
01897            if ( $this->return )
01898            {
01899               $ul->AddOption(new MButton('_return','Voltar','RETURN'));
01900            }
01901            $d = (count($ul->options) ? new MDiv('',$ul,'m-form-button-box') : NULL);
01902            return ($d ? new MDiv('',$d,'m-form-row') : NULL);
01903     }
01904 
01905 
01915     function GenerateHiddenFields($hidden)
01916     {
01917         $f[] = "\n<!-- START OF HIDDEN FIELDS -->\n";
01918         foreach ( $hidden as $h )
01919         {
01920             $f[] = new HiddenField($h->name,$h->value);
01921         }
01922         $f[] = "\n<!-- END OF HIDDEN FIELDS -->";
01923         return $f;
01924     }
01925 
01926     
01937     function GenerateScript()
01938     {
01939         $f = array();
01940         if ( $this->validations )
01941         {
01942             // we generate the form onSubmit handler right in the body of the form
01943             $f[] = "\n<!-- START OF FORM SCRIPT CODE -->\n";
01944             $f[] =  "<script language=\"JavaScript\">\n";
01945 //            echo "document.{$this->name}.elements[0].focus();\n";
01946             $this->OnSubmit("MIOLO_Validate_Input({$this->name}_validations)");
01947             $f[] =  "\n";
01948             $f[] =  "/*\n";
01949             $f[] =  " * MIOLO Form Validation Objects\n";
01950             $f[] =  "*/\n";
01951             foreach ( $this->validations as $v )
01952             {
01953                 $f[] = $v;
01954                 $f[] = "\n";
01955             }
01956             $f[] = "var {$this->name}_validations = Array( ";
01957             $i   = 0;
01958             foreach ( $this->validations as $v )
01959             {
01960                 if ( $i++ )
01961                 {
01962                     $f[] = ",\n                                ";
01963                 }
01964                 $f[] = $v->name;
01965             }
01966             $f[] = " );\n";
01967             $f[] = "\n";
01968             $f[] = "</script>\n";
01969             $f[] = "<!-- END OF FORM SCRIPT CODE -->\n";
01970         }
01971         return $f;
01972     }
01973 
01974     function Generate()
01975     {
01976         if (!isset($this->buttons))
01977         {
01978             if ($this->defaultButton)
01979             {
01980                 $this->buttons[] = new FormButton(FORM_SUBMIT_BTN_NAME, 'Enviar', 'SUBMIT');
01981             }
01982         }
01983         $footer = $this->GenerateFooter();
01984         if ($this->cssForm)
01985         {
01986             $body = $this->GenerateBody();
01987             $this->box->SetControls(array($body));
01988             $this->box->SetBoxClass('m-form-css');
01989             return $this->box->Generate();
01990         }
01991         else
01992         {
01993             $body = new MDiv('',$this->GenerateBody(),'m-form-body');
01994             if (!is_null($this->bgColor)) $body->AddStyle('backgroundColor',$this->bgColor);
01995             $this->box->SetControls(array($body, $footer));
01996             $id   = $this->GetUniqueId();
01997             $this->box->SetBoxClass("m-form-outer");
01998             $form = new MDiv("frm$id", $this->box,"m-form-box");
01999             if (!is_null($this->align)) $form->AddBoxStyle('text-align',$this->align);
02000             return $form->Generate();
02001 //            return $this->box->Generate();
02002         }
02003     }
02004 }
02005 
02006 class FormData
02007 {
02008 }
02009 ?>
CopyLeft (L) 2001-2006 - [MIOLO Development Team] SOLIS - Cooperativa de Soluções Livres - Lajeado/RS - Brasil