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