OSDN Git Service

FIX: リファレンスにまつわるコードを修正
[nucleus-jp/nucleus-next.git] / nucleus / libs / TEMPLATE.php
index 2087631..10af5d1 100644 (file)
-<?php\r
-\r
-/*\r
- * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)\r
- * Copyright (C) 2002-2012 The Nucleus Group\r
- *\r
- * This program is free software; you can redistribute it and/or\r
- * modify it under the terms of the GNU General Public License\r
- * as published by the Free Software Foundation; either version 2\r
- * of the License, or (at your option) any later version.\r
- * (see nucleus/documentation/index.html#license for more info)\r
- */\r
-/**\r
- * A class representing a template\r
- *\r
- * @license http://nucleuscms.org/license.txt GNU General Public License\r
- * @copyright Copyright (C) 2002-2012 The Nucleus Group\r
- * @version $Id: TEMPLATE.php 1726 2012-04-07 02:23:46Z sakamocchi $\r
- */\r
-class Template\r
-{\r
-       /**\r
-        * Template::$id\r
-        */\r
-       private $id;\r
-       \r
-       /**\r
-        * Template::__construct()\r
-        * \r
-        * @param       integer $templateid     id for template\r
-        * @return      void\r
-        */\r
-       public function __construct($templateid)\r
-       {\r
-               $this->id = intval($templateid);\r
-               return;\r
-       }\r
-       \r
-       /**\r
-        * Template::getID()\r
-        * \r
-        * @param       void\r
-        * @return      integer id for this instance of Template class\r
-        */\r
-       public function getID()\r
-       {\r
-               return (integer) $this->id;\r
-       }\r
-       \r
-       /**\r
-        * Template::createFromName()\r
-        * \r
-        * @statc\r
-        * @param       string  $name   template name\r
-        * @return      object  instance of Template class generated by the name\r
-        */\r
-       static public function createFromName($name)\r
-       {\r
-               return new Template(Template::getIdFromName($name));\r
-       }\r
-       \r
-       /**\r
-        * Template::getIdFromName()\r
-        * \r
-        * @static\r
-        * @param       string  $name   template name\r
-        * @return      integer id for the template\r
-        */\r
-       static public function getIdFromName($name)\r
-       {\r
-               $name = sql_real_escape_string($name);\r
-               $query =  "SELECT tdnumber FROM %s WHERE tdname='%s';";\r
-               $query = sprintf($query, sql_table('template_desc'), $name);\r
-               $res = sql_query($query);\r
-               $obj = sql_fetch_object($res);\r
-               return $obj->tdnumber;\r
-       }\r
-       \r
-       /**\r
-        * Template::updateGeneralInfo()\r
-        * Updates the general information about the template\r
-        * \r
-        * @param       string  $name   template name\r
-        * @param       string  $desc   description for this template\r
-        * @return      void\r
-        */\r
-       public function updateGeneralInfo($name, $desc)\r
-       {\r
-               $query =  "UPDATE %s SET tdname='%s', tddesc='%s' WHERE tdnumber=%d;";\r
-               $query = sprintf($query, sql_table('template_desc'), sql_real_escape_string($name), sql_real_escape_string($desc), (integer) $this->getID());\r
-               sql_query($query);\r
-               return;\r
-       }\r
-       \r
-       /**\r
-        * Template::update()\r
-        * Updates the contents of one part of the template\r
-        * \r
-        * @param       String  $type   value for nucleus_template.tpartname\r
-        * @param       String  $content        value for nucleus_template.tcontent\r
-        * @return      Void\r
-        */\r
-       public function update($type, $content)\r
-       {\r
-               // delete old thingie\r
-               $query = "DELETE FROM %s WHERE tpartname='%s' and tdesc=%d";\r
-               $query = sprintf($query, sql_table('template'), sql_real_escape_string($type), (integer) $this->getID());\r
-               sql_query($query);\r
-               \r
-               // write new thingie\r
-               if ( $content )\r
-               {\r
-                       $query = "INSERT %s (tcontent, tpartname, tdesc) VALUE ('%s', '%s', %d)";\r
-                       $query = sprintf($query, sql_table('template'), sql_real_escape_string($content), sql_real_escape_string($type), (integer) $this->getID());\r
-                       sql_query($query);\r
-               }\r
-               return;\r
-       }\r
-       \r
-       /**\r
-        * Template::deleteAllParts()\r
-        * Deletes all template parts from the database\r
-        * \r
-        * @param       void\r
-        * @return      void\r
-        */\r
-       public function deleteAllParts()\r
-       {\r
-               $query = "DELETE FROM %s WHERE tdesc=%d";\r
-               $query = sprintf($query, sql_table('template'), (integer) $this->getID());\r
-               sql_query($query);\r
-               return;\r
-       }\r
-       \r
-       /**\r
-        * Template::createNew()\r
-        * Creates a new template\r
-        *\r
-        * @static\r
-        * @param       string  $name   name for new template\r
-        * @param       string  $desc   description for new template\r
-        * @return      integer id for new template\r
-        */\r
-       static public function createNew($name, $desc)\r
-       {\r
-               global $manager;\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 ('" . sql_real_escape_string($name) . "','" . sql_real_escape_string($desc) . "')");\r
-               $newId = sql_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
-        * Reads a template and returns an array with the parts.\r
-        *\r
-        * @static\r
-        * @param       string  $name name of the template file\r
-        * @return      array   template array\r
-        */\r
-       static public function read($name)\r
-       {\r
-               global $manager;\r
-               $manager->notify(\r
-                       'PreTemplateRead',\r
-                       array(\r
-                               'template' => &$name\r
-                       )\r
-               );\r
-               \r
-               $query = "SELECT tpartname, tcontent FROM %s, %s WHERE tdesc=tdnumber and tdname='%s'";\r
-               $query = sprintf($query, sql_table('template_desc'), sql_table('template'), sql_real_escape_string($name));\r
-               $res = sql_query($query);\r
-               \r
-               while ($obj = sql_fetch_object($res))\r
-               {\r
-                       $template[$obj->tpartname] = $obj->tcontent;\r
-               }\r
-               \r
-               /*\r
-                * TODO: this is appropriate or not?\r
-                */\r
-               if ( array_key_exists('LOCALE', $template) && !empty($template['LOCALE']) )\r
-               {\r
-                       setlocale(LC_TIME, $template['LOCALE']);\r
-               }\r
-               else\r
-               {\r
-                       setlocale(LC_TIME,'');\r
-               }\r
-               \r
-               return $template;\r
-       }\r
-       \r
-       /**\r
-        * fills a template with values\r
-        * \r
-        * @static\r
-        * @param       string  $template       Template to be used\r
-        * @param       array   $values         Array of all the values\r
-        * @return      string  string filled with tag contents\r
-        */\r
-       static public function fill($template, $values)\r
-       {\r
-               \r
-               if ( sizeof($values) != 0 )\r
-               {\r
-                       foreach ( $values as $key => $value )\r
-                       {\r
-                               $template = preg_replace('#<%' . preg_quote($key, '#') . '%>#', $value, $template);\r
-                       }\r
-               }\r
-               \r
-               // remove non matched template-tags\r
-               return preg_replace('#<%([a-zA-Z]+)?%>#', '', $template);\r
-       }\r
-       \r
-       /**\r
-        * TEMPLATE::exists()\r
-        * returns true if there is a template with the given shortname\r
-        * \r
-        * @static\r
-        * @param       string  $name   template name\r
-        * @return      boolean exists or not\r
-        */\r
-       static public function exists($name)\r
-       {\r
-               $query = "SELECT * FROM %s WHERE tdname='%s';";\r
-               $query = sprintf($query, sql_table('template_desc'), sql_real_escape_string($name));\r
-               $r = sql_query($query);\r
-               return (sql_num_rows($r) != 0);\r
-       }\r
-       \r
-       /**\r
-        * TEMPLATE::existsID()\r
-        * returns true if there is a template with the given ID\r
-        * \r
-        * @static\r
-        * @param       integer $id     id for template\r
-        * @return      bookean exists or not\r
-        */\r
-       static public function existsID($id)\r
-       {\r
-               $query = "SELECT * FROM %s WHERE tdnumber=%d;";\r
-               $query = sprintf($query, sql_table('template_desc'), (integer) $id);\r
-               $r = sql_query($query);\r
-               return (sql_num_rows($r) != 0);\r
-       }\r
-       \r
-       /**\r
-        * TEMPLATE::getNameFromId()\r
-        * \r
-        * @static\r
-        * @param       integer $id     id for template\r
-        * @return      object  sql object\r
-        */\r
-       static public function getNameFromId($id)\r
-       {\r
-               $query = "SELECT tdname as result FROM %s WHERE tdnumber=%d";\r
-               $query = sprintf($query, sql_table('template_desc'), (integer) $id);\r
-               return quickQuery($query);\r
-       }\r
-       \r
-       /**\r
-        * TEMPLATE::getDesc()\r
-        * \r
-        * @static\r
-        * @param       integer $id     id for template\r
-        * @return      string  description for the template\r
-        */\r
-       static public function getDesc($id)\r
-       {\r
-               $query = "SELECT tddesc FROM %s WHERE tdnumber=%d;";\r
-               $query = sprintf($query, sql_table('template_desc'), (integer) $id);\r
-               $res = sql_query($query);\r
-               $obj = sql_fetch_object($res);\r
-               return $obj->tddesc;\r
-       }\r
-}\r
+<?php
+
+/*
+ * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
+ * Copyright (C) 2002-2009 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
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ * (see nucleus/documentation/index.html#license for more info)
+ */
+/**
+ * A class representing a template
+ *
+ * @license http://nucleuscms.org/license.txt GNU General Public License
+ * @copyright Copyright (C) 2002-2009 The Nucleus Group
+ * @version $Id: TEMPLATE.php 1880 2012-06-17 07:48:14Z sakamocchi $
+ */
+class Template
+{
+       /**
+        * Template::$id
+        */
+       private $id;
+       
+       /**
+        * Template::__construct()
+        * 
+        * @param       integer $templateid     id for template
+        * @return      void
+        */
+       public function __construct($templateid)
+       {
+               $this->id = intval($templateid);
+               return;
+       }
+       
+       /**
+        * Template::getID()
+        * 
+        * @param       void
+        * @return      integer id for this instance of Template class
+        */
+       public function getID()
+       {
+               return (integer) $this->id;
+       }
+       
+       /**
+        * Template::createFromName()
+        * 
+        * @statc
+        * @param       string  $name   template name
+        * @return      object  instance of Template class generated by the name
+        */
+       static public function createFromName($name)
+       {
+               return new Template(Template::getIdFromName($name));
+       }
+       
+       /**
+        * Template::getIdFromName()
+        * 
+        * @static
+        * @param       string  $name   template name
+        * @return      integer id for the template
+        */
+       static public function getIdFromName($name)
+       {
+               $name = DB::quoteValue($name);
+               $query = "SELECT tdnumber FROM %s WHERE tdname=%s";
+               $query = sprintf($query, sql_table('template_desc'), $name);
+               return DB::getValue($query);
+       }
+       
+       /**
+        * Template::updateGeneralInfo()
+        * Updates the general information about the template
+        * 
+        * @param       string  $name   template name
+        * @param       string  $desc   description for this template
+        * @return      void
+        */
+       public function updateGeneralInfo($name, $desc)
+       {
+               $query =  "UPDATE %s SET tdname=%s, tddesc=%s WHERE tdnumber=%d";
+               $query = sprintf($query, sql_table('template_desc'), DB::quoteValue($name), DB::quoteValue($desc), (integer) $this->getID());
+               DB::execute($query);
+               return;
+       }
+       
+       /**
+        * Template::update()
+        * Updates the contents of one part of the template
+        * 
+        * @param       String  $type   value for nucleus_template.tpartname
+        * @param       String  $content        value for nucleus_template.tcontent
+        * @return      Void
+        */
+       public function update($type, $content)
+       {
+               // delete old thingie
+               $query = "DELETE FROM %s WHERE tpartname=%s and tdesc=%d";
+               $query = sprintf($query, sql_table('template'), DB::quoteValue($type), (integer) $this->getID());
+               DB::execute($query);
+               
+               // write new thingie
+               if ( $content )
+               {
+                       $query = "INSERT INTO %s (tcontent, tpartname, tdesc) VALUES (%s, %s, %d)";
+                       $query = sprintf($query, sql_table('template'), DB::quoteValue($content), DB::quoteValue($type), (integer) $this->getID());
+                       DB::execute($query);
+               }
+               return;
+       }
+       
+       /**
+        * Template::deleteAllParts()
+        * Deletes all template parts from the database
+        * 
+        * @param       void
+        * @return      void
+        */
+       public function deleteAllParts()
+       {
+               $query = "DELETE FROM %s WHERE tdesc=%d";
+               $query = sprintf($query, sql_table('template'), (integer) $this->getID());
+               DB::execute($query);
+               return;
+       }
+       
+       /**
+        * Template::createNew()
+        * Creates a new template
+        *
+        * @static
+        * @param       string  $name   name for new template
+        * @param       string  $desc   description for new template
+        * @return      integer id for new template
+        */
+       static public function createNew($name, $desc)
+       {
+               global $manager;
+
+               $data = array(
+                       'name'                  => &$name,
+                       'description'   => &$desc
+               );
+               $manager->notify('PreAddTemplate', $data);
+               
+               DB::execute('INSERT INTO '.sql_table('template_desc').' (tdname, tddesc) VALUES (' . DB::quoteValue($name) . ',' . DB::quoteValue($desc) . ')');
+               $newId = DB::getInsertId();
+
+               $data = array(
+                       'templateid'    => $newId,
+                       'name'                  => $name,
+                       'description'   => $desc
+               );
+               $manager->notify('PostAddTemplate', $data);
+               
+               return $newId;
+       }
+       
+       /**
+        * Reads a template and returns an array with the parts.
+        *
+        * @static
+        * @param       string  $name name of the template file
+        * @return      array   template array
+        */
+       static public function read($name)
+       {
+               global $manager;
+               $data = array('template' => &$name);
+               $manager->notify('PreTemplateRead', $data);
+               
+               $query = "SELECT tpartname, tcontent FROM %s, %s WHERE tdesc=tdnumber and tdname=%s";
+               $query = sprintf($query, sql_table('template_desc'), sql_table('template'), DB::quoteValue($name));
+               $res = DB::getResult($query);
+               
+               $template = array();
+               foreach ( $res as $row )
+               {
+                       $template[$row['tpartname']] = $row['tcontent'];
+               }
+               
+               return $template;
+       }
+       
+       /**
+        * fills a template with values
+        * 
+        * @static
+        * @param       string  $template       Template to be used
+        * @param       array   $values         Array of all the values
+        * @return      string  string filled with tag contents
+        */
+       static public function fill($template, $values)
+       {
+               
+               if ( sizeof($values) != 0 )
+               {
+                       foreach ( $values as $key => $value )
+                       {
+                               $template = preg_replace('#<%' . preg_quote($key, '#') . '%>#', $value, $template);
+                       }
+               }
+               
+               // remove non matched template-tags
+               return preg_replace('#<%([a-zA-Z]+)?%>#', '', $template);
+       }
+       
+       /**
+        * Template::exists()
+        * returns true if there is a template with the given shortname
+        * 
+        * @static
+        * @param       string  $name   template name
+        * @return      boolean exists or not
+        */
+       static public function exists($name)
+       {
+               $query = "SELECT * FROM %s WHERE tdname=%s";
+               $query = sprintf($query, sql_table('template_desc'), DB::quoteValue($name));
+               $r = DB::getResult($query);
+               return ($r->rowCount() != 0);
+       }
+       
+       /**
+        * Template::existsID()
+        * returns true if there is a template with the given ID
+        * 
+        * @static
+        * @param       integer $id     id for template
+        * @return      bookean exists or not
+        */
+       static public function existsID($id)
+       {
+               $query = "SELECT * FROM %s WHERE tdnumber=%d";
+               $query = sprintf($query, sql_table('template_desc'), (integer) $id);
+               $r = DB::getResult($query);
+               return ($r->rowCount() != 0);
+       }
+       
+       /**
+        * Template::getNameFromId()
+        * 
+        * @static
+        * @param       integer $id     id for template
+        * @return      object  sql object
+        */
+       static public function getNameFromId($id)
+       {
+               $query = "SELECT tdname as result FROM %s WHERE tdnumber=%d";
+               $query = sprintf($query, sql_table('template_desc'), (integer) $id);
+               return DB::getValue($query);
+       }
+       
+       /**
+        * Template::getDesc()
+        * 
+        * @static
+        * @param       integer $id     id for template
+        * @return      string  description for the template
+        */
+       static public function getDesc($id)
+       {
+               $query = "SELECT tddesc FROM %s WHERE tdnumber=%d";
+               $query = sprintf($query, sql_table('template_desc'), (integer) $id);
+               return DB::getValue($query);
+       }
+}