00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 class Mage_Eav_Model_Entity_Attribute_Source_Table extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
00029 {
00030
00031
00032
00033
00034
00035 protected $_optionsDefault = array();
00036
00037
00038
00039
00040
00041
00042
00043
00044 public function getAllOptions($withEmpty = true, $defaultValues = false)
00045 {
00046 $storeId = $this->getAttribute()->getStoreId();
00047 if (!is_array($this->_options)) {
00048 $this->_options = array();
00049 }
00050 if (!is_array($this->_optionsDefault)) {
00051 $this->_optionsDefault = array();
00052 }
00053 if (!isset($this->_options[$storeId])) {
00054 $collection = Mage::getResourceModel('eav/entity_attribute_option_collection')
00055 ->setPositionOrder('asc')
00056 ->setAttributeFilter($this->getAttribute()->getId())
00057 ->setStoreFilter($this->getAttribute()->getStoreId())
00058 ->load();
00059 $this->_options[$storeId] = $collection->toOptionArray();
00060 $this->_optionsDefault[$storeId] = $collection->toOptionArray('default_value');
00061 }
00062 $options = ($defaultValues ? $this->_optionsDefault[$storeId] : $this->_options[$storeId]);
00063 if ($withEmpty) {
00064 array_unshift($options, array('label'=>'', 'value'=>''));
00065 }
00066 return $options;
00067 }
00068
00069
00070
00071
00072
00073
00074
00075 public function getOptionText($value)
00076 {
00077 $isMultiple = false;
00078 if (strpos($value, ',')) {
00079 $isMultiple = true;
00080 $value = explode(',', $value);
00081 }
00082
00083 $options = $this->getAllOptions(false);
00084
00085 if ($isMultiple) {
00086 $values = array();
00087 foreach ($options as $item) {
00088 if (in_array($item['value'], $value)) {
00089 $values[] = $item['label'];
00090 }
00091 }
00092 return $values;
00093 }
00094 else {
00095 foreach ($options as $item) {
00096 if ($item['value'] == $value) {
00097 return $item['label'];
00098 }
00099 }
00100 return false;
00101 }
00102 }
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112 public function addValueSortToCollection($collection, $dir = 'asc')
00113 {
00114 $valueTable1 = $this->getAttribute()->getAttributeCode() . '_t1';
00115 $valueTable2 = $this->getAttribute()->getAttributeCode() . '_t2';
00116 $collection->getSelect()
00117 ->joinLeft(
00118 array($valueTable1 => $this->getAttribute()->getBackend()->getTable()),
00119 "`e`.`entity_id`=`{$valueTable1}`.`entity_id`"
00120 . " AND `{$valueTable1}`.`attribute_id`='{$this->getAttribute()->getId()}'"
00121 . " AND `{$valueTable1}`.`store_id`='0'",
00122 array())
00123 ->joinLeft(
00124 array($valueTable2 => $this->getAttribute()->getBackend()->getTable()),
00125 "`e`.`entity_id`=`{$valueTable2}`.`entity_id`"
00126 . " AND `{$valueTable2}`.`attribute_id`='{$this->getAttribute()->getId()}'"
00127 . " AND `{$valueTable2}`.`store_id`='{$collection->getStoreId()}'",
00128 array()
00129 );
00130 $valueExpr = new Zend_Db_Expr("IFNULL(`{$valueTable2}`.`value`, `{$valueTable1}`.`value`)");
00131
00132 Mage::getResourceModel('eav/entity_attribute_option')
00133 ->addOptionValueToCollection($collection, $this->getAttribute(), $valueExpr);
00134
00135 $collection->getSelect()
00136 ->order("{$this->getAttribute()->getAttributeCode()} {$dir}");
00137
00138 return $this;
00139 }
00140
00141
00142
00143
00144
00145
00146 public function getFlatColums()
00147 {
00148 $columns = array();
00149 $columns[$this->getAttribute()->getAttributeCode()] = array(
00150 'type' => 'int',
00151 'unsigned' => false,
00152 'is_null' => true,
00153 'default' => null,
00154 'extra' => null
00155 );
00156 if ($this->getAttribute()->getFrontend()->getInputType() != 'multiselect') {
00157 $columns[$this->getAttribute()->getAttributeCode() . '_value'] = array(
00158 'type' => 'varchar(255)',
00159 'unsigned' => false,
00160 'is_null' => true,
00161 'default' => null,
00162 'extra' => null
00163 );
00164 }
00165
00166 return $columns;
00167 }
00168
00169
00170
00171
00172
00173
00174 public function getFlatIndexes()
00175 {
00176 $indexes = array();
00177
00178 $index = 'IDX_' . strtoupper($this->getAttribute()->getAttributeCode());
00179 $indexes[$index] = array(
00180 'type' => 'index',
00181 'fields' => array($this->getAttribute()->getAttributeCode())
00182 );
00183
00184 $sortable = $this->getAttribute()->getUsedForSortBy();
00185 if ($sortable and $this->getAttribute()->getFrontend()->getInputType() != 'multiselect') {
00186 $index = 'IDX_' . strtoupper($this->getAttribute()->getAttributeCode()) . '_VALUE';
00187 $indexes[$index] = array(
00188 'type' => 'index',
00189 'fields' => array($this->getAttribute()->getAttributeCode() . '_value')
00190 );
00191 }
00192
00193 return $indexes;
00194 }
00195
00196
00197
00198
00199
00200
00201
00202
00203 public function getFlatUpdateSelect($store)
00204 {
00205 return Mage::getResourceModel('eav/entity_attribute_option')
00206 ->getFlatUpdateSelect($this->getAttribute(), $store);
00207 }
00208 }