[ Index ]
 

Code source de Horde 3.1.3

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

title

Body

[fermer]

/lib/Horde/ -> Help.php (source)

   1  <?php
   2  
   3  /**
   4   * Raw help in the string.
   5   */
   6  define('HELP_SOURCE_RAW', 0);
   7  
   8  /**
   9   * Help text is in a file.
  10   */
  11  define('HELP_SOURCE_FILE', 1);
  12  
  13  /**
  14   * The Help:: class provides an interface to the online help subsystem.
  15   *
  16   * $Horde: framework/Horde/Horde/Help.php,v 1.68.8.18 2006/03/02 05:27:35 slusarz Exp $
  17   *
  18   * Copyright 1999-2006 Jon Parise <jon@horde.org>
  19   *
  20   * See the enclosed file COPYING for license information (LGPL). If you
  21   * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  22   *
  23   * @author  Jon Parise <jon@horde.org>
  24   * @since   Horde 1.3
  25   * @package Horde_Framework
  26   */
  27  class Help {
  28  
  29      /**
  30       * Handle for the XML parser object.
  31       *
  32       * @var resource
  33       */
  34      var $_parser = 0;
  35  
  36      /**
  37       * String buffer to hold the XML help source.
  38       *
  39       * @var string
  40       */
  41      var $_buffer = '';
  42  
  43      /**
  44       * String containing the ID of the requested help entry.
  45       *
  46       * @var string
  47       */
  48      var $_reqEntry = '';
  49  
  50      /**
  51       * String containing the ID of the current help entry.
  52       *
  53       * @var string
  54       */
  55      var $_curEntry = '';
  56  
  57      /**
  58       * String containing the formatted output.
  59       *
  60       * @var string
  61       */
  62      var $_output = '';
  63  
  64      /**
  65       * Boolean indicating whether we're inside a <help> block.
  66       *
  67       * @var boolean
  68       */
  69      var $_inHelp = false;
  70  
  71      /**
  72       * Boolean indicating whether we're inside the requested block.
  73       *
  74       * @var boolean
  75       */
  76      var $_inBlock = false;
  77  
  78      /**
  79       * Boolean indicating whether we're inside a <title> block.
  80       *
  81       * @var boolean
  82       */
  83      var $_inTitle = false;
  84  
  85      /**
  86       * Hash containing an index of all of the help entries.
  87       *
  88       * @var array
  89       */
  90      var $_entries = array();
  91  
  92      /**
  93       * String containing the charset of the XML data source.
  94       *
  95       * @var string
  96       */
  97      var $_charset = 'iso-8859-1';
  98  
  99      /**
 100       * Hash of user-defined function handlers for the XML elements.
 101       *
 102       * @var array
 103       */
 104      var $_handlers = array(
 105          'help'     =>  '_helpHandler',
 106          'entry'    =>  '_entryHandler',
 107          'title'    =>  '_titleHandler',
 108          'heading'  =>  '_headingHandler',
 109          'para'     =>  '_paraHandler',
 110          'ref'      =>  '_refHandler',
 111          'eref'     =>  '_erefHandler',
 112          'href'     =>  '_hrefHandler',
 113          'b'        =>  '_bHandler',
 114          'i'        =>  '_iHandler',
 115          'pre'      =>  '_preHandler',
 116          'tip'      =>  '_tipHandler',
 117          'warn'     =>  '_warnHandler'
 118      );
 119  
 120      /**
 121       * Constructor
 122       *
 123       * @param integer $source  The source of the XML help data, based on the
 124       *                         HELP_SOURCE_* constants.
 125       * @param string $arg      Source-dependent argument for this Help
 126       *                         instance.
 127       */
 128      function Help($source, $arg = null)
 129      {
 130          global $language, $nls;
 131  
 132          if (!Util::extensionExists('xml')) {
 133              Horde::fatal(PEAR::raiseError('The XML functions are not available. Rebuild PHP with --with-xml.'), __FILE__, __LINE__, false);
 134          }
 135  
 136          $this->_charset = isset($nls['charsets'][$language]) ? $nls['charsets'][$language] : 'ISO-8859-1';
 137  
 138          /* Populate $this->_buffer based on $source. */
 139          switch ($source) {
 140          case HELP_SOURCE_RAW:
 141              $this->_buffer = $arg;
 142              break;
 143  
 144          case HELP_SOURCE_FILE:
 145              if (@file_exists($arg[0]) && @filesize($arg[0])) {
 146                  $this->_buffer = file_get_contents($arg[0]);
 147              } elseif (@file_exists($arg[1]) && @filesize($arg[1])) {
 148                  $this->_buffer = file_get_contents($arg[1]);
 149              } else {
 150                  $this->_buffer = '';
 151              }
 152              break;
 153  
 154          default:
 155              $this->_buffer = '';
 156              break;
 157          }
 158      }
 159  
 160      /**
 161       * Generates the HTML link that will pop up a help window for the
 162       * requested topic.
 163       *
 164       * @param string $module  The name of the current Horde module.
 165       * @param string $topic   The help topic to be displayed.
 166       *
 167       * @return string  The HTML to create the help link.
 168       */
 169      function link($module, $topic)
 170      {
 171          global $conf, $registry, $browser;
 172  
 173          if (!Horde::showService('help')) {
 174              return '&nbsp;';
 175          }
 176  
 177          if ($browser->hasFeature('javascript')) {
 178              Horde::addScriptFile('popup.js', 'horde');
 179          }
 180  
 181          $url = Horde::url($registry->get('webroot', 'horde') . '/services/help/', true);
 182          $url = Util::addParameter($url, array('module' => $module,
 183                                                'topic' => $topic));
 184          $html = Horde::link($url, _("Help"), '', 'hordehelpwin', 'popup(this.href); return false;');
 185  
 186          return $html . Horde::img('help.png', _("Help"), 'width="16" height="16"', $registry->getImageDir('horde')) . '</a>';
 187      }
 188  
 189      /**
 190       * Cleans up the Help class resources.
 191       *
 192       * @return boolean  Returns true on success, false on failure.
 193       */
 194      function cleanup()
 195      {
 196          $this->_buffer = '';
 197          return xml_parser_free($this->_parser);
 198      }
 199  
 200      /**
 201       * Looks up the requested entry in the XML help buffer.
 202       *
 203       * @param string $entry  String containing the entry ID.
 204       */
 205      function lookup($entry)
 206      {
 207          $this->_output = '';
 208          $this->_reqEntry = String::upper($entry);
 209          if (!$this->_parser) {
 210              $this->_init();
 211          }
 212          xml_parse($this->_parser, $this->_buffer, true);
 213      }
 214  
 215      /**
 216       * Returns a hash of all of the topics in this help buffer.
 217       *
 218       * @return array  Hash of all of the topics in this buffer.
 219       */
 220      function topics()
 221      {
 222          if (!$this->_parser) {
 223              $this->_init();
 224          }
 225          xml_parse($this->_parser, $this->_buffer, true);
 226  
 227          return $this->_entries;
 228      }
 229  
 230      /**
 231       * Display the contents of the formatted output buffer.
 232       */
 233      function display()
 234      {
 235          echo $this->_output;
 236      }
 237  
 238      /**
 239       * Initialzes the XML parser.
 240       *
 241       * @access private
 242       *
 243       * @return boolean  Returns true on success, false on failure.
 244       */
 245      function _init()
 246      {
 247          /* Create a new parser and set its default properties. */
 248          $this->_parser = xml_parser_create();
 249          xml_set_object($this->_parser, $this);
 250          xml_parser_set_option($this->_parser, XML_OPTION_CASE_FOLDING, false);
 251          xml_set_element_handler($this->_parser, '_startElement', '_endElement');
 252          xml_set_character_data_handler($this->_parser, '_defaultHandler');
 253  
 254          return ($this->_parser != 0);
 255      }
 256  
 257      /**
 258       * User-defined function callback for start elements.
 259       *
 260       * @access private
 261       *
 262       * @param object $parser  Handle to the parser instance.
 263       * @param string $name    The name of this XML element.
 264       * @param array $attrs    List of this element's attributes.
 265       */
 266      function _startElement($parser, $name, $attrs)
 267      {
 268          /* Call the assigned handler for this element, if one is
 269           * available. */
 270          if (in_array($name, array_keys($this->_handlers))) {
 271              call_user_func(array(&$this, $this->_handlers[$name]), true, $attrs);
 272          }
 273      }
 274  
 275      /**
 276       * User-defined function callback for end elements.
 277       *
 278       * @access private
 279       *
 280       * @param object $parser  Handle to the parser instance.
 281       * @param string $name    The name of this XML element.
 282       */
 283      function _endElement($parser, $name)
 284      {
 285          /* Call the assigned handler for this element, if one is available. */
 286          if (in_array($name, array_keys($this->_handlers))) {
 287              call_user_func(array(&$this, $this->_handlers[$name]), false);
 288          }
 289      }
 290  
 291      /**
 292       * User-defined function callback for character data.
 293       *
 294       * @access private
 295       *
 296       * @param object $parser  Handle to the parser instance.
 297       * @param string $data    String of character data.
 298       */
 299      function _defaultHandler($parser, $data)
 300      {
 301          $data = String::convertCharset($data, version_compare(zend_version(), '2', '<') ? $this->_charset : 'UTF-8');
 302          if ($this->_inTitle) {
 303              $this->_entries[$this->_curEntry] .= $data;
 304          }
 305          if ($this->_inHelp && $this->_inBlock) {
 306              $this->_output .= htmlspecialchars($data);
 307          }
 308      }
 309  
 310      /**
 311       * XML element handler for the <help> tag.
 312       *
 313       * @access private
 314       *
 315       * @param boolean $startTag  Boolean indicating whether this instance is a
 316       *                           start tag.
 317       * @param array $attrs       Additional element attributes (Not used).
 318       */
 319      function _helpHandler($startTag, $attrs = array())
 320      {
 321          $this->_inHelp = $startTag ?  true : false;
 322      }
 323  
 324      /**
 325       * XML element handler for the <entry> tag.
 326       * Attributes: id
 327       *
 328       * @access private
 329       *
 330       * @param boolean $startTag  Boolean indicating whether this instance is a
 331       *                           start tag.
 332       * @param array $attrs       Additional element attributes.
 333       */
 334      function _entryHandler($startTag, $attrs = array())
 335      {
 336          if (!$startTag) {
 337              $this->_inBlock = false;
 338          } else {
 339              $id = String::upper($attrs['id']);
 340              $this->_curEntry = $id;
 341              $this->_entries[$id] = '';
 342              $this->_inBlock = ($id == $this->_reqEntry);
 343          }
 344      }
 345  
 346      /**
 347       * XML element handler for the <title> tag.
 348       *
 349       * @access private
 350       *
 351       * @param boolean $startTag  Boolean indicating whether this instance is a
 352       *                           start tag.
 353       * @param array $attrs       Additional element attributes (Not used).
 354       */
 355      function _titleHandler($startTag, $attrs = array())
 356      {
 357          $this->_inTitle = $startTag;
 358          if ($this->_inHelp && $this->_inBlock) {
 359              $this->_output .= $startTag ? '<h1>' : '</h1>';
 360          }
 361      }
 362  
 363      /**
 364       * XML element handler for the <heading> tag.
 365       *
 366       * @access private
 367       *
 368       * @param boolean $startTag  Boolean indicating whether this instance is a
 369       *                           start tag.
 370       * @param  array $attrs      Additional element attributes (Not used).
 371       */
 372      function _headingHandler($startTag, $attrs = array())
 373      {
 374          if ($this->_inHelp && $this->_inBlock) {
 375              $this->_output .= $startTag ? '<h2>' : '</h2>';
 376          }
 377      }
 378  
 379      /**
 380       * XML element handler for the <para> tag.
 381       *
 382       * @access private
 383       *
 384       * @param boolean $startTag  Boolean indicating whether this instance is a
 385       *                           start tag.
 386       * @param array $attrs       Additional element attributes (Not used).
 387       */
 388      function _paraHandler($startTag, $attrs = array())
 389      {
 390          if ($this->_inHelp && $this->_inBlock) {
 391              $this->_output .= $startTag ? '<p>' : '</p>';
 392          }
 393      }
 394  
 395      /**
 396       * XML element handler for the <ref> tag.
 397       * Required attributes: ENTRY, MODULE
 398       *
 399       * @access private
 400       *
 401       * @param boolean $startTag  Boolean indicating whether this instance is a
 402       *                           start tag.
 403       * @param array $attrs       Additional element attributes.
 404       */
 405      function _refHandler($startTag, $attrs = array())
 406      {
 407          if ($this->_inHelp && $this->_inBlock) {
 408              if ($startTag && isset($attrs['module']) && isset($attrs['entry'])) {
 409                  $url = Util::addParameter(Horde::selfUrl(),
 410                                            array('show' => 'entry',
 411                                                  'module' => $attrs['module'],
 412                                                  'topic'  => $attrs['entry']));
 413                  $this->_output .= Horde::link($url);
 414              } else {
 415                  $this->_output .= '</a>';
 416              }
 417          }
 418      }
 419  
 420      /**
 421       * XML element handler for the <eref> tag.
 422       * Required elements: URL
 423       *
 424       * @access private
 425       *
 426       * @param boolean $startTag  Boolean indicating whether this instance is a
 427       *                           start tag.
 428       * @param array $attrs       Additional element attributes.
 429       */
 430      function _erefHandler($startTag, $attrs = array())
 431      {
 432          if ($this->_inHelp && $this->_inBlock) {
 433              if ($startTag) {
 434                  $this->_output .= Horde::link($attrs['url'], null, '', '_blank');
 435              } else {
 436                  $this->_output .= '</a>';
 437              }
 438          }
 439      }
 440  
 441      /**
 442       * XML element handler for the <href> tag.
 443       * Required elements: url, app.
 444       *
 445       * @access private
 446       *
 447       * @param boolean $startTag  Boolean indicating whether this instance is a
 448       *                           start tag.
 449       * @param array $attrs       Additional element attributes.
 450       */
 451      function _hrefHandler($startTag, $attrs = array())
 452      {
 453          if ($this->_inHelp && $this->_inBlock) {
 454              if ($startTag) {
 455                  global $registry;
 456                  $url = Horde::url($registry->get('webroot', $attrs['app']) . '/' . $attrs['url']);
 457                  $this->_output .= Horde::link($url, null, '', '_blank');
 458              } else {
 459                  $this->_output .= '</a>';
 460              }
 461          }
 462      }
 463  
 464      /**
 465       * XML element handler for the &lt;b&gt; tag.
 466       *
 467       * @access private
 468       *
 469       * @param boolean $startTag  Boolean indicating whether this instance is a
 470       *                           start tag.
 471       * @param array $attrs       Additional element attributes (Not used).
 472       */
 473      function _bHandler($startTag, $attrs = array())
 474      {
 475          if ($this->_inHelp && $this->_inBlock) {
 476              $this->_output .= $startTag ? '<strong>' : '</strong>';
 477          }
 478      }
 479  
 480      /**
 481       * XML element handler for the &lt;i&gt; tag.
 482       *
 483       * @access private
 484       *
 485       * @param boolean $startTag  Boolean indicating whether this instance is a
 486       *                           start tag.
 487       * @param array $attrs       Additional element attributes.
 488       */
 489      function _iHandler($startTag, $attrs = array())
 490      {
 491          if ($this->_inHelp && $this->_inBlock) {
 492              $this->_output .= $startTag ? '<em>' : '</em>';
 493          }
 494      }
 495  
 496      /**
 497       * XML element handler for the &lt;pre&gt; tag.
 498       *
 499       * @access private
 500       *
 501       * @param boolean $startTag  Boolean indicating whether this instance is a
 502       *                           start tag.
 503       * @param array $attrs       Additional element attributes.
 504       */
 505      function _preHandler($startTag, $attrs = array())
 506      {
 507          if ($this->_inHelp && $this->_inBlock) {
 508              $this->_output .= $startTag ? '<pre>' : '</pre>';
 509          }
 510      }
 511  
 512      /**
 513       * XML element handler for the <tip> tag.
 514       *
 515       * @access private
 516       *
 517       * @param boolean $startTag  Boolean indicating whether this instance is a
 518       *                           start tag.
 519       * @param array $attrs       Additional element attributes.
 520       */
 521      function _tipHandler($startTag, $attrs = array())
 522      {
 523          if ($this->_inHelp && $this->_inBlock) {
 524              $this->_output .= $startTag ? '<em class="helpTip">' : '</em>';
 525          }
 526      }
 527  
 528      /**
 529       * XML element handler for the <warn> tag.
 530       *
 531       * @access private
 532       *
 533       * @param boolean $startTag  Boolean indicating whether this instance is a
 534       *                           start tag.
 535       * @param array $attrs       Additional element attributes.
 536       */
 537      function _warnHandler($startTag, $attrs = array())
 538      {
 539          if ($this->_inHelp && $this->_inBlock) {
 540              $this->_output .= $startTag ? '<em class="helpWarn">' : '</em>';
 541          }
 542      }
 543  
 544  }


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