OSDN Git Service

Merge branch 'skinnable-master' of sakamocchi@git.sourceforge.jp:/gitroot/nucleus...
[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 INTO %s (tcontent, tpartname, tdesc) VALUES (%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                 $template = array();\r
191                 foreach ( $res as $row )\r
192                 {\r
193                         $template[$row['tpartname']] = $row['tcontent'];\r
194                 }\r
195                 \r
196                 /*\r
197                  * TODO: this is appropriate or not?\r
198                  */\r
199                 if ( array_key_exists('LOCALE', $template) && !empty($template['LOCALE']) )\r
200                 {\r
201                         setlocale(LC_TIME, $template['LOCALE']);\r
202                 }\r
203                 else\r
204                 {\r
205                         setlocale(LC_TIME,'');\r
206                 }\r
207                 \r
208                 return $template;\r
209         }\r
210         \r
211         /**\r
212          * fills a template with values\r
213          * \r
214          * @static\r
215          * @param       string  $template       Template to be used\r
216          * @param       array   $values         Array of all the values\r
217          * @return      string  string filled with tag contents\r
218          */\r
219         static public function fill($template, $values)\r
220         {\r
221                 \r
222                 if ( sizeof($values) != 0 )\r
223                 {\r
224                         foreach ( $values as $key => $value )\r
225                         {\r
226                                 $template = preg_replace('#<%' . preg_quote($key, '#') . '%>#', $value, $template);\r
227                         }\r
228                 }\r
229                 \r
230                 // remove non matched template-tags\r
231                 return preg_replace('#<%([a-zA-Z]+)?%>#', '', $template);\r
232         }\r
233         \r
234         /**\r
235          * Template::exists()\r
236          * returns true if there is a template with the given shortname\r
237          * \r
238          * @static\r
239          * @param       string  $name   template name\r
240          * @return      boolean exists or not\r
241          */\r
242         static public function exists($name)\r
243         {\r
244                 $query = "SELECT * FROM %s WHERE tdname=%s";\r
245                 $query = sprintf($query, sql_table('template_desc'), DB::quoteValue($name));\r
246                 $r = DB::getResult($query);\r
247                 return ($r->rowCount() != 0);\r
248         }\r
249         \r
250         /**\r
251          * Template::existsID()\r
252          * returns true if there is a template with the given ID\r
253          * \r
254          * @static\r
255          * @param       integer $id     id for template\r
256          * @return      bookean exists or not\r
257          */\r
258         static public function existsID($id)\r
259         {\r
260                 $query = "SELECT * FROM %s WHERE tdnumber=%d";\r
261                 $query = sprintf($query, sql_table('template_desc'), (integer) $id);\r
262                 $r = DB::getResult($query);\r
263                 return ($r->rowCount() != 0);\r
264         }\r
265         \r
266         /**\r
267          * Template::getNameFromId()\r
268          * \r
269          * @static\r
270          * @param       integer $id     id for template\r
271          * @return      object  sql object\r
272          */\r
273         static public function getNameFromId($id)\r
274         {\r
275                 $query = "SELECT tdname as result FROM %s WHERE tdnumber=%d";\r
276                 $query = sprintf($query, sql_table('template_desc'), (integer) $id);\r
277                 return DB::getValue($query);\r
278         }\r
279         \r
280         /**\r
281          * Template::getDesc()\r
282          * \r
283          * @static\r
284          * @param       integer $id     id for template\r
285          * @return      string  description for the template\r
286          */\r
287         static public function getDesc($id)\r
288         {\r
289                 $query = "SELECT tddesc FROM %s WHERE tdnumber=%d";\r
290                 $query = sprintf($query, sql_table('template_desc'), (integer) $id);\r
291                 return DB::getValue($query);\r
292         }\r
293 }\r