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