summaryrefslogtreecommitdiff
path: root/framework
diff options
context:
space:
mode:
authorxue <>2006-01-19 05:37:08 +0000
committerxue <>2006-01-19 05:37:08 +0000
commit5c333928f7f328319f1de4486d5e7ce3a54fc2ce (patch)
tree91961c55d0d9bf40538cd5fec0a3afbb588a35b3 /framework
parentfd5e244960a5824de0b3e7193d373d15be25e4e2 (diff)
Added DummyDataSource. TDataGrid completed (tests pending).
Diffstat (limited to 'framework')
-rw-r--r--framework/Collections/TDummyDataSource.php131
-rw-r--r--framework/Collections/TPagedDataSource.php56
-rw-r--r--framework/Web/UI/TForm.php9
-rw-r--r--framework/Web/UI/WebControls/TDataGrid.php172
4 files changed, 339 insertions, 29 deletions
diff --git a/framework/Collections/TDummyDataSource.php b/framework/Collections/TDummyDataSource.php
new file mode 100644
index 00000000..74f5f667
--- /dev/null
+++ b/framework/Collections/TDummyDataSource.php
@@ -0,0 +1,131 @@
+<?php
+/**
+ * TDummyDataSource, TDummyDataSourceIterator classes
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @link http://www.pradosoft.com/
+ * @copyright Copyright &copy; 2005 PradoSoft
+ * @license http://www.pradosoft.com/license/
+ * @version $Revision: $ $Date: $
+ * @package System.Collections
+ */
+
+/**
+ * TDummyDataSource class
+ *
+ * TDummyDataSource implements a dummy data collection with a specified number
+ * of dummy data items. You can traverse it using <b>foreach</b>
+ * PHP statement like the following,
+ * <code>
+ * foreach($dummyDataSource as $dataItem)
+ * </code>
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @version $Revision: $ $Date: $
+ * @package System.Collections
+ * @since 3.0
+ */
+class TDummyDataSource extends TComponent implements IteratorAggregate
+{
+ private $_count;
+
+ public function __construct($count)
+ {
+ $this->_count=$count;
+ }
+
+ public function getCount()
+ {
+ return $this->_count;
+ }
+
+ /**
+ * @return Iterator iterator
+ */
+ public function getIterator()
+ {
+ return new TDummyDataSourceIterator($this->_count);
+ }
+}
+
+/**
+ * TDummyDataSourceIterator class
+ *
+ * TDummyDataSourceIterator implements Iterator interface.
+ *
+ * TDummyDataSourceIterator is used by {@link TDummyDataSource}.
+ * It allows TDummyDataSource to return a new iterator
+ * for traversing its dummy items.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @version $Revision: $ $Date: $
+ * @package System.Collections
+ * @since 3.0
+ */
+class TDummyDataSourceIterator implements Iterator
+{
+ private $_index;
+ private $_count;
+
+ /**
+ * Constructor.
+ * @param TList the data to be iterated through
+ * @param integer start index
+ * @param integer number of items to be iterated through
+ */
+ public function __construct($count)
+ {
+ $this->_count=$count;
+ $this->_index=0;
+ }
+
+ /**
+ * Rewinds internal array pointer.
+ * This method is required by the interface Iterator.
+ */
+ public function rewind()
+ {
+ $this->_index=0;
+ }
+
+ /**
+ * Returns the key of the current array item.
+ * This method is required by the interface Iterator.
+ * @return integer the key of the current array item
+ */
+ public function key()
+ {
+ return $this->_index;
+ }
+
+ /**
+ * Returns the current array item.
+ * This method is required by the interface Iterator.
+ * @return mixed the current array item
+ */
+ public function current()
+ {
+ return null;
+ }
+
+ /**
+ * Moves the internal pointer to the next array item.
+ * This method is required by the interface Iterator.
+ */
+ public function next()
+ {
+ $this->_index++;
+ }
+
+ /**
+ * Returns whether there is an item at current position.
+ * This method is required by the interface Iterator.
+ * @return boolean
+ */
+ public function valid()
+ {
+ return $this->_index<$this->_count;
+ }
+}
+
+?> \ No newline at end of file
diff --git a/framework/Collections/TPagedDataSource.php b/framework/Collections/TPagedDataSource.php
index 71e7c9e6..02c15b64 100644
--- a/framework/Collections/TPagedDataSource.php
+++ b/framework/Collections/TPagedDataSource.php
@@ -1,5 +1,33 @@
<?php
+/**
+ * TPagedDataSource, TPagedListIterator, TPagedMapIterator classes
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @link http://www.pradosoft.com/
+ * @copyright Copyright &copy; 2005 PradoSoft
+ * @license http://www.pradosoft.com/license/
+ * @version $Revision: $ $Date: $
+ * @package System.Collections
+ */
+/**
+ * TPagedDataSource class
+ *
+ * TPagedDataSource implements an integer-indexed collection class with paging functionality.
+ *
+ * Data items in TPagedDataSource can be traversed using <b>foreach</b>
+ * PHP statement like the following,
+ * <code>
+ * foreach($pagedDataSource as $dataItem)
+ * </code>
+ * The data are fetched from {@link setDataSource DataSource}. Only the items
+ * within the specified page will be returned and traversed.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @version $Revision: $ $Date: $
+ * @package System.Collections
+ * @since 3.0
+ */
class TPagedDataSource extends TComponent implements IteratorAggregate
{
/**
@@ -214,6 +242,9 @@ class TPagedDataSource extends TComponent implements IteratorAggregate
return $this->_dataSource->getCount();
}
+ /**
+ * @return Iterator iterator
+ */
public function getIterator()
{
if($this->_dataSource instanceof TList)
@@ -228,12 +259,12 @@ class TPagedDataSource extends TComponent implements IteratorAggregate
/**
- * TListIterator class
+ * TPagedListIterator class
*
- * TListIterator implements Iterator interface.
+ * TPagedListIterator implements Iterator interface.
*
- * TListIterator is used by TList. It allows TList to return a new iterator
- * for traversing the items in the list.
+ * TPagedListIterator is used by {@link TPagedDataSource}. It allows TPagedDataSource
+ * to return a new iterator for traversing the items in a {@link TList} object.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Revision: $ $Date: $
@@ -249,7 +280,9 @@ class TPagedListIterator implements Iterator
/**
* Constructor.
- * @param array the data to be iterated through
+ * @param TList the data to be iterated through
+ * @param integer start index
+ * @param integer number of items to be iterated through
*/
public function __construct(TList $list,$startIndex,$count)
{
@@ -311,6 +344,19 @@ class TPagedListIterator implements Iterator
}
}
+/**
+ * TPagedMapIterator class
+ *
+ * TPagedMapIterator implements Iterator interface.
+ *
+ * TPagedMapIterator is used by {@link TPagedDataSource}. It allows TPagedDataSource
+ * to return a new iterator for traversing the items in a {@link TMap} object.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @version $Revision: $ $Date: $
+ * @package System.Collections
+ * @since 3.0
+ */
class TPagedMapIterator implements Iterator
{
private $_map;
diff --git a/framework/Web/UI/TForm.php b/framework/Web/UI/TForm.php
index 4cb97911..7155627a 100644
--- a/framework/Web/UI/TForm.php
+++ b/framework/Web/UI/TForm.php
@@ -11,13 +11,10 @@ class TForm extends TControl
protected function addAttributesToRender($writer)
{
$attributes=$this->getAttributes();
-// $writer->addAttribute('name',$this->getName());
$writer->addAttribute('method',$this->getMethod());
$writer->addAttribute('action',$this->getRequest()->getRequestURI());
if(($enctype=$this->getEnctype())!=='')
$writer->addAttribute('enctype',$enctype);
- $attributes->remove('name');
- $attributes->remove('method');
$attributes->remove('action');
$page=$this->getPage();
@@ -35,6 +32,7 @@ class TForm extends TControl
}*/
if($this->getDefaultButton()!=='')
{//todo
+ /*
$control=$this->findControl($this->getDefaultButton());
if(!$control)
$control=$page->findControl($this->getDefaultButton());
@@ -42,6 +40,7 @@ class TForm extends TControl
$page->getClientScript()->registerDefaultButtonScript($control,$writer,false);
else
throw new Exception('Only IButtonControl can be default button.');
+ */
}
$writer->addAttribute('id',$this->getClientID());
foreach($attributes as $name=>$value)
@@ -101,7 +100,7 @@ class TForm extends TControl
{
$this->setViewState('Enctype',$value,'');
}
-
+/*
public function getSubmitDisabledControls()
{
return $this->getViewState('SubmitDisabledControls',false);
@@ -111,7 +110,7 @@ class TForm extends TControl
{
$this->setViewState('SubmitDisabledControls',TPropertyValue::ensureBoolean($value),false);
}
-
+*/
public function getName()
{
return $this->getUniqueID();
diff --git a/framework/Web/UI/WebControls/TDataGrid.php b/framework/Web/UI/WebControls/TDataGrid.php
index f25f8036..1042ba84 100644
--- a/framework/Web/UI/WebControls/TDataGrid.php
+++ b/framework/Web/UI/WebControls/TDataGrid.php
@@ -12,6 +12,7 @@
Prado::using('System.Web.UI.WebControls.TBaseDataList');
Prado::using('System.Collections.TPagedDataSource');
+Prado::using('System.Collections.TDummyDataSource');
Prado::using('System.Web.UI.WebControls.TTable');
/**
@@ -725,6 +726,15 @@ class TDataGrid extends TBaseDataList
$this->setViewState('ItemCount',$this->_items->getCount(),0);
else
$this->clearViewState('ItemCount');
+ if($this->_autoColumns)
+ {
+ $state=array();
+ foreach($this->_autoColumns as $column)
+ $state[]=$column->saveState();
+ $this->setViewState('ColumnState',$state,array());
+ }
+ else
+ $this->clearViewState('ColumnState');
}
/**
@@ -735,7 +745,17 @@ class TDataGrid extends TBaseDataList
protected function onLoadState($param)
{
if(!$this->getIsDataBound())
- $this->restoreItemsFromViewState();
+ {
+ $state=$this->getViewState('ColumnState',array());
+ $columns=$this->getAutoColumns();
+ foreach($state as $st)
+ {
+ $column=new TBoundColumn;
+ $column->loadState($st);
+ $columns->add($column);
+ }
+ $this->restoreGridFromViewState();
+ }
$this->clearViewState('ItemCount');
}
@@ -764,21 +784,28 @@ class TDataGrid extends TBaseDataList
protected function restoreGridFromViewState()
{
$this->reset();
+ if(($itemCount=$this->getViewState('ItemCount',0))<=0)
+ return;
$this->_pagedDataSource=$ds=$this->createPagedDataSource();
- // set dummy data source
- // create columns from viewstate
+ if($ds->getAllowCustomPaging())
+ $ds->setDataSource(new TDummyDataSource($itemCount));
+ else
+ $ds->setDataSource(new TDummyDataSource($this->getViewState('DataSourceCount',0));
+
+ $columns=new TList($this->getColumns());
+ $columns->mergeWith($this->_autoColumns);
+
if($columns->getCount()>0)
{
foreach($columns as $column)
$column->initialize();
- $allowPaging=$ds->getAllowPaging();
if($allowPaging)
- // create pager
- // create header
- // may need to use the first row of data to build items here
+ $this->createPager(-1,-1,$columnCount,$ds);
+ $this->createItemInternal(-1,-1,'Header',false,null,$columns);
$selectedIndex=$this->getSelectedItemIndex();
$editIndex=$this->getEditItemIndex();
$index=0;
+ $dsIndex=$ds->getAllowPaging()?$ds->getFirstIndexInPage():0;
foreach($ds as $data)
{
if($index===$editIndex)
@@ -789,11 +816,13 @@ class TDataGrid extends TBaseDataList
$itemType='AlternatingItem';
else
$itemType='Item';
- // create item
+ $items->add($this->createItemInternal($index,$dsIndex,$itemType,false,null,$columns));
$index++;
+ $dsIndex++;
}
- // create footer
- // create pager
+ $this->createItemInternal(-1,-1,'Footer',false,null,$columns);
+ if($allowPaging)
+ $this->createPager(-1,-1,$columnCount,$ds);
}
$this->_pagedDataSource=null;
}
@@ -815,7 +844,11 @@ class TDataGrid extends TBaseDataList
$allowPaging=$ds->getAllowPaging();
if($allowPaging && $ds->getCurrentPageIndex()>=$ds->getPageCount())
throw new TInvalidDataValueException('datagrid_currentpageindex_invalid');
- $columns=$this->getAllColumns($ds);
+ // get all columns
+ $columns=new TList($this->getColumns());
+ $autoColumns=$this->createAutoColumns($ds);
+ $columns->mergeWith($autoColumns);
+
$items=$this->getItems();
if(($columnCount=$columns->getCount())>0)
@@ -1046,16 +1079,9 @@ class TDataGrid extends TBaseDataList
}
}
- protected function getAllColumns($dataSource)
- {
- $list=new TList($this->getColumns());
- $list->mergeWith($this->createAutoColumns($dataSource));
- return $list;
- }
-
protected function createAutoColumns($dataSource)
{
- if(!$dataSource || $dataSource->getCount()<=0)
+ if(!$dataSource)
return null;
$autoColumns=$this->getAutoColumns();
$autoColumns->clear();
@@ -1085,6 +1111,114 @@ class TDataGrid extends TBaseDataList
}
return $autoColumns;
}
+
+ /**
+ * Applies styles to items, header, footer and separators.
+ * Item styles are applied in a hierarchical way. Style in higher hierarchy
+ * will inherit from styles in lower hierarchy.
+ * Starting from the lowest hierarchy, the item styles include
+ * item's own style, {@link getItemStyle ItemStyle}, {@link getAlternatingItemStyle AlternatingItemStyle},
+ * {@link getSelectedItemStyle SelectedItemStyle}, and {@link getEditItemStyle EditItemStyle}.
+ * Therefore, if background color is set as red in {@link getItemStyle ItemStyle},
+ * {@link getEditItemStyle EditItemStyle} will also have red background color
+ * unless it is set to a different value explicitly.
+ */
+ protected function applyItemStyles()
+ {
+ $headerStyle=$this->getViewState('HeaderStyle',null);
+ $footerStyle=$this->getViewState('FooterStyle',null);
+ $pagerStyle=$this->getViewState('PagerStyle',null);
+ $separatorStyle=$this->getViewState('SeparatorStyle',null);
+ $itemStyle=$this->getViewState('ItemStyle',null);
+ $alternatingItemStyle=$this->getViewState('AlternatingItemStyle',null);
+ if($itemStyle!==null)
+ {
+ if($alternatingItemStyle===null)
+ $alternatingItemStyle=new TTableItemStyle;
+ $alternatingItemStyle->mergeWith($itemStyle);
+ }
+ $selectedItemStyle=$this->getViewState('SelectedItemStyle',null);
+ if($alternatingItemStyle!==null)
+ {
+ if($selectedItemStyle===null)
+ $selectedItemStyle=new TTableItemStyle;
+ $selectedItemStyle->mergeWith($alternatingItemStyle);
+ }
+ $editItemStyle=$this->getViewState('EditItemStyle',null);
+ if($selectedItemStyle!==null)
+ {
+ if($editItemStyle===null)
+ $editItemStyle=new TTableItemStyle;
+ $editItemStyle->mergeWith($selectedItemStyle);
+ }
+
+ foreach($this->getControls() as $index=>$control)
+ {
+ switch($control->getItemType())
+ {
+ case 'Header':
+ if($headerStyle)
+ $control->getStyle()->mergeWith($headerStyle);
+ if(!$this->getShowHeader())
+ $control->setVisible(false);
+ break;
+ case 'Footer':
+ if($footerStyle)
+ $control->getStyle()->mergeWith($footerStyle);
+ if(!$this->getShowFooter())
+ $control->setVisible(false);
+ break;
+ case 'Separator':
+ if($separatorStyle)
+ $control->getStyle()->mergeWith($separatorStyle);
+ break;
+ case 'Item':
+ if($itemStyle)
+ $control->getStyle()->mergeWith($itemStyle);
+ break;
+ case 'AlternatingItem':
+ if($alternatingItemStyle)
+ $control->getStyle()->mergeWith($alternatingItemStyle);
+ break;
+ case 'SelectedItem':
+ if($selectedItemStyle)
+ $control->getStyle()->mergeWith($selectedItemStyle);
+ break;
+ case 'EditItem':
+ if($editItemStyle)
+ $control->getStyle()->mergeWith($editItemStyle);
+ break;
+ case 'Pager':
+ if($pagerStyle)
+ {
+ $control->getStyle()->mergeWith($pagerStyle);
+ $mode=$pagerStyle->getMode();
+ if($index===0)
+ {
+ if($mode==='Bottom')
+ $control->setVisible(false);
+ }
+ else
+ {
+ if($mode==='Top')
+ $control->setVisible(false);
+ }
+ }
+ break;
+ default:
+ break;
+ }
+ }
+ }
+
+ protected function renderContents($writer)
+ {
+ if($this->getHasControls())
+ {
+ $this->applyItemStyles();
+ parent::renderContents($writer);
+ }
+ }
}
/**