PageRenderTime 61ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Customer/Model/Attribute/Data/Abstract.php

https://bitbucket.org/claudiu_marginean/magento-hg-mirror
PHP | 562 lines | 320 code | 39 blank | 203 comment | 38 complexity | 2fa97ae749c803aca15410da305edca2 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Customer
  23. * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Customer Attribute Abstract Data Model
  28. *
  29. * @category Mage
  30. * @package Mage_Customer
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. abstract class Mage_Customer_Model_Attribute_Data_Abstract
  34. {
  35. /**
  36. * Attribute instance
  37. *
  38. * @var Mage_Customer_Model_Attribute
  39. */
  40. protected $_attribite;
  41. /**
  42. * Entity instance
  43. *
  44. * @var Mage_Core_Model_Abstract
  45. */
  46. protected $_entity;
  47. /**
  48. * Request Scope name
  49. *
  50. * @var string
  51. */
  52. protected $_requestScope;
  53. protected $_requestScopeOnly = true;
  54. /**
  55. * Is AJAX request flag
  56. *
  57. * @var boolean
  58. */
  59. protected $_isAjax = false;
  60. /**
  61. * Array of full extracted data
  62. * Needed for depends attributes
  63. *
  64. * @var array
  65. */
  66. protected $_extractedData = array();
  67. /**
  68. * Mage_Core_Model_Locale FORMAT
  69. *
  70. * @var string
  71. */
  72. protected $_dateFilterFormat;
  73. /**
  74. * Set attribute instance
  75. *
  76. * @param Mage_Eav_Model_Entity_Attribute_Abstract $attribute
  77. * @return Mage_Customer_Model_Attribute_Data_Abstract
  78. */
  79. public function setAttribute(Mage_Eav_Model_Entity_Attribute_Abstract $attribute)
  80. {
  81. $this->_attribite = $attribute;
  82. return $this;
  83. }
  84. /**
  85. * Return Attribute instance
  86. *
  87. * @throws Mage_Core_Exception
  88. * @return Mage_Customer_Model_Attribute
  89. */
  90. public function getAttribute()
  91. {
  92. if (!$this->_attribite) {
  93. Mage::throwException(Mage::helper('customer')->__('Attribute object is undefined'));
  94. }
  95. return $this->_attribite;
  96. }
  97. /**
  98. * Set Request scope
  99. *
  100. * @param string $scope
  101. * @return string
  102. */
  103. public function setRequestScope($scope)
  104. {
  105. $this->_requestScope = $scope;
  106. return $this;
  107. }
  108. /**
  109. * Set scope visibility
  110. * Search value only in scope or search value in scope and global
  111. *
  112. * @param boolean $flag
  113. * @return Mage_Customer_Model_Attribute_Data_Abstract
  114. */
  115. public function setRequestScopeOnly($flag)
  116. {
  117. $this->_requestScopeOnly = (bool)$flag;
  118. return $this;
  119. }
  120. /**
  121. * Set entity instance
  122. *
  123. * @param Mage_Core_Model_Abstract $entity
  124. * @return Mage_Customer_Model_Attribute_Data_Abstract
  125. */
  126. public function setEntity(Mage_Core_Model_Abstract $entity)
  127. {
  128. $this->_entity = $entity;
  129. return $this;
  130. }
  131. /**
  132. * Returns entity instance
  133. *
  134. * @return Mage_Core_Model_Abstract
  135. */
  136. public function getEntity()
  137. {
  138. if (!$this->_entity) {
  139. Mage::throwException(Mage::helper('customer')->__('Entity object is undefined'));
  140. }
  141. return $this->_entity;
  142. }
  143. /**
  144. * Set array of full extracted data
  145. *
  146. * @param array $data
  147. * @return Mage_Customer_Model_Attribute_Data_Abstract
  148. */
  149. public function setExtractedData(array $data)
  150. {
  151. $this->_extractedData = $data;
  152. return $this;
  153. }
  154. /**
  155. * Return extracted data
  156. *
  157. * @param string $index
  158. * @return mixed
  159. */
  160. public function getExtractedData($index = null)
  161. {
  162. if (!is_null($index)) {
  163. if (isset($this->_extractedData[$index])) {
  164. return $this->_extractedData[$index];
  165. }
  166. return null;
  167. }
  168. return $this->_extractedData;
  169. }
  170. /**
  171. * Apply attribute input filter to value
  172. *
  173. * @param string $value
  174. * @return string
  175. */
  176. protected function _applyInputFilter($value)
  177. {
  178. if ($value === false) {
  179. return false;
  180. }
  181. $filter = $this->_getFormFilter();
  182. if ($filter) {
  183. $value = $filter->inputFilter($value);
  184. }
  185. return $value;
  186. }
  187. /**
  188. * Return Data Form Input/Output Filter
  189. *
  190. * @return Varien_Data_Form_Filter_Interface|false
  191. */
  192. protected function _getFormFilter()
  193. {
  194. $filterCode = $this->getAttribute()->getInputFilter();
  195. if ($filterCode) {
  196. $filterClass = 'Varien_Data_Form_Filter_' . ucfirst($filterCode);
  197. if ($filterCode == 'date') {
  198. $filter = new $filterClass($this->_dateFilterFormat(), Mage::app()->getLocale()->getLocale());
  199. } else {
  200. $filter = new $filterClass();
  201. }
  202. return $filter;
  203. }
  204. return false;
  205. }
  206. /**
  207. * Get/Set/Reset date filter format
  208. *
  209. * @param string|null|false $format
  210. * @return Mage_Customer_Model_Attribute_Data_Abstract|string
  211. */
  212. protected function _dateFilterFormat($format = null)
  213. {
  214. if (is_null($format)) {
  215. // get format
  216. if (is_null($this->_dateFilterFormat)) {
  217. $this->_dateFilterFormat = Mage_Core_Model_Locale::FORMAT_TYPE_SHORT;
  218. }
  219. return Mage::app()->getLocale()->getDateFormat($this->_dateFilterFormat);
  220. } else if ($format === false) {
  221. // reset value
  222. $this->_dateFilterFormat = null;
  223. return $this;
  224. }
  225. $this->_dateFilterFormat = $format;
  226. return $this;
  227. }
  228. /**
  229. * Apply attribute output filter to value
  230. *
  231. * @param string $value
  232. * @return string
  233. */
  234. protected function _applyOutputFilter($value)
  235. {
  236. $filter = $this->_getFormFilter();
  237. if ($filter) {
  238. $value = $filter->outputFilter($value);
  239. }
  240. return $value;
  241. }
  242. /**
  243. * Validate value by attribute input validation rule
  244. *
  245. * @param string $value
  246. * @return string
  247. */
  248. protected function _validateInputRule($value)
  249. {
  250. // skip validate empty value
  251. if (empty($value)) {
  252. return true;
  253. }
  254. $label = Mage::helper('customer')->__($this->getAttribute()->getStoreLabel());
  255. $validateRules = $this->getAttribute()->getValidateRules();
  256. if (!empty($validateRules['input_validation'])) {
  257. switch ($validateRules['input_validation']) {
  258. case 'alphanumeric':
  259. $validator = new Zend_Validate_Alnum(true);
  260. $validator->setMessage(
  261. Mage::helper('customer')->__('"%s" invalid type entered.', $label),
  262. Zend_Validate_Alnum::INVALID
  263. );
  264. $validator->setMessage(
  265. Mage::helper('customer')->__('"%s" has not only alphabetic and digit characters.', $label),
  266. Zend_Validate_Alnum::NOT_ALNUM
  267. );
  268. $validator->setMessage(
  269. Mage::helper('customer')->__('"%s" is an empty string.', $label),
  270. Zend_Validate_Alnum::STRING_EMPTY
  271. );
  272. if (!$validator->isValid($value)) {
  273. return $validator->getMessages();
  274. }
  275. break;
  276. case 'numeric':
  277. $validator = new Zend_Validate_Digits();
  278. $validator->setMessage(
  279. Mage::helper('customer')->__('"%s" invalid type entered.', $label),
  280. Zend_Validate_Digits::INVALID
  281. );
  282. $validator->setMessage(
  283. Mage::helper('customer')->__('"%s" contains not only digit characters.', $label),
  284. Zend_Validate_Digits::NOT_DIGITS
  285. );
  286. $validator->setMessage(
  287. Mage::helper('customer')->__('"%s" is an empty string.', $label),
  288. Zend_Validate_Digits::STRING_EMPTY
  289. );
  290. if (!$validator->isValid($value)) {
  291. return $validator->getMessages();
  292. }
  293. break;
  294. case 'alpha':
  295. $validator = new Zend_Validate_Alpha(true);
  296. $validator->setMessage(
  297. Mage::helper('customer')->__('"%s" invalid type entered.', $label),
  298. Zend_Validate_Alpha::INVALID
  299. );
  300. $validator->setMessage(
  301. Mage::helper('customer')->__('"%s" has not only alphabetic characters.', $label),
  302. Zend_Validate_Alpha::NOT_ALPHA
  303. );
  304. $validator->setMessage(
  305. Mage::helper('customer')->__('"%s" is an empty string.', $label),
  306. Zend_Validate_Alpha::STRING_EMPTY
  307. );
  308. if (!$validator->isValid($value)) {
  309. return $validator->getMessages();
  310. }
  311. break;
  312. case 'email':
  313. /**
  314. $this->__("'%value%' appears to be a DNS hostname but the given punycode notation cannot be decoded")
  315. $this->__("Invalid type given. String expected")
  316. $this->__("'%value%' appears to be a DNS hostname but contains a dash in an invalid position")
  317. $this->__("'%value%' does not match the expected structure for a DNS hostname")
  318. $this->__("'%value%' appears to be a DNS hostname but cannot match against hostname schema for TLD '%tld%'")
  319. $this->__("'%value%' does not appear to be a valid local network name")
  320. $this->__("'%value%' does not appear to be a valid URI hostname")
  321. $this->__("'%value%' appears to be an IP address, but IP addresses are not allowed")
  322. $this->__("'%value%' appears to be a local network name but local network names are not allowed")
  323. $this->__("'%value%' appears to be a DNS hostname but cannot extract TLD part")
  324. $this->__("'%value%' appears to be a DNS hostname but cannot match TLD against known list")
  325. */
  326. $validator = new Zend_Validate_EmailAddress();
  327. $validator->setMessage(
  328. Mage::helper('customer')->__('"%s" invalid type entered.', $label),
  329. Zend_Validate_EmailAddress::INVALID
  330. );
  331. $validator->setMessage(
  332. Mage::helper('customer')->__('"%s" is not a valid email address.', $label),
  333. Zend_Validate_EmailAddress::INVALID_FORMAT
  334. );
  335. $validator->setMessage(
  336. Mage::helper('customer')->__('"%s" is not a valid hostname.', $label),
  337. Zend_Validate_EmailAddress::INVALID_HOSTNAME
  338. );
  339. $validator->setMessage(
  340. Mage::helper('customer')->__('"%s" is not a valid hostname.', $label),
  341. Zend_Validate_EmailAddress::INVALID_MX_RECORD
  342. );
  343. $validator->setMessage(
  344. Mage::helper('customer')->__('"%s" is not a valid hostname.', $label),
  345. Zend_Validate_EmailAddress::INVALID_MX_RECORD
  346. );
  347. $validator->setMessage(
  348. Mage::helper('customer')->__('"%s" is not a valid email address.', $label),
  349. Zend_Validate_EmailAddress::DOT_ATOM
  350. );
  351. $validator->setMessage(
  352. Mage::helper('customer')->__('"%s" is not a valid email address.', $label),
  353. Zend_Validate_EmailAddress::QUOTED_STRING
  354. );
  355. $validator->setMessage(
  356. Mage::helper('customer')->__('"%s" is not a valid email address.', $label),
  357. Zend_Validate_EmailAddress::INVALID_LOCAL_PART
  358. );
  359. $validator->setMessage(
  360. Mage::helper('customer')->__('"%s" exceeds the allowed length.', $label),
  361. Zend_Validate_EmailAddress::LENGTH_EXCEEDED
  362. );
  363. $validator->setMessage(
  364. Mage::helper('customer')->__("'%value%' appears to be an IP address, but IP addresses are not allowed"),
  365. Zend_Validate_Hostname::IP_ADDRESS_NOT_ALLOWED
  366. );
  367. $validator->setMessage(
  368. Mage::helper('customer')->__("'%value%' appears to be a DNS hostname but cannot match TLD against known list"),
  369. Zend_Validate_Hostname::UNKNOWN_TLD
  370. );
  371. $validator->setMessage(
  372. Mage::helper('customer')->__("'%value%' appears to be a DNS hostname but contains a dash in an invalid position"),
  373. Zend_Validate_Hostname::INVALID_DASH
  374. );
  375. $validator->setMessage(
  376. Mage::helper('customer')->__("'%value%' appears to be a DNS hostname but cannot match against hostname schema for TLD '%tld%'"),
  377. Zend_Validate_Hostname::INVALID_HOSTNAME_SCHEMA
  378. );
  379. $validator->setMessage(
  380. Mage::helper('customer')->__("'%value%' appears to be a DNS hostname but cannot extract TLD part"),
  381. Zend_Validate_Hostname::UNDECIPHERABLE_TLD
  382. );
  383. $validator->setMessage(
  384. Mage::helper('customer')->__("'%value%' does not appear to be a valid local network name"),
  385. Zend_Validate_Hostname::INVALID_LOCAL_NAME
  386. );
  387. $validator->setMessage(
  388. Mage::helper('customer')->__("'%value%' appears to be a local network name but local network names are not allowed"),
  389. Zend_Validate_Hostname::LOCAL_NAME_NOT_ALLOWED
  390. );
  391. $validator->setMessage(
  392. Mage::helper('customer')->__("'%value%' appears to be a DNS hostname but the given punycode notation cannot be decoded"),
  393. Zend_Validate_Hostname::CANNOT_DECODE_PUNYCODE
  394. );
  395. if (!$validator->isValid($value)) {
  396. return array_unique($validator->getMessages());
  397. }
  398. break;
  399. case 'url':
  400. $parsedUrl = parse_url($value);
  401. if ($parsedUrl === false || empty($parsedUrl['scheme']) || empty($parsedUrl['host'])) {
  402. return array(Mage::helper('customer')->__('"%s" is not a valid URL.', $label));
  403. }
  404. $validator = new Zend_Validate_Hostname();
  405. if (!$validator->isValid($parsedUrl['host'])) {
  406. return array(Mage::helper('customer')->__('"%s" is not a valid URL.', $label));
  407. }
  408. break;
  409. case 'date':
  410. $validator = new Zend_Validate_Date(Varien_Date::DATE_INTERNAL_FORMAT);
  411. $validator->setMessage(
  412. Mage::helper('customer')->__('"%s" invalid type entered.', $label),
  413. Zend_Validate_Date::INVALID
  414. );
  415. $validator->setMessage(
  416. Mage::helper('customer')->__('"%s" is not a valid date.', $label),
  417. Zend_Validate_Date::INVALID_DATE
  418. );
  419. $validator->setMessage(
  420. Mage::helper('customer')->__('"%s" does not fit the entered date format.', $label),
  421. Zend_Validate_Date::FALSEFORMAT
  422. );
  423. if (!$validator->isValid($value)) {
  424. return array_unique($validator->getMessages());
  425. }
  426. break;
  427. }
  428. }
  429. return true;
  430. }
  431. /**
  432. * Set is AJAX Request flag
  433. *
  434. * @param boolean $flag
  435. * @return Mage_Customer_Model_Form
  436. */
  437. public function setIsAjaxRequest($flag = true)
  438. {
  439. $this->_isAjax = (bool)$flag;
  440. return $this;
  441. }
  442. /**
  443. * Return is AJAX Request
  444. *
  445. * @return boolean
  446. */
  447. public function getIsAjaxRequest()
  448. {
  449. return $this->_isAjax;
  450. }
  451. /**
  452. * Return Original Attribute value from Request
  453. *
  454. * @param Zend_Controller_Request_Http $request
  455. * @return mixed
  456. */
  457. protected function _getRequestValue(Zend_Controller_Request_Http $request)
  458. {
  459. $attrCode = $this->getAttribute()->getAttributeCode();
  460. if ($this->_requestScope) {
  461. if (strpos($this->_requestScope, '/') !== false) {
  462. $params = $request->getParams();
  463. $parts = explode('/', $this->_requestScope);
  464. foreach ($parts as $part) {
  465. if (isset($params[$part])) {
  466. $params = $params[$part];
  467. } else {
  468. $params = array();
  469. }
  470. }
  471. } else {
  472. $params = $request->getParam($this->_requestScope);
  473. }
  474. if (isset($params[$attrCode])) {
  475. $value = $params[$attrCode];
  476. } else {
  477. $value = false;
  478. }
  479. if (!$this->_requestScopeOnly && $value === false) {
  480. $value = $request->getParam($attrCode, false);
  481. }
  482. } else {
  483. $value = $request->getParam($attrCode, false);
  484. }
  485. return $value;
  486. }
  487. /**
  488. * Extract data from request and return value
  489. *
  490. * @param Zend_Controller_Request_Http $request
  491. * @return array|string
  492. */
  493. abstract public function extractValue(Zend_Controller_Request_Http $request);
  494. /**
  495. * Validate data
  496. *
  497. * @param array|string $value
  498. * @throws Mage_Core_Exception
  499. * @return boolean
  500. */
  501. abstract public function validateValue($value);
  502. /**
  503. * Export attribute value to entity model
  504. *
  505. * @param array|string $value
  506. * @return Mage_Customer_Model_Attribute_Data_Abstract
  507. */
  508. abstract public function compactValue($value);
  509. /**
  510. * Restore attribute value from SESSION to entity model
  511. *
  512. * @param array|string $value
  513. * @return Mage_Customer_Model_Attribute_Data_Abstract
  514. */
  515. abstract public function restoreValue($value);
  516. /**
  517. * Return formated attribute value from entity model
  518. *
  519. * @param string $format
  520. * @return string|array
  521. */
  522. abstract public function outputValue($format = Mage_Customer_Model_Attribute_Data::OUTPUT_FORMAT_TEXT);
  523. }