OSDN Git Service

「スキン編集」/管理画面用「スキン編集」 画面表示対応
[nucleus-jp/nucleus-next.git] / nucleus / libs / TEMPLATE.php
1 <?php\r
2 \r
3 /*\r
4  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)\r
5  * Copyright (C) 2002-2012 The Nucleus Group\r
6  *\r
7  * This program is free software; you can redistribute it and/or\r
8  * modify it under the terms of the GNU General Public License\r
9  * as published by the Free Software Foundation; either version 2\r
10  * of the License, or (at your option) any later version.\r
11  * (see nucleus/documentation/index.html#license for more info)\r
12  */\r
13 /**\r
14  * A class representing a template\r
15  *\r
16  * @license http://nucleuscms.org/license.txt GNU General Public License\r
17  * @copyright Copyright (C) 2002-2012 The Nucleus Group\r
18  * @version $Id: TEMPLATE.php 1726 2012-04-07 02:23:46Z sakamocchi $\r
19  */\r
20 class Template\r
21 {\r
22         /**\r
23          * Template::$id\r
24          */\r
25         private $id;\r
26         \r
27         /**\r
28          * Template::__construct()\r
29          * \r
30          * @param       integer $templateid     id for template\r
31          * @return      void\r
32          */\r
33         public function __construct($templateid)\r
34         {\r
35                 $this->id = intval($templateid);\r
36                 return;\r
37         }\r
38         \r
39         /**\r
40          * Template::getID()\r
41          * \r
42          * @param       void\r
43          * @return      integer id for this instance of Template class\r
44          */\r
45         public function getID()\r
46         {\r
47                 return (integer) $this->id;\r
48         }\r
49         \r
50         /**\r
51          * Template::createFromName()\r
52          * \r
53          * @statc\r
54          * @param       string  $name   template name\r
55          * @return      object  instance of Template class generated by the name\r
56          */\r
57         static public function createFromName($name)\r
58         {\r
59                 return new Template(Template::getIdFromName($name));\r
60         }\r
61         \r
62         /**\r
63          * Template::getIdFromName()\r
64          * \r
65          * @static\r
66          * @param       string  $name   template name\r
67          * @return      integer id for the template\r
68          */\r
69         static public function getIdFromName($name)\r
70         {\r
71                 $name = sql_real_escape_string($name);\r
72                 $query =  "SELECT tdnumber FROM %s WHERE tdname='%s';";\r
73                 $query = sprintf($query, sql_table('template_desc'), $name);\r
74                 $res = sql_query($query);\r
75                 $obj = sql_fetch_object($res);\r
76                 return $obj->tdnumber;\r
77         }\r
78         \r
79         /**\r
80          * Template::updateGeneralInfo()\r
81          * Updates the general information about the template\r
82          * \r
83          * @param       string  $name   template name\r
84          * @param       string  $desc   description for this template\r
85          * @return      void\r
86          */\r
87         public function updateGeneralInfo($name, $desc)\r
88         {\r
89                 $query =  "UPDATE %s SET tdname='%s', tddesc='%s' WHERE tdnumber=%d;";\r
90                 $query = sprintf($query, sql_table('template_desc'), sql_real_escape_string($name), sql_real_escape_string($desc), (integer) $this->getID());\r
91                 sql_query($query);\r
92                 return;\r
93         }\r
94         \r
95         /**\r
96          * Template::update()\r
97          * Updates the contents of one part of the template\r
98          * \r
99          * @param       String  $type   value for nucleus_template.tpartname\r
100          * @param       String  $content        value for nucleus_template.tcontent\r
101          * @return      Void\r
102          */\r
103         public function update($type, $content)\r
104         {\r
105                 // delete old thingie\r
106                 $query = "DELETE FROM %s WHERE tpartname='%s' and tdesc=%d";\r
107                 $query = sprintf($query, sql_table('template'), sql_real_escape_string($type), (integer) $this->getID());\r
108                 sql_query($query);\r
109                 \r
110                 // write new thingie\r
111                 if ( $content )\r
112                 {\r
113                         $query = "INSERT %s (tcontent, tpartname, tdesc) VALUE ('%s', '%s', %d)";\r
114                         $query = sprintf($query, sql_table('template'), sql_real_escape_string($content), sql_real_escape_string($type), (integer) $this->getID());\r
115                         sql_query($query);\r
116                 }\r
117                 return;\r
118         }\r
119         \r
120         /**\r
121          * Template::deleteAllParts()\r
122          * Deletes all template parts from the database\r
123          * \r
124          * @param       void\r
125          * @return      void\r
126          */\r
127         public function deleteAllParts()\r
128         {\r
129                 $query = "DELETE FROM %s WHERE tdesc=%d";\r
130                 $query = sprintf($query, sql_table('template'), (integer) $this->getID());\r
131                 sql_query($query);\r
132                 return;\r
133         }\r
134         \r
135         /**\r
136          * Template::createNew()\r
137          * Creates a new template\r
138          *\r
139          * @static\r
140          * @param       string  $name   name for new template\r
141          * @param       string  $desc   description for new template\r
142          * @return      integer id for new template\r
143          */\r
144         static public function createNew($name, $desc)\r
145         {\r
146                 global $manager;\r
147                 \r
148                 $manager->notify(\r
149                         'PreAddTemplate',\r
150                         array(\r
151                                 'name' => &$name,\r
152                                 'description' => &$desc\r
153                         )\r
154                 );\r
155                 \r
156                 sql_query('INSERT INTO '.sql_table('template_desc')." (tdname, tddesc) VALUES ('" . sql_real_escape_string($name) . "','" . sql_real_escape_string($desc) . "')");\r
157                 $newId = sql_insert_id();\r
158                 \r
159                 $manager->notify(\r
160                         'PostAddTemplate',\r
161                         array(\r
162                                 'templateid' => $newId,\r
163                                 'name' => $name,\r
164                                 'description' => $desc\r
165                         )\r
166                 );\r
167                 \r
168                 return $newId;\r
169         }\r
170         \r
171         /**\r
172          * Reads a template and returns an array with the parts.\r
173          *\r
174          * @static\r
175          * @param       string  $name name of the template file\r
176          * @return      array   template array\r
177          */\r
178         static public function read($name)\r
179         {\r
180                 global $manager;\r
181                 $manager->notify(\r
182                         'PreTemplateRead',\r
183                         array(\r
184                                 'template' => &$name\r
185                         )\r
186                 );\r
187                 \r
188                 $query = "SELECT tpartname, tcontent FROM %s, %s WHERE tdesc=tdnumber and tdname='%s'";\r
189                 $query = sprintf($query, sql_table('template_desc'), sql_table('template'), sql_real_escape_string($name));\r
190                 $res = sql_query($query);\r
191 \r
192                 $template = array();\r
193                 while ($obj = sql_fetch_object($res))\r
194                 {\r
195                         $template[$obj->tpartname] = $obj->tcontent;\r
196                 }\r
197                 \r
198                 /*\r
199                  * TODO: this is appropriate or not?\r
200                  */\r
201                 if ( array_key_exists('LOCALE', $template) && !empty($template['LOCALE']) )\r
202                 {\r
203                         setlocale(LC_TIME, $template['LOCALE']);\r
204                 }\r
205                 else\r
206                 {\r
207                         setlocale(LC_TIME,'');\r
208                 }\r
209                 \r
210                 return $template;\r
211         }\r
212         \r
213         /**\r
214          * fills a template with values\r
215          * \r
216          * @static\r
217          * @param       string  $template       Template to be used\r
218          * @param       array   $values         Array of all the values\r
219          * @return      string  string filled with tag contents\r
220          */\r
221         static public function fill($template, $values)\r
222         {\r
223                 \r
224                 if ( sizeof($values) != 0 )\r
225                 {\r
226                         foreach ( $values as $key => $value )\r
227                         {\r
228                                 $template = preg_replace('#<%' . preg_quote($key, '#') . '%>#', $value, $template);\r
229                         }\r
230                 }\r
231                 \r
232                 // remove non matched template-tags\r
233                 return preg_replace('#<%([a-zA-Z]+)?%>#', '', $template);\r
234         }\r
235         \r
236         /**\r
237          * Template::exists()\r
238          * returns true if there is a template with the given shortname\r
239          * \r
240          * @static\r
241          * @param       string  $name   template name\r
242          * @return      boolean exists or not\r
243          */\r
244         static public function exists($name)\r
245         {\r
246                 $query = "SELECT * FROM %s WHERE tdname='%s';";\r
247                 $query = sprintf($query, sql_table('template_desc'), sql_real_escape_string($name));\r
248                 $r = sql_query($query);\r
249                 return (sql_num_rows($r) != 0);\r
250         }\r
251         \r
252         /**\r
253          * Template::existsID()\r
254          * returns true if there is a template with the given ID\r
255          * \r
256          * @static\r
257          * @param       integer $id     id for template\r
258          * @return      bookean exists or not\r
259          */\r
260         static public function existsID($id)\r
261         {\r
262                 $query = "SELECT * FROM %s WHERE tdnumber=%d;";\r
263                 $query = sprintf($query, sql_table('template_desc'), (integer) $id);\r
264                 $r = sql_query($query);\r
265                 return (sql_num_rows($r) != 0);\r
266         }\r
267         \r
268         /**\r
269          * Template::getNameFromId()\r
270          * \r
271          * @static\r
272          * @param       integer $id     id for template\r
273          * @return      object  sql object\r
274          */\r
275         static public function getNameFromId($id)\r
276         {\r
277                 $query = "SELECT tdname as result FROM %s WHERE tdnumber=%d";\r
278                 $query = sprintf($query, sql_table('template_desc'), (integer) $id);\r
279                 return quickQuery($query);\r
280         }\r
281         \r
282         /**\r
283          * Template::getDesc()\r
284          * \r
285          * @static\r
286          * @param       integer $id     id for template\r
287          * @return      string  description for the template\r
288          */\r
289         static public function getDesc($id)\r
290         {\r
291                 $query = "SELECT tddesc FROM %s WHERE tdnumber=%d;";\r
292                 $query = sprintf($query, sql_table('template_desc'), (integer) $id);\r
293                 $res = sql_query($query);\r
294                 $obj = sql_fetch_object($res);\r
295                 return $obj->tddesc;\r
296         }\r
297 }\r