4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
\r
5 * Copyright (C) 2002-2012 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
14 * A class representing a template
\r
16 * @license http://nucleuscms.org/license.txt GNU General Public License
\r
17 * @copyright Copyright (C) 2002-2012 The Nucleus Group
\r
18 * @version $Id: TEMPLATE.php 1470 2010-11-29 22:10:16Z ftruscot $
\r
24 function TEMPLATE($templateid) {
\r
25 $this->id = intval($templateid);
\r
29 return intval($this->id);
\r
33 function createFromName($name) {
\r
34 return new Template(Template::getIdFromName($name));
\r
38 function getIdFromName($name) {
\r
39 $query = 'SELECT tdnumber'
\r
40 . ' FROM '.sql_table('template_desc')
\r
41 . ' WHERE tdname="'.sql_real_escape_string($name).'"';
\r
42 $res = sql_query($query);
\r
43 $obj = sql_fetch_object($res);
\r
44 return $obj->tdnumber;
\r
48 * Updates the general information about the template
\r
50 function updateGeneralInfo($name, $desc) {
\r
51 $query = 'UPDATE '.sql_table('template_desc').' SET'
\r
52 . " tdname='" . sql_real_escape_string($name) . "',"
\r
53 . " tddesc='" . sql_real_escape_string($desc) . "'"
\r
54 . " WHERE tdnumber=" . $this->getID();
\r
59 * Template::update()
\r
60 * Updates the contents of one part of the template
\r
62 * @param String $type value for nucleus_template.tpartname
\r
63 * @param String $content value for nucleus_template.tcontent
\r
66 function update($type, $content)
\r
68 // delete old thingie
\r
69 $query = "DELETE FROM %s WHERE tpartname='%s' and tdesc=%d";
\r
70 $query = sprintf($query, sql_table('template'), sql_real_escape_string($type), (integer) $this->getID());
\r
73 // write new thingie
\r
76 $query = "INSERT %s (tcontent, tpartname, tdesc) VALUE ('%s', '%s', %d)";
\r
77 $query = sprintf($query, sql_table('template'), sql_real_escape_string($content), sql_real_escape_string($type), (integer) $this->getID());
\r
85 * Deletes all template parts from the database
\r
87 function deleteAllParts() {
\r
88 sql_query('DELETE FROM '.sql_table('template').' WHERE tdesc='.$this->getID());
\r
92 * Creates a new template
\r
96 function createNew($name, $desc) {
\r
103 'description' => &$desc
\r
107 sql_query('INSERT INTO '.sql_table('template_desc')." (tdname, tddesc) VALUES ('" . sql_real_escape_string($name) . "','" . sql_real_escape_string($desc) . "')");
\r
108 $newId = sql_insert_id();
\r
113 'templateid' => $newId,
\r
115 'description' => $desc
\r
125 * Reads a template and returns an array with the parts.
\r
128 * @param $name name of the template file
\r
130 function read($name) {
\r
135 'template' => &$name
\r
139 $query = 'SELECT tpartname, tcontent'
\r
140 . ' FROM '.sql_table('template_desc').', '.sql_table('template')
\r
141 . ' WHERE tdesc=tdnumber and tdname="' . sql_real_escape_string($name) . '"';
\r
142 $res = sql_query($query);
\r
143 while ($obj = sql_fetch_object($res))
\r
144 $template[$obj->tpartname] = $obj->tcontent;
\r
146 // set locale according to template:
\r
147 if (isset($template['LOCALE']))
\r
148 setlocale(LC_TIME,$template['LOCALE']);
\r
150 setlocale(LC_TIME,'');
\r
156 * fills a template with values
\r
160 * Template to be used
\r
162 * Array of all the values
\r
164 function fill($template, $values) {
\r
166 if (sizeof($values) != 0) {
\r
167 // go through all the values
\r
168 for(reset($values); $key = key($values); next($values)) {
\r
169 $template = str_replace("<%$key%>",$values[$key],$template);
\r
173 // remove non matched template-tags
\r
174 return preg_replace('/<%[a-zA-Z]+%>/','',$template);
\r
177 // returns true if there is a template with the given shortname
\r
179 function exists($name) {
\r
180 $r = sql_query('select * FROM '.sql_table('template_desc').' WHERE tdname="'.sql_real_escape_string($name).'"');
\r
181 return (sql_num_rows($r) != 0);
\r
184 // returns true if there is a template with the given ID
\r
186 function existsID($id) {
\r
187 $r = sql_query('select * FROM '.sql_table('template_desc').' WHERE tdnumber='.intval($id));
\r
188 return (sql_num_rows($r) != 0);
\r
192 function getNameFromId($id) {
\r
193 return quickQuery('SELECT tdname as result FROM '.sql_table('template_desc').' WHERE tdnumber=' . intval($id));
\r
197 function getDesc($id) {
\r
198 $query = 'SELECT tddesc FROM '.sql_table('template_desc').' WHERE tdnumber='. intval($id);
\r
199 $res = sql_query($query);
\r
200 $obj = sql_fetch_object($res);
\r
201 return $obj->tddesc;
\r