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
00035 class Mage_Catalog_Model_Design extends Mage_Core_Model_Abstract
00036 {
00037 const APPLY_FOR_PRODUCT = 1;
00038 const APPLY_FOR_CATEGORY = 2;
00039
00040
00041
00042
00043
00044 const CATEGORY_APPLY_CATEGORY_AND_PRODUCT_RECURSIVE = 1;
00045 const CATEGORY_APPLY_CATEGORY_ONLY = 2;
00046 const CATEGORY_APPLY_CATEGORY_AND_PRODUCT_ONLY = 3;
00047 const CATEGORY_APPLY_CATEGORY_RECURSIVE = 4;
00048
00049
00050
00051
00052
00053
00054
00055
00056 public function applyDesign($object, $calledFrom = 0)
00057 {
00058 if ($calledFrom != self::APPLY_FOR_CATEGORY && $calledFrom != self::APPLY_FOR_PRODUCT) {
00059 return $this;
00060 }
00061
00062 if (Mage::helper('catalog/category_flat')->isEnabled()) {
00063 $this->_applyDesign($object, $calledFrom);
00064 } else {
00065 $this->_applyDesignRecursively($object, $calledFrom);
00066 }
00067
00068 return $this;
00069 }
00070
00071
00072
00073
00074
00075
00076
00077 protected function _apply($package, $theme)
00078 {
00079 Mage::getSingleton('core/design_package')
00080 ->setPackageName($package)
00081 ->setTheme($theme);
00082 }
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 protected function _isApplyFor($applyForObject, $applyTo, $pass = 0)
00093 {
00094 $hasError = false;
00095 if ($pass == 0) {
00096 switch ($applyForObject) {
00097 case self::APPLY_FOR_CATEGORY:
00098 break;
00099 case self::APPLY_FOR_PRODUCT:
00100 $validApplyTo = array(
00101 self::CATEGORY_APPLY_CATEGORY_AND_PRODUCT_RECURSIVE,
00102 self::CATEGORY_APPLY_CATEGORY_AND_PRODUCT_ONLY
00103 );
00104 if ($applyTo && !in_array($applyTo, $validApplyTo)) {
00105 $hasError = true;
00106 }
00107 break;
00108 default:
00109 $hasError = true;
00110 break;
00111 }
00112 }
00113 else {
00114 switch ($applyForObject) {
00115 case self::APPLY_FOR_CATEGORY:
00116 $validApplyTo = array(
00117 self::CATEGORY_APPLY_CATEGORY_AND_PRODUCT_RECURSIVE,
00118 self::CATEGORY_APPLY_CATEGORY_RECURSIVE
00119 );
00120 if ($applyTo && !in_array($applyTo, $validApplyTo)) {
00121 $hasError = true;
00122 }
00123 break;
00124 case self::APPLY_FOR_PRODUCT:
00125 $validApplyTo = array(
00126 self::CATEGORY_APPLY_CATEGORY_AND_PRODUCT_RECURSIVE
00127 );
00128 if ($applyTo && !in_array($applyTo, $validApplyTo)) {
00129 $hasError = true;
00130 }
00131 break;
00132 default:
00133 $hasError = true;
00134 break;
00135 }
00136 }
00137 return !$hasError;
00138 }
00139
00140
00141
00142
00143
00144
00145
00146 protected function _isApplyDesign($design, array $date)
00147 {
00148 if (!array_key_exists('from', $date) || !array_key_exists('to', $date)) {
00149 return false;
00150 }
00151
00152 $designInfo = explode("/", $design);
00153 if (count($designInfo) != 2) {
00154 return false;
00155 }
00156
00157
00158 $package = $designInfo[0];
00159 $theme = $designInfo[1];
00160
00161
00162 if (Mage::app()->getLocale()->IsStoreDateInInterval(null, $date['from'], $date['to'])) {
00163 $this->_apply($package, $theme);
00164 return true;
00165 }
00166
00167 return false;
00168 }
00169
00170
00171
00172
00173
00174
00175
00176
00177 protected function _applyDesignRecursively($object, $calledFrom = 0, $pass = 0)
00178 {
00179 $design = $object->getCustomDesign();
00180 $date = $object->getCustomDesignDate();
00181 $applyTo = $object->getCustomDesignApply();
00182
00183 $checkAndApply = $this->_isApplyFor($calledFrom, $applyTo, $pass)
00184 && $this->_isApplyDesign($design, $date);
00185 if ($checkAndApply) {
00186 return $this;
00187 }
00188
00189 $pass ++;
00190
00191 $category = null;
00192 if ($object instanceof Mage_Catalog_Model_Product) {
00193 $category = $object->getCategory();
00194 $pass --;
00195 }
00196 elseif ($object instanceof Mage_Catalog_Model_Category) {
00197 $category = $object->getParentCategory();
00198 }
00199
00200 if ($category && $category->getId()){
00201 $this->_applyDesignRecursively($category, $calledFrom, $pass);
00202 }
00203
00204 return $this;
00205 }
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215 protected function _applyDesign($designUpdateData, $calledFrom = 0, $loaded = false, $pass = 0)
00216 {
00217 $objects = array();
00218 if (is_object($designUpdateData)) {
00219 $objects = array($designUpdateData);
00220 } elseif (is_array($designUpdateData)) {
00221 $objects = &$designUpdateData;
00222 }
00223 foreach ($objects as $object) {
00224 $design = $object->getCustomDesign();
00225 $date = $object->getCustomDesignDate();
00226 $applyTo = $object->getCustomDesignApply();
00227
00228 $checkAndApply = $this->_isApplyFor($calledFrom, $applyTo, $pass)
00229 && $this->_isApplyDesign($design, $date);
00230 if ($checkAndApply) {
00231 return $this;
00232 }
00233 }
00234
00235 $pass ++;
00236
00237 if (false === $loaded && is_object($designUpdateData)) {
00238 $_designUpdateData = array();
00239 if ($designUpdateData instanceof Mage_Catalog_Model_Product) {
00240 $_category = $designUpdateData->getCategory();
00241 $_designUpdateData = array_merge(
00242 $_designUpdateData, array($_category)
00243 );
00244 $pass --;
00245 } elseif ($designUpdateData instanceof Mage_Catalog_Model_Category) {
00246 $_category = &$designUpdateData;
00247 }
00248 if ($_category && $_category->getId()) {
00249 $_designUpdateData = array_merge(
00250 $_designUpdateData,
00251 $_category->getResource()->getDesignUpdateData($_category)
00252 );
00253 $this->_applyDesign($_designUpdateData, $calledFrom, true, $pass);
00254 }
00255 }
00256 return $this;
00257 }
00258 }