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: * Constructor to create an instance of this class.
105: *
106: * @param bool $defaultstyle [optional]
107: * use the default style for object initializing?
108: * @param string $action [optional]
109: */
110: public function __construct($defaultstyle = true, $action = "") {
111: global $cfg, $area, $frame;
112:
113: $this->resultsPerPage = 0;
114: $this->listStart = 1;
115: $this->sortable = false;
116:
117: $this->objTable = new cHTMLTable();
118: if ($defaultstyle == true) {
119: $this->objTable->setClass("generic");
120: $this->objTable->updateAttributes(array(
121: "cellpadding" => "2"
122: ));
123: }
124:
125: $this->objHeaderRow = new cHTMLTableRow();
126:
127: $this->objHeaderItem = new cHTMLTableHead();
128:
129: $this->objRow = new cHTMLTableRow();
130:
131: $this->objItem = new cHTMLTableData();
132:
133: $this->sortlink = new cHTMLLink();
134: $this->sortlink->setStyle("color: #666666;");
135: $this->sortlink->setCLink($area, $frame, $action);
136: }
137:
138: /**
139: * Sets the sortable flag for a specific row.
140: *
141: * $obj->setSortable(true);
142: *
143: * @param int $key
144: * @param bool $sortable
145: * true or false
146: */
147: public function setSortable($key, $sortable) {
148: $this->sortable[$key] = $sortable;
149: }
150:
151: /**
152: * Sets the custom parameters for sortable links.
153: *
154: * $obj->setCustom($key, $custom);
155: *
156: * @param string $key
157: * Custom entry key
158: * @param string $custom
159: * Custom entry value
160: */
161: public function setCustom($key, $custom) {
162: $this->sortlink->setCustom($key, $custom);
163: }
164:
165: /**
166: * Is called when a new row is rendered.
167: *
168: * @param unknown_type $row
169: * The current row which is being rendered
170: */
171: public function onRenderRow($row) {
172: $this->objRow->setStyle("white-space:nowrap;");
173: }
174:
175: /**
176: * Is called when a new column is rendered.
177: *
178: * @param unknown_type $column
179: * The current column which is being rendered
180: */
181: public function onRenderColumn($column) {
182: }
183:
184: /**
185: * Sets header data.
186: *
187: * Note: This public function eats as many parameters as you specify.
188: *
189: * Example:
190: * $obj->setHeader("foo", "bar");
191: *
192: * Make sure that the amount of parameters stays the same for all
193: * setData calls in a single object.
194: *
195: * @SuppressWarnings docBlocks
196: * @param Additional parameters (data)
197: */
198: public function setHeader() {
199: $numargs = func_num_args();
200:
201: for ($i = 0; $i < $numargs; $i++) {
202: $this->header[$i] = func_get_arg($i);
203: }
204: }
205:
206: /**
207: * Sets data.
208: *
209: * Note: This public function eats as many parameters as you specify.
210: *
211: * Example:
212: * $obj->setData(0, "foo", "bar");
213: *
214: * Make sure that the amount of parameters stays the same for all
215: * setData calls in a single object. Also make sure that your index
216: * starts from 0 and ends with the actual number - 1.
217: *
218: * @param int $index
219: * Numeric index
220: * @SuppressWarnings docBlocks
221: * @param Additional parameters (data)
222: */
223: public function setData($index) {
224: $numargs = func_num_args();
225:
226: for ($i = 1; $i < $numargs; $i++) {
227: $this->data[$index][$i] = func_get_arg($i);
228: }
229: }
230:
231: /**
232: * Sets hidden data.
233: *
234: * Note: This public function eats as many parameters as you specify.
235: *
236: * Example:
237: * $obj->setHiddenData(0, "foo", "bar");
238: *
239: * Make sure that the amount of parameters stays the same for all
240: * setData calls in a single object. Also make sure that your index
241: * starts from 0 and ends with the actual number - 1.
242: *
243: * @param int $index
244: * Numeric index
245: * @SuppressWarnings docBlocks
246: * @param Additional parameters (data)
247: */
248: public function setHiddenData($index) {
249: $numargs = func_num_args();
250:
251: for ($i = 1; $i < $numargs; $i++) {
252: $this->data[$index]["hiddendata"][$i] = func_get_arg($i);
253: }
254: }
255:
256: /**
257: * Sets the number of records per page.
258: *
259: * @param int $numresults
260: * Amount of records per page
261: */
262: public function setResultsPerPage($numresults) {
263: $this->resultsPerPage = $numresults;
264: }
265:
266: /**
267: * Sets the starting page number.
268: *
269: * @param int $startpage
270: * Page number on which the list display starts
271: */
272: public function setListStart($startpage) {
273: $this->listStart = $startpage;
274: }
275:
276: /**
277: * Returns the current page.
278: *
279: * @return int
280: * Current page number
281: */
282: public function getCurrentPage() {
283: if ($this->resultsPerPage == 0) {
284: return 1;
285: }
286:
287: return $this->listStart;
288: }
289:
290: /**
291: * Returns the amount of pages.
292: *
293: * @return float
294: * Amount of pages
295: */
296: public function getNumPages() {
297: return ceil(count($this->data) / $this->resultsPerPage);
298: }
299:
300: /**
301: * Sorts the list by a given field and a given order.
302: *
303: * @param int $field
304: * Field index
305: * @param string|int $order
306: * Sort order (see php's sort documentation)
307: */
308: public function sort($field, $order) {
309: if ($order == "") {
310: $order = SORT_ASC;
311: }
312:
313: if ($order == "ASC") {
314: $order = SORT_ASC;
315: }
316:
317: if ($order == "DESC") {
318: $order = SORT_DESC;
319: }
320:
321: $this->sortkey = $field;
322: $this->sortmode = $order;
323:
324: $field = $field + 1;
325: $this->data = cArray::csort($this->data, "$field", $order);
326: }
327:
328: /**
329: * Field converting facility.
330: * Needs to be overridden in the child class to work properbly.
331: *
332: * @param unknown_type $field
333: * Field index
334: * @param unknown_type $value
335: * Field value
336: * @param unknown_type $hiddendata
337: * @return unknown
338: */
339: public function convert($field, $value, $hiddendata) {
340: return $value;
341: }
342:
343: /**
344: * Outputs or optionally returns.
345: *
346: * @param bool $return [optional]
347: * If true, returns the list
348: * @return string|void
349: */
350: public function render($return = true) {
351: global $cfg;
352:
353: $currentpage = $this->getCurrentPage();
354:
355: $itemstart = (($currentpage - 1) * $this->resultsPerPage) + 1;
356:
357: $headeroutput = "";
358: $output = "";
359:
360: // Render header
361: foreach ($this->header as $key => $value) {
362: if (is_array($this->sortable)) {
363: if (array_key_exists($key, $this->sortable) && $this->sortable[$key] == true) {
364: $this->sortlink->setContent($value);
365: $this->sortlink->setCustom("sortby", $key);
366:
367: if ($this->sortkey == $key && $this->sortmode == SORT_ASC) {
368: $this->sortlink->setCustom("sortmode", "DESC");
369: } else {
370: $this->sortlink->setCustom("sortmode", "ASC");
371: }
372:
373: $this->objHeaderItem->setContent($this->sortlink->render());
374: $headeroutput .= $this->objHeaderItem->render();
375: } else {
376: $this->objHeaderItem->setContent($value);
377: $headeroutput .= $this->objHeaderItem->render();
378: }
379: } else {
380: $this->objHeaderItem->setContent($value);
381: $headeroutput .= $this->objHeaderItem->render();
382: }
383: $this->objHeaderItem->advanceID();
384: }
385:
386: $this->objHeaderRow->setContent($headeroutput);
387:
388: $headeroutput = $this->objHeaderRow->render();
389:
390: if ($this->resultsPerPage == 0) {
391: $itemend = count($this->data) - ($itemstart - 1);
392: } else {
393: $itemend = $currentpage * $this->resultsPerPage;
394: }
395:
396: if ($itemend > count($this->data)) {
397: $itemend = count($this->data);
398: }
399:
400: for ($i = $itemstart; $i < $itemend + 1; $i++) {
401:
402: // At the last entry we get NULL as result
403: // This produce an error, therefore use continue
404: if ($this->data[$i - 1] == NULL) {
405: continue;
406: }
407:
408: $items = "";
409:
410: $this->onRenderRow($i);
411:
412: foreach ($this->data[$i - 1] as $key => $value) {
413: $this->onRenderColumn($key);
414:
415: if ($key != "hiddendata") {
416: $hiddendata = $this->data[$i - 1]["hiddendata"];
417:
418: $this->objItem->setContent($this->convert($key, $value, $hiddendata));
419: $items .= $this->objItem->render();
420: }
421: $this->objItem->advanceID();
422: }
423:
424: $this->objRow->setContent($items);
425: $items = "";
426:
427: $output .= $this->objRow->render();
428: $this->objRow->advanceID();
429: }
430:
431: $this->objTable->setContent($headeroutput . $output);
432:
433: $output = stripslashes($this->objTable->render());
434:
435: if ($return == true) {
436: return $output;
437: } else {
438: echo $output;
439: }
440: }
441:
442: }
443: