5 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
\r
6 * Copyright (C) 2002-2012 The Nucleus Group
\r
8 * This program is free software; you can redistribute it and/or
\r
9 * modify it under the terms of the GNU General Public License
\r
10 * as published by the Free Software Foundation; either version 2
\r
11 * of the License, or (at your option) any later version.
\r
12 * (see nucleus/documentation/index.html#license for more info)
\r
15 * A class representing a template
\r
17 * @license http://nucleuscms.org/license.txt GNU General Public License
\r
18 * @copyright Copyright (C) 2002-2012 The Nucleus Group
\r
19 * @version $Id: TEMPLATE.php 1726 2012-04-07 02:23:46Z sakamocchi $
\r
29 * Template::__construct()
\r
31 * @param integer $templateid id for template
\r
34 public function __construct($templateid)
\r
36 $this->id = intval($templateid);
\r
44 * @return integer id for this instance of Template class
\r
46 public function getID()
\r
48 return (integer) $this->id;
\r
52 * Template::createFromName()
\r
55 * @param string $name template name
\r
56 * @return object instance of Template class generated by the name
\r
58 static public function createFromName($name)
\r
60 return new Template(Template::getIdFromName($name));
\r
64 * Template::getIdFromName()
\r
67 * @param string $name template name
\r
68 * @return integer id for the template
\r
70 static public function getIdFromName($name)
\r
72 $name = DB::quoteValue($name);
\r
73 $query = "SELECT tdnumber FROM %s WHERE tdname=%s";
\r
74 $query = sprintf($query, sql_table('template_desc'), $name);
\r
75 return DB::getValue($query);
\r
79 * Template::updateGeneralInfo()
\r
80 * Updates the general information about the template
\r
82 * @param string $name template name
\r
83 * @param string $desc description for this template
\r
86 public function updateGeneralInfo($name, $desc)
\r
88 $query = "UPDATE %s SET tdname=%s, tddesc=%s WHERE tdnumber=%d";
\r
89 $query = sprintf($query, sql_table('template_desc'), DB::quoteValue($name), DB::quoteValue($desc), (integer) $this->getID());
\r
90 DB::execute($query);
\r
95 * Template::update()
\r
96 * Updates the contents of one part of the template
\r
98 * @param String $type value for nucleus_template.tpartname
\r
99 * @param String $content value for nucleus_template.tcontent
\r
102 public function update($type, $content)
\r
104 // delete old thingie
\r
105 $query = "DELETE FROM %s WHERE tpartname=%s and tdesc=%d";
\r
106 $query = sprintf($query, sql_table('template'), DB::quoteValue($type), (integer) $this->getID());
\r
107 DB::execute($query);
\r
109 // write new thingie
\r
112 $query = "INSERT %s (tcontent, tpartname, tdesc) VALUE (%s, %s, %d)";
\r
113 $query = sprintf($query, sql_table('template'), DB::quoteValue($content), DB::quoteValue($type), (integer) $this->getID());
\r
114 DB::execute($query);
\r
120 * Template::deleteAllParts()
\r
121 * Deletes all template parts from the database
\r
126 public function deleteAllParts()
\r
128 $query = "DELETE FROM %s WHERE tdesc=%d";
\r
129 $query = sprintf($query, sql_table('template'), (integer) $this->getID());
\r
130 DB::execute($query);
\r
135 * Template::createNew()
\r
136 * Creates a new template
\r
139 * @param string $name name for new template
\r
140 * @param string $desc description for new template
\r
141 * @return integer id for new template
\r
143 static public function createNew($name, $desc)
\r
151 'description' => &$desc
\r
155 DB::execute('INSERT INTO '.sql_table('template_desc').' (tdname, tddesc) VALUES (' . DB::quoteValue($name) . ',' . DB::quoteValue($desc) . ')');
\r
156 $newId = DB::getInsertId();
\r
161 'templateid' => $newId,
\r
163 'description' => $desc
\r
171 * Reads a template and returns an array with the parts.
\r
174 * @param string $name name of the template file
\r
175 * @return array template array
\r
177 static public function read($name)
\r
183 'template' => &$name
\r
187 $query = "SELECT tpartname, tcontent FROM %s, %s WHERE tdesc=tdnumber and tdname=%s";
\r
188 $query = sprintf($query, sql_table('template_desc'), sql_table('template'), DB::quoteValue($name));
\r
189 $res = DB::getResult($query);
\r
191 foreach ( $res as $row )
\r
193 $template[$row['tpartname']] = $row['tcontent'];
\r
197 * TODO: this is appropriate or not?
\r
199 if ( array_key_exists('LOCALE', $template) && !empty($template['LOCALE']) )
\r
201 setlocale(LC_TIME, $template['LOCALE']);
\r
205 setlocale(LC_TIME,'');
\r
212 * fills a template with values
\r
215 * @param string $template Template to be used
\r
216 * @param array $values Array of all the values
\r
217 * @return string string filled with tag contents
\r
219 static public function fill($template, $values)
\r
222 if ( sizeof($values) != 0 )
\r
224 foreach ( $values as $key => $value )
\r
226 $template = preg_replace('#<%' . preg_quote($key, '#') . '%>#', $value, $template);
\r
230 // remove non matched template-tags
\r
231 return preg_replace('#<%([a-zA-Z]+)?%>#', '', $template);
\r
235 * TEMPLATE::exists()
\r
236 * returns true if there is a template with the given shortname
\r
239 * @param string $name template name
\r
240 * @return boolean exists or not
\r
242 static public function exists($name)
\r
244 $query = "SELECT * FROM %s WHERE tdname=%s";
\r
245 $query = sprintf($query, sql_table('template_desc'), DB::quoteValue($name));
\r
246 $r = DB::getResult($query);
\r
247 return ($r->rowCount() != 0);
\r
251 * TEMPLATE::existsID()
\r
252 * returns true if there is a template with the given ID
\r
255 * @param integer $id id for template
\r
256 * @return bookean exists or not
\r
258 static public function existsID($id)
\r
260 $query = "SELECT * FROM %s WHERE tdnumber=%d";
\r
261 $query = sprintf($query, sql_table('template_desc'), (integer) $id);
\r
262 $r = DB::getResult($query);
\r
263 return ($r->rowCount() != 0);
\r
267 * TEMPLATE::getNameFromId()
\r
270 * @param integer $id id for template
\r
271 * @return object sql object
\r
273 static public function getNameFromId($id)
\r
275 $query = "SELECT tdname as result FROM %s WHERE tdnumber=%d";
\r
276 $query = sprintf($query, sql_table('template_desc'), (integer) $id);
\r
277 return DB::getValue($query);
\r
281 * TEMPLATE::getDesc()
\r
284 * @param integer $id id for template
\r
285 * @return string description for the template
\r
287 static public function getDesc($id)
\r
289 $query = "SELECT tddesc FROM %s WHERE tdnumber=%d";
\r
290 $query = sprintf($query, sql_table('template_desc'), (integer) $id);
\r
291 return DB::getValue($query);
\r
298 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
299 * Copyright (C) 2002-2009 The Nucleus Group
301 * This program is free software; you can redistribute it and/or
302 * modify it under the terms of the GNU General Public License
303 * as published by the Free Software Foundation; either version 2
304 * of the License, or (at your option) any later version.
305 * (see nucleus/documentation/index.html#license for more info)
308 * A class representing a template
310 * @license http://nucleuscms.org/license.txt GNU General Public License
311 * @copyright Copyright (C) 2002-2009 The Nucleus Group
312 * @version $Id: TEMPLATE.php 1880 2012-06-17 07:48:14Z sakamocchi $
322 * Template::__construct()
324 * @param integer $templateid id for template
327 public function __construct($templateid)
329 $this->id = intval($templateid);
337 * @return integer id for this instance of Template class
339 public function getID()
341 return (integer) $this->id;
345 * Template::createFromName()
348 * @param string $name template name
349 * @return object instance of Template class generated by the name
351 static public function createFromName($name)
353 return new Template(Template::getIdFromName($name));
357 * Template::getIdFromName()
360 * @param string $name template name
361 * @return integer id for the template
363 static public function getIdFromName($name)
365 $name = DB::quoteValue($name);
366 $query = "SELECT tdnumber FROM %s WHERE tdname=%s";
367 $query = sprintf($query, sql_table('template_desc'), $name);
368 return DB::getValue($query);
372 * Template::updateGeneralInfo()
373 * Updates the general information about the template
375 * @param string $name template name
376 * @param string $desc description for this template
379 public function updateGeneralInfo($name, $desc)
381 $query = "UPDATE %s SET tdname=%s, tddesc=%s WHERE tdnumber=%d";
382 $query = sprintf($query, sql_table('template_desc'), DB::quoteValue($name), DB::quoteValue($desc), (integer) $this->getID());
389 * Updates the contents of one part of the template
391 * @param String $type value for nucleus_template.tpartname
392 * @param String $content value for nucleus_template.tcontent
395 public function update($type, $content)
397 // delete old thingie
398 $query = "DELETE FROM %s WHERE tpartname=%s and tdesc=%d";
399 $query = sprintf($query, sql_table('template'), DB::quoteValue($type), (integer) $this->getID());
405 $query = "INSERT INTO %s (tcontent, tpartname, tdesc) VALUES (%s, %s, %d)";
406 $query = sprintf($query, sql_table('template'), DB::quoteValue($content), DB::quoteValue($type), (integer) $this->getID());
413 * Template::deleteAllParts()
414 * Deletes all template parts from the database
419 public function deleteAllParts()
421 $query = "DELETE FROM %s WHERE tdesc=%d";
422 $query = sprintf($query, sql_table('template'), (integer) $this->getID());
428 * Template::createNew()
429 * Creates a new template
432 * @param string $name name for new template
433 * @param string $desc description for new template
434 * @return integer id for new template
436 static public function createNew($name, $desc)
442 'description' => &$desc
444 $manager->notify('PreAddTemplate', $data);
446 DB::execute('INSERT INTO '.sql_table('template_desc').' (tdname, tddesc) VALUES (' . DB::quoteValue($name) . ',' . DB::quoteValue($desc) . ')');
447 $newId = DB::getInsertId();
450 'templateid' => $newId,
452 'description' => $desc
454 $manager->notify('PostAddTemplate', $data);
460 * Reads a template and returns an array with the parts.
463 * @param string $name name of the template file
464 * @return array template array
466 static public function read($name)
469 $data = array('template' => &$name);
470 $manager->notify('PreTemplateRead', $data);
472 $query = "SELECT tpartname, tcontent FROM %s, %s WHERE tdesc=tdnumber and tdname=%s";
473 $query = sprintf($query, sql_table('template_desc'), sql_table('template'), DB::quoteValue($name));
474 $res = DB::getResult($query);
477 foreach ( $res as $row )
479 $template[$row['tpartname']] = $row['tcontent'];
486 * fills a template with values
489 * @param string $template Template to be used
490 * @param array $values Array of all the values
491 * @return string string filled with tag contents
493 static public function fill($template, $values)
496 if ( sizeof($values) != 0 )
498 foreach ( $values as $key => $value )
500 $template = preg_replace('#<%' . preg_quote($key, '#') . '%>#', $value, $template);
504 // remove non matched template-tags
505 return preg_replace('#<%([a-zA-Z]+)?%>#', '', $template);
510 * returns true if there is a template with the given shortname
513 * @param string $name template name
514 * @return boolean exists or not
516 static public function exists($name)
518 $query = "SELECT * FROM %s WHERE tdname=%s";
519 $query = sprintf($query, sql_table('template_desc'), DB::quoteValue($name));
520 $r = DB::getResult($query);
521 return ($r->rowCount() != 0);
525 * Template::existsID()
526 * returns true if there is a template with the given ID
529 * @param integer $id id for template
530 * @return bookean exists or not
532 static public function existsID($id)
534 $query = "SELECT * FROM %s WHERE tdnumber=%d";
535 $query = sprintf($query, sql_table('template_desc'), (integer) $id);
536 $r = DB::getResult($query);
537 return ($r->rowCount() != 0);
541 * Template::getNameFromId()
544 * @param integer $id id for template
545 * @return object sql object
547 static public function getNameFromId($id)
549 $query = "SELECT tdname as result FROM %s WHERE tdnumber=%d";
550 $query = sprintf($query, sql_table('template_desc'), (integer) $id);
551 return DB::getValue($query);
555 * Template::getDesc()
558 * @param integer $id id for template
559 * @return string description for the template
561 static public function getDesc($id)
563 $query = "SELECT tddesc FROM %s WHERE tdnumber=%d";
564 $query = sprintf($query, sql_table('template_desc'), (integer) $id);
565 return DB::getValue($query);
568 >>>>>>> skinnable-master