summaryrefslogtreecommitdiff
path: root/demos/quickstart/protected/pages/ActiveControls/Samples/DragDrop/Home.php
blob: 30ef750bfd9aa7a7efc70beb07601aa898768350 (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
<?php

prado::using ('System.Web.UI.ActiveControls.*');

class Home extends TPage
{
	public function onInit ($param)
	{
		parent::onInit($param);
		if (!$this->getIsPostBack() && !$this->getIsCallBack())
		{
			
			$this->populateProductList();
			$this->populateShoppingList();
		}
	}
	
	private function getProductData ()
	{
		return array (
			array (
				'ProductId' => 'Product1',
				'ProductImageUrl' => $this->publishAsset('images/product1.png'),
				'ProductTitle' => 'Cup'
			),
			array (
				'ProductId' => 'Product2',
				'ProductImageUrl' => $this->publishAsset('images/product2.png'),
				'ProductTitle' => 'T-Shirt'
			)
		);
	}
	
	private function getProduct ($key)
	{
		foreach ($this->getProductData() as $product)
			if ($product['ProductId']==$key) return $product;
		return null;	
	}
	
	protected function populateProductList ()
	{
		$this->ProductList->DataSource=$this->getProductData();
		$this->ProductList->Databind();
	}
	
	protected function populateShoppingList ()
	{
		$this->ShoppingList->DataSource=$this->getShoppingListData();
		$this->ShoppingList->Databind();
		
	}
	
	
	public function getShoppingListData ()
	{
		return $this->getViewState('ShoppingList', array ());
	}
	
	public function setShoppingListData ($value)
	{
		$this->setViewState('ShoppingList', TPropertyValue::ensureArray($value), array ());
	}
	
	public function addItemToCart ($sender, $param)
	{
		$control=$param->getDroppedControl();
		// Get the Key from the repeater item
		$item=$control->getNamingContainer();
		$key=$this->ProductList->getDataKeys()->itemAt($item->getItemIndex());
		$product=$this->getProduct($key);
		$shoppingList=$this->getShoppingListData();
		if (isset ($shoppingList[$key]))
		{
			// Already an item of this type, increment counter
			$shoppingList[$key]['ProductCount']++;
		}
		else
		{
			// Add an item to the shopping list
			$shoppingList[$key]=$product;
			$shoppingList[$key]['ProductCount']=1;
		}
		$this->setShoppingListData($shoppingList);
		
	}
	
	public function removeItemFromCart ($sender, $param)
	{
		$control=$param->getDroppedControl();
		$item=$control->getNamingContainer();
		$key=$this->ShoppingList->getDataKeys()->itemAt($item->getItemIndex());
		$shoppingList=$this->getShoppingListData();
		if (isset($shoppingList[$key]))
		{
			if ($shoppingList[$key]['ProductCount'] > 1)
				$shoppingList[$key]['ProductCount'] --;
			else
				unset($shoppingList[$key]);
		}
		$this->setShoppingListData($shoppingList);
		
	}
	
	public function redrawCart ($sender, $param)
	{
		$this->populateShoppingList();
		$this->cart->render($param->NewWriter);
		
	}
}