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:
35: abstract class Mage_Eav_Model_Attribute_Data_Abstract
36: {
37: 38: 39: 40: 41:
42: protected $_attribite;
43:
44: 45: 46: 47: 48:
49: protected $_entity;
50:
51: 52: 53: 54: 55:
56: protected $_requestScope;
57:
58: 59: 60: 61: 62:
63: protected $_requestScopeOnly = true;
64:
65: 66: 67: 68: 69:
70: protected $_isAjax = false;
71:
72: 73: 74: 75: 76: 77:
78: protected = array();
79:
80: 81: 82: 83: 84:
85: protected $_dateFilterFormat;
86:
87: 88: 89: 90: 91: 92:
93: public function setAttribute(Mage_Eav_Model_Entity_Attribute_Abstract $attribute)
94: {
95: $this->_attribite = $attribute;
96: return $this;
97: }
98:
99: 100: 101: 102: 103: 104:
105: public function getAttribute()
106: {
107: if (!$this->_attribite) {
108: Mage::throwException(Mage::helper('eav')->__('Attribute object is undefined'));
109: }
110: return $this->_attribite;
111: }
112:
113: 114: 115: 116: 117: 118:
119: public function setRequestScope($scope)
120: {
121: $this->_requestScope = $scope;
122: return $this;
123: }
124:
125: 126: 127: 128: 129: 130: 131:
132: public function setRequestScopeOnly($flag)
133: {
134: $this->_requestScopeOnly = (bool)$flag;
135: return $this;
136: }
137:
138: 139: 140: 141: 142: 143:
144: public function setEntity(Mage_Core_Model_Abstract $entity)
145: {
146: $this->_entity = $entity;
147: return $this;
148: }
149:
150: 151: 152: 153: 154:
155: public function getEntity()
156: {
157: if (!$this->_entity) {
158: Mage::throwException(Mage::helper('eav')->__('Entity object is undefined'));
159: }
160: return $this->_entity;
161: }
162:
163: 164: 165: 166: 167: 168:
169: public function (array $data)
170: {
171: $this->_extractedData = $data;
172: return $this;
173: }
174:
175: 176: 177: 178: 179: 180:
181: public function ($index = null)
182: {
183: if (!is_null($index)) {
184: if (isset($this->_extractedData[$index])) {
185: return $this->_extractedData[$index];
186: }
187: return null;
188: }
189: return $this->_extractedData;
190: }
191:
192: 193: 194: 195: 196: 197:
198: protected function _applyInputFilter($value)
199: {
200: if ($value === false) {
201: return false;
202: }
203:
204: $filter = $this->_getFormFilter();
205: if ($filter) {
206: $value = $filter->inputFilter($value);
207: }
208:
209: return $value;
210: }
211:
212: 213: 214: 215: 216:
217: protected function _getFormFilter()
218: {
219: $filterCode = $this->getAttribute()->getInputFilter();
220: if ($filterCode) {
221: $filterClass = 'Varien_Data_Form_Filter_' . ucfirst($filterCode);
222: if ($filterCode == 'date') {
223: $filter = new $filterClass($this->_dateFilterFormat(), Mage::app()->getLocale()->getLocale());
224: } else {
225: $filter = new $filterClass();
226: }
227: return $filter;
228: }
229: return false;
230: }
231:
232: 233: 234: 235: 236: 237:
238: protected function _dateFilterFormat($format = null)
239: {
240: if (is_null($format)) {
241:
242: if (is_null($this->_dateFilterFormat)) {
243: $this->_dateFilterFormat = Mage_Core_Model_Locale::FORMAT_TYPE_SHORT;
244: }
245: return Mage::app()->getLocale()->getDateFormat($this->_dateFilterFormat);
246: } else if ($format === false) {
247:
248: $this->_dateFilterFormat = null;
249: return $this;
250: }
251:
252: $this->_dateFilterFormat = $format;
253: return $this;
254: }
255:
256: 257: 258: 259: 260: 261:
262: protected function _applyOutputFilter($value)
263: {
264: $filter = $this->_getFormFilter();
265: if ($filter) {
266: $value = $filter->outputFilter($value);
267: }
268:
269: return $value;
270: }
271:
272: 273: 274: 275: 276: 277:
278: protected function _validateInputRule($value)
279: {
280:
281: if (empty($value)) {
282: return true;
283: }
284:
285: $label = $this->getAttribute()->getStoreLabel();
286: $validateRules = $this->getAttribute()->getValidateRules();
287:
288: if (!empty($validateRules['input_validation'])) {
289: switch ($validateRules['input_validation']) {
290: case 'alphanumeric':
291: $validator = new Zend_Validate_Alnum(true);
292: $validator->setMessage(
293: Mage::helper('eav')->__('"%s" invalid type entered.', $label),
294: Zend_Validate_Alnum::INVALID
295: );
296: $validator->setMessage(
297: Mage::helper('eav')->__('"%s" has not only alphabetic and digit characters.', $label),
298: Zend_Validate_Alnum::NOT_ALNUM
299: );
300: $validator->setMessage(
301: Mage::helper('eav')->__('"%s" is an empty string.', $label),
302: Zend_Validate_Alnum::STRING_EMPTY
303: );
304: if (!$validator->isValid($value)) {
305: return $validator->getMessages();
306: }
307: break;
308: case 'numeric':
309: $validator = new Zend_Validate_Digits();
310: $validator->setMessage(
311: Mage::helper('eav')->__('"%s" invalid type entered.', $label),
312: Zend_Validate_Digits::INVALID
313: );
314: $validator->setMessage(
315: Mage::helper('eav')->__('"%s" contains not only digit characters.', $label),
316: Zend_Validate_Digits::NOT_DIGITS
317: );
318: $validator->setMessage(
319: Mage::helper('eav')->__('"%s" is an empty string.', $label),
320: Zend_Validate_Digits::STRING_EMPTY
321: );
322: if (!$validator->isValid($value)) {
323: return $validator->getMessages();
324: }
325: break;
326: case 'alpha':
327: $validator = new Zend_Validate_Alpha(true);
328: $validator->setMessage(
329: Mage::helper('eav')->__('"%s" invalid type entered.', $label),
330: Zend_Validate_Alpha::INVALID
331: );
332: $validator->setMessage(
333: Mage::helper('eav')->__('"%s" has not only alphabetic characters.', $label),
334: Zend_Validate_Alpha::NOT_ALPHA
335: );
336: $validator->setMessage(
337: Mage::helper('eav')->__('"%s" is an empty string.', $label),
338: Zend_Validate_Alpha::STRING_EMPTY
339: );
340: if (!$validator->isValid($value)) {
341: return $validator->getMessages();
342: }
343: break;
344: case 'email':
345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357:
358: $validator = new Zend_Validate_EmailAddress();
359: $validator->setMessage(
360: Mage::helper('eav')->__('"%s" invalid type entered.', $label),
361: Zend_Validate_EmailAddress::INVALID
362: );
363: $validator->setMessage(
364: Mage::helper('eav')->__('"%s" is not a valid email address.', $label),
365: Zend_Validate_EmailAddress::INVALID_FORMAT
366: );
367: $validator->setMessage(
368: Mage::helper('eav')->__('"%s" is not a valid hostname.', $label),
369: Zend_Validate_EmailAddress::INVALID_HOSTNAME
370: );
371: $validator->setMessage(
372: Mage::helper('eav')->__('"%s" is not a valid hostname.', $label),
373: Zend_Validate_EmailAddress::INVALID_MX_RECORD
374: );
375: $validator->setMessage(
376: Mage::helper('eav')->__('"%s" is not a valid hostname.', $label),
377: Zend_Validate_EmailAddress::INVALID_MX_RECORD
378: );
379: $validator->setMessage(
380: Mage::helper('eav')->__('"%s" is not a valid email address.', $label),
381: Zend_Validate_EmailAddress::DOT_ATOM
382: );
383: $validator->setMessage(
384: Mage::helper('eav')->__('"%s" is not a valid email address.', $label),
385: Zend_Validate_EmailAddress::QUOTED_STRING
386: );
387: $validator->setMessage(
388: Mage::helper('eav')->__('"%s" is not a valid email address.', $label),
389: Zend_Validate_EmailAddress::INVALID_LOCAL_PART
390: );
391: $validator->setMessage(
392: Mage::helper('eav')->__('"%s" exceeds the allowed length.', $label),
393: Zend_Validate_EmailAddress::LENGTH_EXCEEDED
394: );
395: $validator->setMessage(
396: Mage::helper('customer')->__("'%value%' appears to be an IP address, but IP addresses are not allowed"),
397: Zend_Validate_Hostname::IP_ADDRESS_NOT_ALLOWED
398: );
399: $validator->setMessage(
400: Mage::helper('customer')->__("'%value%' appears to be a DNS hostname but cannot match TLD against known list"),
401: Zend_Validate_Hostname::UNKNOWN_TLD
402: );
403: $validator->setMessage(
404: Mage::helper('customer')->__("'%value%' appears to be a DNS hostname but contains a dash in an invalid position"),
405: Zend_Validate_Hostname::INVALID_DASH
406: );
407: $validator->setMessage(
408: Mage::helper('customer')->__("'%value%' appears to be a DNS hostname but cannot match against hostname schema for TLD '%tld%'"),
409: Zend_Validate_Hostname::INVALID_HOSTNAME_SCHEMA
410: );
411: $validator->setMessage(
412: Mage::helper('customer')->__("'%value%' appears to be a DNS hostname but cannot extract TLD part"),
413: Zend_Validate_Hostname::UNDECIPHERABLE_TLD
414: );
415: $validator->setMessage(
416: Mage::helper('customer')->__("'%value%' does not appear to be a valid local network name"),
417: Zend_Validate_Hostname::INVALID_LOCAL_NAME
418: );
419: $validator->setMessage(
420: Mage::helper('customer')->__("'%value%' appears to be a local network name but local network names are not allowed"),
421: Zend_Validate_Hostname::LOCAL_NAME_NOT_ALLOWED
422: );
423: $validator->setMessage(
424: Mage::helper('customer')->__("'%value%' appears to be a DNS hostname but the given punycode notation cannot be decoded"),
425: Zend_Validate_Hostname::CANNOT_DECODE_PUNYCODE
426: );
427: if (!$validator->isValid($value)) {
428: return array_unique($validator->getMessages());
429: }
430: break;
431: case 'url':
432: $parsedUrl = parse_url($value);
433: if ($parsedUrl === false || empty($parsedUrl['scheme']) || empty($parsedUrl['host'])) {
434: return array(Mage::helper('eav')->__('"%s" is not a valid URL.', $label));
435: }
436: $validator = new Zend_Validate_Hostname();
437: if (!$validator->isValid($parsedUrl['host'])) {
438: return array(Mage::helper('eav')->__('"%s" is not a valid URL.', $label));
439: }
440: break;
441: case 'date':
442: $validator = new Zend_Validate_Date(Varien_Date::DATE_INTERNAL_FORMAT);
443: $validator->setMessage(
444: Mage::helper('eav')->__('"%s" invalid type entered.', $label),
445: Zend_Validate_Date::INVALID
446: );
447: $validator->setMessage(
448: Mage::helper('eav')->__('"%s" is not a valid date.', $label),
449: Zend_Validate_Date::INVALID_DATE
450: );
451: $validator->setMessage(
452: Mage::helper('eav')->__('"%s" does not fit the entered date format.', $label),
453: Zend_Validate_Date::FALSEFORMAT
454: );
455: if (!$validator->isValid($value)) {
456: return array_unique($validator->getMessages());
457: }
458:
459: break;
460: }
461: }
462: return true;
463: }
464:
465: 466: 467: 468: 469: 470:
471: public function setIsAjaxRequest($flag = true)
472: {
473: $this->_isAjax = (bool)$flag;
474: return $this;
475: }
476:
477: 478: 479: 480: 481:
482: public function getIsAjaxRequest()
483: {
484: return $this->_isAjax;
485: }
486:
487: 488: 489: 490: 491: 492:
493: protected function _getRequestValue(Zend_Controller_Request_Http $request)
494: {
495: $attrCode = $this->getAttribute()->getAttributeCode();
496: if ($this->_requestScope) {
497: if (strpos($this->_requestScope, '/') !== false) {
498: $params = $request->getParams();
499: $parts = explode('/', $this->_requestScope);
500: foreach ($parts as $part) {
501: if (isset($params[$part])) {
502: $params = $params[$part];
503: } else {
504: $params = array();
505: }
506: }
507: } else {
508: $params = $request->getParam($this->_requestScope);
509: }
510:
511: if (isset($params[$attrCode])) {
512: $value = $params[$attrCode];
513: } else {
514: $value = false;
515: }
516:
517: if (!$this->_requestScopeOnly && $value === false) {
518: $value = $request->getParam($attrCode, false);
519: }
520: } else {
521: $value = $request->getParam($attrCode, false);
522: }
523: return $value;
524: }
525:
526: 527: 528: 529: 530: 531:
532: abstract public function (Zend_Controller_Request_Http $request);
533:
534: 535: 536: 537: 538: 539: 540:
541: abstract public function validateValue($value);
542:
543: 544: 545: 546: 547: 548:
549: abstract public function compactValue($value);
550:
551: 552: 553: 554: 555: 556:
557: abstract public function restoreValue($value);
558:
559: 560: 561: 562: 563: 564:
565: abstract public function outputValue($format = Mage_Eav_Model_Attribute_Data::OUTPUT_FORMAT_TEXT);
566: }
567: