Quick Search:

View

Revision:

Diff

Diff from 356 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 356 2005-12-20 04:13:12Z 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
322
125  * Setup the template class with the requested page module
126  *
127  * @param string $module The default page module to setup
128  * @return object
129  */
130
131     function &setup($module) {
132       $group = basename($_SERVER['PHP_SELF']);
133
134       if (($pos = strrpos($group, '.')) !== false) {
135         $group = substr($group, 0, $pos);
136       }
137
138       if (empty($_GET) === false) {
139         $first_array = array_slice($_GET, 0, 1);
140         $_module = tep_sanitize_string(basename(key($first_array)));
141
142         if (file_exists('includes/content/' . $group . '/' . $_module . '.php')) {
143           $module = $_module;
144         }
145       }
146
147       include('includes/content/' . $group . '/' . $module . '.php');
148
149       $_page_module_name = 'osC_' . ucfirst($group) . '_' . ucfirst($module);
150       $object = new $_page_module_name();
151
152       return $object;
153     }
154
155 /**
hpdl
351
156  * Returns the template ID
157  *
158  * @access public
159  * @return int
160  */
161
162     function getID() {
163       if (isset($this->_template) === false) {
hpdl
353
164         $this->set();
hpdl
351
165       }
166
167       return $this->_template_id;
168     }
169
170 /**
hpdl
125
171  * Returns the template name
172  *
173  * @access public
174  * @return string
175  */
176
hpdl
352
177     function getCode() {
hpdl
351
178       if (isset($this->_template) === false) {
hpdl
353
179         $this->set();
hpdl
351
180       }
181
hpdl
125
182       return $this->_template;
183     }
184
185 /**
hpdl
154
186  * Returns the page module name
187  *
188  * @access public
189  * @return string
190  */
191
192     function getModule() {
193       return $this->_module;
194     }
195
196 /**
hpdl
192
197  * Returns the page group name
198  *
199  * @access public
200  * @return string
201  */
202
203     function getGroup() {
204       return $this->_group;
205     }
206
207 /**
hpdl
125
208  * Returns the title of the page
209  *
210  * @access public
211  * @return string
212  */
213
214     function getPageTitle() {
215       return $this->_page_title;
216     }
217
218 /**
hpdl
248
219  * Returns the tags of the page separated by a comma
220  *
221  * @access public
222  * @return string
223  */
224
225     function getPageTags() {
226       $tag_string = '';
227
228       foreach ($this->_page_tags as $key => $values) {
229         $tag_string .= '<meta name="' . $key . '" content="' . implode(', ', $values) . '" />' . "\n";
230       }
231
232       return $tag_string . "\n";
233     }
234
hpdl
324
235 /**
hpdl
333
236  * Return the box modules assigned to the page
hpdl
324
237  *
hpdl
333
238  * @param string $group The group name of box modules to include that the template has provided
hpdl
324
239  * @return array
240  */
241
hpdl
333
242     function getBoxModules($group) {
243       if (isset($this->osC_Modules_Boxes) === false) {
244         $this->osC_Modules_Boxes = new osC_Modules('boxes');
hpdl
323
245       }
246
hpdl
333
247       return $this->osC_Modules_Boxes->getGroup($group);
hpdl
323
248     }
249
hpdl
248
250 /**
hpdl
333
251  * Return the content modules assigned to the page
hpdl
331
252  *
hpdl
333
253  * @param string $group The group name of content modules to include that the template has provided
hpdl
331
254  * @return array
255  */
256
hpdl
333
257     function getContentModules($group) {
258       if (isset($this->osC_Modules_Content) === false) {
259         $this->osC_Modules_Content = new osC_Modules('content');
hpdl
331
260       }
261
hpdl
333
262       return $this->osC_Modules_Content->getGroup($group);
hpdl
331
263     }
264
265 /**
hpdl
208
266  * Returns the image of the page
267  *
268  * @access public
269  * @return string
270  */
271
272     function getPageImage() {
273       return $this->_page_image;
274     }
275
276 /**
hpdl
125
277  * Returns the content filename of the page
278  *
279  * @access public
280  * @return string
281  */
282
283     function getPageContentsFilename() {
hpdl
157
284       return $this->_page_contents;
hpdl
125
285     }
286
287 /**
288  * Returns the javascript to link from or embedd to on the page
289  *
290  * @access public
291  * @return string
292  */
293
294     function getJavascript() {
295       if (!empty($this->_javascript_filenames)) {
296         echo $this->_getJavascriptFilenames();
297       }
298
299       if (!empty($this->_javascript_php_filenames)) {
300         $this->_getJavascriptPhpFilenames();
301       }
302
303       if (!empty($this->_javascript_blocks)) {
304         echo $this->_getJavascriptBlocks();
305       }
306     }
307
308 /**
hpdl
356
309  * Return all templates in an array
310  *
311  * @access public
312  * @return array
313  */
314
315     function &getTemplates() {
316       global $osC_Database;
317
318       $templates = array();
319
320       $Qtemplates = $osC_Database->query('select id, code, title from :table_templates');
321       $Qtemplates->bindTable(':table_templates', TABLE_TEMPLATES);
322       $Qtemplates->setCache('templates');
323       $Qtemplates->execute();
324
325       while ($Qtemplates->next()) {
326         $templates [] = $Qtemplates->toArray();
327       }
328
329       $Qtemplates->freeResult();
330
331       return $templates;
332     }
333
334 /**
hpdl
125
335  * Checks to see if the page has a title set
336  *
337  * @access public
338  * @return boolean
339  */
340
341     function hasPageTitle() {
342       return !empty($this->_page_title);
343     }
344
345 /**
hpdl
248
346  * Checks to see if the page has a meta tag set
347  *
348  * @access public
349  * @return boolean
350  */
351
352     function hasPageTags() {
353       return !empty($this->_page_tags);
354     }
355
356 /**
hpdl
125
357  * Checks to see if the page has javascript to link to or embedd from
358  *
359  * @access public
360  * @return boolean
361  */
362
363     function hasJavascript() {
364       return (!empty($this->_javascript_filenames) || !empty($this->_javascript_php_filenames) || !empty($this->_javascript_blocks));
365     }
366
367 /**
hpdl
351
368  * Sets the template to use
hpdl
125
369  *
370  * @access public
371  */
372
hpdl
353
373     function set() {
hpdl
351
374       global $osC_Database;
375
376       if ( (isset($_SESSION['template']) === false) || (isset($_GET['template']) && !empty($_GET['template'])) ) {
hpdl
356
377         $set_template = (isset($_GET['template']) && !empty($_GET['template'])) ? $_GET['template'] : DEFAULT_TEMPLATE;
hpdl
351
378
379         $data = array();
380         $data_default = array();
381
hpdl
356
382         foreach ($this->getTemplates() as $template) {
383           if ($template['code'] == DEFAULT_TEMPLATE) {
384             $data_default = array('id' => $template['id'], 'code' => $template['code']);
385           } elseif ($template['code'] == $set_template) {
386             $data = array('id' => $template['id'], 'code' => $template['code']);
hpdl
351
387           }
388         }
389
390         if (empty($data)) {
391           $data =& $data_default;
392         }
393
394         $_SESSION['template'] =& $data;
395       }
396
397       $this->_template_id =& $_SESSION['template']['id'];
398       $this->_template =& $_SESSION['template']['code'];
hpdl
125
399     }
400
401 /**
402  * Sets the title of the page
403  *
404  * @param string $title The title of the page to set to
405  * @access public
406  */
407
408     function setPageTitle($title) {
409       $this->_page_title = $title;
410     }
411
412 /**
hpdl
208
413  * Sets the image of the page
414  *
415  * @param string $image The image of the page to set to
416  * @access public
417  */
418
419     function setPageImage($image) {
420       $this->_page_image = $image;
421     }
422
423 /**
hpdl
125
424  * Sets the content of the page
425  *
426  * @param string $filename The content filename to include on the page
427  * @access public
428  */
429
430     function setPageContentsFilename($filename) {
431       $this->_page_contents_filename = $filename;
432     }
433
434 /**
hpdl
248
435  * Adds a tag to the meta keywords array
436  *
437  * @param string $key The keyword for the meta tag
438  * @param string $value The value for the meta tag using the key
439  * @access public
440  */
441
442     function addPageTags($key, $value) {
443       $this->_page_tags[$key][] = $value;
444     }
445
446 /**
hpdl
125
447  * Adds a javascript file to link to
448  *
449  * @param string $filename The javascript filename to link to
450  * @access public
451  */
452
453     function addJavascriptFilename($filename) {
454       $this->_javascript_filenames[] = $filename;
455     }
456
457 /**
458  * Adds a PHP based javascript file to embedd on the page
459  *
460  * @param string $filename The PHP based javascript filename to embedd
461  * @access public
462  */
463
464     function addJavascriptPhpFilename($filename) {
465       $this->_javascript_php_filenames[] = $filename;
466     }
467
468 /**
469  * Adds javascript logic to the page
470  *
471  * @param string $javascript The javascript block to add on the page
472  * @access public
473  */
474
475     function addJavascriptBlock($javascript) {
476       $this->_javascript_blocks[] = $javascript;
477     }
478
479 /**
480  * Returns the javascript filenames to link to on the page
481  *
482  * @access private
483  * @return string
484  */
485
486     function _getJavascriptFilenames() {
487       $js_files = '';
488
489       foreach ($this->_javascript_filenames as $filenames) {
490         $js_files .= '<script language="javascript" type="text/javascript" src="' . $filenames . '"></script>' . "\n";
491       }
492
493       return $js_files;
494     }
495
496 /**
497  * Returns the PHP javascript files to embedd on the page
498  *
499  * @access private
500  */
501
502     function _getJavascriptPhpFilenames() {
503       foreach ($this->_javascript_php_filenames as $filenames) {
504         include($filenames);
505       }
506     }
507
508 /**
509  * Returns javascript blocks to add to the page
510  *
511  * @access private
512  * @return string
513  */
514
515     function _getJavascriptBlocks() {
516       return implode("\n", $this->_javascript_blocks);
517     }
518   }
519 ?>