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_Page_Block_Html_Head extends Mage_Core_Block_Template
36: {
37: 38: 39: 40:
41: protected function _construct()
42: {
43: $this->setTemplate('page/html/head.phtml');
44: }
45:
46: 47: 48: 49: 50: 51: 52:
53: public function addCss($name, $params = "")
54: {
55: $this->addItem('skin_css', $name, $params);
56: return $this;
57: }
58:
59: 60: 61: 62: 63: 64: 65:
66: public function addJs($name, $params = "")
67: {
68: $this->addItem('js', $name, $params);
69: return $this;
70: }
71:
72: 73: 74: 75: 76: 77: 78:
79: public function addCssIe($name, $params = "")
80: {
81: $this->addItem('skin_css', $name, $params, 'IE');
82: return $this;
83: }
84:
85: 86: 87: 88: 89: 90: 91:
92: public function addJsIe($name, $params = "")
93: {
94: $this->addItem('js', $name, $params, 'IE');
95: return $this;
96: }
97:
98: 99: 100: 101: 102: 103: 104:
105: public function addLinkRel($rel, $href)
106: {
107: $this->addItem('link_rel', $href, 'rel="' . $rel . '"');
108: return $this;
109: }
110:
111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127:
128: public function addItem($type, $name, $params=null, $if=null, $cond=null)
129: {
130: if ($type==='skin_css' && empty($params)) {
131: $params = 'media="all"';
132: }
133: $this->_data['items'][$type.'/'.$name] = array(
134: 'type' => $type,
135: 'name' => $name,
136: 'params' => $params,
137: 'if' => $if,
138: 'cond' => $cond,
139: );
140: return $this;
141: }
142:
143: 144: 145: 146: 147: 148: 149:
150: public function removeItem($type, $name)
151: {
152: unset($this->_data['items'][$type.'/'.$name]);
153: return $this;
154: }
155:
156: 157: 158: 159: 160: 161:
162: public function getCssJsHtml()
163: {
164:
165: $lines = array();
166: foreach ($this->_data['items'] as $item) {
167: if (!is_null($item['cond']) && !$this->getData($item['cond']) || !isset($item['name'])) {
168: continue;
169: }
170: $if = !empty($item['if']) ? $item['if'] : '';
171: $params = !empty($item['params']) ? $item['params'] : '';
172: switch ($item['type']) {
173: case 'js':
174: case 'skin_js':
175: case 'js_css':
176: case 'skin_css':
177: $lines[$if][$item['type']][$params][$item['name']] = $item['name'];
178: break;
179: default:
180: $this->_separateOtherHtmlHeadElements($lines, $if, $item['type'], $params, $item['name'], $item);
181: break;
182: }
183: }
184:
185:
186: $shouldMergeJs = Mage::getStoreConfigFlag('dev/js/merge_files');
187: $shouldMergeCss = Mage::getStoreConfigFlag('dev/css/merge_css_files');
188: $html = '';
189: foreach ($lines as $if => $items) {
190: if (empty($items)) {
191: continue;
192: }
193: if (!empty($if)) {
194: $html .= '<!--[if '.$if.']>'."\n";
195: }
196:
197:
198: $html .= $this->_prepareStaticAndSkinElements('<link rel="stylesheet" type="text/css" href="%s"%s />' . "\n",
199: empty($items['js_css']) ? array() : $items['js_css'],
200: empty($items['skin_css']) ? array() : $items['skin_css'],
201: $shouldMergeCss ? array(Mage::getDesign(), 'getMergedCssUrl') : null
202: );
203:
204:
205: $html .= $this->_prepareStaticAndSkinElements('<script type="text/javascript" src="%s"%s></script>' . "\n",
206: empty($items['js']) ? array() : $items['js'],
207: empty($items['skin_js']) ? array() : $items['skin_js'],
208: $shouldMergeJs ? array(Mage::getDesign(), 'getMergedJsUrl') : null
209: );
210:
211:
212: if (!empty($items['other'])) {
213: $html .= $this->_prepareOtherHtmlHeadElements($items['other']) . "\n";
214: }
215:
216: if (!empty($if)) {
217: $html .= '<![endif]-->'."\n";
218: }
219: }
220: return $html;
221: }
222:
223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235:
236: protected function &_prepareStaticAndSkinElements($format, array $staticItems, array $skinItems, $mergeCallback = null)
237: {
238: $designPackage = Mage::getDesign();
239: $baseJsUrl = Mage::getBaseUrl('js');
240: $items = array();
241: if ($mergeCallback && !is_callable($mergeCallback)) {
242: $mergeCallback = null;
243: }
244:
245:
246: foreach ($staticItems as $params => $rows) {
247: foreach ($rows as $name) {
248: $items[$params][] = $mergeCallback ? Mage::getBaseDir() . DS . 'js' . DS . $name : $baseJsUrl . $name;
249: }
250: }
251:
252:
253: foreach ($skinItems as $params => $rows) {
254: foreach ($rows as $name) {
255: $items[$params][] = $mergeCallback ? $designPackage->getFilename($name, array('_type' => 'skin'))
256: : $designPackage->getSkinUrl($name, array());
257: }
258: }
259:
260: $html = '';
261: foreach ($items as $params => $rows) {
262:
263: $mergedUrl = false;
264: if ($mergeCallback) {
265: $mergedUrl = call_user_func($mergeCallback, $rows);
266: }
267:
268: $params = trim($params);
269: $params = $params ? ' ' . $params : '';
270: if ($mergedUrl) {
271: $html .= sprintf($format, $mergedUrl, $params);
272: } else {
273: foreach ($rows as $src) {
274: $html .= sprintf($format, $src, $params);
275: }
276: }
277: }
278: return $html;
279: }
280:
281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291:
292: protected function _separateOtherHtmlHeadElements(&$lines, $itemIf, $itemType, $itemParams, $itemName, $itemThe)
293: {
294: $params = $itemParams ? ' ' . $itemParams : '';
295: $href = $itemName;
296: switch ($itemType) {
297: case 'rss':
298: $lines[$itemIf]['other'][] = sprintf('<link href="%s"%s rel="alternate" type="application/rss+xml" />',
299: $href, $params
300: );
301: break;
302: case 'link_rel':
303: $lines[$itemIf]['other'][] = sprintf('<link%s href="%s" />', $params, $href);
304: break;
305: }
306: }
307:
308: 309: 310: 311: 312: 313: 314:
315: protected function _prepareOtherHtmlHeadElements($items)
316: {
317: return implode("\n", $items);
318: }
319:
320: 321: 322: 323: 324: 325: 326: 327:
328: public function getChunkedItems($items, $prefix = '', $maxLen = 450)
329: {
330: $chunks = array();
331: $chunk = $prefix;
332: foreach ($items as $item) {
333: if (strlen($chunk.','.$item)>$maxLen) {
334: $chunks[] = $chunk;
335: $chunk = $prefix;
336: }
337: $chunk .= ','.$item;
338: }
339: $chunks[] = $chunk;
340: return $chunks;
341: }
342:
343: 344: 345: 346: 347:
348: public function getContentType()
349: {
350: if (empty($this->_data['content_type'])) {
351: $this->_data['content_type'] = $this->getMediaType().'; charset='.$this->getCharset();
352: }
353: return $this->_data['content_type'];
354: }
355:
356: 357: 358: 359: 360:
361: public function getMediaType()
362: {
363: if (empty($this->_data['media_type'])) {
364: $this->_data['media_type'] = Mage::getStoreConfig('design/head/default_media_type');
365: }
366: return $this->_data['media_type'];
367: }
368:
369: 370: 371: 372: 373:
374: public function getCharset()
375: {
376: if (empty($this->_data['charset'])) {
377: $this->_data['charset'] = Mage::getStoreConfig('design/head/default_charset');
378: }
379: return $this->_data['charset'];
380: }
381:
382: 383: 384: 385: 386: 387:
388: public function setTitle($title)
389: {
390: $this->_data['title'] = Mage::getStoreConfig('design/head/title_prefix') . ' ' . $title
391: . ' ' . Mage::getStoreConfig('design/head/title_suffix');
392: return $this;
393: }
394:
395: 396: 397: 398: 399:
400: public function getTitle()
401: {
402: if (empty($this->_data['title'])) {
403: $this->_data['title'] = $this->getDefaultTitle();
404: }
405: return htmlspecialchars(html_entity_decode(trim($this->_data['title']), ENT_QUOTES, 'UTF-8'));
406: }
407:
408: 409: 410: 411: 412:
413: public function getDefaultTitle()
414: {
415: return Mage::getStoreConfig('design/head/default_title');
416: }
417:
418: 419: 420: 421: 422:
423: public function getDescription()
424: {
425: if (empty($this->_data['description'])) {
426: $this->_data['description'] = Mage::getStoreConfig('design/head/default_description');
427: }
428: return $this->_data['description'];
429: }
430:
431: 432: 433: 434: 435:
436: public function getKeywords()
437: {
438: if (empty($this->_data['keywords'])) {
439: $this->_data['keywords'] = Mage::getStoreConfig('design/head/default_keywords');
440: }
441: return $this->_data['keywords'];
442: }
443:
444: 445: 446: 447: 448:
449: public function getRobots()
450: {
451: if (empty($this->_data['robots'])) {
452: $this->_data['robots'] = Mage::getStoreConfig('design/head/default_robots');
453: }
454: return $this->_data['robots'];
455: }
456:
457: 458: 459: 460: 461:
462: public function getIncludes()
463: {
464: if (empty($this->_data['includes'])) {
465: $this->_data['includes'] = Mage::getStoreConfig('design/head/includes');
466: }
467: return $this->_data['includes'];
468: }
469:
470: 471: 472: 473: 474:
475: public function getFaviconFile()
476: {
477: if (empty($this->_data['favicon_file'])) {
478: $this->_data['favicon_file'] = $this->_getFaviconFile();
479: }
480: return $this->_data['favicon_file'];
481: }
482:
483: 484: 485: 486: 487:
488: protected function _getFaviconFile()
489: {
490: $folderName = Mage_Adminhtml_Model_System_Config_Backend_Image_Favicon::UPLOAD_DIR;
491: $storeConfig = Mage::getStoreConfig('design/head/shortcut_icon');
492: $faviconFile = Mage::getBaseUrl('media') . $folderName . '/' . $storeConfig;
493: $absolutePath = Mage::getBaseDir('media') . '/' . $folderName . '/' . $storeConfig;
494:
495: if(!is_null($storeConfig) && $this->_isFile($absolutePath)) {
496: $url = $faviconFile;
497: } else {
498: $url = $this->getSkinUrl('favicon.ico');
499: }
500: return $url;
501: }
502:
503: 504: 505: 506: 507: 508:
509: protected function _isFile($filename) {
510: if (Mage::helper('core/file_storage_database')->checkDbUsage() && !is_file($filename)) {
511: Mage::helper('core/file_storage_database')->saveFileToFilesystem($filename);
512: }
513: return is_file($filename);
514: }
515: }
516: