Author: Leon.van.der.Ree
Date: 2010-04-14 00:10:18 +0200 (Wed, 14 Apr 2010)
New Revision: 29131
Added:
plugins/sfGridPlugin/trunk/lib/grid/sfContextGrid.class.php
plugins/sfGridPlugin/trunk/lib/routing/
plugins/sfGridPlugin/trunk/lib/routing/sfGridRoute.php
Modified:
plugins/sfGridPlugin/trunk/lib/grid/sfGrid.class.php
plugins/sfGridPlugin/trunk/lib/grid/sfWebGrid.class.php
plugins/sfGridPlugin/trunk/lib/grid/sfWebGridJavaScript.class.php
plugins/sfGridPlugin/trunk/test/unit/mock/sfDataSourceMock.class.php
Log:
updated interfaces of grids, adding routing-class for grids
Added: plugins/sfGridPlugin/trunk/lib/grid/sfContextGrid.class.php
===================================================================
--- plugins/sfGridPlugin/trunk/lib/grid/sfContextGrid.class.php
(rev 0)
+++ plugins/sfGridPlugin/trunk/lib/grid/sfContextGrid.class.php 2010-04-13
22:10:18 UTC (rev 29131)
@@ -0,0 +1,119 @@
+<?php
+
+/*
+ * This file is part of the symfony package.
+ * (c) Leon van der Ree <[email protected]>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+class sfContextGrid extends sfGrid
+{
+ /**
+ * the namespace addition for this grid,
+ * to store attributes like: sort, type and page in the context
+ *
+ * @var string
+ */
+ protected $namespaceAddition = '';
+
+ /**
+ *
+ * @var sfContext
+ */
+ protected $context;
+
+ /**
+ * Adds context awareness to the grid, to store attributes like: sort, type
and page.
+ *
+ * @see sfGrid::__construct
+ */
+ public function __construct($source)
+ {
+ $this->context = sfContext::getInstance();
+
+ parent::__construct($source);
+ }
+
+ /**
+ * configures the context awareness
+ *
+ * @see sfGrid::configure
+ */
+ public function configure()
+ {
+ parent::configure();
+
+ $user = $this->context->getUser();
+
+ $this->setUri('@'.$this->context->getRouting()->getCurrentRouteName());
+
+ $page = $user->getAttribute('page', null, $this->getNamespace());
+ if ($page != null)
+ {
+ $this->getPager()->setPage($page);
+ }
+
+ $sort = $user->getAttribute('sort', null, $this->getNamespace());
+ if ($sort != null)
+ {
+ $type = $user->getAttribute('type', null, $this->getNamespace());
+ $this->setSort($sort, $type);
+ }
+ }
+
+ /**
+ * adds context-awareness to the grid-page functionality
+ *
+ * @see sfGrid::setPage
+ */
+ public function setPage($page)
+ {
+ parent::setPage($page);
+
+ $user = $this->context->getUser();
+
+ $user->setAttribute('page', $page, $this->getNamespace());
+ }
+
+ /**
+ * adds context-awareness to the grid-sort functionality
+ *
+ * @see sfGrid::setSort
+ */
+ public function setSort($column, $order = null)
+ {
+ parent::setSort($column, $order);
+
+ $user = $this->context->getUser();
+
+ $user->setAttribute('sort', $column, $this->getNamespace());
+ $user->setAttribute('type', $order, $this->getNamespace());
+ }
+
+ /**
+ * Get the complete namespace for this Grid, including the addition if set
+ * by default the namespace equals the Uri,
+ * the uri is by default modulename/actionname
+ *
+ * @return string
+ */
+ public function getNamespace()
+ {
+ return $this->getUri().$this->namespaceAddition;
+ }
+
+ /**
+ * Add an addition at the end of the namespace, in case you have
+ * a couple of grids on one page and want to separate their interaction
+ * or want to reuse the same grid on several pages for different usage.
+ *
+ * @param string $namespaceAddition the addition to the namespace
+ */
+ public function setNamespaceAddition($namespaceAddition)
+ {
+ $this->namespaceAddition = '/'.$namespaceAddition;
+ }
+
+}
\ No newline at end of file
Modified: plugins/sfGridPlugin/trunk/lib/grid/sfGrid.class.php
===================================================================
--- plugins/sfGridPlugin/trunk/lib/grid/sfGrid.class.php 2010-04-13
21:16:29 UTC (rev 29130)
+++ plugins/sfGridPlugin/trunk/lib/grid/sfGrid.class.php 2010-04-13
22:10:18 UTC (rev 29131)
@@ -68,6 +68,13 @@
*/
protected $pager = null;
+ /**
+ * the title of the grid
+ *
+ * @var string
+ */
+ protected $title;
+
protected
$sortColumn = null,
$sortOrder = null,
@@ -352,6 +359,7 @@
{
$this->setSort($this->defaultSortColumn, $this->defaultSortOrder);
}
+ $this->doSort();
// update offset lazy, now is a good time to request last page and check
if we don't requested a higher pager
$this->getDataSource()->setOffset($this->getPager()->getFirstIndex());
@@ -383,6 +391,17 @@
{
return $this->getDataSource()->count();
}
+
+
+ /**
+ * proxy to the setPage method of the pager
+ *
+ * @see sfDataSourcePager::setPage
+ */
+ public function setPage($page)
+ {
+ $this->getPager()->setPage($page);
+ }
/**
* Sets the column and the order by which the grid should be sorted. The
@@ -393,10 +412,17 @@
* @param string $order The order to sort in. Must be one of sfGrid::ASC,
* sfGrid::DESC, "asc" or "desc".
*/
- public function setSort($column, $order = sfGrid::ASC)
+ public function setSort($column, $order = null)
{
- if ($order !== sfGrid::ASC && $order !== sfGrid::DESC)
+ // case insensitive
+ $order = strtolower($order);
+
+ if ($order == null)
{
+ $order = sfGrid::ASC;
+ }
+ elseif ($order !== sfGrid::ASC && $order !== sfGrid::DESC)
+ {
throw new DomainException(sprintf('The value "%s" is no valid sort
order. Should be sfGrid::ASC or sfGrid::DESC', $order));
}
@@ -410,11 +436,15 @@
}
$this->sortColumn = $column;
- $this->sortOrder = $order;
+ $this->sortOrder = ($order == sfGrid::ASC ? sfDataSourceInterface::ASC :
sfDataSourceInterface::DESC);
+ }
- $this->getDataSource()->setSort($column, $order == sfGrid::ASC
- ? sfDataSourceInterface::ASC
- : sfDataSourceInterface::DESC);
+ protected function doSort()
+ {
+ if ($this->sortColumn)
+ {
+ $this->getDataSource()->setSort($this->sortColumn, $this->sortOrder);
+ }
}
/**
@@ -658,4 +688,20 @@
return $this->widgets[$column];
}
+ /**
+ * @param string $title
+ */
+ public function setTitle($title)
+ {
+ $this->title = $title;
+ }
+
+ /**
+ * @return string the title above this grid
+ */
+ public function getTitle()
+ {
+ return $this->title;
+ }
+
}
\ No newline at end of file
Modified: plugins/sfGridPlugin/trunk/lib/grid/sfWebGrid.class.php
===================================================================
--- plugins/sfGridPlugin/trunk/lib/grid/sfWebGrid.class.php 2010-04-13
21:16:29 UTC (rev 29130)
+++ plugins/sfGridPlugin/trunk/lib/grid/sfWebGrid.class.php 2010-04-13
22:10:18 UTC (rev 29131)
@@ -8,51 +8,35 @@
* file that was distributed with this source code.
*/
-class sfWebGrid extends sfGrid
+class sfWebGrid extends sfContextGrid
{
/**
- * the namespace addition for this grid,
- * to store attributes like: sort, type and page in
- *
- * @var string
- */
- protected $namespaceAddition = '';
-
- /**
* Bind the webRequest to the Grid, to make it automatically parse all
parameters
*
* @param sfWebRequest $request
*/
public function bind(sfWebRequest $request)
{
- $context = sfContext::getInstance();
- $user = $context->getUser();
+ $moduleName = $this->context->getModuleName();
+ $actionName = $this->context->getActionName();
- $moduleName = $context->getModuleName();
- $actionName = $context->getActionName();
-
$uri = $moduleName.'/'.$actionName;
$this->setUri($uri);
-// $parameters = $request->getRequestParameters();
-
- $page = $request->getParameter('page', $user->getAttribute('page', null,
$this->getNamespace()));
+ $page = $request->getParameter('page');
if ($page != null)
{
- $this->getPager()->setPage($page);
- $user->setAttribute('page', $page, $this->getNamespace());
+ $this->setPage($page);
}
- $sort = $request->getParameter('sort', $user->getAttribute('sort', null,
$this->getNamespace()));
+ $sort = $request->getParameter('sort');
if ($sort != null)
{
- $user->setAttribute('sort', $sort, $this->getNamespace());
- $type = $request->getParameter('type', $user->getAttribute('type', null,
$this->getNamespace()));
+ $type = $request->getParameter('type');
if ($type != null)
{
$this->setSort($sort, $type);
- $user->setAttribute('type', $type, $this->getNamespace());
}
else
{
@@ -61,27 +45,4 @@
}
}
- /**
- * Get the complete namespace for this Grid, including the addition if set
- * by default the namespace equals the Uri,
- * the uri is by default modulename/actionname
- *
- * @return string
- */
- public function getNamespace()
- {
- return $this->getUri().$this->namespaceAddition;
- }
-
- /**
- * Add an addition at the end of the namespace, in case you have
- * a couple of grids on one page and want to separate their interaction
- *
- * @param string $namespaceAddition the addition to the namespace
- */
- public function setNamespaceAddition($namespaceAddition)
- {
- $this->namespaceAddition = '/'.$namespaceAddition;
- }
-
}
\ No newline at end of file
Modified: plugins/sfGridPlugin/trunk/lib/grid/sfWebGridJavaScript.class.php
===================================================================
--- plugins/sfGridPlugin/trunk/lib/grid/sfWebGridJavaScript.class.php
2010-04-13 21:16:29 UTC (rev 29130)
+++ plugins/sfGridPlugin/trunk/lib/grid/sfWebGridJavaScript.class.php
2010-04-13 22:10:18 UTC (rev 29131)
@@ -67,6 +67,26 @@
$this->dataFormatter = $formatter;
}
+ public function renderData()
+ {
+ // set default sort-column, if set
+ if (!$this->getSortColumn() && $this->defaultSortColumn)
+ {
+ $this->setSort($this->defaultSortColumn, $this->defaultSortOrder);
+ }
+
+ // update offset lazy, now is a good time to request last page and check
if we don't requested a higher pager
+ $this->getDataSource()->setOffset($this->getPager()->getFirstIndex());
+
+ if ($this->getDataFormatter() === null)
+ {
+ throw new LogicException('A Data formatter must be set before calling
renderData()');
+ }
+
+ return $this->getDataFormatter()->render();
+ }
+
+
/**
* returns the JavaScriptFormatter (to format (the structure) in
(unobstrusive) JavaScript)
*
@@ -89,32 +109,10 @@
/**
- * Re
+ * Renders static JavaScript (not based on rows)
*
* return string
*/
- public function renderData()
- {
- // set default sort-column, if set
- if (!$this->getSortColumn() && $this->defaultSortColumn)
- {
- $this->setSort($this->defaultSortColumn, $this->defaultSortOrder);
- }
-
- // update offset lazy, now is a good time to request last page and check
if we don't requested a higher pager
- $this->getDataSource()->setOffset($this->getPager()->getFirstIndex());
-
- if ($this->getDataFormatter() === null)
- {
- throw new LogicException('A Data formatter must be set before calling
renderData()');
- }
-
- return $this->getDataFormatter()->render();
- }
-
- /**
- * return string
- */
public function renderJavaScript()
{
if ($this->getJavaScriptFormatter() === null)
Added: plugins/sfGridPlugin/trunk/lib/routing/sfGridRoute.php
===================================================================
--- plugins/sfGridPlugin/trunk/lib/routing/sfGridRoute.php
(rev 0)
+++ plugins/sfGridPlugin/trunk/lib/routing/sfGridRoute.php 2010-04-13
22:10:18 UTC (rev 29131)
@@ -0,0 +1,47 @@
+<?php
+
+/**
+ * sfGridRoute represents a route that is bound to a Grid-class.
+ *
+ * A grid route can only represent a single Grid object.
+ *
+ * @package symfony
+ * @subpackage routing
+ * @author Leon van der Ree
+ * @version SVN: $Id: $
+ */
+class sfGridRoute extends sfObjectRoute
+{
+ public function __construct($pattern, array $defaults = array(), array
$requirements = array(), array $options = array())
+ {
+ $options['type'] = 'object';
+
+ parent::__construct($pattern, $defaults, $requirements, $options);
+ }
+
+ protected function getObjectForParameters($parameters)
+ {
+ $className = $this->options['model'];
+ $grid = new $className();
+
+ if (!$grid instanceof sfContextGrid)
+ {
+ throw new InvalidArgumentException('The model should extend
sfContextGrid');
+ }
+
+ if (isset($parameters['page']))
+ {
+ $grid->setPage($parameters['page']);
+ }
+
+ if (isset($parameters['sort']))
+ {
+ $sort = $parameters['sort'];
+ $type = isset($parameters['type']) ? $parameters['type'] : null;
+
+ $grid->setSort($sort, $type);
+ }
+
+ return $grid;
+ }
+}
Modified: plugins/sfGridPlugin/trunk/test/unit/mock/sfDataSourceMock.class.php
===================================================================
--- plugins/sfGridPlugin/trunk/test/unit/mock/sfDataSourceMock.class.php
2010-04-13 21:16:29 UTC (rev 29130)
+++ plugins/sfGridPlugin/trunk/test/unit/mock/sfDataSourceMock.class.php
2010-04-13 22:10:18 UTC (rev 29131)
@@ -80,7 +80,7 @@
/**
* @see sfDataSourceInterface
*/
- public function setFilter($fields)
+ public function addFilter($column, $value, $comparison = sfDataSource::EQUAL)
{
throw new Exception('This method has not been implemented yet');
}
--
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.