Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * TActiveFileUpload.php
4
 *
5
 * @author Bradley Booms <Bradley.Booms@nsighttel.com>
6
 * @author Christophe Boulain <Christophe.Boulain@gmail.com>
7
 * @version $Id: TActiveFileUpload.php 2592 2008-12-22 21:36:50Z carlgmathisen $
8
 */
9
 
10
/**
11
 * Load TActiveControlAdapter and TFileUpload.
12
 */
13
Prado::using('System.Web.UI.ActiveControls.TActiveControlAdapter');
14
Prado::using('System.Web.UI.WebControls.TFileUpload');
15
 
16
/**
17
 * TActiveFileUpload
18
 *
19
 * TActiveFileUpload displays a file upload field on a page. Upon postback,
20
 * the text entered into the field will be treated as the name of the file
21
 * that will be uploaded to the server. The property {@link getHasFile HasFile}
22
 * indicates whether the file upload is successful. If successful, the file
23
 * may be obtained by calling {@link saveAs} to save it at a specified place.
24
 * You can use {@link getFileName FileName}, {@link getFileType FileType},
25
 * {@link getFileSize FileSize} to get the original client-side file name,
26
 * the file mime type, and the file size information. If the upload is not
27
 * successful, {@link getErrorCode ErrorCode} contains the error code
28
 * describing the cause of failure.
29
 *
30
 * TActiveFileUpload raises {@link onFileUpload OnFileUpload} event if a file is uploaded
31
 * (whether it succeeds or not).
32
 *
33
 * TActiveFileUpload actually does a postback in a hidden IFrame, and then does a callback.
34
 * This callback then raises the {@link onFileUpload OnFileUpload} event. After the postback
35
 * a status icon is displayed; either a green checkmark if the upload is successful,
36
 * or a red x if there was an error.
37
 *
38
 * @author Bradley Booms <Bradley.Booms@nsighttel.com>
39
 * @author Christophe Boulain <Christophe.Boulain@gmail.com>
40
 *
41
 * @version $Id: TActiveFileUpload.php 2592 2008-12-22 21:36:50Z carlgmathisen $
42
 */
43
class TActiveFileUpload extends TFileUpload implements IActiveControl, ICallbackEventHandler, INamingContainer
44
{
45
 
46
	const SCRIPT_PATH = 'prado/activefileupload';
47
 
48
	/**
49
	 * @var THiddenField a flag to tell which component is doing the callback.
50
	 */
51
	private $_flag;
52
	/**
53
	 * @var TImage that spins to show that the file is being uploaded.
54
	 */
55
	private $_busy;
56
	/**
57
	 * @var TImage that shows a green check mark for completed upload.
58
	 */
59
	private $_success;
60
	/**
61
	 * @var TImage that shows a red X for incomplete upload.
62
	 */
63
	private $_error;
64
	/**
65
	 * @var TInlineFrame used to submit the data in an "asynchronous" fashion.
66
	 */
67
	private $_target;
68
 
69
 
70
	/**
71
	 * Creates a new callback control, sets the adapter to
72
	 * TActiveControlAdapter. If you override this class, be sure to set the
73
	 * adapter appropriately by, for example, by calling this constructor.
74
	 */
75
	public function __construct(){
76
		parent::__construct();
77
		$this->setAdapter(new TActiveControlAdapter($this));
78
	}
79
 
80
 
81
	/**
82
	 * @param string asset file in the self::SCRIPT_PATH directory.
83
	 * @return string asset file url.
84
	 */
85
	protected function getAssetUrl($file='')
86
	{
87
		$base = $this->getPage()->getClientScript()->getPradoScriptAssetUrl();
88
		return $base.'/'.self::SCRIPT_PATH.'/'.$file;
89
	}
90
 
91
 
92
	/**
93
	 * This method is invoked when a file is uploaded.
94
	 * If you override this method, be sure to call the parent implementation to ensure
95
	 * the invocation of the attached event handlers.
96
	 * @param TEventParameter event parameter to be passed to the event handlers
97
	 */
98
	public function onFileUpload($param){
99
		if ($this->_flag->getValue() && $this->getPage()->getIsPostBack()){
100
			// save the file so that it will persist past the end of this return.
101
			$localName = str_replace('\\', '/', tempnam(Prado::getPathOfNamespace($this->getTempPath()),''));
102
			parent::saveAs($localName);
103
 
104
			$filename=addslashes($this->getFileName());
105
			// return some javascript to display a completion status.
106
			echo <<<EOS
107
<script language="Javascript">
108
	Options = new Object();
109
	Options.clientID = '{$this->getClientID()}';
110
	Options.targetID = '{$this->_target->getUniqueID()}';
111
	Options.localName = '$localName';
112
	Options.fileName = '{$filename}';
113
	Options.fileSize = '{$this->getFileSize()}';
114
	Options.fileType = '{$this->getFileType()}';
115
	Options.errorCode = '{$this->getErrorCode()}';
116
	parent.Prado.WebUI.TActiveFileUpload.onFileUpload(Options);
117
</script>
118
EOS;
119
			exit();
120
		}
121
	}
122
 
123
	/**
124
	 * @return string the path where the uploaded file will be stored temporarily, in namespace format
125
	 * default "Application.runtime.*"
126
	 */
127
	public function getTempPath(){
128
		return $this->getViewState('TempPath', 'Application.runtime.*');
129
	}
130
 
131
	/**
132
	 * @param string the path where the uploaded file will be stored temporarily in namespace format
133
	 * default "Application.runtime.*"
134
	 */
135
	public function setTempPath($value){
136
		$this->setViewState('TempNamespace',$value,'Application.runtime.*');
137
	}
138
 
139
	/**
140
	 * @throws TInvalidDataValueException if the {@link getTempPath TempPath} is not writable.
141
	 */
142
	public function onInit($sender){
143
		parent::onInit($sender);
144
		if (!is_writable(Prado::getPathOfNamespace($this->getTempPath()))){
145
			throw new TInvalidDataValueException("activefileupload_temppath_invalid", $this->getTempPath());
146
		}
147
	}
148
 
149
	/**
150
	 * Raises <b>OnFileUpload</b> event.
151
	 *
152
	 * This method is required by {@link ICallbackEventHandler} interface.
153
	 * This method is mainly used by framework and control developers.
154
	 * @param TCallbackEventParameter the event parameter
155
	 */
156
 	public function raiseCallbackEvent($param){
157
 		$cp = $param->getCallbackParameter();
158
		if ($key = $cp->targetID == $this->_target->getUniqueID()){
159
			$_FILES[$key]['name'] = $cp->fileName;
160
			$_FILES[$key]['size'] = intval($cp->fileSize);
161
			$_FILES[$key]['type'] = $cp->fileType;
162
			$_FILES[$key]['error'] = intval($cp->errorCode);
163
			$_FILES[$key]['tmp_name'] = $cp->localName;
164
			$this->loadPostData($key, null);
165
 
166
			$this->raiseEvent('OnFileUpload', $this, $param);
167
		}
168
	}
169
 
170
	/**
171
	 * Publish the javascript
172
	 */
173
	public function onPreRender($param){
174
		parent::onPreRender($param);
175
		$this->getPage()->getClientScript()->registerPradoScript('effects');
176
		$this->getPage()->getClientScript()->registerPradoScript('activefileupload');
177
	}
178
 
179
 
180
	public function createChildControls(){
181
		$this->_flag = Prado::createComponent('THiddenField');
182
		$this->_flag->setID('Flag');
183
		$this->getControls()->add($this->_flag);
184
 
185
		$this->_busy = Prado::createComponent('TImage');
186
		$this->_busy->setID('Busy');
187
		$this->_busy->setImageUrl($this->getAssetUrl('ActiveFileUploadIndicator.gif'));
188
		$this->_busy->setStyle("display:none");
189
		$this->getControls()->add($this->_busy);
190
 
191
		$this->_success = Prado::createComponent('TImage');
192
		$this->_success->setID('Success');
193
		$this->_success->setImageUrl($this->getAssetUrl('ActiveFileUploadComplete.png'));
194
		$this->_success->setStyle("display:none");
195
		$this->getControls()->add($this->_success);
196
 
197
		$this->_error = Prado::createComponent('TImage');
198
		$this->_error->setID('Error');
199
		$this->_error->setImageUrl($this->getAssetUrl('ActiveFileUploadError.png'));
200
		$this->_error->setStyle("display:none");
201
		$this->getControls()->add($this->_error);
202
 
203
		$this->_target = Prado::createComponent('TInlineFrame');
204
		$this->_target->setID('Target');
205
		$this->_target->setFrameUrl($this->getAssetUrl('ActiveFileUploadBlank.html'));
206
		$this->_target->setStyle("width:0px; height:0px;");
207
		$this->_target->setShowBorder(false);
208
		$this->getControls()->add($this->_target);
209
	}
210
 
211
 
212
	/**
213
	 * Removes localfile on ending of the callback.
214
	 */
215
	public function onUnload($param){
216
		if ($this->getPage()->getIsCallback() &&
217
			$this->getHasFile() &&
218
			file_exists($this->getLocalName())){
219
				unlink($this->getLocalName());
220
		}
221
		parent::onUnload($param);
222
	}
223
 
224
	/**
225
	 * @return TBaseActiveCallbackControl standard callback control options.
226
	 */
227
	public function getActiveControl(){
228
		return $this->getAdapter()->getBaseActiveControl();
229
	}
230
 
231
	/**
232
	 * Adds ID attribute, and renders the javascript for active component.
233
	 * @param THtmlWriter the writer used for the rendering purpose
234
	 */
235
	public function addAttributesToRender($writer){
236
		parent::addAttributesToRender($writer);
237
		$writer->addAttribute('id',$this->getClientID());
238
 
239
		$this->getActiveControl()->registerCallbackClientScript($this->getClientClassName(),$this->getClientOptions());
240
	}
241
 
242
	/**
243
	 * @return string corresponding javascript class name for this control.
244
	 */
245
	protected function getClientClassName(){
246
		return 'Prado.WebUI.TActiveFileUpload';
247
	}
248
 
249
	/**
250
	 * Gets the client side options for this control.
251
	 * @return array (	inputID => input client ID,
252
	 * 					flagID => flag client ID,
253
	 * 					targetName => target unique ID,
254
	 * 					formID => form client ID,
255
	 * 					indicatorID => upload indicator client ID,
256
	 * 					completeID => complete client ID,
257
	 * 					errorID => error client ID)
258
	 */
259
	protected function getClientOptions(){
260
		$options['ID'] = $this->getClientID();
261
		$options['EventTarget'] = $this->getUniqueID();
262
 
263
		$options['inputID'] = $this->getClientID();
264
		$options['flagID'] = $this->_flag->getClientID();
265
		$options['targetID'] = $this->_target->getUniqueID();
266
		$options['formID'] = $this->getPage()->getForm()->getClientID();
267
		$options['indicatorID'] = $this->_busy->getClientID();
268
		$options['completeID'] = $this->_success->getClientID();
269
		$options['errorID'] = $this->_error->getClientID();
270
		return $options;
271
	}
272
 
273
	/**
274
	 * Saves the uploaded file.
275
	 * @param string the file name used to save the uploaded file
276
	 * @param boolean whether to delete the temporary file after saving.
277
	 * If true, you will not be able to save the uploaded file again.
278
	 * @return boolean true if the file saving is successful
279
	 */
280
	public function saveAs($fileName,$deleteTempFile=true){
281
		if (($this->getErrorCode()===UPLOAD_ERR_OK) && (file_exists($this->getLocalName()))){
282
			if ($deleteTempFile)
283
				return rename($this->getLocalName(),$fileName);
284
			else
285
				return copy($this->getLocalName(),$fileName);
286
		} else
287
			return false;
288
	}
289
 
290
	/**
291
	 * @return TImage the image displayed when an upload
292
	 * 		completes successfully.
293
	 */
294
	public function getSuccessImage(){
295
		$this->ensureChildControls();
296
		return $this->_success;
297
	}
298
 
299
	/**
300
	 * @return TImage the image displayed when an upload
301
	 * 		does not complete successfully.
302
	 */
303
	public function getErrorImage(){
304
		$this->ensureChildControls();
305
		return $this->_error;
306
	}
307
 
308
	/**
309
	 * @return TImage the image displayed when an upload
310
	 * 		is in progress.
311
	 */
312
	public function getBusyImage(){
313
		$this->ensureChildControls();
314
		return $this->_busy;
315
	}
316
}