[ Index ]
 

Code source de Horde 3.1.3

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/Horde/Block/ -> Collection.php (source)

   1  <?php
   2  
   3  require_once  'Horde/Block.php';
   4  
   5  /**
   6   * The Horde_Block_Collection:: class provides an API to the blocks
   7   * (applets) framework.
   8   *
   9   * $Horde: framework/Block/Block/Collection.php,v 1.36.4.17 2006/03/08 17:53:52 chuck Exp $
  10   *
  11   * Copyright 2003-2006 Mike Cochrane <mike@graftonhall.co.nz>
  12   * Copyright 2003-2006 Jan Schneider <jan@horde.org>
  13   *
  14   * See the enclosed file COPYING for license information (LGPL). If you
  15   * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  16   *
  17   * @author  Mike Cochrane <mike@graftonhall.co.nz>
  18   * @author  Jan Schneider <jan@horde.org>
  19   * @since   Horde 3.0
  20   * @package Horde_Block
  21   */
  22  class Horde_Block_Collection {
  23  
  24      /**
  25       * What kind of blocks are we collecting? Defaults to any.
  26       *
  27       * @var string
  28       */
  29      var $_type = 'portal';
  30  
  31      /**
  32       * A hash storing the information about all available blocks from
  33       * all applications.
  34       *
  35       * @var array
  36       */
  37      var $_blocks = array();
  38  
  39      /**
  40       * Constructor.
  41       *
  42       * @param string $type  The kind of blocks to list.
  43       * @param array $apps   The applications whose blocks to list.
  44       */
  45      function Horde_Block_Collection($type = null, $apps = array())
  46      {
  47          if (!is_null($type)) {
  48              $this->_type = $type;
  49          }
  50  
  51          $signature = serialize($apps);
  52          if (isset($_SESSION['horde']['blocks'][$signature])) {
  53              $this->_blocks = &$_SESSION['horde']['blocks'][$signature];
  54              return;
  55          }
  56  
  57          global $registry;
  58          require_once  'Horde/Block.php';
  59  
  60          foreach ($registry->listApps() as $app) {
  61              if (count($apps) && !in_array($app, $apps)) {
  62                  continue;
  63              }
  64              if (is_a($result = $registry->pushApp($app), 'PEAR_Error')) {
  65                  continue;
  66              }
  67              $blockdir = $registry->get('fileroot', $app) . '/lib/Block';
  68              $dh = @opendir($blockdir);
  69              if (is_resource($dh)) {
  70                  while (($file = readdir($dh)) !== false) {
  71                      if (substr($file, -4) == '.php') {
  72                          $block_name = null;
  73                          $block_type = null;
  74                          if (is_readable($blockdir . '/' . $file)) {
  75                              include_once $blockdir . '/' . $file;
  76                          }
  77                          if (!is_null($block_type) && !is_null($this->_type) &&
  78                              $block_type != $this->_type) {
  79                              continue;
  80                          }
  81                          if (!empty($block_name)) {
  82                              $this->_blocks[$app][substr($file, 0, -4)]['name'] = $block_name;
  83                          }
  84                      }
  85                  }
  86                  closedir($dh);
  87              }
  88              $registry->popApp($app);
  89          }
  90  
  91          uksort($this->_blocks,
  92                 create_function('$a, $b',
  93                                 'global $registry;
  94                                  return strcasecmp($registry->get("name", $a), $registry->get("name", $b));')
  95                 );
  96  
  97          $_SESSION['horde']['blocks'][$signature] = &$this->_blocks;
  98      }
  99  
 100      /**
 101       * Returns a single instance of the Horde_Blocks class.
 102       *
 103       * @static
 104       *
 105       * @param string $type  The kind of blocks to list.
 106       * @param array $apps   The applications whose blocks to list.
 107       *
 108       * @return Horde_Block_Collection  The Horde_Block_Collection instance.
 109       */
 110      function &singleton($type = null, $apps = array())
 111      {
 112          static $instances = array();
 113  
 114          $signature = serialize(array($type, $apps));
 115          if (!isset($instances[$signature])) {
 116              $instances[$signature] = new Horde_Block_Collection($type, $apps);
 117          }
 118  
 119          return $instances[$signature];
 120      }
 121  
 122      function &getBlock($app, $name, $params = null)
 123      {
 124          global $registry;
 125  
 126          $path = $registry->get('fileroot', $app) . '/lib/Block/' . $name . '.php';
 127          $class = 'Horde_Block_' . $app . '_' . $name;
 128          if (is_readable($path)) {
 129              include_once $path;
 130          }
 131          if (!class_exists($class)) {
 132              $error = PEAR::raiseError(sprintf(_("%s not found."), $class));
 133              return $error;
 134          }
 135  
 136          $block = &new $class($params);
 137          return $block;
 138      }
 139  
 140      /**
 141       * Returns a pretty printed list of all available blocks.
 142       *
 143       * @return array  A hash with block IDs as keys and application plus block
 144       *                block names as values.
 145       */
 146      function getBlocksList()
 147      {
 148          static $blocks = array();
 149          if (!empty($blocks)) {
 150              return $blocks;
 151          }
 152  
 153          global $registry;
 154  
 155          /* Get available blocks from all apps. */
 156          foreach ($this->_blocks as $app => $app_blocks) {
 157              foreach ($app_blocks as $block_id => $block) {
 158                  $blocks[$app . ':' . $block_id] = $registry->get('name', $app) . ': ' . $block['name'];
 159              }
 160          }
 161  
 162          return $blocks;
 163      }
 164  
 165      /**
 166       * Returns a layout hash with all fixed blocks as per configuration.
 167       *
 168       * @return array  A hash representing a default layout with all fixed
 169       *                blocks.
 170       */
 171      function getFixedBlocks()
 172      {
 173          global $conf;
 174  
 175          $layout = array();
 176          if (isset($conf['portal']['fixed_blocks'])) {
 177              foreach ($conf['portal']['fixed_blocks'] as $block) {
 178                  list($app, $type) = explode(':', $block, 2);
 179                  $layout[] = array(array('app' => $app,
 180                                          'params' => array('type' => $type,
 181                                                            'params' => false),
 182                                          'height' => 1,
 183                                          'width' => 1));
 184              }
 185          }
 186  
 187          return $layout;
 188      }
 189  
 190      /**
 191       * Returns a select widget with all available blocks.
 192       *
 193       * @param string $cur_app    The block from this application gets selected.
 194       * @param string $cur_block  The block with this name gets selected.
 195       *
 196       * @return string  The select tag with all available blocks.
 197       */
 198      function getBlocksWidget($cur_app = null, $cur_block = null, $onchange = false)
 199      {
 200          global $registry;
 201  
 202          $widget = '<select name="app"';
 203          if ($onchange) {
 204              $widget .= ' onchange="document.blockform.action.value=\'save-resume\';document.blockform.submit()"';
 205          }
 206          $widget .= ">\n";
 207          $blocks_list = $this->getBlocksList();
 208          foreach ($blocks_list as $id => $name) {
 209              $widget .= sprintf("<option value=\"%s\"%s>%s</option>\n",
 210                                     $id,
 211                                     ($id == $cur_app . ':' . $cur_block) ? ' selected="selected"' : '',
 212                                     $name);
 213          }
 214          $widget .= "</select>\n";
 215  
 216          return $widget;
 217      }
 218  
 219      /**
 220       * Returns the option type.
 221       */
 222      function getOptionType($app, $block, $param_id)
 223      {
 224          $this->getParams($app, $block);
 225          return $this->_blocks[$app][$block]['params'][$param_id]['type'];
 226      }
 227  
 228      /**
 229       * Returns whether the option is required or not. Defaults to true.
 230       */
 231      function getOptionRequired($app, $block, $param_id)
 232      {
 233          $this->getParams($app, $block);
 234          if (!isset($this->_blocks[$app][$block]['params'][$param_id]['required'])) {
 235              return true;
 236          } else {
 237              return $this->_blocks[$app][$block]['params'][$param_id]['required'];
 238          }
 239      }
 240  
 241      /**
 242       * Returns the values for an option.
 243       */
 244      function &getOptionValues($app, $block, $param_id)
 245      {
 246          $this->getParams($app, $block);
 247          return $this->_blocks[$app][$block]['params'][$param_id]['values'];
 248      }
 249  
 250      /**
 251       * Returns the widget necessary to configure this block.
 252       */
 253      function getOptionsWidget($app, $block, $param_id, $val = null)
 254      {
 255          $widget = '';
 256  
 257          $this->getParams($app, $block);
 258          $param = $this->_blocks[$app][$block]['params'][$param_id];
 259          if (!isset($param['default'])) {
 260              $param['default'] = '';
 261          }
 262  
 263          switch ($param['type']) {
 264          case 'boolean':
 265          case 'checkbox':
 266              $checked = !empty($val[$param_id]) ? ' checked="checked"' : '';
 267              $widget = sprintf('<input type="checkbox" name="params[%s]"%s />', $param_id, $checked);
 268              break;
 269  
 270          case 'enum':
 271              $widget = sprintf('<select name="params[%s]">', $param_id);
 272              foreach ($param['values'] as $key => $name) {
 273                  if (String::length($name) > 30) {
 274                      $name = substr($name, 0, 27) . '...';
 275                  }
 276                  $widget .= sprintf("<option value=\"%s\"%s>%s</option>\n",
 277                                     htmlspecialchars($key),
 278                                     (isset($val[$param_id]) && $val[$param_id] == $key) ? ' selected="selected"' : '',
 279                                     htmlspecialchars($name));
 280              }
 281  
 282              $widget .= '</select>';
 283              break;
 284  
 285          case 'multienum':
 286              $widget = sprintf('<select multiple="multiple" name="params[%s][]">', $param_id);
 287              foreach ($param['values'] as $key => $name) {
 288                  if (String::length($name) > 30) {
 289                      $name = substr($name, 0, 27) . '...';
 290                  }
 291                  $widget .= sprintf("<option value=\"%s\"%s>%s</option>\n",
 292                                     htmlspecialchars($key),
 293                                     (isset($val[$param_id]) && in_array($key, $val[$param_id])) ? ' selected="selected"' : '',
 294                                     htmlspecialchars($name));
 295              }
 296  
 297              $widget .= '</select>';
 298              break;
 299  
 300          case 'mlenum':
 301              // Multi-level enum.
 302              if (is_array($val) && isset($val['__' . $param_id])) {
 303                  $firstval = $val['__' . $param_id];
 304              } else {
 305                  $tmp = array_keys($param['values']);
 306                  $firstval = current($tmp);
 307              }
 308              $blockvalues = $param['values'][$firstval];
 309              asort($blockvalues);
 310  
 311              $widget = sprintf('<select name="params[__%s]" onchange="document.blockform.action.value=\'save-resume\';document.blockform.submit()">', $param_id) . "\n";
 312              foreach ($param['values'] as $key => $values) {
 313                  $name = String::length($key) > 30 ? String::substr($key, 0, 27) . '...' : $key;
 314                  $widget .= sprintf("<option value=\"%s\"%s>%s</option>\n",
 315                                     htmlspecialchars($key),
 316                                     $key == $firstval ? ' selected="selected"' : '',
 317                                     htmlspecialchars($name));
 318              }
 319              $widget .= "</select><br />\n";
 320  
 321              $widget .= sprintf("<select name=\"params[%s]\">\n", $param_id);
 322              foreach ($blockvalues as $key => $name) {
 323                  $name = (String::length($name) > 30) ? String::substr($name, 0, 27) . '...' : $name;
 324                  $widget .= sprintf("<option value=\"%s\"%s>%s</option>\n",
 325                                     htmlspecialchars($key),
 326                                     $val[$param_id] == $key ? ' selected="selected"' : '',
 327                                     htmlspecialchars($name));
 328              }
 329              $widget .= "</select><br />\n";
 330              break;
 331  
 332          case 'color':
 333              $val = isset($block->_params[$param_id]) ? $block->_params[$param_id] : '';
 334              $widget = sprintf('<input style="background-color:%s" type="text" name="params[%s]" value="%s" />', $val, $param_id, $val);
 335              $url  = $registry->get('webroot', 'horde');
 336              $url .= Util::addParameter('/services/images/colorpicker.php?target=params[' . $param_id . ']', 'form', 'blockform');
 337              $widget .= sprintf('<a href="%s" onclick="window.open(\'%s\', \'colorpicker\', \'toolbar=no,location=no,status=no,scrollbars=no,resizable=no,width=120,height=250,left=100,top=100\'); return false;" onmouseout="window.status=\'\';" onmouseover="window.status=\'%s\'; return true;" class="widget" target="colorpicker">',
 338                                 $url, $url, _("Color Picker"));
 339              $widget .= Horde::img('colorpicker.png', _("Color Picker"), 'height="16"', $registry->getImageDir('horde'));
 340              $widget .= '</a>';
 341              break;
 342  
 343          case 'int':
 344          case 'text':
 345              $widget = sprintf('<input type="text" name="params[%s]" value="%s" />', $param_id, !isset($val[$param_id]) ? $param['default'] : $val[$param_id]);
 346              break;
 347  
 348          case 'error':
 349              $widget = '<span class="form-error">' . $param['default'] . '</span>';
 350              break;
 351          }
 352  
 353          return $widget;
 354      }
 355  
 356      /**
 357       * Returns the name of the specified block.
 358       *
 359       * @param string $app    An application name.
 360       * @param string $block  A block name.
 361       *
 362       * @return string  The name of the specified block.
 363       */
 364      function getName($app, $block)
 365      {
 366          if (!isset($this->_blocks[$app][$block])) {
 367              return sprintf(_("Block \"%s\" of application \"%s\" not found."), $block, $app);
 368          }
 369          return $this->_blocks[$app][$block]['name'];
 370      }
 371  
 372      /**
 373       * Returns the parameter list of the specified block.
 374       *
 375       * @param string $app    An application name.
 376       * @param string $block  A block name.
 377       *
 378       * @return array  An array with all paramter names.
 379       */
 380      function getParams($app, $block)
 381      {
 382          if (!isset($this->_blocks[$app][$block]['params'])) {
 383              $blockOb = &$this->getBlock($app, $block);
 384              if (is_a($blockOb, 'PEAR_Error')) {
 385                  return $blockOb;
 386              }
 387              $this->_blocks[$app][$block]['params'] = $blockOb->getParams();
 388          }
 389  
 390          if (isset($this->_blocks[$app][$block]['params']) &&
 391              is_array($this->_blocks[$app][$block]['params'])) {
 392              return array_keys($this->_blocks[$app][$block]['params']);
 393          } else {
 394              return array();
 395          }
 396      }
 397  
 398      /**
 399       * Returns the (clear text) name of the specified parameter.
 400       *
 401       * @param string $app    An application name.
 402       * @param string $block  A block name.
 403       * @param string $param  A parameter name.
 404       *
 405       * @return string  The name of the specified parameter.
 406       */
 407      function getParamName($app, $block, $param)
 408      {
 409          $this->getParams($app, $block);
 410          return $this->_blocks[$app][$block]['params'][$param]['name'];
 411      }
 412  
 413      /**
 414       * Returns the default value of the specified parameter.
 415       *
 416       * @param string $app    An application name.
 417       * @param string $block  A block name.
 418       * @param string $param  A parameter name.
 419       *
 420       * @return string  The default value of the specified parameter or null.
 421       */
 422      function getDefaultValue($app, $block, $param)
 423      {
 424          $this->getParams($app, $block);
 425          if (isset($this->_blocks[$app][$block]['params'][$param]['default'])) {
 426              return $this->_blocks[$app][$block]['params'][$param]['default'];
 427          }
 428          return null;
 429      }
 430  
 431      /**
 432       * Returns if the specified block is customizeable by the user.
 433       *
 434       * @param string $app    An application name.
 435       * @param string $block  A block name.
 436       *
 437       * @return boolean  True is the block is customizeable.
 438       */
 439      function isEditable($app, $block)
 440      {
 441          $this->getParams($app, $block);
 442          return isset($this->_blocks[$app][$block]['params']) &&
 443              count($this->_blocks[$app][$block]['params']);
 444      }
 445  
 446  }


Généré le : Sun Feb 25 18:01:28 2007 par Balluche grâce à PHPXref 0.7