OSDN Git Service

preparation for 4.0 trunk: move whole scripts just under trunk directory
[nucleus-jp/nucleus-jp-ancient.git] / nucleus / libs / TEMPLATE.php
1 <?php
2
3 /*
4  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5  * Copyright (C) 2002-2011 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-2011 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 {
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="'.sql_real_escape_string($name).'"';
43                 $res = sql_query($query);
44                 $obj = sql_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='" . sql_real_escape_string($name) . "',"
54                            . " tddesc='" . sql_real_escape_string($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='". sql_real_escape_string($type) ."' and tdesc=" . intval($id));
67
68                 // write new thingie
69                 if ($content) {
70                         sql_query('INSERT INTO '.sql_table('template')." SET tcontent='" . sql_real_escape_string($content) . "', tpartname='" . sql_real_escape_string($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 ('" . sql_real_escape_string($name) . "','" . sql_real_escape_string($desc) . "')");
99                 $newId = sql_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                 global $manager;
123                 $manager->notify(
124                         'PreTemplateRead',
125                         array(
126                                 'template' => &$name
127                         )
128                 );
129
130                 $query = 'SELECT tpartname, tcontent'
131                            . ' FROM '.sql_table('template_desc').', '.sql_table('template')
132                            . ' WHERE tdesc=tdnumber and tdname="' . sql_real_escape_string($name) . '"';
133                 $res = sql_query($query);
134                 while ($obj = sql_fetch_object($res))
135                         $template[$obj->tpartname] = $obj->tcontent;
136
137                 // set locale according to template:
138                 if (isset($template['LOCALE']))
139                         setlocale(LC_TIME,$template['LOCALE']);
140                 else
141                         setlocale(LC_TIME,'');
142
143                 return $template;
144         }
145
146         /**
147           * fills a template with values
148           * (static)
149           *
150           * @param $template
151           *             Template to be used
152           * @param $values
153           *             Array of all the values
154           */
155         function fill($template, $values) {
156
157                 if (sizeof($values) != 0) {
158                         // go through all the values
159                         for(reset($values); $key = key($values); next($values)) {
160                                 $template = str_replace("<%$key%>",$values[$key],$template);
161                         }
162                 }
163
164                 // remove non matched template-tags
165                 return preg_replace('/<%[a-zA-Z]+%>/','',$template);
166         }
167
168         // returns true if there is a template with the given shortname
169         // (static)
170         function exists($name) {
171                 $r = sql_query('select * FROM '.sql_table('template_desc').' WHERE tdname="'.sql_real_escape_string($name).'"');
172                 return (sql_num_rows($r) != 0);
173         }
174
175         // returns true if there is a template with the given ID
176         // (static)
177         function existsID($id) {
178                 $r = sql_query('select * FROM '.sql_table('template_desc').' WHERE tdnumber='.intval($id));
179                 return (sql_num_rows($r) != 0);
180         }
181
182         // (static)
183         function getNameFromId($id) {
184                 return quickQuery('SELECT tdname as result FROM '.sql_table('template_desc').' WHERE tdnumber=' . intval($id));
185         }
186
187         // (static)
188         function getDesc($id) {
189                 $query = 'SELECT tddesc FROM '.sql_table('template_desc').' WHERE tdnumber='. intval($id);
190                 $res = sql_query($query);
191                 $obj = sql_fetch_object($res);
192                 return $obj->tddesc;
193         }
194
195
196
197 }
198
199 ?>