Quick Search:

View

Revision:

Diff

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