dolibarr  7.0.0-beta
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@capnetworks.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
28 class InfoBox
29 {
35  static function getListOfPagesForBoxes()
36  {
37  return array(0=>'Home');
38  }
39 
51  static function listBoxes($db, $mode, $zone, $user=null, $excludelist=array(), $includehidden=1)
52  {
53  global $conf;
54 
55  $boxes=array();
56 
57  $confuserzone='MAIN_BOXES_'.$zone;
58  if ($mode == 'activated') // activated
59  {
60  $sql = "SELECT b.rowid, b.position, b.box_order, b.fk_user,";
61  $sql.= " d.rowid as box_id, d.file, d.note, d.tms";
62  $sql.= " FROM ".MAIN_DB_PREFIX."boxes as b, ".MAIN_DB_PREFIX."boxes_def as d";
63  $sql.= " WHERE b.box_id = d.rowid";
64  $sql.= " AND b.entity IN (0,".$conf->entity.")";
65  if ($zone >= 0) $sql.= " AND b.position = ".$zone;
66  if (is_object($user)) $sql.= " AND b.fk_user IN (0,".$user->id.")";
67  else $sql.= " AND b.fk_user = 0";
68  $sql.= " ORDER BY b.box_order";
69  }
70  else // available
71  {
72  $sql = "SELECT d.rowid as box_id, d.file, d.note, d.tms";
73  $sql.= " FROM ".MAIN_DB_PREFIX."boxes_def as d";
74  $sql.= " WHERE d.entity IN (0,".$conf->entity.")";
75  }
76 
77  dol_syslog(get_class()."::listBoxes get default box list for mode=".$mode." userid=".(is_object($user)?$user->id:'')."", LOG_DEBUG);
78  $resql = $db->query($sql);
79  if ($resql)
80  {
81  $num = $db->num_rows($resql);
82  $j = 0;
83  while ($j < $num)
84  {
85  $obj = $db->fetch_object($resql);
86 
87  if (! in_array($obj->box_id, $excludelist))
88  {
89 
90  if (preg_match('/^([^@]+)@([^@]+)$/i',$obj->file,$regs))
91  {
92  $boxname = preg_replace('/\.php$/i','',$regs[1]);
93  $module = $regs[2];
94  $relsourcefile = "/".$module."/core/boxes/".$boxname.".php";
95  }
96  else
97  {
98  $boxname=preg_replace('/\.php$/i','',$obj->file);
99  $relsourcefile = "/core/boxes/".$boxname.".php";
100  }
101 
102  //print $obj->box_id.'-'.$boxname.'-'.$relsourcefile.'<br>';
103 
104  // 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
105  // the "enabled" condition for modules forbidden for external users and the depends condition can be done.
106  // Goal is to avoid making a "new" done for each boxes returned by select.
107  dol_include_once($relsourcefile);
108  if (class_exists($boxname))
109  {
110  $box=new $boxname($db,$obj->note); // Constructor may set properties like box->enabled. obj->note is note into box def, not user params.
111  //$box=new stdClass();
112 
113  // box properties
114  $box->rowid = (empty($obj->rowid) ? '' : $obj->rowid);
115  $box->id = (empty($obj->box_id) ? '' : $obj->box_id);
116  $box->position = ($obj->position == '' ? '' : $obj->position); // '0' must staty '0'
117  $box->box_order = (empty($obj->box_order) ? '' : $obj->box_order);
118  $box->fk_user = (empty($obj->fk_user) ? 0 : $obj->fk_user);
119  $box->sourcefile= $relsourcefile;
120  $box->class = $boxname;
121 
122  if ($mode == 'activated' && ! is_object($user)) // List of activated box was not yet personalized into database
123  {
124  if (is_numeric($box->box_order))
125  {
126  if ($box->box_order % 2 == 1) $box->box_order='A'.$box->box_order;
127  elseif ($box->box_order % 2 == 0) $box->box_order='B'.$box->box_order;
128  }
129  }
130  // box_def properties
131  $box->box_id = (empty($obj->box_id) ? '' : $obj->box_id);
132  $box->note = (empty($obj->note) ? '' : $obj->note);
133 
134  // Filter on box->enabled (used for example by box_comptes)
135  // Filter also on box->depends. Example: array("product|service") or array("contrat", "service")
136  $enabled=$box->enabled;
137  if (isset($box->depends) && count($box->depends) > 0)
138  {
139  foreach($box->depends as $moduleelem)
140  {
141  $arrayelem=explode('|',$moduleelem);
142  $tmpenabled=0; // $tmpenabled is used for the '|' test (OR)
143  foreach($arrayelem as $module)
144  {
145  $tmpmodule=preg_replace('/@[^@]+/','',$module);
146  if (! empty($conf->$tmpmodule->enabled)) $tmpenabled=1;
147  //print $boxname.'-'.$module.'-module enabled='.(empty($conf->$tmpmodule->enabled)?0:1).'<br>';
148  }
149  if (empty($tmpenabled)) // We found at least one module required that is disabled
150  {
151  $enabled=0;
152  break;
153  }
154  }
155  }
156  //print '=>'.$boxname.'-enabled='.$enabled.'<br>';
157 
158  //print 'xx module='.$module.' enabled='.$enabled;
159  if ($enabled && ($includehidden || empty($box->hidden))) $boxes[]=$box;
160  else unset($box);
161  }
162  else
163  {
164  dol_syslog("Failed to load box '".$boxname."' into file '".$relsourcefile."'", LOG_WARNING);
165  }
166  }
167  $j++;
168  }
169  }
170  else
171  {
172  dol_syslog($db->lasterror(),LOG_ERR);
173  return array('error'=>$db->lasterror());
174  }
175 
176  return $boxes;
177  }
178 
179 
189  static function saveboxorder($db, $zone,$boxorder,$userid=0)
190  {
191  global $conf;
192 
193  $error=0;
194 
195  require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
196 
197  dol_syslog(get_class()."::saveboxorder zone=".$zone." userid=".$userid);
198 
199  if (! $userid || $userid == 0) return 0;
200 
201  $user = new User($db);
202  $user->id=$userid;
203 
204  $db->begin();
205 
206  // Save parameters to say user has a dedicated setup
207  $tab=array();
208  $confuserzone='MAIN_BOXES_'.$zone;
209  $tab[$confuserzone]=1;
210  if (dol_set_user_param($db, $conf, $user, $tab) < 0)
211  {
212  $error=$db->lasterror();
213  $db->rollback();
214  return -3;
215  }
216 
217  // Delete all lines
218  $sql = "DELETE FROM ".MAIN_DB_PREFIX."boxes";
219  $sql.= " WHERE entity = ".$conf->entity;
220  $sql.= " AND fk_user = ".$userid;
221  $sql.= " AND position = ".$zone;
222 
223  dol_syslog(get_class()."::saveboxorder", LOG_DEBUG);
224  $result = $db->query($sql);
225  if ($result)
226  {
227  $colonnes=explode('-',$boxorder);
228  foreach ($colonnes as $collist)
229  {
230  $part=explode(':',$collist);
231  $colonne=$part[0];
232  $list=$part[1];
233  dol_syslog(get_class()."::saveboxorder column=".$colonne.' list='.$list);
234 
235  $i=0;
236  $listarray=explode(',',$list);
237  foreach ($listarray as $id)
238  {
239  if (is_numeric($id))
240  {
241  //dol_syslog("aaaaa".count($listarray));
242  $i++;
243  $ii=sprintf('%02d',$i);
244  $sql = "INSERT INTO ".MAIN_DB_PREFIX."boxes";
245  $sql.= "(box_id, position, box_order, fk_user, entity)";
246  $sql.= " values (";
247  $sql.= " ".$id.",";
248  $sql.= " ".$zone.",";
249  $sql.= " '".$colonne.$ii."',";
250  $sql.= " ".$userid.",";
251  $sql.= " ".$conf->entity;
252  $sql.= ")";
253 
254  dol_syslog(get_class()."::saveboxorder", LOG_DEBUG);
255  $result = $db->query($sql);
256  if ($result < 0)
257  {
258  $error++;
259  break;
260  }
261  }
262  }
263  }
264  if ($error)
265  {
266  $error=$db->error();
267  $db->rollback();
268  return -2;
269  }
270  else
271  {
272  $db->commit();
273  return 1;
274  }
275  }
276  else
277  {
278  $error=$db->lasterror();
279  $db->rollback();
280  dol_syslog(get_class()."::saveboxorder ".$error);
281  return -1;
282  }
283  }
284 
285 }
286 
static getListOfPagesForBoxes()
Name of positions 0=Home, 1=...
Class to manage Dolibarr users.
Definition: user.class.php:39
static saveboxorder($db, $zone, $boxorder, $userid=0)
Save order of boxes for area and user.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
static listBoxes($db, $mode, $zone, $user=null, $excludelist=array(), $includehidden=1)
Return array of boxes qualified for area and user.
Class to manage boxes on pages.
dol_set_user_param($db, $conf, &$user, $tab)
Save personnal parameter.
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
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Return a prefix to use for this Dolibarr instance, for session/cookie names or email id...