terms.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once __DIR__.'/../../main/inc/global.inc.php';
  4. api_protect_admin_script();
  5. $plugin = DictionaryPlugin::create();
  6. $table = 'plugin_dictionary';
  7. $sql = "SELECT * FROM $table ORDER BY TERM";
  8. $result = Database::query($sql);
  9. $terms = Database::store_result($result, 'ASSOC');
  10. $action = isset($_GET['action']) ? Security::remove_XSS($_GET['action']) : 'add';
  11. $id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
  12. $term = null;
  13. if (!empty($id)) {
  14. $sql = "SELECT * FROM $table WHERE id = $id";
  15. $result = Database::query($sql);
  16. $term = Database::fetch_array($result, 'ASSOC');
  17. if (empty($term)) {
  18. api_not_allowed(true);
  19. }
  20. }
  21. $form = new FormValidator('dictionary', 'post', api_get_self().'?action='.$action.'&id='.$id);
  22. $form->addText('term', $plugin->get_lang('Term'), true);
  23. $form->addTextarea('definition', $plugin->get_lang('Definition'), [], true);
  24. //$form->addHtmlEditor('definition', get_lang('Definition'), true);
  25. $form->addButtonSave(get_lang('Save'));
  26. switch ($action) {
  27. case 'add':
  28. if ($form->validate()) {
  29. $values = $form->getSubmitValues();
  30. $params = [
  31. 'term' => $values['term'],
  32. 'definition' => $values['definition'],
  33. ];
  34. $result = Database::insert($table, $params);
  35. if ($result) {
  36. Display::addFlash(Display::return_message(get_lang('Added')));
  37. }
  38. header('Location: '.api_get_self());
  39. exit;
  40. }
  41. break;
  42. case 'edit':
  43. $form->setDefaults($term);
  44. if ($form->validate()) {
  45. $values = $form->getSubmitValues();
  46. $params = [
  47. 'term' => $values['term'],
  48. 'definition' => $values['definition'],
  49. ];
  50. Database::update($table, $params, ['id = ?' => $id]);
  51. Display::addFlash(Display::return_message(get_lang('Update successful')));
  52. header('Location: '.api_get_self());
  53. exit;
  54. }
  55. break;
  56. case 'delete':
  57. if (!empty($term)) {
  58. Database::delete($table, ['id = ?' => $id]);
  59. Display::addFlash(Display::return_message(get_lang('Deleted')));
  60. header('Location: '.api_get_self());
  61. exit;
  62. }
  63. break;
  64. }
  65. $tpl = new Template($plugin->get_lang('plugin_title'));
  66. $tpl->assign('terms', $terms);
  67. $tpl->assign('form', $form->returnForm());
  68. $content = $tpl->fetch('/'.$plugin->get_name().'/view/terms.html.twig');
  69. // Assign into content
  70. $tpl->assign('content', $content);
  71. // Display
  72. $tpl->display_one_col_template();