OSDN Git Service

7f972d8346f9795f57ce32a7edd43304cd1a852a
[nucleus-jp/nucleus-jp-ancient.git] / utf8 / nucleus / libs / TEMPLATE.php
1 <?php
2
3 /*
4  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5  * Copyright (C) 2002-2009 The Nucleus Group
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * (see nucleus/documentation/index.html#license for more info)
12  */
13 /**
14  * A class representing a template
15  *
16  * @license http://nucleuscms.org/license.txt GNU General Public License
17  * @copyright Copyright (C) 2002-2009 The Nucleus Group
18  * @version $Id$
19  * @version $NucleusJP: TEMPLATE.php,v 1.6 2006/07/20 08:01:52 kimitake Exp $
20  */
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                 // <temporary hack. for 3.4x ONLY !!>\r
91                 global $CONF;\r
92                 if (!$manager && $CONF['installscript']) {\r
93                         include_once($DIR_LIBS . 'MANAGER.php');\r
94                         $manager =& MANAGER::instance();\r
95                 }\r
96                 // </temporary hack. for 3.4x ONLY !!>\r
97 \r
98                 $manager->notify(\r
99                         'PreAddTemplate',\r
100                         array(\r
101                                 'name' => &$name,\r
102                                 'description' => &$desc\r
103                         )\r
104                 );\r
105 \r
106                 sql_query('INSERT INTO '.sql_table('template_desc')." (tdname, tddesc) VALUES ('" . addslashes($name) . "','" . addslashes($desc) . "')");\r
107                 $newId = mysql_insert_id();\r
108 \r
109                 $manager->notify(\r
110                         'PostAddTemplate',\r
111                         array(\r
112                                 'templateid' => $newId,\r
113                                 'name' => $name,\r
114                                 'description' => $desc\r
115                         )\r
116                 );\r
117 \r
118                 return $newId;\r
119         }\r
120 \r
121 \r
122 \r
123         /**\r
124          * Reads a template and returns an array with the parts.\r
125          * (static)\r
126          *\r
127          * @param $name name of the template file\r
128          */\r
129         function read($name) {\r
130                 global $manager;\r
131                 $manager->notify(\r
132                         'PreTemplateRead',\r
133                         array(\r
134                                 'template' => &$name\r
135                         )\r
136                 );\r
137 \r
138                 $query = 'SELECT tpartname, tcontent'\r
139                            . ' FROM '.sql_table('template_desc').', '.sql_table('template')\r
140                            . ' WHERE tdesc=tdnumber and tdname="' . addslashes($name) . '"';\r
141                 $res = sql_query($query);\r
142                 while ($obj = mysql_fetch_object($res))\r
143                         $template[$obj->tpartname] = $obj->tcontent;\r
144 \r
145                 // set locale according to template:\r
146                 if (isset($template['LOCALE']))\r
147                         setlocale(LC_TIME,$template['LOCALE']);\r
148                 else\r
149                         setlocale(LC_TIME,'');\r
150 \r
151                 return $template;\r
152         }\r
153 \r
154         /**\r
155           * fills a template with values\r
156           * (static)\r
157           *\r
158           * @param $template\r
159           *             Template to be used\r
160           * @param $values\r
161           *             Array of all the values\r
162           */\r
163         function fill($template, $values) {\r
164 \r
165                 if (sizeof($values) != 0) {\r
166                         // go through all the values\r
167                         for(reset($values); $key = key($values); next($values)) {\r
168                                 $template = str_replace("<%$key%>",$values[$key],$template);\r
169                         }\r
170                 }\r
171 \r
172                 // remove non matched template-tags\r
173                 return preg_replace('/<%[a-zA-Z]+%>/','',$template);\r
174         }\r
175 \r
176         // returns true if there is a template with the given shortname\r
177         // (static)\r
178         function exists($name) {\r
179                 $r = sql_query('select * FROM '.sql_table('template_desc').' WHERE tdname="'.addslashes($name).'"');\r
180                 return (mysql_num_rows($r) != 0);\r
181         }\r
182 \r
183         // returns true if there is a template with the given ID\r
184         // (static)\r
185         function existsID($id) {\r
186                 $r = sql_query('select * FROM '.sql_table('template_desc').' WHERE tdnumber='.intval($id));\r
187                 return (mysql_num_rows($r) != 0);\r
188         }\r
189 \r
190         // (static)\r
191         function getNameFromId($id) {\r
192                 return quickQuery('SELECT tdname as result FROM '.sql_table('template_desc').' WHERE tdnumber=' . intval($id));\r
193         }\r
194 \r
195         // (static)\r
196         function getDesc($id) {\r
197                 $query = 'SELECT tddesc FROM '.sql_table('template_desc').' WHERE tdnumber='. intval($id);\r
198                 $res = sql_query($query);\r
199                 $obj = mysql_fetch_object($res);\r
200                 return $obj->tddesc;\r
201         }\r
202 \r
203 \r
204 \r
205 }\r
206 \r
207 ?>