00001 <?php 00002 00003 /* 00004 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/) 00005 * Copyright (C) 2002-2007 The Nucleus Group 00006 * 00007 * This program is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU General Public License 00009 * as published by the Free Software Foundation; either version 2 00010 * of the License, or (at your option) any later version. 00011 * (see nucleus/documentation/index.html#license for more info) 00012 */ 00021 class TEMPLATE { 00022 00023 var $id; 00024 00025 function TEMPLATE($templateid) { 00026 $this->id = intval($templateid); 00027 } 00028 00029 function getID() { 00030 return intval($this->id); 00031 } 00032 00033 // (static) 00034 function createFromName($name) { 00035 return new TEMPLATE(TEMPLATE::getIdFromName($name)); 00036 } 00037 00038 // (static) 00039 function getIdFromName($name) { 00040 $query = 'SELECT tdnumber' 00041 . ' FROM '.sql_table('template_desc') 00042 . ' WHERE tdname="'.addslashes($name).'"'; 00043 $res = sql_query($query); 00044 $obj = mysql_fetch_object($res); 00045 return $obj->tdnumber; 00046 } 00047 00051 function updateGeneralInfo($name, $desc) { 00052 $query = 'UPDATE '.sql_table('template_desc').' SET' 00053 . " tdname='" . addslashes($name) . "'," 00054 . " tddesc='" . addslashes($desc) . "'" 00055 . " WHERE tdnumber=" . $this->getID(); 00056 sql_query($query); 00057 } 00058 00062 function update($type, $content) { 00063 $id = $this->getID(); 00064 00065 // delete old thingie 00066 sql_query('DELETE FROM '.sql_table('template')." WHERE tpartname='". addslashes($type) ."' and tdesc=" . intval($id)); 00067 00068 // write new thingie 00069 if ($content) { 00070 sql_query('INSERT INTO '.sql_table('template')." SET tcontent='" . addslashes($content) . "', tpartname='" . addslashes($type) . "', tdesc=" . intval($id)); 00071 } 00072 } 00073 00074 00078 function deleteAllParts() { 00079 sql_query('DELETE FROM '.sql_table('template').' WHERE tdesc='.$this->getID()); 00080 } 00081 00087 function createNew($name, $desc) { 00088 global $manager; 00089 00090 $manager->notify( 00091 'PreAddTemplate', 00092 array( 00093 'name' => &$name, 00094 'description' => &$desc 00095 ) 00096 ); 00097 00098 sql_query('INSERT INTO '.sql_table('template_desc')." (tdname, tddesc) VALUES ('" . addslashes($name) . "','" . addslashes($desc) . "')"); 00099 $newId = mysql_insert_id(); 00100 00101 $manager->notify( 00102 'PostAddTemplate', 00103 array( 00104 'templateid' => $newId, 00105 'name' => $name, 00106 'description' => $desc 00107 ) 00108 ); 00109 00110 return $newId; 00111 } 00112 00113 00114 00121 function read($name) { 00122 $query = 'SELECT tpartname, tcontent' 00123 . ' FROM '.sql_table('template_desc').', '.sql_table('template') 00124 . ' WHERE tdesc=tdnumber and tdname="' . addslashes($name) . '"'; 00125 $res = sql_query($query); 00126 while ($obj = mysql_fetch_object($res)) 00127 $template[$obj->tpartname] = $obj->tcontent; 00128 00129 // set locale according to template: 00130 if ($template['LOCALE']) 00131 setlocale(LC_TIME,$template['LOCALE']); 00132 else 00133 setlocale(LC_TIME,''); 00134 00135 return $template; 00136 } 00137 00147 function fill($template, $values) { 00148 00149 if (sizeof($values) != 0) { 00150 // go through all the values 00151 for(reset($values); $key = key($values); next($values)) { 00152 $template = str_replace("<%$key%>",$values[$key],$template); 00153 } 00154 } 00155 00156 // remove non matched template-tags 00157 return preg_replace('/<%[a-zA-Z]+%>/','',$template); 00158 } 00159 00160 // returns true if there is a template with the given shortname 00161 // (static) 00162 function exists($name) { 00163 $r = sql_query('select * FROM '.sql_table('template_desc').' WHERE tdname="'.addslashes($name).'"'); 00164 return (mysql_num_rows($r) != 0); 00165 } 00166 00167 // returns true if there is a template with the given ID 00168 // (static) 00169 function existsID($id) { 00170 $r = sql_query('select * FROM '.sql_table('template_desc').' WHERE tdnumber='.intval($id)); 00171 return (mysql_num_rows($r) != 0); 00172 } 00173 00174 // (static) 00175 function getNameFromId($id) { 00176 return quickQuery('SELECT tdname as result FROM '.sql_table('template_desc').' WHERE tdnumber=' . intval($id)); 00177 } 00178 00179 // (static) 00180 function getDesc($id) { 00181 $query = 'SELECT tddesc FROM '.sql_table('template_desc').' WHERE tdnumber='. intval($id); 00182 $res = sql_query($query); 00183 $obj = mysql_fetch_object($res); 00184 return $obj->tddesc; 00185 } 00186 00187 00188 00189 } 00190 00191 ?>