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_Page_Block_Html_Head extends Mage_Core_Block_Template
00035 {
00036 protected function _construct()
00037 {
00038 $this->setTemplate('page/html/head.phtml');
00039 }
00040
00041 public function addCss($name, $params="")
00042 {
00043 $this->addItem('skin_css', $name, $params);
00044 return $this;
00045 }
00046
00047 public function addJs($name, $params="")
00048 {
00049 $this->addItem('js', $name, $params);
00050 return $this;
00051 }
00052
00053 public function addCssIe($name, $params="")
00054 {
00055 $this->addItem('skin_css', $name, $params, 'IE');
00056 return $this;
00057 }
00058
00059 public function addJsIe($name, $params="")
00060 {
00061 $this->addItem('js', $name, $params, 'IE');
00062 return $this;
00063 }
00064
00065 public function addItem($type, $name, $params=null, $if=null, $cond=null)
00066 {
00067 if ($type==='skin_css' && empty($params)) {
00068 $params = 'media="all"';
00069 }
00070 $this->_data['items'][$type.'/'.$name] = array(
00071 'type' => $type,
00072 'name' => $name,
00073 'params' => $params,
00074 'if' => $if,
00075 'cond' => $cond,
00076 );
00077 return $this;
00078 }
00079
00080 public function removeItem($type, $name)
00081 {
00082 unset($this->_data['items'][$type.'/'.$name]);
00083 return $this;
00084 }
00085
00086 public function getCssJsHtml()
00087 {
00088
00089 $lines = array();
00090 $baseJs = Mage::getBaseUrl('js');
00091 $html = '';
00092
00093 $script = '<script type="text/javascript" src="%s" %s></script>';
00094 $stylesheet = '<link rel="stylesheet" type="text/css" href="%s" %s />';
00095 $alternate = '<link rel="alternate" type="%s" href="%s" %s />';
00096
00097 foreach ($this->_data['items'] as $item) {
00098 if (!is_null($item['cond']) && !$this->getData($item['cond'])) {
00099 continue;
00100 }
00101 $if = !empty($item['if']) ? $item['if'] : '';
00102 switch ($item['type']) {
00103 case 'js':
00104 #$lines[$if]['other'][] = sprintf($script, $baseJs.$item['name'], $item['params']);
00105 $lines[$if]['script'][] = $item['name'];
00106 break;
00107
00108 case 'js_css':
00109
00110 $lines[$if]['other'][] = sprintf($stylesheet, $baseJs.$item['name'], $item['params']);
00111 #$lines[$if]['stylesheet'][] = $item['name'];
00112 break;
00113
00114 case 'skin_js':
00115 $lines[$if]['other'][] = sprintf($script, $this->getSkinUrl($item['name']), $item['params']);
00116 break;
00117
00118 case 'skin_css':
00119 $lines[$if]['other'][] = sprintf($stylesheet, $this->getSkinUrl($item['name']), $item['params']);
00120 break;
00121
00122 case 'rss':
00123 $lines[$if]['other'][] = sprintf($alternate, 'application/rss+xml', $item['name'], $item['params']);
00124 break;
00125 }
00126 }
00127
00128 foreach ($lines as $if=>$items) {
00129 if (!empty($if)) {
00130 $html .= '<!--[if '.$if.']>'."\n";
00131 }
00132 if (!empty($items['script'])) {
00133 $scriptItems = array();
00134 if (Mage::getStoreConfigFlag('dev/js/merge_files')) {
00135 $scriptItems = $this->getChunkedItems($items['script'], 'index.php?c=auto&f=');
00136 } else {
00137 $scriptItems = $items['script'];
00138 }
00139 foreach ($scriptItems as $item) {
00140 $html .= sprintf($script, $baseJs.$item, '') . "\n";
00141 }
00142
00143
00144
00145 }
00146 if (!empty($items['stylesheet'])) {
00147 foreach ($this->getChunkedItems($items['stylesheet'], $baseJs.'index.php?c=auto&f=') as $item) {
00148 $html .= sprintf($stylesheet, $item, '')."\n";
00149 }
00150
00151
00152
00153 }
00154 if (!empty($items['other'])) {
00155 $html .= join("\n", $items['other'])."\n";
00156 }
00157 if (!empty($if)) {
00158 $html .= '<![endif]-->'."\n";
00159 }
00160 }
00161
00162 return $html;
00163 }
00164
00165 public function getChunkedItems($items, $prefix='', $maxLen=450)
00166 {
00167 $chunks = array();
00168 $chunk = $prefix;
00169 foreach ($items as $i=>$item) {
00170 if (strlen($chunk.','.$item)>$maxLen) {
00171 $chunks[] = $chunk;
00172 $chunk = $prefix;
00173 }
00174 $chunk .= ','.$item;
00175 }
00176 $chunks[] = $chunk;
00177 return $chunks;
00178 }
00179
00180 public function getContentType()
00181 {
00182 if (empty($this->_data['content_type'])) {
00183 $this->_data['content_type'] = $this->getMediaType().'; charset='.$this->getCharset();
00184 }
00185 return $this->_data['content_type'];
00186 }
00187
00188 public function getMediaType()
00189 {
00190 if (empty($this->_data['media_type'])) {
00191 $this->_data['media_type'] = Mage::getStoreConfig('design/head/default_media_type');
00192 }
00193 return $this->_data['media_type'];
00194 }
00195
00196 public function getCharset()
00197 {
00198 if (empty($this->_data['charset'])) {
00199 $this->_data['charset'] = Mage::getStoreConfig('design/head/default_charset');
00200 }
00201 return $this->_data['charset'];
00202 }
00203
00204 public function setTitle($title)
00205 {
00206 $this->_data['title'] = Mage::getStoreConfig('design/head/title_prefix').' '.$title
00207 .' '.Mage::getStoreConfig('design/head/title_suffix');
00208 return $this;
00209 }
00210
00211 public function getTitle()
00212 {
00213 if (empty($this->_data['title'])) {
00214 $this->_data['title'] = $this->getDefaultTitle();
00215 }
00216 return htmlspecialchars(html_entity_decode($this->_data['title'], ENT_QUOTES, 'UTF-8'));
00217 }
00218
00219 public function getDefaultTitle()
00220 {
00221 return Mage::getStoreConfig('design/head/default_title');
00222 }
00223
00224 public function getDescription()
00225 {
00226 if (empty($this->_data['description'])) {
00227 $this->_data['description'] = Mage::getStoreConfig('design/head/default_description');
00228 }
00229 return $this->_data['description'];
00230 }
00231
00232 public function getKeywords()
00233 {
00234 if (empty($this->_data['keywords'])) {
00235 $this->_data['keywords'] = Mage::getStoreConfig('design/head/default_keywords');
00236 }
00237 return $this->_data['keywords'];
00238 }
00239
00240 public function getRobots()
00241 {
00242 if (empty($this->_data['robots'])) {
00243 $this->_data['robots'] = Mage::getStoreConfig('design/head/default_robots');
00244 }
00245 return $this->_data['robots'];
00246 }
00247
00248
00249
00250
00251
00252
00253 public function getIncludes()
00254 {
00255 if (empty($this->_data['includes'])) {
00256 $this->_data['includes'] = Mage::getStoreConfig('design/head/includes');
00257 }
00258 return $this->_data['includes'];
00259 }
00260
00261 }