/
dashboard.php
518 lines (386 loc) · 13.9 KB
/
dashboard.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
<?php
if(!empty($_GET['uid'])) {
define('INC_FROM_CRON_SCRIPT',true);
}
require 'config.php';
dol_include_once('/query/class/query.class.php');
dol_include_once('/query/class/dashboard.class.php');
$langs->load('query@query');
$action = GETPOST('action','alpha');
// HOTFIX: l'action "add" nécessite un jeton CSRF (car dans les modules standard, c'est une action impactante)
// mais pour query, "add" correspond au "create" du standard.
if ($action === 'create') $action = 'add';
$dashboard=new TQDashBoard;
$PDOdb=new TPDOdb;
$newToken = function_exists('newToken') ? newToken() : $_SESSION['newtoken'];
$fk_user_to_use = GETPOST('fk_user','int');
if(empty($user->id) && !empty($fk_user_to_use)) {
$user->fetch($fk_user_to_use);
}
switch ($action) {
case 'delete':
$dashboard->load($PDOdb, GETPOST('id','int'));
$dashboard->delete($PDOdb);
setEventMessage($langs->trans('DeleteSuccess'));
header('Location:dashboard.php');
exit;
break;
case 'view':
$dashboard->load($PDOdb, GETPOST('id','int'));
fiche($dashboard);
break;
case 'add':
if(empty($user->rights->query->dashboard->create)) accessforbidden();
fiche($dashboard);
break;
case 'run':
if(GETPOST('uid','alpha')) {
if(GETPOST('storechoice','int')>0 && $user->id > 0) {
dol_include_once('/core/lib/admin.lib.php');
$res = dolibarr_set_const($db, 'QUERY_HOME_DEFAULT_DASHBOARD_USER_'.$user->id, GETPOST('uid','alpha'));
}
$dashboard->loadBy($PDOdb, GETPOST('uid','alpha'),'uid',true);
run($PDOdb, $dashboard, false);
}
else {
$dashboard->load($PDOdb, GETPOST('id','int'));
run($PDOdb, $dashboard);
}
break;
default:
liste();
break;
}
function run(&$PDOdb, &$dashboard, $withHeader = true) {
echo fiche($dashboard, 'view', $withHeader);
}
function liste()
{
global $langs, $conf,$user, $db, $newToken;
llxHeader('', 'Query DashBoard', '', '', 0, 0, array('/query/js/dashboard.js', '/query/js/jquery.gridster.min.js'), array('/query/css/jquery.gridster.min.css', '/query/css/dashboard.css'));
dol_fiche_head(array(), 0, '', -1);
$sql = '
SELECT qd.rowid as "Id", qd.title, "" as action
FROM ' . MAIN_DB_PREFIX . 'qdashboard qd
WHERE TRUE';
$sql.= TQDashBoard::getUserRightsSQLFilter($user);
$r=new Listview($db, 'lDash');
echo $r->render($sql,array(
'link'=>array(
'title'=>'<a href="?action=run&id=@Id@">'.img_picto('Run', 'object_cron.png').' @val@</a>'
,'action'=>'<a href="?action=view&id=@Id@">'.img_picto('Edit', 'edit.png').'</a> <a href="?action=delete&token='.$newToken.'&id=@Id@" onclick="return(confirm(\''.$langs->trans('ConfirmDeleteMessage').'\'));">'.img_picto('Delete', 'delete.png').'</a>'
)
,'title'=>array(
'title'=>$langs->trans('Title')
,'action'=>''
)
,'position' => array(
'text-align' => array(
'action' => 'right'
)
)
,'sortorder'=>''
,'sortfield'=>''
,'list' =>array('param_url'=>'','morehtmlrighttitle'=>'')
));
dol_fiche_end(-1);
llxFooter();
}
function fiche(&$dashboard, $action = 'edit', $withHeader=true) {
global $langs, $conf,$user, $db;
$PDOdb=new TPDOdb;
$form=new TFormCore;
$cell_height = 200;
$tab_object = GETPOST('tab_object','alpha');
$table_element = GETPOST('table_element','alpha');
$fk_object = GETPOST('fk_object','int');
if(empty($table_element)) {
if($tab_object == 'thirdparty') $table_element = 'societe';
else if($tab_object == 'project') $table_element = 'projet';
else $table_element = $tab_object;
}
if($withHeader)
{
llxHeader('', 'Query DashBoard', '', '', 0, 0, array('/query/js/dashboard.js', '/query/js/jquery.gridster.min.js', '/query/js/query-resize.js') , array('/query/css/jquery.gridster.min.css', '/query/css/dashboard.css'));
if($dashboard->getId() > 0)
{
$head = TQueryMenu::getHeadForObject($tab_object, $fk_object);
echo dol_get_fiche_head($head, 'tabQuery' . GETPOST('menuId','int'), 'Query');
}
$title = ! empty($dashboard->title) ? $langs->trans('DashboardTitle', $dashboard->title) : $langs->trans('NewDashboard');
$morehtmlright = '<a href="' . $_SERVER['PHP_SELF'] . '">' . $langs->trans('BackToList') . '</a>';
print load_fiche_titre($title, $morehtmlright, '');
if($dashboard->getId() <= 0)
{
echo dol_get_fiche_head('');
}
}
else if(GETPOST('for_incusion','int')>0) {
?>
<div class="querydashboard">
<link rel="stylesheet" type="text/css" title="default" href="<?php echo dol_buildpath('/query/css/jquery.gridster.min.css', 1); ?>" />
<link rel="stylesheet" type="text/css" title="default" href="<?php echo dol_buildpath('/query/css/dashboard.css', 1); ?>" />
<script type="text/javascript" src="<?php echo dol_buildpath('/query/js/dashboard.js', 1); ?>"></script>
<script type="text/javascript" src="<?php echo dol_buildpath('/query/js/jquery.gridster.min.js', 1); ?>"></script>
<script type="text/javascript" src="<?php echo dol_buildpath('/query/js/query-resize.js', 1); ?>"></script>
<?php
}
else {
?><!doctype html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="<?php echo dol_buildpath('/theme/eldy/style.css.php?lang=fr_FR&theme=eldy', 1); ?>" />
<link rel="stylesheet" type="text/css" title="default" href="<?php echo dol_buildpath('/query/css/jquery.gridster.min.css', 1); ?>" />
<link rel="stylesheet" type="text/css" title="default" href="<?php echo dol_buildpath('/query/css/dashboard.css', 1); ?>" />
<script type="text/javascript" src="<?php echo dol_buildpath('/includes/jquery/js/jquery.min.js', 1); ?>"></script>
<script type="text/javascript" src="<?php echo dol_buildpath('/query/js/dashboard.js', 1); ?>"></script>
<script type="text/javascript" src="<?php echo dol_buildpath('/query/js/jquery.gridster.min.js', 1); ?>"></script>
<script type="text/javascript" src="<?php echo dol_buildpath('/query/js/query-resize.js', 1); ?>"></script>
<style type="text/css">
.pagination { display : none; }
<?php if((int)GETPOST('allow_gen','int')!=1) echo '.notInGeneration { display : none; }'; ?>
table.liste tr.impair,table.liste tr.pair,table.liste tr.liste_titre,div.titre {
font-size: 12px;
}
</style>
</head>
<body>
<?php
}
?>
<script type="text/javascript">
function calculateGridsterWidth()
{
// 4 => nombre de colonnes, 10 => CSS margin de chaque cellule, 8 => nombre de marges ( = nombre de colonnes * 2)
let gridster_width = Math.floor(($('.gridster').innerWidth() - 8 * 10) / 4);
// On ne va pas en dessous d'une certaine largeur
if(gridster_width < 100) gridster_width = 100;
return gridster_width;
}
var MODQUERY_INTERFACE = "<?php echo dol_buildpath('/query/script/interface.php',1); ?>";
var cellHeight = <?= $cell_height ?>;
$(document).ready(function() //DOM Ready
{
// jQuery().gridster() retourne un objet jQuery, on chaîne un .data('gridster') pour récupérer l'instance de Gridster.js
gridster = $(".gridster ul").gridster({
widget_margins: [10, 10]
,widget_base_dimensions: [calculateGridsterWidth(), cellHeight]
,min_cols:4
,max_cols:4
,min_rows:1
,serialize_params: function($w, wgd) {
return { posx: wgd.col, posy: wgd.row, width: wgd.size_x, height: wgd.size_y, k : $w.attr('data-k') }
}
<?php
if($action == 'edit') {
?>,resize: {
enabled: true
,max_size: [4, 4]
,min_size: [1, 1]
,stop: function()
{
handleResizing();
}
}
<?php
}
?>
}).data('gridster');
<?php
if($action == 'view')
{
echo 'gridster.disable();';
}
?>
$('#addQuery').click(function() {
var fk_query = $('select[name=fk_query]').val();
$.ajax({
url: MODQUERY_INTERFACE
,data: {
put:'dashboard-query'
,fk_query : fk_query
,fk_qdashboard:<?php echo $dashboard->getId() ?>
}
,dataType:'json'
}).done(function(data) {
var title = $('select[name=fk_query] option:selected').text();
gridster.add_widget('<li data-k="'+data+'">'+title+'</li>',1,1,1,1);
});
return false;
});
$('#saveDashboard').click(function() {
var $button = $(this);
$button.hide();
$.ajax({
url: MODQUERY_INTERFACE
,data: {
put:'dashboard'
,id:<?php echo $dashboard->getId() ?>
,title:$('input[name=title]').val()
,fk_usergroup:$('select[name=fk_usergroup]').val()
,send_by_mail:$('select[name=send_by_mail]').val()
,hook:$('select[name=hook]').val()
,use_as_landing_page:$('select[name=use_as_landing_page]').val()
,refresh_dashboard:$('input[name=refresh_dashboard]').val()
}
}).done(function(data) {
<?php
if($dashboard->getId()> 0) {
?>$.ajax({
url: MODQUERY_INTERFACE
,data: {
put:'dashboard-query-link'
,TCoord:gridster.serialize( )
,fk_qdashboard:<?php echo $dashboard->getId() ?>
}
,dataType:'json'
}).done(function(data) {
$button.show();
});
<?php
}
else {
echo 'document.location.href="?action=view&id="+data;';
}
?>
});
});
$(window).on('resize', handleResizing);
handleResizing();
});
function delTile(idTile) {
$('li[tile-id='+idTile+']').css('opacity',.5);
$.ajax({
url: MODQUERY_INTERFACE
,data: {
put:'dashboard-query-remove'
,id : idTile
}
,dataType:'json'
}).done(function(data) {
$('li[tile-id='+idTile+']').toggle();
//document.location.hre
});
}
</script>
<?php
if($action == 'edit')
{
$formDoli = new Form($db);
?>
<table class="border centpercent">
<tr>
<td class="titlefieldcreate"><?= $langs->trans('Title') ?></td>
<td><?= $form->texte('', 'title', $dashboard->title, 50,255) ?></td>
</tr>
<tr>
<td class="titlefieldcreate"><?= $langs->trans('UseAsLandingPage') ?></td>
<td><?= $formDoli->selectarray('use_as_landing_page', array($langs->trans('No'),$langs->trans('Yes')),$dashboard->use_as_landing_page) ?></td>
</tr>
<tr>
<td class="titlefieldcreate"><?= $langs->trans('LimitAccessToThisGroup') ?></td>
<td><?= $formDoli->select_dolgroups($dashboard->fk_usergroup, 'fk_usergroup', 1) ?></td>
</tr>
<tr>
<td class="titlefieldcreate"><?= $langs->trans('SendByMailToThisGroup') ?></td>
<td><?= $form->combo('', 'send_by_mail', $dashboard->TSendByMail, $dashboard->send_by_mail) ?></td>
</tr>
<tr>
<td class="titlefieldcreate"><?= $langs->trans('ShowThisInCard') ?></td>
<td><?= $form->combo('','hook', $dashboard->THook, $dashboard->hook) ?></td>
</tr>
<tr>
<td class="titlefieldcreate"><?= $langs->trans('RefreshDashboard') ?></td>
<td><?= $form->number('','refresh_dashboard', $dashboard->refresh_dashboard, 20, 1, 0) ?></td>
</tr>
</table>
<div class="center"><a href="#" class="butAction" id="saveDashboard"><?php echo $langs->trans('SaveDashboard'); ?></a></div>
<?php
}
else {
if(true || ! empty($conf->global->QUERY_SHOW_PDF_TRANSFORM)) echo '<div style="text-align:right" class="notInGeneration"><a class="butAction" style=";z-index:999;" href="download-dashboard.php?uid='.$dashboard->uid.'">'.$langs->trans('Download').'</a></div>';
}
if($withHeader && $action == 'edit')
{
dol_fiche_end(-1);
if ($dashboard->getId() > 0)
{
$TQueries = TQuery::getQueries($PDOdb);
$morehtmlright = $form->combo('', 'fk_query', $TQueries, 0);
$morehtmlright .= ' <a href="#" class="butAction" id="addQuery">' . $langs->trans('AddThisQuery') . '</a>';
print load_fiche_titre($langs->trans('Queries'), $morehtmlright, '');
}
}
?>
<div class="gridster">
<ul>
<?php
foreach($dashboard->TQDashBoardQuery as $k=>&$cell) {
echo '<li tile-id="'.$cell->getId().'" data-k="'.$k.'" data-row="'.$cell->posy.'" data-col="'.$cell->posx.'" data-sizex="'.$cell->width.'" data-sizey="'.$cell->height.'">';
if($action == 'edit') {
echo '<a style="position:absolute; top:3px; right:3px; z-index:999;" href="javascript:delTile('.$cell->getId().')">'.img_delete('DeleteThisTile').'</a>';
}
else {
$actionToDo = 'run-in';
$target = '';
if(! empty($conf->global->QUERY_DASHBOARD_OPEN_IN_NEW_TAB))
{
$actionToDo = 'run';
$target = ' target="_blank"';
}
echo '<a style="position:absolute; top:3px; right:3px; z-index:999;" href="'.dol_buildpath('/query/query.php?action='.$actionToDo.'&id='.$cell->query->getId(),1).'"'.$target.'>'.img_picto($langs->trans('Run'),'object_cron.png').'</a>';
}
if($cell->query->type=='LIST')$cell->query->type='SIMPLELIST';
$trueHeight = $cell->height * $cell_height;
if($cell->height > 1)
{
$trueHeight += ($cell->height - 1) * 20;
}
if(!empty($cell->query)) {
if(!$withHeader) {
echo $cell->query->run(false, $trueHeight, $table_element, $fk_object, 0, false, true);
}
else{
echo $cell->query->run(false, $trueHeight, $table_element, $fk_object, -1, false, true);
}
}
echo '</li>';
}
?>
</ul>
</div>
<div style="clear:both"></div>
<?php
if(! $withHeader && $dashboard->refresh_dashboard > 0)
{
echo '
<script type="text/javascript">
// Automatically refresh
$(document).ready(function()
{
setTimeout("window.location.reload()", ' . (60000 * $dashboard->refresh_dashboard) . ');
});
</script>';
}
if($withHeader)
{
if($dashboard->getId() > 0)
{
print dol_buildpath('/query/dashboard.php?action=run&uid=' . $dashboard->uid, 2);
}
if($action != 'edit')
{
dol_fiche_end(-1);
}
llxFooter();
}
else if(GETPOST('for_incusion','int')>0) {
?></div><?php
}
else {
?>
</body></html>
<?php
}
}