OSDN Git Service

Merge branch 'skinnable-master'
[nucleus-jp/nucleus-next.git] / nucleus / libs / TEMPLATE.php
1 <<<<<<< HEAD
2 <?php\r
3 \r
4 /*\r
5  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)\r
6  * Copyright (C) 2002-2012 The Nucleus Group\r
7  *\r
8  * This program is free software; you can redistribute it and/or\r
9  * modify it under the terms of the GNU General Public License\r
10  * as published by the Free Software Foundation; either version 2\r
11  * of the License, or (at your option) any later version.\r
12  * (see nucleus/documentation/index.html#license for more info)\r
13  */\r
14 /**\r
15  * A class representing a template\r
16  *\r
17  * @license http://nucleuscms.org/license.txt GNU General Public License\r
18  * @copyright Copyright (C) 2002-2012 The Nucleus Group\r
19  * @version $Id: TEMPLATE.php 1726 2012-04-07 02:23:46Z sakamocchi $\r
20  */\r
21 class Template\r
22 {\r
23         /**\r
24          * Template::$id\r
25          */\r
26         private $id;\r
27         \r
28         /**\r
29          * Template::__construct()\r
30          * \r
31          * @param       integer $templateid     id for template\r
32          * @return      void\r
33          */\r
34         public function __construct($templateid)\r
35         {\r
36                 $this->id = intval($templateid);\r
37                 return;\r
38         }\r
39         \r
40         /**\r
41          * Template::getID()\r
42          * \r
43          * @param       void\r
44          * @return      integer id for this instance of Template class\r
45          */\r
46         public function getID()\r
47         {\r
48                 return (integer) $this->id;\r
49         }\r
50         \r
51         /**\r
52          * Template::createFromName()\r
53          * \r
54          * @statc\r
55          * @param       string  $name   template name\r
56          * @return      object  instance of Template class generated by the name\r
57          */\r
58         static public function createFromName($name)\r
59         {\r
60                 return new Template(Template::getIdFromName($name));\r
61         }\r
62         \r
63         /**\r
64          * Template::getIdFromName()\r
65          * \r
66          * @static\r
67          * @param       string  $name   template name\r
68          * @return      integer id for the template\r
69          */\r
70         static public function getIdFromName($name)\r
71         {\r
72                 $name = DB::quoteValue($name);\r
73                 $query = "SELECT tdnumber FROM %s WHERE tdname=%s";\r
74                 $query = sprintf($query, sql_table('template_desc'), $name);\r
75                 return DB::getValue($query);\r
76         }\r
77         \r
78         /**\r
79          * Template::updateGeneralInfo()\r
80          * Updates the general information about the template\r
81          * \r
82          * @param       string  $name   template name\r
83          * @param       string  $desc   description for this template\r
84          * @return      void\r
85          */\r
86         public function updateGeneralInfo($name, $desc)\r
87         {\r
88                 $query =  "UPDATE %s SET tdname=%s, tddesc=%s WHERE tdnumber=%d";\r
89                 $query = sprintf($query, sql_table('template_desc'), DB::quoteValue($name), DB::quoteValue($desc), (integer) $this->getID());\r
90                 DB::execute($query);\r
91                 return;\r
92         }\r
93         \r
94         /**\r
95          * Template::update()\r
96          * Updates the contents of one part of the template\r
97          * \r
98          * @param       String  $type   value for nucleus_template.tpartname\r
99          * @param       String  $content        value for nucleus_template.tcontent\r
100          * @return      Void\r
101          */\r
102         public function update($type, $content)\r
103         {\r
104                 // delete old thingie\r
105                 $query = "DELETE FROM %s WHERE tpartname=%s and tdesc=%d";\r
106                 $query = sprintf($query, sql_table('template'), DB::quoteValue($type), (integer) $this->getID());\r
107                 DB::execute($query);\r
108                 \r
109                 // write new thingie\r
110                 if ( $content )\r
111                 {\r
112                         $query = "INSERT %s (tcontent, tpartname, tdesc) VALUE (%s, %s, %d)";\r
113                         $query = sprintf($query, sql_table('template'), DB::quoteValue($content), DB::quoteValue($type), (integer) $this->getID());\r
114                         DB::execute($query);\r
115                 }\r
116                 return;\r
117         }\r
118         \r
119         /**\r
120          * Template::deleteAllParts()\r
121          * Deletes all template parts from the database\r
122          * \r
123          * @param       void\r
124          * @return      void\r
125          */\r
126         public function deleteAllParts()\r
127         {\r
128                 $query = "DELETE FROM %s WHERE tdesc=%d";\r
129                 $query = sprintf($query, sql_table('template'), (integer) $this->getID());\r
130                 DB::execute($query);\r
131                 return;\r
132         }\r
133         \r
134         /**\r
135          * Template::createNew()\r
136          * Creates a new template\r
137          *\r
138          * @static\r
139          * @param       string  $name   name for new template\r
140          * @param       string  $desc   description for new template\r
141          * @return      integer id for new template\r
142          */\r
143         static public function createNew($name, $desc)\r
144         {\r
145                 global $manager;\r
146                 \r
147                 $manager->notify(\r
148                         'PreAddTemplate',\r
149                         array(\r
150                                 'name' => &$name,\r
151                                 'description' => &$desc\r
152                         )\r
153                 );\r
154                 \r
155                 DB::execute('INSERT INTO '.sql_table('template_desc').' (tdname, tddesc) VALUES (' . DB::quoteValue($name) . ',' . DB::quoteValue($desc) . ')');\r
156                 $newId = DB::getInsertId();\r
157                 \r
158                 $manager->notify(\r
159                         'PostAddTemplate',\r
160                         array(\r
161                                 'templateid' => $newId,\r
162                                 'name' => $name,\r
163                                 'description' => $desc\r
164                         )\r
165                 );\r
166                 \r
167                 return $newId;\r
168         }\r
169         \r
170         /**\r
171          * Reads a template and returns an array with the parts.\r
172          *\r
173          * @static\r
174          * @param       string  $name name of the template file\r
175          * @return      array   template array\r
176          */\r
177         static public function read($name)\r
178         {\r
179                 global $manager;\r
180                 $manager->notify(\r
181                         'PreTemplateRead',\r
182                         array(\r
183                                 'template' => &$name\r
184                         )\r
185                 );\r
186                 \r
187                 $query = "SELECT tpartname, tcontent FROM %s, %s WHERE tdesc=tdnumber and tdname=%s";\r
188                 $query = sprintf($query, sql_table('template_desc'), sql_table('template'), DB::quoteValue($name));\r
189                 $res = DB::getResult($query);\r
190                 \r
191                 foreach ( $res as $row )\r
192                 {\r
193                         $template[$row['tpartname']] = $row['tcontent'];\r
194                 }\r
195                 \r
196                 /*\r
197                  * TODO: this is appropriate or not?\r
198                  */\r
199                 if ( array_key_exists('LOCALE', $template) && !empty($template['LOCALE']) )\r
200                 {\r
201                         setlocale(LC_TIME, $template['LOCALE']);\r
202                 }\r
203                 else\r
204                 {\r
205                         setlocale(LC_TIME,'');\r
206                 }\r
207                 \r
208                 return $template;\r
209         }\r
210         \r
211         /**\r
212          * fills a template with values\r
213          * \r
214          * @static\r
215          * @param       string  $template       Template to be used\r
216          * @param       array   $values         Array of all the values\r
217          * @return      string  string filled with tag contents\r
218          */\r
219         static public function fill($template, $values)\r
220         {\r
221                 \r
222                 if ( sizeof($values) != 0 )\r
223                 {\r
224                         foreach ( $values as $key => $value )\r
225                         {\r
226                                 $template = preg_replace('#<%' . preg_quote($key, '#') . '%>#', $value, $template);\r
227                         }\r
228                 }\r
229                 \r
230                 // remove non matched template-tags\r
231                 return preg_replace('#<%([a-zA-Z]+)?%>#', '', $template);\r
232         }\r
233         \r
234         /**\r
235          * TEMPLATE::exists()\r
236          * returns true if there is a template with the given shortname\r
237          * \r
238          * @static\r
239          * @param       string  $name   template name\r
240          * @return      boolean exists or not\r
241          */\r
242         static public function exists($name)\r
243         {\r
244                 $query = "SELECT * FROM %s WHERE tdname=%s";\r
245                 $query = sprintf($query, sql_table('template_desc'), DB::quoteValue($name));\r
246                 $r = DB::getResult($query);\r
247                 return ($r->rowCount() != 0);\r
248         }\r
249         \r
250         /**\r
251          * TEMPLATE::existsID()\r
252          * returns true if there is a template with the given ID\r
253          * \r
254          * @static\r
255          * @param       integer $id     id for template\r
256          * @return      bookean exists or not\r
257          */\r
258         static public function existsID($id)\r
259         {\r
260                 $query = "SELECT * FROM %s WHERE tdnumber=%d";\r
261                 $query = sprintf($query, sql_table('template_desc'), (integer) $id);\r
262                 $r = DB::getResult($query);\r
263                 return ($r->rowCount() != 0);\r
264         }\r
265         \r
266         /**\r
267          * TEMPLATE::getNameFromId()\r
268          * \r
269          * @static\r
270          * @param       integer $id     id for template\r
271          * @return      object  sql object\r
272          */\r
273         static public function getNameFromId($id)\r
274         {\r
275                 $query = "SELECT tdname as result FROM %s WHERE tdnumber=%d";\r
276                 $query = sprintf($query, sql_table('template_desc'), (integer) $id);\r
277                 return DB::getValue($query);\r
278         }\r
279         \r
280         /**\r
281          * TEMPLATE::getDesc()\r
282          * \r
283          * @static\r
284          * @param       integer $id     id for template\r
285          * @return      string  description for the template\r
286          */\r
287         static public function getDesc($id)\r
288         {\r
289                 $query = "SELECT tddesc FROM %s WHERE tdnumber=%d";\r
290                 $query = sprintf($query, sql_table('template_desc'), (integer) $id);\r
291                 return DB::getValue($query);\r
292         }\r
293 }\r
294 =======
295 <?php
296
297 /*
298  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
299  * Copyright (C) 2002-2009 The Nucleus Group
300  *
301  * This program is free software; you can redistribute it and/or
302  * modify it under the terms of the GNU General Public License
303  * as published by the Free Software Foundation; either version 2
304  * of the License, or (at your option) any later version.
305  * (see nucleus/documentation/index.html#license for more info)
306  */
307 /**
308  * A class representing a template
309  *
310  * @license http://nucleuscms.org/license.txt GNU General Public License
311  * @copyright Copyright (C) 2002-2009 The Nucleus Group
312  * @version $Id: TEMPLATE.php 1880 2012-06-17 07:48:14Z sakamocchi $
313  */
314 class Template
315 {
316         /**
317          * Template::$id
318          */
319         private $id;
320         
321         /**
322          * Template::__construct()
323          * 
324          * @param       integer $templateid     id for template
325          * @return      void
326          */
327         public function __construct($templateid)
328         {
329                 $this->id = intval($templateid);
330                 return;
331         }
332         
333         /**
334          * Template::getID()
335          * 
336          * @param       void
337          * @return      integer id for this instance of Template class
338          */
339         public function getID()
340         {
341                 return (integer) $this->id;
342         }
343         
344         /**
345          * Template::createFromName()
346          * 
347          * @statc
348          * @param       string  $name   template name
349          * @return      object  instance of Template class generated by the name
350          */
351         static public function createFromName($name)
352         {
353                 return new Template(Template::getIdFromName($name));
354         }
355         
356         /**
357          * Template::getIdFromName()
358          * 
359          * @static
360          * @param       string  $name   template name
361          * @return      integer id for the template
362          */
363         static public function getIdFromName($name)
364         {
365                 $name = DB::quoteValue($name);
366                 $query = "SELECT tdnumber FROM %s WHERE tdname=%s";
367                 $query = sprintf($query, sql_table('template_desc'), $name);
368                 return DB::getValue($query);
369         }
370         
371         /**
372          * Template::updateGeneralInfo()
373          * Updates the general information about the template
374          * 
375          * @param       string  $name   template name
376          * @param       string  $desc   description for this template
377          * @return      void
378          */
379         public function updateGeneralInfo($name, $desc)
380         {
381                 $query =  "UPDATE %s SET tdname=%s, tddesc=%s WHERE tdnumber=%d";
382                 $query = sprintf($query, sql_table('template_desc'), DB::quoteValue($name), DB::quoteValue($desc), (integer) $this->getID());
383                 DB::execute($query);
384                 return;
385         }
386         
387         /**
388          * Template::update()
389          * Updates the contents of one part of the template
390          * 
391          * @param       String  $type   value for nucleus_template.tpartname
392          * @param       String  $content        value for nucleus_template.tcontent
393          * @return      Void
394          */
395         public function update($type, $content)
396         {
397                 // delete old thingie
398                 $query = "DELETE FROM %s WHERE tpartname=%s and tdesc=%d";
399                 $query = sprintf($query, sql_table('template'), DB::quoteValue($type), (integer) $this->getID());
400                 DB::execute($query);
401                 
402                 // write new thingie
403                 if ( $content )
404                 {
405                         $query = "INSERT INTO %s (tcontent, tpartname, tdesc) VALUES (%s, %s, %d)";
406                         $query = sprintf($query, sql_table('template'), DB::quoteValue($content), DB::quoteValue($type), (integer) $this->getID());
407                         DB::execute($query);
408                 }
409                 return;
410         }
411         
412         /**
413          * Template::deleteAllParts()
414          * Deletes all template parts from the database
415          * 
416          * @param       void
417          * @return      void
418          */
419         public function deleteAllParts()
420         {
421                 $query = "DELETE FROM %s WHERE tdesc=%d";
422                 $query = sprintf($query, sql_table('template'), (integer) $this->getID());
423                 DB::execute($query);
424                 return;
425         }
426         
427         /**
428          * Template::createNew()
429          * Creates a new template
430          *
431          * @static
432          * @param       string  $name   name for new template
433          * @param       string  $desc   description for new template
434          * @return      integer id for new template
435          */
436         static public function createNew($name, $desc)
437         {
438                 global $manager;
439
440                 $data = array(
441                         'name'                  => &$name,
442                         'description'   => &$desc
443                 );
444                 $manager->notify('PreAddTemplate', $data);
445                 
446                 DB::execute('INSERT INTO '.sql_table('template_desc').' (tdname, tddesc) VALUES (' . DB::quoteValue($name) . ',' . DB::quoteValue($desc) . ')');
447                 $newId = DB::getInsertId();
448
449                 $data = array(
450                         'templateid'    => $newId,
451                         'name'                  => $name,
452                         'description'   => $desc
453                 );
454                 $manager->notify('PostAddTemplate', $data);
455                 
456                 return $newId;
457         }
458         
459         /**
460          * Reads a template and returns an array with the parts.
461          *
462          * @static
463          * @param       string  $name name of the template file
464          * @return      array   template array
465          */
466         static public function read($name)
467         {
468                 global $manager;
469                 $data = array('template' => &$name);
470                 $manager->notify('PreTemplateRead', $data);
471                 
472                 $query = "SELECT tpartname, tcontent FROM %s, %s WHERE tdesc=tdnumber and tdname=%s";
473                 $query = sprintf($query, sql_table('template_desc'), sql_table('template'), DB::quoteValue($name));
474                 $res = DB::getResult($query);
475                 
476                 $template = array();
477                 foreach ( $res as $row )
478                 {
479                         $template[$row['tpartname']] = $row['tcontent'];
480                 }
481                 
482                 return $template;
483         }
484         
485         /**
486          * fills a template with values
487          * 
488          * @static
489          * @param       string  $template       Template to be used
490          * @param       array   $values         Array of all the values
491          * @return      string  string filled with tag contents
492          */
493         static public function fill($template, $values)
494         {
495                 
496                 if ( sizeof($values) != 0 )
497                 {
498                         foreach ( $values as $key => $value )
499                         {
500                                 $template = preg_replace('#<%' . preg_quote($key, '#') . '%>#', $value, $template);
501                         }
502                 }
503                 
504                 // remove non matched template-tags
505                 return preg_replace('#<%([a-zA-Z]+)?%>#', '', $template);
506         }
507         
508         /**
509          * Template::exists()
510          * returns true if there is a template with the given shortname
511          * 
512          * @static
513          * @param       string  $name   template name
514          * @return      boolean exists or not
515          */
516         static public function exists($name)
517         {
518                 $query = "SELECT * FROM %s WHERE tdname=%s";
519                 $query = sprintf($query, sql_table('template_desc'), DB::quoteValue($name));
520                 $r = DB::getResult($query);
521                 return ($r->rowCount() != 0);
522         }
523         
524         /**
525          * Template::existsID()
526          * returns true if there is a template with the given ID
527          * 
528          * @static
529          * @param       integer $id     id for template
530          * @return      bookean exists or not
531          */
532         static public function existsID($id)
533         {
534                 $query = "SELECT * FROM %s WHERE tdnumber=%d";
535                 $query = sprintf($query, sql_table('template_desc'), (integer) $id);
536                 $r = DB::getResult($query);
537                 return ($r->rowCount() != 0);
538         }
539         
540         /**
541          * Template::getNameFromId()
542          * 
543          * @static
544          * @param       integer $id     id for template
545          * @return      object  sql object
546          */
547         static public function getNameFromId($id)
548         {
549                 $query = "SELECT tdname as result FROM %s WHERE tdnumber=%d";
550                 $query = sprintf($query, sql_table('template_desc'), (integer) $id);
551                 return DB::getValue($query);
552         }
553         
554         /**
555          * Template::getDesc()
556          * 
557          * @static
558          * @param       integer $id     id for template
559          * @return      string  description for the template
560          */
561         static public function getDesc($id)
562         {
563                 $query = "SELECT tddesc FROM %s WHERE tdnumber=%d";
564                 $query = sprintf($query, sql_table('template_desc'), (integer) $id);
565                 return DB::getValue($query);
566         }
567 }
568 >>>>>>> skinnable-master