OSDN Git Service

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