settings.lib.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Library of the settings.php file
  5. *
  6. * @author Julio Montoya <gugli100@gmail.com>
  7. * @author Guillaume Viguier <guillaume@viguierjust.com>
  8. *
  9. * @since Chamilo 1.8.7
  10. * @package chamilo.admin
  11. */
  12. /**
  13. * The function that retrieves all the possible settings for a certain config setting
  14. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  15. */
  16. function get_settings_options($var)
  17. {
  18. $table_settings_options = Database :: get_main_table(TABLE_MAIN_SETTINGS_OPTIONS);
  19. $sql = "SELECT * FROM $table_settings_options WHERE variable='$var'";
  20. $result = Database::query($sql);
  21. while ($row = Database::fetch_array($result))
  22. {
  23. $temp_array = array ('value' => $row['value'], 'display_text' => $row['display_text']);
  24. $settings_options_array[] = $temp_array;
  25. }
  26. return $settings_options_array;
  27. }
  28. /**
  29. * This function allows easy activating and inactivating of plugins
  30. * @todo: a similar function needs to be written to activate or inactivate additional tools.
  31. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  32. */
  33. function handle_plugins()
  34. {
  35. global $SettingsStored;
  36. $userplugins = array();
  37. $table_settings_current = Database :: get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  38. if (isset($_POST['submit_plugins']))
  39. {
  40. store_plugins();
  41. // add event to system log
  42. $time = time();
  43. $user_id = api_get_user_id();
  44. $category = $_GET['category'];
  45. event_system(LOG_CONFIGURATION_SETTINGS_CHANGE, LOG_CONFIGURATION_SETTINGS_CATEGORY, $category, $time, $user_id);
  46. Display :: display_confirmation_message(get_lang('SettingsStored'));
  47. }
  48. //echo get_lang('AvailablePlugins')."<br/>
  49. echo '<br />';
  50. /* We scan the plugin directory. Each folder is a potential plugin. */
  51. $pluginpath = api_get_path(SYS_PLUGIN_PATH);
  52. $handle = @opendir($pluginpath);
  53. while (false !== ($file = readdir($handle)))
  54. {
  55. if ($file <> '.' AND $file <> '..' AND is_dir(api_get_path(SYS_PLUGIN_PATH).$file))
  56. {
  57. $possibleplugins[] = $file;
  58. }
  59. }
  60. @closedir($handle);
  61. /* for each of the possible plugin dirs we check if a file plugin.php (that contains all the needed information about this plugin)
  62. can be found in the dir.
  63. this plugin.php file looks like
  64. $plugin_info['title']='The title of the plugin'; //
  65. $plugin_info['comment']="Some comment about the plugin";
  66. $plugin_info['location']=array("loginpage_menu", "campushomepage_menu","banner"); // the possible locations where the plugins can be used
  67. $plugin_info['version']='0.1 alpha'; // The version number of the plugin
  68. $plugin_info['author']='Patrick Cool'; // The author of the plugin
  69. */
  70. echo '<form name="plugins" method="post" action="'.api_get_self().'?category='.$_GET['category'].'">';
  71. echo "<table class=\"data_table\">\n";
  72. echo "\t<tr>\n";
  73. echo "\t\t<th>\n";
  74. echo get_lang('Plugin');
  75. echo "\t\t</th>\n";
  76. echo "\t\t<th>\n";
  77. echo get_lang('LoginPageMainArea');
  78. echo "\t\t</th>\n";
  79. echo "\t\t<th>\n";
  80. echo get_lang('LoginPageMenu');
  81. echo "\t\t</th>\n";
  82. echo "\t\t<th>\n";
  83. echo get_lang('CampusHomepageMainArea');
  84. echo "\t\t</th>\n";
  85. echo "\t\t<th>\n";
  86. echo get_lang('CampusHomepageMenu');
  87. echo "\t\t</th>\n";
  88. echo "\t\t<th>\n";
  89. echo get_lang('MyCoursesMainArea');
  90. echo "\t\t</th>\n";
  91. echo "\t\t<th>\n";
  92. echo get_lang('MyCoursesMenu');
  93. echo "\t\t</th>\n";
  94. echo "\t\t<th>\n";
  95. echo get_lang('Header');
  96. echo "\t\t</th>\n";
  97. echo "\t\t<th>\n";
  98. echo get_lang('Footer');
  99. echo "\t\t</th>\n";
  100. echo "\t</tr>\n";
  101. /* We retrieve all the active plugins. */
  102. //$sql = "SELECT * FROM $table_settings_current WHERE category='Plugins'";
  103. //$result = Database::query($sql);
  104. $result = api_get_settings('Plugins');
  105. //while ($row = Database::fetch_array($result))
  106. foreach($result as $row)
  107. {
  108. $usedplugins[$row['variable']][] = $row['selected_value'];
  109. }
  110. /* We display all the possible plugins and the checkboxes */
  111. foreach ($possibleplugins as $testplugin)
  112. {
  113. $plugin_info_file = api_get_path(SYS_PLUGIN_PATH).$testplugin."/plugin.php";
  114. if (file_exists($plugin_info_file))
  115. {
  116. $plugin_info = array();
  117. include ($plugin_info_file);
  118. echo "\t<tr>\n";
  119. echo "\t\t<td>\n";
  120. foreach ($plugin_info as $key => $value)
  121. {
  122. if ($key <> 'location')
  123. {
  124. if ($key == 'title')
  125. {
  126. $value = '<strong>'.$value.'</strong>';
  127. }
  128. echo get_lang(ucwords($key)).': '.$value.'<br />';
  129. }
  130. }
  131. if (file_exists(api_get_path(SYS_PLUGIN_PATH).$testplugin.'/readme.txt'))
  132. {
  133. echo "<a href='".api_get_path(WEB_PLUGIN_PATH).$testplugin."/readme.txt'>readme.txt</a>";
  134. }
  135. echo "\t\t</td>\n";
  136. // column: LoginPageMainArea
  137. if(empty($usedplugins))
  138. {
  139. $usedplugins = array();
  140. }
  141. display_plugin_cell('loginpage_main', $plugin_info, $testplugin, $usedplugins);
  142. display_plugin_cell('loginpage_menu', $plugin_info, $testplugin, $usedplugins);
  143. display_plugin_cell('campushomepage_main', $plugin_info, $testplugin, $usedplugins);
  144. display_plugin_cell('campushomepage_menu', $plugin_info, $testplugin, $usedplugins);
  145. display_plugin_cell('mycourses_main', $plugin_info, $testplugin, $usedplugins);
  146. display_plugin_cell('mycourses_menu', $plugin_info, $testplugin, $usedplugins);
  147. display_plugin_cell('header', $plugin_info, $testplugin, $usedplugins);
  148. display_plugin_cell('footer', $plugin_info, $testplugin, $usedplugins);
  149. echo "\t</tr>\n";
  150. }
  151. }
  152. echo '</table>';
  153. echo '<br />';
  154. echo '<button class="save" type="submit" name="submit_plugins">'.get_lang('EnablePlugins').'</button></form>';
  155. echo '<br />';
  156. }
  157. function display_plugin_cell($location, $plugin_info, $current_plugin, $active_plugins)
  158. {
  159. echo "\t\t<td align=\"center\">\n";
  160. if (in_array($location, $plugin_info['location']))
  161. {
  162. if (isset($active_plugins[$location]) && is_array($active_plugins[$location])
  163. && in_array($current_plugin, $active_plugins[$location]))
  164. {
  165. $checked = "checked";
  166. }
  167. else
  168. {
  169. $checked = '';
  170. }
  171. echo '<input type="checkbox" name="'.$current_plugin.'-'.$location.'" value="true" '.$checked.'/>';
  172. }
  173. echo "\t\t</td>\n";
  174. }
  175. /**
  176. * This function allows the platform admin to choose the default stylesheet
  177. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  178. * @author Julio Montoya <gugli100@gmail.com>, Chamilo
  179. */
  180. function handle_stylesheets()
  181. {
  182. global $_configuration;
  183. // Current style
  184. $currentstyle = api_get_setting('stylesheets');
  185. $is_style_changeable=false;
  186. if ($_configuration['access_url']!=1) {
  187. $style_info = api_get_settings('stylesheets','',1,0);
  188. $url_info = api_get_access_url($_configuration['access_url']);
  189. if ($style_info[0]['access_url_changeable']==1 && $url_info['active']==1) {
  190. $is_style_changeable=true;
  191. echo '<div class="actions" id="stylesheetuploadlink">';
  192. Display::display_icon('theme_add.gif');
  193. echo '<a href="" onclick="document.getElementById(\'newstylesheetform\').style.display = \'block\'; document.getElementById(\'stylesheetuploadlink\').style.display = \'none\';return false; ">'.get_lang('UploadNewStylesheet').'</a>';
  194. echo '</div>';
  195. }
  196. } else {
  197. $is_style_changeable=true;
  198. echo '<div class="actions" id="stylesheetuploadlink">';
  199. Display::display_icon('theme_add.gif');
  200. echo '<a href="" onclick="document.getElementById(\'newstylesheetform\').style.display = \'block\'; document.getElementById(\'stylesheetuploadlink\').style.display = \'none\';return false; ">'.get_lang('UploadNewStylesheet').'</a>';
  201. echo '</div>';
  202. }
  203. $form = new FormValidator('stylesheet_upload','post','settings.php?category=stylesheets&showuploadform=true');
  204. $form->addElement('text','name_stylesheet',get_lang('NameStylesheet'),array('size' => '40', 'maxlength' => '40'));
  205. $form->addRule('name_stylesheet', get_lang('ThisFieldIsRequired'), 'required');
  206. $form->addElement('file', 'new_stylesheet', get_lang('UploadNewStylesheet'));
  207. $allowed_file_types = array ('css');
  208. $form->addRule('new_stylesheet', get_lang('InvalidExtension').' ('.implode(',', $allowed_file_types).')', 'filetype', $allowed_file_types);
  209. $form->addRule('new_stylesheet', get_lang('ThisFieldIsRequired'), 'required');
  210. $form->addElement('style_submit_button', 'stylesheet_upload', get_lang('Ok'), array('class'=>'save'));
  211. if( $form->validate() AND is_writable(api_get_path(SYS_CODE_PATH).'css/')) {
  212. $values = $form->exportValues();
  213. $picture_element = & $form->getElement('new_stylesheet');
  214. $picture = $picture_element->getValue();
  215. upload_stylesheet($values, $picture);
  216. // add event to system log
  217. $time = time();
  218. $user_id = api_get_user_id();
  219. $category = $_GET['category'];
  220. event_system(LOG_CONFIGURATION_SETTINGS_CHANGE, LOG_CONFIGURATION_SETTINGS_CATEGORY, $category, $time, $user_id);
  221. Display::display_confirmation_message(get_lang('StylesheetAdded'));
  222. } else {
  223. if (!is_writable(api_get_path(SYS_CODE_PATH).'css/'))
  224. {
  225. Display::display_error_message(api_get_path(SYS_CODE_PATH).'css/'.get_lang('IsNotWritable'));
  226. }
  227. else
  228. {
  229. if ($_GET['showuploadform'] == 'true')
  230. {
  231. echo '<div id="newstylesheetform">';
  232. }
  233. else
  234. {
  235. echo '<div id="newstylesheetform" style="display: none;">';
  236. }
  237. // uploading a new stylesheet
  238. if ($_configuration['access_url']==1)
  239. {
  240. $form->display();
  241. }
  242. else
  243. {
  244. if ($is_style_changeable)
  245. {
  246. $form->display();
  247. }
  248. }
  249. echo '</div>';
  250. }
  251. }
  252. // Preview of the stylesheet
  253. echo '<div><iframe src="style_preview.php" width="100%" height="300" name="preview"></iframe></div>';
  254. ?>
  255. <script type="text/javascript">
  256. function load_preview(selectobj){
  257. var style_dir = selectobj.options[selectobj.selectedIndex].value;
  258. parent.preview.location='style_preview.php?style=' + style_dir;
  259. }
  260. </script>
  261. <?php
  262. echo '<form name="stylesheets" method="post" action="'.api_get_self().'?category='.Security::remove_XSS($_GET['category']).'">';
  263. echo '<br /><select name="style" onChange="load_preview(this)" >';
  264. $list_of_styles = array();
  265. $list_of_names = array();
  266. if ($handle = @opendir(api_get_path(SYS_PATH).'main/css/')) {
  267. $counter=1;
  268. while (false !== ($style_dir = readdir($handle))) {
  269. if(substr($style_dir,0,1)=='.') { //skip dirs starting with a '.'
  270. continue;
  271. }
  272. $dirpath = api_get_path(SYS_PATH).'main/css/'.$style_dir;
  273. if (is_dir($dirpath)) {
  274. if ($style_dir != '.' && $style_dir != '..') {
  275. if ($currentstyle == $style_dir OR ($style_dir == 'dokeos_classic' AND !$currentstyle)) {
  276. $selected = 'selected="true"';
  277. } else {
  278. $selected = '';
  279. }
  280. $show_name=ucwords(str_replace('_',' ', $style_dir));
  281. if ($is_style_changeable) {
  282. $list_of_styles[$style_dir] = "<option value=\"".$style_dir."\" ".$selected." /> $show_name </option>";
  283. $list_of_names[$style_dir] = $show_name;
  284. //echo "<input type=\"radio\" name=\"style\" value=\"".$style_dir."\" ".$selected." onClick=\"parent.preview.location='style_preview.php?style=".$style_dir."';\"/>";
  285. //echo '<a href="style_preview.php?style='.$style_dir.'" target="preview">'.$show_name.'</a>';
  286. } else {
  287. echo '<a href="style_preview.php?style='.$style_dir.'" target="preview">'.$show_name.'</a>';
  288. }
  289. echo "<br />\n";
  290. $counter++;
  291. }
  292. }
  293. }
  294. @closedir($handle);
  295. }
  296. //Sort styles in alphabetical order
  297. asort($list_of_names);
  298. foreach($list_of_names as $style_dir=>$item) {
  299. echo $list_of_styles[$style_dir];
  300. }
  301. echo '</select><br />';
  302. //var_dump($list_of_names);
  303. if ($is_style_changeable){
  304. echo '<button class="save" type="submit" name="submit_stylesheets"> '.get_lang('SaveSettings').' </button></form>';
  305. }
  306. }
  307. /**
  308. * creates the folder (if needed) and uploads the stylesheet in it
  309. *
  310. * @param array $values the values of the form
  311. * @param array $picture the values of the uploaded file
  312. *
  313. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  314. * @version May 2008
  315. * @since Dokeos 1.8.5
  316. */
  317. function upload_stylesheet($values,$picture)
  318. {
  319. // valid name for the stylesheet folder
  320. $style_name = api_preg_replace('/[^A-Za-z0-9]/', '', $values['name_stylesheet']);
  321. // create the folder if needed
  322. if (!is_dir(api_get_path(SYS_CODE_PATH).'css/'.$style_name.'/'))
  323. {
  324. mkdir(api_get_path(SYS_CODE_PATH).'css/'.$style_name.'/', api_get_permissions_for_new_directories());
  325. }
  326. // move the file in the folder
  327. move_uploaded_file($picture['tmp_name'], api_get_path(SYS_CODE_PATH).'css/'.$style_name.'/'.$picture['name']);
  328. }
  329. /**
  330. * This function allows easy activating and inactivating of plugins
  331. * @todo: a similar function needs to be written to activate or inactivate additional tools.
  332. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  333. */
  334. function store_plugins()
  335. {
  336. $table_settings_current = Database :: get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  337. global $_configuration;
  338. // Step 1 : we remove all the plugins
  339. //$sql = "DELETE FROM $table_settings_current WHERE category='Plugins'";
  340. //Database::query($sql);
  341. $r = api_delete_category_settings('Plugins',$_configuration['access_url']);
  342. // step 2: looping through all the post values we only store these which are really a valid plugin location.
  343. foreach ($_POST as $form_name => $formvalue)
  344. {
  345. $form_name_elements = explode("-", $form_name);
  346. if (is_valid_plugin_location($form_name_elements[1]))
  347. {
  348. //$sql = "INSERT into $table_settings_current (variable,category,selected_value) VALUES ('".$form_name_elements['1']."','Plugins','".$form_name_elements['0']."')";
  349. //Database::query($sql);
  350. 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);
  351. }
  352. }
  353. }
  354. /**
  355. * Check if the post information is really a valid plugin location.
  356. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  357. */
  358. function is_valid_plugin_location($location)
  359. {
  360. $valid_locations=array('loginpage_main', 'loginpage_menu', 'campushomepage_main', 'campushomepage_menu', 'mycourses_main', 'mycourses_menu','header', 'footer');
  361. if (in_array($location, $valid_locations))
  362. {
  363. return true;
  364. }
  365. else
  366. {
  367. return false;
  368. }
  369. }
  370. /**
  371. * This function allows the platform admin to choose which should be the default stylesheet
  372. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  373. */
  374. function store_stylesheets()
  375. {
  376. global $_configuration;
  377. // Database Table Definitions
  378. $table_settings_current = Database :: get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  379. // Insert the stylesheet
  380. $style = Database::escape_string($_POST['style']);
  381. if (is_style($style))
  382. {
  383. /*
  384. $sql = 'UPDATE '.$table_settings_current.' SET
  385. selected_value = "'.$style.'"
  386. WHERE variable = "stylesheets"
  387. AND category = "stylesheets"';
  388. Database::query($sql);
  389. */
  390. api_set_setting('stylesheets',$style,null,'stylesheets',$_configuration['access_url']);
  391. }
  392. return true;
  393. }
  394. /**
  395. * This function checks if the given style is a recognize style that exists in the css directory as
  396. * a standalone directory.
  397. * @param string Style
  398. * @return bool True if this style is recognized, false otherwise
  399. */
  400. function is_style($style)
  401. {
  402. $dir = api_get_path(SYS_PATH).'main/css/';
  403. $dirs = scandir($dir);
  404. $style = str_replace(array('/','\\'),array('',''),$style); //avoid slashes or backslashes
  405. if (in_array($style,$dirs) && is_dir($dir.$style))
  406. {
  407. return true;
  408. }
  409. return false;
  410. }
  411. /**
  412. * Search options
  413. * TODO: support for multiple site. aka $_configuration['access_url'] == 1
  414. * @author Marco Villegas <marvil07@gmail.com>
  415. */
  416. function handle_search() {
  417. global $SettingsStored, $_configuration;
  418. $search_enabled = api_get_setting('search_enabled');
  419. $settings = api_get_settings('Search');
  420. if ($search_enabled !== 'true' || count($settings) < 1) {
  421. Display::display_error_message(get_lang('SearchFeatureNotEnabledComment'));
  422. return;
  423. }
  424. require_once api_get_path(LIBRARY_PATH) . 'specific_fields_manager.lib.php';
  425. $form = new FormValidator('search-options', 'post', api_get_self().'?category=Search');
  426. $renderer = & $form->defaultRenderer();
  427. $renderer->setHeaderTemplate('<div class="sectiontitle">{header}</div>'."\n");
  428. $renderer->setElementTemplate('<div class="sectioncomment">{label}</div>'."\n".'<div class="sectionvalue">{element}</div>'."\n");
  429. //search_show_unlinked_results
  430. $form->addElement('header', null, get_lang('SearchShowUnlinkedResultsTitle'));
  431. $form->addElement('label', null, get_lang('SearchShowUnlinkedResultsComment'));
  432. $values = get_settings_options('search_show_unlinked_results');
  433. $group = array ();
  434. foreach ($values as $key => $value) {
  435. $element = & $form->createElement('radio', 'search_show_unlinked_results', '', get_lang($value['display_text']), $value['value']);
  436. $group[] = $element;
  437. }
  438. $form->addGroup($group, 'search_show_unlinked_results', get_lang('SearchShowUnlinkedResultsComment'), '<br />', false);
  439. $default_values['search_show_unlinked_results'] = api_get_setting('search_show_unlinked_results');
  440. //search_prefilter_prefix
  441. $form->addElement('header', null, get_lang('SearchPrefilterPrefix'));
  442. $form->addElement('label', null, get_lang('SearchPrefilterPrefixComment'));
  443. $specific_fields = get_specific_field_list();
  444. $sf_values = array();
  445. foreach ($specific_fields as $sf) {
  446. $sf_values[$sf['code']] = $sf['name'];
  447. }
  448. $group = array ();
  449. $form->addElement('select', 'search_prefilter_prefix', get_lang('SearchPrefilterPrefix'), $sf_values, '');
  450. $default_values['search_prefilter_prefix'] = api_get_setting('search_prefilter_prefix');
  451. //$form->addRule('search_show_unlinked_results', get_lang('ThisFieldIsRequired'), 'required');
  452. $form->addElement('style_submit_button', 'search-options-save', get_lang('Ok'));
  453. $form->setDefaults($default_values);
  454. if( $form->validate()) {
  455. $formvalues = $form->exportValues();
  456. $r = api_set_settings_category('Search','false',$_configuration['access_url']);
  457. // Save the settings
  458. foreach ($formvalues as $key => $value)
  459. {
  460. $result = api_set_setting($key, $value, null, null);
  461. }
  462. Display :: display_confirmation_message($SettingsStored);
  463. } else {
  464. echo '<div id="search-options-form">';
  465. $form->display();
  466. echo '</div>';
  467. }
  468. }
  469. /**
  470. * wrapper for the templates
  471. *
  472. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  473. * @version August 2008
  474. * @since Dokeos 1.8.6
  475. */
  476. function handle_templates()
  477. {
  478. if ($_GET['action'] != 'add') {
  479. echo "\n<div class=\"actions\" style=\"margin-left:1px\" >";
  480. echo '<a href="settings.php?category=Templates&amp;action=add">'.Display::return_icon('template_add.gif', get_lang('AddTemplate')).get_lang('AddTemplate').'</a>';
  481. echo "\n</div>";
  482. }
  483. if ($_GET['action'] == 'add' OR ( $_GET['action'] == 'edit' AND is_numeric($_GET['id']))) {
  484. add_edit_template();
  485. // add event to system log
  486. $time = time();
  487. $user_id = api_get_user_id();
  488. $category = $_GET['category'];
  489. event_system(LOG_CONFIGURATION_SETTINGS_CHANGE, LOG_CONFIGURATION_SETTINGS_CATEGORY, $category, $time, $user_id);
  490. } else {
  491. if ($_GET['action'] == 'delete' and is_numeric($_GET['id'])) {
  492. delete_template($_GET['id']);
  493. // add event to system log
  494. $time = time();
  495. $user_id = api_get_user_id();
  496. $category = $_GET['category'];
  497. event_system(LOG_CONFIGURATION_SETTINGS_CHANGE, LOG_CONFIGURATION_SETTINGS_CATEGORY, $category, $time, $user_id);
  498. }
  499. display_templates();
  500. }
  501. }
  502. /**
  503. * Display a sortable table with all the templates that the platform administrator has defined.
  504. *
  505. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  506. * @version August 2008
  507. * @since Dokeos 1.8.6
  508. */
  509. function display_templates()
  510. {
  511. $table = new SortableTable('templates', 'get_number_of_templates', 'get_template_data',1);
  512. $table->set_additional_parameters(array('category'=>Security::remove_XSS($_GET['category'])));
  513. $table->set_header(0, get_lang('Image'), true, array ('style' => 'width:101px;'));
  514. $table->set_header(1, get_lang('Title'));
  515. $table->set_header(2, get_lang('Actions'), false, array ('style' => 'width:50px;'));
  516. $table->set_column_filter(2,'actions_filter');
  517. $table->set_column_filter(0,'image_filter');
  518. $table->display();
  519. }
  520. /**
  521. * Get the number of templates that are defined by the platform admin.
  522. *
  523. * @return integer
  524. *
  525. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  526. * @version August 2008
  527. * @since Dokeos 1.8.6
  528. */
  529. function get_number_of_templates()
  530. {
  531. // Database table definition
  532. $table_system_template = Database :: get_main_table('system_template');
  533. // The sql statement
  534. $sql = "SELECT COUNT(id) AS total FROM $table_system_template";
  535. $result = Database::query($sql);
  536. $row = Database::fetch_array($result);
  537. // returning the number of templates
  538. return $row['total'];
  539. }
  540. /**
  541. * Get all the template data for the sortable table
  542. *
  543. * @param integer $from the start of the limit statement
  544. * @param integer $number_of_items the number of elements that have to be retrieved from the database
  545. * @param integer $column the column that is
  546. * @param string $direction the sorting direction (ASC or DESC�
  547. * @return array
  548. *
  549. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  550. * @version August 2008
  551. * @since Dokeos 1.8.6
  552. */
  553. function get_template_data($from, $number_of_items, $column, $direction)
  554. {
  555. // Database table definition
  556. $table_system_template = Database :: get_main_table('system_template');
  557. // the sql statement
  558. $sql = "SELECT image as col0, title as col1, id as col2 FROM $table_system_template";
  559. $sql .= " ORDER BY col$column $direction ";
  560. $sql .= " LIMIT $from,$number_of_items";
  561. $result = Database::query($sql);
  562. while ($row = Database::fetch_array($result)) {
  563. $row['1'] = get_lang($row['1']);
  564. $return[] = $row;
  565. }
  566. // returning all the information for the sortable table
  567. return $return;
  568. }
  569. /**
  570. * display the edit and delete icons in the sortable table
  571. *
  572. * @param integer $id the id of the template
  573. * @return html code for the link to edit and delete the template
  574. *
  575. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  576. * @version August 2008
  577. * @since Dokeos 1.8.6
  578. */
  579. function actions_filter($id)
  580. {
  581. $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>';
  582. $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>';
  583. return $return;
  584. }
  585. /**
  586. * Display the image of the template in the sortable table
  587. *
  588. * @param string $image the image
  589. * @return html code for the image
  590. *
  591. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  592. * @version August 2008
  593. * @since Dokeos 1.8.6
  594. */
  595. function image_filter($image)
  596. {
  597. if (!empty($image))
  598. {
  599. return '<img src="'.api_get_path(WEB_PATH).'home/default_platform_document/template_thumb/'.$image.'" alt="'.get_lang('TemplatePreview').'"/>';
  600. }
  601. else
  602. {
  603. return '<img src="'.api_get_path(WEB_PATH).'home/default_platform_document/template_thumb/noimage.gif" alt="'.get_lang('NoTemplatePreview').'"/>';
  604. }
  605. }
  606. /**
  607. * 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
  608. *
  609. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  610. * @version August 2008
  611. * @since Dokeos 1.8.6
  612. */
  613. function add_edit_template()
  614. {
  615. // initiate the object
  616. $form = new FormValidator('template', 'post', 'settings.php?category=Templates&action='.Security::remove_XSS($_GET['action']).'&id='.Security::remove_XSS($_GET['id']));
  617. // settting the form elements: the header
  618. if ($_GET['action'] == 'add') {
  619. $title = get_lang('AddTemplate');
  620. }
  621. else
  622. {
  623. $title = get_lang('EditTemplate');
  624. }
  625. $form->addElement('header', '', $title);
  626. // settting the form elements: the title of the template
  627. $form->add_textfield('title', get_lang('Title'), false);
  628. // settting the form elements: the content of the template (wysiwyg editor)
  629. $form->addElement('html_editor', 'template_text', get_lang('Text'), null, array('ToolbarSet' => 'AdminTemplates', 'Width' => '100%', 'Height' => '400'));
  630. // settting the form elements: the form to upload an image to be used with the template
  631. $form->addElement('file','template_image',get_lang('Image'),'');
  632. // settting the form elements: a little bit information about the template image
  633. $form->addElement('static', 'file_comment', '', get_lang('TemplateImageComment100x70'));
  634. // getting all the information of the template when editing a template
  635. if ($_GET['action'] == 'edit') {
  636. // Database table definition
  637. $table_system_template = Database :: get_main_table('system_template');
  638. $sql = "SELECT * FROM $table_system_template WHERE id = '".Database::escape_string($_GET['id'])."'";
  639. $result = Database::query($sql);
  640. $row = Database::fetch_array($result);
  641. $defaults['template_id'] = intval($_GET['id']);
  642. $defaults['template_text'] = $row['content'];
  643. //forcing a get_lang
  644. $defaults['title'] = get_lang($row['title']);
  645. // adding an extra field: a hidden field with the id of the template we are editing
  646. $form->addElement('hidden','template_id');
  647. // adding an extra field: a preview of the image that is currently used
  648. if (!empty($row['image']))
  649. {
  650. $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').'"/>');
  651. }
  652. else
  653. {
  654. $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').'"/>');
  655. }
  656. // setting the information of the template that we are editing
  657. $form->setDefaults($defaults);
  658. }
  659. // settting the form elements: the submit button
  660. $form->addElement('style_submit_button' , 'submit', get_lang('Ok') ,'class="save"');
  661. // setting the rules: the required fields
  662. $form->addRule('title', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  663. $form->addRule('template_text', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  664. // if the form validates (complies to all rules) we save the information, else we display the form again (with error message if needed)
  665. if( $form->validate() )
  666. {
  667. $check = Security::check_token('post');
  668. if ($check) {
  669. // exporting the values
  670. $values = $form->exportValues();
  671. // upload the file
  672. if (!empty($_FILES['template_image']['name']))
  673. {
  674. include_once (api_get_path(LIBRARY_PATH).'fileUpload.lib.php');
  675. $upload_ok = process_uploaded_file($_FILES['template_image']);
  676. if ($upload_ok)
  677. {
  678. // Try to add an extension to the file if it hasn't one
  679. $new_file_name = add_ext_on_mime(stripslashes($_FILES['template_image']['name']), $_FILES['template_image']['type']);
  680. // upload dir
  681. $upload_dir = api_get_path(SYS_PATH).'home/default_platform_document/template_thumb/';
  682. // create dir if not exists
  683. if (!is_dir($upload_dir)) {
  684. mkdir($upload_dir, api_get_permissions_for_new_directories());
  685. }
  686. // resize image to max default and upload
  687. require_once (api_get_path(LIBRARY_PATH).'image.lib.php');
  688. $temp = new image($_FILES['template_image']['tmp_name']);
  689. $picture_infos=@getimagesize($_FILES['template_image']['tmp_name']);
  690. $max_width_for_picture = 100;
  691. if ($picture_infos[0]>$max_width_for_picture)
  692. {
  693. $thumbwidth = $max_width_for_picture;
  694. if (empty($thumbwidth) or $thumbwidth==0) {
  695. $thumbwidth=$max_width_for_picture;
  696. }
  697. $new_height = round(($thumbwidth/$picture_infos[0])*$picture_infos[1]);
  698. $temp->resize($thumbwidth,$new_height,0);
  699. }
  700. $type=$picture_infos[2];
  701. switch (!empty($type))
  702. {
  703. case 2 : $temp->send_image('JPG',$upload_dir.$new_file_name);
  704. break;
  705. case 3 : $temp->send_image('PNG',$upload_dir.$new_file_name);
  706. break;
  707. case 1 : $temp->send_image('GIF',$upload_dir.$new_file_name);
  708. break;
  709. }
  710. }
  711. }
  712. // store the information in the database (as insert or as update)
  713. $table_system_template = Database :: get_main_table('system_template');
  714. if ($_GET['action'] == 'add') {
  715. $content_template = '<head>{CSS}<style type="text/css">.text{font-weight: normal;}</style></head><body>'.Database::escape_string($values['template_text']).'</body>';
  716. $sql = "INSERT INTO $table_system_template (title, content, image) VALUES ('".Database::escape_string($values['title'])."','".$content_template."','".Database::escape_string($new_file_name)."')";
  717. $result = Database::query($sql);
  718. // display a feedback message
  719. Display::display_confirmation_message(get_lang('TemplateAdded'));
  720. echo '<a href="settings.php?category=Templates&amp;action=add">'.Display::return_icon('template_add.gif', get_lang('AddTemplate')).get_lang('AddTemplate').'</a>';
  721. } else {
  722. $content_template = '<head>{CSS}<style type="text/css">.text{font-weight: normal;}</style></head><body>'.Database::escape_string($values['template_text']).'</body>';
  723. $sql = "UPDATE $table_system_template set title = '".Database::escape_string($values['title'])."',
  724. content = '".$content_template."'";
  725. if (!empty($new_file_name))
  726. {
  727. $sql .= ", image = '".Database::escape_string($new_file_name)."'";
  728. }
  729. $sql .= " WHERE id='".Database::escape_string($_GET['id'])."'";
  730. $result = Database::query($sql);
  731. // display a feedback message
  732. Display::display_confirmation_message(get_lang('TemplateEdited'));
  733. }
  734. }
  735. Security::clear_token();
  736. display_templates();
  737. }
  738. else
  739. {
  740. $token = Security::get_token();
  741. $form->addElement('hidden','sec_token');
  742. $form->setConstants(array('sec_token' => $token));
  743. // display the form
  744. $form->display();
  745. }
  746. }
  747. /**
  748. * Delete a template
  749. *
  750. * @param integer $id the id of the template that has to be deleted
  751. *
  752. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  753. * @version August 2008
  754. * @since Dokeos 1.8.6
  755. */
  756. function delete_template($id)
  757. {
  758. // first we remove the image
  759. $table_system_template = Database :: get_main_table('system_template');
  760. $sql = "SELECT * FROM $table_system_template WHERE id = '".Database::escape_string($id)."'";
  761. $result = Database::query($sql);
  762. $row = Database::fetch_array($result);
  763. if (!empty($row['image']))
  764. {
  765. @unlink(api_get_path(SYS_PATH).'home/default_platform_document/template_thumb/'.$row['image']);
  766. }
  767. // now we remove it from the database
  768. $sql = "DELETE FROM $table_system_template WHERE id = '".Database::escape_string($id)."'";
  769. $result = Database::query($sql);
  770. // display a feedback message
  771. Display::display_confirmation_message(get_lang('TemplateDeleted'));
  772. }
  773. /**
  774. * Returns the list of timezone identifiers used to populate the select
  775. *
  776. * @return array List of timezone identifiers
  777. *
  778. * @author Guillaume Viguier <guillaume.viguier@beeznest.com>
  779. * @since Chamilo 1.8.7
  780. */
  781. function select_timezone_value() {
  782. return api_get_timezones();
  783. }