Passed
Branch develop (def2bd)
by
unknown
26:55
created

ActionComm::replaceThirdparty()   A

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nop 3
dl 0
loc 7
rs 10
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-2020  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/html.formmail.class.php';
32
require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
33
require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncommreminder.class.php';
34
35
36
37
38
39
/**
40
 *		Class to manage agenda events (actions)
41
 */
42
class ActionComm extends CommonObject
43
{
44
    /**
45
     * @var string ID to identify managed object
46
     */
47
    public $element = 'action';
48
49
    /**
50
     * @var string Name of table without prefix where object is stored
51
     */
52
    public $table_element = 'actioncomm';
53
54
    /**
55
     * @var string Name of id column
56
     */
57
    public $table_rowid = 'id';
58
59
    /**
60
     * @var string Name of icon for actioncomm object. Filename of icon is object_action.png
61
     */
62
    public $picto = 'action';
63
64
    /**
65
     * @var int 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
66
     */
67
    public $ismultientitymanaged = 1;
68
69
    /**
70
     * @var integer 0=Default
71
     *              1=View may be restricted to sales representative only if no permission to see all or to company of external user if external user
72
     *              2=Same than 1 but accept record if fksoc is empty
73
     */
74
    public $restrictiononfksoc = 2;
75
76
    /**
77
     * @var int Id of the event
78
     */
79
    public $id;
80
81
    /**
82
     * @var int Id of the event. Use $id as possible
83
     */
84
    public $ref;
85
86
    /**
87
     * @var int Id into parent table llx_c_actioncomm (used only if option to use type is set)
88
     */
89
    public $type_id;
90
91
    /**
92
     * @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.
93
     */
94
    public $type_code;
95
96
    /**
97
     * @var string Type label
98
     */
99
    public $type_label;
100
101
    /**
102
     * @var string Label into parent table llx_c_actioncomm (used only if option to use type is set)
103
     */
104
    public $type;
105
106
    /**
107
     * @var string Color into parent table llx_c_actioncomm (used only if option to use type is set)
108
     */
109
    public $type_color;
110
111
    /**
112
     * @var string Free code to identify action. Ie: Agenda trigger add here AC_TRIGGERNAME ('AC_COMPANY_CREATE', 'AC_PROPAL_VALIDATE', ...)
113
     */
114
    public $code;
115
116
    /**
117
     * @var string Agenda event label
118
     */
119
    public $label;
120
121
    /**
122
     * @var integer Date creation record (datec)
123
     */
124
    public $datec;
125
126
    /**
127
     * @var integer Date end record (datef)
128
     */
129
    public $datef;
130
131
    /**
132
     * @var integer Duration (duree)
133
     */
134
    public $duree;
135
136
    /**
137
     * @var integer Date modification record (tms)
138
     */
139
    public $datem;
140
141
    /**
142
     * @var User Object user that create action
143
     * @deprecated
144
     * @see $authorid
145
     */
146
    public $author;
147
148
    /**
149
     * @var User Object user that modified action
150
     * @deprecated
151
     * @see $usermodid
152
     */
153
    public $usermod;
154
155
    /**
156
     * @var int Id user that create action
157
     */
158
    public $authorid;
159
160
    /**
161
     * @var int Id user that modified action
162
     */
163
    public $usermodid;
164
165
    /**
166
     * @var integer Date action start (datep)
167
     */
168
    public $datep;
169
170
    /**
171
     * @var integer Date action end (datep2)
172
     */
173
    public $datep2;
174
175
    /**
176
     * @var int -1=Unkown duration
177
     * @deprecated
178
     */
179
    public $durationp = -1;
180
181
    /**
182
     * @var int 1=Event on full day
183
     */
184
    public $fulldayevent = 0;
185
186
    /**
187
     * @var integer Percentage
188
     */
189
    public $percentage;
190
191
    /**
192
     * @var string Location
193
     */
194
    public $location;
195
196
    /**
197
     * @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)
198
     */
199
    public $transparency;
200
201
    /**
202
     * @var int (0 By default)
203
     */
204
    public $priority;
205
206
    /**
207
     * @var int[] Array of user ids
208
     */
209
    public $userassigned = array();
210
211
    /**
212
     * @var int Id of user owner = fk_user_action into table
213
     */
214
    public $userownerid;
215
216
    /**
217
     * @var int Id of user done (deprecated)
218
     * @deprecated
219
     */
220
    public $userdoneid;
221
222
    /**
223
     * @var int[] Array of contact ids
224
     */
225
    public $socpeopleassigned = array();
226
227
    /**
228
     * @var int[] Array of other contact emails (not user, not contact)
229
     */
230
    public $otherassigned = array();
231
232
233
    /**
234
     * @var User Object user of owner
235
     * @deprecated
236
     * @see $userownerid
237
     */
238
    public $usertodo;
239
240
    /**
241
     * @var User Object user that did action
242
     * @deprecated
243
     * @see $userdoneid
244
     */
245
    public $userdone;
246
247
    /**
248
     * @var int thirdparty id linked to action
249
     */
250
    public $socid;
251
252
    /**
253
     * @var int socpeople id linked to action
254
     */
255
    public $contactid;
256
257
    /**
258
     * @var Societe|null Company linked to action (optional)
259
     * @deprecated
260
     * @see $socid
261
     */
262
    public $societe;
263
264
    /**
265
     * @var Contact|null Contact linked to action (optional)
266
     * @deprecated
267
     * @see $contactid
268
     */
269
    public $contact;
270
271
    // Properties for links to other objects
272
    /**
273
     * @var int Id of linked object
274
     */
275
    public $fk_element; // Id of record
276
277
    /**
278
     * @var int Id of record alternative for API
279
     */
280
    public $elementid;
281
282
    /**
283
     * @var string Type of record. This if property ->element of object linked to.
284
     */
285
    public $elementtype;
286
287
    /**
288
     * @var string Ical name
289
     */
290
    public $icalname;
291
292
    /**
293
     * @var string Ical color
294
     */
295
    public $icalcolor;
296
297
    /**
298
     * @var string Extraparam
299
     */
300
    public $extraparams;
301
302
    /**
303
     * @var array Actions
304
     */
305
    public $actions = array();
306
307
    /**
308
     * @var string Email msgid
309
     */
310
    public $email_msgid;
311
312
    /**
313
     * @var string Email from
314
     */
315
    public $email_from;
316
317
    /**
318
     * @var string Email sender
319
     */
320
    public $email_sender;
321
322
    /**
323
     * @var string Email to
324
     */
325
    public $email_to;
326
327
    /**
328
     * @var string Email tocc
329
     */
330
    public $email_tocc;
331
    /**
332
     * @var string Email tobcc
333
     */
334
    public $email_tobcc;
335
336
    /**
337
     * @var string Email subject
338
     */
339
    public $email_subject;
340
341
    /**
342
     * @var string Email errors to
343
     */
344
    public $errors_to;
345
346
	/**
347
	 * Typical value for a event that is in a todo state
348
	 */
349
	const EVENT_TODO = 0;
350
351
	/**
352
	 * Typical value for a event that is in a progress state
353
	 */
354
	const EVENT_IN_PROGRESS = 50;
355
356
	/**
357
	 * Typical value for a event that is in a finished state
358
	 */
359
	const EVENT_FINISHED = 100;
360
361
    /**
362
     *      Constructor
363
     *
364
     *      @param      DoliDB		$db      Database handler
365
     */
366
    public function __construct(DoliDB $db)
367
    {
368
        $this->db = $db;
369
    }
370
371
    /**
372
     *    Add an action/event into database.
373
     *    $this->type_id OR $this->type_code must be set.
374
     *
375
     *    @param	User	$user      		Object user making action
376
     *    @param    int		$notrigger		1 = disable triggers, 0 = enable triggers
377
     *    @return   int 		        	Id of created event, < 0 if KO
378
     */
379
    public function create(User $user, $notrigger = 0)
380
    {
381
        global $langs, $conf, $hookmanager;
382
383
        $error = 0;
384
        $now = dol_now();
385
386
        // Check parameters
387
        if (!isset($this->userownerid) || $this->userownerid === '')	// $this->userownerid may be 0 (anonymous event) of > 0
388
        {
389
            dol_syslog("You tried to create an event but mandatory property ownerid was not defined", LOG_WARNING);
390
        	$this->errors[] = 'ErrorPropertyUserowneridNotDefined';
391
        	return -1;
392
        }
393
394
        // Clean parameters
395
        $this->label = dol_trunc(trim($this->label), 128);
396
        $this->location = dol_trunc(trim($this->location), 128);
397
        $this->note_private = dol_htmlcleanlastbr(trim(empty($this->note_private) ? $this->note : $this->note_private));
398
        if (empty($this->percentage))   $this->percentage = 0;
399
        if (empty($this->priority) || !is_numeric($this->priority)) $this->priority = 0;
400
        if (empty($this->fulldayevent)) $this->fulldayevent = 0;
401
        if (empty($this->transparency)) $this->transparency = 0;
402
        if ($this->percentage > 100) $this->percentage = 100;
403
        //if ($this->percentage == 100 && ! $this->dateend) $this->dateend = $this->date;
404
        if (!empty($this->datep) && !empty($this->datef))   $this->durationp = ($this->datef - $this->datep); // deprecated
405
        //if (! empty($this->date)  && ! empty($this->dateend)) $this->durationa=($this->dateend - $this->date);
406
        if (!empty($this->datep) && !empty($this->datef) && $this->datep > $this->datef) $this->datef = $this->datep;
407
        //if (! empty($this->date)  && ! empty($this->dateend) && $this->date > $this->dateend) $this->dateend=$this->date;
408
        if (!isset($this->fk_project) || $this->fk_project < 0) $this->fk_project = 0;
409
        // For backward compatibility
410
        if ($this->elementtype == 'facture')  $this->elementtype = 'invoice';
411
        if ($this->elementtype == 'commande') $this->elementtype = 'order';
412
        if ($this->elementtype == 'contrat')  $this->elementtype = 'contract';
413
414
        if (!is_array($this->userassigned) && !empty($this->userassigned))	// For backward compatibility when userassigned was an int instead fo array
415
        {
416
        	$tmpid = $this->userassigned;
417
        	$this->userassigned = array();
418
        	$this->userassigned[$tmpid] = array('id'=>$tmpid, 'transparency'=>$this->transparency);
419
        }
420
421
        $userownerid = $this->userownerid;
422
        $userdoneid = $this->userdoneid;
423
424
        // Be sure assigned user is defined as an array of array('id'=>,'mandatory'=>,...).
425
        if (empty($this->userassigned) || count($this->userassigned) == 0 || !is_array($this->userassigned))
426
        	$this->userassigned = array($userownerid=>array('id'=>$userownerid, 'transparency'=>$this->transparency));
427
428
        if (!$this->type_id || !$this->type_code)
429
        {
430
        	$key = empty($this->type_id) ? $this->type_code : $this->type_id;
431
432
            // Get id from code
433
            $cactioncomm = new CActionComm($this->db);
434
            $result = $cactioncomm->fetch($key);
435
436
            if ($result > 0)
437
            {
438
                $this->type_id = $cactioncomm->id;
439
                $this->type_code = $cactioncomm->code;
440
            } elseif ($result == 0)
441
            {
442
                $this->error = 'Failed to get record with id '.$this->type_id.' code '.$this->type_code.' from dictionary "type of events"';
443
                return -1;
444
            } else {
445
                $this->error = $cactioncomm->error;
446
                return -1;
447
            }
448
        }
449
        $code = empty($this->code) ? $this->type_code : $this->code;
450
451
        // Check parameters
452
        if (!$this->type_id)
453
        {
454
            $this->error = "ErrorWrongParameters";
455
            return -1;
456
        }
457
458
        $this->db->begin();
459
460
        $sql = "INSERT INTO ".MAIN_DB_PREFIX."actioncomm";
461
        $sql .= "(datec,";
462
        $sql .= "datep,";
463
        $sql .= "datep2,";
464
        $sql .= "durationp,"; // deprecated
465
        $sql .= "fk_action,";
466
        $sql .= "code,";
467
	 	$sql .= "ref_ext,";
468
        $sql .= "fk_soc,";
469
        $sql .= "fk_project,";
470
        $sql .= "note,";
471
        $sql .= "fk_contact,";
472
        $sql .= "fk_user_author,";
473
        $sql .= "fk_user_action,";
474
        $sql .= "fk_user_done,";
475
        $sql .= "label,percent,priority,fulldayevent,location,";
476
        $sql .= "transparency,";
477
        $sql .= "fk_element,";
478
        $sql .= "elementtype,";
479
        $sql .= "entity,";
480
        $sql .= "extraparams,";
481
		// Fields emails
482
        $sql .= "email_msgid,";
483
        $sql .= "email_from,";
484
        $sql .= "email_sender,";
485
        $sql .= "email_to,";
486
        $sql .= "email_tocc,";
487
        $sql .= "email_tobcc,";
488
        $sql .= "email_subject,";
489
        $sql .= "errors_to";
490
        $sql .= ") VALUES (";
491
        $sql .= "'".$this->db->idate($now)."', ";
492
        $sql .= (strval($this->datep) != '' ? "'".$this->db->idate($this->datep)."'" : "null").", ";
493
        $sql .= (strval($this->datef) != '' ? "'".$this->db->idate($this->datef)."'" : "null").", ";
494
        $sql .= ((isset($this->durationp) && $this->durationp >= 0 && $this->durationp != '') ? "'".$this->db->escape($this->durationp)."'" : "null").", "; // deprecated
495
        $sql .= (isset($this->type_id) ? $this->type_id : "null").",";
496
        $sql .= ($code ? ("'".$this->db->escape($code)."'") : "null").", ";
497
        $sql .= ($this->ref_ext ? ("'".$this->db->idate($this->ref_ext)."'") : "null").", ";
498
        $sql .= ((isset($this->socid) && $this->socid > 0) ? $this->socid : "null").", ";
499
        $sql .= ((isset($this->fk_project) && $this->fk_project > 0) ? $this->fk_project : "null").", ";
500
        $sql .= " '".$this->db->escape($this->note_private)."', ";
501
        $sql .= ((isset($this->contact_id) && $this->contact_id > 0) ? $this->contact_id : "null").", ";	// deprecated, use ->socpeopleassigned
502
        $sql .= (isset($user->id) && $user->id > 0 ? $user->id : "null").", ";
503
        $sql .= ($userownerid > 0 ? $userownerid : "null").", ";
504
        $sql .= ($userdoneid > 0 ? $userdoneid : "null").", ";
505
        $sql .= "'".$this->db->escape($this->label)."','".$this->db->escape($this->percentage)."','".$this->db->escape($this->priority)."','".$this->db->escape($this->fulldayevent)."','".$this->db->escape($this->location)."', ";
506
        $sql .= "'".$this->db->escape($this->transparency)."', ";
507
        $sql .= (!empty($this->fk_element) ? $this->fk_element : "null").", ";
508
        $sql .= (!empty($this->elementtype) ? "'".$this->db->escape($this->elementtype)."'" : "null").", ";
509
        $sql .= $conf->entity.",";
510
        $sql .= (!empty($this->extraparams) ? "'".$this->db->escape($this->extraparams)."'" : "null").", ";
511
        // Fields emails
512
        $sql .= (!empty($this->email_msgid) ? "'".$this->db->escape($this->email_msgid)."'" : "null").", ";
513
        $sql .= (!empty($this->email_from) ? "'".$this->db->escape($this->email_from)."'" : "null").", ";
514
        $sql .= (!empty($this->email_sender) ? "'".$this->db->escape($this->email_sender)."'" : "null").", ";
515
        $sql .= (!empty($this->email_to) ? "'".$this->db->escape($this->email_to)."'" : "null").", ";
516
        $sql .= (!empty($this->email_tocc) ? "'".$this->db->escape($this->email_tocc)."'" : "null").", ";
517
        $sql .= (!empty($this->email_tobcc) ? "'".$this->db->escape($this->email_tobcc)."'" : "null").", ";
518
        $sql .= (!empty($this->email_subject) ? "'".$this->db->escape($this->email_subject)."'" : "null").", ";
519
        $sql .= (!empty($this->errors_to) ? "'".$this->db->escape($this->errors_to)."'" : "null");
520
        $sql .= ")";
521
522
        dol_syslog(get_class($this)."::add", LOG_DEBUG);
523
        $resql = $this->db->query($sql);
524
        if ($resql)
525
        {
526
            $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."actioncomm", "id");
527
528
            // Now insert assigned users
529
			if (!$error)
530
			{
531
				//dol_syslog(var_export($this->userassigned, true));
532
				foreach ($this->userassigned as $key => $val)
533
				{
534
			        if (!is_array($val))	// For backward compatibility when val=id
535
			        {
536
			        	$val = array('id'=>$val);
537
			        }
538
539
			        if ($val['id'] > 0)
540
			        {
541
						$sql = "INSERT INTO ".MAIN_DB_PREFIX."actioncomm_resources(fk_actioncomm, element_type, fk_element, mandatory, transparency, answer_status)";
542
						$sql .= " VALUES(".$this->id.", 'user', ".$val['id'].", ".(empty($val['mandatory']) ? '0' : $val['mandatory']).", ".(empty($val['transparency']) ? '0' : $val['transparency']).", ".(empty($val['answer_status']) ? '0' : $val['answer_status']).")";
543
544
						$resql = $this->db->query($sql);
545
						if (!$resql)
546
						{
547
							$error++;
548
							dol_syslog('Error to process userassigned: '.$this->db->lasterror(), LOG_ERR);
549
			           		$this->errors[] = $this->db->lasterror();
550
						}
551
						//var_dump($sql);exit;
552
			        }
553
				}
554
			}
555
556
			if (!$error)
557
			{
558
				if (!empty($this->socpeopleassigned))
559
				{
560
					foreach ($this->socpeopleassigned as $id => $val)
561
					{
562
						$sql = "INSERT INTO ".MAIN_DB_PREFIX."actioncomm_resources(fk_actioncomm, element_type, fk_element, mandatory, transparency, answer_status)";
563
						$sql .= " VALUES(".$this->id.", 'socpeople', ".$id.", 0, 0, 0)";
564
565
						$resql = $this->db->query($sql);
566
						if (!$resql)
567
						{
568
							$error++;
569
							dol_syslog('Error to process socpeopleassigned: '.$this->db->lasterror(), LOG_ERR);
570
							$this->errors[] = $this->db->lasterror();
571
						}
572
					}
573
				}
574
			}
575
576
            if (!$error)
577
            {
578
	            // Actions on extra fields
579
           		$result = $this->insertExtraFields();
580
           		if ($result < 0)
581
           		{
582
           			$error++;
583
           		}
584
            }
585
586
            if (!$error && !$notrigger)
587
            {
588
                // Call trigger
589
                $result = $this->call_trigger('ACTION_CREATE', $user);
590
                if ($result < 0) { $error++; }
591
                // End call triggers
592
            }
593
594
            if (!$error)
595
            {
596
            	$this->db->commit();
597
            	return $this->id;
598
            } else {
599
                $this->db->rollback();
600
                return -1;
601
            }
602
        } else {
603
            $this->db->rollback();
604
            $this->error = $this->db->lasterror();
605
            return -1;
606
        }
607
    }
608
609
    /**
610
     *  Load an object from its id and create a new one in database
611
     *
612
     *  @param	    User	        $fuser      	Object user making action
613
	 *  @param		int				$socid			Id of thirdparty
614
     *  @return		int								New id of clone
615
     */
616
    public function createFromClone(User $fuser, $socid)
617
    {
618
        global $db, $conf, $hookmanager;
619
620
        $error = 0;
621
        $now = dol_now();
622
623
        $this->db->begin();
624
625
		// Load source object
626
		$objFrom = clone $this;
627
628
		// Retreive all extrafield
629
		// fetch optionals attributes and labels
630
		$this->fetch_optionals();
631
632
		//$this->fetch_userassigned();
633
		$this->fetchResources();
634
635
        $this->id = 0;
636
637
        // Create clone
638
		$this->context['createfromclone'] = 'createfromclone';
639
		$result = $this->create($fuser);
640
        if ($result < 0) $error++;
641
642
        if (!$error)
643
        {
644
            // Hook of thirdparty module
645
            if (is_object($hookmanager))
646
            {
647
                $parameters = array('objFrom'=>$objFrom);
648
                $action = '';
649
                $reshook = $hookmanager->executeHooks('createFrom', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
650
                if ($reshook < 0) $error++;
651
            }
652
653
            // Call trigger
654
            $result = $this->call_trigger('ACTION_CLONE', $fuser);
655
            if ($result < 0) { $error++; }
656
            // End call triggers
657
        }
658
659
        unset($this->context['createfromclone']);
660
661
        // End
662
        if (!$error)
663
        {
664
            $this->db->commit();
665
            return $this->id;
666
        } else {
667
            $this->db->rollback();
668
            return -1;
669
        }
670
    }
671
672
    /**
673
     *  Load object from database
674
     *
675
     *  @param  int		$id     		Id of action to get
676
     *  @param  string	$ref    		Ref of action to get
677
     *  @param  string	$ref_ext		Ref ext to get
678
	 *  @param	string	$email_msgid	Email msgid
679
     *  @return	int						<0 if KO, >0 if OK
680
     */
681
    public function fetch($id, $ref = '', $ref_ext = '', $email_msgid = '')
682
    {
683
        global $langs;
684
685
        $sql = "SELECT a.id,";
686
        $sql .= " a.id as ref,";
687
        $sql .= " a.entity,";
688
        $sql .= " a.ref_ext,";
689
        $sql .= " a.datep,";
690
        $sql .= " a.datep2,";
691
        $sql .= " a.durationp,"; // deprecated
692
        $sql .= " a.datec,";
693
        $sql .= " a.tms as datem,";
694
        $sql .= " a.code, a.label, a.note,";
695
        $sql .= " a.fk_soc,";
696
        $sql .= " a.fk_project,";
697
        $sql .= " a.fk_user_author, a.fk_user_mod,";
698
        $sql .= " a.fk_user_action, a.fk_user_done,";
699
        $sql .= " a.fk_contact, a.percent as percentage,";
700
        $sql .= " a.fk_element as elementid, a.elementtype,";
701
        $sql .= " a.priority, a.fulldayevent, a.location, a.transparency,";
702
        $sql .= " a.email_msgid, a.email_subject, a.email_from, a.email_to, a.email_tocc, a.email_tobcc, a.errors_to,";
703
        $sql .= " c.id as type_id, c.code as type_code, c.libelle as type_label, c.color as type_color, c.picto as type_picto,";
704
        $sql .= " s.nom as socname,";
705
        $sql .= " u.firstname, u.lastname as lastname";
706
        $sql .= " FROM ".MAIN_DB_PREFIX."actioncomm as a ";
707
        $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_actioncomm as c ON a.fk_action=c.id ";
708
        $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as u on u.rowid = a.fk_user_author";
709
        $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s on s.rowid = a.fk_soc";
710
        $sql .= " WHERE ";
711
        if ($ref) $sql .= " a.id = ".((int) $ref); // No field ref, we use id
712
        elseif ($ref_ext) $sql .= " a.ref_ext = '".$this->db->escape($ref_ext)."'";
713
        elseif ($email_msgid) $sql .= " a.email_msgid = '".$this->db->escape($email_msgid)."'";
714
        else $sql .= " a.id = ".((int) $id);
715
716
        dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
717
        $resql = $this->db->query($sql);
718
        if ($resql)
719
        {
720
        	$num = $this->db->num_rows($resql);
721
            if ($num)
722
            {
723
                $obj = $this->db->fetch_object($resql);
724
725
                $this->id         = $obj->id;
726
				$this->entity     = $obj->entity;
727
                $this->ref        = $obj->ref;
728
                $this->ref_ext    = $obj->ref_ext;
729
730
                // Properties of parent table llx_c_actioncomm
731
                $this->type_id    = $obj->type_id;
732
                $this->type_code  = $obj->type_code;
733
                $this->type_color = $obj->type_color;
734
                $this->type_picto = $obj->type_picto;
735
                $transcode = $langs->trans("Action".$obj->type_code);
736
                $this->type       = (($transcode != "Action".$obj->type_code) ? $transcode : $obj->type_label);
737
                $transcode = $langs->trans("Action".$obj->type_code.'Short');
738
                $this->type_short = (($transcode != "Action".$obj->type_code.'Short') ? $transcode : '');
739
740
				$this->code = $obj->code;
741
                $this->label = $obj->label;
742
                $this->datep = $this->db->jdate($obj->datep);
743
                $this->datef = $this->db->jdate($obj->datep2);
744
745
                $this->datec = $this->db->jdate($obj->datec);
746
                $this->datem = $this->db->jdate($obj->datem);
747
748
                $this->note = $obj->note; // deprecated
749
                $this->note_private = $obj->note;
750
                $this->percentage = $obj->percentage;
751
752
                $this->authorid = $obj->fk_user_author;
753
                $this->usermodid = $obj->fk_user_mod;
754
755
                if (!is_object($this->author)) $this->author = new stdClass(); // To avoid warning
756
                $this->author->id = $obj->fk_user_author; // deprecated
757
                $this->author->firstname = $obj->firstname; // deprecated
758
                $this->author->lastname = $obj->lastname; // deprecated
759
                if (!is_object($this->usermod)) $this->usermod = new stdClass(); // To avoid warning
760
                $this->usermod->id = $obj->fk_user_mod; // deprecated
761
762
                $this->userownerid = $obj->fk_user_action;
763
                $this->userdoneid = $obj->fk_user_done;
764
                $this->priority				= $obj->priority;
765
                $this->fulldayevent			= $obj->fulldayevent;
766
                $this->location				= $obj->location;
767
                $this->transparency			= $obj->transparency;
768
769
                $this->socid = $obj->fk_soc; // To have fetch_thirdparty method working
770
                $this->contact_id			= $obj->fk_contact; // To have fetch_contact method working
771
                $this->fk_project = $obj->fk_project; // To have fetch_projet method working
772
773
                //$this->societe->id			= $obj->fk_soc;			// deprecated
774
                //$this->contact->id			= $obj->fk_contact;		// deprecated
775
776
                $this->fk_element = $obj->elementid;
777
                $this->elementid = $obj->elementid;
778
                $this->elementtype = $obj->elementtype;
779
780
                $this->fetchResources();
781
            }
782
            $this->db->free($resql);
783
        } else {
784
            $this->error = $this->db->lasterror();
785
            return -1;
786
        }
787
788
        return $num;
789
    }
790
791
    /**
792
     *    Initialize $this->userassigned & this->socpeopleassigned array with list of id of user and contact assigned to event
793
     *
794
     *    @return   int				<0 if KO, >0 if OK
795
     */
796
    public function fetchResources()
797
    {
798
    	$this->userassigned = array();
799
    	$this->socpeopleassigned = array();
800
801
    	$sql = 'SELECT fk_actioncomm, element_type, fk_element, answer_status, mandatory, transparency';
802
		$sql .= ' FROM '.MAIN_DB_PREFIX.'actioncomm_resources';
803
		$sql .= ' WHERE fk_actioncomm = '.$this->id;
804
		$sql .= " AND element_type IN ('user', 'socpeople')";
805
		$resql = $this->db->query($sql);
806
		if ($resql)
807
		{
808
			// If owner is known, we must but id first into list
809
			if ($this->userownerid > 0) $this->userassigned[$this->userownerid] = array('id'=>$this->userownerid); // Set first so will be first into list.
810
811
            while ($obj = $this->db->fetch_object($resql))
812
            {
813
            	if ($obj->fk_element > 0)
814
				{
815
					switch ($obj->element_type) {
816
						case 'user':
817
							$this->userassigned[$obj->fk_element] = array('id'=>$obj->fk_element, 'mandatory'=>$obj->mandatory, 'answer_status'=>$obj->answer_status, 'transparency'=>$obj->transparency);
818
							if (empty($this->userownerid)) $this->userownerid = $obj->fk_element; // If not defined (should not happened, we fix this)
819
							break;
820
						case 'socpeople':
821
							$this->socpeopleassigned[$obj->fk_element] = array('id'=>$obj->fk_element, 'mandatory'=>$obj->mandatory, 'answer_status'=>$obj->answer_status, 'transparency'=>$obj->transparency);
822
							break;
823
					}
824
				}
825
            }
826
827
        	return 1;
828
		} else {
829
			dol_print_error($this->db);
830
			return -1;
831
		}
832
	}
833
834
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
835
    /**
836
     *    Initialize this->userassigned array with list of id of user assigned to event
837
     *
838
     *    @param    bool    $override   Override $this->userownerid when empty. TODO This should be false by default. True is here to fix corrupted data.
839
     *    @return   int                 <0 if KO, >0 if OK
840
     */
841
    public function fetch_userassigned($override = true)
842
    {
843
        // phpcs:enable
844
        $sql = "SELECT fk_actioncomm, element_type, fk_element, answer_status, mandatory, transparency";
845
        $sql .= " FROM ".MAIN_DB_PREFIX."actioncomm_resources";
846
        $sql .= " WHERE element_type = 'user' AND fk_actioncomm = ".$this->id;
847
848
        $resql2 = $this->db->query($sql);
849
        if ($resql2)
850
        {
851
            $this->userassigned = array();
852
853
            // If owner is known, we must but id first into list
854
            if ($this->userownerid > 0)
855
            {
856
                // Set first so will be first into list.
857
                $this->userassigned[$this->userownerid] = array('id'=>$this->userownerid);
858
            }
859
860
            while ($obj = $this->db->fetch_object($resql2))
861
            {
862
                if ($obj->fk_element > 0)
863
                {
864
                    $this->userassigned[$obj->fk_element] = array('id'=>$obj->fk_element,
865
                                                                  'mandatory'=>$obj->mandatory,
866
                                                                  'answer_status'=>$obj->answer_status,
867
                                                                  'transparency'=>$obj->transparency);
868
                }
869
870
                if ($override === true)
871
                {
872
                    // If not defined (should not happened, we fix this)
873
                    if (empty($this->userownerid))
874
                    {
875
                        $this->userownerid = $obj->fk_element;
876
                    }
877
                }
878
            }
879
880
            return 1;
881
        } else {
882
            dol_print_error($this->db);
883
            return -1;
884
        }
885
    }
886
887
    /**
888
     *    Delete event from database
889
     *
890
     *    @param    int		$notrigger		1 = disable triggers, 0 = enable triggers
891
     *    @return   int 					<0 if KO, >0 if OK
892
     */
893
    public function delete($notrigger = 0)
894
    {
895
        global $user;
896
897
        $error = 0;
898
899
        dol_syslog(get_class($this)."::delete", LOG_DEBUG);
900
901
        $this->db->begin();
902
903
        // remove categorie association
904
        if (!$error) {
905
			$sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_actioncomm";
906
			$sql .= " WHERE fk_actioncomm=".$this->id;
907
908
			$res = $this->db->query($sql);
909
			if (!$res) {
910
				$this->error = $this->db->lasterror();
911
				$error++;
912
			}
913
        }
914
915
        // remove actioncomm_resources
916
        if (!$error) {
917
            $sql = "DELETE FROM ".MAIN_DB_PREFIX."actioncomm_resources";
918
            $sql .= " WHERE fk_actioncomm=".$this->id;
919
920
            $res = $this->db->query($sql);
921
            if (!$res) {
922
                $this->error = $this->db->lasterror();
923
                $error++;
924
            }
925
        }
926
927
        // Removed extrafields
928
        if (!$error) {
929
        	  $result = $this->deleteExtraFields();
930
          	if ($result < 0)
931
           	{
932
           		$error++;
933
           		dol_syslog(get_class($this)."::delete error -3 ".$this->error, LOG_ERR);
934
           	}
935
        }
936
937
        // remove actioncomm
938
        if (!$error) {
939
            $sql = "DELETE FROM ".MAIN_DB_PREFIX."actioncomm";
940
            $sql .= " WHERE id=".$this->id;
941
942
            $res = $this->db->query($sql);
943
            if (!$res) {
944
                $this->error = $this->db->lasterror();
945
                $error++;
946
            }
947
        }
948
949
        if (!$error)
950
        {
951
            if (!$notrigger)
952
            {
953
                // Call trigger
954
                $result = $this->call_trigger('ACTION_DELETE', $user);
955
                if ($result < 0) { $error++; }
956
                // End call triggers
957
            }
958
959
            if (!$error)
960
            {
961
                $this->db->commit();
962
                return 1;
963
            } else {
964
                $this->db->rollback();
965
                return -2;
966
            }
967
        } else {
968
            $this->db->rollback();
969
            $this->error = $this->db->lasterror();
970
            return -1;
971
        }
972
    }
973
974
    /**
975
     *    Update action into database
976
     *	  If percentage = 100, on met a jour date 100%
977
     *
978
     *    @param    User	$user			Object user making change
979
     *    @param    int		$notrigger		1 = disable triggers, 0 = enable triggers
980
     *    @return   int     				<0 if KO, >0 if OK
981
     */
982
    public function update($user, $notrigger = 0)
983
    {
984
        global $langs, $conf, $hookmanager;
985
986
        $error = 0;
987
988
        // Clean parameters
989
        $this->label = trim($this->label);
990
        $this->note_private = dol_htmlcleanlastbr(trim(empty($this->note_private) ? $this->note : $this->note_private));
991
        if (empty($this->percentage))    $this->percentage = 0;
992
        if (empty($this->priority) || !is_numeric($this->priority)) $this->priority = 0;
993
        if (empty($this->transparency))  $this->transparency = 0;
994
        if (empty($this->fulldayevent))  $this->fulldayevent = 0;
995
        if ($this->percentage > 100) $this->percentage = 100;
996
        //if ($this->percentage == 100 && ! $this->dateend) $this->dateend = $this->date;
997
        if ($this->datep && $this->datef)   $this->durationp = ($this->datef - $this->datep); // deprecated
1 ignored issue
show
Deprecated Code introduced by
The property ActionComm::$durationp has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

997
        if ($this->datep && $this->datef)   /** @scrutinizer ignore-deprecated */ $this->durationp = ($this->datef - $this->datep); // deprecated
Loading history...
998
        //if ($this->date  && $this->dateend) $this->durationa=($this->dateend - $this->date);
999
        if ($this->datep && $this->datef && $this->datep > $this->datef) $this->datef = $this->datep;
1000
        //if ($this->date  && $this->dateend && $this->date > $this->dateend) $this->dateend=$this->date;
1001
        if ($this->fk_project < 0) $this->fk_project = 0;
1002
1003
        // Check parameters
1004
        if ($this->percentage == 0 && $this->userdoneid > 0)
1 ignored issue
show
Deprecated Code introduced by
The property ActionComm::$userdoneid has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1004
        if ($this->percentage == 0 && /** @scrutinizer ignore-deprecated */ $this->userdoneid > 0)
Loading history...
1005
        {
1006
            $this->error = "ErrorCantSaveADoneUserWithZeroPercentage";
1007
            return -1;
1008
        }
1009
1010
        $socid = (($this->socid > 0) ? $this->socid : 0);
1011
        $contactid = (($this->contact_id > 0) ? $this->contact_id : 0);
1012
		$userownerid = ($this->userownerid ? $this->userownerid : 0);
1013
		$userdoneid = ($this->userdoneid ? $this->userdoneid : 0);
1 ignored issue
show
Deprecated Code introduced by
The property ActionComm::$userdoneid has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1013
		$userdoneid = ($this->userdoneid ? /** @scrutinizer ignore-deprecated */ $this->userdoneid : 0);
Loading history...
1014
1015
        $this->db->begin();
1016
1017
        $sql = "UPDATE ".MAIN_DB_PREFIX."actioncomm ";
1018
        $sql .= " SET percent = '".$this->db->escape($this->percentage)."'";
1019
        if ($this->type_id > 0) $sql .= ", fk_action = '".$this->db->escape($this->type_id)."'";
1020
        $sql .= ", label = ".($this->label ? "'".$this->db->escape($this->label)."'" : "null");
1021
        $sql .= ", datep = ".(strval($this->datep) != '' ? "'".$this->db->idate($this->datep)."'" : 'null');
1022
        $sql .= ", datep2 = ".(strval($this->datef) != '' ? "'".$this->db->idate($this->datef)."'" : 'null');
1023
        $sql .= ", durationp = ".(isset($this->durationp) && $this->durationp >= 0 && $this->durationp != '' ? "'".$this->db->escape($this->durationp)."'" : "null"); // deprecated
1 ignored issue
show
Deprecated Code introduced by
The property ActionComm::$durationp has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1023
        $sql .= ", durationp = ".(isset(/** @scrutinizer ignore-deprecated */ $this->durationp) && $this->durationp >= 0 && $this->durationp != '' ? "'".$this->db->escape($this->durationp)."'" : "null"); // deprecated
Loading history...
1024
        $sql .= ", note = '".$this->db->escape($this->note_private)."'";
1025
        $sql .= ", fk_project =".($this->fk_project > 0 ? $this->fk_project : "null");
1026
        $sql .= ", fk_soc =".($socid > 0 ? $socid : "null");
1027
        $sql .= ", fk_contact =".($contactid > 0 ? $contactid : "null");
1028
        $sql .= ", priority = '".$this->db->escape($this->priority)."'";
1029
        $sql .= ", fulldayevent = '".$this->db->escape($this->fulldayevent)."'";
1030
        $sql .= ", location = ".($this->location ? "'".$this->db->escape($this->location)."'" : "null");
1031
        $sql .= ", transparency = '".$this->db->escape($this->transparency)."'";
1032
        $sql .= ", fk_user_mod = ".$user->id;
1033
        $sql .= ", fk_user_action=".($userownerid > 0 ? "'".$userownerid."'" : "null");
1034
        $sql .= ", fk_user_done=".($userdoneid > 0 ? "'".$userdoneid."'" : "null");
1035
        if (!empty($this->fk_element)) $sql .= ", fk_element=".($this->fk_element ? $this->db->escape($this->fk_element) : "null");
1036
        if (!empty($this->elementtype)) $sql .= ", elementtype=".($this->elementtype ? "'".$this->db->escape($this->elementtype)."'" : "null");
1037
        $sql .= " WHERE id=".$this->id;
1038
1039
        dol_syslog(get_class($this)."::update", LOG_DEBUG);
1040
        if ($this->db->query($sql))
1041
        {
1042
			$action = 'update';
1043
1044
        	// Actions on extra fields
1045
       		if (!$error)
1046
       		{
1047
       			$result = $this->insertExtraFields();
1048
       			if ($result < 0)
1049
       			{
1050
       				$error++;
1051
       			}
1052
        	}
1053
1054
            // Now insert assignedusers
1055
			if (!$error)
1056
			{
1057
				$sql = "DELETE FROM ".MAIN_DB_PREFIX."actioncomm_resources where fk_actioncomm = ".$this->id." AND element_type = 'user'";
1058
				$resql = $this->db->query($sql);
1059
1060
				foreach ($this->userassigned as $key => $val)
1061
				{
1062
			        if (!is_array($val))	// For backward compatibility when val=id
1063
			        {
1064
			        	$val = array('id'=>$val);
1065
			        }
1066
					$sql = "INSERT INTO ".MAIN_DB_PREFIX."actioncomm_resources(fk_actioncomm, element_type, fk_element, mandatory, transparency, answer_status)";
1067
					$sql .= " VALUES(".$this->id.", 'user', ".$val['id'].", ".(empty($val['mandatory']) ? '0' : $val['mandatory']).", ".(empty($val['transparency']) ? '0' : $val['transparency']).", ".(empty($val['answer_status']) ? '0' : $val['answer_status']).")";
1068
1069
					$resql = $this->db->query($sql);
1070
					if (!$resql)
1071
					{
1072
						$error++;
1073
		           		$this->errors[] = $this->db->lasterror();
1074
					}
1075
					//var_dump($sql);exit;
1076
				}
1077
			}
1078
1079
			if (!$error)
1080
			{
1081
				$sql = "DELETE FROM ".MAIN_DB_PREFIX."actioncomm_resources where fk_actioncomm = ".$this->id." AND element_type = 'socpeople'";
1082
				$resql = $this->db->query($sql);
1083
1084
				if (!empty($this->socpeopleassigned))
1085
				{
1086
					foreach (array_keys($this->socpeopleassigned) as $id)
1087
					{
1088
						$sql = "INSERT INTO ".MAIN_DB_PREFIX."actioncomm_resources(fk_actioncomm, element_type, fk_element, mandatory, transparency, answer_status)";
1089
						$sql .= " VALUES(".$this->id.", 'socpeople', ".$id.", 0, 0, 0)";
1090
1091
						$resql = $this->db->query($sql);
1092
						if (!$resql)
1093
						{
1094
							$error++;
1095
							$this->errors[] = $this->db->lasterror();
1096
						}
1097
					}
1098
				}
1099
			}
1100
1101
            if (!$error && !$notrigger)
1102
            {
1103
                // Call trigger
1104
                $result = $this->call_trigger('ACTION_MODIFY', $user);
1105
                if ($result < 0) { $error++; }
1106
                // End call triggers
1107
            }
1108
1109
            if (!$error)
1110
            {
1111
                $this->db->commit();
1112
                return 1;
1113
            } else {
1114
                $this->db->rollback();
1115
                dol_syslog(get_class($this)."::update ".join(',', $this->errors), LOG_ERR);
1116
                return -2;
1117
            }
1118
        } else {
1119
            $this->db->rollback();
1120
            $this->error = $this->db->lasterror();
1121
            return -1;
1122
        }
1123
    }
1124
1125
    /**
1126
     *  Load all objects with filters.
1127
     *  @todo WARNING: This make a fetch on all records instead of making one request with a join.
1128
     *
1129
     *  @param		DoliDb	$db				Database handler
1130
     *  @param		int		$socid			Filter by thirdparty
1131
     *  @param		int		$fk_element		Id of element action is linked to
1132
     *  @param		string	$elementtype	Type of element action is linked to
1133
     *  @param		string	$filter			Other filter
1134
     *  @param		string	$sortfield		Sort on this field
1135
     *  @param		string	$sortorder		ASC or DESC
1136
     *  @param		string	$limit			Limit number of answers
1137
     *  @return		array|string			Error string if KO, array with actions if OK
1138
     */
1139
    public static function getActions($db, $socid = 0, $fk_element = 0, $elementtype = '', $filter = '', $sortfield = 'a.datep', $sortorder = 'DESC', $limit = 0)
1140
    {
1141
        global $conf, $langs;
1142
1143
        $resarray = array();
1144
1145
        dol_syslog(get_class()."::getActions", LOG_DEBUG);
1146
1147
        $sql = "SELECT a.id";
1148
        $sql .= " FROM ".MAIN_DB_PREFIX."actioncomm as a";
1149
        $sql .= " WHERE a.entity IN (".getEntity('agenda').")";
1150
        if (!empty($socid)) $sql .= " AND a.fk_soc = ".$socid;
1151
        if (!empty($elementtype))
1152
        {
1153
        	if ($elementtype == 'project') {
1154
        		$sql .= ' AND a.fk_project = '.$fk_element;
1155
        	}
1156
            elseif ($elementtype == 'contact') {
1157
            	$sql .= ' AND a.id IN';
1158
            	$sql .= " (SELECT fk_actioncomm FROM ".MAIN_DB_PREFIX."actioncomm_resources WHERE";
1159
            	$sql .= " element_type = 'socpeople' AND fk_element = ".$fk_element.')';
1160
            }
1161
            else {
1162
            	$sql .= " AND a.fk_element = ".(int) $fk_element." AND a.elementtype = '".$elementtype."'";
1163
            }
1164
        }
1165
        if (!empty($filter)) $sql .= $filter;
1166
		if ($sortorder && $sortfield) $sql .= $db->order($sortfield, $sortorder);
1167
		$sql .= $db->plimit($limit, 0);
1168
1169
        $resql = $db->query($sql);
1170
        if ($resql)
1171
        {
1172
            $num = $db->num_rows($resql);
1173
1174
            if ($num)
1175
            {
1176
                for ($i = 0; $i < $num; $i++)
1177
                {
1178
                    $obj = $db->fetch_object($resql);
1179
                    $actioncommstatic = new ActionComm($db);
1180
                    $actioncommstatic->fetch($obj->id);
1181
                    $resarray[$i] = $actioncommstatic;
1182
                }
1183
            }
1184
            $db->free($resql);
1185
            return $resarray;
1186
        } else {
1187
            return $db->lasterror();
1188
        }
1189
    }
1190
1191
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1192
    /**
1193
     * Load indicators for dashboard (this->nbtodo and this->nbtodolate)
1194
     *
1195
     * @param	User	$user   			Objet user
1196
     * @param	int		$load_state_board	Charge indicateurs this->nb de tableau de bord
1197
     * @return WorkboardResponse|int <0 if KO, WorkboardResponse if OK
1198
     */
1199
    public function load_board($user, $load_state_board = 0)
1200
    {
1201
        // phpcs:enable
1202
        global $conf, $langs;
1203
1204
    	if (empty($load_state_board)) $sql = "SELECT a.id, a.datep as dp";
1205
    	else {
1206
    		$this->nb = array();
1207
    		$sql = "SELECT count(a.id) as nb";
1208
    	}
1209
    	$sql .= " FROM ".MAIN_DB_PREFIX."actioncomm as a";
1210
    	if (!$user->rights->societe->client->voir && !$user->socid) $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON a.fk_soc = sc.fk_soc";
1211
    	$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON a.fk_soc = s.rowid";
1212
    	$sql .= " WHERE 1 = 1";
1213
    	if (empty($load_state_board)) $sql .= " AND a.percent >= 0 AND a.percent < 100";
1214
    	$sql .= " AND a.entity IN (".getEntity('agenda').")";
1215
    	if (!$user->rights->societe->client->voir && !$user->socid) $sql .= " AND (a.fk_soc IS NULL OR sc.fk_user = ".$user->id.")";
1216
    	if ($user->socid) $sql .= " AND a.fk_soc = ".$user->socid;
1217
    	if (!$user->rights->agenda->allactions->read) $sql .= " AND (a.fk_user_author = ".$user->id." OR a.fk_user_action = ".$user->id." OR a.fk_user_done = ".$user->id.")";
1218
1219
    	$resql = $this->db->query($sql);
1220
    	if ($resql)
1221
    	{
1222
    		if (empty($load_state_board)) {
1223
	    		$agenda_static = new ActionComm($this->db);
1224
	    		$response = new WorkboardResponse();
1225
	    		$response->warning_delay = $conf->agenda->warning_delay / 60 / 60 / 24;
1226
	    		$response->label = $langs->trans("ActionsToDo");
1227
	    		$response->labelShort = $langs->trans("ActionsToDoShort");
1228
	    		$response->url = DOL_URL_ROOT.'/comm/action/list.php?action=show_list&actioncode=0&status=todo&mainmenu=agenda';
1229
	    		if ($user->rights->agenda->allactions->read) $response->url .= '&filtert=-1';
1230
	    		$response->img = img_object('', "action", 'class="inline-block valigntextmiddle"');
1231
    		}
1232
    		// This assignment in condition is not a bug. It allows walking the results.
1233
    		while ($obj = $this->db->fetch_object($resql))
1234
    		{
1235
    			if (empty($load_state_board)) {
1236
	    			$response->nbtodo++;
1 ignored issue
show
Comprehensibility Best Practice introduced by
The variable $response does not seem to be defined for all execution paths leading up to this point.
Loading history...
1237
	    			$agenda_static->datep = $this->db->jdate($obj->dp);
1 ignored issue
show
Comprehensibility Best Practice introduced by
The variable $agenda_static does not seem to be defined for all execution paths leading up to this point.
Loading history...
1238
	    			if ($agenda_static->hasDelay()) $response->nbtodolate++;
1239
    			} else $this->nb["actionscomm"] = $obj->nb;
1240
    		}
1241
1242
    		$this->db->free($resql);
1243
    		if (empty($load_state_board)) return $response;
1244
    		else return 1;
1245
    	} else {
1246
    		dol_print_error($this->db);
1247
    		$this->error = $this->db->error();
1248
    		return -1;
1249
    	}
1250
    }
1251
1252
1253
    /**
1254
     *  Charge les informations d'ordre info dans l'objet facture
1255
     *
1256
     *  @param	int		$id       	Id de la facture a charger
1257
     *  @return	void
1258
     */
1259
    public function info($id)
1260
    {
1261
        $sql = 'SELECT ';
1262
        $sql .= ' a.id,';
1263
        $sql .= ' datec,';
1264
        $sql .= ' tms as datem,';
1265
        $sql .= ' fk_user_author,';
1266
        $sql .= ' fk_user_mod';
1267
        $sql .= ' FROM '.MAIN_DB_PREFIX.'actioncomm as a';
1268
        $sql .= ' WHERE a.id = '.$id;
1269
1270
        dol_syslog(get_class($this)."::info", LOG_DEBUG);
1271
        $result = $this->db->query($sql);
1272
        if ($result)
1273
        {
1274
            if ($this->db->num_rows($result))
1275
            {
1276
                $obj = $this->db->fetch_object($result);
1277
                $this->id = $obj->id;
1278
                if ($obj->fk_user_author)
1279
                {
1280
                    $cuser = new User($this->db);
1281
                    $cuser->fetch($obj->fk_user_author);
1282
                    $this->user_creation = $cuser;
1283
                }
1284
                if ($obj->fk_user_mod)
1285
                {
1286
                    $muser = new User($this->db);
1287
                    $muser->fetch($obj->fk_user_mod);
1288
                    $this->user_modification = $muser;
1289
                }
1290
1291
                $this->date_creation = $this->db->jdate($obj->datec);
1292
                if (!empty($obj->fk_user_mod)) $this->date_modification = $this->db->jdate($obj->datem);
1293
            }
1294
            $this->db->free($result);
1295
        } else {
1296
            dol_print_error($this->db);
1297
        }
1298
    }
1299
1300
1301
    /**
1302
     *  Return label of status
1303
     *
1304
     *  @param	int		$mode           0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
1305
     *  @param  int		$hidenastatus   1=Show nothing if status is "Not applicable"
1306
     *  @return string          		String with status
1307
     */
1308
    public function getLibStatut($mode, $hidenastatus = 0)
1309
    {
1310
        return $this->LibStatut($this->percentage, $mode, $hidenastatus, $this->datep);
1311
    }
1312
1313
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1314
    /**
1315
     *  Return label of action status
1316
     *
1317
     *  @param  int     $percent        Percent
1318
     *  @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
1319
     *  @param  int		$hidenastatus   1=Show nothing if status is "Not applicable"
1320
     *  @param  int     $datestart      Date start of event
1321
     *  @return string		    		Label
1322
     */
1323
    public function LibStatut($percent, $mode, $hidenastatus = 0, $datestart = '')
1324
    {
1325
        // phpcs:enable
1326
        global $langs;
1327
1328
        $labelStatus = $langs->trans('StatusNotApplicable');
1329
       	if ($percent == -1 && !$hidenastatus) $labelStatus = $langs->trans('StatusNotApplicable');
1330
       	elseif ($percent == 0) $labelStatus = $langs->trans('StatusActionToDo').' (0%)';
1331
       	elseif ($percent > 0 && $percent < 100) $labelStatus = $langs->trans('StatusActionInProcess').' ('.$percent.'%)';
1332
       	elseif ($percent >= 100) $labelStatus = $langs->trans('StatusActionDone').' (100%)';
1333
1334
        $labelStatusShort = $langs->trans('StatusNotApplicable');
1335
        if ($percent == -1 && !$hidenastatus) $labelStatusShort = $langs->trans('NA');
1336
        elseif ($percent == 0) $labelStatusShort = '0%';
1337
        elseif ($percent > 0 && $percent < 100) $labelStatusShort = $percent.'%';
1338
        elseif ($percent >= 100) $labelStatusShort = '100%';
1339
1340
        $statusType = 'status9';
1341
        if ($percent == -1 && !$hidenastatus) $statusType = 'status9';
1342
        if ($percent == 0) $statusType = 'status1';
1343
        if ($percent > 0 && $percent < 100) $statusType = 'status3';
1344
        if ($percent >= 100) $statusType = 'status6';
1345
1346
        return dolGetStatus($labelStatus, $labelStatusShort, '', $statusType, $mode);
1347
    }
1348
1349
    /**
1350
     *  Return URL of event
1351
     *  Use $this->id, $this->type_code, $this->label and $this->type_label
1352
     *
1353
     *  @param	int		$withpicto				0 = No picto, 1 = Include picto into link, 2 = Only picto
1354
     *  @param	int		$maxlength				Max number of charaters into label. If negative, use the ref as label.
1355
     *  @param	string	$classname				Force style class on a link
1356
     *  @param	string	$option					'' = Link to action, 'birthday'= Link to contact, 'holiday' = Link to leave
1357
     *  @param	int		$overwritepicto			1 = Overwrite picto
1358
     *  @param	int   	$notooltip		    	1 = Disable tooltip
1359
     *  @param  int     $save_lastsearch_value  -1 = Auto, 0 = No save of lastsearch_values when clicking, 1 = Save lastsearch_values whenclicking
1360
     *  @return	string							Chaine avec URL
1361
     */
1362
    public function getNomUrl($withpicto = 0, $maxlength = 0, $classname = '', $option = '', $overwritepicto = 0, $notooltip = 0, $save_lastsearch_value = -1)
1363
    {
1364
        global $conf, $langs, $user, $hookmanager, $action;
1365
1366
        if (!empty($conf->dol_no_mouse_hover)) $notooltip = 1; // Force disable tooltips
1367
1368
		$canread = 0;
1369
		if ($user->rights->agenda->myactions->read && $this->authorid == $user->id) $canread = 1; // Can read my event
1370
		if ($user->rights->agenda->myactions->read && array_key_exists($user->id, $this->userassigned)) $canread = 1; // Can read my event i am assigned
1371
		if ($user->rights->agenda->allactions->read) $canread = 1; // Can read all event of other
1372
		if (!$canread)
1373
		{
1374
            $option = 'nolink';
1375
		}
1376
1377
        $label = $this->label;
1378
		if (empty($label)) $label = $this->libelle; // For backward compatibility
1 ignored issue
show
Bug Best Practice introduced by
The property libelle does not exist on ActionComm. Did you maybe forget to declare it?
Loading history...
1379
1380
		$result = '';
1381
1382
		// Set label of type
1383
		$labeltype = '';
1384
		if ($this->type_code)
1385
		{
1386
			$labeltype = ($langs->transnoentities("Action".$this->type_code) != "Action".$this->type_code) ? $langs->transnoentities("Action".$this->type_code) : $this->type_label;
1387
		}
1388
		if (empty($conf->global->AGENDA_USE_EVENT_TYPE))
1389
		{
1390
		    if ($this->type_code != 'AC_OTH_AUTO') $labeltype = $langs->trans('ActionAC_MANUAL');
1391
		}
1392
1393
		$tooltip = '<u>'.$langs->trans('Action').'</u>';
1394
		if (!empty($this->ref))
1395
			$tooltip .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
1396
		if (!empty($label))
1397
			$tooltip .= '<br><b>'.$langs->trans('Title').':</b> '.$label;
1398
		if (!empty($labeltype))
1399
			$tooltip .= '<br><b>'.$langs->trans('Type').':</b> '.$labeltype;
1400
		if (!empty($this->location))
1401
			$tooltip .= '<br><b>'.$langs->trans('Location').':</b> '.$this->location;
1402
		if (isset($this->transparency))
1403
			$tooltip .= '<br><b>'.$langs->trans('Busy').':</b> '.yn($this->transparency);
1404
		if (!empty($this->note_private))
1405
		    $tooltip .= '<br><b>'.$langs->trans('Note').':</b> '.(dol_textishtml($this->note_private) ? str_replace(array("\r", "\n"), "", $this->note_private) : str_replace(array("\r", "\n"), '<br>', $this->note_private));
1406
		$linkclose = '';
1407
		if (!empty($conf->global->AGENDA_USE_EVENT_TYPE) && $this->type_color)
1408
			$linkclose = ' style="background-color:#'.$this->type_color.'"';
1409
1410
		if (empty($notooltip))
1411
		{
1412
		    if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
1413
		    {
1414
		        $label = $langs->trans("ShowAction");
1415
		        $linkclose .= ' alt="'.dol_escape_htmltag($tooltip, 1).'"';
1416
		    }
1417
		    $linkclose .= ' title="'.dol_escape_htmltag($tooltip, 1).'"';
1418
		    $linkclose .= ' class="'.$classname.' classfortooltip"';
1419
1420
		    /*
1421
		    $hookmanager->initHooks(array('actiondao'));
1422
		    $parameters=array('id'=>$this->id);
1423
		    $reshook=$hookmanager->executeHooks('getnomurltooltip',$parameters,$this,$action);    // Note that $action and $object may have been modified by some hooks
1424
		    $linkclose = ($hookmanager->resPrint ? $hookmanager->resPrint : $linkclose);
1425
		    */
1426
		} else $linkclose .= ' class="'.$classname.'"';
1427
1428
		$url = '';
1429
		if ($option == 'birthday')
1430
			$url = DOL_URL_ROOT.'/contact/perso.php?id='.$this->id;
1431
		elseif ($option == 'holiday')
1432
            $url = DOL_URL_ROOT.'/holiday/card.php?id='.$this->id;
1433
		else $url = DOL_URL_ROOT.'/comm/action/card.php?id='.$this->id;
1434
		if ($option !== 'nolink')
1435
		{
1436
			// Add param to save lastsearch_values or not
1437
			$add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1438
			if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) $add_save_lastsearch_values = 1;
1439
			if ($add_save_lastsearch_values) $url .= '&save_lastsearch_values=1';
1440
		}
1441
1442
		$linkstart = '<a href="'.$url.'"';
1443
		$linkstart .= $linkclose.'>';
1444
		$linkend = '</a>';
1445
1446
		if ($option == 'nolink') {
1447
			$linkstart = '';
1448
			$linkend = '';
1449
		}
1450
1451
        if ($withpicto == 2)
1452
        {
1453
            $libelle = $label;
1454
            if (!empty($conf->global->AGENDA_USE_EVENT_TYPE)) $libelle = $labeltype;
1455
            $libelleshort = '';
1456
        } else {
1457
            $libelle = (empty($this->libelle) ? $label : $this->libelle.(($label && $label != $this->libelle) ? ' '.$label : ''));
1458
            if (!empty($conf->global->AGENDA_USE_EVENT_TYPE) && empty($libelle)) $libelle = $labeltype;
1459
            if ($maxlength < 0) $libelleshort = $this->ref;
1460
            else $libelleshort = dol_trunc($libelle, $maxlength);
1461
        }
1462
1463
        if ($withpicto)
1464
        {
1465
            if (!empty($conf->global->AGENDA_USE_EVENT_TYPE))	// Add code into ()
1466
            {
1467
            	if ($labeltype)
1468
            	{
1469
                	$libelle .= (preg_match('/'.preg_quote($labeltype, '/').'/', $libelle) ? '' : ' ('.$langs->transnoentities("Action".$this->type_code).')');
1470
            	}
1471
            }
1472
        }
1473
1474
        $result .= $linkstart;
1475
        if ($withpicto)	$result .= img_object(($notooltip ? '' : $langs->trans("ShowAction").': '.$libelle), ($overwritepicto ? $overwritepicto : 'action'), ($notooltip ? 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'"' : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
1476
        $result .= $libelleshort;
1477
        $result .= $linkend;
1478
1479
        global $action;
1480
        $hookmanager->initHooks(array('actiondao'));
1481
        $parameters = array('id'=>$this->id, 'getnomurl'=>$result);
1482
        $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1483
        if ($reshook > 0) $result = $hookmanager->resPrint;
1484
        else $result .= $hookmanager->resPrint;
1485
1486
        return $result;
1487
    }
1488
1489
    /**
1490
     * Sets object to supplied categories.
1491
     *
1492
     * Deletes object from existing categories not supplied.
1493
     * Adds it to non existing supplied categories.
1494
     * Existing categories are left untouch.
1495
     *
1496
     * @param  int[]|int $categories Category or categories IDs
1497
     * @return void
1498
     */
1499
    public function setCategories($categories)
1500
    {
1501
        // Handle single category
1502
        if (!is_array($categories)) {
1503
            $categories = array($categories);
1504
        }
1505
1506
        // Get current categories
1507
        include_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1508
        $c = new Categorie($this->db);
1509
        $existing = $c->containing($this->id, Categorie::TYPE_ACTIONCOMM, 'id');
1510
1511
        // Diff
1512
        if (is_array($existing)) {
0 ignored issues
show
introduced by
The condition is_array($existing) is always false.
Loading history...
1513
            $to_del = array_diff($existing, $categories);
1514
            $to_add = array_diff($categories, $existing);
1515
        } else {
1516
            $to_del = array(); // Nothing to delete
1517
            $to_add = $categories;
1518
        }
1519
1520
        // Process
1521
        foreach ($to_del as $del) {
1522
            if ($c->fetch($del) > 0) {
1523
                $c->del_type($this, Categorie::TYPE_ACTIONCOMM);
1524
            }
1525
        }
1526
        foreach ($to_add as $add) {
1527
            if ($c->fetch($add) > 0) {
1528
                $c->add_type($this, Categorie::TYPE_ACTIONCOMM);
1529
            }
1530
        }
1531
        return;
1532
    }
1533
1534
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1535
    /**
1536
     * Export events from database into a cal file.
1537
     *
1538
     * @param string    $format         The format of the export 'vcal', 'ical/ics' or 'rss'
1539
     * @param string    $type           The type of the export 'event' or 'journal'
1540
     * @param integer   $cachedelay     Do not rebuild file if date older than cachedelay seconds
1541
     * @param string    $filename       The name for the exported file.
1542
     * @param array     $filters        Array of filters. Example array('notolderthan'=>99, 'year'=>..., 'idfrom'=>..., 'notactiontype'=>'systemauto', 'project'=>123, ...)
1543
     * @param integer   $exportholiday  0 = don't integrate holidays into the export, 1 = integrate holidays into the export
1544
     * @return integer                  -1 = error on build export file, 0 = export okay
1545
     */
1546
    public function build_exportfile($format, $type, $cachedelay, $filename, $filters, $exportholiday = 0)
1547
    {
1548
        global $hookmanager;
1549
1550
        // phpcs:enable
1551
        global $conf, $langs, $dolibarr_main_url_root, $mysoc;
1552
1553
        require_once DOL_DOCUMENT_ROOT."/core/lib/xcal.lib.php";
1554
        require_once DOL_DOCUMENT_ROOT."/core/lib/date.lib.php";
1555
        require_once DOL_DOCUMENT_ROOT."/core/lib/files.lib.php";
1556
1557
        dol_syslog(get_class($this)."::build_exportfile Build export file format=".$format.", type=".$type.", cachedelay=".$cachedelay.", filename=".$filename.", filters size=".count($filters), LOG_DEBUG);
1558
1559
        // Check parameters
1560
        if (empty($format)) return -1;
1561
1562
        // Clean parameters
1563
        if (!$filename)
1564
        {
1565
            $extension = 'vcs';
1566
            if ($format == 'ical') $extension = 'ics';
1567
            $filename = $format.'.'.$extension;
1568
        }
1569
1570
        // Create dir and define output file (definitive and temporary)
1571
        $result = dol_mkdir($conf->agenda->dir_temp);
1572
        $outputfile = $conf->agenda->dir_temp.'/'.$filename;
1573
1574
        $result = 0;
1575
1576
        $buildfile = true;
1577
        $login = ''; $logina = ''; $logind = ''; $logint = '';
1578
1579
        $now = dol_now();
1580
1581
        if ($cachedelay)
1582
        {
1583
            $nowgmt = dol_now();
1584
            include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1585
            if (dol_filemtime($outputfile) > ($nowgmt - $cachedelay))
1586
            {
1587
                dol_syslog(get_class($this)."::build_exportfile file ".$outputfile." is not older than now - cachedelay (".$nowgmt." - ".$cachedelay."). Build is canceled");
1588
                $buildfile = false;
1589
            }
1590
        }
1591
1592
        if ($buildfile)
1593
        {
1594
            // Build event array
1595
            $eventarray = array();
1596
1597
            $sql = "SELECT a.id,";
1598
            $sql .= " a.datep,"; // Start
1599
            $sql .= " a.datep2,"; // End
1600
            $sql .= " a.durationp,"; // deprecated
1601
            $sql .= " a.datec, a.tms as datem,";
1602
            $sql .= " a.label, a.code, a.note, a.fk_action as type_id,";
1603
            $sql .= " a.fk_soc,";
1604
            $sql .= " a.fk_user_author, a.fk_user_mod,";
1605
            $sql .= " a.fk_user_action,";
1606
            $sql .= " a.fk_contact, a.percent as percentage,";
1607
            $sql .= " a.fk_element, a.elementtype,";
1608
            $sql .= " a.priority, a.fulldayevent, a.location, a.transparency,";
1609
            $sql .= " u.firstname, u.lastname, u.email,";
1610
            $sql .= " s.nom as socname,";
1611
            $sql .= " c.id as type_id, c.code as type_code, c.libelle as type_label";
1612
            $sql .= " FROM (".MAIN_DB_PREFIX."c_actioncomm as c, ".MAIN_DB_PREFIX."actioncomm as a)";
1613
            $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as u on u.rowid = a.fk_user_author"; // Link to get author of event for export
1614
            $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s on s.rowid = a.fk_soc";
1615
1616
			$parameters = array('filters' => $filters);
1617
			$reshook = $hookmanager->executeHooks('printFieldListFrom', $parameters); // Note that $action and $object may have been modified by hook
1618
			$sql .= $hookmanager->resPrint;
1619
1620
			// We must filter on assignement table
1621
			if ($filters['logint']) $sql .= ", ".MAIN_DB_PREFIX."actioncomm_resources as ar";
1622
			$sql .= " WHERE a.fk_action=c.id";
1623
            $sql .= " AND a.entity IN (".getEntity('agenda').")";
1624
            foreach ($filters as $key => $value)
1625
            {
1626
                if ($key == 'notolderthan' && $value != '') $sql .= " AND a.datep >= '".$this->db->idate($now - ($value * 24 * 60 * 60))."'";
1627
                if ($key == 'year')         $sql .= " AND a.datep BETWEEN '".$this->db->idate(dol_get_first_day($value, 1))."' AND '".$this->db->idate(dol_get_last_day($value, 12))."'";
1628
                if ($key == 'id')           $sql .= " AND a.id=".(is_numeric($value) ? $value : 0);
1629
                if ($key == 'idfrom')       $sql .= " AND a.id >= ".(is_numeric($value) ? $value : 0);
1630
                if ($key == 'idto')         $sql .= " AND a.id <= ".(is_numeric($value) ? $value : 0);
1631
                if ($key == 'project')      $sql .= " AND a.fk_project=".(is_numeric($value) ? $value : 0);
1632
                if ($key == 'actiontype')    $sql .= " AND c.type = '".$this->db->escape($value)."'";
1633
                if ($key == 'notactiontype') $sql .= " AND c.type <> '".$this->db->escape($value)."'";
1634
                // We must filter on assignement table
1635
				if ($key == 'logint')       $sql .= " AND ar.fk_actioncomm = a.id AND ar.element_type='user'";
1636
                if ($key == 'logina')
1637
                {
1638
                    $logina = $value;
1639
                    $condition = '=';
1640
                    if (preg_match('/^!/', $logina))
1641
                    {
1642
                        $logina = preg_replace('/^!/', '', $logina);
1643
                        $condition = '<>';
1644
                    }
1645
                    $userforfilter = new User($this->db);
1646
                    $result = $userforfilter->fetch('', $logina);
1647
                    if ($result > 0) $sql .= " AND a.fk_user_author ".$condition." ".$userforfilter->id;
1648
                    elseif ($result < 0 || $condition == '=') $sql .= " AND a.fk_user_author = 0";
1649
                }
1650
                if ($key == 'logint')
1651
                {
1652
                    $logint = $value;
1653
                    $condition = '=';
1654
                    if (preg_match('/^!/', $logint))
1655
                    {
1656
                        $logint = preg_replace('/^!/', '', $logint);
1657
                        $condition = '<>';
1658
                    }
1659
                    $userforfilter = new User($this->db);
1660
                    $result = $userforfilter->fetch('', $logint);
1661
                    if ($result > 0) $sql .= " AND ar.fk_element = ".$userforfilter->id;
1662
                    elseif ($result < 0 || $condition == '=') $sql .= " AND ar.fk_element = 0";
1663
                }
1664
            }
1665
1666
            $sql .= " AND a.datep IS NOT NULL"; // To exclude corrupted events and avoid errors in lightning/sunbird import
1667
1668
			$parameters = array('filters' => $filters);
1669
			$reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters); // Note that $action and $object may have been modified by hook
1670
			$sql .= $hookmanager->resPrint;
1671
1672
            $sql .= " ORDER by datep";
1673
            //print $sql;exit;
1674
1675
            dol_syslog(get_class($this)."::build_exportfile select events", LOG_DEBUG);
1676
            $resql = $this->db->query($sql);
1677
            if ($resql)
1678
            {
1679
                // Note: Output of sql request is encoded in $conf->file->character_set_client
1680
                // This assignment in condition is not a bug. It allows walking the results.
1681
				$diff = 0;
1682
                while ($obj = $this->db->fetch_object($resql))
1683
                {
1684
                    $qualified = true;
1685
1686
                    // 'eid','startdate','duration','enddate','title','summary','category','email','url','desc','author'
1687
                    $event = array();
1688
                    $event['uid'] = 'dolibarragenda-'.$this->db->database_name.'-'.$obj->id."@".$_SERVER["SERVER_NAME"];
1689
                    $event['type'] = $type;
1690
                    $datestart = $this->db->jdate($obj->datep) - (empty($conf->global->AGENDA_EXPORT_FIX_TZ) ? 0 : ($conf->global->AGENDA_EXPORT_FIX_TZ * 3600));
1691
1692
                    // fix for -> Warning: A non-numeric value encountered
1693
                    if (is_numeric($this->db->jdate($obj->datep2)))
1694
                    {
1695
                        $dateend = $this->db->jdate($obj->datep2)
1696
                                 - (empty($conf->global->AGENDA_EXPORT_FIX_TZ) ? 0 : ($conf->global->AGENDA_EXPORT_FIX_TZ * 3600));
1697
                    } else {
1698
                        // use start date as fall-back to avoid import erros on empty end date
1699
                        $dateend = $datestart;
1700
                    }
1701
1702
                    $duration = ($datestart && $dateend) ? ($dateend - $datestart) : 0;
1703
                    $event['summary'] = $obj->label.($obj->socname ? " (".$obj->socname.")" : "");
1704
                    $event['desc'] = $obj->note;
1705
                    $event['startdate'] = $datestart;
1706
                    $event['enddate'] = $dateend; // Not required with type 'journal'
1707
                    $event['duration'] = $duration; // Not required with type 'journal'
1708
                    $event['author'] = dolGetFirstLastname($obj->firstname, $obj->lastname);
1709
                    $event['priority'] = $obj->priority;
1710
                    $event['fulldayevent'] = $obj->fulldayevent;
1711
                    $event['location'] = $obj->location;
1712
                    $event['transparency'] = (($obj->transparency > 0) ? 'OPAQUE' : 'TRANSPARENT'); // OPAQUE (busy) or TRANSPARENT (not busy)
1713
                    $event['category'] = $obj->type_label;
1714
                    $event['email'] = $obj->email;
1715
					// Define $urlwithroot
1716
					$urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
1717
					$urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
1718
					//$urlwithroot=DOL_MAIN_URL_ROOT;						// This is to use same domain name than current
1719
                    $url = $urlwithroot.'/comm/action/card.php?id='.$obj->id;
1720
                    $event['url'] = $url;
1721
                    $event['created'] = $this->db->jdate($obj->datec) - (empty($conf->global->AGENDA_EXPORT_FIX_TZ) ? 0 : ($conf->global->AGENDA_EXPORT_FIX_TZ * 3600));
1722
                    $event['modified'] = $this->db->jdate($obj->datem) - (empty($conf->global->AGENDA_EXPORT_FIX_TZ) ? 0 : ($conf->global->AGENDA_EXPORT_FIX_TZ * 3600));
1723
1724
                    // TODO: find a way to call "$this->fetch_userassigned();" without override "$this" properties
1725
                    $this->id = $obj->id;
1726
                    $this->fetch_userassigned(false);
1727
1728
                    $assignedUserArray = array();
1729
1730
                    foreach ($this->userassigned as $key => $value)
1731
                    {
1732
                        $assignedUser = new User($this->db);
1733
                        $assignedUser->fetch($value['id']);
1734
1735
                        $assignedUserArray[$key] = $assignedUser;
1736
                    }
1737
1738
                    $event['assignedUsers'] = $assignedUserArray;
1739
1740
                    if ($qualified && $datestart)
1741
                    {
1742
                        $eventarray[] = $event;
1743
                    }
1744
                    $diff++;
1745
                }
1746
1747
				$parameters = array('filters' => $filters, 'eventarray' => &$eventarray);
1748
				$reshook = $hookmanager->executeHooks('addMoreEventsExport', $parameters); // Note that $action and $object may have been modified by hook
1749
				if ($reshook > 0)
1750
				{
1751
					$eventarray = $hookmanager->resArray;
1752
				}
1753
            } else {
1754
                $this->error = $this->db->lasterror();
1755
                return -1;
1756
            }
1757
1758
			if ($exportholiday == 1)
1759
            {
1760
                $langs->load("holidays");
1761
                $title = $langs->trans("Holidays");
1762
1763
                $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";
1764
                $sql .= " FROM ".MAIN_DB_PREFIX."holiday as x, ".MAIN_DB_PREFIX."user as u";
1765
                $sql .= " WHERE u.rowid = x.fk_user";
1766
                $sql .= " AND u.statut = '1'"; // Show only active users  (0 = inactive user, 1 = active user)
1767
                $sql .= " AND (x.statut = '2' OR x.statut = '3')"; // Show only public leaves (2 = leave wait for approval, 3 = leave approved)
1768
1769
                $resql = $this->db->query($sql);
1770
                if ($resql)
1771
                {
1772
                    $num = $this->db->num_rows($resql);
1773
                    $i   = 0;
1774
1775
                    while ($i < $num)
1776
                    {
1777
                        $obj   = $this->db->fetch_object($resql);
1778
                        $event = array();
1779
1780
                        if ($obj->halfday == -1)
1781
                        {
1782
                            $event['fulldayevent'] = false;
1783
1784
                            $timestampStart = dol_stringtotime($obj->date_start." 00:00:00", 0);
1785
                            $timestampEnd   = dol_stringtotime($obj->date_end." 12:00:00", 0);
1786
                        } elseif ($obj->halfday == 1)
1787
                        {
1788
                            $event['fulldayevent'] = false;
1789
1790
                            $timestampStart = dol_stringtotime($obj->date_start." 12:00:00", 0);
1791
                            $timestampEnd   = dol_stringtotime($obj->date_end." 23:59:59", 0);
1792
                        } else {
1793
                            $event['fulldayevent'] = true;
1794
1795
                            $timestampStart = dol_stringtotime($obj->date_start." 00:00:00", 0);
1796
                            $timestampEnd   = dol_stringtotime($obj->date_end." 23:59:59", 0);
1797
                        }
1798
1799
                        if (!empty($conf->global->AGENDA_EXPORT_FIX_TZ))
1800
                        {
1801
                            $timestampStart = - ($conf->global->AGENDA_EXPORT_FIX_TZ * 3600);
1802
                            $timestampEnd   = - ($conf->global->AGENDA_EXPORT_FIX_TZ * 3600);
1803
                        }
1804
1805
                        $urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
1806
                        $urlwithroot       = $urlwithouturlroot.DOL_URL_ROOT;
1807
                        $url               = $urlwithroot.'/holiday/card.php?id='.$obj->rowid;
1808
1809
                        $event['uid']          = 'dolibarrholiday-'.$this->db->database_name.'-'.$obj->rowid."@".$_SERVER["SERVER_NAME"];
1810
                        $event['author']       = dolGetFirstLastname($obj->firstname, $obj->lastname);
1811
                        $event['type']         = 'event';
1812
                        $event['category']     = "Holiday";
1813
                        $event['transparency'] = 'OPAQUE';
1814
                        $event['email']        = $obj->email;
1815
                        $event['created']      = $timestampStart;
1816
                        $event['modified']     = $timestampStart;
1817
                        $event['startdate']    = $timestampStart;
1818
                        $event['enddate']      = $timestampEnd;
1819
                        $event['duration']     = $timestampEnd - $timestampStart;
1820
                        $event['url']          = $url;
1821
1822
                        if ($obj->status == 2)
1823
                        {
1824
                            // 2 = leave wait for approval
1825
                            $event['summary'] = $title." - ".$obj->lastname." (wait for approval)";
1826
                        } else {
1827
                            // 3 = leave approved
1828
                            $event['summary'] = $title." - ".$obj->lastname;
1829
                        }
1830
1831
                        $eventarray[] = $event;
1832
1833
                        $i++;
1834
                    }
1835
                }
1836
            }
1837
1838
            $langs->load("agenda");
1839
1840
            // Define title and desc
1841
            $more = '';
1842
            if ($login)  $more = $langs->transnoentities("User").' '.$login;
1843
            if ($logina) $more = $langs->transnoentities("ActionsAskedBy").' '.$logina;
1844
            if ($logint) $more = $langs->transnoentities("ActionsToDoBy").' '.$logint;
1845
            if ($logind) $more = $langs->transnoentities("ActionsDoneBy").' '.$logind;
1846
            if ($more)
1847
            {
1848
                $title = 'Dolibarr actions '.$mysoc->name.' - '.$more;
1849
                $desc = $more;
1850
                $desc .= ' ('.$mysoc->name.' - built by Dolibarr)';
1851
            } else {
1852
                $title = 'Dolibarr actions '.$mysoc->name;
1853
                $desc = $langs->transnoentities('ListOfActions');
1854
                $desc .= ' ('.$mysoc->name.' - built by Dolibarr)';
1855
            }
1856
1857
            // Create temp file
1858
            $outputfiletmp = tempnam($conf->agenda->dir_temp, 'tmp'); // Temporary file (allow call of function by different threads
1859
            @chmod($outputfiletmp, octdec($conf->global->MAIN_UMASK));
1860
1861
            // Write file
1862
            if ($format == 'vcal') $result = build_calfile($format, $title, $desc, $eventarray, $outputfiletmp);
1863
            elseif ($format == 'ical') $result = build_calfile($format, $title, $desc, $eventarray, $outputfiletmp);
1864
            elseif ($format == 'rss')  $result = build_rssfile($format, $title, $desc, $eventarray, $outputfiletmp);
1865
1866
            if ($result >= 0)
1867
            {
1868
                if (dol_move($outputfiletmp, $outputfile, 0, 1)) $result = 1;
1869
                else {
1870
                	$this->error = 'Failed to rename '.$outputfiletmp.' into '.$outputfile;
1871
                    dol_syslog(get_class($this)."::build_exportfile ".$this->error, LOG_ERR);
1872
                    dol_delete_file($outputfiletmp, 0, 1);
1873
                    $result = -1;
1874
                }
1875
            } else {
1876
                dol_syslog(get_class($this)."::build_exportfile build_xxxfile function fails to for format=".$format." outputfiletmp=".$outputfile, LOG_ERR);
1877
                dol_delete_file($outputfiletmp, 0, 1);
1878
                $langs->load("errors");
1879
                $this->error = $langs->trans("ErrorFailToCreateFile", $outputfile);
1880
            }
1881
        }
1882
1883
        return $result;
1884
    }
1885
1886
    /**
1887
     *  Initialise an instance with random values.
1888
     *  Used to build previews or test instances.
1889
     *  id must be 0 if object instance is a specimen.
1890
     *
1891
     *  @return	int >0 if ok
1892
     */
1893
    public function initAsSpecimen()
1894
    {
1895
        global $user;
1896
1897
        $now = dol_now();
1898
1899
        // Initialise parametres
1900
        $this->id = 0;
1901
        $this->specimen = 1;
1902
1903
        $this->type_code = 'AC_OTH';
1904
        $this->code = 'AC_SPECIMEN_CODE';
1905
        $this->label = 'Label of event Specimen';
1906
        $this->datec = $now;
1907
        $this->datem = $now;
1908
        $this->datep = $now;
1909
        $this->datef = $now;
1910
        $this->fulldayevent = 0;
1911
        $this->percentage = 0;
1912
        $this->location = 'Location';
1913
        $this->transparency = 1; // 1 means opaque
1914
        $this->priority = 1;
1915
        //$this->note_public = "This is a 'public' note.";
1916
		$this->note_private = "This is a 'private' note.";
1917
1918
        $this->userownerid = $user->id;
1919
        $this->userassigned[$user->id] = array('id'=>$user->id, 'transparency'=> 1);
1920
        return 1;
1921
    }
1922
1923
	/**
1924
	 *  Function used to replace a thirdparty id with another one.
1925
	 *
1926
	 *  @param DoliDB $db Database handler
1927
	 *  @param int $origin_id Old thirdparty id
1928
	 *  @param int $dest_id New thirdparty id
1929
	 *  @return bool
1930
	 */
1931
	public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
1932
	{
1933
		$tables = array(
1934
			'actioncomm'
1935
		);
1936
1937
		return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
1938
	}
1939
1940
    /**
1941
     *  Is the action delayed?
1942
     *
1943
     *  @return bool
1944
     */
1945
    public function hasDelay()
1946
    {
1947
        global $conf;
1948
1949
        $now = dol_now();
1950
1951
        return $this->datep && ($this->datep < ($now - $conf->agenda->warning_delay));
1952
    }
1953
1954
1955
    /**
1956
     *  Send reminders by emails
1957
     *  CAN BE A CRON TASK
1958
     *
1959
     *  @return int         0 if OK, <>0 if KO (this function is used also by cron so only 0 is OK)
1960
     */
1961
    public function sendEmailsReminder()
1962
    {
1963
    	global $conf, $langs, $user;
1964
1965
    	$error = 0;
1966
    	$this->output = '';
1967
		$this->error = '';
1968
1969
    	if (empty($conf->agenda->enabled))	// Should not happen. If module disabled, cron job should not be visible.
1970
		{
1971
			$langs->load("agenda");
1972
			$this->output = $langs->trans('ModuleNotEnabled', $langs->transnoentitiesnoconv("Agenda"));
1973
			return 0;
1974
		}
1975
		if (empty($conf->global->AGENDA_REMINDER_EMAIL))
1976
    	{
1977
    		$langs->load("agenda");
1978
    		$this->output = $langs->trans('EventRemindersByEmailNotEnabled', $langs->transnoentitiesnoconv("Agenda"));
1979
    		return 0;
1980
    	}
1981
1982
    	$now = dol_now();
1983
1984
    	dol_syslog(__METHOD__, LOG_DEBUG);
1985
1986
        $this->db->begin();
1987
1988
        //Select all action comm reminder
1989
    	$sql = "SELECT rowid as id FROM ".MAIN_DB_PREFIX."actioncomm_reminder";
1990
		$sql .= " WHERE typeremind = 'email' AND status = 0";
1991
		$sql .= " AND dateremind <= '".$this->db->idate(dol_now())."'";
1992
		$sql .= $this->db->order("dateremind", "ASC");
1993
        $resql = $this->db->query($sql);
1994
1995
        if ($resql) {
1996
            $formmail = new FormMail($this->db);
1997
            $actionCommReminder = new ActionCommReminder($this->db);
1998
1999
			while ($obj = $this->db->fetch_object($resql)){
2000
                $res = $actionCommReminder->fetch($obj->id);
2001
                if ($res < 0) {
2002
                    $error++;
2003
                    $errorsMsg[] = "Failed to load invoice ActionComm Reminder";
2004
                }
2005
2006
                if (!$error)
2007
                {
2008
                        //Select email template
2009
                        $arraymessage = $formmail->getEMailTemplate($this->db, 'actioncomm_send', $user, $langs, (!empty($actionCommReminder->fk_email_template)) ? $actionCommReminder->fk_email_template : -1, 1);
2010
2011
						// Load event
2012
                        $res = $this->fetch($actionCommReminder->fk_actioncomm);
2013
                        if ($res > 0)
2014
                        {
2015
                            // PREPARE EMAIL
2016
2017
                            // Make substitution in email content
2018
                            $substitutionarray = getCommonSubstitutionArray($langs, 0, '', $this);
2019
2020
                            complete_substitutions_array($substitutionarray, $langs, $this);
2021
2022
                            // Content
2023
                            $sendContent = make_substitutions($langs->trans($arraymessage->content), $substitutionarray);
2024
2025
                            //Topic
2026
                            $sendTopic = (!empty($arraymessage->topic)) ? $arraymessage->topic : html_entity_decode($langs->trans('EventReminder'));
2027
2028
                            // Recipient
2029
                            $recipient = new User($this->db);
2030
                            $res = $recipient->fetch($actionCommReminder->fk_user);
2031
                            if ($res > 0 && !empty($recipient->email)) $to = $recipient->email;
2032
                            else {
2033
                                $errorsMsg[] = "Failed to load recipient";
2034
                                $error++;
2035
                            }
2036
2037
                            // Sender
2038
                            $from = $conf->global->MAIN_MAIL_EMAIL_FROM;
2039
                            if (empty($from)) {
2040
                                $errorsMsg[] = "Failed to load recipient";
2041
                                $error++;
2042
                            }
2043
2044
                            // Errors Recipient
2045
                            $errors_to = $conf->global->MAIN_MAIL_ERRORS_TO;
2046
2047
                            // Mail Creation
2048
                            $cMailFile = new CMailFile($sendTopic, $to, $from, $sendContent, array(), array(), array(), '', "", 0, 1, $errors_to, '', '', '', '', '');
1 ignored issue
show
Comprehensibility Best Practice introduced by
The variable $to does not seem to be defined for all execution paths leading up to this point.
Loading history...
2049
2050
                            // Sending Mail
2051
                            if ($cMailFile->sendfile())
2052
                            {
2053
                                $actionCommReminder->status = $actionCommReminder::STATUS_DONE;
2054
                                $res = $actionCommReminder->update($user);
2055
                                if ($res < 0)
2056
                                {
2057
                                    $errorsMsg[] = "Failed to update status of ActionComm Reminder";
2058
                                    $error++;
2059
                                }
2060
                                else $nbMailSend++;
1 ignored issue
show
Comprehensibility Best Practice introduced by
The variable $nbMailSend seems to be never defined.
Loading history...
2061
                            }
2062
                            else {
2063
                                $errorsMsg[] = $cMailFile->error.' : '.$to;
2064
                                $error++;
2065
                            }
2066
                        }
2067
                        else {
2068
                            $error++;
2069
                        }
2070
                }
2071
            }
2072
        } else {
2073
            $error++;
2074
        }
2075
2076
        if (!$error)
2077
        {
2078
            // Delete also very old past events (we do not keep more than 1 month record in past)
2079
            $sql = "DELETE FROM ".MAIN_DB_PREFIX."actioncomm_reminder";
2080
			$sql .= " WHERE dateremind < '".$this->db->idate($now - (3600 * 24 * 32))."'";
2081
            $resql = $this->db->query($sql);
2082
2083
            if (!$resql) {
2084
                $errorsMsg[] = 'Failed to delete old reminders';
2085
                $error ++;
2086
            }
2087
        }
2088
2089
        if (!$error) {
2090
            $this->db->commit();
2091
            return 0;
2092
        }
2093
        else {
2094
            $this->db->rollback();
2095
            return (!empty($errorsMsg)) ? end($errorsMsg) : $error;
2096
        }
2097
2098
    }
2099
2100
	/**
2101
	 * Udpate the percent value of a event with the given id
2102
	 *
2103
	 * @param int		$id			The id of the event
2104
	 * @param int		$percent	The new percent value for the event
2105
	 * @return int					1 when update of the event was suscessfull, otherwise -1
2106
	 */
2107
	public function updatePercent($id, $percent)
2108
	{
2109
		$this->db->begin();
2110
2111
		$sql = "UPDATE ".MAIN_DB_PREFIX."actioncomm ";
2112
		$sql .= " SET percent = ".(int) $percent;
2113
		$sql .= " WHERE id=".$id;
2114
2115
		if ($this->db->query($sql))
2116
		{
2117
			$this->db->commit();
2118
			return 1;
2119
		} else {
2120
			$this->db->rollback();
2121
			$this->error = $this->db->lasterror();
2122
			return -1;
2123
		}
2124
	}
2125
}
2126