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

Go to the documentation of this file.
00001 <?
00034 abstract class MControl extends MComponent
00035 {
00036 
00041     const FORM_MODE_WHOLE_ROW  = 0;
00042     const FORM_MODE_SHOW_SIDE  = 1;
00043     const FORM_MODE_SHOW_ABOVE = 2;
00044     const FORM_MODE_SHOW_NBSP  = 3;
00045 
00049     static  $_number = 0;
00050 
00054     private $_numberId;
00055 
00060     public $id;
00061 
00066     public $uniqueId;
00067 
00071     public $cssClass;
00072 
00076     public $enabled;
00077 
00081     public $style;
00082 
00086     public $visible;
00087 
00091     public $attrs; // array with HTML attributes
00092 
00097     public $attributes;
00098 
00106     public $formMode;
00107 
00111     public $maintainState;
00112 
00116     public $readonly;
00117 
00121     public $caption;
00122 
00126     public $jsHint;
00127 
00132     public $inner;
00133 
00138     public $controlBox;
00139     public $box;
00140 
00144     public $cssp;
00145 
00149     public $parent;
00150 
00154     public $controls;
00155 
00159     public $controlsId;
00160     //    var $controlsCount;
00161 
00165     public $eventHandlers;
00166 
00170     public $painter;
00171 
00172 
00177     public function __construct( $name = NULL )
00178     {
00179         parent::__construct( $name );
00180 
00181         $this->_numberId = MControl::$_number++;
00182         $this->id        = ( ( $this->name == NULL ) ? 'm'.$this->_numberId : $this->name );
00183         $this->cssClass  = '';
00184         $this->enabled   = true;
00185         $this->attrs     = new MStringList();
00186         $this->style     = new MStringList();
00187         $this->visible   = true;
00188         $this->formMode  = FORM_MODE_WHOLE_ROW;
00189         $this->maintainState = false;
00190         $this->readonly   = false;
00191         $this->cssp       = false;
00192         $this->uniqueId   = $this->id;
00193         $this->controls   = new MObjectList();
00194         $this->controlsId = new MObjectList();
00195         $this->eventHandlers = array ( );
00196         $this->inner   = '';
00197         $this->box     = NULL;
00198         $this->painter = $this->manager->GetPainter();
00199     }
00200 
00205     public function __clone()
00206     {
00207         $this->attrs      = clone $this->attrs; 
00208         $this->style      = clone $this->style; 
00209         $this->controls   = clone $this->controls; 
00210         $this->controlsId = clone $this->controlsId; 
00211     }
00212  
00217     public function __set( $name, $value )
00218     {
00219         switch ( $name )
00220             {
00221             case 'color':
00222             case 'font':
00223             case 'border':
00224                 $this->_addStyle( $name, $value );
00225 
00226                 break;
00227 
00228             case 'fontSize':
00229                 $this->_addStyle( 'font-size', $value );
00230 
00231                 break;
00232 
00233             case 'fontStyle':
00234                 $this->_addStyle( 'font-style', $value );
00235 
00236                 break;
00237 
00238             case 'fontFamily':
00239                 $this->_addStyle( 'font-family', $value );
00240 
00241                 break;
00242 
00243             case 'fontWeight':
00244                 $this->_addStyle( 'font-weight', $value );
00245 
00246                 break;
00247 
00248             case 'cursor':
00249                 $this->_addStyle( 'cursor', $value );
00250                 $this->addBoxStyle('cursor', $value);
00251 
00252                 break;
00253 
00254             case 'textAlign':
00255                 $this->_addStyle  ( 'text-align', $value );
00256                 $this->addBoxStyle( 'text-align', $value );
00257 
00258                 break;
00259 
00260             case 'textIndent':
00261                 $this->_addStyle  ( 'text-indent', $value );
00262                 $this->addBoxStyle( 'text-indent', $value );
00263 
00264                 break;
00265 
00266             case 'lineHeight':
00267                 $this->_addStyle( 'line-height', $value );
00268 
00269                 break;
00270 
00271             case 'padding':
00272             case 'width':
00273             case 'height':
00274             case 'float':
00275             case 'clear':
00276             case 'visibility':
00277                 $this->addBoxStyle( $name, $value );
00278 
00279                 break;
00280 
00281             case 'top':
00282             case 'left':
00283             case 'position':
00284                 $this->addBoxStyle( $name, $value );
00285 
00286                 break;
00287 
00288             case 'zIndex':
00289                 $this->addBoxStyle( 'z-index', $value );
00290 
00291                 $this->cssp = true;
00292                 break;
00293 
00294             case 'backgroundColor':
00295                 $this->addBoxStyle( 'background-color', $value );
00296 
00297                 break;
00298 
00299             case 'verticalAlign':
00300                 $this->addBoxStyle( 'vertical-align', $value );
00301 
00302                 break;
00303             }
00304     }
00305 
00306 
00307     public function __get( $name )
00308     {
00309         switch ( $name )
00310             {
00311             case 'top':
00312             case 'left':
00313             case 'width':
00314             case 'height':
00315             case 'padding':
00316             case 'float':
00317             case 'position':
00318                 return $this->getBox()->style->get( $name );
00319 
00320                 break;
00321             }
00322     }
00323 
00324 
00325     protected function _AddStyle($name, $value)
00326     {
00327         if ( $value != '' )
00328         {
00329             $this->style->addValue($name, $value);
00330         }
00331     }
00332 
00333 
00334     public function setReadOnly($status)
00335     {
00336         $this->readonly = $status;
00337     }
00338 
00345     public function setEnabled($state)
00346     {
00347         $this->enabled = $state;
00348     }
00349 
00350     public function setName($name)
00351     {
00352         MUtil::setIfNull($this->id, $name);
00353 
00354         parent::setName($name);
00355     }
00356 
00357 
00358     public function setId($id)
00359     {
00360         $this->id = $id;
00361         MUtil::setIfNull( $this->name, $id );
00362     }
00363 
00364 
00365     public function getId()
00366     {
00367         return $this->id;
00368     }
00369 
00370 
00371     function getName()
00372     {
00373         return $this->name;
00374     }
00375 
00376 
00377     function getUniqueId()
00378     {
00379         return $this->uniqueId;
00380     }
00381 
00382 
00383     function setClass( $cssClass, $add = true )
00384     {
00385         if ( $add )
00386         {
00387             $this->cssClass .= MUtil::IfNull($this->cssClass, '', ' ') . $cssClass;
00388         }
00389         else
00390         {
00391             $this->cssClass = $cssClass;
00392         }
00393 
00394     }
00395 
00396 
00397     public function addStyleFile( $styleFile )
00398     {
00399         $this->page->AddStyle($styleFile);
00400     }
00401 
00402 
00403     public function getClass()
00404     {
00405         return $this->cssClass;
00406     }
00407 
00408 
00409     function addStyle($name, $value)
00410     {
00411         $this->$name = $value;
00412     }
00413 
00414     function setStyle($style)
00415     {
00416         $this->style->items = $style;
00417     }
00418 
00419     function getStyle()
00420     {
00421         return $this->style->hasItems() ? " style=\"" . $this->style->getText(':', ';') . "\"" : '';
00422     }
00423 
00424 
00425     function addAttribute( $name, $value = '' )
00426     {
00427         $this->attrs->addValue( $name, ( $value != '' ) ? "\"$value\"" : '' );
00428     }
00429 
00430 
00431     function setAttribute( $name, $value )
00432     {
00433         $this->addAttribute( $name, $value );
00434     }
00435 
00436 
00437     function setAttributes($attr)
00438     {
00439         if ( $attr != NULL )
00440         {
00441             if ( is_array($attr) )
00442             {
00443                 foreach( $attr as $ak => $av )
00444                 {
00445                     $this->setAttribute($ak, $av);
00446                 }
00447             }
00448             else if ( is_string($attr) )
00449             {
00450                 $attr = str_replace( "\"", '', trim($attr) );
00451 
00452                 foreach ( explode(' ', $attr) as $a )
00453                 {
00454                     $a = explode('=', $a);
00455                     $this->setAttribute($a[0], $a[1]);
00456                 }
00457             }
00458         }
00459     }
00460 
00461     function attributes( $mergeDuplicates=false )
00462     {
00463         if ( $mergeDuplicates )
00464         {
00465             $items = $this->attrs->getItems();
00466 
00467             $items_new = array( );
00468             foreach( $items as $id=>$item )
00469             {
00470                 if ( $items_new[ strtolower($id) ] )
00471                 {
00472                     $items_new[ strtolower($id) ] = substr($items_new[ strtolower($id) ], 0, -1) .';' . substr($item, 1);
00473                 }
00474                 else
00475                 {
00476                     $items_new[ strtolower($id) ] = $item;
00477                 }
00478             }
00479             $this->attrs->setItems( $items_new );
00480         }
00481         return $this->attrs->hasItems() ? ' ' . $this->attrs->getText("=", " ") : '';
00482     }
00483 
00484     function getAttributes( $mergeDuplicates=false )
00485     {
00486         return $this->attributes( $mergeDuplicates ) . $this->getStyle();
00487     }
00488 
00489     function setFormMode( $mode )
00490     {
00491         $this->formMode = $mode;
00492     }
00493 
00494     function setJsHint( $hint )
00495     {
00496         if ( $hint != '' )
00497         {
00498             $this->jsHint = $hint;
00499             $this->page->addScript('x/x_core.js');
00500             $this->page->addScript('x/x_dom.js');
00501             $this->page->addScript('x/x_event.js');
00502             $this->page->addScript('x/x_tip.js');
00503             $this->page->onLoad("new xTooltipGroup('tipHint', '', 'right', 3, 0);");
00504         }
00505     }
00506 
00507     function setPosition($left, $top, $position = 'absolute')
00508     {
00509         $this->addBoxStyle('position', $position);
00510         $this->addBoxStyle('left', "{$left}px");
00511         $this->addBoxStyle('top', "{$top}px");
00512     }
00513 
00514     function setWidth($value)
00515     {
00516         if ( ! $value )
00517         {
00518             return;
00519         }
00520 
00521         if ( strpos($value, '%') === false )
00522         {
00523             $v = "{$value}px";
00524         }
00525         else
00526         {
00527             $v = $value;
00528         }
00529 
00530         $this->addBoxStyle('width', $v);
00531     }
00532 
00533     function setHeight($value)
00534     {
00535         if ( ! $value )
00536         {
00537             return;
00538         }
00539 
00540         if ( strpos($value, '%') === false )
00541         {
00542             $v = "{$value}px";
00543         }
00544         else
00545         {
00546             $v = $value;
00547         }
00548 
00549         $this->addBoxStyle('height', $v);
00550     }
00551 
00552     function setColor($value)
00553     {
00554         $this->AddStyle('color', $value);
00555     }
00556 
00557     function setVisibility($value)
00558     {
00559         $value = ($value ? 'visible' : 'hidden');
00560         $this->visibility = $value;
00561     }
00562 
00563     function setFont($value)
00564     {
00565         $this->addStyle('font', $value);
00566     }
00567 
00568     function setCaption($caption)
00569     {
00570         $this->caption = $caption;
00571     }
00572 
00573     function setInner($inner)
00574     {
00575         $this->inner = $inner;
00576     }
00577 
00578     function getInner()
00579     {
00580         return $this->inner;
00581     }
00582 
00583     private function _AddControl($control, $pos = 0, $op = 'add')
00584     {
00585         if(is_array($control))
00586         {
00587             foreach($control as $c)
00588             {
00589                 $this->_AddControl($c);
00590             }
00591         }
00592         elseif ( $control instanceof MControl )
00593         {
00594             if ( $op == 'add' )
00595             {
00596                 $this->controlsId->add($control, $control->GetId() );
00597                 $this->controls->add($control);
00598             }
00599             elseif ( $op == 'ins' )
00600             {
00601                 $this->controlsId->add($control, $control->GetId() );
00602                 $this->controls->insert($control, $pos);
00603             }
00604             elseif ( $op == 'set' )
00605             {
00606                 $this->controlsId->set( $control->GetId(), $control );
00607                 $this->controls->set($pos, $control);
00608             }
00609 
00610             $control->parent = $this;
00611         }
00612         elseif ( ! is_null($control) )
00613         {
00614             if ( ! is_object($control) )
00615             {
00616                 throw new EControlException(
00617                           "Using non-object with _AddControl;<br>type: " . gettype($control) . ';<br>value: ' . $control
00618                               . ';<br>Try use Label control instead');
00619             }
00620             else
00621             {
00622                 throw new EControlException('Using non-control with _AddControl; class: ' . get_class($control).'; name: '.$control->name.'; id: '.$control->id);
00623             }
00624         }
00625     }
00626 
00627     function addControl($control)
00628     {
00629         $this->_AddControl($control);
00630     }
00631 
00632     function insertControl($control, $pos = 0)
00633     {
00634         $this->_AddControl($control, $pos, 'ins');
00635     }
00636 
00637     function setControl($control, $pos = 0)
00638     {
00639         $this->_AddControl($control, $pos, 'set');
00640     }
00641 
00642     function setControls($controls)
00643     {
00644         $this->clearControls();
00645 
00646         foreach ( $controls as $c )
00647             $this->addControl($c);
00648     }
00649 
00650     function getControls()
00651     {
00652         return $this->controls->items;
00653     }
00654 
00655     function getControl($pos)
00656     {
00657         return $this->controls->get($pos);
00658     }
00659 
00660     function getControlById($id)
00661     {
00662         return $this->controlsId->get($id);
00663     }
00664 
00665     function findControlById($id)
00666     {
00667         $k = NULL;
00668         $controls = $this->controlsId->items;
00669 
00670         foreach ( $controls as $c )
00671         {
00672             if ( $c->id == $id )
00673             {
00674                 return $c;
00675             }
00676 
00677             elseif ( ( $k = $c->FindControlById($id) ) != NULL )
00678                 break;
00679         }
00680 
00681         return $k;
00682     }
00683 
00684     function setControlById($control, $id)
00685     {
00686         $this->controlsId->set($id, $control);
00687     }
00688 
00689     function clearControls()
00690     {
00691         $this->controls->clear();
00692         $this->controlsId->clear();
00693     }
00694 
00695     //
00696     //  EventHandler
00697     // 
00698     function eventHandler()
00699     {
00700         $subject = $this;
00701 
00702         $event = $_REQUEST['__EVENTTARGETVALUE'];
00703         $args  = $_REQUEST['__EVENTARGUMENT'];
00704 
00705         if ($event != '')
00706         {
00707             $eventTokens = explode(':', $event);
00708             $sender = $subject->FindControlById( $eventTokens[0] );
00709             $func   = str_replace(':', '_', $event);
00710 
00711             if ( method_exists($subject, $func) )
00712             {
00713                 $subject->$func($sender, $args);
00714             }
00715             elseif ( $sender instanceof MControl )
00716             {
00717                 $eventType = $eventTokens[1];
00718                 $func      = $sender->eventHandlers[$eventType]['handler'];
00719 
00720                 if ( ! is_null($func) )
00721                 {
00722                     if ( method_exists($subject, $func) )
00723                     {
00724                         $subject->$func($sender, $args);
00725                     }
00726                     elseif ( function_exists($func) )
00727                     {
00728                         $func($sender, $args);
00729                     }
00730                 }
00731             }
00732         }
00733         if($eventTokens = explode(';', MIOLO::_REQUEST('event')))
00734         {
00735             $e = str_replace(':', '_', $eventTokens[0]);
00736 
00737             if ( (strtolower($e) !== strtolower($func)) && (method_exists($subject, $e) ) )
00738             {
00739                 $params = $eventTokens[1];
00740                 $subject->$e(NULL, $params);
00741             }
00742         }
00743     }
00744 
00745 
00746     function attachEventHandler( $name, $handler, $param = NULL )
00747     {
00748         $this->eventHandlers[$name]['handler'] = $handler;
00749         $this->eventHandlers[$name]['param']   = $param;
00750     }
00751 
00752 
00753     function getBox()
00754     {
00755         $this->cssp = true;
00756 
00757         if ( is_null($this->box) )
00758         {
00759             $this->box = new mSpan( 'm_' . MUtil::NVL( $this->name, $this->uniqueId ) );
00760         }
00761 
00762         return $this->box;
00763     }
00764 
00765 
00766     function setBoxId( $id )
00767     {
00768         $this->getBox()->setId( $id );
00769     }
00770 
00771 
00772     function setBoxClass( $cssClass, $add = true )
00773     {
00774         $this->getBox()->setClass( $cssClass, $add );
00775     }
00776 
00777 
00778     function getBoxClass()
00779     {
00780         return $this->getBox()->cssClass;
00781     }
00782 
00783 
00784     function setBoxAttributes($attr)
00785     {
00786         $this->getBox()->setAttributes($attr);
00787     }
00788 
00789     function getBoxAttributes()
00790     {
00791         return $this->getBox()->getAttributes();
00792     }
00793 
00794     function addBoxStyle($name, $value)
00795     {
00796         $this->GetBox()->_AddStyle($name, $value);
00797     }
00798 
00799 
00800     public function generateBox( $content )
00801     {
00802         $box = $this->getBox();
00803         $box->inner = $content;
00804 
00805         return $box->getRender('div');
00806     }
00807 
00808     function getRender( $method )
00809     {
00810         return $this->painter->$method( $this );
00811     }
00812 
00813 
00814     public function getInnerToString()
00815     {
00816         return $this->painter->generateToString( $this->getInner() );
00817     }
00818 
00819 
00820     public function generateInner()
00821     {
00822         if ( $this->inner == '' )
00823         {
00824             if ( $this->controls->hasItems() )
00825             {
00826                 $this->inner = $this->controls->items;
00827             }
00828         }
00829     }
00830 
00831 
00832     public function generate()
00833     {
00834         $this->generateInner();
00835         $content = $this->getInner();
00836 
00837         if ( $this->jsHint != '' )
00838         {
00839             $hint = new mSpan('', $content, 'tipHint');
00840             $hint->addAttribute( "title", "{$this->jsHint}" );
00841 
00842             $content = $hint->generate();
00843         }
00844 
00845         return ( $this->cssp ? $this->generateBox($content) : $this->painter->generateToString($content) );
00846     }
00847 }
00848 
00849 ?>
CopyLeft (L) 2001-2006 - [MIOLO Development Team] SOLIS - Cooperativa de Soluções Livres - Lajeado/RS - Brasil