From 5c333928f7f328319f1de4486d5e7ce3a54fc2ce Mon Sep 17 00:00:00 2001 From: xue <> Date: Thu, 19 Jan 2006 05:37:08 +0000 Subject: Added DummyDataSource. TDataGrid completed (tests pending). --- framework/Collections/TDummyDataSource.php | 131 ++++++++++++++++++++++ framework/Collections/TPagedDataSource.php | 56 +++++++++- framework/Web/UI/TForm.php | 9 +- framework/Web/UI/WebControls/TDataGrid.php | 172 +++++++++++++++++++++++++---- 4 files changed, 339 insertions(+), 29 deletions(-) create mode 100644 framework/Collections/TDummyDataSource.php (limited to 'framework') 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 @@ + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 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 foreach + * PHP statement like the following, + * + * foreach($dummyDataSource as $dataItem) + * + * + * @author Qiang Xue + * @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 + * @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 @@ + * @link http://www.pradosoft.com/ + * @copyright Copyright © 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 foreach + * PHP statement like the following, + * + * foreach($pagedDataSource as $dataItem) + * + * The data are fetched from {@link setDataSource DataSource}. Only the items + * within the specified page will be returned and traversed. + * + * @author Qiang Xue + * @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 * @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 + * @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); + } + } } /** -- cgit v1.2.3