1: <?php
2: /**
3: * This file contains the QFileControl class.
4: *
5: * @package Controls
6: */
7:
8: /**
9: * This class will render an HTML File input.
10: *
11: * @package Controls
12: *
13: * @property-read string $FileName is the name of the file that the user uploads
14: * @property-read string $Type is the MIME type of the file
15: * @property-read integer $Size is the size in bytes of the file
16: * @property-read string $File is the temporary full file path on the server where the file physically resides
17: */
18: class QFileControl extends QControl {
19: ///////////////////////////
20: // Private Member Variables
21: ///////////////////////////
22:
23: // MISC
24: protected $strFileName = null;
25: protected $strType = null;
26: protected $intSize = null;
27: protected $strFile = null;
28:
29: // SETTINGS
30: protected $strFormAttributes = array('enctype'=>'multipart/form-data');
31:
32: //////////
33: // Methods
34: //////////
35: public function ParsePostData() {
36: // Check to see if this Control's Value was passed in via the POST data
37: if ((array_key_exists($this->strControlId, $_FILES)) && ($_FILES[$this->strControlId]['tmp_name'])) {
38: // It was -- update this Control's value with the new value passed in via the POST arguments
39: $this->strFileName = $_FILES[$this->strControlId]['name'];
40: $this->strType = $_FILES[$this->strControlId]['type'];
41: $this->intSize = QType::Cast($_FILES[$this->strControlId]['size'], QType::Integer);
42: $this->strFile = $_FILES[$this->strControlId]['tmp_name'];
43: }
44: }
45:
46: /**
47: * Returns the HTML of the control which can be sent to user's browser
48: *
49: * @return string HTML of the control
50: */
51: protected function GetControlHtml() {
52: // Reset Internal Values
53: $this->strFileName = null;
54: $this->strType = null;
55: $this->intSize = null;
56: $this->strFile = null;
57:
58: $strStyle = $this->GetStyleAttributes();
59: if ($strStyle)
60: $strStyle = sprintf('style="%s"', $strStyle);
61:
62: $strToReturn = sprintf('<input type="file" name="%s" id="%s" %s%s />',
63: $this->strControlId,
64: $this->strControlId,
65: $this->GetAttributes(),
66: $strStyle);
67:
68: return $strToReturn;
69: }
70:
71: /**
72: * Tells if the file control is valid
73: *
74: * @return bool
75: */
76: public function Validate() {
77: if ($this->blnRequired) {
78: if (strlen($this->strFileName) > 0)
79: return true;
80: else {
81: $this->ValidationError = QApplication::Translate($this->strName) . ' ' . QApplication::Translate('is required');
82: return false;
83: }
84: } else
85: return true;
86: }
87:
88: /////////////////////////
89: // Public Properties: GET
90: /////////////////////////
91: /**
92: * PHP magic method
93: * @param string $strName
94: *
95: * @return mixed
96: * @throws Exception
97: * @throws QCallerException
98: */
99: public function __get($strName) {
100: switch ($strName) {
101: // MISC
102: case "FileName": return $this->strFileName;
103: case "Type": return $this->strType;
104: case "Size": return $this->intSize;
105: case "File": return $this->strFile;
106:
107: default:
108: try {
109: return parent::__get($strName);
110: } catch (QCallerException $objExc) {
111: $objExc->IncrementOffset();
112: throw $objExc;
113: }
114: }
115: }
116:
117: /////////////////////////
118: // Public Properties: SET
119: /////////////////////////
120: /**
121: * @param string $strName
122: * @param string $mixValue
123: *
124: * @return mixed
125: * @throws Exception|QCallerException
126: */
127: public function __set($strName, $mixValue) {
128: $this->blnModified = true;
129:
130: switch ($strName) {
131: default:
132: try {
133: parent::__set($strName, $mixValue);
134: } catch (QCallerException $objExc) {
135: $objExc->IncrementOffset();
136: throw $objExc;
137: }
138: break;
139: }
140: }
141: }