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