Quick Search:

View

Revision:

Diff

Diff from 1497 to:

Annotations

Annotate by Age | Author | Mixed | None
/fisheye/browse/osCommerce/branches/hpdl/oscommerce/includes/classes/template.php

Annotated File View

hpdl
125
1 <?php
2 /*
hpdl
156
3   $Id: template.php 1497 2007-03-29 13:40:05Z hpdl $
hpdl
125
4
5   osCommerce, Open Source E-Commerce Solutions
6   http://www.oscommerce.com
7
8   Copyright (c) 2005 osCommerce
9
hpdl
1497
10   This program is free software; you can redistribute it and/or modify
11   it under the terms of the GNU General Public License v2 (1991)
12   as published by the Free Software Foundation.
hpdl
125
13 */
14
15 /**
16  * The osC_Template class defines or adds elements to the page output such as the page title, page content, and javascript blocks
17  */
18
19   class osC_Template {
20
21 /**
22  * Holds the template name value
23  *
24  * @var string
25  * @access private
26  */
27
hpdl
351
28     var $_template;
hpdl
125
29
30 /**
hpdl
351
31  * Holds the template ID value
32  *
33  * @var int
34  * @access private
35  */
36
37     var $_template_id;
38
39 /**
hpdl
154
40  * Holds the title of the page module
41  *
42  * @var string
43  * @access private
44  */
45
46     var $_module;
47
48 /**
hpdl
192
49  * Holds the group name of the page
50  *
51  * @var string
52  * @access private
53  */
54
55     var $_group;
56
57 /**
hpdl
125
58  * Holds the title of the page
59  *
60  * @var string
61  * @access private
62  */
63
64     var $_page_title;
65
66 /**
hpdl
208
67  * Holds the image of the page
68  *
69  * @var string
70  * @access private
71  */
72
73     var $_page_image;
74
75 /**
hpdl
125
76  * Holds the filename of the content to be added to the page
77  *
78  * @var string
79  * @access private
80  */
81
hpdl
157
82     var $_page_contents;
hpdl
125
83
84 /**
hpdl
248
85  * Holds the meta tags of the page
86  *
87  * @var array
88  * @access private
89  */
90
91     var $_page_tags = array('generator' => array('osCommerce, Open Source E-Commerce Solutions'));
92
93 /**
hpdl
125
94  * Holds javascript filenames to be included in the page
95  *
96  * The javascript files must be plain javascript files without any PHP logic, and are linked to from the page
97  *
98  * @var array
99  * @access private
100  */
101
102     var $_javascript_filenames = array('includes/general.js');
103
104 /**
105  * Holds javascript PHP filenames to be included in the page
106  *
107  * The javascript PHP filenames can consist of PHP logic to produce valid javascript syntax, and is embedded in the page
108  *
109  * @var array
110  * @access private
111  */
112
113     var $_javascript_php_filenames = array();
114
115 /**
116  * Holds blocks of javascript syntax to embedd into the page
117  *
118  * Each block must contain its relevant <script> and </script> tags
119  *
120  * @var array
121  * @access private
122  */
123
124     var $_javascript_blocks = array();
125
126 /**
hpdl
593
127  * Defines if the requested page has a header
128  *
129  * @var boolean
130  * @access private
131  */
132
133     var $_has_header = true;
134
135 /**
136  * Defines if the requested page has a footer
137  *
138  * @var boolean
139  * @access private
140  */
141
142     var $_has_footer = true;
143
144 /**
145  * Defines if the requested page has box modules
146  *
147  * @var boolean
148  * @access private
149  */
150
151     var $_has_box_modules = true;
152
153 /**
154  * Defines if the requested page has content modules
155  *
156  * @var boolean
157  * @access private
158  */
159
160     var $_has_content_modules = true;
161
162 /**
163  * Defines if the requested page should display any debug messages
164  *
165  * @var boolean
166  * @access private
167  */
168
169     var $_show_debug_messages = true;
170
171 /**
hpdl
322
172  * Setup the template class with the requested page module
173  *
174  * @param string $module The default page module to setup
175  * @return object
176  */
177
178     function &setup($module) {
hpdl
783
179       $group = basename($_SERVER['SCRIPT_FILENAME']);
hpdl
322
180
181       if (($pos = strrpos($group, '.')) !== false) {
182         $group = substr($group, 0, $pos);
183       }
184
185       if (empty($_GET) === false) {
186         $first_array = array_slice($_GET, 0, 1);
hpdl
725
187         $_module = osc_sanitize_string(basename(key($first_array)));
hpdl
322
188
189         if (file_exists('includes/content/' . $group . '/' . $_module . '.php')) {
190           $module = $_module;
191         }
192       }
193
194       include('includes/content/' . $group . '/' . $module . '.php');
195
196       $_page_module_name = 'osC_' . ucfirst($group) . '_' . ucfirst($module);
197       $object = new $_page_module_name();
198
hpdl
802
199       require('includes/classes/actions.php');
200       osC_Actions::parse();
201
hpdl
322
202       return $object;
203     }
204
205 /**
hpdl
351
206  * Returns the template ID
207  *
208  * @access public
209  * @return int
210  */
211
212     function getID() {
213       if (isset($this->_template) === false) {
hpdl
353
214         $this->set();
hpdl
351
215       }
216
217       return $this->_template_id;
218     }
219
220 /**
hpdl
125
221  * Returns the template name
222  *
223  * @access public
224  * @return string
225  */
226
hpdl
629
227     function getCode($id = null) {
hpdl
351
228       if (isset($this->_template) === false) {
hpdl
353
229         $this->set();
hpdl
351
230       }
231
hpdl
629
232       if (is_numeric($id)) {
233         foreach ($this->getTemplates() as $template) {
234           if ($template['id'] == $id) {
235             return $template['code'];
236           }
237         }
238       } else {
239         return $this->_template;
240       }
hpdl
125
241     }
242
243 /**
hpdl
154
244  * Returns the page module name
245  *
246  * @access public
247  * @return string
248  */
249
250     function getModule() {
251       return $this->_module;
252     }
253
254 /**
hpdl
192
255  * Returns the page group name
256  *
257  * @access public
258  * @return string
259  */
260
261     function getGroup() {
262       return $this->_group;
263     }
264
265 /**
hpdl
125
266  * Returns the title of the page
267  *
268  * @access public
269  * @return string
270  */
271
272     function getPageTitle() {
273       return $this->_page_title;
274     }
275
276 /**
hpdl
248
277  * Returns the tags of the page separated by a comma
278  *
279  * @access public
280  * @return string
281  */
282
283     function getPageTags() {
284       $tag_string = '';
285
286       foreach ($this->_page_tags as $key => $values) {
287         $tag_string .= '<meta name="' . $key . '" content="' . implode(', ', $values) . '" />' . "\n";
288       }
289
290       return $tag_string . "\n";
291     }
292
hpdl
324
293 /**
hpdl
333
294  * Return the box modules assigned to the page
hpdl
324
295  *
hpdl
333
296  * @param string $group The group name of box modules to include that the template has provided
hpdl
324
297  * @return array
298  */
299
hpdl
333
300     function getBoxModules($group) {
301       if (isset($this->osC_Modules_Boxes) === false) {
302         $this->osC_Modules_Boxes = new osC_Modules('boxes');
hpdl
323
303       }
304
hpdl
333
305       return $this->osC_Modules_Boxes->getGroup($group);
hpdl
323
306     }
307
hpdl
248
308 /**
hpdl
333
309  * Return the content modules assigned to the page
hpdl
331
310  *
hpdl
333
311  * @param string $group The group name of content modules to include that the template has provided
hpdl
331
312  * @return array
313  */
314
hpdl
333
315     function getContentModules($group) {
316       if (isset($this->osC_Modules_Content) === false) {
317         $this->osC_Modules_Content = new osC_Modules('content');
hpdl
331
318       }
319
hpdl
333
320       return $this->osC_Modules_Content->getGroup($group);
hpdl
331
321     }
322
323 /**
hpdl
208
324  * Returns the image of the page
325  *
326  * @access public
327  * @return string
328  */
329
330     function getPageImage() {
331       return $this->_page_image;
332     }
333
334 /**
hpdl
125
335  * Returns the content filename of the page
336  *
337  * @access public
338  * @return string
339  */
340
341     function getPageContentsFilename() {
hpdl
157
342       return $this->_page_contents;
hpdl
125
343     }
344
345 /**
346  * Returns the javascript to link from or embedd to on the page
347  *
348  * @access public
349  * @return string
350  */
351
352     function getJavascript() {
353       if (!empty($this->_javascript_filenames)) {
354         echo $this->_getJavascriptFilenames();
355       }
356
357       if (!empty($this->_javascript_php_filenames)) {
358         $this->_getJavascriptPhpFilenames();
359       }
360
361       if (!empty($this->_javascript_blocks)) {
362         echo $this->_getJavascriptBlocks();
363       }
364     }
365
366 /**
hpdl
356
367  * Return all templates in an array
368  *
369  * @access public
370  * @return array
371  */
372
373     function &getTemplates() {
374       global $osC_Database;
375
376       $templates = array();
377
378       $Qtemplates = $osC_Database->query('select id, code, title from :table_templates');
379       $Qtemplates->bindTable(':table_templates', TABLE_TEMPLATES);
380       $Qtemplates->setCache('templates');
381       $Qtemplates->execute();
382
383       while ($Qtemplates->next()) {
hpdl
629
384         $templates[] = $Qtemplates->toArray();
hpdl
356
385       }
386
387       $Qtemplates->freeResult();
388
389       return $templates;
390     }
391
392 /**
hpdl
125
393  * Checks to see if the page has a title set
394  *
395  * @access public
396  * @return boolean
397  */
398
399     function hasPageTitle() {
400       return !empty($this->_page_title);
401     }
402
403 /**
hpdl
248
404  * Checks to see if the page has a meta tag set
405  *
406  * @access public
407  * @return boolean
408  */
409
410     function hasPageTags() {
411       return !empty($this->_page_tags);
412     }
413
414 /**
hpdl
125
415  * Checks to see if the page has javascript to link to or embedd from
416  *
417  * @access public
418  * @return boolean
419  */
420
421     function hasJavascript() {
422       return (!empty($this->_javascript_filenames) || !empty($this->_javascript_php_filenames) || !empty($this->_javascript_blocks));
423     }
424
425 /**
hpdl
593
426  * Checks to see if the page has a footer defined
427  *
428  * @access public
429  * @return boolean
430  */
431
432     function hasPageFooter() {
433       return $this->_has_footer;
434     }
435
436 /**
437  * Checks to see if the page has a header defined
438  *
439  * @access public
440  * @return boolean
441  */
442
443     function hasPageHeader() {
444       return $this->_has_header;
445     }
446
447 /**
448  * Checks to see if the page has content modules defined
449  *
450  * @access public
451  * @return boolean
452  */
453
454     function hasPageContentModules() {
455       return $this->_has_content_modules;
456     }
457
458 /**
459  * Checks to see if the page has box modules defined
460  *
461  * @access public
462  * @return boolean
463  */
464
465     function hasPageBoxModules() {
466       return $this->_has_box_modules;
467     }
468
469 /**
470  * Checks to see if the page show display debug messages
471  *
472  * @access public
473  * @return boolean
474  */
475
476     function showDebugMessages() {
477       return $this->_show_debug_messages;
478     }
479
480 /**
hpdl
351
481  * Sets the template to use
hpdl
125
482  *
hpdl
1102
483  * @param string $code The code of the template to use
hpdl
125
484  * @access public
485  */
486
hpdl
1102
487     function set($code = null) {
488       if ( (isset($_SESSION['template']) === false) || !empty($code) || (isset($_GET['template']) && !empty($_GET['template'])) ) {
489         if ( !empty( $code ) ) {
490           $set_template = $code;
491         } else {
492           $set_template = (isset($_GET['template']) && !empty($_GET['template'])) ? $_GET['template'] : DEFAULT_TEMPLATE;
493         }
hpdl
351
494
495         $data = array();
496         $data_default = array();
497
hpdl
356
498         foreach ($this->getTemplates() as $template) {
499           if ($template['code'] == DEFAULT_TEMPLATE) {
500             $data_default = array('id' => $template['id'], 'code' => $template['code']);
501           } elseif ($template['code'] == $set_template) {
502             $data = array('id' => $template['id'], 'code' => $template['code']);
hpdl
351
503           }
504         }
505
506         if (empty($data)) {
507           $data =& $data_default;
508         }
509
510         $_SESSION['template'] =& $data;
511       }
512
513       $this->_template_id =& $_SESSION['template']['id'];
514       $this->_template =& $_SESSION['template']['code'];
hpdl
125
515     }
516
517 /**
518  * Sets the title of the page
519  *
520  * @param string $title The title of the page to set to
521  * @access public
522  */
523
524     function setPageTitle($title) {
525       $this->_page_title = $title;
526     }
527
528 /**
hpdl
208
529  * Sets the image of the page
530  *
531  * @param string $image The image of the page to set to
532  * @access public
533  */
534
535     function setPageImage($image) {
536       $this->_page_image = $image;
537     }
538
539 /**
hpdl
125
540  * Sets the content of the page
541  *
542  * @param string $filename The content filename to include on the page
543  * @access public
544  */
545
546     function setPageContentsFilename($filename) {
hpdl
1034
547       $this->_page_contents = $filename;
hpdl
125
548     }
549
550 /**
hpdl
248
551  * Adds a tag to the meta keywords array
552  *
553  * @param string $key The keyword for the meta tag
554  * @param string $value The value for the meta tag using the key
555  * @access public
556  */
557
558     function addPageTags($key, $value) {
559       $this->_page_tags[$key][] = $value;
560     }
561
562 /**
hpdl
125
563  * Adds a javascript file to link to
564  *
565  * @param string $filename The javascript filename to link to
566  * @access public
567  */
568
569     function addJavascriptFilename($filename) {
570       $this->_javascript_filenames[] = $filename;
571     }
572
573 /**
574  * Adds a PHP based javascript file to embedd on the page
575  *
576  * @param string $filename The PHP based javascript filename to embedd
577  * @access public
578  */
579
580     function addJavascriptPhpFilename($filename) {
581       $this->_javascript_php_filenames[] = $filename;
582     }
583
584 /**
585  * Adds javascript logic to the page
586  *
587  * @param string $javascript The javascript block to add on the page
588  * @access public
589  */
590
591     function addJavascriptBlock($javascript) {
592       $this->_javascript_blocks[] = $javascript;
593     }
594
595 /**
596  * Returns the javascript filenames to link to on the page
597  *
598  * @access private
599  * @return string
600  */
601
602     function _getJavascriptFilenames() {
603       $js_files = '';
604
605       foreach ($this->_javascript_filenames as $filenames) {
606         $js_files .= '<script language="javascript" type="text/javascript" src="' . $filenames . '"></script>' . "\n";
607       }
608
609       return $js_files;
610     }
611
612 /**
613  * Returns the PHP javascript files to embedd on the page
614  *
615  * @access private
616  */
617
618     function _getJavascriptPhpFilenames() {
619       foreach ($this->_javascript_php_filenames as $filenames) {
620         include($filenames);
621       }
622     }
623
624 /**
625  * Returns javascript blocks to add to the page
626  *
627  * @access private
628  * @return string
629  */
630
631     function _getJavascriptBlocks() {
632       return implode("\n", $this->_javascript_blocks);
633     }
634   }
635 ?>