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 ((int)$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: } elseif ($page === 1 && count($results) < $limit) {
206: $count = count($results);
207: } else {
208: $parameters = compact('conditions');
209: if ($recursive != $object->recursive) {
210: $parameters['recursive'] = $recursive;
211: }
212: $count = $object->find('count', array_merge($parameters, $extra));
213: }
214: $pageCount = (int)ceil($count / $limit);
215: $requestedPage = $page;
216: $page = max(min($page, $pageCount), 1);
217:
218: $paging = array(
219: 'page' => $page,
220: 'current' => count($results),
221: 'count' => $count,
222: 'prevPage' => ($page > 1),
223: 'nextPage' => ($count > ($page * $limit)),
224: 'pageCount' => $pageCount,
225: 'order' => $order,
226: 'limit' => $limit,
227: 'options' => Hash::diff($options, $defaults),
228: 'paramType' => $options['paramType']
229: );
230:
231: if (!isset($this->Controller->request['paging'])) {
232: $this->Controller->request['paging'] = array();
233: }
234: $this->Controller->request['paging'] = array_merge(
235: (array)$this->Controller->request['paging'],
236: array($object->alias => $paging)
237: );
238:
239: if ($requestedPage > $page) {
240: throw new NotFoundException();
241: }
242:
243: if (!in_array('Paginator', $this->Controller->helpers) &&
244: !array_key_exists('Paginator', $this->Controller->helpers)
245: ) {
246: $this->Controller->helpers[] = 'Paginator';
247: }
248: return $results;
249: }
250:
251: 252: 253: 254: 255: 256:
257: protected function _getObject($object) {
258: if (is_string($object)) {
259: $assoc = null;
260: if (strpos($object, '.') !== false) {
261: list($object, $assoc) = pluginSplit($object);
262: }
263: if ($assoc && isset($this->Controller->{$object}->{$assoc})) {
264: return $this->Controller->{$object}->{$assoc};
265: }
266: if ($assoc && isset($this->Controller->{$this->Controller->modelClass}->{$assoc})) {
267: return $this->Controller->{$this->Controller->modelClass}->{$assoc};
268: }
269: if (isset($this->Controller->{$object})) {
270: return $this->Controller->{$object};
271: }
272: if (isset($this->Controller->{$this->Controller->modelClass}->{$object})) {
273: return $this->Controller->{$this->Controller->modelClass}->{$object};
274: }
275: }
276: if (empty($object) || $object === null) {
277: if (isset($this->Controller->{$this->Controller->modelClass})) {
278: return $this->Controller->{$this->Controller->modelClass};
279: }
280:
281: $className = null;
282: $name = $this->Controller->uses[0];
283: if (strpos($this->Controller->uses[0], '.') !== false) {
284: list($name, $className) = explode('.', $this->Controller->uses[0]);
285: }
286: if ($className) {
287: return $this->Controller->{$className};
288: }
289:
290: return $this->Controller->{$name};
291: }
292: return $object;
293: }
294:
295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309:
310: public function mergeOptions($alias) {
311: $defaults = $this->getDefaults($alias);
312: switch ($defaults['paramType']) {
313: case 'named':
314: $request = $this->Controller->request->params['named'];
315: break;
316: case 'querystring':
317: $request = $this->Controller->request->query;
318: break;
319: }
320: $request = array_intersect_key($request, array_flip($this->whitelist));
321: return array_merge($defaults, $request);
322: }
323:
324: 325: 326: 327: 328: 329: 330:
331: public function getDefaults($alias) {
332: $defaults = $this->settings;
333: if (isset($this->settings[$alias])) {
334: $defaults = $this->settings[$alias];
335: }
336: if (isset($defaults['limit']) &&
337: (empty($defaults['maxLimit']) || $defaults['limit'] > $defaults['maxLimit'])
338: ) {
339: $defaults['maxLimit'] = $defaults['limit'];
340: }
341: return array_merge(
342: array('page' => 1, 'limit' => 20, 'maxLimit' => 100, 'paramType' => 'named'),
343: $defaults
344: );
345: }
346:
347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362:
363: public function validateSort(Model $object, array $options, array $whitelist = array()) {
364: if (empty($options['order']) && is_array($object->order)) {
365: $options['order'] = $object->order;
366: }
367:
368: if (isset($options['sort'])) {
369: $direction = null;
370: if (isset($options['direction'])) {
371: $direction = strtolower($options['direction']);
372: }
373: if (!in_array($direction, array('asc', 'desc'))) {
374: $direction = 'asc';
375: }
376: $options['order'] = array($options['sort'] => $direction);
377: }
378:
379: if (!empty($whitelist) && isset($options['order']) && is_array($options['order'])) {
380: $field = key($options['order']);
381: $inWhitelist = in_array($field, $whitelist, true);
382: if (!$inWhitelist) {
383: $options['order'] = null;
384: }
385: return $options;
386: }
387:
388: if (!empty($options['order']) && is_array($options['order'])) {
389: $order = array();
390: foreach ($options['order'] as $key => $value) {
391: $field = $key;
392: $alias = $object->alias;
393: if (strpos($key, '.') !== false) {
394: list($alias, $field) = explode('.', $key);
395: }
396: $correctAlias = ($object->alias === $alias);
397:
398: if ($correctAlias && $object->hasField($field)) {
399: $order[$object->alias . '.' . $field] = $value;
400: } elseif ($correctAlias && $object->hasField($key, true)) {
401: $order[$field] = $value;
402: } elseif (isset($object->{$alias}) && $object->{$alias}->hasField($field, true)) {
403: $order[$alias . '.' . $field] = $value;
404: }
405: }
406: $options['order'] = $order;
407: }
408:
409: return $options;
410: }
411:
412: 413: 414: 415: 416: 417:
418: public function checkLimit(array $options) {
419: $options['limit'] = (int)$options['limit'];
420: if (empty($options['limit']) || $options['limit'] < 1) {
421: $options['limit'] = 1;
422: }
423: $options['limit'] = min($options['limit'], $options['maxLimit']);
424: return $options;
425: }
426:
427: }
428: