OSDN Git Service

FIX: リファレンスにまつわるコードを修正
[nucleus-jp/nucleus-next.git] / nucleus / libs / TEMPLATE.php
index 49aef7d..10af5d1 100644 (file)
-<?php\r
-\r
-/*\r
- * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)\r
- * Copyright (C) 2002-2009 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-2009 The Nucleus Group\r
- * @version $Id: TEMPLATE.php 1470 2010-11-29 22:10:16Z ftruscot $\r
- */\r
-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="'.sql_real_escape_string($name).'"';\r
-               $res = sql_query($query);\r
-               $obj = sql_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='" . sql_real_escape_string($name) . "',"\r
-                          . " tddesc='" . sql_real_escape_string($desc) . "'"\r
-                          . " WHERE tdnumber=" . $this->getID();\r
-               sql_query($query);\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
-       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
-       /**\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
-               $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
-\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="' . sql_real_escape_string($name) . '"';\r
-               $res = sql_query($query);\r
-               while ($obj = sql_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="'.sql_real_escape_string($name).'"');\r
-               return (sql_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 (sql_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 = sql_fetch_object($res);\r
-               return $obj->tddesc;\r
-       }\r
-\r
-\r
-\r
-}\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);
+       }
+}