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:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_Eav_Model_Attribute_Data_File extends Mage_Eav_Model_Attribute_Data_Abstract
35: {
36: 37: 38: 39: 40:
41: protected $_validatorNotProtectedExtensions;
42:
43: 44: 45: 46: 47: 48:
49: public function (Zend_Controller_Request_Http $request)
50: {
51: if ($this->getIsAjaxRequest()) {
52: return false;
53: }
54:
55: $extend = $this->_getRequestValue($request);
56:
57: $attrCode = $this->getAttribute()->getAttributeCode();
58: if ($this->_requestScope) {
59: $value = array();
60: if (strpos($this->_requestScope, '/') !== false) {
61: $scopes = explode('/', $this->_requestScope);
62: $mainScope = array_shift($scopes);
63: } else {
64: $mainScope = $this->_requestScope;
65: $scopes = array();
66: }
67:
68: if (!empty($_FILES[$mainScope])) {
69: foreach ($_FILES[$mainScope] as $fileKey => $scopeData) {
70: foreach ($scopes as $scopeName) {
71: if (isset($scopeData[$scopeName])) {
72: $scopeData = $scopeData[$scopeName];
73: } else {
74: $scopeData[$scopeName] = array();
75: }
76: }
77:
78: if (isset($scopeData[$attrCode])) {
79: $value[$fileKey] = $scopeData[$attrCode];
80: }
81: }
82: } else {
83: $value = array();
84: }
85: } else {
86: if (isset($_FILES[$attrCode])) {
87: $value = $_FILES[$attrCode];
88: } else {
89: $value = array();
90: }
91: }
92:
93: if (!empty($extend['delete'])) {
94: $value['delete'] = true;
95: }
96:
97: return $value;
98: }
99:
100: 101: 102: 103: 104: 105: 106:
107: protected function _validateByRules($value)
108: {
109: $label = $this->getAttribute()->getStoreLabel();
110: $rules = $this->getAttribute()->getValidateRules();
111: $extension = pathinfo($value['name'], PATHINFO_EXTENSION);
112:
113: if (!empty($rules['file_extensions'])) {
114: $extensions = explode(',', $rules['file_extensions']);
115: $extensions = array_map('trim', $extensions);
116: if (!in_array($extension, $extensions)) {
117: return array(
118: Mage::helper('eav')->__('"%s" is not a valid file extension.', $label)
119: );
120: }
121: }
122:
123: 124: 125:
126:
127: $validator = Mage::getSingleton('core/file_validator_notProtectedExtension');
128: if (!$validator->isValid($extension)) {
129: return $validator->getMessages();
130: }
131:
132: if (!is_uploaded_file($value['tmp_name'])) {
133: return array(
134: Mage::helper('eav')->__('"%s" is not a valid file.', $label)
135: );
136: }
137:
138: if (!empty($rules['max_file_size'])) {
139: $size = $value['size'];
140: if ($rules['max_file_size'] < $size) {
141: return array(
142: Mage::helper('eav')->__('"%s" exceeds the allowed file size.', $label)
143: );
144: };
145: }
146:
147: return array();
148: }
149:
150: 151: 152: 153: 154: 155: 156:
157: public function validateValue($value)
158: {
159: if ($this->getIsAjaxRequest()) {
160: return true;
161: }
162:
163: $errors = array();
164: $attribute = $this->getAttribute();
165: $label = $attribute->getStoreLabel();
166:
167: $toDelete = !empty($value['delete']) ? true : false;
168: $toUpload = !empty($value['tmp_name']) ? true : false;
169:
170: if (!$toUpload && !$toDelete && $this->getEntity()->getData($attribute->getAttributeCode())) {
171: return true;
172: }
173:
174: if (!$attribute->getIsRequired() && !$toUpload) {
175: return true;
176: }
177:
178: if ($attribute->getIsRequired() && !$toUpload) {
179: $errors[] = Mage::helper('eav')->__('"%s" is a required value.', $label);
180: }
181:
182: if ($toUpload) {
183: $errors = array_merge($errors, $this->_validateByRules($value));
184: }
185:
186: if (count($errors) == 0) {
187: return true;
188: }
189:
190: return $errors;
191: }
192:
193: 194: 195: 196: 197: 198: 199:
200: public function compactValue($value)
201: {
202: if ($this->getIsAjaxRequest()) {
203: return $this;
204: }
205:
206: $attribute = $this->getAttribute();
207: $original = $this->getEntity()->getData($attribute->getAttributeCode());
208: $toDelete = false;
209: if ($original) {
210: if (!$attribute->getIsRequired() && !empty($value['delete'])) {
211: $toDelete = true;
212: }
213: if (!empty($value['tmp_name'])) {
214: $toDelete = true;
215: }
216: }
217:
218: $path = Mage::getBaseDir('media') . DS . $attribute->getEntity()->getEntityTypeCode();
219:
220:
221: if ($toDelete) {
222: $this->getEntity()->setData($attribute->getAttributeCode(), '');
223: $file = $path . $original;
224: $ioFile = new Varien_Io_File();
225: if ($ioFile->fileExists($file)) {
226: $ioFile->rm($file);
227: }
228: }
229:
230: if (!empty($value['tmp_name'])) {
231: try {
232: $uploader = new Varien_File_Uploader($value);
233: $uploader->setFilesDispersion(true);
234: $uploader->setFilenamesCaseSensitivity(false);
235: $uploader->setAllowRenameFiles(true);
236: $uploader->save($path, $value['name']);
237: $fileName = $uploader->getUploadedFileName();
238: $this->getEntity()->setData($attribute->getAttributeCode(), $fileName);
239: } catch (Exception $e) {
240: Mage::logException($e);
241: }
242: }
243:
244: return $this;
245: }
246:
247: 248: 249: 250: 251: 252:
253: public function restoreValue($value)
254: {
255: return $this;
256: }
257:
258: 259: 260: 261: 262:
263: public function outputValue($format = Mage_Eav_Model_Attribute_Data::OUTPUT_FORMAT_TEXT)
264: {
265: $output = '';
266: $value = $this->getEntity()->getData($this->getAttribute()->getAttributeCode());
267: if ($value) {
268: switch ($format) {
269: case Mage_Eav_Model_Attribute_Data::OUTPUT_FORMAT_JSON:
270: $output = array(
271: 'value' => $value,
272: 'url_key' => Mage::helper('core')->urlEncode($value)
273: );
274: break;
275: }
276: }
277:
278: return $output;
279: }
280: }
281: