OSDN Git Service

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