From fd4b8d9f45d1707035021bc19b8d5bc17ede66ce Mon Sep 17 00:00:00 2001 From: wei <> Date: Mon, 12 Feb 2007 12:46:11 +0000 Subject: Add IBM DB2 driver for active record. --- .../ActiveRecord/Scaffold/TScaffoldEditView.php | 168 ++++++++++++++++++--- 1 file changed, 145 insertions(+), 23 deletions(-) (limited to 'framework/Data/ActiveRecord/Scaffold/TScaffoldEditView.php') diff --git a/framework/Data/ActiveRecord/Scaffold/TScaffoldEditView.php b/framework/Data/ActiveRecord/Scaffold/TScaffoldEditView.php index a8faa6c8..a792aeb9 100644 --- a/framework/Data/ActiveRecord/Scaffold/TScaffoldEditView.php +++ b/framework/Data/ActiveRecord/Scaffold/TScaffoldEditView.php @@ -1,29 +1,82 @@ + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2007 PradoSoft + * @license http://www.pradosoft.com/license/ + * @version $Id$ + * @package System.Data.ActiveRecord.Scaffold + */ +/** + * Load scaffold base. + */ Prado::using('System.Data.ActiveRecord.Scaffold.TScaffoldBase'); -Prado::using('System.Data.ActiveRecord.Scaffold.InputBuilder.TScaffoldInputBase'); +/** + * Template control for editing an Active Record instance. + * + * The default editor input controls are created based on the column types. + * + * The editor layout can be specified by a renderer. A renderer is an external + * template control that implements IScaffoldEditRenderer. + * + * The Data of the IScaffoldEditRenderer will be set as the current Active + * Record to be edited. The UpdateRecord() 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 Parent of the IScaffoldEditRenderer in most + * cases. + * + * The RecordClass 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). + * + * @author Wei Zhuo + * @version $Id$ + * @package System.Data.ActiveRecord.Scaffold + * @since 3.1 + */ class TScaffoldEditView extends TScaffoldBase { - private static $_builders=array(); + /** + * @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(); @@ -31,16 +84,25 @@ class TScaffoldEditView extends TScaffoldBase $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(); @@ -60,6 +122,13 @@ class TScaffoldEditView extends TScaffoldBase } } + /** + * 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); @@ -76,7 +145,10 @@ class TScaffoldEditView extends TScaffoldBase } } - protected function repeaterItemCreated($sender, $param) + /** + * 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) @@ -92,14 +164,16 @@ class TScaffoldEditView extends TScaffoldBase } } + /** + * 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': - if($this->getPage()->getIsValid()) - return $this->doSave() === true ? false : true; - return true; + return $this->doSave() ? false : true; case 'clear': $this->setRecordPk(null); $this->initializeEditForm(); @@ -109,68 +183,116 @@ class TScaffoldEditView extends TScaffoldBase } } + /** + * Check the validators, then tries to save the record. + * @return boolean true if the validators are true, false otherwise. + */ protected function doSave() { - $record = $this->getCurrentRecord(); - if($this->_editRenderer===null) + if($this->getPage()->getIsValid()) { - $table = $this->getTableMetaData(); - $builder = $this->getScaffoldInputBuilder($record); - foreach($this->getInputRepeater()->getItems() as $item) + $record = $this->getCurrentRecord(); + if($this->_editRenderer===null) { - $column = $table->getColumn($item->getCustomData()); - $builder->loadScaffoldInput($this, $item, $column, $record); + $table = $this->getTableMetaData(); + $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 - { - $this->_editRenderer->updateRecord($record); - } - - $record->save(); - return true; + 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(self::$_builders[$class])) - self::$_builders[$class] = TScaffoldInputBase::createInputBuilder($record); - return self::$_builders[$class]; + 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 + * @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); } -- cgit v1.2.3