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