summaryrefslogtreecommitdiff
path: root/plugins/data.ledgertable.php
blob: cb3ef06f8146de7740f002f892c665c27767583a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php

namespace Bitweaver\Liberty;

use Bitweaver\KernelTools;

// $Id$
/**
 * assigned_modules
 *
 * @author   KainX <mej@kainx.org>
 * @version  $Revision$
 * @package  liberty
 * @subpackage plugins_data
 * @copyright Copyright (c) 2004, bitweaver.org
 * All Rights Reserved. See below for details and a complete list of authors.
 * @license Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See http://www.gnu.org/copyleft/lesser.html for details.
 */
/******************
 * Initialization *
 ******************/
define( 'PLUGIN_GUID_DATALEDGERTABLE', 'dataledgertable' );
global $gLibertySystem;
$pluginParams = [
	'tag' => 'LEDGERTABLE',
	'auto_activate' => false,
	'requires_pair' => true,
	'load_function' => '\data_ledgertable',
	'title' => 'Ledger Table',
	'help_page' => 'DataPluginLedgertable',
	'description' => KernelTools::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."),
	'help_function' => '\data_ledgertable_help',
	'syntax' => "{LEDGERTABLE loc= head= }",
	'plugin_type' => DATA_PLUGIN,
];
$gLibertySystem->registerPlugin( PLUGIN_GUID_DATALEDGERTABLE, $pluginParams );
$gLibertySystem->registerDataTag( $pluginParams['tag'], PLUGIN_GUID_DATALEDGERTABLE );
/*****************
 * Help Function *
 *****************/
function data_ledgertable_help() {
	$help =
		'<table class="data help">'
			.'<tr>'
				.'<th>' . KernelTools::tra( "Key" ) . '</th>'
				.'<th>' . KernelTools::tra( "Type" ) . '</th>'
				.'<th>' . KernelTools::tra( "Comments" ) . '</th>'
			.'</tr>'
			.'<tr class="odd">'
				.'<td>loc</td>'
				.'<td>' . KernelTools::tra( "string") . '<br />' . KernelTools::tra("(optional)") . '</td>'
				.'<td>' . KernelTools::tra( "Where to display row/column headers (\"left\" or \"top\", default <strong>top</strong>).")
				.'</td>'
			.'</tr>'
			.'<tr class="even">'
				.'<td>head</td>'
				.'<td>' . KernelTools::tra( "string") . '<br />' . KernelTools::tra("(optional)") . '</td>'
				.'<td>' . KernelTools::tra( "Header(s) separated by \"~|~\", default <strong>none</strong>")
				.'</td>'
			.'</tr>'
		.'</table>'
		. KernelTools::tra("LedgerTable: ") . "{LEDGERTABLE loc=>left head=>Row1~|~Row2~|~Row3}<br />"
		. KernelTools::tra("This will display")
		. data_ledgertable('Example', ['loc' => 'left', 'head' => 'Row1~|~Row2~|~Row3']);
	return $help;
}
/****************
* Load Function *
 ****************/
function data_ledgertable($data, $params) {
	global $gBitSystem;

	if (empty($data)) {
		return "<!-- Error:  No data passed to LEDGERTABLE plugin. -->";
	}
	if (substr($data, 0, 1) == "\n") {
		$data = substr($data, 1);
	}

	$ret = '';
	if (isset($params['loc'])) {
		$ret .= "<!-- Header row set to $params[loc]. -->";
		$plugdata_loc = $params['loc'];
	} else {
		$ret .= "<!-- Defaulting header row to top. -->";
		$plugdata_loc = 'top';
	}
	if (isset($params['head'])) {
		$ret .= "<!-- Got headers. -->";
		$plugdata_head = $params['head'];
	} else {
		$ret .= "<!-- No headers specified. -->";
	}
	if (isset($params['width'])) {
		$ret .= "<!-- Got width $params[width]. -->";
		$plugdata_width = " style=\"width: " . $params['width'] . '"';
	} else {
		$plugdata_width = "";
	}

	$ret .= "<table class=\"ledgertable\"$plugdata_width>";

	if (isset($plugdata_head)) {
		$headers = explode('~|~', $plugdata_head);
		if ($plugdata_loc == 'top') {
			$ret .= "    <!-- Placing header row on top. -->";
			$ret .= "    <tr class=\"ledgertable header row\">";
			foreach ($headers as $hdr) {
				$ret .= "        <th class=\"header highlight\">$hdr</td>";
			}
			$ret .= "  </tr>";
		}
	}

	$lines = mb_split("\n", $data);
	$line_count = 0;
	foreach ($lines as $line) {
		$line = trim($line);
		if (strlen($line) <= 0) {
			continue;
		}
		$line_count++;

		$ret .= "    <!-- Displaying row $line_count. -->";
		$ret .= "    <tr class=\"" . (($line_count % 2) ? "odd" : "even") . "\">";
		if (isset($plugdata_head) && ($plugdata_loc == "left")) {
			$ret .= "        <!-- Placing header on left. -->";
			$ret .= "        <th class=\"header highlight\"";
			$header = array_shift($headers);
			if (preg_match("/^~(row|col)span:(\d+)~(.*)$/", $header, $matches)) {
				$ret .= " $matches[1]span=\"$matches[2]\"";
				$header = $matches[3];
			}
			$ret .= ">$header</td>";
		}
		$cells = explode("~|~", $line);
		foreach ($cells as $col) {
			$ret .= "        <td class=\"" . (($line_count % 2) ? "odd" : "even") . "\"";
			$col = trim($col);
			if (!strcmp($col, "~blank~")) {
				$col = "&nbsp;";
			} else if (preg_match("/^~(row|col)span:(\d+)~(.*)$/", $col, $matches)) {
				$ret .= " $matches[1]span=\"$matches[2]\"";
				$col = $matches[3];
			}
			$ret .= ">$col</td>";
		}
		$ret .= "    </tr>";
	}
	$ret .= "</table>";

	return $ret;
}