1<?php
2/* Copyright (C) 2010-2012 Regis Houssin       <regis.houssin@inodbox.com>
3 * Copyright (C) 2010-2016 Laurent Destailleur <eldy@users.sourceforge.net>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 *
18 * Javascript code to activate drag and drop on lines
19 * You can use this if you want to be able to drag and drop rows of a table.
20 * You must add id="tablelines" on table level tag
21 * and $object and $object->id is defined
22 * and $object->fk_element or $fk_element is defined
23 * and have ($nboflines or count($object->lines) or count($taskarray) > 0)
24 * and have $table_element_line = 'tablename' or $object->table_element_line with line to move
25 *
26 */
27
28// Protection to avoid direct call of template
29if (empty($object) || !is_object($object)) {
30	print "Error, template page ".basename(__FILE__)." can't be called with no object defined.";
31	exit;
32}
33
34?>
35
36<!-- BEGIN PHP TEMPLATE AJAXROW.TPL.PHP - Script to enable drag and drop on lines of a table -->
37<?php
38$id = $object->id;
39$fk_element = empty($object->fk_element) ? $fk_element : $object->fk_element;
40$table_element_line = (empty($table_element_line) ? $object->table_element_line : $table_element_line);
41$nboflines = (isset($object->lines) ?count($object->lines) : (isset($tasksarray) ?count($tasksarray) : (empty($nboflines) ? 0 : $nboflines)));
42$forcereloadpage = empty($conf->global->MAIN_FORCE_RELOAD_PAGE) ? 0 : 1;
43$tagidfortablednd = (empty($tagidfortablednd) ? 'tablelines' : $tagidfortablednd);
44$filepath = (empty($filepath) ? '' : $filepath);
45
46
47if (GETPOST('action', 'aZ09') != 'editline' && $nboflines > 1 && $conf->browser->layout != 'phone') { ?>
48<script>
49$(document).ready(function(){
50	$(".imgupforline").hide();
51	$(".imgdownforline").hide();
52	$(".lineupdown").removeAttr('href');
53	$(".tdlineupdown").css("background-image",'url(<?php echo DOL_URL_ROOT.'/theme/'.$conf->theme.'/img/grip.png'; ?>)');
54	$(".tdlineupdown").css("background-repeat","no-repeat");
55	$(".tdlineupdown").css("background-position","center center");
56
57	console.log("Prepare tableDnd for #<?php echo $tagidfortablednd; ?>");
58	$("#<?php echo $tagidfortablednd; ?>").tableDnD({
59		onDrop: function(table, row) {
60			var reloadpage = "<?php echo $forcereloadpage; ?>";
61			console.log("tableDND onDrop");
62			console.log(decodeURI($("#<?php echo $tagidfortablednd; ?>").tableDnDSerialize()));
63			$('#<?php echo $tagidfortablednd; ?> tr[data-element=extrafield]').attr('id', '');	// Set extrafields id to empty value in order to ignore them in tableDnDSerialize function
64			var roworder = cleanSerialize(decodeURI($("#<?php echo $tagidfortablednd; ?>").tableDnDSerialize()));
65			var table_element_line = "<?php echo $table_element_line; ?>";
66			var fk_element = "<?php echo $fk_element; ?>";
67			var element_id = "<?php echo $id; ?>";
68			var filepath = "<?php echo urlencode($filepath); ?>";
69			var token = "<?php echo $_SESSION["token"]; ?>";	// We use old 'token' and not 'newtoken' for Ajax call because the ajax page has the NOTOKENRENEWAL constant set.
70			$.post("<?php echo DOL_URL_ROOT; ?>/core/ajax/row.php",
71					{
72						roworder: roworder,
73						table_element_line: table_element_line,
74						fk_element: fk_element,
75						element_id: element_id,
76						filepath: filepath,
77						token: token
78					},
79					function() {
80						console.log("tableDND end of ajax call");
81						if (reloadpage == 1) {
82							<?php
83							$redirectURL = empty($urltorefreshaftermove) ? ($_SERVER['PHP_SELF'].'?'.dol_escape_js($_SERVER['QUERY_STRING'])) : $urltorefreshaftermove;
84							// remove action parameter from URL
85							$redirectURL = preg_replace('/(&|\?)action=[^&#]*/', '', $redirectURL);
86							?>
87							location.href = '<?php echo dol_escape_js($redirectURL); ?>';
88						} else {
89							$("#<?php echo $tagidfortablednd; ?> .drag").each(
90									function( intIndex ) {
91										// $(this).removeClass("pair impair");
92										//if (intIndex % 2 == 0) $(this).addClass('impair');
93										//if (intIndex % 2 == 1) $(this).addClass('pair');
94									});
95						}
96					});
97		},
98		onDragClass: "dragClass",
99		dragHandle: "td.tdlineupdown"
100	});
101	$(".tdlineupdown").hover( function() { $(this).addClass('showDragHandle'); },
102		function() { $(this).removeClass('showDragHandle'); }
103	);
104});
105</script>
106<?php } else { ?>
107<script>
108$(document).ready(function(){
109	$(".imgupforline").hide();
110	$(".imgdownforline").hide();
111	$(".lineupdown").removeAttr('href');
112});
113</script>
114<?php } ?>
115<!-- END PHP TEMPLATE AJAXROW.TPL.PHP -->
116