legal_add.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Management of legal conditions.
  5. *
  6. * @package chamilo.admin
  7. */
  8. $cidReset = true;
  9. require_once __DIR__.'/../inc/global.inc.php';
  10. $this_section = SECTION_PLATFORM_ADMIN;
  11. api_protect_admin_script();
  12. if (api_get_setting('allow_terms_conditions') !== 'true') {
  13. api_not_allowed(true);
  14. }
  15. // Create the form
  16. $form = new FormValidator('addlegal');
  17. $defaults = [];
  18. $term_preview = [
  19. 'type' => 0,
  20. 'content' => '',
  21. 'changes' => '',
  22. ];
  23. $extraField = new ExtraField('terms_and_condition');
  24. $types = LegalManager::getTreatmentTypeList();
  25. foreach ($types as $variable => $name) {
  26. $label = 'PersonalData'.ucfirst($name).'Title';
  27. $params = [
  28. 'variable' => $variable,
  29. 'display_text' => $label,
  30. 'field_type' => ExtraField::FIELD_TYPE_TEXTAREA,
  31. 'default_value' => '',
  32. 'visible' => true,
  33. 'changeable' => true,
  34. 'filter' => true,
  35. 'visible_to_self' => true,
  36. 'visible_to_others' => true,
  37. ];
  38. $extraField->save($params);
  39. }
  40. if ($form->validate()) {
  41. $check = Security::check_token('post');
  42. if ($check) {
  43. $values = $form->getSubmitValues();
  44. $lang = $values['language'];
  45. // language id
  46. $lang = api_get_language_id($lang);
  47. $type = 0;
  48. if (isset($values['type'])) {
  49. $type = $values['type'];
  50. }
  51. $content = '';
  52. if (isset($values['content'])) {
  53. $content = $values['content'];
  54. }
  55. $changes = '';
  56. if (isset($values['changes'])) {
  57. $changes = $values['changes'];
  58. }
  59. $submit = $values['send'];
  60. $default['content'] = $content;
  61. if (isset($values['language'])) {
  62. if ($submit == 'back') {
  63. header('Location: legal_add.php');
  64. exit;
  65. } elseif ($submit === 'save') {
  66. $id = LegalManager::add($lang, $content, $type, $changes, $values);
  67. if (!empty($id)) {
  68. Display::addFlash(Display::return_message(get_lang('TermAndConditionSaved'), 'success'));
  69. } else {
  70. Display::addFlash(Display::return_message(get_lang('TermAndConditionNotSaved'), 'warning'));
  71. }
  72. Security::clear_token();
  73. $tok = Security::get_token();
  74. header('Location: legal_list.php?sec_token='.$tok);
  75. exit();
  76. } elseif ($submit === 'preview') {
  77. $defaults['type'] = $type;
  78. $defaults['content'] = $content;
  79. $defaults['changes'] = $changes;
  80. $term_preview = $defaults;
  81. $term_preview['type'] = (int) $_POST['type'];
  82. } else {
  83. $my_lang = $_POST['language'];
  84. if (isset($_POST['language'])) {
  85. $all_langs = api_get_languages();
  86. if (in_array($my_lang, $all_langs['folder'])) {
  87. $language = api_get_language_id($my_lang);
  88. $term_preview = LegalManager::get_last_condition($language);
  89. $defaults = $term_preview;
  90. if (!$term_preview) {
  91. // there are not terms and conditions
  92. $term_preview['type'] = -1;
  93. $defaults['type'] = 0;
  94. }
  95. }
  96. }
  97. }
  98. }
  99. }
  100. }
  101. $form->setDefaults($defaults);
  102. if (isset($_POST['send'])) {
  103. Security::clear_token();
  104. }
  105. $token = Security::get_token();
  106. $form->addElement('hidden', 'sec_token');
  107. $defaults['sec_token'] = $token;
  108. $form->addElement('header', get_lang('DisplayTermsConditions'));
  109. $jqueryReady = '';
  110. if (isset($_POST['language'])) {
  111. $form->addElement('static', Security::remove_XSS($_POST['language']));
  112. $form->addElement('hidden', 'language', Security::remove_XSS($_POST['language']));
  113. $form->addHtmlEditor(
  114. 'content',
  115. get_lang('Content'),
  116. true,
  117. false,
  118. ['ToolbarSet' => 'terms_and_conditions', 'Width' => '100%', 'Height' => '250']
  119. );
  120. $form->addElement('radio', 'type', '', get_lang('HTMLText'), '0');
  121. $form->addElement('radio', 'type', '', get_lang('PageLink'), '1');
  122. $preview = LegalManager::show_last_condition($term_preview);
  123. if ($term_preview['type'] != -1) {
  124. $preview = LegalManager::replaceTags($preview);
  125. $form->addElement('label', get_lang('Preview'), $preview);
  126. }
  127. $termId = isset($term_preview['id']) ? $term_preview['id'] : 0;
  128. $returnParams = $extraField->addElements(
  129. $form,
  130. $termId,
  131. [],
  132. false,
  133. false,
  134. [],
  135. [],
  136. [],
  137. false,
  138. true,
  139. [],
  140. [],
  141. false,
  142. [],
  143. [],
  144. false,
  145. true
  146. );
  147. $jqueryReady = $returnParams['jquery_ready_content'];
  148. $form->addElement('textarea', 'changes', get_lang('ExplainChanges'), ['width' => '20']);
  149. // Submit & preview button
  150. $buttons = '<div class="row" align="center">
  151. <div class="formw">
  152. <button type="submit" class="btn btn-default back" name="send" value="back">'.get_lang('Back').'</button>
  153. <button type="submit" class="btn btn-default search" name="send" value="preview">'.get_lang('Preview').'</button>
  154. <button type="submit" class="btn btn-primary save" name="send" value="save">'.get_lang('Save').'</button>
  155. </div>
  156. </div>';
  157. $form->addElement('html', $buttons);
  158. } else {
  159. $form->addSelectLanguage('language', get_lang('Language'), null, []);
  160. $form->addButtonSearch(get_lang('Load'), 'send');
  161. }
  162. $tool_name = get_lang('AddTermsAndConditions');
  163. $interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('PlatformAdmin')];
  164. // the $jquery_ready_content variable collects all functions that will be load in the $(document).ready javascript function
  165. $htmlHeadXtra[] = '<script>
  166. $(function () {
  167. '.$jqueryReady.'
  168. });
  169. </script>';
  170. Display::display_header($tool_name);
  171. echo '<script>
  172. function sendlang() {
  173. document.addlegal.sec_token.value=\''.$token.'\';
  174. document.addlegal.submit();
  175. }
  176. </script>';
  177. // action menu
  178. echo '<div class="actions">';
  179. echo '<a href="'.api_get_path(WEB_CODE_PATH).'admin/legal_list.php">'.
  180. Display::return_icon('search.gif', get_lang('EditTermsAndConditions'), '').
  181. get_lang('AllVersions').'</a>';
  182. echo '</div>';
  183. $form->setDefaults($defaults);
  184. $form->display();
  185. Display::display_footer();