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
00029
00030
00031
00032
00033
00034 class Mage_Catalog_Model_Product_Option_Type_Select extends Mage_Catalog_Model_Product_Option_Type_Default
00035 {
00036
00037
00038
00039
00040
00041
00042
00043 public function validateUserValue($values)
00044 {
00045 parent::validateUserValue($values);
00046
00047 $option = $this->getOption();
00048 $value = $this->getUserValue();
00049
00050 if (empty($value) && $option->getIsRequire() && !$this->getProduct()->getSkipCheckRequiredOption()) {
00051 $this->setIsValid(false);
00052 Mage::throwException(Mage::helper('catalog')->__('Please specify the product required option(s)'));
00053 }
00054 if (!$this->_isSingleSelection()) {
00055 $valuesCollection = $option->getOptionValuesByOptionId($value, $this->getProduct()->getStoreId())
00056 ->load();
00057 if ($valuesCollection->count() != count($value)) {
00058 $this->setIsValid(false);
00059 Mage::throwException(Mage::helper('catalog')->__('Please specify the product required option(s)'));
00060 }
00061 }
00062 return $this;
00063 }
00064
00065
00066
00067
00068
00069
00070
00071 public function prepareForCart()
00072 {
00073 if ($this->getIsValid() && $this->getUserValue()) {
00074 return is_array($this->getUserValue()) ? implode(',', $this->getUserValue()) : $this->getUserValue();
00075 } else {
00076 return null;
00077 }
00078 }
00079
00080
00081
00082
00083
00084
00085
00086 public function getFormattedOptionValue($optionValue)
00087 {
00088 if ($this->_formattedOptionValue === null) {
00089 $this->_formattedOptionValue = Mage::helper('core')->htmlEscape(
00090 $this->getEditableOptionValue($optionValue)
00091 );
00092 }
00093 return $this->_formattedOptionValue;
00094 }
00095
00096
00097
00098
00099
00100
00101
00102 public function getPrintableOptionValue($optionValue)
00103 {
00104 return $this->getFormattedOptionValue($optionValue);
00105 }
00106
00107
00108
00109
00110
00111
00112
00113 public function getEditableOptionValue($optionValue)
00114 {
00115 $option = $this->getOption();
00116 $result = '';
00117 if (!$this->_isSingleSelection()) {
00118 foreach (explode(',', $optionValue) as $_value) {
00119 $result .= $option->getValueById($_value)->getTitle() . ', ';
00120 }
00121 $result = Mage::helper('core/string')->substr($result, 0, -2);
00122 } elseif ($this->_isSingleSelection()) {
00123 $result = $option->getValueById($optionValue)->getTitle();
00124 } else {
00125 $result = $optionValue;
00126 }
00127 return $result;
00128 }
00129
00130
00131
00132
00133
00134
00135
00136
00137 public function parseOptionValue($optionValue, $productOptionValues)
00138 {
00139 $_values = array();
00140 if (!$this->_isSingleSelection()) {
00141 foreach (explode(',', $optionValue) as $_value) {
00142 $_value = trim($_value);
00143 if (array_key_exists($_value, $productOptionValues)) {
00144 $_values[] = $productOptionValues[$_value];
00145 }
00146 }
00147 } elseif ($this->_isSingleSelection() && array_key_exists($optionValue, $productOptionValues)) {
00148 $_values[] = $productOptionValues[$optionValue];
00149 }
00150 if (count($_values)) {
00151 return implode(',', $_values);
00152 } else {
00153 return null;
00154 }
00155 }
00156
00157
00158
00159
00160
00161
00162
00163 public function prepareOptionValueForRequest($optionValue)
00164 {
00165 if (!$this->_isSingleSelection()) {
00166 return explode(',', $optionValue);
00167 }
00168 return $optionValue;
00169 }
00170
00171
00172
00173
00174
00175
00176
00177 public function getOptionPrice($optionValue, $basePrice)
00178 {
00179 $option = $this->getOption();
00180 $result = 0;
00181
00182 if (!$this->_isSingleSelection()) {
00183 foreach(explode(',', $optionValue) as $value) {
00184 $result += $this->_getChargableOptionPrice(
00185 $option->getValueById($value)->getPrice(),
00186 $option->getValueById($value)->getPriceType() == 'percent',
00187 $basePrice
00188 );
00189 }
00190 } elseif ($this->_isSingleSelection()) {
00191 $result = $this->_getChargableOptionPrice(
00192 $option->getValueById($optionValue)->getPrice(),
00193 $option->getValueById($optionValue)->getPriceType() == 'percent',
00194 $basePrice
00195 );
00196 }
00197
00198 return $result;
00199 }
00200
00201
00202
00203
00204
00205
00206
00207
00208 public function getOptionSku($optionValue, $skuDelimiter)
00209 {
00210 $option = $this->getOption();
00211
00212 if (!$this->_isSingleSelection()) {
00213 $skus = array();
00214 foreach(explode(',', $optionValue) as $value) {
00215 if ($optionSku = $option->getValueById($value)->getSku()) {
00216 $skus[] = $optionSku;
00217 }
00218 }
00219 $result = implode($skuDelimiter, $skus);
00220 } elseif ($this->_isSingleSelection()) {
00221 $result = $option->getValueById($optionValue)->getSku();
00222 } else {
00223 $result = parent::getOptionSku($optionValue, $skuDelimiter);
00224 }
00225
00226 return $result;
00227 }
00228
00229
00230
00231
00232
00233
00234 protected function _isSingleSelection()
00235 {
00236 $_single = array(
00237 Mage_Catalog_Model_Product_Option::OPTION_TYPE_DROP_DOWN,
00238 Mage_Catalog_Model_Product_Option::OPTION_TYPE_RADIO
00239 );
00240 return in_array($this->getOption()->getType(), $_single);
00241 }
00242 }