Ejemplo n.º 1
0
 /**
  * 获取新的URI
  *
  * @param  array $params additional route parameters
  * @return string
  * @uses   HttpIO::param
  * @uses   Route::uri
  */
 public static function uri(array $params = null)
 {
     $params_array = HttpIO::param();
     if (null === Core_Route::$current_route) {
         $tmpstr = array();
         if ($params_array['directory']) {
             $tmpstr[] = $params_array['directory'];
         }
         # 记录控制器和action的序号
         $controller_index = $action_index = null;
         if ($params_array['controller']) {
             $controller = strtolower($params_array['controller']);
             if (preg_match('#^Library_[a-z0-9]+_[a-z0-9]+_([a-z0-9_]+)$#i', $params_array['controller'], $m)) {
                 # 类库中的控制器
                 $controller = $m[1];
             }
             $pos = strrpos($controller, '_');
             if (false !== $pos) {
                 $controller = substr($controller, $pos + 1);
             }
             $controller_index = count($tmpstr);
             if ($controller != 'default') {
                 $tmpstr[] = $controller;
             }
         }
         $action = strtolower(substr($params_array['action'], 7));
         // Action_Abc -> abc
         if ($action && $action != 'default') {
             $action_index = count($tmpstr);
             $tmpstr[] = $action;
         }
         if ($params) {
             foreach ($params as $k => $v) {
                 $params_array['arguments'][$k] = $v;
             }
         }
         if ($params_array['arguments']) {
             foreach ($params_array['arguments'] as $v) {
                 $tmpstr[] = $v;
             }
         }
         $count = count($tmpstr);
         for ($i = $count - 1; $i >= 0; $i--) {
             if ($tmpstr[$i] === '' || null === $tmpstr[$i]) {
                 unset($tmpstr[$i]);
             } elseif ($tmpstr[$i] == 'index') {
                 if ($i === $action_index) {
                     unset($tmpstr[$i]);
                 } elseif ($i === $controller_index) {
                     unset($tmpstr[$i]);
                 } else {
                     break;
                 }
             } else {
                 break;
             }
         }
         return rtrim(implode('/', $tmpstr), '/') . '/';
     } else {
         if (!isset($params['directory'])) {
             // Add the current directory
             $params['directory'] = $params_array['directory'];
         }
         if (!isset($params['controller'])) {
             // Add the current controller
             $params['controller'] = $params_array['controller'];
         }
         if (!isset($params['action'])) {
             // Add the current action
             $params['action'] = $params_array['action'];
         }
         // Add the current parameters
         $params += $params_array;
         return Core::route()->uri($params);
     }
 }
Ejemplo n.º 2
0
 /**
  * Loads configuration settings into the object and (re)calculates pagination if needed.
  * Allows you to update config settings after a Pagination object has been constructed.
  *
  * @param   array   configuration
  * @return  object  Pagination
  */
 public function setup(array $config = array())
 {
     if (isset($config['group'])) {
         // Recursively load requested config groups
         $config += $this->config_group($config['group']);
     }
     // Overwrite the current config settings
     $this->config = $config + $this->config;
     // Only (re)calculate pagination when needed
     if ($this->current_page === null || isset($config['current_page']) || isset($config['total_items']) || isset($config['items_per_page'])) {
         // Retrieve the current page number
         if (!empty($this->config['current_page']['page'])) {
             // The current page number has been set manually
             $this->current_page = (int) $this->config['current_page']['page'];
         } else {
             switch ($this->config['current_page']['source']) {
                 case 'query_string':
                     $this->current_page = isset($_GET[$this->config['current_page']['key']]) ? (int) $_GET[$this->config['current_page']['key']] : 1;
                     break;
                 case 'route':
                     $this->current_page = (int) \HttpIO::param($this->config['current_page']['key'], 1);
                 case 'default':
                 default:
                     $this->current_page = isset(\HttpIO::$params['arguments'][$this->config['current_page']['key']]) ? (int) \HttpIO::$params['arguments'][$this->config['current_page']['key']] : 1;
                     break;
             }
         }
         // Calculate and clean all pagination variables
         $this->total_items = (int) \max(0, $this->config['total_items']);
         $this->items_per_page = (int) \max(1, $this->config['items_per_page']);
         $this->total_pages = (int) ceil($this->total_items / $this->items_per_page);
         $this->current_page = (int) \min(\max(1, $this->current_page), \max(1, $this->total_pages));
         $this->current_first_item = (int) \min(($this->current_page - 1) * $this->items_per_page + 1, $this->total_items);
         $this->current_last_item = (int) \min($this->current_first_item + $this->items_per_page - 1, $this->total_items);
         $this->previous_page = $this->current_page > 1 ? $this->current_page - 1 : false;
         $this->next_page = $this->current_page < $this->total_pages ? $this->current_page + 1 : false;
         $this->first_page = $this->current_page === 1 ? false : 1;
         $this->last_page = $this->current_page >= $this->total_pages ? false : $this->total_pages;
         $this->offset = (int) (($this->current_page - 1) * $this->items_per_page);
     }
     // Chainable method
     return $this;
 }
Ejemplo n.º 3
0
 /**
  * 设置更新
  *
  * @param   array   configuration
  * @return  object  Pagination
  */
 protected function renew()
 {
     $this->_is_renew = true;
     if (null === $this->current_page) {
         if (!empty($this->config['page'])) {
             $this->current_page = (int) $this->config['page'];
         } else {
             switch ($this->config['source']) {
                 case 'query_string':
                     $this->current_page = isset($_GET[$this->config['key']]) ? (int) $_GET[$this->config['key']] : 1;
                     break;
                 case 'route':
                     $this->current_page = (int) HttpIO::param($this->config['key'], 1);
                     break;
                 case 'default':
                 default:
                     $this->current_page = isset(HttpIO::$params['arguments'][$this->config['key']]) ? (int) HttpIO::$params['arguments'][$this->config['key']] : 1;
                     break;
             }
         }
     } else {
         $this->current_page = (int) max(1, $this->current_page);
     }
     $this->items_per_page = (int) $this->items_per_page;
     if (!$this->items_per_page > 0) {
         $this->items_per_page = (int) max(1, $this->config['items_per_page']);
     }
     $this->total_pages = (int) ceil($this->total_items / $this->items_per_page);
     $this->current_page = (int) min(max(1, $this->current_page), max(1, $this->total_pages));
     $this->current_first_item = (int) min(($this->current_page - 1) * $this->items_per_page + 1, $this->total_items);
     $this->current_last_item = (int) min($this->current_first_item + $this->items_per_page - 1, $this->total_items);
     $this->previous_page = $this->current_page > 1 ? $this->current_page - 1 : false;
     $this->next_page = $this->current_page < $this->total_pages ? $this->current_page + 1 : false;
     $this->first_page = $this->current_page === 1 ? false : 1;
     $this->last_page = $this->current_page >= $this->total_pages ? false : $this->total_pages;
     $this->offset = (int) (($this->current_page - 1) * $this->items_per_page);
 }