1: <?php
2:
3: if (!defined('XOOPS_ROOT_PATH')) exit();
4:
5: class Legacy_HeaderScript
6: {
7: protected $_mType = 'google';
8: protected $_mCore = "1";
9: protected $_mUi = "1";
10:
11: protected $_mLibrary = array();
12: protected $_mScript = array();
13: protected $_mMeta = array('keywords'=>'','description'=>'','robots'=>'','rating'=>'','author'=>'','copyright'=>'',);
14: protected $_mOnloadScript = array();
15: protected $_mStylesheet = array();
16: protected $_mLink = array();
17:
18: public $mUsePrototype = false;
19: public $mFuncNamePrefix = "";
20:
21: 22: 23: 24: 25: 26: 27:
28: public function __construct()
29: {
30: $root = XCube_Root::getSingleton();
31:
32:
33: $this->_mCore = $this->_getRenderConfig('jquery_core');
34: $this->_mUi = $this->_getRenderConfig('jquery_ui');
35: $core = str_replace('.', '', $this->_mCore);
36: $this->_mType = is_numeric($core) ? 'google' : 'local';
37:
38:
39: if($root->getSiteConfig('jQuery', 'usePrototype')==1){
40: $this->mUsePrototype = true;
41: $this->mPrototypeUrl = $root->getSiteConfig('jQuery', 'prototypeUrl');
42: $this->mFuncNamePrefix = $root->getSiteConfig('jQuery', 'funcNamePrefix');
43: }
44:
45: $this->_setupDefaultStylesheet();
46: }
47:
48: 49: 50: 51: 52: 53: 54:
55: public function _setupDefaultStylesheet()
56: {
57: if($this->_getRenderConfig('css_file')) $this->addStylesheet($this->_getRenderConfig('css_file'), false);
58: }
59:
60: 61: 62: 63: 64: 65: 66: 67:
68: public function addLibrary($url, $xoopsUrl=true)
69: {
70: $libUrl = ($xoopsUrl==true) ? XOOPS_URL. $url : $url;
71: if(! in_array($libUrl, $this->_mLibrary)){
72: $this->_mLibrary[] = $libUrl;
73: }
74: }
75:
76: 77: 78: 79: 80: 81: 82: 83:
84: public function addStylesheet($url, $xoopsUrl=true)
85: {
86: $libUrl = ($xoopsUrl==true) ? XOOPS_URL. $url : $url;
87: if(! in_array($libUrl, $this->_mStylesheet)){
88: $this->_mStylesheet[] = $libUrl;
89: }
90: }
91:
92: 93: 94: 95: 96: 97: 98: 99:
100: public function addScript($script, $isOnloadFunction=true)
101: {
102: if($isOnloadFunction==true){
103: $this->_mOnloadScript[] = $script;
104: }
105: else{
106: $this->_mScript[] = $script;
107: }
108: }
109:
110: 111: 112: 113: 114: 115: 116:
117: public function getLibraryArr()
118: {
119: return $this->_mLibrary;
120: }
121:
122: 123: 124: 125: 126: 127: 128:
129: public function getScriptArr($isOnloadFunction=true)
130: {
131: if($isOnloadFunction==true){
132: return $this->_mOnloadScript;
133: }
134: else{
135: return $this->_mScript;
136: }
137: }
138:
139: 140: 141: 142: 143: 144: 145: 146: 147: 148:
149: public function addLink( $rel, $href, $type, $title=null)
150: {
151: $this->_mLink[] = array('rel'=>$rel, 'type'=>$type, 'title'=>$title, 'href'=>$href);
152: }
153:
154: 155: 156: 157: 158: 159: 160: 161:
162: public function addMeta( $name, $content)
163: {
164: $this->_mMeta[$name] = $content;
165: }
166:
167: 168: 169: 170: 171: 172: 173:
174: public function getMeta( $name)
175: {
176: return $this->_mMeta[$name];
177: }
178:
179: 180: 181: 182: 183: 184: 185:
186: public function createLibraryTag()
187: {
188: $html = "";
189:
190:
191: if($this->mUsePrototype){
192: $html .= '<script type="text/javascript" src="'. $this->mPrototypeUrl .'"></script>';
193: }
194:
195:
196: if($this->_mType=='google'){
197: $html .= $this->_loadGoogleJQueryLibrary();
198: }
199: elseif($this->_mType=='local'){
200: $html .= $this->_loadLocalJQueryLibrary();
201: }
202:
203:
204: foreach($this->_mLibrary as $lib){
205: $html .= "<script type=\"text/javascript\" src=\"". $lib ."\"></script>\n";
206: }
207:
208:
209: foreach($this->_mStylesheet as $css){
210: $html .= "<link type=\"text/css\" rel=\"stylesheet\" href=\"". $css ."\" />\n";
211: }
212:
213:
214: foreach($this->_mLink as $link){
215: $title = $link['title'] ? 'title="'.$link['title'].'" ' : null;
216: $html .= sprintf("<link type=\"%s\" rel=\"%s\" href=\"%s\" $title/>\n", $link['type'], $link['rel'], $link['href']);
217: }
218:
219:
220: if($this->_getRenderConfig('feed_url')){
221: $html .= sprintf('<link rel="alternate" type="application/rss+xml" title="rss" href="%s" />'."\n", $this->_getRenderConfig('feed_url'));
222: }
223: return $html;
224: }
225:
226: 227: 228: 229: 230: 231: 232:
233: protected function _loadGoogleJQueryLibrary()
234: {
235: $apiKey = XCube_Root::getSingleton()->getSiteConfig('jQuery', 'GoogleApiKey');
236: $apiKey = (isset($apiKey)) ? '?key='.$apiKey : null;
237: return '<script type="text/javascript" src="http://www.google.com/jsapi'.$apiKey.'"></script>
238: <script type="text/javascript"><!--
239: google.load("language", "1");
240: google.load("jquery", "'. $this->_mCore .'");
241: google.load("jqueryui", "'. $this->_mUi .'");
242: //-->
243: </script>
244: ';
245: }
246:
247: 248: 249: 250: 251: 252: 253:
254: protected function _loadLocalJQueryLibrary()
255: {
256: $html = "";
257: if($this->_mCore) $html .= '<script type="text/javascript" src="'. $this->_mCore .'"></script>';
258: if($this->_mUi) $html .= '<script type="text/javascript" src="'. $this->_mUi .'"></script>';
259:
260: return $html;
261: }
262:
263: 264: 265: 266: 267: 268: 269:
270: public function createOnloadFunctionTag()
271: {
272: $html = null;
273: if(count($this->_mOnloadScript)>0||count($this->_mScript)>0){
274: $html = "<script type=\"text/javascript\"><!--\n";
275: if($this->_mType == "google"){
276: $html .= "google.setOnLoadCallback(function() {\n";
277: }
278: if($this->mUsePrototype == true){
279: $html .= "jQuery.noConflict();\n";
280: }
281: $html .= "jQuery(function($){\n";
282: $html .= $this->_makeScript(true);
283: if($this->_mType == "google"){
284: $html .= "\n});\n";
285: }
286: $html .= "\n});\n";
287: $html .= $this->_makeScript(false);
288: $html .= "//--></script>"."\n";
289: }
290: return $html;
291: }
292:
293: 294: 295: 296: 297: 298: 299:
300: protected function _makeScript($isOnloadFunction=true)
301: {
302: $html = null;
303: $scriptArr = ($isOnloadFunction===true) ? $this->_mOnloadScript : $this->_mScript;
304: foreach($scriptArr as $script){
305: $html .= $this->_convertFuncName($script);
306: }
307: return $html;
308: }
309:
310: 311: 312: 313: 314: 315: 316:
317: protected function _convertFuncName($script)
318: {
319: if($this->mFuncNamePrefix){
320: $script = str_replace("$(", $this->mFuncNamePrefix."$(", $script);
321: }
322: return $script;
323: }
324:
325: 326: 327: 328: 329: 330: 331:
332: protected function _getRenderConfig($key)
333: {
334: $handler =& xoops_gethandler('config');
335: $configArr =& $handler->getConfigsByDirname('legacyRender');
336: return $configArr[$key];
337: }
338:
339: }
340: ?>
341: