4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5 * Copyright (C) 2002-2009 The Nucleus Group
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 * (see nucleus/documentation/index.html#license for more info)
14 * A class representing a template
16 * @license http://nucleuscms.org/license.txt GNU General Public License
17 * @copyright Copyright (C) 2002-2009 The Nucleus Group
18 * @version $Id: TEMPLATE.php 1470 2010-11-29 22:10:16Z ftruscot $
24 function TEMPLATE($templateid) {
25 $this->id = intval($templateid);
29 return intval($this->id);
33 function createFromName($name) {
34 return new TEMPLATE(TEMPLATE::getIdFromName($name));
38 function getIdFromName($name) {
39 $query = 'SELECT tdnumber'
40 . ' FROM '.sql_table('template_desc')
41 . ' WHERE tdname="'.sql_real_escape_string($name).'"';
42 $res = sql_query($query);
43 $obj = sql_fetch_object($res);
44 return $obj->tdnumber;
48 * Updates the general information about the template
50 function updateGeneralInfo($name, $desc) {
51 $query = 'UPDATE '.sql_table('template_desc').' SET'
52 . " tdname='" . sql_real_escape_string($name) . "',"
53 . " tddesc='" . sql_real_escape_string($desc) . "'"
54 . " WHERE tdnumber=" . $this->getID();
59 * Updates the contents of one part of the template
61 function update($type, $content) {
65 sql_query('DELETE FROM '.sql_table('template')." WHERE tpartname='". sql_real_escape_string($type) ."' and tdesc=" . intval($id));
69 sql_query('INSERT INTO '.sql_table('template')." SET tcontent='" . sql_real_escape_string($content) . "', tpartname='" . sql_real_escape_string($type) . "', tdesc=" . intval($id));
75 * Deletes all template parts from the database
77 function deleteAllParts() {
78 sql_query('DELETE FROM '.sql_table('template').' WHERE tdesc='.$this->getID());
82 * Creates a new template
86 function createNew($name, $desc) {
93 'description' => &$desc
97 sql_query('INSERT INTO '.sql_table('template_desc')." (tdname, tddesc) VALUES ('" . sql_real_escape_string($name) . "','" . sql_real_escape_string($desc) . "')");
98 $newId = sql_insert_id();
103 'templateid' => $newId,
105 'description' => $desc
115 * Reads a template and returns an array with the parts.
118 * @param $name name of the template file
120 function read($name) {
129 $query = 'SELECT tpartname, tcontent'
130 . ' FROM '.sql_table('template_desc').', '.sql_table('template')
131 . ' WHERE tdesc=tdnumber and tdname="' . sql_real_escape_string($name) . '"';
132 $res = sql_query($query);
133 while ($obj = sql_fetch_object($res))
134 $template[$obj->tpartname] = $obj->tcontent;
136 // set locale according to template:
137 if (isset($template['LOCALE']))
138 setlocale(LC_TIME,$template['LOCALE']);
140 setlocale(LC_TIME,'');
146 * fills a template with values
150 * Template to be used
152 * Array of all the values
154 function fill($template, $values) {
156 if (sizeof($values) != 0) {
157 // go through all the values
158 for(reset($values); $key = key($values); next($values)) {
159 $template = str_replace("<%$key%>",$values[$key],$template);
163 // remove non matched template-tags
164 return preg_replace('/<%[a-zA-Z]+%>/','',$template);
167 // returns true if there is a template with the given shortname
169 function exists($name) {
170 $r = sql_query('select * FROM '.sql_table('template_desc').' WHERE tdname="'.sql_real_escape_string($name).'"');
171 return (sql_num_rows($r) != 0);
174 // returns true if there is a template with the given ID
176 function existsID($id) {
177 $r = sql_query('select * FROM '.sql_table('template_desc').' WHERE tdnumber='.intval($id));
178 return (sql_num_rows($r) != 0);
182 function getNameFromId($id) {
183 return quickQuery('SELECT tdname as result FROM '.sql_table('template_desc').' WHERE tdnumber=' . intval($id));
187 function getDesc($id) {
188 $query = 'SELECT tddesc FROM '.sql_table('template_desc').' WHERE tdnumber='. intval($id);
189 $res = sql_query($query);
190 $obj = sql_fetch_object($res);