dolibarr 18.0.6
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 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
26// Load Dolibarr environment
27require '../main.inc.php';
28include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
29require_once DOL_DOCUMENT_ROOT.'/core/class/infobox.class.php';
30include_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
31
32// Load translation files required by the page
33$langs->loadLangs(array('admin', 'boxes', 'accountancy'));
34
35if (!$user->admin) {
37}
38
39$rowid = GETPOST('rowid', 'int');
40$action = GETPOST('action', 'aZ09');
41
42
43// Define possible position of boxes
44$arrayofhomepages = InfoBox::getListOfPagesForBoxes();
45$boxes = array();
46
47
48/*
49 * Actions
50 */
51
52if ($action == 'addconst') {
53 dolibarr_set_const($db, "MAIN_BOXES_MAXLINES", GETPOST("MAIN_BOXES_MAXLINES", 'int'), '', 0, '', $conf->entity);
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(GETPOST("switchfrom", 'int'));
169
170 $objto = new ModeleBoxes($db);
171 $objto->fetch(GETPOST('switchto', 'int'));
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 = 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"));
214
215print load_fiche_titre($langs->trans("Boxes"), '', 'title_setup');
216
217print '<span class="opacitymedium">'.$langs->trans("BoxesDesc")." ".$langs->trans("OnlyActiveElementsAreShown")."</span><br>\n";
218
219/*
220 * Search for the default active boxes for each possible position
221 * We store the active boxes by default in $boxes[position][id_boite]=1
222 */
223
224$actives = array();
225
226$sql = "SELECT b.rowid, b.box_id, b.position, b.box_order,";
227$sql .= " bd.rowid as boxid";
228$sql .= " FROM ".MAIN_DB_PREFIX."boxes as b, ".MAIN_DB_PREFIX."boxes_def as bd";
229$sql .= " WHERE b.box_id = bd.rowid";
230$sql .= " AND b.entity IN (0,".$conf->entity.")";
231$sql .= " AND b.fk_user=0";
232$sql .= " ORDER by b.position, b.box_order";
233//print $sql;
234
235dol_syslog("Search available boxes", LOG_DEBUG);
236$resql = $db->query($sql);
237if ($resql) {
238 $num = $db->num_rows($resql);
239
240 // Check record to know if we must recalculate sort order
241 $i = 0;
242 $decalage = 0;
243 while ($i < $num) {
244 $obj = $db->fetch_object($resql);
245 $boxes[$obj->position][$obj->box_id] = 1;
246 $i++;
247
248 array_push($actives, $obj->box_id);
249
250 if ($obj->box_order == '' || $obj->box_order == '0' || $decalage) {
251 $decalage++;
252 }
253 // We renumber the order of the boxes if one of them is in ''
254 // This occurs just after an insert.
255 if ($decalage) {
256 $sql = "UPDATE ".MAIN_DB_PREFIX."boxes SET box_order='".$db->escape($decalage)."' WHERE rowid=".((int) $obj->rowid);
257 $db->query($sql);
258 }
259 }
260
261 if ($decalage) {
262 // If we have renumbered, we correct the field box_order
263 // This occurs just after an insert.
264 $sql = "SELECT box_order";
265 $sql .= " FROM ".MAIN_DB_PREFIX."boxes";
266 $sql .= " WHERE entity = ".$conf->entity;
267 $sql .= " AND LENGTH(box_order) <= 2";
268
269 dol_syslog("Execute requests to renumber box order", LOG_DEBUG);
270 $result = $db->query($sql);
271 if ($result) {
272 while ($record = $db->fetch_array($result)) {
273 if (dol_strlen($record['box_order']) == 1) {
274 if (preg_match("/[13579]{1}/", substr($record['box_order'], -1))) {
275 $box_order = "A0".$record['box_order'];
276 $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'])."'";
277 $resql = $db->query($sql);
278 } elseif (preg_match("/[02468]{1}/", substr($record['box_order'], -1))) {
279 $box_order = "B0".$record['box_order'];
280 $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'])."'";
281 $resql = $db->query($sql);
282 }
283 } elseif (dol_strlen($record['box_order']) == 2) {
284 if (preg_match("/[13579]{1}/", substr($record['box_order'], -1))) {
285 $box_order = "A".$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 = "B".$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 }
294 }
295 }
296 }
297 $db->free($resql);
298}
299
300// Available boxes to activate
301$boxtoadd = InfoBox::listBoxes($db, 'available', -1, null, $actives);
302// Activated boxes
303$boxactivated = InfoBox::listBoxes($db, 'activated', -1, null);
304
305print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">'."\n";
306print '<input type="hidden" name="token" value="'.newToken().'">'."\n";
307print '<input type="hidden" name="action" value="add">'."\n";
308
309
310print '<br>';
311
312print '<div class="div-table-responsive-no-min">';
313print '<table class="tagtable liste centpercent">'."\n";
314
315print '<tr class="liste_titre">';
316print '<td>'.$langs->trans("Box").'</td>';
317print '<td>'.$langs->trans("Note").'/'.$langs->trans("Parameters").'</td>';
318print '<td></td>';
319print '<td class="center" width="160">'.$langs->trans("ActivatableOn").'</td>';
320print '<td class="center" width="60" colspan="2">'.$langs->trans("PositionByDefault").'</td>';
321print '<td class="center" width="80">'.$langs->trans("Disable").'</td>';
322print '</tr>'."\n";
323
324print "\n\n".'<!-- Boxes Available -->'."\n";
325foreach ($boxtoadd as $box) {
326 if (preg_match('/^([^@]+)@([^@]+)$/i', $box->boximg)) {
327 $logo = $box->boximg;
328 } else {
329 $logo = preg_replace("/^object_/i", "", $box->boximg);
330 }
331
332 print "\n".'<!-- Box '.$box->boxcode.' -->'."\n";
333 print '<tr class="oddeven" style="height:3em !important;">'."\n";
334 print '<td class="tdoverflowmax300" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv($box->boxlabel)).'">'.img_object("", $logo, 'class="pictofixedwidth" height="14px"').' '.$langs->transnoentitiesnoconv($box->boxlabel);
335 if (!empty($box->class) && preg_match('/graph_/', $box->class)) {
336 print img_picto('', 'graph', 'class="paddingleft"');
337 }
338 if (!empty($box->version)) {
339 if ($box->version == 'experimental') {
340 print ' <span class="opacitymedium">('.$langs->trans("Experimental").')</span>';
341 } elseif ($box->version == 'development') {
342 print ' <span class="opacitymedium">('.$langs->trans("Development").')</span>';
343 }
344 }
345 print '</td>'."\n";
346 print '<td class="tdoverflowmax300" title="'.dol_escape_htmltag($box->note).'">';
347 if ($box->note == '(WarningUsingThisBoxSlowDown)') {
348 $langs->load("errors");
349 print $langs->trans("WarningUsingThisBoxSlowDown");
350 } else {
351 print ($box->note ? $box->note : '&nbsp;');
352 }
353 print '</td>'."\n";
354 print '<td>';
355 print $form->textwithpicto('', $langs->trans("SourceFile").' : '.$box->sourcefile);
356 print '</td>'."\n";
357
358 // For each possible position, an activation link is displayed if the box is not already active for that position
359 print '<td class="center">';
360 print $form->selectarray("boxid[".$box->box_id."][pos]", $arrayofhomepages, -1, 1, 0, 0, '', 1, 0, 0, '', 'minwidth75', 1)."\n";
361 print '<input type="hidden" name="boxid['.$box->box_id.'][value]" value="'.$box->box_id.'">'."\n";
362 print '</td>';
363
364 print '<td>';
365 print '</td>';
366
367 print '<td>';
368 print '</td>';
369
370 print '<td>';
371 print '<input type="submit" class="button small smallpaddingimp" value="'.$langs->trans("Activate").'">';
372 print '</td>';
373
374 print '</tr>'."\n";
375}
376print "\n".'<!-- End Boxes Available -->'."\n";
377
378
379$box_order = 1;
380$foundrupture = 1;
381foreach ($boxactivated as $key => $box) {
382 if (preg_match('/^([^@]+)@([^@]+)$/i', $box->boximg)) {
383 $logo = $box->boximg;
384 } else {
385 $logo = preg_replace("/^object_/i", "", $box->boximg);
386 }
387
388 print "\n".'<!-- Box '.$box->boxcode.' -->'."\n";
389 print '<tr class="oddeven" style="height:3em !important;">';
390 print '<td>'.img_object("", $logo, 'class="pictofixedwidth" height="14px"').' '.$langs->transnoentitiesnoconv($box->boxlabel);
391 if (!empty($box->class) && preg_match('/graph_/', $box->class)) {
392 print img_picto('', 'graph', 'class="paddingleft"');
393 }
394 if (!empty($box->version)) {
395 if ($box->version == 'experimental') {
396 print ' <span class="opacitymedium">('.$langs->trans("Experimental").')</span>';
397 } elseif ($box->version == 'development') {
398 print ' <span class="opacitymedium">('.$langs->trans("Development").')</span>';
399 }
400 }
401 print '</td>';
402 $langs->load("errors");
403 print '<td class="tdoverflowmax300" title="'.dol_escape_htmltag($box->note == '(WarningUsingThisBoxSlowDown)' ? $langs->trans("WarningUsingThisBoxSlowDown") : $box->note).'">';
404 if ($box->note == '(WarningUsingThisBoxSlowDown)') {
405 print img_warning('', 0).' '.$langs->trans("WarningUsingThisBoxSlowDown");
406 } else {
407 print ($box->note ? $box->note : '&nbsp;');
408 }
409 print '</td>';
410 print '<td>';
411 print $form->textwithpicto('', $langs->trans("SourceFile").' : '.$box->sourcefile);
412 print '</td>'."\n";
413 print '<td class="center">'.(empty($arrayofhomepages[$box->position]) ? '' : $langs->trans($arrayofhomepages[$box->position])).'</td>';
414 $hasnext = ($key < (count($boxactivated) - 1));
415 $hasprevious = ($key != 0);
416 print '<td class="center">'.($key + 1).'</td>';
417 print '<td class="center nowraponall">';
418 print ($hasnext ? '<a class="reposition" href="boxes.php?action=switch&token='.newToken().'&switchfrom='.$box->rowid.'&switchto='.$boxactivated[$key + 1]->rowid.'">'.img_down().'</a>&nbsp;' : '');
419 print ($hasprevious ? '<a class="reposition" href="boxes.php?action=switch&token='.newToken().'&switchfrom='.$box->rowid.'&switchto='.$boxactivated[$key - 1]->rowid.'">'.img_up().'</a>' : '');
420 print '</td>';
421 print '<td class="center">';
422 print '<a class="reposition" href="boxes.php?rowid='.$box->rowid.'&action=delete&token='.newToken().'">'.img_delete().'</a>';
423 print '</td>';
424
425 print '</tr>'."\n";
426}
427
428print '</table>';
429print '</div>';
430print '<br>';
431
432print '</form>';
433
434
435// Other parameters
436
437print "\n\n".'<!-- Other Const -->'."\n";
438print load_fiche_titre($langs->trans("Other"), '', '');
439print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
440print '<input type="hidden" name="token" value="'.newToken().'">';
441print '<input type="hidden" name="action" value="addconst">';
442print '<input type="hidden" name="page_y" value="">';
443print '<div class="div-table-responsive-no-min">';
444print '<table class="noborder centpercent">';
445
446print '<tr class="liste_titre">';
447print '<td class="liste_titre">'.$langs->trans("Parameter").'</td>';
448print '<td class="liste_titre">'.$langs->trans("Value").'</td>';
449print '</tr>';
450
451print '<tr class="oddeven">';
452print '<td>';
453print $langs->trans("MaxNbOfLinesForBoxes");
454print '</td>'."\n";
455print '<td>';
456print '<input type="text" class="flat" size="6" name="MAIN_BOXES_MAXLINES" value="'.(getDolGlobalString('MAIN_BOXES_MAXLINES')).'">';
457print '</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>'.$langs->trans("EnableFileCache").'</td><td>';
462print $form->selectyesno('MAIN_ACTIVATE_FILECACHE', getDolGlobalInt('MAIN_ACTIVATE_FILECACHE', 0), 1);
463print '</td>';
464print '</tr>';
465
466print '</table>';
467print '</div>';
468
469print $form->buttonsSaveCancel("Save", '', array(), 0, 'reposition');
470
471print '</form>';
472print "\n".'<!-- End Other Const -->'."\n";
473
474// End of page
475llxFooter();
476$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:56
llxFooter()
Empty footer.
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.
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
img_warning($titlealt='default', $moreatt='', $morecss='pictowarning')
Show warning logo.
img_delete($titlealt='default', $other='class="pictodelete"', $morecss='')
Show delete logo.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
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 dolibarr global constant int value.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
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.
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.