adherent.class.php 75 KB

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