1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19:
20:
21: App::uses('Component', 'Controller');
22: App::uses('Hash', 'Utility');
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: 72:
73: class PaginatorComponent extends Component {
74:
75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87:
88: public $settings = array(
89: 'page' => 1,
90: 'limit' => 20,
91: 'maxLimit' => 100,
92: 'paramType' => 'named'
93: );
94:
95: 96: 97: 98: 99: 100: 101:
102: public $whitelist = array(
103: 'limit', 'sort', 'page', 'direction'
104: );
105:
106: 107: 108: 109: 110: 111:
112: public function __construct(ComponentCollection $collection, $settings = array()) {
113: $settings = array_merge($this->settings, (array)$settings);
114: $this->Controller = $collection->getController();
115: parent::__construct($collection, $settings);
116: }
117:
118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129:
130: public function paginate($object = null, $scope = array(), $whitelist = array()) {
131: if (is_array($object)) {
132: $whitelist = $scope;
133: $scope = $object;
134: $object = null;
135: }
136:
137: $object = $this->_getObject($object);
138:
139: if (!is_object($object)) {
140: throw new MissingModelException($object);
141: }
142:
143: $options = $this->mergeOptions($object->alias);
144: $options = $this->validateSort($object, $options, $whitelist);
145: $options = $this->checkLimit($options);
146:
147: $conditions = $fields = $order = $limit = $page = $recursive = null;
148:
149: if (!isset($options['conditions'])) {
150: $options['conditions'] = array();
151: }
152:
153: $type = 'all';
154:
155: if (isset($options[0])) {
156: $type = $options[0];
157: unset($options[0]);
158: }
159:
160: extract($options);
161:
162: if (is_array($scope) && !empty($scope)) {
163: $conditions = array_merge($conditions, $scope);
164: } elseif (is_string($scope)) {
165: $conditions = array($conditions, $scope);
166: }
167: if ($recursive === null) {
168: $recursive = $object->recursive;
169: }
170:
171: $extra = array_diff_key($options, compact(
172: 'conditions', 'fields', 'order', 'limit', 'page', 'recursive'
173: ));
174:
175: if (!empty($extra['findType'])) {
176: $type = $extra['findType'];
177: unset($extra['findType']);
178: }
179:
180: if ($type !== 'all') {
181: $extra['type'] = $type;
182: }
183:
184: if (intval($page) < 1) {
185: $page = 1;
186: }
187: $page = $options['page'] = (int)$page;
188:
189: if ($object->hasMethod('paginate')) {
190: $results = $object->paginate(
191: $conditions, $fields, $order, $limit, $page, $recursive, $extra
192: );
193: } else {
194: $parameters = compact('conditions', 'fields', 'order', 'limit', 'page');
195: if ($recursive != $object->recursive) {
196: $parameters['recursive'] = $recursive;
197: }
198: $results = $object->find($type, array_merge($parameters, $extra));
199: }
200: $defaults = $this->getDefaults($object->alias);
201: unset($defaults[0]);
202:
203: if (!$results) {
204: $count = 0;
205: } elseif ($object->hasMethod('paginateCount')) {
206: $count = $object->paginateCount($conditions, $recursive, $extra);
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 = intval(ceil($count / $limit));
215: $requestedPage = $page;
216: $page = max(min($page, $pageCount), 1);
217: if ($requestedPage > $page) {
218: throw new NotFoundException();
219: }
220:
221: $paging = array(
222: 'page' => $page,
223: 'current' => count($results),
224: 'count' => $count,
225: 'prevPage' => ($page > 1),
226: 'nextPage' => ($count > ($page * $limit)),
227: 'pageCount' => $pageCount,
228: 'order' => $order,
229: 'limit' => $limit,
230: 'options' => Hash::diff($options, $defaults),
231: 'paramType' => $options['paramType']
232: );
233:
234: if (!isset($this->Controller->request['paging'])) {
235: $this->Controller->request['paging'] = array();
236: }
237: $this->Controller->request['paging'] = array_merge(
238: (array)$this->Controller->request['paging'],
239: array($object->alias => $paging)
240: );
241:
242: if (
243: !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 (isset($options['sort'])) {
365: $direction = null;
366: if (isset($options['direction'])) {
367: $direction = strtolower($options['direction']);
368: }
369: if (!in_array($direction, array('asc', 'desc'))) {
370: $direction = 'asc';
371: }
372: $options['order'] = array($options['sort'] => $direction);
373: }
374:
375: if (!empty($whitelist) && isset($options['order']) && is_array($options['order'])) {
376: $field = key($options['order']);
377: $inWhitelist = in_array($field, $whitelist, true);
378: if (!$inWhitelist) {
379: $options['order'] = null;
380: }
381: return $options;
382: }
383:
384: if (!empty($options['order']) && is_array($options['order'])) {
385: $order = array();
386: foreach ($options['order'] as $key => $value) {
387: $field = $key;
388: $alias = $object->alias;
389: if (strpos($key, '.') !== false) {
390: list($alias, $field) = explode('.', $key);
391: }
392: $correctAlias = ($object->alias == $alias);
393:
394: if ($correctAlias && $object->hasField($field)) {
395: $order[$object->alias . '.' . $field] = $value;
396: } elseif ($correctAlias && $object->hasField($key, true)) {
397: $order[$field] = $value;
398: } elseif (isset($object->{$alias}) && $object->{$alias}->hasField($field, true)) {
399: $order[$alias . '.' . $field] = $value;
400: }
401: }
402: $options['order'] = $order;
403: }
404: if (empty($options['order']) && !empty($object->order)) {
405: $options['order'] = $object->order;
406: }
407: return $options;
408: }
409:
410: 411: 412: 413: 414: 415:
416: public function checkLimit(array $options) {
417: $options['limit'] = (int)$options['limit'];
418: if (empty($options['limit']) || $options['limit'] < 1) {
419: $options['limit'] = 1;
420: }
421: $options['limit'] = min($options['limit'], $options['maxLimit']);
422: return $options;
423: }
424:
425: }
426: