dolibarr 25.0.0-alpha
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-2026 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 CommonObject $object
51@phan-var-force ?string $fk_element
52@phan-var-force ?Task[] $tasksarray
53';
54
55?>
56
57<!-- BEGIN PHP TEMPLATE SUBTOTAL_AJAXROW.TPL.PHP - Script to enable drag and drop on lines of a table using subtotal lines -->
58<?php
59$id = $object->id;
60$fk_element = empty($object->fk_element) ? $fk_element : $object->fk_element;
61$table_element_line = (empty($table_element_line) ? $object->table_element_line : $table_element_line);
62$nboflines = count($object->lines);
63$forcereloadpage = getDolGlobalInt('MAIN_FORCE_RELOAD_PAGE');
64$tagidfortablednd = (empty($tagidfortablednd) ? 'tablelines' : $tagidfortablednd);
65$filepath = (empty($filepath) ? '' : $filepath);
66$langs->load("subtotals");
67
68if (GETPOST('action', 'aZ09') != 'editline' && $nboflines > 1 && $conf->browser->layout != 'phone') { ?>
69<div id="notification-message" hidden=""></div>
70<script>
71function openDialog() {
72 jQuery(function() {
73 console.log("open dialog");
74 jQuery("#notification-message").dialog({
75 resizable: false,
76 modal: true,
77 buttons: {
78 Ok: function() {
79 jQuery(this).dialog('close');
80 }
81 }
82 });
83 });
84}
85
86function init(){
87 $(".imgupforline").hide();
88 $(".imgdownforline").hide();
89 $(".lineupdown").removeAttr('href');
90
91 console.log("init() Prepare tableDnd for #<?php echo $tagidfortablednd; ?>");
92
93 $(".tdlineupdown").each(function (tdindex, tdline) {
94 var gripimg = tdline.dataset.gripimg ?? 'grip.png';
95
96 $(tdline).css("background-image",'url(<?php echo DOL_URL_ROOT.'/theme/'.$conf->theme.'/img/'; ?>' + gripimg + ')');
97 $(tdline).css("background-repeat","no-repeat");
98 $(tdline).css("background-position","center center");
99 /* console.log($(".tdlineupdown")[tdindex], tdline); */
100 })
101
102 var inital_table = $("#<?php echo $tagidfortablednd; ?> .drag").map((_, el) => $(el)[0]).get();
103 var rowsToMove = [];
104 $("#<?php echo $tagidfortablednd; ?>").tableDnD({
105 onDragStart: function (table, row) {
106 if (row.parentNode.dataset.level > 0) {
107 var hide = false;
108 $("#<?php echo $tagidfortablednd; ?> .drag").each(
109 function (intIndex) {
110 if (hide) {
111 if ($(this)[0].dataset.level>-row.parentNode.dataset.level && $(this)[0].dataset.level<=row.parentNode.dataset.level) {
112 hide = false;
113 return false;
114 }
115 rowsToMove.unshift($(this));
116 if (Math.abs($(this)[0].dataset.level) <= Math.abs(row.parentNode.dataset.level)) {
117 hide = false;
118 }
119 }
120 if ($(this)[0].id === row.parentNode.id) {
121 rowsToMove.unshift($(this));
122 hide = true;
123 }
124 });
125 if (!hide) {
126 rowsToMove.forEach(function ($hiddenRow, i) {
127 if (i<rowsToMove.length-1) {
128 $hiddenRow.hide();
129 }
130 });
131 } else {
132 rowsToMove = [];
133 }
134 }
135 },
136 onDragStop: function(table, row) {
137 if (rowsToMove.length !== 0) {
138 rowsToMove.forEach(function (hiddenRow) {
139 hiddenRow.insertAfter($("#" + row.id));
140 hiddenRow.show();
141 });
142 rowsToMove = [];
143 }
144
145 if (row.dataset.desc !== undefined) {
146 checkLinePosition(row, inital_table);
147 }
148 inital_table = $("#<?php echo $tagidfortablednd; ?> .drag").map((_, el) => $(el)[0]).get();
149
150 var reloadpage = "<?php echo $forcereloadpage; ?>";
151 console.log("tableDND onDrop");
152 console.log(decodeURI($("#<?php echo $tagidfortablednd; ?>").tableDnDSerialize()));
153 $('#<?php echo $tagidfortablednd; ?> tr[data-element=extrafield]').attr('id', ''); // Set extrafields id to empty value in order to ignore them in tableDnDSerialize function
154 $('#<?php echo $tagidfortablednd; ?> tr[data-ignoreidfordnd=1]').attr('id', ''); // Set id to empty value in order to ignore them in tableDnDSerialize function
155 var roworder = cleanSerialize(decodeURI($("#<?php echo $tagidfortablednd; ?>").tableDnDSerialize()));
156 var table_element_line = "<?php echo $table_element_line; ?>";
157 var fk_element = "<?php echo $fk_element; ?>";
158 var element_id = "<?php echo $id; ?>";
159 var filepath = "<?php echo urlencode($filepath); ?>";
160 var token = "<?php echo currentToken(); ?>"; // We use old 'token' and not 'newtoken' for Ajax call because the ajax page has the NOTOKENRENEWAL constant set.
161 $.post("<?php echo DOL_URL_ROOT; ?>/core/ajax/row.php",
162 {
163 roworder: roworder,
164 table_element_line: table_element_line,
165 fk_element: fk_element,
166 element_id: element_id,
167 filepath: filepath,
168 token: token
169 },
170 function() {
171 console.log("tableDND end of ajax call");
172 console.log(roworder, table_element_line, fk_element, element_id, filepath, token);
173 if (reloadpage == 1) {
174 <?php
175 $redirectURL = empty($urltorefreshaftermove) ? ($_SERVER['PHP_SELF'].'?'.dol_escape_js($_SERVER['QUERY_STRING'])) : $urltorefreshaftermove;
176 // remove action parameter from URL
177 $redirectURL = preg_replace('/(&|\?)action=[^&#]*/', '', $redirectURL);
178 ?>
179 location.href = '<?php echo dol_escape_js($redirectURL); ?>';
180 }
181 });
182 },
183 onDragClass: "dragClass",
184 dragHandle: "td.tdlineupdown"
185 });
186 $(".tdlineupdown").hover( function() { $(this).addClass('showDragHandle'); },
187 function() { $(this).removeClass('showDragHandle'); }
188 );
189}
190
191function checkLinePosition(row, inital_table) {
192 const tbody = $("#<?php echo $tagidfortablednd; ?> .drag").map((_, el) => $(el)[0]).get();
193
194 for (var k = 0; k < tbody.length; k++) {
195 const currentRow = tbody[k];
196 if (currentRow.dataset.rang !== undefined) {
197 currentRow.dataset.rang = k+1;
198 }
199 }
200
201 var rowLevel = parseInt(row.dataset.level);
202 var cancelLineMove = false;
203 var found_title = rowLevel >= 0;
204 var ignore_level = 0;
205
206 for (var i = row.dataset.rang-2; i >= 0; i--) {
207
208 if (tbody[i].dataset.desc !== undefined) {
209 const currentRowLevel1 = parseInt(tbody[i].dataset.level, 10);
210 console.log(currentRowLevel1, -rowLevel, currentRowLevel1 < -rowLevel);
211 if (rowLevel > 0) {
212 // Title line placement managing
213 if (currentRowLevel1 <= ignore_level && ignore_level !== 0) {
214 console.log("here");
215 // continue
216 } else if (-currentRowLevel1 === rowLevel) {
217 break;
218 } else if (currentRowLevel1 > 0 && currentRowLevel1 < rowLevel) {
219 if (rowLevel - currentRowLevel1 > 1) {
220 $("#notification-message").text("<?php echo $langs->trans("PreviousTitleLevelTooHigh"); ?>");
221 cancelLineMove = true;
222 break;
223 }
224 break;
225 // console.log(tbody[i].dataset.desc, currentRowLevel1, rowLevel);
226 } else if (currentRowLevel1 === rowLevel) {
227 for (var j = row.dataset.rang; j < tbody.length; j++) {
228 if (tbody[j].dataset.desc !== undefined) {
229 const currentRowLevel2 = parseInt(tbody[j].dataset.level, 10);
230 if (tbody[i].dataset.desc === tbody[j].dataset.desc && currentRowLevel1 === -currentRowLevel2) {
231 $("#notification-message").text("<?php echo $langs->trans("TitleUnderSameLevelSTLine"); ?>");
232 cancelLineMove = true;
233 break;
234 }
235 }
236 }
237 } else if (currentRowLevel1 > rowLevel) {
238 for (var j = row.dataset.rang; j < tbody.length; j++) {
239 if (tbody[j].dataset.desc !== undefined) {
240 const currentRowLevel2 = parseInt(tbody[j].dataset.level, 10);
241 if (row.dataset.desc !== tbody[j].dataset.desc && currentRowLevel2 <= -rowLevel) {
242 $("#notification-message").text("<?php echo $langs->trans("TitleAfterStLineOfSameLevelTitle"); ?>");
243 cancelLineMove = true;
244 break;
245 }
246 }
247 }
248 break;
249 } else if (currentRowLevel1 < -rowLevel) {
250 ignore_level = currentRowLevel1;
251 } else {
252 $("#notification-message").text("<?php echo $langs->trans("TitleUnderSameLevelOrGreater"); ?>");
253 cancelLineMove = true;
254 break;
255 }
256 } else if (rowLevel < 0) {
257 // Subtotal line placement managing
258 if (currentRowLevel1 < 0 && rowLevel <= currentRowLevel1 || currentRowLevel1 >0 && -rowLevel >= currentRowLevel1) {
259 console.log(rowLevel, currentRowLevel1);
260 if (tbody[i].dataset.desc === row.dataset.desc) {
261 found_title = true;
262 break;
263 }else if (-rowLevel === currentRowLevel1) {
264 $("#notification-message").text("<?php echo $langs->trans("STLineUnderCorrespondingTitleDesc"); ?>");
265 cancelLineMove = true;
266 break;
267 } else if (-rowLevel > currentRowLevel1) {
268 $("#notification-message").text("<?php echo $langs->trans("STLineUnderCorrespondingTitle"); ?>");
269 cancelLineMove = true;
270 break;
271 }
272 }
273 }
274 }
275 }
276 if (!found_title) {
277 $("#notification-message").text("<?php echo $langs->trans("STLineUnderTitle"); ?>");
278 cancelLineMove = true;
279 }
280
281 if (cancelLineMove) {
282 const tbody_to_replace = $(row).parent()[0];
283 $(tbody_to_replace).empty();
284 $(tbody_to_replace).append(inital_table);
285 init();
286 openDialog();
287 }
288}
289
290$(document).ready(function(){
291 init()
292});
293
294</script>
295<?php } else { ?>
296<script>
297$(document).ready(function(){
298 $(".imgupforline").hide();
299 $(".imgdownforline").hide();
300 $(".lineupdown").removeAttr('href');
301});
302</script>
303<?php } ?>
304<!-- 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
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
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, $nodefault=0)
Return value of a param into GET or POST supervariable.
treeview li table
No Email.