Passed
Branch develop (356b3a)
by
unknown
98:06
created

ActionComm::getTooltipContentArray()   C

Complexity

Conditions 14

Size

Total Lines 59
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 35
nop 1
dl 0
loc 59
rs 6.2666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/* Copyright (C) 2002-2004  Rodolphe Quiedeville    <[email protected]>
3
 * Copyright (C) 2004-2011  Laurent Destailleur     <[email protected]>
4
 * Copyright (C) 2005-2012  Regis Houssin           <[email protected]>
5
 * Copyright (C) 2011-2017  Juanjo Menent           <[email protected]>
6
 * Copyright (C) 2015	    Marcos García		    <[email protected]>
7
 * Copyright (C) 2018	    Nicolas ZABOURI	        <[email protected]>
8
 * Copyright (C) 2018-2023  Frédéric France         <[email protected]>
9
 *
10
 * This program is free software; you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation; either version 3 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22
 */
23
24
/**
25
 *       \file       htdocs/comm/action/class/actioncomm.class.php
26
 *       \ingroup    agenda
27
 *       \brief      File of class to manage agenda events (actions)
28
 */
29
require_once DOL_DOCUMENT_ROOT.'/comm/action/class/cactioncomm.class.php';
30
require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
31
require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
32
require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncommreminder.class.php';
33
34
35
/**
36
 *		Class to manage agenda events (actions)
37
 */
38
class ActionComm extends CommonObject
39
{
40
	/**
41
	 * @var string ID to identify managed object
42
	 */
43
	public $element = 'action';
44
45
	/**
46
	 * @var string Name of table without prefix where object is stored
47
	 */
48
	public $table_element = 'actioncomm';
49
50
	/**
51
	 * @var string Name of id column
52
	 */
53
	public $table_rowid = 'id';
54
55
	/**
56
	 * @var string Name of icon for actioncomm object. Filename of icon is object_action.png
57
	 */
58
	public $picto = 'action';
59
60
	/**
61
	 * @var int 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
62
	 */
63
	public $ismultientitymanaged = 1;
64
65
	/**
66
	 * @var integer 0=Default
67
	 *              1=View may be restricted to sales representative only if no permission to see all or to company of external user if external user
68
	 *              2=Same than 1 but accept record if fksoc is empty
69
	 */
70
	public $restrictiononfksoc = 2;
71
72
	/**
73
	 * @var int Id of the event
74
	 */
75
	public $id;
76
77
	/**
78
	 * @var int Id of the event. Use $id as possible
79
	 */
80
	public $ref;
81
82
	/**
83
	 * @var int Id into parent table llx_c_actioncomm (used only if option to use type is set)
84
	 * 			This field is stored info fk_action. It contains the id into table llx_ac_actioncomm.
85
	 */
86
	public $type_id;
87
88
	/**
89
	 * @var string Calendar of event (Type of type of event). 'system'=Default calendar, 'systemauto'=Auto calendar, 'birthdate', 'holiday', 'module'=Calendar specific to a module
90
	 *             This field contains the type into table llx_ac_actioncomm ('system', 'systemauto', ...). It should be named 'type_type'.
91
	 */
92
	public $type;
93
94
	/**
95
	 * @var string Code into parent table llx_c_actioncomm (used only if option to use type is set). With default setup, should be AC_OTH_AUTO or AC_OTH.
96
	 *             This field contains the code into table llx_ac_actioncomm.
97
	 */
98
	public $type_code;
99
100
	/**
101
	 * @var string Type label
102
	 */
103
	public $type_label;
104
105
	/**
106
	 * @var string Color into parent table llx_c_actioncomm (used only if option to use type is set)
107
	 */
108
	public $type_color;
109
110
	/**
111
	 * @var string Picto for type of event (used only if option to use type is set)
112
	 */
113
	public $type_picto;
114
115
	/**
116
	 * @var string Free code to identify action. Ie: Agenda trigger add here AC_TRIGGERNAME ('AC_COMPANY_CREATE', 'AC_PROPAL_VALIDATE', ...)
117
	 * 			   This field is stored into field 'code' into llx_actioncomm.
118
	 */
119
	public $code;
120
121
	/**
122
	 * @var string Agenda event label
123
	 */
124
	public $label;
125
126
	/**
127
	 * @var string Agenda event label
128
	 * @deprecated Use $label
129
	 */
130
	public $libelle;
131
132
	/**
133
	 * @var integer Date creation record (datec)
134
	 */
135
	public $datec;
136
137
	/**
138
	 * @var integer Duration (duree)
139
	 */
140
	public $duree;
141
142
	/**
143
	 * @var integer Date modification record (tms)
144
	 */
145
	public $datem;
146
147
	/**
148
	 * @var User Object user that create action
149
	 * @deprecated
150
	 * @see $authorid
151
	 */
152
	public $author;
153
154
	/**
155
	 * @var User Object user that modified action
156
	 * @deprecated
157
	 * @see $usermodid
158
	 */
159
	public $usermod;
160
161
	/**
162
	 * @var int Id user that create action
163
	 */
164
	public $authorid;
165
166
	/**
167
	 * @var int Id user that modified action
168
	 */
169
	public $usermodid;
170
171
	/**
172
	 * @var integer Date action start (datep)
173
	 */
174
	public $datep;
175
176
	/**
177
	 * @var integer Date action end (datef)
178
	 */
179
	public $datef;
180
181
	/**
182
	 * @var integer This is date start action (datep) but modified to not be outside calendar view.
183
	 */
184
	public $date_start_in_calendar;
185
186
	/**
187
	 * @var integer This is date end action (datef) but modified to not be outside calendar view.
188
	 */
189
	public $date_end_in_calendar;
190
191
	/**
192
	 * @var integer Date action end (datep2)
193
	 */
194
	public $datep2;
195
196
	/**
197
	 * @var int -1=Unkown duration
198
	 * @deprecated
199
	 */
200
	public $durationp = -1;
201
202
	/**
203
	 * @var int 1=Event on full day
204
	 */
205
	public $fulldayevent = 0;
206
207
	/**
208
	 * @var int 1=???
209
	 */
210
	public $ponctuel;
211
212
	/**
213
	 * @var integer Percentage
214
	 */
215
	public $percentage;
216
217
	/**
218
	 * @var string Location
219
	 */
220
	public $location;
221
222
	/**
223
	 * @var int Transparency (ical standard). Used to say if people assigned to event are busy or not by event. 0=available, 1=busy, 2=busy (refused events)
224
	 */
225
	public $transparency;
226
227
	/**
228
	 * @var int 	(0 By default)
229
	 */
230
	public $priority;
231
232
	/**
233
	 * @var int[] 	Array of user ids
234
	 */
235
	public $userassigned = array();
236
237
	/**
238
	 * @var int 	Id of user owner = fk_user_action into table
239
	 */
240
	public $userownerid;
241
242
	/**
243
	 * @var int 	Id of user that has done the event. Used only if AGENDA_ENABLE_DONEBY is set.
244
	 */
245
	public $userdoneid;
246
247
	/**
248
	 * @var int[] Array of contact ids
249
	 */
250
	public $socpeopleassigned = array();
251
252
	/**
253
	 * @var int[] Array of other contact emails (not user, not contact)
254
	 */
255
	public $otherassigned = array();
256
257
	/**
258
	 * @var array	Array of reminders
259
	 */
260
	public $reminders = array();
261
262
	/**
263
	 * @var int thirdparty id linked to action
264
	 */
265
	public $socid;
266
267
	/**
268
	 * @var int socpeople id linked to action
269
	 */
270
	public $contact_id;
271
272
273
	/**
274
	 * @var Societe|null Company linked to action (optional)
275
	 * @deprecated
276
	 * @see $socid
277
	 */
278
	public $societe;
279
280
	/**
281
	 * @var Contact|null Contact linked to action (optional)
282
	 * @deprecated
283
	 * @see $contact_id
284
	 */
285
	public $contact;
286
287
	// Properties for links to other objects
288
	/**
289
	 * @var int 		Id of linked object
290
	 */
291
	public $fk_element; // Id of record
292
293
	/**
294
	 * @var int 		Id of record alternative for API
295
	 */
296
	public $elementid;
297
298
	/**
299
	 * @var string 		Type of record. This if property ->element of object linked to.
300
	 */
301
	public $elementtype;
302
303
	/**
304
	 * @var string Ical name
305
	 */
306
	public $icalname;
307
308
	/**
309
	 * @var string Ical color
310
	 */
311
	public $icalcolor;
312
313
	/**
314
	 * @var string Extraparam
315
	 */
316
	public $extraparams;
317
318
	/**
319
	 * @var array Actions
320
	 */
321
	public $actions = array();
322
323
	/**
324
	 * @var string Email msgid
325
	 */
326
	public $email_msgid;
327
328
	/**
329
	 * @var string Email from
330
	 */
331
	public $email_from;
332
333
	/**
334
	 * @var string Email sender
335
	 */
336
	public $email_sender;
337
338
	/**
339
	 * @var string Email to
340
	 */
341
	public $email_to;
342
343
	/**
344
	 * @var string Email tocc
345
	 */
346
	public $email_tocc;
347
	/**
348
	 * @var string Email tobcc
349
	 */
350
	public $email_tobcc;
351
352
	/**
353
	 * @var string Email subject
354
	 */
355
	public $email_subject;
356
357
	/**
358
	 * @var string Email errors to
359
	 */
360
	public $errors_to;
361
362
	/**
363
	 * @var int number of vote for an event
364
	 */
365
	public $num_vote;
366
367
	/**
368
	 * @var int if event is paid
369
	 */
370
	public $event_paid;
371
372
	/**
373
	 * @var int status use but Event organisation module
374
	 */
375
	public $status;
376
377
	/**
378
	 * Properties to manage the recurring events
379
	 */
380
	public $recurid;		/* A string YYYYMMDDHHMMSS shared by allevent of same serie */
381
	public $recurrule;		/* Rule of recurring */
382
	public $recurdateend;	/* Repeat until this date */
383
384
	public $calling_duration;
385
386
387
	/**
388
	 * Typical value for a event that is in a todo state
389
	 */
390
	const EVENT_TODO = 0;
391
392
	/**
393
	 * Typical value for a event that is in a progress state
394
	 */
395
	const EVENT_IN_PROGRESS = 50;
396
397
	/**
398
	 * Typical value for a event that is in a finished state
399
	 */
400
	const EVENT_FINISHED = 100;
401
402
403
	public $fields = array();
404
405
406
	/**
407
	 *      Constructor
408
	 *
409
	 *      @param      DoliDB		$db      Database handler
410
	 */
411
	public function __construct(DoliDB $db)
412
	{
413
		$this->db = $db;
414
	}
415
416
	/**
417
	 *    Add an action/event into database.
418
	 *    $this->type_id OR $this->type_code must be set.
419
	 *
420
	 *    @param	User	$user      		Object user making action
421
	 *    @param    int		$notrigger		1 = disable triggers, 0 = enable triggers
422
	 *    @return   int 		        	Id of created event, < 0 if KO
423
	 */
424
	public function create(User $user, $notrigger = 0)
425
	{
426
		global $langs, $conf;
427
428
		$error = 0;
429
		$now = dol_now();
430
431
		// Check parameters
432
		if (!isset($this->userownerid) || (string) $this->userownerid === '') {	// $this->userownerid may be 0 (anonymous event) or > 0
433
			dol_syslog("You tried to create an event but mandatory property ownerid was not defined", LOG_WARNING);
434
			$this->errors[] = 'ErrorActionCommPropertyUserowneridNotDefined';
435
			return -1;
436
		}
437
438
		// Clean parameters
439
		$this->label = dol_trunc(trim($this->label), 128);
440
		$this->location = dol_trunc(trim($this->location), 128);
441
		$this->note_private = dol_htmlcleanlastbr(trim(empty($this->note_private) ? $this->note : $this->note_private));
442
		if (empty($this->percentage)) {
443
			$this->percentage = 0;
444
		}
445
		if (empty($this->priority) || !is_numeric($this->priority)) {
446
			$this->priority = 0;
447
		}
448
		if (empty($this->fulldayevent)) {
449
			$this->fulldayevent = 0;
450
		}
451
		if (empty($this->transparency)) {
452
			$this->transparency = 0;
453
		}
454
		if ($this->percentage > 100) {
455
			$this->percentage = 100;
456
		}
457
		//if ($this->percentage == 100 && ! $this->dateend) $this->dateend = $this->date;
458
		if (!empty($this->datep) && !empty($this->datef)) {
459
			$this->durationp = ($this->datef - $this->datep); // deprecated
460
		}
461
		//if (!empty($this->date)  && !empty($this->dateend)) $this->durationa=($this->dateend - $this->date);
462
		if (!empty($this->datep) && !empty($this->datef) && $this->datep > $this->datef) {
463
			$this->datef = $this->datep;
464
		}
465
		//if (!empty($this->date)  && !empty($this->dateend) && $this->date > $this->dateend) $this->dateend=$this->date;
466
		if (!isset($this->fk_project) || $this->fk_project < 0) {
467
			$this->fk_project = 0;
468
		}
469
		// For backward compatibility
470
		if ($this->elementtype == 'facture') {
471
			$this->elementtype = 'invoice';
472
		}
473
		if ($this->elementtype == 'commande') {
474
			$this->elementtype = 'order';
475
		}
476
		if ($this->elementtype == 'contrat') {
477
			$this->elementtype = 'contract';
478
		}
479
480
		if (!is_array($this->userassigned) && !empty($this->userassigned)) {	// For backward compatibility when userassigned was an int instead of an array
481
			$tmpid = (int) $this->userassigned;
482
			$this->userassigned = array();
483
			$this->userassigned[$tmpid] = array('id'=>$tmpid, 'transparency'=>$this->transparency);
484
		}
485
486
		$userownerid = $this->userownerid;
487
		$userdoneid = $this->userdoneid;
488
489
		// Be sure assigned user is defined as an array of array('id'=>,'mandatory'=>,...).
490
		if (empty($this->userassigned) || count($this->userassigned) == 0 || !is_array($this->userassigned)) {
491
			$this->userassigned = array($userownerid=>array('id'=>$userownerid, 'transparency'=>$this->transparency));
492
		}
493
494
		if (!$this->type_id || !$this->type_code) {
495
			$key = empty($this->type_id) ? $this->type_code : $this->type_id;
496
497
			// Get id from code
498
			$cactioncomm = new CActionComm($this->db);
499
			$result = $cactioncomm->fetch($key);
500
501
			if ($result > 0) {
502
				$this->type_id = $cactioncomm->id;
503
				$this->type_code = $cactioncomm->code;
504
			} elseif ($result == 0) {
505
				$this->error = $langs->trans('ErrorActionCommBadType', $this->type_id, $this->type_code);
506
				return -1;
507
			} else {
508
				$this->error = $cactioncomm->error;
509
				return -1;
510
			}
511
		}
512
		$code = empty($this->code) ? $this->type_code : $this->code;
513
514
		// Check parameters
515
		if (!$this->type_id) {
516
			$this->error = "ErrorWrongParameters";
517
			return -1;
518
		}
519
520
		$this->db->begin();
521
522
		$sql = "INSERT INTO ".MAIN_DB_PREFIX."actioncomm";
523
		$sql .= "(ref,";
524
		$sql .= "datec,";
525
		$sql .= "datep,";
526
		$sql .= "datep2,";
527
		$sql .= "durationp,"; // deprecated
528
		$sql .= "fk_action,";
529
		$sql .= "code,";
530
		$sql .= "ref_ext,";
531
		$sql .= "fk_soc,";
532
		$sql .= "fk_project,";
533
		$sql .= "note,";
534
		$sql .= "fk_contact,";
535
		$sql .= "fk_user_author,";
536
		$sql .= "fk_user_action,";
537
		$sql .= "fk_user_done,";
538
		$sql .= "label,percent,priority,fulldayevent,location,";
539
		$sql .= "transparency,";
540
		$sql .= "fk_element,";
541
		$sql .= "elementtype,";
542
		$sql .= "entity,";
543
		$sql .= "extraparams,";
544
		// Fields emails
545
		$sql .= "email_msgid,";
546
		$sql .= "email_from,";
547
		$sql .= "email_sender,";
548
		$sql .= "email_to,";
549
		$sql .= "email_tocc,";
550
		$sql .= "email_tobcc,";
551
		$sql .= "email_subject,";
552
		$sql .= "errors_to,";
553
		$sql .= "recurid,";
554
		$sql .= "recurrule,";
555
		$sql .= "recurdateend,";
556
		$sql .= "num_vote,";
557
		$sql .= "event_paid,";
558
		$sql .= "status,";
559
		$sql .= "ip";
560
		$sql .= ") VALUES (";
561
		$sql .= "'(PROV)', ";
562
		$sql .= "'".$this->db->idate($now)."', ";
563
		$sql .= (strval($this->datep) != '' ? "'".$this->db->idate($this->datep)."'" : "null").", ";
564
		$sql .= (strval($this->datef) != '' ? "'".$this->db->idate($this->datef)."'" : "null").", ";
565
		$sql .= ((isset($this->durationp) && $this->durationp >= 0 && $this->durationp != '') ? "'".$this->db->escape($this->durationp)."'" : "null").", "; // deprecated
566
		$sql .= (isset($this->type_id) ? $this->type_id : "null").",";
567
		$sql .= ($code ? ("'".$this->db->escape($code)."'") : "null").", ";
568
		$sql .= (!empty($this->ref_ext) ? "'".$this->db->escape($this->ref_ext)."'" : "null").", ";
569
		$sql .= ((isset($this->socid) && $this->socid > 0) ? ((int) $this->socid) : "null").", ";
570
		$sql .= ((isset($this->fk_project) && $this->fk_project > 0) ? ((int) $this->fk_project) : "null").", ";
571
		$sql .= " '".$this->db->escape($this->note_private)."', ";
572
		$sql .= ((isset($this->contact_id) && $this->contact_id > 0) ? ((int) $this->contact_id) : "null").", "; // deprecated, use ->socpeopleassigned
573
		$sql .= (isset($user->id) && $user->id > 0 ? $user->id : "null").", ";
574
		$sql .= ($userownerid > 0 ? $userownerid : "null").", ";
575
		$sql .= ($userdoneid > 0 ? $userdoneid : "null").", ";
576
		$sql .= "'".$this->db->escape($this->label)."', ";
577
		$sql .= "'".$this->db->escape($this->percentage)."', ";
578
		$sql .= "'".$this->db->escape($this->priority)."', ";
579
		$sql .= "'".$this->db->escape($this->fulldayevent)."', ";
580
		$sql .= "'".$this->db->escape($this->location)."', ";
581
		$sql .= "'".$this->db->escape($this->transparency)."', ";
582
		$sql .= (!empty($this->fk_element) ? ((int) $this->fk_element) : "null").", ";
583
		$sql .= (!empty($this->elementtype) ? "'".$this->db->escape($this->elementtype)."'" : "null").", ";
584
		$sql .= ((int) $conf->entity).",";
585
		$sql .= (!empty($this->extraparams) ? "'".$this->db->escape($this->extraparams)."'" : "null").", ";
586
		// Fields emails
587
		$sql .= (!empty($this->email_msgid) ? "'".$this->db->escape($this->email_msgid)."'" : "null").", ";
588
		$sql .= (!empty($this->email_from) ? "'".$this->db->escape($this->email_from)."'" : "null").", ";
589
		$sql .= (!empty($this->email_sender) ? "'".$this->db->escape($this->email_sender)."'" : "null").", ";
590
		$sql .= (!empty($this->email_to) ? "'".$this->db->escape($this->email_to)."'" : "null").", ";
591
		$sql .= (!empty($this->email_tocc) ? "'".$this->db->escape($this->email_tocc)."'" : "null").", ";
592
		$sql .= (!empty($this->email_tobcc) ? "'".$this->db->escape($this->email_tobcc)."'" : "null").", ";
593
		$sql .= (!empty($this->email_subject) ? "'".$this->db->escape($this->email_subject)."'" : "null").", ";
594
		$sql .= (!empty($this->errors_to) ? "'".$this->db->escape($this->errors_to)."'" : "null").", ";
595
		$sql .= (!empty($this->recurid) ? "'".$this->db->escape($this->recurid)."'" : "null").", ";
596
		$sql .= (!empty($this->recurrule) ? "'".$this->db->escape($this->recurrule)."'" : "null").", ";
597
		$sql .= (!empty($this->recurdateend) ? "'".$this->db->idate($this->recurdateend)."'" : "null").", ";
598
		$sql .= (!empty($this->num_vote) ? (int) $this->num_vote : "null").", ";
599
		$sql .= (!empty($this->event_paid) ? (int) $this->event_paid : 0).", ";
600
		$sql .= (!empty($this->status) ? (int) $this->status : "0").", ";
601
		$sql .= (!empty($this->ip) ? "'".$this->db->escape($this->ip)."'" : "null");
0 ignored issues
show
Bug Best Practice introduced by
The property ip does not exist on ActionComm. Did you maybe forget to declare it?
Loading history...
602
		$sql .= ")";
603
604
		dol_syslog(get_class($this)."::add", LOG_DEBUG);
605
		$resql = $this->db->query($sql);
606
		if ($resql) {
607
			$this->ref = $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."actioncomm", "id");
608
			$sql = "UPDATE ".MAIN_DB_PREFIX."actioncomm SET ref='".$this->db->escape($this->ref)."' WHERE id=".$this->id;
609
			$resql = $this->db->query($sql);
610
			if (!$resql) {
611
				$error++;
612
				dol_syslog('Error to process ref: '.$this->db->lasterror(), LOG_ERR);
613
				$this->errors[] = $this->db->lasterror();
614
			}
615
			// Now insert assigned users
616
			if (!$error) {
617
				//dol_syslog(var_export($this->userassigned, true));
618
				$already_inserted = array();
619
				foreach ($this->userassigned as $key => $val) {
620
					// Common value with new behavior is to have $val = array('id'=>iduser, 'transparency'=>0|1) and $this->userassigned is an array of iduser => $val.
621
					if (!is_array($val)) {	// For backward compatibility when $val='id'.
622
						$val = array('id'=>$val);
623
					}
624
625
					if ($val['id'] > 0) {
626
						if (!empty($already_inserted[$val['id']])) {
627
							continue;
628
						}
629
630
						$sql = "INSERT INTO ".MAIN_DB_PREFIX."actioncomm_resources(fk_actioncomm, element_type, fk_element, mandatory, transparency, answer_status)";
631
						$sql .= " VALUES(".((int) $this->id).", 'user', ".((int) $val['id']).", ".(empty($val['mandatory']) ? '0' : ((int) $val['mandatory'])).", ".(empty($val['transparency']) ? '0' : ((int) $val['transparency'])).", ".(empty($val['answer_status']) ? '0' : ((int) $val['answer_status'])).")";
632
633
						$resql = $this->db->query($sql);
634
						if (!$resql) {
635
							$error++;
636
							dol_syslog('Error to process userassigned: ' . $this->db->lasterror(), LOG_ERR);
637
							$this->errors[] = $this->db->lasterror();
638
						} else {
639
							$already_inserted[$val['id']] = true;
640
						}
641
						//var_dump($sql);exit;
642
					}
643
				}
644
			}
645
646
			if (!$error) {
647
				if (!empty($this->socpeopleassigned)) {
648
					$already_inserted = array();
649
					foreach ($this->socpeopleassigned as $id => $val) {
650
						// Common value with new behavior is to have $val = iduser and $this->socpeopleassigned is an array of iduser => $val.
651
						if (!empty($already_inserted[$id])) {
652
							continue;
653
						}
654
655
						$sql = "INSERT INTO ".MAIN_DB_PREFIX."actioncomm_resources(fk_actioncomm, element_type, fk_element, mandatory, transparency, answer_status)";
656
						$sql .= " VALUES(".((int) $this->id).", 'socpeople', ".((int) $id).", 0, 0, 0)";
657
658
						$resql = $this->db->query($sql);
659
						if (!$resql) {
660
							$error++;
661
							dol_syslog('Error to process socpeopleassigned: ' . $this->db->lasterror(), LOG_ERR);
662
							$this->errors[] = $this->db->lasterror();
663
						} else {
664
							$already_inserted[$id] = true;
665
						}
666
					}
667
				}
668
			}
669
670
			if (!$error) {
671
				// Actions on extra fields
672
				$result = $this->insertExtraFields();
673
				if ($result < 0) {
674
					$error++;
675
				}
676
			}
677
678
			if (!$error && !$notrigger) {
679
				// Call trigger
680
				$result = $this->call_trigger('ACTION_CREATE', $user);
681
				if ($result < 0) {
682
					$error++;
683
				}
684
				// End call triggers
685
			}
686
687
			if (!$error) {
688
				$this->db->commit();
689
				return $this->id;
690
			} else {
691
				$this->db->rollback();
692
				return -1;
693
			}
694
		} else {
695
			$this->db->rollback();
696
			$this->error = $this->db->lasterror();
697
			return -1;
698
		}
699
	}
700
701
	/**
702
	 *  Load an object from its id and create a new one in database
703
	 *
704
	 *  @param	    User	        $fuser      	Object user making action
705
	 *  @param		int				$socid			Id of thirdparty
706
	 *  @return		int								New id of clone
707
	 */
708
	public function createFromClone(User $fuser, $socid)
709
	{
710
		global $hookmanager;
711
712
		$error = 0;
713
714
		$this->db->begin();
715
716
		// Load source object
717
		$objFrom = clone $this;
718
719
		// Retrieve all extrafield
720
		// fetch optionals attributes and labels
721
		$this->fetch_optionals();
722
723
		//$this->fetch_userassigned();
724
		$this->fetchResources();
725
726
		$this->id = 0;
727
		$this->recurid = '';
728
		$this->recurrule = '';
729
		$this->recurdateend = '';
730
731
		// Create clone
732
		$this->context['createfromclone'] = 'createfromclone';
733
		$result = $this->create($fuser);
734
		if ($result < 0) {
735
			$error++;
736
		}
737
738
		if (!$error) {
739
			// Hook of thirdparty module
740
			if (is_object($hookmanager)) {
741
				$parameters = array('objFrom'=>$objFrom);
742
				$action = '';
743
				$reshook = $hookmanager->executeHooks('createFrom', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
744
				if ($reshook < 0) {
745
					$this->errors += $hookmanager->errors;
746
					$this->error = $hookmanager->error;
747
					$error++;
748
				}
749
			}
750
751
			// Call trigger
752
			$result = $this->call_trigger('ACTION_CLONE', $fuser);
753
			if ($result < 0) {
754
				$error++;
755
			}
756
			// End call triggers
757
		}
758
759
		unset($this->context['createfromclone']);
760
761
		// End
762
		if (!$error) {
763
			$this->db->commit();
764
			return $this->id;
765
		} else {
766
			$this->db->rollback();
767
			return -1;
768
		}
769
	}
770
771
	/**
772
	 *  Load object from database
773
	 *
774
	 *  @param  int		$id     			Id of action to get
775
	 *  @param  string	$ref    			Ref of action to get
776
	 *  @param  string	$ref_ext			Ref ext to get
777
	 *  @param	string	$email_msgid		Email msgid
778
	 *  @param	string	$loadresources		1=Load also resources
779
	 *  @return	int							<0 if KO, >0 if OK
780
	 */
781
	public function fetch($id, $ref = '', $ref_ext = '', $email_msgid = '', $loadresources = 1)
782
	{
783
		global $langs;
784
785
		if (empty($id) && empty($ref) && empty($ref_ext) && empty($email_msgid)) {
786
			dol_syslog(get_class($this)."::fetch Bad parameters", LOG_WARNING);
787
			return -1;
788
		}
789
790
		$sql = "SELECT a.id,";
791
		$sql .= " a.ref as ref,";
792
		$sql .= " a.entity,";
793
		$sql .= " a.ref_ext,";
794
		$sql .= " a.datep,";
795
		$sql .= " a.datep2,";
796
		$sql .= " a.durationp,"; // deprecated
797
		$sql .= " a.datec,";
798
		$sql .= " a.tms as datem,";
799
		$sql .= " a.code, a.label, a.note as note_private,";
800
		$sql .= " a.fk_soc,";
801
		$sql .= " a.fk_project,";
802
		$sql .= " a.fk_user_author, a.fk_user_mod,";
803
		$sql .= " a.fk_user_action, a.fk_user_done,";
804
		$sql .= " a.fk_contact, a.percent as percentage,";
805
		$sql .= " a.fk_element as elementid, a.elementtype,";
806
		$sql .= " a.priority, a.fulldayevent, a.location, a.transparency,";
807
		$sql .= " a.email_msgid, a.email_subject, a.email_from, a.email_sender, a.email_to, a.email_tocc, a.email_tobcc, a.errors_to,";
808
		$sql .= " c.id as type_id, c.type as type_type, c.code as type_code, c.libelle as type_label, c.color as type_color, c.picto as type_picto,";
809
		$sql .= " s.nom as socname,";
810
		$sql .= " u.firstname, u.lastname as lastname,";
811
		$sql .= " num_vote, event_paid, a.status";
812
		$sql .= " FROM ".MAIN_DB_PREFIX."actioncomm as a ";
813
		$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_actioncomm as c ON a.fk_action=c.id ";
814
		$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as u on u.rowid = a.fk_user_author";
815
		$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s on s.rowid = a.fk_soc";
816
		$sql .= " WHERE ";
817
		if ($ref) {
818
			$sql .= " a.ref = '".$this->db->escape($ref)."'";
819
		} elseif ($ref_ext) {
820
			$sql .= " a.ref_ext = '".$this->db->escape($ref_ext)."'";
821
		} elseif ($email_msgid) {
822
			$sql .= " a.email_msgid = '".$this->db->escape($email_msgid)."'";
823
		} else {
824
			$sql .= " a.id = ".((int) $id);
825
		}
826
827
		dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
828
		$resql = $this->db->query($sql);
829
		if ($resql) {
830
			$num = $this->db->num_rows($resql);
831
			if ($num) {
832
				$obj = $this->db->fetch_object($resql);
833
834
				$this->id         = $obj->id;
835
				$this->entity = $obj->entity;
836
				$this->ref        = $obj->ref;
837
				$this->ref_ext    = $obj->ref_ext;
838
839
				// Properties of parent table llx_c_actioncomm
840
				$this->type_id    = $obj->type_id;
841
				$this->type_code  = $obj->type_code;
842
				$this->type_color = $obj->type_color;
843
				$this->type_picto = $obj->type_picto;
844
				$this->type       = $obj->type_type;
845
				/*$transcode = $langs->trans("Action".$obj->type_code);
846
				$this->type       = (($transcode != "Action".$obj->type_code) ? $transcode : $obj->type_label); */
847
				$transcode = $langs->trans("Action".$obj->type_code.'Short');
848
				$this->type_short = (($transcode != "Action".$obj->type_code.'Short') ? $transcode : '');
849
850
				$this->code = $obj->code;
851
				$this->label = $obj->label;
852
				$this->datep = $this->db->jdate($obj->datep);
853
				$this->datef = $this->db->jdate($obj->datep2);
854
855
				$this->datec = $this->db->jdate($obj->datec);
856
				$this->datem = $this->db->jdate($obj->datem);
857
858
				$this->note = $obj->note_private; // deprecated
859
				$this->note_private = $obj->note_private;
860
				$this->percentage = $obj->percentage;
861
862
				$this->authorid = $obj->fk_user_author;
863
				$this->usermodid = $obj->fk_user_mod;
864
865
				if (!is_object($this->author)) {
866
					$this->author = new User($this->db); // To avoid warning
867
				}
868
				$this->author->id = $obj->fk_user_author; // deprecated
869
				$this->author->firstname = $obj->firstname; // deprecated
870
				$this->author->lastname = $obj->lastname; // deprecated
871
				if (!is_object($this->usermod)) {
872
					$this->usermod = new User($this->db); // To avoid warning
873
				}
874
				$this->usermod->id = $obj->fk_user_mod; // deprecated
875
876
				$this->userownerid = $obj->fk_user_action;
877
				$this->priority				= $obj->priority;
878
				$this->fulldayevent			= $obj->fulldayevent;
879
				$this->location				= $obj->location;
880
				$this->transparency			= $obj->transparency;
881
882
				$this->socid = $obj->fk_soc; // To have fetch_thirdparty method working
883
				$this->contact_id = $obj->fk_contact; // To have fetch_contact method working
884
				$this->fk_project = $obj->fk_project; // To have fetch_projet method working
885
886
				//$this->societe->id			= $obj->fk_soc;			// deprecated
887
				//$this->contact->id			= $obj->fk_contact;		// deprecated
888
889
				$this->fk_element = $obj->elementid;
890
				$this->elementid = $obj->elementid;
891
				$this->elementtype = $obj->elementtype;
892
893
				$this->num_vote = $obj->num_vote;
894
				$this->event_paid = $obj->event_paid;
895
				$this->status = $obj->status;
896
897
				//email information
898
				$this->email_msgid=$obj->email_msgid;
899
				$this->email_from=$obj->email_from;
900
				$this->email_sender=$obj->email_sender;
901
				$this->email_to=$obj->email_to;
902
				$this->email_tocc=$obj->email_tocc;
903
				$this->email_tobcc=$obj->email_tobcc;
904
				$this->email_subject=$obj->email_subject;
905
				$this->errors_to=$obj->errors_to;
906
907
				$this->fetch_optionals();
908
909
				if ($loadresources) {
910
					$this->fetchResources();
911
				}
912
			}
913
914
			$this->db->free($resql);
915
		} else {
916
			$this->error = $this->db->lasterror();
917
			return -1;
918
		}
919
920
		return $num;
921
	}
922
923
	/**
924
	 *    Initialize $this->userassigned & this->socpeopleassigned array with list of id of user and contact assigned to event
925
	 *
926
	 *    @return   int				<0 if KO, >0 if OK
927
	 */
928
	public function fetchResources()
929
	{
930
		$this->userassigned = array();
931
		$this->socpeopleassigned = array();
932
933
		$sql = 'SELECT fk_actioncomm, element_type, fk_element, answer_status, mandatory, transparency';
934
		$sql .= ' FROM '.MAIN_DB_PREFIX.'actioncomm_resources';
935
		$sql .= ' WHERE fk_actioncomm = '.((int) $this->id);
936
		$sql .= " AND element_type IN ('user', 'socpeople')";
937
		$resql = $this->db->query($sql);
938
		if ($resql) {
939
			// If owner is known, we must but id first into list
940
			if ($this->userownerid > 0) {
941
				$this->userassigned[$this->userownerid] = array('id'=>$this->userownerid); // Set first so will be first into list.
942
			}
943
944
			while ($obj = $this->db->fetch_object($resql)) {
945
				if ($obj->fk_element > 0) {
946
					switch ($obj->element_type) {
947
						case 'user':
948
							$this->userassigned[$obj->fk_element] = array('id'=>$obj->fk_element, 'mandatory'=>$obj->mandatory, 'answer_status'=>$obj->answer_status, 'transparency'=>$obj->transparency);
949
							if (empty($this->userownerid)) {
950
								$this->userownerid = $obj->fk_element; // If not defined (should not happened, we fix this)
951
							}
952
							break;
953
						case 'socpeople':
954
							$this->socpeopleassigned[$obj->fk_element] = array('id'=>$obj->fk_element, 'mandatory'=>$obj->mandatory, 'answer_status'=>$obj->answer_status, 'transparency'=>$obj->transparency);
955
							break;
956
					}
957
				}
958
			}
959
960
			return 1;
961
		} else {
962
			dol_print_error($this->db);
963
			return -1;
964
		}
965
	}
966
967
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
968
	/**
969
	 *    Initialize this->userassigned array with list of id of user assigned to event
970
	 *
971
	 *    @param    bool    $override   Override $this->userownerid when empty. TODO This should be false by default. True is here to fix corrupted data.
972
	 *    @return   int                 <0 if KO, >0 if OK
973
	 */
974
	public function fetch_userassigned($override = true)
975
	{
976
		// phpcs:enable
977
		$sql = "SELECT fk_actioncomm, element_type, fk_element, answer_status, mandatory, transparency";
978
		$sql .= " FROM ".MAIN_DB_PREFIX."actioncomm_resources";
979
		$sql .= " WHERE element_type = 'user' AND fk_actioncomm = ".((int) $this->id);
980
981
		$resql2 = $this->db->query($sql);
982
		if ($resql2) {
983
			$this->userassigned = array();
984
985
			// If owner is known, we must but id first into list
986
			if ($this->userownerid > 0) {
987
				// Set first so will be first into list.
988
				$this->userassigned[$this->userownerid] = array('id'=>$this->userownerid);
989
			}
990
991
			while ($obj = $this->db->fetch_object($resql2)) {
992
				if ($obj->fk_element > 0) {
993
					$this->userassigned[$obj->fk_element] = array('id'=>$obj->fk_element,
994
																  'mandatory'=>$obj->mandatory,
995
																  'answer_status'=>$obj->answer_status,
996
																  'transparency'=>$obj->transparency);
997
				}
998
999
				if ($override === true) {
1000
					// If not defined (should not happened, we fix this)
1001
					if (empty($this->userownerid)) {
1002
						$this->userownerid = $obj->fk_element;
1003
					}
1004
				}
1005
			}
1006
1007
			return 1;
1008
		} else {
1009
			dol_print_error($this->db);
1010
			return -1;
1011
		}
1012
	}
1013
1014
	/**
1015
	 *    Delete event from database
1016
	 *
1017
	 *    @param    int		$notrigger		1 = disable triggers, 0 = enable triggers
1018
	 *    @return   int 					<0 if KO, >0 if OK
1019
	 */
1020
	public function delete($notrigger = 0)
1021
	{
1022
		global $user;
1023
1024
		$error = 0;
1025
1026
		dol_syslog(get_class($this)."::delete", LOG_DEBUG);
1027
1028
		$this->db->begin();
1029
1030
		// remove categorie association
1031
		if (!$error) {
1032
			$sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_actioncomm";
1033
			$sql .= " WHERE fk_actioncomm=".((int) $this->id);
1034
1035
			$res = $this->db->query($sql);
1036
			if (!$res) {
1037
				$this->error = $this->db->lasterror();
1038
				$error++;
1039
			}
1040
		}
1041
1042
		// remove actioncomm_resources
1043
		if (!$error) {
1044
			$sql = "DELETE FROM ".MAIN_DB_PREFIX."actioncomm_resources";
1045
			$sql .= " WHERE fk_actioncomm=".((int) $this->id);
1046
1047
			$res = $this->db->query($sql);
1048
			if (!$res) {
1049
				$this->error = $this->db->lasterror();
1050
				$error++;
1051
			}
1052
		}
1053
1054
		if (!$error) {
1055
			$sql = "DELETE FROM ".MAIN_DB_PREFIX."actioncomm_reminder";
1056
			$sql .= " WHERE fk_actioncomm = ".((int) $this->id);
1057
1058
			$res = $this->db->query($sql);
1059
			if (!$res) {
1060
				$this->error = $this->db->lasterror();
1061
				$error++;
1062
			}
1063
		}
1064
1065
		// Removed extrafields
1066
		if (!$error) {
1067
			  $result = $this->deleteExtraFields();
1068
			if ($result < 0) {
1069
				$error++;
1070
				dol_syslog(get_class($this)."::delete error -3 ".$this->error, LOG_ERR);
1071
			}
1072
		}
1073
1074
		// remove actioncomm
1075
		if (!$error) {
1076
			$sql = "DELETE FROM ".MAIN_DB_PREFIX."actioncomm";
1077
			$sql .= " WHERE id=".((int) $this->id);
1078
1079
			$res = $this->db->query($sql);
1080
			if (!$res) {
1081
				$this->error = $this->db->lasterror();
1082
				$error++;
1083
			}
1084
		}
1085
1086
		if (!$error) {
1087
			if (!$notrigger) {
1088
				// Call trigger
1089
				$result = $this->call_trigger('ACTION_DELETE', $user);
1090
				if ($result < 0) {
1091
					$error++;
1092
				}
1093
				// End call triggers
1094
			}
1095
1096
			if (!$error) {
1097
				$this->db->commit();
1098
				return 1;
1099
			} else {
1100
				$this->db->rollback();
1101
				return -2;
1102
			}
1103
		} else {
1104
			$this->db->rollback();
1105
			$this->error = $this->db->lasterror();
1106
			return -1;
1107
		}
1108
	}
1109
1110
	/**
1111
	 *    Update action into database
1112
	 *	  If percentage = 100, on met a jour date 100%
1113
	 *
1114
	 *    @param    User	$user			Object user making change
1115
	 *    @param    int		$notrigger		1 = disable triggers, 0 = enable triggers
1116
	 *    @return   int     				<0 if KO, >0 if OK
1117
	 */
1118
	public function update(User $user, $notrigger = 0)
1119
	{
1120
		global $langs, $conf, $hookmanager;
1121
1122
		$error = 0;
1123
1124
		// Clean parameters
1125
		$this->label = trim($this->label);
1126
		$this->note_private = dol_htmlcleanlastbr(trim(!isset($this->note_private) ? $this->note : $this->note_private));
1127
		if (empty($this->percentage)) {
1128
			$this->percentage = 0;
1129
		}
1130
		if (empty($this->priority) || !is_numeric($this->priority)) {
1131
			$this->priority = 0;
1132
		}
1133
		if (empty($this->transparency)) {
1134
			$this->transparency = 0;
1135
		}
1136
		if (empty($this->fulldayevent)) {
1137
			$this->fulldayevent = 0;
1138
		}
1139
		if ($this->percentage > 100) {
1140
			$this->percentage = 100;
1141
		}
1142
		//if ($this->percentage == 100 && ! $this->dateend) $this->dateend = $this->date;
1143
		if ($this->datep && $this->datef) {
1144
			$this->durationp = ($this->datef - $this->datep); // deprecated
1145
		}
1146
		//if ($this->date  && $this->dateend) $this->durationa=($this->dateend - $this->date);
1147
		if ($this->datep && $this->datef && $this->datep > $this->datef) {
1148
			$this->datef = $this->datep;
1149
		}
1150
		//if ($this->date  && $this->dateend && $this->date > $this->dateend) $this->dateend=$this->date;
1151
		if ($this->fk_project < 0) {
1152
			$this->fk_project = 0;
1153
		}
1154
1155
		// Check parameters
1156
		if ($this->percentage == 0 && $this->userdoneid > 0) {
1157
			$this->error = "ErrorCantSaveADoneUserWithZeroPercentage";
1158
			return -1;
1159
		}
1160
1161
		$socid = (($this->socid > 0) ? $this->socid : 0);
1162
		$contactid = (($this->contact_id > 0) ? $this->contact_id : 0);
1163
		$userownerid = ($this->userownerid ? $this->userownerid : 0);
1164
		$userdoneid = ($this->userdoneid ? $this->userdoneid : 0);
1165
1166
		// If a type_id is set, we must also have the type_code set
1167
		if ($this->type_id > 0) {
1168
			if (empty($this->type_code)) {
1169
				$cactioncomm = new CActionComm($this->db);
1170
				$result = $cactioncomm->fetch($this->type_id);
1171
				if ($result >= 0 && !empty($cactioncomm->code)) {
1172
					$this->type_code = $cactioncomm->code;
1173
				}
1174
			}
1175
		}
1176
1177
		$code = $this->code;
1178
		if (empty($code) || (!empty($this->oldcopy) && $this->oldcopy->type_code != $this->type_code)) {	// If code unknown or if we change the type, we reset $code too
1179
			$code = $this->type_code;
1180
		}
1181
1182
		$this->db->begin();
1183
1184
		$sql = "UPDATE ".MAIN_DB_PREFIX."actioncomm";
1185
		$sql .= " SET percent = '".$this->db->escape($this->percentage)."'";
1186
		$sql .= ", fk_action = ".(int) $this->type_id;
1187
		$sql .= ", code = " . ($code ? "'".$this->db->escape($code)."'" : "null");
1188
		$sql .= ", label = ".($this->label ? "'".$this->db->escape($this->label)."'" : "null");
1189
		$sql .= ", datep = ".(strval($this->datep) != '' ? "'".$this->db->idate($this->datep)."'" : 'null');
1190
		$sql .= ", datep2 = ".(strval($this->datef) != '' ? "'".$this->db->idate($this->datef)."'" : 'null');
1191
		$sql .= ", durationp = ".(isset($this->durationp) && $this->durationp >= 0 && $this->durationp != '' ? "'".$this->db->escape($this->durationp)."'" : "null"); // deprecated
1192
		$sql .= ", note = '".$this->db->escape($this->note_private)."'";
1193
		$sql .= ", fk_project =".($this->fk_project > 0 ? ((int) $this->fk_project) : "null");
1194
		$sql .= ", fk_soc =".($socid > 0 ? ((int) $socid) : "null");
1195
		$sql .= ", fk_contact =".($contactid > 0 ? ((int) $contactid) : "null");
1196
		$sql .= ", priority = '".$this->db->escape($this->priority)."'";
1197
		$sql .= ", fulldayevent = '".$this->db->escape($this->fulldayevent)."'";
1198
		$sql .= ", location = ".($this->location ? "'".$this->db->escape($this->location)."'" : "null");
1199
		$sql .= ", transparency = '".$this->db->escape($this->transparency)."'";
1200
		$sql .= ", fk_user_mod = ".((int) $user->id);
1201
		$sql .= ", fk_user_action = ".($userownerid > 0 ? ((int) $userownerid) : "null");
1202
		$sql .= ", fk_user_done = ".($userdoneid > 0 ? ((int) $userdoneid) : "null");
1203
		if (!empty($this->fk_element)) {
1204
			$sql .= ", fk_element=".($this->fk_element ? ((int) $this->fk_element) : "null");
1205
		}
1206
		if (!empty($this->elementtype)) {
1207
			$sql .= ", elementtype=".($this->elementtype ? "'".$this->db->escape($this->elementtype)."'" : "null");
1208
		}
1209
		if (!empty($this->num_vote)) {
1210
			$sql .= ", num_vote=".($this->num_vote ? (int) $this->num_vote : null);
1211
		}
1212
		if (!empty($this->event_paid)) {
1213
			$sql .= ", event_paid=".($this->event_paid ? (int) $this->event_paid : 0);
1214
		}
1215
		if (!empty($this->status)) {
1216
			$sql .= ", status=".($this->status ? (int) $this->status : 0);
1217
		}
1218
		$sql .= " WHERE id=".((int) $this->id);
1219
1220
		dol_syslog(get_class($this)."::update", LOG_DEBUG);
1221
		if ($this->db->query($sql)) {
1222
			$action = 'update';
1223
1224
			// Actions on extra fields
1225
			if (!$error) {
1226
				$result = $this->insertExtraFields();
1227
				if ($result < 0) {
1228
					$error++;
1229
				}
1230
			}
1231
1232
			// Now insert assignedusers
1233
			if (!$error) {
1234
				$sql = "DELETE FROM ".MAIN_DB_PREFIX."actioncomm_resources where fk_actioncomm = ".((int) $this->id)." AND element_type = 'user'";
1235
				$resql = $this->db->query($sql);
1236
1237
				$already_inserted = array();
1238
				foreach ($this->userassigned as $key => $val) {
1239
					if (!is_array($val)) {	// For backward compatibility when val=id
1240
						$val = array('id'=>$val);
1241
					}
1242
					if (!empty($already_inserted[$val['id']])) continue;
1243
1244
					$sql = "INSERT INTO ".MAIN_DB_PREFIX."actioncomm_resources(fk_actioncomm, element_type, fk_element, mandatory, transparency, answer_status)";
1245
					$sql .= " VALUES(".((int) $this->id).", 'user', ".((int) $val['id']).", ".(empty($val['mandatory']) ? '0' : ((int) $val['mandatory'])).", ".(empty($val['transparency']) ? '0' : ((int) $val['transparency'])).", ".(empty($val['answer_status']) ? '0' : ((int) $val['answer_status'])).")";
1246
1247
					$resql = $this->db->query($sql);
1248
					if (!$resql) {
1249
						$error++;
1250
						$this->errors[] = $this->db->lasterror();
1251
					} else {
1252
						$already_inserted[$val['id']] = true;
1253
					}
1254
					//var_dump($sql);exit;
1255
				}
1256
			}
1257
1258
			if (!$error) {
1259
				$sql = "DELETE FROM ".MAIN_DB_PREFIX."actioncomm_resources where fk_actioncomm = ".((int) $this->id)." AND element_type = 'socpeople'";
1260
				$resql = $this->db->query($sql);
1261
1262
				if (!empty($this->socpeopleassigned)) {
1263
					$already_inserted = array();
1264
					foreach (array_keys($this->socpeopleassigned) as $key => $val) {
1265
						if (!is_array($val)) {	// For backward compatibility when val=id
1266
							$val = array('id'=>$val);
1267
						}
1268
						if (!empty($already_inserted[$val['id']])) continue;
1269
1270
						$sql = "INSERT INTO ".MAIN_DB_PREFIX."actioncomm_resources(fk_actioncomm, element_type, fk_element, mandatory, transparency, answer_status)";
1271
						$sql .= " VALUES(".((int) $this->id).", 'socpeople', ".((int) $val['id']).", 0, 0, 0)";
1272
1273
						$resql = $this->db->query($sql);
1274
						if (!$resql) {
1275
							$error++;
1276
							$this->errors[] = $this->db->lasterror();
1277
						} else {
1278
							$already_inserted[$val['id']] = true;
1279
						}
1280
					}
1281
				}
1282
			}
1283
1284
			if (!$error && !$notrigger) {
1285
				// Call trigger
1286
				$result = $this->call_trigger('ACTION_MODIFY', $user);
1287
				if ($result < 0) {
1288
					$error++;
1289
				}
1290
				// End call triggers
1291
			}
1292
1293
			if (!$error) {
1294
				$this->db->commit();
1295
				return 1;
1296
			} else {
1297
				$this->db->rollback();
1298
				dol_syslog(get_class($this)."::update ".join(',', $this->errors), LOG_ERR);
1299
				return -2;
1300
			}
1301
		} else {
1302
			$this->db->rollback();
1303
			$this->error = $this->db->lasterror();
1304
			return -1;
1305
		}
1306
	}
1307
1308
	/**
1309
	 *  Load all objects with filters.
1310
	 *  @todo WARNING: This make a fetch on all records instead of making one request with a join.
1311
	 *
1312
	 *  @param		int		$socid			Filter by thirdparty
1313
	 *  @param		int		$fk_element		Id of element action is linked to
1314
	 *  @param		string	$elementtype	Type of element action is linked to
1315
	 *  @param		string	$filter			Other filter
1316
	 *  @param		string	$sortfield		Sort on this field
1317
	 *  @param		string	$sortorder		ASC or DESC
1318
	 *  @param		string	$limit			Limit number of answers
1319
	 *  @return		array|string			Error string if KO, array with actions if OK
1320
	 */
1321
	public function getActions($socid = 0, $fk_element = 0, $elementtype = '', $filter = '', $sortfield = 'a.datep', $sortorder = 'DESC', $limit = 0)
1322
	{
1323
		global $conf, $langs;
1324
1325
		$resarray = array();
1326
1327
		dol_syslog(get_class()."::getActions", LOG_DEBUG);
1328
1329
		require_once DOL_DOCUMENT_ROOT . '/core/class/hookmanager.class.php';
1330
		$hookmanager = new HookManager($this->db);
1331
		// Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
1332
		$hookmanager->initHooks(array('agendadao'));
1333
1334
		$sql = "SELECT a.id";
1335
		$sql .= " FROM ".MAIN_DB_PREFIX."actioncomm as a";
1336
		// Fields from hook
1337
		$parameters = array('sql' => &$sql, 'socid' => $socid, 'fk_element' => $fk_element, 'elementtype' => $elementtype);
1338
		$reshook = $hookmanager->executeHooks('getActionsListFrom', $parameters);    // Note that $action and $object may have been modified by hook
1339
		if (!empty($hookmanager->resPrint)) $sql.= $hookmanager->resPrint;
1340
		$sql .= " WHERE a.entity IN (".getEntity('agenda').")";
1341
		if (!empty($socid)) {
1342
			$sql .= " AND a.fk_soc = ".((int) $socid);
1343
		}
1344
		if (!empty($elementtype)) {
1345
			if ($elementtype == 'project') {
1346
				$sql .= ' AND a.fk_project = '.((int) $fk_element);
1347
			} elseif ($elementtype == 'contact') {
1348
				$sql .= ' AND a.id IN';
1349
				$sql .= " (SELECT fk_actioncomm FROM ".MAIN_DB_PREFIX."actioncomm_resources WHERE";
1350
				$sql .= " element_type = 'socpeople' AND fk_element = ".((int) $fk_element).')';
1351
			} else {
1352
				$sql .= " AND a.fk_element = ".((int) $fk_element)." AND a.elementtype = '".$this->db->escape($elementtype)."'";
1353
			}
1354
		}
1355
		if (!empty($filter)) {
1356
			$sql .= $filter;
1357
		}
1358
		// Fields where hook
1359
		$parameters = array('sql' => &$sql, 'socid' => $socid, 'fk_element' => $fk_element, 'elementtype' => $elementtype);
1360
		$reshook = $hookmanager->executeHooks('getActionsListWhere', $parameters);    // Note that $action and $object may have been modified by hook
1361
		if (!empty($hookmanager->resPrint)) $sql.= $hookmanager->resPrint;
1362
		if ($sortorder && $sortfield) {
1363
			$sql .= $this->db->order($sortfield, $sortorder);
1364
		}
1365
		$sql .= $this->db->plimit($limit, 0);
1366
1367
		$resql = $this->db->query($sql);
1368
		if ($resql) {
1369
			$num = $this->db->num_rows($resql);
1370
1371
			if ($num) {
1372
				for ($i = 0; $i < $num; $i++) {
1373
					$obj = $this->db->fetch_object($resql);
1374
					$actioncommstatic = new ActionComm($this->db);
1375
					$actioncommstatic->fetch($obj->id);
1376
					$resarray[$i] = $actioncommstatic;
1377
				}
1378
			}
1379
			$this->db->free($resql);
1380
			return $resarray;
1381
		} else {
1382
			return $this->db->lasterror();
1383
		}
1384
	}
1385
1386
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1387
	/**
1388
	 * Load indicators for dashboard (this->nbtodo and this->nbtodolate)
1389
	 *
1390
	 * @param	User	$user   			Objet user
1391
	 * @param	int		$load_state_board	Load indicator array this->nb
1392
	 * @return WorkboardResponse|int 		<0 if KO, WorkboardResponse if OK
1393
	 */
1394
	public function load_board($user, $load_state_board = 0)
1395
	{
1396
		// phpcs:enable
1397
		global $conf, $langs;
1398
1399
		if (empty($load_state_board)) {
1400
			$sql = "SELECT a.id, a.datep as dp";
1401
		} else {
1402
			$this->nb = array();
1403
			$sql = "SELECT count(a.id) as nb";
1404
		}
1405
		$sql .= " FROM ".MAIN_DB_PREFIX."actioncomm as a";
1406
		if (empty($user->rights->societe->client->voir) && !$user->socid) {
1407
			$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON a.fk_soc = sc.fk_soc";
1408
		}
1409
		if (empty($user->rights->agenda->allactions->read)) {
1410
			$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."actioncomm_resources AS ar ON a.id = ar.fk_actioncomm AND ar.element_type ='user' AND ar.fk_element = ".((int) $user->id);
1411
		}
1412
		$sql .= " WHERE 1 = 1";
1413
		if (empty($load_state_board)) {
1414
			$sql .= " AND a.percent >= 0 AND a.percent < 100";
1415
		}
1416
		$sql .= " AND a.entity IN (".getEntity('agenda').")";
1417
		if (empty($user->rights->societe->client->voir) && !$user->socid) {
1418
			$sql .= " AND (a.fk_soc IS NULL OR sc.fk_user = ".((int) $user->id).")";
1419
		}
1420
		if ($user->socid) {
1421
			$sql .= " AND a.fk_soc = ".((int) $user->socid);
1422
		}
1423
		if (empty($user->rights->agenda->allactions->read)) {
1424
			$sql .= " AND (a.fk_user_author = ".((int) $user->id)." OR a.fk_user_action = ".((int) $user->id)." OR a.fk_user_done = ".((int) $user->id);
1425
			$sql .= " OR ar.fk_element = ".((int) $user->id);
1426
			$sql .= ")";
1427
		}
1428
1429
		$resql = $this->db->query($sql);
1430
		if ($resql) {
1431
			if (empty($load_state_board)) {
1432
				$agenda_static = new ActionComm($this->db);
1433
				$response = new WorkboardResponse();
1434
				$response->warning_delay = $conf->agenda->warning_delay / 60 / 60 / 24;
1435
				$response->label = $langs->trans("ActionsToDo");
1436
				$response->labelShort = $langs->trans("ActionsToDoShort");
1437
				$response->url = DOL_URL_ROOT.'/comm/action/list.php?mode=show_list&actioncode=0&status=todo&mainmenu=agenda';
1438
				if ($user->hasRight("agenda", "allactions", "read")) {
1439
					$response->url .= '&filtert=-1';
1440
				}
1441
				$response->img = img_object('', "action", 'class="inline-block valigntextmiddle"');
1442
			}
1443
			// This assignment in condition is not a bug. It allows walking the results.
1444
			while ($obj = $this->db->fetch_object($resql)) {
1445
				if (empty($load_state_board)) {
1446
					$response->nbtodo++;
1447
					$agenda_static->datep = $this->db->jdate($obj->dp);
1448
					if ($agenda_static->hasDelay()) {
1449
						$response->nbtodolate++;
1450
					}
1451
				} else {
1452
					$this->nb["actionscomm"] = $obj->nb;
1453
				}
1454
			}
1455
1456
			$this->db->free($resql);
1457
			if (empty($load_state_board)) {
1458
				return $response;
1459
			} else {
1460
				return 1;
1461
			}
1462
		} else {
1463
			dol_print_error($this->db);
1464
			$this->error = $this->db->error();
1465
			return -1;
1466
		}
1467
	}
1468
1469
1470
	/**
1471
	 *  Charge les informations d'ordre info dans l'objet facture
1472
	 *
1473
	 *  @param	int		$id       	Id de la facture a charger
1474
	 *  @return	void
1475
	 */
1476
	public function info($id)
1477
	{
1478
		$sql = 'SELECT ';
1479
		$sql .= ' a.id,';
1480
		$sql .= ' datec,';
1481
		$sql .= ' tms as datem,';
1482
		$sql .= ' fk_user_author,';
1483
		$sql .= ' fk_user_mod';
1484
		$sql .= ' FROM '.MAIN_DB_PREFIX.'actioncomm as a';
1485
		$sql .= ' WHERE a.id = '.((int) $id);
1486
1487
		dol_syslog(get_class($this)."::info", LOG_DEBUG);
1488
		$result = $this->db->query($sql);
1489
		if ($result) {
1490
			if ($this->db->num_rows($result)) {
1491
				$obj = $this->db->fetch_object($result);
1492
				$this->id = $obj->id;
1493
				$this->user_creation_id = $obj->fk_user_author;
1494
				$this->user_modification_id = $obj->fk_user_mod;
1495
				$this->date_creation     = $this->db->jdate($obj->datec);
1496
				$this->date_modification = empty($obj->datem) ? '' : $this->db->jdate($obj->datem);
1497
			}
1498
			$this->db->free($result);
1499
		} else {
1500
			dol_print_error($this->db);
1501
		}
1502
	}
1503
1504
1505
	/**
1506
	 *  Return the label of the status
1507
	 *
1508
	 *  @param  int		$mode           0=Long label, 1=Short label, 2=Picto+Short label, 3=Picto, 4=Picto+Short label, 5=Short label+Picto, 6=Picto+Long label, 7=Very short label+Picto
1509
	 *  @param  int		$hidenastatus   1=Show nothing if status is "Not applicable"
1510
	 *  @return string          		String with status
1511
	 */
1512
	public function getLibStatut($mode, $hidenastatus = 0)
1513
	{
1514
		return $this->LibStatut($this->percentage, $mode, $hidenastatus, $this->datep);
1515
	}
1516
1517
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1518
	/**
1519
	 *  Return label of action status
1520
	 *
1521
	 *  @param  int     $percent        Percent
1522
	 *  @param  int		$mode           0=Long label, 1=Short label, 2=Picto+Short label, 3=Picto, 4=Picto+Short label, 5=Short label+Picto, 6=Picto+Long label, 7=Very short label+Picto
1523
	 *  @param  int		$hidenastatus   1=Show nothing if status is "Not applicable"
1524
	 *  @param  int     $datestart      Date start of event
1525
	 *  @return string		    		Label
1526
	 */
1527
	public function LibStatut($percent, $mode, $hidenastatus = 0, $datestart = '')
1528
	{
1529
		// phpcs:enable
1530
		global $langs;
1531
1532
		$labelStatus = $langs->transnoentitiesnoconv('StatusNotApplicable');
1533
		if ($percent == -1 && !$hidenastatus) {
1534
			$labelStatus = $langs->transnoentitiesnoconv('StatusNotApplicable');
1535
		} elseif ($percent == 0) {
1536
			$labelStatus = $langs->transnoentitiesnoconv('StatusActionToDo').' (0%)';
1537
		} elseif ($percent > 0 && $percent < 100) {
1538
			$labelStatus = $langs->transnoentitiesnoconv('StatusActionInProcess').' ('.$percent.'%)';
1539
		} elseif ($percent >= 100) {
1540
			$labelStatus = $langs->transnoentitiesnoconv('StatusActionDone').' (100%)';
1541
		}
1542
1543
		$labelStatusShort = $langs->transnoentitiesnoconv('StatusNotApplicable');
1544
		if ($percent == -1 && !$hidenastatus) {
1545
			$labelStatusShort = $langs->trans('NA');
1546
		} elseif ($percent == 0) {
1547
			$labelStatusShort = '0%';
1548
		} elseif ($percent > 0 && $percent < 100) {
1549
			$labelStatusShort = $percent.'%';
1550
		} elseif ($percent >= 100) {
1551
			$labelStatusShort = '100%';
1552
		}
1553
1554
		$statusType = 'status9';
1555
		if ($percent == -1 && !$hidenastatus) {
1556
			$statusType = 'status9';
1557
		}
1558
		if ($percent == 0) {
1559
			$statusType = 'status1';
1560
		}
1561
		if ($percent > 0 && $percent < 100) {
1562
			$statusType = 'status3';
1563
		}
1564
		if ($percent >= 100) {
1565
			$statusType = 'status6';
1566
		}
1567
1568
		return dolGetStatus($labelStatus, $labelStatusShort, '', $statusType, $mode);
1569
	}
1570
1571
	/**
1572
	 * getTooltipContentArray
1573
	 * @param array $params params to construct tooltip data
1574
	 * @since v18
1575
	 * @return array
1576
	 */
1577
	public function getTooltipContentArray($params)
1578
	{
1579
		global $conf, $langs, $user;
1580
		$langs->load('agenda');
1581
		$datas = [];
1582
1583
		// Set label of type
1584
		$labeltype = '';
1585
		if ($this->type_code) {
1586
			$labeltype = ($langs->transnoentities("Action".$this->type_code) != "Action".$this->type_code) ? $langs->transnoentities("Action".$this->type_code) : $this->type_label;
1587
		}
1588
		if (empty($conf->global->AGENDA_USE_EVENT_TYPE)) {
1589
			if ($this->type_code != 'AC_OTH_AUTO') {
1590
				$labeltype = $langs->trans('ActionAC_MANUAL');
1591
			}
1592
		}
1593
1594
		$datas['picto'] = img_picto('', $this->picto).' <u>'.$langs->trans('Action').'</u>';
1595
		if (!empty($this->ref)) {
1596
			$datas['ref'] = '<br><b>'.$langs->trans('Ref').':</b> '.dol_escape_htmltag($this->ref);
1597
		}
1598
		if (!empty($this->label)) {
1599
			$datas['title'] = '<br><b>'.$langs->trans('Title').':</b> '.dol_escape_htmltag($this->label);
1600
		}
1601
		if (!empty($labeltype)) {
1602
			$datas['labeltype'] = '<br><b>'.$langs->trans('Type').':</b> '.dol_escape_htmltag($labeltype);
1603
		}
1604
		if (!empty($this->location)) {
1605
			$datas['location'] = '<br><b>'.$langs->trans('Location').':</b> '.dol_escape_htmltag($this->location);
1606
		}
1607
		if (isset($this->transparency)) {
1608
			$datas['transparency'] = '<br><b>'.$langs->trans('Busy').':</b> '.yn($this->transparency);
1609
		}
1610
		if (!empty($this->email_msgid)) {
1611
			$langs->load("mails");
1612
			$datas['space'] = '<br>';
1613
			// $datas['email'] = '<br><b>'.img_picto('', 'email').' '.$langs->trans("Email").'</b>';
1614
			$datas['mailtopic'] = '<br><b>'.$langs->trans('MailTopic').':</b> '.dol_escape_htmltag($this->email_subject);
1615
			$datas['mailfrom'] = '<br><b>'.$langs->trans('MailFrom').':</b> '.str_replace(array('<', '>'), array('&amp;lt', '&amp;gt'), $this->email_from);
1616
			$datas['mailto'] = '<br><b>'.$langs->trans('MailTo').':</b> '.str_replace(array('<', '>'), array('&amp;lt', '&amp;gt'), $this->email_to);
1617
			if (!empty($this->email_tocc)) {
1618
				$datas['mailcc'] = '<br><b>'.$langs->trans('MailCC').':</b> '.str_replace(array('<', '>'), array('&amp;lt', '&amp;gt'), $this->email_tocc);
1619
			}
1620
			/* Disabled because bcc must remain by defintion not visible
1621
			if (!empty($this->email_tobcc)) {
1622
				$datas['mailccc'] = '<br><b>'.$langs->trans('MailCCC').':</b> '.$this->email_tobcc;
1623
			} */
1624
		}
1625
		if (!empty($this->note_private)) {
1626
			$datas['description'] = '<br><b>'.$langs->trans('Description').':</b><br>';
1627
			// Try to limit length of content
1628
			$texttoshow = dolGetFirstLineOfText($this->note_private, 10);
1629
			// Restrict height of content into the tooltip
1630
			$datas['note'] = '<div class="tenlinesmax">';
1631
			$datas['note'] .= (dol_textishtml($texttoshow) ? str_replace(array("\r", "\n"), "", $texttoshow) : str_replace(array("\r", "\n"), '<br>', $texttoshow));
1632
			$datas['note'] .= '</div>';
1633
		}
1634
1635
		return $datas;
1636
	}
1637
1638
	/**
1639
	 *  Return URL of event
1640
	 *  Use $this->id, $this->type_code, $this->label and $this->type_label
1641
	 *
1642
	 *  @param	int		$withpicto				0 = No picto, 1 = Include picto into link, 2 = Only picto
1643
	 *  @param	int		$maxlength				Max number of charaters into label. If negative, use the ref as label.
1644
	 *  @param	string	$classname				Force style class on a link
1645
	 *  @param	string	$option					'' = Link to action, 'birthday'= Link to contact, 'holiday' = Link to leave
1646
	 *  @param	int		$overwritepicto			1 = Overwrite picto with this one
1647
	 *  @param	int   	$notooltip		    	1 = Disable tooltip
1648
	 *  @param  int     $save_lastsearch_value  -1 = Auto, 0 = No save of lastsearch_values when clicking, 1 = Save lastsearch_values whenclicking
1649
	 *  @return	string							Chaine avec URL
1650
	 */
1651
	public function getNomUrl($withpicto = 0, $maxlength = 0, $classname = '', $option = '', $overwritepicto = 0, $notooltip = 0, $save_lastsearch_value = -1)
1652
	{
1653
		global $conf, $langs, $user, $hookmanager, $action;
1654
1655
		if (!empty($conf->dol_no_mouse_hover)) {
1656
			$notooltip = 1; // Force disable tooltips
1657
		}
1658
1659
		$canread = 0;
1660
		if (!empty($user->rights->agenda->myactions->read) && $this->authorid == $user->id) {
1661
			$canread = 1; // Can read my event
1662
		}
1663
		if (!empty($user->rights->agenda->myactions->read) && array_key_exists($user->id, $this->userassigned)) {
1664
			$canread = 1; // Can read my event i am assigned
1665
		}
1666
		if (!empty($user->rights->agenda->allactions->read)) {
1667
			$canread = 1; // Can read all event of other
1668
		}
1669
		if (!$canread) {
1670
			$option = 'nolink';
1671
		}
1672
1673
		$label = $this->label;
1674
		if (empty($label)) {
1675
			$label = $this->libelle; // For backward compatibility
1676
		}
1677
1678
		$result = '';
1679
1680
		// Set label of type
1681
		$labeltype = '';
1682
		if ($this->type_code) {
1683
			$labeltype = ($langs->transnoentities("Action".$this->type_code) != "Action".$this->type_code) ? $langs->transnoentities("Action".$this->type_code) : $this->type_label;
1684
		}
1685
		if (empty($conf->global->AGENDA_USE_EVENT_TYPE)) {
1686
			if ($this->type_code != 'AC_OTH_AUTO') {
1687
				$labeltype = $langs->trans('ActionAC_MANUAL');
1688
			}
1689
		}
1690
1691
		$tooltip = img_picto('', $this->picto).' <u>'.$langs->trans('Action').'</u>';
1692
		if (!empty($this->ref)) {
1693
			$tooltip .= '<br><b>'.$langs->trans('Ref').':</b> '.dol_escape_htmltag($this->ref);
1694
		}
1695
		if (!empty($label)) {
1696
			$tooltip .= '<br><b>'.$langs->trans('Title').':</b> '.dol_escape_htmltag($label);
1697
		}
1698
		if (!empty($labeltype)) {
1699
			$tooltip .= '<br><b>'.$langs->trans('Type').':</b> '.dol_escape_htmltag($labeltype);
1700
		}
1701
		if (!empty($this->location)) {
1702
			$tooltip .= '<br><b>'.$langs->trans('Location').':</b> '.dol_escape_htmltag($this->location);
1703
		}
1704
		if (isset($this->transparency)) {
1705
			$tooltip .= '<br><b>'.$langs->trans('Busy').':</b> '.yn($this->transparency);
1706
		}
1707
		if (!empty($this->email_msgid)) {
1708
			$langs->load("mails");
1709
			$tooltip .= '<br>';
1710
			//$tooltip .= '<br><b>'.img_picto('', 'email').' '.$langs->trans("Email").'</b>';
1711
			$tooltip .= '<br><b>'.$langs->trans('MailTopic').':</b> '.dol_escape_htmltag($this->email_subject);
1712
			$tooltip .= '<br><b>'.$langs->trans('MailFrom').':</b> '.str_replace(array('<', '>'), array('&amp;lt', '&amp;gt'), $this->email_from);
1713
			$tooltip .= '<br><b>'.$langs->trans('MailTo').':</b> '.str_replace(array('<', '>'), array('&amp;lt', '&amp;gt'), $this->email_to);
1714
			if (!empty($this->email_tocc)) {
1715
				$tooltip .= '<br><b>'.$langs->trans('MailCC').':</b> '.str_replace(array('<', '>'), array('&amp;lt', '&amp;gt'), $this->email_tocc);
1716
			}
1717
			/* Disabled because bcc must remain by defintion not visible
1718
			if (!empty($this->email_tobcc)) {
1719
				$tooltip .= '<br><b>'.$langs->trans('MailCCC').':</b> '.$this->email_tobcc;
1720
			} */
1721
		}
1722
		if (!empty($this->note_private)) {
1723
			$tooltip .= '<br><br><b>'.$langs->trans('Description').':</b><br>';
1724
			$texttoshow = dolGetFirstLineOfText($this->note_private, 10);	// Try to limit length of content
1725
			$tooltip .= '<div class="tenlinesmax">';						// Restrict height of content into the tooltip
1726
			$tooltip .= (dol_textishtml($texttoshow) ? str_replace(array("\r", "\n"), "", $texttoshow) : str_replace(array("\r", "\n"), '<br>', $texttoshow));
1727
			$tooltip .= '</div>';
1728
		}
1729
		$linkclose = '';
1730
		$classfortooltip = 'classfortooltip';
1731
		$dataparams = '';
1732
		if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
1733
			$params = [
1734
				'id' => $this->id,
1735
				'objecttype' => $this->element,
1736
				'option' => $option,
1737
			];
1738
			$classfortooltip = 'classforajaxtooltip';
1739
			$dataparams = ' data-params='.json_encode($params);
1740
			// $label = $langs->trans('Loading');
1741
		}
1742
		//if (!empty($conf->global->AGENDA_USE_EVENT_TYPE) && $this->type_color)
1743
		//	$linkclose = ' style="background-color:#'.$this->type_color.'"';
1744
1745
		if (empty($notooltip)) {
1746
			if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
1747
				$label = $langs->trans("ShowAction");
1748
				$linkclose .= ' alt="'.dol_escape_htmltag($tooltip, 1).'"';
1749
			}
1750
			$linkclose .= ' title="'.dol_escape_htmltag($tooltip, 1, 0, '', 1).'"';
1751
			$linkclose .= $dataparams.' class="'.$classname.' '.$classfortooltip.'"';
1752
		} else {
1753
			$linkclose .= ' class="'.$classname.'"';
1754
		}
1755
1756
		$url = '';
1757
		if ($option == 'birthday') {
1758
			$url = DOL_URL_ROOT.'/contact/perso.php?id='.$this->id;
1759
		} elseif ($option == 'holiday') {
1760
			$url = DOL_URL_ROOT.'/holiday/card.php?id='.$this->id;
1761
		} else {
1762
			$url = DOL_URL_ROOT.'/comm/action/card.php?id='.$this->id;
1763
		}
1764
1765
		if ($option !== 'nolink') {
1766
			// Add param to save lastsearch_values or not
1767
			$add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1768
			if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1769
				$add_save_lastsearch_values = 1;
1770
			}
1771
			if ($add_save_lastsearch_values) {
1772
				$url .= '&save_lastsearch_values=1';
1773
			}
1774
		}
1775
1776
		$linkstart = '<a href="'.$url.'"';
1777
		$linkstart .= $linkclose.'>';
1778
		$linkend = '</a>';
1779
1780
		if ($option == 'nolink') {
1781
			$linkstart = '';
1782
			$linkend = '';
1783
		}
1784
1785
		if ($withpicto == 2) {
1786
			if (!empty($conf->global->AGENDA_USE_EVENT_TYPE)) {
1787
				$label = $labeltype;
1788
			}
1789
			$labelshort = '';
1790
		} else {
1791
			if (!empty($conf->global->AGENDA_USE_EVENT_TYPE) && empty($label)) {
1792
				$label = $labeltype;
1793
			}
1794
			if ($maxlength < 0) {
1795
				$labelshort = $this->ref;
1796
			} else {
1797
				$labelshort = dol_trunc($label, $maxlength);
1798
			}
1799
		}
1800
1801
		if ($withpicto) {
1802
			if (!empty($conf->global->AGENDA_USE_EVENT_TYPE)) {	// Add code into ()
1803
				if ($labeltype) {
1804
					$label .= (preg_match('/'.preg_quote($labeltype, '/').'/', $label) ? '' : ' ('.$langs->transnoentities("Action".$this->type_code).')');
1805
				}
1806
			}
1807
		}
1808
1809
		$result .= $linkstart;
1810
		if ($withpicto) {
1811
			$result .= img_object(($notooltip ? '' : $langs->trans("ShowAction").': '.$label), ($overwritepicto ? $overwritepicto : 'action'), (($this->type_color && $overwritepicto) ? 'style="color: #'.$this->type_color.' !important;" ' : '').($notooltip ? 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'"' : $dataparams.' class="'.(($withpicto != 2) ? 'paddingright ' : '').$classfortooltip.'"'), 0, 0, $notooltip ? 0 : 1);
1812
		}
1813
		$result .= dol_escape_htmltag($labelshort);
1814
		$result .= $linkend;
1815
1816
		global $action;
1817
		$hookmanager->initHooks(array('actiondao'));
1818
		$parameters = array('id'=>$this->id, 'getnomurl' => &$result);
1819
		$reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1820
		if ($reshook > 0) {
1821
			$result = $hookmanager->resPrint;
1822
		} else {
1823
			$result .= $hookmanager->resPrint;
1824
		}
1825
1826
		return $result;
1827
	}
1828
1829
	/**
1830
	 *  Return Picto of type of event
1831
	 *
1832
	 *  @return	string							HTML String
1833
	 */
1834
	public function getTypePicto()
1835
	{
1836
		global $conf;
1837
1838
		$imgpicto = '';
1839
		if (!empty($conf->global->AGENDA_USE_EVENT_TYPE)) {
1840
			$color = '';
1841
			if ($this->type_color) {
1842
				$color = 'style="color: #'.$this->type_color.' !important;"';
1843
			}
1844
			if ($this->type_picto) {
1845
				$imgpicto = img_picto('', $this->type_picto, 'class="paddingright"');
1846
			} else {
1847
				if ($this->type_code == 'AC_RDV') {
1848
					$imgpicto = img_picto('', 'meeting', $color, false, 0, 0, '', 'paddingright');
1849
				} elseif ($this->type_code == 'AC_TEL') {
1850
					$imgpicto = img_picto('', 'object_phoning', $color, false, 0, 0, '', 'paddingright');
1851
				} elseif ($this->type_code == 'AC_FAX') {
1852
					$imgpicto = img_picto('', 'object_phoning_fax', $color, false, 0, 0, '', 'paddingright');
1853
				} elseif ($this->type_code == 'AC_EMAIL' || $this->type_code == 'AC_EMAIL_IN' || preg_match('/_SENTBYMAIL/', $this->code)) {
1854
					$imgpicto = img_picto('', 'object_email', $color, false, 0, 0, '', 'paddingright');
1855
				} elseif ($this->type_code == 'AC_INT') {
1856
					$imgpicto = img_picto('', 'object_intervention', $color, false, 0, 0, '', 'paddingright');
1857
				} elseif (preg_match('/^TICKET_MSG/', $this->code)) {
1858
					$imgpicto = img_picto('', 'object_conversation', $color, false, 0, 0, '', 'paddingright');
1859
				} elseif ($this->type != 'systemauto') {
1860
					$imgpicto = img_picto('', 'user-cog', $color, false, 0, 0, '', 'paddingright');
1861
				} else {
1862
					$imgpicto = img_picto('', 'cog', $color, false, 0, 0, '', 'paddingright');
1863
				}
1864
			}
1865
		} else {
1866
			// 2 picto: 1 for auto, 1 for manual
1867
			if ($this->type != 'systemauto') {
1868
				$imgpicto = img_picto('', 'user-cog', '', false, 0, 0, '', 'paddingright');
1869
			} else {
1870
				$imgpicto = img_picto('', 'cog', '', false, 0, 0, '', 'paddingright');
1871
			}
1872
		}
1873
		return $imgpicto;
1874
	}
1875
1876
1877
	/**
1878
	 * Sets object to supplied categories.
1879
	 *
1880
	 * Deletes object from existing categories not supplied.
1881
	 * Adds it to non existing supplied categories.
1882
	 * Existing categories are left untouch.
1883
	 *
1884
	 * @param  int[]|int 	$categories 	Category or categories IDs
1885
	 * @return int							<0 if KO, >0 if OK
1886
	 */
1887
	public function setCategories($categories)
1888
	{
1889
		// Handle single category
1890
		if (!is_array($categories)) {
1891
			$categories = array($categories);
1892
		}
1893
1894
		// Get current categories
1895
		include_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1896
		$c = new Categorie($this->db);
1897
		$existing = $c->containing($this->id, Categorie::TYPE_ACTIONCOMM, 'id');
1898
1899
		// Diff
1900
		if (is_array($existing)) {
1901
			$to_del = array_diff($existing, $categories);
1902
			$to_add = array_diff($categories, $existing);
1903
		} else {
1904
			$to_del = array(); // Nothing to delete
1905
			$to_add = $categories;
1906
		}
1907
1908
		// Process
1909
		foreach ($to_del as $del) {
1910
			if ($c->fetch($del) > 0) {
1911
				$c->del_type($this, Categorie::TYPE_ACTIONCOMM);
1912
			}
1913
		}
1914
		foreach ($to_add as $add) {
1915
			if ($c->fetch($add) > 0) {
1916
				$c->add_type($this, Categorie::TYPE_ACTIONCOMM);
1917
			}
1918
		}
1919
		return 1;
1920
	}
1921
1922
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1923
	/**
1924
	 * Export events from database into a cal file.
1925
	 *
1926
	 * @param string    $format         The format of the export 'vcal', 'ical/ics' or 'rss'
1927
	 * @param string    $type           The type of the export 'event' or 'journal'
1928
	 * @param integer   $cachedelay     Do not rebuild file if date older than cachedelay seconds
1929
	 * @param string    $filename       The name for the exported file.
1930
	 * @param array     $filters        Array of filters. Example array('notolderthan'=>99, 'year'=>..., 'idfrom'=>..., 'notactiontype'=>'systemauto', 'project'=>123, ...)
1931
	 * @param integer   $exportholiday  0 = don't integrate holidays into the export, 1 = integrate holidays into the export
1932
	 * @return integer                  -1 = error on build export file, 0 = export okay
1933
	 */
1934
	public function build_exportfile($format, $type, $cachedelay, $filename, $filters, $exportholiday = 0)
1935
	{
1936
		global $hookmanager;
1937
1938
		// phpcs:enable
1939
		global $conf, $langs, $dolibarr_main_url_root, $mysoc;
1940
1941
		require_once DOL_DOCUMENT_ROOT."/core/lib/xcal.lib.php";
1942
		require_once DOL_DOCUMENT_ROOT."/core/lib/date.lib.php";
1943
		require_once DOL_DOCUMENT_ROOT."/core/lib/files.lib.php";
1944
1945
		dol_syslog(get_class($this)."::build_exportfile Build export file format=".$format.", type=".$type.", cachedelay=".$cachedelay.", filename=".$filename.", filters size=".count($filters), LOG_DEBUG);
1946
1947
		// Check parameters
1948
		if (empty($format)) {
1949
			return -1;
1950
		}
1951
1952
		// Clean parameters
1953
		if (!$filename) {
1954
			$extension = 'vcs';
1955
			if ($format == 'ical') {
1956
				$extension = 'ics';
1957
			}
1958
			$filename = $format.'.'.$extension;
1959
		}
1960
1961
		// Create dir and define output file (definitive and temporary)
1962
		$result = dol_mkdir($conf->agenda->dir_temp);
1963
		$outputfile = $conf->agenda->dir_temp.'/'.$filename;
1964
1965
		$result = 0;
1966
1967
		$buildfile = true;
1968
		$login = ''; $logina = ''; $logind = ''; $logint = '';
1969
1970
		$now = dol_now();
1971
1972
		if ($cachedelay) {
1973
			$nowgmt = dol_now();
1974
			include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1975
			if (dol_filemtime($outputfile) > ($nowgmt - $cachedelay)) {
1976
				dol_syslog(get_class($this)."::build_exportfile file ".$outputfile." is not older than now - cachedelay (".$nowgmt." - ".$cachedelay."). Build is canceled");
1977
				$buildfile = false;
1978
			}
1979
		}
1980
1981
		if ($buildfile) {
1982
			// Build event array
1983
			$eventarray = array();
1984
1985
			$sql = "SELECT a.id,";
1986
			$sql .= " a.datep,"; // Start
1987
			$sql .= " a.datep2,"; // End
1988
			$sql .= " a.durationp,"; // deprecated
1989
			$sql .= " a.datec, a.tms as datem,";
1990
			$sql .= " a.label, a.code, a.note as note_private, a.fk_action as type_id,";
1991
			$sql .= " a.fk_soc,";
1992
			$sql .= " a.fk_user_author, a.fk_user_mod,";
1993
			$sql .= " a.fk_user_action,";
1994
			$sql .= " a.fk_contact, a.percent as percentage,";
1995
			$sql .= " a.fk_element, a.elementtype,";
1996
			$sql .= " a.priority, a.fulldayevent, a.location, a.transparency,";
1997
			$sql .= " u.firstname, u.lastname, u.email,";
1998
			$sql .= " s.nom as socname,";
1999
			$sql .= " c.id as type_id, c.code as type_code, c.libelle as type_label,";
2000
			$sql .= " num_vote, event_paid, a.status";
2001
			$sql .= " FROM (".MAIN_DB_PREFIX."c_actioncomm as c, ".MAIN_DB_PREFIX."actioncomm as a)";
2002
			$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as u on u.rowid = a.fk_user_author"; // Link to get author of event for export
2003
			$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s on s.rowid = a.fk_soc";
2004
2005
			$parameters = array('filters' => $filters);
2006
			$reshook = $hookmanager->executeHooks('printFieldListFrom', $parameters); // Note that $action and $object may have been modified by hook
2007
			$sql .= $hookmanager->resPrint;
2008
2009
			// We must filter on assignement table
2010
			if ($filters['logint']) {
2011
				$sql .= ", ".MAIN_DB_PREFIX."actioncomm_resources as ar";
2012
			}
2013
			$sql .= " WHERE a.fk_action=c.id";
2014
			$sql .= " AND a.entity IN (".getEntity('agenda').")";
2015
			foreach ($filters as $key => $value) {
2016
				if ($key == 'notolderthan' && $value != '') {
2017
					$sql .= " AND a.datep >= '".$this->db->idate($now - ($value * 24 * 60 * 60))."'";
2018
				}
2019
				if ($key == 'year') {
2020
					$sql .= " AND a.datep BETWEEN '".$this->db->idate(dol_get_first_day($value, 1))."' AND '".$this->db->idate(dol_get_last_day($value, 12))."'";
2021
				}
2022
				if ($key == 'id') {
2023
					$sql .= " AND a.id=".(is_numeric($value) ? $value : 0);
2024
				}
2025
				if ($key == 'idfrom') {
2026
					$sql .= " AND a.id >= ".(is_numeric($value) ? $value : 0);
2027
				}
2028
				if ($key == 'idto') {
2029
					$sql .= " AND a.id <= ".(is_numeric($value) ? $value : 0);
2030
				}
2031
				if ($key == 'project') {
2032
					$sql .= " AND a.fk_project=".(is_numeric($value) ? $value : 0);
2033
				}
2034
				if ($key == 'actiontype') {
2035
					$sql .= " AND c.type = '".$this->db->escape($value)."'";
2036
				}
2037
				if ($key == 'notactiontype') {
2038
					$sql .= " AND c.type <> '".$this->db->escape($value)."'";
2039
				}
2040
				// We must filter on assignement table
2041
				if ($key == 'logint') {
2042
					$sql .= " AND ar.fk_actioncomm = a.id AND ar.element_type='user'";
2043
				}
2044
				if ($key == 'logina') {
2045
					$logina = $value;
2046
					$condition = '=';
2047
					if (preg_match('/^!/', $logina)) {
2048
						$logina = preg_replace('/^!/', '', $logina);
2049
						$condition = '<>';
2050
					}
2051
					$userforfilter = new User($this->db);
2052
					$result = $userforfilter->fetch('', $logina);
2053
					if ($result > 0) {
2054
						$sql .= " AND a.fk_user_author ".$condition." ".$userforfilter->id;
2055
					} elseif ($result < 0 || $condition == '=') {
2056
						$sql .= " AND a.fk_user_author = 0";
2057
					}
2058
				}
2059
				if ($key == 'logint') {
2060
					$logint = $value;
2061
					$condition = '=';
2062
					if (preg_match('/^!/', $logint)) {
2063
						$logint = preg_replace('/^!/', '', $logint);
2064
						$condition = '<>';
2065
					}
2066
					$userforfilter = new User($this->db);
2067
					$result = $userforfilter->fetch('', $logint);
2068
					if ($result > 0) {
2069
						$sql .= " AND ar.fk_element = ".((int) $userforfilter->id);
2070
					} elseif ($result < 0 || $condition == '=') {
2071
						$sql .= " AND ar.fk_element = 0";
2072
					}
2073
				}
2074
				if ($key == 'module') {
2075
					$sql .= " AND c.module LIKE '%".$this->db->escape($value)."'";
2076
				}
2077
				if ($key == 'status') {
2078
					$sql .= " AND a.status =".((int) $value);
2079
				}
2080
			}
2081
2082
			$sql .= " AND a.datep IS NOT NULL"; // To exclude corrupted events and avoid errors in lightning/sunbird import
2083
2084
			$parameters = array('filters' => $filters);
2085
			$reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters); // Note that $action and $object may have been modified by hook
2086
			$sql .= $hookmanager->resPrint;
2087
2088
			$sql .= " ORDER by datep";
2089
			//print $sql;exit;
2090
2091
			dol_syslog(get_class($this)."::build_exportfile select events", LOG_DEBUG);
2092
			$resql = $this->db->query($sql);
2093
			if ($resql) {
2094
				// Note: Output of sql request is encoded in $conf->file->character_set_client
2095
				// This assignment in condition is not a bug. It allows walking the results.
2096
				$diff = 0;
2097
				while ($obj = $this->db->fetch_object($resql)) {
2098
					$qualified = true;
2099
2100
					// 'eid','startdate','duration','enddate','title','summary','category','email','url','desc','author'
2101
					$event = array();
2102
					$event['uid'] = 'dolibarragenda-'.$this->db->database_name.'-'.$obj->id."@".$_SERVER["SERVER_NAME"];
2103
					$event['type'] = $type;
2104
2105
					$datestart = $this->db->jdate($obj->datep) - (empty($conf->global->AGENDA_EXPORT_FIX_TZ) ? 0 : ($conf->global->AGENDA_EXPORT_FIX_TZ * 3600));
2106
2107
					// fix for -> Warning: A non-numeric value encountered
2108
					if (is_numeric($this->db->jdate($obj->datep2))) {
2109
						$dateend = $this->db->jdate($obj->datep2) - (empty($conf->global->AGENDA_EXPORT_FIX_TZ) ? 0 : ($conf->global->AGENDA_EXPORT_FIX_TZ * 3600));
2110
					} else {
2111
						// use start date as fall-back to avoid pb with empty end date on ICS readers
2112
						$dateend = $datestart;
2113
					}
2114
2115
					$duration = ($datestart && $dateend) ? ($dateend - $datestart) : 0;
2116
					$event['summary'] = $obj->label.($obj->socname ? " (".$obj->socname.")" : "");
2117
2118
					$event['desc'] = $obj->note_private;
2119
					$event['startdate'] = $datestart;
2120
					$event['enddate'] = $dateend; // Not required with type 'journal'
2121
					$event['duration'] = $duration; // Not required with type 'journal'
2122
					$event['author'] = dolGetFirstLastname($obj->firstname, $obj->lastname);
2123
					$event['priority'] = $obj->priority;
2124
					$event['fulldayevent'] = $obj->fulldayevent;
2125
					$event['location'] = $obj->location;
2126
					$event['transparency'] = (($obj->transparency > 0) ? 'OPAQUE' : 'TRANSPARENT'); // OPAQUE (busy) or TRANSPARENT (not busy)
2127
					$event['category'] = $obj->type_label;
2128
					$event['email'] = $obj->email;
2129
					// Define $urlwithroot
2130
					$urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
2131
					$urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
2132
					//$urlwithroot=DOL_MAIN_URL_ROOT;						// This is to use same domain name than current
2133
					$url = $urlwithroot.'/comm/action/card.php?id='.$obj->id;
2134
					$event['url'] = $url;
2135
					$event['created'] = $this->db->jdate($obj->datec) - (empty($conf->global->AGENDA_EXPORT_FIX_TZ) ? 0 : ($conf->global->AGENDA_EXPORT_FIX_TZ * 3600));
2136
					$event['modified'] = $this->db->jdate($obj->datem) - (empty($conf->global->AGENDA_EXPORT_FIX_TZ) ? 0 : ($conf->global->AGENDA_EXPORT_FIX_TZ * 3600));
2137
					$event['num_vote'] = $this->num_vote;
2138
					$event['event_paid'] = $this->event_paid;
2139
					$event['status'] = $this->status;
2140
2141
					// TODO: find a way to call "$this->fetch_userassigned();" without override "$this" properties
2142
					$this->id = $obj->id;
2143
					$this->fetch_userassigned(false);
2144
2145
					$assignedUserArray = array();
2146
2147
					foreach ($this->userassigned as $key => $value) {
2148
						$assignedUser = new User($this->db);
2149
						$assignedUser->fetch($value['id']);
2150
2151
						$assignedUserArray[$key] = $assignedUser;
2152
					}
2153
2154
					$event['assignedUsers'] = $assignedUserArray;
2155
2156
					if ($qualified && $datestart) {
2157
						$eventarray[] = $event;
2158
					}
2159
					$diff++;
2160
				}
2161
2162
				$parameters = array('filters' => $filters, 'eventarray' => &$eventarray);
2163
				$reshook = $hookmanager->executeHooks('addMoreEventsExport', $parameters); // Note that $action and $object may have been modified by hook
2164
				if ($reshook > 0) {
2165
					$eventarray = $hookmanager->resArray;
2166
				}
2167
			} else {
2168
				$this->error = $this->db->lasterror();
2169
				return -1;
2170
			}
2171
2172
			if ($exportholiday == 1) {
2173
				$langs->load("holiday");
2174
				$title = $langs->trans("Holidays");
2175
2176
				$sql = "SELECT u.rowid as uid, u.lastname, u.firstname, u.email, u.statut, x.rowid, x.date_debut as date_start, x.date_fin as date_end, x.halfday, x.statut as status";
2177
				$sql .= " FROM ".MAIN_DB_PREFIX."holiday as x, ".MAIN_DB_PREFIX."user as u";
2178
				$sql .= " WHERE u.rowid = x.fk_user";
2179
				$sql .= " AND u.statut = '1'"; // Show only active users  (0 = inactive user, 1 = active user)
2180
				$sql .= " AND (x.statut = '2' OR x.statut = '3')"; // Show only public leaves (2 = leave wait for approval, 3 = leave approved)
2181
2182
				$resql = $this->db->query($sql);
2183
				if ($resql) {
2184
					$num = $this->db->num_rows($resql);
2185
					$i   = 0;
2186
2187
					while ($i < $num) {
2188
						$obj   = $this->db->fetch_object($resql);
2189
						$event = array();
2190
2191
						if ($obj->halfday == -1) {
2192
							$event['fulldayevent'] = false;
2193
2194
							$timestampStart = dol_stringtotime($obj->date_start." 00:00:00", 0);
2195
							$timestampEnd   = dol_stringtotime($obj->date_end." 12:00:00", 0);
2196
						} elseif ($obj->halfday == 1) {
2197
							$event['fulldayevent'] = false;
2198
2199
							$timestampStart = dol_stringtotime($obj->date_start." 12:00:00", 0);
2200
							$timestampEnd   = dol_stringtotime($obj->date_end." 23:59:59", 0);
2201
						} else {
2202
							$event['fulldayevent'] = true;
2203
2204
							$timestampStart = dol_stringtotime($obj->date_start." 00:00:00", 0);
2205
							$timestampEnd   = dol_stringtotime($obj->date_end." 23:59:59", 0);
2206
						}
2207
2208
						if (!empty($conf->global->AGENDA_EXPORT_FIX_TZ)) {
2209
							$timestampStart = $timestampStart - ($conf->global->AGENDA_EXPORT_FIX_TZ * 3600);
2210
							$timestampEnd   = $timestampEnd - ($conf->global->AGENDA_EXPORT_FIX_TZ * 3600);
2211
						}
2212
2213
						$urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
2214
						$urlwithroot       = $urlwithouturlroot.DOL_URL_ROOT;
2215
						$url               = $urlwithroot.'/holiday/card.php?id='.$obj->rowid;
2216
2217
						$event['uid']          = 'dolibarrholiday-'.$this->db->database_name.'-'.$obj->rowid."@".$_SERVER["SERVER_NAME"];
2218
						$event['author']       = dolGetFirstLastname($obj->firstname, $obj->lastname);
2219
						$event['type']         = 'event';
2220
						$event['category']     = "Holiday";
2221
						$event['transparency'] = 'OPAQUE';
2222
						$event['email']        = $obj->email;
2223
						$event['created']      = $timestampStart;
2224
						$event['modified']     = $timestampStart;
2225
						$event['startdate']    = $timestampStart;
2226
						$event['enddate']      = $timestampEnd;
2227
						$event['duration']     = $timestampEnd - $timestampStart;
2228
						$event['url']          = $url;
2229
2230
						if ($obj->status == 2) {
2231
							// 2 = leave wait for approval
2232
							$event['summary'] = $title." - ".$obj->lastname." (wait for approval)";
2233
						} else {
2234
							// 3 = leave approved
2235
							$event['summary'] = $title." - ".$obj->lastname;
2236
						}
2237
2238
						$eventarray[] = $event;
2239
2240
						$i++;
2241
					}
2242
				}
2243
			}
2244
2245
			$langs->load("agenda");
2246
2247
			// Define title and desc
2248
			$more = '';
2249
			if ($login) {
2250
				$more = $langs->transnoentities("User").' '.$login;
2251
			}
2252
			if ($logina) {
2253
				$more = $langs->transnoentities("ActionsAskedBy").' '.$logina;
2254
			}
2255
			if ($logint) {
2256
				$more = $langs->transnoentities("ActionsToDoBy").' '.$logint;
2257
			}
2258
			if ($logind) {
2259
				$more = $langs->transnoentities("ActionsDoneBy").' '.$logind;
2260
			}
2261
			if ($more) {
2262
				$title = 'Dolibarr actions '.$mysoc->name.' - '.$more;
2263
				$desc = $more;
2264
				$desc .= ' ('.$mysoc->name.' - built by Dolibarr)';
2265
			} else {
2266
				$title = 'Dolibarr actions '.$mysoc->name;
2267
				$desc = $langs->transnoentities('ListOfActions');
2268
				$desc .= ' ('.$mysoc->name.' - built by Dolibarr)';
2269
			}
2270
2271
			// Create temp file
2272
			$outputfiletmp = tempnam($conf->agenda->dir_temp, 'tmp'); // Temporary file (allow call of function by different threads
2273
			@chmod($outputfiletmp, octdec($conf->global->MAIN_UMASK));
2274
2275
			// Write file
2276
			if ($format == 'vcal') {
2277
				$result = build_calfile($format, $title, $desc, $eventarray, $outputfiletmp);
2278
			} elseif ($format == 'ical') {
2279
				$result = build_calfile($format, $title, $desc, $eventarray, $outputfiletmp);
2280
			} elseif ($format == 'rss') {
2281
				$result = build_rssfile($format, $title, $desc, $eventarray, $outputfiletmp);
2282
			}
2283
2284
			if ($result >= 0) {
2285
				if (dol_move($outputfiletmp, $outputfile, 0, 1)) {
2286
					$result = 1;
2287
				} else {
2288
					$this->error = 'Failed to rename '.$outputfiletmp.' into '.$outputfile;
2289
					dol_syslog(get_class($this)."::build_exportfile ".$this->error, LOG_ERR);
2290
					dol_delete_file($outputfiletmp, 0, 1);
2291
					$result = -1;
2292
				}
2293
			} else {
2294
				dol_syslog(get_class($this)."::build_exportfile build_xxxfile function fails to for format=".$format." outputfiletmp=".$outputfile, LOG_ERR);
2295
				dol_delete_file($outputfiletmp, 0, 1);
2296
				$langs->load("errors");
2297
				$this->error = $langs->trans("ErrorFailToCreateFile", $outputfile);
2298
			}
2299
		}
2300
2301
		return $result;
2302
	}
2303
2304
	/**
2305
	 *  Initialise an instance with random values.
2306
	 *  Used to build previews or test instances.
2307
	 *  id must be 0 if object instance is a specimen.
2308
	 *
2309
	 *  @return	int >0 if ok
2310
	 */
2311
	public function initAsSpecimen()
2312
	{
2313
		global $user;
2314
2315
		$now = dol_now();
2316
2317
		// Initialise parametres
2318
		$this->id = 0;
2319
		$this->specimen = 1;
2320
2321
		$this->type_code = 'AC_OTH';
2322
		$this->code = 'AC_SPECIMEN_CODE';
2323
		$this->label = 'Label of event Specimen';
2324
		$this->datec = $now;
2325
		$this->datem = $now;
2326
		$this->datep = $now;
2327
		$this->datef = $now;
2328
		$this->fulldayevent = 0;
2329
		$this->percentage = 0;
2330
		$this->status = 0;
2331
		$this->location = 'Location';
2332
		$this->transparency = 1; // 1 means opaque
2333
		$this->priority = 1;
2334
		//$this->note_public = "This is a 'public' note.";
2335
		$this->note_private = "This is a 'private' note.";
2336
2337
		$this->userownerid = $user->id;
2338
		$this->userassigned[$user->id] = array('id'=>$user->id, 'transparency'=> 1);
2339
		return 1;
2340
	}
2341
2342
	/**
2343
	 *  Function used to replace a thirdparty id with another one.
2344
	 *
2345
	 * @param 	DoliDB 	$dbs 		Database handler, because function is static we name it $dbs not $db to avoid breaking coding test
2346
	 * @param 	int 	$origin_id 	Old thirdparty id
2347
	 * @param 	int 	$dest_id 	New thirdparty id
2348
	 * @return 	bool
2349
	 */
2350
	public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
2351
	{
2352
		$tables = array(
2353
			'actioncomm'
2354
		);
2355
2356
		return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
2357
	}
2358
2359
	/**
2360
	 *  Function used to replace a product id with another one.
2361
	 *
2362
	 *  @param DoliDB $dbs Database handler
2363
	 *  @param int $origin_id Old product id
2364
	 *  @param int $dest_id New product id
2365
	 *  @return bool
2366
	 */
2367
	public static function replaceProduct(DoliDB $dbs, $origin_id, $dest_id)
2368
	{
2369
		$sql = 'UPDATE ' . MAIN_DB_PREFIX . 'actioncomm SET fk_element = ' . ((int) $dest_id) . ' WHERE elementtype="product" AND fk_element = '.((int) $origin_id);
2370
		// using $dbs, not $this->db because function is static
2371
		if (!$dbs->query($sql)) {
2372
			//$this->errors = $dbs->lasterror();
2373
			return false;
2374
		}
2375
2376
		return true;
2377
	}
2378
2379
	/**
2380
	 *  Is the action delayed?
2381
	 *
2382
	 *  @return bool
2383
	 */
2384
	public function hasDelay()
2385
	{
2386
		global $conf;
2387
2388
		$now = dol_now();
2389
2390
		return $this->datep && ($this->datep < ($now - $conf->agenda->warning_delay));
2391
	}
2392
2393
2394
	/**
2395
	 *  Load event reminder of events
2396
	 *
2397
	 *  @param	string	$type		Type of reminder 'browser' or 'email'
2398
	 *  @param	int		$fk_user	Id of user
2399
	 *  @param	bool	$onlypast	true = get only past reminder, false = get all reminders linked to this
2400
	 *  @return int         		0 if OK, <>0 if KO (this function is used also by cron so only 0 is OK)
2401
	 */
2402
	public function loadReminders($type = '', $fk_user = 0, $onlypast = true)
2403
	{
2404
		global $conf, $langs, $user;
2405
2406
		$error = 0;
2407
2408
		$this->reminders = array();
2409
2410
		//Select all action comm reminders for event
2411
		$sql = "SELECT rowid as id, typeremind, dateremind, status, offsetvalue, offsetunit, fk_user";
2412
		$sql .= " FROM ".MAIN_DB_PREFIX."actioncomm_reminder";
2413
		$sql .= " WHERE fk_actioncomm = ".((int) $this->id);
2414
		if ($onlypast) {
2415
			$sql .= " AND dateremind <= '".$this->db->idate(dol_now())."'";
2416
		}
2417
		if ($type) {
2418
			$sql .= " AND typeremind ='".$this->db->escape($type)."'";
2419
		}
2420
		if ($fk_user > 0) {
2421
			$sql .= " AND fk_user = ".((int) $fk_user);
2422
		}
2423
		if (empty($conf->global->AGENDA_REMINDER_EMAIL)) {
2424
			$sql .= " AND typeremind != 'email'";
2425
		}
2426
		if (empty($conf->global->AGENDA_REMINDER_BROWSER)) {
2427
			$sql .= " AND typeremind != 'browser'";
2428
		}
2429
2430
		$sql .= $this->db->order("dateremind", "ASC");
2431
		$resql = $this->db->query($sql);
2432
2433
		if ($resql) {
2434
			while ($obj = $this->db->fetch_object($resql)) {
2435
				$tmpactioncommreminder = new ActionCommReminder($this->db);
2436
				$tmpactioncommreminder->id = $obj->id;
2437
				$tmpactioncommreminder->typeremind = $obj->typeremind;
2438
				$tmpactioncommreminder->dateremind = $obj->dateremind;
2439
				$tmpactioncommreminder->offsetvalue = $obj->offsetvalue;
2440
				$tmpactioncommreminder->offsetunit = $obj->offsetunit;
2441
				$tmpactioncommreminder->status = $obj->status;
2442
				$tmpactioncommreminder->fk_user = $obj->fk_user;
2443
2444
				$this->reminders[$obj->id] = $tmpactioncommreminder;
2445
			}
2446
		} else {
2447
			$this->error = $this->db->lasterror();
2448
			$error++;
2449
		}
2450
2451
		return count($this->reminders);
2452
	}
2453
2454
2455
	/**
2456
	 *  Send reminders by emails
2457
	 *  CAN BE A CRON TASK
2458
	 *
2459
	 *  @return int         0 if OK, <>0 if KO (this function is used also by cron so only 0 is OK)
2460
	 */
2461
	public function sendEmailsReminder()
2462
	{
2463
		global $conf, $langs, $user;
2464
2465
		$error = 0;
2466
		$this->output = '';
2467
		$this->error = '';
2468
		$nbMailSend = 0;
2469
		$errorsMsg = array();
2470
2471
		if (!isModEnabled('agenda')) {	// Should not happen. If module disabled, cron job should not be visible.
2472
			$langs->load("agenda");
2473
			$this->output = $langs->trans('ModuleNotEnabled', $langs->transnoentitiesnoconv("Agenda"));
2474
			return 0;
2475
		}
2476
		if (empty($conf->global->AGENDA_REMINDER_EMAIL)) {
2477
			$langs->load("agenda");
2478
			$this->output = $langs->trans('EventRemindersByEmailNotEnabled', $langs->transnoentitiesnoconv("Agenda"));
2479
			return 0;
2480
		}
2481
2482
		$now = dol_now();
2483
		$actionCommReminder = new ActionCommReminder($this->db);
2484
2485
		dol_syslog(__METHOD__, LOG_DEBUG);
2486
2487
		$this->db->begin();
2488
2489
		//Select all action comm reminders
2490
		$sql = "SELECT rowid as id FROM ".MAIN_DB_PREFIX."actioncomm_reminder";
2491
		$sql .= " WHERE typeremind = 'email' AND status = 0";
2492
		$sql .= " AND dateremind <= '".$this->db->idate($now)."'";
2493
		$sql .= " AND entity IN (".getEntity('actioncomm').")";
2494
		$sql .= $this->db->order("dateremind", "ASC");
2495
		$resql = $this->db->query($sql);
2496
2497
		if ($resql) {
2498
			require_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
2499
			$formmail = new FormMail($this->db);
2500
2501
			while ($obj = $this->db->fetch_object($resql)) {
2502
				$res = $actionCommReminder->fetch($obj->id);
2503
				if ($res < 0) {
2504
					$error++;
2505
					$errorsMsg[] = "Failed to load invoice ActionComm Reminder";
2506
				}
2507
2508
				if (!$error) {
2509
					//Select email template
2510
					$arraymessage = $formmail->getEMailTemplate($this->db, 'actioncomm_send', $user, $langs, (!empty($actionCommReminder->fk_email_template)) ? $actionCommReminder->fk_email_template : -1, 1);
2511
2512
					// Load event
2513
					$res = $this->fetch($actionCommReminder->fk_actioncomm);
2514
					if ($res > 0) {
2515
						// PREPARE EMAIL
2516
						$errormesg = '';
2517
2518
						// Make substitution in email content
2519
						$substitutionarray = getCommonSubstitutionArray($langs, 0, '', $this);
2520
2521
						complete_substitutions_array($substitutionarray, $langs, $this);
2522
2523
						// Content
2524
						$sendContent = make_substitutions($langs->trans($arraymessage->content), $substitutionarray);
2525
2526
						//Topic
2527
						$sendTopic = (!empty($arraymessage->topic)) ? $arraymessage->topic : html_entity_decode($langs->transnoentities('EventReminder'));
2528
2529
						// Recipient
2530
						$recipient = new User($this->db);
2531
						$res = $recipient->fetch($actionCommReminder->fk_user);
2532
						if ($res > 0) {
2533
							if (!empty($recipient->email)) {
2534
								$to = $recipient->email;
2535
							} else {
2536
								$errormesg = "Failed to send remind to user id=".$actionCommReminder->fk_user.". No email defined for user.";
2537
								$error++;
2538
							}
2539
						} else {
2540
							$errormesg = "Failed to load recipient with user id=".$actionCommReminder->fk_user;
2541
							$error++;
2542
						}
2543
2544
						// Sender
2545
						$from = $conf->global->MAIN_MAIL_EMAIL_FROM;
2546
						if (empty($from)) {
2547
							$errormesg = "Failed to get sender into global setup MAIN_MAIL_EMAIL_FROM";
2548
							$error++;
2549
						}
2550
2551
						if (!$error) {
2552
							// Errors Recipient
2553
							$errors_to = $conf->global->MAIN_MAIL_ERRORS_TO;
2554
2555
							// Mail Creation
2556
							$cMailFile = new CMailFile($sendTopic, $to, $from, $sendContent, array(), array(), array(), '', "", 0, 1, $errors_to, '', '', '', '', '');
2557
2558
							// Sending Mail
2559
							if ($cMailFile->sendfile()) {
2560
								$nbMailSend++;
2561
							} else {
2562
								$errormesg = $cMailFile->error.' : '.$to;
2563
								$error++;
2564
							}
2565
						}
2566
2567
						if (!$error) {
2568
							$actionCommReminder->status = $actionCommReminder::STATUS_DONE;
2569
2570
							$res = $actionCommReminder->update($user);
2571
							if ($res < 0) {
2572
								$errorsMsg[] = "Failed to update status to done of ActionComm Reminder";
2573
								$error++;
2574
								break; // This is to avoid to have this error on all the selected email. If we fails here for one record, it may fails for others. We must solve first.
2575
							}
2576
						} else {
2577
							$actionCommReminder->status = $actionCommReminder::STATUS_ERROR;
2578
							$actionCommReminder->lasterror = dol_trunc($errormesg, 128, 'right', 'UTF-8', 1);
2579
2580
							$res = $actionCommReminder->update($user);
2581
							if ($res < 0) {
2582
								$errorsMsg[] = "Failed to update status to error of ActionComm Reminder";
2583
								$error++;
2584
								break; // This is to avoid to have this error on all the selected email. If we fails here for one record, it may fails for others. We must solve first.
2585
							} else {
2586
								$errorsMsg[] = $errormesg;
2587
							}
2588
						}
2589
					} else {
2590
						$errorsMsg[] = 'Failed to fetch record actioncomm with ID = '.$actionCommReminder->fk_actioncomm;
2591
						$error++;
2592
					}
2593
				}
2594
			}
2595
		} else {
2596
			$error++;
2597
		}
2598
2599
		if (!$error) {
2600
			// Delete also very old past events (we do not keep more than 1 month record in past)
2601
			$sql = "DELETE FROM ".MAIN_DB_PREFIX."actioncomm_reminder";
2602
			$sql .= " WHERE dateremind < '".$this->db->idate($now - (3600 * 24 * 32))."'";
2603
			$sql .= " AND status = ".((int) $actionCommReminder::STATUS_DONE);
2604
			$resql = $this->db->query($sql);
2605
2606
			if (!$resql) {
2607
				$errorsMsg[] = 'Failed to delete old reminders';
2608
				//$error++;		// If this fails, we must not rollback other SQL requests already done. Never mind.
2609
			}
2610
		}
2611
2612
		if (!$error) {
2613
			$this->output = 'Nb of emails sent : '.$nbMailSend;
2614
			$this->db->commit();
2615
			return 0;
2616
		} else {
2617
			$this->db->commit(); // We commit also on error, to have the error message recorded.
2618
			$this->error = 'Nb of emails sent : '.$nbMailSend.', '.(!empty($errorsMsg)) ? join(', ', $errorsMsg) : $error;
2619
			return $error;
2620
		}
2621
	}
2622
2623
	/**
2624
	 * Udpate the percent value of a event with the given id
2625
	 *
2626
	 * @param int		$id			The id of the event
2627
	 * @param int		$percent	The new percent value for the event
2628
	 * @param int		$usermodid	The user who modified the percent
2629
	 * @return int					1 when update of the event was suscessfull, otherwise -1
2630
	 */
2631
	public function updatePercent($id, $percent, $usermodid = 0)
2632
	{
2633
		$this->db->begin();
2634
2635
		$sql = "UPDATE ".MAIN_DB_PREFIX."actioncomm ";
2636
		$sql .= " SET percent = ".(int) $percent;
2637
		if ($usermodid > 0) $sql .= ", fk_user_mod = ".$usermodid;
2638
		$sql .= " WHERE id = ".((int) $id);
2639
2640
		if ($this->db->query($sql)) {
2641
			$this->db->commit();
2642
			return 1;
2643
		} else {
2644
			$this->db->rollback();
2645
			$this->error = $this->db->lasterror();
2646
			return -1;
2647
		}
2648
	}
2649
}
2650