adherent.class.php 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053
  1. <?php
  2. /* Copyright (C) 2002-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2002-2003 Jean-Louis Bergamo <jlb@j1b.org>
  4. * Copyright (C) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  5. * Copyright (C) 2004 Sebastien Di Cintio <sdicintio@ressource-toi.org>
  6. * Copyright (C) 2004 Benoit Mortier <benoit.mortier@opensides.be>
  7. * Copyright (C) 2009 Regis Houssin <regis@dolibarr.fr>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file htdocs/adherents/class/adherent.class.php
  24. * \ingroup member
  25. * \brief File of class to manage members of a foundation
  26. */
  27. require_once(DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php");
  28. require_once(DOL_DOCUMENT_ROOT."/adherents/class/cotisation.class.php");
  29. require_once(DOL_DOCUMENT_ROOT."/lib/functions2.lib.php");
  30. require_once(DOL_DOCUMENT_ROOT."/lib/date.lib.php");
  31. /**
  32. * \class Adherent
  33. * \brief Class to manage members of a foundation
  34. */
  35. class Adherent extends CommonObject
  36. {
  37. var $db;
  38. var $error;
  39. var $errors=array();
  40. var $element='member';
  41. var $table_element='adherent';
  42. var $ismultientitymanaged = 1; // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  43. var $id;
  44. var $ref;
  45. var $civilite_id;
  46. var $firstname;
  47. var $prenom; // deprecated
  48. var $lastname;
  49. var $nom; // deprecated
  50. var $login;
  51. var $pass;
  52. var $societe;
  53. var $adresse;
  54. var $address;
  55. var $cp;
  56. var $zip;
  57. var $ville;
  58. var $town;
  59. var $state_id;
  60. var $state_code;
  61. var $state;
  62. var $fk_departement; // Id of department
  63. var $departement_code; // Code of department
  64. var $departement; // Label of department
  65. var $pays_id;
  66. var $pays_code;
  67. var $pays;
  68. var $email;
  69. var $phone;
  70. var $phone_perso;
  71. var $phone_mobile;
  72. var $morphy;
  73. var $public;
  74. var $note; // Note
  75. var $statut; // -1:brouillon, 0:resilie, >=1:valide,paye
  76. var $photo;
  77. var $datec;
  78. var $datem;
  79. var $datefin;
  80. var $datevalid;
  81. var $naiss;
  82. var $typeid; // Id type adherent
  83. var $type; // Libelle type adherent
  84. var $need_subscription;
  85. var $user_id;
  86. var $user_login;
  87. var $fk_soc;
  88. // Fields loaded by fetch_subscriptions()
  89. var $first_subscription_date;
  90. var $first_subscription_amount;
  91. var $last_subscription_date;
  92. var $last_subscription_amount;
  93. var $subscriptions=array();
  94. // var $public;
  95. var $array_options;
  96. var $oldcopy; // To contains a clone of this when we need to save old properties of object
  97. /**
  98. * Adherent
  99. * @param DB base de donnees
  100. */
  101. function Adherent($DB)
  102. {
  103. $this->db = $DB ;
  104. $this->statut = -1;
  105. // l'adherent n'est pas public par defaut
  106. $this->public = 0;
  107. // les champs optionnels sont vides
  108. $this->array_options=array();
  109. }
  110. /**
  111. * Fonction envoyant un email a l'adherent avec le texte fourni en parametre.
  112. * @param text contenu du message (not html entities encoded)
  113. * @param subject subject of message
  114. * @param filename_list tableau de fichiers attaches
  115. * @param mimetype_list tableau des types des fichiers attaches
  116. * @param mimefilename_list tableau des noms des fichiers attaches
  117. * @param addr_cc email cc
  118. * @param addr_bcc email bcc
  119. * @param deliveryreceipt demande accuse reception
  120. * @param msgishtml 1=String IS already html, 0=String IS NOT html, -1=Unknown need autodetection
  121. * @return int <0 si ko, >0 si ok
  122. */
  123. 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='')
  124. {
  125. global $conf,$langs;
  126. // Detect if message is HTML
  127. if ($msgishtml == -1)
  128. {
  129. $msgishtml = 0;
  130. if (dol_textishtml($text,1)) $msgishtml = 1;
  131. }
  132. $texttosend=$this->makeSubstitution($text);
  133. $subjecttosend=$this->makeSubstitution($subject);
  134. if ($msgishtml) $texttosend=dol_htmlentitiesbr($texttosend);
  135. // Envoi mail confirmation
  136. $from=$conf->email_from;
  137. if ($conf->global->ADHERENT_MAIL_FROM) $from=$conf->global->ADHERENT_MAIL_FROM;
  138. include_once(DOL_DOCUMENT_ROOT."/lib/CMailFile.class.php");
  139. $mailfile = new CMailFile($subjecttosend, $this->email, $from, $texttosend, $filename_list, $mimetype_list, $mimefilename_list, $addr_cc, $addr_bcc, $deliveryreceipt, $msgishtml);
  140. if ($mailfile->sendfile())
  141. {
  142. return 1;
  143. }
  144. else
  145. {
  146. $this->error=$langs->trans("ErrorFailedToSendMail",$from,$this->email).'. '.$mailfile->error;
  147. return -1;
  148. }
  149. }
  150. /**
  151. * Make substitution
  152. * @param text Text to make substitution to
  153. * @return string Value of input text string with substitutions done
  154. */
  155. function makeSubstitution($text)
  156. {
  157. global $langs;
  158. $birthday = dol_print_date($this->naiss,'day');
  159. $msgishtml = 0;
  160. if (dol_textishtml($text,1)) $msgishtml = 1;
  161. $infos='';
  162. if ($this->civilite_id) $infos.= $langs->transnoentities("UserTitle").": ".$this->getCivilityLabel(1)."\n";
  163. $infos.= $langs->transnoentities("Lastname").": ".$this->nom."\n";
  164. $infos.= $langs->transnoentities("Firstname").": ".$this->prenom."\n";
  165. $infos.= $langs->transnoentities("Company").": ".$this->societe."\n";
  166. $infos.= $langs->transnoentities("Address").": ".$this->adresse."\n";
  167. $infos.= $langs->transnoentities("Zip").": ".$this->cp."\n";
  168. $infos.= $langs->transnoentities("Town").": ".$this->ville."\n";
  169. $infos.= $langs->transnoentities("Country").": ".$this->pays."\n";
  170. $infos.= $langs->transnoentities("EMail").": ".$this->email."\n";
  171. $infos.= $langs->transnoentities("Login").": ".$this->login."\n";
  172. $infos.= $langs->transnoentities("Password").": ".$this->pass."\n";
  173. $infos.= $langs->transnoentities("Birthday").": ".$birthday."\n";
  174. $infos.= $langs->transnoentities("Photo").": ".$this->photo."\n";
  175. $infos.= $langs->transnoentities("Public").": ".yn($this->public);
  176. // Substitutions
  177. $substitutionarray=array(
  178. '%DOL_MAIN_URL_ROOT%'=>DOL_MAIN_URL_ROOT,
  179. '%INFOS%'=>$msgishtml?dol_htmlentitiesbr($infos):$infos,
  180. '%CIVILITE%'=>$this->getCivilityLabel($msgishtml?0:1),
  181. '%PRENOM%'=>$msgishtml?dol_htmlentitiesbr($this->prenom):$this->prenom,
  182. '%NOM%'=>$msgishtml?dol_htmlentitiesbr($this->nom):$this->nom,
  183. '%SOCIETE%'=>$msgishtml?dol_htmlentitiesbr($this->societe):$this->societe,
  184. '%ADRESSE%'=>$msgishtml?dol_htmlentitiesbr($this->adresse):$this->adresse,
  185. '%CP%'=>$msgishtml?dol_htmlentitiesbr($this->cp):$this->cp,
  186. '%VILLE%'=>$msgishtml?dol_htmlentitiesbr($this->ville):$this->ville,
  187. '%PAYS%'=>$msgishtml?dol_htmlentitiesbr($this->pays):$this->pays,
  188. '%EMAIL%'=>$msgishtml?dol_htmlentitiesbr($this->email):$this->email,
  189. '%NAISS%'=>$msgishtml?dol_htmlentitiesbr($birthday):$birthday,
  190. '%PHOTO%'=>$msgishtml?dol_htmlentitiesbr($this->photo):$this->photo,
  191. '%LOGIN%'=>$msgishtml?dol_htmlentitiesbr($this->login):$this->login,
  192. '%PASSWORD%'=>$msgishtml?dol_htmlentitiesbr($this->pass):$this->pass
  193. );
  194. complete_substitutions_array($substitutionarray, $langs);
  195. return make_substitutions($text,$substitutionarray);
  196. }
  197. /**
  198. * Renvoie le libelle traduit de la nature d'un adherent (physique ou morale)
  199. * @param morphy Nature physique ou morale de l'adherent
  200. */
  201. function getmorphylib($morphy='')
  202. {
  203. global $langs;
  204. if (! $morphy) { $morphy=$this->morphy; }
  205. if ($morphy == 'phy') { return $langs->trans("Physical"); }
  206. if ($morphy == 'mor') { return $langs->trans("Moral"); }
  207. return $morphy;
  208. }
  209. /**
  210. * Create a member into database
  211. * @param user Objet user qui demande la creation
  212. * @param notrigger 1 ne declenche pas les triggers, 0 sinon
  213. * @return int <0 if KO, >0 if OK
  214. */
  215. function create($user,$notrigger=0)
  216. {
  217. global $conf,$langs;
  218. $now=dol_now();
  219. // Check parameters
  220. if (! empty($conf->global->ADHERENT_MAIL_REQUIRED) && ! isValidEMail($this->email))
  221. {
  222. $langs->load("errors");
  223. $this->error = $langs->trans("ErrorBadEMail",$this->email);
  224. return -1;
  225. }
  226. if (! $this->datec) $this->datec=$now;
  227. if (empty($conf->global->ADHERENT_LOGIN_NOT_REQUIRED))
  228. {
  229. if (empty($this->login))
  230. {
  231. $this->error = $langs->trans("ErrorWrongValueForParameterX","Login");
  232. return -1;
  233. }
  234. }
  235. $this->db->begin();
  236. // Insert member
  237. $sql = "INSERT INTO ".MAIN_DB_PREFIX."adherent";
  238. $sql.= " (datec,login,fk_user_author,fk_user_mod,fk_user_valid,morphy,fk_adherent_type,entity)";
  239. $sql.= " VALUES (";
  240. $sql.= " '".$this->db->idate($this->datec)."'";
  241. $sql.= ", ".($this->login?"'".$this->db->escape($this->login)."'":"null");
  242. $sql.= ", ".($user->id>0?$user->id:"null"); // Can be null because member can be createb by a guest or a script
  243. $sql.= ", null, null, '".$this->morphy."'";
  244. $sql.= ", '".$this->typeid."'";
  245. $sql.= ", ".$conf->entity;
  246. $sql.= ")";
  247. dol_syslog(get_class($this)."::create sql=".$sql);
  248. $result = $this->db->query($sql);
  249. if ($result)
  250. {
  251. $id = $this->db->last_insert_id(MAIN_DB_PREFIX."adherent");
  252. if ($id > 0)
  253. {
  254. $this->id=$id;
  255. $this->ref=$id;
  256. // Update minor fields
  257. $result=$this->update($user,1,1); // nosync is 1 to avoid update data of user
  258. if ($result < 0)
  259. {
  260. $this->db->rollback();
  261. return -1;
  262. }
  263. // Add link to user
  264. if ($this->user_id)
  265. {
  266. // Add link to user
  267. $sql = "UPDATE ".MAIN_DB_PREFIX."user SET";
  268. $sql.= " fk_member = '".$this->id."'";
  269. $sql.= " WHERE rowid = ".$this->user_id;
  270. dol_syslog(get_class($this)."::create sql=".$sql);
  271. $resql = $this->db->query($sql);
  272. if (! $resql)
  273. {
  274. $this->error='Failed to update user to make link with member';
  275. $this->db->rollback();
  276. return -4;
  277. }
  278. }
  279. $this->use_webcal=($conf->global->PHPWEBCALENDAR_MEMBERSTATUS=='always'?1:0);
  280. if (! $notrigger)
  281. {
  282. // Appel des triggers
  283. include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
  284. $interface=new Interfaces($this->db);
  285. $result=$interface->run_triggers('MEMBER_CREATE',$this,$user,$langs,$conf);
  286. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  287. // Fin appel triggers
  288. }
  289. if (sizeof($this->errors))
  290. {
  291. dol_syslog(get_class($this)."::create ".join(',',$this->errors), LOG_ERR);
  292. $this->db->rollback();
  293. return -3;
  294. }
  295. else
  296. {
  297. $this->db->commit();
  298. return $this->id;
  299. }
  300. }
  301. else
  302. {
  303. $this->error='Failed to get last insert id';
  304. dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
  305. $this->db->rollback();
  306. return -2;
  307. }
  308. }
  309. else
  310. {
  311. $this->error=$this->db->error();
  312. dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
  313. $this->db->rollback();
  314. return -1;
  315. }
  316. }
  317. /**
  318. * Update a member in database (standard information and password)
  319. * @param user User making update
  320. * @param notrigger 1=disable trigger UPDATE (when called by create)
  321. * @param nosyncuser 0=Synchronize linked user (standard info), 1=Do not synchronize linked user
  322. * @param nosyncuserpass 0=Synchronize linked user (password), 1=Do not synchronize linked user
  323. * @return int <0 si KO, >0 si OK
  324. */
  325. function update($user,$notrigger=0,$nosyncuser=0,$nosyncuserpass=0)
  326. {
  327. global $conf, $langs;
  328. $nbrowsaffected=0;
  329. $error=0;
  330. dol_syslog(get_class($this)."::update notrigger=".$notrigger.", nosyncuser=".$nosyncuser.", nosyncuserpass=".$nosyncuserpass.", email=".$this->email);
  331. // Clean parameters
  332. if (! empty($conf->global->MAIN_FIRST_TO_UPPER)) $this->nom=ucwords(trim($this->nom));
  333. if (! empty($conf->global->MAIN_FIRST_TO_UPPER)) $this->prenom=ucwords(trim($this->prenom));
  334. // Check parameters
  335. if (! empty($conf->global->ADHERENT_MAIL_REQUIRED) && ! isValidEMail($this->email))
  336. {
  337. $langs->load("errors");
  338. $this->error = $langs->trans("ErrorBadEMail",$this->email);
  339. return -1;
  340. }
  341. $this->db->begin();
  342. $sql = "UPDATE ".MAIN_DB_PREFIX."adherent SET";
  343. $sql.= " civilite = ".($this->civilite_id?"'".$this->civilite_id."'":"null");
  344. $sql.= ", prenom = ".($this->prenom?"'".$this->db->escape($this->prenom)."'":"null");
  345. $sql.= ", nom=" .($this->nom?"'".$this->db->escape($this->nom)."'":"null");
  346. $sql.= ", login=" .($this->login?"'".$this->db->escape($this->login)."'":"null");
  347. $sql.= ", societe=" .($this->societe?"'".$this->db->escape($this->societe)."'":"null");
  348. $sql.= ", fk_soc=" .($this->fk_soc > 0?"'".$this->fk_soc."'":"null");
  349. $sql.= ", adresse=" .($this->adresse?"'".$this->db->escape($this->adresse)."'":"null");
  350. $sql.= ", cp=" .($this->cp?"'".$this->db->escape($this->cp)."'":"null");
  351. $sql.= ", ville=" .($this->ville?"'".$this->db->escape($this->ville)."'":"null");
  352. $sql.= ", pays=" .($this->pays_id>0?"'".$this->pays_id."'":"null");
  353. $sql.= ", fk_departement=".($this->fk_departement>0?"'".$this->fk_departement."'":"null");
  354. $sql.= ", email=" ."'".$this->email."'";
  355. $sql.= ", phone=" .($this->phone?"'".$this->db->escape($this->phone)."'":"null");
  356. $sql.= ", phone_perso=" .($this->phone_perso?"'".$this->db->escape($this->phone_perso)."'":"null");
  357. $sql.= ", phone_mobile=" .($this->phone_mobile?"'".$this->db->escape($this->phone_mobile)."'":"null");
  358. $sql.= ", note=" .($this->note?"'".$this->db->escape($this->note)."'":"null");
  359. $sql.= ", photo=" .($this->photo?"'".$this->photo."'":"null");
  360. $sql.= ", public=" ."'".$this->public."'";
  361. $sql.= ", statut=" .$this->statut;
  362. $sql.= ", fk_adherent_type=".$this->typeid;
  363. $sql.= ", morphy=" ."'".$this->morphy."'";
  364. $sql.= ", naiss=" .($this->naiss?"'".$this->db->idate($this->naiss)."'":"null");
  365. if ($this->datefin) $sql.= ", datefin='".$this->db->idate($this->datefin)."'"; // Ne doit etre modifie que par effacement cotisation
  366. if ($this->datevalid) $sql.= ", datevalid='".$this->db->idate($this->datevalid)."'"; // Ne doit etre modifie que par validation adherent
  367. $sql.= ", fk_user_mod=".($user->id>0?$user->id:'null'); // Can be null because member can be create by a guest
  368. $sql.= " WHERE rowid = ".$this->id;
  369. dol_syslog(get_class($this)."::update update member sql=".$sql);
  370. $resql = $this->db->query($sql);
  371. if ($resql)
  372. {
  373. $nbrowsaffected+=$this->db->affected_rows($resql);
  374. // Actions on extra fields (by external module or standard code)
  375. include_once(DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php');
  376. $hookmanager=new HookManager($this->db);
  377. $hookmanager->callHooks(array('member_extrafields'));
  378. $parameters=array('socid'=>$socid);
  379. $reshook=$hookmanager->executeHooks('insertExtraFields',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
  380. if (empty($reshook))
  381. {
  382. $result=$this->insertExtraFields();
  383. if ($result < 0)
  384. {
  385. $error++;
  386. }
  387. }
  388. else if ($reshook < 0) $error++;
  389. // Update password
  390. if (! $error && $this->pass)
  391. {
  392. dol_syslog(get_class($this)."::update update password");
  393. if ($this->pass != $this->pass_indatabase && $this->pass != $this->pass_indatabase_crypted)
  394. {
  395. // Si mot de passe saisi et different de celui en base
  396. $result=$this->setPassword($user,$this->pass,0,$notrigger,$nosyncuserpass);
  397. if (! $nbrowsaffected) $nbrowsaffected++;
  398. }
  399. }
  400. // Remove link to user
  401. if (! $error)
  402. {
  403. dol_syslog(get_class($this)."::update update link to user");
  404. $sql = "UPDATE ".MAIN_DB_PREFIX."user SET fk_member = NULL WHERE fk_member = ".$this->id;
  405. dol_syslog(get_class($this)."::update sql=".$sql, LOG_DEBUG);
  406. $resql = $this->db->query($sql);
  407. if (! $resql) { $this->error=$this->db->error(); $this->db->rollback(); return -5; }
  408. // If there is a user linked to this member
  409. if ($this->user_id > 0)
  410. {
  411. $sql = "UPDATE ".MAIN_DB_PREFIX."user SET fk_member = ".$this->id." WHERE rowid = ".$this->user_id;
  412. dol_syslog(get_class($this)."::update sql=".$sql, LOG_DEBUG);
  413. $resql = $this->db->query($sql);
  414. if (! $resql) { $this->error=$this->db->error(); $this->db->rollback(); return -5; }
  415. }
  416. }
  417. if (! $error && $nbrowsaffected) // If something has change in main data
  418. {
  419. if ($this->user_id > 0 && ! $nosyncuser)
  420. {
  421. require_once(DOL_DOCUMENT_ROOT."/user/class/user.class.php");
  422. dol_syslog(get_class($this)."::update update linked user");
  423. // This member is linked with a user, so we also update users informations
  424. // if this is an update.
  425. $luser=new User($this->db);
  426. $result=$luser->fetch($this->user_id);
  427. if ($result >= 0)
  428. {
  429. $luser->civilite_id=$this->civilite_id;
  430. $luser->prenom=$this->prenom;
  431. $luser->nom=$this->nom;
  432. $luser->login=$this->user_login;
  433. $luser->pass=$this->pass;
  434. $luser->societe_id=$this->societe;
  435. $luser->email=$this->email;
  436. $luser->office_phone=$this->phone;
  437. $luser->user_mobile=$this->phone_mobile;
  438. $luser->note=$this->note;
  439. $luser->fk_member=$this->id;
  440. $result=$luser->update($user,0,1,1); // Use nosync to 1 to avoid cyclic updates
  441. if ($result < 0)
  442. {
  443. $this->error=$luser->error;
  444. dol_syslog(get_class($this)."::update ".$this->error,LOG_ERR);
  445. $error++;
  446. }
  447. }
  448. else
  449. {
  450. $this->error=$luser->error;
  451. $error++;
  452. }
  453. }
  454. if (! $error && ! $notrigger)
  455. {
  456. $this->use_webcal=($conf->global->PHPWEBCALENDAR_MEMBERSTATUS=='always'?1:0);
  457. // Appel des triggers
  458. include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
  459. $interface=new Interfaces($this->db);
  460. $result=$interface->run_triggers('MEMBER_MODIFY',$this,$user,$langs,$conf);
  461. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  462. // Fin appel triggers
  463. }
  464. }
  465. if (! $error)
  466. {
  467. $this->db->commit();
  468. return $nbrowsaffected;
  469. }
  470. else
  471. {
  472. $this->db->rollback();
  473. return -1;
  474. }
  475. }
  476. else
  477. {
  478. $this->db->rollback();
  479. $this->error=$this->db->lasterror();
  480. dol_syslog(get_class($this)."::Update ".$this->error,LOG_ERR);
  481. return -2;
  482. }
  483. }
  484. /**
  485. * Update denormalized last subscription date.
  486. * This function is called when we delete a subscription for example.
  487. * @param user Utilisateur qui realise la mise a jour
  488. * @return int <0 if KO, >0 if OK
  489. */
  490. function update_end_date($user)
  491. {
  492. global $conf, $langs;
  493. $error=0;
  494. $this->db->begin();
  495. // Search for last subscription id and end date
  496. $sql = "SELECT rowid, datec as dateop, dateadh as datedeb, datef as datefin";
  497. $sql.= " FROM ".MAIN_DB_PREFIX."cotisation";
  498. $sql.= " WHERE fk_adherent='".$this->id."'";
  499. $sql.= " ORDER by dateadh DESC"; // Sort by start subscription date
  500. dol_syslog(get_class($this)."::update_end_date sql=".$sql);
  501. $resql=$this->db->query($sql);
  502. if ($resql)
  503. {
  504. $obj=$this->db->fetch_object($resql);
  505. $dateop=$this->db->jdate($obj->dateop);
  506. $datedeb=$this->db->jdate($obj->datedeb);
  507. $datefin=$this->db->jdate($obj->datefin);
  508. $sql = "UPDATE ".MAIN_DB_PREFIX."adherent SET";
  509. $sql.= " datefin=".($datefin != '' ? "'".$this->db->idate($datefin)."'" : "null");
  510. $sql.= " WHERE rowid = ".$this->id;
  511. dol_syslog(get_class($this)."::update_end_date sql=".$sql);
  512. $resql=$this->db->query($sql);
  513. if ($resql)
  514. {
  515. $this->last_subscription_date=$dateop;
  516. $this->last_subscription_date_start=$datedeb;
  517. $this->last_subscription_date_end=$datefin;
  518. $this->datefin=$datefin;
  519. $this->db->commit();
  520. return 1;
  521. }
  522. else
  523. {
  524. $this->db->rollback();
  525. return -1;
  526. }
  527. }
  528. else
  529. {
  530. $this->error=$this->db->lasterror();
  531. dol_syslog(get_class($this)."::update_end_date ".$this->error, LOG_ERR);
  532. $this->db->rollback();
  533. return -1;
  534. }
  535. }
  536. /**
  537. * Fonction qui supprime l'adherent et les donnees associees
  538. * @param rowid Id de l'adherent a effacer
  539. * @return int <0 si KO, 0=rien a effacer, >0 si OK
  540. */
  541. function delete($rowid)
  542. {
  543. global $conf, $langs, $user;
  544. $result = 0;
  545. $this->db->begin();
  546. // Suppression options
  547. $sql = "DELETE FROM ".MAIN_DB_PREFIX."adherent_extrafields WHERE fk_object = ".$rowid;
  548. dol_syslog(get_class($this)."::delete sql=".$sql);
  549. $resql=$this->db->query($sql);
  550. if ($resql)
  551. {
  552. $sql = "DELETE FROM ".MAIN_DB_PREFIX."cotisation WHERE fk_adherent = ".$rowid;
  553. dol_syslog(get_class($this)."::delete sql=".$sql);
  554. $resql=$this->db->query( $sql);
  555. if ($resql)
  556. {
  557. $sql = "DELETE FROM ".MAIN_DB_PREFIX."adherent WHERE rowid = ".$rowid;
  558. dol_syslog(get_class($this)."::delete sql=".$sql);
  559. $resql=$this->db->query($sql);
  560. if ($resql)
  561. {
  562. if ($this->db->affected_rows($resql))
  563. {
  564. $this->use_webcal=($conf->global->PHPWEBCALENDAR_MEMBERSTATUS=='always'?1:0);
  565. // Appel des triggers
  566. include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
  567. $interface=new Interfaces($this->db);
  568. $result=$interface->run_triggers('MEMBER_DELETE',$this,$user,$langs,$conf);
  569. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  570. // Fin appel triggers
  571. $this->db->commit();
  572. return 1;
  573. }
  574. else
  575. {
  576. // Rien a effacer
  577. $this->db->rollback();
  578. return 0;
  579. }
  580. }
  581. else
  582. {
  583. $this->error=$this->db->error();
  584. $this->db->rollback();
  585. return -3;
  586. }
  587. }
  588. else
  589. {
  590. $this->error=$this->db->error();
  591. $this->db->rollback();
  592. return -2;
  593. }
  594. }
  595. else
  596. {
  597. $this->error=$this->db->error();
  598. $this->db->rollback();
  599. return -1;
  600. }
  601. return $result;
  602. }
  603. /**
  604. * Change password of a user
  605. * @param user Object user de l'utilisateur qui fait la modification
  606. * @param password Nouveau mot de passe (a generer si non communique)
  607. * @param isencrypted 0 ou 1 si il faut crypter le mot de passe en base (0 par defaut)
  608. * @param notrigger 1=Ne declenche pas les triggers
  609. * @param nosyncuser Do not synchronize linked user
  610. * @return string If OK return clear password, 0 if no change, < 0 if error
  611. */
  612. function setPassword($user, $password='', $isencrypted=0, $notrigger=0, $nosyncuser=0)
  613. {
  614. global $conf, $langs;
  615. $error=0;
  616. dol_syslog(get_class($this)."::setPassword user=".$user->id." password=".preg_replace('/./i','*',$password)." isencrypted=".$isencrypted);
  617. // If new password not provided, we generate one
  618. if (! $password)
  619. {
  620. include_once(DOL_DOCUMENT_ROOT.'/lib/security.lib.php');
  621. $password=getRandomPassword('');
  622. }
  623. // Cryptage mot de passe
  624. if ($isencrypted)
  625. {
  626. // Crypte avec systeme encodage par defaut du PHP
  627. //$sqlpass = crypt($password, makesalt());
  628. $password_indatabase = md5($password);
  629. }
  630. else
  631. {
  632. $password_indatabase = $password;
  633. }
  634. // Mise a jour
  635. $sql = "UPDATE ".MAIN_DB_PREFIX."adherent SET pass = '".$this->db->escape($password_indatabase)."'";
  636. $sql.= " WHERE rowid = ".$this->id;
  637. //dol_syslog("Adherent::Password sql=hidden");
  638. dol_syslog(get_class($this)."::setPassword sql=".$sql);
  639. $result = $this->db->query($sql);
  640. if ($result)
  641. {
  642. $nbaffectedrows=$this->db->affected_rows($result);
  643. if ($nbaffectedrows)
  644. {
  645. $this->pass=$password;
  646. $this->pass_indatabase=$password_indatabase;
  647. if ($this->user_id && ! $nosyncuser)
  648. {
  649. require_once(DOL_DOCUMENT_ROOT."/user/class/user.class.php");
  650. // This member is linked with a user, so we also update users informations
  651. // if this is an update.
  652. $luser=new User($this->db);
  653. $result=$luser->fetch($this->user_id);
  654. if ($result >= 0)
  655. {
  656. $result=$luser->setPassword($user,$this->pass,0,0,1);
  657. if ($result < 0)
  658. {
  659. $this->error=$luser->error;
  660. dol_syslog(get_class($this)."::setPassword ".$this->error,LOG_ERR);
  661. $error++;
  662. }
  663. }
  664. else
  665. {
  666. $this->error=$luser->error;
  667. $error++;
  668. }
  669. }
  670. if (! $error && ! $notrigger)
  671. {
  672. // Appel des triggers
  673. include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
  674. $interface=new Interfaces($this->db);
  675. $result=$interface->run_triggers('MEMBER_NEW_PASSWORD',$this,$user,$langs,$conf);
  676. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  677. // Fin appel triggers
  678. }
  679. return $this->pass;
  680. }
  681. else
  682. {
  683. return 0;
  684. }
  685. }
  686. else
  687. {
  688. dol_print_error($this->db);
  689. return -1;
  690. }
  691. }
  692. /**
  693. * Set link to a user
  694. * @param userid Id of user to link to
  695. * @return int 1=OK, -1=KO
  696. */
  697. function setUserId($userid)
  698. {
  699. global $conf, $langs;
  700. $this->db->begin();
  701. // If user is linked to this member, remove old link to this member
  702. $sql = "UPDATE ".MAIN_DB_PREFIX."user SET fk_member = NULL WHERE fk_member = ".$this->id;
  703. dol_syslog(get_class($this)."::setUserId sql=".$sql, LOG_DEBUG);
  704. $resql = $this->db->query($sql);
  705. if (! $resql) { $this->error=$this->db->error(); $this->db->rollback(); return -1; }
  706. // Set link to user
  707. if ($userid > 0)
  708. {
  709. $sql = "UPDATE ".MAIN_DB_PREFIX."user SET fk_member = ".$this->id;
  710. $sql.= " WHERE rowid = ".$userid;
  711. dol_syslog(get_class($this)."::setUserId sql=".$sql, LOG_DEBUG);
  712. $resql = $this->db->query($sql);
  713. if (! $resql) { $this->error=$this->db->error(); $this->db->rollback(); return -2; }
  714. }
  715. $this->db->commit();
  716. return 1;
  717. }
  718. /**
  719. * Set link to a third party
  720. * @param userid Id of user to link to
  721. * @return int 1=OK, -1=KO
  722. */
  723. function setThirdPartyId($thirdpartyid)
  724. {
  725. global $conf, $langs;
  726. $this->db->begin();
  727. // Update link to third party
  728. if ($thirdpartyid > 0)
  729. {
  730. $sql = "UPDATE ".MAIN_DB_PREFIX."adherent SET fk_soc = null";
  731. $sql.= " WHERE fk_soc = '".$thirdpartyid."'";
  732. $sql.= " AND entity = ".$conf->entity;
  733. dol_syslog(get_class($this)."::setThirdPartyId sql=".$sql);
  734. $resql = $this->db->query($sql);
  735. }
  736. // Update link to third party
  737. $sql = "UPDATE ".MAIN_DB_PREFIX."adherent SET fk_soc = ".($thirdpartyid>0 ? $thirdpartyid : 'null');
  738. $sql.= " WHERE rowid = ".$this->id;
  739. dol_syslog(get_class($this)."::setThirdPartyId sql=".$sql);
  740. $resql = $this->db->query($sql);
  741. if ($resql)
  742. {
  743. $this->db->commit();
  744. return 1;
  745. }
  746. else
  747. {
  748. $this->error=$this->db->error();
  749. dol_syslog(get_class($this)."::setThirdPartyId ".$this->error, LOG_ERR);
  750. $this->db->rollback();
  751. return -1;
  752. }
  753. }
  754. /**
  755. * Method to get member from its login
  756. * @param login login of member
  757. */
  758. function fetch_login($login)
  759. {
  760. global $conf;
  761. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."adherent";
  762. $sql.= " WHERE login='".$login."'";
  763. $sql.= " AND entity = ".$conf->entity;
  764. $resql=$this->db->query( $sql);
  765. if ($resql)
  766. {
  767. if ($this->db->num_rows($resql))
  768. {
  769. $obj = $this->db->fetch_object($resql);
  770. $this->fetch($obj->rowid);
  771. }
  772. }
  773. else
  774. {
  775. dol_print_error($this->db);
  776. }
  777. }
  778. /**
  779. * Load member from database
  780. * @param rowid Id of object to load
  781. * @param ref To load member from its ref
  782. * @param fk_soc To load member from its link to third party
  783. * @return int >0 if OK, 0 if not found, <0 if KO
  784. */
  785. function fetch($rowid,$ref='',$fk_soc='')
  786. {
  787. global $conf, $langs;
  788. $sql = "SELECT d.rowid, d.civilite, d.prenom as firstname, d.nom as name, d.societe, d.fk_soc, d.statut, d.public, d.adresse as address, d.cp as zip, d.ville as town, d.note,";
  789. $sql.= " d.email, d.phone, d.phone_perso, d.phone_mobile, d.login, d.pass,";
  790. $sql.= " d.photo, d.fk_adherent_type, d.morphy,";
  791. $sql.= " d.datec as datec,";
  792. $sql.= " d.tms as datem,";
  793. $sql.= " d.datefin as datefin,";
  794. $sql.= " d.naiss as datenaiss,";
  795. $sql.= " d.datevalid as datev,";
  796. $sql.= " d.pays,";
  797. $sql.= " d.fk_departement,";
  798. $sql.= " p.rowid as pays_id, p.code as pays_code, p.libelle as pays_lib,";
  799. $sql.= " dep.nom as departement, dep.code_departement as departement_code,";
  800. $sql.= " t.libelle as type, t.cotisation as cotisation,";
  801. $sql.= " u.rowid as user_id, u.login as user_login";
  802. $sql.= " FROM ".MAIN_DB_PREFIX."adherent_type as t, ".MAIN_DB_PREFIX."adherent as d";
  803. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_pays as p ON d.pays = p.rowid";
  804. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as dep ON d.fk_departement = dep.rowid";
  805. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."user as u ON d.rowid = u.fk_member";
  806. $sql.= " WHERE d.fk_adherent_type = t.rowid";
  807. $sql.= " AND d.entity = ".$conf->entity;
  808. if ($ref) $sql.= " AND d.rowid='".$ref."'";
  809. elseif ($fk_soc) $sql.= " AND d.fk_soc='".$fk_soc."'";
  810. else $sql.= " AND d.rowid=".$rowid;
  811. dol_syslog(get_class($this)."::fetch sql=".$sql);
  812. $resql=$this->db->query($sql);
  813. if ($resql)
  814. {
  815. if ($this->db->num_rows($resql))
  816. {
  817. $obj = $this->db->fetch_object($resql);
  818. $this->ref = $obj->rowid;
  819. $this->id = $obj->rowid;
  820. $this->civilite_id = $obj->civilite;
  821. $this->prenom = $obj->firstname;
  822. $this->firstname = $obj->firstname;
  823. $this->nom = $obj->name;
  824. $this->lastname = $obj->name;
  825. $this->login = $obj->login;
  826. $this->pass = $obj->pass;
  827. $this->societe = $obj->societe;
  828. $this->fk_soc = $obj->fk_soc;
  829. $this->adresse = $obj->address; // TODO deprecated
  830. $this->address = $obj->address;
  831. $this->cp = $obj->zip; // TODO deprecated
  832. $this->zip = $obj->zip;
  833. $this->ville = $obj->town; // TODO deprecated
  834. $this->town = $obj->town;
  835. $this->state_id = $obj->fk_departement;
  836. $this->state_id = $obj->fk_departement?$obj->departement_code:'';
  837. $this->state_id = $obj->fk_departement?$obj->departement:'';
  838. $this->fk_departement = $obj->fk_departement; // TODO deprecated
  839. $this->departement_code = $obj->fk_departement?$obj->departement_code:''; // TODO deprecated
  840. $this->departement = $obj->fk_departement?$obj->departement:''; // TODO deprecated
  841. $this->country_id = $obj->pays_id;
  842. $this->country_code = $obj->pays_code;
  843. $this->pays_id = $obj->pays_id; // TODO deprecated
  844. $this->pays_code = $obj->pays_code; // TODO deprecated
  845. if ($langs->trans("Country".$obj->pays_code) != "Country".$obj->pays_code) $this->pays = $langs->trans("Country".$obj->pays_code);
  846. elseif ($obj->pays_lib) $this->pays=$obj->pays_lib;
  847. else $this->pays=$obj->pays;
  848. $this->country = $this->pays;
  849. $this->phone = $obj->phone;
  850. $this->phone_perso = $obj->phone_perso;
  851. $this->phone_mobile = $obj->phone_mobile;
  852. $this->email = $obj->email;
  853. $this->photo = $obj->photo;
  854. $this->statut = $obj->statut;
  855. $this->public = $obj->public;
  856. $this->datec = $this->db->jdate($obj->datec);
  857. $this->datem = $this->db->jdate($obj->datem);
  858. $this->datefin = $this->db->jdate($obj->datefin);
  859. $this->datevalid = $this->db->jdate($obj->datev);
  860. $this->naiss = $this->db->jdate($obj->datenaiss);
  861. $this->note = $obj->note;
  862. $this->morphy = $obj->morphy;
  863. $this->typeid = $obj->fk_adherent_type;
  864. $this->type = $obj->type;
  865. $this->need_subscription = ($obj->cotisation=='yes'?1:0);
  866. $this->user_id = $obj->user_id;
  867. $this->user_login = $obj->user_login;
  868. // Load other properties
  869. $result=$this->fetch_subscriptions();
  870. return $result;
  871. }
  872. else
  873. {
  874. return 0;
  875. }
  876. }
  877. else
  878. {
  879. $this->error=$this->db->lasterror();
  880. dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
  881. return -1;
  882. }
  883. }
  884. /**
  885. * Fonction qui recupere pour un adherent les parametres
  886. * first_subscription_date
  887. * first_subscription_amount
  888. * last_subscription_date
  889. * last_subscription_amount
  890. * @return int <0 si KO, >0 si OK
  891. */
  892. function fetch_subscriptions()
  893. {
  894. global $langs;
  895. $sql = "SELECT c.rowid, c.fk_adherent, c.cotisation, c.note, c.fk_bank,";
  896. $sql.= " c.tms as datem,";
  897. $sql.= " c.datec as datec,";
  898. $sql.= " c.dateadh as dateadh,";
  899. $sql.= " c.datef as datef";
  900. $sql.= " FROM ".MAIN_DB_PREFIX."cotisation as c";
  901. $sql.= " WHERE c.fk_adherent = ".$this->id;
  902. $sql.= " ORDER BY c.dateadh";
  903. dol_syslog(get_class($this)."::fetch_subscriptions sql=".$sql);
  904. $resql=$this->db->query($sql);
  905. if ($resql)
  906. {
  907. $this->subscriptions=array();
  908. $i=0;
  909. while ($obj = $this->db->fetch_object($resql))
  910. {
  911. if ($i==0)
  912. {
  913. $this->first_subscription_date=$obj->dateadh;
  914. $this->first_subscription_amount=$obj->cotisation;
  915. }
  916. $this->last_subscription_date=$obj->dateadh;
  917. $this->last_subscription_amount=$obj->cotisation;
  918. $subscription=new Cotisation($this->db);
  919. $subscription->id=$obj->rowid;
  920. $subscription->fk_adherent=$obj->fk_adherent;
  921. $subscription->amount=$obj->cotisation;
  922. $subscription->note=$obj->note;
  923. $subscription->fk_bank=$obj->fk_bank;
  924. $subscription->datem=$this->db->jdate($obj->datem);
  925. $subscription->datec=$this->db->jdate($obj->datec);
  926. $subscription->dateadh=$this->db->jdate($obj->dateadh);
  927. $subscription->datef=$this->db->jdate($obj->datef);
  928. $this->subscriptions[]=$subscription;
  929. $i++;
  930. }
  931. return 1;
  932. }
  933. else
  934. {
  935. $this->error=$this->db->error().' sql='.$sql;
  936. return -1;
  937. }
  938. }
  939. /**
  940. * Fonction qui insere la cotisation dans la base de donnees
  941. * et eventuellement liens dans banques, mailman, etc...
  942. * @param date Date d'effet de la cotisation
  943. * @param montant Montant cotisation (accepte 0 pour les adherents non soumis e cotisation)
  944. * @param account_id Id compte bancaire
  945. * @param operation Type operation (si Id compte bancaire fourni)
  946. * @param label Label operation (si Id compte bancaire fourni)
  947. * @param num_chq Numero cheque (si Id compte bancaire fourni)
  948. * @param emetteur_nom Nom emetteur cheque
  949. * @param emetteur_banque Nom banque emetteur cheque
  950. * @param datesubend Date fin adhesion
  951. * @return int rowid de l'entree ajoutee, <0 si erreur
  952. */
  953. function cotisation($date, $montant, $accountid=0, $operation='', $label='', $num_chq='', $emetteur_nom='', $emetteur_banque='', $datesubend=0)
  954. {
  955. global $conf,$langs,$user;
  956. // Clean parameters
  957. if (! $montant) $montant=0;
  958. $this->db->begin();
  959. if ($datesubend)
  960. {
  961. $datefin=$datesubend;
  962. }
  963. else
  964. {
  965. // If no end date, end date = date + 1 year - 1 day
  966. $datefin = dol_time_plus_duree($date,1,'y');
  967. $datefin = dol_time_plus_duree($datefin,-1,'d');
  968. }
  969. // Create subscription
  970. $cotisation=new Cotisation($this->db);
  971. $cotisation->fk_adherent=$this->id;
  972. $cotisation->dateh=$date; // Date of new subscription
  973. $cotisation->datef=$datefin; // End data of new subscription
  974. $cotisation->amount=$montant;
  975. $cotisation->note=$label;
  976. $rowid=$cotisation->create($user);
  977. if ($rowid > 0)
  978. {
  979. // Update denormalized subscription end date (read database subscription to find values)
  980. // This will also update this->datefin
  981. $result=$this->update_end_date($user);
  982. if ($result > 0)
  983. {
  984. // Change properties of object (used by triggers)
  985. $this->last_subscription_date=$dateop;
  986. $this->last_subscription_amount=$montant;
  987. $this->last_subscription_date_start=$date;
  988. $this->last_subscription_date_end=$datefin;
  989. $this->use_webcal=($conf->global->PHPWEBCALENDAR_MEMBERSTATUS=='always'?1:0);
  990. // Appel des triggers
  991. include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
  992. $interface=new Interfaces($this->db);
  993. $result=$interface->run_triggers('MEMBER_SUBSCRIPTION',$this,$user,$langs,$conf);
  994. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  995. // Fin appel triggers
  996. $this->db->commit();
  997. return $rowid;
  998. }
  999. else
  1000. {
  1001. $this->db->rollback();
  1002. return -2;
  1003. }
  1004. }
  1005. else
  1006. {
  1007. $this->error=$cotisation->error;
  1008. $this->db->rollback();
  1009. return -1;
  1010. }
  1011. }
  1012. /**
  1013. * Function that validate a member
  1014. * @param user user adherent qui valide
  1015. * @return int <0 if KO, 0 if nothing done, >0 if OK
  1016. */
  1017. function validate($user)
  1018. {
  1019. global $langs,$conf;
  1020. if ($this->statut == 1)
  1021. {
  1022. dol_syslog(get_class($this)."::validate statut of member does not allow this", LOG_WARNING);
  1023. return 0;
  1024. }
  1025. $this->db->begin();
  1026. $sql = "UPDATE ".MAIN_DB_PREFIX."adherent SET";
  1027. $sql.= " statut = 1";
  1028. $sql.= ", datevalid = ".$this->db->idate(mktime());
  1029. $sql.= ", fk_user_valid=".$user->id;
  1030. $sql.= " WHERE rowid = ".$this->id;
  1031. dol_syslog(get_class($this)."::validate sql=".$sql);
  1032. $result = $this->db->query($sql);
  1033. if ($result)
  1034. {
  1035. $this->statut=1;
  1036. $this->use_webcal=($conf->global->PHPWEBCALENDAR_MEMBERSTATUS=='always'?1:0);
  1037. // Appel des triggers
  1038. include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
  1039. $interface=new Interfaces($this->db);
  1040. $result=$interface->run_triggers('MEMBER_VALIDATE',$this,$user,$langs,$conf);
  1041. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  1042. // Fin appel triggers
  1043. $this->db->commit();
  1044. return 1;
  1045. }
  1046. else
  1047. {
  1048. $this->error=$this->db->error();
  1049. $this->db->rollback();
  1050. return -1;
  1051. }
  1052. }
  1053. /**
  1054. * Fonction qui resilie un adherent
  1055. * @param user user adherent qui resilie
  1056. * @return int <0 si ko, >0 si ok
  1057. */
  1058. function resiliate($user)
  1059. {
  1060. global $langs,$conf;
  1061. if ($this->statut == 0)
  1062. {
  1063. dol_syslog(get_class($this)."::resiliate statut of member does not allow this", LOG_WARNING);
  1064. return 0;
  1065. }
  1066. $this->db->begin();
  1067. $sql = "UPDATE ".MAIN_DB_PREFIX."adherent SET";
  1068. $sql.= " statut = 0";
  1069. $sql.= ", fk_user_valid=".$user->id;
  1070. $sql.= " WHERE rowid = ".$this->id;
  1071. $result = $this->db->query($sql);
  1072. if ($result)
  1073. {
  1074. $this->statut=0;
  1075. $this->use_webcal=($conf->global->PHPWEBCALENDAR_MEMBERSTATUS=='always'?1:0);
  1076. // Appel des triggers
  1077. include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
  1078. $interface=new Interfaces($this->db);
  1079. $result=$interface->run_triggers('MEMBER_RESILIATE',$this,$user,$langs,$conf);
  1080. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  1081. // Fin appel triggers
  1082. $this->db->commit();
  1083. return 1;
  1084. }
  1085. else
  1086. {
  1087. $this->error=$this->db->error();
  1088. $this->db->rollback();
  1089. return -1;
  1090. }
  1091. }
  1092. /**
  1093. * Fonction qui ajoute l'adherent au abonnements automatiques
  1094. * mailing-list, spip, etc...
  1095. * @return int <0 if KO, >0 if OK
  1096. */
  1097. function add_to_abo()
  1098. {
  1099. global $conf;
  1100. $err=0;
  1101. // mailman
  1102. if (! empty($conf->global->ADHERENT_USE_MAILMAN))
  1103. {
  1104. $result=$this->add_to_mailman();
  1105. if ($result < 0)
  1106. {
  1107. $err+=1;
  1108. }
  1109. }
  1110. // spip
  1111. if ($conf->global->ADHERENT_USE_SPIP && $conf->global->ADHERENT_USE_SPIP_AUTO)
  1112. {
  1113. $result=$this->add_to_spip();
  1114. if ($result < 0)
  1115. {
  1116. $err+=1;
  1117. }
  1118. }
  1119. if ($err)
  1120. {
  1121. // error
  1122. return -$err;
  1123. }
  1124. else
  1125. {
  1126. return 1;
  1127. }
  1128. }
  1129. /**
  1130. * Fonction qui supprime l'adherent des abonnements automatiques
  1131. * mailing-list, spip, etc...
  1132. * @return int <0 if KO, >0 if OK
  1133. */
  1134. function del_to_abo()
  1135. {
  1136. global $conf;
  1137. $err=0;
  1138. // mailman
  1139. if (! empty($conf->global->ADHERENT_USE_MAILMAN))
  1140. {
  1141. $result=$this->del_to_mailman();
  1142. if ($result < 0)
  1143. {
  1144. $err+=1;
  1145. }
  1146. }
  1147. if ($conf->global->ADHERENT_USE_SPIP && $conf->global->ADHERENT_USE_SPIP_AUTO)
  1148. {
  1149. $result=$this->del_to_spip();
  1150. if ($result < 0)
  1151. {
  1152. $err+=1;
  1153. }
  1154. }
  1155. if ($err)
  1156. {
  1157. // error
  1158. return -$err;
  1159. }
  1160. else
  1161. {
  1162. return 1;
  1163. }
  1164. }
  1165. /**
  1166. * Fonction qui donne les droits redacteurs dans spip
  1167. * @return int =0 si KO, >0 si OK
  1168. */
  1169. function add_to_spip()
  1170. {
  1171. require_once(DOL_DOCUMENT_ROOT.'/lib/security.lib.php');
  1172. dol_syslog(get_class($this)."::add_to_spip");
  1173. if (defined("ADHERENT_USE_SPIP") && ADHERENT_USE_SPIP ==1 &&
  1174. defined('ADHERENT_SPIP_SERVEUR') && ADHERENT_SPIP_SERVEUR != '' &&
  1175. defined('ADHERENT_SPIP_USER') && ADHERENT_SPIP_USER != '' &&
  1176. defined('ADHERENT_SPIP_PASS') && ADHERENT_SPIP_PASS != '' &&
  1177. defined('ADHERENT_SPIP_DB') && ADHERENT_SPIP_DB != ''
  1178. ){
  1179. $mdpass=md5($this->pass);
  1180. $htpass=crypt($this->pass,makesalt());
  1181. $query = "INSERT INTO spip_auteurs (nom, email, login, pass, htpass, alea_futur, statut) VALUES(\"".$this->prenom." ".$this->nom."\",\"".$this->email."\",\"".$this->login."\",\"$mdpass\",\"$htpass\",FLOOR(32000*RAND()),\"1comite\")";
  1182. // $mydb=new Db('mysql',ADHERENT_SPIP_SERVEUR,ADHERENT_SPIP_USER,ADHERENT_SPIP_PASS,ADHERENT_SPIP_DB);
  1183. $mydb=new DoliDb('mysql',ADHERENT_SPIP_SERVEUR,ADHERENT_SPIP_USER,ADHERENT_SPIP_PASS,ADHERENT_SPIP_DB,ADHERENT_SPIP_PORT);
  1184. $result = $mydb->query($query);
  1185. if ($result)
  1186. {
  1187. $mydb->close();
  1188. return 1;
  1189. }
  1190. else
  1191. {
  1192. $this->error=$mydb->error();
  1193. return 0;
  1194. }
  1195. }
  1196. }
  1197. /**
  1198. * Fonction qui enleve les droits redacteurs dans spip
  1199. */
  1200. function del_to_spip()
  1201. {
  1202. if (defined("ADHERENT_USE_SPIP") && ADHERENT_USE_SPIP ==1 &&
  1203. defined('ADHERENT_SPIP_SERVEUR') && ADHERENT_SPIP_SERVEUR != '' &&
  1204. defined('ADHERENT_SPIP_USER') && ADHERENT_SPIP_USER != '' &&
  1205. defined('ADHERENT_SPIP_PASS') && ADHERENT_SPIP_PASS != '' &&
  1206. defined('ADHERENT_SPIP_DB') && ADHERENT_SPIP_DB != ''
  1207. ){
  1208. $query = "DELETE FROM spip_auteurs WHERE login='".$this->login."'";
  1209. $mydb=new DoliDb('mysql',ADHERENT_SPIP_SERVEUR,ADHERENT_SPIP_USER,ADHERENT_SPIP_PASS,ADHERENT_SPIP_DB,ADHERENT_SPIP_PORT);
  1210. $result = $mydb->query($query);
  1211. if ($result)
  1212. {
  1213. $mydb->close();
  1214. return 1;
  1215. }
  1216. else
  1217. {
  1218. $this->error=$mydb->error();
  1219. return 0;
  1220. }
  1221. }
  1222. }
  1223. /**
  1224. * Fonction qui dit si cet utilisateur est un redacteur existant dans spip
  1225. * @return int 1=existe, 0=n'existe pas, -1=erreur
  1226. */
  1227. function is_in_spip()
  1228. {
  1229. if (defined("ADHERENT_USE_SPIP") && ADHERENT_USE_SPIP ==1 &&
  1230. defined('ADHERENT_SPIP_SERVEUR') && ADHERENT_SPIP_SERVEUR != '' &&
  1231. defined('ADHERENT_SPIP_USER') && ADHERENT_SPIP_USER != '' &&
  1232. defined('ADHERENT_SPIP_PASS') && ADHERENT_SPIP_PASS != '' &&
  1233. defined('ADHERENT_SPIP_DB') && ADHERENT_SPIP_DB != '')
  1234. {
  1235. $query = "SELECT login FROM spip_auteurs WHERE login='".$this->login."'";
  1236. $mydb=new DoliDb('mysql',ADHERENT_SPIP_SERVEUR,ADHERENT_SPIP_USER,ADHERENT_SPIP_PASS,ADHERENT_SPIP_DB,ADHERENT_SPIP_PORT);
  1237. if ($mydb->ok) {
  1238. $result = $mydb->query($query);
  1239. if ($result)
  1240. {
  1241. if ($mydb->num_rows($result))
  1242. {
  1243. # nous avons au moins une reponse
  1244. $mydb->close($result);
  1245. return 1;
  1246. }
  1247. else
  1248. {
  1249. # nous n'avons pas de reponse => n'existe pas
  1250. $mydb->close($result);
  1251. return 0;
  1252. }
  1253. }
  1254. else
  1255. {
  1256. # error
  1257. $this->error=$mydb->error();
  1258. return -1;
  1259. }
  1260. } else {
  1261. $this->error="Echec de connexion avec les identifiants ".ADHERENT_SPIP_SERVEUR." ".ADHERENT_SPIP_USER." ".ADHERENT_SPIP_PASS." ".ADHERENT_SPIP_DB;
  1262. return -1;
  1263. }
  1264. }
  1265. }
  1266. /**
  1267. * Subscribe an email to all mailing-lists
  1268. * @param listes To force mailing-list (string separated with ,)
  1269. * @return int <=0 if KO, >0 if OK
  1270. */
  1271. function add_to_mailman($listes='')
  1272. {
  1273. global $conf,$langs,$user;
  1274. dol_syslog(get_class($this)."::add_to_mailman");
  1275. if (! function_exists("curl_init"))
  1276. {
  1277. $langs->load("errors");
  1278. $this->error=$langs->trans("ErrorFunctionNotAvailableInPHP","curl_init");
  1279. return -1;
  1280. }
  1281. if (! empty($conf->global->ADHERENT_MAILMAN_URL))
  1282. {
  1283. if ($listes == '' && ! empty($conf->global->ADHERENT_MAILMAN_LISTS))
  1284. {
  1285. $lists=explode(',',$conf->global->ADHERENT_MAILMAN_LISTS);
  1286. }
  1287. else
  1288. {
  1289. $lists=explode(',',$listes);
  1290. }
  1291. foreach ($lists as $list)
  1292. {
  1293. // on remplace dans l'url le nom de la liste ainsi
  1294. // que l'email et le mot de passe
  1295. $patterns = array (
  1296. '/%LISTE%/',
  1297. '/%EMAIL%/',
  1298. '/%PASSWORD%/',
  1299. '/%MAILMAN_ADMINPW%/'
  1300. );
  1301. $replace = array (
  1302. $list,
  1303. $this->email,
  1304. $this->pass,
  1305. $conf->global->ADHERENT_MAILMAN_ADMINPW
  1306. );
  1307. $curl_url = preg_replace ($patterns, $replace, $conf->global->ADHERENT_MAILMAN_URL);
  1308. dol_syslog("Call URL to subscribe : ".$curl_url);
  1309. $ch = curl_init();
  1310. curl_setopt($ch, CURLOPT_URL,"$curl_url");
  1311. //curl_setopt($ch, CURLOPT_URL,"http://www.j1b.org/");
  1312. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  1313. curl_setopt($ch, CURLOPT_FAILONERROR, 1);
  1314. @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  1315. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  1316. //curl_setopt($ch, CURLOPT_POST, 0);
  1317. //curl_setopt($ch, CURLOPT_POSTFIELDS, "a=3&b=5");
  1318. //--- Start buffering
  1319. //ob_start();
  1320. $result=curl_exec ($ch);
  1321. dol_syslog($result);
  1322. //--- End buffering and clean output
  1323. //ob_end_clean();
  1324. if (curl_error($ch) > 0)
  1325. {
  1326. // error
  1327. return -2;
  1328. }
  1329. curl_close ($ch);
  1330. }
  1331. return 1;
  1332. }
  1333. else
  1334. {
  1335. $this->error="ADHERENT_MAILMAN_URL not defined";
  1336. return -1;
  1337. }
  1338. }
  1339. /**
  1340. * Unsubscribe an email from all mailing-lists
  1341. * Used when a user is resiliated
  1342. * @param listes To force mailing-list (string separated with ,)
  1343. * @return int <=0 if KO, >0 if OK
  1344. */
  1345. function del_to_mailman($listes='')
  1346. {
  1347. global $conf,$langs,$user;
  1348. if (! empty($conf->global->ADHERENT_MAILMAN_UNSUB_URL))
  1349. {
  1350. if ($listes=='' && ! empty($conf->global->ADHERENT_MAILMAN_LISTS))
  1351. {
  1352. $lists=explode(',',$conf->global->ADHERENT_MAILMAN_LISTS);
  1353. }
  1354. else
  1355. {
  1356. $lists=explode(',',$listes);
  1357. }
  1358. foreach ($lists as $list)
  1359. {
  1360. // on remplace dans l'url le nom de la liste ainsi
  1361. // que l'email et le mot de passe
  1362. $patterns = array (
  1363. '/%LISTE%/',
  1364. '/%EMAIL%/',
  1365. '/%PASSWORD%/',
  1366. '/%MAILMAN_ADMINPW%/'
  1367. );
  1368. $replace = array (
  1369. trim($list),
  1370. $this->email,
  1371. $this->pass,
  1372. $conf->global->ADHERENT_MAILMAN_ADMINPW
  1373. );
  1374. $curl_url = preg_replace ($patterns, $replace, $conf->global->ADHERENT_MAILMAN_UNSUB_URL);
  1375. dol_syslog("Call URL to unsubscribe : ".$curl_url);
  1376. $ch = curl_init();
  1377. curl_setopt($ch, CURLOPT_URL,"$curl_url");
  1378. //curl_setopt($ch, CURLOPT_URL,"http://www.j1b.org/");
  1379. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  1380. curl_setopt($ch, CURLOPT_FAILONERROR, 1);
  1381. @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  1382. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  1383. //curl_setopt($ch, CURLOPT_POST, 0);
  1384. //curl_setopt($ch, CURLOPT_POSTFIELDS, "a=3&b=5");
  1385. //--- Start buffering
  1386. //ob_start();
  1387. $result=curl_exec ($ch);
  1388. dol_syslog($result);
  1389. //--- End buffering and clean output
  1390. //ob_end_clean();
  1391. $rescode=curl_error($ch);
  1392. if ($rescode > 0)
  1393. {
  1394. dol_syslog("Error using CURL : ".$rescode, LOG_ERR);
  1395. // error
  1396. return -2;
  1397. }
  1398. curl_close ($ch);
  1399. }
  1400. return 1;
  1401. }
  1402. else
  1403. {
  1404. $this->error="ADHERENT_MAILMAN_UNSUB_URL not defined";
  1405. return -1;
  1406. }
  1407. }
  1408. /**
  1409. * Return full name (civility+' '+name+' '+lastname)
  1410. * @param langs Language object for translation of civility
  1411. * @param option 0=No option, 1=Add civility
  1412. * @param nameorder -1=Auto, 0=Lastname+Firstname, 1=Firstname+Lastname
  1413. * @return string String with full name
  1414. */
  1415. function getFullName($langs,$option=0,$nameorder=-1)
  1416. {
  1417. global $conf;
  1418. $ret='';
  1419. if ($option && $this->civilite_id)
  1420. {
  1421. if ($langs->transnoentitiesnoconv("Civility".$this->civilite_id)!="Civility".$this->civilite_id) $ret.=$langs->transnoentitiesnoconv("Civility".$this->civilite_id).' ';
  1422. else $ret.=$this->civilite_id.' ';
  1423. }
  1424. // If order not defined, we use the setup
  1425. if ($nameorder < 0) $nameorder=(! $conf->global->MAIN_FIRSTNAME_NAME_POSITION);
  1426. if ($nameorder)
  1427. {
  1428. if ($this->prenom) $ret.=$this->prenom;
  1429. if ($this->prenom && $this->nom) $ret.=' ';
  1430. if ($this->nom) $ret.=$this->nom;
  1431. }
  1432. else
  1433. {
  1434. if ($this->nom) $ret.=$this->nom;
  1435. if ($this->prenom && $this->nom) $ret.=' ';
  1436. if ($this->prenom) $ret.=$this->prenom;
  1437. }
  1438. return trim($ret);
  1439. }
  1440. /**
  1441. * Return label of a civility of a contact
  1442. * @param nohtmlentities 0=Encode with htmlentities for HTML output, 1=No htmlentities for memory translation
  1443. * @return string Name translated of civility
  1444. */
  1445. function getCivilityLabel($nohtmlentities=0)
  1446. {
  1447. global $langs;
  1448. $langs->load("dict");
  1449. $code=$this->civilite_id;
  1450. if ($nohtmlentities) return $langs->transnoentities("Civility".$code)!="Civility".$code ? $langs->transnoentities("Civility".$code) : $code;
  1451. else return $langs->trans("Civility".$code)!="Civility".$code ? $langs->trans("Civility".$code) : $code;
  1452. }
  1453. /**
  1454. * Renvoie nom clicable (avec eventuellement le picto)
  1455. * @param withpicto 0=Pas de picto, 1=Inclut le picto dans le lien, 2=Picto seul
  1456. * @param maxlen length max libelle
  1457. * @param option Page lien
  1458. * @return string Chaine avec URL
  1459. */
  1460. function getNomUrl($withpicto=0,$maxlen=0,$option='card')
  1461. {
  1462. global $langs;
  1463. $result='';
  1464. if ($option == 'card')
  1465. {
  1466. $lien = '<a href="'.DOL_URL_ROOT.'/adherents/fiche.php?rowid='.$this->id.'">';
  1467. $lienfin='</a>';
  1468. }
  1469. if ($option == 'subscription')
  1470. {
  1471. $lien = '<a href="'.DOL_URL_ROOT.'/adherents/card_subscriptions.php?rowid='.$this->id.'">';
  1472. $lienfin='</a>';
  1473. }
  1474. $picto='user';
  1475. $label=$langs->trans("ShowMember");
  1476. if ($withpicto) $result.=($lien.img_object($label,$picto).$lienfin);
  1477. if ($withpicto && $withpicto != 2) $result.=' ';
  1478. $result.=$lien.($maxlen?dol_trunc($this->ref,$maxlen):$this->ref).$lienfin;
  1479. return $result;
  1480. }
  1481. /**
  1482. * Return full address of member
  1483. * @param withcountry 1=Add country into address string
  1484. * @param sep Separator to use to build string
  1485. * @return string Full address string
  1486. */
  1487. function getFullAddress($withcountry=0,$sep="\n")
  1488. {
  1489. $ret='';
  1490. if (in_array($this->country,array('us')))
  1491. {
  1492. $ret.=($this->address?$this->address.$sep:'');
  1493. $ret.=trim($this->zip.' '.$this->town);
  1494. if ($withcountry) $ret.=($this->country?$sep.$this->country:'');
  1495. }
  1496. else
  1497. {
  1498. $ret.=($this->address?$this->address.$sep:'');
  1499. $ret.=trim($this->zip.' '.$this->town);
  1500. if ($withcountry) $ret.=($this->country?$sep.$this->country:'');
  1501. }
  1502. return trim($ret);
  1503. }
  1504. /**
  1505. * Retourne le libelle du statut d'un adherent (brouillon, valide, resilie)
  1506. * @param mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  1507. * @return string Libelle
  1508. */
  1509. function getLibStatut($mode=0)
  1510. {
  1511. return $this->LibStatut($this->statut,$this->need_subscription,$this->datefin,$mode);
  1512. }
  1513. /**
  1514. * Renvoi le libelle d'un statut donne
  1515. * @param statut Id statut
  1516. * @param need_subscription 1 si type adherent avec cotisation, 0 sinon
  1517. * @param date_end_subscription Date fin adhesion
  1518. * @param mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  1519. * @return string Libelle
  1520. */
  1521. function LibStatut($statut,$need_subscription,$date_end_subscription,$mode=0)
  1522. {
  1523. global $langs;
  1524. $langs->load("members");
  1525. if ($mode == 0)
  1526. {
  1527. if ($statut == -1) return $langs->trans("MemberStatusDraft");
  1528. if ($statut >= 1)
  1529. {
  1530. if (! $date_end_subscription) return $langs->trans("MemberStatusActive");
  1531. elseif ($date_end_subscription < time()) return $langs->trans("MemberStatusActiveLate");
  1532. else return $langs->trans("MemberStatusPaid");
  1533. }
  1534. if ($statut == 0) return $langs->trans("MemberStatusResiliated");
  1535. }
  1536. if ($mode == 1)
  1537. {
  1538. if ($statut == -1) return $langs->trans("MemberStatusDraftShort");
  1539. if ($statut >= 1)
  1540. {
  1541. if (! $date_end_subscription) return $langs->trans("MemberStatusActiveShort");
  1542. elseif ($date_end_subscription < time()) return $langs->trans("MemberStatusActiveLateShort");
  1543. else return $langs->trans("MemberStatusPaidShort");
  1544. }
  1545. if ($statut == 0) return $langs->trans("MemberStatusResiliatedShort");
  1546. }
  1547. if ($mode == 2)
  1548. {
  1549. if ($statut == -1) return img_picto($langs->trans('MemberStatusDraft'),'statut0').' '.$langs->trans("MemberStatusDraftShort");
  1550. if ($statut >= 1)
  1551. {
  1552. if (! $date_end_subscription) return img_picto($langs->trans('MemberStatusActive'),'statut1').' '.$langs->trans("MemberStatusActiveShort");
  1553. elseif ($date_end_subscription < time()) return img_picto($langs->trans('MemberStatusActiveLate'),'statut3').' '.$langs->trans("MemberStatusActiveLateShort");
  1554. else return img_picto($langs->trans('MemberStatusPaid'),'statut4').' '.$langs->trans("MemberStatusPaidShort");
  1555. }
  1556. if ($statut == 0) return img_picto($langs->trans('MemberStatusResiliated'),'statut5').' '.$langs->trans("MemberStatusResiliatedShort");
  1557. }
  1558. if ($mode == 3)
  1559. {
  1560. if ($statut == -1) return img_picto($langs->trans('MemberStatusDraft'),'statut0');
  1561. if ($statut >= 1)
  1562. {
  1563. if (! $date_end_subscription) return img_picto($langs->trans('MemberStatusActive'),'statut1');
  1564. elseif ($date_end_subscription < time()) return img_picto($langs->trans('MemberStatusActiveLate'),'statut3');
  1565. else return img_picto($langs->trans('MemberStatusPaid'),'statut4');
  1566. }
  1567. if ($statut == 0) return img_picto($langs->trans('MemberStatusResiliated'),'statut5');
  1568. }
  1569. if ($mode == 4)
  1570. {
  1571. if ($statut == -1) return img_picto($langs->trans('MemberStatusDraft'),'statut0').' '.$langs->trans("MemberStatusDraft");
  1572. if ($statut >= 1)
  1573. {
  1574. if (! $date_end_subscription) return img_picto($langs->trans('MemberStatusActive'),'statut1').' '.$langs->trans("MemberStatusActive");
  1575. elseif ($date_end_subscription < time()) return img_picto($langs->trans('MemberStatusActiveLate'),'statut3').' '.$langs->trans("MemberStatusActiveLate");
  1576. else return img_picto($langs->trans('MemberStatusPaid'),'statut4').' '.$langs->trans("MemberStatusPaid");
  1577. }
  1578. if ($statut == 0) return img_picto($langs->trans('MemberStatusResiliated'),'statut5').' '.$langs->trans("MemberStatusResiliated");
  1579. }
  1580. if ($mode == 5)
  1581. {
  1582. if ($statut == -1) return $langs->trans("MemberStatusDraft").' '.img_picto($langs->trans('MemberStatusDraft'),'statut0');
  1583. if ($statut >= 1)
  1584. {
  1585. if (! $date_end_subscription) return $langs->trans("MemberStatusActive").' '.img_picto($langs->trans('MemberStatusActive'),'statut1');
  1586. elseif ($date_end_subscription < time()) return $langs->trans("MemberStatusActiveLate").' '.img_picto($langs->trans('MemberStatusActiveLate'),'statut3');
  1587. else return $langs->trans("MemberStatusPaid").' '.img_picto($langs->trans('MemberStatusPaid'),'statut4');
  1588. }
  1589. if ($statut == 0) return $langs->trans("MemberStatusResiliated").' '.img_picto($langs->trans('MemberStatusResiliated'),'statut5');
  1590. }
  1591. }
  1592. /**
  1593. * Charge indicateurs this->nb de tableau de bord
  1594. * @return int <0 si ko, >0 si ok
  1595. */
  1596. function load_state_board()
  1597. {
  1598. global $conf;
  1599. $this->nb=array();
  1600. $sql = "SELECT count(a.rowid) as nb";
  1601. $sql.= " FROM ".MAIN_DB_PREFIX."adherent as a";
  1602. $sql.= " WHERE a.statut > 0";
  1603. $sql.= " AND a.entity = ".$conf->entity;
  1604. $resql=$this->db->query($sql);
  1605. if ($resql)
  1606. {
  1607. while ($obj=$this->db->fetch_object($resql))
  1608. {
  1609. $this->nb["members"]=$obj->nb;
  1610. }
  1611. return 1;
  1612. }
  1613. else
  1614. {
  1615. dol_print_error($this->db);
  1616. $this->error=$this->db->error();
  1617. return -1;
  1618. }
  1619. }
  1620. /**
  1621. * Load indicators for dashboard (this->nbtodo and this->nbtodolate)
  1622. * @param user Objet user
  1623. * @return int <0 if KO, >0 if OK
  1624. */
  1625. function load_board($user)
  1626. {
  1627. global $conf;
  1628. $now=dol_now();
  1629. if ($user->societe_id) return -1; // protection pour eviter appel par utilisateur externe
  1630. $this->nbtodo=$this->nbtodolate=0;
  1631. $sql = "SELECT a.rowid, a.datefin";
  1632. $sql.= " FROM ".MAIN_DB_PREFIX."adherent as a";
  1633. $sql.= " WHERE a.statut = 1";
  1634. $sql.= " AND a.entity = ".$conf->entity;
  1635. $sql.= " AND (a.datefin IS NULL or a.datefin < '".$this->db->idate($now)."')";
  1636. $resql=$this->db->query($sql);
  1637. if ($resql)
  1638. {
  1639. while ($obj=$this->db->fetch_object($resql))
  1640. {
  1641. $this->nbtodo++;
  1642. if ($this->db->jdate($obj->datefin) < ($now - $conf->adherent->cotisation->warning_delay)) $this->nbtodolate++;
  1643. }
  1644. return 1;
  1645. }
  1646. else
  1647. {
  1648. dol_print_error($this->db);
  1649. $this->error=$this->db->error();
  1650. return -1;
  1651. }
  1652. }
  1653. /**
  1654. * Initialise le membre avec valeurs fictives aleatoire
  1655. */
  1656. function initAsSpecimen()
  1657. {
  1658. global $user,$langs;
  1659. // Initialise parametres
  1660. $this->id=0;
  1661. $this->specimen=1;
  1662. $this->civilite_id = 0;
  1663. $this->nom = 'DOLIBARR';
  1664. $this->prenom = 'SPECIMEN';
  1665. $this->login='dolibspec';
  1666. $this->pass='dolibspec';
  1667. $this->societe = 'Societe ABC';
  1668. $this->adresse = '61 jump street';
  1669. $this->address = '61 jump street';
  1670. $this->cp = '75000';
  1671. $this->zip = '75000';
  1672. $this->ville = 'Paris';
  1673. $this->town = 'Paris';
  1674. $this->pays_id = 1;
  1675. $this->pays_code = 'FR';
  1676. $this->pays = 'France';
  1677. $this->morphy = 1;
  1678. $this->email = 'specimen@specimen.com';
  1679. $this->phone = '0999999999';
  1680. $this->phone_perso = '0999999998';
  1681. $this->phone_mobile = '0999999997';
  1682. $this->note='No comment';
  1683. $this->naiss=time();
  1684. $this->photo='';
  1685. $this->public=1;
  1686. $this->statut=0;
  1687. $this->datefin=time();
  1688. $this->datevalid=time();
  1689. $this->typeid=1; // Id type adherent
  1690. $this->type='Type adherent'; // Libelle type adherent
  1691. $this->need_subscription=0;
  1692. $this->first_subscription_date=time();
  1693. $this->first_subscription_amount=10;
  1694. $this->last_subscription_date=time();
  1695. $this->last_subscription_amount=10;
  1696. }
  1697. /**
  1698. * Retourne chaine DN complete dans l'annuaire LDAP pour l'objet
  1699. * @param info Info string loaded by _load_ldap_info
  1700. * @param mode 0=Return full DN (uid=qqq,ou=xxx,dc=aaa,dc=bbb)
  1701. * 1=Return DN without key inside (ou=xxx,dc=aaa,dc=bbb)
  1702. * 2=Return key only (uid=qqq)
  1703. * @return string DN
  1704. */
  1705. function _load_ldap_dn($info,$mode=0)
  1706. {
  1707. global $conf;
  1708. $dn='';
  1709. if ($mode==0) $dn=$conf->global->LDAP_KEY_MEMBERS."=".$info[$conf->global->LDAP_KEY_MEMBERS].",".$conf->global->LDAP_MEMBER_DN;
  1710. if ($mode==1) $dn=$conf->global->LDAP_MEMBER_DN;
  1711. if ($mode==2) $dn=$conf->global->LDAP_KEY_MEMBERS."=".$info[$conf->global->LDAP_KEY_MEMBERS];
  1712. return $dn;
  1713. }
  1714. /**
  1715. * Initialise tableau info (tableau des attributs LDAP)
  1716. * @return array Tableau info des attributs
  1717. */
  1718. function _load_ldap_info()
  1719. {
  1720. global $conf,$langs;
  1721. $info=array();
  1722. // Object classes
  1723. $info["objectclass"]=explode(',',$conf->global->LDAP_MEMBER_OBJECT_CLASS);
  1724. $this->fullname=$this->getFullName($langs);
  1725. // Member
  1726. if ($this->fullname && $conf->global->LDAP_MEMBER_FIELD_FULLNAME) $info[$conf->global->LDAP_MEMBER_FIELD_FULLNAME] = $this->fullname;
  1727. if ($this->nom && $conf->global->LDAP_MEMBER_FIELD_NAME) $info[$conf->global->LDAP_MEMBER_FIELD_NAME] = $this->nom;
  1728. if ($this->prenom && $conf->global->LDAP_MEMBER_FIELD_FIRSTNAME) $info[$conf->global->LDAP_MEMBER_FIELD_FIRSTNAME] = $this->prenom;
  1729. if ($this->login && $conf->global->LDAP_MEMBER_FIELD_LOGIN) $info[$conf->global->LDAP_MEMBER_FIELD_LOGIN] = $this->login;
  1730. if ($this->pass && $conf->global->LDAP_MEMBER_FIELD_PASSWORD) $info[$conf->global->LDAP_MEMBER_FIELD_PASSWORD] = $this->pass; // this->pass = mot de passe non crypte
  1731. if ($this->poste && $conf->global->LDAP_MEMBER_FIELD_TITLE) $info[$conf->global->LDAP_MEMBER_FIELD_TITLE] = $this->poste;
  1732. if ($this->adresse && $conf->global->LDAP_MEMBER_FIELD_ADDRESS) $info[$conf->global->LDAP_MEMBER_FIELD_ADDRESS] = $this->adresse;
  1733. if ($this->cp && $conf->global->LDAP_MEMBER_FIELD_ZIP) $info[$conf->global->LDAP_MEMBER_FIELD_ZIP] = $this->cp;
  1734. if ($this->ville && $conf->global->LDAP_MEMBER_FIELD_TOWN) $info[$conf->global->LDAP_MEMBER_FIELD_TOWN] = $this->ville;
  1735. if ($this->pays_code && $conf->global->LDAP_MEMBER_FIELD_COUNTRY) $info[$conf->global->LDAP_MEMBER_FIELD_COUNTRY] = $this->pays_code;
  1736. if ($this->email && $conf->global->LDAP_MEMBER_FIELD_MAIL) $info[$conf->global->LDAP_MEMBER_FIELD_MAIL] = $this->email;
  1737. if ($this->phone && $conf->global->LDAP_MEMBER_FIELD_PHONE) $info[$conf->global->LDAP_MEMBER_FIELD_PHONE] = $this->phone;
  1738. if ($this->phone_perso && $conf->global->LDAP_MEMBER_FIELD_PHONE_PERSO) $info[$conf->global->LDAP_MEMBER_FIELD_PHONE_PERSO] = $this->phone_perso;
  1739. if ($this->phone_mobile && $conf->global->LDAP_MEMBER_FIELD_MOBILE) $info[$conf->global->LDAP_MEMBER_FIELD_MOBILE] = $this->phone_mobile;
  1740. if ($this->fax && $conf->global->LDAP_MEMBER_FIELD_FAX) $info[$conf->global->LDAP_MEMBER_FIELD_FAX] = $this->fax;
  1741. if ($this->note && $conf->global->LDAP_MEMBER_FIELD_DESCRIPTION) $info[$conf->global->LDAP_MEMBER_FIELD_DESCRIPTION] = $this->note;
  1742. if ($this->naiss && $conf->global->LDAP_MEMBER_FIELD_BIRTHDATE) $info[$conf->global->LDAP_MEMBER_FIELD_BIRTHDATE] = dol_print_date($this->naiss,'dayhourldap');
  1743. if (isset($this->statut) && $conf->global->LDAP_FIELD_MEMBER_STATUS) $info[$conf->global->LDAP_FIELD_MEMBER_STATUS] = $this->statut;
  1744. if ($this->datefin && $conf->global->LDAP_FIELD_MEMBER_END_LASTSUBSCRIPTION) $info[$conf->global->LDAP_FIELD_MEMBER_END_LASTSUBSCRIPTION] = dol_print_date($this->datefin,'dayhourldap');
  1745. // Subscriptions
  1746. if ($this->first_subscription_date && $conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE) $info[$conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE] = dol_print_date($this->first_subscription_date,'dayhourldap');
  1747. if (isset($this->first_subscription_amount) && $conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_AMOUNT) $info[$conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_AMOUNT] = $this->first_subscription_amount;
  1748. if ($this->last_subscription_date && $conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_DATE) $info[$conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_DATE] = dol_print_date($this->last_subscription_date,'dayhourldap');
  1749. if (isset($this->last_subscription_amount) && $conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_AMOUNT) $info[$conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_AMOUNT] = $this->last_subscription_amount;
  1750. return $info;
  1751. }
  1752. /**
  1753. * Charge les informations d'ordre info dans l'objet adherent
  1754. * @param id Id du membre a charger
  1755. */
  1756. function info($id)
  1757. {
  1758. $sql = 'SELECT a.rowid, a.datec as datec,';
  1759. $sql.= ' a.datevalid as datev,';
  1760. $sql.= ' a.tms as datem,';
  1761. $sql.= ' a.fk_user_author, a.fk_user_valid, a.fk_user_mod';
  1762. $sql.= ' FROM '.MAIN_DB_PREFIX.'adherent as a';
  1763. $sql.= ' WHERE a.rowid = '.$id;
  1764. dol_syslog(get_class($this)."::info sql=".$sql, LOG_DEBUG);
  1765. $result=$this->db->query($sql);
  1766. if ($result)
  1767. {
  1768. if ($this->db->num_rows($result))
  1769. {
  1770. $obj = $this->db->fetch_object($result);
  1771. $this->id = $obj->rowid;
  1772. if ($obj->fk_user_author)
  1773. {
  1774. $cuser = new User($this->db);
  1775. $cuser->fetch($obj->fk_user_author);
  1776. $this->user_creation = $cuser;
  1777. }
  1778. if ($obj->fk_user_valid)
  1779. {
  1780. $vuser = new User($this->db);
  1781. $vuser->fetch($obj->fk_user_valid);
  1782. $this->user_validation = $vuser;
  1783. }
  1784. if ($obj->fk_user_mod)
  1785. {
  1786. $muser = new User($this->db);
  1787. $muser->fetch($obj->fk_user_mod);
  1788. $this->user_modification = $muser;
  1789. }
  1790. $this->date_creation = $this->db->jdate($obj->datec);
  1791. $this->date_validation = $this->db->jdate($obj->datev);
  1792. $this->date_modification = $this->db->jdate($obj->datem);
  1793. }
  1794. $this->db->free($result);
  1795. }
  1796. else
  1797. {
  1798. dol_print_error($this->db);
  1799. }
  1800. }
  1801. }
  1802. ?>