home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / Widget.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  11.7 KB  |  430 lines

  1. <?php
  2. //
  3. // +---------------------------------------------------------------------------+
  4. // | PEAR :: XML :: Transformer :: Widget Namespace Handler                    |
  5. // +---------------------------------------------------------------------------+
  6. // | Copyright (c) 2002-2004 Sebastian Bergmann <sb@sebastian-bergmann.de> and |
  7. // |                         Kristian K÷hntopp <kris@koehntopp.de>.            |
  8. // +---------------------------------------------------------------------------+
  9. // | This source file is subject to version 3.00 of the PHP License,           |
  10. // | that is available at http://www.php.net/license/3_0.txt.                  |
  11. // | If you did not receive a copy of the PHP license and are unable to        |
  12. // | obtain it through the world-wide-web, please send a note to               |
  13. // | license@php.net so we can mail you a copy immediately.                    |
  14. // +---------------------------------------------------------------------------+
  15. //
  16. // $Id: Widget.php,v 1.7 2004/01/01 10:31:55 sebastian Exp $
  17. //
  18.  
  19. require_once 'XML/Transformer/Namespace.php';
  20.  
  21. /**
  22. * Handler for the Widget Namespace.
  23. *
  24. * Implements <widget:obox /> similar to http://docs.roxen.com/roxen/2.2/creator/text/obox.tag.
  25. * Implements <widget:oboxtitle> as counterpart to <obox><title>..</title></obox> in Roxen.
  26. *
  27. *
  28. * @author  Sebastian Bergmann <sb@sebastian-bergmann.de>
  29. * @author  Kristian K÷hntopp <kris@koehntopp.de>
  30. * @version $Revision: 1.7 $
  31. * @access  public
  32. */
  33. class XML_Transformer_Namespace_Widget extends XML_Transformer_Namespace {
  34.     // {{{ Members
  35.     
  36.     /**
  37.     * @var    boolean
  38.     * @access public
  39.     */
  40.     var $defaultNamespacePrefix = 'widget';
  41.  
  42.     /**
  43.     * @var    array
  44.     * @access private
  45.     */
  46.     var $_oboxAttributes = array();
  47.  
  48.     /**
  49.     * @var    string
  50.     * @access private
  51.     */
  52.     var $_oboxUnitPngPath = "";
  53.  
  54.     /**
  55.     * @var    string
  56.     * @access private
  57.     */
  58.     var $_oboxUnitPngURL  = "/cache/unit.png";
  59.  
  60.     // }}}
  61.     // {{{ function _makeUnitPngPath()
  62.  
  63.     /**
  64.     * Create the filesystem pathname for the unitPng
  65.     *
  66.     * @return void
  67.     * @access private
  68.     */
  69.     function _makeUnitPngPath() {
  70.       $this->_oboxUnitPngPath = $_SERVER['DOCUMENT_ROOT']
  71.                               . "/"
  72.                               . $this->_oboxUnitPngURL;
  73.  
  74.       return;
  75.     }
  76.  
  77.     // }}}
  78.     // {{{ function _unitPng()
  79.  
  80.     /**
  81.     * Create the transparent unitPng and return its URL
  82.     *
  83.     * @return string
  84.     * @access private
  85.     */
  86.     function _unitpng() {
  87.         if (file_exists($this->_oboxUnitPngPath)) {
  88.             return $this->_oboxUnitPngURL;
  89.         }
  90.  
  91.         $im    = ImageCreate(1, 1);
  92.         $trans = ImageColorAllocate($im, 128, 128, 128);
  93.  
  94.         ImageColorTransparent($im, $trans);
  95.         ImageFilledRectangle($im, 0,0,1,1,$trans);
  96.  
  97.         $this->_makeUnitPngPath();
  98.  
  99.         ImagePNG($im, $this->_oboxUnitPngPath);
  100.         ImageDestroy($im);
  101.  
  102.         return $this->_oboxUnitURL;
  103.     }
  104.  
  105.     // }}}
  106.     // {{{ function _imagePlaceholder($h = false, $w = false)
  107.  
  108.     /**
  109.     * Create a placeholder image of $h pixel height and $w pixel width
  110.     *
  111.     * @param  integer
  112.     * @param  integer
  113.     * @return string
  114.     * @access private
  115.     */
  116.     function _imagePlaceholder($h = false, $w = false) {
  117.         if ($h === false) {
  118.             $h = isset($this->_oboxAttributes['outlinewidth']) ? $this->_oboxAttributes['outlinewidth'] : 1;
  119.         }
  120.  
  121.         if ($w === false) {
  122.             $w = $h;
  123.         }
  124.  
  125.         return sprintf(
  126.           '<img src="%s" alt="" width="%s" height="%s" />',
  127.           $this->_unitpng(),
  128.           $w,
  129.           $h
  130.         );
  131.     }
  132.  
  133.     // }}}
  134.     // {{{ function _oboxGetAttr($name)
  135.  
  136.     /**
  137.     * Return value of $name suitable for attribute printing (name='value')
  138.     * or an empty string ('')
  139.     *
  140.     * @param  string
  141.     * @return string
  142.     * @access private
  143.     */
  144.     function _oboxGetAttr($name) {
  145.         if (isset($this->_oboxAttributes[$name])) {
  146.             return sprintf(
  147.               " %s='%s'",
  148.               $name,
  149.               $this->_oboxAttributes[$name]
  150.             );
  151.         } else {
  152.             return '';
  153.         }
  154.     }
  155.  
  156.     // }}}
  157.     // {{{ function _oboxGetAttrAs($name, $attributes)
  158.  
  159.     /**
  160.     * Return value of $name suitable as printable attr $attr (attr='valueofname')
  161.     * or an empty string ('')
  162.     *
  163.     * @param  string
  164.     * @param  string
  165.     * @return string
  166.     * @access private
  167.     */
  168.     function _oboxGetAttrAs($name, $attributes) {
  169.         if (isset($this->_oboxAttributes[$name])) {
  170.             return sprintf(
  171.               " %s='%s'",
  172.               $attributes,
  173.               $this->_oboxAttributes[$name]
  174.             );
  175.         } else {
  176.             return '';
  177.         }
  178.     }
  179.  
  180.     // }}}
  181.     // {{{ function _oboxGetValueWithDefault($name, $def)
  182.  
  183.     /**
  184.     * Return value of $name as value or $def, if empty.
  185.     *
  186.     * @param  string
  187.     * @param  string
  188.     * @return string
  189.     * @access private
  190.     */
  191.     function _oboxGetValueWithDefault($name, $def) {
  192.         if (isset($this->_oboxAttributes[$name])) {
  193.             return $this->_oboxAttributes[$name];
  194.         } else {
  195.             return $def;
  196.         }
  197.     }
  198.  
  199.     // }}}
  200.     // {{{ function _titlebox()
  201.  
  202.     /**
  203.     * Create the obox titlebox. Ugly.
  204.     *
  205.     * @return string
  206.     * @access private
  207.     */
  208.     function _titlebox() {
  209.         if (!isset($this->_oboxAttributes['title'])) {
  210.             return sprintf(
  211.               " <tr>\n  <td colspan='5'%s>%s</td>\n </tr>\n",
  212.               $this->_oboxGetAttrAs("outlinecolor", "bgcolor"),
  213.               $this->_imagePlaceholder()
  214.             );
  215.         }
  216.  
  217.         $left      = $this->_oboxGetValueWithDefault('left',      20);
  218.         $right     = $this->_oboxGetValueWithDefault('right',     20);
  219.         $leftskip  = $this->_oboxGetValueWithDefault('leftskip',  10);
  220.         $rightskip = $this->_oboxGetValueWithDefault('rightskip', 10);
  221.  
  222.         if (!isset($this->_oboxAttributes['titlecolor']) &&
  223.              isset($this->_oboxAttributes['bgcolor'])) {
  224.             $this->_oboxAttributes['titlecolor'] = $this->_oboxAttributes['bgcolor'];
  225.         }
  226.  
  227.         $r .= sprintf(
  228.           " <tr>\n  <td>%s</td>\n  <td>%s</td>\n  <td nowrap='nowrap' rowspan='3'%s%s%s>%s%s%s</td>\n  <td>%s</td>\n  <td>%s</td>\n </tr>\n",
  229.           $this->_imagePlaceholder(1,1),
  230.           $this->_imagePlaceholder(1, $left),
  231.           $this->_oboxGetAttrAs('titlealign', 'align'),
  232.           $this->_oboxGetAttrAs('titlevalign', 'valign'),
  233.           $this->_oboxGetAttrAs('titlecolor', 'bgcolor'),
  234.           $this->_imagePlaceholder(1, $leftskip),
  235.           $this->_oboxAttributes['title'],
  236.           $this->_imagePlaceholder(1, $rightskip),
  237.           $this->_imagePlaceholder(1, $right),
  238.           $this->_imagePlaceholder(1,1)
  239.         );
  240.  
  241.         $r .= sprintf(
  242.           " <tr%s>\n  <td colspan='2' height='1'%s>%s</td>\n  <td colspan='2' height='1'%s>%s</td>\n </tr>\n",
  243.           $this->_oboxGetAttrAs("bgcolor", "bgcolor"),
  244.           $this->_oboxGetAttrAs("outlinecolor", "bgcolor"),
  245.           $this->_imagePlaceholder($this->_oboxGetValueWithDefault("outlinewidth", 1), 1),
  246.           $this->_oboxGetAttrAs("outlinecolor", "bgcolor"),
  247.           $this->_imagePlaceholder($this->_oboxGetValueWithDefault("outlinewidth", 1), 1)
  248.         );
  249.  
  250.         $r .= sprintf(
  251.           " <tr%s>\n  <td%s>%s</td>\n  <td>%s</td>\n  <td>%s</td>\n  <td%s>%s</td>\n </tr>\n",
  252.           $this->_oboxGetAttrAs("bgcolor", "bgcolor"),
  253.           $this->_oboxGetAttrAs('outlinecolor', 'bgcolor'),
  254.           $this->_imagePlaceholder(1, $this->_oboxGetValueWithDefault("outlinewidth", 1)),
  255.           $this->_imagePlaceholder(1, 1),
  256.           $this->_imagePlaceholder(1, 1),
  257.           $this->_oboxGetAttrAs('outlinecolor', 'bgcolor'),
  258.           $this->_imagePlaceholder(1, $this->_oboxGetValueWithDefault("outlinewidth", 1))
  259.         );
  260.  
  261.         return $r;
  262.     }
  263.  
  264.     // }}}
  265.     // {{{ function _box($cdata)
  266.  
  267.     /**
  268.     * Create the actual obox.
  269.     *
  270.     * @param  string
  271.     * @return string
  272.     * @access private
  273.     */
  274.     function _box($cdata) {
  275.         /* Outer container */
  276.         $r  = sprintf(
  277.           "<table border='0' cellpadding='0' cellspacing='0'%s%s>\n",
  278.           $this->_oboxGetAttr("align"),
  279.           $this->_oboxGetAttr("width")
  280.         );
  281.  
  282.         /* Title */
  283.         $r .= $this->_titlebox();
  284.  
  285.         /* Content container */
  286.         $r .= sprintf(
  287.           " <tr%s>\n",
  288.           $this->_oboxGetAttr("bgcolor")
  289.         );
  290.  
  291.         $r .= sprintf(
  292.           "  <td%s%s>%s</td>\n  <td colspan='3'>\n",
  293.           $this->_oboxGetAttrAs("outlinewidth", "width"),
  294.           $this->_oboxGetAttrAs("outlinecolor", "bgcolor"),
  295.           $this->_imagePlaceholder(1, $this->_oboxGetValueWithDefault("outlinewidth", 1))
  296.         );
  297.  
  298.         $r .= sprintf(
  299.           "<table %s%s border='0' cellspacing='0' cellpadding='%s'><tr><td%s%s>%s</td></tr></table>\n  </td>\n",
  300.           $this->_oboxGetAttrAs("contentwidth", "width"),
  301.           $this->_oboxGetAttrAs("contentheight", "height"),
  302.           $this->_oboxGetValueWithDefault("contentpadding", 0),
  303.           $this->_oboxGetAttrAs("contentalign", "align"),
  304.           $this->_oboxGetAttrAs("contentvalign", "valign"),
  305.           $cdata
  306.         );
  307.  
  308.         $r .= sprintf(
  309.           "  <td%s%s>%s</td>\n </tr>\n",
  310.           $this->_oboxGetAttrAs("outlinewidth", "width"),
  311.           $this->_oboxGetAttrAs("outlinecolor", "bgcolor"),
  312.           $this->_imagePlaceholder(1, $this->_oboxGetValueWithDefault("outlinewidth", 1))
  313.         );
  314.  
  315.         /* Footer line */
  316.         $r .= sprintf(
  317.           " <tr>\n  <td colspan='5'%s>%s</td>\n </tr>\n</table>\n",
  318.           $this->_oboxGetAttrAs("outlinecolor", "bgcolor"),
  319.           $this->_imagePlaceholder()
  320.         );
  321.  
  322.         return $r;
  323.     }
  324.  
  325.     // }}}
  326.     // {{{ function start_obox($attributes)
  327.  
  328.     /**
  329.     * <obox /> -- This container creates an outlined box.
  330.     *
  331.     * The outer Table is controlled by
  332.     *   align=...
  333.     *   width=...
  334.     *
  335.     * The title is controlled by
  336.     *   title=...
  337.     *   titlealign=...
  338.     *   titlevalign=...
  339.     *   titlecolor=...
  340.     *
  341.     * The outline is controlled by
  342.     *   outlinecolor=...
  343.     *   outlinewidth=...
  344.     *   left=...
  345.     *   leftskip=...
  346.     *   right=...
  347.     *   rightskip=...
  348.     *
  349.     * The inner table cell is controlled by
  350.     *   contentalign=...
  351.     *   contentvalign=...
  352.     *   contentpadding=...
  353.     *   contentwidth=...
  354.     *   contentheight=...
  355.     *   bgcolor=...
  356.     *
  357.     * @param  string
  358.     * @return string
  359.     * @access public
  360.     */
  361.     function start_obox($attributes) {
  362.         $this->_oboxAttributes = $attributes;
  363.  
  364.         return '';
  365.     }
  366.  
  367.     // }}}
  368.     // {{{ function end_obox($cdata)
  369.  
  370.     /**
  371.     * @param  string
  372.     * @return string
  373.     * @access public
  374.     */
  375.     function end_obox($cdata) {
  376.         return $this->_box($cdata);
  377.     }
  378.  
  379.     // }}}
  380.     // {{{ function start_oboxtitle($attributes)
  381.  
  382.     /**
  383.     * <oboxtitle /> -- Alternate method to set the obox title
  384.     *
  385.     * align=...
  386.     * valign=...
  387.     *
  388.     * @param  string
  389.     * @return string
  390.     * @access public
  391.     */
  392.     function start_oboxtitle($attributes) {
  393.         if (isset($attributes['align'])) {
  394.             $this->_oboxAttributes['titlealign'] = $attributes['align'];
  395.         }
  396.  
  397.         if (isset($attributes['valign'])) {
  398.             $this->_oboxAttributes['titlevalign'] = $attributes['valign'];
  399.         }
  400.  
  401.         if (isset($attributes['bgcolor'])) {
  402.             $this->_oboxAttributes['titlecolor'] = $attributes['bgcolor'];
  403.         }
  404.  
  405.         return '';
  406.     }
  407.  
  408.     // }}}
  409.     // {{{ function end_oboxtitle($cdata)
  410.  
  411.     /**
  412.     * @param  string
  413.     * @return string
  414.     * @access public
  415.     */
  416.     function end_oboxtitle($cdata) {
  417.         $this->_oboxAttributes['title'] = $cdata;
  418.  
  419.         return '';
  420.     }
  421.  
  422.     // }}}
  423. }
  424.  
  425. /*
  426.  * vim600:  et sw=2 ts=2 fdm=marker
  427.  * vim<600: et sw=2 ts=2
  428.  */
  429. ?>
  430.