tools.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once __DIR__.'/../inc/global.inc.php';
  4. // The section for the tabs
  5. $this_section = SECTION_COURSES;
  6. $sessionId = api_get_session_id();
  7. if (!empty($sessionId)) {
  8. api_not_allowed();
  9. }
  10. api_protect_course_script(true);
  11. if (!api_is_allowed_to_edit()) {
  12. api_not_allowed(true);
  13. }
  14. $action = isset($_GET['action']) ? $_GET['action'] : '';
  15. $id = isset($_GET['id']) ? (int) $_GET['id'] : '';
  16. $toolName = get_lang('Customize icons');
  17. switch ($action) {
  18. case 'delete_icon':
  19. $tool = CourseHome::getTool($id);
  20. if (empty($tool)) {
  21. api_not_allowed(true);
  22. }
  23. $currentUrl = api_get_self().'?'.api_get_cidreq();
  24. Display::addFlash(Display::return_message(get_lang('Update successful')));
  25. CourseHome::deleteIcon($id);
  26. header('Location: '.$currentUrl);
  27. exit;
  28. break;
  29. case 'edit_icon':
  30. $tool = CourseHome::getTool($id);
  31. if (empty($tool)) {
  32. api_not_allowed(true);
  33. }
  34. $interbreadcrumb[] = [
  35. 'url' => api_get_self().'?'.api_get_cidreq(),
  36. 'name' => get_lang('Customize icons'),
  37. ];
  38. $toolName = Security::remove_XSS(stripslashes($tool['name']));
  39. $currentUrl = api_get_self().'?action=edit_icon&id='.$id.'&'.api_get_cidreq();
  40. $form = new FormValidator('icon_edit', 'post', $currentUrl);
  41. $form->addHeader(get_lang('Edit icon'));
  42. $form->addHtml('<div class="col-md-7">');
  43. $form->addText('name', get_lang('Name'));
  44. $form->addText('link', get_lang('Links'));
  45. $allowedPictureTypes = ['jpg', 'jpeg', 'png'];
  46. $form->addFile('icon', get_lang('Custom icon'));
  47. $form->addRule(
  48. 'icon',
  49. get_lang('Only PNG, JPG or GIF images allowed').' ('.implode(',', $allowedPictureTypes).')',
  50. 'filetype',
  51. $allowedPictureTypes
  52. );
  53. $form->addSelect(
  54. 'target',
  55. get_lang('Link\'s target'),
  56. [
  57. '_self' => get_lang('Open self'),
  58. '_blank' => get_lang('Open blank'),
  59. ]
  60. );
  61. $form->addSelect(
  62. 'visibility',
  63. get_lang('Visibility'),
  64. [1 => get_lang('Visible'), 0 => get_lang('invisible')]
  65. );
  66. $form->addTextarea(
  67. 'description',
  68. get_lang('Description'),
  69. ['rows' => '3', 'cols' => '40']
  70. );
  71. $form->addButtonUpdate(get_lang('Update'));
  72. $form->addHtml('</div>');
  73. $form->addHtml('<div class="col-md-5">');
  74. if (isset($tool['custom_icon']) && !empty($tool['custom_icon'])) {
  75. $form->addLabel(
  76. get_lang('Current icon'),
  77. Display::img(
  78. CourseHome::getCustomWebIconPath().$tool['custom_icon']
  79. )
  80. );
  81. $form->addCheckBox('delete_icon', null, get_lang('Delete picture'));
  82. }
  83. $form->addHtml('</div>');
  84. $form->setDefaults($tool);
  85. $content = $form->returnForm();
  86. if ($form->validate()) {
  87. $data = $form->getSubmitValues();
  88. CourseHome::updateTool($id, $data);
  89. Display::addFlash(Display::return_message(get_lang('Update successful')));
  90. if (isset($data['delete_icon'])) {
  91. CourseHome::deleteIcon($id);
  92. }
  93. $currentUrlReturn = api_get_self().'?'.api_get_cidreq();
  94. header('Location: '.$currentUrlReturn);
  95. exit;
  96. }
  97. break;
  98. case 'list':
  99. default:
  100. $toolList = CourseHome::toolsIconsAction(
  101. api_get_course_int_id(),
  102. api_get_session_id()
  103. );
  104. $list = [];
  105. $tmp = [];
  106. foreach ($toolList as $tool) {
  107. $tmp['id'] = $tool['id'];
  108. $tmp['name'] = Security::remove_XSS(stripslashes($tool['name']));
  109. $toolIconName = 'Tool'.api_underscore_to_camel_case($tool['name']);
  110. $toolIconName = isset($$toolIconName) ? get_lang($toolIconName) : $tool['name'];
  111. $tmp['name'] = $toolIconName;
  112. $tmp['link'] = $tool['link'];
  113. if (!empty($tool['custom_icon'])) {
  114. $image = CourseHome::getCustomWebIconPath().$tool['custom_icon'];
  115. $icon = Display::img($image, $tool['name']);
  116. } else {
  117. $image = 'tool_'.(substr($tool['image'], 0, strpos($tool['image'], '.'))).'.png';
  118. $icon = Display::return_icon(
  119. $image,
  120. null,
  121. ['id' => 'tool_'.$tool['id']],
  122. ICON_SIZE_BIG,
  123. false,
  124. true
  125. );
  126. }
  127. $tmp['image'] = $icon;
  128. $tmp['visibility'] = $tool['visibility'];
  129. $delete = (!empty($tool['custom_icon'])) ? "<a class=\"btn btn-default\" onclick=\"javascript:
  130. if(!confirm('".addslashes(api_htmlentities(get_lang('Please confirm your choice'), ENT_QUOTES, $charset)).
  131. "')) return false;\" href=\"".api_get_self().'?action=delete_icon&id='.$tool['iid'].'&'.api_get_cidreq()."\">
  132. <i class=\"fas fa-trash-alt\"></i></a>" : "";
  133. $edit = '<a class="btn btn-outline-secondary btn-sm" href="'.api_get_self().'?action=edit_icon&id='.$tool['iid'].'&'.api_get_cidreq().'"><i class="fas fa-pencil-alt"></i></a>';
  134. $tmp['action'] = $edit.$delete;
  135. $list[] = $tmp;
  136. }
  137. $tpl->assign('tools', $list);
  138. $layout = $tpl->get_template("course_info/tools.html.twig");
  139. $content = $tpl->fetch($layout);
  140. break;
  141. }
  142. $tpl = new Template($toolName);
  143. $tpl->assign('content', $content);
  144. $tpl->display_one_col_template();