4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5 * Copyright (C) 2002-2012 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)
13 * A class representing a template
19 function TEMPLATE($templateid) {
20 $this->id = intval($templateid);
24 return intval($this->id);
28 function createFromName($name) {
29 return new TEMPLATE(TEMPLATE::getIdFromName($name));
33 function getIdFromName($name) {
34 $query = 'SELECT tdnumber'
35 . ' FROM '.sql_table('template_desc')
36 . ' WHERE tdname="'.sql_real_escape_string($name).'"';
37 $res = sql_query($query);
38 $obj = sql_fetch_object($res);
39 return $obj->tdnumber;
43 * Updates the general information about the template
45 function updateGeneralInfo($name, $desc) {
46 $query = 'UPDATE '.sql_table('template_desc').' SET'
47 . " tdname='" . sql_real_escape_string($name) . "',"
48 . " tddesc='" . sql_real_escape_string($desc) . "'"
49 . " WHERE tdnumber=" . $this->getID();
54 * Updates the contents of one part of the template
56 function update($type, $content) {
60 sql_query('DELETE FROM '.sql_table('template')." WHERE tpartname='". sql_real_escape_string($type) ."' and tdesc=" . intval($id));
64 sql_query('INSERT INTO '.sql_table('template')." SET tcontent='" . sql_real_escape_string($content) . "', tpartname='" . sql_real_escape_string($type) . "', tdesc=" . intval($id));
70 * Deletes all template parts from the database
72 function deleteAllParts() {
73 sql_query('DELETE FROM '.sql_table('template').' WHERE tdesc='.$this->getID());
77 * Creates a new template
81 function createNew($name, $desc) {
86 'description' => &$desc
88 $manager->notify('PreAddTemplate', $param);
90 sql_query('INSERT INTO '.sql_table('template_desc')." (tdname, tddesc) VALUES ('" . sql_real_escape_string($name) . "','" . sql_real_escape_string($desc) . "')");
91 $newId = sql_insert_id();
94 'templateid' => $newId,
96 'description' => $desc
98 $manager->notify('PostAddTemplate', $param);
106 * Reads a template and returns an array with the parts.
109 * @param $name name of the template file
111 function read($name) {
117 $manager->notify('PreTemplateRead', $param);
119 $query = 'SELECT tpartname, tcontent'
120 . ' FROM '.sql_table('template_desc').', '.sql_table('template')
121 . ' WHERE tdesc=tdnumber and tdname="' . sql_real_escape_string($name) . '"';
122 $res = sql_query($query);
123 while ($obj = sql_fetch_object($res))
124 $template[$obj->tpartname] = $obj->tcontent;
126 // set locale according to template:
127 if (isset($template['LOCALE']))
128 setlocale(LC_TIME,$template['LOCALE']);
130 setlocale(LC_TIME,'');
136 * fills a template with values
140 * Template to be used
142 * Array of all the values
144 function fill($template, $values) {
146 if (sizeof($values) != 0) {
147 // go through all the values
148 for(reset($values); $key = key($values); next($values)) {
149 $template = str_replace("<%$key%>",$values[$key],$template);
153 // remove non matched template-tags
154 return preg_replace('/<%[a-zA-Z]+%>/','',$template);
157 // returns true if there is a template with the given shortname
159 function exists($name) {
160 $r = sql_query('select * FROM '.sql_table('template_desc').' WHERE tdname="'.sql_real_escape_string($name).'"');
161 return (sql_num_rows($r) != 0);
164 // returns true if there is a template with the given ID
166 function existsID($id) {
167 $r = sql_query('select * FROM '.sql_table('template_desc').' WHERE tdnumber='.intval($id));
168 return (sql_num_rows($r) != 0);
172 function getNameFromId($id) {
173 return quickQuery('SELECT tdname as result FROM '.sql_table('template_desc').' WHERE tdnumber=' . intval($id));
177 function getDesc($id) {
178 $query = 'SELECT tddesc FROM '.sql_table('template_desc').' WHERE tdnumber='. intval($id);
179 $res = sql_query($query);
180 $obj = sql_fetch_object($res);