1: <?php
2:
3: /**
4: * This file contains the scrollable lists GUI class.
5: *
6: * @package Core
7: * @subpackage GUI
8: *
9: * @author Mischa Holz
10: * @copyright four for business AG <www.4fb.de>
11: * @license http://www.contenido.org/license/LIZENZ.txt
12: * @link http://www.4fb.de
13: * @link http://www.contenido.org
14: */
15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: /**
19: * Scrollable lists GUI class.
20: *
21: * @package Core
22: * @subpackage GUI
23: */
24: class cGuiScrollList {
25:
26: /**
27: * Data container.
28: *
29: * @var array
30: */
31: public $data = array();
32:
33: /**
34: * Header container.
35: *
36: * @var array
37: */
38: public $header = array();
39:
40: /**
41: * Number of records displayed per page.
42: *
43: * @var string
44: */
45: public $resultsPerPage;
46:
47: /**
48: * Start page.
49: *
50: * @var string
51: */
52: public $listStart;
53:
54: /**
55: * sortable flag
56: *
57: * @var string
58: */
59: public $sortable;
60:
61: /**
62: * sortlink
63: *
64: * @var cHTMLLink
65: */
66: public $sortlink;
67:
68: /**
69: * Table item
70: *
71: * @var cHTMLTable
72: */
73: public $objTable;
74:
75: /**
76: * Header row
77: *
78: * @var cHTMLTableRow
79: */
80: public $objHeaderRow;
81:
82: /**
83: * Header item
84: *
85: * @var cHTMLTableHead
86: */
87: public $objHeaderItem;
88:
89: /**
90: * Header item
91: *
92: * @var cHTMLTableRow
93: */
94: public $objRow;
95:
96: /**
97: * Header item
98: *
99: * @var cHTMLTableData
100: */
101: public $objItem;
102:
103: /**
104: * @var string
105: */
106: public $sortkey;
107:
108: /**
109: * @var int - SORT_ASC or SORT_DESC
110: */
111: public $sortmode;
112:
113: /**
114: * Constructor to create an instance of this class.
115: *
116: * @param bool $defaultstyle [optional]
117: * use the default style for object initializing?
118: * @param string $action [optional]
119: */
120: public function __construct($defaultstyle = true, $action = "") {
121: global $area, $frame;
122:
123: $this->resultsPerPage = 0;
124: $this->listStart = 1;
125: $this->sortable = false;
126:
127: $this->objTable = new cHTMLTable();
128: if ($defaultstyle == true) {
129: $this->objTable->setClass("generic");
130: $this->objTable->updateAttributes(array(
131: "cellpadding" => "2"
132: ));
133: }
134:
135: $this->objHeaderRow = new cHTMLTableRow();
136:
137: $this->objHeaderItem = new cHTMLTableHead();
138:
139: $this->objRow = new cHTMLTableRow();
140:
141: $this->objItem = new cHTMLTableData();
142:
143: $this->sortlink = new cHTMLLink();
144: $this->sortlink->setStyle("color: #666666;");
145: $this->sortlink->setCLink($area, $frame, $action);
146: }
147:
148: /**
149: * Sets the sortable flag for a specific row.
150: *
151: * $obj->setSortable(true);
152: *
153: * @param int $key
154: * @param bool $sortable
155: * true or false
156: */
157: public function setSortable($key, $sortable) {
158: $this->sortable[$key] = $sortable;
159: }
160:
161: /**
162: * Sets the custom parameters for sortable links.
163: *
164: * $obj->setCustom($key, $custom);
165: *
166: * @param string $key
167: * Custom entry key
168: * @param string $custom
169: * Custom entry value
170: */
171: public function setCustom($key, $custom) {
172: $this->sortlink->setCustom($key, $custom);
173: }
174:
175: /**
176: * Is called when a new row is rendered.
177: *
178: * @param int $row
179: * The current row which is being rendered
180: */
181: public function onRenderRow($row) {
182: $this->objRow->setStyle("white-space:nowrap;");
183: }
184:
185: /**
186: * Is called when a new column is rendered.
187: *
188: * @param int $column
189: * The current column which is being rendered
190: */
191: public function onRenderColumn($column) {
192: }
193:
194: /**
195: * Sets header data.
196: *
197: * Note: This public function eats as many parameters as you specify.
198: *
199: * Example:
200: * $obj->setHeader("foo", "bar");
201: *
202: * Make sure that the amount of parameters stays the same for all
203: * setData calls in a single object.
204: *
205: * @SuppressWarnings docBlocks
206: * @param mixed ... Additional parameters (data)
207: */
208: public function setHeader() {
209: $numargs = func_num_args();
210:
211: for ($i = 0; $i < $numargs; $i++) {
212: $this->header[$i] = func_get_arg($i);
213: }
214: }
215:
216: /**
217: * Sets data.
218: *
219: * Note: This public function eats as many parameters as you specify.
220: *
221: * Example:
222: * $obj->setData(0, "foo", "bar");
223: *
224: * Make sure that the amount of parameters stays the same for all
225: * setData calls in a single object. Also make sure that your index
226: * starts from 0 and ends with the actual number - 1.
227: *
228: * @param int $index
229: * Numeric index
230: * @SuppressWarnings docBlocks
231: * @param mixed ... - Additional parameters (data)
232: */
233: public function setData($index) {
234: $numargs = func_num_args();
235:
236: for ($i = 1; $i < $numargs; $i++) {
237: $this->data[$index][$i] = func_get_arg($i);
238: }
239: }
240:
241: /**
242: * Sets hidden data.
243: *
244: * Note: This public function eats as many parameters as you specify.
245: *
246: * Example:
247: * $obj->setHiddenData(0, "foo", "bar");
248: *
249: * Make sure that the amount of parameters stays the same for all
250: * setData calls in a single object. Also make sure that your index
251: * starts from 0 and ends with the actual number - 1.
252: *
253: * @param int $index
254: * Numeric index
255: * @SuppressWarnings docBlocks
256: * @param mixed ... - Additional parameters (data)
257: */
258: public function setHiddenData($index) {
259: $numargs = func_num_args();
260:
261: for ($i = 1; $i < $numargs; $i++) {
262: $this->data[$index]["hiddendata"][$i] = func_get_arg($i);
263: }
264: }
265:
266: /**
267: * Sets the number of records per page.
268: *
269: * @param int $numresults
270: * Amount of records per page
271: */
272: public function setResultsPerPage($numresults) {
273: $this->resultsPerPage = $numresults;
274: }
275:
276: /**
277: * Sets the starting page number.
278: *
279: * @param int $startpage
280: * Page number on which the list display starts
281: */
282: public function setListStart($startpage) {
283: $this->listStart = $startpage;
284: }
285:
286: /**
287: * Returns the current page.
288: *
289: * @return int
290: * Current page number
291: */
292: public function getCurrentPage() {
293: if ($this->resultsPerPage == 0) {
294: return 1;
295: }
296:
297: return $this->listStart;
298: }
299:
300: /**
301: * Returns the amount of pages.
302: *
303: * @return float
304: * Amount of pages
305: */
306: public function getNumPages() {
307: return ceil(count($this->data) / $this->resultsPerPage);
308: }
309:
310: /**
311: * Sorts the list by a given field and a given order.
312: *
313: * @param int $field
314: * Field index
315: * @param string|int $order
316: * Sort order (see php's sort documentation)
317: */
318: public function sort($field, $order) {
319: if ($order == "") {
320: $order = SORT_ASC;
321: }
322:
323: if ($order == "ASC") {
324: $order = SORT_ASC;
325: }
326:
327: if ($order == "DESC") {
328: $order = SORT_DESC;
329: }
330:
331: $this->sortkey = $field;
332: $this->sortmode = $order;
333:
334: $field = $field + 1;
335: $this->data = cArray::csort($this->data, "$field", $order);
336: }
337:
338: /**
339: * Field converting facility.
340: * Needs to be overridden in the child class to work properbly.
341: *
342: * @param int $field
343: * Field index
344: * @param string $value
345: * Field value
346: * @param array $hiddendata
347: * @return string
348: */
349: public function convert($field, $value, $hiddendata) {
350: return $value;
351: }
352:
353: /**
354: * Outputs or optionally returns.
355: *
356: * @param bool $return [optional]
357: * If true, returns the list
358: * @return string|void
359: */
360: public function render($return = true) {
361:
362: $currentpage = $this->getCurrentPage();
363:
364: $itemstart = (($currentpage - 1) * $this->resultsPerPage) + 1;
365:
366: $headeroutput = "";
367: $output = "";
368:
369: // Render header
370: foreach ($this->header as $key => $value) {
371: if (is_array($this->sortable)) {
372: if (array_key_exists($key, $this->sortable) && $this->sortable[$key] == true) {
373: $this->sortlink->setContent($value);
374: $this->sortlink->setCustom("sortby", $key);
375:
376: if ($this->sortkey == $key && $this->sortmode == SORT_ASC) {
377: $this->sortlink->setCustom("sortmode", "DESC");
378: } else {
379: $this->sortlink->setCustom("sortmode", "ASC");
380: }
381:
382: $this->objHeaderItem->setContent($this->sortlink->render());
383: $headeroutput .= $this->objHeaderItem->render();
384: } else {
385: $this->objHeaderItem->setContent($value);
386: $headeroutput .= $this->objHeaderItem->render();
387: }
388: } else {
389: $this->objHeaderItem->setContent($value);
390: $headeroutput .= $this->objHeaderItem->render();
391: }
392: $this->objHeaderItem->advanceID();
393: }
394:
395: $this->objHeaderRow->setContent($headeroutput);
396:
397: $headeroutput = $this->objHeaderRow->render();
398:
399: if ($this->resultsPerPage == 0) {
400: $itemend = count($this->data) - ($itemstart - 1);
401: } else {
402: $itemend = $currentpage * $this->resultsPerPage;
403: }
404:
405: if ($itemend > count($this->data)) {
406: $itemend = count($this->data);
407: }
408:
409: for ($i = $itemstart; $i < $itemend + 1; $i++) {
410:
411: // At the last entry we get NULL as result
412: // This produce an error, therefore use continue
413: if ($this->data[$i - 1] == NULL) {
414: continue;
415: }
416:
417: $items = "";
418:
419: $this->onRenderRow($i);
420:
421: foreach ($this->data[$i - 1] as $key => $value) {
422: $this->onRenderColumn($key);
423:
424: if ($key != "hiddendata") {
425: $hiddendata = !empty($this->data[$i - 1]["hiddendata"]) && is_array($this->data[$i - 1]["hiddendata"]) ? $this->data[$i - 1]["hiddendata"] : [];
426:
427: $this->objItem->setContent($this->convert($key, $value, $hiddendata));
428: $items .= $this->objItem->render();
429: }
430: $this->objItem->advanceID();
431: }
432:
433: $this->objRow->setContent($items);
434:
435: $output .= $this->objRow->render();
436: $this->objRow->advanceID();
437: }
438:
439: $this->objTable->setContent($headeroutput . $output);
440:
441: $output = $this->objTable->render();
442:
443: if ($return == true) {
444: return $output;
445: } else {
446: echo $output;
447: }
448: }
449:
450: }
451: