Quick Search:

View

Revision:

Diff

Diff from 1674 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 1674 2007-08-20 22:56:00Z hpdl $
hpdl
125
4
5   osCommerce, Open Source E-Commerce Solutions
6   http://www.oscommerce.com
7
hpdl
1674
8   Copyright (c) 2007 osCommerce
hpdl
125
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
1674
199       if ( isset($_GET['action']) && !empty($_GET['action']) ) {
200         include('includes/classes/actions.php');
hpdl
802
201
hpdl
1674
202         osC_Actions::parse($_GET['action']);
203       }
204
hpdl
322
205       return $object;
206     }
207
208 /**
hpdl
351
209  * Returns the template ID
210  *
211  * @access public
212  * @return int
213  */
214
215     function getID() {
216       if (isset($this->_template) === false) {
hpdl
353
217         $this->set();
hpdl
351
218       }
219
220       return $this->_template_id;
221     }
222
223 /**
hpdl
125
224  * Returns the template name
225  *
226  * @access public
227  * @return string
228  */
229
hpdl
629
230     function getCode($id = null) {
hpdl
351
231       if (isset($this->_template) === false) {
hpdl
353
232         $this->set();
hpdl
351
233       }
234
hpdl
629
235       if (is_numeric($id)) {
236         foreach ($this->getTemplates() as $template) {
237           if ($template['id'] == $id) {
238             return $template['code'];
239           }
240         }
241       } else {
242         return $this->_template;
243       }
hpdl
125
244     }
245
246 /**
hpdl
154
247  * Returns the page module name
248  *
249  * @access public
250  * @return string
251  */
252
253     function getModule() {
254       return $this->_module;
255     }
256
257 /**
hpdl
192
258  * Returns the page group name
259  *
260  * @access public
261  * @return string
262  */
263
264     function getGroup() {
265       return $this->_group;
266     }
267
268 /**
hpdl
125
269  * Returns the title of the page
270  *
271  * @access public
272  * @return string
273  */
274
275     function getPageTitle() {
276       return $this->_page_title;
277     }
278
279 /**
hpdl
248
280  * Returns the tags of the page separated by a comma
281  *
282  * @access public
283  * @return string
284  */
285
286     function getPageTags() {
287       $tag_string = '';
288
289       foreach ($this->_page_tags as $key => $values) {
290         $tag_string .= '<meta name="' . $key . '" content="' . implode(', ', $values) . '" />' . "\n";
291       }
292
293       return $tag_string . "\n";
294     }
295
hpdl
324
296 /**
hpdl
333
297  * Return the box modules assigned to the page
hpdl
324
298  *
hpdl
333
299  * @param string $group The group name of box modules to include that the template has provided
hpdl
324
300  * @return array
301  */
302
hpdl
333
303     function getBoxModules($group) {
304       if (isset($this->osC_Modules_Boxes) === false) {
305         $this->osC_Modules_Boxes = new osC_Modules('boxes');
hpdl
323
306       }
307
hpdl
333
308       return $this->osC_Modules_Boxes->getGroup($group);
hpdl
323
309     }
310
hpdl
248
311 /**
hpdl
333
312  * Return the content modules assigned to the page
hpdl
331
313  *
hpdl
333
314  * @param string $group The group name of content modules to include that the template has provided
hpdl
331
315  * @return array
316  */
317
hpdl
333
318     function getContentModules($group) {
319       if (isset($this->osC_Modules_Content) === false) {
320         $this->osC_Modules_Content = new osC_Modules('content');
hpdl
331
321       }
322
hpdl
333
323       return $this->osC_Modules_Content->getGroup($group);
hpdl
331
324     }
325
326 /**
hpdl
208
327  * Returns the image of the page
328  *
329  * @access public
330  * @return string
331  */
332
333     function getPageImage() {
334       return $this->_page_image;
335     }
336
337 /**
hpdl
125
338  * Returns the content filename of the page
339  *
340  * @access public
341  * @return string
342  */
343
344     function getPageContentsFilename() {
hpdl
157
345       return $this->_page_contents;
hpdl
125
346     }
347
348 /**
349  * Returns the javascript to link from or embedd to on the page
350  *
351  * @access public
352  * @return string
353  */
354
355     function getJavascript() {
356       if (!empty($this->_javascript_filenames)) {
357         echo $this->_getJavascriptFilenames();
358       }
359
360       if (!empty($this->_javascript_php_filenames)) {
361         $this->_getJavascriptPhpFilenames();
362       }
363
364       if (!empty($this->_javascript_blocks)) {
365         echo $this->_getJavascriptBlocks();
366       }
367     }
368
369 /**
hpdl
356
370  * Return all templates in an array
371  *
372  * @access public
373  * @return array
374  */
375
376     function &getTemplates() {
377       global $osC_Database;
378
379       $templates = array();
380
381       $Qtemplates = $osC_Database->query('select id, code, title from :table_templates');
382       $Qtemplates->bindTable(':table_templates', TABLE_TEMPLATES);
383       $Qtemplates->setCache('templates');
384       $Qtemplates->execute();
385
386       while ($Qtemplates->next()) {
hpdl
629
387         $templates[] = $Qtemplates->toArray();
hpdl
356
388       }
389
390       $Qtemplates->freeResult();
391
392       return $templates;
393     }
394
395 /**
hpdl
125
396  * Checks to see if the page has a title set
397  *
398  * @access public
399  * @return boolean
400  */
401
402     function hasPageTitle() {
403       return !empty($this->_page_title);
404     }
405
406 /**
hpdl
248
407  * Checks to see if the page has a meta tag set
408  *
409  * @access public
410  * @return boolean
411  */
412
413     function hasPageTags() {
414       return !empty($this->_page_tags);
415     }
416
417 /**
hpdl
125
418  * Checks to see if the page has javascript to link to or embedd from
419  *
420  * @access public
421  * @return boolean
422  */
423
424     function hasJavascript() {
425       return (!empty($this->_javascript_filenames) || !empty($this->_javascript_php_filenames) || !empty($this->_javascript_blocks));
426     }
427
428 /**
hpdl
593
429  * Checks to see if the page has a footer defined
430  *
431  * @access public
432  * @return boolean
433  */
434
435     function hasPageFooter() {
436       return $this->_has_footer;
437     }
438
439 /**
440  * Checks to see if the page has a header defined
441  *
442  * @access public
443  * @return boolean
444  */
445
446     function hasPageHeader() {
447       return $this->_has_header;
448     }
449
450 /**
451  * Checks to see if the page has content modules defined
452  *
453  * @access public
454  * @return boolean
455  */
456
457     function hasPageContentModules() {
458       return $this->_has_content_modules;
459     }
460
461 /**
462  * Checks to see if the page has box modules defined
463  *
464  * @access public
465  * @return boolean
466  */
467
468     function hasPageBoxModules() {
469       return $this->_has_box_modules;
470     }
471
472 /**
473  * Checks to see if the page show display debug messages
474  *
475  * @access public
476  * @return boolean
477  */
478
479     function showDebugMessages() {
480       return $this->_show_debug_messages;
481     }
482
483 /**
hpdl
351
484  * Sets the template to use
hpdl
125
485  *
hpdl
1102
486  * @param string $code The code of the template to use
hpdl
125
487  * @access public
488  */
489
hpdl
1102
490     function set($code = null) {
491       if ( (isset($_SESSION['template']) === false) || !empty($code) || (isset($_GET['template']) && !empty($_GET['template'])) ) {
492         if ( !empty( $code ) ) {
493           $set_template = $code;
494         } else {
495           $set_template = (isset($_GET['template']) && !empty($_GET['template'])) ? $_GET['template'] : DEFAULT_TEMPLATE;
496         }
hpdl
351
497
498         $data = array();
499         $data_default = array();
500
hpdl
356
501         foreach ($this->getTemplates() as $template) {
502           if ($template['code'] == DEFAULT_TEMPLATE) {
503             $data_default = array('id' => $template['id'], 'code' => $template['code']);
504           } elseif ($template['code'] == $set_template) {
505             $data = array('id' => $template['id'], 'code' => $template['code']);
hpdl
351
506           }
507         }
508
509         if (empty($data)) {
510           $data =& $data_default;
511         }
512
513         $_SESSION['template'] =& $data;
514       }
515
516       $this->_template_id =& $_SESSION['template']['id'];
517       $this->_template =& $_SESSION['template']['code'];
hpdl
125
518     }
519
520 /**
521  * Sets the title of the page
522  *
523  * @param string $title The title of the page to set to
524  * @access public
525  */
526
527     function setPageTitle($title) {
528       $this->_page_title = $title;
529     }
530
531 /**
hpdl
208
532  * Sets the image of the page
533  *
534  * @param string $image The image of the page to set to
535  * @access public
536  */
537
538     function setPageImage($image) {
539       $this->_page_image = $image;
540     }
541
542 /**
hpdl
125
543  * Sets the content of the page
544  *
545  * @param string $filename The content filename to include on the page
546  * @access public
547  */
548
549     function setPageContentsFilename($filename) {
hpdl
1034
550       $this->_page_contents = $filename;
hpdl
125
551     }
552
553 /**
hpdl
248
554  * Adds a tag to the meta keywords array
555  *
556  * @param string $key The keyword for the meta tag
557  * @param string $value The value for the meta tag using the key
558  * @access public
559  */
560
561     function addPageTags($key, $value) {
562       $this->_page_tags[$key][] = $value;
563     }
564
565 /**
hpdl
125
566  * Adds a javascript file to link to
567  *
568  * @param string $filename The javascript filename to link to
569  * @access public
570  */
571
572     function addJavascriptFilename($filename) {
573       $this->_javascript_filenames[] = $filename;
574     }
575
576 /**
577  * Adds a PHP based javascript file to embedd on the page
578  *
579  * @param string $filename The PHP based javascript filename to embedd
580  * @access public
581  */
582
583     function addJavascriptPhpFilename($filename) {
584       $this->_javascript_php_filenames[] = $filename;
585     }
586
587 /**
588  * Adds javascript logic to the page
589  *
590  * @param string $javascript The javascript block to add on the page
591  * @access public
592  */
593
594     function addJavascriptBlock($javascript) {
595       $this->_javascript_blocks[] = $javascript;
596     }
597
598 /**
599  * Returns the javascript filenames to link to on the page
600  *
601  * @access private
602  * @return string
603  */
604
605     function _getJavascriptFilenames() {
606       $js_files = '';
607
608       foreach ($this->_javascript_filenames as $filenames) {
609         $js_files .= '<script language="javascript" type="text/javascript" src="' . $filenames . '"></script>' . "\n";
610       }
611
612       return $js_files;
613     }
614
615 /**
616  * Returns the PHP javascript files to embedd on the page
617  *
618  * @access private
619  */
620
621     function _getJavascriptPhpFilenames() {
622       foreach ($this->_javascript_php_filenames as $filenames) {
623         include($filenames);
624       }
625     }
626
627 /**
628  * Returns javascript blocks to add to the page
629  *
630  * @access private
631  * @return string
632  */
633
634     function _getJavascriptBlocks() {
635       return implode("\n", $this->_javascript_blocks);
636     }
637   }
638 ?>