1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Catalog_Model_Design extends Mage_Core_Model_Abstract
36: {
37: const APPLY_FOR_PRODUCT = 1;
38: const APPLY_FOR_CATEGORY = 2;
39:
40: 41: 42: 43:
44: const CATEGORY_APPLY_CATEGORY_AND_PRODUCT_RECURSIVE = 1;
45: const CATEGORY_APPLY_CATEGORY_ONLY = 2;
46: const CATEGORY_APPLY_CATEGORY_AND_PRODUCT_ONLY = 3;
47: const CATEGORY_APPLY_CATEGORY_RECURSIVE = 4;
48:
49: 50: 51: 52: 53: 54: 55: 56: 57:
58: public function applyDesign($object, $calledFrom = 0)
59: {
60: if ($calledFrom != self::APPLY_FOR_CATEGORY && $calledFrom != self::APPLY_FOR_PRODUCT) {
61: return $this;
62: }
63:
64: if (Mage::helper('catalog/category_flat')->isEnabled()) {
65: $this->_applyDesign($object, $calledFrom);
66: } else {
67: $this->_inheritDesign($object, $calledFrom);
68: }
69:
70: return $this;
71: }
72:
73: 74: 75: 76: 77: 78:
79: protected function _apply($package, $theme)
80: {
81: Mage::getSingleton('core/design_package')
82: ->setPackageName($package)
83: ->setTheme($theme);
84: }
85:
86: 87: 88: 89: 90:
91: public function applyCustomDesign($design)
92: {
93: $designInfo = explode('/', $design);
94: if (count($designInfo) != 2) {
95: return false;
96: }
97: $package = $designInfo[0];
98: $theme = $designInfo[1];
99: $this->_apply($package, $theme);
100: }
101:
102: 103: 104: 105: 106: 107: 108: 109: 110: 111:
112: protected function _isApplyFor($applyForObject, $applyTo, $pass = 0)
113: {
114: $hasError = false;
115:
116: if ($pass == 0) {
117: switch ($applyForObject) {
118: case self::APPLY_FOR_CATEGORY:
119: break;
120: case self::APPLY_FOR_PRODUCT:
121: $validApplyTo = array(
122: self::CATEGORY_APPLY_CATEGORY_AND_PRODUCT_RECURSIVE,
123: self::CATEGORY_APPLY_CATEGORY_AND_PRODUCT_ONLY
124: );
125: if ($applyTo && !in_array($applyTo, $validApplyTo)) {
126: $hasError = true;
127: }
128: break;
129: default:
130: $hasError = true;
131: break;
132: }
133: }
134: else {
135: switch ($applyForObject) {
136: case self::APPLY_FOR_CATEGORY:
137: $validApplyTo = array(
138: self::CATEGORY_APPLY_CATEGORY_AND_PRODUCT_RECURSIVE,
139: self::CATEGORY_APPLY_CATEGORY_RECURSIVE
140: );
141: if ($applyTo && !in_array($applyTo, $validApplyTo)) {
142: $hasError = true;
143: }
144: break;
145: case self::APPLY_FOR_PRODUCT:
146: $validApplyTo = array(
147: self::CATEGORY_APPLY_CATEGORY_AND_PRODUCT_RECURSIVE
148: );
149: if ($applyTo && !in_array($applyTo, $validApplyTo)) {
150: $hasError = true;
151: }
152: break;
153: default:
154: $hasError = true;
155: break;
156: }
157: }
158:
159: return !$hasError;
160: }
161:
162: 163: 164: 165: 166: 167: 168: 169:
170: protected function _isApplyDesign($design, array $date)
171: {
172: if (!array_key_exists('from', $date) || !array_key_exists('to', $date)) {
173: return false;
174: }
175:
176: $designInfo = explode("/", $design);
177: if (count($designInfo) != 2) {
178: return false;
179: }
180:
181:
182: $package = $designInfo[0];
183: $theme = $designInfo[1];
184:
185:
186: if (Mage::app()->getLocale()->isStoreDateInInterval(null, $date['from'], $date['to'])) {
187: $this->_apply($package, $theme);
188: return true;
189: }
190:
191: return false;
192: }
193:
194: 195: 196: 197: 198: 199: 200: 201: 202: 203:
204: protected function _inheritDesign($object, $calledFrom = 0)
205: {
206: $useParentSettings = false;
207: if ($object instanceof Mage_Catalog_Model_Product) {
208: $category = $object->getCategory();
209:
210: if ($category && $category->getId()) {
211: return $this->_inheritDesign($category, $calledFrom);
212: }
213: }
214: elseif ($object instanceof Mage_Catalog_Model_Category) {
215: $category = $object->getParentCategory();
216:
217: $useParentSettings = $object->getCustomUseParentSettings();
218: if ($useParentSettings) {
219: if ($category &&
220: $category->getId() &&
221: $category->getLevel() > 1 &&
222: $category->getId() != Mage_Catalog_Model_Category::TREE_ROOT_ID) {
223: return $this->_inheritDesign($category, $calledFrom);
224: }
225: }
226:
227: if ($calledFrom == self::APPLY_FOR_PRODUCT) {
228: $applyToProducts = $object->getCustomApplyToProducts();
229: if (!$applyToProducts) {
230: return $this;
231: }
232: }
233: }
234:
235: if (!$useParentSettings) {
236: $design = $object->getCustomDesign();
237: $date = $object->getCustomDesignDate();
238: $this->_isApplyDesign($design, $date);
239: }
240:
241: return $this;
242: }
243:
244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254:
255: protected function _applyDesignRecursively($object, $calledFrom = 0, $pass = 0)
256: {
257: $design = $object->getCustomDesign();
258: $date = $object->getCustomDesignDate();
259: $applyTo = $object->getCustomDesignApply();
260:
261: $checkAndApply = $this->_isApplyFor($calledFrom, $applyTo, $pass)
262: && $this->_isApplyDesign($design, $date);
263: if ($checkAndApply) {
264: return $this;
265: }
266:
267: $pass ++;
268:
269: $category = null;
270: if ($object instanceof Mage_Catalog_Model_Product) {
271: $category = $object->getCategory();
272: $pass --;
273: }
274: elseif ($object instanceof Mage_Catalog_Model_Category) {
275: $category = $object->getParentCategory();
276: }
277:
278: if ($category && $category->getId()) {
279: $this->_applyDesignRecursively($category, $calledFrom, $pass);
280: }
281:
282: return $this;
283: }
284:
285: 286: 287:
288: protected function _applyDesign($designUpdateData, $calledFrom = 0, $loaded = false, $pass = 0)
289: {
290: $objects = array();
291: if (is_object($designUpdateData)) {
292: $objects = array($designUpdateData);
293: } elseif (is_array($designUpdateData)) {
294: $objects = &$designUpdateData;
295: }
296: foreach ($objects as $object) {
297: $design = $object->getCustomDesign();
298: $date = $object->getCustomDesignDate();
299: $applyTo = $object->getCustomDesignApply();
300:
301: $checkAndApply = $this->_isApplyFor($calledFrom, $applyTo, $pass)
302: && $this->_isApplyDesign($design, $date);
303: if ($checkAndApply) {
304: return $this;
305: }
306: }
307:
308: $pass ++;
309:
310: if (false === $loaded && is_object($designUpdateData)) {
311: $_designUpdateData = array();
312: if ($designUpdateData instanceof Mage_Catalog_Model_Product) {
313: $_category = $designUpdateData->getCategory();
314: $_designUpdateData = array_merge(
315: $_designUpdateData, array($_category)
316: );
317: $pass --;
318: } elseif ($designUpdateData instanceof Mage_Catalog_Model_Category) {
319: $_category = &$designUpdateData;
320: }
321: if ($_category && $_category->getId()) {
322: $_designUpdateData = array_merge(
323: $_designUpdateData,
324: $_category->getResource()->getDesignUpdateData($_category)
325: );
326: $this->_applyDesign($_designUpdateData, $calledFrom, true, $pass);
327: }
328: }
329: return $this;
330: }
331:
332: 333: 334: 335: 336: 337:
338: public function getDesignSettings($object)
339: {
340: if ($object instanceof Mage_Catalog_Model_Product) {
341: $currentCategory = $object->getCategory();
342: } else {
343: $currentCategory = $object;
344: }
345:
346: $category = null;
347: if ($currentCategory) {
348: $category = $currentCategory->getParentDesignCategory($currentCategory);
349: }
350:
351: if ($object instanceof Mage_Catalog_Model_Product) {
352: if ($category && $category->getCustomApplyToProducts()) {
353: return $this->_mergeSettings($this->_extractSettings($category), $this->_extractSettings($object));
354: } else {
355: return $this->_extractSettings($object);
356: }
357: } else {
358: return $this->_extractSettings($category);
359: }
360: }
361:
362: 363: 364: 365: 366: 367:
368: protected function ($object)
369: {
370: $settings = new Varien_Object;
371: if (!$object) {
372: return $settings;
373: }
374: $date = $object->getCustomDesignDate();
375: if (array_key_exists('from', $date) && array_key_exists('to', $date)
376: && Mage::app()->getLocale()->isStoreDateInInterval(null, $date['from'], $date['to'])) {
377: $settings->setCustomDesign($object->getCustomDesign())
378: ->setPageLayout($object->getPageLayout())
379: ->setLayoutUpdates((array)$object->getCustomLayoutUpdate());
380: }
381: return $settings;
382: }
383:
384: 385: 386: 387: 388: 389: 390:
391: protected function _mergeSettings($categorySettings, $productSettings)
392: {
393: if ($productSettings->getCustomDesign()) {
394: $categorySettings->setCustomDesign($productSettings->getCustomDesign());
395: }
396: if ($productSettings->getPageLayout()) {
397: $categorySettings->setPageLayout($productSettings->getPageLayout());
398: }
399: if ($productSettings->getLayoutUpdates()) {
400: $update = array_merge($categorySettings->getLayoutUpdates(), $productSettings->getLayoutUpdates());
401: $categorySettings->setLayoutUpdates($update);
402: }
403: return $categorySettings;
404: }
405: }
406: