Author: sergiovier
Date: 2010-05-21 22:11:49 +0200 (Fri, 21 May 2010)
New Revision: 29579

Added:
   plugins/sfGridPlugin/trunk/lib/helper/
   plugins/sfGridPlugin/trunk/lib/helper/GridHelper.php
   plugins/sfGridPlugin/trunk/lib/widget/sfWidgetGrid.class.php
   plugins/sfGridPlugin/trunk/lib/widget/sfWidgetGridLink.class.php
Removed:
   plugins/sfGridPlugin/trunk/lib/widget/sfWidgetLink.class.php
   plugins/sfGridPlugin/trunk/lib/widget/sfWidgetText.class.php
Modified:
   plugins/sfGridPlugin/trunk/README
   plugins/sfGridPlugin/trunk/lib/grid/sfContextGridJavaScript.class.php
   plugins/sfGridPlugin/trunk/lib/grid/sfGrid.class.php
   plugins/sfGridPlugin/trunk/test/bootstrap/unit.php
   plugins/sfGridPlugin/trunk/test/unit/grid/sfGridTest.php
   plugins/sfGridPlugin/trunk/test/unit/mock/sfWidgetMock.class.php
Log:
Some refactoring:
 - the widget for the grid are based on sfWidget from symfony form framework.
 - added a grid helper, for manage js and css inclusion.
 - fixed some unit test.


Modified: plugins/sfGridPlugin/trunk/README
===================================================================
--- plugins/sfGridPlugin/trunk/README   2010-05-21 20:00:05 UTC (rev 29578)
+++ plugins/sfGridPlugin/trunk/README   2010-05-21 20:11:49 UTC (rev 29579)
@@ -6,7 +6,7 @@
 This plugin contains a grid subframework that allows to read various data 
sources in a unified way. 
 These data sources will be rendered in a customizable grid with a custom 
formatter.
 
-Currently a HTMLand text formatter are supported. The release of the JSON 
formatter is expected soon.
+Currently a HTML and text formatter are supported. The release of the JSON 
formatter is expected soon.
 
 
 Howto

Modified: plugins/sfGridPlugin/trunk/lib/grid/sfContextGridJavaScript.class.php
===================================================================
--- plugins/sfGridPlugin/trunk/lib/grid/sfContextGridJavaScript.class.php       
2010-05-21 20:00:05 UTC (rev 29578)
+++ plugins/sfGridPlugin/trunk/lib/grid/sfContextGridJavaScript.class.php       
2010-05-21 20:11:49 UTC (rev 29579)
@@ -11,27 +11,27 @@
 /**
  * This abstract class is there as a base for your JavaScript-Grid class.
  * You should define the JavaScript formatter in the specialised class
- * 
+ *
  * All JavaScript Grids "should" still render to HTML, while unobtrusive 
JavaScript should progressive enhance the intactivity of your grid,
- * To dynamically load new content a dataFormatter can be defined to output 
JSON or XML-data.  
- * 
+ * To dynamically load new content a dataFormatter can be defined to output 
JSON or XML-data.
+ *
  */
 abstract class sfContextGridJavaScript extends sfContextGrid
 {
   /**
    * formatter to output data (json/xml)
-   * 
+   *
    * @var sfGridFormatterInterface
    */
   protected $dataFormatter;
 
   /**
    * formatter that returns (unobtrusive) JavaScript
-   * 
+   *
    * @var sfGridFormatterInterface
    */
   protected $javaScriptFormatter;
-  
+
   /**
    * This method should be implemented in your specialised GridClass, to define
    * the dataFormatter and javaScriptFormatter
@@ -40,16 +40,16 @@
   {
     //set html formatter
     parent::configure();
-    
+
     // define the data formatter
     $this->setDataFormatter(new sfGridFormatterJson($this));
-    
+
     // $this->setJavaScriptFormatter(new 
sfGridFormatterYOUR_JS_FORMATTER($this));
   }
-  
+
   /**
-   * returns the DataFormatter (to format data in json/xml) 
-   * 
+   * returns the DataFormatter (to format data in json/xml)
+   *
    * @return sfGridFormatterInterface
    */
   public function getDataFormatter()
@@ -66,8 +66,8 @@
   {
     $this->dataFormatter = $formatter;
   }
-  
-    public function renderData()
+
+  public function renderData()
   {
     // set default sort-column, if set
     if (!$this->getSortColumn() && $this->defaultSortColumn)
@@ -86,11 +86,11 @@
 
     return $this->getDataFormatter()->render();
   }
-  
-  
+
+
   /**
    * returns the JavaScriptFormatter (to format (the structure) in 
(unobstrusive) JavaScript)
-   * 
+   *
    * @return sfGridFormatterInterface
    */
   public function getJavaScriptFormatter()
@@ -107,11 +107,11 @@
   {
     $this->javaScriptFormatter = $formatter;
   }
-  
-  
+
+
   /**
    * Renders static JavaScript (not based on rows)
-   * 
+   *
    * return string
    */
   public function renderJavaScript()
@@ -123,4 +123,24 @@
 
     return $this->getJavaScriptFormatter()->render();
   }
+
+  /**
+   * Gets the JavaScript paths associated with the widget.
+   *
+   * @return array An array of JavaScript paths
+   */
+  public function getJavascripts()
+  {
+    return array();
+  }
+
+  /**
+   * Gets the stylesheet paths associated with the widget.
+   *
+   * @return array An array of stylesheet paths
+   */
+  public function getStylesheets()
+  {
+    return array();
+  }
 }
\ No newline at end of file

Modified: plugins/sfGridPlugin/trunk/lib/grid/sfGrid.class.php
===================================================================
--- plugins/sfGridPlugin/trunk/lib/grid/sfGrid.class.php        2010-05-21 
20:00:05 UTC (rev 29578)
+++ plugins/sfGridPlugin/trunk/lib/grid/sfGrid.class.php        2010-05-21 
20:11:49 UTC (rev 29579)
@@ -53,29 +53,29 @@
     $columnTitlesOptions = array(),
     $sortable     = array(),
     $widgets      = array();
-    
+
   /**
    * formatter that returns (unobtrusive) JavaScript
-   * 
+   *
    * @var sfGridFormatterInterface
    */
   protected $formatter    = null;
-  
+
   /**
    * formatter that returns (unobtrusive) JavaScript
-   * 
+   *
    * @var sfDataSourcePager
    */
   protected $pager        = null;
 
   /**
    * the title of the grid
-   * 
+   *
    * @var string
    */
   protected $title;
-  
-  protected 
+
+  protected
     $sortColumn   = null,
     $sortOrder    = null,
 
@@ -153,7 +153,7 @@
    */
   protected function getDefaultWidget()
   {
-    return new sfWidgetText();
+    return new sfWidgetGrid();
   }
 
   /**
@@ -391,11 +391,11 @@
   {
     return $this->getDataSource()->count();
   }
-  
-    
+
+
   /**
    * proxy to the setPage method of the pager
-   * 
+   *
    * @see sfDataSourcePager::setPage
    */
   public function setPage($page)
@@ -416,7 +416,7 @@
   {
     // case insensitive
     $order = strtolower($order);
-    
+
     if ($order == null)
     {
       $order = sfGrid::ASC;
@@ -434,7 +434,7 @@
     {
       throw new LogicException('Unable to sort on column: "'.$column.'"');
     }
-    
+
     $this->sortColumn = $column;
     $this->sortOrder = ($order == sfGrid::ASC ? sfDataSourceInterface::ASC : 
sfDataSourceInterface::DESC);
   }
@@ -693,9 +693,9 @@
    */
   public function setTitle($title)
   {
-   $this->title = $title; 
+   $this->title = $title;
   }
-    
+
   /**
    * @return string the title above this grid
    */

Added: plugins/sfGridPlugin/trunk/lib/helper/GridHelper.php
===================================================================
--- plugins/sfGridPlugin/trunk/lib/helper/GridHelper.php                        
        (rev 0)
+++ plugins/sfGridPlugin/trunk/lib/helper/GridHelper.php        2010-05-21 
20:11:49 UTC (rev 29579)
@@ -0,0 +1,92 @@
+<?php
+
+/*
+ * This file is part of the sfGrid plugin.
+ * (c) 2010 Sergio Fabian Vier <[email protected]>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ * GridHelper.
+ *
+ * @package    symfony
+ * @subpackage helper
+ * @author     Sergio Fabian Vier <[email protected]>
+ * @version    SVN: $Id$
+ */
+
+/**
+ * Returns <script> tags for all javascripts associated with the given grid.
+ *
+ * The scripts are set by implementing the getJavaScripts() method in the
+ * corresponding widget.
+ *
+ * @return string <script> tags
+ */
+function get_javascripts_for_grid(sfGrid $grid)
+{
+  $html = '';
+  foreach ($grid->getJavascripts() as $file)
+  {
+    $html .= javascript_include_tag($file);
+  }
+
+  return $html;
+}
+
+/**
+ * Prints <script> tags for all javascripts associated with the given grid.
+ *
+ * @see get_javascripts_for_grid()
+ */
+function include_javascripts_for_grid(sfGrid $grid)
+{
+  echo get_javascripts_for_grid($grid);
+}
+
+/**
+ * Returns <link> tags for all stylesheets associated with the given grid.
+ *
+ * The stylesheets are set by implementing the getStyleSheets() method in the
+ * corresponding grid.
+ *
+ *
+ * @return string <link> tags
+ */
+function get_stylesheets_for_grid(sfGrid $grid)
+{
+  $html = '';
+  foreach ($grid->getStylesheets() as $file => $media)
+  {
+    $html .= stylesheet_tag($file, array('media' => $media));
+  }
+
+  return $html;
+}
+
+/**
+ * Prints <link> tags for all stylesheets associated with the given grid.
+ *
+ * @see get_stylesheets_for_grid()
+ */
+function include_stylesheets_for_grid(sfGrid $grid)
+{
+  echo get_stylesheets_for_grid($grid);
+}
+
+/**
+ * Adds stylesheets from the supplied grid to the response object.
+ *
+ * @param sfGrid $grid
+ */
+function use_stylesheets_for_grid(sfGrid $grid)
+{
+  $response = sfContext::getInstance()->getResponse();
+
+  foreach ($grid->getStylesheets() as $file => $media)
+  {
+    $response->addStylesheet($file, '', array('media' => $media));
+  }
+}
\ No newline at end of file

Added: plugins/sfGridPlugin/trunk/lib/widget/sfWidgetGrid.class.php
===================================================================
--- plugins/sfGridPlugin/trunk/lib/widget/sfWidgetGrid.class.php                
                (rev 0)
+++ plugins/sfGridPlugin/trunk/lib/widget/sfWidgetGrid.class.php        
2010-05-21 20:11:49 UTC (rev 29579)
@@ -0,0 +1,64 @@
+<?php
+
+/*
+ * This file is part of the symfony package.
+ * (c) Sergio Fabian Vier <[email protected]>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ * sfWidgetGrid is the base class for all grid widgets.
+ *
+ * @package    lib
+ * @subpackage widget
+ * @author     Sergio Fabian Vier <[email protected]>
+ * @version    SVN: $Id$
+ */
+class sfWidgetGrid extends sfWidget
+{
+
+  /**
+   * the Grid reference
+   * @var sfGrid
+   */
+  protected $grid = null;
+
+  /**
+   * Render a plain value
+   *
+   * Extend this class and override this method for pre-process this value.
+   *
+   * @see sfWidget#render()
+   */
+  public function render($name, $value = null, $attributes = array(), $errors 
= array())
+  {
+    return $value;
+  }
+
+  /**
+   * Set the grid reference
+   *
+   * @param sfGrid $grid
+   */
+  public function setGrid(sfGrid $grid)
+  {
+    $this->grid = $grid;
+  }
+
+  /**
+   * Returns the grid reference
+   *
+   * @return sfGrid
+   */
+  public function getGrid()
+  {
+    if ($this->grid !== null){
+      return $this->grid;
+    }
+    // realy, throw a exception?
+    throw new LogicException(sprintf("The widget '%s' class has not setted the 
'grid' option.", getclass($this)));
+  }
+
+}
\ No newline at end of file


Property changes on: 
plugins/sfGridPlugin/trunk/lib/widget/sfWidgetGrid.class.php
___________________________________________________________________
Added: svn:executable
   + *

Added: plugins/sfGridPlugin/trunk/lib/widget/sfWidgetGridLink.class.php
===================================================================
--- plugins/sfGridPlugin/trunk/lib/widget/sfWidgetGridLink.class.php            
                (rev 0)
+++ plugins/sfGridPlugin/trunk/lib/widget/sfWidgetGridLink.class.php    
2010-05-21 20:11:49 UTC (rev 29579)
@@ -0,0 +1,90 @@
+<?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 sfWidgetGridLink extends sfWidgetGrid
+{
+
+  /**
+   * Configures the current widget.
+   *
+   * This method allows each widget to add options or HTML attributes
+   * during widget creation.
+   *
+   * @param array $options     An array of options
+   * @param array $attributes  An array of HTML attributes
+   *
+   * @see __construct()
+   */
+  public function configure($options = array(), $attributes = array())
+  {
+    parent::configure($options, $attributes);
+
+    $this->addRequiredOption('key_column');
+    $this->addRequiredOption('action', null);
+    $this->addOption('mapping', array());
+
+    // options for render
+    $this->addOption('label', null);
+
+  }
+
+  /**
+   * Returns the internal uri for the current widget
+   *
+   * @return string
+   */
+  public function getUri()
+  {
+    $uri = url_for($this->getOption('action'), array(), true).'?';
+
+    $key_column = $this->getOption('key_column');
+    if ($key_column)
+    {
+      $source = $this->getGrid()->getDataSource();
+      $key = $key_column;
+
+      if (isset($this->mapping[$key]))
+      {
+        $key = $this->mapping[$key];
+      }
+
+      $uri .= $key.'='.$source[$key_column];
+    }
+
+    return $uri;
+  }
+
+  public function getKeyColumn()
+  {
+    if ($this->getOption('key_column'))
+    {
+      return $this->getOption('key_column');
+    }
+
+    throw new UnexpectedValueException(sprintf("For use this method, it's 
necesary a valid 'key_column' option in widget '%s' class.", getclass($this)));
+  }
+
+  /**
+   * @param  string $name        The element name
+   * @param  string $value       The value displayed in this widget
+   * @param  array  $attributes  An array of HTML attributes to be merged with 
the default HTML attributes
+   * @param  array  $errors      An array of errors for the field
+   *
+   * @return string An HTML tag string
+   *
+   * @see sfWidgetForm
+   */
+  public function render($name, $value = null, $attributes = array(), $errors 
= array())
+  {
+    sfContext::getInstance()->getConfiguration()->loadHelpers(array('Url', 
'Tag'));
+
+    return link_to($name, $this->getUri(), $attributes);
+  }
+}
\ No newline at end of file

Deleted: plugins/sfGridPlugin/trunk/lib/widget/sfWidgetLink.class.php
===================================================================
--- plugins/sfGridPlugin/trunk/lib/widget/sfWidgetLink.class.php        
2010-05-21 20:00:05 UTC (rev 29578)
+++ plugins/sfGridPlugin/trunk/lib/widget/sfWidgetLink.class.php        
2010-05-21 20:11:49 UTC (rev 29579)
@@ -1,87 +0,0 @@
-<?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 sfWidgetLink extends sfWidget
-{
-  /**
-   * @var sfGrid
-   */
-  protected $grid;
-  /**
-   * @var string
-   */
-  protected $key_column; 
-  /**
-   * @var string
-   */
-  protected $action; 
-  /**
-   * @var string
-   */
-  protected $mapping;
-  
-  /**
-   * The constructor for the sfWidgetLink class, allowing to render links in a 
column
-   *
-   * @param sfGrid $grid        an instance of the sfGrid, containing a 
datasource
-   * @param string $action      the name of the action the link is directing to
-   * @param string $key_column  the column name from the datasource that 
should be used as key
-   * @param array $mapping      a mapping to translate the column-name to a 
parameter in the url
-   */
-  public function __construct($grid, $action, $key_column = null, $mapping = 
array())
-  {
-    $this->grid       = $grid;
-    $this->action     = $action; 
-    $this->key_column = $key_column;
-    $this->mapping    = $mapping;
-  }
-  
-  /**
-   * Returns the internal uri for the current widget 
-   *
-   * @return string
-   */
-  public function getUri()
-  {
-    $uri = sfContext::getInstance()->getModuleName().'/'.$this->action.'?';
-    
-    if ($this->key_column)
-    {
-      $source = $this->grid->getDataSource();
-      $key = $this->key_column;
-      
-      if (isset($this->mapping[$key]))
-      {
-        $key = $this->mapping[$key];
-      }
-      
-      $uri .= $key.'='.$source[$this->key_column];
-    }
-
-    return $uri;
-  }
-  
-  /**
-   * @param  string $name        The element name
-   * @param  string $value       The value displayed in this widget
-   * @param  array  $attributes  An array of HTML attributes to be merged with 
the default HTML attributes
-   * @param  array  $errors      An array of errors for the field
-   *
-   * @return string An HTML tag string
-   *
-   * @see sfWidgetForm
-   */
-  public function render($name, $value = null, $attributes = array(), $errors 
= array())
-  {
-    sfContext::getInstance()->getConfiguration()->loadHelpers(array('Url', 
'Tag'));
-    
-    return link_to($value, $this->getUri());
-  }
-}
\ No newline at end of file

Deleted: plugins/sfGridPlugin/trunk/lib/widget/sfWidgetText.class.php
===================================================================
--- plugins/sfGridPlugin/trunk/lib/widget/sfWidgetText.class.php        
2010-05-21 20:00:05 UTC (rev 29578)
+++ plugins/sfGridPlugin/trunk/lib/widget/sfWidgetText.class.php        
2010-05-21 20:11:49 UTC (rev 29579)
@@ -1,17 +0,0 @@
-<?php
-
-/*
- * This file is part of the symfony package.
- * (c) Bernhard Schussek <[email protected]>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
-class sfWidgetText extends sfWidget
-{
-  public function render($name, $value = null, $attributes = array(), $errors 
= array())
-  {
-    return $value;
-  }
-}
\ No newline at end of file

Modified: plugins/sfGridPlugin/trunk/test/bootstrap/unit.php
===================================================================
--- plugins/sfGridPlugin/trunk/test/bootstrap/unit.php  2010-05-21 20:00:05 UTC 
(rev 29578)
+++ plugins/sfGridPlugin/trunk/test/bootstrap/unit.php  2010-05-21 20:11:49 UTC 
(rev 29579)
@@ -18,7 +18,7 @@
 {
   //throw new RuntimeException('Could not find symfony core libraries.');
 //  $_SERVER['SYMFONY'] = $_test_dir.'/../../../lib/symfony';
-  $_SERVER['SYMFONY'] = $plugin_dir.'/../../../../sf1.4';  
+  $_SERVER['SYMFONY'] = $plugin_dir.'/../../../lib/vendor/symfony';
 }
 
 // register symfony files
@@ -49,17 +49,17 @@
 }
 
 class TestContext extends sfContext
-{ 
+{
   public function loadFactories()
   {
     $this->factories['controller'] = new sfFrontWebController($this);
   }
-  
+
   public function shutdown()
   {
-    
+
   }
-  
+
 }
 
 $configuration = new Configuration('unitTest', true);

Modified: plugins/sfGridPlugin/trunk/test/unit/grid/sfGridTest.php
===================================================================
--- plugins/sfGridPlugin/trunk/test/unit/grid/sfGridTest.php    2010-05-21 
20:00:05 UTC (rev 29578)
+++ plugins/sfGridPlugin/trunk/test/unit/grid/sfGridTest.php    2010-05-21 
20:11:49 UTC (rev 29579)
@@ -14,9 +14,9 @@
 
 class sfGridTest extends sfGrid
 {
-  public 
+  public
     $configured = false;
-    
+
   public function configure()
   {
     $this->configured = true;
@@ -26,12 +26,12 @@
 
 class sfGridFormatterInvalid {}
 
-class sfWidgetMock extends sfWidget 
+class sfWidgetMock extends sfWidgetGrid
 {
   public function render($name, $value = null, $attributes = array(), $errors 
= array()) {}
 }
 
-class sfWidgetMock2 extends sfWidget 
+class sfWidgetMock2 extends sfWidgetGrid
 {
   public function render($name, $value = null, $attributes = array(), $errors 
= array()) {}
 }
@@ -206,7 +206,7 @@
 $g->setUri('http://mydomain.com');
 $t->is($g->getUri(), 'http://mydomain.com', '->setUri() sets the URI used by 
formatters');
 $g->setUri('http://mydomain.com?param=value');
-// Bernhard, just like mentioned in sfGridFormatterHtmlTest I changed this, so 
no more parameters (these can be stored in the user session)... 
+// Bernhard, just like mentioned in sfGridFormatterHtmlTest I changed this, so 
no more parameters (these can be stored in the user session)...
 $t->is($g->getUri(), 'http://mydomain.com', '->setUri() sets the URI used by 
formatters');
 //try
 //{
@@ -288,8 +288,8 @@
 }
 
 $g = new sfGridTest(new sfDataSourceMock());
-$t->is(array_keys($g->getWidgets()), $g->getColumns(), 'sfWidgetText', 
'->getWidgets() returns sfWidgetText instances for every column by default');
+$t->is(array_keys($g->getWidgets()), $g->getColumns(), 'sfWidgetGrid', 
'->getWidgets() returns sfWidgetText instances for every column by default');
 foreach ($g->getWidgets() as $widget)
 {
-  $t->isa_ok($widget, 'sfWidgetText', '->getWidgets() returns sfWidgetText 
instances for every column by default');
+  $t->isa_ok($widget, 'sfWidgetGrid', '->getWidgets() returns sfWidgetText 
instances for every column by default');
 }

Modified: plugins/sfGridPlugin/trunk/test/unit/mock/sfWidgetMock.class.php
===================================================================
--- plugins/sfGridPlugin/trunk/test/unit/mock/sfWidgetMock.class.php    
2010-05-21 20:00:05 UTC (rev 29578)
+++ plugins/sfGridPlugin/trunk/test/unit/mock/sfWidgetMock.class.php    
2010-05-21 20:11:49 UTC (rev 29579)
@@ -8,7 +8,7 @@
  * file that was distributed with this source code.
  */
 
-class sfWidgetMock extends sfWidget
+class sfWidgetMock extends sfWidgetGrid
 {
   public function render($name, $value = null, $attributes = array(), $errors 
= array())
   {

-- 
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