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_Date 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 $dateValid = true;
00051 if ($this->_dateExists()) {
00052 if ($this->useCalendar()) {
00053 $dateValid = isset($value['date']) && preg_match('/^[0-9]{1,4}.+[0-9]{1,4}.+[0-9]{1,4}$/', $value['date']);
00054 } else {
00055 $dateValid = isset($value['day']) && isset($value['month']) && isset($value['year'])
00056 && $value['day'] > 0 && $value['month'] > 0 && $value['year'] > 0;
00057 }
00058 }
00059
00060 $timeValid = true;
00061 if ($this->_timeExists()) {
00062 $timeValid = isset($value['hour']) && isset($value['minute'])
00063 && is_numeric($value['hour']) && is_numeric($value['minute']);
00064 }
00065
00066 $isValid = $dateValid && $timeValid;
00067
00068 if ($isValid) {
00069 $this->setUserValue(
00070 array(
00071 'date' => isset($value['date']) ? $value['date'] : '',
00072 'year' => isset($value['year']) ? intval($value['year']) : 0,
00073 'month' => isset($value['month']) ? intval($value['month']) : 0,
00074 'day' => isset($value['day']) ? intval($value['day']) : 0,
00075 'hour' => isset($value['hour']) ? intval($value['hour']) : 0,
00076 'minute' => isset($value['minute']) ? intval($value['minute']) : 0,
00077 'day_part' => isset($value['day_part']) ? $value['day_part'] : '',
00078 'date_internal' => isset($value['date_internal']) ? $value['date_internal'] : '',
00079 )
00080 );
00081 } elseif (!$isValid && $option->getIsRequire() && !$this->getProduct()->getSkipCheckRequiredOption()) {
00082 $this->setIsValid(false);
00083 if (!$dateValid) {
00084 Mage::throwException(Mage::helper('catalog')->__('Please specify date required option(s)'));
00085 } elseif (!$timeValid) {
00086 Mage::throwException(Mage::helper('catalog')->__('Please specify time required option(s)'));
00087 } else {
00088 Mage::throwException(Mage::helper('catalog')->__('Please specify the product required option(s)'));
00089 }
00090 } else {
00091 $this->setUserValue(null);
00092 return $this;
00093 }
00094
00095 return $this;
00096 }
00097
00098
00099
00100
00101
00102
00103
00104 public function prepareForCart()
00105 {
00106 if ($this->getIsValid() && $this->getUserValue() !== null) {
00107 $option = $this->getOption();
00108 $value = $this->getUserValue();
00109
00110 if (isset($value['date_internal']) && $value['date_internal'] != '') {
00111 $this->_setInternalInRequest($value['date_internal']);
00112 return $value['date_internal'];
00113 }
00114
00115 $timestamp = 0;
00116
00117 if ($this->_dateExists()) {
00118 if ($this->useCalendar()) {
00119 $format = Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
00120 $timestamp += Mage::app()->getLocale()->date($value['date'], $format, null, false)->getTimestamp();
00121 } else {
00122 $timestamp += mktime(0, 0, 0, $value['month'], $value['day'], $value['year']);
00123 }
00124 } else {
00125 $timestamp += mktime(0, 0, 0, date('m'), date('d'), date('Y'));
00126 }
00127
00128 if ($this->_timeExists()) {
00129 $hour = $this->is24hTimeFormat() ? $value['hour'] : $value['hour'] + 12;
00130 $timestamp += 60 * 60 * $hour + 60 * $value['minute'];
00131 }
00132
00133 $date = new Zend_Date($timestamp);
00134 $result = $date->toString(Varien_date::DATETIME_INTERNAL_FORMAT);
00135
00136
00137 $this->_setInternalInRequest($result);
00138
00139 return $result;
00140 } else {
00141 return null;
00142 }
00143 }
00144
00145
00146
00147
00148
00149
00150
00151 public function getFormattedOptionValue($optionValue)
00152 {
00153 if ($this->_formattedOptionValue === null) {
00154
00155 $option = $this->getOption();
00156 if ($this->getOption()->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_DATE) {
00157 $format = Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM);
00158 $result = Mage::app()->getLocale()->date($optionValue, Zend_Date::ISO_8601, null, false)->toString($format);
00159 } elseif ($this->getOption()->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_DATE_TIME) {
00160 $format = Mage::app()->getLocale()->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
00161 $result = Mage::app()->getLocale()->date($optionValue, Varien_Date::DATETIME_INTERNAL_FORMAT, null, false)->toString($format);
00162 } elseif ($this->getOption()->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_TIME) {
00163 $date = new Zend_Date($optionValue);
00164 $result = date($this->is24hTimeFormat() ? 'H:i' : 'h:i a', $date->getTimestamp());
00165 } else {
00166 $result = $optionValue;
00167 }
00168 $this->_formattedOptionValue = $result;
00169 }
00170 return $this->_formattedOptionValue;
00171 }
00172
00173
00174
00175
00176
00177
00178
00179 public function getPrintableOptionValue($optionValue)
00180 {
00181 return $this->getFormattedOptionValue($optionValue);
00182 }
00183
00184
00185
00186
00187
00188
00189
00190 public function getEditableOptionValue($optionValue)
00191 {
00192 return $this->getFormattedOptionValue($optionValue);
00193 }
00194
00195
00196
00197
00198
00199
00200
00201
00202 public function parseOptionValue($optionValue, $productOptionValues)
00203 {
00204 $timestamp = strtotime($optionValue);
00205 if ($timestamp === false || $timestamp == -1) {
00206 return null;
00207 }
00208
00209 $date = new Zend_Date($timestamp);
00210 return $date->toString(Varien_date::DATETIME_INTERNAL_FORMAT);
00211 }
00212
00213
00214
00215
00216
00217
00218
00219 public function prepareOptionValueForRequest($optionValue)
00220 {
00221
00222 $quoteItem = $this->getQuoteItem();
00223 $infoBuyRequest = $quoteItem->getOptionByCode('info_buyRequest');
00224 try {
00225 $value = unserialize($infoBuyRequest->getValue());
00226 if (is_array($value) && isset($value['options']) && isset($value['options'][$this->getOption()->getId()])) {
00227 return $value['options'][$this->getOption()->getId()];
00228 } else {
00229 return array('date_internal' => $optionValue);
00230 }
00231 } catch (Exception $e) {
00232 return array('date_internal' => $optionValue);
00233 }
00234 }
00235
00236
00237
00238
00239
00240
00241 public function useCalendar()
00242 {
00243 return (bool)$this->getConfigData('use_calendar');
00244 }
00245
00246
00247
00248
00249
00250
00251 public function is24hTimeFormat()
00252 {
00253 return (bool)($this->getConfigData('time_format') == '24h');
00254 }
00255
00256
00257
00258
00259
00260
00261 public function getYearStart()
00262 {
00263 $_range = explode(',', $this->getConfigData('year_range'));
00264 if (isset($_range[0]) && !empty($_range[0])) {
00265 return $_range[0];
00266 } else {
00267 return date('Y');
00268 }
00269 }
00270
00271
00272
00273
00274
00275
00276 public function getYearEnd()
00277 {
00278 $_range = explode(',', $this->getConfigData('year_range'));
00279 if (isset($_range[1]) && !empty($_range[1])) {
00280 return $_range[1];
00281 } else {
00282 return date('Y');
00283 }
00284 }
00285
00286
00287
00288
00289
00290
00291
00292 protected function _setInternalInRequest($internalValue)
00293 {
00294 $requestOptions = $this->getRequest()->getOptions();
00295 if (!isset($requestOptions[$this->getOption()->getId()])) {
00296 $requestOptions[$this->getOption()->getId()] = array();
00297 }
00298 $requestOptions[$this->getOption()->getId()]['date_internal'] = $internalValue;
00299 $this->getRequest()->setOptions($requestOptions);
00300 }
00301
00302
00303
00304
00305
00306
00307 protected function _dateExists()
00308 {
00309 return in_array($this->getOption()->getType(), array(
00310 Mage_Catalog_Model_Product_Option::OPTION_TYPE_DATE,
00311 Mage_Catalog_Model_Product_Option::OPTION_TYPE_DATE_TIME
00312 ));
00313 }
00314
00315
00316
00317
00318
00319
00320 protected function _timeExists()
00321 {
00322 return in_array($this->getOption()->getType(), array(
00323 Mage_Catalog_Model_Product_Option::OPTION_TYPE_DATE_TIME,
00324 Mage_Catalog_Model_Product_Option::OPTION_TYPE_TIME
00325 ));
00326 }
00327 }