Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * TActivePager class file.
4
 *
5
 * @author "gevik" (forum contributor) and Christophe Boulain (Christophe.Boulain@gmail.com)
6
 * @link http://www.pradosoft.com/
7
 * @copyright Copyright &copy; 2005-2008 PradoSoft
8
 * @license http://www.pradosoft.com/license/
9
 * @version $Id: TActivePager.php 2541 2008-10-21 15:05:13Z qiang.xue $
10
 * @package System.Web.UI.ActiveControls
11
 */
12
 
13
/**
14
 * Load active control adapter.
15
 */
16
Prado::using('System.Web.UI.ActiveControls.TActiveControlAdapter');
17
 
18
/**
19
 * TActivePager is the active control counter part of TPager.
20
 *
21
 * When a page change is requested, TActivePager raises a callback instead of the
22
 * traditional postback.
23
 *
24
 * The {@link onCallback OnCallback} event is raised during a callback request
25
 * and it is raise <b>after</b> the {@link onPageIndexChanged OnPageIndexChanged} event.
26
 *
27
 * @author "gevik" (forum contributor) and Christophe Boulain (Christophe.Boulain@gmail.com)
28
 * @version $Id: TActivePager.php 2541 2008-10-21 15:05:13Z qiang.xue $
29
 * @package System.Web.UI.ActiveControls
30
 * @since 3.1.2
31
 */
32
class TActivePager extends TPager implements IActiveControl, ICallbackEventHandler
33
{
34
	/**
35
	 * Creates a new callback control, sets the adapter to
36
	 * TActiveControlAdapter. If you override this class, be sure to set the
37
	 * adapter appropriately by, for example, by calling this constructor.
38
	 */
39
	public function __construct()
40
	{
41
		parent::__construct();
42
		$this->setAdapter(new TActiveControlAdapter($this));
43
	}
44
 
45
	/**
46
	 * @return TBaseActiveControl standard active control options.
47
	 */
48
	public function getActiveControl()
49
	{
50
		return $this->getAdapter()->getBaseActiveControl();
51
	}
52
 
53
 
54
	/**
55
	 * @return TCallbackClientSide client side request options.
56
	 */
57
	public function getClientSide()
58
	{
59
		return $this->getAdapter()->getBaseActiveControl()->getClientSide();
60
	}
61
 
62
	/**
63
	 * Raises the callback event. This method is required by {@link
64
	 * ICallbackEventHandler} interface.
65
	 * This method is mainly used by framework and control developers.
66
	 * @param TCallbackEventParameter the event parameter
67
	 */
68
 
69
 	public function raiseCallbackEvent($param)
70
	{
71
		$this->onCallback($param);
72
	}
73
 
74
	/**
75
	 * This method is invoked when a callback is requested. The method raises
76
	 * 'OnCallback' event to fire up the event handlers. If you override this
77
	 * method, be sure to call the parent implementation so that the event
78
	 * handler can be invoked.
79
	 * @param TCallbackEventParameter event parameter to be passed to the event handlers
80
	 */
81
 
82
	public function onCallback($param)
83
	{
84
		$this->raiseEvent('OnCallback', $this, $param);
85
	}
86
 
87
	/**
88
	 * Builds a dropdown list pager
89
	 * Override parent implementation to build Active dropdown lists.
90
	 */
91
	protected function buildListPager()
92
	{
93
		$list=new TActiveDropDownList;
94
 
95
		$list->getAdapter()->getBaseActiveControl()->setClientSide(
96
			$this->getClientSide()
97
		);
98
 
99
		$this->getControls()->add($list);
100
		$list->setDataSource(range(1,$this->getPageCount()));
101
		$list->dataBind();
102
		$list->setSelectedIndex($this->getCurrentPageIndex());
103
		$list->setAutoPostBack(true);
104
		$list->attachEventHandler('OnSelectedIndexChanged',array($this,'listIndexChanged'));
105
		$list->attachEventHandler('OnCallback', array($this, 'handleCallback'));
106
	}
107
 
108
	/**
109
	 * Creates a pager button.
110
	 * Override parent implementation to create, depending on the button type, a TActiveLinkButton,
111
	 * a TActiveButton or a TActiveImageButton may be created.
112
	 *
113
	 * @param string button type, either LinkButton or PushButton
114
	 * @param boolean whether the button should be enabled
115
	 * @param string caption of the button
116
	 * @param string CommandName corresponding to the OnCommand event of the button
117
	 * @param string CommandParameter corresponding to the OnCommand event of the button
118
	 * @return mixed the button instance
119
	 */
120
	protected function createPagerButton($buttonType,$enabled,$text,$commandName,$commandParameter)
121
	{
122
		if($buttonType===TPagerButtonType::LinkButton)
123
		{
124
			if($enabled)
125
				$button=new TActiveLinkButton;
126
			else
127
			{
128
				$button=new TLabel;
129
				$button->setText($text);
130
				return $button;
131
			}
132
		}
133
		else if($buttonType===TPagerButtonType::ImageButton)
134
		{
135
			$button = new TActiveImageButton;
136
			$button->setImageUrl($this->getPageImageUrl($text,$commandName));
137
			if($enabled)
138
				$button->Visible = true;
139
			else
140
				$button->Visible = false;
141
		}
142
		else
143
		{
144
			$button=new TActiveButton;
145
			if(!$enabled)
146
				$button->setEnabled(false);
147
		}
148
 
149
		if($buttonType===TPagerButtonType::ImageButton)
150
		{
151
			$button->ImageUrl = $text;
152
		}
153
 
154
		$button->setText($text);
155
		$button->setCommandName($commandName);
156
		$button->setCommandParameter($commandParameter);
157
		$button->setCausesValidation(false);
158
 
159
		$button->attachEventHandler('OnCallback', array($this, 'handleCallback'));
160
		$button->getAdapter()->getBaseActiveControl()->setClientSide(
161
			$this->getClientSide()
162
		);
163
 
164
		return $button;
165
	}
166
 
167
	/**
168
	 * Event handler to the OnCallback active buttons or active dropdownlist.
169
	 * This handler will raise the {@link onCallback OnCallback} event
170
	 *
171
	 * @param mixed $sender
172
	 * @param TCallbackEventParameter $param
173
	 */
174
	public function handleCallback ($sender,$param)
175
	{
176
		// Update all the buttons pagers attached to the same control.
177
		// Dropdown pagers doesn't need to be re-rendered.
178
		$controlToPaginate=$this->getControlToPaginate();
179
		foreach ($this->getNamingContainer()->findControlsByType('TActivePager', false) as $control)
180
		{
181
			if ($control->getMode() !== TPagerMode::DropDownList && $control->getControlToPaginate()===$controlToPaginate)
182
			{
183
				$control->render($param->getNewWriter());
184
				// FIXME : With some very fast machine, the getNewWriter() consecutive calls are in the same microsecond, resulting
185
				// of getting the same boundaries in ajax response. Wait 1 microsecond to avoid this.
186
				usleep(1);
187
			}
188
		}
189
		// Raise callback event
190
		$this->onCallback($param);
191
	}
192
 
193
	public function render ($writer)
194
	{
195
		if($this->getHasPreRendered())
196
		{
197
			$this->setDisplay(($this->getPageCount()==1)?TDisplayStyle::None:TDisplayStyle::Dynamic);
198
			TWebControl::render($writer);
199
			if($this->getActiveControl()->canUpdateClientSide())
200
				$this->getPage()->getCallbackClient()->replaceContent($this,$writer);
201
		}
202
		else
203
		{
204
			$this->getPage()->getAdapter()->registerControlToRender($this,$writer);
205
		}
206
	}
207
}
208