Skip to content

Commit

Permalink
fix pagination for composer based apps
Browse files Browse the repository at this point in the history
  • Loading branch information
basemkhirat committed Feb 19, 2017
1 parent 6135d46 commit bcba752
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 9 deletions.
42 changes: 42 additions & 0 deletions src/Pagination.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Basemkhirat\Elasticsearch;

use Illuminate\Pagination\LengthAwarePaginator;

class Pagination extends LengthAwarePaginator
{

/**
* Render the paginator using the given view.
*
* @param string $view
* @param array $data
* @return string
*/
public function links($view = "default", $data = [])
{
return $this->render($view, $data);
}

/**
* Render the paginator using the given view.
*
* @param string $view
* @param array $data
* @return string
*/
public function render($view = "default", $data = [])
{

extract($data);

$paginator = $this;

$elements = $this->elements();

require dirname(__FILE__) . "/pagination/" . $view . ".php";

}

}
21 changes: 12 additions & 9 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

namespace Basemkhirat\Elasticsearch;

use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Request;

/**
* Class Query
Expand Down Expand Up @@ -331,7 +328,6 @@ protected function getTake()
*/
public function skip($skip = 0)
{

$this->skip = $skip;

return $this;
Expand Down Expand Up @@ -810,6 +806,11 @@ protected function getResult($scroll_id)
}


/**
* Get non cached results
* @param null $scroll_id
* @return mixed
*/
protected function getLiveResult($scroll_id = NULL)
{

Expand Down Expand Up @@ -905,22 +906,24 @@ protected function getFirst($result = [])
}

/**
* Paginate the collection of results
* Paginate collection of results
* @param int $per_page
* @return LengthAwarePaginator
* @param $page_name
* @param null $page
* @return Pagination
*/
public function paginate($per_page = 10)
public function paginate($per_page = 10, $page_name = "page", $page = null)
{

$this->take($per_page);

$page = (int)Request::get('page', 1);
$page = $page ?: Request::get($page_name, 1);

$this->skip(($page * $per_page) - $per_page);

$objects = $this->get();

return new LengthAwarePaginator($objects, $objects->total, $per_page, Request::get("page"), ['path' => Request::url(), 'query' => Request::query()]);
return new Pagination($objects, $objects->total, $per_page, $page, ['path' => Request::url(), 'query' => Request::query()]);

}

Expand Down
58 changes: 58 additions & 0 deletions src/Request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Basemkhirat\Elasticsearch;

/**
* Class Request
* @package Basemkhirat\Elasticsearch
*/
class Request
{

/**
* get the request url
* @return string
*/
public static function url()
{

$server = $_SERVER;

$ssl = (!empty($server['HTTPS']) && $server['HTTPS'] == 'on');

$sp = strtolower($server['SERVER_PROTOCOL']);

$protocol = substr($sp, 0, strpos($sp, '/')) . (($ssl) ? 's' : '');

$port = $server['SERVER_PORT'];

$port = ((!$ssl && $port == '80') || ($ssl && $port == '443')) ? '' : ':' . $port;

$host = isset($host) ? $host : $server['SERVER_NAME'] . $port;

$host .= preg_replace("/\?.*/", "", $server["REQUEST_URI"]);

return $protocol . '://' . $host;
}

/**
* get all query string parameters
* @return mixed
*/
public static function query()
{
return $_GET;
}

/**
* get value of query string parameter
* @param $name
* @param null $value
* @return null
*/
public static function get($name, $value = NULL)
{
return isset($_GET[$name]) ? $_GET[$name] : $value;
}

}
36 changes: 36 additions & 0 deletions src/pagination/bootstrap-4.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php if ($paginator->hasPages()) { ?>
<ul class="pagination">
<?php if ($paginator->onFirstPage()) { ?>
<li class="page-item disabled"><span class="page-link">&laquo;</span></li>
<?php } else { ?>
<li class="page-item"><a class="page-link" href="<?php echo $paginator->previousPageUrl() ?>"
rel="prev">&laquo;</a></li>
<?php } ?>

<?php foreach ($elements as $element) { ?>

<?php if (is_string($element)) { ?>
<li class="page-item disabled"><span class="page-link"><?php echo $element ?></span></li>
<?php } ?>

<?php if (is_array($element)) { ?>
<?php foreach ($element as $page => $url) { ?>
<?php if ($page == $paginator->currentPage()) { ?>
<li class="page-item active"><span class="page-link"><?php echo $page ?></span></li>
<?php } else { ?>
<li class="page-item"><a class="page-link" href="<?php echo $url ?>"><?php echo $page ?></a>
</li>
<?php } ?>
<?php } ?>
<?php } ?>
<?php } ?>


<?php if ($paginator->hasMorePages()) { ?>
<li class="page-item"><a class="page-link" href="<?php echo $paginator->nextPageUrl() ?>"
rel="next">&raquo;</a></li>
<?php } else { ?>
<li class="page-item disabled"><span class="page-link">&raquo;</span></li>
<?php } ?>
</ul>
<?php } ?>
33 changes: 33 additions & 0 deletions src/pagination/default.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php if ($paginator->hasPages()) { ?>
<ul class="pagination">

<?php if ($paginator->onFirstPage()) { ?>
<li class="disabled"><span>&laquo;</span></li>
<?php } else { ?>
<li><a href="<?php echo $paginator->previousPageUrl() ?>" rel="prev">&laquo;</a></li>
<?php } ?>

<?php foreach ($elements as $element) { ?>

<?php if (is_string($element)) { ?>
<li class="disabled"><span><?php echo $element ?></span></li>
<?php } ?>

<?php if (is_array($element)) { ?>
<?php foreach ($element as $page => $url) { ?>
<?php if ($page == $paginator->currentPage()) { ?>
<li class="active"><span><?php echo $page ?></span></li>
<?php } else { ?>
<li><a href="<?php echo $url ?>"><?php echo $page ?></a></li>
<?php } ?>
<?php } ?>
<?php } ?>
<?php } ?>

<?php if ($paginator->hasMorePages()) { ?>
<li><a href="<?php echo $paginator->nextPageUrl() ?>" rel="next">&raquo;</a></li>
<?php } else { ?>
<li class="disabled"><span>&raquo;</span></li>
<?php } ?>
</ul>
<?php } ?>
17 changes: 17 additions & 0 deletions src/pagination/simple-bootstrap-4.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php if ($paginator->hasPages()){ ?>
<ul class="pagination">

<?php if ($paginator->onFirstPage()){ ?>
<li class="page-item disabled"><span class="page-link">&laquo;</span></li>
<?php }else{ ?>
<li class="page-item"><a class="page-link" href="<?php echo $paginator->previousPageUrl() ?>" rel="prev">&laquo;</a></li>
<?php } ?>

<?php if ($paginator->hasMorePages()){ ?>
<li class="page-item"><a class="page-link" href="<?php echo $paginator->nextPageUrl() ?>" rel="next">&raquo;</a></li>
<?php }else{ ?>
<li class="page-item disabled"><span class="page-link">&raquo;</span></li>
<?php } ?>

</ul>
<?php } ?>
15 changes: 15 additions & 0 deletions src/pagination/simple-default.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php if ($paginator->hasPages()) { ?>
<ul class="pagination">
<?php if ($paginator->onFirstPage()) { ?>
<li class="disabled"><span>&laquo;</span></li>
<?php } else { ?>
<li><a href="<?php echo $paginator->previousPageUrl() ?>" rel="prev">&laquo;</a></li>
<?php } ?>

<?php if ($paginator->hasMorePages()) { ?>
<li><a href="<?php echo $paginator->nextPageUrl() ?>" rel="next">&raquo;</a></li>
<?php } else { ?>
<li class="disabled"><span>&raquo;</span></li>
<?php } ?>
</ul>
<?php } ?>

0 comments on commit bcba752

Please sign in to comment.