OSDN Git Service

5838fed26e0da5361ae5629a4fbb011c5c85eb36
[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 /**
14  * A class representing a template
15  *
16  * @license http://nucleuscms.org/license.txt GNU General Public License
17  * @copyright Copyright (C) 2002-2012 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                 $param = array(
91                         'name'                  => &$name,
92                         'description'   => &$desc
93                 );
94                 $manager->notify('PreAddTemplate', $param);
95
96                 sql_query('INSERT INTO '.sql_table('template_desc')." (tdname, tddesc) VALUES ('" . sql_real_escape_string($name) . "','" . sql_real_escape_string($desc) . "')");
97                 $newId = sql_insert_id();
98
99                 $param = array(
100                         'templateid'    => $newId,
101                         'name'                  => $name,
102                         'description'   => $desc
103                 );
104                 $manager->notify('PostAddTemplate', $param);
105
106                 return $newId;
107         }
108
109
110
111         /**
112          * Reads a template and returns an array with the parts.
113          * (static)
114          *
115          * @param $name name of the template file
116          */
117         function read($name) {
118                 global $manager;
119                 
120                 $param = array(
121                         'template' => &$name
122                 );
123                 $manager->notify('PreTemplateRead', $param);
124
125                 $query = 'SELECT tpartname, tcontent'
126                            . ' FROM '.sql_table('template_desc').', '.sql_table('template')
127                            . ' WHERE tdesc=tdnumber and tdname="' . sql_real_escape_string($name) . '"';
128                 $res = sql_query($query);
129                 while ($obj = sql_fetch_object($res))
130                         $template[$obj->tpartname] = $obj->tcontent;
131
132                 // set locale according to template:
133                 if (isset($template['LOCALE']))
134                         setlocale(LC_TIME,$template['LOCALE']);
135                 else
136                         setlocale(LC_TIME,'');
137
138                 return $template;
139         }
140
141         /**
142           * fills a template with values
143           * (static)
144           *
145           * @param $template
146           *             Template to be used
147           * @param $values
148           *             Array of all the values
149           */
150         function fill($template, $values) {
151
152                 if (sizeof($values) != 0) {
153                         // go through all the values
154                         for(reset($values); $key = key($values); next($values)) {
155                                 $template = str_replace("<%$key%>",$values[$key],$template);
156                         }
157                 }
158
159                 // remove non matched template-tags
160                 return preg_replace('/<%[a-zA-Z]+%>/','',$template);
161         }
162
163         // returns true if there is a template with the given shortname
164         // (static)
165         function exists($name) {
166                 $r = sql_query('select * FROM '.sql_table('template_desc').' WHERE tdname="'.sql_real_escape_string($name).'"');
167                 return (sql_num_rows($r) != 0);
168         }
169
170         // returns true if there is a template with the given ID
171         // (static)
172         function existsID($id) {
173                 $r = sql_query('select * FROM '.sql_table('template_desc').' WHERE tdnumber='.intval($id));
174                 return (sql_num_rows($r) != 0);
175         }
176
177         // (static)
178         function getNameFromId($id) {
179                 return quickQuery('SELECT tdname as result FROM '.sql_table('template_desc').' WHERE tdnumber=' . intval($id));
180         }
181
182         // (static)
183         function getDesc($id) {
184                 $query = 'SELECT tddesc FROM '.sql_table('template_desc').' WHERE tdnumber='. intval($id);
185                 $res = sql_query($query);
186                 $obj = sql_fetch_object($res);
187                 return $obj->tddesc;
188         }
189
190
191
192 }
193
194 ?>