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 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 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 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: * @param $none
135: * @return s Current page number
136: */
137: function getCurrentPage() {
138: if ($this->resultsPerPage == 0) {
139: return 1;
140: }
141:
142: return ($this->listStart);
143: }
144:
145: /**
146: * Returns the amount of pages.
147: *
148: * @param $none
149: * @return s Amount of pages
150: */
151: function getNumPages() {
152: return (ceil(count($this->data) / $this->resultsPerPage));
153: }
154:
155: /**
156: * Sorts the list by a given field and a given order.
157: *
158: * @param $field Field index
159: * @param $order Sort order (see php's sort documentation)
160: */
161: function sort($field, $order) {
162: $this->data = cArray::csort($this->data, "$field", $order);
163: }
164:
165: /**
166: * Field converting facility.
167: * Needs to be overridden in the child class to work properbly.
168: *
169: * @param $field Field index
170: * @param $value Field value
171: */
172: function convert($field, $value) {
173: return $value;
174: }
175:
176: /**
177: * Outputs or optionally returns
178: *
179: * @param $return If true, returns the list
180: */
181: function output($return = false) {
182: $output = $this->startwrap;
183:
184: $currentpage = $this->getCurrentPage();
185:
186: $itemstart = (($currentpage - 1) * $this->resultsPerPage) + 1;
187:
188: if ($this->resultsPerPage == 0) {
189: $itemend = count($this->data) - ($itemstart - 1);
190: } else {
191: $itemend = $currentpage * $this->resultsPerPage;
192: }
193:
194: if ($itemend > count($this->data)) {
195: $itemend = count($this->data);
196: }
197:
198: for ($i = $itemstart; $i < $itemend + 1; $i++) {
199: if (is_array($this->data[$i - 1])) {
200: $items = "";
201: foreach ($this->data[$i - 1] as $key => $value) {
202: $items .= ", '" . addslashes($this->convert($key, $value)) . "'";
203: }
204:
205: $execute = '$output .= sprintf($this->itemwrap ' . $items . ');';
206: eval($execute);
207: }
208: }
209:
210: $output .= $this->endwrap;
211:
212: $output = stripslashes($output);
213:
214: if ($return == true) {
215: return $output;
216: } else {
217: echo $output;
218: }
219: }
220:
221: }
222: