OSDN Git Service

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