Author: fizyk
Date: 2010-03-13 21:34:20 +0100 (Sat, 13 Mar 2010)
New Revision: 28518

Added:
   plugins/fzDoctrinePagedRoutePlugin/trunk/LICENSE
   plugins/fzDoctrinePagedRoutePlugin/trunk/README
   plugins/fzDoctrinePagedRoutePlugin/trunk/lib/
   plugins/fzDoctrinePagedRoutePlugin/trunk/lib/routing/
   
plugins/fzDoctrinePagedRoutePlugin/trunk/lib/routing/fzDoctrineListRouteCollection.class.php
   plugins/fzDoctrinePagedRoutePlugin/trunk/lib/routing/fzDoctrinePagedRoute.php
   plugins/fzDoctrinePagedRoutePlugin/trunk/package.xml.tmpl
Log:
initial upload


Added: plugins/fzDoctrinePagedRoutePlugin/trunk/LICENSE
===================================================================
--- plugins/fzDoctrinePagedRoutePlugin/trunk/LICENSE                            
(rev 0)
+++ plugins/fzDoctrinePagedRoutePlugin/trunk/LICENSE    2010-03-13 20:34:20 UTC 
(rev 28518)
@@ -0,0 +1,7 @@
+Copyright (c) Grzegorz Śliwiński
+
+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.

Added: plugins/fzDoctrinePagedRoutePlugin/trunk/README
===================================================================
--- plugins/fzDoctrinePagedRoutePlugin/trunk/README                             
(rev 0)
+++ plugins/fzDoctrinePagedRoutePlugin/trunk/README     2010-03-13 20:34:20 UTC 
(rev 28518)
@@ -0,0 +1,125 @@
+    #fzDoctrinePagedRoutePlugin#
+
+Plugin created to ease creation and management and creating of frontend routes 
that usually need only list (index) and show pages to the user.
+
+Biggest advantage is that you actually **can't** visit page that will show no 
items on a list *(higher list-page number than elemts/elements_per_page or 
lower than 1)*, and you'll get 404 HTTP response instead.
+
+##Requirements##
+
+*Doctrine
+
+Plugin is working with symfony 1.4 and should be working with 1.3 as well. It 
needs doctrine, as it utilises Doctrine Pager.
+
+##Installation##
+
+To install fzDoctrinePagedRoutePlugin simply execute plugin:install commend:
+
+    ./symfony plugin:install fzDoctrinePagedRoutePlugin
+
+you can install downloaded package as well:
+
+    ./symfony plugin:install fzDoctrinePagedRoutePlugin.tgz
+
+just place downloaded package in your project's root first.
+
+You can also install it manually, unpacking archiwe, placing it's content in 
your
+project's plugin/ directory, and enabling it in your 
ProjectConfiguration.class.php file:
+
+config/ProjectConfiguration.class.php
+
+    class ProjectConfiguration extends sfProjectConfiguration
+    {
+        //....//
+        public function setup()
+        {
+            //....//
+            $this->enablePlugins('fzDoctrinePagedRoutePlugin');
+            //....//
+        }
+    }
+
+
+
+##Usage##
+
+Plugin contains one route class: **fzDoctrinePagedRoute** and one route 
collection class: **fzDoctrineLisPagedRouteCollection**.
+
+You **can** set two settings in your "app.yml" page:
+
+    all:
+      #...
+      fzDoctrinePagedRoutePlugin:
+        page_indicator: page_name_for_url
+        per_page: 5
+
+* page_indicator setting is a word that will be used in list (index) url, like 
that: /url/to/your/list/**page_name_for_url**/page_number
+* per_page is number (or your own setting name from app.yml tha contains 
number) of elements shown on a list per page.
+
+###Single route definition###
+
+The **fzDoctrinePagedRoute** one servers to create one route for paged list in 
**routing.yml** file.
+
+    route_name:
+      class: fzDoctrinePagedRoute
+      url:   /url/to/your/list/:page
+      param: { module: blog, action: index }
+      options:
+        model: Model
+        browse_method: getElements
+        per_page: app_max_per_page
+
+There are three mandatory options:
+
+* model, that tells route class which model should it use for pager,
+* browse_method, which tells which method should it use to create list and 
pages
+* per_page which tells how many items should be used per page, it overrides 
setting from **app.yml** if set, and can be omitted if set.
+
+###Routes collection definition###
+
+The **fzDoctrineLisPagedRouteCollection** is a collection route class that 
creates three routes:
+
+* clean, without page indicator (default for 1st page of list, index action, 
fzDoctrinePagedRoute type).
+* _paged, which uses page_indicator and page number word at the end (index 
action, fzDoctrinePagedRoute type)
+* _show which is used to show one element and directs to the module (show 
action, sfDoctrineRoute type).
+
+Should be set like that:
+
+    route_collection_name:
+      class: fzDoctrineListRouteCollection
+      options:
+        module: module_name
+        model: Model
+        type: object
+        browse_method: getElements
+        per_page: app_max_per_page
+        page_indicator: page_name_for_url
+        show_pattern: :id/:slug
+
+Three new options are mandatory:
+
+* module - passes module_name which will be used as a parameter for the route
+* page_indicator - overrides page_indicator setting from app.yml. It isn't 
mandatorym and falls back just to the word page which gets append before page 
number.
+* show_pattern - defines pattern that should be used for _show route, just as 
you'd set a route for sfDoctrineRoute (since it creates sfDoctrineRoute).
+
+The model, browse_method and per_page options are used the same way as in 
**fzDoctrinePagedRoute**.
+
+
+###Action usage###
+
+fzDoctrinePagedRoute provides *getPager()* method that returns 
**DoctrinePager** defined and ready to use in your template.
+
+    class moduleActions extends sfActions
+    {
+
+        public function executeIndex(sfWebRequest $request)
+        {
+            $this->pager = $this->getRoute()->getPager();
+        }
+        //....//
+    }
+
+
+##Changelog##
+
+###Version 1.0.0###
+*fizyk: plugin creation
\ No newline at end of file

Added: 
plugins/fzDoctrinePagedRoutePlugin/trunk/lib/routing/fzDoctrineListRouteCollection.class.php
===================================================================
--- 
plugins/fzDoctrinePagedRoutePlugin/trunk/lib/routing/fzDoctrineListRouteCollection.class.php
                                (rev 0)
+++ 
plugins/fzDoctrinePagedRoutePlugin/trunk/lib/routing/fzDoctrineListRouteCollection.class.php
        2010-03-13 20:34:20 UTC (rev 28518)
@@ -0,0 +1,133 @@
+<?php
+/**
+ *
+ * fzListRouteCollection generates three routes for modules: default, paged 
and show.
+ * It doesn't follow CRUD, as it's not always necessary to create CRUD route 
set for some lists,
+ * but still great to have them generated.<br/>
+ * 
+ * @author fizyk
+ */
+class fzDoctrineListRouteCollection extends sfRouteCollection
+{
+    private $defaults;
+
+    /**
+     * fzListRouteCollection generates three routes for modules: default, 
paged and show.
+     * It doesn't follow CRUD, as it's not always necessary to create CRUD 
route set for some lists,
+     * but still great to have them generated.<br/>
+     * It utilises those options:<br/>
+     * <b>model:</b> name of the model used in given list<br/>
+     * <b>type:</b> type of the model, typically: object<br/>
+     * <b>page_indicator:</b> name for another pages used like that: 
/prefix/page_indicator/[page_number]<br/>
+     * <b>show_pattern:</b> usual pattern used for show, default is 
:id<br/><br/>
+     * This colelction uses fzDoctrinePagedRoute for default and paged routes,
+     * so be sure to include it's required options as well.
+     * @param array $options
+     */
+    public function  __construct( array $options )
+    {
+        parent::__construct($options);
+
+        $this->checkOptions();
+        $this->setDefaults();
+        $this->generateRoutes();
+    }
+
+
+    private function checkOptions()
+    {
+        //If prefix path's not set, we'll use Collection's name
+        if( !isset( $this->options['prefix_path'] ) )
+        {
+            $this->options['prefix_path'] = $this->options[ 'name' ];
+        }
+        
+        //Default action will be index as well
+        if( !isset( $this->options['action']) )
+        {
+            $this->options['action'] = 'index';
+        }
+
+        //Default show_pattern will be page
+        if( !isset( $this->options['page_indicator'] ) )
+        {
+            if( null === ( $this->options['page_indicator'] = 
+                    sfConfig::get( 
'app_fzDoctrinePagedRoutePlugin_page_indicator', null) ) )
+            {
+                $this->options['page_indicator'] = 'page';
+            }
+        }
+
+        //Default page indicator will be :id
+        if( !isset( $this->options['show_pattern'] ) )
+        {
+            $this->options['show_pattern'] = ':id';
+        }
+
+        //Default module will be set as route name.
+        if( !isset( $this->options['module'] ) )
+        {
+            $this->options['module'] = $this->options['name'];
+        }
+        
+    }
+
+    private function setDefaults()
+    {
+        $this->defaults = array(
+           'module' => $this->options['module'],
+           'action' => $this->options['action'],
+           'sf_format' => 'html'
+        );
+    }
+    
+    protected function generateRoutes()
+    {
+        $this->routes[ $this->options['name'] ] = $this->getDefaultRoute();
+        $this->routes[ $this->options['name'].'_paged' ] = 
$this->getPagedRoute();
+        $this->routes[ $this->options['name'].'_show' ] = 
$this->getShowRoute();
+    }
+
+    private function getDefaultRoute()
+    {
+        $pattern = sprintf(
+           '%s.:sf_format',
+           $this->options['prefix_path']
+        );
+        $requirements = array('sf_method' => 'get');
+        return new fzDoctrinePagedRoute($pattern, $this->defaults, 
$requirements, $this->options);
+    }
+
+    private function getPagedRoute()
+    {
+        $pattern = sprintf(
+           '%s/%s/%s',
+           $this->options['prefix_path'],
+           $this->options['page_indicator'],
+           ':page'
+        );
+        $requirements = array('sf_method' => 'get');
+        return new fzDoctrinePagedRoute($pattern, $this->defaults, 
$requirements, $this->options);
+    }
+
+    public function getShowRoute()
+    {
+        $pattern = sprintf(
+           '%s/%s',
+           $this->options['prefix_path'],
+           $this->options['show_pattern']
+        );
+
+        $defaults = array(
+           'module' => $this->options['module'],
+           'action' => 'show'
+        );
+        $requirements = array(
+            'sf_method' => 'get',
+            'id' => '\d+'
+        );
+        
+        return new sfDoctrineRoute( $pattern, $defaults, $requirements, 
$this->options );
+    }
+}
+?>

Added: 
plugins/fzDoctrinePagedRoutePlugin/trunk/lib/routing/fzDoctrinePagedRoute.php
===================================================================
--- 
plugins/fzDoctrinePagedRoutePlugin/trunk/lib/routing/fzDoctrinePagedRoute.php   
                            (rev 0)
+++ 
plugins/fzDoctrinePagedRoutePlugin/trunk/lib/routing/fzDoctrinePagedRoute.php   
    2010-03-13 20:34:20 UTC (rev 28518)
@@ -0,0 +1,112 @@
+<?php
+/**
+ * fzDoctrinePagedRoute represents a route that is bound to a doctrine class 
and Pager.
+ * It doesn't extends sfDoctrineRoute nor sfObjectRoute, becouse we don't want 
to
+ * create models object as it. We do want to get it's list for Pager however.
+ *
+ * @author fizyk
+ */
+class fzDoctrinePagedRoute extends sfRoute
+{
+    private $pager;
+    private $items_per_page = 0;
+
+    /**
+     * fzDoctrinePagedRoute represents a route that is bound to a doctrine 
class and Pager.
+     * It doesn't extends sfDoctrineRoute nor sfObjectRoute, becouse we don't 
want to
+     * create models object as it. We do want to get it's list for Pager 
however.
+     * aditionaly to parent's class, required options are:<br/>
+     * <b>model:</b> model name used on given page<br/>
+     * <b>browse_method:</b> method used to get objects for list (for 
pager)<br/>
+     * <b>per_page:</b> number of items per page, or setting name from app.yml 
with that number
+     * @param string $pattern
+     * @param array $defaults
+     * @param array $requirements
+     * @param array $options
+     */
+    public function __construct( $pattern, array $defaults, array 
$requirements, array $options )
+    {
+        //parent's construction, we just add our stuff below
+        parent::__construct( $pattern, $defaults, $requirements, $options );
+
+        //Checking requirements below:
+        if( !isset( $this->options['model'] ) )
+        {
+            throw new InvalidArgumentException(sprintf('You must pass a 
"model" option to %s ("%s" route)', get_class($this), $pattern ) );
+        }
+        if( !isset( $this->options['browse_method'] ) )
+        {
+            throw new InvalidArgumentException(sprintf('You must pass a 
"browse_method" option to %s ("%s" route)', get_class($this), $pattern ) );
+        }
+        if( !isset( $this->options['per_page'] ) )
+        {
+            if( null === ( $this->options['per_page'] = sfConfig::get( 
'app_fzDoctrinePagedRoutePlugin_per_page' , null ) ) )
+            {
+                throw new InvalidArgumentException( 
+                        sprintf('You must pass a "per_page" option to %s ("%s" 
route), or set app_fzDoctrinePagedRoutePlugin_per_page setting in your app.yml 
file!',
+                                get_class($this), $pattern ) );
+            }
+        }
+    }
+
+    /**
+     *
+     * @param mixed $url
+     * @param array $context
+     * @return boolean
+     * false if there's no match (or page number too far), otherwise an array 
of parameters
+     */
+    public function matchesUrl($url, $context = array())
+    {
+        //Parent's match first:
+        if (false === $parameters = parent::matchesUrl($url, $context))
+        {
+            return false;
+        }
+        //setting default page value of 1, because it can be
+        if( !isset( $parameters['page'] ) )
+        {
+            $parameters['page'] = 1;
+        }
+        //Getting page number:
+        if( is_numeric( $this->options['per_page'] ) )
+        {
+            $this->items_per_page = $this->options['per_page'];
+        }
+        else
+        {
+           if(! is_integer( $this->items_per_page = sfConfig::get( 
$this->options['per_page'] ) ) )
+           {
+               throw new InvalidArgumentException( 'Your per_page 
option/setting must provide integer value!' );
+           }
+        }
+
+        //getting browse_method name
+        $browse_method = $this->options['browse_method'];
+
+        //creating pager
+        $this->pager = new sfDoctrinePager( $this->options['model'] , 
$this->items_per_page );
+        $this->pager->setQuery( Doctrine::getTable( $this->options['model'] 
)->$browse_method() );
+        $this->pager->setPage( $parameters['page'] );
+        $this->pager->init();
+
+        //Obviously, if page is out of reach, we shouldn't generate it
+        if( $this->pager->getLastPage() < $parameters['page'] || 
$parameters['page'] < 1 )
+        {
+            return false;
+        }
+        
+        return $parameters;
+    }
+
+    /**
+     * Returns sfDoctrinePager object used in this route, same way the 
getObject from sfObjectRoute, becouse of DRY rule.
+     * @return sfDoctrinePager
+     */
+    public function getPager()
+    {
+        return $this->pager;
+    }
+
+}
+?>

Added: plugins/fzDoctrinePagedRoutePlugin/trunk/package.xml.tmpl
===================================================================
--- plugins/fzDoctrinePagedRoutePlugin/trunk/package.xml.tmpl                   
        (rev 0)
+++ plugins/fzDoctrinePagedRoutePlugin/trunk/package.xml.tmpl   2010-03-13 
20:34:20 UTC (rev 28518)
@@ -0,0 +1,79 @@
+<?xml version="1.0" encoding="##ENCODING##"?>
+<package xmlns="http://pear.php.net/dtd/package-2.0"; 
xmlns:tasks="http://pear.php.net/dtd/tasks-1.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; packagerversion="1.4.1" 
version="2.0" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 
http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 
http://pear.php.net/dtd/package-2.0.xsd";>
+  <name>fzDoctrinePagedRoutePlugin</name>
+  <channel>plugins.symfony-project.org</channel>
+  <summary>Makes it easy to create friendly urls for paged lists.</summary>
+  <description>Provides route and route collection classes which makes it easy 
to create routes for paged lists and their elements. Additionally it only 
passes pages that will have elements on, giving 404 HTTP response for the 
others</description>
+  <lead>
+    <name>Grzegorz Śliwiński</name>
+    <user>fizyk</user>
+    <email>[email protected]</email>
+    <active>yes</active>
+  </lead>
+  <date>##CURRENT_DATE##</date>
+  <version>
+    <release>##PLUGIN_VERSION##</release>
+    <api>##API_VERSION##</api>
+  </version>
+  <stability>
+    <release>##STABILITY##</release>
+    <api>##STABILITY##</api>
+  </stability>
+  <license uri="http://www.symfony-project.org/license";>MIT license</license>
+  <notes>-</notes>
+  <contents>
+    ##CONTENTS##
+  </contents>
+  <dependencies>
+    <required>
+      <php>
+        <min>5.2.4</min>
+      </php>
+      <pearinstaller>
+        <min>1.4.1</min>
+      </pearinstaller>
+      <package>
+        <name>symfony</name>
+        <channel>pear.symfony-project.com</channel>
+        <min>1.3.0</min>
+        <max>1.5.0</max>
+        <exclude>1.5.0</exclude>
+      </package>
+    </required>
+  </dependencies>
+  <phprelease></phprelease>
+    <changelog>
+      <release>
+        <version>
+          <release>1.0.1</release>
+          <api>1.0.0</api>
+        </version>
+        <stability>
+         <release>stable</release>
+         <api>stable</api>
+        </stability>
+        <license uri="http://www.symfony-project.org/license";>MIT 
license</license>
+        <date>2010-03-??</date>
+        <license>MIT</license>
+        <notes>
+          * fizyk: fixed typo in description
+        </notes>
+      </release>
+      <release>
+        <version>
+          <release>1.0.0</release>
+          <api>1.0.0</api>
+        </version>
+        <stability>
+         <release>stable</release>
+         <api>stable</api>
+        </stability>
+        <license uri="http://www.symfony-project.org/license";>MIT 
license</license>
+        <date>2010-03-13</date>
+        <license>MIT</license>
+        <notes>
+          * fizyk: first release
+        </notes>
+      </release>
+    </changelog>
+</package>

-- 
You received this message because you are subscribed to the Google Groups 
"symfony SVN" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/symfony-svn?hl=en.

Reply via email to