dolibarr 21.0.0-alpha
cactioncomm.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2002-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2014 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.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 <https://www.gnu.org/licenses/>.
18 */
19
31{
35 public $error = '';
36
40 public $db;
41
45 public $id;
46
50 public $code;
51
55 public $type;
56
62 public $libelle;
63
67 public $label;
68
72 public $active;
73
77 public $color;
78
82 public $picto;
83
87 public $type_actions = array();
88
89
93 public $liste_array;
94
95
101 public function __construct($db)
102 {
103 $this->db = $db;
104 }
105
112 public function fetch($id)
113 {
114 $sql = "SELECT id, code, type, libelle as label, color, active, picto";
115 $sql .= " FROM ".MAIN_DB_PREFIX."c_actioncomm";
116 if (is_numeric($id)) {
117 $sql .= " WHERE id=".(int) $id;
118 } else {
119 $sql .= " WHERE code='".$this->db->escape($id)."'";
120 }
121
122 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
123 $resql = $this->db->query($sql);
124 if ($resql) {
125 if ($this->db->num_rows($resql)) {
126 $obj = $this->db->fetch_object($resql);
127
128 $this->id = $obj->id;
129 $this->code = $obj->code;
130 $this->type = $obj->type;
131 $this->libelle = $obj->label; // deprecated
132 $this->label = $obj->label;
133 $this->active = $obj->active;
134 $this->color = $obj->color;
135
136 $this->db->free($resql);
137 return 1;
138 } else {
139 $this->db->free($resql);
140 return 0;
141 }
142 } else {
143 $this->error = $this->db->error();
144 return -1;
145 }
146 }
147
148 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
160 public function liste_array($active = '', $idorcode = 'id', $excludetype = '', $onlyautoornot = 0, $morefilter = '', $shortlabel = 0)
161 {
162 // phpcs:enable
163 global $langs, $conf, $user;
164 $langs->load("commercial");
165
166 $actionstatic = new ActionComm($this->db);
167
168 $rep_id = array();
169 $rep_code = array();
170 $rep_all = array();
171
172 $sql = "SELECT id, code, libelle as label, module, type, color, picto";
173 $sql .= " FROM ".MAIN_DB_PREFIX."c_actioncomm";
174 $sql .= " WHERE 1=1";
175 if ($active != '') {
176 $sql .= " AND active=".(int) $active;
177 }
178 if (!empty($excludetype)) {
179 $sql .= " AND type <> '".$this->db->escape($excludetype)."'";
180 }
181 if ($morefilter) {
182 $sql .= " AND ".$morefilter;
183 }
184 // If AGENDA_SORT_EVENT_TYPE_BY_POSITION_FIRST is defined, we use position as main sort criterion
185 // otherwise we use type as main sort criterion
186 if (getDolGlobalString('AGENDA_SORT_EVENT_TYPE_BY_POSITION_FIRST')) {
187 $sql .= " ORDER BY position, type, module";
188 } else {
189 $sql .= " ORDER BY type, position, module";
190 }
191
192 dol_syslog(get_class($this)."::liste_array", LOG_DEBUG);
193 $resql = $this->db->query($sql);
194 if ($resql) {
195 $nump = $this->db->num_rows($resql);
196 if ($nump) {
197 $idforallfornewmodule = 96;
198 $TSystem = array(
199 'id' => [],
200 'code' => [],
201 'all' => []
202 );
203 $TSystemAuto = array(
204 'id' => [],
205 'code' => [],
206 'all' => []
207 );
208 $TModule = array(
209 'id' => [],
210 'code' => [],
211 'all' => []
212 );
213 $i = 0;
214 while ($i < $nump) {
215 $obj = $this->db->fetch_object($resql);
216
217 $qualified = 1;
218
219 // $obj->type into c_actioncomm can be 'system', 'systemauto', 'module', 'moduleauto', 'xxx', 'xxxauto'
220 // Note: type = system... than type of event is added among other standard events.
221 // type = module... then type of event is grouped into module defined into module = myobject@mymodule. Example: Event organization or external modules
222 // type = xxx... then type of event is added into list as a new flat value (not grouped). Example: Agefod external module
223 if ($qualified && $onlyautoornot > 0 && preg_match('/^system/', $obj->type) && !preg_match('/^AC_OTH/', $obj->code)) {
224 $qualified = 0; // We discard detailed system events. We keep only the 2 generic lines (AC_OTH and AC_OTH_AUTO)
225 }
226
227 if ($qualified && !empty($obj->module)) {
228 //var_dump($obj->type.' '.$obj->module.' '); var_dump($user->hasRight('facture', 'lire'));
229 $qualified = 0;
230 // Special cases
231 if ($obj->module == 'invoice' && isModEnabled('invoice') && $user->hasRight('facture', 'lire')) {
232 $qualified = 1;
233 }
234 if ($obj->module == 'order' && isModEnabled('order') && !$user->hasRight('commande', 'lire')) {
235 $qualified = 1;
236 }
237 if ($obj->module == 'propal' && isModEnabled("propal") && $user->hasRight('propal', 'lire')) {
238 $qualified = 1;
239 }
240 if ($obj->module == 'invoice_supplier' && ((isModEnabled("fournisseur") && !getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight('fournisseur', 'facture', 'lire')) || (isModEnabled('supplier_invoice') && $user->hasRight('supplier_invoice', 'lire')))) {
241 $qualified = 1;
242 }
243 if ($obj->module == 'order_supplier' && ((isModEnabled("fournisseur") && !getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight('fournisseur', 'commande', 'lire')) || (!isModEnabled('supplier_order') && $user->hasRight('supplier_order', 'lire')))) {
244 $qualified = 1;
245 }
246 if ($obj->module == 'shipping' && isModEnabled("shipping") && $user->hasRight('expedition', 'lire')) {
247 $qualified = 1;
248 }
249 // For case module = 'myobject@eventorganization'
250 $tmparray = explode("@", $obj->module);
251 if (count($tmparray) > 1 && $tmparray[1] == 'eventorganization' && isModEnabled('eventorganization')) {
252 $qualified = 1;
253 }
254 // For the generic case with type = 'module...' and module = 'myobject@mymodule'
255 $regs = array();
256 if (preg_match('/^module/', $obj->type)) {
257 if (preg_match('/^(.+)@(.+)$/', $obj->module, $regs)) {
258 $tmpobject = $regs[1];
259 $tmpmodule = $regs[2];
260 //var_dump($user->$tmpmodule);
261 if ($tmpmodule && isset($conf->$tmpmodule) && isModEnabled($tmpmodule) && ($user->hasRight($tmpmodule, 'read') || $user->hasRight($tmpmodule, 'lire') || $user->hasRight($tmpmodule, $tmpobject, 'read') || $user->hasRight($tmpmodule, $tmpobject, 'lire'))) {
262 $qualified = 1;
263 }
264 }
265 }
266 // For the case type is not 'system...' neither 'module', we just check module is on
267 if (! in_array($obj->type, array('system', 'systemauto', 'module', 'moduleauto'))) {
268 $tmpmodule = $obj->module;
269 //var_dump($tmpmodule);
270 if ($tmpmodule && isset($conf->$tmpmodule) && isModEnabled($tmpmodule)) {
271 $qualified = 1;
272 }
273 }
274 }
275
276 if ($qualified) {
277 $keyfortrans = '';
278 $transcode = '';
279 $code = $obj->code;
280 $typecalendar = $obj->type;
281
282 if ($onlyautoornot > 0 && $typecalendar == 'system') {
283 $code = 'AC_MANUAL';
284 } elseif ($onlyautoornot > 0 && $typecalendar == 'systemauto') {
285 $code = 'AC_AUTO';
286 } elseif ($onlyautoornot > 0) {
287 $code = 'AC_'.strtoupper($obj->module);
288 }
289
290 if ($shortlabel) {
291 $keyfortrans = "Action".$code.'Short';
292 $transcode = $langs->trans($keyfortrans);
293 }
294 if (empty($keyfortrans) || $keyfortrans == $transcode) {
295 $keyfortrans = "Action".$code;
296 $transcode = $langs->trans($keyfortrans);
297 }
298
299 $label = (($transcode != $keyfortrans) ? $transcode : $langs->trans($obj->label));
300 /*
301 $actionstatic->type_color = $obj->type_color;
302 $actionstatic->type_picto = $obj->type_picto;
303 $actionstatic->type = $obj->type;
304 $picto = $actionstatic->getTypePicto();
305 $label = $picto.$label;
306 */
307
308 if (($onlyautoornot == -1 || $onlyautoornot == -2) && getDolGlobalString('AGENDA_USE_EVENT_TYPE')) {
309 // Add a group of elements
310 if ($typecalendar == 'system' || $typecalendar == 'user') {
311 //$label = '&nbsp;&nbsp; '.$label;
312 $TSystem['id'][-99] = $langs->trans("ActionAC_MANUAL");
313 $TSystem['code']['AC_NON_AUTO'] = '<span class="smallincombo">-- '.$langs->trans("ActionAC_MANUAL").'</span>';
314 }
315 if ($typecalendar == 'systemauto') {
316 //$label = '&nbsp;&nbsp; '.$label;
317 $TSystemAuto['id'][-98] = $langs->trans("ActionAC_AUTO");
318 $TSystemAuto['code']['AC_ALL_AUTO'] = '<span class="smallincombo">-- '.$langs->trans("ActionAC_AUTO").'</span>';
319 }
320
321 if ($typecalendar == 'module') {
322 $module = preg_replace('/^[^@]+@/', '', $obj->module);
323 //$label = '&nbsp;&nbsp; '.$label;
324 if (!isset($TModule['id'][-1 * $idforallfornewmodule])) { // If first time for this module
325 $idforallfornewmodule--;
326 }
327 $TModule['id'][-1 * $idforallfornewmodule] = $langs->trans("ActionAC_ALL_".strtoupper($module));
328 $TModule['code']['AC_ALL_'.strtoupper($module)] = '<span class="smallincombo">-- '.$langs->trans("Module").' '.ucfirst($module).'</span>';
329 }
330 }
331 // Add element
332 if ($typecalendar == 'system' || $typecalendar == 'user') {
333 $TSystem['id'][$obj->id] = $label;
334 $TSystem['code'][$obj->code] = $label;
335 $TSystem['all'][$obj->code] = array('id' => $label, 'label' => $label, 'type' => $typecalendar, 'color' => $obj->color, 'picto' => $obj->picto);
336 } elseif ($typecalendar == 'systemauto') {
337 $TSystemAuto['id'][$obj->id] = $label;
338 $TSystemAuto['code'][$obj->code] = $label;
339 $TSystemAuto['all'][$obj->code] = array('id' => $label, 'label' => $label, 'type' => $typecalendar, 'color' => $obj->color, 'picto' => $obj->picto);
340 } elseif ($typecalendar == 'module') { // Can be automatic or manual
341 $module = preg_replace('/^[^@]+@/', '', $obj->module);
342 $TModule['id'][$obj->id] = $label;
343 $TModule['code'][$obj->code] = $label;
344 $TModule['all'][$obj->code] = array('id' => $label, 'label' => $langs->trans("Module").' '.ucfirst($module).' - '.$label, 'type' => $typecalendar, 'color' => $obj->color, 'picto' => $obj->picto);
345 }
346
347 if ($onlyautoornot > 0 && preg_match('/^module/', $obj->type) && $obj->module) {
348 $moduletoshow = ucfirst(preg_replace('/^[^@]+@/', '', $obj->module));
349 //array_key_exists($obj->code, $TModule['code']) ? ($TModule['code'][$obj->code] .= $langs->trans("Module").': '.$moduletoshow.' - '.$label) : ($TModule['code'][$obj->code] = $langs->trans("Module").': '.$moduletoshow.' - '.$label);
350 //array_key_exists($obj->code, $TModule['all']) ? ($TModule['all'][$obj->code]['label'] .= $langs->trans("Module").': '.$moduletoshow.' - '.$label) : ($TModule['all'][$obj->code]['label'] = $langs->trans("Module").': '.$moduletoshow.' - '.$label);
351 $TModule['code'][$obj->code] = $moduletoshow.' - '.$label;
352 $TModule['all'][$obj->code]['label'] = $moduletoshow.' - '.$label;
353 }
354 }
355 $i++;
356 }
357 }
358
359 if (empty($idorcode)) {
360 $idorcode = 'all';
361 }
362 $TType = $TSystem[$idorcode];
363 if (! empty($TSystemAuto[$idorcode])) {
364 $TType = array_merge($TType, $TSystemAuto[$idorcode]);
365 }
366 if (! empty($TModule[$idorcode])) {
367 $TType = array_merge($TType, $TModule[$idorcode]);
368 }
369 $this->liste_array = $TType;
370
371 return $this->liste_array;
372 } else {
373 $this->error = $this->db->lasterror();
374 return -1;
375 }
376 }
377
378
385 public function getNomUrl($withpicto = 0)
386 {
387 global $langs;
388
389 // Check if translation available
390 $transcode = $langs->trans("Action".$this->code);
391 if ($transcode != "Action".$this->code) {
392 return $transcode;
393 }
394 return -1;
395 }
396}
Class to manage agenda events (actions)
Class to manage different types of events.
liste_array($active='', $idorcode='id', $excludetype='', $onlyautoornot=0, $morefilter='', $shortlabel=0)
Return list of event types: array(id=>label) or array(code=>label)
fetch($id)
Load action type from database.
__construct($db)
Constructor.
getNomUrl($withpicto=0)
Return name of action type as a label translated.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition repair.php:137