From 68c81d74a593d8e0225078d6982f9b5fe4986d2a Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 28 Jan 2015 15:59:45 -0500 Subject: raising update views-ar functions to TTemplateControl --- framework/Web/UI/TTemplateControl.php | 86 +++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) (limited to 'framework/Web/UI/TTemplateControl.php') diff --git a/framework/Web/UI/TTemplateControl.php b/framework/Web/UI/TTemplateControl.php index df6482c6..3040571e 100644 --- a/framework/Web/UI/TTemplateControl.php +++ b/framework/Web/UI/TTemplateControl.php @@ -237,5 +237,91 @@ class TTemplateControl extends TCompositeControl throw new TConfigurationException('templatecontrol_mastercontrol_required',get_class($this)); parent::initRecursive($namingContainer); } + + /** + * Function to update view controls with data in a given AR object. + * View controls and AR object need to have the same name in IDs and Attrs respectively. + * @param TActiveRecord $arObj + * @param Boolean $throwExceptions Wheter or not to throw exceptions + * @author Daniel Sampedro + */ + public function tryToUpdateView($arObj, $throwExceptions = false) + { + $objAttrs = get_class_vars(get_class($arObj)); + foreach (array_keys($objAttrs) as $key) + { + try + { + if ($key != "RELATIONS") + { + $control = $this->{$key}; + if ($control instanceof TTextBox) + $control->Text = $arObj->{$key}; + elseif ($control instanceof TCheckBox) + $control->Checked = (boolean) $arObj->{$key}; + elseif ($control instanceof TDatePicker) + $control->Date = $arObj->{$key}; + } + else + { + foreach ($objAttrs["RELATIONS"] as $relKey => $relValues) + { + $relControl = $this->{$relKey}; + switch ($relValues[0]) + { + case TActiveRecord::BELONGS_TO: + case TActiveRecord::HAS_ONE: + $relControl->Text = $arObj->{$relKey}; + break; + case TActiveRecord::HAS_MANY: + if ($relControl instanceof TListControl) + { + $relControl->DataSource = $arObj->{$relKey}; + $relControl->dataBind(); + } + break; + } + } + break; + } + } + catch (Exception $ex) + { + if ($throwExceptions) + throw $ex; + } + } + } + + /** + * Function to try to update an AR object with data in view controls. + * @param TActiveRecord $arObj + * @param Boolean $throwExceptions Wheter or not to throw exceptions + * @author Daniel Sampedro + */ + public function tryToUpdateAR($arObj, $throwExceptions = false) + { + $objAttrs = get_class_vars(get_class($arObj)); + foreach (array_keys($objAttrs) as $key) + { + try + { + if ($key == "RELATIONS") + break; + $control = $this->{$key}; + if ($control instanceof TTextBox) + $arObj->{$key} = $control->Text; + elseif ($control instanceof TCheckBox) + $arObj->{$key} = $control->Checked; + elseif ($control instanceof TDatePicker) + $arObj->{$key} = $control->Date; + } + catch (Exception $ex) + { + if ($throwExceptions) + throw $ex; + } + } + } } -- cgit v1.2.3