1: <?php
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: class QFileAssetBase extends QPanel {
27: public $imgFileIcon;
28: public $btnDelete;
29: public $btnUpload;
30: public $dlgFileAsset;
31:
32: protected $strAcceptibleMimeArray;
33: protected $strUnacceptableMessage;
34: protected $intFileAssetType;
35: protected $strFile;
36: protected $strFileName;
37: protected $intSize;
38: protected $strRandomFileName;
39: protected $blnClickToView;
40:
41: protected $strIconFilePathArray = array();
42:
43: protected $strTemporaryUploadPath;
44:
45: public function __construct($objParentObject, $strControlId = null) {
46: parent::__construct($objParentObject, $strControlId);
47:
48:
49: $this->SetupIconFilePathArray();
50:
51:
52: $this->dlgFileAsset = new QFileAssetDialog($this, 'dlgFileAsset_Upload');
53:
54: $this->imgFileIcon = new QImageControl($this);
55: $this->imgFileIcon->Width = 80;
56: $this->imgFileIcon->Height = 80;
57: $this->imgFileIcon->ImagePath = $this->strIconFilePathArray['blank'];
58:
59:
60:
61: $this->imgFileIcon->CacheFolder = __IMAGE_CACHE_ASSETS__;
62:
63:
64: $this->btnUpload = new QLinkButton($this);
65: $this->btnUpload->HtmlEntities = false;
66: $this->SetupUpdateActions();
67:
68:
69: $this->btnDelete = new QLinkButton($this);
70: $this->btnDelete->HtmlEntities = false;
71: $this->btnDelete->AddAction(new QClickEvent(), new QAjaxControlAction($this, 'btnDelete_Click'));
72: $this->btnDelete->AddAction(new QClickEvent(), new QTerminateAction());
73: }
74:
75: protected function SetupUpdateActions() {
76: $this->btnUpload->RemoveAllActions('click');
77: $this->btnUpload->AddAction(new QClickEvent(), new QShowDialog($this->dlgFileAsset));
78: $this->btnUpload->AddAction(new QClickEvent(), new QTerminateAction());
79: }
80:
81: protected function SetupIconFilePathArray() {
82: $this->strIconFilePathArray['blank'] = __DOCROOT__ . __IMAGE_ASSETS__ . '/file_asset_blank.png';
83: $this->strIconFilePathArray['default'] = __DOCROOT__ . __IMAGE_ASSETS__ . '/file_asset_default.png';
84: $this->strIconFilePathArray['pdf'] = __DOCROOT__ . __IMAGE_ASSETS__ . '/file_asset_pdf.png';
85: }
86:
87: public function Validate() {
88: $blnToReturn = parent::Validate();
89:
90: if ($blnToReturn) {
91: if ($this->blnRequired && !$this->strFile) {
92: $blnToReturn = false;
93: if ($this->strName)
94: $this->ValidationError = $this->strName . QApplication::Translate(' is required');
95: else
96: $this->ValidationError = $this->strName . QApplication::Translate('Required');
97: }
98: }
99:
100: return $blnToReturn;
101: }
102:
103: public function dlgFileAsset_Upload() {
104:
105: if (!file_exists($this->dlgFileAsset->flcFileAsset->File) || !$this->dlgFileAsset->flcFileAsset->Size) {
106: $this->dlgFileAsset->ShowError($this->strUnacceptableMessage);
107:
108:
109: } else if (is_array($this->strAcceptibleMimeArray) && (!array_key_exists($this->dlgFileAsset->flcFileAsset->Type, $this->strAcceptibleMimeArray))) {
110: $this->dlgFileAsset->ShowError($this->strUnacceptableMessage);
111:
112:
113: } else {
114:
115: $strFilename = $this->dlgFileAsset->flcFileAsset->FileName;
116: $intPosition = strrpos($strFilename, '.');
117:
118: if (is_array($this->strAcceptibleMimeArray) && array_key_exists($this->dlgFileAsset->flcFileAsset->Type, $this->strAcceptibleMimeArray))
119: $strExtension = $this->strAcceptibleMimeArray[$this->dlgFileAsset->flcFileAsset->Type];
120: else {
121: if ($intPosition)
122: $strExtension = substr($strFilename, $intPosition + 1);
123: else
124: $strExtension = null;
125: }
126:
127: $strBaseFilename = substr($strFilename, 0, $intPosition);
128: $strExtension = strtolower($strExtension);
129:
130:
131: $this->strRandomFileName = basename($this->dlgFileAsset->flcFileAsset->File) . rand(1000, 9999) . '.' . $strExtension;
132: $strTempFilePath = $this->strTemporaryUploadPath . '/' . $this->strRandomFileName;
133: copy($this->dlgFileAsset->flcFileAsset->File, $strTempFilePath);
134: $this->File = $strTempFilePath;
135:
136:
137: $this->strFileName = preg_replace('/[^A-Z^a-z^0-9_\-]/', '', $strBaseFilename) . '.' . $strExtension;
138:
139: $this->intSize = $this->dlgFileAsset->flcFileAsset->Size;
140:
141:
142: $this->dlgFileAsset->Close();
143:
144:
145: $this->Refresh();
146: }
147: }
148:
149: public function GetControlHtml() {
150: 151: 152: 153: 154: 155: 156:
157: return parent::GetControlHtml();
158: }
159:
160: public function btnDelete_Click() {
161:
162: $this->File = null;
163: $this->Refresh();
164: }
165:
166: public function __get($strName) {
167: switch ($strName) {
168: case 'File': return $this->strFile;
169: case 'FileName': return $this->strFileName;
170: case 'Size': return $this->intSize;
171: case 'RandomFileName': return $this->strRandomFileName;
172: case 'UnacceptableMessage': return $this->strUnacceptableMessage;
173: case 'FileAssetType': return $this->intFileAssetType;
174: case 'TemporaryUploadPath': return $this->strTemporaryUploadPath;
175: case 'ClickToView': return $this->blnClickToView;
176:
177: case 'DialogBoxCssClass': return $this->dlgFileAsset->CssClass;
178: case 'DialogBoxWidth': return $this->dlgFileAsset->Width;
179: case 'DialogBoxHeight': return $this->dlgFileAsset->Height;
180: case 'UploadText': return $this->dlgFileAsset->btnUpload->Text;
181: case 'CancelText': return $this->dlgFileAsset->btnCancel->Text;
182: case 'DialogBoxHtml': return $this->dlgFileAsset->lblMessage->Text;
183:
184:
185: default:
186: try {
187: return parent::__get($strName);
188: } catch (QCallerException $objExc) {
189: $objExc->IncrementOffset();
190: throw $objExc;
191: }
192: }
193: }
194:
195: 196: 197: 198: 199: 200:
201: public function GetWebUrl() {
202:
203: if (!$this->blnClickToView) {
204: return null;
205: }
206:
207:
208:
209: if ($this->strFile) {
210: $filePathSubstr = substr($this->strFile, 0, strlen(__DOCROOT__));
211:
212:
213: if (substr($this->strFile, 1, 2) == ':\\') {
214: $this->strFile = str_replace('\\', '/', $this->strFile);
215: }
216:
217:
218:
219: $filePathSubstr = str_replace("\\", "/", $filePathSubstr);
220: $docrootNormalized = str_replace("\\", "/", __DOCROOT__);
221: if ($filePathSubstr == $docrootNormalized) {
222: return __VIRTUAL_DIRECTORY__ . substr(str_replace("\\", "/", $this->strFile), strlen(__DOCROOT__));
223: }
224: }
225:
226: return null;
227: }
228:
229: protected function SetFile($strFile) {
230: if (!strlen($strFile)) {
231:
232: $this->strFile = null;
233: $this->imgFileIcon->ImagePath = $this->strIconFilePathArray['blank'];
234: } else if (!is_file($strFile)) {
235:
236: throw new QCallerException('File Not Found: ' . $strFile);
237: } else {
238:
239: $this->strFile = realpath($strFile);
240:
241:
242: $strExtension = substr($this->strFile, strrpos($this->strFile, '.') + 1);
243: switch (trim(strtolower($strExtension))) {
244: case 'jpg':
245: case 'jpeg':
246: case 'png':
247: case 'gif':
248: $this->imgFileIcon->ImagePath = $this->strFile;
249: break;
250: case 'pdf':
251: $this->imgFileIcon->ImagePath = $this->strIconFilePathArray[trim(strtolower($strExtension))];
252: break;
253: default:
254: $this->imgFileIcon->ImagePath = $this->strIconFilePathArray['default'];
255: break;
256: }
257: }
258:
259: $this->strFileName = basename($this->strFile);
260: return $this->strFile;
261: }
262:
263: protected function SetFileAssetType($intFileAssetType) {
264: switch ($intFileAssetType) {
265: case QFileAssetType::Image:
266: $this->intFileAssetType = $intFileAssetType;
267: $this->strAcceptibleMimeArray = array(
268: 'image/pjpeg' => 'jpg',
269: 'image/jpeg' => 'jpg',
270: 'image/jpg' => 'jpg',
271: 'image/png' => 'png',
272: 'image/x-png' => 'png',
273: 'image/gif' => 'gif');
274: $this->strUnacceptableMessage = QApplication::Translate('Must be a JPG, PNG or GIF');
275: break;
276: case QFileAssetType::Pdf:
277: $this->intFileAssetType = $intFileAssetType;
278: $this->strAcceptibleMimeArray = array(
279: 'application/pdf' => 'pdf',
280: 'application/octet-stream' => 'pdf'
281: );
282: $this->strUnacceptableMessage = QApplication::Translate('Must be a PDF');
283: break;
284: case QFileAssetType::Document:
285: $this->intFileAssetType = $intFileAssetType;
286: $this->strAcceptibleMimeArray = array(
287: 'application/pdf' => 'pdf',
288: 'application/octet-stream' => 'pdf',
289: 'image/pjpeg' => 'jpg',
290: 'image/jpeg' => 'jpg',
291: 'image/jpg' => 'jpg',
292: 'image/png' => 'png',
293: 'image/x-png' => 'png',
294: 'image/gif' => 'gif');
295: $this->strUnacceptableMessage = QApplication::Translate('Must be a valid document (Image, PDF, etc.)');
296: break;
297: default:
298: throw new QCallerException('FileAssetType must be a valid QFileAssetType constant value');
299: break;
300: }
301:
302: return $intFileAssetType;
303: }
304:
305: public function __set($strName, $mixValue) {
306: $this->blnModified = true;
307:
308: switch ($strName) {
309: case 'File':
310: try {
311: return $this->SetFile($mixValue);
312: } catch (QCallerException $objExc) {
313: $objExc->IncrementOffset();
314: throw $objExc;
315: }
316:
317: case 'DialogBoxCssClass':
318: try {
319: $this->dlgFileAsset->CssClass = $mixValue;
320: $this->SetupUpdateActions();
321: return $this->dlgFileAsset->CssClass;
322: } catch (QCallerException $objExc) {
323: $objExc->IncrementOffset();
324: throw $objExc;
325: }
326:
327: case 'DialogBoxWidth':
328: try {
329: $this->dlgFileAsset->Width = $mixValue;
330: $this->SetupUpdateActions();
331: return $this->dlgFileAsset->Width;
332: } catch (QCallerException $objExc) {
333: $objExc->IncrementOffset();
334: throw $objExc;
335: }
336:
337: case 'DialogBoxHeight':
338: try {
339: $this->dlgFileAsset->Height = $mixValue;
340: $this->SetupUpdateActions();
341: return $this->dlgFileAsset->Height;
342: } catch (QCallerException $objExc) {
343: $objExc->IncrementOffset();
344: throw $objExc;
345: }
346:
347: case 'UploadText':
348: try {
349: return ($this->dlgFileAsset->btnUpload->Text = $mixValue);
350: } catch (QCallerException $objExc) {
351: $objExc->IncrementOffset();
352: throw $objExc;
353: }
354:
355: case 'CancelText':
356: try {
357: return ($this->dlgFileAsset->btnCancel->Text = $mixValue);
358: } catch (QCallerException $objExc) {
359: $objExc->IncrementOffset();
360: throw $objExc;
361: }
362:
363: case 'DialogBoxHtml':
364: try {
365: return ($this->dlgFileAsset->lblMessage->Text = $mixValue);
366: } catch (QCallerException $objExc) {
367: $objExc->IncrementOffset();
368: throw $objExc;
369: }
370:
371: case 'FileAssetType':
372: try {
373: return $this->SetFileAssetType($mixValue);
374: } catch (QCallerException $objExc) {
375: $objExc->IncrementOffset();
376: throw $objExc;
377: }
378:
379: case 'UnacceptableMessage':
380: try {
381: return ($this->strUnacceptableMessage = QType::Cast($mixValue, QType::String));
382: } catch (QCallerException $objExc) {
383: $objExc->IncrementOffset();
384: throw $objExc;
385: }
386:
387: case 'TemporaryUploadPath':
388: try {
389: return ($this->strTemporaryUploadPath = QType::Cast($mixValue, QType::String));
390: } catch (QCallerException $objExc) {
391: $objExc->IncrementOffset();
392: throw $objExc;
393: }
394:
395: case 'ClickToView':
396: try {
397: return ($this->blnClickToView = QType::Cast($mixValue, QType::Boolean));
398: } catch (QCallerException $objExc) {
399: $objExc->IncrementOffset();
400: throw $objExc;
401: }
402:
403: default:
404: try {
405: return parent::__set($strName, $mixValue);
406: } catch (QCallerException $objExc) {
407: $objExc->IncrementOffset();
408: throw $objExc;
409: }
410: }
411: }
412: }