dolibarr 23.0.3
subtotal_ajaxrow.tpl.php
1<?php
2/* Copyright (C) 2014-2017 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
4 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 * or see https://www.gnu.org/
19 */
20
44// Protection to avoid direct call of template
45if (empty($object) || !is_object($object)) {
46 print "Error, template page ".basename(__FILE__)." can't be called with no object defined.";
47 exit;
48}
49'
50@phan-var-force ?string $fk_element
51@phan-var-force ?Task[] $tasksarray
52';
53
54?>
55
56<!-- BEGIN PHP TEMPLATE SUBTOTAL_AJAXROW.TPL.PHP - Script to enable drag and drop on lines of a table using subtotal lines -->
57<?php
58$id = $object->id;
59$fk_element = empty($object->fk_element) ? $fk_element : $object->fk_element;
60$table_element_line = (empty($table_element_line) ? $object->table_element_line : $table_element_line);
61$nboflines = count($object->lines);
62$forcereloadpage = getDolGlobalInt('MAIN_FORCE_RELOAD_PAGE');
63$tagidfortablednd = (empty($tagidfortablednd) ? 'tablelines' : $tagidfortablednd);
64$filepath = (empty($filepath) ? '' : $filepath);
65$langs->load("subtotals");
66
67if (GETPOST('action', 'aZ09') != 'editline' && $nboflines > 1 && $conf->browser->layout != 'phone') { ?>
68<div id="notification-message" hidden=""></div>
69<script>
70function openDialog() {
71 jQuery(function() {
72 jQuery("#notification-message").dialog({
73 resizable: false,
74 modal: true,
75 buttons: {
76 Ok: function() {
77 jQuery(this).dialog('close');
78 }
79 }
80 });
81 });
82}
83
84function init(){
85 $(".imgupforline").hide();
86 $(".imgdownforline").hide();
87 $(".lineupdown").removeAttr('href');
88 console.log($(".tdlineupdown"));
89 $(".tdlineupdown").each(function (tdindex, tdline) {
90 var gripimg = tdline.dataset.gripimg ?? 'grip.png';
91 console.log(gripimg);
92 $(tdline).css("background-image",'url(<?php echo DOL_URL_ROOT.'/theme/'.$conf->theme.'/img/'; ?>' + gripimg + ')');
93 $(tdline).css("background-repeat","no-repeat");
94 $(tdline).css("background-position","center center");
95 console.log($(".tdlineupdown")[tdindex], tdline);
96 })
97
98 console.log("Prepare tableDnd for #<?php echo $tagidfortablednd; ?>");
99 var inital_table = $("#<?php echo $tagidfortablednd; ?> .drag").map((_, el) => $(el)[0]).get();
100 var rowsToMove = [];
101 $("#<?php echo $tagidfortablednd; ?>").tableDnD({
102 onDragStart: function (table, row) {
103 if (row.parentNode.dataset.level > 0) {
104 var hide = false;
105 $("#<?php echo $tagidfortablednd; ?> .drag").each(
106 function (intIndex) {
107 if (hide) {
108 if ($(this)[0].dataset.level>-row.parentNode.dataset.level && $(this)[0].dataset.level<=row.parentNode.dataset.level) {
109 hide = false;
110 return false;
111 }
112 rowsToMove.unshift($(this));
113 if (Math.abs($(this)[0].dataset.level) <= Math.abs(row.parentNode.dataset.level)) {
114 hide = false;
115 }
116 }
117 if ($(this)[0].id === row.parentNode.id) {
118 rowsToMove.unshift($(this));
119 hide = true;
120 }
121 });
122 if (!hide) {
123 rowsToMove.forEach(function ($hiddenRow, i) {
124 if (i<rowsToMove.length-1) {
125 $hiddenRow.hide();
126 }
127 });
128 } else {
129 rowsToMove = [];
130 }
131 }
132 },
133 onDragStop: function(table, row) {
134 if (rowsToMove.length !== 0) {
135 rowsToMove.forEach(function (hiddenRow) {
136 hiddenRow.insertAfter($("#" + row.id));
137 hiddenRow.show();
138 });
139 rowsToMove = [];
140 }
141
142 if (row.dataset.desc !== undefined) {
143 checkLinePosition(row, inital_table);
144 }
145 inital_table = $("#<?php echo $tagidfortablednd; ?> .drag").map((_, el) => $(el)[0]).get();
146
147 var reloadpage = "<?php echo $forcereloadpage; ?>";
148 console.log("tableDND onDrop");
149 console.log(decodeURI($("#<?php echo $tagidfortablednd; ?>").tableDnDSerialize()));
150 $('#<?php echo $tagidfortablednd; ?> tr[data-element=extrafield]').attr('id', ''); // Set extrafields id to empty value in order to ignore them in tableDnDSerialize function
151 $('#<?php echo $tagidfortablednd; ?> tr[data-ignoreidfordnd=1]').attr('id', ''); // Set id to empty value in order to ignore them in tableDnDSerialize function
152 var roworder = cleanSerialize(decodeURI($("#<?php echo $tagidfortablednd; ?>").tableDnDSerialize()));
153 var table_element_line = "<?php echo $table_element_line; ?>";
154 var fk_element = "<?php echo $fk_element; ?>";
155 var element_id = "<?php echo $id; ?>";
156 var filepath = "<?php echo urlencode($filepath); ?>";
157 var token = "<?php echo currentToken(); ?>"; // We use old 'token' and not 'newtoken' for Ajax call because the ajax page has the NOTOKENRENEWAL constant set.
158 $.post("<?php echo DOL_URL_ROOT; ?>/core/ajax/row.php",
159 {
160 roworder: roworder,
161 table_element_line: table_element_line,
162 fk_element: fk_element,
163 element_id: element_id,
164 filepath: filepath,
165 token: token
166 },
167 function() {
168 console.log("tableDND end of ajax call");
169 console.log(roworder, table_element_line, fk_element, element_id, filepath, token);
170 if (reloadpage == 1) {
171 <?php
172 $redirectURL = empty($urltorefreshaftermove) ? ($_SERVER['PHP_SELF'].'?'.dol_escape_js($_SERVER['QUERY_STRING'])) : $urltorefreshaftermove;
173 // remove action parameter from URL
174 $redirectURL = preg_replace('/(&|\?)action=[^&#]*/', '', $redirectURL);
175 ?>
176 location.href = '<?php echo dol_escape_js($redirectURL); ?>';
177 }
178 });
179 },
180 onDragClass: "dragClass",
181 dragHandle: "td.tdlineupdown"
182 });
183 $(".tdlineupdown").hover( function() { $(this).addClass('showDragHandle'); },
184 function() { $(this).removeClass('showDragHandle'); }
185 );
186}
187
188function checkLinePosition(row, inital_table) {
189 const tbody = $("#<?php echo $tagidfortablednd; ?> .drag").map((_, el) => $(el)[0]).get();
190
191 for (var k = 0; k < tbody.length; k++) {
192 const currentRow = tbody[k];
193 if (currentRow.dataset.rang !== undefined) {
194 currentRow.dataset.rang = k+1;
195 }
196 }
197
198 var rowLevel = parseInt(row.dataset.level);
199 var cancelLineMove = false;
200 var found_title = rowLevel >= 0;
201 var ignore_level = 0;
202
203 for (var i = row.dataset.rang-2; i >= 0; i--) {
204
205 if (tbody[i].dataset.desc !== undefined) {
206 const currentRowLevel1 = parseInt(tbody[i].dataset.level, 10);
207 console.log(currentRowLevel1, -rowLevel, currentRowLevel1 < -rowLevel);
208 if (rowLevel > 0) {
209 // Title line placement managing
210 if (currentRowLevel1 <= ignore_level && ignore_level !== 0) {
211 console.log("here");
212 // continue
213 } else if (-currentRowLevel1 === rowLevel) {
214 break;
215 } else if (currentRowLevel1 > 0 && currentRowLevel1 < rowLevel) {
216 if (rowLevel - currentRowLevel1 > 1) {
217 $("#notification-message").text("<?php echo $langs->trans("PreviousTitleLevelTooHigh"); ?>");
218 cancelLineMove = true;
219 break;
220 }
221 break;
222 // console.log(tbody[i].dataset.desc, currentRowLevel1, rowLevel);
223 } else if (currentRowLevel1 === rowLevel) {
224 for (var j = row.dataset.rang; j < tbody.length; j++) {
225 if (tbody[j].dataset.desc !== undefined) {
226 const currentRowLevel2 = parseInt(tbody[j].dataset.level, 10);
227 if (tbody[i].dataset.desc === tbody[j].dataset.desc && currentRowLevel1 === -currentRowLevel2) {
228 $("#notification-message").text("<?php echo $langs->trans("TitleUnderSameLevelSTLine"); ?>");
229 cancelLineMove = true;
230 break;
231 }
232 }
233 }
234 } else if (currentRowLevel1 > rowLevel) {
235 for (var j = row.dataset.rang; j < tbody.length; j++) {
236 if (tbody[j].dataset.desc !== undefined) {
237 const currentRowLevel2 = parseInt(tbody[j].dataset.level, 10);
238 if (row.dataset.desc !== tbody[j].dataset.desc && currentRowLevel2 <= -rowLevel) {
239 $("#notification-message").text("<?php echo $langs->trans("TitleAfterStLineOfSameLevelTitle"); ?>");
240 cancelLineMove = true;
241 break;
242 }
243 }
244 }
245 break;
246 } else if (currentRowLevel1 < -rowLevel) {
247 ignore_level = currentRowLevel1;
248 } else {
249 $("#notification-message").text("<?php echo $langs->trans("TitleUnderSameLevelOrGreater"); ?>");
250 cancelLineMove = true;
251 break;
252 }
253 } else if (rowLevel < 0) {
254 // Subtotal line placement managing
255 if (currentRowLevel1 < 0 && rowLevel <= currentRowLevel1 || currentRowLevel1 >0 && -rowLevel >= currentRowLevel1) {
256 console.log(rowLevel, currentRowLevel1);
257 if (tbody[i].dataset.desc === row.dataset.desc) {
258 found_title = true;
259 break;
260 }else if (-rowLevel === currentRowLevel1) {
261 $("#notification-message").text("<?php echo $langs->trans("STLineUnderCorrespondingTitleDesc"); ?>");
262 cancelLineMove = true;
263 break;
264 } else if (-rowLevel > currentRowLevel1) {
265 $("#notification-message").text("<?php echo $langs->trans("STLineUnderCorrespondingTitle"); ?>");
266 cancelLineMove = true;
267 break;
268 }
269 }
270 }
271 }
272 }
273 if (!found_title) {
274 $("#notification-message").text("<?php echo $langs->trans("STLineUnderTitle"); ?>");
275 cancelLineMove = true;
276 }
277
278 if (cancelLineMove) {
279 const tbody_to_replace = $(row).parent()[0];
280 $(tbody_to_replace).empty();
281 $(tbody_to_replace).append(inital_table);
282 init();
283 openDialog();
284 }
285}
286
287$(document).ready(function(){
288 init()
289});
290
291</script>
292<?php } else { ?>
293<script>
294$(document).ready(function(){
295 $(".imgupforline").hide();
296 $(".imgdownforline").hide();
297 $(".lineupdown").removeAttr('href');
298});
299</script>
300<?php } ?>
301<!-- END PHP TEMPLATE AJAXROW.TPL.PHP -->
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_escape_js($stringtoescape, $mode=0, $noescapebackslashn=0)
Returns text escaped for inclusion into javascript code.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
treeview li table
No Email.