dolibarr  7.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-2013 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@capnetworks.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 <http://www.gnu.org/licenses/>.
19  */
20 
26 require '../main.inc.php';
27 include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
28 require_once DOL_DOCUMENT_ROOT.'/core/class/infobox.class.php';
29 include_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
30 
31 $langs->load("admin");
32 $langs->load("boxes");
33 
34 if (! $user->admin) accessforbidden();
35 
36 $rowid = GETPOST('rowid','int');
37 $action = GETPOST('action','alpha');
38 
39 
40 // Define possible position of boxes
42 $boxes = array();
43 
44 
45 /*
46  * Actions
47  */
48 
49 if ($action == 'addconst')
50 {
51  dolibarr_set_const($db, "MAIN_BOXES_MAXLINES", $_POST["MAIN_BOXES_MAXLINES"],'',0,'',$conf->entity);
52  dolibarr_set_const($db, "MAIN_ACTIVATE_FILECACHE", $_POST["MAIN_ACTIVATE_FILECACHE"],'chaine',0,'',$conf->entity);
53 }
54 
55 if ($action == 'add') {
56  $error=0;
57  $db->begin();
58  if (isset($_POST['boxid']) && is_array($_POST['boxid']))
59  {
60  foreach($_POST['boxid'] as $boxid)
61  {
62  if (is_numeric($boxid['pos']) && $boxid['pos'] >= 0) // 0=Home, 1=...
63  {
64  $pos = $boxid['pos'];
65 
66  // Initialize distinct fkuser with all already existing values of fk_user (user that use a personalized view of boxes for page "pos")
67  $distinctfkuser=array();
68  if (! $error)
69  {
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  {
78  $num = $db->num_rows($resql);
79  $i=0;
80  while ($i < $num)
81  {
82  $obj=$db->fetch_object($resql);
83  $distinctfkuser[$obj->fk_user]=$obj->fk_user;
84  $i++;
85  }
86  }
87  else
88  {
89  setEventMessages($db->lasterror(), null, 'errors');
90  $error++;
91  }
92  }
93 
94  $distinctfkuser['0']='0'; // Add entry for fk_user = 0. We must use string as key and val
95 
96  foreach($distinctfkuser as $fk_user)
97  {
98  if (! $error && $fk_user != '')
99  {
100  $nbboxonleft=$nbboxonright=0;
101  $sql = "SELECT box_order FROM ".MAIN_DB_PREFIX."boxes WHERE position = ".$pos." AND fk_user = ".$fk_user." AND entity = ".$conf->entity;
102  dol_syslog("boxes.php activate box", LOG_DEBUG);
103  $resql = $db->query($sql);
104  if ($resql)
105  {
106  while($obj = $db->fetch_object($resql))
107  {
108  $boxorder=$obj->box_order;
109  if (preg_match('/A/',$boxorder)) $nbboxonleft++;
110  if (preg_match('/B/',$boxorder)) $nbboxonright++;
111  }
112  }
113  else dol_print_error($db);
114 
115  $sql = "INSERT INTO ".MAIN_DB_PREFIX."boxes (";
116  $sql.= "box_id, position, box_order, fk_user, entity";
117  $sql.= ") values (";
118  $sql.= $boxid['value'].", ".$pos.", '".(($nbboxonleft > $nbboxonright) ? 'B01' : 'A01')."', ".$fk_user.", ".$conf->entity;
119  $sql.= ")";
120 
121  dol_syslog("boxes.php activate box", LOG_DEBUG);
122  $resql = $db->query($sql);
123  if (! $resql)
124  {
125  setEventMessages($db->lasterror(), null, 'errors');
126  $error++;
127  }
128  }
129  }
130  }
131  }
132  }
133  if (! $error)
134  {
135  $db->commit();
136  $action='';
137  }
138  else
139  {
140  $db->rollback();
141  }
142 }
143 
144 if ($action == 'delete')
145 {
146  $sql = "SELECT box_id FROM ".MAIN_DB_PREFIX."boxes";
147  $sql.= " WHERE rowid=".$rowid;
148 
149  $resql = $db->query($sql);
150  $obj=$db->fetch_object($resql);
151  if (! empty($obj->box_id))
152  {
153  $db->begin();
154 
155  // Remove all personalized setup when a box is activated or disabled (why removing all ? We removed only removed boxes)
156  // $sql = "DELETE FROM ".MAIN_DB_PREFIX."user_param";
157  // $sql.= " WHERE param LIKE 'MAIN_BOXES_%'";
158  // $resql = $db->query($sql);
159 
160  $sql = "DELETE FROM ".MAIN_DB_PREFIX."boxes";
161  $sql.= " WHERE entity = ".$conf->entity;
162  $sql.= " AND box_id=".$obj->box_id;
163 
164  $resql = $db->query($sql);
165 
166  $db->commit();
167  }
168 }
169 
170 if ($action == 'switch')
171 {
172  // On permute les valeur du champ box_order des 2 lignes de la table boxes
173  $db->begin();
174 
175  $objfrom=new ModeleBoxes($db);
176  $objfrom->fetch($_GET["switchfrom"]);
177 
178  $objto=new ModeleBoxes($db);
179  $objto->fetch($_GET["switchto"]);
180 
181  $resultupdatefrom=0;
182  $resultupdateto=0;
183  if (is_object($objfrom) && is_object($objto))
184  {
185  $newfirst=$objto->box_order;
186  $newsecond=$objfrom->box_order;
187  if ($newfirst == $newsecond)
188  {
189  $newsecondchar=preg_replace('/[0-9]+/','',$newsecond);
190  $newsecondnum=preg_replace('/[a-zA-Z]+/','',$newsecond);
191  $newsecond=sprintf("%s%02d",$newsecondchar?$newsecondchar:'A',$newsecondnum+1);
192  }
193  $sql="UPDATE ".MAIN_DB_PREFIX."boxes SET box_order='".$newfirst."' WHERE rowid=".$objfrom->rowid;
194  dol_syslog($sql);
195  $resultupdatefrom = $db->query($sql);
196  if (! $resultupdatefrom) { dol_print_error($db); }
197 
198  $sql="UPDATE ".MAIN_DB_PREFIX."boxes SET box_order='".$newsecond."' WHERE rowid=".$objto->rowid;
199  dol_syslog($sql);
200  $resultupdateto = $db->query($sql);
201  if (! $resultupdateto) { dol_print_error($db); }
202  }
203 
204  if ($resultupdatefrom && $resultupdateto)
205  {
206  $db->commit();
207  }
208  else
209  {
210  $db->rollback();
211  }
212 }
213 
214 
215 /*
216  * View
217  */
218 
219 $form=new Form($db);
220 
221 llxHeader('',$langs->trans("Boxes"));
222 
223 print load_fiche_titre($langs->trans("Boxes"),'','title_setup');
224 
225 print $langs->trans("BoxesDesc")." ".$langs->trans("OnlyActiveElementsAreShown")."<br>\n";
226 
227 /*
228  * Recherche des boites actives par defaut pour chaque position possible
229  * On stocke les boites actives par defaut dans $boxes[position][id_boite]=1
230  */
231 
232 $actives = array();
233 
234 $sql = "SELECT b.rowid, b.box_id, b.position, b.box_order,";
235 $sql.= " bd.rowid as boxid";
236 $sql.= " FROM ".MAIN_DB_PREFIX."boxes as b, ".MAIN_DB_PREFIX."boxes_def as bd";
237 $sql.= " WHERE b.box_id = bd.rowid";
238 $sql.= " AND b.entity IN (0,".$conf->entity.")";
239 $sql.= " AND b.fk_user=0";
240 $sql.= " ORDER by b.position, b.box_order";
241 
242 dol_syslog("Search available boxes", LOG_DEBUG);
243 $resql = $db->query($sql);
244 if ($resql)
245 {
246  $num = $db->num_rows($resql);
247 
248  // Check record to know if we must recalculate sort order
249  $i = 0;
250  $decalage=0;
251  while ($i < $num)
252  {
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) $decalage++;
260  // On renumerote l'ordre des boites si l'une d'elle est a ''
261  // This occurs just after an insert.
262  if ($decalage)
263  {
264  $sql="UPDATE ".MAIN_DB_PREFIX."boxes SET box_order='".$decalage."' WHERE rowid=".$obj->rowid;
265  $db->query($sql);
266  }
267  }
268 
269  if ($decalage)
270  {
271  // Si on a renumerote, on corrige champ 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  {
282  while ($record = $db->fetch_array($result))
283  {
284  if (dol_strlen($record['box_order']) == 1)
285  {
286  if (preg_match("/[13579]{1}/",substr($record['box_order'],-1)))
287  {
288  $box_order = "A0".$record['box_order'];
289  $sql="UPDATE ".MAIN_DB_PREFIX."boxes SET box_order = '".$box_order."' WHERE entity = ".$conf->entity." AND box_order = '".$record['box_order']."'";
290  $resql = $db->query($sql);
291  }
292  else if (preg_match("/[02468]{1}/",substr($record['box_order'],-1)))
293  {
294  $box_order = "B0".$record['box_order'];
295  $sql="UPDATE ".MAIN_DB_PREFIX."boxes SET box_order = '".$box_order."' WHERE entity = ".$conf->entity." AND box_order = '".$record['box_order']."'";
296  $resql = $db->query($sql);
297  }
298  }
299  else if (dol_strlen($record['box_order']) == 2)
300  {
301  if (preg_match("/[13579]{1}/",substr($record['box_order'],-1)))
302  {
303  $box_order = "A".$record['box_order'];
304  $sql="UPDATE ".MAIN_DB_PREFIX."boxes SET box_order = '".$box_order."' WHERE entity = ".$conf->entity." AND box_order = '".$record['box_order']."'";
305  $resql = $db->query($sql);
306  }
307  else if (preg_match("/[02468]{1}/",substr($record['box_order'],-1)))
308  {
309  $box_order = "B".$record['box_order'];
310  $sql="UPDATE ".MAIN_DB_PREFIX."boxes SET box_order = '".$box_order."' WHERE entity = ".$conf->entity." AND box_order = '".$record['box_order']."'";
311  $resql = $db->query($sql);
312  }
313  }
314  }
315  }
316  }
317  $db->free($resql);
318 }
319 
320 // Available boxes to activate
321 $boxtoadd=InfoBox::listBoxes($db,'available',-1,null,$actives);
322 // Activated boxes
323 $boxactivated=InfoBox::listBoxes($db,'activated',-1,null);
324 
325 print "<br>\n";
326 print "\n\n".'<!-- Boxes Available -->'."\n";
327 print load_fiche_titre($langs->trans("BoxesAvailable"));
328 
329 print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">'."\n";
330 print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">'."\n";
331 print '<input type="hidden" name="action" value="add">'."\n";
332 
333 print '<div class="div-table-responsive-no-min">';
334 print '<table class="tagtable liste centpercent">'."\n";
335 
336 print '<tr class="liste_titre">';
337 print '<td width="300">'.$langs->trans("Box").'</td>';
338 print '<td>'.$langs->trans("Note").'/'.$langs->trans("Parameters").'</td>';
339 print '<td>'.$langs->trans("SourceFile").'</td>';
340 print '<td width="160" align="center">'.$langs->trans("ActivateOn").'</td>';
341 print "</tr>\n";
342 
343 foreach($boxtoadd as $box)
344 {
345  if (preg_match('/^([^@]+)@([^@]+)$/i',$box->boximg))
346  {
347  $logo = $box->boximg;
348  }
349  else
350  {
351  $logo=preg_replace("/^object_/i","",$box->boximg);
352  }
353 
354  print "\n".'<!-- Box '.$box->boxcode.' -->'."\n";
355  print '<tr class="oddeven">'."\n";
356  print '<td>'.img_object("",$logo).' '.$langs->transnoentitiesnoconv($box->boxlabel);
357  if (! empty($box->class) && preg_match('/graph_/',$box->class)) print ' ('.$langs->trans("Graph").')';
358  print '</td>'."\n";
359  print '<td>';
360  if ($box->note == '(WarningUsingThisBoxSlowDown)')
361  {
362  $langs->load("errors");
363  print $langs->trans("WarningUsingThisBoxSlowDown");
364  }
365  else print ($box->note?$box->note:'&nbsp;');
366  print '</td>'."\n";
367  print '<td>' . $box->sourcefile . '</td>'."\n";
368 
369  // Pour chaque position possible, on affiche un lien d'activation si boite non deja active pour cette position
370  print '<td class="center">';
371  print $form->selectarray("boxid[".$box->box_id."][pos]", $pos_name, 0, 1, 0, 0, '', 1)."\n";
372  print '<input type="hidden" name="boxid['.$box->box_id.'][value]" value="'.$box->box_id.'">'."\n";
373  print '</td>';
374 
375  print '</tr>'."\n";
376 }
377 if (! count($boxtoadd) && count($boxactivated))
378 {
379  print '<tr><td class="opacitymedium" colspan="4">'.$langs->trans("AllWidgetsWereEnabled").'</td></tr>';
380 }
381 print '</table>'."\n";
382 print '</div>';
383 
384 print '<div class="right">';
385 print '<input type="submit" class="button"'.(count($boxtoadd)?'':' disabled').' value="'.$langs->trans("Activate").'">';
386 print '</div>'."\n";
387 print '</form>';
388 print "\n".'<!-- End Boxes Available -->'."\n";
389 
390 
391 //var_dump($boxactivated);
392 print "<br>\n\n";
393 print load_fiche_titre($langs->trans("BoxesActivated"));
394 
395 print '<div class="div-table-responsive-no-min">';
396 print '<table class="tagtable liste">'."\n";
397 
398 print '<tr class="liste_titre">';
399 print '<td width="300">'.$langs->trans("Box").'</td>';
400 print '<td>'.$langs->trans("Note").'/'.$langs->trans("Parameters").'</td>';
401 print '<td align="center" width="160">'.$langs->trans("ActiveOn").'</td>';
402 print '<td align="center" width="60" colspan="2">'.$langs->trans("PositionByDefault").'</td>';
403 print '<td align="center" width="80">'.$langs->trans("Disable").'</td>';
404 print '</tr>'."\n";
405 
406 $box_order=1;
407 $foundrupture=1;
408 foreach($boxactivated as $key => $box)
409 {
410  if (preg_match('/^([^@]+)@([^@]+)$/i',$box->boximg))
411  {
412  $logo = $box->boximg;
413  }
414  else
415  {
416  $logo=preg_replace("/^object_/i","",$box->boximg);
417  }
418 
419  print "\n".'<!-- Box '.$box->boxcode.' -->'."\n";
420  print '<tr class="oddeven">';
421  print '<td>'.img_object("",$logo).' '.$langs->transnoentitiesnoconv($box->boxlabel);
422  if (! empty($box->class) && preg_match('/graph_/',$box->class)) print ' ('.$langs->trans("Graph").')';
423  print '</td>';
424  print '<td>';
425  if ($box->note == '(WarningUsingThisBoxSlowDown)')
426  {
427  $langs->load("errors");
428  print img_warning('',0).' '.$langs->trans("WarningUsingThisBoxSlowDown");
429  }
430  else print ($box->note?$box->note:'&nbsp;');
431  print '</td>';
432  print '<td align="center">' . (empty($pos_name[$box->position])?'':$langs->trans($pos_name[$box->position])) . '</td>';
433  $hasnext=($key < (count($boxactivated)-1));
434  $hasprevious=($key != 0);
435  print '<td align="center">'.($key+1).'</td>';
436  print '<td align="center">';
437  print ($hasnext?'<a href="boxes.php?action=switch&amp;switchfrom='.$box->rowid.'&amp;switchto='.$boxactivated[$key+1]->rowid.'">'.img_down().'</a>&nbsp;':'');
438  print ($hasprevious?'<a href="boxes.php?action=switch&amp;switchfrom='.$box->rowid.'&amp;switchto='.$boxactivated[$key-1]->rowid.'">'.img_up().'</a>':'');
439  print '</td>';
440  print '<td align="center">';
441  print '<a href="boxes.php?rowid='.$box->rowid.'&amp;action=delete">'.img_delete().'</a>';
442  print '</td>';
443 
444  print '</tr>'."\n";
445 }
446 
447 print '</table>';
448 print '</div>';
449 print '<br>';
450 
451 
452 // Other parameters
453 
454 print "\n\n".'<!-- Other Const -->'."\n";
455 print load_fiche_titre($langs->trans("Other"));
456 print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
457 print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
458 print '<input type="hidden" name="action" value="addconst">';
459 print '<table class="noborder" width="100%">';
460 
461 print '<tr class="liste_titre">';
462 print '<td class="liste_titre">'.$langs->trans("Parameter").'</td>';
463 print '<td class="liste_titre">'.$langs->trans("Value").'</td>';
464 print '</tr>';
465 
466 print '<tr class="oddeven">';
467 print '<td>';
468 print $langs->trans("MaxNbOfLinesForBoxes");
469 print '</td>'."\n";
470 print '<td>';
471 print '<input type="text" class="flat" size="6" name="MAIN_BOXES_MAXLINES" value="'.$conf->global->MAIN_BOXES_MAXLINES.'">';
472 print '</td>';
473 print '</tr>';
474 
475 // Activate FileCache - Developement
476 if ($conf->global->MAIN_FEATURES_LEVEL == 2 || ! empty($conf->global->MAIN_ACTIVATE_FILECACHE)) {
477 
478  print '<tr class="oddeven"><td width="35%">'.$langs->trans("EnableFileCache").'</td><td>';
479  print $form->selectyesno('MAIN_ACTIVATE_FILECACHE',$conf->global->MAIN_ACTIVATE_FILECACHE,1);
480  print '</td>';
481  print '</tr>';
482 }
483 
484 print '</table>';
485 
486 print '<div class="center"><input type="submit" class="button" value="'.$langs->trans("Save").'" name="Button"></div>';
487 
488 print '</form>';
489 print "\n".'<!-- End Other Const -->'."\n";
490 
491 
492 llxFooter();
493 
494 $db->close();
llxFooter()
Empty footer.
Definition: wrapper.php:58
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
static getListOfPagesForBoxes()
Name of positions 0=Home, 1=...
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).
Definition: admin.lib.php:485
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
Class ModeleBoxes.
if(empty($reshook)) $form
View.
Definition: perms.php:103
GETPOST($paramname, $check='none', $method=0, $filter=NULL, $options=NULL, $noreplace=0)
Return value of a param into GET or POST supervariable.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
img_down($titlealt= 'default', $selected=0, $moreclass='')
Show down arrow logo.
Class to manage generation of HTML components Only common components must be here.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
img_warning($titlealt= 'default', $moreatt= '')
Show warning logo.
img_up($titlealt= 'default', $selected=0, $moreclass='')
Show top arrow logo.
load_fiche_titre($titre, $morehtmlright='', $picto='title_generic.png', $pictoisfullpath=0, $id=0, $morecssontable='', $morehtmlcenter='')
Load a title with picto.
static listBoxes($db, $mode, $zone, $user=null, $excludelist=array(), $includehidden=1)
Return array of boxes qualified for area and user.
llxHeader()
Empty header.
Definition: wrapper.php:46
img_delete($titlealt= 'default', $other= 'class="pictodelete"')
Show delete logo.
print
Draft customers invoices.
Definition: index.php:91
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if(!empty($conf->fournisseur->enabled)&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->societe->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if(!empty($conf->fournisseur->enabled)&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1013
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.