OSDN Git Service

Merge branch 'skinnable-master'
[nucleus-jp/nucleus-next.git] / nucleus / libs / TEMPLATE.php
index 10af5d1..ea7a6ac 100644 (file)
@@ -1,3 +1,297 @@
+<<<<<<< HEAD
+<?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 = DB::quoteValue($name);\r
+               $query = "SELECT tdnumber FROM %s WHERE tdname=%s";\r
+               $query = sprintf($query, sql_table('template_desc'), $name);\r
+               return DB::getValue($query);\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'), DB::quoteValue($name), DB::quoteValue($desc), (integer) $this->getID());\r
+               DB::execute($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'), DB::quoteValue($type), (integer) $this->getID());\r
+               DB::execute($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'), DB::quoteValue($content), DB::quoteValue($type), (integer) $this->getID());\r
+                       DB::execute($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
+               DB::execute($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
+               DB::execute('INSERT INTO '.sql_table('template_desc').' (tdname, tddesc) VALUES (' . DB::quoteValue($name) . ',' . DB::quoteValue($desc) . ')');\r
+               $newId = DB::getInsertId();\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'), DB::quoteValue($name));\r
+               $res = DB::getResult($query);\r
+               \r
+               foreach ( $res as $row )\r
+               {\r
+                       $template[$row['tpartname']] = $row['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'), DB::quoteValue($name));\r
+               $r = DB::getResult($query);\r
+               return ($r->rowCount() != 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 = DB::getResult($query);\r
+               return ($r->rowCount() != 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 DB::getValue($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
+               return DB::getValue($query);\r
+       }\r
+}\r
+=======
 <?php
 
 /*
@@ -271,3 +565,4 @@ class Template
                return DB::getValue($query);
        }
 }
+>>>>>>> skinnable-master