PageRenderTime 64ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Eav/Model/Attribute/Data/Multiline.php

https://gitlab.com/blingbang2016/shop
PHP | 157 lines | 84 code | 9 blank | 64 comment | 18 complexity | faf0756527157bbb78ac40d08f0e6ea6 MD5 | raw file
  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@magento.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.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Eav
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * EAV Entity Attribute Multiply line Data Model
  28. *
  29. * @category Mage
  30. * @package Mage_Eav
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Eav_Model_Attribute_Data_Multiline extends Mage_Eav_Model_Attribute_Data_Text
  34. {
  35. /**
  36. * Extract data from request and return value
  37. *
  38. * @param Zend_Controller_Request_Http $request
  39. * @return array|string
  40. */
  41. public function extractValue(Zend_Controller_Request_Http $request)
  42. {
  43. $value = $this->_getRequestValue($request);
  44. if (!is_array($value)) {
  45. $value = false;
  46. } else {
  47. $value = array_map(array($this, '_applyInputFilter'), $value);
  48. }
  49. return $value;
  50. }
  51. /**
  52. * Validate data
  53. * Return true or array of errors
  54. *
  55. * @param array|string $value
  56. * @return boolean|array
  57. */
  58. public function validateValue($value)
  59. {
  60. $errors = array();
  61. $attribute = $this->getAttribute();
  62. if ($value === false) {
  63. // try to load original value and validate it
  64. $value = $this->getEntity()->getDataUsingMethod($attribute->getAttributeCode());
  65. if (!is_array($value)) {
  66. $value = explode("\n", $value);
  67. }
  68. }
  69. if (!is_array($value)) {
  70. $value = array($value);
  71. }
  72. for ($i = 0; $i < $attribute->getMultilineCount(); $i ++) {
  73. if (!isset($value[$i])) {
  74. $value[$i] = null;
  75. }
  76. // validate first line
  77. if ($i == 0) {
  78. $result = parent::validateValue($value[$i]);
  79. if ($result !== true) {
  80. $errors = $result;
  81. }
  82. } else {
  83. if (!empty($value[$i])) {
  84. $result = parent::validateValue($value[$i]);
  85. if ($result !== true) {
  86. $errors = array_merge($errors, $result);
  87. }
  88. }
  89. }
  90. }
  91. if (count($errors) == 0) {
  92. return true;
  93. }
  94. return $errors;
  95. }
  96. /**
  97. * Export attribute value to entity model
  98. *
  99. * @param Mage_Core_Model_Abstract $entity
  100. * @param array|string $value
  101. * @return Mage_Eav_Model_Attribute_Data_Multiline
  102. */
  103. public function compactValue($value)
  104. {
  105. if (is_array($value)) {
  106. $value = trim(implode("\n", $value));
  107. }
  108. return parent::compactValue($value);
  109. }
  110. /**
  111. * Restore attribute value from SESSION to entity model
  112. *
  113. * @param array|string $value
  114. * @return Mage_Eav_Model_Attribute_Data_Multiline
  115. */
  116. public function restoreValue($value)
  117. {
  118. return $this->compactValue($value);
  119. }
  120. /**
  121. * Return formated attribute value from entity model
  122. *
  123. * @return string|array
  124. */
  125. public function outputValue($format = Mage_Eav_Model_Attribute_Data::OUTPUT_FORMAT_TEXT)
  126. {
  127. $values = $this->getEntity()->getData($this->getAttribute()->getAttributeCode());
  128. if (!is_array($values)) {
  129. $values = explode("\n", $values);
  130. }
  131. $values = array_map(array($this, '_applyOutputFilter'), $values);
  132. switch ($format) {
  133. case Mage_Eav_Model_Attribute_Data::OUTPUT_FORMAT_ARRAY:
  134. $output = $values;
  135. break;
  136. case Mage_Eav_Model_Attribute_Data::OUTPUT_FORMAT_HTML:
  137. $output = implode("<br />", $values);
  138. break;
  139. case Mage_Eav_Model_Attribute_Data::OUTPUT_FORMAT_ONELINE:
  140. $output = implode(" ", $values);
  141. break;
  142. default:
  143. $output = implode("\n", $values);
  144. break;
  145. }
  146. return $output;
  147. }
  148. }