dolibarr  16.0.5
infobox.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
5  * Copyright (C) 2019 Nicolas ZABOURI <info@inovea-conseil.com>
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 
29 class InfoBox
30 {
36  public static function getListOfPagesForBoxes()
37  {
38  global $conf;
39 
40  if (getDolGlobalInt('MAIN_FEATURES_LEVEL') < 2) {
41  return array(
42  0 => 'Home',
43  1 => 'UsersHome',
44  2 => 'MembersHome',
45  3 => 'ThirdpartiesHome',
46  11 => 'TicketsHome',
47  27 => 'AccountancyHome'
48  );
49  } else {
50  return array(
51  0 => 'Home',
52  1 => 'UsersHome',
53  2 => 'MembersHome',
54  3 => 'ThirdpartiesHome',
55  4 => 'productindex',
56  5 => 'productindex',
57  6 => 'mrpindex',
58  7 => 'commercialindex',
59  8 => 'projectsindex',
60  9 => 'invoiceindex',
61  10 => 'hrmindex',
62  11 => 'TicketsHome',
63  12 => 'stockindex',
64  13 => 'sendingindex',
65  14 => 'receptionindex',
66  15 => 'activityindex',
67  16 => 'proposalindex',
68  17 => 'ordersindex',
69  18 => 'orderssuppliersindex',
70  19 => 'contractindex',
71  20 => 'interventionindex',
72  21 => 'suppliersproposalsindex',
73  22 => 'donationindex',
74  23 => 'specialexpensesindex',
75  24 => 'expensereportindex',
76  25 => 'mailingindex',
77  26 => 'opensurveyindex',
78  27 => 'AccountancyHome'
79  );
80  }
81  }
82 
94  public static function listBoxes($dbs, $mode, $zone, $user = null, $excludelist = array(), $includehidden = 1)
95  {
96  global $conf;
97 
98  $boxes = array();
99 
100  if ($mode == 'activated') { // activated
101  $sql = "SELECT b.rowid, b.position, b.box_order, b.fk_user,";
102  $sql .= " d.rowid as box_id, d.file, d.note, d.tms";
103  $sql .= " FROM ".$dbs->prefix()."boxes as b, ".$dbs->prefix()."boxes_def as d";
104  $sql .= " WHERE b.box_id = d.rowid";
105  $sql .= " AND b.entity IN (0,".$conf->entity.")";
106  if ($zone >= 0) {
107  $sql .= " AND b.position = ".((int) $zone);
108  }
109  if (is_object($user)) {
110  $sql .= " AND b.fk_user IN (0,".$user->id.")";
111  } else {
112  $sql .= " AND b.fk_user = 0";
113  }
114  $sql .= " ORDER BY b.box_order";
115  } else { // available
116  $sql = "SELECT d.rowid as box_id, d.file, d.note, d.tms";
117  $sql .= " FROM ".$dbs->prefix()."boxes_def as d";
118  $sql .= " WHERE d.entity IN (0, ".$conf->entity.")";
119  }
120 
121  dol_syslog(get_class()."::listBoxes get default box list for mode=".$mode." userid=".(is_object($user) ? $user->id : '')."", LOG_DEBUG);
122  $resql = $dbs->query($sql);
123  if ($resql) {
124  $num = $dbs->num_rows($resql);
125  $j = 0;
126  while ($j < $num) {
127  $obj = $dbs->fetch_object($resql);
128 
129  if (!in_array($obj->box_id, $excludelist)) {
130  $regs = array();
131  if (preg_match('/^([^@]+)@([^@]+)$/i', $obj->file, $regs)) {
132  $boxname = preg_replace('/\.php$/i', '', $regs[1]);
133  $module = $regs[2];
134  $relsourcefile = "/".$module."/core/boxes/".$boxname.".php";
135  } else {
136  $boxname = preg_replace('/\.php$/i', '', $obj->file);
137  $relsourcefile = "/core/boxes/".$boxname.".php";
138  }
139 
140  //print $obj->box_id.'-'.$boxname.'-'.$relsourcefile.'<br>';
141 
142  // TODO PERF Do not make "dol_include_once" here, nor "new" later. This means, we must store a 'depends' field to store modules list, then
143  // the "enabled" condition for modules forbidden for external users and the depends condition can be done.
144  // Goal is to avoid making a "new" done for each boxes returned by select.
145  dol_include_once($relsourcefile);
146  if (class_exists($boxname)) {
147  $box = new $boxname($dbs, $obj->note); // Constructor may set properties like box->enabled. obj->note is note into box def, not user params.
148  //$box=new stdClass();
149 
150  // box properties
151  $box->rowid = (empty($obj->rowid) ? '' : $obj->rowid);
152  $box->id = (empty($obj->box_id) ? '' : $obj->box_id);
153  $box->position = ((isset($obj->position) && $obj->position == '') ? '' : (isset($obj->position) ? $obj->position : '')); // '0' must stay '0'
154  $box->box_order = (empty($obj->box_order) ? '' : $obj->box_order);
155  $box->fk_user = (empty($obj->fk_user) ? 0 : $obj->fk_user);
156  $box->sourcefile = $relsourcefile;
157  $box->class = $boxname;
158 
159  if ($mode == 'activated' && !is_object($user)) { // List of activated box was not yet personalized into database
160  if (is_numeric($box->box_order)) {
161  if ($box->box_order % 2 == 1) {
162  $box->box_order = 'A'.$box->box_order;
163  } elseif ($box->box_order % 2 == 0) {
164  $box->box_order = 'B'.$box->box_order;
165  }
166  }
167  }
168  // box_def properties
169  $box->box_id = (empty($obj->box_id) ? '' : $obj->box_id);
170  $box->note = (empty($obj->note) ? '' : $obj->note);
171 
172  // Filter on box->enabled (used for example by box_comptes)
173  // Filter also on box->depends. Example: array("product|service") or array("contrat", "service")
174  $enabled = $box->enabled;
175  if (isset($box->depends) && count($box->depends) > 0) {
176  foreach ($box->depends as $moduleelem) {
177  $arrayelem = explode('|', $moduleelem);
178  $tmpenabled = 0; // $tmpenabled is used for the '|' test (OR)
179  foreach ($arrayelem as $module) {
180  $tmpmodule = preg_replace('/@[^@]+/', '', $module);
181  if (!empty($conf->$tmpmodule->enabled)) {
182  $tmpenabled = 1;
183  }
184  //print $boxname.'-'.$module.'-module enabled='.(empty($conf->$tmpmodule->enabled)?0:1).'<br>';
185  }
186  if (empty($tmpenabled)) { // We found at least one module required that is disabled
187  $enabled = 0;
188  break;
189  }
190  }
191  }
192  //print '=>'.$boxname.'-enabled='.$enabled.'<br>';
193 
194  //print 'xx module='.$module.' enabled='.$enabled;
195  if ($enabled && ($includehidden || empty($box->hidden))) {
196  $boxes[] = $box;
197  } else {
198  unset($box);
199  }
200  } else {
201  dol_syslog("Failed to load box '".$boxname."' into file '".$relsourcefile."'", LOG_WARNING);
202  }
203  }
204  $j++;
205  }
206  } else {
207  dol_syslog($dbs->lasterror(), LOG_ERR);
208  return array('error'=>$dbs->lasterror());
209  }
210 
211  return $boxes;
212  }
213 
214 
224  public static function saveboxorder($dbs, $zone, $boxorder, $userid = 0)
225  {
226  global $conf;
227 
228  $error = 0;
229 
230  require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
231 
232  dol_syslog(get_class()."::saveboxorder zone=".$zone." userid=".$userid);
233 
234  if (!$userid || $userid == 0) {
235  return 0;
236  }
237 
238  $user = new User($dbs);
239  $user->id = $userid;
240 
241  $dbs->begin();
242 
243  // Save parameters to say user has a dedicated setup
244  $tab = array();
245  $confuserzone = 'MAIN_BOXES_'.$zone;
246  $tab[$confuserzone] = 1;
247  if (dol_set_user_param($dbs, $conf, $user, $tab) < 0) {
248  $error = $dbs->lasterror();
249  $dbs->rollback();
250  return -3;
251  }
252 
253  // Delete all lines
254  $sql = "DELETE FROM ".$dbs->prefix()."boxes";
255  $sql .= " WHERE entity = ".$conf->entity;
256  $sql .= " AND fk_user = ".((int) $userid);
257  $sql .= " AND position = ".((int) $zone);
258 
259  dol_syslog(get_class()."::saveboxorder", LOG_DEBUG);
260  $result = $dbs->query($sql);
261  if ($result) {
262  $colonnes = explode('-', $boxorder);
263  foreach ($colonnes as $collist) {
264  $part = explode(':', $collist);
265  $colonne = $part[0];
266  $list = $part[1];
267  dol_syslog(get_class()."::saveboxorder column=".$colonne.' list='.$list);
268 
269  $i = 0;
270  $listarray = explode(',', $list);
271  foreach ($listarray as $id) {
272  if (is_numeric($id)) {
273  //dol_syslog("aaaaa".count($listarray));
274  $i++;
275  $ii = sprintf('%02d', $i);
276 
277  $sql = "INSERT INTO ".$dbs->prefix()."boxes";
278  $sql .= "(box_id, position, box_order, fk_user, entity)";
279  $sql .= " values (";
280  $sql .= " ".((int) $id).",";
281  $sql .= " ".((int) $zone).",";
282  $sql .= " '".$dbs->escape($colonne.$ii)."',";
283  $sql .= " ".((int) $userid).",";
284  $sql .= " ".((int) $conf->entity);
285  $sql .= ")";
286 
287  $result = $dbs->query($sql);
288  if ($result < 0) {
289  $error++;
290  break;
291  }
292  }
293  }
294  }
295  } else {
296  $error++;
297  }
298 
299  if ($error) {
300  $dbs->rollback();
301  return -2;
302  } else {
303  $dbs->commit();
304  return 1;
305  }
306  }
307 }
dol_include_once
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
Definition: functions.lib.php:1033
InfoBox\saveboxorder
static saveboxorder($dbs, $zone, $boxorder, $userid=0)
Save order of boxes for area and user.
Definition: infobox.class.php:224
InfoBox
Class to manage boxes on pages.
Definition: infobox.class.php:29
dol_set_user_param
dol_set_user_param($db, $conf, &$user, $tab)
Save personnal parameter.
Definition: functions2.lib.php:1786
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
InfoBox\listBoxes
static listBoxes($dbs, $mode, $zone, $user=null, $excludelist=array(), $includehidden=1)
Return array of boxes qualified for area and user.
Definition: infobox.class.php:94
User
Class to manage Dolibarr users.
Definition: user.class.php:44
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->supplier_invoice->lire)) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
InfoBox\getListOfPagesForBoxes
static getListOfPagesForBoxes()
Name of positions (See below)
Definition: infobox.class.php:36
getDolGlobalInt
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
Definition: functions.lib.php:93