1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17:
18:
19: App::uses('Component', 'Controller');
20: App::uses('Hash', 'Utility');
21:
22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70:
71: class PaginatorComponent extends Component {
72:
73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85:
86: public $settings = array(
87: 'page' => 1,
88: 'limit' => 20,
89: 'maxLimit' => 100,
90: 'paramType' => 'named'
91: );
92:
93: 94: 95: 96: 97: 98: 99:
100: public $whitelist = array(
101: 'limit', 'sort', 'page', 'direction'
102: );
103:
104: 105: 106: 107: 108: 109:
110: public function __construct(ComponentCollection $collection, $settings = array()) {
111: $settings = array_merge($this->settings, (array)$settings);
112: $this->Controller = $collection->getController();
113: parent::__construct($collection, $settings);
114: }
115:
116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127:
128: public function paginate($object = null, $scope = array(), $whitelist = array()) {
129: if (is_array($object)) {
130: $whitelist = $scope;
131: $scope = $object;
132: $object = null;
133: }
134:
135: $object = $this->_getObject($object);
136:
137: if (!is_object($object)) {
138: throw new MissingModelException($object);
139: }
140:
141: $options = $this->mergeOptions($object->alias);
142: $options = $this->validateSort($object, $options, $whitelist);
143: $options = $this->checkLimit($options);
144:
145: $conditions = $fields = $order = $limit = $page = $recursive = null;
146:
147: if (!isset($options['conditions'])) {
148: $options['conditions'] = array();
149: }
150:
151: $type = 'all';
152:
153: if (isset($options[0])) {
154: $type = $options[0];
155: unset($options[0]);
156: }
157:
158: extract($options);
159:
160: if (is_array($scope) && !empty($scope)) {
161: $conditions = array_merge($conditions, $scope);
162: } elseif (is_string($scope)) {
163: $conditions = array($conditions, $scope);
164: }
165: if ($recursive === null) {
166: $recursive = $object->recursive;
167: }
168:
169: $extra = array_diff_key($options, compact(
170: 'conditions', 'fields', 'order', 'limit', 'page', 'recursive'
171: ));
172:
173: if (!empty($extra['findType'])) {
174: $type = $extra['findType'];
175: unset($extra['findType']);
176: }
177:
178: if ($type !== 'all') {
179: $extra['type'] = $type;
180: }
181:
182: if (intval($page) < 1) {
183: $page = 1;
184: }
185: $page = $options['page'] = (int)$page;
186:
187: if ($object->hasMethod('paginate')) {
188: $results = $object->paginate(
189: $conditions, $fields, $order, $limit, $page, $recursive, $extra
190: );
191: } else {
192: $parameters = compact('conditions', 'fields', 'order', 'limit', 'page');
193: if ($recursive != $object->recursive) {
194: $parameters['recursive'] = $recursive;
195: }
196: $results = $object->find($type, array_merge($parameters, $extra));
197: }
198: $defaults = $this->getDefaults($object->alias);
199: unset($defaults[0]);
200:
201: if (!$results) {
202: $count = 0;
203: } elseif ($object->hasMethod('paginateCount')) {
204: $count = $object->paginateCount($conditions, $recursive, $extra);
205: } else {
206: $parameters = compact('conditions');
207: if ($recursive != $object->recursive) {
208: $parameters['recursive'] = $recursive;
209: }
210: $count = $object->find('count', array_merge($parameters, $extra));
211: }
212: $pageCount = intval(ceil($count / $limit));
213: $requestedPage = $page;
214: $page = max(min($page, $pageCount), 1);
215:
216: $paging = array(
217: 'page' => $page,
218: 'current' => count($results),
219: 'count' => $count,
220: 'prevPage' => ($page > 1),
221: 'nextPage' => ($count > ($page * $limit)),
222: 'pageCount' => $pageCount,
223: 'order' => $order,
224: 'limit' => $limit,
225: 'options' => Hash::diff($options, $defaults),
226: 'paramType' => $options['paramType']
227: );
228:
229: if (!isset($this->Controller->request['paging'])) {
230: $this->Controller->request['paging'] = array();
231: }
232: $this->Controller->request['paging'] = array_merge(
233: (array)$this->Controller->request['paging'],
234: array($object->alias => $paging)
235: );
236:
237: if ($requestedPage > $page) {
238: throw new NotFoundException();
239: }
240:
241: if (
242: !in_array('Paginator', $this->Controller->helpers) &&
243: !array_key_exists('Paginator', $this->Controller->helpers)
244: ) {
245: $this->Controller->helpers[] = 'Paginator';
246: }
247: return $results;
248: }
249:
250: 251: 252: 253: 254: 255:
256: protected function _getObject($object) {
257: if (is_string($object)) {
258: $assoc = null;
259: if (strpos($object, '.') !== false) {
260: list($object, $assoc) = pluginSplit($object);
261: }
262: if ($assoc && isset($this->Controller->{$object}->{$assoc})) {
263: return $this->Controller->{$object}->{$assoc};
264: }
265: if ($assoc && isset($this->Controller->{$this->Controller->modelClass}->{$assoc})) {
266: return $this->Controller->{$this->Controller->modelClass}->{$assoc};
267: }
268: if (isset($this->Controller->{$object})) {
269: return $this->Controller->{$object};
270: }
271: if (isset($this->Controller->{$this->Controller->modelClass}->{$object})) {
272: return $this->Controller->{$this->Controller->modelClass}->{$object};
273: }
274: }
275: if (empty($object) || $object === null) {
276: if (isset($this->Controller->{$this->Controller->modelClass})) {
277: return $this->Controller->{$this->Controller->modelClass};
278: }
279:
280: $className = null;
281: $name = $this->Controller->uses[0];
282: if (strpos($this->Controller->uses[0], '.') !== false) {
283: list($name, $className) = explode('.', $this->Controller->uses[0]);
284: }
285: if ($className) {
286: return $this->Controller->{$className};
287: }
288:
289: return $this->Controller->{$name};
290: }
291: return $object;
292: }
293:
294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308:
309: public function mergeOptions($alias) {
310: $defaults = $this->getDefaults($alias);
311: switch ($defaults['paramType']) {
312: case 'named':
313: $request = $this->Controller->request->params['named'];
314: break;
315: case 'querystring':
316: $request = $this->Controller->request->query;
317: break;
318: }
319: $request = array_intersect_key($request, array_flip($this->whitelist));
320: return array_merge($defaults, $request);
321: }
322:
323: 324: 325: 326: 327: 328: 329:
330: public function getDefaults($alias) {
331: $defaults = $this->settings;
332: if (isset($this->settings[$alias])) {
333: $defaults = $this->settings[$alias];
334: }
335: if (isset($defaults['limit']) &&
336: (empty($defaults['maxLimit']) || $defaults['limit'] > $defaults['maxLimit'])
337: ) {
338: $defaults['maxLimit'] = $defaults['limit'];
339: }
340: return array_merge(
341: array('page' => 1, 'limit' => 20, 'maxLimit' => 100, 'paramType' => 'named'),
342: $defaults
343: );
344: }
345:
346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361:
362: public function validateSort(Model $object, array $options, array $whitelist = array()) {
363: if (empty($options['order']) && is_array($object->order)) {
364: $options['order'] = $object->order;
365: }
366:
367: if (isset($options['sort'])) {
368: $direction = null;
369: if (isset($options['direction'])) {
370: $direction = strtolower($options['direction']);
371: }
372: if (!in_array($direction, array('asc', 'desc'))) {
373: $direction = 'asc';
374: }
375: $options['order'] = array($options['sort'] => $direction);
376: }
377:
378: if (!empty($whitelist) && isset($options['order']) && is_array($options['order'])) {
379: $field = key($options['order']);
380: $inWhitelist = in_array($field, $whitelist, true);
381: if (!$inWhitelist) {
382: $options['order'] = null;
383: }
384: return $options;
385: }
386:
387: if (!empty($options['order']) && is_array($options['order'])) {
388: $order = array();
389: foreach ($options['order'] as $key => $value) {
390: $field = $key;
391: $alias = $object->alias;
392: if (strpos($key, '.') !== false) {
393: list($alias, $field) = explode('.', $key);
394: }
395: $correctAlias = ($object->alias === $alias);
396:
397: if ($correctAlias && $object->hasField($field)) {
398: $order[$object->alias . '.' . $field] = $value;
399: } elseif ($correctAlias && $object->hasField($key, true)) {
400: $order[$field] = $value;
401: } elseif (isset($object->{$alias}) && $object->{$alias}->hasField($field, true)) {
402: $order[$alias . '.' . $field] = $value;
403: }
404: }
405: $options['order'] = $order;
406: }
407:
408: return $options;
409: }
410:
411: 412: 413: 414: 415: 416:
417: public function checkLimit(array $options) {
418: $options['limit'] = (int)$options['limit'];
419: if (empty($options['limit']) || $options['limit'] < 1) {
420: $options['limit'] = 1;
421: }
422: $options['limit'] = min($options['limit'], $options['maxLimit']);
423: return $options;
424: }
425:
426: }
427: