summaryrefslogtreecommitdiff
path: root/demos/quickstart/protected/pages/Controls/Samples/TDataList/Sample2.php
blob: 7273dd39d06112c4c9fed9146601764d190c3082 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php

class Sample2 extends TPage
{
	private $_data=null;

	protected function getData()
	{
		if($this->_data===null)
			$this->loadData();
		return $this->_data;
	}

	protected function loadData()
	{
		// We use viewstate keep track of data.
		// In real applications, data should come from database using an SQL SELECT statement.
		// In the following tabular data, field 'id' is the primary key.
		// All update and delete operations should come with an 'id' value in order to go through.
		if(($this->_data=$this->getViewState('Data',null))===null)
		{
			$this->_data=array(
				array('id'=>'ITN001','name'=>'Motherboard','quantity'=>1,'price'=>100.00,'imported'=>true),
				array('id'=>'ITN002','name'=>'CPU','quantity'=>1,'price'=>150.00,'imported'=>true),
				array('id'=>'ITN003','name'=>'Harddrive','quantity'=>2,'price'=>80.00,'imported'=>false),
				array('id'=>'ITN004','name'=>'Sound card','quantity'=>1,'price'=>40.00,'imported'=>false),
				array('id'=>'ITN005','name'=>'Video card','quantity'=>1,'price'=>150.00,'imported'=>true),
				array('id'=>'ITN006','name'=>'Keyboard','quantity'=>1,'price'=>20.00,'imported'=>true),
				array('id'=>'ITN007','name'=>'Monitor','quantity'=>2,'price'=>300.00,'imported'=>false),
			);
			$this->saveData();
		}
	}

	protected function saveData()
	{
		$this->setViewState('Data',$this->_data);
	}

	protected function updateProduct($id,$name,$quantity,$price,$imported)
	{
		// In real applications, data should be saved to database using an SQL UPDATE statement
		if($this->_data===null)
			$this->loadData();
		$updateRow=null;
		foreach($this->_data as $index=>$row)
			if($row['id']===$id)
				$updateRow=&$this->_data[$index];
		if($updateRow!==null)
		{
			$updateRow['name']=$name;
			$updateRow['quantity']=TPropertyValue::ensureInteger($quantity);
			$updateRow['price']=TPropertyValue::ensureFloat($price);
			$updateRow['imported']=TPropertyValue::ensureBoolean($imported);
			$this->saveData();
		}
	}

	protected function deleteProduct($id)
	{
		// In real applications, data should be saved to database using an SQL DELETE statement
		if($this->_data===null)
			$this->loadData();
		$deleteIndex=-1;
		foreach($this->_data as $index=>$row)
			if($row['id']===$id)
				$deleteIndex=$index;
		if($deleteIndex>=0)
		{
			unset($this->_data[$deleteIndex]);
			$this->saveData();
		}
	}

	public function onLoad($param)
	{
		parent::onLoad($param);
		if(!$this->IsPostBack)
		{
			$this->DataList->DataSource=$this->Data;
			$this->DataList->dataBind();
		}
	}

	public function editItem($sender,$param)
	{
		$this->DataList->SelectedItemIndex=-1;
		$this->DataList->EditItemIndex=$param->Item->ItemIndex;
		$this->DataList->DataSource=$this->Data;
		$this->DataList->dataBind();
	}

	public function cancelItem($sender,$param)
	{
		$this->DataList->SelectedItemIndex=-1;
		$this->DataList->EditItemIndex=-1;
		$this->DataList->DataSource=$this->Data;
		$this->DataList->dataBind();
	}

	public function updateItem($sender,$param)
	{
		$item=$param->Item;
		$this->updateProduct(
			$this->DataList->DataKeys[$item->ItemIndex],
			$item->ProductName->Text,
			$item->ProductQuantity->Text,
			$item->ProductPrice->Text,
			$item->ProductImported->Checked);
		$this->DataList->EditItemIndex=-1;
		$this->DataList->DataSource=$this->Data;
		$this->DataList->dataBind();
	}

	public function deleteItem($sender,$param)
	{
		$this->deleteProduct($this->DataList->DataKeys[$param->Item->ItemIndex]);
		$this->DataList->SelectedItemIndex=-1;
		$this->DataList->EditItemIndex=-1;
		$this->DataList->DataSource=$this->Data;
		$this->DataList->dataBind();
	}

	public function selectItem($sender,$param)
	{
		$this->DataList->EditItemIndex=-1;
		$this->DataList->DataSource=$this->Data;
		$this->DataList->dataBind();
	}
}

?>