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