dolibarr 23.0.3
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 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
31{
37 public static function getListOfPagesForBoxes()
38 {
39 global $conf;
40
41 if (getDolGlobalInt('MAIN_FEATURES_LEVEL') < 2) {
42 return array(
43 0 => 'Home',
44 1 => 'UsersHome',
45 2 => 'MembersHome',
46 3 => 'ThirdpartiesHome',
47 4 => 'productindex',
48 11 => 'TicketsHome',
49 20 => 'interventionindex',
50 27 => 'AccountancyHome'
51 );
52 } else {
53 return array(
54 0 => 'Home',
55 1 => 'UsersHome',
56 2 => 'MembersHome',
57 3 => 'ThirdpartiesHome',
58 4 => 'productindex',
59 5 => 'productindex',
60 6 => 'mrpindex',
61 7 => 'commercialindex',
62 8 => 'projectsindex',
63 9 => 'invoiceindex',
64 10 => 'hrmindex',
65 11 => 'TicketsHome',
66 12 => 'stockindex',
67 13 => 'sendingindex',
68 14 => 'receptionindex',
69 15 => 'activityindex',
70 16 => 'proposalindex',
71 17 => 'ordersindex',
72 18 => 'orderssuppliersindex',
73 19 => 'contractindex',
74 20 => 'interventionindex',
75 21 => 'suppliersproposalsindex',
76 22 => 'donationindex',
77 23 => 'specialexpensesindex',
78 24 => 'expensereportindex',
79 25 => 'mailingindex',
80 26 => 'opensurveyindex',
81 27 => 'AccountancyHome'
82 );
83 }
84 }
85
97 public static function listBoxes($dbs, $mode, $zone, $user = null, $excludelist = array(), $includehidden = 1)
98 {
99 global $conf;
100
101 $boxes = array();
102
103 if ($mode == 'activated') { // activated
104 $sql = "SELECT b.rowid, b.position, b.box_order, b.fk_user,";
105 $sql .= " d.rowid as box_id, d.file, d.note, d.tms";
106 $sql .= " FROM ".$dbs->prefix()."boxes as b, ".$dbs->prefix()."boxes_def as d";
107 $sql .= " WHERE b.box_id = d.rowid";
108 $sql .= " AND b.entity IN (0,".$conf->entity.")";
109 if ($zone >= 0) {
110 $sql .= " AND b.position = ".((int) $zone);
111 }
112 if (is_object($user)) {
113 $sql .= " AND b.fk_user IN (0,".$user->id.")";
114 } else {
115 $sql .= " AND b.fk_user = 0";
116 }
117 $sql .= " ORDER BY b.box_order";
118 } else { // available
119 $sql = "SELECT d.rowid as box_id, d.file, d.note, d.tms";
120 $sql .= " FROM ".$dbs->prefix()."boxes_def as d";
121 $sql .= " WHERE d.entity IN (0, ".$conf->entity.")";
122 }
123
124 dol_syslog(self::class."::listBoxes get default box list for mode=".$mode." userid=".(is_object($user) ? $user->id : ''), LOG_DEBUG);
125 $resql = $dbs->query($sql);
126 if ($resql) {
127 $num = $dbs->num_rows($resql);
128 $j = 0;
129 while ($j < $num) {
130 $obj = $dbs->fetch_object($resql);
131
132 if (!in_array($obj->box_id, $excludelist)) {
133 $regs = array();
134 if (preg_match('/^([^@]+)@([^@]+)$/i', $obj->file, $regs)) {
135 $boxname = preg_replace('/\.php$/i', '', $regs[1]);
136 $module = $regs[2];
137 $relsourcefile = "/".$module."/core/boxes/".$boxname.".php";
138 } else {
139 $boxname = preg_replace('/\.php$/i', '', $obj->file);
140 $relsourcefile = "/core/boxes/".$boxname.".php";
141 }
142
143 //print $obj->box_id.'-'.$boxname.'-'.$relsourcefile.'<br>';
144
145 // 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
146 // the "enabled" condition for modules forbidden for external users and the depends condition can be done.
147 // Goal is to avoid to make a "new" done for each boxes returned by select.
148 dol_include_once($relsourcefile);
149 if (class_exists($boxname)) {
150 $box = new $boxname($dbs, $obj->note); // Constructor may set properties like box->enabled. obj->note is note into box def, not user params.
151 '@phan-var-force ModeleBoxes $box';
152 //$box=new stdClass();
153
154 // box properties
155 $box->rowid = (empty($obj->rowid) ? '' : $obj->rowid);
156 $box->id = (empty($obj->box_id) ? '' : $obj->box_id);
157 $box->position = ((isset($obj->position) && $obj->position == '') ? '' : (isset($obj->position) ? $obj->position : '')); // '0' must stay '0'
158 $box->box_order = (empty($obj->box_order) ? '' : $obj->box_order);
159 $box->fk_user = (empty($obj->fk_user) ? 0 : $obj->fk_user);
160 $box->sourcefile = $relsourcefile;
161 $box->class = $boxname;
162
163 if ($mode == 'activated' && !is_object($user)) { // List of activated box was not yet personalized into database
164 if (is_numeric($box->box_order)) {
165 if (((int) $box->box_order % 2) == 1) {
166 $box->box_order = 'A'.$box->box_order;
167 } elseif (((int) $box->box_order % 2) == 0) {
168 $box->box_order = 'B'.$box->box_order;
169 }
170 }
171 }
172 // box_def properties
173 $box->box_id = (empty($obj->box_id) ? '' : $obj->box_id);
174 $box->note = (empty($obj->note) ? '' : $obj->note);
175
176 // Filter on box->enabled (used for example by box_comptes)
177 // Filter also on box->depends. Example: array("product|service") or array("contrat", "service")
178 $enabled = $box->enabled;
179 if (isset($box->depends) && count($box->depends) > 0) {
180 foreach ($box->depends as $moduleelem) {
181 $arrayelem = explode('|', $moduleelem);
182 $tmpenabled = 0; // $tmpenabled is used for the '|' test (OR)
183 foreach ($arrayelem as $module) {
184 $tmpmodule = preg_replace('/@[^@]+/', '', $module);
185 if (!empty($tmpmodule) && isModEnabled($tmpmodule)) {
186 $tmpenabled = 1;
187 }
188 //print $boxname.'-'.$module.'-module enabled='.(empty($conf->$tmpmodule->enabled)?0:1).'<br>';
189 }
190 if (empty($tmpenabled)) { // We found at least one module required that is disabled
191 $enabled = 0;
192 break;
193 }
194 }
195 }
196 //print '=>'.$boxname.'-enabled='.$enabled.'<br>';
197
198 //print 'xx module='.$module.' enabled='.$enabled;
199 if ($enabled && ($includehidden || empty($box->hidden))) {
200 $boxes[] = $box;
201 } else {
202 unset($box);
203 }
204 } else {
205 dol_syslog("Failed to load box '".$boxname."' into file '".$relsourcefile."'", LOG_WARNING);
206 }
207 }
208 $j++;
209 }
210 } else {
211 dol_syslog($dbs->lasterror(), LOG_ERR);
212 return array('error' => $dbs->lasterror());
213 }
214
215 return $boxes;
216 }
217
218
228 public static function saveboxorder($dbs, $zone, $boxorder, $userid = 0)
229 {
230 global $conf;
231
232 $error = 0;
233
234 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
235
236 dol_syslog(self::class."::saveboxorder zone=".$zone." userid=".$userid);
237
238 if (!$userid || $userid == 0) {
239 return 0;
240 }
241
242 $user = new User($dbs);
243 $user->id = $userid;
244
245 $dbs->begin();
246
247 // Save parameters to say user has a dedicated setup
248 $tab = array();
249 $confuserzone = 'MAIN_BOXES_'.$zone;
250 $tab[$confuserzone] = '1';
251 if (dol_set_user_param($dbs, $conf, $user, $tab) < 0) {
252 $error = $dbs->lasterror();
253 $dbs->rollback();
254 return -3;
255 }
256
257 if (!is_numeric($zone)) {
258 $zone = '0'; // Force $zone to a numeric value string
259 }
260
261 // Delete all lines
262 $sql = "DELETE FROM ".$dbs->prefix()."boxes";
263 $sql .= " WHERE entity = ".$conf->entity;
264 $sql .= " AND fk_user = ".((int) $userid);
265 $sql .= " AND position = ".((int) $zone);
266
267 dol_syslog(self::class."::saveboxorder", LOG_DEBUG);
268 $result = $dbs->query($sql);
269 if ($result) {
270 $colonnes = explode('-', $boxorder);
271 foreach ($colonnes as $collist) {
272 $part = explode(':', $collist);
273 $colonne = $part[0];
274 $list = $part[1];
275 dol_syslog(self::class."::saveboxorder column=".$colonne.' list='.$list);
276
277 $i = 0;
278 $listarray = explode(',', $list);
279 foreach ($listarray as $id) {
280 if (is_numeric($id)) {
281 //dol_syslog("aaaaa".count($listarray));
282 $i++;
283 $ii = sprintf('%02d', $i);
284
285 $sql = "INSERT INTO ".$dbs->prefix()."boxes";
286 $sql .= "(box_id, position, box_order, fk_user, entity)";
287 $sql .= " values (";
288 $sql .= " ".((int) $id).",";
289 $sql .= " ".((int) $zone).",";
290 $sql .= " '".$dbs->escape($colonne.$ii)."',";
291 $sql .= " ".((int) $userid).",";
292 $sql .= " ".((int) $conf->entity);
293 $sql .= ")";
294
295 $result = $dbs->query($sql);
296 if ($result < 0) {
297 $error++;
298 break;
299 }
300 }
301 }
302 }
303 } else {
304 $error++;
305 }
306
307 if ($error) {
308 $dbs->rollback();
309 return -2;
310 } else {
311 $dbs->commit();
312 return 1;
313 }
314 }
315}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
Class to manage boxes on pages.
static saveboxorder($dbs, $zone, $boxorder, $userid=0)
Save order of boxes for area and user.
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 to manage Dolibarr users.
dol_set_user_param($db, $conf, &$user, $tab, $entity=-1)
Save personal parameter.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.