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 = DB::quoteValue($name);
\r
72 $query = "SELECT tdnumber FROM %s WHERE tdname=%s";
\r
73 $query = sprintf($query, sql_table('template_desc'), $name);
\r
74 return DB::getValue($query);
\r
78 * Template::updateGeneralInfo()
\r
79 * Updates the general information about the template
\r
81 * @param string $name template name
\r
82 * @param string $desc description for this template
\r
85 public function updateGeneralInfo($name, $desc)
\r
87 $query = "UPDATE %s SET tdname=%s, tddesc=%s WHERE tdnumber=%d";
\r
88 $query = sprintf($query, sql_table('template_desc'), DB::quoteValue($name), DB::quoteValue($desc), (integer) $this->getID());
\r
89 DB::execute($query);
\r
94 * Template::update()
\r
95 * Updates the contents of one part of the template
\r
97 * @param String $type value for nucleus_template.tpartname
\r
98 * @param String $content value for nucleus_template.tcontent
\r
101 public function update($type, $content)
\r
103 // delete old thingie
\r
104 $query = "DELETE FROM %s WHERE tpartname=%s and tdesc=%d";
\r
105 $query = sprintf($query, sql_table('template'), DB::quoteValue($type), (integer) $this->getID());
\r
106 DB::execute($query);
\r
108 // write new thingie
\r
111 $query = "INSERT %s (tcontent, tpartname, tdesc) VALUE (%s, %s, %d)";
\r
112 $query = sprintf($query, sql_table('template'), DB::quoteValue($content), DB::quoteValue($type), (integer) $this->getID());
\r
113 DB::execute($query);
\r
119 * Template::deleteAllParts()
\r
120 * Deletes all template parts from the database
\r
125 public function deleteAllParts()
\r
127 $query = "DELETE FROM %s WHERE tdesc=%d";
\r
128 $query = sprintf($query, sql_table('template'), (integer) $this->getID());
\r
129 DB::execute($query);
\r
134 * Template::createNew()
\r
135 * Creates a new template
\r
138 * @param string $name name for new template
\r
139 * @param string $desc description for new template
\r
140 * @return integer id for new template
\r
142 static public function createNew($name, $desc)
\r
150 'description' => &$desc
\r
154 DB::execute('INSERT INTO '.sql_table('template_desc').' (tdname, tddesc) VALUES (' . DB::quoteValue($name) . ',' . DB::quoteValue($desc) . ')');
\r
155 $newId = DB::getInsertId();
\r
160 'templateid' => $newId,
\r
162 'description' => $desc
\r
170 * Reads a template and returns an array with the parts.
\r
173 * @param string $name name of the template file
\r
174 * @return array template array
\r
176 static public function read($name)
\r
182 'template' => &$name
\r
186 $query = "SELECT tpartname, tcontent FROM %s, %s WHERE tdesc=tdnumber and tdname=%s";
\r
187 $query = sprintf($query, sql_table('template_desc'), sql_table('template'), DB::quoteValue($name));
\r
188 $res = DB::getResult($query);
\r
190 foreach ( $res as $row )
\r
192 $template[$row['tpartname']] = $row['tcontent'];
\r
196 * TODO: this is appropriate or not?
\r
198 if ( array_key_exists('LOCALE', $template) && !empty($template['LOCALE']) )
\r
200 setlocale(LC_TIME, $template['LOCALE']);
\r
204 setlocale(LC_TIME,'');
\r
211 * fills a template with values
\r
214 * @param string $template Template to be used
\r
215 * @param array $values Array of all the values
\r
216 * @return string string filled with tag contents
\r
218 static public function fill($template, $values)
\r
221 if ( sizeof($values) != 0 )
\r
223 foreach ( $values as $key => $value )
\r
225 $template = preg_replace('#<%' . preg_quote($key, '#') . '%>#', $value, $template);
\r
229 // remove non matched template-tags
\r
230 return preg_replace('#<%([a-zA-Z]+)?%>#', '', $template);
\r
234 * TEMPLATE::exists()
\r
235 * returns true if there is a template with the given shortname
\r
238 * @param string $name template name
\r
239 * @return boolean exists or not
\r
241 static public function exists($name)
\r
243 $query = "SELECT * FROM %s WHERE tdname=%s";
\r
244 $query = sprintf($query, sql_table('template_desc'), DB::quoteValue($name));
\r
245 $r = DB::getResult($query);
\r
246 return ($r->rowCount() != 0);
\r
250 * TEMPLATE::existsID()
\r
251 * returns true if there is a template with the given ID
\r
254 * @param integer $id id for template
\r
255 * @return bookean exists or not
\r
257 static public function existsID($id)
\r
259 $query = "SELECT * FROM %s WHERE tdnumber=%d";
\r
260 $query = sprintf($query, sql_table('template_desc'), (integer) $id);
\r
261 $r = DB::getResult($query);
\r
262 return ($r->rowCount() != 0);
\r
266 * TEMPLATE::getNameFromId()
\r
269 * @param integer $id id for template
\r
270 * @return object sql object
\r
272 static public function getNameFromId($id)
\r
274 $query = "SELECT tdname as result FROM %s WHERE tdnumber=%d";
\r
275 $query = sprintf($query, sql_table('template_desc'), (integer) $id);
\r
276 return DB::getValue($query);
\r
280 * TEMPLATE::getDesc()
\r
283 * @param integer $id id for template
\r
284 * @return string description for the template
\r
286 static public function getDesc($id)
\r
288 $query = "SELECT tddesc FROM %s WHERE tdnumber=%d";
\r
289 $query = sprintf($query, sql_table('template_desc'), (integer) $id);
\r
290 return DB::getValue($query);
\r