/**
  * Index action
  */
 public function indexAction()
 {
     $page = $this->request->getQuery('page', 'int', 1);
     $limit = $this->request->getQuery('limit', 'int', 5);
     $orderBy = $this->request->getQuery('order', 'string', 'id');
     $searchName = $this->request->getQuery('q', 'string', null);
     $projects = new Projects();
     $totalCount = $projects->count();
     $qOrderAsc = $this->request->getQuery('asc', 'int', 1);
     $orderAsc = $qOrderAsc == 1 ? ' ASC' : ' DESC';
     $builder = $this->modelsManager->createBuilder()->columns('*')->from('Projects')->orderBy($orderBy . ' ' . $orderAsc);
     if ($searchName !== null) {
         $builder->andWhere("name like '%{$searchName}%'")->orWhere("uri like '%{$searchName}%'");
     }
     $paginator = new PaginatorQueryBuilder(array("builder" => $builder, "limit" => $limit, "page" => $page));
     $paginate = $paginator->getPaginate();
     if ($page > $paginate->last && $paginate->last > 0) {
         $q = $this->request->getQuery();
         if ($totalCount > 0 && $limit > $totalCount) {
             $q['page'] = $paginate->last;
             return $this->response->redirect($this->url->get('', $q));
         } elseif ($totalCount > 0) {
             return $this->dispatcher->forward(['controller' => 'error', 'action' => 'error404']);
         }
     }
     $this->view->setVar("totalProjects", $totalCount);
     $this->view->setVar("projectList", $paginate);
 }
Example #2
0
 /**
  * list
  */
 public function indexAction()
 {
     error_reporting(E_ALL & ~E_NOTICE);
     //$this->persistent->parameters = null;
     $numberPage = 1;
     if ($this->request->isPost()) {
         $query = Criteria::fromInput($this->di, "Topic", $_POST);
         $this->persistent->parameters = $query->getParams();
     } else {
         $numberPage = $this->request->getQuery("page", "int");
     }
     if ($numberPage == null) {
         $this->persistent->parameters = null;
     }
     $parameters = $this->persistent->parameters;
     if (!is_array($parameters)) {
         $parameters = array();
     }
     if ($this->request->getPost("topic")) {
         $parameters["conditions"] .= " and topictype = 2 and status = 0";
         //status:
     } else {
         $parameters["conditions"] = "topictype = 2 and status = 0";
         //status:
     }
     //$parameters["order"] = "id";
     $builder = $this->modelsManager->createBuilder()->columns('*')->from('Topic')->where($parameters['conditions'], $parameters['bind'])->orderBy('id');
     $paginator = new PaginatorQueryBuilder(array("builder" => $builder, "limit" => 20, "page" => $numberPage));
     $this->view->page = $paginator->getPaginate();
 }
Example #3
0
 /**
  * @desc - สำหรับแสดงข้อมูลพนักงาน
  * @return object
  */
 public function indexAction()
 {
     //สร้างคลาสตัวแปร สำหรับสร้างคำสั่ง SQL เพื่อคิวรี่ Select * from Employee
     $employees = $this->modelsManager->createBuilder()->from('Employee')->orderBy('SSN');
     //กำหนดเงื่อนไขการโหลดข้อมูลจากตาราง Employee ครั้งละ 5 แถว ด้วยเงื่อนไข limit เช่น Select * from Employee limit 1,5
     $paginator = new PaginacionBuilder(array("builder" => $employees, "limit" => 5, "page" => $this->request->getQuery('page', 'int')));
     //ส่งข้อมูลสู่ View ภายใต้ตัวแปร page สำหรับแสดงผลบนหน้าเว็บ
     $this->view->page = $paginator->getPaginate();
 }
 public function page($table, $field, $where, $orderBy, $currentPage = 1, $limit = self::LIMIT_ITEM)
 {
     if (empty($limit)) {
         $limit = self::LIMIT_ITEM;
     }
     $builder = $this->modelsManager->createBuilder()->columns($field)->from($table)->where($where)->orderBy($orderBy);
     $paginator = new PaginatorQueryBuilder(array("builder" => $builder, "limit" => $limit, "page" => $currentPage));
     return $paginator->getPaginate();
 }
Example #5
0
 public function getLink($page = 1)
 {
     global $app;
     $result = $app->modelsManager->createBuilder()->columns('url, anchor')->addFrom('generation')->where('domain=:domain: AND parent_url=:parent_url:', ['domain' => $this->data['domain'], 'parent_url' => $this->data['url']]);
     $paginate = new QueryBuilder(array("builder" => $result, "limit" => 30, "page" => $page));
     $page = $paginate->getPaginate();
     //        print_r($page);
     //        die();
     $this->paginate = ['total' => $page->total_pages, 'current' => $page->current, 'before' => $page->before, 'next' => $page->next, 'last' => $page->last];
     foreach ($page->items as $item) {
         $this->link[$item->url] = mb_convert_encoding($item->anchor, 'UTF-8');
     }
 }
Example #6
0
 /**
  * 注意:同一个 Paginator 对象一旦执行了 getPaginate 方法以后,
  * 即使修改了 paginator 的其他属性,获取的都是同一个 paginate.
  *
  * @return \Phalcon\Paginator\Adapter\stdClass
  */
 public function getPaginate()
 {
     if ($this->_paginate !== null) {
         return $this->_paginate;
     }
     $paginate = parent::getPaginate();
     $paginate->offset_start = 0;
     $paginate->offset_end = 0;
     if ($paginate->total_items > 0) {
         $paginate->offset_start = ($paginate->current - 1) * ceil($paginate->total_items / $paginate->total_pages) + 1;
         $paginate->offset_end = $paginate->offset_start + count($paginate->items) - 1;
     }
     $i = 0;
     $pageRange = $this->getPagerRange();
     $prevPageRange = array();
     $prevPageRangeSkip = false;
     if ($paginate->current > 1) {
         $i = $paginate->current - $pageRange;
         $i = $i <= 1 ? 1 : $i;
         for (; $i < $paginate->current; $i++) {
             $prevPageRange[] = $i;
         }
         if ($prevPageRange && $prevPageRange[0] > 1) {
             $prevPageRangeSkip = true;
         }
     }
     $nextPageRange = array();
     $nextPageRangeSkip = false;
     if ($paginate->current < $paginate->total_pages) {
         $limit = $paginate->current + $pageRange;
         $limit = $limit >= $paginate->total_pages ? $paginate->total_pages : $limit;
         $i = $paginate->current + 1;
         for (; $i <= $limit; $i++) {
             $nextPageRange[] = $i;
         }
         if ($nextPageRange && $nextPageRange[count($nextPageRange) - 1] < $paginate->total_pages) {
             $nextPageRangeSkip = true;
         }
     }
     $paginate->page_range = $pageRange;
     $paginate->prev_skip = $prevPageRangeSkip;
     $paginate->prev_range = $prevPageRange;
     $paginate->next_skip = $nextPageRangeSkip;
     $paginate->next_range = $nextPageRange;
     $paginate->query = $this->getQuery();
     $this->_paginate = $paginate;
     return $this->_paginate;
 }
 public function indexAction()
 {
     $limit = $this->getQueryLimit();
     $page = $this->getQueryPage();
     $builder = $this->modelsManager->createBuilder()->from('Catalog\\Model\\Product')->orderBy('id ASC');
     $paginator = new QueryBuilder(array("builder" => $builder, "limit" => $limit, "page" => $page));
     $result = array();
     $page = $paginator->getPaginate();
     /**
      * @var $product Product
      */
     foreach ($page->items as $product) {
         $result[] = ['id' => $product->id, 'title' => $product->title];
     }
     return array('success' => true, 'result' => array('products' => $result, 'pages' => $page->total_pages, 'total' => $page->total_items));
 }
 public function indexAction()
 {
     $limit = $this->getQueryLimit();
     $page = $this->getQueryPage();
     $builder = $this->modelsManager->createBuilder()->from('User\\Model\\User')->orderBy('id ASC');
     $paginator = new QueryBuilder(array("builder" => $builder, "limit" => $limit, "page" => $page));
     $result = array();
     $page = $paginator->getPaginate();
     /**
      * @var $user  User
      */
     foreach ($page->items as $user) {
         $result[] = ['id' => $user->id, 'nick' => $user->nick, 'firstname' => $user->firstname, 'lastname' => $user->lastname];
     }
     return array('success' => true, 'result' => array('users' => $result, 'pages' => $page->total_pages, 'total' => $page->total_items));
 }
 public function getResponse()
 {
     $builder = new PQueryBuilder(['builder' => $this->builder, 'limit' => 1, 'page' => 1]);
     $total = $builder->getPaginate();
     $this->bind('global_search', function ($column, $search) {
         $this->builder->orWhere("{$column} LIKE :key_{$column}:", ["key_{$column}" => "%{$search}%"]);
     });
     $this->bind('column_search', function ($column, $search) {
         $this->builder->andWhere("{$column} LIKE :key_{$column}:", ["key_{$column}" => "%{$search}%"]);
     });
     $this->bind('order', function ($order) {
         $this->builder->orderBy(implode(', ', $order));
     });
     $builder = new PQueryBuilder(['builder' => $this->builder, 'limit' => $this->parser->getLimit(), 'page' => $this->parser->getPage()]);
     $filtered = $builder->getPaginate();
     return $this->formResponse(['total' => $total->total_items, 'filtered' => $filtered->total_items, 'data' => $filtered->items->toArray()]);
 }
 /**
  * 重写QueryBuilder类的getPaginate方法,主要是实现group by查询的支持
  *
  * @return \stdClass
  * @author : Hunter.<*****@*****.**>
  */
 public function getPaginate()
 {
     $paginate = parent::getPaginate();
     $builder = $this->getQueryBuilder();
     if ($group = $builder->getGroupBy()) {
         //根据group by语句构建count查询
         $result = $builder->columns('count(distinct ' . $group . ') as total')->groupBy(null)->getQuery()->execute()->getFirst();
         //总条数
         $paginate->total_items = $result->total;
         //总页数
         $paginate->total_pages = ceil($paginate->total_items / $this->getLimit());
         //上一页
         $paginate->before = $this->getCurrentPage() - 1 <= 0 ? 1 : $this->getCurrentPage() - 1;
         //下一页
         if ($paginate->total_pages > 1 and $this->getCurrentPage() <= 1) {
             $paginate->next = 2;
         } else {
             $paginate->next = $this->getCurrentPage() + 1 > $paginate->total_pages ? $paginate->total_pages : $this->getCurrentPage() + 1;
         }
         //最后一页
         $paginate->last = $paginate->total_pages;
     }
     return $paginate;
 }
 /**
  * @param $page
  * @param $limit
  * @return \stdClass
  */
 public function fetchLoginHistory($page, $limit)
 {
     $builder = $this->getModelsManager()->createBuilder()->columns('*')->where("user_id = :user_id:", ['user_id' => $this->user_id])->from(UserLoginHistory::class);
     $paginator = new PaginatorQueryBuilder(["builder" => $builder, "limit" => empty($limit) || $limit < 1 ? self::DEFAULT_PAGE_LIMIT : $limit, "page" => $page < 1 ? 1 : $page]);
     return $paginator->getPaginate();
 }
Example #12
0
 public static function runPaginate($params, $limit, $offset)
 {
     $builder = new PhBuilder($params);
     $paginator = new PhQueryBuilder(['builder' => $builder, 'limit' => $limit, 'page' => $offset]);
     return $paginator->getPaginate();
 }
Example #13
0
 /**
  * Get pagination from query builder
  *
  * @param \Phalcon\Mvc\Model\Query\BuilderInterface $queryBuilder
  * @param int $paginationLimit
  * @param int $currentPage
  * @return \stdClass
  */
 public static function getPaginationQueryBuilder($queryBuilder, $paginationLimit, $currentPage)
 {
     $pagination = new PaginationQueryBuilder(['builder' => $queryBuilder, 'limit' => self::checkInt($paginationLimit), 'page' => self::checkInt($currentPage)]);
     return $pagination->getPaginate();
 }
Example #14
0
 public function getResponse()
 {
     $builder = new PQueryBuilder(['builder' => $this->builder, 'limit' => 1, 'page' => 1]);
     $total = $builder->getPaginate();
     $this->bind('global_search', function ($column, $search) {
         $this->builder->orWhere("{$column} LIKE :key_{$column}:", ["key_{$column}" => "%{$search}%"]);
     });
     $this->bind('column_search', function ($column, $search) {
         $this->builder->andWhere("{$column} LIKE :key_{$column}:", ["key_{$column}" => "%{$search}%"]);
     });
     $this->bind('order', function ($order) {
         if (!empty($order)) {
             $this->builder->orderBy(implode(', ', $order));
         }
     });
     $builder = new PQueryBuilder(['builder' => $this->builder, 'limit' => $this->parser->getLimit(), 'page' => $this->parser->getPage()]);
     /* @var $filtered \Phalcon\Mvc\Model\Resultset  */
     $filtered = $builder->getPaginate();
     /* @var $metadata \Phalcon\Mvc\Model\MetaData  */
     $metadata = \Phalcon\Di::getDefault()->get('modelsMetadata');
     $item = $filtered->items->getFirst();
     if ($item instanceof \Phalcon\Mvc\Model) {
         $filtered->items->rewind();
         $columnMap = $metadata->getAttributes($item);
         $columnMap = array_combine($columnMap, $columnMap);
         $extractMethods = function ($item) {
             $reflection = new \ReflectionClass($item);
             $itemMethods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC);
             $itemMethods = array_map(function (\ReflectionMethod $reflectionMethod) {
                 return $reflectionMethod->getName();
             }, $itemMethods);
             return array_combine($itemMethods, $itemMethods);
         };
         // if use array_diff we can catch error, because $this->originalColumns can have array item
         $attributes = $methods = [];
         foreach ($this->originalColumns as $itemColumn) {
             $itemData = [];
             if (is_string($itemColumn)) {
                 // check that it is item attribute
                 if (isset($columnMap[$itemColumn])) {
                     $attributes[] = $itemColumn;
                 }
             } elseif (is_array($itemColumn)) {
                 /**
                  * Possible variants
                  * itemColumn => [methodName => [param1, param2]] - method with parameters
                  * itemColumn => methodName] - method without parameters
                  * 
                  */
                 $columnName = array_keys($itemColumn)[0];
                 $methodData = $itemColumn[$columnName];
                 if (!isset($columnMap[$columnName])) {
                     // undefined columnName
                     //continue;
                 }
                 $parameters = null;
                 if (is_array($methodData)) {
                     $methodName = array_keys($methodData)[0];
                     $parameters = $methodData[$methodName];
                 } else {
                     $methodName = $methodData;
                 }
                 // check that it is existed method
                 if (empty($itemMethods)) {
                     $itemMethods = $extractMethods($item);
                 }
                 if (isset($itemMethods[$methodName])) {
                     $methods[$columnName] = compact('methodName', 'parameters');
                 }
             }
         }
         $data = [];
         foreach ($filtered->items as $item) {
             $itemData = [];
             foreach ($attributes as $attr) {
                 $itemData[$attr] = $item->readAttribute($attr);
             }
             foreach ($methods as $columnName => $method) {
                 $parameters = !empty($method['parameters']) ? $method['parameters'] : [];
                 $itemData[$columnName] = call_user_func_array([$item, $method['methodName']], $parameters);
             }
             $data[] = $itemData;
         }
     } else {
         $data = $filtered->items->toArray();
     }
     return $this->formResponse(['total' => $total->total_items, 'filtered' => $filtered->total_items, 'data' => $data]);
 }
 /**
  * 管理列表页面
  */
 public function articlesAction()
 {
     $currentPage = intval($this->request->get('page'));
     $currentPage = $currentPage ? $currentPage : 1;
     $author = $this->session->get(self::sessPrefix . 'name');
     $builder = $this->modelsManager->createBuilder()->columns('id, title, content, pubtime, author')->where('author = :author:', array('author' => $author))->from('Article')->orderBy('id desc');
     $paginator = new PaginatorQueryBuilder(array("builder" => $builder, "limit" => 5, "page" => $currentPage));
     $page = $paginator->getPaginate();
     $this->view->page = $page;
     /*$sql = 'select id,title,content,pubtime,author from article where author=? order by pubtime desc';
             $this->session->start();
             $author = $this->session->get(self::sessPrefix. 'name');
     
             $conn = $this->getConnection();
             $res = $conn->fetchAll($sql, Phalcon\Db::FETCH_ASSOC, array($author));
             if (!$res) $res = array();
             $this->view->data = $res;
     
             $paginator = new PaginatorArray(
                 array(
                     "data"  => $res,
                     "limit" => 3,
                     "page"  => $currentPage
                 )
             );
             $page = $paginator->getPaginate();*/
     //die;
 }
Example #16
0
 /**
  * 通用的分页获取
  *
  * @param        $builder
  * @param int    $limit
  * @param string $getPageParam  get参数的分页标示,一般为page
  * @param string $model
  * @param string $userBuilder   自定义QueryBuilder,填写my时使用自定义的MyQueryBuilder
  *
  * @return \ST\Common\Models\User
  * @author Hunter.<*****@*****.**>
  */
 public static function getList($builder, $limit = 20, $getPageParam = 'page', $model = '', $userBuilder = 'sys')
 {
     if (!$builder) {
         if (!$model) {
             return false;
         }
         //根据指定模型获取无条件的所有数据
         $builder = Di::getDefault()->get('modelsManager')->createBuilder()->from($model);
     }
     $getPageParam || ($getPageParam = 'page');
     $params = ["builder" => $builder, "limit" => $limit, "page" => Di::getDefault()->get('dispatcher')->getParam($getPageParam, 'int')];
     //构建分页查询
     if ($userBuilder == 'my') {
         $r = new MyQueryBuilder($params);
     } else {
         $r = new QueryBuilder($params);
     }
     return $r->getPaginate();
 }
Example #17
0
 /**
  * Get list record with pagination
  * @param $params
  * @param $limit
  * @param $offset
  * @return mixed
  */
 public static function getList($params, $limit, $offset)
 {
     $builder = new Builder($params);
     $pagination = new PaginatorQueryBuilder(['builder' => $builder, 'limit' => $limit, 'page' => $offset]);
     return $pagination->getPaginate();
 }
Example #18
0
File: model.php Project: LWFeng/xnx
{
    // fields
    public $id;
    public $name;
    public function initialize()
    {
        // $this->setSource("the_robots");
    }
}
// Find record with id = 3
$robot = Robots::findFirst(3);
// *** paginator ***
use Phalcon\Paginator\Adapter\Model as ModelPaginator;
use Phalcon\Paginator\Adapter\NativeArray as ArrayPaginator;
use Phalcon\Paginator\Adapter\QueryBuilder as BuildPaginator;
//Passing a resultset as data
$paginator = new ModelPaginator(array("data" => Products::find(), "limit" => 10, "page" => $currentPage));
//Passing an array as data
$paginator = new ArrayPaginator(array("data" => array(array('id' => 1, 'name' => 'Artichoke'), array('id' => 2, 'name' => 'Carrots'), array('id' => 3, 'name' => 'Beet'), array('id' => 4, 'name' => 'Lettuce'), array('id' => 5, 'name' => '')), "limit" => 2, "page" => $currentPage));
$builder = $this->modelsManager->createBuilder()->from("App\\Models\\Article")->where($where)->orderBy("create_time DESC");
$paginator = new BuildPaginator(array("builder" => $builder, "limit" => 5, "page" => $this->request->queryPage()));
$dataset = $paginator->getPaginate();
/*
items       The set of records to be displayed at the current page
current     The current page
before      The previous page to the current one
next        The next page to the current one
last        The last page in the set of records
total_pages The number of pages
total_items The number of items in the source data
//*/
 public function listAction()
 {
     $user = $this->session->get('auth');
     $id = $this->dispatcher->getParam('id');
     $mission = $id ? Model\Mission::findFirst(array('id = :mid: AND active = 1', 'bind' => array('mid' => $id))) : Model\Mission::findFirst(array('active = 1'));
     if (!$mission) {
         $this->flashSession->error('Sorry, no active missions at this time');
         return $this->response->redirect('user');
     }
     $add_form = new MissionForm\AddToList();
     $wait_list = new Model\WaitList();
     if ($this->request->isPost()) {
         $add_form->bind($this->request->getPost(), $wait_list);
         if ($add_form->isValid()) {
             $wait_list->mission_id = $mission->id;
             $wait_list->user_id = $user->id;
             $wait_list->character_id = $wait_list->fit->character->id;
             $wait_list->waiting = 1;
             $wait_list->save();
             $this->flashSession->success('You have been added to the list');
             return $this->response->redirect('mission/list/' . $mission->id);
         }
     }
     //pull all the relevant data for sorting the results out of the url
     $meta = new PaginationMeta($this->dispatcher, array('cid' => 'character_id', 'fid' => 'fit_id', 'date' => 'date_created'), array('order_by' => 'date', 'order' => 0, 'limit' => 10));
     $meta->baseLink = $id ? "mission/list/{$id}" : "mission/list";
     $builder = $this->modelsManager->createBuilder()->from('Eve\\Model\\WaitList')->where('mission_id = :mid: AND waiting = 1', array('mid' => $mission->id))->orderBy($meta->query);
     //build the paginator
     $paginator = new Paginator\Adapter\QueryBuilder(array('builder' => $builder, 'limit' => $meta->limit, 'page' => $meta->page));
     $this->view->title = $mission->name;
     $this->view->mission = $mission;
     $this->view->page = $paginator->getPaginate();
     $this->view->page->meta = $meta;
     $this->view->add_form = $add_form;
     $this->view->mission_form = new MissionForm\Selector();
 }