ajaxPackage.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. $res = 0;
  3. // Try main.inc.php into web root known defined into CONTEXT_DOCUMENT_ROOT (not always defined)
  4. if (!$res && !empty($_SERVER["CONTEXT_DOCUMENT_ROOT"])) {
  5. $res = @include $_SERVER["CONTEXT_DOCUMENT_ROOT"] . "/main.inc.php";
  6. }
  7. // Try main.inc.php into web root detected using web root calculated from SCRIPT_FILENAME
  8. $tmp = empty($_SERVER['SCRIPT_FILENAME']) ? '' : $_SERVER['SCRIPT_FILENAME'];
  9. $tmp2 = realpath(__FILE__);
  10. $i = strlen($tmp) - 1;
  11. $j = strlen($tmp2) - 1;
  12. while ($i > 0 && $j > 0 && isset($tmp[$i]) && isset($tmp2[$j]) && $tmp[$i] == $tmp2[$j]) {
  13. $i--;
  14. $j--;
  15. }
  16. if (!$res && $i > 0 && file_exists(substr($tmp, 0, ($i + 1)) . "/main.inc.php")) {
  17. $res = @include substr($tmp, 0, ($i + 1)) . "/main.inc.php";
  18. }
  19. if (!$res && $i > 0 && file_exists(dirname(substr($tmp, 0, ($i + 1))) . "/main.inc.php")) {
  20. $res = @include dirname(substr($tmp, 0, ($i + 1))) . "/main.inc.php";
  21. }
  22. // Try main.inc.php using relative path
  23. if (!$res && file_exists("../main.inc.php")) {
  24. $res = @include "../main.inc.php";
  25. }
  26. if (!$res && file_exists("../../main.inc.php")) {
  27. $res = @include "../../main.inc.php";
  28. }
  29. if (!$res && file_exists("../../../main.inc.php")) {
  30. $res = @include "../../../main.inc.php";
  31. }
  32. if (!$res) {
  33. die("Include of main fails");
  34. }
  35. require_once DOL_DOCUMENT_ROOT . '/custom/settlements/class/helper.class.php';
  36. $helper = new Helper($db);
  37. $oneselect = GETPOST('oneselect', 'int');
  38. $search_package_name = GETPOST('search_package_name', 'alpha');
  39. $toselectnewpackage = GETPOST('toselectnewpackage', 'alpha');
  40. $reduction = GETPOST('reduction', 'alpha');
  41. $groupid = GETPOST('groupid', 'int');
  42. $selectedArray = explode(',', $toselectnewpackage);
  43. if (isset($_SESSION['package_array'])) {
  44. $package_array = $_SESSION['package_array'];
  45. if ($toselectnewpackage !== '') {
  46. $_SESSION['package_array'] = array_unique(array_merge($package_array, $selectedArray));
  47. }
  48. if ($reduction !== '') {
  49. $_SESSION['package_array'] = explode(',', $reduction);
  50. }
  51. $selectedArray = $_SESSION['package_array'];
  52. if ($toselectnewpackage === '' && $reduction === '' && $search_package_name === '') {
  53. unset($_SESSION['package_array']);
  54. $selectedArray = [];
  55. }
  56. } else {
  57. $_SESSION['package_array'] = $selectedArray;
  58. }
  59. $allPackages = $search_package_name !== '' ? $helper->getFilteredPackages($search_package_name, $groupid) : $helper->getAllPackagesFromSelectedEntity($groupid);
  60. $form = new Form($db);
  61. if (is_array($allPackages)) {
  62. print '<form method="POST" id="AssociationFormList" action="' . $url . '" onkeyup="submitFormAjaxTools()">' . "\n";
  63. $searchedString = '<table style="width:100%;">';
  64. $DeviceCounter = $helper->getNumberOfDevices();
  65. if (!empty($allPackages['freePackages'])) {
  66. foreach ($allPackages['freePackages'] as $freePackage) {
  67. $numberOfDevices3 = '';
  68. $devicesArray3 = [];
  69. if (isset($DeviceCounter[$freePackage['rowid']])) {
  70. $numberOfDevices3 = '(' . $DeviceCounter[$freePackage['rowid']] . ' ' . $langs->trans('device') . ')';
  71. $devicesArray3 = $helper->getDevicesById($db, $freePackage['rowid']);
  72. }
  73. $popupBox3Text = $helper->createPopoupBoxForDeviceList($numberOfDevices3, $devicesArray3);
  74. $popupBox3 = $popupBox3Text != '' ? $popupBox3Text : '<font style="color:red;">' . $langs->trans('emptyPackage') . '</font>';
  75. $selected = in_array($freePackage['rowid'], $selectedArray) ? 'checked' : '';
  76. $searchedString .= '<tr style="height:30px;">
  77. <td class="nowrap center"><input id="id_3_' . $freePackage['rowid'] . '" class="flat checkforselectadd" type="checkbox" onclick="submitFormAjaxAddPackage(event)" name="toselectnewpackage[]" value="' . $freePackage['rowid'] . '" ' . $selected . '></td>
  78. <td><span class="fa fa-toolbox pictofixedwidth"></span> ' . $freePackage['ref'] . '</td>
  79. <td>' . $popupBox3 . '</td>
  80. </tr>';
  81. }
  82. }
  83. $searchedString .= '</table><table style="width:100%; color: red;"><tr><td>';
  84. if (is_array($allPackages['addedPackages'])) {
  85. foreach ($allPackages['addedPackages'] as $addedPackage) {
  86. $numberOfDevices4 = '';
  87. $devicesArray4 = [];
  88. if (isset($DeviceCounter[$addedPackage['gprowid']])) {
  89. $numberOfDevices4 = '(' . $DeviceCounter[$addedPackage['gprowid']] . ' ' . $langs->trans('device') . ')';
  90. $devicesArray4 = $helper->getDevicesById($db, $addedPackage['rowid']);
  91. }
  92. $popupBox4Text = $helper->createPopoupBoxForDeviceList($numberOfDevices4, $devicesArray4);
  93. $popupBox4 = $popupBox4Text != '' ? $popupBox4Text : '<font style="color:red;">' . $langs->trans('emptyPackage') . '</font>';
  94. $searchedString .= '
  95. <table style="width:100%; border: 1px solid lightgrey; border-radius: 10px; margin-bottom: 20px;">
  96. <tr>
  97. <td colspan="2" style="text-align: center; width: 100%;"><a href="settlementtoolssettingsindex.php?idmenu=926&mainmenu=settlements&leftmenu=&id=' . $addedPackage['gprowid'] . '&nopackageuser=1">' . $addedPackage['gplabel'] . '</td>
  98. </tr>
  99. <tr style="height:30px;">
  100. <td><span class="fa fa-toolbox pictofixedwidth"></span><a href="/custom/settlements/settlementpackagetoolssettingsindex.php?idmenu=1592&mainmenu=settlements&leftmenu=&id=' . $addedPackage['rowid'] . '"> ' . $addedPackage['ref'] . '</a></td>
  101. <td>' . $popupBox4 . '</td>
  102. </tr>
  103. </table>'
  104. ;
  105. }
  106. }
  107. $searchedString .= '</td></tr></table>';
  108. }
  109. print $searchedString;
  110. ?>
  111. <script>
  112. function submitFormAjaxAddPackage(event) {
  113. console.log('submit');
  114. var checkbox = event.target;
  115. var groupid = document.getElementById("groupid");
  116. if (checkbox.checked) {
  117. var input = document.getElementById("package_search");
  118. var checkboxes = document.querySelectorAll('.checkforselectadd:checked');
  119. var values = Array.from(checkboxes).map(function (checkbox1) {
  120. return checkbox1.value;
  121. });
  122. var xhr = new XMLHttpRequest();
  123. xhr.open("GET", "ajaxPackage.php?search_package_name=" + input.value + "&toselectnewpackage=" + values + "&groupid=" + groupid.value, true);
  124. xhr.onreadystatechange = function () {
  125. if (xhr.readyState === 4 && xhr.status === 200) {
  126. $("#ajaxPackage").html(xhr.responseText);
  127. }
  128. };
  129. xhr.send();
  130. } else {
  131. var input = document.getElementById("package_search");
  132. var checkboxes = document.querySelectorAll('.checkforselectadd:checked');
  133. var values = Array.from(checkboxes).map(function (checkbox1) {
  134. return checkbox1.value;
  135. });
  136. var xhr = new XMLHttpRequest();
  137. xhr.open("GET", "ajaxPackage.php?search_package_name=" + input.value + "&reduction=" + values + "&groupid=" + groupid.value, true);
  138. xhr.onreadystatechange = function () {
  139. if (xhr.readyState === 4 && xhr.status === 200) {
  140. $("#ajaxPackage").html(xhr.responseText);
  141. }
  142. };
  143. xhr.send();
  144. }
  145. }
  146. $('.checkforselectadd').click(function () {
  147. if ($(this).is(':checked')) {
  148. $('#divAdd').css('display', 'inline-block');
  149. $('#massactionadd').css('display', 'inline-block');
  150. $('.massactionaddselect ').css('display', '');
  151. $('.massactionaddconfirmed ').css('display', '');
  152. } else {
  153. var checkboxes = document.querySelectorAll('.checkforselectadd:checked');
  154. var values = Array.from(checkboxes).map(function (checkbox) {
  155. return checkbox.value;
  156. });
  157. if (values.length === 0) {
  158. $('#divAdd').css('display', 'none');
  159. $('#massactionadd').css('display', 'none');
  160. $('.massactionaddselect ').css('display', 'none');
  161. $('.massactionaddconfirmed ').css('display', 'none');
  162. }
  163. }
  164. })
  165. </script>