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 1726 2012-04-07 02:23:46Z sakamocchi $
\r
28 * Template::__construct()
\r
30 * @param integer $templateid id for template
\r
33 public function __construct($templateid)
\r
35 $this->id = intval($templateid);
\r
43 * @return integer id for this instance of Template class
\r
45 public function getID()
\r
47 return (integer) $this->id;
\r
51 * Template::createFromName()
\r
54 * @param string $name template name
\r
55 * @return object instance of Template class generated by the name
\r
57 static public function createFromName($name)
\r
59 return new Template(Template::getIdFromName($name));
\r
63 * Template::getIdFromName()
\r
66 * @param string $name template name
\r
67 * @return integer id for the template
\r
69 static public function getIdFromName($name)
\r
71 $name = sql_real_escape_string($name);
\r
72 $query = "SELECT tdnumber FROM %s WHERE tdname='%s';";
\r
73 $query = sprintf($query, sql_table('template_desc'), $name);
\r
74 $res = sql_query($query);
\r
75 $obj = sql_fetch_object($res);
\r
76 return $obj->tdnumber;
\r
80 * Template::updateGeneralInfo()
\r
81 * Updates the general information about the template
\r
83 * @param string $name template name
\r
84 * @param string $desc description for this template
\r
87 public function updateGeneralInfo($name, $desc)
\r
89 $query = "UPDATE %s SET tdname='%s', tddesc='%s' WHERE tdnumber=%d;";
\r
90 $query = sprintf($query, sql_table('template_desc'), sql_real_escape_string($name), sql_real_escape_string($desc), (integer) $this->getID());
\r
96 * Template::update()
\r
97 * Updates the contents of one part of the template
\r
99 * @param String $type value for nucleus_template.tpartname
\r
100 * @param String $content value for nucleus_template.tcontent
\r
103 public function update($type, $content)
\r
105 // delete old thingie
\r
106 $query = "DELETE FROM %s WHERE tpartname='%s' and tdesc=%d";
\r
107 $query = sprintf($query, sql_table('template'), sql_real_escape_string($type), (integer) $this->getID());
\r
110 // write new thingie
\r
113 $query = "INSERT %s (tcontent, tpartname, tdesc) VALUE ('%s', '%s', %d)";
\r
114 $query = sprintf($query, sql_table('template'), sql_real_escape_string($content), sql_real_escape_string($type), (integer) $this->getID());
\r
121 * Template::deleteAllParts()
\r
122 * Deletes all template parts from the database
\r
127 public function deleteAllParts()
\r
129 $query = "DELETE FROM %s WHERE tdesc=%d";
\r
130 $query = sprintf($query, sql_table('template'), (integer) $this->getID());
\r
136 * Template::createNew()
\r
137 * Creates a new template
\r
140 * @param string $name name for new template
\r
141 * @param string $desc description for new template
\r
142 * @return integer id for new template
\r
144 static public function createNew($name, $desc)
\r
152 'description' => &$desc
\r
156 sql_query('INSERT INTO '.sql_table('template_desc')." (tdname, tddesc) VALUES ('" . sql_real_escape_string($name) . "','" . sql_real_escape_string($desc) . "')");
\r
157 $newId = sql_insert_id();
\r
162 'templateid' => $newId,
\r
164 'description' => $desc
\r
172 * Reads a template and returns an array with the parts.
\r
175 * @param string $name name of the template file
\r
176 * @return array template array
\r
178 static public function read($name)
\r
184 'template' => &$name
\r
188 $query = "SELECT tpartname, tcontent FROM %s, %s WHERE tdesc=tdnumber and tdname='%s'";
\r
189 $query = sprintf($query, sql_table('template_desc'), sql_table('template'), sql_real_escape_string($name));
\r
190 $res = sql_query($query);
\r
192 $template = array();
\r
193 while ($obj = sql_fetch_object($res))
\r
195 $template[$obj->tpartname] = $obj->tcontent;
\r
199 * TODO: this is appropriate or not?
\r
201 if ( array_key_exists('LOCALE', $template) && !empty($template['LOCALE']) )
\r
203 setlocale(LC_TIME, $template['LOCALE']);
\r
207 setlocale(LC_TIME,'');
\r
214 * fills a template with values
\r
217 * @param string $template Template to be used
\r
218 * @param array $values Array of all the values
\r
219 * @return string string filled with tag contents
\r
221 static public function fill($template, $values)
\r
224 if ( sizeof($values) != 0 )
\r
226 foreach ( $values as $key => $value )
\r
228 $template = preg_replace('#<%' . preg_quote($key, '#') . '%>#', $value, $template);
\r
232 // remove non matched template-tags
\r
233 return preg_replace('#<%([a-zA-Z]+)?%>#', '', $template);
\r
237 * Template::exists()
\r
238 * returns true if there is a template with the given shortname
\r
241 * @param string $name template name
\r
242 * @return boolean exists or not
\r
244 static public function exists($name)
\r
246 $query = "SELECT * FROM %s WHERE tdname='%s';";
\r
247 $query = sprintf($query, sql_table('template_desc'), sql_real_escape_string($name));
\r
248 $r = sql_query($query);
\r
249 return (sql_num_rows($r) != 0);
\r
253 * Template::existsID()
\r
254 * returns true if there is a template with the given ID
\r
257 * @param integer $id id for template
\r
258 * @return bookean exists or not
\r
260 static public function existsID($id)
\r
262 $query = "SELECT * FROM %s WHERE tdnumber=%d;";
\r
263 $query = sprintf($query, sql_table('template_desc'), (integer) $id);
\r
264 $r = sql_query($query);
\r
265 return (sql_num_rows($r) != 0);
\r
269 * Template::getNameFromId()
\r
272 * @param integer $id id for template
\r
273 * @return object sql object
\r
275 static public function getNameFromId($id)
\r
277 $query = "SELECT tdname as result FROM %s WHERE tdnumber=%d";
\r
278 $query = sprintf($query, sql_table('template_desc'), (integer) $id);
\r
279 return quickQuery($query);
\r
283 * Template::getDesc()
\r
286 * @param integer $id id for template
\r
287 * @return string description for the template
\r
289 static public function getDesc($id)
\r
291 $query = "SELECT tddesc FROM %s WHERE tdnumber=%d;";
\r
292 $query = sprintf($query, sql_table('template_desc'), (integer) $id);
\r
293 $res = sql_query($query);
\r
294 $obj = sql_fetch_object($res);
\r
295 return $obj->tddesc;
\r