Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
prado::using ('System.Web.UI.ActiveControls.*');
4
 
5
class Home extends TPage
6
{
7
	public function onInit ($param)
8
	{
9
		parent::onInit($param);
10
		if (!$this->getIsPostBack() && !$this->getIsCallBack())
11
		{
12
 
13
			$this->populateProductList();
14
			$this->populateShoppingList();
15
		}
16
	}
17
 
18
	private function getProductData ()
19
	{
20
		return array (
21
			array (
22
				'ProductId' => 'Product1',
23
				'ProductImageUrl' => $this->publishAsset('assets/product1.png'),
24
				'ProductTitle' => 'Cup'
25
			),
26
			array (
27
				'ProductId' => 'Product2',
28
				'ProductImageUrl' => $this->publishAsset('assets/product2.png'),
29
				'ProductTitle' => 'T-Shirt'
30
			)
31
		);
32
	}
33
 
34
	private function getProduct ($key)
35
	{
36
		foreach ($this->getProductData() as $product)
37
			if ($product['ProductId']==$key) return $product;
38
		return null;
39
	}
40
 
41
	protected function populateProductList ()
42
	{
43
		$this->ProductList->DataSource=$this->getProductData();
44
		$this->ProductList->Databind();
45
	}
46
 
47
	protected function populateShoppingList ()
48
	{
49
		$this->ShoppingList->DataSource=$this->getShoppingListData();
50
		$this->ShoppingList->Databind();
51
 
52
	}
53
 
54
 
55
	public function getShoppingListData ()
56
	{
57
		return $this->getViewState('ShoppingList', array ());
58
	}
59
 
60
	public function setShoppingListData ($value)
61
	{
62
		$this->setViewState('ShoppingList', TPropertyValue::ensureArray($value), array ());
63
	}
64
 
65
	public function addItemToCart ($sender, $param)
66
	{
67
		$control=$param->getDroppedControl();
68
		// Get the Key from the repeater item
69
		$item=$control->getNamingContainer();
70
		$key=$this->ProductList->getDataKeys()->itemAt($item->getItemIndex());
71
		$product=$this->getProduct($key);
72
		$shoppingList=$this->getShoppingListData();
73
		if (isset ($shoppingList[$key]))
74
		{
75
			// Already an item of this type, increment counter
76
			$shoppingList[$key]['ProductCount']++;
77
		}
78
		else
79
		{
80
			// Add an item to the shopping list
81
			$shoppingList[$key]=$product;
82
			$shoppingList[$key]['ProductCount']=1;
83
		}
84
		$this->setShoppingListData($shoppingList);
85
 
86
	}
87
 
88
	public function removeItemFromCart ($sender, $param)
89
	{
90
		$control=$param->getDroppedControl();
91
		$item=$control->getNamingContainer();
92
		$key=$this->ShoppingList->getDataKeys()->itemAt($item->getItemIndex());
93
		$shoppingList=$this->getShoppingListData();
94
		if (isset($shoppingList[$key]))
95
		{
96
			if ($shoppingList[$key]['ProductCount'] > 1)
97
				$shoppingList[$key]['ProductCount'] --;
98
			else
99
				unset($shoppingList[$key]);
100
		}
101
		$this->setShoppingListData($shoppingList);
102
 
103
	}
104
 
105
	public function redrawCart ($sender, $param)
106
	{
107
		$this->populateShoppingList();
108
		$this->cart->render($param->NewWriter);
109
 
110
	}
111
}
112
?>