/*
* Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
- * Copyright (C) 2002-2009 The Nucleus Group
+ * Copyright (C) 2002-2010 The Nucleus Group
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* A class representing a template
*
* @license http://nucleuscms.org/license.txt GNU General Public License
- * @copyright Copyright (C) 2002-2009 The Nucleus Group
+ * @copyright Copyright (C) 2002-2010 The Nucleus Group
* @version $Id$
* @version $NucleusJP: TEMPLATE.php,v 1.6 2006/07/20 08:01:52 kimitake Exp $
*/
-class TEMPLATE {\r
-\r
- var $id;\r
-\r
- function TEMPLATE($templateid) {\r
- $this->id = intval($templateid);\r
- }\r
-\r
- function getID() {\r
- return intval($this->id);\r
- }\r
-\r
- // (static)\r
- function createFromName($name) {\r
- return new TEMPLATE(TEMPLATE::getIdFromName($name));\r
- }\r
-\r
- // (static)\r
- function getIdFromName($name) {\r
- $query = 'SELECT tdnumber'\r
- . ' FROM '.sql_table('template_desc')\r
- . ' WHERE tdname="'.addslashes($name).'"';\r
- $res = sql_query($query);\r
- $obj = mysql_fetch_object($res);\r
- return $obj->tdnumber;\r
- }\r
-\r
- /**\r
- * Updates the general information about the template\r
- */\r
- function updateGeneralInfo($name, $desc) {\r
- $query = 'UPDATE '.sql_table('template_desc').' SET'\r
- . " tdname='" . addslashes($name) . "',"\r
- . " tddesc='" . addslashes($desc) . "'"\r
- . " WHERE tdnumber=" . $this->getID();\r
- sql_query($query);\r
- }\r
-\r
- /**\r
- * Updates the contents of one part of the template\r
- */\r
- function update($type, $content) {\r
- $id = $this->getID();\r
-\r
- // delete old thingie\r
- sql_query('DELETE FROM '.sql_table('template')." WHERE tpartname='". addslashes($type) ."' and tdesc=" . intval($id));\r
-\r
- // write new thingie\r
- if ($content) {\r
- sql_query('INSERT INTO '.sql_table('template')." SET tcontent='" . addslashes($content) . "', tpartname='" . addslashes($type) . "', tdesc=" . intval($id));\r
- }\r
- }\r
-\r
-\r
- /**\r
- * Deletes all template parts from the database\r
- */\r
- function deleteAllParts() {\r
- sql_query('DELETE FROM '.sql_table('template').' WHERE tdesc='.$this->getID());\r
- }\r
-\r
- /**\r
- * Creates a new template\r
- *\r
- * (static)\r
- */\r
- function createNew($name, $desc) {\r
- global $manager;\r
-\r
- // <temporary hack. for 3.4x ONLY !!>\r
- global $CONF;\r
- if (!$manager && $CONF['installscript']) {\r
- include_once($DIR_LIBS . 'MANAGER.php');\r
- $manager =& MANAGER::instance();\r
- }\r
- // </temporary hack. for 3.4x ONLY !!>\r
-\r
- $manager->notify(\r
- 'PreAddTemplate',\r
- array(\r
- 'name' => &$name,\r
- 'description' => &$desc\r
- )\r
- );\r
-\r
- sql_query('INSERT INTO '.sql_table('template_desc')." (tdname, tddesc) VALUES ('" . addslashes($name) . "','" . addslashes($desc) . "')");\r
- $newId = mysql_insert_id();\r
-\r
- $manager->notify(\r
- 'PostAddTemplate',\r
- array(\r
- 'templateid' => $newId,\r
- 'name' => $name,\r
- 'description' => $desc\r
- )\r
- );\r
-\r
- return $newId;\r
- }\r
-\r
-\r
-\r
- /**\r
- * Reads a template and returns an array with the parts.\r
- * (static)\r
- *\r
- * @param $name name of the template file\r
- */\r
- function read($name) {\r
- global $manager;\r
- $manager->notify(\r
- 'PreTemplateRead',\r
- array(\r
- 'template' => &$name\r
- )\r
- );\r
-\r
- $query = 'SELECT tpartname, tcontent'\r
- . ' FROM '.sql_table('template_desc').', '.sql_table('template')\r
- . ' WHERE tdesc=tdnumber and tdname="' . addslashes($name) . '"';\r
- $res = sql_query($query);\r
- while ($obj = mysql_fetch_object($res))\r
- $template[$obj->tpartname] = $obj->tcontent;\r
-\r
- // set locale according to template:\r
- if (isset($template['LOCALE']))\r
- setlocale(LC_TIME,$template['LOCALE']);\r
- else\r
- setlocale(LC_TIME,'');\r
-\r
- return $template;\r
- }\r
-\r
- /**\r
- * fills a template with values\r
- * (static)\r
- *\r
- * @param $template\r
- * Template to be used\r
- * @param $values\r
- * Array of all the values\r
- */\r
- function fill($template, $values) {\r
-\r
- if (sizeof($values) != 0) {\r
- // go through all the values\r
- for(reset($values); $key = key($values); next($values)) {\r
- $template = str_replace("<%$key%>",$values[$key],$template);\r
- }\r
- }\r
-\r
- // remove non matched template-tags\r
- return preg_replace('/<%[a-zA-Z]+%>/','',$template);\r
- }\r
-\r
- // returns true if there is a template with the given shortname\r
- // (static)\r
- function exists($name) {\r
- $r = sql_query('select * FROM '.sql_table('template_desc').' WHERE tdname="'.addslashes($name).'"');\r
- return (mysql_num_rows($r) != 0);\r
- }\r
-\r
- // returns true if there is a template with the given ID\r
- // (static)\r
- function existsID($id) {\r
- $r = sql_query('select * FROM '.sql_table('template_desc').' WHERE tdnumber='.intval($id));\r
- return (mysql_num_rows($r) != 0);\r
- }\r
-\r
- // (static)\r
- function getNameFromId($id) {\r
- return quickQuery('SELECT tdname as result FROM '.sql_table('template_desc').' WHERE tdnumber=' . intval($id));\r
- }\r
-\r
- // (static)\r
- function getDesc($id) {\r
- $query = 'SELECT tddesc FROM '.sql_table('template_desc').' WHERE tdnumber='. intval($id);\r
- $res = sql_query($query);\r
- $obj = mysql_fetch_object($res);\r
- return $obj->tddesc;\r
- }\r
-\r
-\r
-\r
-}\r
-\r
+class TEMPLATE {
+
+ var $id;
+
+ function TEMPLATE($templateid) {
+ $this->id = intval($templateid);
+ }
+
+ function getID() {
+ return intval($this->id);
+ }
+
+ // (static)
+ function createFromName($name) {
+ return new TEMPLATE(TEMPLATE::getIdFromName($name));
+ }
+
+ // (static)
+ function getIdFromName($name) {
+ $query = 'SELECT tdnumber'
+ . ' FROM '.sql_table('template_desc')
+ . ' WHERE tdname="'.addslashes($name).'"';
+ $res = sql_query($query);
+ $obj = sql_fetch_object($res);
+ return $obj->tdnumber;
+ }
+
+ /**
+ * Updates the general information about the template
+ */
+ function updateGeneralInfo($name, $desc) {
+ $query = 'UPDATE '.sql_table('template_desc').' SET'
+ . " tdname='" . addslashes($name) . "',"
+ . " tddesc='" . addslashes($desc) . "'"
+ . " WHERE tdnumber=" . $this->getID();
+ sql_query($query);
+ }
+
+ /**
+ * Updates the contents of one part of the template
+ */
+ function update($type, $content) {
+ $id = $this->getID();
+
+ // delete old thingie
+ sql_query('DELETE FROM '.sql_table('template')." WHERE tpartname='". addslashes($type) ."' and tdesc=" . intval($id));
+
+ // write new thingie
+ if ($content) {
+ sql_query('INSERT INTO '.sql_table('template')." SET tcontent='" . addslashes($content) . "', tpartname='" . addslashes($type) . "', tdesc=" . intval($id));
+ }
+ }
+
+
+ /**
+ * Deletes all template parts from the database
+ */
+ function deleteAllParts() {
+ sql_query('DELETE FROM '.sql_table('template').' WHERE tdesc='.$this->getID());
+ }
+
+ /**
+ * Creates a new template
+ *
+ * (static)
+ */
+ function createNew($name, $desc) {
+ global $manager;
+
+ $manager->notify(
+ 'PreAddTemplate',
+ array(
+ 'name' => &$name,
+ 'description' => &$desc
+ )
+ );
+
+ sql_query('INSERT INTO '.sql_table('template_desc')." (tdname, tddesc) VALUES ('" . addslashes($name) . "','" . addslashes($desc) . "')");
+ $newId = sql_insert_id();
+
+ $manager->notify(
+ 'PostAddTemplate',
+ array(
+ 'templateid' => $newId,
+ 'name' => $name,
+ 'description' => $desc
+ )
+ );
+
+ return $newId;
+ }
+
+
+
+ /**
+ * Reads a template and returns an array with the parts.
+ * (static)
+ *
+ * @param $name name of the template file
+ */
+ function read($name) {
+ global $manager;
+ $manager->notify(
+ 'PreTemplateRead',
+ array(
+ 'template' => &$name
+ )
+ );
+
+ $query = 'SELECT tpartname, tcontent'
+ . ' FROM '.sql_table('template_desc').', '.sql_table('template')
+ . ' WHERE tdesc=tdnumber and tdname="' . addslashes($name) . '"';
+ $res = sql_query($query);
+ while ($obj = sql_fetch_object($res))
+ $template[$obj->tpartname] = $obj->tcontent;
+
+ // set locale according to template:
+ if (isset($template['LOCALE']))
+ setlocale(LC_TIME,$template['LOCALE']);
+ else
+ setlocale(LC_TIME,'');
+
+ return $template;
+ }
+
+ /**
+ * fills a template with values
+ * (static)
+ *
+ * @param $template
+ * Template to be used
+ * @param $values
+ * Array of all the values
+ */
+ function fill($template, $values) {
+
+ if (sizeof($values) != 0) {
+ // go through all the values
+ for(reset($values); $key = key($values); next($values)) {
+ $template = str_replace("<%$key%>",$values[$key],$template);
+ }
+ }
+
+ // remove non matched template-tags
+ return preg_replace('/<%[a-zA-Z]+%>/','',$template);
+ }
+
+ // returns true if there is a template with the given shortname
+ // (static)
+ function exists($name) {
+ $r = sql_query('select * FROM '.sql_table('template_desc').' WHERE tdname="'.addslashes($name).'"');
+ return (sql_num_rows($r) != 0);
+ }
+
+ // returns true if there is a template with the given ID
+ // (static)
+ function existsID($id) {
+ $r = sql_query('select * FROM '.sql_table('template_desc').' WHERE tdnumber='.intval($id));
+ return (sql_num_rows($r) != 0);
+ }
+
+ // (static)
+ function getNameFromId($id) {
+ return quickQuery('SELECT tdname as result FROM '.sql_table('template_desc').' WHERE tdnumber=' . intval($id));
+ }
+
+ // (static)
+ function getDesc($id) {
+ $query = 'SELECT tddesc FROM '.sql_table('template_desc').' WHERE tdnumber='. intval($id);
+ $res = sql_query($query);
+ $obj = sql_fetch_object($res);
+ return $obj->tddesc;
+ }
+
+
+
+}
+
?>
\ No newline at end of file