OSDN Git Service

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