liberty
[ class tree: liberty ] [ index: liberty ] [ all elements ]

Source for file data.ledgertable.php

Documentation is available at data.ledgertable.php

  1. <?php
  2. // $Id$
  3. /**
  4.  * assigned_modules
  5.  *
  6.  * @author   KainX <mej@kainx.org>
  7.  * @version  $Revision$
  8.  * @package  liberty
  9.  * @subpackage plugins_data
  10.  * @copyright Copyright (c) 2004, bitweaver.org
  11.  *  All Rights Reserved. See below for details and a complete list of authors.
  12.  * @license Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See http://www.gnu.org/copyleft/lesser.html for details.
  13.  */
  14. /******************
  15.  * Initialization *
  16.  ******************/
  17. define( 'PLUGIN_GUID_DATALEDGERTABLE', 'dataledgertable' );
  18. global $gLibertySystem;
  19. $pluginParams = array (
  20.     'tag' => 'LEDGERTABLE',
  21.     'auto_activate' => FALSE,
  22.     'requires_pair' => TRUE,
  23.     'load_function' => 'data_ledgertable',
  24.     'title' => 'Ledger Table',
  25.     'help_page' => 'DataPluginLedgertable',
  26.     'description' => tra("This Plugin creates a ledger-like table with even/odd row colors, optional top- or left-placed headers, and support for row/column spans."),
  27.     'help_function' => 'data_ledgertable_help',
  28.     'syntax' => "{LEDGERTABLE loc= head= }",
  29.     'plugin_type' => DATA_PLUGIN
  30. );
  31. $gLibertySystem->registerPlugin( PLUGIN_GUID_DATALEDGERTABLE, $pluginParams );
  32. $gLibertySystem->registerDataTag( $pluginParams['tag'], PLUGIN_GUID_DATALEDGERTABLE );
  33. /*****************
  34.  * Help Function *
  35.  *****************/
  36. function data_ledgertable_help() {
  37.     $help =
  38.         '<table class="data help">'
  39.             .'<tr>'
  40.                 .'<th>' . tra( "Key" ) . '</th>'
  41.                 .'<th>' . tra( "Type" ) . '</th>'
  42.                 .'<th>' . tra( "Comments" ) . '</th>'
  43.             .'</tr>'
  44.             .'<tr class="odd">'
  45.                 .'<td>loc</td>'
  46.                 .'<td>' . tra( "string") . '<br />' . tra("(optional)") . '</td>'
  47.                 .'<td>' . tra( "Where to display row/column headers (\"left\" or \"top\", default <strong>top</strong>).")
  48.                 .'</td>'
  49.             .'</tr>'
  50.             .'<tr class="even">'
  51.                 .'<td>head</td>'
  52.                 .'<td>' . tra( "string") . '<br />' . tra("(optional)") . '</td>'
  53.                 .'<td>' . tra( "Header(s) separated by \"~|~\", default <strong>none</strong>")
  54.                 .'</td>'
  55.             .'</tr>'
  56.         .'</table>'
  57.         . tra("LedgerTable: ") . "{LEDGERTABLE loc=>left head=>Row1~|~Row2~|~Row3}<br />"
  58.         . tra("This will display")
  59.         . data_ledgertable('Example', array('loc' => 'left', 'head' => 'Row1~|~Row2~|~Row3'));
  60.     return $help;
  61. }
  62. /****************
  63. * Load Function *
  64.  ****************/
  65. function data_ledgertable($data, $params) {
  66.     global $gBitSystem;
  67.  
  68.     if (empty($data)) {
  69.         return "<!-- Error:  No data passed to LEDGERTABLE plugin. -->";
  70.     }
  71.     if (substr($data, 0, 1) == "\n") {
  72.         $data = substr($data, 1);
  73.     }
  74.  
  75.     $ret = '';
  76.     if (isset($params['loc'])) {
  77.         $ret .= "<!-- Header row set to $params[loc]. -->";
  78.         $plugdata_loc = $params['loc'];
  79.     } else {
  80.         $ret .= "<!-- Defaulting header row to top. -->";
  81.         $plugdata_loc = 'top';
  82.     }
  83.     if (isset($params['head'])) {
  84.         $ret .= "<!-- Got headers. -->";
  85.         $plugdata_head = $params['head'];
  86.     } else {
  87.         $ret .= "<!-- No headers specified. -->";
  88.     }
  89.     if (isset($params['width'])) {
  90.         $ret .= "<!-- Got width $params[width]. -->";
  91.         $plugdata_width = " style=\"width: " . $params['width'] . '"';
  92.     } else {
  93.         $plugdata_width = "";
  94.     }
  95.  
  96.     $ret .= "<table class=\"ledgertable\"$plugdata_width>";
  97.  
  98.     if (isset($plugdata_head)) {
  99.         $headers = explode('~|~', $plugdata_head);
  100.         if ($plugdata_loc == 'top') {
  101.             $ret .= "    <!-- Placing header row on top. -->";
  102.             $ret .= "    <tr class=\"ledgertable header row\">";
  103.             foreach ($headers as $hdr) {
  104.                 $ret .= "        <th class=\"header highlight\">$hdr</td>";
  105.             }
  106.             $ret .= "  </tr>";
  107.         }
  108.     }
  109.  
  110.     $lines = split("\n", $data);
  111.     $line_count = 0;
  112.     foreach ($lines as $line) {
  113.         $line = trim($line);
  114.         if (strlen($line) <= 0) {
  115.             continue;
  116.         }
  117.         $line_count++;
  118.  
  119.         $ret .= "    <!-- Displaying row $line_count. -->";
  120.         $ret .= "    <tr class=\"" . (($line_count % 2) ? ("odd") : ("even")) . "\">";
  121.         if (isset($plugdata_head) && ($plugdata_loc == "left")) {
  122.             $ret .= "        <!-- Placing header on left. -->";
  123.             $ret .= "        <th class=\"header highlight\"";
  124.             $header = array_shift($headers);
  125.             if (preg_match("/^~(row|col)span:(\d+)~(.*)$/", $header, $matches)) {
  126.                 $ret .= " $matches[1]span=\"$matches[2]\"";
  127.                 $header = $matches[3];
  128.             }
  129.             $ret .= ">$header</td>";
  130.         }
  131.         $cells = explode("~|~", $line);
  132.         foreach ($cells as $col) {
  133.             $ret .= "        <td class=\"" . (($line_count % 2) ? ("odd") : ("even")) . "\"";
  134.             $col = trim($col);
  135.             if (!strcmp($col, "~blank~")) {
  136.                 $col = "&nbsp;";
  137.             } else if (preg_match("/^~(row|col)span:(\d+)~(.*)$/", $col, $matches)) {
  138.                 $ret .= " $matches[1]span=\"$matches[2]\"";
  139.                 $col = $matches[3];
  140.             }
  141.             $ret .= ">$col</td>";
  142.         }
  143.         $ret .= "    </tr>";
  144.     }
  145.     $ret .= "</table>";
  146.  
  147.     return $ret;
  148. }
  149. ?>

Documentation generated on Wed, 29 Jul 2015 13:56:20 +0000 by phpDocumentor 1.5.0-lsces