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