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