MIOLO20
Carregando...
Procurando...
Nenhuma entrada encontrada
msubdetail.class
Ir para a documentação deste ficheiro.
1<?php
2session_start();
3
29global $MIOLO;
30$MIOLO->page->addScript('m_util_validate.js');
31
33{
34 private $item = 'item';
35 private $tableItem = '';
36 private $actions = '';
37 public $title = ''; // table Title
38 private $table = NULL;
39 private $fields = NULL;
40 private $showButtons = true;
41 private $hideButtonsOnAdd = false;
42 private $overflowWidth = NULL;
43 private $overflowType = NULL;
44 public $cleanFields = true;
45 public $cleanHiddenF = false;
46 public $actionCelWidth = false;
47 public $transaction = NULL;
48 protected $updateButton = false;
50 private $namesArray;
52 public $defaultValueFields = array();
53
55// get all data from form (the data is cleaned with getAjaxFields from class MTableRawSession)
56// ir uses escape, that convert characters to html entities
57function getFieldValues()
58{
59 var temp ='';
60
61 for (i=0; i<document.forms[0].elements.length; i++)
62 {
63 var field = document.forms[0].elements[i];
64
65 if ( field.type == 'checkbox' || field.type == 'radio' )
66 {
67 if ( field.checked )
68 {
69 temp += field.id + '|' + field.value + '#';
70 }
71 }
72 else if ( field.className.match(/select m-multitext.*/) )
73 {
74 temp += field.id + '|type_select-multiple';
75 for ( var j = 0; j < field.options.length; j++ )
76 {
77 if ( j != 0 )
78 {
79 temp += '@';
80 }
81 temp += escape(field.options[j].value);
82 }
83 temp += '#';
84 }
85 else if ( field.className.match(/m-combo.*/) )
86 {
87 var selecionou=false;
88 temp += field.id + '|';
89
90 if( field.options )
91 {
92 for ( var j = 0; j < field.options.length; j++ )
93 {
94 if (field.options[j].selected)
95 {
96 if ( selecionou )
97 {
98 temp += ',';
99 }
100
101 temp += field.options[j].value;
102 selecionou = true;
103 }
104 }
105 }
106 temp += '#';
107
108 }
109 else if ( $(field).hasClass('m-multiline-field') && $(field).hasClass('m-editor') )
110 {
111 eval('value = CKEDITOR.instances.' + field.id + '.getData();');
112 temp += field.id + '|' + escape(value) + '#';
113 }
114 else if ( field.type == 'file' && field.value.length > 0 )
115 {
116 var args = new FormData();
117 var file = field.files[0];
118 args.append(field.name, file);
119
120 $.ajax({
121 url: 'upload.php',
122 data: args,
123 method: 'POST',
124 cache: false,
125 contentType: false,
126 processData: false,
127 async: false,
128 success: function(data) {
129 temp += field.id + '|' + data + '#';
130 },
131 complete: function(){}
132 });
133 }
134 else
135 {
136 temp += field.id + '|' + escape(field.value) + '#';
137 }
138 }
139 return temp;
140}
141";
142
143 const HIDDEN_FIELD_PREFIX_NAME = "MSubDetail";
144 const EVENT_ADD = 'add';
145 const EVENT_EDIT = 'edit';
146 const EVENT_REMOVE = 'remove';
147
159 public function __construct($item, $title, $columns=NULL, $fields=NULL, $opts=array('edit','remove'), $checkTransaction=NULL)
160 {
161 global $MIOLO;
162
163 // Para controle de dados.
164 $randomId = (Integer) $MIOLO->_REQUEST('randomid');
165 if ( empty($randomId) )
166 {
167 $randomId = rand();
168 $_REQUEST["randomid"] = (Integer) $randomId;
169 }
170
171 parent::__construct($item, $title, null, $align);
172 $this->formMode = MControl::FORM_MODE_SHOW_SIDE;
173 $this->title = $title;
174 $module = MIOLO::getCurrentModule();
175 $this->item = $item;
176 $this->gValidator = new MSubDetailValidators();
177 // adds default column
178 $this->addColumn(_M('Actions'), 'left', true, '10%', true, '');
179
180 $jsGetFieldValues = self::jsGetFieldValues();
181 $code = <<<JS
182if ( document.getElementById('$item') )
183{
184 campos = new Array();
185 campos[0] = 'MSubDetail';
186 campos[1] = 'arrayItemTemp';
187 campos[2] = 'divResponse';
188
189 for ( i=0; i < 3; i++ )
190 {
191 if ( !document.getElementById(campos[i]) )
192 {
193 var input = document.createElement('input');
194 input.setAttribute('type', 'hidden');
195 input.setAttribute('id', campos[i]);
196 document.getElementById('$item').appendChild(input);
197 }
198 }
199}
200
201$jsGetFieldValues
202
203// Converts + => and make unescape (to receive data from server...
204function decode(str)
205{
206 if (str)
207 {
208 return unescape(str.replace(/\+/g, ' '));
209 }
210 else
211 {
212 return '';
213 }
214}
215
216function mioloDoAjax(phpFunction, responseElement)
217{
218 args = getFieldValues();
219
220 if (responseElement)
221 {
222 document.getElementById('divResponse').value = responseElement;
223 }
224 MIOLO_ajaxCall('$url ', 'POST', phpFunction, args, ajaxResponse, 'TEXT');
225}
226
227function ajaxResponse( result )
228{
229 MIOLO_parseAjaxJavascript(result);
230 divResponse = document.getElementById('divResponse').value;
231 document.getElementById(divResponse).innerHTML = result;
232}
233
234JS;
235
236 $cpaintfunction = MIOLO::_REQUEST('cpaint_function');
237 if (strlen(MIOLO::_REQUEST('cpaint_function')) > 0 && strlen($cpaintfunction[0]) > 0)
238 {
239 $this->page->addAJAXJsCode($code);
240 }
241 else
242 {
243 $this->page->addJsCode($code);
244 }
245
246
247 if ( $columns )
248 {
249 $this->setColumns($columns);
250 }
251
252 //FIXME Forces default value
253 $this->setHideButtonsOnAdd(false);
254
255 // FIXME: this was added to change the default $opts value to prevent
256 // session problems on some environments (with unknown reasons)
257 if ( $opts === true )
258 {
259 $opts = array('edit','remove');
260 }
261
262 $allowUpdate = TRUE;
263 $allowDelete = TRUE;
264 $allowInsert = TRUE;
265
266 if ( strlen($checkTransaction) )
267 {
268 $isAdmin = $MIOLO->checkAccess($checkTransaction, A_ADMIN, FALSE, TRUE);
269
270 if ( !$isAdmin )
271 {
272 $allowUpdate = $MIOLO->checkAccess($checkTransaction, A_UPDATE, FALSE, TRUE);
273 $allowDelete = $MIOLO->checkAccess($checkTransaction, A_DELETE, FALSE, TRUE);
274 $allowInsert = $MIOLO->checkAccess($checkTransaction, A_INSERT, FALSE, TRUE);
275
276 if ( !$allowInsert && !$allowUpdate )
277 {
278 $this->setHideButtonsOnAdd(TRUE);
279 $fields = array(new MDiv(NULL, NULL));
280 }
281 elseif ( !$allowInsert )
282 {
283 $this->setHideButtonsOnAdd(TRUE);
284 }
285 }
286 }
287
288 if ( $fields )
289 {
290 $this->setFields($fields);
291 }
292
293 if ( $opts && is_array($opts) )
294 {
295 if ( in_array('edit', $opts) && $allowUpdate )
296 {
297 $this->addAction('editFromTable', 'table-edit.png', $module);
298 }
299
300 if ( in_array('duplicate', $opts) && $allowInsert )
301 {
302 $this->addAction('duplicateFromTable', 'table-duplicate.png', $module);
303 }
304
305 if ( in_array('remove', $opts) && $allowDelete )
306 {
307 $this->addAction('removeFromTable', 'table-delete.png', $module);
308 }
309
310 if ( in_array('up', $opts) )
311 {
312 $this->addAction('upFromTable', 'table-up.png', $module);
313 }
314
315 if ( in_array('down', $opts) )
316 {
317 $this->addAction('downFromTable', 'table-down.png', $module);
318 }
319
320 if ( in_array('noButtons', $opts) )
321 {
322 $this->showButtons = false;
323 }
324
325 if ( in_array('updateButton', $opts) )
326 {
327 $this->setUpdateButton(true);
328 }
329 if ( in_array('hideButtonsOnAdd', $opts) )
330 {
331 $this->setHideButtonsOnAdd(true);
332 }
333 }
334 }
335
339 public function setUpdateButton($show)
340 {
341 $this->updateButton = $show;
342 self::setSessionValue('updateButton', $show, $this->item);
343 }
344
348 public function setHideButtonsOnAdd($hide)
349 {
350 $this->hideButtonsOnAdd = $hide;
351 self::setSessionValue('hideButtonsOnAdd', $hide, $this->item);
352
353 if ( $hide )
354 {
355 $this->page->addJsCode("document.getElementById('divButtons').style.display = 'none';");
356 }
357 }
358
359 public static function setSessionValue($var, $value, $itemForce = null)
360 {
361 if ( $itemForce )
362 {
363 $item = $itemForce;
364 }
365 $session = new MSession('CLASSNAME');
366 $object = $session->getValue($item);
367 $object->$var = $value;
368 $session->setValue($item, $object);
369 }
370
371 public static function getSessionValue($var, $itemForce = null)
372 {
373 if ( $itemForce )
374 {
375 $item = $itemForce;
376 }
377 $session = new MSession('CLASSNAME');
378
379 $object = $session->getValue($item);
380
381 return $object->$var;
382 }
383
384 public static function getUpdateButton($itemForce = null)
385 {
386 $value = self::getSessionValue('updateButton', $itemForce);
387 return $value;
388 }
389
390 public static function getHideButtonsOnAdd($itemForce = null)
391 {
392 $value = self::getSessionValue('hideButtonsOnAdd', $itemForce);
393 return $value;
394 }
395
396 public function setShowButtons($showButtons)
397 {
398 $this->showButtons = $showButtons;
399 }
400
401 public function getShowButtons()
402 {
403 return $this->showButtons();
404 }
405
412 public function setOverFlowWidth($width, $type = 'auto')
413 {
414 $this->overflowWidth = $width;
415 $this->overflowType = $type;
416 }
417
418 protected function processNamesArray($field)
419 {
420 if ( is_array($field) )
421 {
422 foreach ( $field as $value )
423 {
424 $this->processNamesArray($value);
425 }
426 }
427 elseif ( $field instanceof MDiv )
428 {
429 $this->processNamesArray($field->getInner());
430 }
431 elseif ( $field instanceof MContainer )
432 {
433 $this->processNamesArray($field->getControls());
434 }
435 elseif ( $field->name )
436 {
437 $this->namesArray[] = $field->name;
438 }
439 return $this->namesArray;
440 }
441
447 public function setDefaultValueFields($fields)
448 {
449 foreach ( (array) $fields as $field )
450 {
451 if ( $field instanceof MRadioButton || $field instanceof MCheckBox )
452 {
453 $this->defaultValueFields[] = $dvField = new MTextField("MSubDetail_defaultValue_$field->id", $field->checked ? $field->value : NULL);
454 $dvField->addBoxStyle('display', 'none');
455 }
456 elseif ( $field instanceof MDiv )
457 {
458 $this->setDefaultValueFields($field->getInner());
459 }
460 elseif ( $field instanceof MContainer )
461 {
462 $this->setDefaultValueFields($field->getControls());
463 }
464 elseif ( $field instanceof MControl && strlen($field->id) )
465 {
466 $this->defaultValueFields[] = $dvField = new MTextField("MSubDetail_defaultValue_$field->id", $field->value);
467 $dvField->addBoxStyle('display', 'none');
468 }
469 }
470 }
471
472 public function setFields($fields)
473 {
474 if ( $fields )
475 {
476 $this->setDefaultValueFields($fields);
477 $fields = array_merge($fields, $this->defaultValueFields);
478
479 $this->fields = $fields;
480 $container = new MVContainer(NULL, $fields);
481 $container->width = '100%';
482 $this->setControls(array( $container ));
483 $fieldNames = $this->processNamesArray($fields);
484 self::setSessionValue('fieldNames', $fieldNames, $this->item);
485 }
486 }
487
488 public function setColumns($columns)
489 {
490 unset($tempColumns);
491 if ( $columns && is_array($columns) )
492 {
493 foreach ( $columns as $line => $info )
494 {
495 if ( $info->visible == true )
496 {
497 $titles[] = $info->title;
498 unset($temp);
499 $temp->align = $info->align;
500 $temp->title = $info->title;
501 $temp->width = $info->width;
502 $temp->visible = $info->visible;
503 $temp->options = $info->options;
504 $temp->replace = $info->replace;
505 $tempColumns[] = $temp;
506 }
507 }
508 }
509 self::setSessionValue('titles', $titles, $this->item);
510 self::setSessionValue('columns', $tempColumns, $this->item);
511 }
512
513 private function addColumn($title, $align='left', $nowrap='notImplemented', $width = NULL, $visible = TRUE, $options = NULL, $order = 'notImplemented', $filter='notImplemented')
514 {
515 //Inserir na sessão tambem
516 $this->columns[] = new MGridColumn($title, $align, $nowrap, $width, $visible, $options, $order, $filter);
517
518 if ( $visible == true )
519 {
520 $this->titles[] = $title;
521 }
522 }
523
531 public function setFieldValidators($fields, $validators)
532 {
533 foreach ( (array) $fields as $field )
534 {
535 if ( $field instanceof MContainer )
536 {
537 $this->setFieldValidators($field->getControls(), $validators);
538 }
539 elseif ( $field instanceof MDiv )
540 {
541 $this->setFieldValidators($field->getInner(), $validators);
542 }
543 elseif ( is_object($field) && !$field->validator )
544 {
545 $validator = $this->getFieldValidator($field->name, $validators);
546
547 if ( $validator )
548 {
549 if ( $validator->mask )
550 {
551 $field->addAttribute('onkeypress', "return MUtil_Validate_Mask('$validator->mask', event)");
552 $field->addAttribute('onkeyup', "MUtil_Apply_Mask('$field->name', '$validator->mask', event)");
553 }
554 }
555 }
556 }
557 }
558
564 public function setValidators($validators = null)
565 {
566 if ( is_null($validators) )
567 {
568 self::setSessionValue('validators', '', $this->item);
569 return;
570 }
571
572 $this->setFieldValidators($this->fields, $validators);
573
574 $this->gValidator->setValidators($validators);
575 $valids = $this->gValidator->transformToStdClasses();
576 self::setSessionValue('validators', $valids, $this->item);
577 }
578
585 public static function setValidatorsAjax($validators, $item)
586 {
588 $gValidator->setValidators($validators);
589 $valids = $gValidator->transformToStdClasses();
590 self::setSessionValue('validators', $valids, $item);
591 }
592
598 public static function getValidators($itemForce = null)
599 {
600 if ( $itemForce )
601 {
602 $item = $itemForce;
603 }
604 $validators = self::getSessionValue('validators', $item);
606 }
607
615 public function getFieldValidator($name, $validators)
616 {
617 $fieldValidator = NULL;
618
619 foreach ( $validators as $validator )
620 {
621 if ( $validator && $validator->field == $name )
622 {
623 $fieldValidator = $validator;
624 break;
625 }
626 }
627
628 return $fieldValidator;
629 }
630
631 public function addAction($phpFunction, $img, $imgModule)
632 {
633 if ( $this )
634 {
635 $action->event = "AddTableResult" . $this->item;
636 $action->jsFunction = "AddTableResult" . $this->item;
637 $action->phpFunction = $phpFunction;
638 $action->img = $img;
639 $action->imgModule = $imgModule;
640 $actions = self::getSessionValue('actions', $this->item);
641 $actions[$phpFunction] = $action;
642 self::setSessionValue('actions', $actions, $this->item);
643 }
644 }
645
646 protected static function generateActionString($i , $itemForce=NULL)
647 {
649 $url = str_replace('&amp;', '&', $MIOLO->getCurrentURL());
650
651 if ( isset($this ))
652 {
653 $item = $this->item;
654 }
655 if ( $itemForce )
656 {
657 $item = $itemForce;
658 }
659
660 $actions = self::getSessionValue('actions', $item);
661
662 unset($tempString);
663 unset($result);
664
665 if ( $actions )
666 {
667 foreach ( $actions as $line => $info )
668 {
669 unset($link);
670 unset($linkImg);
671 $tempString = "arrayItem|$i#sessionItem|$item#";
672 $link = 'javascript:document.getElementById(\'MSubDetail\').value=\'' . $item . '\';document.getElementById(\'arrayItemTemp\').value=\'' . $i . '\';mioloDoAjax(\'' . $info->phpFunction . '\')';
673 $linkImg = new MImageLink("link$i", null, $link, $MIOLO->getUI()->getImageTheme($MIOLO->getConf('theme.main'), $info->img));
674 $result .= $linkImg->generate();
675 }
676 }
677 return $result;
678 }
679
684 public static function getDataStatic($item = NULL)
685 {
686 $MIOLO = MIOLO::getInstance();
687 $cpaintArgument = SdAjaxFields($MIOLO->_REQUEST("cpaint_argument"));
688 $randomid = SAGU::NVL($cpaintArgument->randomid, $MIOLO->_REQUEST("randomid"));
689
690 $itens = $_SESSION['MSubDetail' . $randomid][$item];
691 if ( !$itens )
692 {
693 $itens = $_SESSION['MSubDetail' . $randomid][$item][0]; //bug??/
694 }
695 unset($_SESSION['MSubDetail' . $randomid][$item]); //clearData
696 // rewrite the session with correct arrayItem values
697 unset($itens['event']);
698 if ( $itens )
699 {
700 $x = 0;
701 foreach ( $itens as $line )
702 {
703 $line->arrayItem = $x;
704 $x++;
705 $_SESSION['MSubDetail' . $randomid][$item][] = $line;
706 }
707 }
708
709 return $_SESSION['MSubDetail' . $randomid][$item];
710 }
711
717 public static function getDataItem($arrayItem, $itemForce=NULL)
718 {
719 $MIOLO = MIOLO::getInstance();
720 $cpaintArgument = SdAjaxFields($MIOLO->_REQUEST("cpaint_argument"));
721 $randomid = SAGU::NVL($MIOLO->_REQUEST("randomid"), $cpaintArgument->randomid);
722
723 $item = $itemForce;
724
725 $itens = $_SESSION['MSubDetail' . $randomid][$item];
726
727 if ( $itens )
728 {
729 foreach ( $itens as $line => $info )
730 {
731 if ( $info->arrayItem == $arrayItem )
732 {
733 return $info;
734 }
735 }
736 }
737 }
738
745 public static function defineData($arrayItem, $obj, $item=NULL)
746 {
747 $MIOLO = MIOLO::getInstance();
748 $randomid = SAGU::NVL($MIOLO->_REQUEST("randomid"), $obj->randomid);
749
750 $_SESSION['MSubDetail' . $randomid][$item][$arrayItem] = $obj;
751 }
752
757 public static function addDataStatic($data, $itemForce = NULL)
758 {
759 $MIOLO = MIOLO::getInstance();
760 $cpaintArgument = SdAjaxFields($MIOLO->_REQUEST("cpaint_argument"));
761 $randomid = SAGU::NVL($MIOLO->_REQUEST("randomid"), $cpaintArgument->randomid, $data->randomid);
762
763 if ( $itemForce )
764 {
765 $item = $itemForce;
766 }
767
768 if ( $data )
769 {
770 if ( is_array($data) )
771 {
772 foreach ( $data as $line => $info )
773 {
774 MSubDetail::addData($info, $item);
775 }
776 }
777 else
778 {
779 $_SESSION['MSubDetail' . $randomid][$item][] = $data;
780 }
781 }
782 }
783
790 public static function setDataStatic($data, $itemForce=NULL)
791 {
792 if (!strlen($itemForce) > 0)
793 {
794 MIOLO::getInstance()->error("Erro de código fonte! O id da MSubDetail não foi informado!");
795 }
796
797 MSubDetail::clearData($itemForce);
798 MSubDetail::addData($data, $itemForce);
799 MSubDetail::getData($itemForce);
800 }
801
802 public function setDataThis($data)
803 {
804 self::setDataStatic($data, $this->getName());
805 }
806
807 public function getDataThis($subDetail = null)
808 {
809 if ( !$subDetail )
810 {
811 $subDetail = $this->getName();
812 }
813 return self::getDataStatic($subDetail);
814 }
815
816 public function clearDataThis()
817 {
818 self::clearDataStatic($this->getName());
819 }
820
821 public function addDataThis($data)
822 {
823 self::addDataStatic($data, $this->getName());
824 }
825
826 public function __call($name, $arguments)
827 {
828 if ($name == 'setData')
829 {
830 $this->setDataThis($arguments[0]);
831 }
832 else if ($name == 'clearData')
833 {
834 $this->clearDataThis();
835 }
836 else if ($name == 'addData')
837 {
838 $this->addDataThis($arguments[0]);
839 }
840 else if ($name == 'getData')
841 {
842 return $this->getDataThis($arguments[0]);
843 }
844 }
845
846 public static function __callStatic($name, $arguments)
847 {
848 if ($name == 'setData')
849 {
850 self::setDataStatic($arguments[0], $arguments[1]);
851 }
852 else if ($name == 'clearData')
853 {
854 self::clearDataStatic($arguments[0]);
855 }
856 else if ($name == 'addData')
857 {
858 self::addDataStatic($arguments[0], $arguments[1]);
859 }
860 else if ($name == 'getData')
861 {
862 return self::getDataStatic($arguments[0]);
863 }
864 }
865
866 public static function clearDataStatic($itemForce=NULL)
867 {
868 $MIOLO = MIOLO::getInstance();
869 $cpaintArgument = SdAjaxFields($MIOLO->_REQUEST("cpaint_argument"));
870 $randomid = SAGU::NVL($MIOLO->_REQUEST("randomid"), $cpaintArgument->randomid);
871
872 if ( $itemForce )
873 {
874 $item = $itemForce;
875 }
876
877 unset($_SESSION['MSubDetail' . $randomid][$item]);
878 }
879
884 public function removeData($arrayItem)
885 {
886 $MIOLO = MIOLO::getInstance();
887 $cpaintArgument = SdAjaxFields($MIOLO->_REQUEST("cpaint_argument"));
888 $randomid = SAGU::NVL($MIOLO->_REQUEST("randomid"), $cpaintArgument->randomid);
889 unset($_SESSION['MSubDetail' . $randomid][$this->item][$arrayItem]);
890 }
891
892 public function generateRequiredLabels($requireds, $fields=NULL)
893 {
894 if ( !$fields )
895 {
896 foreach ( $this->fields as $field )
897 {
898 if ( $field instanceof MContainer )
899 {
900 $this->generateRequiredLabels($requireds, $field->getControls());
901 continue;
902 }
903
904 if ( in_array($field->id, $requireds) )
905 {
906 $label = new MFieldLabel($field->id, $field->label);
907 $label->setClass(MControl::CLASS_CAPTION_REQUIRED);
908 $field->label = $label->generate();
909 }
910 }
911 }
912 else
913 {
914 foreach ( $fields as $field )
915 {
916 if ( in_array($field->id, $requireds) )
917 {
918 $label = new MFieldLabel($field->id, $field->label);
919 $label->setClass(MControl::CLASS_CAPTION);
920 $label->setClass(MControl::CLASS_CAPTION_REQUIRED);
921 $field->label = $label->generate();
922 }
923 }
924 }
925 }
926
927 public function generate($doNothing=false, $itemForce = NULL, $isStaticCall = false)
928 {
929 $MIOLO = MIOLO::getInstance();
930 $url = str_replace('&amp;', '&', $MIOLO->getCurrentURL());
931 $MIOLO->getRequiredJS4Ajax();
932
933 // add a '*' to required labels
934 if ( $this && !$isStaticCall )
935 {
936 $validators = MSubDetail::getValidators($this->item);
937
938 $requireds = array();
939 if ( is_array($validators) )
940 {
941 foreach ( $validators as $validator )
942 {
943 if ( $validator->type == 'required' )
944 {
945 $requireds[] = $validator->field;
946 }
947 }
948 }
949
950 $this->generateRequiredLabels($requireds);
951 }
952
953 if ( $this && !$isStaticCall )
954 {
955 $item = $this->item;
956 }
957 if ( $itemForce )
958 {
959 $item = $itemForce;
960 }
961 if ( !$item )
962 {
963 return '';
964 }
965
966 $jsGetFieldValues = self::jsGetFieldValues();
967
968 $code = <<<SCRIPT
969<script>
970$jsGetFieldValues
971
972// Converts + => and make unescape (to receive data from server...
973function decode(str)
974{
975 if (str)
976 {
977 return unescape(str.replace(/\+/g, ' '));
978 }
979 else
980 {
981 return '';
982 }
983}
984function mioloDoAjax(phpFunction, responseElement)
985{
986 args = getFieldValues();
987 if (responseElement)
988 {
989 document.getElementById('divResponse').value = responseElement;
990 }
991 MIOLO_ajaxCall('$url', 'POST', phpFunction, args, ajaxResponse, 'TEXT');
992}
993
994function ajaxResponse( result )
995{
996 MIOLO_parseAjaxJavascript(result);
997 divResponse = document.getElementById('divResponse').value;
998 document.getElementById(divResponse).innerHTML = result;
999
1000}
1001
1002</script>
1003SCRIPT;
1004
1005
1006 $titles = MSubDetail::getSessionValue('titles', $item);
1007 $actions = MSubDetail::getSessionValue('actions', $item);
1008 $columns = MSubDetail::getSessionValue('columns', $item);
1009
1010 if ( $actions )
1011 {
1012 $titles = array_merge(array( _M('Actions') ), $titles);
1013
1014 unset($temp);
1015 $temp->align = 'left';
1016 $temp->title = _M('Actions');
1017 $temp->width = '';
1018 $temp->visible = true;
1019 $temp->options = '';
1020
1021 $columns = array_filter($columns);
1022 $columns = array_merge(array( $temp ), $columns); //limpa
1023 }
1024
1025 $itens = self::getData($item);
1026
1027 $table = new MTableRaw('', array(), $titles);
1028 $table->setAlternate(true);
1029 $table->addAttribute('width', '100%');
1030 $table->addBoxStyle('width', '100%');
1031 $table->addStyle('width', '100%');
1032 $table->addAttribute("style", "width:100%");
1033 $table->addAttribute("cellspacing", "0");
1034 $table->setCellAttribute(0, 0, "width", '5%');
1035
1036 if ( $this && !$isStaticCall )
1037 {
1038 if ( $this->showButtons )
1039 {
1040 $addTitle = _M('Add');
1041 if ( $this->updateButton )
1042 {
1043 $js = "document.getElementById( 'MSubDetail' ).value = '$item';
1044 mioloDoAjax( 'forceAddToTable' )";
1045 $buttons[] = new MButton('addData' . $item, $addTitle, $js);
1046 $js = "document.getElementById( 'MSubDetail' ).value = '$item';
1047 mioloDoAjax( 'addToTable' )";
1048 $buttons[] = new MButton('updateData' . $item, _M('Update'), $js);
1049 }
1050 else
1051 {
1052 $js = "document.getElementById( 'MSubDetail' ).value = '$item'; mioloDoAjax('addToTable');";
1053 $buttons[] = new MButton('addData' . $item, $addTitle, $js);
1054 }
1055 $js = "document.getElementById('MSubDetail').value = '$item'; mioloDoAjax( 'clearTableFields'); document.getElementById('addData$item').innerHTML = '$addTitle'";
1056 $buttons[] = new MButton('clearData' . $item, _M('Clear'), $js);
1057 $divButtons = new MDIV('divButtons', $buttons);
1058 $divButtons->addBoxStyle('width', '100%');
1059 $divButtons->addBoxStyle('text-align', 'center');
1060
1061 $this->addControl($divButtons);
1062 }
1063 }
1064
1065 if ( $itens )
1066 {
1067 $tablePos = 0;
1068
1069 foreach ( $itens as $i => $info )
1070 {
1071 if ( !$info->removeData )
1072 {
1073 unset($encodedInfo);
1074 foreach ( $info as $l => $i )
1075 {
1076 if ( is_string($i) )
1077 {
1078 $encodedInfo->$l = urlencode($i);
1079 $encodedInfo->$l = str_replace("\n", '\n', $encodedInfo->$l);
1080 }
1081 else
1082 {
1083 $encodedInfo->$l = $i;
1084 }
1085 }
1086
1087 unset($args);
1088
1089 if ( $actions )
1090 {
1091 $actions[$i] = self::generateActionString($i, $item);
1092 $args[] = $actions[$info->arrayItem];
1093 $table->setCellAttribute($i, 0, "align", 'left');
1094 }
1095
1096 foreach ( $columns as $line => $column )
1097 {
1098 if ( $column->visible == true )
1099 {
1100 if ( $column->options )
1101 {
1102 $opt = $column->options;
1103
1104 if ( $column->replace )
1105 {
1106 $args[] = $column->replace[$info->$opt];
1107 }
1108 elseif ( strpos($info->$opt, 'type_select-multiple') !== false )
1109 {
1110 $value = substr($info->$opt, strlen('type_select-multiple'));
1111 $itens[$i]->$opt = explode('@', $value);
1112 $args[] = str_replace('@', '<br/>', $value);
1113 }
1114 else
1115 {
1116 $args[] = $info->$opt;
1117 }
1118
1119 if ( $column->align )
1120 {
1121 $table->setCellAttribute($tablePos, $line, 'align', $column->align);
1122 }
1123 if ( $column->width )
1124 {
1125 $table->setCellAttribute($tablePos, $line, 'width', $column->width);
1126 }
1127 }
1128 }
1129
1130 $table->setCellAttribute($i, $line, 'id', 'MTableRow-' . $i . '-' . $line);
1131 }
1132
1133 $tableData[] = $args;
1134
1135 $tablePos ++;
1136 }
1137 }
1138 }
1139 $table->array = $tableData;
1140 $temp = $table->generate();
1141
1142 $tempData = self::getData($item);
1143 if ( $tempData )
1144 {
1145 foreach ( $tempData as $line => $info )
1146 {
1147 unset($stringTemp);
1148
1149 foreach ( $columns as $l => $i )
1150 {
1151 $options = $i->options;
1152 if ( $options )
1153 {
1154 if ( strpos($info->$options, 'type_select-multiple') !== false )
1155 {
1156 $value = implode('@', $info->$options);
1157 $stringTemp .= empty($value) ? "$options=$value&" : "$options=&";
1158 }
1159 else
1160 {
1161 $stringTemp .= $options . '=' . urlencode($info->$options) . '&';
1162 }
1163 }
1164 }
1165 unset($tempField);
1166 $tempField = new MHiddenField(self::HIDDEN_FIELD_PREFIX_NAME . "_{$item}[]", $stringTemp);
1167 $temp .= $code;
1168 $temp .= $tempField->generate();
1169 }
1170 }
1171
1172 if ( !$itemForce && !$isStaticCall )
1173 {
1174 $div = new MDiv('div' . $item, $temp);
1175
1176 if ( $this->overflowWidth )
1177 {
1178 $div->addBoxStyle('overflow-x', $this->overflowType);
1179 $div->addBoxStyle('width', $this->overflowWidth . 'px');
1180 }
1181 else
1182 {
1183 $div->addBoxStyle('width', '100%');
1184 }
1185 $this->controls->add($div);
1186 return parent::generate();
1187 }
1188 else
1189 {
1190 return $temp;
1191 }
1192 }
1193
1200 public function update($data = NULL, $itemForce)
1201 {
1202 $MIOLO = MIOLO::getInstance();
1203 MSubDetail::setData($data, $itemForce);
1204 $subClass = new MSubDetail(rand(), rand());
1205 $generate = $subClass->generate(false, $itemForce, true);
1206 SdsetResponse($generate, 'div' . $itemForce);
1207 }
1208
1209 public function checkRemovedItem($field, $line)
1210 {
1211 $MIOLO = MIOLO::getInstance();
1212 $cpaintArgument = SdAjaxFields($MIOLO->_REQUEST("cpaint_argument"));
1213 $randomid = SAGU::NVL($MIOLO->_REQUEST("randomid"), $cpaintArgument->randomid);
1214
1215 return (isset($_SESSION['MSubDetail' . $randomid][$field][$line]->removeData) && $_SESSION['MSubDetail' . $randomid][$field][$line]->removeData == 1);
1216 }
1217
1218 public function prepareArgs($formData)
1219 {
1220 $f = null;
1221
1222 foreach ( $formData as $index => $fullValue )
1223 {
1224 if ( preg_match('/'.self::HIDDEN_FIELD_PREFIX_NAME . "_/", $index) )
1225 {
1226 $fieldIndex = str_replace(self::HIDDEN_FIELD_PREFIX_NAME . "_", "", $index);
1227
1228 if ( !is_array($fullValue) )
1229 {
1230 continue;
1231 }
1232
1233 foreach ( $fullValue as $x => $content )
1234 {
1235 $values = explode("&", urldecode($content));
1236
1237 if ( !is_array($values) )
1238 {
1239 continue;
1240 }
1241
1242 foreach ( $values as $fieldContent )
1243 {
1244 list($fieldName, $fieldValue) = explode("=", $fieldContent);
1245
1246 if ( !strlen($fieldName) )
1247 {
1248 continue;
1249 }
1250 $fieldValue = strlen($fieldValue) ? $fieldValue : null;
1251
1252 if ( !MSubDetail::checkRemovedItem($fieldIndex, $x) )
1253 {
1254 eval("\$f->{$fieldIndex}[{$x}]->{$fieldName} = \$fieldValue;");
1255 }
1256 }
1257 }
1258 }
1259 else
1260 {
1261 $f->$index = $fullValue;
1262 }
1263 }
1264
1265 return $f;
1266 }
1267
1268 function sumFields($fields, $float=false)
1269 {
1270 $MIOLO = MIOLO::getInstance();
1271 $cpaintArgument = SdAjaxFields($MIOLO->_REQUEST("cpaint_argument"));
1272 $randomid = SAGU::NVL($MIOLO->_REQUEST("randomid"), $cpaintArgument->randomid);
1273
1274 if ( is_array($fields) )
1275 {
1276 $_SESSION['MSubDetail' . $randomid]['field_' . $this->item] = $fields;
1277 }
1278 $_SESSION['MSubDetail' . $randomid]['type_' . $this->item] = $float;
1279 return true;
1280 }
1281
1282 public function addActionEvent($action, $function, $class = null)
1283 {
1284 $_SESSION[$this->item]->event[$action]->function = $function;
1285 $_SESSION[$this->item]->event[$action]->class = $class;
1286 }
1287
1288
1289 public static function parseMultiTextValue($value)
1290 {
1291 $codeValue = array();
1292 $data = explode('<br/>', $value);
1293
1294 foreach ( (array) $data as $line )
1295 {
1296 $string = trim($line, '[]');
1297 $codeValue[] = explode('] [', $string);
1298 }
1299
1300 return $codeValue;
1301 }
1302
1303 public static function jsGetFieldValues()
1304 {
1305
1306 if ( strpos(MIOLO::getInstance()->getTheme()->id, "sbootstrap") === false )
1307 {
1308 $ckEditorScript = "eval('value = CKEDITOR.instances.' + field.id + '.getData();');";
1309 }
1310 else
1311 {
1312 $ckEditorScript = "eval('value = ck_editors.get(\"' + field.id + '\").getData();');";
1313 }
1314
1315 return "
1316 // get all data from form (the data is cleaned with getAjaxFields from class MTableRawSession)
1317 // ir uses escape, that convert characters to html entities
1318 function getFieldValues()
1319 {
1320 var temp ='';
1321
1322 for (i=0; i<document.forms[0].elements.length; i++)
1323 {
1324 var field = document.forms[0].elements[i];
1325
1326 if ( field.type == 'checkbox' || field.type == 'radio' )
1327 {
1328 if ( field.checked )
1329 {
1330 temp += field.id + '|' + field.value + '#';
1331 }
1332 }
1333 else if ( field.className.match(/select m-multitext.*/) )
1334 {
1335 temp += field.id + '|type_select-multiple';
1336 for ( var j = 0; j < field.options.length; j++ )
1337 {
1338 if ( j != 0 )
1339 {
1340 temp += '@';
1341 }
1342 temp += escape(field.options[j].value);
1343 }
1344 temp += '#';
1345 }
1346 else if ( field.className.match(/m-combo.*/) )
1347 {
1348 var selecionou=false;
1349 temp += field.id + '|';
1350
1351 if( field.options )
1352 {
1353 for ( var j = 0; j < field.options.length; j++ )
1354 {
1355 if (field.options[j].selected)
1356 {
1357 if ( selecionou )
1358 {
1359 temp += ',';
1360 }
1361
1362 temp += field.options[j].value;
1363 selecionou = true;
1364 }
1365 }
1366 }
1367 temp += '#';
1368
1369 }
1370 else if ( $(field).hasClass('m-multiline-field') && $(field).hasClass('m-editor') )
1371 {
1372 $ckEditorScript
1373 temp += field.id + '|' + escape(value) + '#';
1374 }
1375 else if ( field.type == 'file' && field.value.length > 0 )
1376 {
1377 var args = new FormData();
1378 var file = field.files[0];
1379 args.append(field.name, file);
1380
1381 $.ajax({
1382 url: 'upload.php',
1383 data: args,
1384 method: 'POST',
1385 cache: false,
1386 contentType: false,
1387 processData: false,
1388 async: false,
1389 success: function(data) {
1390 temp += field.id + '|' + data + '#';
1391 },
1392 complete: function(){}
1393 });
1394 }
1395 else
1396 {
1397 temp += field.id + '|' + escape(field.value) + '#';
1398 }
1399 }
1400 return temp;
1401 }
1402 ";
1403 }
1404}
1405
1409function autoRemoveAction($data)
1410{
1412 $randomid = SAGU::NVL($MIOLO->_REQUEST("randomid"), $data->randomid);
1413 $object = $data->MSubDetail;
1414 $_SESSION['MSubDetail' . $randomid][$object][$data->arrayItemTemp]->removeData = true;
1415 $subClass = new MSubDetail(rand(), rand());
1416 SdsetResponse($subClass->generate(false, $object, true), 'div' . $object, "document.getElementById('arrayItemTemp').value = '';");
1417}
1418
1419function autoForceAddAction($args)
1420{
1421 $args->arrayItemTemp = null;
1422 autoAddAction($args);
1423}
1424
1425function autoAddAction($data, $object = NULL, $errors = NULL)
1426{
1428
1429 $randomid = SAGU::NVL($MIOLO->_REQUEST("randomid"), $data->randomid);
1430
1431 if ( $_SESSION['MSubDetail' . $randomid]['type_' . $data->MSubDetail] == 'TRUE' )
1432 {
1433 formatFieldToFloat($data, $data->MSubDetail);
1434 }
1435
1436 unset($temp);
1437 $validators = MSubDetail::getValidators($data->MSubDetail);
1438
1439 $hideButtonsOnAdd = MSubDetail::getHideButtonsOnAdd($data->MSubDetail);
1440
1442 $_errors = $gValidator->validate($data, $validators);
1443 if ( $_errors )
1444 {
1445 $errors = ($errors) ? array_merge($errors, $_errors) : $_errors;
1446 }
1447
1448 if ( $errors )
1449 {
1450 $temp.= '<span style="color: red;">';
1451 $temp.= '<b>' . _M('The following errors were found') . ':</b>';
1452 $temp.= '<ul>';
1453 foreach ( $errors as $value )
1454 {
1455 $temp.= '<li>' . $value . '</li>';
1456 }
1457 $temp.= '</ul>';
1458 $temp.= '</span>';
1459 }
1460 else
1461 {
1462 foreach ( $data as $id => $value )
1463 {
1464 if ( substr($value, 0, strlen('type_select-multiple')) == 'type_select-multiple' )
1465 {
1466 $multiples[$id] = str_replace('@', '<br/>', substr($value, strlen('type_select-multiple')));
1467 }
1468 }
1469
1470 if ( !empty($multiples) )
1471 {
1472 foreach ( $multiples as $id => $value )
1473 {
1474 $data->$id = $value;
1475 }
1476 }
1477
1478 if ( $data->arrayItemTemp || $data->arrayItemTemp === '0' )
1479 {
1480 MSubDetail::defineData($data->arrayItemTemp, $data, $data->MSubDetail);
1481 }
1482 else
1483 {
1484 MSubDetail::addData($data, $data->MSubDetail);
1485 }
1486
1487 $fieldNames = MSubDetail::getSessionValue('fieldNames', $data->MSubDetail);
1488
1489 foreach ( $fieldNames as $value )
1490 {
1491 $script .= <<<JS
1492var element = document.getElementById('{$value}');
1493var defaultValue = '';
1494var defaultValueField = document.getElementById('MSubDetail_defaultValue_{$value}');
1495
1496if ( defaultValueField )
1497{
1498 defaultValue = defaultValueField.value;
1499}
1500
1501if ( element && '{$value}'.substr(0, 24) != 'MSubDetail_defaultValue_' )
1502{
1503 if ( element.type == 'checkbox' || element.type == 'radio' )
1504 {
1505 if ( defaultValueField )
1506 {
1507 element.checked = defaultValueField.value != '';
1508 }
1509 else
1510 {
1511 element.checked = false;
1512 }
1513 }
1514 else if ( element.type != 'hidden' && element.className != 'm-readonly' )
1515 {
1516 if ( element.className == 'select m-multitext' )
1517 {
1518 element.innerHTML = defaultValue;
1519 }
1520 else if ( $(element).hasClass('m-multiline-field') && $(element).hasClass('m-editor') )
1521 {
1522JS;
1523
1524 if ( strpos(MIOLO::getInstance()->getTheme()->id, "sbootstrap") === false )
1525 {
1526 $script .= <<<JS
1527 eval('value = CKEDITOR.instances.' + element.id + '.setData("");');
1528JS;
1529 }
1530 else
1531 {
1532 $script .= <<<JS
1533 eval('value = ck_editors.get(\"' + element.id + '\").setData("");');
1534JS;
1535 }
1536
1537 $script .= <<<JS
1538 element.value = defaultValue;
1539 }
1540 else if ( element.tagName == 'SELECT' && defaultValue == '' && element.options && element.options[0] )
1541 {
1542 element.value = element.options[0].value;
1543 }
1544 else if ( $(element).hasClass("m-multi-select") )
1545 {
1546 element.value = defaultValue;
1547 }
1548 else
1549 {
1550 element.value = defaultValue;
1551
1552 // sEscolha
1553 if ( $('#'+$(element).prop('name')+"Description").length > 0 )
1554 {
1555 $('#'+$(element).prop('name')+"Description").val(element.value);
1556 }
1557
1558 // Float field
1559 if ( $('#'+$(element).prop('name')+"user").length > 0 )
1560 {
1561 $('#'+$(element).prop('name')+"user").val(element.value);
1562 }
1563 }
1564 }
1565}
1566
1567if ( document.getElementById('{$value}_sel') )
1568{
1569 document.getElementById('{$value}_sel').value = defaultValue;
1570}
1571JS;
1572 }
1573 $script .= "document.getElementById('arrayItemTemp').value='';";
1574 $addTitle = _M('Add');
1575 $script .= "document.getElementById('addData{$data->MSubDetail}').innerHTML = '$addTitle';";
1576
1577 if ( $hideButtonsOnAdd )
1578 {
1579 $script .= "document.getElementById('divButtons').style.display = 'none';";
1580 }
1581
1582 }
1583
1584 $subClass = new MSubDetail(rand(), rand());
1585 $temp .= $subClass->generate(false, $data->MSubDetail, true);
1586 SdsetResponse($temp, 'div' . $data->MSubDetail, $script);
1587}
1588
1594function autoClearAction($args)
1595{
1596 $fieldNames = MSubDetail::getSessionValue('fieldNames', $args->MSubDetail);
1597
1598 if ( is_array($fieldNames) )
1599 {
1600 foreach ( $fieldNames as $line => $info )
1601 {
1602 if ( strpos(MIOLO::getInstance()->getTheme()->id, "sbootstrap") === false )
1603 {
1604 $ckEditorScript = "value = CKEDITOR.instances.$info.setData(\"\")";
1605 }
1606 else
1607 {
1608 $ckEditorScript = "value = ck_editors.get(\"$info\").setData(\"\")";
1609 }
1610
1611 $temp.= <<<JS
1612var element = document.getElementById('{$info}');
1613
1614if( element.type != 'hidden' &&
1615 element.className != 'm-readonly' &&
1616 !$(element).hasClass('m-tag-select') &&
1617 (!$(element).hasClass('m-combo') && !$(element).attr('multiple') ) &&
1618 element.type != 'checkbox' &&
1619 element.type != 'radio' )
1620{
1621 element.value = '';
1622
1623 if ( $(element).hasClass('m-multiline-field') && $(element).hasClass('m-editor') )
1624 {
1625 eval('$ckEditorScript');
1626 }
1627
1628 // sEscolha
1629 if ( $('#'+$(element).prop('name')+"Description").length > 0 )
1630 {
1631 $('#'+$(element).prop('name')+"Description").val('');
1632 }
1633
1634 // Float field
1635 if ( $('#'+$(element).prop('name')+"user").length > 0 )
1636 {
1637 $('#'+$(element).prop('name')+"user").val('');
1638 }
1639
1640}
1641else if ( $(element).hasClass("m-multi-select") )
1642{
1643 element.value = '';
1644}
1645else if ($(element).hasClass('m-tag-select'))
1646{
1647 $(element).val('').change();
1648}
1649else if ( element.className == 'select m-multitext' )
1650{
1651 element.innerHTML = '';
1652}
1653JS;
1654
1655 // MComboBox
1656 $temp.= "
1657 if( document.getElementById('{$info}_sel') )
1658 {
1659 document.getElementById('{$info}_sel').value = '';
1660 }";
1661 }
1662 }
1663
1664 $temp .= "document.getElementById('arrayItemTemp').value='';";
1665
1666 if ( MSubDetail::getHideButtonsOnAdd($args->MSubDetail) )
1667 {
1668 $temp .= "document.getElementById('divButtons').style.display = 'none';";
1669 }
1670
1671 $subClass = new MSubDetail(rand(), rand());
1672 SdsetResponse($subClass->generate(false, $args->MSubDetail, true), 'div' . $args->MSubDetail, $temp);
1673}
1674
1675function autoUpAction($data)
1676{
1677 $object = $data->MSubDetail;
1678 $data->sessionItem = $data->MSubDetail;
1679 $nivel = $data->arrayItem;
1680 if ( !$nivel )
1681 {
1682 $nivel = $data->arrayItemTemp;
1683 }
1684 if ( $nivel != 0 )
1685 {
1686 $tempObjAtual = MSubDetail::getDataItem($nivel, $object);
1687 $tempObjSuperior = MSubDetail::getDataItem($nivel - 1, $object);
1688 MSubDetail::defineData($nivel - 1, $tempObjAtual, $object);
1689 MSubDetail::defineData($nivel, $tempObjSuperior, $object);
1690 }
1692
1693 $subClass = new MSubDetail(rand(), rand());
1694 SdsetResponse($subClass->generate(false, $object, true), 'div' . $data->MSubDetail);
1695}
1696
1697function autoDownAction($data)
1698{
1699 $object = $data->MSubDetail;
1700 $data->sessionItem = $data->MSubDetail;
1701 $nivel = $data->arrayItem;
1702 if ( !$nivel )
1703 {
1704 $nivel = $data->arrayItemTemp;
1705 }
1706 $item = MSubDetail::getData($object);
1707 if ( $nivel < count($item) - 1 )
1708 {
1709 $tempObjAtual = MSubDetail::getDataItem($nivel, $object);
1710 $tempObjInferior = MSubDetail::getDataItem($nivel + 1, $object);
1711 MSubDetail::defineData($nivel + 1, $tempObjAtual, $object);
1712 MSubDetail::defineData($nivel, $tempObjInferior, $object);
1713 }
1715 $subClass = new MSubDetail(rand(), rand());
1716 SdsetResponse($subClass->generate(false, $object, true), 'div' . $data->MSubDetail);
1717}
1718
1720{
1721 $multiples = array();
1722
1723 foreach ( $args as $id => $a )
1724 {
1725 if ( $a == 'type_select-multiple' )
1726 {
1727 $value = '';
1728 $url = $args->{"MSubDetail_personellData[]"};
1729
1730 $data = explode('&', $url);
1731
1732 foreach ( $data as $param )
1733 {
1734 if ( strpos($param, $id) !== false )
1735 {
1736 list($param, $value) = explode('=', $param);
1737 break;
1738 }
1739 }
1740
1741 $multiples[$id] = $value;
1742 }
1743 }
1744
1745 return $multiples;
1746}
1747
1753function autoEditAction($args, $values=NULL)
1754{
1755 if ( !$values )
1756 {
1757 $values = MSubDetail::getDataItem($args->arrayItemTemp, $args->MSubDetail);
1758 }
1759
1760 $updateButton = MSubDetail::getUpdateButton($args->MSubDetail);
1761 $hideButtonsOnAdd = MSubDetail::getHideButtonsOnAdd($args->MSubDetail);
1762 $fieldNames = MSubDetail::getSessionValue('fieldNames', $args->MSubDetail);
1763 if ( is_array($fieldNames) )
1764 {
1765
1766 foreach ( $fieldNames as $line => $info )
1767 {
1768 $value = $values->$info;
1769 $value = str_replace("\n", '\n', $value);
1770 $value = str_replace("\r", '\r', $value);
1771 $value = str_replace("'", "\'", $value);
1772 $value = str_replace('"', '\"', $value);
1773
1774 if ( is_array($value) )
1775 {
1776 $value = implode('<br/>', $value);
1777 }
1778
1779 if ( strpos(MIOLO::getInstance()->getTheme()->id, "sbootstrap") === false )
1780 {
1781 $ckEditorScript = "var editor = CKEDITOR.instances.' + field.id + ';";
1782 }
1783 else
1784 {
1785 $ckEditorScript = "var editor = ck_editors.get(\"' + field.id + '\");";
1786 }
1787
1788 $temp .= <<<JS
1789function inArray(needle, haystack) {
1790 var length = haystack.length;
1791 for(var i = 0; i < length; i++) {
1792 if(haystack[i] == needle) return true;
1793 }
1794 return false;
1795}
1796
1797var field = document.getElementById('$info');
1798
1799try
1800{
1801
1802
1803 if ( $(field).hasClass('m-tag-select') )
1804 {
1805 $(field).val('$value').change();
1806 }
1807 else if ( field.type == 'checkbox' )
1808 {
1809 field.checked = field.value == '$value';
1810 }
1811 else if ( field.type == 'radio' )
1812 {
1813 var radios = document.getElementsByName('$info');
1814
1815 for ( i = 0; i < radios.length; i++ )
1816 {
1817 radios[i].checked = radios[i].value == '$value';
1818 }
1819 }
1820 else if ( $(field).hasClass('m-multiline-field') && $(field).hasClass('m-editor') )
1821 {
1822 eval('$ckEditorScript');
1823 editor.setData('$value');
1824 field.value = '$value';
1825 }
1826 else if ( field.className == 'm-combo' )
1827 {
1828 var v = '$value'.split(',');
1829 for ( var j = 0; j < field.options.length; j++ )
1830 {
1831 if (inArray(field.options[j].value, v) )
1832 {
1833 field.options[j].selected = true;
1834 }
1835 else
1836 {
1837 field.options[j].selected = false;
1838 }
1839 }
1840
1841 }
1842 else if ( field.className == 'select m-multitext' )
1843 {
1844 var v = '$value'.split('<br/>');
1845
1846 field.innerHTML = '';
1847 for ( i=0; i<v.length; i++)
1848 {
1849 if ( !v[i] )
1850 {
1851 continue;
1852 }
1853
1854 option = document.createElement('option');
1855 option.value = v[i];
1856 option.innerHTML = v[i];
1857
1858 field.appendChild(option);
1859 }
1860
1861 }
1862 else
1863 {
1864 field.value = '$value';
1865
1866 // sEscolha
1867 if ( $('#'+$(field).prop('name')+"Description").length > 0 )
1868 {
1869 $(field).change();
1870 }
1871
1872 // Float field
1873 if ( $('#'+$(field).prop('name')+"user").length > 0 )
1874 {
1875 $('#'+$(field).prop('name')+"user").val(field.value);
1876 }
1877 }
1878}
1879catch ( err )
1880{
1881}
1882
1883var radio;
1884for (i=0; i < 10; i++)
1885{
1886 radio = document.getElementById('{$info}_' + i);
1887 if ( radio && radio.value == '{$value}' )
1888 {
1889 radio.checked = true;
1890 }
1891}
1892if( document.getElementById('{$info}_sel') )
1893{
1894 document.getElementById('{$info}_sel').value='{$value}';
1895}
1896JS;
1897
1898 if ( !$updateButton )
1899 {
1900 $update = _M('Update');
1901 $temp .= "document.getElementById('addData$args->MSubDetail').innerHTML = '$update';\n";
1902 }
1903 if ( $hideButtonsOnAdd )
1904 {
1905 $temp .= "document.getElementById('divButtons').style.display = 'block';\n";
1906 }
1907 $subClass = new MSubDetail(rand(), rand());
1908 SdsetResponse($subClass->generate(false, $object, true), 'div' . $object, $temp);
1909 }
1910 }
1911
1912 $subClass = new MSubDetail(rand(), rand());
1913 SdsetResponse($subClass->generate(false, $args->MSubDetail, true), 'div' . $args->MSubDetail, $temp);
1914}
1915
1916function autoDuplicateAction($args)
1917{
1918 $multiples = getMultipleSelectData($args);
1919
1920 $values = MSubDetail::getDataItem($args->arrayItemTemp, $args->MSubDetail);
1921 $fieldNames = MSubDetail::getSessionValue('fieldNames', $args->MSubDetail);
1922 if ( is_array($fieldNames) )
1923 {
1924
1925 foreach ( $fieldNames as $line => $info )
1926 {
1927 $value = $values->$info;
1928 $value = str_replace("\n", '\n', $value);
1929 $value = str_replace("\r", '\r', $value);
1930 $value = str_replace("'", "\'", $value);
1931 $value = str_replace('"', '\"', $value);
1932
1933 if ( !empty($multiples) && $multiples[$info] )
1934 {
1935 $value = $multiples[$info];
1936 }
1937
1938 if ( strpos(MIOLO::getInstance()->getTheme()->id, "sbootstrap") === false )
1939 {
1940 $ckEditorScript = "var editor = CKEDITOR.instances.' + field.id + ';";
1941 }
1942 else
1943 {
1944 $ckEditorScript = "var editor = ck_editors.get(\"' + field.id + '\");";
1945 }
1946
1947
1948 $temp .= <<<JS
1949
1950var field = document.getElementById('$info');
1951
1952if ( field.type == 'checkbox' )
1953{
1954 field.checked = field.value == '$value';
1955}
1956else if ( field.type == 'radio' )
1957{
1958 var radios = document.getElementsByName('$info');
1959
1960 for ( i = 0; i < radios.length; i++ )
1961 {
1962 radios[i].checked = radios[i].value == '$value';
1963 }
1964}
1965else if ( $(field).hasClass('m-multiline-field') && $(field).hasClass('m-editor') )
1966{
1967 eval('$ckEditorScript');
1968 editor.setData('$value');
1969 field.value = '$value';
1970}
1971else if ( field.className == 'select m-multitext' )
1972{
1973 var v = '$value'.split('@');
1974
1975 for ( i=0; i<v.length; i++)
1976 {
1977 if ( !v[i] )
1978 {
1979 continue;
1980 }
1981
1982 option = document.createElement('option');
1983 option.value = v[i];
1984 option.innerHTML = v[i];
1985
1986 field.appendChild(option);
1987 }
1988}
1989else
1990{
1991 field.value = '$value';
1992}
1993
1994var radio;
1995for (i=0; i < 10; i++)
1996{
1997 radio = document.getElementById('{$info}_' + i);
1998 if ( radio && radio.value == '{$value}' )
1999 {
2000 radio.checked = true;
2001 }
2002}
2003if( document.getElementById('{$info}_sel') )
2004{
2005 document.getElementById('{$info}_sel').value='{$value}';
2006}
2007JS;
2008 $subClass = new MSubDetail(rand(), rand());
2009 SdsetResponse($subClass->generate(false, $object, true), 'div' . $object, $temp);
2010 }
2011 }
2012
2013 $temp .= "document.getElementById('arrayItemTemp').value = '';";
2014
2015 $subClass = new MSubDetail(rand(), rand());
2016 SdsetResponse($subClass->generate(false, $args->MSubDetail, true), 'div' . $args->MSubDetail, $temp);
2017}
2018
2019function sumValue($table)
2020{
2022 $cpaintArgument = SdAjaxFields($MIOLO->_REQUEST("cpaint_argument"));
2023 $randomid = SAGU::NVL($MIOLO->_REQUEST("randomid"), $cpaintArgument->randomid);
2024
2025 if ( $_SESSION['MSubDetail' . $randomid]['field_' . $table] )
2026 {
2027 foreach ( array_keys($_SESSION['MSubDetail' . $randomid]['field_' . $table]) as $field )
2028 {
2029 if ( $_SESSION['MSubDetail' . $randomid][$table] )
2030 {
2031 foreach ( $_SESSION['MSubDetail' . $randomid][$table] as $f )
2032 {
2033 if ( $f->removeData != 'TRUE' )
2034 {
2035 $total[] = $f->$field;
2036 }
2037 }
2038 if ( is_array($total) && array_sum($total) )
2039 {
2040 $t = array_sum($total);
2041 }
2042 else
2043 {
2044 $t = '0';
2045 }
2046 if ( $_SESSION['MSubDetail' . $randomid]['type_' . $table] == 'TRUE' )
2047 {
2048 $js = '<script>
2049 document.getElementById("' . $_SESSION['MSubDetail' . $randomid]['field_' . $table][$field] . '").value = (' . $t . ').toFixed(2);
2050 </script>';
2051 }
2052 else
2053 {
2054 $js = '<script>
2055 document.getElementById("' . $_SESSION['MSubDetail' . $randomid]['field_' . $table][$field] . '").value = ' . $t . ';
2056 </script>';
2057 }
2058 echo $js;
2059 unset($total);
2060 }
2061 else
2062 {
2063 return false;
2064 }
2065 }
2066 }
2067 return true;
2068}
2069
2070function removeFromTable($args)
2071{
2072 $data = SdAjaxFields($args);
2073
2074 if ( ($event = $_SESSION[$data->MSubDetail]->event[MSubDetail::EVENT_REMOVE] ) )
2075 {
2076 $class = $event->class;
2077 $function = $event->function;
2078 $values = MSubDetail::getDataItem($data->arrayItemTemp, $data->MSubDetail);
2079
2080 if ( $class )
2081 {
2082 eval("{$class}::{$function}(\$values);");
2083 }
2084 else
2085 {
2086 eval("{$function}(\$values);");
2087 }
2088 }
2089
2090 autoRemoveAction($data);
2091 sumValue($data->MSubDetail);
2092}
2093
2094function upFromTable($args)
2095{
2097 $cpaintArgument = SdAjaxFields($MIOLO->_REQUEST("cpaint_argument"));
2098 $randomid = SAGU::NVL($MIOLO->_REQUEST("randomid"), $cpaintArgument->randomid);
2099 $data = sdAjaxFields($args);
2100
2101 if ( ($event = $_SESSION['MSubDetail' . $randomid]['event']['add'] ) )
2102 {
2103 $class = $event->class;
2104 $function = $event->function;
2105 if ( $class )
2106 {
2107 eval("\$data = {$class}::{$function}(\$data);");
2108 }
2109 else
2110 {
2111 eval("\$data = {$function}(\$data);");
2112 }
2113 }
2114 autoUpAction($data);
2115}
2116
2117function downFromTable($args)
2118{
2119 $data = sdAjaxFields($args);
2120 {
2121 $class = $event->class;
2122 $function = $event->function;
2123 if ( $class )
2124 {
2125 eval("\$data = {$class}::{$function}(\$data);");
2126 }
2127 else
2128 {
2129 eval("\$data = {$function}(\$data);");
2130 }
2131 }
2132 autoDownAction($data);
2133}
2134
2135function addToTable($args)
2136{
2137 $data = SdAjaxFields($args);
2138
2139 if ( $event = $_SESSION[$data->MSubDetail]->event[MSubDetail::EVENT_ADD] )
2140 {
2141 $class = $event->class;
2142 $function = $event->function;
2143 if ( $class )
2144 {
2145 eval("\$data = {$class}::{$function}(\$data);");
2146 }
2147 else
2148 {
2149 eval("\$data = {$function}(\$data);");
2150 }
2151 }
2152 autoAddAction($data);
2153 sumValue($data->MSubDetail);
2154}
2155
2156function editFromTable($args)
2157{
2158 $data = SdAjaxFields($args);
2159 $values = clone MSubDetail::getDataItem($data->arrayItemTemp, $data->MSubDetail);
2160
2161 if ( ($event = $_SESSION[$data->MSubDetail]->event[MSubDetail::EVENT_EDIT] ) )
2162 {
2163 $class = $event->class;
2164 $function = $event->function;
2165
2166 if ( $class )
2167 {
2168 eval("\$values = {$class}::{$function}(\$values);");
2169 }
2170 else
2171 {
2172 eval("\$values = {$function}(\$values);");
2173 }
2174
2175 }
2176
2177 autoEditAction($data, $values);
2178 sumValue($data->MSubDetail);
2179
2180
2181}
2182
2183function duplicateFromTable($args)
2184{
2185 $data = SdAjaxFields($args);
2186
2187 if ( $event = $_SESSION[$data->MSubDetail]->event[MSubDetail::EVENT_EDIT] )
2188 {
2189 $class = $event->class;
2190 $function = $event->function;
2191 $values = MSubDetail::getDataItem($data->arrayItemTemp, $data->MSubDetail);
2192 if ( $class )
2193 {
2194 eval("\$values = {$class}::{$function}(\$values);");
2195 }
2196 else
2197 {
2198 eval("\$values = {$function}(\$values);");
2199 }
2200 MSubDetail::defineData($data->arrayItemTemp, $values, $data->MSubDetail);
2201 }
2202
2203 autoDuplicateAction($data);
2204 sumValue($data->MSubDetail);
2205}
2206
2207function clearTableFields($args)
2208{
2209 $data = SdAjaxFields($args);
2210 autoClearAction($data);
2211}
2212
2213function SdsetResponse($fields, $divResponse, $js=null)
2214{
2215 if ( is_array($fields) )
2216 {
2217 foreach ( $fields as $line => $field )
2218 {
2219 echo $field->generate();
2220 }
2221 }
2222 else if ( is_object($fields) )
2223 {
2224 echo $fields->generate();
2225 }
2226 else
2227 {
2228 echo $fields;
2229 }
2231
2232 if ( $divResponse )
2233 {
2234 $js .="document.getElementById('divResponse').value = '$divResponse'; ";
2235 }
2236
2237 echo "<script type='text/javascript'>$js</script>";
2238}
2239
2244function SdAjaxFields($args)
2245{
2246 $args = explode('#', $args[0]); //converts from the "escape" javascript function
2247
2248 foreach ( $args as $line => $info )
2249 {
2250 $temp = explode('|', utf8_encode(urldecode($info)));
2251 if ( ($temp[1] && $temp[1] != 'undefined') || $temp[1] === 0 || $temp[1] == '0' )
2252 {
2253 $data[$temp[0]] = $temp[1];
2254 }
2255 }
2256 $data = (object) $data;
2257
2258 return $data;
2259}
2260
2261function formatFieldToFloat($data, $table)
2262{
2264 $randomid = SAGU::NVL($MIOLO->_REQUEST("randomid"), $data->randomid);
2265
2266 $sKey = array_keys($_SESSION['MSubDetail' . $randomid]['field_' . $table]);
2267 foreach ( $data as $key => $value )
2268 {
2269 if ( $key == $sKey[0] )
2270 {
2271 $data->$key = number_format(str_replace(',', '.', $value), 2, '.', '');
2272 }
2273 }
2274 return $data;
2275}
2276
2278if ( ($f = $MIOLO->page->request('cpaint_function')) != '' )
2279{
2280 if ( function_exists($f) )
2281 {
2282 die($f($MIOLO->page->request('cpaint_argument')));
2283 }
2284}
2285?>
setControls($controls, $recursive=false)
addBoxStyle($name, $value='')
Definição mcontrol.class:819
const FORM_MODE_SHOW_SIDE
Definição mcontrol.class:42
static getCurrentModule()
Definição miolo.class:1066
static _REQUEST( $vars, $from='ALL')
Definição miolo.class:1109
static getInstance()
Definição miolo.class:134
static transformToMValidators($valids)
setShowButtons($showButtons)
static getHideButtonsOnAdd($itemForce=null)
static getValidators($itemForce=null)
const EVENT_ADD
setFieldValidators($fields, $validators)
static getDataItem($arrayItem, $itemForce=NULL)
const EVENT_EDIT
setValidators($validators=null)
setOverFlowWidth($width, $type='auto')
processNamesArray($field)
static getUpdateButton($itemForce=null)
setHideButtonsOnAdd($hide)
const EVENT_REMOVE
static setValidatorsAjax($validators, $item)
setFields($fields)
__construct($item, $title, $columns=NULL, $fields=NULL, $opts=array('edit', 'remove'), $checkTransaction=NULL)
setDefaultValueFields($fields)
setColumns($columns)
addAction($phpFunction, $img, $imgModule)
setUpdateButton($show)
static defineData($arrayItem, $obj, $item=NULL)
const HIDDEN_FIELD_PREFIX_NAME
static getSessionValue($var, $itemForce=null)
const jsGetFieldValues
static setSessionValue($var, $value, $itemForce=null)
getFieldValidator($name, $validators)
static generateActionString($i, $itemForce=NULL)
autoEditAction($args, $values=NULL)
autoClearAction($args)
editFromTable($args)
autoUpAction($data)
upFromTable($args)
autoForceAddAction($args)
formatFieldToFloat($data, $table)
clearTableFields($args)
SdAjaxFields($args)
duplicateFromTable($args)
sumValue($table)
autoDuplicateAction($args)
removeFromTable($args)
downFromTable($args)
SdsetResponse($fields, $divResponse, $js=null)
addToTable($args)
autoDownAction($data)
autoAddAction($data, $object=NULL, $errors=NULL)
global $MIOLO
getMultipleSelectData($args)
autoRemoveAction($data)
$action
Definição base.php:4
$url
Definição base.php:2