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 1880 2012-06-17 07:48:14Z sakamocchi $
28 * Template::__construct()
30 * @param integer $templateid id for template
33 public function __construct($templateid)
35 $this->id = intval($templateid);
43 * @return integer id for this instance of Template class
45 public function getID()
47 return (integer) $this->id;
51 * Template::createFromName()
54 * @param string $name template name
55 * @return object instance of Template class generated by the name
57 static public function createFromName($name)
59 return new Template(Template::getIdFromName($name));
63 * Template::getIdFromName()
66 * @param string $name template name
67 * @return integer id for the template
69 static public function getIdFromName($name)
71 $name = DB::quoteValue($name);
72 $query = "SELECT tdnumber FROM %s WHERE tdname=%s";
73 $query = sprintf($query, sql_table('template_desc'), $name);
74 return DB::getValue($query);
78 * Template::updateGeneralInfo()
79 * Updates the general information about the template
81 * @param string $name template name
82 * @param string $desc description for this template
85 public function updateGeneralInfo($name, $desc)
87 $query = "UPDATE %s SET tdname=%s, tddesc=%s WHERE tdnumber=%d";
88 $query = sprintf($query, sql_table('template_desc'), DB::quoteValue($name), DB::quoteValue($desc), (integer) $this->getID());
95 * Updates the contents of one part of the template
97 * @param String $type value for nucleus_template.tpartname
98 * @param String $content value for nucleus_template.tcontent
101 public function update($type, $content)
103 // delete old thingie
104 $query = "DELETE FROM %s WHERE tpartname=%s and tdesc=%d";
105 $query = sprintf($query, sql_table('template'), DB::quoteValue($type), (integer) $this->getID());
111 $query = "INSERT INTO %s (tcontent, tpartname, tdesc) VALUES (%s, %s, %d)";
112 $query = sprintf($query, sql_table('template'), DB::quoteValue($content), DB::quoteValue($type), (integer) $this->getID());
119 * Template::deleteAllParts()
120 * Deletes all template parts from the database
125 public function deleteAllParts()
127 $query = "DELETE FROM %s WHERE tdesc=%d";
128 $query = sprintf($query, sql_table('template'), (integer) $this->getID());
134 * Template::createNew()
135 * Creates a new template
138 * @param string $name name for new template
139 * @param string $desc description for new template
140 * @return integer id for new template
142 static public function createNew($name, $desc)
148 'description' => &$desc
150 $manager->notify('PreAddTemplate', $data);
152 DB::execute('INSERT INTO '.sql_table('template_desc').' (tdname, tddesc) VALUES (' . DB::quoteValue($name) . ',' . DB::quoteValue($desc) . ')');
153 $newId = DB::getInsertId();
156 'templateid' => $newId,
158 'description' => $desc
160 $manager->notify('PostAddTemplate', $data);
166 * Reads a template and returns an array with the parts.
169 * @param string $name name of the template file
170 * @return array template array
172 static public function read($name)
175 $data = array('template' => &$name);
176 $manager->notify('PreTemplateRead', $data);
178 $query = "SELECT tpartname, tcontent FROM %s, %s WHERE tdesc=tdnumber and tdname=%s";
179 $query = sprintf($query, sql_table('template_desc'), sql_table('template'), DB::quoteValue($name));
180 $res = DB::getResult($query);
183 foreach ( $res as $row )
185 $template[$row['tpartname']] = $row['tcontent'];
189 * TODO: this is appropriate or not?
191 if ( array_key_exists('LOCALE', $template) && !empty($template['LOCALE']) )
193 setlocale(LC_TIME, $template['LOCALE']);
197 setlocale(LC_TIME,'');
204 * fills a template with values
207 * @param string $template Template to be used
208 * @param array $values Array of all the values
209 * @return string string filled with tag contents
211 static public function fill($template, $values)
214 if ( sizeof($values) != 0 )
216 foreach ( $values as $key => $value )
218 $template = preg_replace('#<%' . preg_quote($key, '#') . '%>#', $value, $template);
222 // remove non matched template-tags
223 return preg_replace('#<%([a-zA-Z]+)?%>#', '', $template);
228 * returns true if there is a template with the given shortname
231 * @param string $name template name
232 * @return boolean exists or not
234 static public function exists($name)
236 $query = "SELECT * FROM %s WHERE tdname=%s";
237 $query = sprintf($query, sql_table('template_desc'), DB::quoteValue($name));
238 $r = DB::getResult($query);
239 return ($r->rowCount() != 0);
243 * Template::existsID()
244 * returns true if there is a template with the given ID
247 * @param integer $id id for template
248 * @return bookean exists or not
250 static public function existsID($id)
252 $query = "SELECT * FROM %s WHERE tdnumber=%d";
253 $query = sprintf($query, sql_table('template_desc'), (integer) $id);
254 $r = DB::getResult($query);
255 return ($r->rowCount() != 0);
259 * Template::getNameFromId()
262 * @param integer $id id for template
263 * @return object sql object
265 static public function getNameFromId($id)
267 $query = "SELECT tdname as result FROM %s WHERE tdnumber=%d";
268 $query = sprintf($query, sql_table('template_desc'), (integer) $id);
269 return DB::getValue($query);
273 * Template::getDesc()
276 * @param integer $id id for template
277 * @return string description for the template
279 static public function getDesc($id)
281 $query = "SELECT tddesc FROM %s WHERE tdnumber=%d";
282 $query = sprintf($query, sql_table('template_desc'), (integer) $id);
283 return DB::getValue($query);