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: * @param $startwrap
88: * @param $endwrap
89: * @param $itemwrap
90: * @deprecated [2016-04-06] This method is deprecated and is not needed any longer. Please use __construct() as constructor function.
91: */
92: public function FrontendList($startwrap, $endwrap, $itemwrap) {
93: cDeprecated('This method is deprecated and is not needed any longer. Please use __construct() as constructor function.');
94: return $this->__construct($startwrap, $endwrap, $itemwrap);
95: }
96:
97: /**
98: * Sets data.
99: *
100: * Note: This function eats as many parameters as you specify.
101: *
102: * Example:
103: * $obj->setData(0, "foo", "bar");
104: *
105: * Make sure that the amount of parameters stays the same for all
106: * setData calls in a single object.
107: *
108: * @param int $index
109: * Numeric index
110: * @param ... Additional parameters (data)
111: * @SuppressWarnings docBlocks
112: */
113: public function setData($index) {
114: $numargs = func_num_args();
115:
116: for ($i = 1; $i < $numargs; $i++) {
117: $this->_data[$index][$i] = func_get_arg($i);
118: }
119: }
120:
121: /**
122: * Sets the number of records per page.
123: *
124: * @param int $resultsPerPage
125: * Amount of records per page
126: */
127: public function setResultsPerPage($resultsPerPage) {
128: $this->_resultsPerPage = $resultsPerPage;
129: }
130:
131: /**
132: * Sets the starting page number.
133: *
134: * @param int $listStart
135: * Page number on which the list display starts
136: */
137: public function setListStart($listStart) {
138: $this->_listStart = $listStart;
139: }
140:
141: /**
142: * Returns the current page.
143: *
144: * @return int
145: * Current page number
146: */
147: public function getCurrentPage() {
148: if ($this->_resultsPerPage == 0) {
149: return 1;
150: }
151:
152: return $this->_listStart;
153: }
154:
155: /**
156: * Returns the amount of pages.
157: *
158: * @return int
159: * Amount of pages
160: */
161: public function getNumPages() {
162: return ceil(count($this->_data) / $this->_resultsPerPage);
163: }
164:
165: /**
166: * Sorts the list by a given field and a given order.
167: *
168: * @param string $field
169: * name of field to sort for
170: * @param int $order
171: * Sort order (see php's sort documentation)
172: * one of SORT_ASC, SORT_DESC, SORT_REGULAR, SORT_NUMERIC, SORT_STRING
173: */
174: public function sort($field, $order) {
175: $this->_data = cArray::csort($this->_data, "$field", $order);
176: }
177:
178: /**
179: * Field converting facility.
180: * Needs to be overridden in the child class to work properbly.
181: *
182: * @param int $field
183: * Field index
184: * @param mixed $value
185: * Field value
186: * @return mixed
187: */
188: protected function convert($field, $value) {
189: return $value;
190: }
191:
192: /**
193: * Outputs or optionally returns
194: *
195: * @param bool $return
196: * if true, returns the list
197: * @return string
198: */
199: public function output($return = false) {
200: $output = $this->_startwrap;
201:
202: $currentpage = $this->getCurrentPage();
203:
204: $itemstart = (($currentpage - 1) * $this->_resultsPerPage) + 1;
205:
206: if ($this->_resultsPerPage == 0) {
207: $itemend = count($this->_data) - ($itemstart - 1);
208: } else {
209: $itemend = $currentpage * $this->_resultsPerPage;
210: }
211:
212: if ($itemend > count($this->_data)) {
213: $itemend = count($this->_data);
214: }
215:
216: for ($i = $itemstart; $i < $itemend + 1; $i++) {
217: if (is_array($this->_data[$i - 1])) {
218: $items = "";
219: foreach ($this->_data[$i - 1] as $key => $value) {
220: $items .= ", '" . addslashes($this->convert($key, $value)) . "'";
221: }
222:
223: $execute = '$output .= sprintf($this->_itemwrap ' . $items . ');';
224: eval($execute);
225: }
226: }
227:
228: $output .= $this->_endwrap;
229:
230: $output = stripslashes($output);
231:
232: if ($return == true) {
233: return $output;
234: } else {
235: echo $output;
236: }
237: }
238:
239: }
240: