WebControls.php

浏览该文件的文档。
00001 <?php
00003 // FleaPHP Framework
00004 //
00005 // Copyright (c) 2005 - 2008 QeeYuan China Inc. (http://www.qeeyuan.com)
00006 //
00007 // 许可协议,请查看源代码中附带的 LICENSE.txt 文件,
00008 // 或者访问 http://www.fleaphp.org/ 获得详细信息。
00010 
00029 class FLEA_WebControls
00030 {
00036     var $_extends = array();
00037 
00043     var $_extendsDir = array();
00044 
00052     function FLEA_WebControls($extendsDir = null)
00053     {
00054         if (is_array($extendsDir)) {
00055             $this->_extendsDir = array_merge($this->_extendsDir, $extendsDir);
00056         } elseif ($extendsDir != '') {
00057             $this->_extendsDir[] = $extendsDir;
00058         }
00059         $extendsDir = FLEA::getAppInf('webControlsExtendsDir');
00060         if (is_array($extendsDir)) {
00061             $this->_extendsDir = array_merge($this->_extendsDir, $extendsDir);
00062         } elseif ($extendsDir != '') {
00063             $this->_extendsDir[] = $extendsDir;
00064         }
00065     }
00066 
00077     function control($type, $name, $attribs = null, $return = false)
00078     {
00079         $type = strtolower($type);
00080         $render = '_ctl' . ucfirst($type);
00081         $attribs = (array)$attribs;
00082 
00083         $__ctl_out = false;
00084         if (method_exists($this, $render)) {
00085             $__ctl_out = $this->{$render}($name, $attribs);
00086         } else {
00087             $extfilename = ucfirst($type) . '.php';
00088             if (!isset($this->_extends[$type])) {
00089                 foreach ($this->_extendsDir as $dir) {
00090                     if (file_exists($dir . DS . $extfilename)) {
00091                         require($dir . DS . $extfilename);
00092                         $this->_extends[$type] = true;
00093                         break;
00094                     }
00095                 }
00096             }
00097 
00098             if (isset($this->_extends[$type])) {
00099                 $__ctl_out = call_user_func_array($render,
00100                         array('name' => $name, 'attribs' => $attribs));
00101             }
00102         }
00103 
00104         if ($__ctl_out === false) {
00105             $__ctl_out = "INVALID CONTROL TYPE \"{$type}\"";
00106         }
00107 
00108         if ($return) { return $__ctl_out; }
00109         echo $__ctl_out;
00110         return '';
00111     }
00112 
00120     function attribsToString($attribs)
00121     {
00122         $__ctl_out = '';
00123         foreach ($attribs as $attrib => $value) {
00124             $__ctl_out .= $attrib . '="' . str_replace('"', '\'', $value) . '" ';
00125         }
00126         return $__ctl_out;
00127     }
00128 
00137     function extractAttribs(& $attribs, $req)
00138     {
00139         $extract = array();
00140         foreach ($req as $attrib) {
00141             if (array_key_exists($attrib, $attribs)) {
00142                 $extract[$attrib] = $attribs[$attrib];
00143                 unset($attribs[$attrib]);
00144             } else {
00145                 $extract[$attrib] = null;
00146             }
00147         }
00148         return $extract;
00149     }
00150 
00156     function mergeAttribs(& $attribs)
00157     {
00158         $args = array();
00159         foreach ($attribs as $key => $arg) {
00160             if (is_array($arg)) {
00161                 $args = array_merge($args, $arg);
00162             } else if (!is_null($arg)) {
00163                 $args[$key] = $arg;
00164             }
00165         }
00166         $attribs = $args;
00167     }
00168 
00174     function & _getView()
00175     {
00176         $viewClass = FLEA::getAppInf('view');
00177         if ($viewClass != 'PHP') {
00178             return FLEA::getSingleton($viewClass);
00179         } else {
00180             $view = false;
00181             return $view;
00182         }
00183     }
00184 
00193     function _ctlTextbox($name, $attribs)
00194     {
00195         return $this->__baseCtlInput($name, $attribs, 'text');
00196     }
00197 
00206     function _ctlPassword($name, $attribs)
00207     {
00208         return $this->__baseCtlInput($name, $attribs, 'password');
00209     }
00210 
00219     function _ctlMemo($name, $attribs)
00220     {
00221         extract($this->extractAttribs($attribs, array('id', 'value', 'disabled')));
00222         if (empty($id)) { $id = $name; }
00223 
00224         $__ctl_out = '<textarea ';
00225         if ($name) {
00226             $__ctl_out .= 'name="' . h($name) . '" ';
00227             $__ctl_out .= 'id="' . h($id) . '" ';
00228         }
00229         $__ctl_out .= $this->attribsToString($attribs);
00230         if ($disabled) {
00231             $__ctl_out .= 'disabled="disabled" ';
00232         }
00233         $__ctl_out .= '>';
00234         $__ctl_out .= h($value);
00235         $__ctl_out .= '</textarea>';
00236         return $__ctl_out;
00237     }
00238 
00247     function _ctlCheckbox($name, $attribs)
00248     {
00249         return $this->__baseCtlCheckboxOrRadio($name, $attribs, 'checkbox');
00250     }
00251 
00260     function _ctlCheckBoxGroup($name, $attribs)
00261     {
00262         return $this->__baseCtlCheckboxOrRadioGroup($name, $attribs, 'checkbox', '[]');
00263     }
00264 
00273     function _ctlRadio($name, $attribs)
00274     {
00275         return $this->__baseCtlCheckboxOrRadio($name, $attribs, 'radio');
00276     }
00277 
00286     function _ctlRadioGroup($name, $attribs)
00287     {
00288         return $this->__baseCtlCheckboxOrRadioGroup($name, $attribs, 'radio', '');
00289     }
00290 
00299     function _ctlListBox($name, $attribs)
00300     {
00301         extract($this->extractAttribs($attribs,
00302                 array('id', 'size', 'items', 'selected', 'multiple', 'disabled', 'key', 'caption')));
00303         if (empty($id)) { $id = $name; }
00304 
00305         if (!is_array($selected) && substr($selected, 0, 1) == ':') {
00306             $selected = intval(substr($selected, 1));
00307             $selectedByIndex = true;
00308         } else {
00309             $selectedByIndex = false;
00310         }
00311         $__ctl_out = '<select ';
00312         if ($name) {
00313             $__ctl_out .= 'name="' . h($name) . '" ';
00314             $__ctl_out .= 'id="' . h($id) . '" ';
00315         }
00316         if ($size <= 0) {
00317             $size = 4;
00318         }
00319         $__ctl_out .= 'size="' . $size . '" ';
00320         if ($multiple) {
00321             $__ctl_out .= 'multiple="multiple" ';
00322         }
00323         if ($disabled) {
00324             $__ctl_out .= 'disabled="disabled" ';
00325         }
00326         $__ctl_out .= $this->attribsToString($attribs);
00327         $__ctl_out .= ">\n";
00328 
00329         $items = (array)$items;
00330 
00331         if ($key) {
00332             if (!$this->__processMultiDimArray($items, $key, $caption)) {
00333                 return 'INVALID ITEMS';
00334             }
00335         }
00336 
00337         $ix = 0;
00338         foreach ($items as $caption => $value) {
00339             $__ctl_out .= '<option value="' . h($value) . '" ';
00340             $checked = false;
00341             if ($selectedByIndex) {
00342                 if (is_array($selected)) {
00343                     if (in_array($ix, $selected)) {
00344                         $checked = true;
00345                     }
00346                 } else if ($ix == $selected) {
00347                     $checked = true;
00348                 }
00349             } else {
00350                 if (is_array($selected)) {
00351                     if (in_array($value, $selected)) {
00352                         $checked = true;
00353                     }
00354                 } else if ($value == $selected) {
00355                     $checked = true;
00356                 }
00357             }
00358             if ($checked) {
00359                 $__ctl_out .= 'selected="selected" ';
00360             }
00361             $__ctl_out .= '>';
00362             $__ctl_out .= h($caption);
00363             $__ctl_out .= "</option>\n";
00364             $ix++;
00365         }
00366         $__ctl_out .= "</select>\n";
00367         return $__ctl_out;
00368     }
00369 
00378     function _ctlDropdownList($name, $attribs)
00379     {
00380         extract($this->extractAttribs($attribs,
00381                 array('id', 'items', 'selected', 'disabled', 'key', 'caption')));
00382         if (empty($id)) { $id = $name; }
00383 
00384         if (substr($selected, 0, 1) == ':') {
00385             $selected = intval(substr($selected, 1));
00386             $selectedByIndex = true;
00387         } else {
00388             $selectedByIndex = false;
00389         }
00390         $__ctl_out = '<select ';
00391         if ($name) {
00392             $__ctl_out .= 'name="' . h($name) . '" ';
00393             $__ctl_out .= 'id="' . h($id) . '" ';
00394         }
00395         if ($disabled) {
00396             $__ctl_out .= 'disabled="disabled" ';
00397         }
00398         $__ctl_out .= $this->attribsToString($attribs);
00399         $__ctl_out .= ">\n";
00400 
00401         $items = (array)$items;
00402 
00403         if ($key) {
00404             if (!$this->__processMultiDimArray($items, $key, $caption)) {
00405                 return 'INVALID ITEMS';
00406             }
00407         }
00408 
00409         $ix = 0;
00410         foreach ($items as $caption => $value) {
00411             $__ctl_out .= '<option value="' . h($value) . '" ';
00412             if ($selectedByIndex) {
00413                 if ($ix == $selected) {
00414                     $__ctl_out .= 'selected="selected" ';
00415                 }
00416             } else {
00417                 if ($value == $selected) {
00418                     $__ctl_out .= 'selected="selected" ';
00419                 }
00420             }
00421             $__ctl_out .= '>';
00422             $__ctl_out .= h($caption);
00423             $__ctl_out .= "</option>\n";
00424             $ix++;
00425         }
00426         $__ctl_out .= "</select>\n";
00427         return $__ctl_out;
00428     }
00429 
00438     function _ctlFileUpload($name, $attribs)
00439     {
00440         return $this->__baseCtlInput($name, $attribs, 'file');
00441     }
00442 
00452     function _ctlButton($name, $attribs, $buttonType = 'button')
00453     {
00454         extract($this->extractAttribs($attribs, array('caption')));
00455         if ($caption != '') { $attribs['value'] = $caption; }
00456         return $this->__baseCtlInput($name, $attribs, $buttonType);
00457     }
00458 
00467     function _ctlSubmit($name, $attribs)
00468     {
00469         return $this->_ctlButton($name, $attribs, 'submit');
00470     }
00471 
00480     function _ctlReset($name, $attribs)
00481     {
00482         return $this->_ctlButton($name, $attribs, 'reset');
00483     }
00484 
00493     function _ctlLabel($name, $attribs)
00494     {
00495         extract($this->extractAttribs($attribs, array('id', 'caption')));
00496         if (empty($id)) { $id = $name; }
00497 
00498         $__ctl_out = '<label ';
00499         if ($name) {
00500             $__ctl_out .= 'name="' . h($name) . '" ';
00501             $__ctl_out .= 'id="' . h($id) . '" ';
00502         }
00503         $__ctl_out .= $this->attribsToString($attribs);
00504         $__ctl_out .= '>';
00505         $__ctl_out .= h($caption);
00506         $__ctl_out .= '</label>';
00507         return $__ctl_out;
00508     }
00509 
00510 
00519     function _ctlStatic($name, $attribs)
00520     {
00521         extract($this->extractAttribs($attribs, array('id', 'value')));
00522         if (empty($id)) { $id = $name; }
00523 
00524         $__ctl_out = '<div ';
00525         if ($name) {
00526             $__ctl_out .= 'name="' . h($name) . '" ';
00527             $__ctl_out .= 'id="' . h($id) . '" ';
00528         }
00529         $__ctl_out .= $this->attribsToString($attribs);
00530         $__ctl_out .= '>';
00531         $__ctl_out .= h($value);
00532         $__ctl_out .= '</div>';
00533         return $__ctl_out;
00534     }
00535 
00544     function _ctlHidden($name, $attribs)
00545     {
00546         return $this->__baseCtlInput($name, $attribs, 'hidden');
00547     }
00548 
00558     function __baseCtlInput($name, $attribs, $type)
00559     {
00560         extract($this->extractAttribs($attribs, array('id', 'value', 'disabled')));
00561         if (empty($id)) { $id = $name; }
00562 
00563         $__ctl_out = "<input type=\"{$type}\" ";
00564         if ($name) {
00565             $__ctl_out .= 'name="' . h($name) . '" ';
00566             $__ctl_out .= 'id="' . h($id) . '" ';
00567         }
00568         $__ctl_out .= 'value="' . h($value) . '" ';
00569         $__ctl_out .= $this->attribsToString($attribs);
00570         if ($disabled) {
00571             $__ctl_out .= 'disabled="disabled" ';
00572         }
00573         $__ctl_out .= '/>';
00574         return $__ctl_out;
00575     }
00576 
00586     function __baseCtlCheckboxOrRadio($name, $attribs, $type)
00587     {
00588         extract($this->extractAttribs($attribs,
00589                 array('id', 'value', 'checked', 'disabled', 'caption')));
00590         if (empty($id)) { $id = $name; }
00591 
00592         $__ctl_out = "<input type=\"{$type}\" ";
00593         if ($name) {
00594             $__ctl_out .= 'name="' . h($name) . '" ';
00595             $__ctl_out .= 'id="' . h($id) . '" ';
00596         }
00597         if (strlen($value) == 0) { $value = 1; }
00598         $__ctl_out .= 'value="' . h($value) . '" ';
00599         $__ctl_out .= $this->attribsToString($attribs);
00600         if ($checked) {
00601             $__ctl_out .= 'checked="checked" ';
00602         }
00603         if ($disabled) {
00604             $__ctl_out .= 'disabled="disabled" ';
00605         }
00606         $__ctl_out .= '/>';
00607         if (strlen($caption)) {
00608             $__ctl_out .= $this->_ctlLabel(null, array('for' => $id, 'caption' => $caption));
00609         }
00610         return $__ctl_out;
00611     }
00612 
00623     function __baseCtlCheckboxOrRadioGroup($name, $attribs, $type, $suffix)
00624     {
00625         static $idSuffix = 1;
00626 
00627         extract($this->extractAttribs($attribs, array('items', 'selected', 'disabled',
00628                 'multirow', 'cols', 'key', 'caption', 'table', 'border', 'cellspacing',
00629                 'cellpadding', 'key2caption')));
00630 
00631         if (!is_array($selected) && substr($selected, 0, 1) == ':') {
00632             $selected = intval(substr($selected, 1));
00633             $selectedByIndex = true;
00634         } else {
00635             $selectedByIndex = false;
00636         }
00637 
00638         $__ctl_out = '';
00639         $items = (array)$items;
00640         $max = count($items);
00641         if ($max <= 0) { return ''; }
00642 
00643         if ($key) {
00644             if (!$this->__processMultiDimArray($items, $key, $caption, $key2caption)) {
00645                 return 'INVALID ITEMS';
00646             }
00647         } else if ($key2caption) {
00648             $tmp = array();
00649             foreach ($items as $caption => $key) {
00650                 $tmp[$key] = $caption;
00651             }
00652             $items = $tmp;
00653         }
00654 
00655         $ix = 0;
00656         $col = 0;
00657         if ($table) {
00658             $border = is_null($border) ? 0 : $border;
00659             $cellspacing = is_null($cellspacing) ? 0 : $cellspacing;
00660             $cellpadding = is_null($cellpadding) ? 0 : $cellpadding;
00661             $__ctl_out .= "<table border=\"{$border}\" cellspacing=\"{$cellspacing}\" cellpadding=\"{$cellpadding}\">\n";
00662             if ($multirow) { $__ctl_out .= "<tr>\n"; }
00663         }
00664         foreach ($items as $caption => $value) {
00665             if ($table) { $__ctl_out .= "<td>"; }
00666             $checked = false;
00667             if ($selectedByIndex) {
00668                 if (is_array($selected)) {
00669                     if (in_array($ix, $selected)) { $checked = true; }
00670                 } else if ($ix == $selected) {
00671                     $checked = true;
00672                 }
00673             } else {
00674                 if (is_array($selected)) {
00675                     if (in_array($value, $selected)) { $checked = true; }
00676                 } else if ($value == $selected) {
00677                     $checked = true;
00678                 }
00679             }
00680 
00681             $__ctl_out .= "<input type=\"{$type}\" ";
00682             if ($name) {
00683                 $__ctl_out .= 'name="' . h($name) . $suffix . '" ';
00684                 $idSuffix++;
00685                 $__ctl_out .= 'id="' . h($name) . "_{$idSuffix}\" ";
00686             }
00687             if (strlen($value) == 0) { $value = 1; }
00688             $__ctl_out .= 'value="' . h($value) . '" ';
00689             $__ctl_out .= $this->attribsToString($attribs);
00690             if ($checked) {
00691                 $__ctl_out .= 'checked="checked" ';
00692             }
00693             if ($disabled) {
00694                 $__ctl_out .= 'disabled="disabled" ';
00695             }
00696             $__ctl_out .= '/>';
00697             if ($caption) {
00698                 $__ctl_out .= $this->_ctlLabel(null, array(
00699                     'for' => "{$name}_{$idSuffix}", 'caption' => $caption
00700                 ));
00701             }
00702 
00703             if ($ix < $max) {
00704                 if ($multirow) {
00705                     if ($cols) {
00706                         $col++;
00707                         if ($col >= $cols) {
00708                             if ($table) { $__ctl_out .= "</td>\n</tr>\n<tr>\n"; }
00709                             else { $__ctl_out .= "<br />\n"; }
00710                             $col = 0;
00711                         } else {
00712                             if ($table) { $__ctl_out .= "</td>\n"; }
00713                             else { $__ctl_out .= "&nbsp;&nbsp;\n"; }
00714                         }
00715                     } else {
00716                         if ($table) { $__ctl_out .= "</td>\n</tr>\n<tr>\n"; }
00717                         else { $__ctl_out .= "<br />\n"; }
00718                     }
00719                 } else {
00720                     if ($table) { $__ctl_out .= "</td>\n"; }
00721                     else { $__ctl_out .= "&nbsp;&nbsp;\n"; }
00722                 }
00723             }
00724 
00725             $ix++;
00726         }
00727 
00728         if ($table) {
00729             if ($cols && $ix % $cols > 0) {
00730                $__ctl_out .= str_repeat("<td>&nbsp;</td>\n", $cols - $ix % $cols);
00731             }
00732             $__ctl_out .= "</tr>\n</table>\n";
00733         }
00734         return $__ctl_out;
00735     }
00736 
00747     function __processMultiDimArray(& $items, & $key, & $caption, $key2caption = false)
00748     {
00749         if ($caption == '') {
00750             $first = reset($items);
00751             if (!is_array($first)) { return false; }
00752             next($first);
00753             $caption = key($first);
00754         }
00755 
00756         // 传入的 items 是一个多维数组
00757         $new = array();
00758         if ($key2caption) {
00759             foreach ($items as $item) {
00760                 $new[$item[$key]] = $item[$caption];
00761             }
00762         } else {
00763             foreach ($items as $item) {
00764                 $new[$item[$caption]] = $item[$key];
00765             }
00766         }
00767         $items = $new;
00768         return true;
00769     }
00770 }

Generated at Sat Feb 2 15:18:51 2008 for FleaPHP by  doxygen 1.5.3