settings.php 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266
  1. <?php // $Id: settings.php 22316 2009-07-23 16:18:33Z cfasanando $
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2009 Dokeos SPRL
  6. Copyright (c) 2003 Ghent University
  7. Copyright (c) Patrick Cool, Ghent University
  8. Copyright (c) Julio Montoya, Dokeos
  9. Copyright (c) Roan Embrechts, Vrije Universiteit Brussel
  10. Copyright (c) Bart Mollet, Hogeschool Gent
  11. For a full list of contributors, see "credits.txt".
  12. The full license can be read in "license.txt".
  13. This program is free software; you can redistribute it and/or
  14. modify it under the terms of the GNU General Public License
  15. as published by the Free Software Foundation; either version 2
  16. of the License, or (at your option) any later version.
  17. See the GNU General Public License for more details.
  18. Contact address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium
  19. Mail: info@dokeos.com
  20. ==============================================================================
  21. */
  22. /**
  23. ==============================================================================
  24. * With this tool you can easily adjust non critical configuration settings.
  25. * Non critical means that changing them will not result in a broken campus.
  26. *
  27. * @author Patrick Cool
  28. * @since Dokeos 1.6
  29. * @author Julio Montoya - Multiple URL site
  30. * @package dokeos.admin
  31. ==============================================================================
  32. */
  33. /*
  34. ==============================================================================
  35. INIT SECTION
  36. ==============================================================================
  37. */
  38. // name of the language file that needs to be included
  39. if ($_GET['category']=='Templates') {
  40. $language_file = array('admin','document');
  41. } else {
  42. $language_file = array('admin');
  43. }
  44. // resetting the course id
  45. $cidReset=true;
  46. // including some necessary dokeos files
  47. include_once ('../inc/global.inc.php');
  48. require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  49. require_once (api_get_path(LIBRARY_PATH).'fileManage.lib.php');
  50. require_once (api_get_path(LIBRARY_PATH).'fileUpload.lib.php');
  51. // setting the section (for the tabs)
  52. $this_section = SECTION_PLATFORM_ADMIN;
  53. $_SESSION['this_section'] = $this_section;
  54. // Access restrictions
  55. api_protect_admin_script();
  56. // Submit Stylesheets
  57. if (isset($_POST['submit_stylesheets']))
  58. {
  59. $message = store_stylesheets();
  60. header("Location: ".api_get_self()."?category=stylesheets");
  61. exit;
  62. }
  63. // Database Table Definitions
  64. $table_settings_current = Database :: get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  65. // setting breadcrumbs
  66. $interbreadcrumb[] = array ("url" => 'index.php', "name" => get_lang('PlatformAdmin'));
  67. // setting the name of the tool
  68. $tool_name = get_lang('DokeosConfigSettings');
  69. // Build the form
  70. if (!empty($_GET['category']) and !in_array($_GET['category'], array('Plugins', 'stylesheets', 'Search')))
  71. {
  72. $form = new FormValidator('settings', 'post', 'settings.php?category='.$_GET['category']);
  73. $renderer = & $form->defaultRenderer();
  74. $renderer->setHeaderTemplate('<div class="sectiontitle">{header}</div>'."\n");
  75. $renderer->setElementTemplate('<div class="sectioncomment">{label}</div>'."\n".'<div class="sectionvalue">{element}</div>'."\n");
  76. $my_category = mysql_real_escape_string($_GET['category']);
  77. $sqlcountsettings = "SELECT COUNT(*) FROM $table_settings_current WHERE category='".$my_category."' AND type<>'checkbox'";
  78. $resultcountsettings = api_sql_query($sqlcountsettings, __FILE__, __LINE__);
  79. $countsetting = mysql_fetch_array($resultcountsettings);
  80. if ($_configuration['access_url']==1)
  81. {
  82. $settings = api_get_settings($my_category,'group',$_configuration['access_url']);
  83. }
  84. else
  85. {
  86. $url_info = api_get_access_url($_configuration['access_url']);
  87. if ($url_info['active']==1)
  88. {
  89. //the default settings of Dokeos
  90. $settings = api_get_settings($my_category,'group',1,0);
  91. //the settings that are changeable from a particular site
  92. $settings_by_access = api_get_settings($my_category,'group',$_configuration['access_url'],1);
  93. //echo '<pre>';
  94. //print_r($settings_by_access);
  95. $settings_by_access_list=array();
  96. foreach($settings_by_access as $row)
  97. {
  98. if (empty($row['variable']))
  99. $row['variable']=0;
  100. if (empty($row['subkey']))
  101. $row['subkey']=0;
  102. if (empty($row['category']))
  103. $row['category']=0;
  104. // one more validation if is changeable
  105. if ($row['access_url_changeable']==1)
  106. $settings_by_access_list[ $row['variable'] ] [ $row['subkey'] ] [ $row['category'] ] = $row;
  107. else
  108. $settings_by_access_list[ $row['variable'] ] [ $row['subkey'] ] [ $row['category'] ] = array();
  109. }
  110. }
  111. }
  112. //print_r($settings_by_access_list);echo '</pre>';
  113. //$sqlsettings = "SELECT DISTINCT * FROM $table_settings_current WHERE category='$my_category' GROUP BY variable ORDER BY id ASC";
  114. //$resultsettings = api_sql_query($sqlsettings, __FILE__, __LINE__);
  115. //while ($row = mysql_fetch_array($resultsettings))
  116. $default_values = array();
  117. foreach($settings as $row) {
  118. if ($row['variable'] == 'search_enabled') { continue; }
  119. $anchor_name = $row['variable'].(!empty($row['subkey']) ? '_'.$row['subkey'] : '');
  120. $form->addElement('html',"\n<a name=\"$anchor_name\"></a>\n");
  121. ($countsetting['0']%10) < 5 ?$b=$countsetting['0']-10:$b=$countsetting['0'];
  122. if ($i % 10 == 0 and $i<$b){
  123. if ($_GET['category'] <> "Languages"){
  124. $form->addElement('html','<div align="right">');
  125. $form->addElement('style_submit_button', null,get_lang('SaveSettings'), 'class="save"');
  126. $form->addElement('html','</div>');
  127. }
  128. }
  129. $i++;
  130. $form->addElement('header', null, get_lang($row['title']));
  131. if ($row['access_url_changeable']=='1' && $_configuration['multiple_access_urls']==true) {
  132. $form->addElement('html', '<div style="float:right;">'.Display::return_icon('shared_setting.png',get_lang('SharedSettingIconComment')).'</div>');
  133. }
  134. $hideme=array();
  135. $hide_element=false;
  136. if ($_configuration['access_url']!=1)
  137. {
  138. if ($row['access_url_changeable']==0)
  139. {
  140. //we hide the element in other cases (checkbox, radiobutton) we 'freeze' the element
  141. $hide_element=true;
  142. $hideme=array('disabled');
  143. }
  144. elseif($url_info['active']==1)
  145. {
  146. // we show the elements
  147. if (empty($row['variable']))
  148. $row['variable']=0;
  149. if (empty($row['subkey']))
  150. $row['subkey']=0;
  151. if (empty($row['category']))
  152. $row['category']=0;
  153. if (is_array ($settings_by_access_list[ $row['variable'] ] [ $row['subkey'] ] [ $row['category'] ]))
  154. {
  155. // we are sure that the other site have a selected value
  156. if ($settings_by_access_list[ $row['variable'] ] [ $row['subkey'] ] [ $row['category'] ]['selected_value']!='')
  157. $row['selected_value'] =$settings_by_access_list[$row['variable']] [$row['subkey']] [ $row['category'] ]['selected_value'];
  158. }
  159. // there is no else because we load the default $row['selected_value'] of the main Dokeos site
  160. }
  161. }
  162. switch ($row['type']) {
  163. case 'textfield' :
  164. if ($row['variable']=='account_valid_duration') {
  165. $form->addElement('text', $row['variable'], get_lang($row['comment']),array('maxlength'=>'5'));
  166. $form->applyFilter($row['variable'],'html_filter');
  167. $default_values[$row['variable']] = $row['selected_value'];
  168. } else {
  169. $form->addElement('text', $row['variable'], get_lang($row['comment']),$hideme);
  170. $form->applyFilter($row['variable'],'html_filter');
  171. $default_values[$row['variable']] = $row['selected_value'];
  172. }
  173. break;
  174. case 'textarea' :
  175. $form->addElement('textarea', $row['variable'], get_lang($row['comment']),$hideme);
  176. $default_values[$row['variable']] = $row['selected_value'];
  177. break;
  178. case 'radio' :
  179. $values = get_settings_options($row['variable']);
  180. $group = array ();
  181. if (is_array($values )) {
  182. foreach ($values as $key => $value) {
  183. $element = & $form->createElement('radio', $row['variable'], '', get_lang($value['display_text']), $value['value']);
  184. if ($hide_element) {
  185. $element->freeze();
  186. }
  187. $group[] = $element;
  188. }
  189. }
  190. $form->addGroup($group, $row['variable'], get_lang($row['comment']), '<br />', false);
  191. $default_values[$row['variable']] = $row['selected_value'];
  192. break;
  193. case 'checkbox';
  194. //1. we collect all the options of this variable
  195. $sql = "SELECT * FROM settings_current WHERE variable='".$row['variable']."' AND access_url = 1";
  196. $result = api_sql_query($sql, __FILE__, __LINE__);
  197. $group = array ();
  198. while ($rowkeys = Database::fetch_array($result)) {
  199. if ($rowkeys['variable'] == 'course_create_active_tools' && $rowkeys['subkey'] == 'enable_search') {continue;}
  200. $element = & $form->createElement('checkbox', $rowkeys['subkey'], '', get_lang($rowkeys['subkeytext']));
  201. if ($row['access_url_changeable']==1) {
  202. //2. we look into the DB if there is a setting for a specific access_url
  203. $access_url = $_configuration['access_url'];
  204. if(empty($access_url )) $access_url =1;
  205. $sql = "SELECT selected_value FROM settings_current WHERE variable='".$rowkeys['variable']."' AND subkey='".$rowkeys['subkey']."' AND subkeytext='".$rowkeys['subkeytext']."' AND access_url = $access_url";
  206. $result_access = api_sql_query($sql, __FILE__, __LINE__);
  207. $row_access = Database::fetch_array($result_access);
  208. if ($row_access['selected_value'] == 'true' && ! $form->isSubmitted()) {
  209. $element->setChecked(true);
  210. }
  211. } else {
  212. if ($rowkeys['selected_value'] == 'true' && ! $form->isSubmitted()) {
  213. $element->setChecked(true);
  214. }
  215. }
  216. if ($hide_element) {
  217. $element->freeze();
  218. }
  219. $group[] = $element;
  220. }
  221. $form->addGroup($group, $row['variable'], get_lang($row['comment']), '<br />'."\n");
  222. break;
  223. case "link" :
  224. $form->addElement('static', null, get_lang($row['comment']), get_lang('CurrentValue').' : '.$row['selected_value'],$hideme);
  225. }
  226. }
  227. if ($_GET['category'] <> "Languages"){
  228. $form->addElement('html','<div align="right">');
  229. $form->addElement('style_submit_button', null,get_lang('SaveSettings'), 'class="save"');
  230. $form->addElement('html','</div>');
  231. }
  232. $form->setDefaults($default_values);
  233. if ($form->validate())
  234. {
  235. $values = $form->exportValues();
  236. // the first step is to set all the variables that have type=checkbox of the category
  237. // to false as the checkbox that is unchecked is not in the $_POST data and can
  238. // therefore not be set to false.
  239. // This, however, also means that if the process breaks on the third of five checkboxes, the others
  240. // will be set to false.
  241. $r = api_set_settings_category($my_category,'false',$_configuration['access_url']);
  242. //$sql = "UPDATE $table_settings_current SET selected_value='false' WHERE category='$my_category' AND type='checkbox'";
  243. //$result = api_sql_query($sql, __FILE__, __LINE__);
  244. // Save the settings
  245. $keys = array();
  246. foreach ($values as $key => $value)
  247. {
  248. if (!is_array($value))
  249. {
  250. //$sql = "UPDATE $table_settings_current SET selected_value='".mysql_real_escape_string($value)."' WHERE variable='$key'";
  251. //$result = api_sql_query($sql, __FILE__, __LINE__);
  252. if (api_get_setting($key) != $value) $keys[] = $key;
  253. $result = api_set_setting($key,$value,null,null,$_configuration['access_url']);
  254. }
  255. else
  256. {
  257. $sql = "SELECT subkey FROM $table_settings_current WHERE variable = '$key'";
  258. $res = api_sql_query($sql,__FILE__,__LINE__);
  259. $subkeys = array();
  260. while ($row_subkeys = Database::fetch_array($res)) {
  261. // if subkey is changed
  262. if ( (isset($value[$row_subkeys['subkey']]) && api_get_setting($key,$row_subkeys['subkey']) == 'false') ||
  263. (!isset($value[$row_subkeys['subkey']]) && api_get_setting($key,$row_subkeys['subkey']) == 'true')) {
  264. $keys[] = $key;
  265. break;
  266. }
  267. }
  268. foreach ($value as $subkey => $subvalue)
  269. {
  270. //$sql = "UPDATE $table_settings_current SET selected_value='true' WHERE variable='$key' AND subkey = '$subkey'";
  271. //$result = api_sql_query($sql, __FILE__, __LINE__);
  272. $result = api_set_setting($key,'true',$subkey,null,$_configuration['access_url']);
  273. }
  274. }
  275. }
  276. // add event configuration settings category to system log
  277. $time = time();
  278. $user_id = api_get_user_id();
  279. $category = $_GET['category'];
  280. event_system(LOG_CONFIGURATION_SETTINGS_CHANGE, LOG_CONFIGURATION_SETTINGS_CATEGORY, $category, $time, $user_id);
  281. // add event configuration settings variable to system log
  282. if (is_array($keys) && count($keys) > 0) {
  283. foreach($keys as $variable) {
  284. event_system(LOG_CONFIGURATION_SETTINGS_CHANGE, LOG_CONFIGURATION_SETTINGS_VARIABLE, $variable, $time, $user_id);
  285. }
  286. }
  287. header('Location: settings.php?action=stored&category='.Security::remove_XSS($_GET['category']));
  288. exit;
  289. }
  290. }
  291. // including the header (banner)
  292. Display :: display_header($tool_name);
  293. //api_display_tool_title($tool_name);
  294. // displaying the message that the settings have been stored
  295. if (!empty($_GET['action']) && $_GET['action'] == "stored")
  296. {
  297. Display :: display_normal_message(get_lang('SettingsStored'));
  298. }
  299. // the action images
  300. $action_images['platform'] = 'dokeos.gif';
  301. $action_images['course'] = 'course.gif';
  302. $action_images['tools'] = 'reference.gif';
  303. $action_images['user'] = 'students.gif';
  304. $action_images['gradebook'] = 'gradebook_eval_not_empty.gif';
  305. $action_images['ldap'] = 'loginmanager.gif';
  306. $action_images['security'] = 'passwordprotected.gif';
  307. $action_images['languages'] = 'forum.gif';
  308. $action_images['tuning'] = 'tuning.gif';
  309. $action_images['plugins'] = 'plugin.gif';
  310. $action_images['stylesheets'] = 'theme.gif';
  311. $action_images['templates'] = 'template.gif';
  312. $action_images['search'] = 'search.gif';
  313. // grabbing the categories
  314. //$selectcategories = "SELECT DISTINCT category FROM ".$table_settings_current." WHERE category NOT IN ('stylesheets','Plugins')";
  315. //$resultcategories = api_sql_query($selectcategories, __FILE__, __LINE__);
  316. $resultcategories = api_get_settings_categories(array('stylesheets','Plugins', 'Templates', 'Search'));
  317. echo "\n<div class=\"actions\">";
  318. //while ($row = mysql_fetch_array($resultcategories))
  319. foreach($resultcategories as $row)
  320. {
  321. echo "\n\t<a href=\"".api_get_self()."?category=".$row['category']."\">".Display::return_icon($action_images[strtolower($row['category'])], api_ucfirst(get_lang($row['category']))).api_ucfirst(get_lang($row['category']))."</a>";
  322. }
  323. echo "\n\t<a href=\"".api_get_self()."?category=Plugins\">".Display::return_icon($action_images['plugins'], api_ucfirst(get_lang('Plugins'))).api_ucfirst(get_lang('Plugins'))."</a>";
  324. echo "\n\t<a href=\"".api_get_self()."?category=stylesheets\">".Display::return_icon($action_images['stylesheets'], api_ucfirst(get_lang('Stylesheets'))).api_ucfirst(get_lang('Stylesheets'))."</a>";
  325. echo "\n\t<a href=\"".api_get_self()."?category=Templates\">".Display::return_icon($action_images['templates'], api_ucfirst(get_lang('Templates'))).api_ucfirst(get_lang('Templates'))."</a>";
  326. echo "\n\t<a href=\"".api_get_self()."?category=Search\">".Display::return_icon($action_images['search'], api_ucfirst(get_lang('Search'))).api_ucfirst(get_lang('Search'))."</a>";
  327. echo "\n</div>";
  328. if (isset ($_GET['category']))
  329. {
  330. switch ($_GET['category'])
  331. {
  332. // displaying the extensions: plugins
  333. // this will be available to all the sites (access_urls)
  334. case 'Plugins' :
  335. handle_plugins();
  336. break;
  337. // displaying the extensions: Stylesheets
  338. case 'stylesheets' :
  339. handle_stylesheets();
  340. break;
  341. case 'Search' :
  342. handle_search();
  343. break;
  344. case 'Templates' :
  345. handle_templates();
  346. break;
  347. default :
  348. $form->display();
  349. }
  350. }
  351. /*
  352. ==============================================================================
  353. FOOTER
  354. ==============================================================================
  355. */
  356. Display :: display_footer();
  357. /*
  358. ==============================================================================
  359. FUNCTIONS
  360. ==============================================================================
  361. */
  362. /**
  363. * The function that retrieves all the possible settings for a certain config setting
  364. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  365. */
  366. function get_settings_options($var)
  367. {
  368. $table_settings_options = Database :: get_main_table(TABLE_MAIN_SETTINGS_OPTIONS);
  369. $sql = "SELECT * FROM $table_settings_options WHERE variable='$var'";
  370. $result = api_sql_query($sql, __FILE__, __LINE__);
  371. while ($row = Database::fetch_array($result))
  372. {
  373. $temp_array = array ('value' => $row['value'], 'display_text' => $row['display_text']);
  374. $settings_options_array[] = $temp_array;
  375. }
  376. return $settings_options_array;
  377. }
  378. /**
  379. * This function allows easy activating and inactivating of plugins
  380. * @todo: a similar function needs to be written to activate or inactivate additional tools.
  381. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  382. */
  383. function handle_plugins()
  384. {
  385. global $SettingsStored;
  386. $userplugins = array();
  387. $table_settings_current = Database :: get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  388. if (isset($_POST['submit_plugins']))
  389. {
  390. store_plugins();
  391. // add event to system log
  392. $time = time();
  393. $user_id = api_get_user_id();
  394. $category = $_GET['category'];
  395. event_system(LOG_CONFIGURATION_SETTINGS_CHANGE, LOG_CONFIGURATION_SETTINGS_CATEGORY, $category, $time, $user_id);
  396. Display :: display_normal_message(get_lang('SettingsStored'));
  397. }
  398. echo get_lang('AvailablePlugins')."<br/><br/>";
  399. /* We scan the plugin directory. Each folder is a potential plugin. */
  400. $pluginpath = api_get_path(SYS_PLUGIN_PATH);
  401. $handle = @opendir($pluginpath);
  402. while (false !== ($file = readdir($handle)))
  403. {
  404. if ($file <> '.' AND $file <> '..' AND is_dir(api_get_path(SYS_PLUGIN_PATH).$file))
  405. {
  406. $possibleplugins[] = $file;
  407. }
  408. }
  409. @closedir($handle);
  410. /* for each of the possible plugin dirs we check if a file plugin.php (that contains all the needed information about this plugin)
  411. can be found in the dir.
  412. this plugin.php file looks like
  413. $plugin_info['title']='The title of the plugin'; //
  414. $plugin_info['comment']="Some comment about the plugin";
  415. $plugin_info['location']=array("loginpage_menu", "campushomepage_menu","banner"); // the possible locations where the plugins can be used
  416. $plugin_info['version']='0.1 alpha'; // The version number of the plugin
  417. $plugin_info['author']='Patrick Cool'; // The author of the plugin
  418. */
  419. echo '<form name="plugins" method="post" action="'.api_get_self().'?category='.$_GET['category'].'">';
  420. echo "<table class=\"data_table\">\n";
  421. echo "\t<tr>\n";
  422. echo "\t\t<th>\n";
  423. echo get_lang('Plugin');
  424. echo "\t\t</th>\n";
  425. echo "\t\t<th>\n";
  426. echo get_lang('LoginPageMainArea');
  427. echo "\t\t</th>\n";
  428. echo "\t\t<th>\n";
  429. echo get_lang('LoginPageMenu');
  430. echo "\t\t</th>\n";
  431. echo "\t\t<th>\n";
  432. echo get_lang('CampusHomepageMainArea');
  433. echo "\t\t</th>\n";
  434. echo "\t\t<th>\n";
  435. echo get_lang('CampusHomepageMenu');
  436. echo "\t\t</th>\n";
  437. echo "\t\t<th>\n";
  438. echo get_lang('MyCoursesMainArea');
  439. echo "\t\t</th>\n";
  440. echo "\t\t<th>\n";
  441. echo get_lang('MyCoursesMenu');
  442. echo "\t\t</th>\n";
  443. echo "\t\t<th>\n";
  444. echo get_lang('Header');
  445. echo "\t\t</th>\n";
  446. echo "\t\t<th>\n";
  447. echo get_lang('Footer');
  448. echo "\t\t</th>\n";
  449. echo "\t</tr>\n";
  450. /* We retrieve all the active plugins. */
  451. //$sql = "SELECT * FROM $table_settings_current WHERE category='Plugins'";
  452. //$result = api_sql_query($sql);
  453. $result = api_get_settings('Plugins');
  454. //while ($row = mysql_fetch_array($result))
  455. foreach($result as $row)
  456. {
  457. $usedplugins[$row['variable']][] = $row['selected_value'];
  458. }
  459. /* We display all the possible plugins and the checkboxes */
  460. foreach ($possibleplugins as $testplugin)
  461. {
  462. $plugin_info_file = api_get_path(SYS_PLUGIN_PATH).$testplugin."/plugin.php";
  463. if (file_exists($plugin_info_file))
  464. {
  465. $plugin_info = array();
  466. include ($plugin_info_file);
  467. echo "\t<tr>\n";
  468. echo "\t\t<td>\n";
  469. foreach ($plugin_info as $key => $value)
  470. {
  471. if ($key <> 'location')
  472. {
  473. if ($key == 'title')
  474. {
  475. $value = '<strong>'.$value.'</strong>';
  476. }
  477. echo get_lang(ucwords($key)).': '.$value.'<br />';
  478. }
  479. }
  480. if (file_exists(api_get_path(SYS_PLUGIN_PATH).$testplugin.'/readme.txt'))
  481. {
  482. echo "<a href='".api_get_path(WEB_PLUGIN_PATH).$testplugin."/readme.txt'>readme.txt</a>";
  483. }
  484. echo "\t\t</td>\n";
  485. // column: LoginPageMainArea
  486. if(empty($usedplugins))
  487. {
  488. $usedplugins = array();
  489. }
  490. display_plugin_cell('loginpage_main', $plugin_info, $testplugin, $usedplugins);
  491. display_plugin_cell('loginpage_menu', $plugin_info, $testplugin, $usedplugins);
  492. display_plugin_cell('campushomepage_main', $plugin_info, $testplugin, $usedplugins);
  493. display_plugin_cell('campushomepage_menu', $plugin_info, $testplugin, $usedplugins);
  494. display_plugin_cell('mycourses_main', $plugin_info, $testplugin, $usedplugins);
  495. display_plugin_cell('mycourses_menu', $plugin_info, $testplugin, $usedplugins);
  496. display_plugin_cell('header', $plugin_info, $testplugin, $usedplugins);
  497. display_plugin_cell('footer', $plugin_info, $testplugin, $usedplugins);
  498. echo "\t</tr>\n";
  499. }
  500. }
  501. echo '</table>';
  502. echo '<button class="save" type="submit" name="submit_plugins">'.get_lang('EnablePlugins').'</button></form>';
  503. }
  504. function display_plugin_cell($location, $plugin_info, $current_plugin, $active_plugins)
  505. {
  506. echo "\t\t<td align=\"center\">\n";
  507. if (in_array($location, $plugin_info['location']))
  508. {
  509. if (isset($active_plugins[$location]) && is_array($active_plugins[$location])
  510. && in_array($current_plugin, $active_plugins[$location]))
  511. {
  512. $checked = "checked";
  513. }
  514. else
  515. {
  516. $checked = '';
  517. }
  518. echo '<input type="checkbox" name="'.$current_plugin.'-'.$location.'" value="true" '.$checked.'/>';
  519. }
  520. echo "\t\t</td>\n";
  521. }
  522. /**
  523. * This function allows the platform admin to choose the default stylesheet
  524. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  525. */
  526. function handle_stylesheets()
  527. {
  528. global $_configuration;
  529. // Current style
  530. $currentstyle = api_get_setting('stylesheets');
  531. $is_style_changeable=false;
  532. if ($_configuration['access_url']!=1)
  533. {
  534. $style_info = api_get_settings('stylesheets','',1,0);
  535. $url_info = api_get_access_url($_configuration['access_url']);
  536. if ($style_info[0]['access_url_changeable']==1 && $url_info['active']==1)
  537. {
  538. $is_style_changeable=true;
  539. echo '<div class="actions" id="stylesheetuploadlink">';
  540. Display::display_icon('theme_add.gif');
  541. echo '<a href="" onclick="document.getElementById(\'newstylesheetform\').style.display = \'block\'; document.getElementById(\'stylesheetuploadlink\').style.display = \'none\';return false; ">'.get_lang('UploadNewStylesheet').'</a>';
  542. echo '</div>';
  543. }
  544. }
  545. else
  546. {
  547. $is_style_changeable=true;
  548. echo '<div class="actions" id="stylesheetuploadlink">';
  549. Display::display_icon('theme_add.gif');
  550. echo '<a href="" onclick="document.getElementById(\'newstylesheetform\').style.display = \'block\'; document.getElementById(\'stylesheetuploadlink\').style.display = \'none\';return false; ">'.get_lang('UploadNewStylesheet').'</a>';
  551. echo '</div>';
  552. }
  553. $form = new FormValidator('stylesheet_upload','post','settings.php?category=stylesheets&showuploadform=true');
  554. $form->addElement('text','name_stylesheet',get_lang('NameStylesheet'),array('size' => '40', 'maxlength' => '40'));
  555. $form->addRule('name_stylesheet', get_lang('ThisFieldIsRequired'), 'required');
  556. $form->addElement('file', 'new_stylesheet', get_lang('UploadNewStylesheet'));
  557. $allowed_file_types = array ('css');
  558. $form->addRule('new_stylesheet', get_lang('InvalidExtension').' ('.implode(',', $allowed_file_types).')', 'filetype', $allowed_file_types);
  559. $form->addRule('new_stylesheet', get_lang('ThisFieldIsRequired'), 'required');
  560. $form->addElement('style_submit_button', 'stylesheet_upload', get_lang('Ok'), array('class'=>'save'));
  561. if( $form->validate() AND is_writable(api_get_path(SYS_CODE_PATH).'css/'))
  562. {
  563. $values = $form->exportValues();
  564. $picture_element = & $form->getElement('new_stylesheet');
  565. $picture = $picture_element->getValue();
  566. upload_stylesheet($values, $picture);
  567. // add event to system log
  568. $time = time();
  569. $user_id = api_get_user_id();
  570. $category = $_GET['category'];
  571. event_system(LOG_CONFIGURATION_SETTINGS_CHANGE, LOG_CONFIGURATION_SETTINGS_CATEGORY, $category, $time, $user_id);
  572. Display::display_confirmation_message(get_lang('StylesheetAdded'));
  573. }
  574. else
  575. {
  576. if (!is_writable(api_get_path(SYS_CODE_PATH).'css/'))
  577. {
  578. Display::display_error_message(api_get_path(SYS_CODE_PATH).'css/'.get_lang('IsNotWritable'));
  579. }
  580. else
  581. {
  582. if ($_GET['showuploadform'] == 'true')
  583. {
  584. echo '<div id="newstylesheetform">';
  585. }
  586. else
  587. {
  588. echo '<div id="newstylesheetform" style="display: none;">';
  589. }
  590. // uploading a new stylesheet
  591. if ($_configuration['access_url']==1)
  592. {
  593. $form->display();
  594. }
  595. else
  596. {
  597. if ($is_style_changeable)
  598. {
  599. $form->display();
  600. }
  601. }
  602. echo '</div>';
  603. }
  604. }
  605. // Preview of the stylesheet
  606. echo '<div><iframe src="style_preview.php" width="100%" height="300" name="preview"></iframe></div>';
  607. echo '<form name="stylesheets" method="post" action="'.api_get_self().'?category='.Security::remove_XSS($_GET['category']).'">';
  608. if ($handle = @opendir(api_get_path(SYS_PATH).'main/css/'))
  609. {
  610. $counter=1;
  611. while (false !== ($style_dir = readdir($handle)))
  612. {
  613. if(substr($style_dir,0,1)=='.') //skip dirs starting with a '.'
  614. {
  615. continue;
  616. }
  617. $dirpath = api_get_path(SYS_PATH).'main/css/'.$style_dir;
  618. if (is_dir($dirpath))
  619. {
  620. if ($style_dir != '.' && $style_dir != '..')
  621. {
  622. if ($currentstyle == $style_dir OR ($style_dir == 'dokeos_classic' AND !$currentstyle))
  623. {
  624. $selected = 'checked="checked"';
  625. }
  626. else
  627. {
  628. $selected = '';
  629. }
  630. $show_name=get_lang(str_replace(' ','', ucwords(str_replace('_',' ', $style_dir))), '');
  631. if ($is_style_changeable)
  632. {
  633. echo "<input type=\"radio\" name=\"style\" value=\"".$style_dir."\" ".$selected." onClick=\"parent.preview.location='style_preview.php?style=".$style_dir."';\"/>";
  634. echo '<a href="style_preview.php?style='.$style_dir.'" target="preview">'.$show_name.'</a>';
  635. }
  636. else
  637. echo '<a href="style_preview.php?style='.$style_dir.'" target="preview">'.$show_name.'</a>';
  638. //echo '<div id="Layer'.$counter.'" style="position:relative; width:687px; z-index:2; visibility: hidden;">';
  639. //echo '<a href="#" onClick="MM_showHideLayers(\'Layer'.$counter.'\',\'\',\'hide\')">'.get_lang('Close').'</a>';
  640. //echo '<iframe src="style_preview.php?style='.$file.'" width="100%" style="float:right;"></iframe></div>';
  641. echo "<br />\n";
  642. $counter++;
  643. }
  644. }
  645. }
  646. @closedir($handle);
  647. }
  648. if ($is_style_changeable)
  649. {
  650. echo '<button class="save" type="submit" name="submit_stylesheets"> '.get_lang('SaveSettings').' </button></form>';
  651. }
  652. }
  653. /**
  654. * creates the folder (if needed) and uploads the stylesheet in it
  655. *
  656. * @param array $values the values of the form
  657. * @param array $picture the values of the uploaded file
  658. *
  659. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  660. * @version May 2008
  661. * @since Dokeos 1.8.5
  662. */
  663. function upload_stylesheet($values,$picture)
  664. {
  665. // valid name for the stylesheet folder
  666. $style_name = api_ereg_replace("[^A-Za-z0-9]", "", $values['name_stylesheet'] );
  667. // create the folder if needed
  668. if(!is_dir(api_get_path(SYS_CODE_PATH).'css/'.$style_name.'/'))
  669. {
  670. if(mkdir(api_get_path(SYS_CODE_PATH).'css/'.$style_name.'/'))
  671. {
  672. $perm = api_get_setting('permissions_for_new_directories');
  673. $perm = octdec(!empty($perm)?$perm:'0770');
  674. chmod(api_get_path(SYS_CODE_PATH).'css/'.$style_name.'/', $perm);
  675. }
  676. }
  677. // move the file in the folder
  678. move_uploaded_file($picture['tmp_name'], api_get_path(SYS_CODE_PATH).'css/'.$style_name.'/'.$picture['name']);
  679. }
  680. /**
  681. * This function allows easy activating and inactivating of plugins
  682. * @todo: a similar function needs to be written to activate or inactivate additional tools.
  683. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  684. */
  685. function store_plugins()
  686. {
  687. $table_settings_current = Database :: get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  688. global $_configuration;
  689. // Step 1 : we remove all the plugins
  690. //$sql = "DELETE FROM $table_settings_current WHERE category='Plugins'";
  691. //api_sql_query($sql, __LINE__, __FILE__);
  692. $r = api_delete_category_settings('Plugins',$_configuration['access_url']);
  693. // step 2: looping through all the post values we only store these which are really a valid plugin location.
  694. foreach ($_POST as $form_name => $formvalue)
  695. {
  696. $form_name_elements = explode("-", $form_name);
  697. if (is_valid_plugin_location($form_name_elements[1]))
  698. {
  699. //$sql = "INSERT into $table_settings_current (variable,category,selected_value) VALUES ('".$form_name_elements['1']."','Plugins','".$form_name_elements['0']."')";
  700. //api_sql_query($sql, __LINE__, __FILE__);
  701. api_add_setting($form_name_elements['0'],$form_name_elements['1'],$form_name_elements['0'],null,'Plugins',$form_name_elements['0'],null,null,null,$_configuration['access_url'],1);
  702. }
  703. }
  704. }
  705. /**
  706. * Check if the post information is really a valid plugin location.
  707. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  708. */
  709. function is_valid_plugin_location($location)
  710. {
  711. $valid_locations=array('loginpage_main', 'loginpage_menu', 'campushomepage_main', 'campushomepage_menu', 'mycourses_main', 'mycourses_menu','header', 'footer');
  712. if (in_array($location, $valid_locations))
  713. {
  714. return true;
  715. }
  716. else
  717. {
  718. return false;
  719. }
  720. }
  721. /**
  722. * This function allows the platform admin to choose which should be the default stylesheet
  723. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  724. */
  725. function store_stylesheets()
  726. {
  727. global $_configuration;
  728. // Database Table Definitions
  729. $table_settings_current = Database :: get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  730. // Insert the stylesheet
  731. $style = Database::escape_string($_POST['style']);
  732. if (is_style($style))
  733. {
  734. /*
  735. $sql = 'UPDATE '.$table_settings_current.' SET
  736. selected_value = "'.$style.'"
  737. WHERE variable = "stylesheets"
  738. AND category = "stylesheets"';
  739. api_sql_query($sql, __LINE__, __FILE__);
  740. */
  741. api_set_setting('stylesheets',$style,null,'stylesheets',$_configuration['access_url']);
  742. }
  743. return true;
  744. }
  745. /**
  746. * This function checks if the given style is a recognize style that exists in the css directory as
  747. * a standalone directory.
  748. * @param string Style
  749. * @return bool True if this style is recognized, false otherwise
  750. */
  751. function is_style($style)
  752. {
  753. $dir = api_get_path(SYS_PATH).'main/css/';
  754. $dirs = scandir($dir);
  755. $style = str_replace(array('/','\\'),array('',''),$style); //avoid slashes or backslashes
  756. if (in_array($style,$dirs) && is_dir($dir.$style))
  757. {
  758. return true;
  759. }
  760. return false;
  761. }
  762. /**
  763. * Search options
  764. * TODO: support for multiple site. aka $_configuration['access_url'] == 1
  765. * @author Marco Villegas <marvil07@gmail.com>
  766. */
  767. function handle_search() {
  768. global $SettingsStored, $_configuration;
  769. $search_enabled = api_get_setting('search_enabled');
  770. $settings = api_get_settings('Search');
  771. if ($search_enabled !== 'true' || count($settings) < 1) {
  772. Display::display_error_message(get_lang('SearchFeatureNotEnabledComment'));
  773. return;
  774. }
  775. require_once api_get_path(LIBRARY_PATH) . 'specific_fields_manager.lib.php';
  776. $form = new FormValidator('search-options', 'post', api_get_self().'?category=Search');
  777. $renderer = & $form->defaultRenderer();
  778. $renderer->setHeaderTemplate('<div class="sectiontitle">{header}</div>'."\n");
  779. $renderer->setElementTemplate('<div class="sectioncomment">{label}</div>'."\n".'<div class="sectionvalue">{element}</div>'."\n");
  780. //search_show_unlinked_results
  781. $form->addElement('header', null, get_lang('SearchShowUnlinkedResultsTitle'));
  782. $form->addElement('label', null, get_lang('SearchShowUnlinkedResultsComment'));
  783. $values = get_settings_options('search_show_unlinked_results');
  784. $group = array ();
  785. foreach ($values as $key => $value) {
  786. $element = & $form->createElement('radio', 'search_show_unlinked_results', '', get_lang($value['display_text']), $value['value']);
  787. $group[] = $element;
  788. }
  789. $form->addGroup($group, 'search_show_unlinked_results', get_lang('SearchShowUnlinkedResultsComment'), '<br />', false);
  790. $default_values['search_show_unlinked_results'] = api_get_setting('search_show_unlinked_results');
  791. //search_prefilter_prefix
  792. $form->addElement('header', null, get_lang('SearchPrefilterPrefix'));
  793. $form->addElement('label', null, get_lang('SearchPrefilterPrefixComment'));
  794. $specific_fields = get_specific_field_list();
  795. $sf_values = array();
  796. foreach ($specific_fields as $sf) {
  797. $sf_values[$sf['code']] = $sf['name'];
  798. }
  799. $group = array ();
  800. $form->addElement('select', 'search_prefilter_prefix', get_lang('SearchPrefilterPrefix'), $sf_values, '');
  801. $default_values['search_prefilter_prefix'] = api_get_setting('search_prefilter_prefix');
  802. //$form->addRule('search_show_unlinked_results', get_lang('ThisFieldIsRequired'), 'required');
  803. $form->addElement('style_submit_button', 'search-options-save', get_lang('Ok'));
  804. $form->setDefaults($default_values);
  805. if( $form->validate()) {
  806. $formvalues = $form->exportValues();
  807. $r = api_set_settings_category('Search','false',$_configuration['access_url']);
  808. // Save the settings
  809. foreach ($formvalues as $key => $value)
  810. {
  811. $result = api_set_setting($key,$value,null,null);
  812. }
  813. Display :: display_normal_message($SettingsStored);
  814. } else {
  815. echo '<div id="search-options-form">';
  816. $form->display();
  817. echo '</div>';
  818. }
  819. }
  820. /**
  821. * wrapper for the templates
  822. *
  823. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  824. * @version August 2008
  825. * @since Dokeos 1.8.6
  826. */
  827. function handle_templates()
  828. {
  829. if ($_GET['action'] != 'add') {
  830. echo "\n<div class=\"actions\" style=\"margin-left:1px\" >";
  831. echo '<a href="settings.php?category=Templates&amp;action=add">'.Display::return_icon('template_add.gif', get_lang('AddTemplate')).get_lang('AddTemplate').'</a>';
  832. echo "\n</div>";
  833. }
  834. if ($_GET['action'] == 'add' OR ( $_GET['action'] == 'edit' AND is_numeric($_GET['id']))) {
  835. add_edit_template();
  836. // add event to system log
  837. $time = time();
  838. $user_id = api_get_user_id();
  839. $category = $_GET['category'];
  840. event_system(LOG_CONFIGURATION_SETTINGS_CHANGE, LOG_CONFIGURATION_SETTINGS_CATEGORY, $category, $time, $user_id);
  841. } else {
  842. if ($_GET['action'] == 'delete' and is_numeric($_GET['id'])) {
  843. delete_template($_GET['id']);
  844. // add event to system log
  845. $time = time();
  846. $user_id = api_get_user_id();
  847. $category = $_GET['category'];
  848. event_system(LOG_CONFIGURATION_SETTINGS_CHANGE, LOG_CONFIGURATION_SETTINGS_CATEGORY, $category, $time, $user_id);
  849. }
  850. display_templates();
  851. }
  852. }
  853. /**
  854. * Display a sortable table with all the templates that the platform administrator has defined.
  855. *
  856. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  857. * @version August 2008
  858. * @since Dokeos 1.8.6
  859. */
  860. function display_templates()
  861. {
  862. $table = new SortableTable('templates', 'get_number_of_templates', 'get_template_data',1);
  863. $table->set_additional_parameters(array('category'=>Security::remove_XSS($_GET['category'])));
  864. $table->set_header(0, get_lang('Image'), true, array ('style' => 'width:101px;'));
  865. $table->set_header(1, get_lang('Title'));
  866. $table->set_header(2, get_lang('Actions'), false, array ('style' => 'width:50px;'));
  867. $table->set_column_filter(2,'actions_filter');
  868. $table->set_column_filter(0,'image_filter');
  869. $table->display();
  870. }
  871. /**
  872. * Get the number of templates that are defined by the platform admin.
  873. *
  874. * @return integer
  875. *
  876. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  877. * @version August 2008
  878. * @since Dokeos 1.8.6
  879. */
  880. function get_number_of_templates()
  881. {
  882. // Database table definition
  883. $table_system_template = Database :: get_main_table('system_template');
  884. // The sql statement
  885. $sql = "SELECT COUNT(id) AS total FROM $table_system_template";
  886. $result = api_sql_query($sql, __FILE__, __LINE__);
  887. $row = Database::fetch_array($result);
  888. // returning the number of templates
  889. return $row['total'];
  890. }
  891. /**
  892. * Get all the template data for the sortable table
  893. *
  894. * @param integer $from the start of the limit statement
  895. * @param integer $number_of_items the number of elements that have to be retrieved from the database
  896. * @param integer $column the column that is
  897. * @param string $direction the sorting direction (ASC or DESC�
  898. * @return array
  899. *
  900. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  901. * @version August 2008
  902. * @since Dokeos 1.8.6
  903. */
  904. function get_template_data($from, $number_of_items, $column, $direction)
  905. {
  906. // Database table definition
  907. $table_system_template = Database :: get_main_table('system_template');
  908. // the sql statement
  909. $sql = "SELECT image as col0, title as col1, id as col2 FROM $table_system_template";
  910. $sql .= " ORDER BY col$column $direction ";
  911. $sql .= " LIMIT $from,$number_of_items";
  912. $result = api_sql_query($sql, __FILE__, __LINE__);
  913. while ($row = Database::fetch_array($result)) {
  914. $row['1'] = get_lang($row['1']);
  915. $return[]=$row;
  916. }
  917. // returning all the information for the sortable table
  918. return $return;
  919. }
  920. /**
  921. * display the edit and delete icons in the sortable table
  922. *
  923. * @param integer $id the id of the template
  924. * @return html code for the link to edit and delete the template
  925. *
  926. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  927. * @version August 2008
  928. * @since Dokeos 1.8.6
  929. */
  930. function actions_filter($id)
  931. {
  932. $return .= '<a href="settings.php?category=Templates&amp;action=edit&amp;id='.Security::remove_XSS($id).'">'.Display::return_icon('edit.gif', get_lang('Edit')).'</a>';
  933. $return .= '<a href="settings.php?category=Templates&amp;action=delete&amp;id='.Security::remove_XSS($id).'" onclick="javascript:if(!confirm('."'".get_lang("ConfirmYourChoice")."'".')) return false;">'.Display::return_icon('delete.gif', get_lang('Delete')).'</a>';
  934. return $return;
  935. }
  936. /**
  937. * Display the image of the template in the sortable table
  938. *
  939. * @param string $image the image
  940. * @return html code for the image
  941. *
  942. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  943. * @version August 2008
  944. * @since Dokeos 1.8.6
  945. */
  946. function image_filter($image)
  947. {
  948. if (!empty($image))
  949. {
  950. return '<img src="'.api_get_path(WEB_PATH).'home/default_platform_document/template_thumb/'.$image.'" alt="'.get_lang('TemplatePreview').'"/>';
  951. }
  952. else
  953. {
  954. return '<img src="'.api_get_path(WEB_PATH).'home/default_platform_document/template_thumb/noimage.gif" alt="'.get_lang('NoTemplatePreview').'"/>';
  955. }
  956. }
  957. /**
  958. * Add (or edit) a template. This function displays the form and also takes care of uploading the image and storing the information in the database
  959. *
  960. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  961. * @version August 2008
  962. * @since Dokeos 1.8.6
  963. */
  964. function add_edit_template()
  965. {
  966. // initiate the object
  967. $form = new FormValidator('template', 'post', 'settings.php?category=Templates&action='.$_GET['action'].'&id='.$_GET['id']);
  968. // settting the form elements: the header
  969. if ($_GET['action'] == 'add') {
  970. $title = get_lang('AddTemplate');
  971. }
  972. else
  973. {
  974. $title = get_lang('EditTemplate');
  975. }
  976. $form->addElement('header', '', $title);
  977. // settting the form elements: the title of the template
  978. $form->add_textfield('title', get_lang('Title'), false);
  979. // settting the form elements: the content of the template (wysiwyg editor)
  980. $form->addElement('html_editor', 'template_text', get_lang('Text'), null, array('ToolbarSet' => 'AdminTemplates', 'Width' => '100%', 'Height' => '400'));
  981. // settting the form elements: the form to upload an image to be used with the template
  982. $form->addElement('file','template_image',get_lang('Image'),'');
  983. // settting the form elements: a little bit information about the template image
  984. $form->addElement('static', 'file_comment', '', get_lang('TemplateImageComment100x70'));
  985. // getting all the information of the template when editing a template
  986. if ($_GET['action'] == 'edit') {
  987. // Database table definition
  988. $table_system_template = Database :: get_main_table('system_template');
  989. $sql = "SELECT * FROM $table_system_template WHERE id = '".Database::escape_string($_GET['id'])."'";
  990. $result = api_sql_query($sql, __FILE__, __LINE__);
  991. $row = Database::fetch_array($result);
  992. $defaults['template_id'] = intval($_GET['id']);
  993. $defaults['template_text'] = $row['content'];
  994. //forcing a get_lang
  995. $defaults['title'] = get_lang($row['title']);
  996. // adding an extra field: a hidden field with the id of the template we are editing
  997. $form->addElement('hidden','template_id');
  998. // adding an extra field: a preview of the image that is currently used
  999. if (!empty($row['image']))
  1000. {
  1001. $form->addElement('static','template_image_preview', '', '<img src="'.api_get_path(WEB_PATH).'home/default_platform_document/template_thumb/'.$row['image'].'" alt="'.get_lang('TemplatePreview').'"/>');
  1002. }
  1003. else
  1004. {
  1005. $form->addElement('static','template_image_preview', '', '<img src="'.api_get_path(WEB_PATH).'home/default_platform_document/template_thumb/noimage.gif" alt="'.get_lang('NoTemplatePreview').'"/>');
  1006. }
  1007. // setting the information of the template that we are editing
  1008. $form->setDefaults($defaults);
  1009. }
  1010. // settting the form elements: the submit button
  1011. $form->addElement('style_submit_button' , 'submit', get_lang('Ok') ,'class="save"');
  1012. // setting the rules: the required fields
  1013. $form->addRule('title', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  1014. $form->addRule('template_text', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  1015. // if the form validates (complies to all rules) we save the information, else we display the form again (with error message if needed)
  1016. if( $form->validate() )
  1017. {
  1018. $check = Security::check_token('post');
  1019. if ($check) {
  1020. // exporting the values
  1021. $values = $form->exportValues();
  1022. // upload the file
  1023. if (!empty($_FILES['template_image']['name']))
  1024. {
  1025. include_once (api_get_path(LIBRARY_PATH).'fileUpload.lib.php');
  1026. $upload_ok = process_uploaded_file($_FILES['template_image']);
  1027. if ($upload_ok)
  1028. {
  1029. // Try to add an extension to the file if it hasn't one
  1030. $new_file_name = add_ext_on_mime(stripslashes($_FILES['template_image']['name']), $_FILES['template_image']['type']);
  1031. // upload dir
  1032. $upload_dir = api_get_path(SYS_PATH).'home/default_platform_document/template_thumb/';
  1033. // create dir if not exists
  1034. if (!is_dir($upload_dir)) {
  1035. $perm = api_get_setting('permissions_for_new_directories');
  1036. $perm = octdec(!empty($perm)?$perm:'0770');
  1037. $res = @mkdir($upload_dir,$perm);
  1038. }
  1039. // resize image to max default and upload
  1040. require_once (api_get_path(LIBRARY_PATH).'image.lib.php');
  1041. $temp = new image($_FILES['template_image']['tmp_name']);
  1042. $picture_infos=@getimagesize($_FILES['template_image']['tmp_name']);
  1043. $max_width_for_picture = 100;
  1044. if ($picture_infos[0]>$max_width_for_picture)
  1045. {
  1046. $thumbwidth = $max_width_for_picture;
  1047. if (empty($thumbwidth) or $thumbwidth==0) {
  1048. $thumbwidth=$max_width_for_picture;
  1049. }
  1050. $new_height = round(($thumbwidth/$picture_infos[0])*$picture_infos[1]);
  1051. $temp->resize($thumbwidth,$new_height,0);
  1052. }
  1053. $type=$picture_infos[2];
  1054. switch (!empty($type))
  1055. {
  1056. case 2 : $temp->send_image('JPG',$upload_dir.$new_file_name);
  1057. break;
  1058. case 3 : $temp->send_image('PNG',$upload_dir.$new_file_name);
  1059. break;
  1060. case 1 : $temp->send_image('GIF',$upload_dir.$new_file_name);
  1061. break;
  1062. }
  1063. }
  1064. }
  1065. // store the information in the database (as insert or as update)
  1066. $table_system_template = Database :: get_main_table('system_template');
  1067. if ($_GET['action'] == 'add') {
  1068. $content_template = '<head>{CSS}<style type="text/css">.text{font-weight: normal;}</style></head><body>'.Database::escape_string($values['template_text']).'</body>';
  1069. $sql = "INSERT INTO $table_system_template (title, content, image) VALUES ('".Database::escape_string($values['title'])."','".$content_template."','".Database::escape_string($new_file_name)."')";
  1070. $result = api_sql_query($sql, __FILE__, __LINE__);
  1071. // display a feedback message
  1072. Display::display_confirmation_message(get_lang('TemplateAdded'));
  1073. echo '<a href="settings.php?category=Templates&amp;action=add">'.Display::return_icon('template_add.gif', get_lang('AddTemplate')).get_lang('AddTemplate').'</a>';
  1074. } else {
  1075. $content_template = '<head>{CSS}<style type="text/css">.text{font-weight: normal;}</style></head><body>'.Database::escape_string($values['template_text']).'</body>';
  1076. $sql = "UPDATE $table_system_template set title = '".Database::escape_string($values['title'])."',
  1077. content = '".$content_template."'";
  1078. if (!empty($new_file_name))
  1079. {
  1080. $sql .= ", image = '".Database::escape_string($new_file_name)."'";
  1081. }
  1082. $sql .= " WHERE id='".Database::escape_string($_GET['id'])."'";
  1083. $result = api_sql_query($sql, __FILE__, __LINE__);
  1084. // display a feedback message
  1085. Display::display_confirmation_message(get_lang('TemplateEdited'));
  1086. }
  1087. }
  1088. Security::clear_token();
  1089. display_templates();
  1090. }
  1091. else
  1092. {
  1093. $token = Security::get_token();
  1094. $form->addElement('hidden','sec_token');
  1095. $form->setConstants(array('sec_token' => $token));
  1096. // display the form
  1097. $form->display();
  1098. }
  1099. }
  1100. /**
  1101. * Delete a template
  1102. *
  1103. * @param integer $id the id of the template that has to be deleted
  1104. *
  1105. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  1106. * @version August 2008
  1107. * @since Dokeos 1.8.6
  1108. */
  1109. function delete_template($id)
  1110. {
  1111. // first we remove the image
  1112. $table_system_template = Database :: get_main_table('system_template');
  1113. $sql = "SELECT * FROM $table_system_template WHERE id = '".Database::escape_string($id)."'";
  1114. $result = api_sql_query($sql, __FILE__, __LINE__);
  1115. $row = Database::fetch_array($result);
  1116. if (!empty($row['image']))
  1117. {
  1118. unlink(api_get_path(SYS_PATH).'home/default_platform_document/template_thumb/'.$row['image']);
  1119. }
  1120. // now we remove it from the database
  1121. $sql = "DELETE FROM $table_system_template WHERE id = '".Database::escape_string($id)."'";
  1122. $result = api_sql_query($sql, __FILE__, __LINE__);
  1123. // display a feedback message
  1124. Display::display_confirmation_message(get_lang('TemplateDeleted'));
  1125. }
  1126. ?>