diff options
Diffstat (limited to 'framework/Data/ActiveRecord/Scaffold')
5 files changed, 1110 insertions, 1110 deletions
diff --git a/framework/Data/ActiveRecord/Scaffold/TScaffoldBase.php b/framework/Data/ActiveRecord/Scaffold/TScaffoldBase.php index d2e58bc3..68f5806a 100644 --- a/framework/Data/ActiveRecord/Scaffold/TScaffoldBase.php +++ b/framework/Data/ActiveRecord/Scaffold/TScaffoldBase.php @@ -1,207 +1,207 @@ -<?php
-/**
- * TScaffoldBase class file.
- *
- * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
- * @link http://www.pradosoft.com/
- * @copyright Copyright © 2005-2012 PradoSoft
- * @license http://www.pradosoft.com/license/
- * @version $Id$
- * @package System.Data.ActiveRecord.Scaffold
- */
-
-/**
- * Include the base Active Record class.
- */
-Prado::using('System.Data.ActiveRecord.TActiveRecord');
-
-/**
- * Base class for Active Record scaffold views.
- *
- * Provides common properties for all scaffold views (such as, TScaffoldListView,
- * TScaffoldEditView, TScaffoldListView and TScaffoldView).
- *
- * During the OnPrRender stage the default css style file (filename style.css)
- * is published and registered. To override the default style, provide your own stylesheet
- * file explicitly.
- *
- * @author Wei Zhuo <weizho[at]gmail[dot]com>
- * @version $Id$
- * @package System.Data.ActiveRecord.Scaffold
- * @since 3.1
- */
-abstract class TScaffoldBase extends TTemplateControl
-{
- /**
- * @var TActiveRecord record instance (may be new or retrieved from db)
- */
- private $_record;
-
- /**
- * @return TDbMetaData table/view information
- */
- protected function getTableInfo()
- {
- $finder = $this->getRecordFinder();
- $gateway = $finder->getRecordManager()->getRecordGateWay();
- return $gateway->getRecordTableInfo($finder);
- }
-
- /**
- * @param TActiveRecord record instance
- * @return array record property values
- */
- protected function getRecordPropertyValues($record)
- {
- $data = array();
- foreach($this->getTableInfo()->getColumns() as $name=>$column)
- $data[] = $record->getColumnValue($name);
- return $data;
- }
-
- /**
- * @param TActiveRecord record instance
- * @return array record primary key values.
- */
- protected function getRecordPkValues($record)
- {
- $data=array();
- foreach($this->getTableInfo()->getColumns() as $name=>$column)
- {
- if($column->getIsPrimaryKey())
- $data[] = $record->getColumnValue($name);
- }
- return $data;
- }
-
- /**
- * Name of the Active Record class to be viewed or scaffolded.
- * @return string Active Record class name.
- */
- public function getRecordClass()
- {
- return $this->getViewState('RecordClass');
- }
-
- /**
- * Name of the Active Record class to be viewed or scaffolded.
- * @param string Active Record class name.
- */
- public function setRecordClass($value)
- {
- $this->setViewState('RecordClass', $value);
- }
-
- /**
- * Copy the view details from another scaffold view instance.
- * @param TScaffoldBase scaffold view.
- */
- protected function copyFrom(TScaffoldBase $obj)
- {
- $this->_record = $obj->_record;
- $this->setRecordClass($obj->getRecordClass());
- $this->setEnableDefaultStyle($obj->getEnableDefaultStyle());
- }
-
- /**
- * Unset the current record instance and table information.
- */
- protected function clearRecordObject()
- {
- $this->_record=null;
- }
-
- /**
- * Gets the current Active Record instance. Creates new instance if the
- * primary key value is null otherwise the record is fetched from the db.
- * @param array primary key value
- * @return TActiveRecord record instance
- */
- protected function getRecordObject($pk=null)
- {
- if($this->_record===null)
- {
- if($pk!==null)
- {
- $this->_record=$this->getRecordFinder()->findByPk($pk);
- if($this->_record===null)
- throw new TConfigurationException('scaffold_invalid_record_pk',
- $this->getRecordClass(), $pk);
- }
- else
- {
- $class = $this->getRecordClass();
- if($class!==null)
- $this->_record=Prado::createComponent($class);
- else
- {
- throw new TConfigurationException('scaffold_invalid_record_class',
- $this->getRecordClass(),$this->getID());
- }
- }
- }
- return $this->_record;
- }
-
- /**
- * @param TActiveRecord Active Record instance.
- */
- protected function setRecordObject(TActiveRecord $value)
- {
- $this->_record=$value;
- }
-
- /**
- * @return TActiveRecord Active Record finder instance
- */
- protected function getRecordFinder()
- {
- return TActiveRecord::finder($this->getRecordClass());
- }
-
- /**
- * @return string default scaffold stylesheet name
- */
- public function getDefaultStyle()
- {
- return $this->getViewState('DefaultStyle', 'style');
- }
-
- /**
- * @param string default scaffold stylesheet name
- */
- public function setDefaultStyle($value)
- {
- $this->setViewState('DefaultStyle', TPropertyValue::ensureString($value), 'style');
- }
-
- /**
- * @return boolean enable default stylesheet, default is true.
- */
- public function getEnableDefaultStyle()
- {
- return $this->getViewState('EnableDefaultStyle', true);
- }
-
- /**
- * @param boolean enable default stylesheet, default is true.
- */
- public function setEnableDefaultStyle($value)
- {
- return $this->setViewState('EnableDefaultStyle', TPropertyValue::ensureBoolean($value), true);
- }
-
- /**
- * Publish the default stylesheet file.
- */
- public function onPreRender($param)
- {
- parent::onPreRender($param);
- if($this->getEnableDefaultStyle())
- {
- $url = $this->publishAsset($this->getDefaultStyle().'.css');
- $this->getPage()->getClientScript()->registerStyleSheetFile($url,$url);
- }
- }
-}
-
+<?php +/** + * TScaffoldBase class file. + * + * @author Wei Zhuo <weizhuo[at]gmail[dot]com> + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2012 PradoSoft + * @license http://www.pradosoft.com/license/ + * @version $Id$ + * @package System.Data.ActiveRecord.Scaffold + */ + +/** + * Include the base Active Record class. + */ +Prado::using('System.Data.ActiveRecord.TActiveRecord'); + +/** + * Base class for Active Record scaffold views. + * + * Provides common properties for all scaffold views (such as, TScaffoldListView, + * TScaffoldEditView, TScaffoldListView and TScaffoldView). + * + * During the OnPrRender stage the default css style file (filename style.css) + * is published and registered. To override the default style, provide your own stylesheet + * file explicitly. + * + * @author Wei Zhuo <weizho[at]gmail[dot]com> + * @version $Id$ + * @package System.Data.ActiveRecord.Scaffold + * @since 3.1 + */ +abstract class TScaffoldBase extends TTemplateControl +{ + /** + * @var TActiveRecord record instance (may be new or retrieved from db) + */ + private $_record; + + /** + * @return TDbMetaData table/view information + */ + protected function getTableInfo() + { + $finder = $this->getRecordFinder(); + $gateway = $finder->getRecordManager()->getRecordGateWay(); + return $gateway->getRecordTableInfo($finder); + } + + /** + * @param TActiveRecord record instance + * @return array record property values + */ + protected function getRecordPropertyValues($record) + { + $data = array(); + foreach($this->getTableInfo()->getColumns() as $name=>$column) + $data[] = $record->getColumnValue($name); + return $data; + } + + /** + * @param TActiveRecord record instance + * @return array record primary key values. + */ + protected function getRecordPkValues($record) + { + $data=array(); + foreach($this->getTableInfo()->getColumns() as $name=>$column) + { + if($column->getIsPrimaryKey()) + $data[] = $record->getColumnValue($name); + } + return $data; + } + + /** + * Name of the Active Record class to be viewed or scaffolded. + * @return string Active Record class name. + */ + public function getRecordClass() + { + return $this->getViewState('RecordClass'); + } + + /** + * Name of the Active Record class to be viewed or scaffolded. + * @param string Active Record class name. + */ + public function setRecordClass($value) + { + $this->setViewState('RecordClass', $value); + } + + /** + * Copy the view details from another scaffold view instance. + * @param TScaffoldBase scaffold view. + */ + protected function copyFrom(TScaffoldBase $obj) + { + $this->_record = $obj->_record; + $this->setRecordClass($obj->getRecordClass()); + $this->setEnableDefaultStyle($obj->getEnableDefaultStyle()); + } + + /** + * Unset the current record instance and table information. + */ + protected function clearRecordObject() + { + $this->_record=null; + } + + /** + * Gets the current Active Record instance. Creates new instance if the + * primary key value is null otherwise the record is fetched from the db. + * @param array primary key value + * @return TActiveRecord record instance + */ + protected function getRecordObject($pk=null) + { + if($this->_record===null) + { + if($pk!==null) + { + $this->_record=$this->getRecordFinder()->findByPk($pk); + if($this->_record===null) + throw new TConfigurationException('scaffold_invalid_record_pk', + $this->getRecordClass(), $pk); + } + else + { + $class = $this->getRecordClass(); + if($class!==null) + $this->_record=Prado::createComponent($class); + else + { + throw new TConfigurationException('scaffold_invalid_record_class', + $this->getRecordClass(),$this->getID()); + } + } + } + return $this->_record; + } + + /** + * @param TActiveRecord Active Record instance. + */ + protected function setRecordObject(TActiveRecord $value) + { + $this->_record=$value; + } + + /** + * @return TActiveRecord Active Record finder instance + */ + protected function getRecordFinder() + { + return TActiveRecord::finder($this->getRecordClass()); + } + + /** + * @return string default scaffold stylesheet name + */ + public function getDefaultStyle() + { + return $this->getViewState('DefaultStyle', 'style'); + } + + /** + * @param string default scaffold stylesheet name + */ + public function setDefaultStyle($value) + { + $this->setViewState('DefaultStyle', TPropertyValue::ensureString($value), 'style'); + } + + /** + * @return boolean enable default stylesheet, default is true. + */ + public function getEnableDefaultStyle() + { + return $this->getViewState('EnableDefaultStyle', true); + } + + /** + * @param boolean enable default stylesheet, default is true. + */ + public function setEnableDefaultStyle($value) + { + return $this->setViewState('EnableDefaultStyle', TPropertyValue::ensureBoolean($value), true); + } + + /** + * Publish the default stylesheet file. + */ + public function onPreRender($param) + { + parent::onPreRender($param); + if($this->getEnableDefaultStyle()) + { + $url = $this->publishAsset($this->getDefaultStyle().'.css'); + $this->getPage()->getClientScript()->registerStyleSheetFile($url,$url); + } + } +} + diff --git a/framework/Data/ActiveRecord/Scaffold/TScaffoldEditView.php b/framework/Data/ActiveRecord/Scaffold/TScaffoldEditView.php index c0547627..166e3360 100644 --- a/framework/Data/ActiveRecord/Scaffold/TScaffoldEditView.php +++ b/framework/Data/ActiveRecord/Scaffold/TScaffoldEditView.php @@ -1,309 +1,309 @@ -<?php
-/**
- * TScaffoldEditView class and IScaffoldEditRenderer interface file.
- *
- * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
- * @link http://www.pradosoft.com/
+<?php +/** + * TScaffoldEditView class and IScaffoldEditRenderer interface file. + * + * @author Wei Zhuo <weizhuo[at]gmail[dot]com> + * @link http://www.pradosoft.com/ * @copyright Copyright © 2005-2012 PradoSoft - * @license http://www.pradosoft.com/license/
- * @version $Id$
- * @package System.Data.ActiveRecord.Scaffold
- */
-
-/**
- * Load scaffold base.
- */
-Prado::using('System.Data.ActiveRecord.Scaffold.TScaffoldBase');
-
-/**
- * Template control for editing an Active Record instance.
- * The <b>RecordClass</b> determines the Active Record class to be edited.
- * A particular record can be edited by specifying the {@link setRecordPk RecordPk}
- * value (may be an array for composite keys).
- *
- * The default editor input controls are created based on the column types.
- * The editor layout can be specified by a renderer by set the value
- * of the {@link setEditRenderer EditRenderer} property to the class name of a
- * class that implements TScaffoldEditRenderer. A renderer is an external
- * template control that implements IScaffoldEditRenderer.
- *
- * The <b>Data</b> of the IScaffoldEditRenderer will be set as the current Active
- * Record to be edited. The <b>UpdateRecord()</b> method of IScaffoldEditRenderer
- * is called when request to save the record is requested.
- *
- * Validators in the custom external editor template should have the
- * {@link TBaseValidator::setValidationGroup ValidationGroup} property set to the
- * value of the {@link getValidationGroup} of the TScaffoldEditView instance
- * (the edit view instance is the <b>Parent</b> of the IScaffoldEditRenderer in most
- * cases.
- *
- * Cosmetic changes to the default editor should be done using Cascading Stylesheets.
- * For example, a particular field/property can be hidden by specifying "display:none" for
- * the corresponding style (each field/property has unique Css class name as "property_xxx", where
- * xxx is the property name).
- *
- * @author Wei Zhuo <weizho[at]gmail[dot]com>
- * @version $Id$
- * @package System.Data.ActiveRecord.Scaffold
- * @since 3.1
- */
-class TScaffoldEditView extends TScaffoldBase
-{
- /**
- * @var IScaffoldEditRenderer custom scaffold edit renderer
- */
- private $_editRenderer;
-
- /**
- * Initialize the editor form if it is Visible.
- */
- public function onLoad($param)
- {
- if($this->getVisible())
- $this->initializeEditForm();
- }
-
- /**
- * @return string the class name for scaffold editor. Defaults to empty, meaning not set.
- */
- public function getEditRenderer()
- {
- return $this->getViewState('EditRenderer', '');
- }
-
- /**
- * @param string the class name for scaffold editor. Defaults to empty, meaning not set.
- */
- public function setEditRenderer($value)
- {
- $this->setViewState('EditRenderer', $value, '');
- }
-
- /**
- * @param array Active Record primary key value to be edited.
- */
- public function setRecordPk($value)
- {
- $this->clearRecordObject();
- $val = TPropertyValue::ensureArray($value);
- $this->setViewState('PK', count($val) > 0 ? $val : null);
- }
-
- /**
- * @return array Active Record primary key value.
- */
- public function getRecordPk()
- {
- return $this->getViewState('PK');
- }
-
- /**
- * @return TActiveRecord current Active Record instance
- */
- protected function getCurrentRecord()
- {
- return $this->getRecordObject($this->getRecordPk());
- }
-
- /**
- * Initialize the editor form
- */
- public function initializeEditForm()
- {
- $record = $this->getCurrentRecord();
- $classPath = $this->getEditRenderer();
- if($classPath === '')
- {
- $columns = $this->getTableInfo()->getColumns();
- $this->getInputRepeater()->setDataSource($columns);
- $this->getInputRepeater()->dataBind();
- }
- else
- {
- if($this->_editRenderer===null)
- $this->createEditRenderer($record, $classPath);
- else
- $this->_editRenderer->setData($record);
- }
- }
-
- /**
- * Instantiate the external edit renderer.
- * @param TActiveRecord record to be edited
- * @param string external edit renderer class name.
- * @throws TConfigurationException raised when renderer is not an
- * instance of IScaffoldEditRenderer.
- */
- protected function createEditRenderer($record, $classPath)
- {
- $this->_editRenderer = Prado::createComponent($classPath);
- if($this->_editRenderer instanceof IScaffoldEditRenderer)
- {
- $index = $this->getControls()->remove($this->getInputRepeater());
- $this->getControls()->insertAt($index,$this->_editRenderer);
- $this->_editRenderer->setData($record);
- }
- else
- {
- throw new TConfigurationException(
- 'scaffold_invalid_edit_renderer', $this->getID(), get_class($record));
- }
- }
-
- /**
- * Initialize the default editor using the scaffold input builder.
- */
- protected function createRepeaterEditItem($sender, $param)
- {
- $type = $param->getItem()->getItemType();
- if($type==TListItemType::Item || $type==TListItemType::AlternatingItem)
- {
- $item = $param->getItem();
- $column = $item->getDataItem();
- if($column===null)
- return;
-
- $record = $this->getCurrentRecord();
- $builder = $this->getScaffoldInputBuilder($record);
- $builder->createScaffoldInput($this, $item, $column, $record);
- }
- }
-
- /**
- * Bubble the command name event. Stops bubbling when the page validator false.
- * Otherwise, the bubble event is continued.
- */
- public function bubbleEvent($sender, $param)
- {
- switch(strtolower($param->getCommandName()))
- {
- case 'save':
- return $this->doSave() ? false : true;
- case 'clear':
- $this->setRecordPk(null);
- $this->initializeEditForm();
- return false;
- default:
- return false;
- }
- }
-
- /**
- * Check the validators, then tries to save the record.
- * @return boolean true if the validators are true, false otherwise.
- */
- protected function doSave()
- {
- if($this->getPage()->getIsValid())
- {
- $record = $this->getCurrentRecord();
- if($this->_editRenderer===null)
- {
- $table = $this->getTableInfo();
- $builder = $this->getScaffoldInputBuilder($record);
- foreach($this->getInputRepeater()->getItems() as $item)
- {
- $column = $table->getColumn($item->getCustomData());
- $builder->loadScaffoldInput($this, $item, $column, $record);
- }
- }
- else
- {
- $this->_editRenderer->updateRecord($record);
- }
- $record->save();
- return true;
- }
- else if($this->_editRenderer!==null)
- {
- //preserve the form data.
- $this->_editRenderer->updateRecord($this->getCurrentRecord());
- }
-
- return false;
- }
-
- /**
- * @return TRepeater default editor input controls repeater
- */
- protected function getInputRepeater()
- {
- $this->ensureChildControls();
- return $this->getRegisteredObject('_repeater');
- }
-
- /**
- * @return TButton Button triggered to save the Active Record.
- */
- public function getSaveButton()
- {
- $this->ensureChildControls();
- return $this->getRegisteredObject('_save');
- }
-
- /**
- * @return TButton Button to clear the editor inputs.
- */
- public function getClearButton()
- {
- $this->ensureChildControls();
- return $this->getRegisteredObject('_clear');
- }
-
- /**
- * @return TButton Button to cancel the edit action (e.g. hide the edit view).
- */
- public function getCancelButton()
- {
- $this->ensureChildControls();
- return $this->getRegisteredObject('_cancel');
- }
-
- /**
- * Create the default scaffold editor control factory.
- * @param TActiveRecord record instance.
- * @return TScaffoldInputBase scaffold editor control factory.
- */
- protected function getScaffoldInputBuilder($record)
- {
- static $_builders=array();
- $class = get_class($record);
- if(!isset($_builders[$class]))
- {
- Prado::using('System.Data.ActiveRecord.Scaffold.InputBuilder.TScaffoldInputBase');
- $_builders[$class] = TScaffoldInputBase::createInputBuilder($record);
- }
- return $_builders[$class];
- }
-
- /**
- * @return string editor validation group name.
- */
- public function getValidationGroup()
- {
- return 'group_'.$this->getUniqueID();
- }
-}
-
-/**
- * IScaffoldEditRenderer interface.
- *
- * IScaffoldEditRenderer defines the interface that an edit renderer
- * needs to implement. Besides the {@link getData Data} property, an edit
- * renderer also needs to provide {@link updateRecord updateRecord} method
- * that is called before the save() method is called on the TActiveRecord.
- *
- * @author Wei Zhuo <weizho[at]gmail[dot]com>
- * @version $Id$
- * @package System.Data.ActiveRecord.Scaffold
- * @since 3.1
- */
-interface IScaffoldEditRenderer extends IDataRenderer
-{
- /**
- * This method should update the record with the user input data.
- * @param TActiveRecord record to be saved.
- */
- public function updateRecord($record);
-}
-
+ * @license http://www.pradosoft.com/license/ + * @version $Id$ + * @package System.Data.ActiveRecord.Scaffold + */ + +/** + * Load scaffold base. + */ +Prado::using('System.Data.ActiveRecord.Scaffold.TScaffoldBase'); + +/** + * Template control for editing an Active Record instance. + * The <b>RecordClass</b> determines the Active Record class to be edited. + * A particular record can be edited by specifying the {@link setRecordPk RecordPk} + * value (may be an array for composite keys). + * + * The default editor input controls are created based on the column types. + * The editor layout can be specified by a renderer by set the value + * of the {@link setEditRenderer EditRenderer} property to the class name of a + * class that implements TScaffoldEditRenderer. A renderer is an external + * template control that implements IScaffoldEditRenderer. + * + * The <b>Data</b> of the IScaffoldEditRenderer will be set as the current Active + * Record to be edited. The <b>UpdateRecord()</b> method of IScaffoldEditRenderer + * is called when request to save the record is requested. + * + * Validators in the custom external editor template should have the + * {@link TBaseValidator::setValidationGroup ValidationGroup} property set to the + * value of the {@link getValidationGroup} of the TScaffoldEditView instance + * (the edit view instance is the <b>Parent</b> of the IScaffoldEditRenderer in most + * cases. + * + * Cosmetic changes to the default editor should be done using Cascading Stylesheets. + * For example, a particular field/property can be hidden by specifying "display:none" for + * the corresponding style (each field/property has unique Css class name as "property_xxx", where + * xxx is the property name). + * + * @author Wei Zhuo <weizho[at]gmail[dot]com> + * @version $Id$ + * @package System.Data.ActiveRecord.Scaffold + * @since 3.1 + */ +class TScaffoldEditView extends TScaffoldBase +{ + /** + * @var IScaffoldEditRenderer custom scaffold edit renderer + */ + private $_editRenderer; + + /** + * Initialize the editor form if it is Visible. + */ + public function onLoad($param) + { + if($this->getVisible()) + $this->initializeEditForm(); + } + + /** + * @return string the class name for scaffold editor. Defaults to empty, meaning not set. + */ + public function getEditRenderer() + { + return $this->getViewState('EditRenderer', ''); + } + + /** + * @param string the class name for scaffold editor. Defaults to empty, meaning not set. + */ + public function setEditRenderer($value) + { + $this->setViewState('EditRenderer', $value, ''); + } + + /** + * @param array Active Record primary key value to be edited. + */ + public function setRecordPk($value) + { + $this->clearRecordObject(); + $val = TPropertyValue::ensureArray($value); + $this->setViewState('PK', count($val) > 0 ? $val : null); + } + + /** + * @return array Active Record primary key value. + */ + public function getRecordPk() + { + return $this->getViewState('PK'); + } + + /** + * @return TActiveRecord current Active Record instance + */ + protected function getCurrentRecord() + { + return $this->getRecordObject($this->getRecordPk()); + } + + /** + * Initialize the editor form + */ + public function initializeEditForm() + { + $record = $this->getCurrentRecord(); + $classPath = $this->getEditRenderer(); + if($classPath === '') + { + $columns = $this->getTableInfo()->getColumns(); + $this->getInputRepeater()->setDataSource($columns); + $this->getInputRepeater()->dataBind(); + } + else + { + if($this->_editRenderer===null) + $this->createEditRenderer($record, $classPath); + else + $this->_editRenderer->setData($record); + } + } + + /** + * Instantiate the external edit renderer. + * @param TActiveRecord record to be edited + * @param string external edit renderer class name. + * @throws TConfigurationException raised when renderer is not an + * instance of IScaffoldEditRenderer. + */ + protected function createEditRenderer($record, $classPath) + { + $this->_editRenderer = Prado::createComponent($classPath); + if($this->_editRenderer instanceof IScaffoldEditRenderer) + { + $index = $this->getControls()->remove($this->getInputRepeater()); + $this->getControls()->insertAt($index,$this->_editRenderer); + $this->_editRenderer->setData($record); + } + else + { + throw new TConfigurationException( + 'scaffold_invalid_edit_renderer', $this->getID(), get_class($record)); + } + } + + /** + * Initialize the default editor using the scaffold input builder. + */ + protected function createRepeaterEditItem($sender, $param) + { + $type = $param->getItem()->getItemType(); + if($type==TListItemType::Item || $type==TListItemType::AlternatingItem) + { + $item = $param->getItem(); + $column = $item->getDataItem(); + if($column===null) + return; + + $record = $this->getCurrentRecord(); + $builder = $this->getScaffoldInputBuilder($record); + $builder->createScaffoldInput($this, $item, $column, $record); + } + } + + /** + * Bubble the command name event. Stops bubbling when the page validator false. + * Otherwise, the bubble event is continued. + */ + public function bubbleEvent($sender, $param) + { + switch(strtolower($param->getCommandName())) + { + case 'save': + return $this->doSave() ? false : true; + case 'clear': + $this->setRecordPk(null); + $this->initializeEditForm(); + return false; + default: + return false; + } + } + + /** + * Check the validators, then tries to save the record. + * @return boolean true if the validators are true, false otherwise. + */ + protected function doSave() + { + if($this->getPage()->getIsValid()) + { + $record = $this->getCurrentRecord(); + if($this->_editRenderer===null) + { + $table = $this->getTableInfo(); + $builder = $this->getScaffoldInputBuilder($record); + foreach($this->getInputRepeater()->getItems() as $item) + { + $column = $table->getColumn($item->getCustomData()); + $builder->loadScaffoldInput($this, $item, $column, $record); + } + } + else + { + $this->_editRenderer->updateRecord($record); + } + $record->save(); + return true; + } + else if($this->_editRenderer!==null) + { + //preserve the form data. + $this->_editRenderer->updateRecord($this->getCurrentRecord()); + } + + return false; + } + + /** + * @return TRepeater default editor input controls repeater + */ + protected function getInputRepeater() + { + $this->ensureChildControls(); + return $this->getRegisteredObject('_repeater'); + } + + /** + * @return TButton Button triggered to save the Active Record. + */ + public function getSaveButton() + { + $this->ensureChildControls(); + return $this->getRegisteredObject('_save'); + } + + /** + * @return TButton Button to clear the editor inputs. + */ + public function getClearButton() + { + $this->ensureChildControls(); + return $this->getRegisteredObject('_clear'); + } + + /** + * @return TButton Button to cancel the edit action (e.g. hide the edit view). + */ + public function getCancelButton() + { + $this->ensureChildControls(); + return $this->getRegisteredObject('_cancel'); + } + + /** + * Create the default scaffold editor control factory. + * @param TActiveRecord record instance. + * @return TScaffoldInputBase scaffold editor control factory. + */ + protected function getScaffoldInputBuilder($record) + { + static $_builders=array(); + $class = get_class($record); + if(!isset($_builders[$class])) + { + Prado::using('System.Data.ActiveRecord.Scaffold.InputBuilder.TScaffoldInputBase'); + $_builders[$class] = TScaffoldInputBase::createInputBuilder($record); + } + return $_builders[$class]; + } + + /** + * @return string editor validation group name. + */ + public function getValidationGroup() + { + return 'group_'.$this->getUniqueID(); + } +} + +/** + * IScaffoldEditRenderer interface. + * + * IScaffoldEditRenderer defines the interface that an edit renderer + * needs to implement. Besides the {@link getData Data} property, an edit + * renderer also needs to provide {@link updateRecord updateRecord} method + * that is called before the save() method is called on the TActiveRecord. + * + * @author Wei Zhuo <weizho[at]gmail[dot]com> + * @version $Id$ + * @package System.Data.ActiveRecord.Scaffold + * @since 3.1 + */ +interface IScaffoldEditRenderer extends IDataRenderer +{ + /** + * This method should update the record with the user input data. + * @param TActiveRecord record to be saved. + */ + public function updateRecord($record); +} + diff --git a/framework/Data/ActiveRecord/Scaffold/TScaffoldListView.php b/framework/Data/ActiveRecord/Scaffold/TScaffoldListView.php index 953420e3..bed5bf88 100644 --- a/framework/Data/ActiveRecord/Scaffold/TScaffoldListView.php +++ b/framework/Data/ActiveRecord/Scaffold/TScaffoldListView.php @@ -1,306 +1,306 @@ -<?php
-/**
- * TScaffoldListView class file.
- *
- * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
- * @link http://www.pradosoft.com/
+<?php +/** + * TScaffoldListView class file. + * + * @author Wei Zhuo <weizhuo[at]gmail[dot]com> + * @link http://www.pradosoft.com/ * @copyright Copyright © 2005-2012 PradoSoft - * @license http://www.pradosoft.com/license/
- * @version $Id$
- * @package System.Data.ActiveRecord.Scaffold
- */
-
-/**
- * Load the scaffold base class.
- */
-Prado::using('System.Data.ActiveRecord.Scaffold.TScaffoldBase');
-
-/**
- * TScaffoldListView displays a list of Active Records.
- *
- * The {@link getHeader Header} property is a TRepeater displaying the
- * Active Record property/field names. The {@link getSort Sort} property
- * is a drop down list displaying the combination of properties and its possible
- * ordering. The {@link getPager Pager} property is a TPager control displaying
- * the links and/or buttons that navigate to different pages in the Active Record data.
- * The {@link getList List} property is a TRepeater that renders a row of
- * Active Record data.
- *
- * Custom rendering of the each Active Record can be achieved by specifying
- * the ItemTemplate or AlternatingItemTemplate property of the main {@linnk getList List}
- * repeater.
- *
- * The TScaffoldListView will listen for two command events named "delete" and
- * "edit". A "delete" command will delete a the record for the row where the
- * "delete" command is originates. An "edit" command will push
- * the record data to be edited by a TScaffoldEditView with ID specified by the
- * {@link setEditViewID EditViewID}.
- *
- * Additional {@link setSearchCondition SearchCondition} and
- * {@link setSearchParameters SearchParameters} (takes array values) can be
- * specified to customize the records to be shown. The {@link setSearchCondition SearchCondition}
- * will be used as the Condition property of TActiveRecordCriteria, and similarly
- * the {@link setSearchParameters SearchParameters} will be the corresponding
- * Parameters property of TActiveRecordCriteria.
- *
- * @author Wei Zhuo <weizho[at]gmail[dot]com>
- * @version $Id$
- * @package System.Data.ActiveRecord.Scaffold
- * @since 3.1
- */
-class TScaffoldListView extends TScaffoldBase
-{
- /**
- * Initialize the sort drop down list and the column names repeater.
- */
- protected function initializeSort()
- {
- $table = $this->getTableInfo();
- $sorts = array('Sort By', str_repeat('-',15));
- $headers = array();
- foreach($table->getColumns() as $name=>$colum)
- {
- $fname = ucwords(str_replace('_', ' ', $name));
- $sorts[$name.' ASC'] = $fname .' Ascending';
- $sorts[$name.' DESC'] = $fname .' Descending';
- $headers[] = $fname ;
- }
- $this->_sort->setDataSource($sorts);
- $this->_sort->dataBind();
- $this->_header->setDataSource($headers);
- $this->_header->dataBind();
- }
-
- /**
- * Loads and display the data.
- */
- public function onPreRender($param)
- {
- parent::onPreRender($param);
- if(!$this->getPage()->getIsPostBack() || $this->getViewState('CurrentClass')!=$this->getRecordClass())
- {
- $this->initializeSort();
- $this->setViewState('CurrentClass', $this->getRecordClass());
- }
- $this->loadRecordData();
- }
-
- /**
- * Fetch the records and data bind it to the list.
- */
- protected function loadRecordData()
- {
- $search = new TActiveRecordCriteria($this->getSearchCondition(), $this->getSearchParameters());
- $this->_list->setVirtualItemCount($this->getRecordFinder()->count($search));
- $finder = $this->getRecordFinder();
- $criteria = $this->getRecordCriteria();
- $this->_list->setDataSource($finder->findAll($criteria));
- $this->_list->dataBind();
- }
-
- /**
- * @return TActiveRecordCriteria sort/search/paging criteria
- */
- protected function getRecordCriteria()
- {
- $total = $this->_list->getVirtualItemCount();
- $limit = $this->_list->getPageSize();
- $offset = $this->_list->getCurrentPageIndex()*$limit;
- if($offset + $limit > $total)
- $limit = $total - $offset;
- $criteria = new TActiveRecordCriteria($this->getSearchCondition(), $this->getSearchParameters());
- if($limit > 0)
- {
- $criteria->setLimit($limit);
- if($offset <= $total)
- $criteria->setOffset($offset);
- }
- $order = explode(' ',$this->_sort->getSelectedValue(), 2);
- if(is_array($order) && count($order) === 2)
- $criteria->OrdersBy[$order[0]] = $order[1];
- return $criteria;
- }
-
- /**
- * @param string search condition, the SQL string after the WHERE clause.
- */
- public function setSearchCondition($value)
- {
- $this->setViewState('SearchCondition', $value);
- }
-
- /**
- * @param string SQL search condition for list display.
- */
- public function getSearchCondition()
- {
- return $this->getViewState('SearchCondition');
- }
-
- /**
- * @param array search parameters
- */
- public function setSearchParameters($value)
- {
- $this->setViewState('SearchParameters', TPropertyValue::ensureArray($value),array());
- }
-
- /**
- * @return array search parameters
- */
- public function getSearchParameters()
- {
- return $this->getViewState('SearchParameters', array());
- }
-
- /**
- * Continue bubbling the "edit" command, "delete" command is handled in this class.
- */
- public function bubbleEvent($sender, $param)
- {
- switch(strtolower($param->getCommandName()))
- {
- case 'delete':
- return $this->deleteRecord($sender, $param);
- case 'edit':
- $this->initializeEdit($sender, $param);
- }
- $this->raiseBubbleEvent($this, $param);
- return true;
- }
-
- /**
- * Initialize the edit view control form when EditViewID is set.
- */
- protected function initializeEdit($sender, $param)
- {
- if(($ctrl=$this->getEditViewControl())!==null)
- {
- if($param instanceof TRepeaterCommandEventParameter)
- {
- $pk = $param->getItem()->getCustomData();
- $ctrl->setRecordPk($pk);
- $ctrl->initializeEditForm();
- }
- }
- }
-
- /**
- * Deletes an Active Record.
- */
- protected function deleteRecord($sender, $param)
- {
- if($param instanceof TRepeaterCommandEventParameter)
- {
- $pk = $param->getItem()->getCustomData();
- $this->getRecordFinder()->deleteByPk($pk);
- }
- }
-
- /**
- * Initialize the default display for each Active Record item.
- */
- protected function listItemCreated($sender, $param)
- {
- $item = $param->getItem();
- if($item instanceof IItemDataRenderer)
- {
- $type = $item->getItemType();
- if($type==TListItemType::Item || $type==TListItemType::AlternatingItem)
- $this->populateField($sender, $param);
- }
- }
-
- /**
- * Sets the Record primary key to the current repeater item's CustomData.
- * Binds the inner repeater with properties of the current Active Record.
- */
- protected function populateField($sender, $param)
- {
- $item = $param->getItem();
- if(($data = $item->getData()) !== null)
- {
- $item->setCustomData($this->getRecordPkValues($data));
- if(($prop = $item->findControl('_properties'))!==null)
- {
- $item->_properties->setDataSource($this->getRecordPropertyValues($data));
- $item->_properties->dataBind();
- }
- }
- }
-
- /**
- * Updates repeater page index with the pager new index value.
- */
- protected function pageChanged($sender, $param)
- {
- $this->_list->setCurrentPageIndex($param->getNewPageIndex());
- }
-
- /**
- * @return TRepeater Repeater control for Active Record instances.
- */
- public function getList()
- {
- $this->ensureChildControls();
- return $this->getRegisteredObject('_list');
- }
-
- /**
- * @return TPager List pager control.
- */
- public function getPager()
- {
- $this->ensureChildControls();
- return $this->getRegisteredObject('_pager');
- }
-
- /**
- * @return TDropDownList Control that displays and controls the record ordering.
- */
- public function getSort()
- {
- $this->ensureChildControls();
- return $this->getRegisteredObject('_sort');
- }
-
- /**
- * @return TRepeater Repeater control for record property names.
- */
- public function getHeader()
- {
- $this->ensureChildControls();
- return $this->getRegisteredObject('_header');
- }
-
- /**
- * @return string TScaffoldEditView control ID for editing selected Active Record.
- */
- public function getEditViewID()
- {
- return $this->getViewState('EditViewID');
- }
-
- /**
- * @param string TScaffoldEditView control ID for editing selected Active Record.
- */
- public function setEditViewID($value)
- {
- $this->setViewState('EditViewID', $value);
- }
-
- /**
- * @return TScaffoldEditView control for editing selected Active Record, null if EditViewID is not set.
- */
- protected function getEditViewControl()
- {
- if(($id=$this->getEditViewID())!==null)
- {
- $ctrl = $this->getParent()->findControl($id);
- if($ctrl===null)
- throw new TConfigurationException('scaffold_unable_to_find_edit_view', $id);
- return $ctrl;
- }
- }
-}
+ * @license http://www.pradosoft.com/license/ + * @version $Id$ + * @package System.Data.ActiveRecord.Scaffold + */ + +/** + * Load the scaffold base class. + */ +Prado::using('System.Data.ActiveRecord.Scaffold.TScaffoldBase'); + +/** + * TScaffoldListView displays a list of Active Records. + * + * The {@link getHeader Header} property is a TRepeater displaying the + * Active Record property/field names. The {@link getSort Sort} property + * is a drop down list displaying the combination of properties and its possible + * ordering. The {@link getPager Pager} property is a TPager control displaying + * the links and/or buttons that navigate to different pages in the Active Record data. + * The {@link getList List} property is a TRepeater that renders a row of + * Active Record data. + * + * Custom rendering of the each Active Record can be achieved by specifying + * the ItemTemplate or AlternatingItemTemplate property of the main {@linnk getList List} + * repeater. + * + * The TScaffoldListView will listen for two command events named "delete" and + * "edit". A "delete" command will delete a the record for the row where the + * "delete" command is originates. An "edit" command will push + * the record data to be edited by a TScaffoldEditView with ID specified by the + * {@link setEditViewID EditViewID}. + * + * Additional {@link setSearchCondition SearchCondition} and + * {@link setSearchParameters SearchParameters} (takes array values) can be + * specified to customize the records to be shown. The {@link setSearchCondition SearchCondition} + * will be used as the Condition property of TActiveRecordCriteria, and similarly + * the {@link setSearchParameters SearchParameters} will be the corresponding + * Parameters property of TActiveRecordCriteria. + * + * @author Wei Zhuo <weizho[at]gmail[dot]com> + * @version $Id$ + * @package System.Data.ActiveRecord.Scaffold + * @since 3.1 + */ +class TScaffoldListView extends TScaffoldBase +{ + /** + * Initialize the sort drop down list and the column names repeater. + */ + protected function initializeSort() + { + $table = $this->getTableInfo(); + $sorts = array('Sort By', str_repeat('-',15)); + $headers = array(); + foreach($table->getColumns() as $name=>$colum) + { + $fname = ucwords(str_replace('_', ' ', $name)); + $sorts[$name.' ASC'] = $fname .' Ascending'; + $sorts[$name.' DESC'] = $fname .' Descending'; + $headers[] = $fname ; + } + $this->_sort->setDataSource($sorts); + $this->_sort->dataBind(); + $this->_header->setDataSource($headers); + $this->_header->dataBind(); + } + + /** + * Loads and display the data. + */ + public function onPreRender($param) + { + parent::onPreRender($param); + if(!$this->getPage()->getIsPostBack() || $this->getViewState('CurrentClass')!=$this->getRecordClass()) + { + $this->initializeSort(); + $this->setViewState('CurrentClass', $this->getRecordClass()); + } + $this->loadRecordData(); + } + + /** + * Fetch the records and data bind it to the list. + */ + protected function loadRecordData() + { + $search = new TActiveRecordCriteria($this->getSearchCondition(), $this->getSearchParameters()); + $this->_list->setVirtualItemCount($this->getRecordFinder()->count($search)); + $finder = $this->getRecordFinder(); + $criteria = $this->getRecordCriteria(); + $this->_list->setDataSource($finder->findAll($criteria)); + $this->_list->dataBind(); + } + + /** + * @return TActiveRecordCriteria sort/search/paging criteria + */ + protected function getRecordCriteria() + { + $total = $this->_list->getVirtualItemCount(); + $limit = $this->_list->getPageSize(); + $offset = $this->_list->getCurrentPageIndex()*$limit; + if($offset + $limit > $total) + $limit = $total - $offset; + $criteria = new TActiveRecordCriteria($this->getSearchCondition(), $this->getSearchParameters()); + if($limit > 0) + { + $criteria->setLimit($limit); + if($offset <= $total) + $criteria->setOffset($offset); + } + $order = explode(' ',$this->_sort->getSelectedValue(), 2); + if(is_array($order) && count($order) === 2) + $criteria->OrdersBy[$order[0]] = $order[1]; + return $criteria; + } + + /** + * @param string search condition, the SQL string after the WHERE clause. + */ + public function setSearchCondition($value) + { + $this->setViewState('SearchCondition', $value); + } + + /** + * @param string SQL search condition for list display. + */ + public function getSearchCondition() + { + return $this->getViewState('SearchCondition'); + } + + /** + * @param array search parameters + */ + public function setSearchParameters($value) + { + $this->setViewState('SearchParameters', TPropertyValue::ensureArray($value),array()); + } + + /** + * @return array search parameters + */ + public function getSearchParameters() + { + return $this->getViewState('SearchParameters', array()); + } + + /** + * Continue bubbling the "edit" command, "delete" command is handled in this class. + */ + public function bubbleEvent($sender, $param) + { + switch(strtolower($param->getCommandName())) + { + case 'delete': + return $this->deleteRecord($sender, $param); + case 'edit': + $this->initializeEdit($sender, $param); + } + $this->raiseBubbleEvent($this, $param); + return true; + } + + /** + * Initialize the edit view control form when EditViewID is set. + */ + protected function initializeEdit($sender, $param) + { + if(($ctrl=$this->getEditViewControl())!==null) + { + if($param instanceof TRepeaterCommandEventParameter) + { + $pk = $param->getItem()->getCustomData(); + $ctrl->setRecordPk($pk); + $ctrl->initializeEditForm(); + } + } + } + + /** + * Deletes an Active Record. + */ + protected function deleteRecord($sender, $param) + { + if($param instanceof TRepeaterCommandEventParameter) + { + $pk = $param->getItem()->getCustomData(); + $this->getRecordFinder()->deleteByPk($pk); + } + } + + /** + * Initialize the default display for each Active Record item. + */ + protected function listItemCreated($sender, $param) + { + $item = $param->getItem(); + if($item instanceof IItemDataRenderer) + { + $type = $item->getItemType(); + if($type==TListItemType::Item || $type==TListItemType::AlternatingItem) + $this->populateField($sender, $param); + } + } + + /** + * Sets the Record primary key to the current repeater item's CustomData. + * Binds the inner repeater with properties of the current Active Record. + */ + protected function populateField($sender, $param) + { + $item = $param->getItem(); + if(($data = $item->getData()) !== null) + { + $item->setCustomData($this->getRecordPkValues($data)); + if(($prop = $item->findControl('_properties'))!==null) + { + $item->_properties->setDataSource($this->getRecordPropertyValues($data)); + $item->_properties->dataBind(); + } + } + } + + /** + * Updates repeater page index with the pager new index value. + */ + protected function pageChanged($sender, $param) + { + $this->_list->setCurrentPageIndex($param->getNewPageIndex()); + } + + /** + * @return TRepeater Repeater control for Active Record instances. + */ + public function getList() + { + $this->ensureChildControls(); + return $this->getRegisteredObject('_list'); + } + + /** + * @return TPager List pager control. + */ + public function getPager() + { + $this->ensureChildControls(); + return $this->getRegisteredObject('_pager'); + } + + /** + * @return TDropDownList Control that displays and controls the record ordering. + */ + public function getSort() + { + $this->ensureChildControls(); + return $this->getRegisteredObject('_sort'); + } + + /** + * @return TRepeater Repeater control for record property names. + */ + public function getHeader() + { + $this->ensureChildControls(); + return $this->getRegisteredObject('_header'); + } + + /** + * @return string TScaffoldEditView control ID for editing selected Active Record. + */ + public function getEditViewID() + { + return $this->getViewState('EditViewID'); + } + + /** + * @param string TScaffoldEditView control ID for editing selected Active Record. + */ + public function setEditViewID($value) + { + $this->setViewState('EditViewID', $value); + } + + /** + * @return TScaffoldEditView control for editing selected Active Record, null if EditViewID is not set. + */ + protected function getEditViewControl() + { + if(($id=$this->getEditViewID())!==null) + { + $ctrl = $this->getParent()->findControl($id); + if($ctrl===null) + throw new TConfigurationException('scaffold_unable_to_find_edit_view', $id); + return $ctrl; + } + } +} diff --git a/framework/Data/ActiveRecord/Scaffold/TScaffoldSearch.php b/framework/Data/ActiveRecord/Scaffold/TScaffoldSearch.php index bd679c94..40335085 100644 --- a/framework/Data/ActiveRecord/Scaffold/TScaffoldSearch.php +++ b/framework/Data/ActiveRecord/Scaffold/TScaffoldSearch.php @@ -1,150 +1,150 @@ -<?php
-/**
- * TScaffoldSearch class file.
- *
- * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
- * @link http://www.pradosoft.com/
+<?php +/** + * TScaffoldSearch class file. + * + * @author Wei Zhuo <weizhuo[at]gmail[dot]com> + * @link http://www.pradosoft.com/ * @copyright Copyright © 2005-2012 PradoSoft - * @license http://www.pradosoft.com/license/
- * @version $Id$
- * @package System.Data.ActiveRecord.Scaffold
- */
-
-/**
- * Import the scaffold base.
- */
-Prado::using('System.Data.ActiveRecord.Scaffold.TScaffoldBase');
-
-/**
- * TScaffoldSearch provide a simple textbox and a button that is used
- * to perform search on a TScaffoldListView with ID given by {@link setListViewID ListViewID}.
- *
- * The {@link getSearchText SearchText} property is a TTextBox and the
- * {@link getSearchButton SearchButton} property is a TButton with label value "Search".
- *
- * Searchable fields of the Active Record can be restricted by specifying
- * a comma delimited string of allowable fields in the
- * {@link setSearchableFields SearchableFields} property. The default is null,
- * meaning that most text type fields are searched (the default searchable fields
- * are database dependent).
- *
- * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
- * @version $Id$
- * @package System.Data.ActiveRecord.Scaffold
- * @since 3.1
- */
-class TScaffoldSearch extends TScaffoldBase
-{
- /**
- * @var TScaffoldListView the scaffold list view.
- */
- private $_list;
-
- /**
- * @return TScaffoldListView the scaffold list view this search box belongs to.
- */
- protected function getListView()
- {
- if($this->_list===null && ($id = $this->getListViewID()) !== null)
- {
- $this->_list = $this->getParent()->findControl($id);
- if($this->_list ===null)
- throw new TConfigurationException('scaffold_unable_to_find_list_view', $id);
- }
- return $this->_list;
- }
-
- /**
- * @param string ID of the TScaffoldListView this search control belongs to.
- */
- public function setListViewID($value)
- {
- $this->setViewState('ListViewID', $value);
- }
-
- /**
- * @return string ID of the TScaffoldListView this search control belongs to.
- */
- public function getListViewID()
- {
- return $this->getViewState('ListViewID');
- }
-
- /**
- * Sets the SearchCondition of the TScaffoldListView as the search terms
- * given by the text of the search text box.
- */
- public function bubbleEvent($sender, $param)
- {
- if(strtolower($param->getCommandName())==='search')
- {
- if(($list = $this->getListView()) !== null)
- {
- $list->setSearchCondition($this->createSearchCondition());
- return false;
- }
- }
- $this->raiseBubbleEvent($this, $param);
- return true;
- }
-
- /**
- * @return string the search criteria for the search terms in the search text box.
- */
- protected function createSearchCondition()
- {
- $table = $this->getTableInfo();
- if(strlen($str=$this->getSearchText()->getText()) > 0)
- {
- $builder = $table->createCommandBuilder($this->getRecordFinder()->getDbConnection());
- return $builder->getSearchExpression($this->getFields(), $str);
- }
- }
-
- /**
- * @return array list of fields to be searched.
- */
- protected function getFields()
- {
- if(strlen(trim($str=$this->getSearchableFields()))>0)
- $fields = preg_split('/\s*,\s*/', $str);
- else
- $fields = $this->getTableInfo()->getColumns()->getKeys();
- return $fields;
- }
-
- /**
- * @return string comma delimited list of fields that may be searched.
- */
- public function getSearchableFields()
- {
- return $this->getViewState('SearchableFields','');
- }
-
- /**
- * @param string comma delimited list of fields that may be searched.
- */
- public function setSearchableFields($value)
- {
- $this->setViewState('SearchableFields', $value, '');
- }
-
- /**
- * @return TButton button with default label "Search".
- */
- public function getSearchButton()
- {
- $this->ensureChildControls();
- return $this->getRegisteredObject('_search');
- }
-
- /**
- * @return TTextBox search text box.
- */
- public function getSearchText()
- {
- $this->ensureChildControls();
- return $this->getRegisteredObject('_textbox');
- }
-}
+ * @license http://www.pradosoft.com/license/ + * @version $Id$ + * @package System.Data.ActiveRecord.Scaffold + */ + +/** + * Import the scaffold base. + */ +Prado::using('System.Data.ActiveRecord.Scaffold.TScaffoldBase'); + +/** + * TScaffoldSearch provide a simple textbox and a button that is used + * to perform search on a TScaffoldListView with ID given by {@link setListViewID ListViewID}. + * + * The {@link getSearchText SearchText} property is a TTextBox and the + * {@link getSearchButton SearchButton} property is a TButton with label value "Search". + * + * Searchable fields of the Active Record can be restricted by specifying + * a comma delimited string of allowable fields in the + * {@link setSearchableFields SearchableFields} property. The default is null, + * meaning that most text type fields are searched (the default searchable fields + * are database dependent). + * + * @author Wei Zhuo <weizhuo[at]gmail[dot]com> + * @version $Id$ + * @package System.Data.ActiveRecord.Scaffold + * @since 3.1 + */ +class TScaffoldSearch extends TScaffoldBase +{ + /** + * @var TScaffoldListView the scaffold list view. + */ + private $_list; + + /** + * @return TScaffoldListView the scaffold list view this search box belongs to. + */ + protected function getListView() + { + if($this->_list===null && ($id = $this->getListViewID()) !== null) + { + $this->_list = $this->getParent()->findControl($id); + if($this->_list ===null) + throw new TConfigurationException('scaffold_unable_to_find_list_view', $id); + } + return $this->_list; + } + + /** + * @param string ID of the TScaffoldListView this search control belongs to. + */ + public function setListViewID($value) + { + $this->setViewState('ListViewID', $value); + } + + /** + * @return string ID of the TScaffoldListView this search control belongs to. + */ + public function getListViewID() + { + return $this->getViewState('ListViewID'); + } + + /** + * Sets the SearchCondition of the TScaffoldListView as the search terms + * given by the text of the search text box. + */ + public function bubbleEvent($sender, $param) + { + if(strtolower($param->getCommandName())==='search') + { + if(($list = $this->getListView()) !== null) + { + $list->setSearchCondition($this->createSearchCondition()); + return false; + } + } + $this->raiseBubbleEvent($this, $param); + return true; + } + + /** + * @return string the search criteria for the search terms in the search text box. + */ + protected function createSearchCondition() + { + $table = $this->getTableInfo(); + if(strlen($str=$this->getSearchText()->getText()) > 0) + { + $builder = $table->createCommandBuilder($this->getRecordFinder()->getDbConnection()); + return $builder->getSearchExpression($this->getFields(), $str); + } + } + + /** + * @return array list of fields to be searched. + */ + protected function getFields() + { + if(strlen(trim($str=$this->getSearchableFields()))>0) + $fields = preg_split('/\s*,\s*/', $str); + else + $fields = $this->getTableInfo()->getColumns()->getKeys(); + return $fields; + } + + /** + * @return string comma delimited list of fields that may be searched. + */ + public function getSearchableFields() + { + return $this->getViewState('SearchableFields',''); + } + + /** + * @param string comma delimited list of fields that may be searched. + */ + public function setSearchableFields($value) + { + $this->setViewState('SearchableFields', $value, ''); + } + + /** + * @return TButton button with default label "Search". + */ + public function getSearchButton() + { + $this->ensureChildControls(); + return $this->getRegisteredObject('_search'); + } + + /** + * @return TTextBox search text box. + */ + public function getSearchText() + { + $this->ensureChildControls(); + return $this->getRegisteredObject('_textbox'); + } +} diff --git a/framework/Data/ActiveRecord/Scaffold/TScaffoldView.php b/framework/Data/ActiveRecord/Scaffold/TScaffoldView.php index 69bc1f81..40eee21d 100644 --- a/framework/Data/ActiveRecord/Scaffold/TScaffoldView.php +++ b/framework/Data/ActiveRecord/Scaffold/TScaffoldView.php @@ -1,143 +1,143 @@ -<?php
-/**
- * TScaffoldView class.
- *
- * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
- * @link http://www.pradosoft.com/
- * @copyright Copyright © 2005-2012 PradoSoft
- * @license http://www.pradosoft.com/license/
- * @version $Id$
- * @package System.Data.ActiveRecord.Scaffold
- */
-
-/**
- * Import scaffold base, list, edit and search controls.
- */
-Prado::using('System.Data.ActiveRecord.Scaffold.TScaffoldBase');
-Prado::using('System.Data.ActiveRecord.Scaffold.TScaffoldListView');
-Prado::using('System.Data.ActiveRecord.Scaffold.TScaffoldEditView');
-Prado::using('System.Data.ActiveRecord.Scaffold.TScaffoldSearch');
-
-/**
- * TScaffoldView is a composite control consisting of TScaffoldListView
- * with a TScaffoldSearch. In addition, it will display a TScaffoldEditView
- * when an "edit" command is raised from the TScaffoldListView (when the
- * edit button is clicked). Futher more, the "add" button can be clicked
- * that shows an empty data TScaffoldListView for creating new records.
- *
- * The {@link getListView ListView} property gives a TScaffoldListView for
- * display the record data. The {@link getEditView EditView} is the
- * TScaffoldEditView that renders the
- * inputs for editing and adding records. The {@link getSearchControl SearchControl}
- * is a TScaffoldSearch responsible to the search user interface.
- *
- * Set the {@link setRecordClass RecordClass} property to the name of
- * the Active Record class to be displayed/edited/added.
- *
- * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
- * @version $Id$
- * @package System.Data.ActiveRecord.Scaffold
- * @since 3.0
- */
-class TScaffoldView extends TScaffoldBase
-{
- /**
- * Copy basic record details to the list/edit/search controls.
- */
- public function onPreRender($param)
- {
- parent::onPreRender($param);
- $this->getListView()->copyFrom($this);
- $this->getEditView()->copyFrom($this);
- $this->getSearchControl()->copyFrom($this);
- }
-
- /**
- * @return TScaffoldListView scaffold list view.
- */
- public function getListView()
- {
- $this->ensureChildControls();
- return $this->getRegisteredObject('_listView');
- }
-
- /**
- * @return TScaffoldEditView scaffold edit view.
- */
- public function getEditView()
- {
- $this->ensureChildControls();
- return $this->getRegisteredObject('_editView');
- }
-
- /**
- * @return TScaffoldSearch scaffold search textbox and button.
- */
- public function getSearchControl()
- {
- $this->ensureChildControls();
- return $this->getRegisteredObject('_search');
- }
-
- /**
- * @return TButton "Add new record" button.
- */
- public function getAddButton()
- {
- $this->ensureChildControls();
- return $this->getRegisteredObject('_newButton');
- }
-
- /**
- * Handle the "edit" and "new" commands by displaying the edit view.
- * Default command shows the list view.
- */
- public function bubbleEvent($sender,$param)
- {
- switch(strtolower($param->getCommandName()))
- {
- case 'edit':
- return $this->showEditView($sender, $param);
- case 'new':
- return $this->showAddView($sender, $param);
- default:
- return $this->showListView($sender, $param);
- }
- return false;
- }
-
- /**
- * Shows the edit record view.
- */
- protected function showEditView($sender, $param)
- {
- $this->getListView()->setVisible(false);
- $this->getEditView()->setVisible(true);
- $this->_panForNewButton->setVisible(false);
- $this->_panForSearch->setVisible(false);
- $this->getEditView()->getCancelButton()->setVisible(true);
- $this->getEditView()->getClearButton()->setVisible(false);
- }
-
- /**
- * Shows the view for listing the records.
- */
- protected function showListView($sender, $param)
- {
- $this->getListView()->setVisible(true);
- $this->getEditView()->setVisible(false);
- $this->_panForNewButton->setVisible(true);
- $this->_panForSearch->setVisible(true);
- }
-
- /**
- * Shows the add record view.
- */
- protected function showAddView($sender, $param)
- {
- $this->getEditView()->setRecordPk(null);
- $this->getEditView()->initializeEditForm();
- $this->showEditView($sender, $param);
- }
-}
-
+<?php +/** + * TScaffoldView class. + * + * @author Wei Zhuo <weizhuo[at]gmail[dot]com> + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2012 PradoSoft + * @license http://www.pradosoft.com/license/ + * @version $Id$ + * @package System.Data.ActiveRecord.Scaffold + */ + +/** + * Import scaffold base, list, edit and search controls. + */ +Prado::using('System.Data.ActiveRecord.Scaffold.TScaffoldBase'); +Prado::using('System.Data.ActiveRecord.Scaffold.TScaffoldListView'); +Prado::using('System.Data.ActiveRecord.Scaffold.TScaffoldEditView'); +Prado::using('System.Data.ActiveRecord.Scaffold.TScaffoldSearch'); + +/** + * TScaffoldView is a composite control consisting of TScaffoldListView + * with a TScaffoldSearch. In addition, it will display a TScaffoldEditView + * when an "edit" command is raised from the TScaffoldListView (when the + * edit button is clicked). Futher more, the "add" button can be clicked + * that shows an empty data TScaffoldListView for creating new records. + * + * The {@link getListView ListView} property gives a TScaffoldListView for + * display the record data. The {@link getEditView EditView} is the + * TScaffoldEditView that renders the + * inputs for editing and adding records. The {@link getSearchControl SearchControl} + * is a TScaffoldSearch responsible to the search user interface. + * + * Set the {@link setRecordClass RecordClass} property to the name of + * the Active Record class to be displayed/edited/added. + * + * @author Wei Zhuo <weizhuo[at]gmail[dot]com> + * @version $Id$ + * @package System.Data.ActiveRecord.Scaffold + * @since 3.0 + */ +class TScaffoldView extends TScaffoldBase +{ + /** + * Copy basic record details to the list/edit/search controls. + */ + public function onPreRender($param) + { + parent::onPreRender($param); + $this->getListView()->copyFrom($this); + $this->getEditView()->copyFrom($this); + $this->getSearchControl()->copyFrom($this); + } + + /** + * @return TScaffoldListView scaffold list view. + */ + public function getListView() + { + $this->ensureChildControls(); + return $this->getRegisteredObject('_listView'); + } + + /** + * @return TScaffoldEditView scaffold edit view. + */ + public function getEditView() + { + $this->ensureChildControls(); + return $this->getRegisteredObject('_editView'); + } + + /** + * @return TScaffoldSearch scaffold search textbox and button. + */ + public function getSearchControl() + { + $this->ensureChildControls(); + return $this->getRegisteredObject('_search'); + } + + /** + * @return TButton "Add new record" button. + */ + public function getAddButton() + { + $this->ensureChildControls(); + return $this->getRegisteredObject('_newButton'); + } + + /** + * Handle the "edit" and "new" commands by displaying the edit view. + * Default command shows the list view. + */ + public function bubbleEvent($sender,$param) + { + switch(strtolower($param->getCommandName())) + { + case 'edit': + return $this->showEditView($sender, $param); + case 'new': + return $this->showAddView($sender, $param); + default: + return $this->showListView($sender, $param); + } + return false; + } + + /** + * Shows the edit record view. + */ + protected function showEditView($sender, $param) + { + $this->getListView()->setVisible(false); + $this->getEditView()->setVisible(true); + $this->_panForNewButton->setVisible(false); + $this->_panForSearch->setVisible(false); + $this->getEditView()->getCancelButton()->setVisible(true); + $this->getEditView()->getClearButton()->setVisible(false); + } + + /** + * Shows the view for listing the records. + */ + protected function showListView($sender, $param) + { + $this->getListView()->setVisible(true); + $this->getEditView()->setVisible(false); + $this->_panForNewButton->setVisible(true); + $this->_panForSearch->setVisible(true); + } + + /** + * Shows the add record view. + */ + protected function showAddView($sender, $param) + { + $this->getEditView()->setRecordPk(null); + $this->getEditView()->initializeEditForm(); + $this->showEditView($sender, $param); + } +} + |