OSDN Git Service

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