OSDN Git Service

translated
[nucleus-jp/nucleus-jp-ancient.git] / utf8 / 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-2004 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   * A class representing a template\r
14   */\r
15 class TEMPLATE {\r
16 \r
17         var $id;\r
18         \r
19         function TEMPLATE($templateid) {\r
20                 $this->id = intval($templateid);\r
21         }\r
22         \r
23         function getID() {\r
24                 return intval($this->id);\r
25         }\r
26         \r
27         // (static)\r
28         function createFromName($name) {\r
29                 return new TEMPLATE(TEMPLATE::getIdFromName($name));\r
30         }\r
31         \r
32         // (static)\r
33         function getIdFromName($name) {\r
34                 $query =  'SELECT tdnumber'\r
35                        . ' FROM '.sql_table('template_desc')\r
36                        . ' WHERE tdname="'.addslashes($name).'"';\r
37                 $res = sql_query($query);\r
38                 $obj = mysql_fetch_object($res);\r
39                 return $obj->tdnumber;  \r
40         }\r
41 \r
42         /**\r
43          * Updates the general information about the template\r
44          */\r
45         function updateGeneralInfo($name, $desc) {\r
46                 $query =  'UPDATE '.sql_table('template_desc').' SET'\r
47                        . " tdname='" . addslashes($name) . "',"\r
48                        . " tddesc='" . addslashes($desc) . "'"\r
49                        . " WHERE tdnumber=" . $this->getID();\r
50                 sql_query($query);              \r
51         }\r
52         \r
53         /**\r
54          * Updates the contents of one part of the template\r
55          */\r
56         function update($type, $content) {\r
57                 $id = $this->getID();\r
58         \r
59                 // delete old thingie\r
60                 sql_query('DELETE FROM '.sql_table('template')." WHERE tpartname='". addslashes($type) ."' and tdesc=" . intval($id));\r
61                 \r
62                 // write new thingie\r
63                 if ($content) {\r
64                         sql_query('INSERT INTO '.sql_table('template')." SET tcontent='" . addslashes($content) . "', tpartname='" . addslashes($type) . "', tdesc=" . intval($id));\r
65                 }       \r
66         }\r
67                 \r
68 \r
69         /**\r
70          * Deletes all template parts from the database\r
71          */\r
72         function deleteAllParts() {\r
73                 sql_query('DELETE FROM '.sql_table('template').' WHERE tdesc='.$this->getID());\r
74         }\r
75 \r
76         /**\r
77          * Creates a new template\r
78          *\r
79          * (static)\r
80          */\r
81         function createNew($name, $desc) {\r
82                 global $manager;\r
83                 \r
84                 $manager->notify(\r
85                         'PreAddTemplate',\r
86                         array(\r
87                                 'name' => &$name,\r
88                                 'description' => &$desc\r
89                         )\r
90                 );\r
91                 \r
92                 sql_query('INSERT INTO '.sql_table('template_desc')." (tdname, tddesc) VALUES ('" . addslashes($name) . "','" . addslashes($desc) . "')");\r
93                 $newId = mysql_insert_id();\r
94                 \r
95                 $manager->notify(\r
96                         'PostAddTemplate',\r
97                         array(\r
98                                 'templateid' => $newId,\r
99                                 'name' => $name,\r
100                                 'description' => $desc\r
101                         )\r
102                 );              \r
103                 \r
104                 return $newId;\r
105         }\r
106 \r
107         \r
108 \r
109         /**\r
110          * Reads a template and returns an array with the parts.\r
111          * (static)\r
112          *\r
113          * @param $name name of the template file\r
114          */\r
115         function read($name) {\r
116                 $query = 'SELECT tpartname, tcontent'\r
117                        . ' FROM '.sql_table('template_desc').', '.sql_table('template')\r
118                        . ' WHERE tdesc=tdnumber and tdname="' . addslashes($name) . '"';\r
119                 $res = sql_query($query);\r
120                 while ($obj = mysql_fetch_object($res)) \r
121                         $template[$obj->tpartname] = $obj->tcontent;\r
122                 \r
123                 // set locale according to template:\r
124                 if ($template['LOCALE'])\r
125                         setlocale(LC_TIME,$template['LOCALE']);\r
126                 else\r
127                         setlocale(LC_TIME,'');  \r
128                         \r
129                 return $template;\r
130         }\r
131         \r
132         /**\r
133           * fills a template with values\r
134           * (static)\r
135           *\r
136           * @param $template \r
137           *             Template to be used\r
138           * @param $values\r
139           *             Array of all the values \r
140           */\r
141         function fill($template, $values) {\r
142 \r
143                 if (sizeof($values) != 0) {\r
144                         // go through all the values\r
145                         for(reset($values); $key = key($values); next($values)) {\r
146                                 $template = str_replace("<%$key%>",$values[$key],$template);\r
147                         }\r
148                 }\r
149 \r
150                 // remove non matched template-tags\r
151                 return preg_replace('/<%[a-zA-Z]+%>/','',$template);\r
152         }       \r
153         \r
154         // returns true if there is a template with the given shortname\r
155         // (static)\r
156         function exists($name) {\r
157                 $r = sql_query('select * FROM '.sql_table('template_desc').' WHERE tdname="'.addslashes($name).'"');\r
158                 return (mysql_num_rows($r) != 0);\r
159         }\r
160         \r
161         // returns true if there is a template with the given ID\r
162         // (static)\r
163         function existsID($id) {\r
164                 $r = sql_query('select * FROM '.sql_table('template_desc').' WHERE tdnumber='.intval($id));\r
165                 return (mysql_num_rows($r) != 0);\r
166         }\r
167         \r
168         // (static)\r
169         function getNameFromId($id) {\r
170                 return quickQuery('SELECT tdname as result FROM '.sql_table('template_desc').' WHERE tdnumber=' . intval($id));\r
171         }\r
172         \r
173         // (static)\r
174         function getDesc($id) {\r
175                 $query = 'SELECT tddesc FROM '.sql_table('template_desc').' WHERE tdnumber='. intval($id);\r
176                 $obj = mysql_fetch_object(sql_query($query));\r
177                 return $obj->tddesc;\r
178         }\r
179         \r
180 \r
181         \r
182 }\r
183 \r
184 ?>