4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5 * Copyright (C) 2002-2011 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-2011 The Nucleus Group
19 * @version $NucleusJP: TEMPLATE.php,v 1.6 2006/07/20 08:01:52 kimitake Exp $
25 function TEMPLATE($templateid) {
26 $this->id = intval($templateid);
30 return intval($this->id);
34 function createFromName($name) {
35 return new TEMPLATE(TEMPLATE::getIdFromName($name));
39 function getIdFromName($name) {
40 $query = 'SELECT tdnumber'
41 . ' FROM '.sql_table('template_desc')
42 . ' WHERE tdname="'.sql_real_escape_string($name).'"';
43 $res = sql_query($query);
44 $obj = sql_fetch_object($res);
45 return $obj->tdnumber;
49 * Updates the general information about the template
51 function updateGeneralInfo($name, $desc) {
52 $query = 'UPDATE '.sql_table('template_desc').' SET'
53 . " tdname='" . sql_real_escape_string($name) . "',"
54 . " tddesc='" . sql_real_escape_string($desc) . "'"
55 . " WHERE tdnumber=" . $this->getID();
60 * Updates the contents of one part of the template
62 function update($type, $content) {
66 sql_query('DELETE FROM '.sql_table('template')." WHERE tpartname='". sql_real_escape_string($type) ."' and tdesc=" . intval($id));
70 sql_query('INSERT INTO '.sql_table('template')." SET tcontent='" . sql_real_escape_string($content) . "', tpartname='" . sql_real_escape_string($type) . "', tdesc=" . intval($id));
76 * Deletes all template parts from the database
78 function deleteAllParts() {
79 sql_query('DELETE FROM '.sql_table('template').' WHERE tdesc='.$this->getID());
83 * Creates a new template
87 function createNew($name, $desc) {
94 'description' => &$desc
98 sql_query('INSERT INTO '.sql_table('template_desc')." (tdname, tddesc) VALUES ('" . sql_real_escape_string($name) . "','" . sql_real_escape_string($desc) . "')");
99 $newId = sql_insert_id();
104 'templateid' => $newId,
106 'description' => $desc
116 * Reads a template and returns an array with the parts.
119 * @param $name name of the template file
121 function read($name) {
130 $query = 'SELECT tpartname, tcontent'
131 . ' FROM '.sql_table('template_desc').', '.sql_table('template')
132 . ' WHERE tdesc=tdnumber and tdname="' . sql_real_escape_string($name) . '"';
133 $res = sql_query($query);
134 while ($obj = sql_fetch_object($res))
135 $template[$obj->tpartname] = $obj->tcontent;
137 // set locale according to template:
138 if (isset($template['LOCALE']))
139 setlocale(LC_TIME,$template['LOCALE']);
141 setlocale(LC_TIME,'');
147 * fills a template with values
151 * Template to be used
153 * Array of all the values
155 function fill($template, $values) {
157 if (sizeof($values) != 0) {
158 // go through all the values
159 for(reset($values); $key = key($values); next($values)) {
160 $template = str_replace("<%$key%>",$values[$key],$template);
164 // remove non matched template-tags
165 return preg_replace('/<%[a-zA-Z]+%>/','',$template);
168 // returns true if there is a template with the given shortname
170 function exists($name) {
171 $r = sql_query('select * FROM '.sql_table('template_desc').' WHERE tdname="'.sql_real_escape_string($name).'"');
172 return (sql_num_rows($r) != 0);
175 // returns true if there is a template with the given ID
177 function existsID($id) {
178 $r = sql_query('select * FROM '.sql_table('template_desc').' WHERE tdnumber='.intval($id));
179 return (sql_num_rows($r) != 0);
183 function getNameFromId($id) {
184 return quickQuery('SELECT tdname as result FROM '.sql_table('template_desc').' WHERE tdnumber=' . intval($id));
188 function getDesc($id) {
189 $query = 'SELECT tddesc FROM '.sql_table('template_desc').' WHERE tdnumber='. intval($id);
190 $res = sql_query($query);
191 $obj = sql_fetch_object($res);