remove old readme
[atutor.git] / mods / _core / themes / lib / themes.inc.php
1 <?php\r
2 /****************************************************************************/\r
3 /* ATutor                                                                                                                                       */\r
4 /****************************************************************************/\r
5 /* Copyright (c) 2002-2010                                                  */\r
6 /* Inclusive Design Institute                                               */\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 ($type = "all") {\r
78         global $db;\r
79         \r
80         if ($type == MOBILE_DEVICE) {\r
81                 $where_clause = " AND type='".MOBILE_DEVICE."' ";\r
82         } else if ($type == DESKTOP_DEVICE) {\r
83                 $where_clause = " AND type='".DESKTOP_DEVICE."' ";\r
84         }\r
85         $sql    = "SELECT title FROM ".TABLE_PREFIX."themes WHERE (status = '1' OR status = '2' OR status = '3') ".$where_clause." ORDER BY title";\r
86         $result = mysql_query($sql, $db);\r
87         \r
88         //Get all theme names into array\r
89         $i = 0;\r
90         while ($row = mysql_fetch_array($result)) {\r
91                 $themes[$i] = $row['title'];\r
92                 $i++;\r
93         }\r
94         \r
95         return $themes;\r
96 }\r
97 \r
98 /**\r
99 * Gets number of enabled themes\r
100 * @access  private\r
101 * @return  int                          the number of enabled themes\r
102 * @author  Shozub Qureshi\r
103 */\r
104 function num_enabled_themes () {\r
105         global $db;\r
106         //Go to db\r
107         $sql    = "SELECT title FROM ".TABLE_PREFIX."themes WHERE status = '1' OR status = '2'";\r
108         $result = mysql_query($sql, $db);\r
109                 \r
110         return mysql_num_rows($result);\r
111 }\r
112 \r
113 /**\r
114 * Gets list of disabled themes\r
115 * @access  private\r
116 * @return  array                                the version of the theme\r
117 * @author  Shozub Qureshi\r
118 */\r
119 function get_disabled_themes () {\r
120         global $db;\r
121         //Go to db\r
122         $sql    = "SELECT title FROM ".TABLE_PREFIX."themes WHERE status = '0'";\r
123         $result = mysql_query($sql, $db);\r
124         \r
125         //Get all theme names into array\r
126         $i = 0;\r
127         while ($row = mysql_fetch_array($result)) {\r
128                 $themes[$i] = $row['title'];\r
129                 $i++;\r
130         }\r
131         \r
132         return $themes;\r
133 }\r
134 \r
135 /**\r
136 * Gets list of all currently installed themes\r
137 * @access  private\r
138 * @return  array                                the version of the theme\r
139 * @author  Shozub Qureshi\r
140 */\r
141 function get_all_themes () {\r
142         global $db;\r
143         \r
144         // The ordering is as follow. The default theme followed by ASC ordering of rest of themes\r
145         \r
146         // Assert, one of them must be a default\r
147         $result = mysql_query('SELECT title FROM ' . TABLE_PREFIX . 'themes WHERE status = 2', $db);\r
148         $row = mysql_fetch_assoc($result);\r
149         $first_one = $row['title'];\r
150         \r
151         $themes[$i] = $first_one;\r
152         \r
153         // Go to db\r
154         $sql    = "SELECT title FROM " . TABLE_PREFIX . "themes WHERE title != '$first_one' ORDER BY title ASC";\r
155         $result = mysql_query($sql, $db);\r
156         \r
157         // Get all theme names into array\r
158         $i = 1;\r
159         while ($row = mysql_fetch_assoc($result)) {\r
160                 $themes[$i] = $row['title'];\r
161                 $i++;\r
162         }\r
163         \r
164         return $themes;\r
165 }\r
166 \r
167 function enable_theme ($theme_dir) {\r
168         global $msg, $db;\r
169 \r
170         if ($_SESSION['prefs']['PREF_THEME'] != $theme_dir) {\r
171                 $sql = "UPDATE ".TABLE_PREFIX."themes SET status = '1' WHERE dir_name = '$theme_dir'";\r
172                 $result = mysql_query($sql, $db);\r
173                 write_to_log(AT_ADMIN_LOG_UPDATE, 'themes', mysql_affected_rows($db), $sql);\r
174         } \r
175         $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');\r
176 }\r
177 \r
178 function disable_theme ($theme_dir) {\r
179         global $msg, $db;\r
180 \r
181         $sql    = "SELECT status FROM ".TABLE_PREFIX."themes WHERE dir_name = '$theme_dir'";\r
182         $result = mysql_query ($sql, $db);\r
183         $row    = mysql_fetch_array($result);\r
184         $status = intval($row['status']);\r
185 \r
186         //If default theme, then it cannot be disabled\r
187         if ($status == 2) {\r
188                 $msg->addError('THEME_NOT_DISABLED');\r
189                 return;\r
190         } else {\r
191                 $sql    = "UPDATE ".TABLE_PREFIX."themes SET status = '0' WHERE dir_name = '$theme_dir'";\r
192                 $result = mysql_query($sql, $db);\r
193 \r
194                 $feedback = array('THEME_DISABLED', $theme_dir);\r
195                 $msg->addFeedback($feedback);\r
196 \r
197                 write_to_log(AT_ADMIN_LOG_UPDATE, 'themes', mysql_affected_rows($db), $sql);\r
198         }\r
199 }\r
200 \r
201 function set_theme_as_default ($theme_dir, $type) {\r
202         global $msg, $db;\r
203         \r
204         //unset current default theme\r
205         if ($type == MOBILE_DEVICE) {\r
206                 $default_status = 3;\r
207         } else {\r
208                 $default_status = 2;\r
209         }\r
210         $sql    = "UPDATE ".TABLE_PREFIX."themes SET status = 1 WHERE status = ".$default_status;\r
211         $result = mysql_query($sql, $db);\r
212         \r
213         write_to_log(AT_ADMIN_LOG_UPDATE, 'themes', mysql_affected_rows($db), $sql);\r
214 \r
215         //set to default\r
216         $sql    = "UPDATE ".TABLE_PREFIX."themes SET status = ".$default_status." WHERE dir_name = '$theme_dir'";\r
217         $result = mysql_query($sql, $db);\r
218 \r
219         $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');\r
220         $feedback = array('THEME_DEFAULT', $theme_dir);\r
221         $msg->addFeedback($feedback);\r
222 \r
223         //only over-ride the current theme iff it's not mobile themes.\r
224         if($type != MOBILE_DEVICE){\r
225                 $_SESSION['prefs']['PREF_THEME'] = $theme_dir;\r
226         }\r
227         write_to_log(AT_ADMIN_LOG_UPDATE, 'themes', mysql_affected_rows($db), $sql);\r
228 }\r
229 \r
230 function delete_theme ($theme_dir) {\r
231         global $msg, $db;\r
232 \r
233         //check status\r
234         $sql    = "SELECT status FROM ".TABLE_PREFIX."themes WHERE dir_name='$theme_dir'";\r
235         $result = mysql_query ($sql, $db);\r
236         $row    = mysql_fetch_assoc($result);\r
237         $status = intval($row['status']);\r
238 \r
239         //can't delete original default or current default theme\r
240         if (($theme_dir == 'default') || ($status == 2)) {\r
241                 $msg->addError('THEME_NOT_DELETED');\r
242                 return FALSE;\r
243 \r
244         } else {        //disable, clear directory and delete theme from db\r
245 \r
246                 require_once(AT_INCLUDE_PATH.'../mods/_core/file_manager/filemanager.inc.php'); /* for clr_dir() */\r
247                 if ($status != 0) {\r
248                         disable_theme($theme_dir);\r
249                         $msg->deleteFeedback('THEME_DISABLED');\r
250                 }\r
251 \r
252                 $dir = '../../../themes/' . $theme_dir;\r
253                 //chmod($dir, 0777);\r
254                 @clr_dir($dir);\r
255 \r
256                 $sql1    = "DELETE FROM ".TABLE_PREFIX."themes WHERE dir_name = '$theme_dir'";\r
257                 $result1 = mysql_query ($sql1, $db);\r
258 \r
259                 write_to_log(AT_ADMIN_LOG_DELETE, 'themes', mysql_affected_rows($db), $sql);\r
260 \r
261                 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');\r
262                 return TRUE;\r
263         }\r
264 }\r
265 \r
266 function export_theme($theme_dir) {\r
267         require(AT_INCLUDE_PATH.'classes/zipfile.class.php');                           /* for zipfile */\r
268         require(AT_INCLUDE_PATH.'classes/XML/XML_HTMLSax/XML_HTMLSax.php');     /* for XML_HTMLSax */\r
269         require('theme_template.inc.php');                                                                      /* for theme XML templates */ \r
270         \r
271         global $db;\r
272         \r
273         //identify current theme and then searches db for relavent info\r
274         $sql    = "SELECT * FROM ".TABLE_PREFIX."themes WHERE dir_name = '$theme_dir'";\r
275         $result = mysql_query($sql, $db);\r
276         $row    = mysql_fetch_assoc($result);\r
277 \r
278         $dir          = $row['dir_name'] . '/';\r
279         $title        = $row['title'];\r
280         $version      = $row['version'];\r
281         $type         = $row['type'];\r
282         $last_updated = $row['last_updated'];\r
283         $extra_info   = $row['extra_info'];\r
284 \r
285 \r
286 \r
287         //generate 'theme_info.xml' file based on info  \r
288         $info_xml = str_replace(array('{TITLE}', '{VERSION}', '{TYPE}', '{LAST_UPDATED}', '{EXTRA_INFO}'), \r
289                                                         array($title, $version, $type, $last_updated, $extra_info),\r
290                                             $theme_template_xml);\r
291 \r
292         //zip together all the contents of the folder along with the XML file\r
293         $zipfile = new zipfile();\r
294         $zipfile->create_dir($dir);\r
295 \r
296         //update installation folder\r
297         $dir1 = '../../../themes/' . $dir;\r
298 \r
299         $zipfile->add_file($info_xml, $dir . 'theme_info.xml');\r
300 \r
301         /* zip other required files */\r
302         $zipfile->add_dir($dir1, $dir);\r
303 \r
304         /*close & send*/\r
305         $zipfile->close();\r
306         //Name the Zip file and sends to user for download\r
307         $zipfile->send_file(str_replace(array(' ', ':'), '_', $title));\r
308 }\r
309 \r
310 ?>