Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding plugin files and documentation

  • Loading branch information...
commit 49ff29a935a5f617f2f347dd954651068cb723a2 1 parent 940c76f
Josh Lockhart authored
View
14 PaginationPlugin/PaginationPlugin.class.php
@@ -0,0 +1,14 @@
+<?php
+
+Library::import('recess.framework.Plugin');
+Library::import('PaginationPlugin.annotations.PaginateAnnotation');
+
+class PaginationPlugin extends Plugin {
+
+ function init( Application $app ) {
+ //Do nothing. Only need to import the annotations and wrappers.
+ }
+
+}
+
+?>
View
77 PaginationPlugin/annotations/PaginateAnnotation.class.php
@@ -0,0 +1,77 @@
+<?php
+/**
+ * Recess Framework Model Pagination Plugin
+ *
+ * Easily paginate your Models
+ *
+ * @author Josh Lockhart <info@joshlockhart.com>
+ * @link http://code.joshlockhart.com/recess/pagination/
+ * @copyright 2010 Josh Lockhart
+ *
+ * MIT LICENSE
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+Library::import('recesss.lang.Annotation');
+Library::import('PaginationPlugin.models.PaginatorDelegate');
+
+/**
+ * Pagination Annotation
+ *
+ * This annotation attaches the paginate() method to a Model object.
+ *
+ * USAGE:
+ *
+ * !Paginate Limit: 5
+ *
+ * Limit:
+ * - Optional
+ * - Determines the number of results to show per page
+ *
+ * @author Josh Lockhart <info@joshlockhart.com>
+ * @since Verson 1.0
+ */
+class PaginateAnnotation extends Annotation {
+
+ public function usage() {
+ return '!Paginate Limit:25';
+ }
+
+ public function isFor() {
+ return Annotation::FOR_CLASS;
+ }
+
+ protected function validate($class) {
+ $this->acceptsNoKeylessValues();
+ $this->acceptedKeys(array('limit'));
+ $this->validOnSubclassesOf($class,'Model');
+ }
+
+ protected function expand($class, $reflection, $descriptor) {
+ $delegate = new PaginatorDelegate();
+ $delegate->perPage = isset($this->limit) ? intval($this->limit) : 25;
+ $descriptor->attachMethod($class, 'paginate', $delegate, 'paginate');
+ return $descriptor;
+ }
+
+}
+
+?>
View
93 PaginationPlugin/models/Paginator.class.php
@@ -0,0 +1,93 @@
+<?php
+/**
+ * Recess Framework Model Pagination Plugin
+ *
+ * Easily paginate your Models
+ *
+ * @author Josh Lockhart <info@joshlockhart.com>
+ * @link http://code.joshlockhart.com/recess/pagination/
+ * @copyright 2010 Josh Lockhart
+ *
+ * MIT LICENSE
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+/**
+ * Paginator
+ *
+ * This class provides the actual pagination computation and helper methods.
+ * A paginator is assigned as a property on the current ModelSet object.
+ * So you can easily extract this paginator from the ModelSet, pass it to
+ * your View, and use it to create pagination links.
+ *
+ * This object was inspired by John Nunemaker's Plucky paginator, also on
+ * GitHub at http://github.com/jnunemaker/plucky/ (Copyright John Nunemaker).
+ *
+ * @author Josh Lockhart <info@joshlockhart.com>
+ * @since Verson 1.0
+ */
+class Paginator {
+
+ private $totalEntries;
+ private $currentPage;
+ private $perPage;
+
+ public function __construct($total, $page, $perPage = null) {
+ $this->totalEntries = intval($total);
+ $this->currentPage = max(array(intval($page), 1));
+ $this->perPage = ( is_null($perPage) ) ? 25 : intval($perPage);
+ }
+
+ public function totalEntries() {
+ return $this->totalEntries;
+ }
+
+ public function currentPage() {
+ return $this->currentPage;
+ }
+
+ public function perPage() {
+ return $this->perPage;
+ }
+
+ public function totalPages() {
+ return ceil($this->totalEntries / $this->perPage);
+ }
+
+ public function outOfBounds() {
+ return $this->currentPage > $this->totalPages();
+ }
+
+ public function previousPage() {
+ return ( $this->currentPage > 1 ) ? $this->currentPage - 1 : null;
+ }
+
+ public function nextPage() {
+ return ( $this->currentPage < $this->totalPages() ) ? $this->currentPage + 1 : null;
+ }
+
+ public function offset() {
+ return ( $this->currentPage - 1 ) * $this->perPage;
+ }
+
+}
+
+?>
View
60 PaginationPlugin/models/PaginatorDelegate.class.php
@@ -0,0 +1,60 @@
+<?php
+/**
+ * Recess Framework Model Pagination Plugin
+ *
+ * Easily paginate your Models
+ *
+ * @author Josh Lockhart <info@joshlockhart.com>
+ * @link http://code.joshlockhart.com/recess/pagination/
+ * @copyright 2010 Josh Lockhart
+ *
+ * MIT LICENSE
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+Library::import('PaginationPlugin.models.Paginator');
+
+/**
+ * Paginator Delegate
+ *
+ * This class provides the attachable method which is attached to
+ * the ModelSet. It instantatites a Paginator object, assigns it to
+ * the ModelSet, and uses it to filter the ModelSet query based on
+ * the current requested page and per-page limit.
+ *
+ * @author Josh Lockhart <info@joshlockhart.com>
+ * @since Verson 1.0
+ */
+class PaginatorDelegate {
+
+ public $perPage = 25;
+
+ public function paginate( ModelSet $modelSet ) {
+ $page = ( isset($_REQUEST['page']) ) ? intval($_REQUEST['page']) : 1;
+ $limit = $this->perPage;
+ $total = $modelSet->count();
+ $modelSet->paginator = new Paginator($total, $page, $limit);
+ return $modelSet->limit( $modelSet->paginator->perPage() )->offset( $modelSet->paginator->offset() );
+ }
+
+}
+
+?>
View
62 README
@@ -0,0 +1,62 @@
+Recess Framework Model Pagination Plugin
+
+Thank you for your interest in my pagination plugin! This plugin is released
+under the MIT public license. This plugin enables you to effortlessly
+paginate ModelSets in your controllers. This plugin was based on the Plucky
+module by John Nunemaker.
+
+How to Install:
+---------------
+
+Download the Pagination Plugin and place the "PaginationPlugin/" directory
+into your Recess installation's "plugins/" directory.
+
+Update your Recess application's class file. Import the "PaginationPlugin"
+plugin. Then register the PaginationPlugin with your application. Ultimately,
+your application class file should look something like this:
+
+<?php
+Library::import('recess.framework.Application');
+Library::import('PaginationPlugin.PaginationPlugin');
+
+class MyApplication extends Application {
+ public function __construct() {
+ $this->name = 'My Cool Application';
+ $this->viewsDir = $_ENV['dir.apps'] . 'myapp/templates/';
+ $this->modelsPrefix = 'myapp.models.';
+ $this->controllersPrefix = 'myapp.controllers.';
+ $this->routingPrefix = '/';
+ $this->plugins = array(
+ new PaginationPlugin()
+ );
+ }
+}
+?>
+
+Example Model:
+--------------
+
+/**
+ * This model, when paginated, will show 5 results per page
+ * !Paginate Limit: 5
+ */
+class Book extends Model {
+
+}
+
+Example Controller:
+-------------------
+
+/**
+ * !Route GET, books
+ */
+function listBooks() {
+ $book = new Book();
+ $books = $book->equals('genre', 'adventure')->orderBy('published_at DESC')->paginate();
+ $paginator = $books->paginator;
+}
+
+Other Tips:
+-----------
+
+To request the "second page" of results, append "?page=2" to your request URL.
Please sign in to comment.
Something went wrong with that request. Please try again.