extra_field.ajax.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\Tag;
  4. require_once __DIR__.'/../global.inc.php';
  5. $action = isset($_GET['a']) ? $_GET['a'] : '';
  6. $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : null;
  7. $fieldId = isset($_REQUEST['field_id']) ? $_REQUEST['field_id'] : null;
  8. switch ($action) {
  9. case 'delete_file':
  10. api_protect_admin_script();
  11. $itemId = isset($_REQUEST['item_id']) ? $_REQUEST['item_id'] : null;
  12. $extraFieldValue = new ExtraFieldValue($type);
  13. $data = $extraFieldValue->get_values_by_handler_and_field_id($itemId, $fieldId);
  14. if (!empty($data) && isset($data['id']) && !empty($data['value'])) {
  15. $extraFieldValue->deleteValuesByHandlerAndFieldAndValue($itemId, $data['field_id'], $data['value']);
  16. echo 1;
  17. break;
  18. }
  19. echo 0;
  20. break;
  21. case 'get_second_select_options':
  22. $option_value_id = isset($_REQUEST['option_value_id']) ? $_REQUEST['option_value_id'] : null;
  23. if (!empty($type) && !empty($fieldId) && !empty($option_value_id)) {
  24. $field_options = new ExtraFieldOption($type);
  25. echo $field_options->get_second_select_field_options_by_field(
  26. $option_value_id,
  27. true
  28. );
  29. }
  30. break;
  31. case 'search_tags':
  32. header('Content-Type: application/json');
  33. $tag = isset($_REQUEST['q']) ? $_REQUEST['q'] : null;
  34. $result = [];
  35. if (empty($tag)) {
  36. echo json_encode(['items' => $result]);
  37. exit;
  38. }
  39. $extraFieldOption = new ExtraFieldOption($type);
  40. $tags = Database::getManager()
  41. ->getRepository('ChamiloCoreBundle:Tag')
  42. ->createQueryBuilder('t')
  43. ->where("t.tag LIKE :tag")
  44. ->andWhere('t.fieldId = :field')
  45. ->setParameter('field', $fieldId)
  46. ->setParameter('tag', "$tag%")
  47. ->getQuery()
  48. ->getResult();
  49. /** @var Tag $tag */
  50. foreach ($tags as $tag) {
  51. $result[] = [
  52. 'id' => $tag->getTag(),
  53. 'text' => $tag->getTag(),
  54. ];
  55. }
  56. echo json_encode(['items' => $result]);
  57. break;
  58. case 'search_options_from_tags':
  59. $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : null;
  60. $fieldId = isset($_REQUEST['field_id']) ? $_REQUEST['field_id'] : null;
  61. $tag = isset($_REQUEST['tag']) ? $_REQUEST['tag'] : null;
  62. $extraFieldOption = new ExtraFieldOption($type);
  63. $from = isset($_REQUEST['from']) ? $_REQUEST['from'] : '';
  64. $search = isset($_REQUEST['search']) ? $_REQUEST['search'] : '';
  65. $options = isset($_REQUEST['options']) ? json_decode($_REQUEST['options']) : '';
  66. $extraField = new ExtraField('session');
  67. $result = $extraField->searchOptionsFromTags($from, $search, $options);
  68. $options = [];
  69. $groups = [];
  70. foreach ($result as $data) {
  71. // Try to get the translation
  72. $displayText = $data['display_text'];
  73. $valueToTranslate = str_replace('-', '', $data['value']);
  74. $valueTranslated = str_replace(['[=', '=]'], '', get_lang($valueToTranslate));
  75. if ($valueToTranslate != $valueTranslated) {
  76. $displayText = $valueTranslated;
  77. }
  78. $groups[$displayText][] = [
  79. 'id' => $data['id'],
  80. 'text' => $data['tag'],
  81. ];
  82. }
  83. foreach ($groups as $key => $data) {
  84. $options[] = [
  85. 'text' => $key,
  86. 'children' => $groups[$key],
  87. ];
  88. }
  89. echo json_encode($options);
  90. break;
  91. case 'order':
  92. $variable = isset($_REQUEST['field_variable']) ? $_REQUEST['field_variable'] : '';
  93. $save = isset($_REQUEST['save']) ? $_REQUEST['save'] : '';
  94. $values = isset($_REQUEST['values']) ? json_decode($_REQUEST['values']) : '';
  95. $extraField = new ExtraField('session');
  96. $extraFieldInfo = $extraField->get_handler_field_info_by_field_variable(str_replace('extra_', '', $variable));
  97. $em = Database::getManager();
  98. $search = [
  99. 'user' => api_get_user_id(),
  100. 'field' => $extraFieldInfo['id'],
  101. ];
  102. $extraFieldSavedSearch = $em->getRepository('ChamiloCoreBundle:ExtraFieldSavedSearch')->findOneBy($search);
  103. if ($save) {
  104. $extraField = new \Chamilo\CoreBundle\Entity\ExtraFieldSavedSearch('session');
  105. if ($extraFieldSavedSearch) {
  106. $extraFieldSavedSearch->setValue($values);
  107. $em->merge($extraFieldSavedSearch);
  108. $em->flush();
  109. }
  110. }
  111. if ($extraFieldInfo) {
  112. /** @var \Chamilo\CoreBundle\Entity\ExtraFieldSavedSearch $options */
  113. $extraFieldSavedSearch = $em->getRepository('ChamiloCoreBundle:ExtraFieldSavedSearch')->findOneBy($search);
  114. $values = $extraFieldSavedSearch->getValue();
  115. $url = api_get_self().'?a=order&save=1&field_variable='.$variable;
  116. $html = '
  117. <script>
  118. $(function() {
  119. $( "#sortable" ).sortable();
  120. $( "#sortable" ).disableSelection();
  121. $( "#link_'.$variable.'" ).on("click", function() {
  122. var newList = [];
  123. $("#sortable").find("li").each(function(){
  124. newList.push($(this).text());
  125. });
  126. var save = JSON.stringify(newList);
  127. $.ajax({
  128. url: "'.$url.'",
  129. dataType: "json",
  130. data: "values="+save,
  131. success: function(data) {
  132. console.log(data);
  133. }
  134. });
  135. alert("'.get_lang('Saved.').'");
  136. location.reload();
  137. return false;
  138. });
  139. });
  140. </script>';
  141. $html .= '<ul id="sortable">';
  142. foreach ($values as $value) {
  143. $html .= '<li class="ui-state-default">';
  144. $html .= $value;
  145. $html .= '</li>';
  146. }
  147. $html .= '</ul>';
  148. $html .= Display::url(get_lang('Save'), '#', ['id' => 'link_'.$variable, 'class' => 'btn btn-primary']);
  149. echo $html;
  150. }
  151. break;
  152. default:
  153. exit;
  154. break;
  155. }
  156. exit;