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();
60 * Updates the contents of one part of the template
62 * @param String $type value for nucleus_template.tpartname
63 * @param String $content value for nucleus_template.tcontent
66 function update($type, $content)
69 $query = "DELETE FROM %s WHERE tpartname='%s' and tdesc=%d";
70 $query = sprintf($query, sql_table('template'), sql_real_escape_string($type), (integer) $this->getID());
76 $query = "INSERT %s (tcontent, tpartname, tdesc) VALUE ('%s', '%s', %d)";
77 $query = sprintf($query, sql_table('template'), sql_real_escape_string($content), sql_real_escape_string($type), (integer) $this->getID());
85 * Deletes all template parts from the database
87 function deleteAllParts() {
88 sql_query('DELETE FROM '.sql_table('template').' WHERE tdesc='.$this->getID());
92 * Creates a new template
96 function createNew($name, $desc) {
103 'description' => &$desc
107 sql_query('INSERT INTO '.sql_table('template_desc')." (tdname, tddesc) VALUES ('" . sql_real_escape_string($name) . "','" . sql_real_escape_string($desc) . "')");
108 $newId = sql_insert_id();
113 'templateid' => $newId,
115 'description' => $desc
125 * Reads a template and returns an array with the parts.
128 * @param $name name of the template file
130 function read($name) {
139 $query = 'SELECT tpartname, tcontent'
140 . ' FROM '.sql_table('template_desc').', '.sql_table('template')
141 . ' WHERE tdesc=tdnumber and tdname="' . sql_real_escape_string($name) . '"';
142 $res = sql_query($query);
143 while ($obj = sql_fetch_object($res))
144 $template[$obj->tpartname] = $obj->tcontent;
146 // set locale according to template:
147 if (isset($template['LOCALE']))
148 setlocale(LC_TIME,$template['LOCALE']);
150 setlocale(LC_TIME,'');
156 * fills a template with values
160 * Template to be used
162 * Array of all the values
164 function fill($template, $values) {
166 if (sizeof($values) != 0) {
167 // go through all the values
168 for(reset($values); $key = key($values); next($values)) {
169 $template = str_replace("<%$key%>",$values[$key],$template);
173 // remove non matched template-tags
174 return preg_replace('/<%[a-zA-Z]+%>/','',$template);
177 // returns true if there is a template with the given shortname
179 function exists($name) {
180 $r = sql_query('select * FROM '.sql_table('template_desc').' WHERE tdname="'.sql_real_escape_string($name).'"');
181 return (sql_num_rows($r) != 0);
184 // returns true if there is a template with the given ID
186 function existsID($id) {
187 $r = sql_query('select * FROM '.sql_table('template_desc').' WHERE tdnumber='.intval($id));
188 return (sql_num_rows($r) != 0);
192 function getNameFromId($id) {
193 return quickQuery('SELECT tdname as result FROM '.sql_table('template_desc').' WHERE tdnumber=' . intval($id));
197 function getDesc($id) {
198 $query = 'SELECT tddesc FROM '.sql_table('template_desc').' WHERE tdnumber='. intval($id);
199 $res = sql_query($query);
200 $obj = sql_fetch_object($res);