OSDN Git Service

16f55861891d7be15debe29a3df369b367883703
[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 1880 2012-06-17 07:48:14Z sakamocchi $
19  */
20 class Template
21 {
22         /**
23          * Template::$id
24          */
25         private $id;
26         
27         /**
28          * Template::__construct()
29          * 
30          * @param       integer $templateid     id for template
31          * @return      void
32          */
33         public function __construct($templateid)
34         {
35                 $this->id = intval($templateid);
36                 return;
37         }
38         
39         /**
40          * Template::getID()
41          * 
42          * @param       void
43          * @return      integer id for this instance of Template class
44          */
45         public function getID()
46         {
47                 return (integer) $this->id;
48         }
49         
50         /**
51          * Template::createFromName()
52          * 
53          * @statc
54          * @param       string  $name   template name
55          * @return      object  instance of Template class generated by the name
56          */
57         static public function createFromName($name)
58         {
59                 return new Template(Template::getIdFromName($name));
60         }
61         
62         /**
63          * Template::getIdFromName()
64          * 
65          * @static
66          * @param       string  $name   template name
67          * @return      integer id for the template
68          */
69         static public function getIdFromName($name)
70         {
71                 $name = DB::quoteValue($name);
72                 $query = "SELECT tdnumber FROM %s WHERE tdname=%s";
73                 $query = sprintf($query, sql_table('template_desc'), $name);
74                 return DB::getValue($query);
75         }
76         
77         /**
78          * Template::updateGeneralInfo()
79          * Updates the general information about the template
80          * 
81          * @param       string  $name   template name
82          * @param       string  $desc   description for this template
83          * @return      void
84          */
85         public function updateGeneralInfo($name, $desc)
86         {
87                 $query =  "UPDATE %s SET tdname=%s, tddesc=%s WHERE tdnumber=%d";
88                 $query = sprintf($query, sql_table('template_desc'), DB::quoteValue($name), DB::quoteValue($desc), (integer) $this->getID());
89                 DB::execute($query);
90                 return;
91         }
92         
93         /**
94          * Template::update()
95          * Updates the contents of one part of the template
96          * 
97          * @param       String  $type   value for nucleus_template.tpartname
98          * @param       String  $content        value for nucleus_template.tcontent
99          * @return      Void
100          */
101         public function update($type, $content)
102         {
103                 // delete old thingie
104                 $query = "DELETE FROM %s WHERE tpartname=%s and tdesc=%d";
105                 $query = sprintf($query, sql_table('template'), DB::quoteValue($type), (integer) $this->getID());
106                 DB::execute($query);
107                 
108                 // write new thingie
109                 if ( $content )
110                 {
111                         $query = "INSERT INTO %s (tcontent, tpartname, tdesc) VALUES (%s, %s, %d)";
112                         $query = sprintf($query, sql_table('template'), DB::quoteValue($content), DB::quoteValue($type), (integer) $this->getID());
113                         DB::execute($query);
114                 }
115                 return;
116         }
117         
118         /**
119          * Template::deleteAllParts()
120          * Deletes all template parts from the database
121          * 
122          * @param       void
123          * @return      void
124          */
125         public function deleteAllParts()
126         {
127                 $query = "DELETE FROM %s WHERE tdesc=%d";
128                 $query = sprintf($query, sql_table('template'), (integer) $this->getID());
129                 DB::execute($query);
130                 return;
131         }
132         
133         /**
134          * Template::createNew()
135          * Creates a new template
136          *
137          * @static
138          * @param       string  $name   name for new template
139          * @param       string  $desc   description for new template
140          * @return      integer id for new template
141          */
142         static public function createNew($name, $desc)
143         {
144                 global $manager;
145                 
146                 $manager->notify(
147                         'PreAddTemplate',
148                         array(
149                                 'name' => &$name,
150                                 'description' => &$desc
151                         )
152                 );
153                 
154                 DB::execute('INSERT INTO '.sql_table('template_desc').' (tdname, tddesc) VALUES (' . DB::quoteValue($name) . ',' . DB::quoteValue($desc) . ')');
155                 $newId = DB::getInsertId();
156                 
157                 $manager->notify(
158                         'PostAddTemplate',
159                         array(
160                                 'templateid' => $newId,
161                                 'name' => $name,
162                                 'description' => $desc
163                         )
164                 );
165                 
166                 return $newId;
167         }
168         
169         /**
170          * Reads a template and returns an array with the parts.
171          *
172          * @static
173          * @param       string  $name name of the template file
174          * @return      array   template array
175          */
176         static public function read($name)
177         {
178                 global $manager;
179                 $manager->notify(
180                         'PreTemplateRead',
181                         array(
182                                 'template' => &$name
183                         )
184                 );
185                 
186                 $query = "SELECT tpartname, tcontent FROM %s, %s WHERE tdesc=tdnumber and tdname=%s";
187                 $query = sprintf($query, sql_table('template_desc'), sql_table('template'), DB::quoteValue($name));
188                 $res = DB::getResult($query);
189                 
190                 $template = array();
191                 foreach ( $res as $row )
192                 {
193                         $template[$row['tpartname']] = $row['tcontent'];
194                 }
195                 
196                 /*
197                  * TODO: this is appropriate or not?
198                  */
199                 if ( array_key_exists('LOCALE', $template) && !empty($template['LOCALE']) )
200                 {
201                         setlocale(LC_TIME, $template['LOCALE']);
202                 }
203                 else
204                 {
205                         setlocale(LC_TIME,'');
206                 }
207                 
208                 return $template;
209         }
210         
211         /**
212          * fills a template with values
213          * 
214          * @static
215          * @param       string  $template       Template to be used
216          * @param       array   $values         Array of all the values
217          * @return      string  string filled with tag contents
218          */
219         static public function fill($template, $values)
220         {
221                 
222                 if ( sizeof($values) != 0 )
223                 {
224                         foreach ( $values as $key => $value )
225                         {
226                                 $template = preg_replace('#<%' . preg_quote($key, '#') . '%>#', $value, $template);
227                         }
228                 }
229                 
230                 // remove non matched template-tags
231                 return preg_replace('#<%([a-zA-Z]+)?%>#', '', $template);
232         }
233         
234         /**
235          * Template::exists()
236          * returns true if there is a template with the given shortname
237          * 
238          * @static
239          * @param       string  $name   template name
240          * @return      boolean exists or not
241          */
242         static public function exists($name)
243         {
244                 $query = "SELECT * FROM %s WHERE tdname=%s";
245                 $query = sprintf($query, sql_table('template_desc'), DB::quoteValue($name));
246                 $r = DB::getResult($query);
247                 return ($r->rowCount() != 0);
248         }
249         
250         /**
251          * Template::existsID()
252          * returns true if there is a template with the given ID
253          * 
254          * @static
255          * @param       integer $id     id for template
256          * @return      bookean exists or not
257          */
258         static public function existsID($id)
259         {
260                 $query = "SELECT * FROM %s WHERE tdnumber=%d";
261                 $query = sprintf($query, sql_table('template_desc'), (integer) $id);
262                 $r = DB::getResult($query);
263                 return ($r->rowCount() != 0);
264         }
265         
266         /**
267          * Template::getNameFromId()
268          * 
269          * @static
270          * @param       integer $id     id for template
271          * @return      object  sql object
272          */
273         static public function getNameFromId($id)
274         {
275                 $query = "SELECT tdname as result FROM %s WHERE tdnumber=%d";
276                 $query = sprintf($query, sql_table('template_desc'), (integer) $id);
277                 return DB::getValue($query);
278         }
279         
280         /**
281          * Template::getDesc()
282          * 
283          * @static
284          * @param       integer $id     id for template
285          * @return      string  description for the template
286          */
287         static public function getDesc($id)
288         {
289                 $query = "SELECT tddesc FROM %s WHERE tdnumber=%d";
290                 $query = sprintf($query, sql_table('template_desc'), (integer) $id);
291                 return DB::getValue($query);
292         }
293 }