OSDN Git Service

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