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