4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
\r
5 * Copyright (C) 2002-2004 The Nucleus Group
\r
7 * This program is free software; you can redistribute it and/or
\r
8 * modify it under the terms of the GNU General Public License
\r
9 * as published by the Free Software Foundation; either version 2
\r
10 * of the License, or (at your option) any later version.
\r
11 * (see nucleus/documentation/index.html#license for more info)
\r
13 * A class representing a template
\r
19 function TEMPLATE($templateid) {
\r
20 $this->id = intval($templateid);
\r
24 return intval($this->id);
\r
28 function createFromName($name) {
\r
29 return new TEMPLATE(TEMPLATE::getIdFromName($name));
\r
33 function getIdFromName($name) {
\r
34 $query = 'SELECT tdnumber'
\r
35 . ' FROM '.sql_table('template_desc')
\r
36 . ' WHERE tdname="'.addslashes($name).'"';
\r
37 $res = sql_query($query);
\r
38 $obj = mysql_fetch_object($res);
\r
39 return $obj->tdnumber;
\r
43 * Updates the general information about the template
\r
45 function updateGeneralInfo($name, $desc) {
\r
46 $query = 'UPDATE '.sql_table('template_desc').' SET'
\r
47 . " tdname='" . addslashes($name) . "',"
\r
48 . " tddesc='" . addslashes($desc) . "'"
\r
49 . " WHERE tdnumber=" . $this->getID();
\r
54 * Updates the contents of one part of the template
\r
56 function update($type, $content) {
\r
57 $id = $this->getID();
\r
59 // delete old thingie
\r
60 sql_query('DELETE FROM '.sql_table('template')." WHERE tpartname='". addslashes($type) ."' and tdesc=" . intval($id));
\r
62 // write new thingie
\r
64 sql_query('INSERT INTO '.sql_table('template')." SET tcontent='" . addslashes($content) . "', tpartname='" . addslashes($type) . "', tdesc=" . intval($id));
\r
70 * Deletes all template parts from the database
\r
72 function deleteAllParts() {
\r
73 sql_query('DELETE FROM '.sql_table('template').' WHERE tdesc='.$this->getID());
\r
77 * Creates a new template
\r
81 function createNew($name, $desc) {
\r
88 'description' => &$desc
\r
92 sql_query('INSERT INTO '.sql_table('template_desc')." (tdname, tddesc) VALUES ('" . addslashes($name) . "','" . addslashes($desc) . "')");
\r
93 $newId = mysql_insert_id();
\r
98 'templateid' => $newId,
\r
100 'description' => $desc
\r
110 * Reads a template and returns an array with the parts.
\r
113 * @param $name name of the template file
\r
115 function read($name) {
\r
116 $query = 'SELECT tpartname, tcontent'
\r
117 . ' FROM '.sql_table('template_desc').', '.sql_table('template')
\r
118 . ' WHERE tdesc=tdnumber and tdname="' . addslashes($name) . '"';
\r
119 $res = sql_query($query);
\r
120 while ($obj = mysql_fetch_object($res))
\r
121 $template[$obj->tpartname] = $obj->tcontent;
\r
123 // set locale according to template:
\r
124 if ($template['LOCALE'])
\r
125 setlocale(LC_TIME,$template['LOCALE']);
\r
127 setlocale(LC_TIME,'');
\r
133 * fills a template with values
\r
136 * @param $template
\r
137 * Template to be used
\r
139 * Array of all the values
\r
141 function fill($template, $values) {
\r
143 if (sizeof($values) != 0) {
\r
144 // go through all the values
\r
145 for(reset($values); $key = key($values); next($values)) {
\r
146 $template = str_replace("<%$key%>",$values[$key],$template);
\r
150 // remove non matched template-tags
\r
151 return preg_replace('/<%[a-zA-Z]+%>/','',$template);
\r
154 // returns true if there is a template with the given shortname
\r
156 function exists($name) {
\r
157 $r = sql_query('select * FROM '.sql_table('template_desc').' WHERE tdname="'.addslashes($name).'"');
\r
158 return (mysql_num_rows($r) != 0);
\r
161 // returns true if there is a template with the given ID
\r
163 function existsID($id) {
\r
164 $r = sql_query('select * FROM '.sql_table('template_desc').' WHERE tdnumber='.intval($id));
\r
165 return (mysql_num_rows($r) != 0);
\r
169 function getNameFromId($id) {
\r
170 return quickQuery('SELECT tdname as result FROM '.sql_table('template_desc').' WHERE tdnumber=' . intval($id));
\r
174 function getDesc($id) {
\r
175 $query = 'SELECT tddesc FROM '.sql_table('template_desc').' WHERE tdnumber='. intval($id);
\r
176 $obj = mysql_fetch_object(sql_query($query));
\r
177 return $obj->tddesc;
\r