Author: Leon.van.der.Ree
Date: 2010-03-24 19:36:32 +0100 (Wed, 24 Mar 2010)
New Revision: 28768
Modified:
plugins/sfGridPlugin/trunk/lib/grid/formatter/html/sfGridFormatterHtml.class.php
Log:
restore GridFormatter-html
Modified:
plugins/sfGridPlugin/trunk/lib/grid/formatter/html/sfGridFormatterHtml.class.php
===================================================================
---
plugins/sfGridPlugin/trunk/lib/grid/formatter/html/sfGridFormatterHtml.class.php
2010-03-24 17:30:54 UTC (rev 28767)
+++
plugins/sfGridPlugin/trunk/lib/grid/formatter/html/sfGridFormatterHtml.class.php
2010-03-24 18:36:32 UTC (rev 28768)
@@ -3,191 +3,306 @@
/*
* This file is part of the symfony package.
* (c) Bernhard Schussek <[email protected]>
- * Leon van der Ree <[email protected]>
+ * 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.
*/
-/**
- * A formatter that renders the HTML of a row
- *
- */
-class sfGridFormatterHtmlRow implements ArrayAccess
+class sfGridFormatterHtml implements sfGridFormatterInterface
{
- const NO_RESULTS_MESSAGE = 'no results';
-
- protected
- $grid = null,
- $index = null,
- $noResultsMessage;
+ const FIRST = "|«";
+ const PREV = "«";
+ const NEXT = "»";
+ const LAST = "»|";
- protected $highlightCondition = array();
+ const SORT_ASC = 'sort_asc';
+ const SORT_DESC = 'sort_desc';
/**
- * Constructs a new sfGridFormatterHtmlRow to render html-rows (tr/td's)
+ * @var sfGrid
+ */
+ protected $grid = null;
+
+ /**
+ * The row formatter
*
- * @param sfGrid $grid the grid that this row-formatter should render
- * @param int $index The index to which to set the internal row pointer
- * @param string $noResultsMessage The message to show when there are no
results in the datasource
+ * @var sfGridFormatterHtmlRow
*/
- public function __construct(sfGrid $grid, $index, $noResultsMessage =
self::NO_RESULTS_MESSAGE)
+ protected
+ $row = null;
+
+ protected
+ $cursor = 0,
+ $uri = null,
+ $sortable = array(),
+ $sortClass = array(sfGrid::ASC => self::SORT_ASC ,
+ sfGrid::DESC => self::SORT_DESC );
+
+ static public function indent($code, $levels)
{
- $this->initialize($grid, $index, $noResultsMessage);
+ $lines = explode("\n", $code);
+ foreach ($lines as &$line)
+ {
+ $line = str_repeat(' ', $levels) . $line;
+ }
+ return implode("\n", $lines);
}
- /**
- * Initialises the new sfGridFormatterHtmlRow
- *
- * @param sfGrid $grid the grid that this row-formatter should render
- * @param int $index The index to which to set the internal row pointer
- * @param string $noResultsMessage The message to show when there are no
results in the datasource
- */
- public function initialize(sfGrid $grid, $index, $noResultsMessage =
self::NO_RESULTS_MESSAGE)
+ public function __construct(sfGrid $grid)
{
$this->grid = $grid;
- $this->index = $index;
- $this->noResultsMessage = $noResultsMessage;
+
+ $this->row = new sfGridFormatterHtmlRow($grid, 0);
}
-
+
/**
- * Returns the associated grid
+ * Sets the css classes used for the actively sorted column
*
- * @return sfGrid
+ * @param array $sortClass
+ * @throws LogicException Throws an exception if no asc or desc have been
defined
*/
- public function getGrid()
+ public function setSortClasses(array $sortClass)
{
- return $this->grid;
+ if (!isset($sortClass['asc']) || !isset($sortClass['desc']))
+ {
+ throw new LogicException('When setting the sortClasses please specify
both asc and desc');
+ }
+
+ $this->sortClass = $sortClass;
}
/**
- * Returns the internal row pointer
+ * Sets the description when the datasource contains no results
*
- * @return int
+ * @param string $noResultsMessage
*/
- public function getIndex()
+ public function setNoResultsMessage($noResultsMessage)
{
- return $this->index;
+ $this->row->setNoResultsMessage($noResultsMessage);
}
+
+ /**
+ * Sets the condition to add a css-class to a row
+ *
+ * @param string $column the column name
+ * @param mixed $value the value the column should be equal to
+ * @param string $class the css-class the row should get
+ */
+ public function setRowHighlightCondition($column, $value = true,
$class='active')
+ {
+ $this->row->setRowHighlightCondition($column, $value, $class);
+ }
/**
- * Renders a row to html
+ * Renders the table in HTML
*
* @return string
*/
public function render()
{
- $source = $this->grid->getDataSource();
- $source->seek($this->index);
+ return $this->renderHead().$this->renderFoot().$this->renderBody();
+ }
- $css = '';
- if (isset($this->highlightCondition['column']))
+ public function renderHead()
+ {
+ $html = "<thead>\n<tr>\n";
+
+ foreach ($this->grid->getColumns() as $column)
{
- if ($source[$this->highlightCondition['column']] ===
$this->highlightCondition['value'])
- {
- $css = ' class="'.$this->highlightCondition['class'].'"';
- }
+ $html .= " " . $this->renderColumnHead($column) . "\n";
}
- $data = "<tr".$css.">\n";
- foreach ($this->grid->getWidgets() as $column => $widget)
+ return $html . "</tr>\n</thead>\n";
+ }
+
+ public function renderPager()
+ {
+ sfProjectConfiguration::getActive()->loadHelpers(array('Url'));
+
+ $uri = $this->grid->getUri();
+ if (empty($uri))
{
- // First render the body. Possible that the tdCss is changed by it.
- $tagBody = $widget->render($column, $source[$column]);
-
- // Check the css options.
- $arrOptions = array();
- if ($widget->getOption('tdCss'))
+ throw new LogicException('Please specify a URI with sfGrid::setUri()
before rendering the pager');
+ }
+ $uriArgs = $this->grid->getUriArgs();
+
+ $pager = $this->grid->getPager();
+ $html = "<div class=\"paging\">\n";
+
+ if ($pager->hasFirstPage())
+ {
+ $html .= " <a href=\"" . url_for($uri. '?' .
http_build_query(array_merge($uriArgs, array('page' => $pager->getFirstPage()))
, '', '&')) . "\">".self::FIRST."</a>\n";
+ }
+ if ($pager->hasPreviousPage())
+ {
+ $html .= " <a href=\"" . url_for($uri. '?' .
http_build_query(array_merge($uriArgs, array('page' =>
$pager->getPreviousPage())), '', '&')) . "\">".self::PREV."</a>\n";
+ }
+ foreach ($pager as $page)
+ {
+ if ($page == $pager->getPage())
{
- $arrOptions['class'] = $widget->getOption('tdCss');
- $widget->setOption('tdCss', null); // reset for next row
+ $html .= " " . $page . "\n";
}
- if ($widget->getOption('tdTitle'))
+ else
{
- $arrOptions['title'] = $widget->getOption('tdTitle');
- $widget->setOption('tdTitle', null); // reset for next row
- }
- $data .= ' '.$widget->renderContentTag('td',
- $tagBody,
- $arrOptions)
- ."\n";
+ $html .= " <a href=\"" . url_for($uri. '?' .
http_build_query(array_merge($uriArgs, array('page' => $page)), '', '&')) .
"\">" . $page . "</a>\n";
+ }
}
+ if ($pager->hasNextPage())
+ {
+ $html .= " <a href=\"" . url_for($uri. '?' .
http_build_query(array_merge($uriArgs, array('page' => $pager->getNextPage())),
'', '&')) . "\">".self::NEXT."</a>\n";
+ }
+ if ($pager->hasLastPage())
+ {
+ $html .= " <a href=\"" . url_for($uri. '?' .
http_build_query(array_merge($uriArgs, array('page' => $pager->getLastPage())),
'', '&')) . "\">".self::LAST."</a>\n";
+ }
- return $data . "</tr>\n";
+ return $html . "</div>\n";
}
- /**
- * renders the html when no data is in the datasource
- *
- * @return string
- */
- public function noRows()
+ public function renderFoot()
{
- $colspan = count($this->grid->getWidgets());
-
- $data = "<tr>\n";
- $data .= "<td
colspan=\"".$colspan."\">".$this->getNoResultsMessage()."</td>";
-
- return $data . "</tr>\n";
+ $pager = $this->grid->getPager();
+ $html = $pager->hasToPaginate() ? "\n".self::indent($this->renderPager(),
2) : '';
+
+ $html = "<tfoot>\n<tr>\n";
+ $html .= " <th colspan=\"".count($this->grid->getColumns())."\">";
+ if ($pager->hasToPaginate())
+ {
+ $html .= "\n".self::indent($this->renderPager(), 2);
+ }
+ $html .= "\n ".$pager->getRecordCount()." results";
+ if ($pager->hasToPaginate())
+ {
+ $html .= " (page ".$pager->getPage()." of ".$pager->getPageCount().")";
+ }
+
+
+ return $html."\n </th>\n</tr>\n</tfoot>\n";
}
-
- /**
- * Returns the description if no results are in the datasource
- *
- * @return string
- */
- public function getNoResultsMessage()
+
+ public function renderBody()
{
- return $this->noResultsMessage;
+ $html = "<tbody>\n";
+
+ if (count($this) != 0)
+ {
+ foreach ($this as $row)
+ {
+ $html .= $row->render();
+ }
+ }
+ else
+ {
+ $html .= $this->row->noRows();
+ }
+
+ return $html . "</tbody>\n";
}
-
+
/**
- * Sets the description when the datasource contains no results
+ * Enter description here...
*
- * @param string $noResultsMessage
+ * @param string $column
+ * @return string html formatted string
*/
- public function setNoResultsMessage($noResultsMessage)
+ public function renderColumnHead($column)
{
- $this->noResultsMessage = $noResultsMessage;
- }
-
- /**
- * Sets the condition to add a css-class to a row
- *
- * @param string $column the column name
- * @param mixed $value the value the column should be equal to
- * @param string $class the css-class the row should get
- */
- public function setRowHighlightCondition($column, $value = true,
$class='active')
+ sfProjectConfiguration::getActive()->loadHelpers(array('Url', 'Tag'));
+
+ $html = $this->grid->getTitleForColumn($column);
+ $arrOptions = $this->grid->getOptionsTitleForColumn($column);
+
+ if (in_array($column, $this->grid->getSortable()))
+ {
+ $uri = $this->grid->getUri();
+ if (empty($uri))
+ {
+ throw new LogicException('Please specify a URI with sfGrid::setUri()
before rendering the pager');
+ }
+ $uriArgs = $this->grid->getUriArgs();
+
+ if ($this->grid->getSortColumn() == $column)
+ {
+ if (isset($arrOptions['class']))
+ {
+ $arrOptions['class'] .= ' '.
$this->sortClass[$this->grid->getSortOrder()];
+ }
+ else
+ {
+ $arrOptions['class'] = $this->sortClass[$this->grid->getSortOrder()];
+ }
+ }
+
+ $nextOrder = $this->grid->getSortColumn() == $column
+ ? ($this->grid->getSortOrder() == sfGrid::ASC ? 'desc' : 'asc')
+ : 'asc';
+
+ // build the HTML with a class attribute sort_asc or sort_desc, if the
+ // column is currently being sorted
+ $html = sprintf("<a href=\"%s\">%s</a>",
+ url_for($uri. '?' .
http_build_query(array_merge($uriArgs,
+
array('sort' => $column,
+
'type' => $nextOrder)),
+ '',
+ '&')),
+ $html);
+ }
+
+ return tag('th', $arrOptions, true).$html.'</th>';
+ }
+
+ public function current()
{
- $this->highlightCondition = array(
- 'column' => $column,
- 'value' => $value,
- 'class' => $class,
- );
+ $this->row->initialize($this->grid, $this->cursor);
+
+ return $this->row;
}
- public function offsetGet($key)
+ public function next()
{
- $source = $this->grid->getDataSource();
- $source->seek($this->index);
+ ++$this->cursor;
+ }
- return $source[$key];
+ public function key()
+ {
+ return $this->cursor;
}
- public function offsetSet($key, $value)
+ public function rewind()
{
- throw new LogicException('Modification of fields is not allowed');
+ $this->cursor = 0;
}
- public function offsetExists($key)
+ public function valid()
{
- return $this->grid->hasColumn($key);
+ return $this->cursor < count($this);
}
- public function offsetUnset($key)
+ public function count()
{
- throw new LogicException('Modification of fields is not allowed');
+ return count($this->grid);
}
-}
\ No newline at end of file
+
+// TODO: this can be removed?! using the url_for method from symfony...
+// static public function makeUri($uri, array $params)
+// {
+// // split the uri
+// $uri = explode('?', $uri);
+//
+// // extract the query string
+// $values = array();
+// if (count($uri) > 1)
+// {
+// $query = explode('#', $uri[1]);
+// parse_str($query[0], $values);
+// }
+// $params = array_merge($values, $params);
+//
+// // build the new uri
+//// return $uri[0] . '?' . http_build_query($params, '', '&');
+// return url_for($module_action . '?' . http_build_query($params, '',
'&'));
+// }
+}
+
--
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.