02.04 php
hiromi2424
Note
patch for paginator to be able to order calclational colmun.
  1.  
  2.     function paginate($object = null, $scope = array(), $whitelist = array()) {
  3.         if (is_array($object)) {
  4.             $whitelist = $scope;
  5.             $scope = $object;
  6.             $object = null;
  7.         }
  8.         $assoc = null;
  9.  
  10.         if (is_string($object)) {
  11.             $assoc = null;
  12.  
  13.             if (strpos($object, '.') !== false) {
  14.                 list($object, $assoc) = explode('.', $object);
  15.             }
  16.  
  17.             if ($assoc && isset($this->{$object}->{$assoc})) {
  18.                 $object =& $this->{$object}->{$assoc};
  19.             } elseif ($assoc && isset($this->{$this->modelClass}) && isset($this->{$this->modelClass}->{$assoc})) {
  20.                 $object =& $this->{$this->modelClass}->{$assoc};
  21.             } elseif (isset($this->{$object})) {
  22.                 $object =& $this->{$object};
  23.             } elseif (isset($this->{$this->modelClass}) && isset($this->{$this->modelClass}->{$object})) {
  24.                 $object =& $this->{$this->modelClass}->{$object};
  25.             }
  26.         } elseif (empty($object) || $object === null) {
  27.             if (isset($this->{$this->modelClass})) {
  28.                 $object =& $this->{$this->modelClass};
  29.             } else {
  30.                 $className = null;
  31.                 $name = $this->uses[0];
  32.                 if (strpos($this->uses[0], '.') !== false) {
  33.                     list($name, $className) = explode('.', $this->uses[0]);
  34.                 }
  35.                 if ($className) {
  36.                     $object =& $this->{$className};
  37.                 } else {
  38.                     $object =& $this->{$name};
  39.                 }
  40.             }
  41.         }
  42.  
  43.         if (!is_object($object)) {
  44.             trigger_error(sprintf(__('Controller::paginate() - can\'t find model %1$s in controller %2$sController', true), $object, $this->name), E_USER_WARNING);
  45.             return array();
  46.         }
  47.         $options = array_merge($this->params, $this->params['url'], $this->passedArgs);
  48.  
  49.         if (isset($this->paginate[$object->alias])) {
  50.             $defaults = $this->paginate[$object->alias];
  51.         } else {
  52.             $defaults = $this->paginate;
  53.         }
  54.  
  55.         if (isset($options['show'])) {
  56.             $options['limit'] = $options['show'];
  57.         }
  58.  
  59.         if (isset($options['sort'])) {
  60.             $direction = null;
  61.             if (isset($options['direction'])) {
  62.                 $direction = strtolower($options['direction']);
  63.             }
  64.             if ($direction != 'asc' && $direction != 'desc') {
  65.                 $direction = 'asc';
  66.             }
  67.             $options['order'] = array($options['sort'] => $direction);
  68.         }
  69.  
  70.         if (!empty($options['order']) && is_array($options['order'])) {
  71.             $alias = $object->alias ;
  72.             $key = $field = key($options['order']);
  73.  
  74.             if (strpos($key, '.') !== false) {
  75.                 list($alias, $field) = explode('.', $key);
  76.             }
  77.             $value = $options['order'][$key];
  78.             unset($options['order'][$key]);
  79.  
  80.             if (isset($object->{$alias}) && $object->{$alias}->hasField($field)) {
  81.                 $options['order'][$alias . '.' . $field] = $value;
  82.             } elseif ($object->hasField($field)) {
  83.                 $options['order'][$alias . '.' . $field] = $value;
  84.             } else{
  85.                 $_fields = isset($defaults['fields']) ? $defaults['fields'] : null;
  86.                 $_fields = isset($options['fields'])  ? $options['fields']  : $_fields;
  87.                
  88.                 foreach((array)$_fields as $_field){
  89.                     $_fieldAlias = $_field;
  90.                    
  91.                     $regex = '/\s+as\s+/';
  92.                     if(preg_match($regex,$_field)){
  93.                         list($_field,$_fieldAlias) = preg_split($regex,$_field);
  94.                     }
  95.                     $_field      = str_replace('`','',trim($_field));
  96.                     $_fieldAlias = str_replace('`','',trim($_fieldAlias));
  97.                    
  98.                     @list($_alias,$_field) = explode('.',$_field);
  99.                    
  100.                     if($_field === null){
  101.                         $_field = $_alias;
  102.                     }
  103.                    
  104.                     if($field === $_field || $field === $_fieldAlias){
  105.                         $options['order'][$field] = $value;
  106.                     }
  107.                 }
  108.             }
  109.         }
  110.         $vars = array('fields', 'order', 'limit', 'page', 'recursive');
  111.         $keys = array_keys($options);
  112.         $count = count($keys);
  113.  
  114.         for ($i = 0; $i < $count; $i++) {
  115.             if (!in_array($keys[$i], $vars, true)) {
  116.                 unset($options[$keys[$i]]);
  117.             }
  118.             if (empty($whitelist) && ($keys[$i] === 'fields' || $keys[$i] === 'recursive')) {
  119.                 unset($options[$keys[$i]]);
  120.             } elseif (!empty($whitelist) && !in_array($keys[$i], $whitelist)) {
  121.                 unset($options[$keys[$i]]);
  122.             }
  123.         }
  124.         $conditions = $fields = $order = $limit = $page = $recursive = null;
  125.  
  126.         if (!isset($defaults['conditions'])) {
  127.             $defaults['conditions'] = array();
  128.         }
  129.  
  130.         $type = 'all';
  131.  
  132.         if (isset($defaults[0])) {
  133.             $type = $defaults[0];
  134.             unset($defaults[0]);
  135.         }
  136.  
  137.         extract($options = array_merge(array('page' => 1, 'limit' => 20), $defaults, $options));
  138.  
  139.         if (is_array($scope) && !empty($scope)) {
  140.             $conditions = array_merge($conditions, $scope);
  141.         } elseif (is_string($scope)) {
  142.             $conditions = array($conditions, $scope);
  143.         }
  144.         if ($recursive === null) {
  145.             $recursive = $object->recursive;
  146.         }
  147.  
  148.         $extra = array_diff_key($defaults, compact(
  149.             'conditions', 'fields', 'order', 'limit', 'page', 'recursive'
  150.         ));
  151.         if ($type !== 'all') {
  152.             $extra['type'] = $type;
  153.         }
  154.  
  155.         if (method_exists($object, 'paginateCount')) {
  156.             $count = $object->paginateCount($conditions, $recursive, $extra);
  157.         } else {
  158.             $parameters = compact('conditions');
  159.             if ($recursive != $object->recursive) {
  160.                 $parameters['recursive'] = $recursive;
  161.             }
  162.             $count = $object->find('count', array_merge($parameters, $extra));
  163.         }
  164.         $pageCount = intval(ceil($count / $limit));
  165.  
  166.         if ($page === 'last' || $page >= $pageCount) {
  167.             $options['page'] = $page = $pageCount;
  168.         } elseif (intval($page) < 1) {
  169.             $options['page'] = $page = 1;
  170.         }
  171.         $page = $options['page'] = (integer)$page;
  172.  
  173.         if (method_exists($object, 'paginate')) {
  174.             $results = $object->paginate($conditions, $fields, $order, $limit, $page, $recursive, $extra);
  175.         } else {
  176.             $parameters = compact('conditions', 'fields', 'order', 'limit', 'page');
  177.             if ($recursive != $object->recursive) {
  178.                 $parameters['recursive'] = $recursive;
  179.             }
  180.             $results = $object->find($type, array_merge($parameters, $extra));
  181.         }
  182.         $paging = array(
  183.             'page'    => $page,
  184.             'current'   => count($results),
  185.             'count'  => $count,
  186.             'prevPage'  => ($page > 1),
  187.             'nextPage'  => ($count > ($page * $limit)),
  188.             'pageCount' => $pageCount,
  189.             'defaults'  => array_merge(array('limit' => 20, 'step' => 1), $defaults),
  190.             'options'   => $options
  191.         );
  192.         $this->params['paging'][$object->alias] = $paging;
  193.  
  194.         if (!in_array('Paginator', $this->helpers) && !array_key_exists('Paginator', $this->helpers)) {
  195.             $this->helpers[] = 'Paginator';
  196.         }
  197.         return $results;
  198.     }
Parsed in 0.340 seconds, using GeSHi 1.0.7.14

Modify this Paste