dolibarr 20.0.2
boxes.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2003-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2022 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
5 * Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
6 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
27// Load Dolibarr environment
28require '../main.inc.php';
29include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
30require_once DOL_DOCUMENT_ROOT.'/core/class/infobox.class.php';
31include_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
32
33// Load translation files required by the page
34$langs->loadLangs(array('admin', 'boxes', 'accountancy'));
35
36if (!$user->admin) {
38}
39
40$rowid = GETPOSTINT('rowid');
41$action = GETPOST('action', 'aZ09');
42
43
44// Define possible position of boxes
45$arrayofhomepages = InfoBox::getListOfPagesForBoxes();
46$boxes = array();
47
48
49/*
50 * Actions
51 */
52
53if ($action == 'addconst') {
54 dolibarr_set_const($db, "MAIN_ACTIVATE_FILECACHE", GETPOST("MAIN_ACTIVATE_FILECACHE", 'alpha'), 'chaine', 0, '', $conf->entity);
55}
56
57if ($action == 'add') {
58 $error = 0;
59 $boxids = GETPOST('boxid', 'array');
60
61 $db->begin();
62 if (is_array($boxids)) {
63 foreach ($boxids as $boxid) {
64 if (is_numeric($boxid['pos']) && $boxid['pos'] >= 0) { // 0=Home, 1=...
65 $pos = $boxid['pos'];
66
67 // Initialize distinct fk_user with all already existing values of fk_user (user that use a personalized view of boxes for page "pos")
68 $distinctfkuser = array();
69 if (!$error) {
70 $sql = "SELECT fk_user";
71 $sql .= " FROM ".MAIN_DB_PREFIX."user_param";
72 $sql .= " WHERE param = 'MAIN_BOXES_".$db->escape($pos)."' AND value = '1'";
73 $sql .= " AND entity = ".$conf->entity;
74 dol_syslog("boxes.php search fk_user to activate box for", LOG_DEBUG);
75 $resql = $db->query($sql);
76 if ($resql) {
77 $num = $db->num_rows($resql);
78 $i = 0;
79 while ($i < $num) {
80 $obj = $db->fetch_object($resql);
81 $distinctfkuser[$obj->fk_user] = $obj->fk_user;
82 $i++;
83 }
84 } else {
85 setEventMessages($db->lasterror(), null, 'errors');
86 $error++;
87 }
88 }
89
90 $distinctfkuser['0'] = '0'; // Add entry for fk_user = 0. We must use string as key and val
91
92 foreach ($distinctfkuser as $fk_user) {
93 if (!$error && $fk_user != '') {
94 $arrayofexistingboxid = array();
95 $nbboxonleft = $nbboxonright = 0;
96 $sql = "SELECT box_id, box_order FROM ".MAIN_DB_PREFIX."boxes";
97 $sql .= " WHERE position = ".((int) $pos)." AND fk_user = ".((int) $fk_user)." AND entity = ".((int) $conf->entity);
98 dol_syslog("boxes.php activate box", LOG_DEBUG);
99 $resql = $db->query($sql);
100 if ($resql) {
101 while ($obj = $db->fetch_object($resql)) {
102 $boxorder = $obj->box_order;
103 if (preg_match('/A/', $boxorder)) {
104 $nbboxonleft++;
105 }
106 if (preg_match('/B/', $boxorder)) {
107 $nbboxonright++;
108 }
109 $arrayofexistingboxid[$obj->box_id] = 1;
110 }
111 } else {
112 dol_print_error($db);
113 }
114
115 if (empty($arrayofexistingboxid[$boxid['value']])) {
116 $sql = "INSERT INTO ".MAIN_DB_PREFIX."boxes (";
117 $sql .= "box_id, position, box_order, fk_user, entity";
118 $sql .= ") VALUES (";
119 $sql .= ((int) $boxid['value']).", ".((int) $pos).", '".(($nbboxonleft > $nbboxonright) ? 'B01' : 'A01')."', ".((int) $fk_user).", ".$conf->entity;
120 $sql .= ")";
121
122 dol_syslog("boxes.php activate box", LOG_DEBUG);
123 $resql = $db->query($sql);
124 if (!$resql) {
125 setEventMessages($db->lasterror(), null, 'errors');
126 $error++;
127 }
128 } else {
129 dol_syslog("boxes.php activate box - already exists in database", LOG_DEBUG);
130 }
131 }
132 }
133 }
134 }
135 }
136 if (!$error) {
137 $db->commit();
138 $action = '';
139 } else {
140 $db->rollback();
141 }
142}
143
144if ($action == 'delete') {
145 $sql = "SELECT box_id FROM ".MAIN_DB_PREFIX."boxes";
146 $sql .= " WHERE rowid=".((int) $rowid);
147
148 $resql = $db->query($sql);
149 $obj = $db->fetch_object($resql);
150 if (!empty($obj->box_id)) {
151 $db->begin();
152
153 $sql = "DELETE FROM ".MAIN_DB_PREFIX."boxes";
154 $sql .= " WHERE entity = ".$conf->entity;
155 $sql .= " AND box_id=".((int) $obj->box_id);
156
157 $resql = $db->query($sql);
158
159 $db->commit();
160 }
161}
162
163if ($action == 'switch') {
164 // We switch values of field box_order for the 2 lines of table boxes
165 $db->begin();
166
167 $objfrom = new ModeleBoxes($db);
168 $objfrom->fetch(GETPOSTINT("switchfrom"));
169
170 $objto = new ModeleBoxes($db);
171 $objto->fetch(GETPOSTINT('switchto'));
172
173 $resultupdatefrom = 0;
174 $resultupdateto = 0;
175 if (is_object($objfrom) && is_object($objto)) {
176 $newfirst = $objto->box_order;
177 $newsecond = $objfrom->box_order;
178 if ($newfirst == $newsecond) {
179 $newsecondchar = preg_replace('/[0-9]+/', '', $newsecond);
180 $newsecondnum = (int) preg_replace('/[a-zA-Z]+/', '', $newsecond);
181 $newsecond = sprintf("%s%02d", $newsecondchar ? $newsecondchar : 'A', $newsecondnum + 1);
182 }
183
184 $sql = "UPDATE ".MAIN_DB_PREFIX."boxes SET box_order='".$db->escape($newfirst)."' WHERE rowid=".((int) $objfrom->rowid);
185 dol_syslog($sql);
186 $resultupdatefrom = $db->query($sql);
187 if (!$resultupdatefrom) {
188 dol_print_error($db);
189 }
190
191 $sql = "UPDATE ".MAIN_DB_PREFIX."boxes SET box_order='".$db->escape($newsecond)."' WHERE rowid=".((int) $objto->rowid);
192 dol_syslog($sql);
193 $resultupdateto = $db->query($sql);
194 if (!$resultupdateto) {
195 dol_print_error($db);
196 }
197 }
198
199 if ($resultupdatefrom && $resultupdateto) {
200 $db->commit();
201 } else {
202 $db->rollback();
203 }
204}
205
206
207/*
208 * View
209 */
210
211$form = new Form($db);
212
213llxHeader('', $langs->trans("Boxes"), '', '', 0, 0, '', '', '', 'mod-admin page-boxes');
214
215print load_fiche_titre($langs->trans("Boxes"), '', 'title_setup');
216
217print '<span class="opacitymedium">'.$langs->trans("BoxesDesc")." ".$langs->trans("OnlyActiveElementsAreShown")."</span><br>\n";
218print '<br>';
219
220/*
221 * Search for the default active boxes for each possible position
222 * We store the active boxes by default in $boxes[position][id_boite]=1
223 */
224
225$actives = array();
226
227$sql = "SELECT b.rowid, b.box_id, b.position, b.box_order,";
228$sql .= " bd.rowid as boxid";
229$sql .= " FROM ".MAIN_DB_PREFIX."boxes as b, ".MAIN_DB_PREFIX."boxes_def as bd";
230$sql .= " WHERE b.box_id = bd.rowid";
231$sql .= " AND b.entity IN (0,".$conf->entity.")";
232$sql .= " AND b.fk_user=0";
233$sql .= " ORDER by b.position, b.box_order";
234//print $sql;
235
236dol_syslog("Search available boxes", LOG_DEBUG);
237$resql = $db->query($sql);
238if ($resql) {
239 $num = $db->num_rows($resql);
240
241 // Check record to know if we must recalculate sort order
242 $i = 0;
243 $decalage = 0;
244 while ($i < $num) {
245 $obj = $db->fetch_object($resql);
246 $boxes[$obj->position][$obj->box_id] = 1;
247 $i++;
248
249 array_push($actives, $obj->box_id);
250
251 if ($obj->box_order == '' || $obj->box_order == '0' || $decalage) {
252 $decalage++;
253 }
254 // We renumber the order of the boxes if one of them is in ''
255 // This occurs just after an insert.
256 if ($decalage) {
257 $sql = "UPDATE ".MAIN_DB_PREFIX."boxes SET box_order='".$db->escape($decalage)."' WHERE rowid=".((int) $obj->rowid);
258 $db->query($sql);
259 }
260 }
261
262 if ($decalage) {
263 // If we have renumbered, we correct the field box_order
264 // This occurs just after an insert.
265 $sql = "SELECT box_order";
266 $sql .= " FROM ".MAIN_DB_PREFIX."boxes";
267 $sql .= " WHERE entity = ".$conf->entity;
268 $sql .= " AND LENGTH(box_order) <= 2";
269
270 dol_syslog("Execute requests to renumber box order", LOG_DEBUG);
271 $result = $db->query($sql);
272 if ($result) {
273 while ($record = $db->fetch_array($result)) {
274 if (dol_strlen($record['box_order']) == 1) {
275 if (preg_match("/[13579]{1}/", substr($record['box_order'], -1))) {
276 $box_order = "A0".$record['box_order'];
277 $sql = "UPDATE ".MAIN_DB_PREFIX."boxes SET box_order = '".$db->escape($box_order)."' WHERE entity = ".$conf->entity." AND box_order = '".$db->escape($record['box_order'])."'";
278 $resql = $db->query($sql);
279 } elseif (preg_match("/[02468]{1}/", substr($record['box_order'], -1))) {
280 $box_order = "B0".$record['box_order'];
281 $sql = "UPDATE ".MAIN_DB_PREFIX."boxes SET box_order = '".$db->escape($box_order)."' WHERE entity = ".$conf->entity." AND box_order = '".$db->escape($record['box_order'])."'";
282 $resql = $db->query($sql);
283 }
284 } elseif (dol_strlen($record['box_order']) == 2) {
285 if (preg_match("/[13579]{1}/", substr($record['box_order'], -1))) {
286 $box_order = "A".$record['box_order'];
287 $sql = "UPDATE ".MAIN_DB_PREFIX."boxes SET box_order = '".$db->escape($box_order)."' WHERE entity = ".$conf->entity." AND box_order = '".$db->escape($record['box_order'])."'";
288 $resql = $db->query($sql);
289 } elseif (preg_match("/[02468]{1}/", substr($record['box_order'], -1))) {
290 $box_order = "B".$record['box_order'];
291 $sql = "UPDATE ".MAIN_DB_PREFIX."boxes SET box_order = '".$db->escape($box_order)."' WHERE entity = ".$conf->entity." AND box_order = '".$db->escape($record['box_order'])."'";
292 $resql = $db->query($sql);
293 }
294 }
295 }
296 }
297 }
298 $db->free($resql);
299}
300
301// Available boxes to activate
302$boxtoadd = InfoBox::listBoxes($db, 'available', -1, null, $actives);
303// Activated boxes
304$boxactivated = InfoBox::listBoxes($db, 'activated', -1, null);
305
306print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">'."\n";
307print '<input type="hidden" name="token" value="'.newToken().'">'."\n";
308print '<input type="hidden" name="action" value="add">'."\n";
309
310
311print '<br>';
312
313print '<div class="div-table-responsive-no-min">';
314print '<table class="tagtable liste centpercent">'."\n";
315
316print '<tr class="liste_titre">';
317print '<td>'.$langs->trans("Box").'</td>';
318print '<td>'.$langs->trans("Note").'/'.$langs->trans("Parameters").'</td>';
319print '<td></td>';
320print '<td class="center" width="160">'.$langs->trans("ActivatableOn").'</td>';
321print '<td class="center" width="60" colspan="2">'.$langs->trans("PositionByDefault").'</td>';
322print '<td class="center" width="80">'.$langs->trans("Disable").'</td>';
323print '</tr>'."\n";
324
325print "\n\n".'<!-- Boxes Available -->'."\n";
326foreach ($boxtoadd as $box) {
327 if (preg_match('/^([^@]+)@([^@]+)$/i', $box->boximg)) {
328 $logo = $box->boximg;
329 } else {
330 $logo = preg_replace("/^object_/i", "", $box->boximg);
331 }
332
333 print "\n".'<!-- Box '.$box->boxcode.' -->'."\n";
334 print '<tr class="oddeven" style="height:3em !important;">'."\n";
335 print '<td class="tdoverflowmax300" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv($box->boxlabel)).'">'.img_object("", $logo, 'class="pictofixedwidth" height="14px"').' '.$langs->transnoentitiesnoconv($box->boxlabel);
336 if (!empty($box->class) && preg_match('/graph_/', $box->class)) {
337 print img_picto('', 'graph', 'class="paddingleft"');
338 }
339 if (!empty($box->version)) {
340 if ($box->version == 'experimental') {
341 print ' <span class="opacitymedium">('.$langs->trans("Experimental").')</span>';
342 } elseif ($box->version == 'development') {
343 print ' <span class="opacitymedium">('.$langs->trans("Development").')</span>';
344 }
345 }
346 print '</td>'."\n";
347 print '<td class="tdoverflowmax300" title="'.dol_escape_htmltag($box->note).'">';
348 if ($box->note == '(WarningUsingThisBoxSlowDown)') {
349 $langs->load("errors");
350 print $langs->trans("WarningUsingThisBoxSlowDown");
351 } else {
352 print($box->note ? $box->note : '&nbsp;');
353 }
354 print '</td>'."\n";
355 print '<td>';
356 print $form->textwithpicto('', $langs->trans("SourceFile").' : '.$box->sourcefile);
357 print '</td>'."\n";
358
359 // For each possible position, an activation link is displayed if the box is not already active for that position
360 print '<td class="center">';
361 print $form->selectarray("boxid[".$box->box_id."][pos]", $arrayofhomepages, -1, 1, 0, 0, '', 1, 0, 0, '', 'minwidth75', 1)."\n";
362 print '<input type="hidden" name="boxid['.$box->box_id.'][value]" value="'.$box->box_id.'">'."\n";
363 print '</td>';
364
365 print '<td>';
366 print '</td>';
367
368 print '<td>';
369 print '</td>';
370
371 print '<td>';
372 print '<input type="submit" class="button small smallpaddingimp" value="'.$langs->trans("Activate").'">';
373 print '</td>';
374
375 print '</tr>'."\n";
376}
377print "\n".'<!-- End Boxes Available -->'."\n";
378
379
380$box_order = 1;
381$foundrupture = 1;
382foreach ($boxactivated as $key => $box) {
383 if (preg_match('/^([^@]+)@([^@]+)$/i', $box->boximg)) {
384 $logo = $box->boximg;
385 } else {
386 $logo = preg_replace("/^object_/i", "", $box->boximg);
387 }
388
389 print "\n".'<!-- Box '.$box->boxcode.' -->'."\n";
390 print '<tr class="oddeven" style="height:3em !important;">';
391 print '<td>'.img_object("", $logo, 'class="pictofixedwidth" height="14px"').' '.$langs->transnoentitiesnoconv($box->boxlabel);
392 if (!empty($box->class) && preg_match('/graph_/', $box->class)) {
393 print img_picto('', 'graph', 'class="paddingleft"');
394 }
395 if (!empty($box->version)) {
396 if ($box->version == 'experimental') {
397 print ' <span class="opacitymedium">('.$langs->trans("Experimental").')</span>';
398 } elseif ($box->version == 'development') {
399 print ' <span class="opacitymedium">('.$langs->trans("Development").')</span>';
400 }
401 }
402 print '</td>';
403 $langs->load("errors");
404 print '<td class="tdoverflowmax300" title="'.dol_escape_htmltag($box->note == '(WarningUsingThisBoxSlowDown)' ? $langs->trans("WarningUsingThisBoxSlowDown") : $box->note).'">';
405 if ($box->note == '(WarningUsingThisBoxSlowDown)') {
406 print img_warning('', 0).' '.$langs->trans("WarningUsingThisBoxSlowDown");
407 } else {
408 print($box->note ? $box->note : '&nbsp;');
409 }
410 print '</td>';
411 print '<td>';
412 print $form->textwithpicto('', $langs->trans("SourceFile").' : '.$box->sourcefile);
413 print '</td>'."\n";
414 print '<td class="center">'.(empty($arrayofhomepages[$box->position]) ? '' : $langs->trans($arrayofhomepages[$box->position])).'</td>';
415 $hasnext = ($key < (count($boxactivated) - 1));
416 $hasprevious = ($key != 0);
417 print '<td class="center">'.($key + 1).'</td>';
418 print '<td class="center nowraponall">';
419 print($hasnext ? '<a class="reposition" href="boxes.php?action=switch&token='.newToken().'&switchfrom='.$box->rowid.'&switchto='.$boxactivated[$key + 1]->rowid.'">'.img_down().'</a>&nbsp;' : '');
420 print($hasprevious ? '<a class="reposition" href="boxes.php?action=switch&token='.newToken().'&switchfrom='.$box->rowid.'&switchto='.$boxactivated[$key - 1]->rowid.'">'.img_up().'</a>' : '');
421 print '</td>';
422 print '<td class="center">';
423 print '<a class="reposition" href="boxes.php?rowid='.$box->rowid.'&action=delete&token='.newToken().'">'.img_delete().'</a>';
424 print '</td>';
425
426 print '</tr>'."\n";
427}
428
429print '</table>';
430print '</div>';
431print '<br>';
432
433print '</form>';
434
435
436// Other parameters
437
438print "\n\n".'<!-- Other Const -->'."\n";
439print load_fiche_titre($langs->trans("Other"), '', '');
440print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
441print '<input type="hidden" name="token" value="'.newToken().'">';
442print '<input type="hidden" name="action" value="addconst">';
443print '<input type="hidden" name="page_y" value="">';
444print '<div class="div-table-responsive-no-min">';
445print '<table class="noborder centpercent">';
446
447print '<tr class="liste_titre">';
448print '<td class="liste_titre">'.$langs->trans("Parameter").'</td>';
449print '<td class="liste_titre">'.$langs->trans("Value").'</td>';
450print '</tr>';
451
452// Activate FileCache (so content of file boxes are stored into a cache file int boxes/temp for 3600 seconds)
453print '<tr class="oddeven"><td>'.$langs->trans("EnableFileCache").'</td><td>';
454print $form->selectyesno('MAIN_ACTIVATE_FILECACHE', getDolGlobalInt('MAIN_ACTIVATE_FILECACHE', 0), 1);
455print '</td>';
456print '</tr>';
457
458print '</table>';
459print '</div>';
460
461print $form->buttonsSaveCancel("Save", '', array(), 0, 'reposition');
462
463print '</form>';
464print "\n".'<!-- End Other Const -->'."\n";
465
466// End of page
467llxFooter();
468$db->close();
dolibarr_set_const($db, $name, $value, $type='chaine', $visible=0, $note='', $entity=1)
Insert a parameter (key,value) into database (delete old key then insert it again).
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader()
Empty header.
Definition wrapper.php:55
llxFooter()
Empty footer.
Definition wrapper.php:69
Class to manage generation of HTML components Only common components must be here.
static listBoxes($dbs, $mode, $zone, $user=null, $excludelist=array(), $includehidden=1)
Return array of boxes qualified for area and user.
static getListOfPagesForBoxes()
Name of positions (See below)
Class ModeleBoxes.
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
img_warning($titlealt='default', $moreatt='', $morecss='pictowarning')
Show warning logo.
img_delete($titlealt='default', $other='class="pictodelete"', $morecss='')
Show delete logo.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
img_down($titlealt='default', $selected=0, $moreclass='')
Show down arrow logo.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
newToken()
Return the value of token currently saved into session with name 'newtoken'.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
img_up($titlealt='default', $selected=0, $moreclass='')
Show top arrow logo.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.