tools.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once '../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']) ? intval($_GET['id']) : '';
  16. $toolName = get_lang('CustomizeIcons');
  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('Updated')));
  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[] = array('url' => api_get_self().'?'.api_get_cidreq(), 'name' => get_lang('CustomizeIcons'));
  35. $toolName = Security::remove_XSS(stripslashes($tool['name']));
  36. $currentUrl = api_get_self().'?action=edit_icon&id=' . $id.'&'.api_get_cidreq();
  37. $form = new FormValidator('icon_edit', 'post', $currentUrl);
  38. $form->addHeader(get_lang('EditIcon'));
  39. $form->addHtml('<div class="col-md-7">');
  40. $form->addText('name', get_lang('Name'));
  41. $form->addText('link', get_lang('Links'));
  42. $allowed_picture_types = array ('jpg', 'jpeg', 'png');
  43. $form->addFile('icon', get_lang('CustomIcon'));
  44. $form->addRule('icon', get_lang('OnlyImagesAllowed').' ('.implode(',', $allowed_picture_types).')', 'filetype', $allowed_picture_types);
  45. $form->addselect('target', get_lang('Target'), array('_self' => '_self', '_blank' => '_blank'));
  46. $form->addselect('visibility', get_lang('Visibility'), array(1 => get_lang('Visible'), 0 => get_lang('Invisible')));
  47. $form->addtextarea('description', get_lang('Description'),array ('rows' => '3', 'cols' => '40'));
  48. $form->addButtonUpdate(get_lang('Update'));
  49. $form->addHtml('</div>');
  50. $form->addHtml('<div class="col-md-5">');
  51. if (isset($tool['custom_icon']) && !empty($tool['custom_icon'])) {
  52. $form->addLabel(
  53. get_lang('Icon'),
  54. Display::img(
  55. CourseHome::getCustomWebIconPath().$tool['custom_icon']
  56. )
  57. );
  58. $form->addCheckBox('delete_icon', null, get_lang('DeletePicture'));
  59. }
  60. $form->addHtml('</div>');
  61. $form->setDefaults($tool);
  62. $content = $form->returnForm();
  63. if ($form->validate()) {
  64. $data = $form->getSubmitValues();
  65. CourseHome::updateTool($id, $data);
  66. Display::addFlash(Display::return_message(get_lang('Updated')));
  67. if (isset($data['delete_icon'])) {
  68. CourseHome::deleteIcon($id);
  69. }
  70. $currentUrlReturn = api_get_self().'?'.api_get_cidreq();
  71. header('Location: '.$currentUrlReturn);
  72. exit;
  73. }
  74. break;
  75. case 'list':
  76. default:
  77. $toolList = CourseHome::toolsIconsAction(
  78. api_get_course_int_id(),
  79. api_get_session_id()
  80. );
  81. $iconsTools = '<div id="custom-icons">';
  82. $iconsTools .= Display::page_header(get_lang('CustomizeIcons'), null, 'h4');
  83. $iconsTools .= '<div class="row">';
  84. foreach ($toolList as $tool) {
  85. $tool['name'] = Security::remove_XSS(stripslashes($tool['name']));
  86. $toolIconName = 'Tool' . api_underscore_to_camel_case($tool['name']);
  87. $toolIconName = isset($$toolIconName) ? get_lang($toolIconName) : $tool['name'];
  88. $iconsTools .= '<div class="col-md-2">';
  89. $iconsTools .= '<div class="items-tools">';
  90. if (!empty($tool['custom_icon'])) {
  91. $image = getCustomWebIconPath().$tool['custom_icon'];
  92. $icon = Display::img($image, $toolIconName);
  93. } else {
  94. $image = (substr($tool['image'], 0, strpos($tool['image'], '.'))).'.png';
  95. $icon = Display::return_icon(
  96. $image,
  97. $toolIconName,
  98. array('id' => 'tool_'.$tool['id']),
  99. ICON_SIZE_BIG,
  100. false
  101. );
  102. }
  103. $delete = (!empty($tool['custom_icon'])) ? "<a class=\"btn btn-default\" onclick=\"javascript:
  104. if(!confirm('".addslashes(api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES, $charset)).
  105. "')) return false;\" href=\"". api_get_self() . '?action=delete_icon&id=' . $tool['iid'] . '&'.api_get_cidreq()."\">
  106. <i class=\"fa fa-trash-o\"></i></a>" : "";
  107. $edit = '<a class="btn btn-default" href="' . api_get_self() . '?action=edit_icon&id=' . $tool['iid'] . '&'.api_get_cidreq().'"><i class="fa fa-pencil"></i></a>';
  108. $iconsTools .= '<div class="icon-tools">'. $icon . '</div>';
  109. $iconsTools .= '<div class="name-tools">' . $toolIconName . '</div>';
  110. $iconsTools .= '<div class="toolbar">' . $edit . $delete . '</div>';
  111. $iconsTools .= '</div>';
  112. $iconsTools .= '</div>';
  113. }
  114. $iconsTools .= '</div>';
  115. $iconsTools .= '</div>';
  116. $content = $iconsTools;
  117. break;
  118. }
  119. /**
  120. * @return string
  121. */
  122. function getCustomWebIconPath()
  123. {
  124. // Check if directory exists or create it if it doesn't
  125. $dir = api_get_path(WEB_COURSE_PATH).api_get_course_path().'/upload/course_home_icons/';
  126. return $dir;
  127. }
  128. $tpl = new Template($toolName);
  129. $tpl->assign('content', $content);
  130. $template = $tpl->get_template('layout/layout_1_col.tpl');
  131. $tpl->display($template);