[ Index ]

PHP Cross Reference of Nucleus CMS 3.32

title

Body

[close]

/nucleus/libs/ -> TEMPLATE.php (source)

   1  <?php
   2  
   3  /*
   4   * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
   5   * Copyright (C) 2002-2007 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-2007 The Nucleus Group
  18   * @version $Id: TEMPLATE.php 1116 2007-02-03 08:24:29Z kimitake $
  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="'.addslashes($name).'"';
  42          $res = sql_query($query);
  43          $obj = mysql_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='" . addslashes($name) . "',"
  53                 . " tddesc='" . addslashes($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='". addslashes($type) ."' and tdesc=" . intval($id));
  66  
  67          // write new thingie
  68          if ($content) {
  69              sql_query('INSERT INTO '.sql_table('template')." SET tcontent='" . addslashes($content) . "', tpartname='" . addslashes($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 ('" . addslashes($name) . "','" . addslashes($desc) . "')");
  98          $newId = mysql_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          $query = 'SELECT tpartname, tcontent'
 122                 . ' FROM '.sql_table('template_desc').', '.sql_table('template')
 123                 . ' WHERE tdesc=tdnumber and tdname="' . addslashes($name) . '"';
 124          $res = sql_query($query);
 125          while ($obj = mysql_fetch_object($res))
 126              $template[$obj->tpartname] = $obj->tcontent;
 127  
 128          // set locale according to template:
 129          if ($template['LOCALE'])
 130              setlocale(LC_TIME,$template['LOCALE']);
 131          else
 132              setlocale(LC_TIME,'');
 133  
 134          return $template;
 135      }
 136  
 137      /**
 138        * fills a template with values
 139        * (static)
 140        *
 141        * @param $template
 142        *        Template to be used
 143        * @param $values
 144        *        Array of all the values
 145        */
 146  	function fill($template, $values) {
 147  
 148          if (sizeof($values) != 0) {
 149              // go through all the values
 150              for(reset($values); $key = key($values); next($values)) {
 151                  $template = str_replace("<%$key%>",$values[$key],$template);
 152              }
 153          }
 154  
 155          // remove non matched template-tags
 156          return preg_replace('/<%[a-zA-Z]+%>/','',$template);
 157      }
 158  
 159      // returns true if there is a template with the given shortname
 160      // (static)
 161  	function exists($name) {
 162          $r = sql_query('select * FROM '.sql_table('template_desc').' WHERE tdname="'.addslashes($name).'"');
 163          return (mysql_num_rows($r) != 0);
 164      }
 165  
 166      // returns true if there is a template with the given ID
 167      // (static)
 168  	function existsID($id) {
 169          $r = sql_query('select * FROM '.sql_table('template_desc').' WHERE tdnumber='.intval($id));
 170          return (mysql_num_rows($r) != 0);
 171      }
 172  
 173      // (static)
 174  	function getNameFromId($id) {
 175          return quickQuery('SELECT tdname as result FROM '.sql_table('template_desc').' WHERE tdnumber=' . intval($id));
 176      }
 177  
 178      // (static)
 179  	function getDesc($id) {
 180          $query = 'SELECT tddesc FROM '.sql_table('template_desc').' WHERE tdnumber='. intval($id);
 181          $res = sql_query($query);
 182          $obj = mysql_fetch_object($res);
 183          return $obj->tddesc;
 184      }
 185  
 186  
 187  
 188  }
 189  
 190  ?>


Generated: Tue Feb 12 15:34:36 2008 Cross-referenced by PHPXref 0.7