1: <?php
2:
3: /**
4: * This file contains the frontend list class.
5: *
6: * @package Core
7: * @subpackage Backend
8: * @version SVN Revision $Rev:$
9: *
10: * @author Timo Hummel
11: * @copyright four for business AG <www.4fb.de>
12: * @license http://www.contenido.org/license/LIZENZ.txt
13: * @link http://www.4fb.de
14: * @link http://www.contenido.org
15: */
16:
17: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
18:
19: /**
20: * Class FrontendList for scrollable frontend lists.
21: */
22: class FrontendList {
23:
24: /**
25: * Wrap for a single item.
26: *
27: * @var string
28: */
29: var $itemwrap;
30:
31: /**
32: * Wrap for table start.
33: *
34: * @var string
35: */
36: var $startwrap;
37:
38: /**
39: * Wrap for table end.
40: *
41: * @var string
42: */
43: var $endwrap;
44:
45: /**
46: * Data container.
47: *
48: * @var array
49: */
50: var $data = Array();
51:
52: /**
53: * Number of records displayed per page.
54: *
55: * @var int
56: */
57: var $resultsPerPage;
58:
59: /**
60: * Start page.
61: *
62: * @var int
63: */
64: var $listStart;
65:
66: /**
67: * Creates a new FrontendList object.
68: *
69: * The placeholder for item wraps are the same as for sprintf.
70: * See the documentation for sprintf.
71: *
72: * Caution: Make sure that percentage signs are written as %%.
73: *
74: * @param string $startwrap
75: * Wrap for the list start
76: * @param string $endwrap
77: * Wrap for the list end
78: * @param string $itemwrap
79: * Wrap for a single item
80: */
81: function FrontendList($startwrap, $endwrap, $itemwrap) {
82: $this->resultsPerPage = 0;
83: $this->listStart = 1;
84:
85: $this->itemwrap = $itemwrap;
86: $this->startwrap = $startwrap;
87: $this->endwrap = $endwrap;
88: }
89:
90: /**
91: * Sets data.
92: *
93: * Note: This function eats as many parameters as you specify.
94: *
95: * Example:
96: * $obj->setData(0, "foo", "bar");
97: *
98: * Make sure that the amount of parameters stays the same for all
99: * setData calls in a single object.
100: *
101: * @param int $index
102: * Numeric index
103: * @param ... Additional parameters (data)
104: * @SuppressWarnings docBlocks
105: */
106: function setData($index) {
107: $numargs = func_num_args();
108:
109: for ($i = 1; $i < $numargs; $i++) {
110: $this->data[$index][$i] = func_get_arg($i);
111: }
112: }
113:
114: /**
115: * Sets the number of records per page.
116: *
117: * @param int $resultsPerPage
118: * Amount of records per page
119: */
120: function setResultsPerPage($resultsPerPage) {
121: $this->resultsPerPage = $resultsPerPage;
122: }
123:
124: /**
125: * Sets the starting page number.
126: *
127: * @param int $listStart
128: * Page number on which the list display starts
129: */
130: function setListStart($listStart) {
131: $this->listStart = $listStart;
132: }
133:
134: /**
135: * Returns the current page.
136: *
137: * @return int
138: * Current page number
139: */
140: function getCurrentPage() {
141: if ($this->resultsPerPage == 0) {
142: return 1;
143: }
144:
145: return $this->listStart;
146: }
147:
148: /**
149: * Returns the amount of pages.
150: *
151: * @return int
152: * Amount of pages
153: */
154: function getNumPages() {
155: return ceil(count($this->data) / $this->resultsPerPage);
156: }
157:
158: /**
159: * Sorts the list by a given field and a given order.
160: *
161: * @param string $field
162: * name of field to sort for
163: * @param int $order
164: * Sort order (see php's sort documentation)
165: * one of SORT_ASC, SORT_DESC, SORT_REGULAR, SORT_NUMERIC, SORT_STRING
166: */
167: function sort($field, $order) {
168: $this->data = cArray::csort($this->data, "$field", $order);
169: }
170:
171: /**
172: * Field converting facility.
173: * Needs to be overridden in the child class to work properbly.
174: *
175: * @param int $field
176: * Field index
177: * @param mixed $value
178: * Field value
179: * @return mixed
180: */
181: function convert($field, $value) {
182: return $value;
183: }
184:
185: /**
186: * Outputs or optionally returns
187: *
188: * @param bool $return
189: * if true, returns the list
190: * @return string
191: */
192: function output($return = false) {
193: $output = $this->startwrap;
194:
195: $currentpage = $this->getCurrentPage();
196:
197: $itemstart = (($currentpage - 1) * $this->resultsPerPage) + 1;
198:
199: if ($this->resultsPerPage == 0) {
200: $itemend = count($this->data) - ($itemstart - 1);
201: } else {
202: $itemend = $currentpage * $this->resultsPerPage;
203: }
204:
205: if ($itemend > count($this->data)) {
206: $itemend = count($this->data);
207: }
208:
209: for ($i = $itemstart; $i < $itemend + 1; $i++) {
210: if (is_array($this->data[$i - 1])) {
211: $items = "";
212: foreach ($this->data[$i - 1] as $key => $value) {
213: $items .= ", '" . addslashes($this->convert($key, $value)) . "'";
214: }
215:
216: $execute = '$output .= sprintf($this->itemwrap ' . $items . ');';
217: eval($execute);
218: }
219: }
220:
221: $output .= $this->endwrap;
222:
223: $output = stripslashes($output);
224:
225: if ($return == true) {
226: return $output;
227: } else {
228: echo $output;
229: }
230: }
231:
232: }
233: