MIOLO20
Carregando...
Procurando...
Nenhuma entrada encontrada
mcontrol.class
Ir para a documentação deste ficheiro.
1<?php
34abstract class MControl extends MComponent
35{
36
45
46 const CLASS_CAPTION = 'm-caption';
47 const CLASS_CAPTION_REQUIRED = 'm-caption-required';
48
49 const TOOLTIP_PLACEMENT_BOTTOM = 'bottom';
50 const TOOLTIP_PLACEMENT_TOP = 'top';
51 const TOOLTIP_PLACEMENT_LEFT = 'left';
52 const TOOLTIP_PLACEMENT_RIGHT = 'right';
53 const TOOLTIP_PLACEMENT_AUTO = 'auto';
54
58 static $_number = 0;
59
63 private $_numberId;
64
69 public $id;
70
75 public $uniqueId;
76
80 public $cssClass;
81
85 public $enabled;
86
90 public $style;
91
95 public $visible;
96
100 public $attrs; // array with HTML attributes
101
107
115 public $formMode;
116
121
125 public $readonly;
126
130 public $caption;
131
135 public $jsHint;
136
141 public $inner;
142
148 public $box;
149
153 public $cssp;
154
158 public $parent;
159
163 public $controls;
164
169 // var $controlsCount;
170
175
179 public $painter;
180
181
186 public function __construct( $name = NULL )
187 {
188 parent::__construct( $name );
189
190 $this->_numberId = MControl::$_number++;
191 $this->id = ( ( $this->name == NULL ) ? 'm'.$this->_numberId : $this->name );
192 $this->cssClass = '';
193 $this->enabled = true;
194 $this->attrs = new MStringList();
195 $this->style = new MStringList();
196 $this->visible = true;
197 $this->formMode = self::FORM_MODE_WHOLE_ROW;
198 $this->maintainState = false;
199 $this->readonly = false;
200 $this->cssp = false;
201 $this->uniqueId = $this->id;
202 $this->controls = new MObjectList();
203 $this->controlsId = new MObjectList();
204 $this->eventHandlers = array ( );
205 $this->inner = '';
206 $this->box = NULL;
207 $this->painter = $this->manager->GetPainter();
208 }
209
214 public function __clone()
215 {
216 $this->attrs = clone $this->attrs;
217 $this->style = clone $this->style;
218 $this->controls = clone $this->controls;
219 $this->controlsId = clone $this->controlsId;
220 }
221
226 public function __set( $name, $value )
227 {
228 switch ( $name )
229 {
230 case 'color':
231 case 'font':
232 case 'border':
233 $this->_addStyle( $name, $value );
234
235 break;
236
237 case 'fontSize':
238 $this->_addStyle( 'font-size', $value );
239
240 break;
241
242 case 'fontStyle':
243 $this->_addStyle( 'font-style', $value );
244
245 break;
246
247 case 'fontFamily':
248 $this->_addStyle( 'font-family', $value );
249
250 break;
251
252 case 'fontWeight':
253 $this->_addStyle( 'font-weight', $value );
254
255 break;
256
257 case 'cursor':
258 $this->_addStyle( 'cursor', $value );
259 $this->addBoxStyle('cursor', $value);
260
261 break;
262
263 case 'textAlign':
264 $this->_addStyle ( 'text-align', $value );
265 $this->addBoxStyle( 'text-align', $value );
266
267 break;
268
269 case 'textIndent':
270 $this->_addStyle ( 'text-indent', $value );
271 $this->addBoxStyle( 'text-indent', $value );
272
273 break;
274
275 case 'lineHeight':
276 $this->_addStyle( 'line-height', $value );
277
278 break;
279
280 case 'padding':
281 case 'width':
282 case 'height':
283 case 'float':
284 case 'clear':
285 case 'visibility':
286 $this->addBoxStyle( $name, $value );
287
288 break;
289
290 case 'top':
291 case 'left':
292 case 'position':
293 $this->addBoxStyle( $name, $value );
294
295 break;
296
297 case 'zIndex':
298 $this->addBoxStyle( 'z-index', $value );
299
300 $this->cssp = true;
301 break;
302
303 case 'backgroundColor':
304 $this->addBoxStyle( 'background-color', $value );
305
306 break;
307
308 case 'verticalAlign':
309 $this->addBoxStyle( 'vertical-align', $value );
310
311 break;
312 }
313 }
314
315
316 public function __get( $name )
317 {
318 switch ( $name )
319 {
320 case 'top':
321 case 'left':
322 case 'width':
323 case 'height':
324 case 'padding':
325 case 'float':
326 case 'position':
327 return $this->getBox()->style->get( $name );
328
329 break;
330 }
331 }
332
333
334 protected function _AddStyle($name, $value)
335 {
336 if ( $value != '' )
337 {
338 $this->style->addValue($name, $value);
339 }
340 }
341
342
343 public function setReadOnly($status)
344 {
345 $this->readonly = $status;
346 }
347
354 public function setEnabled($state)
355 {
356 $this->enabled = $state;
357 }
358
359 public function setName($name)
360 {
361 MUtil::setIfNull($this->id, $name);
362
363 parent::setName($name);
364 }
365
366
367 public function setId($id)
368 {
369 $this->id = $id;
370 MUtil::setIfNull( $this->name, $id );
371 }
372
373 public function setNameAndId($name)
374 {
375 $this->setName($name);
376 $this->setId($name);
377 }
378
379
380 public function getId()
381 {
382 return $this->id;
383 }
384
385
386 function getName()
387 {
388 return $this->name;
389 }
390
391
392 function getUniqueId()
393 {
394 return $this->uniqueId;
395 }
396
397
398 function setClass( $cssClass, $add = true )
399 {
400 if ( $add )
401 {
402 $this->cssClass .= MUtil::IfNull($this->cssClass, '', ' ') . $cssClass;
403 }
404 else
405 {
406 $this->cssClass = $cssClass;
407 }
408
409 }
410
411
412 public function addStyleFile( $styleFile )
413 {
414 if ( !array_key_exists("REQUISICAO_PELA_API", $GLOBALS) || !$GLOBALS['REQUISICAO_PELA_API'])
415 {
416 $this->page->AddStyle($styleFile);
417 }
418 }
419
420
421 public function getClass()
422 {
423 return $this->cssClass;
424 }
425
426
427 function addStyle($name, $value)
428 {
429 $this->$name = $value;
430 }
431
432 function setStyle($style)
433 {
434 $this->style->items = $style;
435 }
436
437 function getStyle()
438 {
439 return $this->style->hasItems() ? " style=\"" . $this->style->getText(':', ';') . "\"" : '';
440 }
441
442
443 function addAttribute( $name, $value = '' )
444 {
445 $this->attrs->addValue( $name, ( $value != '' ) ? "\"$value\"" : '' );
446 }
447
448 function setTooltip($title, $placement = self::TOOLTIP_PLACEMENT_AUTO)
449 {
450 $this->getBox()->addAttribute('title', $title);
451 $this->getBox()->addAttribute('rel', 'tooltip');
452 $this->getBox()->addAttribute('data-html', 'true');
453 $this->getBox()->addAttribute('data-placement', $placement);
454 }
455
456 function setAttribute( $name, $value )
457 {
458 $this->addAttribute( $name, $value );
459 }
460
461
462 function setAttributes($attr)
463 {
464 if ( $attr != NULL )
465 {
466 if ( is_array($attr) )
467 {
468 foreach( $attr as $ak => $av )
469 {
470 $this->setAttribute($ak, $av);
471 }
472 }
473 else if ( is_string($attr) )
474 {
475 $attr = str_replace( "\"", '', trim($attr) );
476
477 foreach ( explode(' ', $attr) as $a )
478 {
479 $a = explode('=', $a);
480 $this->setAttribute($a[0], $a[1]);
481 }
482 }
483 }
484 }
485
486 function attributes( $mergeDuplicates=false )
487 {
488 if ( $mergeDuplicates )
489 {
490 $items = $this->attrs->getItems();
491
492 $items_new = array( );
493 foreach( $items as $id=>$item )
494 {
495 if ( isset($items_new[ strtolower($id) ]) )
496 {
497 $items_new[ strtolower($id) ] = substr($items_new[ strtolower($id) ], 0, -1) .';' . substr($item, 1);
498 }
499 else
500 {
501 $items_new[ strtolower($id) ] = $item;
502 }
503 }
504 $this->attrs->setItems( $items_new );
505 }
506 return $this->attrs->hasItems() ? ' ' . $this->attrs->getText("=", " ") : '';
507 }
508
509 function getAttributes( $mergeDuplicates=false )
510 {
511 return $this->attributes( $mergeDuplicates ) . $this->getStyle();
512 }
513
514 function setFormMode( $mode )
515 {
516 $this->formMode = $mode;
517 }
518
519 function setJsHint( $hint )
520 {
521 if ( $hint != '' )
522 {
523 $this->jsHint = $hint;
524 $this->page->addScript('x/x_core.js');
525 $this->page->addScript('x/x_dom.js');
526 $this->page->addScript('x/x_event.js');
527 $this->page->addScript('x/x_tip.js');
528 $this->page->onLoad("new xTooltipGroup('tipHint', '', 'left', 3, 0);");
529 }
530 }
531
532 function setPosition($left, $top, $position = 'absolute')
533 {
534 $this->addBoxStyle('position', $position);
535 $this->addBoxStyle('left', "{$left}px");
536 $this->addBoxStyle('top', "{$top}px");
537 }
538
539 function setWidth($value)
540 {
541 if ( ! $value )
542 {
543 return;
544 }
545
546 if ( strpos($value, '%') === false && $value != 'auto')
547 {
548 $v = "{$value}px";
549 }
550 else
551 {
552 $v = $value;
553 }
554
555 $this->addBoxStyle('width', $v);
556 }
557
558 function setHeight($value)
559 {
560 if ( ! $value )
561 {
562 return;
563 }
564
565 if ( strpos($value, '%') === false )
566 {
567 $v = "{$value}px";
568 }
569 else
570 {
571 $v = $value;
572 }
573
574 $this->addBoxStyle('height', $v);
575 }
576
577 function setColor($value)
578 {
579 $this->AddStyle('color', $value);
580 }
581
582 function setVisibility($value)
583 {
584 $value = ($value ? 'visible' : 'hidden');
585 $this->visibility = $value;
586 }
587
588 function setFont($value)
589 {
590 $this->addStyle('font', $value);
591 }
592
594 {
595 $this->caption = $caption;
596 }
597
598 function setInner($inner)
599 {
600 $this->inner = $inner;
601 }
602
603 function getInner()
604 {
605 return $this->inner;
606 }
607
608 private function _AddControl($control, $pos = 0, $op = 'add')
609 {
610 if(is_array($control))
611 {
612 foreach($control as $c)
613 {
614 $this->_AddControl($c);
615 }
616 }
617 elseif ( $control instanceof MControl )
618 {
619 if ( $op == 'add' )
620 {
621 $this->controlsId->add($control, $control->GetId() );
622 $this->controls->add($control);
623 }
624 elseif ( $op == 'ins' )
625 {
626 $this->controlsId->add($control, $control->GetId() );
627 $this->controls->insert($control, $pos);
628 }
629 elseif ( $op == 'set' )
630 {
631 $this->controlsId->set( $control->GetId(), $control );
632 $this->controls->set($pos, $control);
633 }
634
635 $control->parent = $this;
636 }
637 elseif ( ! is_null($control) )
638 {
639 if ( ! is_object($control) )
640 {
641 throw new EControlException(
642 "Using non-object with _AddControl;<br/>type: " . gettype($control) . ';<br/>value: ' . $control
643 . ';<br/>Try using Label control instead');
644 }
645 else
646 {
647 throw new EControlException('Using non-control with _AddControl; class: ' . get_class($control).'; name: '.$control->name.'; id: '.$control->id);
648 }
649 }
650 }
651
652 function addControl($control)
653 {
654 $this->_AddControl($control);
655 }
656
657 function insertControl($control, $pos = 0, $width = null, $float = null)
658 {
659 $this->_AddControl($control, $pos, 'ins');
660 }
661
662 public function setControl($control, $pos = 0)
663 {
664 $this->_AddControl($control, $pos, 'set');
665 }
666
668 {
669 $this->clearControls();
670
671 foreach ( $controls as $c )
672 $this->addControl($c);
673 }
674
675 function getControls()
676 {
677 return $this->controls->items;
678 }
679
680 public function getControl($pos)
681 {
682 return $this->controls->get($pos);
683 }
684
686 {
687 return $this->controlsId->get($id);
688 }
689
691 {
692 $k = NULL;
693 $controls = $this->controlsId->items;
694
695 foreach ( $controls as $c )
696 {
697 if ( $c->id == $id )
698 {
699 return $c;
700 }
701
702 elseif ( ( $k = $c->FindControlById($id) ) != NULL )
703 break;
704 }
705
706 return $k;
707 }
708
709 function setControlById($control, $id)
710 {
711 $this->controlsId->set($id, $control);
712 }
713
714 function clearControls()
715 {
716 $this->controls->clear();
717 $this->controlsId->clear();
718 }
719
720 //
721 // EventHandler
722 //
723 function eventHandler()
724 {
725 $subject = $this;
726
727 $event = $_REQUEST['__EVENTTARGETVALUE'] ?? null;
728 $args = $_REQUEST['__EVENTARGUMENT'] ?? null;
729
730 if ($event != '')
731 {
732 $eventTokens = explode(':', $event);
733 $sender = $subject->FindControlById( $eventTokens[0] );
734 $func = str_replace(':', '_', $event);
735
736 if ( method_exists($subject, $func) )
737 {
738 $subject->$func($sender, $args);
739 }
740 elseif ( $sender instanceof MControl )
741 {
742 $eventType = $eventTokens[1];
743 $func = $sender->eventHandlers[$eventType]['handler'];
744
745 if ( ! is_null($func) )
746 {
747 if ( method_exists($subject, $func) )
748 {
749 $subject->$func($sender, $args);
750 }
751 elseif ( function_exists($func) )
752 {
753 $func($sender, $args);
754 }
755 }
756 }
757 }
758 if($eventTokens = explode(';', MIOLO::_REQUEST('event')))
759 {
760 $e = str_replace(':', '_', $eventTokens[0]);
761
762 if ( (strtolower($e) !== strtolower($func ?? '')) && (method_exists($subject, $e) ) )
763 {
764 $params = $eventTokens[1];
765 $subject->$e(NULL, $params);
766 }
767 }
768 }
769
770
771 function attachEventHandler( $name, $handler, $param = NULL )
772 {
773 $this->eventHandlers[$name]['handler'] = $handler;
774 $this->eventHandlers[$name]['param'] = $param;
775 }
776
777
778 function getBox()
779 {
780 $this->cssp = true;
781
782 if ( is_null($this->box) )
783 {
784 $this->box = new mSpan( 'm_' . MUtil::NVL( $this->name, $this->uniqueId ) );
785 }
786
787 return $this->box;
788 }
789
790
791 function setBoxId( $id )
792 {
793 $this->getBox()->setId( $id );
794 }
795
796
797 function setBoxClass( $cssClass, $add = true )
798 {
799 $this->getBox()->setClass( $cssClass, $add );
800 }
801
802
803 function getBoxClass()
804 {
805 return $this->getBox()->cssClass;
806 }
807
808
809 function setBoxAttributes($attr)
810 {
811 $this->getBox()->setAttributes($attr);
812 }
813
815 {
816 return $this->getBox()->getAttributes();
817 }
818
819 function addBoxStyle($name, $value = '')
820 {
821 $this->GetBox()->_AddStyle($name, $value);
822 }
823
824
825 public function generateBox( $content )
826 {
827 $box = $this->getBox();
828 $box->inner = $content;
829
830 return $box->getRender('div');
831 }
832
833 function getRender( $method )
834 {
835 return $this->painter->$method( $this );
836 }
837
838
839 public function getInnerToString()
840 {
841 return $this->painter->generateToString( $this->getInner() );
842 }
843
844
845 public function generateInner()
846 {
847 if ( $this->inner == '' )
848 {
849 if ( $this->controls->hasItems() )
850 {
851 $this->inner = $this->controls->items;
852 }
853 }
854 }
855
856
857 public function generate()
858 {
859 $this->generateInner();
860 $content = $this->getInner();
861
862 if ( $this->jsHint != '' )
863 {
864 $hint = new mSpan('', $content, 'tipHint');
865 $hint->addAttribute( "title", "{$this->jsHint}" );
866
867 $content = $hint->generate();
868 }
869
870 if ( MForm::$showHints && $this->hint != '' )
871 {
872 $hint = new MSpan(NULL, $this->hint, 'm-hint');
873
874 if ( is_string($content) )
875 {
876 $content .= $hint->generate();
877 }
878 else
879 {
880 $content = new MDiv(NULL, array($content, $hint));
881 }
882 }
883
885 if ( strpos(MIOLO::getInstance()->getTheme()->id, "sbootstrap") !== false )
886 {
887 if (property_exists($this, 'label') && !empty($this->label) && $this->label != '&nbsp;')
888 {
889 if ($this instanceof MTextField || $this instanceof MListControl || $this instanceof MChoiceControl)
890 {
891 $divContainer = new MDiv('', $content, 'm-new-container');
892 $content = $divContainer->generate();
893 }
894 }
895 }
896
897 return ( $this->cssp ? $this->generateBox($content) : $this->painter->generateToString($content) );
898 }
899}
900
901?>
const TOOLTIP_PLACEMENT_BOTTOM
Definição mcontrol.class:49
const FORM_MODE_SHOW_NBSP
Definição mcontrol.class:44
setStyle($style)
Definição mcontrol.class:432
setBoxAttributes($attr)
Definição mcontrol.class:809
eventHandler()
Definição mcontrol.class:723
setColor($value)
Definição mcontrol.class:577
setAttributes($attr)
Definição mcontrol.class:462
setBoxId( $id)
Definição mcontrol.class:791
setReadOnly($status)
Definição mcontrol.class:343
setVisibility($value)
Definição mcontrol.class:582
const TOOLTIP_PLACEMENT_TOP
Definição mcontrol.class:50
setWidth($value)
Definição mcontrol.class:539
const CLASS_CAPTION
Definição mcontrol.class:46
addControl($control)
Definição mcontrol.class:652
insertControl($control, $pos=0, $width=null, $float=null)
Definição mcontrol.class:657
setControl($control, $pos=0)
Definição mcontrol.class:662
attributes( $mergeDuplicates=false)
Definição mcontrol.class:486
const TOOLTIP_PLACEMENT_LEFT
Definição mcontrol.class:51
findControlById($id)
Definição mcontrol.class:690
setName($name)
Definição mcontrol.class:359
setCaption($caption)
Definição mcontrol.class:593
clearControls()
Definição mcontrol.class:714
static $_number
Definição mcontrol.class:58
generateInner()
Definição mcontrol.class:845
setEnabled($state)
Definição mcontrol.class:354
setNameAndId($name)
Definição mcontrol.class:373
setAttribute( $name, $value)
Definição mcontrol.class:456
const TOOLTIP_PLACEMENT_RIGHT
Definição mcontrol.class:52
generateBox( $content)
Definição mcontrol.class:825
setControls($controls)
Definição mcontrol.class:667
getBoxAttributes()
Definição mcontrol.class:814
_AddStyle($name, $value)
Definição mcontrol.class:334
setClass( $cssClass, $add=true)
Definição mcontrol.class:398
setInner($inner)
Definição mcontrol.class:598
setBoxClass( $cssClass, $add=true)
Definição mcontrol.class:797
getControlById($id)
Definição mcontrol.class:685
addStyle($name, $value)
Definição mcontrol.class:427
setTooltip($title, $placement=self::TOOLTIP_PLACEMENT_AUTO)
Definição mcontrol.class:448
setId($id)
Definição mcontrol.class:367
const FORM_MODE_WHOLE_ROW
Definição mcontrol.class:41
addStyleFile( $styleFile)
Definição mcontrol.class:412
const TOOLTIP_PLACEMENT_AUTO
Definição mcontrol.class:53
addBoxStyle($name, $value='')
Definição mcontrol.class:819
getAttributes( $mergeDuplicates=false)
Definição mcontrol.class:509
attachEventHandler( $name, $handler, $param=NULL)
Definição mcontrol.class:771
const CLASS_CAPTION_REQUIRED
Definição mcontrol.class:47
addAttribute( $name, $value='')
Definição mcontrol.class:443
__set( $name, $value)
Definição mcontrol.class:226
getRender( $method)
Definição mcontrol.class:833
getInnerToString()
Definição mcontrol.class:839
setHeight($value)
Definição mcontrol.class:558
__get( $name)
Definição mcontrol.class:316
setFormMode( $mode)
Definição mcontrol.class:514
getControl($pos)
Definição mcontrol.class:680
setJsHint( $hint)
Definição mcontrol.class:519
__construct( $name=NULL)
Definição mcontrol.class:186
const FORM_MODE_SHOW_SIDE
Definição mcontrol.class:42
setFont($value)
Definição mcontrol.class:588
setPosition($left, $top, $position='absolute')
Definição mcontrol.class:532
setControlById($control, $id)
Definição mcontrol.class:709
const FORM_MODE_SHOW_ABOVE
Definição mcontrol.class:43
static $showHints
Definição mform.class:68
static _REQUEST( $vars, $from='ALL')
Definição miolo.class:1109
static getInstance()
Definição miolo.class:134
static NVL($value1, $value2)
Definição mutil.class:38
static IfNull($value1, $value2, $value3)
Definição mutil.class:54
$title
Definição base.php:3