/
actioncomm.class.php
1287 lines (1137 loc) · 51.5 KB
/
actioncomm.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/* Copyright (C) 2002-2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2005-2012 Regis Houssin <regis.houssin@capnetworks.com>
* Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
* Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file htdocs/comm/action/class/actioncomm.class.php
* \ingroup agenda
* \brief File of class to manage agenda events (actions)
*/
require_once DOL_DOCUMENT_ROOT.'/comm/action/class/cactioncomm.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
/**
* Class to manage agenda events (actions)
*/
class ActionComm extends CommonObject
{
public $element='action';
public $table_element = 'actioncomm';
public $table_rowid = 'id';
protected $ismultientitymanaged = 1; // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
/**
* Id of the event
* @var int
*/
var $id;
/**
* Id of the event. Use $id as possible
* @var int
*/
public $ref;
var $type_id; // Id into parent table llx_c_actioncomm (used only if option to use type is set)
var $type_code; // 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.
var $type; // Label into parent table llx_c_actioncomm (used only if option to use type is set)
var $type_color; // Color into parent table llx_c_actioncomm (used only if option to use type is set)
var $code; // Free code to identify action. Ie: Agenda trigger add here AC_TRIGGERNAME ('AC_COMPANY_CREATE', 'AC_PROPAL_VALIDATE', ...)
var $label;
/**
* @var string
* @deprecated Use $label
*/
public $libelle;
var $datec; // Date creation record (datec)
var $datem; // Date modification record (tms)
/**
* Object user that create action
* @var User
* @deprecated
*/
var $author;
/**
* Object user that modified action
* @var User
* @deprecated
*/
var $usermod;
var $authorid; // Id user that create action
var $usermodid; // Id user that modified action
var $datep; // Date action start (datep)
var $datef; // Date action end (datep2)
/**
* @var int -1=Unkown duration
* @deprecated
*/
var $durationp = -1;
var $fulldayevent = 0; // 1=Event on full day
/**
* Milestone
* @var int
* @deprecated Milestone is already event with end date = start date
*/
var $punctual = 1;
var $percentage; // Percentage
var $location; // Location
var $transparency; // 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)
var $priority; // Small int (0 By default)
var $note; // Description
var $userassigned = array(); // Array of user ids
var $userownerid; // Id of user owner
var $userdoneid; // Id of user done
/**
* Object user of owner
* @var User
* @deprecated
*/
var $usertodo;
/**
* Object user that did action
* @var User
* @deprecated
*/
var $userdone;
var $socid;
var $contactid;
/**
* Company linked to action (optional)
* @var Societe|null
*/
var $societe;
/**
* Contact linked to action (optional)
* @var Contact|null
*/
var $contact;
/**
* Id of project (optional)
* @var int
*/
var $fk_project;
// Properties for links to other objects
var $fk_element; // Id of record
var $elementtype; // Type of record. This if property ->element of object linked to.
// Ical
var $icalname;
var $icalcolor;
var $actions=array();
/**
* Constructor
*
* @param DoliDB $db Database handler
*/
function __construct($db)
{
$this->db = $db;
$this->societe = new stdClass(); // deprecated
$this->contact = new stdClass(); // deprecated
}
/**
* Add an action/event into database.
* $this->type_id OR $this->type_code must be set.
*
* @param User $user Object user making action
* @param int $notrigger 1 = disable triggers, 0 = enable triggers
* @return int Id of created event, < 0 if KO
*/
function add($user,$notrigger=0)
{
global $langs,$conf,$hookmanager;
$error=0;
$now=dol_now();
// Check parameters
if (empty($this->userownerid))
{
$this->errors[]='ErrorPropertyUserowneridNotDefined';
return -1;
}
// Clean parameters
$this->label=dol_trunc(trim($this->label),128);
$this->location=dol_trunc(trim($this->location),128);
$this->note=dol_htmlcleanlastbr(trim($this->note));
if (empty($this->percentage)) $this->percentage = 0;
if (empty($this->priority) || ! is_numeric($this->priority)) $this->priority = 0;
if (empty($this->fulldayevent)) $this->fulldayevent = 0;
if (empty($this->punctual)) $this->punctual = 0;
if (empty($this->transparency)) $this->transparency = 0;
if ($this->percentage > 100) $this->percentage = 100;
//if ($this->percentage == 100 && ! $this->dateend) $this->dateend = $this->date;
if (! empty($this->datep) && ! empty($this->datef)) $this->durationp=($this->datef - $this->datep); // deprecated
//if (! empty($this->date) && ! empty($this->dateend)) $this->durationa=($this->dateend - $this->date);
if (! empty($this->datep) && ! empty($this->datef) && $this->datep > $this->datef) $this->datef=$this->datep;
//if (! empty($this->date) && ! empty($this->dateend) && $this->date > $this->dateend) $this->dateend=$this->date;
if (! isset($this->fk_project) || $this->fk_project < 0) $this->fk_project = 0;
if ($this->elementtype=='facture') $this->elementtype='invoice';
if ($this->elementtype=='commande') $this->elementtype='order';
if ($this->elementtype=='contrat') $this->elementtype='contract';
if (! is_array($this->userassigned) && ! empty($this->userassigned)) // For backward compatibility
{
$tmpid=$this->userassigned;
$this->userassigned=array();
$this->userassigned[$tmpid]=array('id'=>$tmpid);
}
if (is_object($this->contact) && $this->contact->id > 0 && ! ($this->contactid > 0)) $this->contactid = $this->contact->id; // For backward compatibility. Using this->contact->xx is deprecated
$userownerid=$this->userownerid;
$userdoneid=$this->userdoneid;
// Be sure assigned user is defined as an array of array('id'=>,'mandatory'=>,...).
if (empty($this->userassigned) || count($this->userassigned) == 0 || ! is_array($this->userassigned))
$this->userassigned = array($userownerid=>array('id'=>$userownerid));
if (! $this->type_id || ! $this->type_code)
{
$key=empty($this->type_id)?$this->type_code:$this->type_id;
// Get id from code
$cactioncomm=new CActionComm($this->db);
$result=$cactioncomm->fetch($key);
if ($result > 0)
{
$this->type_id=$cactioncomm->id;
$this->type_code=$cactioncomm->code;
}
else if ($result == 0)
{
$this->error='Failed to get record with id '.$this->type_id.' code '.$this->type_code.' from dictionary "type of events"';
return -1;
}
else
{
$this->error=$cactioncomm->error;
return -1;
}
}
// Check parameters
if (! $this->type_id)
{
$this->error="ErrorWrongParameters";
return -1;
}
$this->db->begin();
$sql = "INSERT INTO ".MAIN_DB_PREFIX."actioncomm";
$sql.= "(datec,";
$sql.= "datep,";
$sql.= "datep2,";
$sql.= "durationp,"; // deprecated
$sql.= "fk_action,";
$sql.= "code,";
$sql.= "fk_soc,";
$sql.= "fk_project,";
$sql.= "note,";
$sql.= "fk_contact,";
$sql.= "fk_user_author,";
$sql.= "fk_user_action,";
$sql.= "fk_user_done,";
$sql.= "label,percent,priority,fulldayevent,location,punctual,";
$sql.= "transparency,";
$sql.= "fk_element,";
$sql.= "elementtype,";
$sql.= "entity";
$sql.= ") VALUES (";
$sql.= "'".$this->db->idate($now)."',";
$sql.= (strval($this->datep)!=''?"'".$this->db->idate($this->datep)."'":"null").",";
$sql.= (strval($this->datef)!=''?"'".$this->db->idate($this->datef)."'":"null").",";
$sql.= ((isset($this->durationp) && $this->durationp >= 0 && $this->durationp != '')?"'".$this->durationp."'":"null").","; // deprecated
$sql.= (isset($this->type_id)?$this->type_id:"null").",";
$sql.= (isset($this->type_code)?" '".$this->type_code."'":"null").",";
$sql.= ((isset($this->socid) && $this->socid > 0)?" '".$this->socid."'":"null").",";
$sql.= ((isset($this->fk_project) && $this->fk_project > 0)?" '".$this->fk_project."'":"null").",";
$sql.= " '".$this->db->escape($this->note)."',";
$sql.= ((isset($this->contactid) && $this->contactid > 0)?"'".$this->contactid."'":"null").",";
$sql.= (isset($user->id) && $user->id > 0 ? "'".$user->id."'":"null").",";
$sql.= ($userownerid>0?"'".$userownerid."'":"null").",";
$sql.= ($userdoneid>0?"'".$userdoneid."'":"null").",";
$sql.= "'".$this->db->escape($this->label)."','".$this->percentage."','".$this->priority."','".$this->fulldayevent."','".$this->db->escape($this->location)."','".$this->punctual."',";
$sql.= "'".$this->transparency."',";
$sql.= (! empty($this->fk_element)?$this->fk_element:"null").",";
$sql.= (! empty($this->elementtype)?"'".$this->elementtype."'":"null").",";
$sql.= $conf->entity;
$sql.= ")";
dol_syslog(get_class($this)."::add", LOG_DEBUG);
$resql=$this->db->query($sql);
if ($resql)
{
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."actioncomm","id");
// Now insert assignedusers
if (! $error)
{
foreach($this->userassigned as $key => $val)
{
if (! is_array($val)) // For backward compatibility when val=id
{
$val=array('id'=>$val);
}
$sql ="INSERT INTO ".MAIN_DB_PREFIX."actioncomm_resources(fk_actioncomm, element_type, fk_element, mandatory, transparency, answer_status)";
$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']).")";
$resql = $this->db->query($sql);
if (! $resql)
{
$error++;
$this->errors[]=$this->db->lasterror();
}
//var_dump($sql);exit;
}
}
if (! $error)
{
$action='create';
// Actions on extra fields (by external module or standard code)
// TODO le hook fait double emploi avec le trigger !!
$hookmanager->initHooks(array('actioncommdao'));
$parameters=array('actcomm'=>$this->id);
$reshook=$hookmanager->executeHooks('insertExtraFields',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
if (empty($reshook))
{
if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
{
$result=$this->insertExtraFields();
if ($result < 0)
{
$error++;
}
}
}
else if ($reshook < 0) $error++;
}
if (! $error && ! $notrigger)
{
// Call trigger
$result=$this->call_trigger('ACTION_CREATE',$user);
if ($result < 0) { $error++; }
// End call triggers
}
if (! $error)
{
$this->db->commit();
return $this->id;
}
else
{
$this->db->rollback();
return -1;
}
}
else
{
$this->db->rollback();
$this->error=$this->db->lasterror();
return -1;
}
}
/**
* Load object from database
*
* @param int $id Id of action to get
* @param string $ref Ref of action to get
* @return int <0 if KO, >0 if OK
*/
function fetch($id, $ref='')
{
global $langs;
$sql = "SELECT a.id,";
$sql.= " a.id as ref,";
$sql.= " a.ref_ext,";
$sql.= " a.datep,";
$sql.= " a.datep2,";
$sql.= " a.durationp,"; // deprecated
$sql.= " a.datec,";
$sql.= " a.tms as datem,";
$sql.= " a.code, a.label, a.note,";
$sql.= " a.fk_soc,";
$sql.= " a.fk_project,";
$sql.= " a.fk_user_author, a.fk_user_mod,";
$sql.= " a.fk_user_action, a.fk_user_done,";
$sql.= " a.fk_contact, a.percent as percentage,";
$sql.= " a.fk_element, a.elementtype,";
$sql.= " a.priority, a.fulldayevent, a.location, a.punctual, a.transparency,";
$sql.= " c.id as type_id, c.code as type_code, c.libelle,";
$sql.= " s.nom as socname,";
$sql.= " u.firstname, u.lastname as lastname";
$sql.= " FROM ".MAIN_DB_PREFIX."actioncomm as a ";
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_actioncomm as c ON a.fk_action=c.id ";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."user as u on u.rowid = a.fk_user_author";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s on s.rowid = a.fk_soc";
$sql.= " WHERE ";
if ($ref) $sql.= " a.id=".$ref; // No field ref, we use id
else $sql.= " a.id=".$id;
dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
$resql=$this->db->query($sql);
if ($resql)
{
$num=$this->db->num_rows($resql);
if ($num)
{
$obj = $this->db->fetch_object($resql);
$this->id = $obj->id;
$this->ref = $obj->ref;
$this->ref_ext = $obj->ref_ext;
// Properties of parent table llx_c_actioncomm (will be deprecated in future)
$this->type_id = $obj->type_id;
$this->type_code = $obj->type_code;
$transcode=$langs->trans("Action".$obj->type_code);
$type_libelle=($transcode!="Action".$obj->type_code?$transcode:$obj->libelle);
$this->type = $type_libelle;
$this->code = $obj->code;
$this->label = $obj->label;
$this->datep = $this->db->jdate($obj->datep);
$this->datef = $this->db->jdate($obj->datep2);
// $this->durationp = $this->durationp; // deprecated
$this->datec = $this->db->jdate($obj->datec);
$this->datem = $this->db->jdate($obj->datem);
$this->note = $obj->note;
$this->percentage = $obj->percentage;
$this->authorid = $obj->fk_user_author;
$this->usermodid = $obj->fk_user_mod;
if (!is_object($this->author)) $this->author = new stdClass(); // For avoid warning
$this->author->id = $obj->fk_user_author; // deprecated
$this->author->firstname = $obj->firstname; // deprecated
$this->author->lastname = $obj->lastname; // deprecated
if (!is_object($this->usermod)) $this->usermod = new stdClass(); // For avoid warning
$this->usermod->id = $obj->fk_user_mod; // deprecated
$this->userownerid = $obj->fk_user_action;
$this->userdoneid = $obj->fk_user_done;
$this->priority = $obj->priority;
$this->fulldayevent = $obj->fulldayevent;
$this->location = $obj->location;
$this->transparency = $obj->transparency;
$this->punctual = $obj->punctual;
$this->socid = $obj->fk_soc; // To have fetch_thirdparty method working
$this->contactid = $obj->fk_contact; // To have fetch_contact method working
$this->fk_project = $obj->fk_project; // To have fetch_project method working
$this->societe->id = $obj->fk_soc; // deprecated
$this->contact->id = $obj->fk_contact; // deprecated
$this->fk_element = $obj->fk_element;
$this->elementtype = $obj->elementtype;
}
$this->db->free($resql);
}
else
{
$this->error=$this->db->lasterror();
return -1;
}
return $num;
}
/**
* Initialize this->userassigned array with list of id of user assigned to event
*
* @return int <0 if KO, >0 if OK
*/
function fetch_userassigned()
{
$sql ="SELECT fk_actioncomm, element_type, fk_element, answer_status, mandatory, transparency";
$sql.=" FROM ".MAIN_DB_PREFIX."actioncomm_resources";
$sql.=" WHERE element_type = 'user' AND fk_actioncomm = ".$this->id;
$resql2=$this->db->query($sql);
if ($resql2)
{
$this->userassigned=array();
// If owner is known, we must but id first into list
if ($this->userownerid > 0) $this->userassigned[$this->userownerid]=array('id'=>$this->userownerid); // Set first so will be first into list.
while ($obj = $this->db->fetch_object($resql2))
{
if ($obj->fk_element > 0) $this->userassigned[$obj->fk_element]=array('id'=>$obj->fk_element, 'mandatory'=>$obj->mandatory, 'answer_status'=>$obj->answer_status, 'transparency'=>$obj->transparency);
if (empty($this->userownerid)) $this->userownerid=$obj->fk_element; // If not defined (should not happened, we fix this)
}
return 1;
}
else
{
dol_print_error($this->db);
return -1;
}
}
/**
* Delete event from database
*
* @param int $notrigger 1 = disable triggers, 0 = enable triggers
* @return int <0 if KO, >0 if OK
*/
function delete($notrigger=0)
{
global $user,$langs,$conf;
$error=0;
$this->db->begin();
$sql = "DELETE FROM ".MAIN_DB_PREFIX."actioncomm";
$sql.= " WHERE id=".$this->id;
dol_syslog(get_class($this)."::delete", LOG_DEBUG);
$res=$this->db->query($sql);
if ($res < 0) {
$this->error=$this->db->lasterror();
$error++;
}
// Removed extrafields
if (! $error) {
$result=$this->deleteExtraFields();
if ($result < 0)
{
$error++;
dol_syslog(get_class($this)."::delete error -3 ".$this->error, LOG_ERR);
}
}
if (!$error)
{
if (! $notrigger)
{
// Call trigger
$result=$this->call_trigger('ACTION_DELETE',$user);
if ($result < 0) { $error++; }
// End call triggers
}
if (! $error)
{
$this->db->commit();
return 1;
}
else
{
$this->db->rollback();
return -2;
}
}
else
{
$this->db->rollback();
$this->error=$this->db->lasterror();
return -1;
}
}
/**
* Update action into database
* If percentage = 100, on met a jour date 100%
*
* @param User $user Object user making change
* @param int $notrigger 1 = disable triggers, 0 = enable triggers
* @return int <0 if KO, >0 if OK
*/
function update($user,$notrigger=0)
{
global $langs,$conf,$hookmanager;
$error=0;
// Clean parameters
$this->label=trim($this->label);
$this->note=trim($this->note);
if (empty($this->percentage)) $this->percentage = 0;
if (empty($this->priority) || ! is_numeric($this->priority)) $this->priority = 0;
if (empty($this->transparency)) $this->transparency = 0;
if (empty($this->fulldayevent)) $this->fulldayevent = 0;
if ($this->percentage > 100) $this->percentage = 100;
//if ($this->percentage == 100 && ! $this->dateend) $this->dateend = $this->date;
if ($this->datep && $this->datef) $this->durationp=($this->datef - $this->datep); // deprecated
//if ($this->date && $this->dateend) $this->durationa=($this->dateend - $this->date);
if ($this->datep && $this->datef && $this->datep > $this->datef) $this->datef=$this->datep;
//if ($this->date && $this->dateend && $this->date > $this->dateend) $this->dateend=$this->date;
if ($this->fk_project < 0) $this->fk_project = 0;
// Check parameters
if ($this->percentage == 0 && $this->userdoneid > 0)
{
$this->error="ErrorCantSaveADoneUserWithZeroPercentage";
return -1;
}
$socid=($this->socid?$this->socid:((isset($this->societe->id) && $this->societe->id > 0) ? $this->societe->id : 0));
$contactid=($this->contactid?$this->contactid:((isset($this->contact->id) && $this->contact->id > 0) ? $this->contact->id : 0));
$userownerid=($this->userownerid?$this->userownerid:0);
$userdoneid=($this->userdoneid?$this->userdoneid:0);
$this->db->begin();
$sql = "UPDATE ".MAIN_DB_PREFIX."actioncomm ";
$sql.= " SET percent = '".$this->percentage."'";
if ($this->fk_action > 0) $sql.= ", fk_action = '".$this->fk_action."'";
$sql.= ", label = ".($this->label ? "'".$this->db->escape($this->label)."'":"null");
$sql.= ", datep = ".(strval($this->datep)!='' ? "'".$this->db->idate($this->datep)."'" : 'null');
$sql.= ", datep2 = ".(strval($this->datef)!='' ? "'".$this->db->idate($this->datef)."'" : 'null');
$sql.= ", durationp = ".(isset($this->durationp) && $this->durationp >= 0 && $this->durationp != ''?"'".$this->durationp."'":"null"); // deprecated
$sql.= ", note = ".($this->note ? "'".$this->db->escape($this->note)."'":"null");
$sql.= ", fk_project =". ($this->fk_project > 0 ? "'".$this->fk_project."'":"null");
$sql.= ", fk_soc =". ($socid > 0 ? "'".$socid."'":"null");
$sql.= ", fk_contact =". ($contactid > 0 ? "'".$contactid."'":"null");
$sql.= ", priority = '".$this->priority."'";
$sql.= ", fulldayevent = '".$this->fulldayevent."'";
$sql.= ", location = ".($this->location ? "'".$this->db->escape($this->location)."'":"null");
$sql.= ", transparency = '".$this->transparency."'";
$sql.= ", fk_user_mod = '".$user->id."'";
$sql.= ", fk_user_action=".($userownerid > 0 ? "'".$userownerid."'":"null");
$sql.= ", fk_user_done=".($userdoneid > 0 ? "'".$userdoneid."'":"null");
if (! empty($this->fk_element)) $sql.= ", fk_element=".($this->fk_element?$this->fk_element:"null");
if (! empty($this->elementtype)) $sql.= ", elementtype=".($this->elementtype?"'".$this->elementtype."'":"null");
$sql.= " WHERE id=".$this->id;
dol_syslog(get_class($this)."::update", LOG_DEBUG);
if ($this->db->query($sql))
{
$action='update';
// Actions on extra fields (by external module or standard code)
// TODO le hook fait double emploi avec le trigger !!
$hookmanager->initHooks(array('actioncommdao'));
$parameters=array('actcomm'=>$this->id);
$reshook=$hookmanager->executeHooks('insertExtraFields',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
if (empty($reshook))
{
if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
{
$result=$this->insertExtraFields();
if ($result < 0)
{
$error++;
}
}
}
else if ($reshook < 0) $error++;
// Now insert assignedusers
if (! $error)
{
$sql ="DELETE FROM ".MAIN_DB_PREFIX."actioncomm_resources where fk_actioncomm = ".$this->id." AND element_type = 'user'";
$resql = $this->db->query($sql);
foreach($this->userassigned as $key => $val)
{
$sql ="INSERT INTO ".MAIN_DB_PREFIX."actioncomm_resources(fk_actioncomm, element_type, fk_element, mandatory, transparency, answer_status)";
$sql.=" VALUES(".$this->id.", 'user', ".$val['id'].", ".(empty($val['manadatory'])?'0':$val['manadatory']).", ".(empty($val['transparency'])?'0':$val['transparency']).", ".(empty($val['answer_status'])?'0':$val['answer_status']).")";
$resql = $this->db->query($sql);
if (! $resql)
{
$error++;
$this->errors[]=$this->db->lasterror();
}
//var_dump($sql);exit;
}
}
if (! $error && ! $notrigger)
{
// Call trigger
$result=$this->call_trigger('ACTION_MODIFY',$user);
if ($result < 0) { $error++; }
// End call triggers
}
if (! $error)
{
$this->db->commit();
return 1;
}
else
{
$this->db->rollback();
dol_syslog(get_class($this)."::update ".join(',',$this->errors),LOG_ERR);
return -2;
}
}
else
{
$this->db->rollback();
$this->error=$this->db->lasterror();
return -1;
}
}
/**
* Load all objects with filters
*
* @param DoliDb $db Database handler
* @param int $socid Filter by thirdparty
* @param int $fk_element Id of element action is linked to
* @param string $elementtype Type of element action is linked to
* @param string $filter Other filter
* @param string $sortfield Sort on this field
* @param string $sortorder ASC or DESC
* @return array or string Error string if KO, array with actions if OK
*/
static function getActions($db, $socid=0, $fk_element=0, $elementtype='', $filter='', $sortfield='', $sortorder='')
{
global $conf, $langs;
$resarray=array();
$sql = "SELECT a.id";
$sql.= " FROM ".MAIN_DB_PREFIX."actioncomm as a";
$sql.= " WHERE a.entity IN (".getEntity('actioncomm', 1).")";
if (! empty($socid)) $sql.= " AND a.fk_soc = ".$socid;
if (! empty($elementtype))
{
if ($elementtype == 'project') $sql.= ' AND a.fk_project = '.$fk_element;
else $sql.= " AND a.fk_element = ".$fk_element." AND a.elementtype = '".$elementtype."'";
}
if (! empty($filter)) $sql.= $filter;
if ($sortorder && $sortfield) $sql.=$db->order($sortfield, $sortorder);
dol_syslog(get_class()."::getActions", LOG_DEBUG);
$resql=$db->query($sql);
if ($resql)
{
$num = $db->num_rows($resql);
if ($num)
{
for($i=0;$i<$num;$i++)
{
$obj = $db->fetch_object($resql);
$actioncommstatic = new ActionComm($db);
$actioncommstatic->fetch($obj->id);
$resarray[$i] = $actioncommstatic;
}
}
$db->free($resql);
return $resarray;
}
else
{
return $db->lasterror();
}
}
/**
* Load indicators for dashboard (this->nbtodo and this->nbtodolate)
*
* @param User $user Objet user
* @return WorkboardResponse|int <0 if KO, WorkboardResponse if OK
*/
function load_board($user)
{
global $conf, $user, $langs;
$sql = "SELECT a.id, a.datep as dp";
$sql.= " FROM (".MAIN_DB_PREFIX."actioncomm as a";
$sql.= ")";
if (! $user->rights->societe->client->voir && ! $user->societe_id) $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON a.fk_soc = sc.fk_soc";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON a.fk_soc = s.rowid";
$sql.= " WHERE a.percent >= 0 AND a.percent < 100";
$sql.= " AND a.entity IN (".getEntity('actioncomm', 1).")";
if (! $user->rights->societe->client->voir && ! $user->societe_id) $sql.= " AND (a.fk_soc IS NULL OR sc.fk_user = " .$user->id . ")";
if ($user->societe_id) $sql.=" AND a.fk_soc = ".$user->societe_id;
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 . ")";
$resql=$this->db->query($sql);
if ($resql)
{
$now = dol_now();
$response = new WorkboardResponse();
$response->warning_delay = $conf->actions->warning_delay/60/60/24;
$response->label = $langs->trans("ActionsToDo");
$response->url = DOL_URL_ROOT.'/comm/action/listactions.php?status=todo&mainmenu=agenda';
$response->img = img_object($langs->trans("Actions"),"action");
// This assignment in condition is not a bug. It allows walking the results.
while ($obj=$this->db->fetch_object($resql))
{
$response->nbtodo++;
if (isset($obj->dp) && $this->db->jdate($obj->dp) < ($now - $conf->actions->warning_delay)) {
$response->nbtodolate++;
}
}
return $response;
}
else
{
$this->error=$this->db->error();
return -1;
}
}
/**
* Charge les informations d'ordre info dans l'objet facture
*
* @param int $id Id de la facture a charger
* @return void
*/
function info($id)
{
$sql = 'SELECT ';
$sql.= ' a.id,';
$sql.= ' datec,';
$sql.= ' tms as datem,';
$sql.= ' fk_user_author,';
$sql.= ' fk_user_mod';
$sql.= ' FROM '.MAIN_DB_PREFIX.'actioncomm as a';
$sql.= ' WHERE a.id = '.$id;
dol_syslog(get_class($this)."::info", LOG_DEBUG);
$result=$this->db->query($sql);
if ($result)
{
if ($this->db->num_rows($result))
{
$obj = $this->db->fetch_object($result);
$this->id = $obj->id;
if ($obj->fk_user_author)
{
$cuser = new User($this->db);
$cuser->fetch($obj->fk_user_author);
$this->user_creation = $cuser;
}
if ($obj->fk_user_mod)
{
$muser = new User($this->db);
$muser->fetch($obj->fk_user_mod);
$this->user_modification = $muser;
}
$this->date_creation = $this->db->jdate($obj->datec);
$this->date_modification = $this->db->jdate($obj->datem);
}
$this->db->free($result);
}
else
{
dol_print_error($this->db);
}
}
/**
* Return label of status
*
* @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
* @param int $hidenastatus 1=Show nothing if status is "Not applicable"
* @return string String with status
*/
function getLibStatut($mode,$hidenastatus=0)
{
return $this->LibStatut($this->percentage,$mode,$hidenastatus);
}
/**
* Return label of action status
*
* @param int $percent Percent
* @param int $mode 0=Long label, 1=Short label, 2=Picto+Short label, 3=Picto, 4=Picto+Short label, 5=Short label+Picto, 6=Very short label+Picto
* @param int $hidenastatus 1=Show nothing if status is "Not applicable"
* @return string Label
*/
function LibStatut($percent,$mode,$hidenastatus=0)
{
global $langs;
if ($mode == 0)
{
if ($percent==-1 && ! $hidenastatus) return $langs->trans('StatusNotApplicable');
else if ($percent==0) return $langs->trans('StatusActionToDo').' (0%)';
else if ($percent > 0 && $percent < 100) return $langs->trans('StatusActionInProcess').' ('.$percent.'%)';
else if ($percent >= 100) return $langs->trans('StatusActionDone').' (100%)';
}
else if ($mode == 1)
{
if ($percent==-1 && ! $hidenastatus) return $langs->trans('StatusNotApplicable');
else if ($percent==0) return $langs->trans('StatusActionToDo');
else if ($percent > 0 && $percent < 100) return $percent.'%';
else if ($percent >= 100) return $langs->trans('StatusActionDone');
}
else if ($mode == 2)
{
if ($percent==-1 && ! $hidenastatus) return img_picto($langs->trans('StatusNotApplicable'),'statut9').' '.$langs->trans('StatusNotApplicable');
else if ($percent==0) return img_picto($langs->trans('StatusActionToDo'),'statut1').' '.$langs->trans('StatusActionToDo');
else if ($percent > 0 && $percent < 100) return img_picto($langs->trans('StatusActionInProcess'),'statut3').' '. $percent.'%';
else if ($percent >= 100) return img_picto($langs->trans('StatusActionDone'),'statut6').' '.$langs->trans('StatusActionDone');
}
else if ($mode == 3)
{
if ($percent==-1 && ! $hidenastatus) return img_picto($langs->trans("Status").': '.$langs->trans('StatusNotApplicable'),'statut9');
else if ($percent==0) return img_picto($langs->trans("Status").': '.$langs->trans('StatusActionToDo').' (0%)','statut1');
else if ($percent > 0 && $percent < 100) return img_picto($langs->trans("Status").': '.$langs->trans('StatusActionInProcess').' ('.$percent.'%)','statut3');
else if ($percent >= 100) return img_picto($langs->trans("Status").': '.$langs->trans('StatusActionDone').' (100%)','statut6');
}
else if ($mode == 4)
{
if ($percent==-1 && ! $hidenastatus) return img_picto($langs->trans('StatusNotApplicable'),'statut9').' '.$langs->trans('StatusNotApplicable');
else if ($percent==0) return img_picto($langs->trans('StatusActionToDo'),'statut1').' '.$langs->trans('StatusActionToDo').' (0%)';
else if ($percent > 0 && $percent < 100) return img_picto($langs->trans('StatusActionInProcess'),'statut3').' '.$langs->trans('StatusActionInProcess').' ('.$percent.'%)';
else if ($percent >= 100) return img_picto($langs->trans('StatusActionDone'),'statut6').' '.$langs->trans('StatusActionDone').' (100%)';
}
else if ($mode == 5)
{
if ($percent==-1 && ! $hidenastatus) return img_picto($langs->trans('StatusNotApplicable'),'statut9');
else if ($percent==0) return '0% '.img_picto($langs->trans('StatusActionToDo'),'statut1');
else if ($percent > 0 && $percent < 100) return $percent.'% '.img_picto($langs->trans('StatusActionInProcess').' - '.$percent.'%','statut3');
else if ($percent >= 100) return $langs->trans('StatusActionDone').' '.img_picto($langs->trans('StatusActionDone'),'statut6');
}
else if ($mode == 6)
{
if ($percent==-1 && ! $hidenastatus) return img_picto($langs->trans('StatusNotApplicable'),'statut9');
else if ($percent==0) return '0% '.img_picto($langs->trans('StatusActionToDo'),'statut1');
else if ($percent > 0 && $percent < 100) return $percent.'% '.img_picto($langs->trans('StatusActionInProcess').' - '.$percent.'%','statut3');
else if ($percent >= 100) return img_picto($langs->trans('StatusActionDone'),'statut6');
}
return '';
}
/**
* Return URL of event
* Use $this->id, $this->type_code, $this->label and $this->type_label
*
* @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto
* @param int $maxlength Nombre de caracteres max dans libelle
* @param string $classname Force style class on a link
* @param string $option ''=Link to action,'birthday'=Link to contact
* @param int $overwritepicto 1=Overwrite picto
* @return string Chaine avec URL
*/
function getNomUrl($withpicto=0,$maxlength=0,$classname='',$option='',$overwritepicto=0)
{
global $conf,$langs;
$result='';
$tooltip = '<u>' . $langs->trans('ShowAction'.$objp->code) . '</u>';
if (! empty($this->ref))
$tooltip .= '<br><b>' . $langs->trans('Ref') . ':</b> ' . $this->ref;
if (! empty($this->label))
$tooltip .= '<br><b>' . $langs->trans('Title') . ':</b> ' . $this->label;
$label = $this->label;
if (empty($label)) $label=$this->libelle; // For backward compatibility
$linkclose = '" title="'.dol_escape_htmltag($tooltip, 1).'">';
if ($option=='birthday') $link = '<a class="'.$classname.' classfortooltip" href="'.DOL_URL_ROOT.'/contact/perso.php?id='.$this->id.$linkclose;
else $link = '<a class="'.$classname.' classfortooltip" href="'.DOL_URL_ROOT.'/comm/action/card.php?id='.$this->id.$linkclose;
$linkend='</a>';
//print 'rrr'.$this->libelle.'-'.$withpicto;
if ($withpicto == 2)
{
$libelle=$label;
if (! empty($conf->global->AGENDA_USE_EVENT_TYPE)) $libelle=$langs->transnoentities("Action".$this->type_code);
$libelleshort='';
}
else
{
$libelle=(empty($this->libelle)?$label:$this->libelle.(($label && $label != $this->libelle)?' '.$label:''));
if (! empty($conf->global->AGENDA_USE_EVENT_TYPE) && empty($libelle)) $libelle=($langs->transnoentities("Action".$this->type_code) != "Action".$this->type_code)?$langs->transnoentities("Action".$this->type_code):$this->type_label;