MIOLO20
Carregando...
Procurando...
Nenhuma entrada encontrada
mform.class
Ir para a documentação deste ficheiro.
1<?php
2define ('FORM_SUBMIT_BTN_NAME', 'submit_button');
3
8class MForm extends MControl
9{
13 var $title;
14
19
24
29
33 var $fields=array();
34
39
43 var $reset;
44
49
53 var $help;
54
59
63 var $width;
64
68 public static $showHints = true;
69
74
79
84
89
93 var $infos;
94
100
104 var $box;
105
106 static $fieldNum = 0;
111
115 var $cssForm = false;
120 var $zebra = false;
124 var $labelWidth = NULL;
125
126 var $bgColor = NULL;
127 var $align = NULL;
128
133
138
144 private $formControlFields = array();
145
155 function __construct($title='',$action='',$close='',$icon='', $help='')
156 {
157 parent::__construct();
158 $this->AddStyleFile('m_forms.css');
159 $this->name = $this->page->name;
160 $this->box = new MBox($title,$close,$icon,$help);
161 $this->title = $title;
162 $this->action = $action;
163 $this->method = 'post';
164 $this->return = false;
165 $this->width = '95%';
166 $this->defaultButton = true;
167 $this->fields = array();
168 $this->validations = array();
169
170 // Adiciona campo oculto para armazenar o randomid gerado da tela, utilizado para não conflitar dados.
171 $this->setRandomField();
172
173 $this->CreateFields();
174 if ($this->IsSubmitted())
175 {
176 $this->GetFormFields(); // set the fields array with form post/get values
177 }
178 $this->onLoad();
179 }
180
186 public function getRandomField()
187 {
189 $randomId = (Integer) $MIOLO->_REQUEST('randomid');
190
191 if ( empty($randomId) )
192 {
193 $randomId = rand();
194 $_REQUEST["randomid"] = (Integer) $randomId;
195 }
196
197 return new MHiddenField('randomid', $randomId);
198 }
199
203 public function setRandomField()
204 {
205 $fieldRandom = $this->getRandomField();
206 $this->_RegisterField($fieldRandom);
207 $this->layout[] = $fieldRandom;
208 }
209
220 function __set($name, $value)
221 {
222 $this->AddControl($value);
223 $this->fields[$name] = $value;
224 }
225
226
236 function __get($name)
237 {
238 return $this->fields[$name] ?? null;
239 }
240
241 public function getValidations()
242 {
243 return $this->validations;
244 }
245
247 {
248 $this->validations = $validations;
249 }
250
258 function OnLoad()
259 {
260 }
261
262
270 function CreateFields()
271 {
272 }
273
274
284 function AddValidator($validator)
285 {
286 $field = $this->{$validator->field};
287
288 if ( !$field )
289 {
290 $field = $this->getFieldFromFields($validator->field, $this->fields);
291 if( isset($field) )
292 {
293 $field->validator = $validator;
294 }
295 }
296
297 if ( !$validator->label )
298 {
299 $validator->label = ( $validator->label == '' ? ( $field->label ?? '' ) : $validator->label );
300 }
301
302 $name = '_validator_' . $this->name . '_' . $validator->id . '_' . $validator->field;
303 $validator->name = strtr( $name, array('['=>'_', ']'=>'_'));
304 $validator->form = $this->name;
305 $validator->max = $validator->max ?? 0;
306 $this->validations[] = $validator;
307 return $name;
308 }
309
314 public function getFormControlFields()
315 {
316 return $this->formControlFields;
317 }
318
326 public function getFieldFromFields($fieldName, $fields)
327 {
328 $field = NULL;
329
330 foreach ( (array) $fields as $f )
331 {
332 if ( $f instanceof MTabbedBaseGroup )
333 {
334 $field = $this->getFieldFromFields($fieldName, $f->getTabs());
335 }
336 elseif ( $f instanceof MContainer )
337 {
338 $field = $this->getFieldFromFields($fieldName, $f->getControls());
339 }
340 elseif ( $f instanceof MDiv )
341 {
342 $field = $this->getFieldFromFields($fieldName, $f->getInner());
343 }
344 elseif ( is_object($f) && ($f->name == $fieldName) )
345 {
346 $field = $f;
347 break;
348 }
349
350 if ( $field )
351 {
352 break;
353 }
354 }
355
356 return $field;
357 }
358
368 function SetValidators($validators)
369 {
370 if (is_array($validators))
371 {
372 foreach($validators as $v)
373 {
374 if ( !is_null($v) )
375 {
376 $this->AddValidator($v);
377 }
378 }
379 }
380 elseif (is_subclass_of( $validators, 'validator'))
381 {
382 $this->AddValidator($validators);
383 }
384 }
385
386
399 function IsSubmitted()
400 {
401 $isSubmitted = $this->defaultButton && MForm::getFormValue(FORM_SUBMIT_BTN_NAME);
402 if (isset($this->buttons))
403 {
404 foreach($this->buttons as $b)
405 {
406 $isSubmitted = $isSubmitted || MForm::GetFormValue($b->name);
407 }
408 }
409 return $isSubmitted;
410 }
411
412
427 function GetTitle()
428 {
429 return $this->title;
430 }
431
432
445 function SetTitle($title)
446 {
447 $this->title = $title;
448 $this->caption = $title;
449 $this->box->SetCaption($title);
450 }
451
465 {
466 if ($this->box->boxTitle != NULL)
467 {
468 $this->box->boxTitle->SetClose($action);
469 }
470 }
471
472
485 public function SetIcon($icon)
486 {
487 if ($this->box->boxTitle != NULL)
488 {
489 $this->box->boxTitle->SetIcon($icon);
490 }
491 }
492
493 function SetAlternate($color0, $color1)
494 {
495 $this->zebra = array($color0, $color1);
496 }
497
498
513 function GetFooter()
514 {
515 return $this->footer;
516 }
517
526 {
527 $this->footer = $footer;
528 }
529
530
542 function GetFormFields()
543 {
544 $this->_GetFormFieldValue($this->fields);
545 }
546
547
557 function _GetFormFieldValue($field)
558 {
559 if ( is_array($field) )
560 {
561 foreach($field as $f)
562 {
563 $this->_GetFormFieldValue($f);
564 }
565 }
566 else
567 {
568 if ($field instanceof MFormControl)
569 {
570 if ( $field->name )
571 {
572 $defvalue = $field->GetValue();
573 $value = $this->page->Request($field->name);
574 if ( ($field instanceof MCheckBox) || ($field instanceof MRadioButton) )
575 {
576 $field->checked = (isset($value) ? ($value == $field->value) : $field->checked);
577 }
578 else
579 {
580 $field->SetValue(isset($value) ? $value : $defvalue);
581 }
582 }
583 }
584 }
585 }
586
587
603 function GetFormValue($name = null ,$value=NULL)
604 {
605 $result = '';
606 if ( ($name != '') && ((strpos($name,'[')) === false))
607 {
608 $result = MIOLO::_REQUEST($name, 'ALL');
609 }
610 if (! isset($result))
611 {
612 $result = $value;
613 }
614
615 return $result;
616 }
617
618
630 function SetFormValue($name,$value)
631 {
632 $value = $this->EscapeValue($value);
633
634 if ( $this->$name )
635 {
636 $this->$name->SetValue($value);
637 }
638
639 $_REQUEST[$name] = $value;
640 }
641
642
652 private function /* PRIVATE */ EscapeValue($value)
653 {
654 if ( is_array($value) )
655 {
656 for ( $i=0, $n = count($value); $i < $n; $i++ )
657 {
658 $val = htmlentities($value[$i], ENT_QUOTES);
659 $value[$i] = $this->EscapeValue($val);
660 }
661 }
662 else
663 {
664 $value = str_replace('\"','&quot;',$value);
665 $value = str_replace('"','&quot;',$value);
666 $value = htmlentities($value, ENT_QUOTES);
667 }
668
669 return $value;
670 }
671
672
692 function OnSubmit($jscode)
693 {
694 $this->page->OnSubmit($jscode);
695 }
696
697
707 function AddJsCode($jscode)
708 {
709 $this->page->AddJsCode($jscode);
710 }
711
712
728 {
729 $this->action = $action;
730 }
731
732
747 function setHelp($help, $module=null, $action=null)
748 {
750 if ($this->box->boxTitle != NULL)
751 {
752 if ( $action != null )
753 {
754 //Verifica actions de relatorios generic e generate
755 $action = $this->normalizaActionParaConsulta($action);
756 $content = MTransaction::getNameTransaction($action, $module);
757
758 if ( strlen($content) > 0 )
759 {
760 $content = $MIOLO->getConf('options.urlHelp').$content;
761 }
762 }
763 else
764 {
765 $content = $help;
766 }
767
768 $this->box->boxTitle->setHelp($content);
769 }
770 else
771 {
772 $this->help = $help;
773 }
774 }
775
785 private function /*PRIVATE*/ GetFields()
786 {
787 return $this->fields;
788 }
789
790
804 {
805 $this->fields = array();
806 if (!is_array($fields)) $fields = array($fields);
807 $this->layout = $fields;
808 $this->_RegisterField($fields);
809
810 $this->setRandomField();
811 }
812
813
829 private function _RegisterField($field, $isNotFormControl = false)
830 {
831 if ( is_array($field) )
832 {
833 for ( $i=0, $n = count($field); $i < $n; $i++ )
834 {
835 $this->_RegisterField($field[$i], $isNotFormControl);
836 }
837 }
838 elseif ($field instanceof MDiv)
839 {
840 $namefield = $field->id;
841 $this->$namefield = $field;
842
843 $field = $field->getInner();
844
845 if(is_array($field) || is_object($field) )
846 {
847 return $this->_registerField($field, $isNotFormControl);
848 }
849 }
850 else
851 {
852 $field instanceof MFormControl;
853 $field->form = $this;
854
855 if ( method_exists($field, 'setFieldForm') )
856 {
857 $field->setFieldForm($this);
858 $this->formControlFields[ $field->getName() ] = $field;
859 }
860
861 $className = $field->className;
862 if ($field instanceof MFileField)
863 {
864 $this->enctype='multipart/form-data';
865 $this->page->setEnctype($this->enctype);
866 }
867 if ($field->name == $field->id)
868 {
869 $namefield = $field->name;
870 }
871 else
872 {
873 $namefield = $field->id;
874 }
875 $this->manager->Assert(!isset($this->$namefield), "Err: property [$namefield] already in use in the form [$this->title]! Choose another name to [$namefield].");
876 $this->$namefield = $field;
877
878
879 if ($field instanceof MFormControl)
880 {
881 $value = $this->page->Request($field->name);
882 if ( ($field instanceof MCheckBox) || ($field instanceof MRadioButton) )
883 {
884 // set checked flag of checkbox or radiobutton if the value matches
885 $field->checked = $this->page->isPostBack() ? ($value == $field->value) : $field->checked;
886 }
887 elseif ( ($field instanceof MIndexedControl) )
888 {
889 $this->AddFields($field->controls);
890 $field->SetValue($value);
891 }
892 elseif (($field instanceof MInputGrid))
893 {
894 $field->SetValue($value);
895 }
896 elseif ( $field->value == '' )
897 {
898 $field->SetValue($value);
899 if ($field instanceof MContainer)
900 {
901 $this->_RegisterField($field->GetControls(), true);
902 }
903 else
904 {
905 $field->SetValue($this->EscapeValue($field->value));
906 }
907 }
908 }
909 }
910 }
911
912
932 function AddField($field,$hint=false)
933 {
934 if ( $hint )
935 {
936 $field->SetHint($hint);
937 }
938 $this->_RegisterField($field);
939 $this->layout[] = $field;
940// $this->fields[] = $field;
941 }
942
943
954 {
955 if (is_array($fields))
956 {
957 foreach($fields as $f)
958 $this->AddField($f);
959 }
960 }
961
962
984 function AddButton($btn)
985 {
986 if (strtoupper($btn->action == 'REPORT'))
987 {
988 $this->page->hasReport = true;
989 }
990 $this->buttons[] = $this->{$btn->GetId()} = $btn;
991 }
992
993
1003 function SetButtons($btn)
1004 {
1005 if ( is_array($btn) )
1006 {
1007 for ( $i=0, $n = count($btn); $i < $n; $i++ )
1008 {
1009 $this->SetButtons($btn[$i]);
1010 }
1011 }
1012 else
1013 {
1014 $this->AddButton($btn);
1015 }
1016 }
1017
1026 public function setButtonLabel( $index, $label )
1027 {
1028 $this->buttons[$index]->label = $label;
1029 }
1030
1034 public function showReturn( $state )
1035 {
1036 $this->setShowReturnButton( $state );
1037 }
1038
1045 public function setShowReturnButton( $state )
1046 {
1047 $this->return = $state;
1048 }
1049
1056 public function setShowPostButton( $state )
1057 {
1058 $this->defaultButton = $state;
1059 }
1060
1064 public function showReset( $state )
1065 {
1066 $this->setShowResetButton( $state );
1067 }
1068
1075 public function setShowResetButton( $state )
1076 {
1077 $this->reset = $state;
1078 }
1079
1086 public static function getShowHints()
1087 {
1088 return self::$showHints;
1089 }
1090
1100 public static function setShowHints($state)
1101 {
1102 self::$showHints = $state;
1103 }
1104
1108 public function showHints($state)
1109 {
1110 self::setShowHints($state);
1111 }
1112
1113
1127 function GetFieldList()
1128 {
1129 return $this->_GetFieldList($this->fields);
1130 }
1131
1141 private function _GetFieldList($allfields)
1142 {
1143 $fields = array();
1144 foreach ($allfields as $f )
1145 {
1146 if ( is_array($f) )
1147 {
1148 foreach ( $f as $a )
1149 {
1150 if (is_a($a,'BaseLabel')) continue;
1151 $fields[] = $a;
1152 }
1153 }
1154 else
1155 {
1156 if ( is_a($f,'BaseLabel') || is_null($f->value) ) continue;
1157 $fields[] = $f;
1158 }
1159 }
1160 return $fields;
1161 }
1162
1163 function ClearFields()
1164 {
1165 $this->fields = NULL;
1166 $this->layout = NULL;
1167 }
1168
1169 function ClearButtons()
1170 {
1171 $this->buttons = NULL;
1172 }
1173
1186 function validateAll( $assert=true )
1187 {
1188 foreach ( $this->getFieldList() as $f )
1189 {
1190 if ( $f->name )
1191 {
1192 $required[] = $f->name;
1193 }
1194 }
1195 return $this->validate($required, $assert);
1196 }
1197
1207 function validateRequiredFields( $assert=true )
1208 {
1209 foreach ( $this->getFieldList() as $f )
1210 {
1211 if ( $f->required )
1212 {
1213 $required[] = $f->name;
1214 }
1215 }
1216 return $this->Validate( $required, $assert );
1217 }
1218
1232 function validate( $required, $assert=true, $args = NULL)
1233 { global $MIOLO,$HTTP_POST_VARS;
1234
1235 $this->errors = array();
1236 // collect fields by label
1237 foreach ( $this->getFieldList() as $f )
1238 {
1239 $fields[$f->name] = $f->name;
1240 }
1241 foreach ( $required as $r )
1242 {
1243 $name = $r;
1244 $label = $fields[$name];
1245 $MIOLO->Assert( $label,
1246 "ERROR: Required field [<b><font color=red>$name</font></b>] is not defined in form!" );
1247
1248 $value = $this->getFormValue( $name );
1249 if ( $value === '' || (is_null($value)) )
1250 {
1251 $this->errors[] = "<b>$label</b> " . _M("was not informed!");
1252 }
1253 }
1254 if ( $assert && count($this->errors) )
1255 {
1256 $theme =& $MIOLO->getTheme();
1257 $theme->setContent( $this );
1258 $theme->generate();
1259 }
1260 return count( $this->errors ) == 0;
1261 }
1262
1272 function Error( $err )
1273 { global $MIOLO;
1274
1275 $MIOLO->logMessage('[DEPRECATED] Call method Form::Error() is deprecated -- use Form::AddError() instead!');
1276 $this->addError( $err );
1277 }
1278
1291 function AddError($err)
1292 {
1293 if ( $err )
1294 {
1295 if ( is_array($err) )
1296 {
1297 if ( $this->errors )
1298 {
1299 $this->errors = array_merge($this->errors,$err);
1300 }
1301
1302 else
1303 {
1304 $this->errors = $err;
1305 }
1306 }
1307 else
1308 {
1309 $this->errors[] = $err;
1310 }
1311 }
1312 }
1313
1314
1325 function HasErrors()
1326 {
1327 return $this->errors ? count($this->errors) : 0;
1328 }
1329
1330
1343 function AddInfo($info)
1344 {
1345 if ( $info )
1346 {
1347 if ( is_array($info) )
1348 {
1349 if ( $this->infos )
1350 {
1351 $this->infos = array_merge($this->infos,$info);
1352 }
1353
1354 else
1355 {
1356 $this->infos = $info;
1357 }
1358 }
1359 else
1360 {
1361 $this->infos[] = $info;
1362 }
1363 }
1364 }
1365
1366
1377 function HasInfos()
1378 {
1379 return $this->infos ? count($this->infos) : 0;
1380 }
1381
1391 function AddAlert($alert)
1392 {
1393 if ( $alert )
1394 {
1395 if ( is_array($alert) )
1396 {
1397 if ( $this->alerts )
1398 {
1399 $this->alerts = array_merge($this->alerts,$alert);
1400 }
1401
1402 else
1403 {
1404 $this->alerts = $alert;
1405 }
1406 }
1407 else
1408 {
1409 $this->alerts[] = $alert;
1410 }
1411 }
1412 }
1413
1414
1427 function HasAlerts()
1428 {
1429 return $this->alerts ? count($this->alerts) : 0;
1430 }
1431
1432
1433
1446 function CollectInput($data)
1447 {
1448 foreach ( $this->GetFieldList() as $f )
1449 {
1450 $field = $f->name;
1451 if ( $f instanceof MSubDetail )
1452 {
1453 $data->$field = MSubDetail::getData($field);
1454 }
1455 else
1456 {
1457 if ( $field != '' )
1458 {
1459 $value = $this->GetFormValue($field);
1460 $data->$field = $value;
1461 }
1462 }
1463 }
1464 return $data;
1465 }
1466
1474 function getData()
1475 {
1476 return $this->collectInput( new FormData() );
1477 }
1478
1482 public function getRequestData()
1483 {
1484 return (object) $_REQUEST;
1485 }
1486
1501 function SetData($data)
1502 {
1503 $this->_SetData($data);
1504 }
1505
1506
1528 private function _SetData($data)
1529 {
1530 foreach( $this->fields as $i=>$field) // ( $i=0, $n = count($this->fields); $i < $n; $i++ )
1531 {
1532 $name = $this->fields[$i]->name;
1533 if ( $name )
1534 {
1535 if ( ($this->fields[$i] instanceof MRadioButton) ||
1536 ($this->fields[$i] instanceof MCheckBox) )
1537 {
1538 $this->fields[$i]->checked = ( $data->$name == $this->fields[$i]->value );
1539 }
1540 elseif ( $field instanceof MSubDetail )
1541 {
1542 MSubDetail::setData($data->$name, $name);
1543 }
1544 else
1545 {
1546 $value = $data->$name;
1547
1548 if ( method_exists($this->fields[$i], 'setValue') )
1549 {
1550 $this->fields[$i]->setValue($value);
1551 }
1552
1553 $_POST[$name] = $value;
1554 }
1555 }
1556 }
1557 }
1558
1559
1573 function GetFieldValue($name,$value=false)
1574 {
1575 $field = $this->fields[$name];
1576 return ($field ? $field->GetValue() : NULL);
1577 }
1578
1579
1593 function SetFieldValue($name, $value)
1594 {
1595 $field = $this->fields[$name];
1596
1597 if( method_exists($field, 'setValue') )
1598 {
1599 $field->setValue( $value );
1600 }
1601 }
1602
1603
1617 function SetFieldValidator($name,$value)
1618 {
1619 for ( $i=0, $n = count($field); $i < $n; $i++ )
1620 {
1621 if ( $name == $this->fields[$i]->name )
1622 {
1623 $this->fields[$i]->validator = $value;
1624 break;
1625 }
1626 }
1627 }
1628
1629
1633 function & GetField($name)
1634 {
1635 return $this->fields[$name];
1636 }
1637
1638
1642 function & GetButton($name)
1643 {
1644 for ( $i=0, $n = count($this->buttons); $i < $n; $i++ )
1645 {
1646 if ( $name == $this->buttons[$i]->name )
1647 {
1648 return $this->buttons[$i];
1649 }
1650 }
1651 }
1652
1653
1657 function & GetPage()
1658 {
1659 return $this->page;
1660 }
1661
1662
1675 function SetPage(&$page)
1676 {
1677 $this->page = &$page;
1678 }
1679
1680
1695 function SetFieldAttr($name,$attr,$value)
1696 {
1697// $field = $this->$name;
1698// $field->$attr = $value;
1699 $this->fields[$name]->$attr = $value;
1700 }
1701
1702
1717 function GetFieldAttr($name,$attr, $index=NULL)
1718 {
1719 $field = $this->fields[$name];
1720 if ( is_array($field->$attr) )
1721 {
1722 $a = $field->$attr;
1723 $value = $a[$index];
1724 }
1725 else
1726 {
1727 $value = $field->$attr;
1728 }
1729 return $value;
1730 }
1731
1732
1747 function SetButtonAttr($name,$attr,$value)
1748 {
1749 $button = &$this->GetButton($name);
1750 $button->$attr = $value;
1751 }
1752
1753
1770 function SetFieldCSS($name,$top,$left,$width=NULL, $position='absolute')
1771 {
1772 $field = $this->$name;
1773// $top = $this->box->top + $top;
1774// $left = $this->box->left + $left;
1775 if ($width)
1776 {
1777 $field->width = "{$width}px";
1778 }
1779 $field->top = "{$top}px";
1780 $field->left = "{$left}px";
1781 if ($position) $field->position = $position;
1782 $field->formMode = 2;
1783 }
1784
1785
1786
1801 function SetFormCSS($height=0, $width=0, $top=0, $left=0, $buttons=0, $position='absolute')
1802 {
1803 if ($height) $this->box->AddStyle('height',"{$height}px");
1804 if ($width) $this->box->AddStyle('width',"{$width}px");
1805 if ($top) $this->box->AddStyle('top',"{$top}px");
1806 if ($left) $this->box->AddStyle('left',"{$left}px");
1807 if ($position) $this->box->AddStyle('position',$position);
1808 $this->cssButtons = "{$buttons}px";
1809 $this->cssForm = true;
1810 }
1811
1812 function SetBackgroundColor($bgcolor)
1813 {
1814 $this->bgColor = $bgcolor;
1815 }
1816
1817 function SetAlign($value)
1818 {
1819 $this->align = $value;
1820 }
1821
1822 function SetWidth($width=NULL)
1823 {
1824 if ($width) $this->box->AddStyle('width',"{$width}");
1825 }
1826
1827 function SetHeight($height=NULL)
1828 {
1829 if ($height) $this->box->AddStyle('height',"{$height}");
1830 }
1844 {
1845 $this->labelWidth = $width;
1846 }
1847
1856 {
1857 $prompt = Prompt::Error($this->errors,'NONE','Erros');
1858 return $prompt;
1859 }
1860
1861
1869 function GenerateInfos()
1870 {
1871 $prompt = Prompt::Information($this->infos,'NONE','Informações');
1872 return $prompt;
1873 }
1874
1883 {
1884 $prompt = Prompt::Alert($this->alerts,'NONE','Alertas');
1885 return $prompt;
1886 }
1887
1888
1889
1897 function GenerateBody()
1898 {
1899 global $MIOLO;
1900
1901 $row = 0;
1902 $t = array();
1903 // optionally generate errors
1904 if ( $this->HasErrors() )
1905 {
1906 $t[] = $this->GenerateErrors();
1907 }
1908 if ( $this->HasInfos() )
1909 {
1910 $t[] = $this->GenerateInfos();
1911 }
1912 if ( $this->HasAlerts() )
1913 {
1914 $t[] = $this->GenerateAlerts();
1915 }
1916 $hidden = null;
1917 $t = array_merge($t, $this->GenerateLayoutFields($hidden));
1918
1919 if( method_exists($this->page,'getLayout') )
1920 {
1921 $layout = $this->page->theme->GetLayout();
1922 }
1923 else
1924 {
1925 $layout = $this->manager->theme->GetLayout();
1926 }
1927
1928 if ( $layout != 'print')
1929 {
1930 $buttons = $this->GenerateButtons();
1931 if ($buttons)
1932 {
1933 $t = array_merge($t,array($buttons));
1934 }
1935 }
1936 if ( $hidden )
1937 {
1938 $t = array_merge($t,$this->GenerateHiddenFields($hidden));
1939 }
1940 $t = array_merge($t,$this->GenerateScript());
1941// $t[] = new MDiv('','&nbsp;','m-spacer');
1942 $body = new MDiv('',$t);
1943 return $body;
1944 }
1945
1946
1955 {
1956 }
1957
1958
1968 function GenerateLayoutFields(&$hidden)
1969 {
1970 $line = 0;
1971 $zebra = is_array($this->zebra);
1972 $t = array();
1973 if (is_array($this->layout))
1974 {
1975 $this->generateValidators($this->layout);
1976 foreach ( $this->layout as $f )
1977 {
1978 $row = $t[] = $this->GenerateLayoutField($f, $hidden);
1979 if ($zebra)
1980 {
1981 $row->AddStyle('backgroundColor', $this->zebra[($line++) % 2]);
1982 }
1983 }
1984 }
1985 return $t;
1986 }
1987
1989 {
1990 foreach ( $fields as $field )
1991 {
1992 if ( $field instanceof MContainer )
1993 {
1994 $this->generateValidators($field->getControls());
1995 }
1996 elseif ( $field instanceof MDiv )
1997 {
1998 $this->generateValidators((array)$field->getInner());
1999 }
2000 elseif ( $field->validator != NULL && $field->validator instanceof MValidator )
2001 {
2002 $this->addValidator($field->validator);
2003 $validator = $this->getFieldValidator($field->name);
2004
2005 if ( $validator )
2006 {
2007 $field->validator = $validator;
2008 }
2009 }
2010 elseif ( $field instanceof MFormControl )
2011 {
2012 $field->validator = $this->getFieldValidator($field->name);
2013 }
2014 }
2015 }
2016
2020 public function getFieldValidator($name)
2021 {
2022 foreach ( $this->validations as $validator )
2023 {
2024 if ( $validator && $validator->field == $name )
2025 {
2026 return $validator;
2027 }
2028 }
2029
2030 return false;
2031 }
2032
2038 public function getFieldValidators($name)
2039 {
2040 $list = array();
2041
2042 foreach( $this->validations as $validator )
2043 {
2044 if ( $validator && $validator->field == $name )
2045 {
2046 $list[] = $validator;
2047 }
2048 }
2049
2050 return $list;
2051 }
2052
2059 public function removeAJAXValidator($fieldId)
2060 {
2061 // Unique identifier. Remove [] to avoid javascript errors.
2062 $identifier = str_replace(']', '_', str_replace('[', '_', $fieldId));
2063
2064 $identifier = str_replace('.', '_', $identifier);
2065
2066 $this->page->addAJAXJsCode("delete window.MIOLO_validators.$identifier;");
2067 }
2068
2079 public function GenerateLayoutField($field, &$hidden)
2080 {
2081 if ( is_array($field) )
2082 {
2083 $c = array();
2084 foreach ( $field as $fld )
2085 {
2086 if ( $fld->visible )
2087 {
2088 $c[] = $fld;
2089 }
2090 }
2091 $field = new MHContainer('', $c);
2092 $field->showLabel = true;
2093 }
2094
2095 if ( !$field->visible )
2096 {
2097 return;
2098 }
2099
2100 $rowContent = NULL;
2101 $label = $field->label;
2102 if ( ( ( ($field->className == 'textfield') || ($field->className == 'mtextfield')) && ($field->size == 0) ) || ($field instanceof MHiddenField) )
2103 {
2104 $hidden[] = $field;
2105 return;
2106 }
2107 if ( $field->maintainstate )
2108 {
2109 $hidden[] = $field;
2110 }
2111 if ( $field->cssp )
2112 {
2113 return $field;
2114 }
2115
2116 if ( ($field->formMode != MFormControl::FORM_MODE_SHOW_SIDE) ||
2117 ( ($field->formMode == MFormControl::FORM_MODE_SHOW_SIDE) && (! $label)) )
2118 {
2119 $rowContent = $field;
2120 }
2121 else
2122 {
2123 //FIXME: Nao processar label aqui, ao invez disso chamar $field->generateLabel()
2124
2125 if ( $label != '' && $label != '&nbsp;' && $label[strlen($label)-1] != ':' )
2126 {
2127 $label .= ':';
2128 }
2129
2130 if ( $label != '' )
2131 {
2132 if ( $field->id != '' )
2133 {
2134 $lbl = new MFieldLabel($field->id, $label);
2135 $lbl->setClass(MControl::CLASS_CAPTION);
2136 }
2137 else
2138 {
2139 $lbl = new MSpan('', $label, MControl::CLASS_CAPTION);
2140 }
2141
2142 if ( $field->validator )
2143 {
2144 if ( $field->validator->type == 'required' )
2145 {
2146 $lbl->setClass( MControl::CLASS_CAPTION_REQUIRED );
2147 }
2148 elseif ( is_array($this->validations) )
2149 {
2150 // checks if there is at least one required validator to put
2151 // or not a required label (*)
2152 foreach ( $this->validations as $validator )
2153 {
2154 if ( $validator && $validator->field == $field->name && $validator->type == 'required' )
2155 {
2156 $lbl->setClass( MControl::CLASS_CAPTION_REQUIRED );
2157 break;
2158 }
2159 }
2160 }
2161 }
2162 elseif ( $field instanceof MContainer && $field->showRequiredLabel )
2163 {
2164 $lbl->setClass( MControl::CLASS_CAPTION_REQUIRED );
2165 }
2166
2167 $slbl = $rowContent[] = new MSpan('', $lbl, 'label');
2168 if ( $this->labelWidth != NULL )
2169 {
2170 $slbl->_AddStyle('width', $this->labelWidth . 'px');
2171 }
2172 }
2173
2174 $sfld = new MSpan('', $field, 'field');
2175 $rowContent[] = $sfld;
2176 }
2177 return new MDiv('', $rowContent, "m-form-row row");
2178 }
2179
2188 {
2189 $ul = new MUnorderedList();
2190 if (isset($this->buttons) )
2191 {
2192 $ul->AddOption(new MHr);
2193 foreach ( $this->buttons as $b )
2194 {
2195 if ($b->visible) $ul->AddOption($b);
2196 }
2197 }
2198 if ( $this->reset )
2199 {
2200 $ul->AddOption(new MButton('_reset','Limpar','RESET'));
2201 }
2202 if ( $this->return )
2203 {
2204 $ul->AddOption(new MButton('_return','Voltar','RETURN'));
2205 }
2206 $d = (count($ul->options) ? new MDiv('',$ul,'m-form-button-box') : NULL);
2207 return ($d ? new MDiv('', $d, 'm-form-row row') : NULL);
2208 }
2209
2210
2220 function GenerateHiddenFields($hidden)
2221 {
2222 $f[] = "\n<!-- START OF HIDDEN FIELDS -->\n";
2223 foreach ( $hidden as $h )
2224 {
2225 $f[] = new HiddenField($h->name,$h->value);
2226 }
2227 $f[] = "\n<!-- END OF HIDDEN FIELDS -->";
2228 return $f;
2229 }
2230
2231
2243 {
2244 $f = array();
2245 if ( $this->validations )
2246 {
2247 // we generate the form onSubmit handler right in the body of the form
2248 $f[] = "\n<!-- START OF FORM SCRIPT CODE -->\n";
2249 $f[] = "<script language=\"JavaScript\">\n";
2250// echo "document.{$this->name}.elements[0].focus();\n";
2251 $this->OnSubmit("MIOLO_Validate_Input()");
2252 $f[] = "\n";
2253 $f[] = "/*\n";
2254 $f[] = " * MIOLO Form Validation Objects\n";
2255 $f[] = "*/\n";
2256 foreach ( $this->validations as $v )
2257 {
2258 $f[] = $v;
2259 $f[] = "\n";
2260 }
2261 $f[] = "var {$this->name}_validations = Array( ";
2262 $i = 0;
2263 foreach ( $this->validations as $v )
2264 {
2265 if ( $i++ )
2266 {
2267 $f[] = ",\n ";
2268 }
2269 $f[] = str_replace('.', '_', $v->name);
2270 }
2271 $f[] = " );\n";
2272 $f[] = "\n";
2273 $f[] = "</script>\n";
2274 $f[] = "<!-- END OF FORM SCRIPT CODE -->\n";
2275 }
2276 return $f;
2277 }
2278
2280 {
2281 $f = '';
2282 $f .= "\n<!-- START OF FORM SCRIPT CODE -->\n";
2283 $f .= "<script language=\"JavaScript\">\n";
2284 if ( $this->validations )
2285 {
2286 $f .= "document.forms[0].onsubmit = function () { return MIOLO_Validate_Input(); }";
2287 $f .= "\n";
2288 $f .= "/*\n";
2289 $f .= " * MIOLO Form Validation Objects\n";
2290 $f .= "*/\n";
2291 foreach ( $this->validations as $v )
2292 {
2293 $f .= $v->generate();
2294 $f .= "\n";
2295 }
2296 $f .= "var {$this->name}_validations = Array( ";
2297 $i = 0;
2298 foreach ( $this->validations as $v )
2299 {
2300 if ( $i++ )
2301 {
2302 $f .= ",\n ";
2303 }
2304 $f .= str_replace('.', '_', $v->name);
2305 }
2306 $f .= " );\n";
2307 $f .= "\n";
2308 }
2309 else
2310 {
2311 $f .= "var {$this->name}_validations = Array();";
2312 $f .= "document.forms[0].onsubmit = function () { return MIOLO_Validate_Input(); }";
2313 }
2314 $f .= "</script>\n";
2315 $f .= "<!-- END OF FORM SCRIPT CODE -->\n";
2316 echo $f;
2317 }
2318
2319 function Generate()
2320 {
2321 if (!isset($this->buttons))
2322 {
2323 if ($this->defaultButton)
2324 {
2325 $this->buttons[] = new FormButton(FORM_SUBMIT_BTN_NAME, 'Enviar', 'SUBMIT');
2326 }
2327 }
2328 $footer = $this->GenerateFooter();
2329 if ($this->cssForm)
2330 {
2331 $body = $this->GenerateBody();
2332 $this->box->SetControls(array($body));
2333 $this->box->SetBoxClass('m-form-css');
2334 return $this->box->Generate();
2335 }
2336 else
2337 {
2338 $body = new MDiv('',$this->GenerateBody(),'m-form-body panel-body card-body');
2339 if (!is_null($this->bgColor)) $body->AddStyle('backgroundColor',$this->bgColor);
2340 $this->box->SetControls(array($body, $footer));
2341 $id = strlen($this->id) > 0 ? $this->id : $this->GetUniqueId();
2342 $this->box->SetBoxClass("m-form-outer");
2343 $form = new MDiv("frm$id", $this->box,"m-form-box");
2344 if (!is_null($this->align)) $form->AddBoxStyle('text-align',$this->align);
2345 return $form->Generate();
2346// return $this->box->Generate();
2347 }
2348 }
2349
2359 protected function getGrid($campos = array(), $gridName = NULL)
2360 {
2361 $grid = null;
2362
2363 // Navega entre os campos
2364 foreach ( $campos as $field )
2365 {
2366 // Verifica se o campo é uma grid, caso seja um container
2367 // verifica os elementos que compõe este recursivamente
2368 $grid = $this->procuraGrid($field, $gridName);
2369
2370 if( $grid )
2371 {
2372 break;
2373
2374 }
2375
2376 }
2377
2378 return $grid;
2379
2380 }
2381
2390 protected function procuraGrid($field, $gridName)
2391 {
2392 // Se é a grid
2393 if ( is_subclass_of($field, "MGrid") && $field->name == $gridName )
2394 {
2395 return $field;
2396
2397 }
2398
2399 $camposAtuais = $this->getCamposConformeContainer($field);
2400
2401 // Se há campos no container
2402 if( $camposAtuais )
2403 {
2404 return $this->getGrid($camposAtuais, $gridName);
2405
2406 }
2407
2408 return null;
2409
2410 }
2411
2420 protected function getCamposConformeContainer($field)
2421 {
2422 $camposAtuais = null;
2423
2424 if( $field instanceof MDiv)
2425 {
2426 $camposAtuais = $field->getInner();
2427
2428 }
2429 else if( $field instanceof MTab)
2430 {
2431 $camposAtuais = $field->getControls();
2432
2433 }
2434 else if( $field instanceof MTabbedBaseGroup)
2435 {
2436 $camposAtuais = $field->getTabs();
2437
2438 }
2439
2440 return $camposAtuais;
2441
2442 }
2443
2444 public function getGridQuery($gridName)
2445 {
2446 $grid = $this->getGrid($this->fields, $gridName);
2447
2448 if ( !$grid )
2449 {
2450 return;
2451 }
2452
2453 return is_subclass_of($grid, "MGrid") ? $grid->getQuery() : null;
2454 }
2455
2456 public function getGridData($gridName)
2457 {
2458 $grid = $this->getGrid($this->fields, $gridName);
2459
2460 if ( !$grid )
2461 {
2462 return;
2463 }
2464
2465 $isArrayGridMode = !isset($grid->columns[0]); // Modo array associativo de grids
2466 $titles = array();
2467
2468 foreach ($grid->columns as $index => $c)
2469 {
2470 if ( $c->visible )
2471 {
2472 $titles[] = $c->title;
2473 }
2474 else
2475 {
2476 $invisibleColumns[] = $index;
2477 }
2478 }
2479
2480 $data = $grid->getData();
2481 if ( !$data )
2482 {
2483 return;
2484 }
2485
2486 $titleCount = count($titles);
2487 $visibleData = array();
2488 foreach ( $data as $line )
2489 {
2490 if ( $invisibleColumns )
2491 {
2492 // Remove invisible columns
2493 foreach ( $invisibleColumns as $index )
2494 {
2495 unset( $line[$index] );
2496 }
2497 }
2498
2499 $newLine = array();
2500 if ( $isArrayGridMode )
2501 {
2502 $keys = array_keys($grid->columns);
2503
2504 foreach ( $keys as $columnName )
2505 {
2506 if( !in_array($columnName, $invisibleColumns) )
2507 {
2508 // Caso exista um options na grid, substitui o valor
2509 if ( $grid->columns[$columnName]->options )
2510 {
2511 $line[$columnName] = $grid->columns[$columnName]->options[$line[$columnName]];
2512 }
2513
2514 $newLine[] = $line[$columnName];
2515 }
2516 }
2517 }
2518 else
2519 {
2520 $newLine = array_values(array_slice($line, 0, $titleCount));
2521 }
2522
2523 $visibleData[] = $newLine;
2524 }
2525
2526 $titles = array_values( $titles );
2527 return array_merge( array($titles), $visibleData );
2528 }
2529
2539 public function addCustomFields($identifier, $customizedId=NULL, $suffix='', $somenteComActionUrl = false)
2540 {
2541 $fields = $this->getCustomFields($identifier, $customizedId, $suffix, $somenteComActionUrl);
2542
2543 if ( count($fields) > 0 )
2544 {
2545 $this->addFields( $fields );
2546 }
2547 }
2548
2552 public function getCustomFields($identifier, $customizedId=NULL, $suffix='', $somenteComActionUrl = false, $step = null)
2553 {
2555
2556 if ( !$identifier )
2557 {
2558 return;
2559 }
2560
2561 $this->mioloCustomizedId = $customizedId;
2562
2563 // If customized id is set, then it's an edit action
2564 if ( isset($customizedId) )
2565 {
2566 // Load the custom fields values
2567 $data = MCustomValue::getFieldValues($identifier, $customizedId);
2568 }
2569
2570 $actionURL = null;
2571 if( $somenteComActionUrl )
2572 {
2573 $actionURL = MIOLO::getCurrentAction();
2574 }
2575 $this->mioloCustomFields = MCustomField::listByIdentifier($identifier, $actionURL, $step);
2576
2577 $fieldsNormalizados = $this->normalizaCamposCustomizados($this->mioloCustomFields);
2578
2579 $fields = $this->generateCustomFields($fieldsNormalizados, $data, $suffix);
2580
2581 return $fields;
2582 }
2583
2590 protected function normalizaCamposCustomizados($customFields)
2591 {
2592 return $customFields;
2593 }
2594
2598 public function generateCustomFields($mioloCustomFields = array(), $data = null, $suffix = null)
2599 {
2600 $fields = array();
2601
2602 $listNoYes = array(
2603 'f' => _M('No'),
2604 't' => _M('Yes'),
2605 );
2606
2607 foreach ( $mioloCustomFields as $cfield )
2608 {
2609 $cfield instanceof MCustomField;
2610
2611 $field = NULL;
2612 $validator = NULL;
2613
2614 $cfield->suffix = $suffix;
2615 $id = $cfield->getInputId();
2616
2617 $value = $this->GetFormValue($id, isset($data) && isset($data->$id) ? $data->$id : $cfield->defaultValue);
2618 $label = _M($cfield->label, MIOLO::getCurrentModule());
2619
2620 if ( $cfield->isRequired() )
2621 {
2622 $validator = new MRequiredValidator($id, $label);
2623 }
2624
2625 switch ( $cfield->fieldFormat )
2626 {
2628 $field = new MSelection($id, MUtil::getBooleanValue($value) ? 't' : 'f', $label, $listNoYes, false, $cfield->hint);
2629 $field->setJsHint($cfield->jshint);
2630 break;
2631
2633 $field = new MCalendarField($id, $value, $label, 20, $cfield->hint);
2634 $field->setJsHint($cfield->jshint);
2635
2636 if ( $cfield->isRequired() && $cfield->isEditable() && $cfield->isVisible() )
2637 {
2638 $field->isRequired = 'true';
2639 $validator = NULL;
2640 }
2641 break;
2642
2644 $field = new MFloatField($id, $value, $label, 10, $cfield->hint);
2645 $field->setJsHint($cfield->jshint);
2646 break;
2647
2649 $field = new MTextField($id, $value, $label, strlen($cfield->largura) > 0 ? $cfield->largura : 10, $cfield->hint);
2650 $field->setJsHint($cfield->jshint);
2651 $validator = new MIntegerValidator($id, $label, $cfield->isRequired() ? 'required' : 'optional');
2652 break;
2653
2655 $field = new sEscolha($id, $value, $label, new $cfield->objetoreferencia(), $cfield->hint);
2656 $field->setJsHint($cfield->jshint);
2657 break;
2658
2660 $field = new MSelection($id, $value, $label, $cfield->getListValues(), false, $cfield->hint);
2661 $field->setJsHint($cfield->jshint);
2662 break;
2663
2665 $field = new MSelection($id, $value, $label, $cfield->getListSQL());
2666 $field->hint = $cfield->getFieldHint() ?? $cfield->hint;
2667 $field->setJsHint($cfield->jshint);
2668 break;
2669
2671 if (isset($value) && SAGU::is_json($value))
2672 {
2673 $value = json_decode($value);
2674 }
2675
2676 $field = new MTagSelection($id, $value, $label, $cfield->getListSQL(), false, '', '3', 1);
2677 $field->hint = $cfield->getFieldHint() ?? $cfield->hint;
2678 if (!empty($cfield->jshint))
2679 {
2680 $field->addAttribute('title', $cfield->jshint);
2681 }
2682
2683 break;
2684
2686 $selectValues = $cfield->getListValues();
2687 $countValues = count($selectValues);
2688
2689 //Verifica se $value foi definida e se é um json
2690 if (isset($value) && SAGU::is_json($value))
2691 {
2692 $value = json_decode($value);
2693 }
2694
2695 $field = new MTagSelection($id, $value, $label, $selectValues, false, $cfield->hint);
2696 $field->size = '10';
2697 if (!empty($cfield->jshint))
2698 {
2699 $field->addAttribute('title', $cfield->jshint);
2700 }
2701 if ($countValues < 10)
2702 {
2703 $field->size = $countValues;
2704 }
2705 break;
2706
2708 $selectValues = $cfield->getListSQL();
2709 $countValues = count($selectValues);
2710
2711 //Verifica se $value foi definida e se é um json
2712 if (isset($value) && sagu::is_json($value))
2713 {
2714 $value = json_decode($value);
2715 }
2716
2717 $field = new MTagSelection($id, $value, $label, $selectValues);
2718 $field->hint = $cfield->getFieldHint() ?? $cfield->hint;
2719 if (!empty($cfield->jshint))
2720 {
2721 $field->addAttribute('title', $cfield->jshint);
2722 }
2723 $field->size = '10';
2724 if ($countValues < 10)
2725 {
2726 $field->size = $countValues;
2727 }
2728 break;
2729
2731 $field = new MMultilineField($id, $value, $label, strlen($cfield->largura) > 0 ? $cfield->largura : 25, strlen($cfield->altura) ? $cfield->altura : 5, strlen($cfield->largura) ? $cfield->largura : 20, $cfield->hint);
2732 $field->setJsHint($cfield->jshint);
2733 break;
2734
2736 $field = new MTextField($id, $value, $label, strlen($cfield->largura) > 0 ? $cfield->largura : 10, $cfield->hint);
2737 $field->setJsHint($cfield->jshint);
2738 break;
2740 $field = new sEscolha($id, $value, $label, null, $cfield->hint, $cfield->getListValues());
2741 $field->setJsHint($cfield->jshint);
2742 $this->page->addJsCode("$('#" . $id . "').hide();");
2743 break;
2745 $field = new sEscolha($id, $value, $label, null, $cfield->hint, $cfield->getListSQL());
2746 $field->setJsHint($cfield->jshint);
2747 break;
2749 $field = new MDiv($id, "<script>" . $value . "</script>");
2750 break;
2752 $field = new MFileField($id, $value, $label, strlen($cfield->largura) > 0 ? $cfield->largura : 10, $cfield->hint);
2753 $field->setJsHint($cfield->jshint);
2754 $field->addAttribute("accept", $cfield->possibleValues);
2755 break;
2757 $field = new MEditor($id, $value);
2758 // $field->setHint($cfield->hint);
2759 // $field->setJsHint($cfield->jshint);
2760 $field->forceShowLabel = true;
2761 $field->setWidth($cfield->largura ?? SAGU::getParameter('BASIC', 'FIELD_EDITOR_SIZE'));
2762 break;
2763
2764 }
2765
2766 if ( $cfield->maxLength != 0 )
2767 {
2768 if ( !$validator )
2769 {
2770 $validator = new MRegExpValidator($id, $label);
2771 }
2772
2773 $validator->min = $cfield->minLength;
2774 $validator->max = $cfield->maxLength;
2775 }
2776
2777 if ( $field )
2778 {
2779 if ( !$cfield->isEditable() )
2780 {
2781 $field->setReadOnly(true);
2782 $validator = NULL;
2783 }
2784
2785 if ( !$cfield->isVisible() )
2786 {
2787 $field->addBoxStyle('display', 'none');
2788 $validator = NULL;
2789 }
2790
2792 {
2793 $fieldLabel = new MLabel($label . ":");
2794 $fieldLabel->setClass("m-caption");
2795 if($cfield->isRequired())
2796 {
2797 $fieldLabel->setClass('m-caption m-caption-required');
2798 }
2799 $fieldLabel->setWidth(SAGU::getParameter('BASIC', 'FIELD_CONTAINER_SIZE'));
2800 $field = new MHContainer('htc'.$id, array($fieldLabel, $field));
2801 }
2802 $fields[] = $field;
2803 }
2804
2805 if ( $validator != NULL )
2806 {
2807 $this->addValidator($validator);
2808 }
2809 }
2810
2811
2812
2813 return $fields;
2814 }
2815
2823 public function saveCustomFields($customizedId, $data = null, $identifier = null, $somenteComActionUrl = false, $cfId = null)
2824 {
2825 if ( strlen($identifier) > 0 )
2826 {
2827 $this->getCustomFields($identifier, $customizedId, '', $somenteComActionUrl);
2828 }
2829
2830 if ( count($this->mioloCustomFields) == 0 )
2831 {
2832 return NULL;
2833 }
2834
2835 if (!isset($this->mioloCustomizedId) && isset($customizedId))
2836 {
2837 $this->mioloCustomizedId = $customizedId;
2838 }
2839
2840 // Se nao passar dados, pega o padrao getData()
2841 if ( !$data )
2842 {
2843 $data = $this->getData();
2844 }
2845
2846 $ok = false;
2847
2848 foreach ( $this->mioloCustomFields as $cf )
2849 {
2850 //se recebeu id só salva este
2851 if ($cfId != null && $cfId != $cf->id)
2852 {
2853 continue;
2854 }
2855
2856 $cf instanceof MCustomField;
2857
2858 $inputId = $cf->getInputId();
2859
2860 $customValue = new MCustomValue();
2861 $customValue->customizedId = $customizedId;
2862 $customValue->customFieldId = $cf->id;
2863 $customValue->value = $data->$inputId;
2864 if ($cf->fieldFormat == MCustomField::FORMAT_FILE && is_array($_FILES[$inputId]))
2865 {
2866 if ($_FILES[$inputId]['error'] == UPLOAD_ERR_NO_FILE)
2867 {
2868 continue;
2869 }
2870 $busFile = new BusinessBasicBusFile();
2871 $fdata = new stdClass();
2872 $fdata->uploadFileName = $_FILES[$inputId]['name'];
2873 $fdata->contentType = mime_content_type($_FILES[$inputId]['tmp_name']);
2874 $fileId = $busFile->insertFile($fdata, $_FILES[$inputId]['tmp_name']);
2875 $customValue->value = $fileId;
2876 }
2877
2878 //Verifica se é um array
2879 if (is_array($data->$inputId))
2880 {
2881 //Transforma o array em JSON e atribui na variavel
2882 $customValue->value = json_encode($data->$inputId, JSON_UNESCAPED_UNICODE);
2883 }
2884 // If customized id is set, then it's an edit action
2885 if ( isset($this->mioloCustomizedId) )
2886 {
2887 if ( $customValue->updateByData() )
2888 {
2889 $ok = true;
2890 }
2891 else
2892 {
2893 $ok = false;
2894 break;
2895 }
2896 }
2897 else
2898 {
2899 if ( $customValue->insert() )
2900 {
2901 $ok = true;
2902 }
2903 else
2904 {
2905 $ok = false;
2906 break;
2907 }
2908 }
2909 }
2910 return $ok;
2911 }
2912
2919 public function searchCustomFieldValues($identifier, $returnSql = false, $onlyCustomizedid = false, $filters = null)
2920 {
2921 if ( !count($this->mioloCustomFields) )
2922 {
2923 return NULL;
2924 }
2925
2926 $searchData = array();
2927 $values = NULL;
2928
2929 foreach ( $this->mioloCustomFields as $cf )
2930 {
2931 $value = MIOLO::_REQUEST($cf->getInputId());
2932
2933 if ( strlen($value) )
2934 {
2935 $searchData[$cf->id] = $value;
2936 }
2937 }
2938 $values = MCustomValue::searchFieldValues($identifier, $searchData, $returnSql, $onlyCustomizedid, $filters);
2939
2940 return $values;
2941 }
2942
2946 public function getCustomFieldValues()
2947 {
2948 $values = array();
2949
2950 foreach ( $this->mioloCustomFields as $cf )
2951 {
2952 $cf instanceof MCustomField;
2953 $value = MIOLO::_REQUEST($cf->getInputId());
2954
2955 if ( strlen($value) )
2956 {
2957 $values[$cf->getInputId()] = $value;
2958 }
2959 elseif ( is_array($value) )
2960 {
2961 $i = 0;
2962 foreach ($value as $customValue)
2963 {
2964 $values[$cf->getInputId()][$i] = $customValue;
2965 $i ++;
2966 }
2967 }
2968 }
2969
2970 return $values;
2971 }
2972
2979 public function deleteCustomFieldValues($customizedId)
2980 {
2981 if ( count($this->mioloCustomFields) == 0 )
2982 {
2983 return NULL;
2984 }
2985
2986 $ok = false;
2987
2988 foreach ( $this->mioloCustomFields as $cf )
2989 {
2990 $customValue = new MCustomValue();
2991 $customValue->customizedId = $customizedId;
2992 $customValue->customFieldId = $cf->id;
2993
2994 $ok = $customValue->deleteByCustomizedId();
2995 }
2996
2997 return $ok;
2998 }
2999
3003 public static function expansibleText($labelExpand, $labelCollapse, $text)
3004 {
3005 $divLabelExpand = md5($text);
3006 $divContent = md5($text) . 'b';
3007
3008 $text = str_replace("\n", '<br />', $text);
3009
3010 $html .= '<div id="'.$divLabelExpand.'">
3011 <a href="javascript: return;"
3012 onclick="document.getElementById(\''.$divContent.'\').style.display=\'block\'; document.getElementById(\''.$divLabelExpand.'\').style.display=\'none\'"
3013 >'.$labelExpand.'</a>
3014
3015 </div>';
3016
3017 $html .= '<div id="'.$divContent.'" style="display: none">
3018 <a href="javascript: return;"
3019 onclick="document.getElementById(\''.$divContent.'\').style.display=\'none\'; document.getElementById(\''.$divLabelExpand.'\').style.display=\'block\'">'.$labelCollapse.'</a>
3020 <pre>' . $text . '</pre>
3021 </div>';
3022
3023 return $html;
3024 }
3025
3032 public static function normalizaActionParaConsulta($action)
3033 {
3034 $type = MIOLO::_REQUEST('type');
3035
3036 // verifica action genericReports apenas para documentos e relatórios, configuração não deve adicionar o parâmetro reportid
3037 if ( strpos($action, 'report:genericReports') !== false || strpos($action, 'document:genericReports') !== false )
3038 {
3039 // obtém o parâmetro reportid e concatena ao $action
3040 $action = $action.'&reportid='.MIOLO::_REQUEST('reportid');
3041 }
3042 // verifica action generateReport
3043 else if ( strpos($action, 'generateReport') !== false )
3044 {
3045 // obtém o parâmetro report e concatena ao $action
3046 $action = $action.'&report='.MIOLO::_REQUEST('report');
3047 }
3048 // Verifica se é um formulário dinâmico
3049 else if ( $type )
3050 {
3051 $action .= '&type='.$type;
3052 }
3053
3054 return $action;
3055 }
3056}
3057
3059{
3060}
3061
3062?>
const CLASS_CAPTION
Definição mcontrol.class:46
const CLASS_CAPTION_REQUIRED
Definição mcontrol.class:47
setJsHint( $hint)
Definição mcontrol.class:519
const FORM_MODE_SHOW_SIDE
Definição mcontrol.class:42
const FORMAT_LONG_TEXT
const FORMAT_SESCOLHA
const FORMAT_SESCOLHA_LISTSQL
const FORMAT_SESCOLHA_LIST
const FORMAT_MULTI_LIST
const FORMAT_INTEGER
const FORMAT_JAVASCRIPT
const FORMAT_DECIMAL
const FORMAT_LISTSQL
const FORMAT_MULTI_LISTSQL
const FORMAT_BOOLEAN
static listByIdentifier($identifier, $actionURL=NULL, $step=null)
static getFieldValues($identifier, $customizedId)
static searchFieldValues($identifier, $searchData, $returnSql=false, $onlyCustomizedid=false, $filters=null)
setFieldForm(MForm $form)
Definição mform.class:9
AddValidator($validator)
Definição mform.class:284
getFieldValidator($name)
Definição mform.class:2020
GetFieldList()
Definição mform.class:1127
GenerateInfos()
Definição mform.class:1869
$cssForm
Definição mform.class:115
$mioloCustomFields
Definição mform.class:132
SetAction($action)
Definição mform.class:727
$method
Definição mform.class:23
SetFormCSS($height=0, $width=0, $top=0, $left=0, $buttons=0, $position='absolute')
Definição mform.class:1801
$align
Definição mform.class:127
getGridData($gridName)
Definição mform.class:2456
SetFields(&$fields)
Definição mform.class:803
SetWidth($width=NULL)
Definição mform.class:1822
setValidations($validations)
Definição mform.class:246
SetFooter($footer)
Definição mform.class:525
generateCustomFields($mioloCustomFields=array(), $data=null, $suffix=null)
Definição mform.class:2598
GenerateAlerts()
Definição mform.class:1882
SetData($data)
Definição mform.class:1501
validate( $required, $assert=true, $args=NULL)
Definição mform.class:1232
deleteCustomFieldValues($customizedId)
Definição mform.class:2979
$buttons
Definição mform.class:28
CreateFields()
Definição mform.class:270
SetButtonAttr($name, $attr, $value)
Definição mform.class:1747
procuraGrid($field, $gridName)
Definição mform.class:2390
HasAlerts()
Definição mform.class:1427
SetHeight($height=NULL)
Definição mform.class:1827
$layout
Definição mform.class:110
showReturn( $state)
Definição mform.class:1034
addCustomFields($identifier, $customizedId=NULL, $suffix='', $somenteComActionUrl=false)
Definição mform.class:2539
GenerateButtons()
Definição mform.class:2187
$labelWidth
Definição mform.class:124
OnSubmit($jscode)
Definição mform.class:692
static setShowHints($state)
Definição mform.class:1100
OnLoad()
Definição mform.class:258
setRandomField()
Definição mform.class:203
getGrid($campos=array(), $gridName=NULL)
Definição mform.class:2359
IsSubmitted()
Definição mform.class:399
SetAlign($value)
Definição mform.class:1817
Generate()
Definição mform.class:2319
GenerateLayoutFields(&$hidden)
Definição mform.class:1968
$defaultButton
Definição mform.class:83
ClearFields()
Definição mform.class:1163
static $showHints
Definição mform.class:68
$width
Definição mform.class:63
SetLabelWidth($width)
Definição mform.class:1843
getFormControlFields()
Definição mform.class:314
_GetFormFieldValue($field)
Definição mform.class:557
__construct($title='', $action='', $close='', $icon='', $help='')
Definição mform.class:155
GenerateScript()
Definição mform.class:2242
SetPage(&$page)
Definição mform.class:1675
getValidations()
Definição mform.class:241
SetBackgroundColor($bgcolor)
Definição mform.class:1812
HasErrors()
Definição mform.class:1325
SetValidators($validators)
Definição mform.class:368
& GetField($name)
Definição mform.class:1633
getRandomField()
Definição mform.class:186
searchCustomFieldValues($identifier, $returnSql=false, $onlyCustomizedid=false, $filters=null)
Definição mform.class:2919
GetFieldValue($name, $value=false)
Definição mform.class:1573
getData()
Definição mform.class:1474
showReset( $state)
Definição mform.class:1064
setShowResetButton( $state)
Definição mform.class:1075
__set($name, $value)
Definição mform.class:220
showHints($state)
Definição mform.class:1108
AddButton($btn)
Definição mform.class:984
GetTitle()
Definição mform.class:427
setShowReturnButton( $state)
Definição mform.class:1045
validateRequiredFields( $assert=true)
Definição mform.class:1207
SetFieldCSS($name, $top, $left, $width=NULL, $position='absolute')
Definição mform.class:1770
$reset
Definição mform.class:43
$styles
Definição mform.class:48
GetFieldAttr($name, $attr, $index=NULL)
Definição mform.class:1717
SetClose($action)
Definição mform.class:464
$validations
Definição mform.class:78
SetTitle($title)
Definição mform.class:445
$return
Definição mform.class:38
& GetButton($name)
Definição mform.class:1642
CollectInput($data)
Definição mform.class:1446
AddError($err)
Definição mform.class:1291
$action
Definição mform.class:18
GenerateHiddenFields($hidden)
Definição mform.class:2220
AddAlert($alert)
Definição mform.class:1391
SetFieldValidator($name, $value)
Definição mform.class:1617
setHelp($help, $module=null, $action=null)
Definição mform.class:747
removeAJAXValidator($fieldId)
Definição mform.class:2059
$fields
Definição mform.class:33
$errors
Definição mform.class:88
SetButtons($btn)
Definição mform.class:1003
SetIcon($icon)
Definição mform.class:485
$mioloCustomizedId
Definição mform.class:137
SetFieldValue($name, $value)
Definição mform.class:1593
$bgColor
Definição mform.class:126
ClearButtons()
Definição mform.class:1169
$footer
Definição mform.class:58
getCamposConformeContainer($field)
Definição mform.class:2420
getGridQuery($gridName)
Definição mform.class:2444
__get($name)
Definição mform.class:236
static $fieldNum
Definição mform.class:106
getCustomFieldValues()
Definição mform.class:2946
static expansibleText($labelExpand, $labelCollapse, $text)
Definição mform.class:3003
GenerateFooter()
Definição mform.class:1954
static getShowHints()
Definição mform.class:1086
GenerateLayoutField($field, &$hidden)
Definição mform.class:2079
AddFields($fields)
Definição mform.class:953
saveCustomFields($customizedId, $data=null, $identifier=null, $somenteComActionUrl=false, $cfId=null)
Definição mform.class:2823
validateAll( $assert=true)
Definição mform.class:1186
AddField($field, $hint=false)
Definição mform.class:932
getFieldFromFields($fieldName, $fields)
Definição mform.class:326
static normalizaActionParaConsulta($action)
Definição mform.class:3032
& GetPage()
Definição mform.class:1657
normalizaCamposCustomizados($customFields)
Definição mform.class:2590
$enctype
Definição mform.class:73
$alerts
Definição mform.class:99
$title
Definição mform.class:13
getFieldValidators($name)
Definição mform.class:2038
getCustomFields($identifier, $customizedId=NULL, $suffix='', $somenteComActionUrl=false, $step=null)
Definição mform.class:2552
setShowPostButton( $state)
Definição mform.class:1056
GenerateErrors()
Definição mform.class:1855
GetFormFields()
Definição mform.class:542
SetFieldAttr($name, $attr, $value)
Definição mform.class:1695
generateAJAXValidators()
Definição mform.class:2279
AddInfo($info)
Definição mform.class:1343
SetAlternate($color0, $color1)
Definição mform.class:493
Error( $err)
Definição mform.class:1272
$help
Definição mform.class:53
$zebra
Definição mform.class:120
getRequestData()
Definição mform.class:1482
generateValidators($fields)
Definição mform.class:1988
HasInfos()
Definição mform.class:1377
GenerateBody()
Definição mform.class:1897
$infos
Definição mform.class:93
GetFooter()
Definição mform.class:513
AddJsCode($jscode)
Definição mform.class:707
SetFormValue($name, $value)
Definição mform.class:630
setButtonLabel( $index, $label)
Definição mform.class:1026
GetFormValue($name=null, $value=NULL)
Definição mform.class:603
$cssButtons
Definição mform.class:116
static getCurrentModule()
Definição miolo.class:1066
static _REQUEST( $vars, $from='ALL')
Definição miolo.class:1109
static getCurrentAction()
Definição miolo.class:1086
static getInstance()
Definição miolo.class:134
static Error($msg='', $goto='', $caption='', $event='')
Definição mprompt.class:90
static Information($msg, $goto='', $event='')
Definição mprompt.class:108
static Alert($msg, $goto='', $event='')
Definição mprompt.class:123
static getNameTransaction($action, $module)
static getBooleanValue($value)
Definição mutil.class:100
const FORM_SUBMIT_BTN_NAME
Definição mform.class:2