OSDN Git Service

8421d64a71898f425f88a9a254175703e95e63f8
[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 = DB::quoteValue($name);\r
72                 $query = "SELECT tdnumber FROM %s WHERE tdname=%s";\r
73                 $query = sprintf($query, sql_table('template_desc'), $name);\r
74                 return DB::getValue($query);\r
75         }\r
76         \r
77         /**\r
78          * Template::updateGeneralInfo()\r
79          * Updates the general information about the template\r
80          * \r
81          * @param       string  $name   template name\r
82          * @param       string  $desc   description for this template\r
83          * @return      void\r
84          */\r
85         public function updateGeneralInfo($name, $desc)\r
86         {\r
87                 $query =  "UPDATE %s SET tdname=%s, tddesc=%s WHERE tdnumber=%d";\r
88                 $query = sprintf($query, sql_table('template_desc'), DB::quoteValue($name), DB::quoteValue($desc), (integer) $this->getID());\r
89                 DB::execute($query);\r
90                 return;\r
91         }\r
92         \r
93         /**\r
94          * Template::update()\r
95          * Updates the contents of one part of the template\r
96          * \r
97          * @param       String  $type   value for nucleus_template.tpartname\r
98          * @param       String  $content        value for nucleus_template.tcontent\r
99          * @return      Void\r
100          */\r
101         public function update($type, $content)\r
102         {\r
103                 // delete old thingie\r
104                 $query = "DELETE FROM %s WHERE tpartname=%s and tdesc=%d";\r
105                 $query = sprintf($query, sql_table('template'), DB::quoteValue($type), (integer) $this->getID());\r
106                 DB::execute($query);\r
107                 \r
108                 // write new thingie\r
109                 if ( $content )\r
110                 {\r
111                         $query = "INSERT %s (tcontent, tpartname, tdesc) VALUE (%s, %s, %d)";\r
112                         $query = sprintf($query, sql_table('template'), DB::quoteValue($content), DB::quoteValue($type), (integer) $this->getID());\r
113                         DB::execute($query);\r
114                 }\r
115                 return;\r
116         }\r
117         \r
118         /**\r
119          * Template::deleteAllParts()\r
120          * Deletes all template parts from the database\r
121          * \r
122          * @param       void\r
123          * @return      void\r
124          */\r
125         public function deleteAllParts()\r
126         {\r
127                 $query = "DELETE FROM %s WHERE tdesc=%d";\r
128                 $query = sprintf($query, sql_table('template'), (integer) $this->getID());\r
129                 DB::execute($query);\r
130                 return;\r
131         }\r
132         \r
133         /**\r
134          * Template::createNew()\r
135          * Creates a new template\r
136          *\r
137          * @static\r
138          * @param       string  $name   name for new template\r
139          * @param       string  $desc   description for new template\r
140          * @return      integer id for new template\r
141          */\r
142         static public function createNew($name, $desc)\r
143         {\r
144                 global $manager;\r
145                 \r
146                 $manager->notify(\r
147                         'PreAddTemplate',\r
148                         array(\r
149                                 'name' => &$name,\r
150                                 'description' => &$desc\r
151                         )\r
152                 );\r
153                 \r
154                 DB::execute('INSERT INTO '.sql_table('template_desc').' (tdname, tddesc) VALUES (' . DB::quoteValue($name) . ',' . DB::quoteValue($desc) . ')');\r
155                 $newId = DB::getInsertId();\r
156                 \r
157                 $manager->notify(\r
158                         'PostAddTemplate',\r
159                         array(\r
160                                 'templateid' => $newId,\r
161                                 'name' => $name,\r
162                                 'description' => $desc\r
163                         )\r
164                 );\r
165                 \r
166                 return $newId;\r
167         }\r
168         \r
169         /**\r
170          * Reads a template and returns an array with the parts.\r
171          *\r
172          * @static\r
173          * @param       string  $name name of the template file\r
174          * @return      array   template array\r
175          */\r
176         static public function read($name)\r
177         {\r
178                 global $manager;\r
179                 $manager->notify(\r
180                         'PreTemplateRead',\r
181                         array(\r
182                                 'template' => &$name\r
183                         )\r
184                 );\r
185                 \r
186                 $query = "SELECT tpartname, tcontent FROM %s, %s WHERE tdesc=tdnumber and tdname=%s";\r
187                 $query = sprintf($query, sql_table('template_desc'), sql_table('template'), DB::quoteValue($name));\r
188                 $res = DB::getResult($query);\r
189                 \r
190                 foreach ( $res as $row )\r
191                 {\r
192                         $template[$row['tpartname']] = $row['tcontent'];\r
193                 }\r
194                 \r
195                 /*\r
196                  * TODO: this is appropriate or not?\r
197                  */\r
198                 if ( array_key_exists('LOCALE', $template) && !empty($template['LOCALE']) )\r
199                 {\r
200                         setlocale(LC_TIME, $template['LOCALE']);\r
201                 }\r
202                 else\r
203                 {\r
204                         setlocale(LC_TIME,'');\r
205                 }\r
206                 \r
207                 return $template;\r
208         }\r
209         \r
210         /**\r
211          * fills a template with values\r
212          * \r
213          * @static\r
214          * @param       string  $template       Template to be used\r
215          * @param       array   $values         Array of all the values\r
216          * @return      string  string filled with tag contents\r
217          */\r
218         static public function fill($template, $values)\r
219         {\r
220                 \r
221                 if ( sizeof($values) != 0 )\r
222                 {\r
223                         foreach ( $values as $key => $value )\r
224                         {\r
225                                 $template = preg_replace('#<%' . preg_quote($key, '#') . '%>#', $value, $template);\r
226                         }\r
227                 }\r
228                 \r
229                 // remove non matched template-tags\r
230                 return preg_replace('#<%([a-zA-Z]+)?%>#', '', $template);\r
231         }\r
232         \r
233         /**\r
234          * TEMPLATE::exists()\r
235          * returns true if there is a template with the given shortname\r
236          * \r
237          * @static\r
238          * @param       string  $name   template name\r
239          * @return      boolean exists or not\r
240          */\r
241         static public function exists($name)\r
242         {\r
243                 $query = "SELECT * FROM %s WHERE tdname=%s";\r
244                 $query = sprintf($query, sql_table('template_desc'), DB::quoteValue($name));\r
245                 $r = DB::getResult($query);\r
246                 return ($r->rowCount() != 0);\r
247         }\r
248         \r
249         /**\r
250          * TEMPLATE::existsID()\r
251          * returns true if there is a template with the given ID\r
252          * \r
253          * @static\r
254          * @param       integer $id     id for template\r
255          * @return      bookean exists or not\r
256          */\r
257         static public function existsID($id)\r
258         {\r
259                 $query = "SELECT * FROM %s WHERE tdnumber=%d";\r
260                 $query = sprintf($query, sql_table('template_desc'), (integer) $id);\r
261                 $r = DB::getResult($query);\r
262                 return ($r->rowCount() != 0);\r
263         }\r
264         \r
265         /**\r
266          * TEMPLATE::getNameFromId()\r
267          * \r
268          * @static\r
269          * @param       integer $id     id for template\r
270          * @return      object  sql object\r
271          */\r
272         static public function getNameFromId($id)\r
273         {\r
274                 $query = "SELECT tdname as result FROM %s WHERE tdnumber=%d";\r
275                 $query = sprintf($query, sql_table('template_desc'), (integer) $id);\r
276                 return DB::getValue($query);\r
277         }\r
278         \r
279         /**\r
280          * TEMPLATE::getDesc()\r
281          * \r
282          * @static\r
283          * @param       integer $id     id for template\r
284          * @return      string  description for the template\r
285          */\r
286         static public function getDesc($id)\r
287         {\r
288                 $query = "SELECT tddesc FROM %s WHERE tdnumber=%d";\r
289                 $query = sprintf($query, sql_table('template_desc'), (integer) $id);\r
290                 return DB::getValue($query);\r
291         }\r
292 }\r