Quick Search:

View

Revision:

Diff

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