4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5 * Copyright (C) 2002-2006 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-2006 The Nucleus Group
18 * @version $Id: TEMPLATE.php,v 1.6 2006-07-20 08:01:52 kimitake Exp $
19 * @version $NucleusJP: TEMPLATE.php,v 1.5 2006/07/12 07:11:47 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="'.addslashes($name).'"';
43 $res = sql_query($query);
44 $obj = mysql_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='" . addslashes($name) . "',"
54 . " tddesc='" . addslashes($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='". addslashes($type) ."' and tdesc=" . intval($id));
70 sql_query('INSERT INTO '.sql_table('template')." SET tcontent='" . addslashes($content) . "', tpartname='" . addslashes($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 ('" . addslashes($name) . "','" . addslashes($desc) . "')");
99 $newId = mysql_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) {
122 $query = 'SELECT tpartname, tcontent'
123 . ' FROM '.sql_table('template_desc').', '.sql_table('template')
124 . ' WHERE tdesc=tdnumber and tdname="' . addslashes($name) . '"';
125 $res = sql_query($query);
126 while ($obj = mysql_fetch_object($res))
127 $template[$obj->tpartname] = $obj->tcontent;
129 // set locale according to template:
130 if ($template['LOCALE'])
131 setlocale(LC_TIME,$template['LOCALE']);
133 setlocale(LC_TIME,'');
139 * fills a template with values
143 * Template to be used
145 * Array of all the values
147 function fill($template, $values) {
149 if (sizeof($values) != 0) {
150 // go through all the values
151 for(reset($values); $key = key($values); next($values)) {
152 $template = str_replace("<%$key%>",$values[$key],$template);
156 // remove non matched template-tags
157 return preg_replace('/<%[a-zA-Z]+%>/','',$template);
160 // returns true if there is a template with the given shortname
162 function exists($name) {
163 $r = sql_query('select * FROM '.sql_table('template_desc').' WHERE tdname="'.addslashes($name).'"');
164 return (mysql_num_rows($r) != 0);
167 // returns true if there is a template with the given ID
169 function existsID($id) {
170 $r = sql_query('select * FROM '.sql_table('template_desc').' WHERE tdnumber='.intval($id));
171 return (mysql_num_rows($r) != 0);
175 function getNameFromId($id) {
176 return quickQuery('SELECT tdname as result FROM '.sql_table('template_desc').' WHERE tdnumber=' . intval($id));
180 function getDesc($id) {
181 $query = 'SELECT tddesc FROM '.sql_table('template_desc').' WHERE tdnumber='. intval($id);
182 $res = sql_query($query);
183 $obj = mysql_fetch_object($res);