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:
188: $paging = array(
189: 'page' => $page,
190: 'current' => count($results),
191: 'count' => $count,
192: 'prevPage' => ($page > 1),
193: 'nextPage' => ($count > ($page * $limit)),
194: 'pageCount' => $pageCount,
195: 'order' => $order,
196: 'limit' => $limit,
197: 'options' => Set::diff($options, $defaults),
198: 'paramType' => $options['paramType']
199: );
200: if (!isset($this->Controller->request['paging'])) {
201: $this->Controller->request['paging'] = array();
202: }
203: $this->Controller->request['paging'] = array_merge(
204: (array)$this->Controller->request['paging'],
205: array($object->alias => $paging)
206: );
207:
208: if (
209: !in_array('Paginator', $this->Controller->helpers) &&
210: !array_key_exists('Paginator', $this->Controller->helpers)
211: ) {
212: $this->Controller->helpers[] = 'Paginator';
213: }
214: return $results;
215: }
216:
217: 218: 219: 220: 221: 222:
223: protected function _getObject($object) {
224: if (is_string($object)) {
225: $assoc = null;
226: if (strpos($object, '.') !== false) {
227: list($object, $assoc) = pluginSplit($object);
228: }
229:
230: if ($assoc && isset($this->Controller->{$object}->{$assoc})) {
231: $object = $this->Controller->{$object}->{$assoc};
232: } elseif (
233: $assoc && isset($this->Controller->{$this->Controller->modelClass}) &&
234: isset($this->Controller->{$this->Controller->modelClass}->{$assoc}
235: )) {
236: $object = $this->Controller->{$this->Controller->modelClass}->{$assoc};
237: } elseif (isset($this->Controller->{$object})) {
238: $object = $this->Controller->{$object};
239: } elseif (
240: isset($this->Controller->{$this->Controller->modelClass}) && isset($this->Controller->{$this->Controller->modelClass}->{$object}
241: )) {
242: $object = $this->Controller->{$this->Controller->modelClass}->{$object};
243: }
244: } elseif (empty($object) || $object === null) {
245: if (isset($this->Controller->{$this->Controller->modelClass})) {
246: $object = $this->Controller->{$this->Controller->modelClass};
247: } else {
248: $className = null;
249: $name = $this->Controller->uses[0];
250: if (strpos($this->Controller->uses[0], '.') !== false) {
251: list($name, $className) = explode('.', $this->Controller->uses[0]);
252: }
253: if ($className) {
254: $object = $this->Controller->{$className};
255: } else {
256: $object = $this->Controller->{$name};
257: }
258: }
259: }
260: return $object;
261: }
262:
263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277:
278: public function mergeOptions($alias) {
279: $defaults = $this->getDefaults($alias);
280: switch ($defaults['paramType']) {
281: case 'named':
282: $request = $this->Controller->request->params['named'];
283: break;
284: case 'querystring':
285: $request = $this->Controller->request->query;
286: break;
287: }
288: $request = array_intersect_key($request, array_flip($this->whitelist));
289: return array_merge($defaults, $request);
290: }
291:
292: 293: 294: 295: 296: 297: 298:
299: public function getDefaults($alias) {
300: if (isset($this->settings[$alias])) {
301: $defaults = $this->settings[$alias];
302: } else {
303: $defaults = $this->settings;
304: }
305: return array_merge(
306: array('page' => 1, 'limit' => 20, 'maxLimit' => 100, 'paramType' => 'named'),
307: $defaults
308: );
309: }
310:
311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323:
324: public function validateSort($object, $options, $whitelist = array()) {
325: if (isset($options['sort'])) {
326: $direction = null;
327: if (isset($options['direction'])) {
328: $direction = strtolower($options['direction']);
329: }
330: if ($direction != 'asc' && $direction != 'desc') {
331: $direction = 'asc';
332: }
333: $options['order'] = array($options['sort'] => $direction);
334: }
335:
336: if (!empty($whitelist) && isset($options['order']) && is_array($options['order'])) {
337: $field = key($options['order']);
338: if (!in_array($field, $whitelist)) {
339: $options['order'] = null;
340: }
341: }
342:
343: if (!empty($options['order']) && is_array($options['order'])) {
344: $order = array();
345: foreach ($options['order'] as $key => $value) {
346: $field = $key;
347: $alias = $object->alias;
348: if (strpos($key, '.') !== false) {
349: list($alias, $field) = explode('.', $key);
350: }
351:
352: if ($object->hasField($field)) {
353: $order[$alias . '.' . $field] = $value;
354: } elseif ($object->hasField($key, true)) {
355: $order[$field] = $value;
356: } elseif (isset($object->{$alias}) && $object->{$alias}->hasField($field, true)) {
357: $order[$alias . '.' . $field] = $value;
358: }
359: }
360: $options['order'] = $order;
361: }
362:
363: return $options;
364: }
365:
366: 367: 368: 369: 370: 371:
372: public function checkLimit($options) {
373: $options['limit'] = (int) $options['limit'];
374: if (empty($options['limit']) || $options['limit'] < 1) {
375: $options['limit'] = 1;
376: }
377: $options['limit'] = min((int)$options['limit'], $options['maxLimit']);
378: return $options;
379: }
380: }
381: