-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
adherent.class.php
2113 lines (1864 loc) · 77.5 KB
/
adherent.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-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2002-2003 Jean-Louis Bergamo <jlb@j1b.org>
* Copyright (C) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2004 Sebastien Di Cintio <sdicintio@ressource-toi.org>
* Copyright (C) 2004 Benoit Mortier <benoit.mortier@opensides.be>
* Copyright (C) 2009 Regis Houssin <regis@dolibarr.fr>
*
* 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 2 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/adherents/class/adherent.class.php
* \ingroup member
* \brief File of class to manage members of a foundation
*/
require_once(DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php");
require_once(DOL_DOCUMENT_ROOT."/adherents/class/cotisation.class.php");
require_once(DOL_DOCUMENT_ROOT."/core/lib/functions2.lib.php");
require_once(DOL_DOCUMENT_ROOT."/core/lib/date.lib.php");
/**
* \class Adherent
* \brief Class to manage members of a foundation
*/
class Adherent extends CommonObject
{
public $element='member';
public $table_element='adherent';
protected $ismultientitymanaged = 1; // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
var $id;
var $ref;
var $civilite_id;
var $firstname;
var $prenom; // deprecated
var $lastname;
var $nom; // deprecated
var $login;
var $pass;
var $societe;
var $adresse;
var $address;
var $cp;
var $zip;
var $ville;
var $town;
var $state_id; // Id of department
var $state_code; // Code of department
var $state; // Label of department
var $fk_departement; // deprecated
var $departement_code; // deprecated
var $departement; // deprecated
var $country_id;
var $country_code;
var $country;
var $pays_id; // deprecated
var $pays_code; // deprecated
var $pays; // deprecated
var $email;
var $phone;
var $phone_perso;
var $phone_mobile;
var $morphy;
var $public;
var $note; // Private note
var $statut; // -1:brouillon, 0:resilie, >=1:valide,paye
var $photo;
var $datec;
var $datem;
var $datefin;
var $datevalid;
var $naiss;
var $typeid; // Id type adherent
var $type; // Libelle type adherent
var $need_subscription;
var $user_id;
var $user_login;
var $fk_soc;
// Fields loaded by fetch_subscriptions()
var $first_subscription_date;
var $first_subscription_amount;
var $last_subscription_date;
var $last_subscription_amount;
var $subscriptions=array();
// var $public;
var $array_options;
var $oldcopy; // To contains a clone of this when we need to save old properties of object
/**
* Constructor
*
* @param DoliDB $db Database handler
*/
function Adherent($db)
{
$this->db = $db;
$this->statut = -1;
// l'adherent n'est pas public par defaut
$this->public = 0;
// les champs optionnels sont vides
$this->array_options=array();
}
/**
* Fonction envoyant un email a l'adherent avec le texte fourni en parametre.
*
* @param string $text Content of message (not html entities encoded)
* @param string $subject Subject of message
* @param array $filename_list Array of attached files
* @param array $mimetype_list Array of mime types of attached files
* @param array $mimefilename_list Array of public names of attached files
* @param string $addr_cc Email cc
* @param string $addr_bcc Email bcc
* @param int $deliveryreceipt Ask a delivery receipt
* @param int $msgishtml 1=String IS already html, 0=String IS NOT html, -1=Unknown need autodetection
* @param string $errors_to erros to
* @return int <0 if KO, >0 if OK
*/
function send_an_email($text, $subject, $filename_list=array(), $mimetype_list=array(), $mimefilename_list=array(), $addr_cc="", $addr_bcc="", $deliveryreceipt=0, $msgishtml=-1, $errors_to='')
{
global $conf,$langs;
// Detect if message is HTML
if ($msgishtml == -1)
{
$msgishtml = 0;
if (dol_textishtml($text,1)) $msgishtml = 1;
}
$texttosend=$this->makeSubstitution($text);
$subjecttosend=$this->makeSubstitution($subject);
if ($msgishtml) $texttosend=dol_htmlentitiesbr($texttosend);
// Envoi mail confirmation
$from=$conf->email_from;
if ($conf->global->ADHERENT_MAIL_FROM) $from=$conf->global->ADHERENT_MAIL_FROM;
include_once(DOL_DOCUMENT_ROOT."/core/class/CMailFile.class.php");
$mailfile = new CMailFile($subjecttosend, $this->email, $from, $texttosend, $filename_list, $mimetype_list, $mimefilename_list, $addr_cc, $addr_bcc, $deliveryreceipt, $msgishtml);
if ($mailfile->sendfile())
{
return 1;
}
else
{
$this->error=$langs->trans("ErrorFailedToSendMail",$from,$this->email).'. '.$mailfile->error;
return -1;
}
}
/**
* Make substitution
*
* @param string $text Text to make substitution to
* @return string Value of input text string with substitutions done
*/
function makeSubstitution($text)
{
global $langs;
$birthday = dol_print_date($this->naiss,'day');
$msgishtml = 0;
if (dol_textishtml($text,1)) $msgishtml = 1;
$infos='';
if ($this->civilite_id) $infos.= $langs->transnoentities("UserTitle").": ".$this->getCivilityLabel(1)."\n";
$infos.= $langs->transnoentities("Lastname").": ".$this->lastname."\n";
$infos.= $langs->transnoentities("Firstname").": ".$this->firstname."\n";
$infos.= $langs->transnoentities("Company").": ".$this->societe."\n";
$infos.= $langs->transnoentities("Address").": ".$this->address."\n";
$infos.= $langs->transnoentities("Zip").": ".$this->zip."\n";
$infos.= $langs->transnoentities("Town").": ".$this->town."\n";
$infos.= $langs->transnoentities("Country").": ".$this->country."\n";
$infos.= $langs->transnoentities("EMail").": ".$this->email."\n";
$infos.= $langs->transnoentities("Login").": ".$this->login."\n";
$infos.= $langs->transnoentities("Password").": ".$this->pass."\n";
$infos.= $langs->transnoentities("Birthday").": ".$birthday."\n";
$infos.= $langs->transnoentities("Photo").": ".$this->photo."\n";
$infos.= $langs->transnoentities("Public").": ".yn($this->public);
// Substitutions
$substitutionarray=array(
'%DOL_MAIN_URL_ROOT%'=>DOL_MAIN_URL_ROOT,
'%INFOS%'=>$msgishtml?dol_htmlentitiesbr($infos):$infos,
'%CIVILITE%'=>$this->getCivilityLabel($msgishtml?0:1),
'%PRENOM%'=>$msgishtml?dol_htmlentitiesbr($this->firstname):$this->firstname,
'%NOM%'=>$msgishtml?dol_htmlentitiesbr($this->lastname):$this->lastname,
'%SOCIETE%'=>$msgishtml?dol_htmlentitiesbr($this->societe):$this->societe,
'%ADRESSE%'=>$msgishtml?dol_htmlentitiesbr($this->address):$this->address,
'%CP%'=>$msgishtml?dol_htmlentitiesbr($this->zip):$this->zip,
'%VILLE%'=>$msgishtml?dol_htmlentitiesbr($this->town):$this->town,
'%PAYS%'=>$msgishtml?dol_htmlentitiesbr($this->country):$this->country,
'%EMAIL%'=>$msgishtml?dol_htmlentitiesbr($this->email):$this->email,
'%NAISS%'=>$msgishtml?dol_htmlentitiesbr($birthday):$birthday,
'%PHOTO%'=>$msgishtml?dol_htmlentitiesbr($this->photo):$this->photo,
'%LOGIN%'=>$msgishtml?dol_htmlentitiesbr($this->login):$this->login,
'%PASSWORD%'=>$msgishtml?dol_htmlentitiesbr($this->pass):$this->pass
);
complete_substitutions_array($substitutionarray, $langs);
return make_substitutions($text,$substitutionarray);
}
/**
* Renvoie le libelle traduit de la nature d'un adherent (physique ou morale)
*
* @param string $morphy Nature physique ou morale de l'adherent
* @return string Label
*/
function getmorphylib($morphy='')
{
global $langs;
if (! $morphy) { $morphy=$this->morphy; }
if ($morphy == 'phy') { return $langs->trans("Physical"); }
if ($morphy == 'mor') { return $langs->trans("Moral"); }
return $morphy;
}
/**
* Create a member into database
*
* @param User $user Objet user qui demande la creation
* @param int $notrigger 1 ne declenche pas les triggers, 0 sinon
* @return int <0 if KO, >0 if OK
*/
function create($user,$notrigger=0)
{
global $conf,$langs;
$error=0;
$now=dol_now();
// Check parameters
if (! empty($conf->global->ADHERENT_MAIL_REQUIRED) && ! isValidEMail($this->email))
{
$langs->load("errors");
$this->error = $langs->trans("ErrorBadEMail",$this->email);
return -1;
}
if (! $this->datec) $this->datec=$now;
if (empty($conf->global->ADHERENT_LOGIN_NOT_REQUIRED))
{
if (empty($this->login))
{
$this->error = $langs->trans("ErrorWrongValueForParameterX","Login");
return -1;
}
}
$this->db->begin();
// Insert member
$sql = "INSERT INTO ".MAIN_DB_PREFIX."adherent";
$sql.= " (datec,login,fk_user_author,fk_user_mod,fk_user_valid,morphy,fk_adherent_type,entity)";
$sql.= " VALUES (";
$sql.= " '".$this->db->idate($this->datec)."'";
$sql.= ", ".($this->login?"'".$this->db->escape($this->login)."'":"null");
$sql.= ", ".($user->id>0?$user->id:"null"); // Can be null because member can be createb by a guest or a script
$sql.= ", null, null, '".$this->morphy."'";
$sql.= ", '".$this->typeid."'";
$sql.= ", ".$conf->entity;
$sql.= ")";
dol_syslog(get_class($this)."::create sql=".$sql);
$result = $this->db->query($sql);
if ($result)
{
$id = $this->db->last_insert_id(MAIN_DB_PREFIX."adherent");
if ($id > 0)
{
$this->id=$id;
$this->ref=$id;
// Update minor fields
$result=$this->update($user,1,1); // nosync is 1 to avoid update data of user
if ($result < 0)
{
$this->db->rollback();
return -1;
}
// Add link to user
if ($this->user_id)
{
// Add link to user
$sql = "UPDATE ".MAIN_DB_PREFIX."user SET";
$sql.= " fk_member = '".$this->id."'";
$sql.= " WHERE rowid = ".$this->user_id;
dol_syslog(get_class($this)."::create sql=".$sql);
$resql = $this->db->query($sql);
if (! $resql)
{
$this->error='Failed to update user to make link with member';
$this->db->rollback();
return -4;
}
}
if (! $notrigger)
{
// Appel des triggers
include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
$interface=new Interfaces($this->db);
$result=$interface->run_triggers('MEMBER_CREATE',$this,$user,$langs,$conf);
if ($result < 0) { $error++; $this->errors=$interface->errors; }
// Fin appel triggers
}
if (count($this->errors))
{
dol_syslog(get_class($this)."::create ".join(',',$this->errors), LOG_ERR);
$this->db->rollback();
return -3;
}
else
{
$this->db->commit();
return $this->id;
}
}
else
{
$this->error='Failed to get last insert id';
dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
$this->db->rollback();
return -2;
}
}
else
{
$this->error=$this->db->error();
dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
$this->db->rollback();
return -1;
}
}
/**
* Update a member in database (standard information and password)
*
* @param User $user User making update
* @param int $notrigger 1=disable trigger UPDATE (when called by create)
* @param int $nosyncuser 0=Synchronize linked user (standard info), 1=Do not synchronize linked user
* @param int $nosyncuserpass 0=Synchronize linked user (password), 1=Do not synchronize linked user
* @param int $nosyncthirdparty 0=Synchronize linked thirdparty (standard info), 1=Do not synchronize linked thirdparty
* @return int <0 if KO, >0 if OK
*/
function update($user,$notrigger=0,$nosyncuser=0,$nosyncuserpass=0,$nosyncthirdparty=0)
{
global $conf, $langs;
$nbrowsaffected=0;
$error=0;
dol_syslog(get_class($this)."::update notrigger=".$notrigger.", nosyncuser=".$nosyncuser.", nosyncuserpass=".$nosyncuserpass.", email=".$this->email);
// Clean parameters
$this->lastname=trim($this->lastname)?trim($this->lastname):trim($this->nom);
$this->firstname=trim($this->firstname)?trim($this->firstname):trim($this->prenom);
$this->address=($this->address?$this->address:$this->adresse);
$this->zip=($this->zip?$this->zip:$this->cp);
$this->town=($this->town?$this->town:$this->ville);
$this->country_id=($this->country_id > 0?$this->country_id:$this->fk_pays);
$this->state_id=($this->state_id > 0?$this->state_id:$this->fk_departement);
if (! empty($conf->global->MAIN_FIRST_TO_UPPER)) $this->lastname=ucwords(trim($this->lastname));
if (! empty($conf->global->MAIN_FIRST_TO_UPPER)) $this->firstname=ucwords(trim($this->firstname));
// Check parameters
if (! empty($conf->global->ADHERENT_MAIL_REQUIRED) && ! isValidEMail($this->email))
{
$langs->load("errors");
$this->error = $langs->trans("ErrorBadEMail",$this->email);
return -1;
}
$this->db->begin();
$sql = "UPDATE ".MAIN_DB_PREFIX."adherent SET";
$sql.= " civilite = ".($this->civilite_id?"'".$this->civilite_id."'":"null");
$sql.= ", prenom = ".($this->firstname?"'".$this->db->escape($this->firstname)."'":"null");
$sql.= ", nom=" .($this->lastname?"'".$this->db->escape($this->lastname)."'":"null");
$sql.= ", login=" .($this->login?"'".$this->db->escape($this->login)."'":"null");
$sql.= ", societe=" .($this->societe?"'".$this->db->escape($this->societe)."'":"null");
$sql.= ", fk_soc=" .($this->fk_soc > 0?"'".$this->fk_soc."'":"null");
$sql.= ", adresse=" .($this->address?"'".$this->db->escape($this->address)."'":"null");
$sql.= ", cp=" .($this->zip?"'".$this->db->escape($this->zip)."'":"null");
$sql.= ", ville=" .($this->town?"'".$this->db->escape($this->town)."'":"null");
$sql.= ", pays=" .($this->country_id>0?"'".$this->country_id."'":"null");
$sql.= ", fk_departement=".($this->state_id>0?"'".$this->state_id."'":"null");
$sql.= ", email='".$this->email."'";
$sql.= ", phone=" .($this->phone?"'".$this->db->escape($this->phone)."'":"null");
$sql.= ", phone_perso=" .($this->phone_perso?"'".$this->db->escape($this->phone_perso)."'":"null");
$sql.= ", phone_mobile=" .($this->phone_mobile?"'".$this->db->escape($this->phone_mobile)."'":"null");
$sql.= ", note=" .($this->note?"'".$this->db->escape($this->note)."'":"null");
$sql.= ", photo=" .($this->photo?"'".$this->photo."'":"null");
$sql.= ", public='".$this->public."'";
$sql.= ", statut=" .$this->statut;
$sql.= ", fk_adherent_type=".$this->typeid;
$sql.= ", morphy='".$this->morphy."'";
$sql.= ", naiss=" .($this->naiss?"'".$this->db->idate($this->naiss)."'":"null");
if ($this->datefin) $sql.= ", datefin='".$this->db->idate($this->datefin)."'"; // Ne doit etre modifie que par effacement cotisation
if ($this->datevalid) $sql.= ", datevalid='".$this->db->idate($this->datevalid)."'"; // Ne doit etre modifie que par validation adherent
$sql.= ", fk_user_mod=".($user->id>0?$user->id:'null'); // Can be null because member can be create by a guest
$sql.= " WHERE rowid = ".$this->id;
dol_syslog(get_class($this)."::update update member sql=".$sql);
$resql = $this->db->query($sql);
if ($resql)
{
unset($this->country_code);
unset($this->country);
unset($this->state_code);
unset($this->state);
$nbrowsaffected+=$this->db->affected_rows($resql);
// Actions on extra fields (by external module)
include_once(DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php');
$hookmanager=new HookManager($this->db);
$hookmanager->callHooks(array('memberdao'));
$parameters=array('id'=>$this->id);
$action='';
$reshook=$hookmanager->executeHooks('insertExtraFields',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
if (empty($reshook))
{
$result=$this->insertExtraFields();
if ($result < 0)
{
$error++;
}
}
else if ($reshook < 0) $error++;
// Update password
if (! $error && $this->pass)
{
dol_syslog(get_class($this)."::update update password");
if ($this->pass != $this->pass_indatabase && $this->pass != $this->pass_indatabase_crypted)
{
// Si mot de passe saisi et different de celui en base
$result=$this->setPassword($user,$this->pass,0,$notrigger,$nosyncuserpass);
if (! $nbrowsaffected) $nbrowsaffected++;
}
}
// Remove links to user and replace with new one
if (! $error)
{
dol_syslog(get_class($this)."::update update link to user");
$sql = "UPDATE ".MAIN_DB_PREFIX."user SET fk_member = NULL WHERE fk_member = ".$this->id;
dol_syslog(get_class($this)."::update sql=".$sql, LOG_DEBUG);
$resql = $this->db->query($sql);
if (! $resql) { $this->error=$this->db->error(); $this->db->rollback(); return -5; }
// If there is a user linked to this member
if ($this->user_id > 0)
{
$sql = "UPDATE ".MAIN_DB_PREFIX."user SET fk_member = ".$this->id." WHERE rowid = ".$this->user_id;
dol_syslog(get_class($this)."::update sql=".$sql, LOG_DEBUG);
$resql = $this->db->query($sql);
if (! $resql) { $this->error=$this->db->error(); $this->db->rollback(); return -5; }
}
}
if (! $error && $nbrowsaffected) // If something has change in main data
{
// Update information on linked user if it is an update
if ($this->user_id > 0 && ! $nosyncuser)
{
require_once(DOL_DOCUMENT_ROOT."/user/class/user.class.php");
dol_syslog(get_class($this)."::update update linked user");
$luser=new User($this->db);
$result=$luser->fetch($this->user_id);
if ($result >= 0)
{
$luser->civilite_id=$this->civilite_id;
$luser->firstname=$this->firstname;
$luser->lastname=$this->lastname;
$luser->prenom=$this->firstname; // deprecated
$luser->nom=$this->lastname; // deprecated
$luser->login=$this->user_login;
$luser->pass=$this->pass;
$luser->societe_id=$this->societe;
$luser->email=$this->email;
$luser->office_phone=$this->phone;
$luser->user_mobile=$this->phone_mobile;
$luser->note=$this->note;
$luser->fk_member=$this->id;
$result=$luser->update($user,0,1,1); // Use nosync to 1 to avoid cyclic updates
if ($result < 0)
{
$this->error=$luser->error;
dol_syslog(get_class($this)."::update ".$this->error,LOG_ERR);
$error++;
}
}
else
{
$this->error=$luser->error;
$error++;
}
}
// Update information on linked thirdparty if it is an update
if ($this->fk_soc > 0 && ! $nosyncthirdparty)
{
require_once(DOL_DOCUMENT_ROOT."/societe/class/societe.class.php");
dol_syslog(get_class($this)."::update update linked thirdparty");
// This member is linked with a thirdparty, so we also update thirdparty informations
// if this is an update.
$lthirdparty=new Societe($this->db);
$result=$lthirdparty->fetch($this->fk_soc);
if ($result >= 0)
{
$lthirdparty->address=$this->address;
$lthirdparty->zip=$this->zip;
$lthirdparty->town=$this->town;
$lthirdparty->email=$this->email;
$lthirdparty->tel=$this->phone;
$lthirdparty->state_id=$this->state_id;
$lthirdparty->country_id=$this->country_id;
$lthirdparty->pays_id=$this->country_id;
//$lthirdparty->phone_mobile=$this->phone_mobile;
$result=$lthirdparty->update($this->fk_soc,$user,0,1,1,'update'); // Use sync to 0 to avoid cyclic updates
if ($result < 0)
{
$this->error=$lthirdparty->error;
dol_syslog(get_class($this)."::update ".$this->error,LOG_ERR);
$error++;
}
}
else
{
$this->error=$lthirdparty->error;
$error++;
}
}
if (! $error && ! $notrigger)
{
// Appel des triggers
include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
$interface=new Interfaces($this->db);
$result=$interface->run_triggers('MEMBER_MODIFY',$this,$user,$langs,$conf);
if ($result < 0) { $error++; $this->errors=$interface->errors; }
// Fin appel triggers
}
}
if (! $error)
{
$this->db->commit();
return $nbrowsaffected;
}
else
{
$this->db->rollback();
return -1;
}
}
else
{
$this->db->rollback();
$this->error=$this->db->lasterror();
dol_syslog(get_class($this)."::Update ".$this->error,LOG_ERR);
return -2;
}
}
/**
* Update denormalized last subscription date.
* This function is called when we delete a subscription for example.
*
* @param User $user User making change
* @return int <0 if KO, >0 if OK
*/
function update_end_date($user)
{
global $conf, $langs;
$error=0;
$this->db->begin();
// Search for last subscription id and end date
$sql = "SELECT rowid, datec as dateop, dateadh as datedeb, datef as datefin";
$sql.= " FROM ".MAIN_DB_PREFIX."cotisation";
$sql.= " WHERE fk_adherent='".$this->id."'";
$sql.= " ORDER by dateadh DESC"; // Sort by start subscription date
dol_syslog(get_class($this)."::update_end_date sql=".$sql);
$resql=$this->db->query($sql);
if ($resql)
{
$obj=$this->db->fetch_object($resql);
$dateop=$this->db->jdate($obj->dateop);
$datedeb=$this->db->jdate($obj->datedeb);
$datefin=$this->db->jdate($obj->datefin);
$sql = "UPDATE ".MAIN_DB_PREFIX."adherent SET";
$sql.= " datefin=".($datefin != '' ? "'".$this->db->idate($datefin)."'" : "null");
$sql.= " WHERE rowid = ".$this->id;
dol_syslog(get_class($this)."::update_end_date sql=".$sql);
$resql=$this->db->query($sql);
if ($resql)
{
$this->last_subscription_date=$dateop;
$this->last_subscription_date_start=$datedeb;
$this->last_subscription_date_end=$datefin;
$this->datefin=$datefin;
$this->db->commit();
return 1;
}
else
{
$this->db->rollback();
return -1;
}
}
else
{
$this->error=$this->db->lasterror();
dol_syslog(get_class($this)."::update_end_date ".$this->error, LOG_ERR);
$this->db->rollback();
return -1;
}
}
/**
* Fonction qui supprime l'adherent et les donnees associees
*
* @param int $rowid Id of member to delete
* @return int <0 if KO, 0=nothing to do, >0 if OK
*/
function delete($rowid)
{
global $conf, $langs, $user;
$result = 0;
$error=0;
$this->db->begin();
// Suppression options
$sql = "DELETE FROM ".MAIN_DB_PREFIX."adherent_extrafields WHERE fk_object = ".$rowid;
dol_syslog(get_class($this)."::delete sql=".$sql);
$resql=$this->db->query($sql);
if ($resql)
{
$sql = "DELETE FROM ".MAIN_DB_PREFIX."cotisation WHERE fk_adherent = ".$rowid;
dol_syslog(get_class($this)."::delete sql=".$sql);
$resql=$this->db->query($sql);
if ($resql)
{
$sql = "DELETE FROM ".MAIN_DB_PREFIX."adherent WHERE rowid = ".$rowid;
dol_syslog(get_class($this)."::delete sql=".$sql);
$resql=$this->db->query($sql);
if ($resql)
{
if ($this->db->affected_rows($resql))
{
// Appel des triggers
include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
$interface=new Interfaces($this->db);
$result=$interface->run_triggers('MEMBER_DELETE',$this,$user,$langs,$conf);
if ($result < 0) { $error++; $this->errors=$interface->errors; }
// Fin appel triggers
$this->db->commit();
return 1;
}
else
{
// Rien a effacer
$this->db->rollback();
return 0;
}
}
else
{
$this->error=$this->db->error();
$this->db->rollback();
return -3;
}
}
else
{
$this->error=$this->db->error();
$this->db->rollback();
return -2;
}
}
else
{
$this->error=$this->db->error();
$this->db->rollback();
return -1;
}
}
/**
* Change password of a user
*
* @param User $user Object user de l'utilisateur qui fait la modification
* @param string $password New password (to generate if empty)
* @param int $isencrypted 0 ou 1 si il faut crypter le mot de passe en base (0 par defaut)
* @param int $notrigger 1=Ne declenche pas les triggers
* @param int $nosyncuser Do not synchronize linked user
* @return string If OK return clear password, 0 if no change, < 0 if error
*/
function setPassword($user, $password='', $isencrypted=0, $notrigger=0, $nosyncuser=0)
{
global $conf, $langs;
$error=0;
dol_syslog(get_class($this)."::setPassword user=".$user->id." password=".preg_replace('/./i','*',$password)." isencrypted=".$isencrypted);
// If new password not provided, we generate one
if (! $password)
{
require_once(DOL_DOCUMENT_ROOT."/core/lib/security2.lib.php");
$password=getRandomPassword('');
}
// Cryptage mot de passe
if ($isencrypted)
{
// Encryption
$password_indatabase = dol_hash($password);
}
else
{
$password_indatabase = $password;
}
// Mise a jour
$sql = "UPDATE ".MAIN_DB_PREFIX."adherent SET pass = '".$this->db->escape($password_indatabase)."'";
$sql.= " WHERE rowid = ".$this->id;
//dol_syslog("Adherent::Password sql=hidden");
dol_syslog(get_class($this)."::setPassword sql=".$sql);
$result = $this->db->query($sql);
if ($result)
{
$nbaffectedrows=$this->db->affected_rows($result);
if ($nbaffectedrows)
{
$this->pass=$password;
$this->pass_indatabase=$password_indatabase;
if ($this->user_id && ! $nosyncuser)
{
require_once(DOL_DOCUMENT_ROOT."/user/class/user.class.php");
// This member is linked with a user, so we also update users informations
// if this is an update.
$luser=new User($this->db);
$result=$luser->fetch($this->user_id);
if ($result >= 0)
{
$result=$luser->setPassword($user,$this->pass,0,0,1);
if ($result < 0)
{
$this->error=$luser->error;
dol_syslog(get_class($this)."::setPassword ".$this->error,LOG_ERR);
$error++;
}
}
else
{
$this->error=$luser->error;
$error++;
}
}
if (! $error && ! $notrigger)
{
// Appel des triggers
include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
$interface=new Interfaces($this->db);
$result=$interface->run_triggers('MEMBER_NEW_PASSWORD',$this,$user,$langs,$conf);
if ($result < 0) { $error++; $this->errors=$interface->errors; }
// Fin appel triggers
}
return $this->pass;
}
else
{
return 0;
}
}
else
{
dol_print_error($this->db);
return -1;
}
}
/**
* Set link to a user
*
* @param int $userid Id of user to link to
* @return int 1=OK, -1=KO
*/
function setUserId($userid)
{
global $conf, $langs;
$this->db->begin();
// If user is linked to this member, remove old link to this member
$sql = "UPDATE ".MAIN_DB_PREFIX."user SET fk_member = NULL WHERE fk_member = ".$this->id;
dol_syslog(get_class($this)."::setUserId sql=".$sql, LOG_DEBUG);
$resql = $this->db->query($sql);
if (! $resql) { $this->error=$this->db->error(); $this->db->rollback(); return -1; }
// Set link to user
if ($userid > 0)
{
$sql = "UPDATE ".MAIN_DB_PREFIX."user SET fk_member = ".$this->id;
$sql.= " WHERE rowid = ".$userid;
dol_syslog(get_class($this)."::setUserId sql=".$sql, LOG_DEBUG);
$resql = $this->db->query($sql);
if (! $resql) { $this->error=$this->db->error(); $this->db->rollback(); return -2; }
}
$this->db->commit();
return 1;
}
/**
* Set link to a third party
*
* @param int $thirdpartyid Id of user to link to
* @return int 1=OK, -1=KO
*/
function setThirdPartyId($thirdpartyid)
{
global $conf, $langs;
$this->db->begin();
// Update link to third party
if ($thirdpartyid > 0)
{
$sql = "UPDATE ".MAIN_DB_PREFIX."adherent SET fk_soc = null";
$sql.= " WHERE fk_soc = '".$thirdpartyid."'";
$sql.= " AND entity = ".$conf->entity;
dol_syslog(get_class($this)."::setThirdPartyId sql=".$sql);
$resql = $this->db->query($sql);
}
// Update link to third party
$sql = "UPDATE ".MAIN_DB_PREFIX."adherent SET fk_soc = ".($thirdpartyid>0 ? $thirdpartyid : 'null');
$sql.= " WHERE rowid = ".$this->id;
dol_syslog(get_class($this)."::setThirdPartyId sql=".$sql);
$resql = $this->db->query($sql);
if ($resql)
{
$this->db->commit();
return 1;
}
else
{
$this->error=$this->db->error();
dol_syslog(get_class($this)."::setThirdPartyId ".$this->error, LOG_ERR);
$this->db->rollback();
return -1;
}
}
/**
* Method to load member from its login
*
* @param string $login login of member
* @return void
*/
function fetch_login($login)
{
global $conf;
$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."adherent";
$sql.= " WHERE login='".$this->db->escape($login)."'";
$sql.= " AND entity = ".$conf->entity;
$resql=$this->db->query($sql);
if ($resql)
{
if ($this->db->num_rows($resql))
{
$obj = $this->db->fetch_object($resql);
$this->fetch($obj->rowid);
}
}
else
{
dol_print_error($this->db);
}
}
/**
* Load member from database
*
* @param int $rowid Id of object to load
* @param string $ref To load member from its ref
* @param int $fk_soc To load member from its link to third party
* @return int >0 if OK, 0 if not found, <0 if KO
*/
function fetch($rowid,$ref='',$fk_soc='')
{
global $conf, $langs;
$sql = "SELECT d.rowid, d.civilite, d.prenom as firstname, d.nom as lastname, d.societe, d.fk_soc, d.statut, d.public, d.adresse as address, d.cp as zip, d.ville as town, d.note,";
$sql.= " d.email, d.phone, d.phone_perso, d.phone_mobile, d.login, d.pass,";
$sql.= " d.photo, d.fk_adherent_type, d.morphy,";
$sql.= " d.datec as datec,";
$sql.= " d.tms as datem,";
$sql.= " d.datefin as datefin,";
$sql.= " d.naiss as datenaiss,";
$sql.= " d.datevalid as datev,";
$sql.= " d.pays,";
$sql.= " d.fk_departement,";
$sql.= " p.rowid as country_id, p.code as country_code, p.libelle as country,";
$sql.= " dep.nom as state, dep.code_departement as state_code,";
$sql.= " t.libelle as type, t.cotisation as cotisation,";
$sql.= " u.rowid as user_id, u.login as user_login";
$sql.= " FROM ".MAIN_DB_PREFIX."adherent_type as t, ".MAIN_DB_PREFIX."adherent as d";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_pays as p ON d.pays = p.rowid";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as dep ON d.fk_departement = dep.rowid";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."user as u ON d.rowid = u.fk_member";
$sql.= " WHERE d.fk_adherent_type = t.rowid";
$sql.= " AND d.entity = ".$conf->entity;
if ($ref) $sql.= " AND d.rowid='".$ref."'";
elseif ($fk_soc) $sql.= " AND d.fk_soc='".$fk_soc."'";
else $sql.= " AND d.rowid=".$rowid;
dol_syslog(get_class($this)."::fetch sql=".$sql);
$resql=$this->db->query($sql);
if ($resql)
{
if ($this->db->num_rows($resql))
{