OSDN Git Service

CHANGE: NOTIFICATIONクラスの整備。i18nのメール送信メソッドを移植
[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          * Updates the contents of one part of the template
60          */
61         function update($type, $content) {
62                 $id = $this->getID();
63
64                 // delete old thingie
65                 sql_query('DELETE FROM '.sql_table('template')." WHERE tpartname='". sql_real_escape_string($type) ."' and tdesc=" . intval($id));
66
67                 // write new thingie
68                 if ($content) {
69                         sql_query('INSERT INTO '.sql_table('template')." SET tcontent='" . sql_real_escape_string($content) . "', tpartname='" . sql_real_escape_string($type) . "', tdesc=" . intval($id));
70                 }
71         }
72
73
74         /**
75          * Deletes all template parts from the database
76          */
77         function deleteAllParts() {
78                 sql_query('DELETE FROM '.sql_table('template').' WHERE tdesc='.$this->getID());
79         }
80
81         /**
82          * Creates a new template
83          *
84          * (static)
85          */
86         function createNew($name, $desc) {
87                 global $manager;
88
89                 $manager->notify(
90                         'PreAddTemplate',
91                         array(
92                                 'name' => &$name,
93                                 'description' => &$desc
94                         )
95                 );
96
97                 sql_query('INSERT INTO '.sql_table('template_desc')." (tdname, tddesc) VALUES ('" . sql_real_escape_string($name) . "','" . sql_real_escape_string($desc) . "')");
98                 $newId = sql_insert_id();
99
100                 $manager->notify(
101                         'PostAddTemplate',
102                         array(
103                                 'templateid' => $newId,
104                                 'name' => $name,
105                                 'description' => $desc
106                         )
107                 );
108
109                 return $newId;
110         }
111
112
113
114         /**
115          * Reads a template and returns an array with the parts.
116          * (static)
117          *
118          * @param $name name of the template file
119          */
120         function read($name) {
121                 global $manager;
122                 $manager->notify(
123                         'PreTemplateRead',
124                         array(
125                                 'template' => &$name
126                         )
127                 );
128
129                 $query = 'SELECT tpartname, tcontent'
130                            . ' FROM '.sql_table('template_desc').', '.sql_table('template')
131                            . ' WHERE tdesc=tdnumber and tdname="' . sql_real_escape_string($name) . '"';
132                 $res = sql_query($query);
133                 while ($obj = sql_fetch_object($res))
134                         $template[$obj->tpartname] = $obj->tcontent;
135
136                 // set locale according to template:
137                 if (isset($template['LOCALE']))
138                         setlocale(LC_TIME,$template['LOCALE']);
139                 else
140                         setlocale(LC_TIME,'');
141
142                 return $template;
143         }
144
145         /**
146           * fills a template with values
147           * (static)
148           *
149           * @param $template
150           *             Template to be used
151           * @param $values
152           *             Array of all the values
153           */
154         function fill($template, $values) {
155
156                 if (sizeof($values) != 0) {
157                         // go through all the values
158                         for(reset($values); $key = key($values); next($values)) {
159                                 $template = str_replace("<%$key%>",$values[$key],$template);
160                         }
161                 }
162
163                 // remove non matched template-tags
164                 return preg_replace('/<%[a-zA-Z]+%>/','',$template);
165         }
166
167         // returns true if there is a template with the given shortname
168         // (static)
169         function exists($name) {
170                 $r = sql_query('select * FROM '.sql_table('template_desc').' WHERE tdname="'.sql_real_escape_string($name).'"');
171                 return (sql_num_rows($r) != 0);
172         }
173
174         // returns true if there is a template with the given ID
175         // (static)
176         function existsID($id) {
177                 $r = sql_query('select * FROM '.sql_table('template_desc').' WHERE tdnumber='.intval($id));
178                 return (sql_num_rows($r) != 0);
179         }
180
181         // (static)
182         function getNameFromId($id) {
183                 return quickQuery('SELECT tdname as result FROM '.sql_table('template_desc').' WHERE tdnumber=' . intval($id));
184         }
185
186         // (static)
187         function getDesc($id) {
188                 $query = 'SELECT tddesc FROM '.sql_table('template_desc').' WHERE tdnumber='. intval($id);
189                 $res = sql_query($query);
190                 $obj = sql_fetch_object($res);
191                 return $obj->tddesc;
192         }
193
194
195
196 }
197
198 ?>