summaryrefslogtreecommitdiff
path: root/framework/Data/ActiveRecord/Scaffold/TScaffoldEditView.php
blob: 28fc8fb91e3f5aadaacdbf9e07c3e3ce62588acb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php

Prado::using('System.Data.ActiveRecord.Scaffold.TScaffoldBase');
Prado::using('System.Data.ActiveRecord.Scaffold.InputBuilder.TScaffoldInputBase');

class TScaffoldEditView extends TScaffoldBase
{
	private static $_builders=array();

	public function onLoad($param)
	{
		$this->initializeEditForm();
	}

	public function setRecordPk($value)
	{
		$this->clearRecordObject();
		$val = TPropertyValue::ensureArray($value);
		$this->setViewState('PK', count($val) > 0 ? $val : null);
	}

	public function getRecordPk()
	{
		return $this->getViewState('PK');
	}

	protected function getCurrentRecord()
	{
		return $this->getRecordObject($this->getRecordPk());
	}

	public function initializeEditForm()
	{
		$this->getCurrentRecord();
		$columns = $this->getTableMetaData()->getColumns();
		$this->_repeater->setDataSource($columns);
		$this->_repeater->dataBind();
	}

	public function repeaterItemCreated($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);
		}
	}

	public function bubbleEvent($sender, $param)
	{
		switch(strtolower($param->getCommandName()))
		{
			case 'save':
				if($this->getPage()->getIsValid())
					return $this->doSave() === true ? false : true;
				return true;
			case 'clear':
				$this->setRecordPk(null);
				$this->initializeEditForm();
				return false;
			default:
				return false;
		}
	}

	protected function doSave()
	{
		$record = $this->getCurrentRecord();
		$table = $this->getTableMetaData();
		$builder = $this->getScaffoldInputBuilder($record);
		foreach($this->_repeater->getItems() as $item)
		{
			$column = $table->getColumn($item->getCustomData());
			$builder->loadScaffoldInput($this, $item, $column, $record);
		}
		$record->save();
		return true;
	}

	public function getSaveButton()
	{
		$this->ensureChildControls();
		return $this->getRegisteredObject('_save');
	}

	public function getClearButton()
	{
		$this->ensureChildControls();
		return $this->getRegisteredObject('_clear');
	}

	public function getCancelButton()
	{
		$this->ensureChildControls();
		return $this->getRegisteredObject('_cancel');
	}

	protected function getScaffoldInputBuilder($record)
	{
		$class = get_class($record);
		if(!isset(self::$_builders[$class]))
			self::$_builders[$class] = TScaffoldInputBase::createInputBuilder($record);
		return self::$_builders[$class];
	}

	public function getValidationGroup()
	{
		return 'group_'.$this->getUniqueID();
	}
}

?>