PageRenderTime 42ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Eav/Model/Entity/Attribute/Source/Table.php

https://bitbucket.org/jokusafet/magento2
PHP | 236 lines | 149 code | 21 blank | 66 comment | 17 complexity | 7ab43bd835de519ce794daaec9f98caa 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@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_Eav
  23. * @copyright Copyright (c) 2012 X.commerce, Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. class Mage_Eav_Model_Entity_Attribute_Source_Table extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
  27. {
  28. /**
  29. * Default values for option cache
  30. *
  31. * @var array
  32. */
  33. protected $_optionsDefault = array();
  34. /**
  35. * Retrieve Full Option values array
  36. *
  37. * @param bool $withEmpty Add empty option to array
  38. * @param bool $defaultValues
  39. * @return array
  40. */
  41. public function getAllOptions($withEmpty = true, $defaultValues = false)
  42. {
  43. $storeId = $this->getAttribute()->getStoreId();
  44. if (!is_array($this->_options)) {
  45. $this->_options = array();
  46. }
  47. if (!is_array($this->_optionsDefault)) {
  48. $this->_optionsDefault = array();
  49. }
  50. if (!isset($this->_options[$storeId])) {
  51. $collection = Mage::getResourceModel('Mage_Eav_Model_Resource_Entity_Attribute_Option_Collection')
  52. ->setPositionOrder('asc')
  53. ->setAttributeFilter($this->getAttribute()->getId())
  54. ->setStoreFilter($this->getAttribute()->getStoreId())
  55. ->load();
  56. $this->_options[$storeId] = $collection->toOptionArray();
  57. $this->_optionsDefault[$storeId] = $collection->toOptionArray('default_value');
  58. }
  59. $options = ($defaultValues ? $this->_optionsDefault[$storeId] : $this->_options[$storeId]);
  60. if ($withEmpty) {
  61. array_unshift($options, array('label' => '', 'value' => ''));
  62. }
  63. return $options;
  64. }
  65. /**
  66. * Get a text for option value
  67. *
  68. * @param string|integer $value
  69. * @return string
  70. */
  71. public function getOptionText($value)
  72. {
  73. $isMultiple = false;
  74. if (strpos($value, ',')) {
  75. $isMultiple = true;
  76. $value = explode(',', $value);
  77. }
  78. $options = $this->getAllOptions(false);
  79. if ($isMultiple) {
  80. $values = array();
  81. foreach ($options as $item) {
  82. if (in_array($item['value'], $value)) {
  83. $values[] = $item['label'];
  84. }
  85. }
  86. return $values;
  87. }
  88. foreach ($options as $item) {
  89. if ($item['value'] == $value) {
  90. return $item['label'];
  91. }
  92. }
  93. return false;
  94. }
  95. /**
  96. * Add Value Sort To Collection Select
  97. *
  98. * @param Mage_Eav_Model_Entity_Collection_Abstract $collection
  99. * @param string $dir
  100. *
  101. * @return Mage_Eav_Model_Entity_Attribute_Source_Table
  102. */
  103. public function addValueSortToCollection($collection, $dir = Varien_Db_Select::SQL_ASC)
  104. {
  105. $valueTable1 = $this->getAttribute()->getAttributeCode() . '_t1';
  106. $valueTable2 = $this->getAttribute()->getAttributeCode() . '_t2';
  107. $collection->getSelect()
  108. ->joinLeft(
  109. array($valueTable1 => $this->getAttribute()->getBackend()->getTable()),
  110. "e.entity_id={$valueTable1}.entity_id"
  111. . " AND {$valueTable1}.attribute_id='{$this->getAttribute()->getId()}'"
  112. . " AND {$valueTable1}.store_id=0",
  113. array())
  114. ->joinLeft(
  115. array($valueTable2 => $this->getAttribute()->getBackend()->getTable()),
  116. "e.entity_id={$valueTable2}.entity_id"
  117. . " AND {$valueTable2}.attribute_id='{$this->getAttribute()->getId()}'"
  118. . " AND {$valueTable2}.store_id='{$collection->getStoreId()}'",
  119. array()
  120. );
  121. $valueExpr = $collection->getSelect()->getAdapter()
  122. ->getCheckSql("{$valueTable2}.value_id > 0", "{$valueTable2}.value", "{$valueTable1}.value");
  123. Mage::getResourceModel('Mage_Eav_Model_Resource_Entity_Attribute_Option')
  124. ->addOptionValueToCollection($collection, $this->getAttribute(), $valueExpr);
  125. $collection->getSelect()
  126. ->order("{$this->getAttribute()->getAttributeCode()} {$dir}");
  127. return $this;
  128. }
  129. /**
  130. * Retrieve Column(s) for Flat
  131. *
  132. * @return array
  133. */
  134. public function getFlatColums()
  135. {
  136. $columns = array();
  137. $attributeCode = $this->getAttribute()->getAttributeCode();
  138. $isMulti = $this->getAttribute()->getFrontend()->getInputType() == 'multiselect';
  139. if (Mage::helper('Mage_Core_Helper_Data')->useDbCompatibleMode()) {
  140. $columns[$attributeCode] = array(
  141. 'type' => $isMulti ? 'varchar(255)' : 'int',
  142. 'unsigned' => false,
  143. 'is_null' => true,
  144. 'default' => null,
  145. 'extra' => null
  146. );
  147. if (!$isMulti) {
  148. $columns[$attributeCode . '_value'] = array(
  149. 'type' => 'varchar(255)',
  150. 'unsigned' => false,
  151. 'is_null' => true,
  152. 'default' => null,
  153. 'extra' => null
  154. );
  155. }
  156. } else {
  157. $type = ($isMulti) ? Varien_Db_Ddl_Table::TYPE_TEXT : Varien_Db_Ddl_Table::TYPE_INTEGER;
  158. $columns[$attributeCode] = array(
  159. 'type' => $type,
  160. 'length' => $isMulti ? '255' : null,
  161. 'unsigned' => false,
  162. 'nullable' => true,
  163. 'default' => null,
  164. 'extra' => null,
  165. 'comment' => $attributeCode . ' column'
  166. );
  167. if (!$isMulti) {
  168. $columns[$attributeCode . '_value'] = array(
  169. 'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
  170. 'length' => 255,
  171. 'unsigned' => false,
  172. 'nullable' => true,
  173. 'default' => null,
  174. 'extra' => null,
  175. 'comment' => $attributeCode . ' column'
  176. );
  177. }
  178. }
  179. return $columns;
  180. }
  181. /**
  182. * Retrieve Indexes for Flat
  183. *
  184. * @return array
  185. */
  186. public function getFlatIndexes()
  187. {
  188. $indexes = array();
  189. $index = sprintf('IDX_%s', strtoupper($this->getAttribute()->getAttributeCode()));
  190. $indexes[$index] = array(
  191. 'type' => 'index',
  192. 'fields' => array($this->getAttribute()->getAttributeCode())
  193. );
  194. $sortable = $this->getAttribute()->getUsedForSortBy();
  195. if ($sortable && $this->getAttribute()->getFrontend()->getInputType() != 'multiselect') {
  196. $index = sprintf('IDX_%s_VALUE', strtoupper($this->getAttribute()->getAttributeCode()));
  197. $indexes[$index] = array(
  198. 'type' => 'index',
  199. 'fields' => array($this->getAttribute()->getAttributeCode() . '_value')
  200. );
  201. }
  202. return $indexes;
  203. }
  204. /**
  205. * Retrieve Select For Flat Attribute update
  206. *
  207. * @param int $store
  208. * @return Varien_Db_Select|null
  209. */
  210. public function getFlatUpdateSelect($store)
  211. {
  212. return Mage::getResourceModel('Mage_Eav_Model_Resource_Entity_Attribute_Option')
  213. ->getFlatUpdateSelect($this->getAttribute(), $store);
  214. }
  215. }