Author: Jonathan.Wage
Date: 2010-02-18 03:34:33 +0100 (Thu, 18 Feb 2010)
New Revision: 28105

Modified:
   
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalContentListPlugin/lib/model/doctrine/PluginsfSympalContentList.class.php
   
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalDataGridPlugin/lib/sfSympalDataGrid.class.php
   plugins/sfSympalPlugin/trunk/test/unit/DataGridTest.php
Log:
[1.4][sfSympalPlugin][1.0] Making sfSympalDataGrid implement Iterator and 
Countable


Modified: 
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalContentListPlugin/lib/model/doctrine/PluginsfSympalContentList.class.php
===================================================================
--- 
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalContentListPlugin/lib/model/doctrine/PluginsfSympalContentList.class.php
   2010-02-18 01:49:55 UTC (rev 28104)
+++ 
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalContentListPlugin/lib/model/doctrine/PluginsfSympalContentList.class.php
   2010-02-18 02:34:33 UTC (rev 28105)
@@ -38,7 +38,6 @@
     $dataGrid->setMaxPerPage(($this->rows_per_page > 0 ? $this->rows_per_page 
: sfSympalConfig::get('rows_per_page', null, 10)));
 
     $dataGridRequestInfo = $request->getParameter($dataGrid->getId());
-    $dataGrid->init();
 
     return $dataGrid;
   }

Modified: 
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalDataGridPlugin/lib/sfSympalDataGrid.class.php
===================================================================
--- 
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalDataGridPlugin/lib/sfSympalDataGrid.class.php
      2010-02-18 01:49:55 UTC (rev 28104)
+++ 
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalDataGridPlugin/lib/sfSympalDataGrid.class.php
      2010-02-18 02:34:33 UTC (rev 28105)
@@ -2,7 +2,7 @@
 
 sfApplicationConfiguration::getActive()->loadHelpers(array('Date', 'Tag'));
 
-class sfSympalDataGrid
+class sfSympalDataGrid implements Iterator, Countable
 {
   protected
     $_id,
@@ -16,9 +16,11 @@
     $_order = 'asc',
     $_columns = array(),
     $_parents = array(),
+    $_rows,
     $_renderingModule = 'sympal_data_grid',
     $_isSortable = true,
-    $_initialized = false;
+    $_initialized = false,
+    $_hydrationMode;
 
   protected static $_symfonyContext;
 
@@ -198,7 +200,7 @@
 
   public function getColumn($name)
   {
-    $this->init();
+    $this->_init();
 
     if (!isset($this->_columns[$name]))
     {
@@ -283,7 +285,7 @@
 
   public function getPagerHeader()
   {
-    $this->init();
+    $this->_init();
 
     $params = array(
       'dataGrid' => $this
@@ -294,7 +296,7 @@
 
   public function getPagerNavigation($url)
   {
-    $this->init();
+    $this->_init();
 
     $params = array(
       'dataGrid' => $this,
@@ -304,11 +306,22 @@
     return 
$this->getSymfonyResource($this->_renderingModule.'/pager_navigation', $params);
   }
 
-  public function getResults($hydrationMode = null)
+  public function setHydrationMode($hydrationMode)
   {
+    $this->_hydrationMode = $hydrationMode;
+  }
+
+  public function getHydrationMode()
+  {
+    return $this->_hydrationMode;
+  }
+
+  public function getResults()
+  {
     if (!$this->_results)
     {
-      $this->_results = $this->_pager->getResults($hydrationMode);
+      $this->_init();
+      $this->_results = $this->_pager->getResults($this->_hydrationMode);
     }
     return $this->_results;
   }
@@ -318,30 +331,37 @@
     $this->_results = $results;
   }
 
-  public function getRows($hydrationMode = null)
+  public function getRows()
   {
-    $this->init();
+    $this->_init();
+    return $this->_rows;
+  }
 
-    $rows = array();
+  protected function _buildRows()
+  {
+    if ($this->_rows === null)
+    {
+      $this->_rows = array();
 
-    $results = $this->getResults($hydrationMode);
-    foreach ($results as $result)
-    {
-      $rows[] = $this->getRow($result);
-      if (is_object($result))
+      $results = $this->getResults();
+      foreach ($results as $result)
       {
-        $result->free();
-        unset($result);
+        $this->_rows[] = $this->_buildRow($result);
+        if (is_object($result))
+        {
+          $result->free();
+          unset($result);
+        }
       }
+      if (is_object($results))
+      {
+        $results->free();
+      }
     }
-    if (is_object($results))
-    {
-      $results->free();
-    }
-    return $rows;
+    return $this->_rows;
   }
 
-  public function getRow($record)
+  protected function _buildRow($record)
   {
     $row = array();
     foreach ($this->_columns as $column)
@@ -367,25 +387,26 @@
     return $row;
   }
 
-  public function render($hydrationMode = null)
+  public function render()
   {
-    $this->init();
+    $this->_init();
 
     $params = array();
     $params['dataGrid'] = $this;
     $params['pager'] = $this->_pager;
-    $params['hydrationMode'] = $hydrationMode;
 
     return $this->getSymfonyResource($this->_renderingModule.'/list', $params);
   }
 
-  public function init()
+  protected function _init()
   {
     if ($this->_initialized)
     {
       return $this;
     }
 
+    $this->_initialized = true;
+
     $this->_query->getSqlQuery(array(), false);
 
     if (!$this->_columns)
@@ -398,9 +419,8 @@
     $this->_initializeSortingAndPaging();
 
     $this->_pager->init();
+    $this->_buildRows();
 
-    $this->_initialized = true;
-
     return $this;
   }
 
@@ -558,6 +578,48 @@
     }
   }
 
+  public function current()
+  {
+    $this->_init();
+    return current($this->_rows);
+  }
+
+  public function next()
+  {
+    $this->_init();
+    return next($this->_rows);
+  }
+
+  public function key()
+  {
+    $this->_init();
+    return key($this->_rows);
+  }
+
+  public function rewind()
+  {
+    $this->_init();
+    return reset($this->_rows);
+  }
+
+  public function valid()
+  {
+    $this->_init();
+    return $this->current() !== false;
+  }
+
+  public function count()
+  {
+    $this->_init();
+    return count($this->_rows);
+  }
+
+  public function getDql()
+  {
+    $this->_init();
+    return $this->_pager->getQuery()->getDql();
+  }
+
   public function __call($method, $arguments)
   {
     if (method_exists($this->_query, $method))

Modified: plugins/sfSympalPlugin/trunk/test/unit/DataGridTest.php
===================================================================
--- plugins/sfSympalPlugin/trunk/test/unit/DataGridTest.php     2010-02-18 
01:49:55 UTC (rev 28104)
+++ plugins/sfSympalPlugin/trunk/test/unit/DataGridTest.php     2010-02-18 
02:34:33 UTC (rev 28105)
@@ -3,7 +3,7 @@
 $app = 'sympal';
 require_once(dirname(__FILE__).'/../bootstrap/unit.php');
 
-$t = new lime_test(8, new lime_output_color());
+$t = new lime_test(10, new lime_output_color());
 
 $dataGrid = sfSympalDataGrid::create(sfSympalConfig::get('user_model'), 'u')
   ->addColumn('u.id', 'renderer=test/data_grid_id')
@@ -16,9 +16,9 @@
     'u.username' => 'Sympal Admin (admin)',
     'name' => 'Sympal Admin'
   )
-));
+), '->getRows() returns correct data for column custom method and renderer');
 
-$t->is($dataGrid->getPagerHeader(), '<div class="sympal_pager_header"><h3>One 
result found.</h3></div>');
+$t->is($dataGrid->getPagerHeader(), '<div class="sympal_pager_header"><h3>One 
result found.</h3></div>', '->getPagerHeader() returns correct html');
 
 $dataGrid = sfSympalDataGrid::create('sfSympalContentType', 'c')
   ->setMaxPerPage(1)
@@ -40,38 +40,49 @@
       'c.action' => NULL,
       'c.slug' => 'page',
     )
-  )
+  ),
+  '->getRows() returns proper rows'
 );
 
 $dataGrid = sfSympalDataGrid::create('sfSympalContentType', 'c')
   ->setMaxPerPage(1)
   ->setRenderingModule('test');
 
-$t->is($dataGrid->render(), 'Test');
+$t->is($dataGrid->render(), 'Test', '->setRenderingModule() renders using 
specified module');
 
 $dataGrid = sfSympalDataGrid::create('sfSympalContentType', 't')
   ->where('t.name = ?', 'sfSympalPage')
   ->addColumn('t.name');
 
 $rows = $dataGrid->getRows();
-$t->is($rows[0]['t.name'], 'sfSympalPage');
+$t->is($rows[0]['t.name'], 'sfSympalPage', '->getRows() returns correct data 
and array key names');
 
 $dataGrid = sfSympalDataGrid::create('sfSympalContentType', 't')
   ->where('t.name = ?', 'sfSympalPage')
   ->addColumn('t.name', 'is_sortable=false label=Test');
 
-$t->is($dataGrid->getColumnSortLink($dataGrid->getColumn('t.name')), 'Test');
+$t->is($dataGrid->getColumnSortLink($dataGrid->getColumn('t.name')), 'Test', 
'Test is_sortable=false on column sort link does not produce link');
 
 $dataGrid = sfSympalDataGrid::create('sfSympalContentType', 't')
   ->where('t.name = ?', 'Register')
   ->addColumn('t.name')
   ->isSortable(false);
 
-$t->is($dataGrid->getColumnSortLink($dataGrid->getColumn('t.name')), 'Name');
+$t->is($dataGrid->getColumnSortLink($dataGrid->getColumn('t.name')), 'Name', 
'Test non real column does not produce html for column sort link');
 
 $dataGrid = sfSympalDataGrid::create('sfSympalContentType', 't')
   ->select('t.id, t.name')
   ->setSort('t.name', 'DESC')
-  ->init();
+  ->setMaxPerPage(2);
 
-$t->is($dataGrid->getDql(), 'SELECT t.id, t.name FROM sfSympalContentType t 
ORDER BY t.name desc LIMIT 10 OFFSET 0');
\ No newline at end of file
+$t->is($dataGrid->getDql(), 'SELECT t.id, t.name FROM sfSympalContentType t 
ORDER BY t.name desc LIMIT 2 OFFSET 0', '->getDql() returns correct dql');
+
+$realCount = count($dataGrid);
+$t->is($realCount, 2, 'Test sfSympalDatagrid implements Countable interface');
+
+$countCheck = 0;
+foreach ($dataGrid as $key => $value)
+{
+  $countCheck++;
+}
+$t->is($countCheck, $realCount, 'Test sfSympalDataGrid implements Iterator');
\ No newline at end of file

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