made a copy
[atutor.git] / include / lib / themes.inc.php
1 <?php\r
2 /****************************************************************************/\r
3 /* ATutor                                                                                                                                       */\r
4 /****************************************************************************/\r
5 /* Copyright (c) 2002-2008 by Greg Gay, Joel Kronenberg & Heidi Hazelton        */\r
6 /* Adaptive Technology Resource Centre / University of Toronto                          */\r
7 /* http://atutor.ca                                                                                                                     */\r
8 /*                                                                                                                                                      */\r
9 /* This program is free software. You can redistribute it and/or                        */\r
10 /* modify it under the terms of the GNU General Public License                          */\r
11 /* as published by the Free Software Foundation.                                                        */\r
12 /****************************************************************************/\r
13 \r
14 if (!defined('AT_INCLUDE_PATH')) { exit; }\r
15 $db;\r
16 \r
17 /**\r
18 * Gets the name of the folder where the theme is stored\r
19 * @access  private\r
20 * @param   string $theme_dir    the name of the theme\r
21 * @return  string                               theme folder\r
22 * @author  Shozub Qureshi\r
23 */\r
24 //used by preferences.tmpl.php only\r
25 function get_folder ($theme_name) {\r
26         global $db;\r
27 \r
28         $sql    = "SELECT dir_name FROM ".TABLE_PREFIX."themes WHERE title = '$theme_name'";\r
29         $result = mysql_query($sql, $db);\r
30         $row    = mysql_fetch_assoc($result);\r
31 \r
32         return $row['dir_name'];\r
33 }\r
34 \r
35 \r
36 /**\r
37 * Gets the attributes of the theme from the themes database table\r
38 * @access  private\r
39 * @param   string $theme_dir    the name of the theme\r
40 * @return  array                                theme info\r
41 * @author  Shozub Qureshi\r
42 */\r
43 function get_themes_info($theme_dir) {\r
44         global $db;\r
45         //Go to db\r
46         $sql    = "SELECT * FROM ".TABLE_PREFIX."themes WHERE dir_name = '$theme_dir'";\r
47         $result = mysql_query($sql, $db);\r
48         \r
49         $info = mysql_fetch_assoc($result);\r
50 \r
51         return $info;\r
52 }\r
53 \r
54 /**\r
55 * Gets the name of the theme\r
56 * @access  private\r
57 * @param   string $theme_dir    theme folder\r
58 * @return  string                               theme name\r
59 * @author  heidi hazelton\r
60 */\r
61 function get_theme_name ($theme_dir) {\r
62         global $db;\r
63 \r
64         $sql    = "SELECT title FROM ".TABLE_PREFIX."themes WHERE dir_name = '$theme_dir'";\r
65         $result = mysql_query($sql, $db);\r
66         $row    = mysql_fetch_assoc($result);\r
67 \r
68         return $row['title'];\r
69 }\r
70 \r
71 /**\r
72 * Gets list of enabled themes\r
73 * @access  private\r
74 * @return  array                                the version of the theme\r
75 * @author  Shozub Qureshi\r
76 */\r
77 function get_enabled_themes () {\r
78         global $db;\r
79         //Go to db\r
80         $sql    = "SELECT title FROM ".TABLE_PREFIX."themes WHERE status = '1' OR status = '2' ORDER BY title";\r
81         $result = mysql_query($sql, $db);\r
82         \r
83         //Get all theme names into array\r
84         $i = 0;\r
85         while ($row = mysql_fetch_array($result)) {\r
86                 $themes[$i] = $row['title'];\r
87                 $i++;\r
88         }\r
89         \r
90         return $themes;\r
91 }\r
92 \r
93 /**\r
94 * Gets number of enabled themes\r
95 * @access  private\r
96 * @return  int                          the number of enabled themes\r
97 * @author  Shozub Qureshi\r
98 */\r
99 function num_enabled_themes () {\r
100         global $db;\r
101         //Go to db\r
102         $sql    = "SELECT title FROM ".TABLE_PREFIX."themes WHERE status = '1' OR status = '2'";\r
103         $result = mysql_query($sql, $db);\r
104                 \r
105         return mysql_num_rows($result);\r
106 }\r
107 \r
108 /**\r
109 * Gets list of disabled themes\r
110 * @access  private\r
111 * @return  array                                the version of the theme\r
112 * @author  Shozub Qureshi\r
113 */\r
114 function get_disabled_themes () {\r
115         global $db;\r
116         //Go to db\r
117         $sql    = "SELECT title FROM ".TABLE_PREFIX."themes WHERE status = '0'";\r
118         $result = mysql_query($sql, $db);\r
119         \r
120         //Get all theme names into array\r
121         $i = 0;\r
122         while ($row = mysql_fetch_array($result)) {\r
123                 $themes[$i] = $row['title'];\r
124                 $i++;\r
125         }\r
126         \r
127         return $themes;\r
128 }\r
129 \r
130 /**\r
131 * Gets list of all currently installed themes\r
132 * @access  private\r
133 * @return  array                                the version of the theme\r
134 * @author  Shozub Qureshi\r
135 */\r
136 function get_all_themes () {\r
137         global $db;\r
138         \r
139         // The ordering is as follow. The default theme followed by ASC ordering of rest of themes\r
140         \r
141         // Assert, one of them must be a default\r
142         $result = mysql_query('SELECT title FROM ' . TABLE_PREFIX . 'themes WHERE status = 2', $db);\r
143         $row = mysql_fetch_assoc($result);\r
144         $first_one = $row['title'];\r
145         \r
146         $themes[$i] = $first_one;\r
147         \r
148         // Go to db\r
149         $sql    = "SELECT title FROM " . TABLE_PREFIX . "themes WHERE title != '$first_one' ORDER BY title ASC";\r
150         $result = mysql_query($sql, $db);\r
151         \r
152         // Get all theme names into array\r
153         $i = 1;\r
154         while ($row = mysql_fetch_assoc($result)) {\r
155                 $themes[$i] = $row['title'];\r
156                 $i++;\r
157         }\r
158         \r
159         return $themes;\r
160 }\r
161 \r
162 function enable_theme ($theme_dir) {\r
163         global $msg, $db;\r
164 \r
165         if ($_SESSION['prefs']['PREF_THEME'] != $theme_dir) {\r
166                 $sql = "UPDATE ".TABLE_PREFIX."themes SET status = '1' WHERE dir_name = '$theme_dir'";\r
167                 $result = mysql_query($sql, $db);\r
168                 write_to_log(AT_ADMIN_LOG_UPDATE, 'themes', mysql_affected_rows($db), $sql);\r
169         } \r
170         $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');\r
171 }\r
172 \r
173 function disable_theme ($theme_dir) {\r
174         global $msg, $db;\r
175 \r
176         $sql    = "SELECT status FROM ".TABLE_PREFIX."themes WHERE dir_name = '$theme_dir'";\r
177         $result = mysql_query ($sql, $db);\r
178         $row    = mysql_fetch_array($result);\r
179         $status = intval($row['status']);\r
180 \r
181         //If default theme, then it cannot be disabled\r
182         if ($status == 2) {\r
183                 $msg->addError('THEME_NOT_DISABLED');\r
184                 return;\r
185         } else {\r
186                 $sql    = "UPDATE ".TABLE_PREFIX."themes SET status = '0' WHERE dir_name = '$theme_dir'";\r
187                 $result = mysql_query($sql, $db);\r
188 \r
189                 $feedback = array('THEME_DISABLED', $theme_dir);\r
190                 $msg->addFeedback($feedback);\r
191 \r
192                 write_to_log(AT_ADMIN_LOG_UPDATE, 'themes', mysql_affected_rows($db), $sql);\r
193         }\r
194 }\r
195 \r
196 function set_theme_as_default ($theme_dir) {\r
197         global $msg, $db;\r
198         \r
199         //unset current default theme\r
200         $sql    = "UPDATE ".TABLE_PREFIX."themes SET status = '1' WHERE status = '2'";\r
201         $result = mysql_query($sql, $db);\r
202         \r
203         write_to_log(AT_ADMIN_LOG_UPDATE, 'themes', mysql_affected_rows($db), $sql);\r
204 \r
205         //set to default\r
206         $sql    = "UPDATE ".TABLE_PREFIX."themes SET status = '2' WHERE dir_name = '$theme_dir'";\r
207         $result = mysql_query($sql, $db);\r
208 \r
209         $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');\r
210         $feedback = array('THEME_DEFAULT', $theme_dir);\r
211         $msg->addFeedback($feedback);\r
212         $_SESSION['prefs']['PREF_THEME'] = $theme_dir;\r
213 \r
214         write_to_log(AT_ADMIN_LOG_UPDATE, 'themes', mysql_affected_rows($db), $sql);\r
215 }\r
216 \r
217 function delete_theme ($theme_dir) {\r
218         global $msg, $db;\r
219 \r
220         //check status\r
221         $sql    = "SELECT status FROM ".TABLE_PREFIX."themes WHERE dir_name='$theme_dir'";\r
222         $result = mysql_query ($sql, $db);\r
223         $row    = mysql_fetch_assoc($result);\r
224         $status = intval($row['status']);\r
225 \r
226         //can't delete original default or current default theme\r
227         if (($theme_dir == 'default') || ($status == 2)) {\r
228                 $msg->addError('THEME_NOT_DELETED');\r
229                 return FALSE;\r
230 \r
231         } else {        //disable, clear directory and delete theme from db\r
232 \r
233                 require (AT_INCLUDE_PATH . 'lib/filemanager.inc.php'); /* for clr_dir() */\r
234                 if ($status != 0) {\r
235                         disable_theme($theme_dir);\r
236                         $msg->deleteFeedback('THEME_DISABLED');\r
237                 }\r
238 \r
239                 $dir = '../../themes/' . $theme_dir;\r
240                 //chmod($dir, 0777);\r
241                 @clr_dir($dir);\r
242 \r
243                 $sql1    = "DELETE FROM ".TABLE_PREFIX."themes WHERE dir_name = '$theme_dir'";\r
244                 $result1 = mysql_query ($sql1, $db);\r
245 \r
246                 write_to_log(AT_ADMIN_LOG_DELETE, 'themes', mysql_affected_rows($db), $sql);\r
247 \r
248                 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');\r
249                 return TRUE;\r
250         }\r
251 }\r
252 \r
253 function export_theme($theme_dir) {\r
254         require(AT_INCLUDE_PATH.'classes/zipfile.class.php');                           /* for zipfile */\r
255         require(AT_INCLUDE_PATH.'classes/XML/XML_HTMLSax/XML_HTMLSax.php');     /* for XML_HTMLSax */\r
256         require('theme_template.inc.php');                                                                      /* for theme XML templates */ \r
257         \r
258         global $db;\r
259         \r
260         //identify current theme and then searches db for relavent info\r
261         $sql    = "SELECT * FROM ".TABLE_PREFIX."themes WHERE dir_name = '$theme_dir'";\r
262         $result = mysql_query($sql, $db);\r
263         $row    = mysql_fetch_assoc($result);\r
264 \r
265         $dir          = $row['dir_name'] . '/';\r
266         $title        = $row['title'];\r
267         $version      = $row['version'];\r
268         $last_updated = $row['last_updated'];\r
269         $extra_info   = $row['extra_info'];\r
270 \r
271 \r
272 \r
273         //generate 'theme_info.xml' file based on info  \r
274         $info_xml = str_replace(array('{TITLE}', '{VERSION}', '{LAST_UPDATED}', '{EXTRA_INFO}'), \r
275                                                         array($title, $version, $last_updated, $extra_info),\r
276                                             $theme_template_xml);\r
277 \r
278         //zip together all the contents of the folder along with the XML file\r
279         $zipfile = new zipfile();\r
280         $zipfile->create_dir($dir);\r
281 \r
282         //update installation folder\r
283         $dir1 = '../../themes/' . $dir;\r
284 \r
285         $zipfile->add_file($info_xml, $dir . 'theme_info.xml');\r
286 \r
287         /* zip other required files */\r
288         $zipfile->add_dir($dir1, $dir);\r
289 \r
290         /*close & send*/\r
291         $zipfile->close();\r
292         //Name the Zip file and sends to user for download\r
293         $zipfile->send_file(str_replace(array(' ', ':'), '_', $title));\r
294 }\r
295 \r
296 ?>