adherent.class.php 101 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062
  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-2017 Regis Houssin <regis.houssin@inodbox.com>
  8. * Copyright (C) 2014-2018 Alexandre Spangaro <aspangaro@open-dsi.fr>
  9. * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
  10. * Copyright (C) 2015-2020 Frédéric France <frederic.france@netlogic.fr>
  11. * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
  12. * Copyright (C) 2016 Juanjo Menent <jmenent@2byte.es>
  13. * Copyright (C) 2018-2019 Thibault FOUCART <support@ptibogxiv.net>
  14. * Copyright (C) 2019 Nicolas ZABOURI <info@inovea-conseil.com>
  15. * Copyright (C) 2020 Josep Lluís Amador <joseplluis@lliuretic.cat>
  16. * Copyright (C) 2021 Waël Almoman <info@almoman.com>
  17. *
  18. * This program is free software; you can redistribute it and/or modify
  19. * it under the terms of the GNU General Public License as published by
  20. * the Free Software Foundation; either version 3 of the License, or
  21. * (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU General Public License
  29. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  30. */
  31. /**
  32. * \file htdocs/adherents/class/adherent.class.php
  33. * \ingroup member
  34. * \brief File of class to manage members of a foundation
  35. */
  36. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  37. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  38. require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
  39. /**
  40. * Class to manage members of a foundation
  41. */
  42. class Adherent extends CommonObject
  43. {
  44. /**
  45. * @var string ID to identify managed object
  46. */
  47. public $element = 'member';
  48. /**
  49. * @var string Name of table without prefix where object is stored
  50. */
  51. public $table_element = 'adherent';
  52. /**
  53. * 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  54. * @var int
  55. */
  56. public $ismultientitymanaged = 1;
  57. /**
  58. * @var string picto
  59. */
  60. public $picto = 'member';
  61. public $mesgs;
  62. /**
  63. * @var string login of member
  64. */
  65. public $login;
  66. /**
  67. * @var string Clear password in memory
  68. */
  69. public $pass;
  70. /**
  71. * @var string Clear password in database (defined if DATABASE_PWD_ENCRYPTED=0)
  72. */
  73. public $pass_indatabase;
  74. /**
  75. * @var string Encrypted password in database (always defined)
  76. */
  77. public $pass_indatabase_crypted;
  78. /**
  79. * @var string company name
  80. * @deprecated
  81. */
  82. public $societe;
  83. /**
  84. * @var string company name
  85. */
  86. public $company;
  87. /**
  88. * @var int Thirdparty ID
  89. */
  90. public $fk_soc;
  91. public $socid;
  92. /**
  93. * @var string Address
  94. */
  95. public $address;
  96. /**
  97. * @var string zipcode
  98. */
  99. public $zip;
  100. /**
  101. * @var string town
  102. */
  103. public $town;
  104. /**
  105. * @var int Id of state
  106. */
  107. public $state_id;
  108. /**
  109. * @var string Code of state
  110. */
  111. public $state_code;
  112. /**
  113. * @var string Label of state
  114. */
  115. public $state;
  116. /**
  117. * @var string email
  118. */
  119. public $email;
  120. /**
  121. * @var array array of socialnetworks
  122. */
  123. public $socialnetworks;
  124. /**
  125. * @var string skype account
  126. * @deprecated
  127. */
  128. public $skype;
  129. /**
  130. * @var string twitter account
  131. * @deprecated
  132. */
  133. public $twitter;
  134. /**
  135. * @var string facebook account
  136. * @deprecated
  137. */
  138. public $facebook;
  139. /**
  140. * @var string linkedin account
  141. * @deprecated
  142. */
  143. public $linkedin;
  144. /**
  145. * @var string Phone number
  146. */
  147. public $phone;
  148. /**
  149. * @var string Private Phone number
  150. */
  151. public $phone_perso;
  152. /**
  153. * @var string Professional Phone number
  154. */
  155. public $phone_pro;
  156. /**
  157. * @var string Mobile phone number
  158. */
  159. public $phone_mobile;
  160. /**
  161. * @var string Fax number
  162. */
  163. public $fax;
  164. /**
  165. * @var string Function
  166. */
  167. public $poste;
  168. /**
  169. * @var string mor or phy
  170. */
  171. public $morphy;
  172. public $public;
  173. // -2:exclu, -1:brouillon, 0:resilie, >=1:valide,paye
  174. // def in common object
  175. //public $status;
  176. /**
  177. * @var string photo of member
  178. */
  179. public $photo;
  180. /**
  181. * Date creation record (datec)
  182. *
  183. * @var integer
  184. */
  185. public $datec;
  186. /**
  187. * Date modification record (tms)
  188. *
  189. * @var integer
  190. */
  191. public $datem;
  192. public $datevalid;
  193. /**
  194. * @var string gender
  195. */
  196. public $gender;
  197. public $birth;
  198. /**
  199. * @var int id type member
  200. */
  201. public $typeid;
  202. /**
  203. * @var string label type member
  204. */
  205. public $type;
  206. public $need_subscription;
  207. public $user_id;
  208. public $user_login;
  209. public $datefin;
  210. // From member table
  211. // Fields loaded by fetch_subscriptions()
  212. public $first_subscription_date;
  213. public $first_subscription_amount;
  214. public $last_subscription_date;
  215. public $last_subscription_date_start;
  216. public $last_subscription_date_end;
  217. public $last_subscription_amount;
  218. public $subscriptions = array();
  219. /**
  220. * @var Adherent To contains a clone of this when we need to save old properties of object
  221. */
  222. public $oldcopy;
  223. /**
  224. * @var int Entity
  225. */
  226. public $entity;
  227. /**
  228. * @var array fields
  229. */
  230. public $fields = array(
  231. 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 10),
  232. 'ref' => array('type' => 'varchar(30)', 'label' => 'Ref', 'default' => 1, 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 12, 'index' => 1),
  233. 'entity' => array('type' => 'integer', 'label' => 'Entity', 'default' => 1, 'enabled' => 1, 'visible' => -2, 'notnull' => 1, 'position' => 15, 'index' => 1),
  234. 'ref_ext' => array('type' => 'varchar(128)', 'label' => 'Ref ext', 'enabled' => 1, 'visible' => 0, 'position' => 20),
  235. 'civility' => array('type' => 'varchar(6)', 'label' => 'Civility', 'enabled' => 1, 'visible' => -1, 'position' => 25),
  236. 'lastname' => array('type' => 'varchar(50)', 'label' => 'Lastname', 'enabled' => 1, 'visible' => -1, 'position' => 30),
  237. 'firstname' => array('type' => 'varchar(50)', 'label' => 'Firstname', 'enabled' => 1, 'visible' => -1, 'position' => 35),
  238. 'login' => array('type' => 'varchar(50)', 'label' => 'Login', 'enabled' => 1, 'visible' => -1, 'position' => 40),
  239. 'gender' => array('type' => 'varchar(10)', 'label' => 'Gender', 'enabled' => 1, 'visible' => -1, 'position' => 250),
  240. 'pass' => array('type' => 'varchar(50)', 'label' => 'Pass', 'enabled' => 1, 'visible' => -1, 'position' => 45),
  241. 'pass_crypted' => array('type' => 'varchar(128)', 'label' => 'Pass crypted', 'enabled' => 1, 'visible' => -1, 'position' => 50),
  242. 'fk_adherent_type' => array('type' => 'integer', 'label' => 'Fk adherent type', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 55),
  243. 'morphy' => array('type' => 'varchar(3)', 'label' => 'MorPhy', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 60),
  244. 'societe' => array('type' => 'varchar(128)', 'label' => 'Societe', 'enabled' => 1, 'visible' => -1, 'position' => 65),
  245. 'fk_soc' => array('type' => 'integer:Societe:societe/class/societe.class.php', 'label' => 'ThirdParty', 'enabled' => 1, 'visible' => -1, 'position' => 70),
  246. 'address' => array('type' => 'text', 'label' => 'Address', 'enabled' => 1, 'visible' => -1, 'position' => 75),
  247. 'zip' => array('type' => 'varchar(10)', 'label' => 'Zip', 'enabled' => 1, 'visible' => -1, 'position' => 80),
  248. 'town' => array('type' => 'varchar(50)', 'label' => 'Town', 'enabled' => 1, 'visible' => -1, 'position' => 85),
  249. 'state_id' => array('type' => 'integer', 'label' => 'State id', 'enabled' => 1, 'visible' => -1, 'position' => 90),
  250. 'country' => array('type' => 'integer:Ccountry:core/class/ccountry.class.php', 'label' => 'Country', 'enabled' => 1, 'visible' => -1, 'position' => 95),
  251. 'email' => array('type' => 'varchar(255)', 'label' => 'Email', 'enabled' => 1, 'visible' => -1, 'position' => 100),
  252. 'socialnetworks' => array('type' => 'text', 'label' => 'Socialnetworks', 'enabled' => 1, 'visible' => -1, 'position' => 105),
  253. 'phone' => array('type' => 'varchar(30)', 'label' => 'Phone', 'enabled' => 1, 'visible' => -1, 'position' => 115),
  254. 'phone_perso' => array('type' => 'varchar(30)', 'label' => 'Phone perso', 'enabled' => 1, 'visible' => -1, 'position' => 120),
  255. 'phone_mobile' => array('type' => 'varchar(30)', 'label' => 'Phone mobile', 'enabled' => 1, 'visible' => -1, 'position' => 125),
  256. 'birth' => array('type' => 'date', 'label' => 'DateOfBirth', 'enabled' => 1, 'visible' => -1, 'position' => 130),
  257. 'photo' => array('type' => 'varchar(255)', 'label' => 'Photo', 'enabled' => 1, 'visible' => -1, 'position' => 135),
  258. 'public' => array('type' => 'smallint(6)', 'label' => 'Public', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 145),
  259. 'datefin' => array('type' => 'datetime', 'label' => 'DateEnd', 'enabled' => 1, 'visible' => -1, 'position' => 150),
  260. 'note_private' => array('type' => 'text', 'label' => 'NotePublic', 'enabled' => 1, 'visible' => 0, 'position' => 155),
  261. 'note_public' => array('type' => 'text', 'label' => 'NotePrivate', 'enabled' => 1, 'visible' => 0, 'position' => 160),
  262. 'datevalid' => array('type' => 'datetime', 'label' => 'DateValidation', 'enabled' => 1, 'visible' => -1, 'position' => 165),
  263. 'datec' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => -1, 'position' => 170),
  264. 'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 175),
  265. 'fk_user_author' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'Fk user author', 'enabled' => 1, 'visible' => -1, 'position' => 180),
  266. 'fk_user_mod' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'Fk user mod', 'enabled' => 1, 'visible' => -1, 'position' => 185),
  267. 'fk_user_valid' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserValidation', 'enabled' => 1, 'visible' => -1, 'position' => 190),
  268. 'canvas' => array('type' => 'varchar(32)', 'label' => 'Canvas', 'enabled' => 1, 'visible' => -1, 'position' => 195),
  269. 'statut' => array('type' => 'smallint(6)', 'label' => 'Statut', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 500,
  270. 'arrayofkeyval' => array(0 => 'Draft', 1 => 'Validated', -1 => 'MemberStatusResiliatedShort', -2 => 'MemberStatusExcludedShort')),
  271. 'model_pdf' => array('type' => 'varchar(255)', 'label' => 'Model pdf', 'enabled' => 1, 'visible' => 0, 'position' => 800),
  272. 'import_key' => array('type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'visible' => -2, 'position' => 805)
  273. );
  274. /**
  275. * Constructor
  276. *
  277. * @param DoliDB $db Database handler
  278. */
  279. public function __construct($db)
  280. {
  281. $this->db = $db;
  282. $this->statut = -1;
  283. // l'adherent n'est pas public par defaut
  284. $this->public = 0;
  285. // les champs optionnels sont vides
  286. $this->array_options = array();
  287. }
  288. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  289. /**
  290. * Function sending an email to the current member with the text supplied in parameter.
  291. *
  292. * @param string $text Content of message (not html entities encoded)
  293. * @param string $subject Subject of message
  294. * @param array $filename_list Array of attached files
  295. * @param array $mimetype_list Array of mime types of attached files
  296. * @param array $mimefilename_list Array of public names of attached files
  297. * @param string $addr_cc Email cc
  298. * @param string $addr_bcc Email bcc
  299. * @param int $deliveryreceipt Ask a delivery receipt
  300. * @param int $msgishtml 1=String IS already html, 0=String IS NOT html, -1=Unknown need autodetection
  301. * @param string $errors_to erros to
  302. * @param string $moreinheader Add more html headers
  303. * @return int <0 if KO, >0 if OK
  304. */
  305. public 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 = '', $moreinheader = '')
  306. {
  307. // phpcs:enable
  308. global $conf, $langs;
  309. // Detect if message is HTML
  310. if ($msgishtml == -1) {
  311. $msgishtml = 0;
  312. if (dol_textishtml($text, 0)) {
  313. $msgishtml = 1;
  314. }
  315. }
  316. dol_syslog('send_an_email msgishtml='.$msgishtml);
  317. $texttosend = $this->makeSubstitution($text);
  318. $subjecttosend = $this->makeSubstitution($subject);
  319. if ($msgishtml) {
  320. $texttosend = dol_htmlentitiesbr($texttosend);
  321. }
  322. // Envoi mail confirmation
  323. $from = $conf->email_from;
  324. if (!empty($conf->global->ADHERENT_MAIL_FROM)) {
  325. $from = $conf->global->ADHERENT_MAIL_FROM;
  326. }
  327. $trackid = 'mem'.$this->id;
  328. // Send email (substitutionarray must be done just before this)
  329. include_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
  330. $mailfile = new CMailFile($subjecttosend, $this->email, $from, $texttosend, $filename_list, $mimetype_list, $mimefilename_list, $addr_cc, $addr_bcc, $deliveryreceipt, $msgishtml, '', '', $trackid, $moreinheader);
  331. if ($mailfile->sendfile()) {
  332. return 1;
  333. } else {
  334. $this->error = $langs->trans("ErrorFailedToSendMail", $from, $this->email).'. '.$mailfile->error;
  335. return -1;
  336. }
  337. }
  338. /**
  339. * Make substitution of tags into text with value of current object.
  340. *
  341. * @param string $text Text to make substitution to
  342. * @return string Value of input text string with substitutions done
  343. */
  344. public function makeSubstitution($text)
  345. {
  346. global $conf, $langs;
  347. $birthday = dol_print_date($this->birth, 'day');
  348. $msgishtml = 0;
  349. if (dol_textishtml($text, 1)) {
  350. $msgishtml = 1;
  351. }
  352. $infos = '';
  353. if ($this->civility_id) {
  354. $infos .= $langs->transnoentities("UserTitle").": ".$this->getCivilityLabel()."\n";
  355. }
  356. $infos .= $langs->transnoentities("id").": ".$this->id."\n";
  357. $infos .= $langs->transnoentities("ref").": ".$this->ref."\n";
  358. $infos .= $langs->transnoentities("Lastname").": ".$this->lastname."\n";
  359. $infos .= $langs->transnoentities("Firstname").": ".$this->firstname."\n";
  360. $infos .= $langs->transnoentities("Company").": ".$this->company."\n";
  361. $infos .= $langs->transnoentities("Address").": ".$this->address."\n";
  362. $infos .= $langs->transnoentities("Zip").": ".$this->zip."\n";
  363. $infos .= $langs->transnoentities("Town").": ".$this->town."\n";
  364. $infos .= $langs->transnoentities("Country").": ".$this->country."\n";
  365. $infos .= $langs->transnoentities("EMail").": ".$this->email."\n";
  366. $infos .= $langs->transnoentities("PhonePro").": ".$this->phone."\n";
  367. $infos .= $langs->transnoentities("PhonePerso").": ".$this->phone_perso."\n";
  368. $infos .= $langs->transnoentities("PhoneMobile").": ".$this->phone_mobile."\n";
  369. if (empty($conf->global->ADHERENT_LOGIN_NOT_REQUIRED)) {
  370. $infos .= $langs->transnoentities("Login").": ".$this->login."\n";
  371. $infos .= $langs->transnoentities("Password").": ".$this->pass."\n";
  372. }
  373. $infos .= $langs->transnoentities("Birthday").": ".$birthday."\n";
  374. $infos .= $langs->transnoentities("Photo").": ".$this->photo."\n";
  375. $infos .= $langs->transnoentities("Public").": ".yn($this->public);
  376. // Substitutions
  377. $substitutionarray = array(
  378. '__ID__' => $this->id,
  379. '__REF__' => $this->ref,
  380. '__MEMBER_ID__' => $this->id,
  381. '__CIVILITY__' => $this->getCivilityLabel(),
  382. '__FIRSTNAME__' => $msgishtml ? dol_htmlentitiesbr($this->firstname) : ($this->firstname ? $this->firstname : ''),
  383. '__LASTNAME__' => $msgishtml ? dol_htmlentitiesbr($this->lastname) : ($this->lastname ? $this->lastname : ''),
  384. '__FULLNAME__' => $msgishtml ? dol_htmlentitiesbr($this->getFullName($langs)) : $this->getFullName($langs),
  385. '__COMPANY__' => $msgishtml ? dol_htmlentitiesbr($this->company) : ($this->company ? $this->company : ''),
  386. '__ADDRESS__' => $msgishtml ? dol_htmlentitiesbr($this->address) : ($this->address ? $this->address : ''),
  387. '__ZIP__' => $msgishtml ? dol_htmlentitiesbr($this->zip) : ($this->zip ? $this->zip : ''),
  388. '__TOWN__' => $msgishtml ? dol_htmlentitiesbr($this->town) : ($this->town ? $this->town : ''),
  389. '__COUNTRY__' => $msgishtml ? dol_htmlentitiesbr($this->country) : ($this->country ? $this->country : ''),
  390. '__EMAIL__' => $msgishtml ? dol_htmlentitiesbr($this->email) : ($this->email ? $this->email : ''),
  391. '__BIRTH__' => $msgishtml ? dol_htmlentitiesbr($birthday) : ($birthday ? $birthday : ''),
  392. '__PHOTO__' => $msgishtml ? dol_htmlentitiesbr($this->photo) : ($this->photo ? $this->photo : ''),
  393. '__LOGIN__' => $msgishtml ? dol_htmlentitiesbr($this->login) : ($this->login ? $this->login : ''),
  394. '__PASSWORD__' => $msgishtml ? dol_htmlentitiesbr($this->pass) : ($this->pass ? $this->pass : ''),
  395. '__PHONE__' => $msgishtml ? dol_htmlentitiesbr($this->phone) : ($this->phone ? $this->phone : ''),
  396. '__PHONEPRO__' => $msgishtml ? dol_htmlentitiesbr($this->phone_perso) : ($this->phone_perso ? $this->phone_perso : ''),
  397. '__PHONEMOBILE__' => $msgishtml ? dol_htmlentitiesbr($this->phone_mobile) : ($this->phone_mobile ? $this->phone_mobile : ''),
  398. '__TYPE__' => $msgishtml ? dol_htmlentitiesbr($this->type) : ($this->type ? $this->type : '')
  399. );
  400. complete_substitutions_array($substitutionarray, $langs, $this);
  401. return make_substitutions($text, $substitutionarray, $langs);
  402. }
  403. /**
  404. * Return translated label by the nature of a adherent (physical or moral)
  405. *
  406. * @param string $morphy Nature of the adherent (physical or moral)
  407. * @return string Label
  408. */
  409. public function getmorphylib($morphy = '')
  410. {
  411. global $langs;
  412. if (!$morphy) {
  413. $morphy = $this->morphy;
  414. }
  415. if ($morphy == 'phy') {
  416. return $langs->trans("Physical");
  417. }
  418. if ($morphy == 'mor') {
  419. return $langs->trans("Moral");
  420. }
  421. return $morphy;
  422. }
  423. /**
  424. * Create a member into database
  425. *
  426. * @param User $user Objet user qui demande la creation
  427. * @param int $notrigger 1 ne declenche pas les triggers, 0 sinon
  428. * @return int <0 if KO, >0 if OK
  429. */
  430. public function create($user, $notrigger = 0)
  431. {
  432. global $conf, $langs;
  433. $error = 0;
  434. $now = dol_now();
  435. // Clean parameters
  436. $this->import_key = trim($this->import_key);
  437. // Check parameters
  438. if (!empty($conf->global->ADHERENT_MAIL_REQUIRED) && !isValidEMail($this->email)) {
  439. $langs->load("errors");
  440. $this->error = $langs->trans("ErrorBadEMail", $this->email);
  441. return -1;
  442. }
  443. if (!$this->datec) {
  444. $this->datec = $now;
  445. }
  446. if (empty($conf->global->ADHERENT_LOGIN_NOT_REQUIRED)) {
  447. if (empty($this->login)) {
  448. $this->error = $langs->trans("ErrorWrongValueForParameterX", "Login");
  449. return -1;
  450. }
  451. }
  452. $this->db->begin();
  453. // Insert member
  454. $sql = "INSERT INTO ".MAIN_DB_PREFIX."adherent";
  455. $sql .= " (ref, datec,login,fk_user_author,fk_user_mod,fk_user_valid,morphy,fk_adherent_type,entity,import_key)";
  456. $sql .= " VALUES (";
  457. $sql .= " '(PROV)'";
  458. $sql .= ", '".$this->db->idate($this->datec)."'";
  459. $sql .= ", ".($this->login ? "'".$this->db->escape($this->login)."'" : "null");
  460. $sql .= ", ".($user->id > 0 ? $user->id : "null"); // Can be null because member can be created by a guest or a script
  461. $sql .= ", null, null, '".$this->db->escape($this->morphy)."'";
  462. $sql .= ", ".$this->typeid;
  463. $sql .= ", ".$conf->entity;
  464. $sql .= ", ".(!empty($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
  465. $sql .= ")";
  466. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  467. $result = $this->db->query($sql);
  468. if ($result) {
  469. $id = $this->db->last_insert_id(MAIN_DB_PREFIX."adherent");
  470. if ($id > 0) {
  471. $this->id = $id;
  472. $this->ref = (string) $id;
  473. // Update minor fields
  474. $result = $this->update($user, 1, 1, 0, 0, 'add'); // nosync is 1 to avoid update data of user
  475. if ($result < 0) {
  476. $this->db->rollback();
  477. return -1;
  478. }
  479. // Add link to user
  480. if ($this->user_id) {
  481. // Add link to user
  482. $sql = "UPDATE ".MAIN_DB_PREFIX."user SET";
  483. $sql .= " fk_member = ".$this->id;
  484. $sql .= " WHERE rowid = ".$this->user_id;
  485. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  486. $resql = $this->db->query($sql);
  487. if (!$resql) {
  488. $this->error = 'Failed to update user to make link with member';
  489. $this->db->rollback();
  490. return -4;
  491. }
  492. }
  493. if (!$notrigger) {
  494. // Call trigger
  495. $result = $this->call_trigger('MEMBER_CREATE', $user);
  496. if ($result < 0) {
  497. $error++;
  498. }
  499. // End call triggers
  500. }
  501. if (count($this->errors)) {
  502. dol_syslog(get_class($this)."::create ".implode(',', $this->errors), LOG_ERR);
  503. $this->db->rollback();
  504. return -3;
  505. } else {
  506. $this->db->commit();
  507. return $this->id;
  508. }
  509. } else {
  510. $this->error = 'Failed to get last insert id';
  511. dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
  512. $this->db->rollback();
  513. return -2;
  514. }
  515. } else {
  516. $this->error = $this->db->error();
  517. $this->db->rollback();
  518. return -1;
  519. }
  520. }
  521. /**
  522. * Update a member in database (standard information and password)
  523. *
  524. * @param User $user User making update
  525. * @param int $notrigger 1=disable trigger UPDATE (when called by create)
  526. * @param int $nosyncuser 0=Synchronize linked user (standard info), 1=Do not synchronize linked user
  527. * @param int $nosyncuserpass 0=Synchronize linked user (password), 1=Do not synchronize linked user
  528. * @param int $nosyncthirdparty 0=Synchronize linked thirdparty (standard info), 1=Do not synchronize linked thirdparty
  529. * @param string $action Current action for hookmanager
  530. * @return int <0 if KO, >0 if OK
  531. */
  532. public function update($user, $notrigger = 0, $nosyncuser = 0, $nosyncuserpass = 0, $nosyncthirdparty = 0, $action = 'update')
  533. {
  534. global $conf, $langs, $hookmanager;
  535. $nbrowsaffected = 0;
  536. $error = 0;
  537. dol_syslog(get_class($this)."::update notrigger=".$notrigger.", nosyncuser=".$nosyncuser.", nosyncuserpass=".$nosyncuserpass." nosyncthirdparty=".$nosyncthirdparty.", email=".
  538. $this->email);
  539. // Clean parameters
  540. $this->lastname = trim($this->lastname) ? trim($this->lastname) : trim($this->lastname);
  541. $this->firstname = trim($this->firstname) ? trim($this->firstname) : trim($this->firstname);
  542. $this->gender = trim($this->gender);
  543. $this->address = ($this->address ? $this->address : $this->address);
  544. $this->zip = ($this->zip ? $this->zip : $this->zip);
  545. $this->town = ($this->town ? $this->town : $this->town);
  546. $this->country_id = ($this->country_id > 0 ? $this->country_id : $this->country_id);
  547. $this->state_id = ($this->state_id > 0 ? $this->state_id : $this->state_id);
  548. $this->setUpperOrLowerCase();
  549. $this->note_public = ($this->note_public ? $this->note_public : $this->note_public);
  550. $this->note_private = ($this->note_private ? $this->note_private : $this->note_private);
  551. // Check parameters
  552. if (!empty($conf->global->ADHERENT_MAIL_REQUIRED) && !isValidEMail($this->email)) {
  553. $langs->load("errors");
  554. $this->error = $langs->trans("ErrorBadEMail", $this->email);
  555. return -1;
  556. }
  557. $this->db->begin();
  558. $sql = "UPDATE ".MAIN_DB_PREFIX."adherent SET";
  559. $sql .= " ref = '".$this->db->escape($this->ref)."'";
  560. $sql .= ", civility = ".($this->civility_id ? "'".$this->db->escape($this->civility_id)."'" : "null");
  561. $sql .= ", firstname = ".($this->firstname ? "'".$this->db->escape($this->firstname)."'" : "null");
  562. $sql .= ", lastname = ".($this->lastname ? "'".$this->db->escape($this->lastname)."'" : "null");
  563. $sql .= ", gender = ".($this->gender != -1 ? "'".$this->db->escape($this->gender)."'" : "null"); // 'man' or 'woman'
  564. $sql .= ", login = ".($this->login ? "'".$this->db->escape($this->login)."'" : "null");
  565. $sql .= ", societe = ".($this->company ? "'".$this->db->escape($this->company)."'" : ($this->societe ? "'".$this->db->escape($this->societe)."'" : "null"));
  566. $sql .= ", fk_soc = ".($this->socid > 0 ? $this->db->escape($this->socid) : "null");
  567. $sql .= ", address = ".($this->address ? "'".$this->db->escape($this->address)."'" : "null");
  568. $sql .= ", zip = ".($this->zip ? "'".$this->db->escape($this->zip)."'" : "null");
  569. $sql .= ", town = ".($this->town ? "'".$this->db->escape($this->town)."'" : "null");
  570. $sql .= ", country = ".($this->country_id > 0 ? $this->db->escape($this->country_id) : "null");
  571. $sql .= ", state_id = ".($this->state_id > 0 ? $this->db->escape($this->state_id) : "null");
  572. $sql .= ", email = '".$this->db->escape($this->email)."'";
  573. $sql .= ", socialnetworks = '".$this->db->escape(json_encode($this->socialnetworks))."'";
  574. $sql .= ", phone = ".($this->phone ? "'".$this->db->escape($this->phone)."'" : "null");
  575. $sql .= ", phone_perso = ".($this->phone_perso ? "'".$this->db->escape($this->phone_perso)."'" : "null");
  576. $sql .= ", phone_mobile = ".($this->phone_mobile ? "'".$this->db->escape($this->phone_mobile)."'" : "null");
  577. $sql .= ", note_private = ".($this->note_private ? "'".$this->db->escape($this->note_private)."'" : "null");
  578. $sql .= ", note_public = ".($this->note_public ? "'".$this->db->escape($this->note_public)."'" : "null");
  579. $sql .= ", photo = ".($this->photo ? "'".$this->db->escape($this->photo)."'" : "null");
  580. $sql .= ", public = '".$this->db->escape($this->public)."'";
  581. $sql .= ", statut = ".$this->db->escape($this->statut);
  582. $sql .= ", fk_adherent_type = ".$this->db->escape($this->typeid);
  583. $sql .= ", morphy = '".$this->db->escape($this->morphy)."'";
  584. $sql .= ", birth = ".($this->birth ? "'".$this->db->idate($this->birth)."'" : "null");
  585. if ($this->socid) {
  586. $sql .= ", fk_soc = '".$this->db->escape($this->socid)."'"; // Must be modified only when creating from a third-party
  587. }
  588. if ($this->datefin) {
  589. $sql .= ", datefin = '".$this->db->idate($this->datefin)."'"; // Must be modified only when deleting a subscription
  590. }
  591. if ($this->datevalid) {
  592. $sql .= ", datevalid = '".$this->db->idate($this->datevalid)."'"; // Must be modified only when validating a member
  593. }
  594. $sql .= ", fk_user_mod = ".($user->id > 0 ? $user->id : 'null'); // Can be null because member can be create by a guest
  595. $sql .= " WHERE rowid = ".((int) $this->id);
  596. // If we change the type of membership, we set also label of new type
  597. if (!empty($this->oldcopy) && $this->typeid != $this->oldcopy->typeid) {
  598. $sql2 = "SELECT libelle as label";
  599. $sql2 .= " FROM ".MAIN_DB_PREFIX."adherent_type";
  600. $sql2 .= " WHERE rowid = ".$this->typeid;
  601. $resql2 = $this->db->query($sql2);
  602. if ($resql2) {
  603. while ($obj = $this->db->fetch_object($resql2)) {
  604. $this->type = $obj->label;
  605. }
  606. }
  607. }
  608. dol_syslog(get_class($this)."::update update member", LOG_DEBUG);
  609. $resql = $this->db->query($sql);
  610. if ($resql) {
  611. unset($this->country_code);
  612. unset($this->country);
  613. unset($this->state_code);
  614. unset($this->state);
  615. $nbrowsaffected += $this->db->affected_rows($resql);
  616. $action = 'update';
  617. // Actions on extra fields
  618. if (!$error) {
  619. $result = $this->insertExtraFields();
  620. if ($result < 0) {
  621. $error++;
  622. }
  623. }
  624. // Update password
  625. if (!$error && $this->pass) {
  626. dol_syslog(get_class($this)."::update update password");
  627. if ($this->pass != $this->pass_indatabase && $this->pass != $this->pass_indatabase_crypted) {
  628. $isencrypted = empty($conf->global->DATABASE_PWD_ENCRYPTED) ? 0 : 1;
  629. // If password to set differs from the one found into database
  630. $result = $this->setPassword($user, $this->pass, $isencrypted, $notrigger, $nosyncuserpass);
  631. if (!$nbrowsaffected) {
  632. $nbrowsaffected++;
  633. }
  634. }
  635. }
  636. // Remove links to user and replace with new one
  637. if (!$error) {
  638. dol_syslog(get_class($this)."::update update link to user");
  639. $sql = "UPDATE ".MAIN_DB_PREFIX."user SET fk_member = NULL WHERE fk_member = ".$this->id;
  640. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  641. $resql = $this->db->query($sql);
  642. if (!$resql) {
  643. $this->error = $this->db->error();
  644. $this->db->rollback();
  645. return -5;
  646. }
  647. // If there is a user linked to this member
  648. if ($this->user_id > 0) {
  649. $sql = "UPDATE ".MAIN_DB_PREFIX."user SET fk_member = ".$this->id." WHERE rowid = ".$this->user_id;
  650. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  651. $resql = $this->db->query($sql);
  652. if (!$resql) {
  653. $this->error = $this->db->error();
  654. $this->db->rollback();
  655. return -5;
  656. }
  657. }
  658. }
  659. if (!$error && $nbrowsaffected) { // If something has change in main data
  660. // Update information on linked user if it is an update
  661. if (!$error && $this->user_id > 0 && !$nosyncuser) {
  662. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  663. dol_syslog(get_class($this)."::update update linked user");
  664. $luser = new User($this->db);
  665. $result = $luser->fetch($this->user_id);
  666. if ($result >= 0) {
  667. //var_dump($this->user_login);exit;
  668. //var_dump($this->login);exit;
  669. // If option ADHERENT_LOGIN_NOT_REQUIRED is on, there is no login of member, so we do not overwrite user login to keep existing one.
  670. if (empty($conf->global->ADHERENT_LOGIN_NOT_REQUIRED)) {
  671. $luser->login = $this->login;
  672. }
  673. $luser->ref = $this->ref;
  674. $luser->civility_id = $this->civility_id;
  675. $luser->firstname = $this->firstname;
  676. $luser->lastname = $this->lastname;
  677. $luser->gender = $this->gender;
  678. $luser->pass = $this->pass;
  679. //$luser->socid=$this->fk_soc; // We do not enable this. This may transform a user into an external user.
  680. $luser->birth = $this->birth;
  681. $luser->address = $this->address;
  682. $luser->zip = $this->zip;
  683. $luser->town = $this->town;
  684. $luser->country_id = $this->country_id;
  685. $luser->state_id = $this->state_id;
  686. $luser->email = $this->email;
  687. $luser->socialnetworks = $this->socialnetworks;
  688. $luser->office_phone = $this->phone;
  689. $luser->user_mobile = $this->phone_mobile;
  690. $luser->fk_member = $this->id;
  691. $result = $luser->update($user, 0, 1, 1); // Use nosync to 1 to avoid cyclic updates
  692. if ($result < 0) {
  693. $this->error = $luser->error;
  694. dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR);
  695. $error++;
  696. }
  697. } else {
  698. $this->error = $luser->error;
  699. $error++;
  700. }
  701. }
  702. // Update information on linked thirdparty if it is an update
  703. if (!$error && $this->fk_soc > 0 && !$nosyncthirdparty) {
  704. require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  705. dol_syslog(get_class($this)."::update update linked thirdparty");
  706. // This member is linked with a thirdparty, so we also update thirdparty informations
  707. // if this is an update.
  708. $lthirdparty = new Societe($this->db);
  709. $result = $lthirdparty->fetch($this->fk_soc);
  710. if ($result > 0) {
  711. $lthirdparty->address = $this->address;
  712. $lthirdparty->zip = $this->zip;
  713. $lthirdparty->town = $this->town;
  714. $lthirdparty->email = $this->email;
  715. $lthirdparty->socialnetworks = $this->socialnetworks;
  716. $lthirdparty->phone = $this->phone;
  717. $lthirdparty->state_id = $this->state_id;
  718. $lthirdparty->country_id = $this->country_id;
  719. //$lthirdparty->phone_mobile=$this->phone_mobile;
  720. $result = $lthirdparty->update($this->fk_soc, $user, 0, 1, 1, 'update'); // Use sync to 0 to avoid cyclic updates
  721. if ($result < 0) {
  722. $this->error = $lthirdparty->error;
  723. $this->errors = $lthirdparty->errors;
  724. dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR);
  725. $error++;
  726. }
  727. } elseif ($result < 0) {
  728. $this->error = $lthirdparty->error;
  729. $error++;
  730. }
  731. }
  732. }
  733. if (!$error && !$notrigger) {
  734. // Call trigger
  735. $result = $this->call_trigger('MEMBER_MODIFY', $user);
  736. if ($result < 0) {
  737. $error++;
  738. }
  739. // End call triggers
  740. }
  741. if (!$error) {
  742. $this->db->commit();
  743. return $nbrowsaffected;
  744. } else {
  745. $this->db->rollback();
  746. return -1;
  747. }
  748. } else {
  749. $this->db->rollback();
  750. $this->error = $this->db->lasterror();
  751. return -2;
  752. }
  753. }
  754. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  755. /**
  756. * Update denormalized last subscription date.
  757. * This function is called when we delete a subscription for example.
  758. *
  759. * @param User $user User making change
  760. * @return int <0 if KO, >0 if OK
  761. */
  762. public function update_end_date($user)
  763. {
  764. // phpcs:enable
  765. $this->db->begin();
  766. // Search for last subscription id and end date
  767. $sql = "SELECT rowid, datec as dateop, dateadh as datedeb, datef as datefin";
  768. $sql .= " FROM ".MAIN_DB_PREFIX."subscription";
  769. $sql .= " WHERE fk_adherent=".$this->id;
  770. $sql .= " ORDER by dateadh DESC"; // Sort by start subscription date
  771. dol_syslog(get_class($this)."::update_end_date", LOG_DEBUG);
  772. $resql = $this->db->query($sql);
  773. if ($resql) {
  774. $obj = $this->db->fetch_object($resql);
  775. $dateop = $this->db->jdate($obj->dateop);
  776. $datedeb = $this->db->jdate($obj->datedeb);
  777. $datefin = $this->db->jdate($obj->datefin);
  778. $sql = "UPDATE ".MAIN_DB_PREFIX."adherent SET";
  779. $sql .= " datefin=".($datefin != '' ? "'".$this->db->idate($datefin)."'" : "null");
  780. $sql .= " WHERE rowid = ".$this->id;
  781. dol_syslog(get_class($this)."::update_end_date", LOG_DEBUG);
  782. $resql = $this->db->query($sql);
  783. if ($resql) {
  784. $this->last_subscription_date = $dateop;
  785. $this->last_subscription_date_start = $datedeb;
  786. $this->last_subscription_date_end = $datefin;
  787. $this->datefin = $datefin;
  788. $this->db->commit();
  789. return 1;
  790. } else {
  791. $this->db->rollback();
  792. return -1;
  793. }
  794. } else {
  795. $this->error = $this->db->lasterror();
  796. $this->db->rollback();
  797. return -1;
  798. }
  799. }
  800. /**
  801. * Fonction qui supprime l'adherent et les donnees associees
  802. *
  803. * @param int $rowid Id of member to delete
  804. * @param User $user User object
  805. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  806. * @return int <0 if KO, 0=nothing to do, >0 if OK
  807. */
  808. public function delete($rowid, $user, $notrigger = 0)
  809. {
  810. global $conf, $langs;
  811. $result = 0;
  812. $error = 0;
  813. $errorflag = 0;
  814. // Check parameters
  815. if (empty($rowid)) {
  816. $rowid = $this->id;
  817. }
  818. $this->db->begin();
  819. if (!$error && !$notrigger) {
  820. // Call trigger
  821. $result = $this->call_trigger('MEMBER_DELETE', $user);
  822. if ($result < 0) {
  823. $error++;
  824. }
  825. // End call triggers
  826. }
  827. // Remove category
  828. $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_member WHERE fk_member = ".((int) $rowid);
  829. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  830. $resql = $this->db->query($sql);
  831. if (!$resql) {
  832. $error++;
  833. $this->error .= $this->db->lasterror();
  834. $errorflag = -1;
  835. }
  836. // Remove subscription
  837. if (!$error) {
  838. $sql = "DELETE FROM ".MAIN_DB_PREFIX."subscription WHERE fk_adherent = ".((int) $rowid);
  839. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  840. $resql = $this->db->query($sql);
  841. if (!$resql) {
  842. $error++;
  843. $this->error .= $this->db->lasterror();
  844. $errorflag = -2;
  845. }
  846. }
  847. // Remove linked user
  848. if (!$error) {
  849. $ret = $this->setUserId(0);
  850. if ($ret < 0) {
  851. $error++;
  852. $this->error .= $this->db->lasterror();
  853. $errorflag = -3;
  854. }
  855. }
  856. // Removed extrafields
  857. if (!$error) {
  858. $result = $this->deleteExtraFields();
  859. if ($result < 0) {
  860. $error++;
  861. $errorflag = -4;
  862. dol_syslog(get_class($this)."::delete erreur ".$errorflag." ".$this->error, LOG_ERR);
  863. }
  864. }
  865. // Remove adherent
  866. if (!$error) {
  867. $sql = "DELETE FROM ".MAIN_DB_PREFIX."adherent WHERE rowid = ".((int) $rowid);
  868. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  869. $resql = $this->db->query($sql);
  870. if (!$resql) {
  871. $error++;
  872. $this->error .= $this->db->lasterror();
  873. $errorflag = -5;
  874. }
  875. }
  876. if (!$error) {
  877. $this->db->commit();
  878. return 1;
  879. } else {
  880. $this->db->rollback();
  881. return $errorflag;
  882. }
  883. }
  884. /**
  885. * Change password of a user
  886. *
  887. * @param User $user Object user de l'utilisateur qui fait la modification
  888. * @param string $password New password (to generate if empty)
  889. * @param int $isencrypted 0 ou 1 si il faut crypter le mot de passe en base (0 par defaut)
  890. * @param int $notrigger 1=Ne declenche pas les triggers
  891. * @param int $nosyncuser Do not synchronize linked user
  892. * @return string If OK return clear password, 0 if no change, < 0 if error
  893. */
  894. public function setPassword($user, $password = '', $isencrypted = 0, $notrigger = 0, $nosyncuser = 0)
  895. {
  896. global $conf, $langs;
  897. $error = 0;
  898. dol_syslog(get_class($this)."::setPassword user=".$user->id." password=".preg_replace('/./i', '*', $password)." isencrypted=".$isencrypted);
  899. // If new password not provided, we generate one
  900. if (!$password) {
  901. require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
  902. $password = getRandomPassword(false);
  903. }
  904. // Crypt password
  905. $password_crypted = dol_hash($password);
  906. $password_indatabase = '';
  907. if (!$isencrypted) {
  908. $password_indatabase = $password;
  909. }
  910. $this->db->begin();
  911. // Mise a jour
  912. $sql = "UPDATE ".MAIN_DB_PREFIX."adherent";
  913. $sql .= " SET pass_crypted = '".$this->db->escape($password_crypted)."'";
  914. //if (! empty($conf->global->DATABASE_PWD_ENCRYPTED))
  915. if ($isencrypted) {
  916. $sql .= ", pass = null";
  917. } else {
  918. $sql .= ", pass = '".$this->db->escape($password_indatabase)."'";
  919. }
  920. $sql .= " WHERE rowid = ".$this->id;
  921. //dol_syslog("Adherent::Password sql=hidden");
  922. dol_syslog(get_class($this)."::setPassword", LOG_DEBUG);
  923. $result = $this->db->query($sql);
  924. if ($result) {
  925. $nbaffectedrows = $this->db->affected_rows($result);
  926. if ($nbaffectedrows) {
  927. $this->pass = $password;
  928. $this->pass_indatabase = $password_indatabase;
  929. $this->pass_indatabase_crypted = $password_crypted;
  930. if ($this->user_id && !$nosyncuser) {
  931. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  932. // This member is linked with a user, so we also update users informations
  933. // if this is an update.
  934. $luser = new User($this->db);
  935. $result = $luser->fetch($this->user_id);
  936. if ($result >= 0) {
  937. $result = $luser->setPassword($user, $this->pass, 0, 0, 1);
  938. if ($result < 0) {
  939. $this->error = $luser->error;
  940. dol_syslog(get_class($this)."::setPassword ".$this->error, LOG_ERR);
  941. $error++;
  942. }
  943. } else {
  944. $this->error = $luser->error;
  945. $error++;
  946. }
  947. }
  948. if (!$error && !$notrigger) {
  949. // Call trigger
  950. $result = $this->call_trigger('MEMBER_NEW_PASSWORD', $user);
  951. if ($result < 0) {
  952. $error++;
  953. $this->db->rollback();
  954. return -1;
  955. }
  956. // End call triggers
  957. }
  958. $this->db->commit();
  959. return $this->pass;
  960. } else {
  961. $this->db->rollback();
  962. return 0;
  963. }
  964. } else {
  965. $this->db->rollback();
  966. dol_print_error($this->db);
  967. return -1;
  968. }
  969. }
  970. /**
  971. * Set link to a user
  972. *
  973. * @param int $userid Id of user to link to
  974. * @return int 1=OK, -1=KO
  975. */
  976. public function setUserId($userid)
  977. {
  978. global $conf, $langs;
  979. $this->db->begin();
  980. // If user is linked to this member, remove old link to this member
  981. $sql = "UPDATE ".MAIN_DB_PREFIX."user SET fk_member = NULL WHERE fk_member = ".((int) $this->id);
  982. dol_syslog(get_class($this)."::setUserId", LOG_DEBUG);
  983. $resql = $this->db->query($sql);
  984. if (!$resql) {
  985. $this->error = $this->db->error();
  986. $this->db->rollback();
  987. return -1;
  988. }
  989. // Set link to user
  990. if ($userid > 0) {
  991. $sql = "UPDATE ".MAIN_DB_PREFIX."user SET fk_member = ".((int) $this->id);
  992. $sql .= " WHERE rowid = ".((int) $userid);
  993. dol_syslog(get_class($this)."::setUserId", LOG_DEBUG);
  994. $resql = $this->db->query($sql);
  995. if (!$resql) {
  996. $this->error = $this->db->error();
  997. $this->db->rollback();
  998. return -2;
  999. }
  1000. }
  1001. $this->db->commit();
  1002. return 1;
  1003. }
  1004. /**
  1005. * Set link to a third party
  1006. *
  1007. * @param int $thirdpartyid Id of user to link to
  1008. * @return int 1=OK, -1=KO
  1009. */
  1010. public function setThirdPartyId($thirdpartyid)
  1011. {
  1012. global $conf, $langs;
  1013. $this->db->begin();
  1014. // Remove link to third party onto any other members
  1015. if ($thirdpartyid > 0) {
  1016. $sql = "UPDATE ".MAIN_DB_PREFIX."adherent SET fk_soc = null";
  1017. $sql .= " WHERE fk_soc = ".((int) $thirdpartyid);
  1018. $sql .= " AND entity = ".$conf->entity;
  1019. dol_syslog(get_class($this)."::setThirdPartyId", LOG_DEBUG);
  1020. $resql = $this->db->query($sql);
  1021. }
  1022. // Add link to third party for current member
  1023. $sql = "UPDATE ".MAIN_DB_PREFIX."adherent SET fk_soc = ".($thirdpartyid > 0 ? $thirdpartyid : 'null');
  1024. $sql .= " WHERE rowid = ".$this->id;
  1025. dol_syslog(get_class($this)."::setThirdPartyId", LOG_DEBUG);
  1026. $resql = $this->db->query($sql);
  1027. if ($resql) {
  1028. $this->db->commit();
  1029. return 1;
  1030. } else {
  1031. $this->error = $this->db->error();
  1032. $this->db->rollback();
  1033. return -1;
  1034. }
  1035. }
  1036. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1037. /**
  1038. * Method to load member from its login
  1039. *
  1040. * @param string $login login of member
  1041. * @return void
  1042. */
  1043. public function fetch_login($login)
  1044. {
  1045. // phpcs:enable
  1046. global $conf;
  1047. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."adherent";
  1048. $sql .= " WHERE login='".$this->db->escape($login)."'";
  1049. $sql .= " AND entity = ".$conf->entity;
  1050. $resql = $this->db->query($sql);
  1051. if ($resql) {
  1052. if ($this->db->num_rows($resql)) {
  1053. $obj = $this->db->fetch_object($resql);
  1054. $this->fetch($obj->rowid);
  1055. }
  1056. } else {
  1057. dol_print_error($this->db);
  1058. }
  1059. }
  1060. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1061. /**
  1062. * Method to load member from its name
  1063. *
  1064. * @param string $firstname Firstname
  1065. * @param string $lastname Lastname
  1066. * @return void
  1067. */
  1068. public function fetch_name($firstname, $lastname)
  1069. {
  1070. // phpcs:enable
  1071. global $conf;
  1072. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."adherent";
  1073. $sql .= " WHERE firstname='".$this->db->escape($firstname)."'";
  1074. $sql .= " AND lastname='".$this->db->escape($lastname)."'";
  1075. $sql .= " AND entity = ".$conf->entity;
  1076. $resql = $this->db->query($sql);
  1077. if ($resql) {
  1078. if ($this->db->num_rows($resql)) {
  1079. $obj = $this->db->fetch_object($resql);
  1080. $this->fetch($obj->rowid);
  1081. }
  1082. } else {
  1083. dol_print_error($this->db);
  1084. }
  1085. }
  1086. /**
  1087. * Load member from database
  1088. *
  1089. * @param int $rowid Id of object to load
  1090. * @param string $ref To load member from its ref
  1091. * @param int $fk_soc To load member from its link to third party
  1092. * @param string $ref_ext External reference
  1093. * @param bool $fetch_optionals To load optionals (extrafields)
  1094. * @param bool $fetch_subscriptions To load member subscriptions
  1095. * @return int >0 if OK, 0 if not found, <0 if KO
  1096. */
  1097. public function fetch($rowid, $ref = '', $fk_soc = '', $ref_ext = '', $fetch_optionals = true, $fetch_subscriptions = true)
  1098. {
  1099. global $langs;
  1100. $sql = "SELECT d.rowid, d.ref, d.ref_ext, d.civility as civility_code, d.gender, d.firstname, d.lastname,";
  1101. $sql .= " d.societe as company, d.fk_soc, d.statut, d.public, d.address, d.zip, d.town, d.note_private,";
  1102. $sql .= " d.note_public,";
  1103. $sql .= " d.email, d.socialnetworks, d.phone, d.phone_perso, d.phone_mobile, d.login, d.pass, d.pass_crypted,";
  1104. $sql .= " d.photo, d.fk_adherent_type, d.morphy, d.entity,";
  1105. $sql .= " d.datec as datec,";
  1106. $sql .= " d.tms as datem,";
  1107. $sql .= " d.datefin as datefin,";
  1108. $sql .= " d.birth as birthday,";
  1109. $sql .= " d.datevalid as datev,";
  1110. $sql .= " d.country,";
  1111. $sql .= " d.state_id,";
  1112. $sql .= " d.model_pdf,";
  1113. $sql .= " c.rowid as country_id, c.code as country_code, c.label as country,";
  1114. $sql .= " dep.nom as state, dep.code_departement as state_code,";
  1115. $sql .= " t.libelle as type, t.subscription as subscription,";
  1116. $sql .= " u.rowid as user_id, u.login as user_login";
  1117. $sql .= " FROM ".MAIN_DB_PREFIX."adherent_type as t, ".MAIN_DB_PREFIX."adherent as d";
  1118. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as c ON d.country = c.rowid";
  1119. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as dep ON d.state_id = dep.rowid";
  1120. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as u ON d.rowid = u.fk_member";
  1121. $sql .= " WHERE d.fk_adherent_type = t.rowid";
  1122. if ($rowid) {
  1123. $sql .= " AND d.rowid=".((int) $rowid);
  1124. } elseif ($ref || $fk_soc) {
  1125. $sql .= " AND d.entity IN (".getEntity('adherent').")";
  1126. if ($ref) {
  1127. $sql .= " AND d.ref='".$this->db->escape($ref)."'";
  1128. } elseif ($fk_soc > 0) {
  1129. $sql .= " AND d.fk_soc=".((int) $fk_soc);
  1130. }
  1131. } elseif ($ref_ext) {
  1132. $sql .= " AND d.ref_ext='".$this->db->escape($ref_ext)."'";
  1133. }
  1134. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  1135. $resql = $this->db->query($sql);
  1136. if ($resql) {
  1137. if ($this->db->num_rows($resql)) {
  1138. $obj = $this->db->fetch_object($resql);
  1139. $this->entity = $obj->entity;
  1140. $this->id = $obj->rowid;
  1141. $this->ref = $obj->ref;
  1142. $this->ref_ext = $obj->ref_ext;
  1143. $this->civility_id = $obj->civility_code; // Bad. Kept for backard compatibility
  1144. $this->civility_code = $obj->civility_code;
  1145. $this->civility = $obj->civility_code ? ($langs->trans("Civility".$obj->civility_code) != ("Civility".$obj->civility_code) ? $langs->trans("Civility".$obj->civility_code) : $obj->civility_code) : '';
  1146. $this->firstname = $obj->firstname;
  1147. $this->lastname = $obj->lastname;
  1148. $this->gender = $obj->gender;
  1149. $this->login = $obj->login;
  1150. $this->societe = $obj->company;
  1151. $this->company = $obj->company;
  1152. $this->socid = $obj->fk_soc;
  1153. $this->fk_soc = $obj->fk_soc; // For backward compatibility
  1154. $this->address = $obj->address;
  1155. $this->zip = $obj->zip;
  1156. $this->town = $obj->town;
  1157. $this->pass = $obj->pass;
  1158. $this->pass_indatabase = $obj->pass;
  1159. $this->pass_indatabase_crypted = $obj->pass_crypted;
  1160. $this->state_id = $obj->state_id;
  1161. $this->state_code = $obj->state_id ? $obj->state_code : '';
  1162. $this->state = $obj->state_id ? $obj->state : '';
  1163. $this->country_id = $obj->country_id;
  1164. $this->country_code = $obj->country_code;
  1165. if ($langs->trans("Country".$obj->country_code) != "Country".$obj->country_code) {
  1166. $this->country = $langs->transnoentitiesnoconv("Country".$obj->country_code);
  1167. } else {
  1168. $this->country = $obj->country;
  1169. }
  1170. $this->phone = $obj->phone;
  1171. $this->phone_perso = $obj->phone_perso;
  1172. $this->phone_mobile = $obj->phone_mobile;
  1173. $this->email = $obj->email;
  1174. $this->socialnetworks = (array) json_decode($obj->socialnetworks, true);
  1175. $this->photo = $obj->photo;
  1176. $this->statut = $obj->statut;
  1177. $this->public = $obj->public;
  1178. $this->datec = $this->db->jdate($obj->datec);
  1179. $this->date_creation = $this->db->jdate($obj->datec);
  1180. $this->datem = $this->db->jdate($obj->datem);
  1181. $this->date_modification = $this->db->jdate($obj->datem);
  1182. $this->datefin = $this->db->jdate($obj->datefin);
  1183. $this->datevalid = $this->db->jdate($obj->datev);
  1184. $this->date_validation = $this->db->jdate($obj->datev);
  1185. $this->birth = $this->db->jdate($obj->birthday);
  1186. $this->note_private = $obj->note_private;
  1187. $this->note_public = $obj->note_public;
  1188. $this->morphy = $obj->morphy;
  1189. $this->typeid = $obj->fk_adherent_type;
  1190. $this->type = $obj->type;
  1191. $this->need_subscription = $obj->subscription;
  1192. $this->user_id = $obj->user_id;
  1193. $this->user_login = $obj->user_login;
  1194. $this->model_pdf = $obj->model_pdf;
  1195. // Retrieve all extrafield
  1196. // fetch optionals attributes and labels
  1197. if ($fetch_optionals) {
  1198. $this->fetch_optionals();
  1199. }
  1200. // Load other properties
  1201. if ($fetch_subscriptions) {
  1202. $result = $this->fetch_subscriptions();
  1203. }
  1204. return $this->id;
  1205. } else {
  1206. return 0;
  1207. }
  1208. } else {
  1209. $this->error = $this->db->lasterror();
  1210. return -1;
  1211. }
  1212. }
  1213. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1214. /**
  1215. * Function to get member subscriptions data
  1216. * first_subscription_date, first_subscription_date_start, first_subscription_date_end, first_subscription_amount
  1217. * last_subscription_date, last_subscription_date_start, last_subscription_date_end, last_subscription_amount
  1218. *
  1219. * @return int <0 si KO, >0 si OK
  1220. */
  1221. public function fetch_subscriptions()
  1222. {
  1223. // phpcs:enable
  1224. global $langs;
  1225. require_once DOL_DOCUMENT_ROOT.'/adherents/class/subscription.class.php';
  1226. $sql = "SELECT c.rowid, c.fk_adherent, c.fk_type, c.subscription, c.note, c.fk_bank,";
  1227. $sql .= " c.tms as datem,";
  1228. $sql .= " c.datec as datec,";
  1229. $sql .= " c.dateadh as dateh,";
  1230. $sql .= " c.datef as datef";
  1231. $sql .= " FROM ".MAIN_DB_PREFIX."subscription as c";
  1232. $sql .= " WHERE c.fk_adherent = ".$this->id;
  1233. $sql .= " ORDER BY c.dateadh";
  1234. dol_syslog(get_class($this)."::fetch_subscriptions", LOG_DEBUG);
  1235. $resql = $this->db->query($sql);
  1236. if ($resql) {
  1237. $this->subscriptions = array();
  1238. $i = 0;
  1239. while ($obj = $this->db->fetch_object($resql)) {
  1240. if ($i == 0) {
  1241. $this->first_subscription_date = $this->db->jdate($obj->datec);
  1242. $this->first_subscription_date_start = $this->db->jdate($obj->dateh);
  1243. $this->first_subscription_date_end = $this->db->jdate($obj->datef);
  1244. $this->first_subscription_amount = $obj->subscription;
  1245. }
  1246. $this->last_subscription_date = $this->db->jdate($obj->datec);
  1247. $this->last_subscription_date_start = $this->db->jdate($obj->datef);
  1248. $this->last_subscription_date_end = $this->db->jdate($obj->datef);
  1249. $this->last_subscription_amount = $obj->subscription;
  1250. $subscription = new Subscription($this->db);
  1251. $subscription->id = $obj->rowid;
  1252. $subscription->fk_adherent = $obj->fk_adherent;
  1253. $subscription->fk_type = $obj->fk_type;
  1254. $subscription->amount = $obj->subscription;
  1255. $subscription->note = $obj->note;
  1256. $subscription->fk_bank = $obj->fk_bank;
  1257. $subscription->datem = $this->db->jdate($obj->datem);
  1258. $subscription->datec = $this->db->jdate($obj->datec);
  1259. $subscription->dateh = $this->db->jdate($obj->dateh);
  1260. $subscription->datef = $this->db->jdate($obj->datef);
  1261. $this->subscriptions[] = $subscription;
  1262. $i++;
  1263. }
  1264. return 1;
  1265. } else {
  1266. $this->error = $this->db->error().' sql='.$sql;
  1267. return -1;
  1268. }
  1269. }
  1270. /**
  1271. * Insert subscription into database and eventually add links to banks, mailman, etc...
  1272. *
  1273. * @param int $date Date of effect of subscription
  1274. * @param double $amount Amount of subscription (0 accepted for some members)
  1275. * @param int $accountid Id bank account
  1276. * @param string $operation Type of payment (if Id bank account provided). Example: 'CB', ...
  1277. * @param string $label Label operation (if Id bank account provided)
  1278. * @param string $num_chq Numero cheque (if Id bank account provided)
  1279. * @param string $emetteur_nom Name of cheque writer
  1280. * @param string $emetteur_banque Name of bank of cheque
  1281. * @param int $datesubend Date end subscription
  1282. * @return int rowid of record added, <0 if KO
  1283. */
  1284. public function subscription($date, $amount, $accountid = 0, $operation = '', $label = '', $num_chq = '', $emetteur_nom = '', $emetteur_banque = '', $datesubend = 0)
  1285. {
  1286. global $conf, $langs, $user;
  1287. require_once DOL_DOCUMENT_ROOT.'/adherents/class/subscription.class.php';
  1288. $error = 0;
  1289. // Clean parameters
  1290. if (!$amount) {
  1291. $amount = 0;
  1292. }
  1293. $this->db->begin();
  1294. if ($datesubend) {
  1295. $datefin = $datesubend;
  1296. } else {
  1297. // If no end date, end date = date + 1 year - 1 day
  1298. $datefin = dol_time_plus_duree($date, 1, 'y');
  1299. $datefin = dol_time_plus_duree($datefin, -1, 'd');
  1300. }
  1301. // Create subscription
  1302. $subscription = new Subscription($this->db);
  1303. $subscription->fk_adherent = $this->id;
  1304. $subscription->dateh = $date; // Date of new subscription
  1305. $subscription->datef = $datefin; // End data of new subscription
  1306. $subscription->amount = $amount;
  1307. $subscription->note = $label; // deprecated
  1308. $subscription->note_public = $label;
  1309. $rowid = $subscription->create($user);
  1310. if ($rowid > 0) {
  1311. // Update denormalized subscription end date (read database subscription to find values)
  1312. // This will also update this->datefin
  1313. $result = $this->update_end_date($user);
  1314. if ($result > 0) {
  1315. // Change properties of object (used by triggers)
  1316. $this->last_subscription_date = dol_now();
  1317. $this->last_subscription_date_start = $date;
  1318. $this->last_subscription_date_end = $datefin;
  1319. $this->last_subscription_amount = $amount;
  1320. }
  1321. if (!$error) {
  1322. $this->db->commit();
  1323. return $rowid;
  1324. } else {
  1325. $this->db->rollback();
  1326. return -2;
  1327. }
  1328. } else {
  1329. $this->error = $subscription->error;
  1330. $this->errors = $subscription->errors;
  1331. $this->db->rollback();
  1332. return -1;
  1333. }
  1334. }
  1335. /**
  1336. * Do complementary actions after subscription recording.
  1337. *
  1338. * @param int $subscriptionid Id of created subscription
  1339. * @param string $option Which action ('bankdirect', 'bankviainvoice', 'invoiceonly', ...)
  1340. * @param int $accountid Id bank account
  1341. * @param int $datesubscription Date of subscription
  1342. * @param int $paymentdate Date of payment
  1343. * @param string $operation Code of type of operation (if Id bank account provided). Example 'CB', ...
  1344. * @param string $label Label operation (if Id bank account provided)
  1345. * @param double $amount Amount of subscription (0 accepted for some members)
  1346. * @param string $num_chq Numero cheque (if Id bank account provided)
  1347. * @param string $emetteur_nom Name of cheque writer
  1348. * @param string $emetteur_banque Name of bank of cheque
  1349. * @param string $autocreatethirdparty Auto create new thirdparty if member not yet linked to a thirdparty and we request an option that generate invoice.
  1350. * @return int <0 if KO, >0 if OK
  1351. */
  1352. public function subscriptionComplementaryActions($subscriptionid, $option, $accountid, $datesubscription, $paymentdate, $operation, $label, $amount, $num_chq, $emetteur_nom = '', $emetteur_banque = '', $autocreatethirdparty = 0)
  1353. {
  1354. global $conf, $langs, $user, $mysoc;
  1355. $error = 0;
  1356. $this->invoice = null; // This will contains invoice if an invoice is created
  1357. dol_syslog("subscriptionComplementaryActions subscriptionid=".$subscriptionid." option=".$option." accountid=".$accountid." datesubscription=".$datesubscription." paymentdate=".
  1358. $paymentdate." label=".$label." amount=".$amount." num_chq=".$num_chq." autocreatethirdparty=".$autocreatethirdparty);
  1359. // Insert into bank account directlty (if option choosed for) + link to llx_subscription if option is 'bankdirect'
  1360. if ($option == 'bankdirect' && $accountid) {
  1361. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  1362. $acct = new Account($this->db);
  1363. $result = $acct->fetch($accountid);
  1364. $dateop = $paymentdate;
  1365. $insertid = $acct->addline($dateop, $operation, $label, $amount, $num_chq, '', $user, $emetteur_nom, $emetteur_banque);
  1366. if ($insertid > 0) {
  1367. $inserturlid = $acct->add_url_line($insertid, $this->id, DOL_URL_ROOT.'/adherents/card.php?rowid=', $this->getFullname($langs), 'member');
  1368. if ($inserturlid > 0) {
  1369. // Update table subscription
  1370. $sql = "UPDATE ".MAIN_DB_PREFIX."subscription SET fk_bank=".$insertid;
  1371. $sql .= " WHERE rowid=".$subscriptionid;
  1372. dol_syslog("subscription::subscription", LOG_DEBUG);
  1373. $resql = $this->db->query($sql);
  1374. if (!$resql) {
  1375. $error++;
  1376. $this->error = $this->db->lasterror();
  1377. $this->errors[] = $this->error;
  1378. }
  1379. } else {
  1380. $error++;
  1381. $this->error = $acct->error;
  1382. $this->errors = $acct->errors;
  1383. }
  1384. } else {
  1385. $error++;
  1386. $this->error = $acct->error;
  1387. $this->errors = $acct->errors;
  1388. }
  1389. }
  1390. // If option choosed, we create invoice
  1391. if (($option == 'bankviainvoice' && $accountid) || $option == 'invoiceonly') {
  1392. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  1393. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/paymentterm.class.php';
  1394. $invoice = new Facture($this->db);
  1395. $customer = new Societe($this->db);
  1396. if (!$error) {
  1397. if (!($this->fk_soc > 0)) { // If not yet linked to a company
  1398. if ($autocreatethirdparty) {
  1399. // Create a linked thirdparty to member
  1400. $companyalias = '';
  1401. $fullname = $this->getFullName($langs);
  1402. if ($this->morphy == 'mor') {
  1403. $companyname = $this->company;
  1404. if (!empty($fullname)) {
  1405. $companyalias = $fullname;
  1406. }
  1407. } else {
  1408. $companyname = $fullname;
  1409. if (!empty($this->company)) {
  1410. $companyalias = $this->company;
  1411. }
  1412. }
  1413. $result = $customer->create_from_member($this, $companyname, $companyalias);
  1414. if ($result < 0) {
  1415. $this->error = $customer->error;
  1416. $this->errors = $customer->errors;
  1417. $error++;
  1418. } else {
  1419. $this->fk_soc = $result;
  1420. }
  1421. } else {
  1422. $langs->load("errors");
  1423. $this->error = $langs->trans("ErrorMemberNotLinkedToAThirpartyLinkOrCreateFirst");
  1424. $this->errors[] = $this->error;
  1425. $error++;
  1426. }
  1427. }
  1428. }
  1429. if (!$error) {
  1430. $result = $customer->fetch($this->fk_soc);
  1431. if ($result <= 0) {
  1432. $this->error = $customer->error;
  1433. $this->errors = $customer->errors;
  1434. $error++;
  1435. }
  1436. }
  1437. if (!$error) {
  1438. // Create draft invoice
  1439. $invoice->type = Facture::TYPE_STANDARD;
  1440. $invoice->cond_reglement_id = $customer->cond_reglement_id;
  1441. if (empty($invoice->cond_reglement_id)) {
  1442. $paymenttermstatic = new PaymentTerm($this->db);
  1443. $invoice->cond_reglement_id = $paymenttermstatic->getDefaultId();
  1444. if (empty($invoice->cond_reglement_id)) {
  1445. $error++;
  1446. $this->error = 'ErrorNoPaymentTermRECEPFound';
  1447. $this->errors[] = $this->error;
  1448. }
  1449. }
  1450. $invoice->socid = $this->fk_soc;
  1451. $invoice->date = $datesubscription;
  1452. // Possibility to add external linked objects with hooks
  1453. $invoice->linked_objects['subscription'] = $subscriptionid;
  1454. if (!empty($_POST['other_linked_objects']) && is_array($_POST['other_linked_objects'])) {
  1455. $invoice->linked_objects = array_merge($invoice->linked_objects, $_POST['other_linked_objects']);
  1456. }
  1457. $result = $invoice->create($user);
  1458. if ($result <= 0) {
  1459. $this->error = $invoice->error;
  1460. $this->errors = $invoice->errors;
  1461. $error++;
  1462. } else {
  1463. $this->invoice = $invoice;
  1464. }
  1465. }
  1466. if (!$error) {
  1467. // Add line to draft invoice
  1468. $idprodsubscription = 0;
  1469. if (!empty($conf->global->ADHERENT_PRODUCT_ID_FOR_SUBSCRIPTIONS) && (!empty($conf->product->enabled) || !empty($conf->service->enabled))) {
  1470. $idprodsubscription = $conf->global->ADHERENT_PRODUCT_ID_FOR_SUBSCRIPTIONS;
  1471. }
  1472. $vattouse = 0;
  1473. if (isset($conf->global->ADHERENT_VAT_FOR_SUBSCRIPTIONS) && $conf->global->ADHERENT_VAT_FOR_SUBSCRIPTIONS == 'defaultforfoundationcountry') {
  1474. $vattouse = get_default_tva($mysoc, $mysoc, $idprodsubscription);
  1475. }
  1476. //print xx".$vattouse." - ".$mysoc." - ".$customer;exit;
  1477. $result = $invoice->addline($label, 0, 1, $vattouse, 0, 0, $idprodsubscription, 0, $datesubscription, '', 0, 0, '', 'TTC', $amount, 1);
  1478. if ($result <= 0) {
  1479. $this->error = $invoice->error;
  1480. $this->errors = $invoice->errors;
  1481. $error++;
  1482. }
  1483. }
  1484. if (!$error) {
  1485. // Validate invoice
  1486. $result = $invoice->validate($user);
  1487. if ($result <= 0) {
  1488. $this->error = $invoice->error;
  1489. $this->errors = $invoice->errors;
  1490. $error++;
  1491. }
  1492. }
  1493. if (!$error) {
  1494. // TODO Link invoice with subscription ?
  1495. }
  1496. // Add payment onto invoice
  1497. if (!$error && $option == 'bankviainvoice' && $accountid) {
  1498. require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
  1499. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  1500. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions.lib.php';
  1501. $amounts = array();
  1502. $amounts[$invoice->id] = price2num($amount);
  1503. $paiement = new Paiement($this->db);
  1504. $paiement->datepaye = $paymentdate;
  1505. $paiement->amounts = $amounts;
  1506. $paiement->paiementid = dol_getIdFromCode($this->db, $operation, 'c_paiement', 'code', 'id', 1);
  1507. $paiement->num_payment = $num_chq;
  1508. $paiement->note_public = $label;
  1509. if (!$error) {
  1510. // Create payment line for invoice
  1511. $paiement_id = $paiement->create($user);
  1512. if (!$paiement_id > 0) {
  1513. $this->error = $paiement->error;
  1514. $this->errors = $paiement->errors;
  1515. $error++;
  1516. }
  1517. }
  1518. if (!$error) {
  1519. // Add transaction into bank account
  1520. $bank_line_id = $paiement->addPaymentToBank($user, 'payment', '(SubscriptionPayment)', $accountid, $emetteur_nom, $emetteur_banque);
  1521. if (!($bank_line_id > 0)) {
  1522. $this->error = $paiement->error;
  1523. $this->errors = $paiement->errors;
  1524. $error++;
  1525. }
  1526. }
  1527. if (!$error && !empty($bank_line_id)) {
  1528. // Update fk_bank into subscription table
  1529. $sql = 'UPDATE '.MAIN_DB_PREFIX.'subscription SET fk_bank='.$bank_line_id;
  1530. $sql .= ' WHERE rowid='.$subscriptionid;
  1531. $result = $this->db->query($sql);
  1532. if (!$result) {
  1533. $error++;
  1534. }
  1535. }
  1536. if (!$error) {
  1537. // Set invoice as paid
  1538. $invoice->setPaid($user);
  1539. }
  1540. }
  1541. if (!$error) {
  1542. // Define output language
  1543. $outputlangs = $langs;
  1544. $newlang = '';
  1545. $lang_id = GETPOST('lang_id');
  1546. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && !empty($lang_id)) {
  1547. $newlang = $lang_id;
  1548. }
  1549. if ($conf->global->MAIN_MULTILANGS && empty($newlang)) {
  1550. $newlang = $customer->default_lang;
  1551. }
  1552. if (!empty($newlang)) {
  1553. $outputlangs = new Translate("", $conf);
  1554. $outputlangs->setDefaultLang($newlang);
  1555. }
  1556. // Generate PDF (whatever is option MAIN_DISABLE_PDF_AUTOUPDATE) so we can include it into email
  1557. //if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
  1558. $invoice->generateDocument($invoice->model_pdf, $outputlangs);
  1559. }
  1560. }
  1561. if ($error) {
  1562. return -1;
  1563. } else {
  1564. return 1;
  1565. }
  1566. }
  1567. /**
  1568. * Function that validate a member
  1569. *
  1570. * @param User $user user adherent qui valide
  1571. * @return int <0 if KO, 0 if nothing done, >0 if OK
  1572. */
  1573. public function validate($user)
  1574. {
  1575. global $langs, $conf;
  1576. $error = 0;
  1577. $now = dol_now();
  1578. // Check parameters
  1579. if ($this->statut == 1) {
  1580. dol_syslog(get_class($this)."::validate statut of member does not allow this", LOG_WARNING);
  1581. return 0;
  1582. }
  1583. $this->db->begin();
  1584. $sql = "UPDATE ".MAIN_DB_PREFIX."adherent SET";
  1585. $sql .= " statut = 1";
  1586. $sql .= ", datevalid = '".$this->db->idate($now)."'";
  1587. $sql .= ", fk_user_valid=".$user->id;
  1588. $sql .= " WHERE rowid = ".$this->id;
  1589. dol_syslog(get_class($this)."::validate", LOG_DEBUG);
  1590. $result = $this->db->query($sql);
  1591. if ($result) {
  1592. $this->statut = 1;
  1593. // Call trigger
  1594. $result = $this->call_trigger('MEMBER_VALIDATE', $user);
  1595. if ($result < 0) {
  1596. $error++;
  1597. $this->db->rollback();
  1598. return -1;
  1599. }
  1600. // End call triggers
  1601. $this->datevalid = $now;
  1602. $this->db->commit();
  1603. return 1;
  1604. } else {
  1605. $this->error = $this->db->error();
  1606. $this->db->rollback();
  1607. return -1;
  1608. }
  1609. }
  1610. /**
  1611. * Fonction qui resilie un adherent
  1612. *
  1613. * @param User $user User making change
  1614. * @return int <0 if KO, >0 if OK
  1615. */
  1616. public function resiliate($user)
  1617. {
  1618. global $langs, $conf;
  1619. $error = 0;
  1620. // Check parameters
  1621. if ($this->statut == 0) {
  1622. dol_syslog(get_class($this)."::resiliate statut of member does not allow this", LOG_WARNING);
  1623. return 0;
  1624. }
  1625. $this->db->begin();
  1626. $sql = "UPDATE ".MAIN_DB_PREFIX."adherent SET";
  1627. $sql .= " statut = 0";
  1628. $sql .= ", fk_user_valid=".$user->id;
  1629. $sql .= " WHERE rowid = ".$this->id;
  1630. $result = $this->db->query($sql);
  1631. if ($result) {
  1632. $this->statut = 0;
  1633. // Call trigger
  1634. $result = $this->call_trigger('MEMBER_RESILIATE', $user);
  1635. if ($result < 0) {
  1636. $error++;
  1637. $this->db->rollback();
  1638. return -1;
  1639. }
  1640. // End call triggers
  1641. $this->db->commit();
  1642. return 1;
  1643. } else {
  1644. $this->error = $this->db->error();
  1645. $this->db->rollback();
  1646. return -1;
  1647. }
  1648. }
  1649. /**
  1650. * Functiun to exlude (set adherent.status to -2) a member
  1651. * TODO
  1652. * A private note should be added to know why the member has been excluded
  1653. * For historical purpose it add an "extra-subscription" type excluded
  1654. *
  1655. * @param User $user User making change
  1656. * @return int <0 if KO, >0 if OK
  1657. */
  1658. public function exclude($user)
  1659. {
  1660. global $langs, $conf;
  1661. $error = 0;
  1662. // Check parameters
  1663. if ($this->statut == 0) {
  1664. dol_syslog(get_class($this)."::resiliate statut of member does not allow this", LOG_WARNING);
  1665. return 0;
  1666. }
  1667. $this->db->begin();
  1668. $sql = "UPDATE ".MAIN_DB_PREFIX."adherent SET";
  1669. $sql .= " statut = -2";
  1670. $sql .= ", fk_user_valid=".$user->id;
  1671. $sql .= " WHERE rowid = ".$this->id;
  1672. $result = $this->db->query($sql);
  1673. if ($result) {
  1674. $this->statut = 0;
  1675. // Call trigger
  1676. $result = $this->call_trigger('MEMBER_EXCLUDE', $user);
  1677. if ($result < 0) {
  1678. $error++;
  1679. $this->db->rollback();
  1680. return -1;
  1681. }
  1682. // End call triggers
  1683. $this->db->commit();
  1684. return 1;
  1685. } else {
  1686. $this->error = $this->db->error();
  1687. $this->db->rollback();
  1688. return -1;
  1689. }
  1690. }
  1691. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1692. /**
  1693. * Function to add member into external tools mailing-list, spip, etc.
  1694. *
  1695. * @return int <0 if KO, >0 if OK
  1696. */
  1697. public function add_to_abo()
  1698. {
  1699. // phpcs:enable
  1700. global $conf, $langs;
  1701. include_once DOL_DOCUMENT_ROOT.'/mailmanspip/class/mailmanspip.class.php';
  1702. $mailmanspip = new MailmanSpip($this->db);
  1703. $err = 0;
  1704. // mailman
  1705. if (!empty($conf->global->ADHERENT_USE_MAILMAN) && !empty($conf->mailmanspip->enabled)) {
  1706. $result = $mailmanspip->add_to_mailman($this);
  1707. if ($result < 0) {
  1708. if (!empty($mailmanspip->error)) {
  1709. $this->errors[] = $mailmanspip->error;
  1710. }
  1711. $err += 1;
  1712. }
  1713. foreach ($mailmanspip->mladded_ko as $tmplist => $tmpemail) {
  1714. $langs->load("errors");
  1715. $this->errors[] = $langs->trans("ErrorFailedToAddToMailmanList", $tmpemail, $tmplist);
  1716. }
  1717. foreach ($mailmanspip->mladded_ok as $tmplist => $tmpemail) {
  1718. $langs->load("mailmanspip");
  1719. $this->mesgs[] = $langs->trans("SuccessToAddToMailmanList", $tmpemail, $tmplist);
  1720. }
  1721. }
  1722. // spip
  1723. if (!empty($conf->global->ADHERENT_USE_SPIP) && !empty($conf->mailmanspip->enabled)) {
  1724. $result = $mailmanspip->add_to_spip($this);
  1725. if ($result < 0) {
  1726. $this->errors[] = $mailmanspip->error;
  1727. $err += 1;
  1728. }
  1729. }
  1730. if ($err) {
  1731. return -$err;
  1732. } else {
  1733. return 1;
  1734. }
  1735. }
  1736. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1737. /**
  1738. * Function to delete a member from external tools like mailing-list, spip, etc.
  1739. *
  1740. * @return int <0 if KO, >0 if OK
  1741. */
  1742. public function del_to_abo()
  1743. {
  1744. // phpcs:enable
  1745. global $conf, $langs;
  1746. include_once DOL_DOCUMENT_ROOT.'/mailmanspip/class/mailmanspip.class.php';
  1747. $mailmanspip = new MailmanSpip($this->db);
  1748. $err = 0;
  1749. // mailman
  1750. if (!empty($conf->global->ADHERENT_USE_MAILMAN)) {
  1751. $result = $mailmanspip->del_to_mailman($this);
  1752. if ($result < 0) {
  1753. if (!empty($mailmanspip->error)) {
  1754. $this->errors[] = $mailmanspip->error;
  1755. }
  1756. $err += 1;
  1757. }
  1758. foreach ($mailmanspip->mlremoved_ko as $tmplist => $tmpemail) {
  1759. $langs->load("errors");
  1760. $this->errors[] = $langs->trans("ErrorFailedToRemoveToMailmanList", $tmpemail, $tmplist);
  1761. }
  1762. foreach ($mailmanspip->mlremoved_ok as $tmplist => $tmpemail) {
  1763. $langs->load("mailmanspip");
  1764. $this->mesgs[] = $langs->trans("SuccessToRemoveToMailmanList", $tmpemail, $tmplist);
  1765. }
  1766. }
  1767. if ($conf->global->ADHERENT_USE_SPIP && !empty($conf->mailmanspip->enabled)) {
  1768. $result = $mailmanspip->del_to_spip($this);
  1769. if ($result < 0) {
  1770. $this->errors[] = $mailmanspip->error;
  1771. $err += 1;
  1772. }
  1773. }
  1774. if ($err) {
  1775. // error
  1776. return -$err;
  1777. } else {
  1778. return 1;
  1779. }
  1780. }
  1781. /**
  1782. * Return civility label of a member
  1783. *
  1784. * @return string Translated name of civility (translated with transnoentitiesnoconv)
  1785. */
  1786. public function getCivilityLabel()
  1787. {
  1788. global $langs;
  1789. $langs->load("dict");
  1790. $code = (empty($this->civility_id) ? '' : $this->civility_id);
  1791. if (empty($code)) {
  1792. return '';
  1793. }
  1794. return $langs->getLabelFromKey($this->db, "Civility".$code, "c_civility", "code", "label", $code);
  1795. }
  1796. /**
  1797. * Return clicable name (with picto eventually)
  1798. *
  1799. * @param int $withpictoimg 0=No picto, 1=Include picto into link, 2=Only picto, -1=Include photo into link, -2=Only picto photo, -3=Only photo very small)
  1800. * @param int $maxlen length max label
  1801. * @param string $option Page for link ('card', 'category', 'subscription', ...)
  1802. * @param string $mode ''=Show firstname+lastname as label (using default order), 'firstname'=Show only firstname, 'lastname'=Show only lastname, 'login'=Show login, 'ref'=Show ref
  1803. * @param string $morecss Add more css on link
  1804. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  1805. * @param int $notooltip 1=Disable tooltip
  1806. * @param int $addlinktonotes 1=Add link to notes
  1807. * @return string Chaine avec URL
  1808. */
  1809. public function getNomUrl($withpictoimg = 0, $maxlen = 0, $option = 'card', $mode = '', $morecss = '', $save_lastsearch_value = -1, $notooltip = 0, $addlinktonotes = 0)
  1810. {
  1811. global $conf, $langs;
  1812. if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER) && $withpictoimg) {
  1813. $withpictoimg = 0;
  1814. }
  1815. $result = '';
  1816. $label = '';
  1817. $linkstart = '';
  1818. $linkend = '';
  1819. if (!empty($this->photo)) {
  1820. $label .= '<div class="photointooltip">';
  1821. $label .= Form::showphoto('memberphoto', $this, 80, 0, 0, 'photowithmargin photologintooltip', 'small', 0, 1);
  1822. $label .= '</div><div style="clear: both;"></div>';
  1823. }
  1824. $label .= '<div class="centpercent">';
  1825. $label .= img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Member").'</u>';
  1826. $label .= ' '.$this->getLibStatut(4);
  1827. if (!empty($this->ref)) {
  1828. $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
  1829. }
  1830. if (!empty($this->firstname) || !empty($this->lastname)) {
  1831. $label .= '<br><b>'.$langs->trans('Name').':</b> '.$this->getFullName($langs);
  1832. }
  1833. if (!empty($this->company)) {
  1834. $label .= '<br><b>'.$langs->trans('Company').':</b> '.$this->company;
  1835. }
  1836. $label .= '</div>';
  1837. $url = DOL_URL_ROOT.'/adherents/card.php?rowid='.((int) $this->id);
  1838. if ($option == 'subscription') {
  1839. $url = DOL_URL_ROOT.'/adherents/subscription.php?rowid='.((int) $this->id);
  1840. }
  1841. if ($option != 'nolink') {
  1842. // Add param to save lastsearch_values or not
  1843. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  1844. if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
  1845. $add_save_lastsearch_values = 1;
  1846. }
  1847. if ($add_save_lastsearch_values) {
  1848. $url .= '&save_lastsearch_values=1';
  1849. }
  1850. }
  1851. $linkstart .= '<a href="'.$url.'"';
  1852. $linkclose = "";
  1853. if (empty($notooltip)) {
  1854. if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
  1855. $langs->load("users");
  1856. $label = $langs->trans("ShowUser");
  1857. $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
  1858. }
  1859. $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
  1860. $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
  1861. }
  1862. $linkstart .= $linkclose.'>';
  1863. $linkend = '</a>';
  1864. $result .= $linkstart;
  1865. if ($withpictoimg) {
  1866. $result .= '<div class="inline-block nopadding valignmiddle">';
  1867. }
  1868. if ($withpictoimg) {
  1869. $paddafterimage = '';
  1870. if (abs($withpictoimg) == 1) {
  1871. $paddafterimage = 'style="margin-right: 3px;"';
  1872. }
  1873. // Only picto
  1874. if ($withpictoimg > 0) {
  1875. $picto = '<span class="nopadding'.($morecss ? ' userimg'.$morecss : '').'">'.
  1876. img_object('', 'user', $paddafterimage.' '.($notooltip ? '' : 'class="classfortooltip"'), 0, 0, $notooltip ? 0 : 1).'</span>';
  1877. } else {
  1878. // Picto must be a photo
  1879. $picto = '<span class="nopadding'.($morecss ? ' userimg'.$morecss : '').'"'.($paddafterimage ? ' '.$paddafterimage : '').'>';
  1880. $picto .= Form::showphoto('memberphoto', $this, 0, 0, 0, 'userphoto'.($withpictoimg == -3 ? 'small' : ''), 'mini', 0, 1);
  1881. $picto .= '</span>';
  1882. }
  1883. $result .= $picto;
  1884. }
  1885. if ($withpictoimg > -2 && $withpictoimg != 2) {
  1886. if (empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
  1887. $result .= '<span class="nopadding valignmiddle'.((!isset($this->statut) || $this->statut) ? '' : ' strikefordisabled').
  1888. ($morecss ? ' usertext'.$morecss : '').'">';
  1889. }
  1890. if ($mode == 'login') {
  1891. $result .= dol_trunc($this->login, $maxlen);
  1892. } elseif ($mode == 'ref') {
  1893. $result .= $this->ref;
  1894. } else {
  1895. $result .= $this->getFullName($langs, '', ($mode == 'firstname' ? 2 : ($mode == 'lastname' ? 4 : -1)), $maxlen);
  1896. }
  1897. if (empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
  1898. $result .= '</span>';
  1899. }
  1900. }
  1901. if ($withpictoimg) {
  1902. $result .= '</div>';
  1903. }
  1904. $result .= $linkend;
  1905. if ($addlinktonotes) {
  1906. if ($this->note_private) {
  1907. $notetoshow = $langs->trans("ViewPrivateNote").':<br>'.dol_string_nohtmltag($this->note_private, 1);
  1908. $result .= ' <span class="note inline-block">';
  1909. $result .= '<a href="'.DOL_URL_ROOT.'/adherents/note.php?id='.$this->id.'" class="classfortooltip" title="'.dol_escape_htmltag($notetoshow).'">';
  1910. $result .= img_picto('', 'note');
  1911. $result .= '</a>';
  1912. $result .= '</span>';
  1913. }
  1914. }
  1915. return $result;
  1916. }
  1917. /**
  1918. * Retourne le libelle du statut d'un adherent (brouillon, valide, resilie, exclu)
  1919. *
  1920. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  1921. * @return string Label
  1922. */
  1923. public function getLibStatut($mode = 0)
  1924. {
  1925. return $this->LibStatut($this->statut, $this->need_subscription, $this->datefin, $mode);
  1926. }
  1927. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1928. /**
  1929. * Renvoi le libelle d'un statut donne
  1930. *
  1931. * @param int $status Id status
  1932. * @param int $need_subscription 1 if member type need subscription, 0 otherwise
  1933. * @param int $date_end_subscription Date fin adhesion
  1934. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto
  1935. * @return string Label
  1936. */
  1937. public function LibStatut($status, $need_subscription, $date_end_subscription, $mode = 0)
  1938. {
  1939. // phpcs:enable
  1940. global $langs;
  1941. $langs->load("members");
  1942. $statusType = '';
  1943. $labelStatus = '';
  1944. $labelStatusShort = '';
  1945. if ($status == -1) {
  1946. $statusType = 'status0';
  1947. $labelStatus = $langs->trans("MemberStatusDraft");
  1948. $labelStatusShort = $langs->trans("MemberStatusDraftShort");
  1949. } elseif ($status >= 1) {
  1950. if ($need_subscription == 0) {
  1951. $statusType = 'status4';
  1952. $labelStatus = $langs->trans("MemberStatusNoSubscription");
  1953. $labelStatusShort = $langs->trans("MemberStatusNoSubscriptionShort");
  1954. } elseif (!$date_end_subscription) {
  1955. $statusType = 'status1';
  1956. $labelStatus = $langs->trans("MemberStatusActive");
  1957. $labelStatusShort = $langs->trans("MemberStatusActiveShort");
  1958. } elseif ($date_end_subscription < dol_now()) {
  1959. $statusType = 'status3';
  1960. $labelStatus = $langs->trans("MemberStatusActiveLate");
  1961. $labelStatusShort = $langs->trans("MemberStatusActiveLateShort");
  1962. } else {
  1963. $statusType = 'status4';
  1964. $labelStatus = $langs->trans("MemberStatusPaid");
  1965. $labelStatusShort = $langs->trans("MemberStatusPaidShort");
  1966. }
  1967. } elseif ($status == 0) {
  1968. $statusType = 'status6';
  1969. $labelStatus = $langs->trans("MemberStatusResiliated");
  1970. $labelStatusShort = $langs->trans("MemberStatusResiliatedShort");
  1971. } elseif ($status == -2) {
  1972. $statusType = 'status10';
  1973. $labelStatus = $langs->trans("MemberStatusExcluded");
  1974. $labelStatusShort = $langs->trans("MemberStatusExcludedShort");
  1975. }
  1976. return dolGetStatus($labelStatus, $labelStatusShort, '', $statusType, $mode);
  1977. }
  1978. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1979. /**
  1980. * Charge indicateurs this->nb de tableau de bord
  1981. *
  1982. * @return int <0 if KO, >0 if OK
  1983. */
  1984. public function load_state_board()
  1985. {
  1986. // phpcs:enable
  1987. global $conf;
  1988. $this->nb = array();
  1989. $sql = "SELECT count(a.rowid) as nb";
  1990. $sql .= " FROM ".MAIN_DB_PREFIX."adherent as a";
  1991. $sql .= " WHERE a.statut > 0";
  1992. $sql .= " AND a.entity IN (".getEntity('adherent').")";
  1993. $resql = $this->db->query($sql);
  1994. if ($resql) {
  1995. while ($obj = $this->db->fetch_object($resql)) {
  1996. $this->nb["members"] = $obj->nb;
  1997. }
  1998. $this->db->free($resql);
  1999. return 1;
  2000. } else {
  2001. dol_print_error($this->db);
  2002. $this->error = $this->db->error();
  2003. return -1;
  2004. }
  2005. }
  2006. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2007. /**
  2008. * Load indicators for dashboard (this->nbtodo and this->nbtodolate)
  2009. *
  2010. * @param User $user Objet user
  2011. * @param string $mode "expired" for membership to renew, "shift" for member to validate
  2012. * @return WorkboardResponse|int <0 if KO, WorkboardResponse if OK
  2013. */
  2014. public function load_board($user, $mode)
  2015. {
  2016. // phpcs:enable
  2017. global $conf, $langs;
  2018. if ($user->socid) {
  2019. return -1; // protection pour eviter appel par utilisateur externe
  2020. }
  2021. $now = dol_now();
  2022. $sql = "SELECT a.rowid, a.datefin, a.statut";
  2023. $sql .= " FROM ".MAIN_DB_PREFIX."adherent as a";
  2024. $sql .= ", ".MAIN_DB_PREFIX."adherent_type as t";
  2025. $sql .= " WHERE a.fk_adherent_type = t.rowid";
  2026. if ($mode == 'expired') {
  2027. $sql .= " AND a.statut = 1";
  2028. $sql .= " AND a.entity IN (".getEntity('adherent').")";
  2029. $sql .= " AND ((a.datefin IS NULL or a.datefin < '".$this->db->idate($now)."') AND t.subscription = '1')";
  2030. } elseif ($mode == 'shift') {
  2031. $sql .= " AND a.statut = -1";
  2032. $sql .= " AND a.entity IN (".getEntity('adherent').")";
  2033. }
  2034. $resql = $this->db->query($sql);
  2035. if ($resql) {
  2036. $langs->load("members");
  2037. $warning_delay = 0;
  2038. $url = '';
  2039. $label = '';
  2040. $labelShort = '';
  2041. if ($mode == 'expired') {
  2042. $warning_delay = $conf->adherent->subscription->warning_delay / 60 / 60 / 24;
  2043. $label = $langs->trans("MembersWithSubscriptionToReceive");
  2044. $labelShort = $langs->trans("MembersWithSubscriptionToReceiveShort");
  2045. $url = DOL_URL_ROOT.'/adherents/list.php?mainmenu=members&amp;statut=1&amp;filter=outofdate';
  2046. } elseif ($mode == 'shift') {
  2047. $warning_delay = $conf->adherent->subscription->warning_delay / 60 / 60 / 24;
  2048. $url = DOL_URL_ROOT.'/adherents/list.php?mainmenu=members&amp;statut=-1';
  2049. $label = $langs->trans("MembersListToValid");
  2050. $labelShort = $langs->trans("ToValidate");
  2051. }
  2052. $response = new WorkboardResponse();
  2053. $response->warning_delay = $warning_delay;
  2054. $response->label = $label;
  2055. $response->labelShort = $labelShort;
  2056. $response->url = $url;
  2057. $response->img = img_object('', "user");
  2058. $adherentstatic = new Adherent($this->db);
  2059. while ($obj = $this->db->fetch_object($resql)) {
  2060. $response->nbtodo++;
  2061. $adherentstatic->datefin = $this->db->jdate($obj->datefin);
  2062. $adherentstatic->statut = $obj->statut;
  2063. if ($adherentstatic->hasDelay()) {
  2064. $response->nbtodolate++;
  2065. }
  2066. }
  2067. return $response;
  2068. } else {
  2069. dol_print_error($this->db);
  2070. $this->error = $this->db->error();
  2071. return -1;
  2072. }
  2073. }
  2074. /**
  2075. * Create a document onto disk according to template module.
  2076. *
  2077. * @param string $modele Force template to use ('' to not force)
  2078. * @param Translate $outputlangs objet lang a utiliser pour traduction
  2079. * @param int $hidedetails Hide details of lines
  2080. * @param int $hidedesc Hide description
  2081. * @param int $hideref Hide ref
  2082. * @param null|array $moreparams Array to provide more information
  2083. * @return int 0 if KO, 1 if OK
  2084. */
  2085. public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams = null)
  2086. {
  2087. global $conf, $langs;
  2088. $langs->load("orders");
  2089. if (!dol_strlen($modele)) {
  2090. $modele = 'standard';
  2091. if ($this->model_pdf) {
  2092. $modele = $this->model_pdf;
  2093. } elseif (!empty($conf->global->ADHERENT_ADDON_PDF)) {
  2094. $modele = $conf->global->ADHERENT_ADDON_PDF;
  2095. }
  2096. }
  2097. $modelpath = "core/modules/member/doc/";
  2098. return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
  2099. }
  2100. /**
  2101. * Initialise an instance with random values.
  2102. * Used to build previews or test instances.
  2103. * id must be 0 if object instance is a specimen.
  2104. *
  2105. * @return int
  2106. */
  2107. public function initAsSpecimen()
  2108. {
  2109. global $user, $langs;
  2110. $now = dol_now();
  2111. // Initialise parametres
  2112. $this->id = 0;
  2113. $this->ref = 'ABC001';
  2114. $this->entity = 1;
  2115. $this->specimen = 1;
  2116. $this->civility_id = 0;
  2117. $this->lastname = 'DOLIBARR';
  2118. $this->firstname = 'SPECIMEN';
  2119. $this->gender = 'man';
  2120. $this->login = 'dolibspec';
  2121. $this->pass = 'dolibspec';
  2122. $this->company = 'Societe ABC';
  2123. $this->address = '61 jump street';
  2124. $this->zip = '75000';
  2125. $this->town = 'Paris';
  2126. $this->country_id = 1;
  2127. $this->country_code = 'FR';
  2128. $this->country = 'France';
  2129. $this->morphy = 'mor';
  2130. $this->email = 'specimen@specimen.com';
  2131. $this->socialnetworks = array(
  2132. 'skype' => 'skypepseudo',
  2133. 'twitter' => 'twitterpseudo',
  2134. 'facebook' => 'facebookpseudo',
  2135. 'linkedin' => 'linkedinpseudo',
  2136. );
  2137. $this->phone = '0999999999';
  2138. $this->phone_perso = '0999999998';
  2139. $this->phone_mobile = '0999999997';
  2140. $this->note_public = 'This is a public note';
  2141. $this->note_private = 'This is a private note';
  2142. $this->birth = $now;
  2143. $this->photo = '';
  2144. $this->public = 1;
  2145. $this->statut = 0;
  2146. $this->datefin = $now;
  2147. $this->datevalid = $now;
  2148. $this->typeid = 1; // Id type adherent
  2149. $this->type = 'Type adherent'; // Libelle type adherent
  2150. $this->need_subscription = 0;
  2151. $this->first_subscription_date = $now;
  2152. $this->first_subscription_date_start = $this->first_subscription_date;
  2153. $this->first_subscription_date_end = dol_time_plus_duree($this->first_subscription_date_start, 1, 'y');
  2154. $this->first_subscription_amount = 10;
  2155. $this->last_subscription_date = $this->first_subscription_date;
  2156. $this->last_subscription_date_start = $this->first_subscription_date;
  2157. $this->last_subscription_date_end = dol_time_plus_duree($this->last_subscription_date_start, 1, 'y');
  2158. $this->last_subscription_amount = 10;
  2159. return 1;
  2160. }
  2161. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2162. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  2163. /**
  2164. * Retourne chaine DN complete dans l'annuaire LDAP pour l'objet
  2165. *
  2166. * @param array $info Info array loaded by _load_ldap_info
  2167. * @param int $mode 0=Return full DN (uid=qqq,ou=xxx,dc=aaa,dc=bbb)
  2168. * 1=Return DN without key inside (ou=xxx,dc=aaa,dc=bbb)
  2169. * 2=Return key only (uid=qqq)
  2170. * @return string DN
  2171. */
  2172. public function _load_ldap_dn($info, $mode = 0)
  2173. {
  2174. // phpcs:enable
  2175. global $conf;
  2176. $dn = '';
  2177. if ($mode == 0) {
  2178. $dn = $conf->global->LDAP_KEY_MEMBERS."=".$info[$conf->global->LDAP_KEY_MEMBERS].",".$conf->global->LDAP_MEMBER_DN;
  2179. }
  2180. if ($mode == 1) {
  2181. $dn = $conf->global->LDAP_MEMBER_DN;
  2182. }
  2183. if ($mode == 2) {
  2184. $dn = $conf->global->LDAP_KEY_MEMBERS."=".$info[$conf->global->LDAP_KEY_MEMBERS];
  2185. }
  2186. return $dn;
  2187. }
  2188. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2189. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  2190. /**
  2191. * Initialise tableau info (tableau des attributs LDAP)
  2192. *
  2193. * @return array Tableau info des attributs
  2194. */
  2195. public function _load_ldap_info()
  2196. {
  2197. // phpcs:enable
  2198. global $conf, $langs;
  2199. $info = array();
  2200. $socialnetworks = getArrayOfSocialNetworks();
  2201. $keymodified = false;
  2202. // Object classes
  2203. $info["objectclass"] = explode(',', $conf->global->LDAP_MEMBER_OBJECT_CLASS);
  2204. $this->fullname = $this->getFullName($langs);
  2205. // For avoid ldap error when firstname and lastname are empty
  2206. if ($this->morphy == 'mor' && (empty($this->fullname) || $this->fullname == $this->company)) {
  2207. $this->fullname = $this->company;
  2208. $this->lastname = $this->company;
  2209. }
  2210. // Possible LDAP KEY (constname => varname)
  2211. $ldapkey = array(
  2212. 'LDAP_MEMBER_FIELD_FULLNAME' => 'fullname',
  2213. 'LDAP_MEMBER_FIELD_NAME' => 'lastname',
  2214. 'LDAP_MEMBER_FIELD_LOGIN' => 'login',
  2215. 'LDAP_MEMBER_FIELD_LOGIN_SAMBA' => 'login',
  2216. 'LDAP_MEMBER_FIELD_MAIL' => 'email'
  2217. );
  2218. // Member
  2219. foreach ($ldapkey as $constname => $varname) {
  2220. if (!empty($this->$varname) && !empty($conf->global->$constname)) {
  2221. $info[$conf->global->$constname] = $this->$varname;
  2222. // Check if it is the LDAP key and if its value has been changed
  2223. if (!empty($conf->global->LDAP_KEY_MEMBERS) && $conf->global->LDAP_KEY_MEMBERS == $conf->global->$constname) {
  2224. if (!empty($this->oldcopy) && $this->$varname != $this->oldcopy->$varname) {
  2225. $keymodified = true; // For check if LDAP key has been modified
  2226. }
  2227. }
  2228. }
  2229. }
  2230. if ($this->firstname && !empty($conf->global->LDAP_MEMBER_FIELD_FIRSTNAME)) {
  2231. $info[$conf->global->LDAP_MEMBER_FIELD_FIRSTNAME] = $this->firstname;
  2232. }
  2233. if ($this->poste && !empty($conf->global->LDAP_MEMBER_FIELD_TITLE)) {
  2234. $info[$conf->global->LDAP_MEMBER_FIELD_TITLE] = $this->poste;
  2235. }
  2236. if ($this->company && !empty($conf->global->LDAP_MEMBER_FIELD_COMPANY)) {
  2237. $info[$conf->global->LDAP_MEMBER_FIELD_COMPANY] = $this->company;
  2238. }
  2239. if ($this->address && !empty($conf->global->LDAP_MEMBER_FIELD_ADDRESS)) {
  2240. $info[$conf->global->LDAP_MEMBER_FIELD_ADDRESS] = $this->address;
  2241. }
  2242. if ($this->zip && !empty($conf->global->LDAP_MEMBER_FIELD_ZIP)) {
  2243. $info[$conf->global->LDAP_MEMBER_FIELD_ZIP] = $this->zip;
  2244. }
  2245. if ($this->town && !empty($conf->global->LDAP_MEMBER_FIELD_TOWN)) {
  2246. $info[$conf->global->LDAP_MEMBER_FIELD_TOWN] = $this->town;
  2247. }
  2248. if ($this->country_code && !empty($conf->global->LDAP_MEMBER_FIELD_COUNTRY)) {
  2249. $info[$conf->global->LDAP_MEMBER_FIELD_COUNTRY] = $this->country_code;
  2250. }
  2251. foreach ($socialnetworks as $key => $value) {
  2252. if ($this->socialnetworks[$value['label']] && !empty($conf->global->{'LDAP_MEMBER_FIELD_'.strtoupper($value['label'])})) {
  2253. $info[$conf->global->{'LDAP_MEMBER_FIELD_'.strtoupper($value['label'])}] = $this->socialnetworks[$value['label']];
  2254. }
  2255. }
  2256. if ($this->phone && !empty($conf->global->LDAP_MEMBER_FIELD_PHONE)) {
  2257. $info[$conf->global->LDAP_MEMBER_FIELD_PHONE] = $this->phone;
  2258. }
  2259. if ($this->phone_perso && !empty($conf->global->LDAP_MEMBER_FIELD_PHONE_PERSO)) {
  2260. $info[$conf->global->LDAP_MEMBER_FIELD_PHONE_PERSO] = $this->phone_perso;
  2261. }
  2262. if ($this->phone_mobile && !empty($conf->global->LDAP_MEMBER_FIELD_MOBILE)) {
  2263. $info[$conf->global->LDAP_MEMBER_FIELD_MOBILE] = $this->phone_mobile;
  2264. }
  2265. if ($this->fax && !empty($conf->global->LDAP_MEMBER_FIELD_FAX)) {
  2266. $info[$conf->global->LDAP_MEMBER_FIELD_FAX] = $this->fax;
  2267. }
  2268. if ($this->note_private && !empty($conf->global->LDAP_MEMBER_FIELD_DESCRIPTION)) {
  2269. $info[$conf->global->LDAP_MEMBER_FIELD_DESCRIPTION] = dol_string_nohtmltag($this->note_private, 2);
  2270. }
  2271. if ($this->note_public && !empty($conf->global->LDAP_MEMBER_FIELD_NOTE_PUBLIC)) {
  2272. $info[$conf->global->LDAP_MEMBER_FIELD_NOTE_PUBLIC] = dol_string_nohtmltag($this->note_public, 2);
  2273. }
  2274. if ($this->birth && !empty($conf->global->LDAP_MEMBER_FIELD_BIRTHDATE)) {
  2275. $info[$conf->global->LDAP_MEMBER_FIELD_BIRTHDATE] = dol_print_date($this->birth, 'dayhourldap');
  2276. }
  2277. if (isset($this->statut) && !empty($conf->global->LDAP_FIELD_MEMBER_STATUS)) {
  2278. $info[$conf->global->LDAP_FIELD_MEMBER_STATUS] = $this->statut;
  2279. }
  2280. if ($this->datefin && !empty($conf->global->LDAP_FIELD_MEMBER_END_LASTSUBSCRIPTION)) {
  2281. $info[$conf->global->LDAP_FIELD_MEMBER_END_LASTSUBSCRIPTION] = dol_print_date($this->datefin, 'dayhourldap');
  2282. }
  2283. // When password is modified
  2284. if (!empty($this->pass)) {
  2285. if (!empty($conf->global->LDAP_MEMBER_FIELD_PASSWORD)) {
  2286. $info[$conf->global->LDAP_MEMBER_FIELD_PASSWORD] = $this->pass; // this->pass = mot de passe non crypte
  2287. }
  2288. if (!empty($conf->global->LDAP_MEMBER_FIELD_PASSWORD_CRYPTED)) {
  2289. $info[$conf->global->LDAP_MEMBER_FIELD_PASSWORD_CRYPTED] = dol_hash($this->pass, 4); // Create OpenLDAP MD5 password (TODO add type of encryption)
  2290. }
  2291. } elseif ($conf->global->LDAP_SERVER_PROTOCOLVERSION !== '3') {
  2292. // Set LDAP password if possible
  2293. // If ldap key is modified and LDAPv3 we use ldap_rename function for avoid lose encrypt password
  2294. if (!empty($conf->global->DATABASE_PWD_ENCRYPTED)) {
  2295. // Just for the default MD5 !
  2296. if (empty($conf->global->MAIN_SECURITY_HASH_ALGO)) {
  2297. if ($this->pass_indatabase_crypted && !empty($conf->global->LDAP_MEMBER_FIELD_PASSWORD_CRYPTED)) {
  2298. // Create OpenLDAP MD5 password from Dolibarr MD5 password
  2299. // Note: This suppose that "pass_indatabase_crypted" is a md5 (guaranted by the previous test if "(empty($conf->global->MAIN_SECURITY_HASH_ALGO))"
  2300. $info[$conf->global->LDAP_MEMBER_FIELD_PASSWORD_CRYPTED] = '{md5}'.base64_encode(hex2bin($this->pass_indatabase_crypted));
  2301. }
  2302. }
  2303. } elseif (!empty($this->pass_indatabase)) {
  2304. // Use $this->pass_indatabase value if exists
  2305. if (!empty($conf->global->LDAP_MEMBER_FIELD_PASSWORD)) {
  2306. $info[$conf->global->LDAP_MEMBER_FIELD_PASSWORD] = $this->pass_indatabase; // $this->pass_indatabase = mot de passe non crypte
  2307. }
  2308. if (!empty($conf->global->LDAP_MEMBER_FIELD_PASSWORD_CRYPTED)) {
  2309. $info[$conf->global->LDAP_MEMBER_FIELD_PASSWORD_CRYPTED] = dol_hash($this->pass_indatabase, 4); // md5 for OpenLdap TODO add type of encryption
  2310. }
  2311. }
  2312. }
  2313. // Subscriptions
  2314. if ($this->first_subscription_date && !empty($conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE)) {
  2315. $info[$conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE] = dol_print_date($this->first_subscription_date, 'dayhourldap');
  2316. }
  2317. if (isset($this->first_subscription_amount) && !empty($conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_AMOUNT)) {
  2318. $info[$conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_AMOUNT] = $this->first_subscription_amount;
  2319. }
  2320. if ($this->last_subscription_date && !empty($conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_DATE)) {
  2321. $info[$conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_DATE] = dol_print_date($this->last_subscription_date, 'dayhourldap');
  2322. }
  2323. if (isset($this->last_subscription_amount) && !empty($conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_AMOUNT)) {
  2324. $info[$conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_AMOUNT] = $this->last_subscription_amount;
  2325. }
  2326. return $info;
  2327. }
  2328. /**
  2329. * Load type info information in the member object
  2330. *
  2331. * @param int $id Id of member to load
  2332. * @return void
  2333. */
  2334. public function info($id)
  2335. {
  2336. $sql = 'SELECT a.rowid, a.datec as datec,';
  2337. $sql .= ' a.datevalid as datev,';
  2338. $sql .= ' a.tms as datem,';
  2339. $sql .= ' a.fk_user_author, a.fk_user_valid, a.fk_user_mod';
  2340. $sql .= ' FROM '.MAIN_DB_PREFIX.'adherent as a';
  2341. $sql .= ' WHERE a.rowid = '.((int) $id);
  2342. dol_syslog(get_class($this)."::info", LOG_DEBUG);
  2343. $result = $this->db->query($sql);
  2344. if ($result) {
  2345. if ($this->db->num_rows($result)) {
  2346. $obj = $this->db->fetch_object($result);
  2347. $this->id = $obj->rowid;
  2348. if ($obj->fk_user_author) {
  2349. $cuser = new User($this->db);
  2350. $cuser->fetch($obj->fk_user_author);
  2351. $this->user_creation = $cuser;
  2352. }
  2353. if ($obj->fk_user_valid) {
  2354. $vuser = new User($this->db);
  2355. $vuser->fetch($obj->fk_user_valid);
  2356. $this->user_validation = $vuser;
  2357. }
  2358. if ($obj->fk_user_mod) {
  2359. $muser = new User($this->db);
  2360. $muser->fetch($obj->fk_user_mod);
  2361. $this->user_modification = $muser;
  2362. }
  2363. $this->date_creation = $this->db->jdate($obj->datec);
  2364. $this->date_validation = $this->db->jdate($obj->datev);
  2365. $this->date_modification = $this->db->jdate($obj->datem);
  2366. }
  2367. $this->db->free($result);
  2368. } else {
  2369. dol_print_error($this->db);
  2370. }
  2371. }
  2372. /**
  2373. * Return number of mass Emailing received by this member with its email
  2374. *
  2375. * @return int Number of EMailings
  2376. */
  2377. public function getNbOfEMailings()
  2378. {
  2379. $sql = "SELECT count(mc.email) as nb";
  2380. $sql .= " FROM ".MAIN_DB_PREFIX."mailing_cibles as mc";
  2381. $sql .= " WHERE mc.email = '".$this->db->escape($this->email)."'";
  2382. $sql .= " AND mc.statut NOT IN (-1,0)"; // -1 erreur, 0 non envoye, 1 envoye avec succes
  2383. $resql = $this->db->query($sql);
  2384. if ($resql) {
  2385. $obj = $this->db->fetch_object($resql);
  2386. $nb = $obj->nb;
  2387. $this->db->free($resql);
  2388. return $nb;
  2389. } else {
  2390. $this->error = $this->db->error();
  2391. return -1;
  2392. }
  2393. }
  2394. /**
  2395. * Sets object to supplied categories.
  2396. *
  2397. * Deletes object from existing categories not supplied.
  2398. * Adds it to non existing supplied categories.
  2399. * Existing categories are left untouch.
  2400. *
  2401. * @param int[]|int $categories Category or categories IDs
  2402. * @return void
  2403. */
  2404. public function setCategories($categories)
  2405. {
  2406. require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
  2407. return parent::setCategoriesCommon($categories, Categorie::TYPE_MEMBER);
  2408. }
  2409. /**
  2410. * Function used to replace a thirdparty id with another one.
  2411. *
  2412. * @param DoliDB $db Database handler
  2413. * @param int $origin_id Old thirdparty id
  2414. * @param int $dest_id New thirdparty id
  2415. * @return bool
  2416. */
  2417. public static function replaceThirdparty($db, $origin_id, $dest_id)
  2418. {
  2419. $tables = array('adherent');
  2420. return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
  2421. }
  2422. /**
  2423. * Return if a member is late (subscription late) or not
  2424. *
  2425. * @return boolean True if late, False if not late
  2426. */
  2427. public function hasDelay()
  2428. {
  2429. global $conf;
  2430. //Only valid members
  2431. if ($this->statut <= 0) {
  2432. return false;
  2433. }
  2434. if (!$this->datefin) {
  2435. return false;
  2436. }
  2437. $now = dol_now();
  2438. return $this->datefin < ($now - $conf->adherent->subscription->warning_delay);
  2439. }
  2440. /**
  2441. * Send reminders by emails before subscription end
  2442. * CAN BE A CRON TASK
  2443. *
  2444. * @param string $daysbeforeendlist Nb of days before end of subscription (negative number = after subscription). Can be a list of delay, separated by a semicolon, for example '10;5;0;-5'
  2445. * @return int 0 if OK, <>0 if KO (this function is used also by cron so only 0 is OK)
  2446. */
  2447. public function sendReminderForExpiredSubscription($daysbeforeendlist = '10')
  2448. {
  2449. global $conf, $langs, $mysoc, $user;
  2450. $error = 0;
  2451. $this->output = '';
  2452. $this->error = '';
  2453. $blockingerrormsg = '';
  2454. if (empty($conf->adherent->enabled)) { // Should not happen. If module disabled, cron job should not be visible.
  2455. $langs->load("agenda");
  2456. $this->output = $langs->trans('ModuleNotEnabled', $langs->transnoentitiesnoconv("Adherent"));
  2457. return 0;
  2458. }
  2459. if (empty($conf->global->MEMBER_REMINDER_EMAIL)) {
  2460. $langs->load("agenda");
  2461. $this->output = $langs->trans('EventRemindersByEmailNotEnabled', $langs->transnoentitiesnoconv("Adherent"));
  2462. return 0;
  2463. }
  2464. $now = dol_now();
  2465. $nbok = 0;
  2466. $nbko = 0;
  2467. $listofmembersok = array();
  2468. $listofmembersko = array();
  2469. $arraydaysbeforeend = explode(';', $daysbeforeendlist);
  2470. foreach ($arraydaysbeforeend as $daysbeforeend) { // Loop on each delay
  2471. dol_syslog(__METHOD__.' - Process delta = '.$daysbeforeend, LOG_DEBUG);
  2472. if (!is_numeric($daysbeforeend)) {
  2473. $blockingerrormsg = "Value for delta is not a positive or negative numeric";
  2474. $nbko++;
  2475. break;
  2476. }
  2477. $tmp = dol_getdate($now);
  2478. $datetosearchfor = dol_time_plus_duree(dol_mktime(0, 0, 0, $tmp['mon'], $tmp['mday'], $tmp['year']), $daysbeforeend, 'd');
  2479. $sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.'adherent';
  2480. $sql .= " WHERE entity = ".$conf->entity; // Do not use getEntity('adherent').")" here, we want the batch to be on its entity only;
  2481. $sql .= " AND datefin = '".$this->db->idate($datetosearchfor)."'";
  2482. $resql = $this->db->query($sql);
  2483. if ($resql) {
  2484. $num_rows = $this->db->num_rows($resql);
  2485. include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
  2486. $adherent = new Adherent($this->db);
  2487. $formmail = new FormMail($this->db);
  2488. $i = 0;
  2489. while ($i < $num_rows) {
  2490. $obj = $this->db->fetch_object($resql);
  2491. $adherent->fetch($obj->rowid, '', '', '', true, true);
  2492. if (empty($adherent->email)) {
  2493. $nbko++;
  2494. $listofmembersko[$adherent->id] = $adherent->id;
  2495. } else {
  2496. $adherent->fetch_thirdparty();
  2497. // Language code to use ($languagecodeformember) is default language of thirdparty, if no thirdparty, the language found from country of member then country of thirdparty, and if still not found we use the language of company.
  2498. $languagefromcountrycode = getLanguageCodeFromCountryCode($adherent->country_code ? $adherent->country_code : $adherent->thirdparty->country_code);
  2499. $languagecodeformember = (empty($adherent->thirdparty->default_lang) ? ($languagefromcountrycode ? $languagefromcountrycode : $mysoc->default_lang) : $adherent->thirdparty->default_lang);
  2500. // Send reminder email
  2501. $outputlangs = new Translate('', $conf);
  2502. $outputlangs->setDefaultLang($languagecodeformember);
  2503. $outputlangs->loadLangs(array("main", "members"));
  2504. dol_syslog("sendReminderForExpiredSubscription Language for member id ".$adherent->id." set to ".$outputlangs->defaultlang." mysoc->default_lang=".$mysoc->default_lang);
  2505. $arraydefaultmessage = null;
  2506. $labeltouse = $conf->global->ADHERENT_EMAIL_TEMPLATE_REMIND_EXPIRATION;
  2507. if (!empty($labeltouse)) {
  2508. $arraydefaultmessage = $formmail->getEMailTemplate($this->db, 'member', $user, $outputlangs, 0, 1, $labeltouse);
  2509. }
  2510. if (!empty($labeltouse) && is_object($arraydefaultmessage) && $arraydefaultmessage->id > 0) {
  2511. $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, null, $adherent);
  2512. //if (is_array($adherent->thirdparty)) $substitutionarraycomp = ...
  2513. complete_substitutions_array($substitutionarray, $outputlangs, $adherent);
  2514. $subject = make_substitutions($arraydefaultmessage->topic, $substitutionarray, $outputlangs);
  2515. $msg = make_substitutions($arraydefaultmessage->content, $substitutionarray, $outputlangs);
  2516. $from = $conf->global->ADHERENT_MAIL_FROM;
  2517. $to = $adherent->email;
  2518. $trackid = 'mem'.$adherent->id;
  2519. $moreinheader = 'X-Dolibarr-Info: sendReminderForExpiredSubscription'."\r\n";
  2520. include_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
  2521. $cmail = new CMailFile($subject, $to, $from, $msg, array(), array(), array(), '', '', 0, 1, '', '', $trackid, $moreinheader);
  2522. $result = $cmail->sendfile();
  2523. if (!$result) {
  2524. $error++;
  2525. $this->error = $cmail->error;
  2526. if (!is_null($cmail->errors)) {
  2527. $this->errors += $cmail->errors;
  2528. }
  2529. $nbko++;
  2530. $listofmembersko[$adherent->id] = $adherent->id;
  2531. } else {
  2532. $nbok++;
  2533. $listofmembersok[$adherent->id] = $adherent->id;
  2534. $message = $msg;
  2535. $sendto = $to;
  2536. $sendtocc = '';
  2537. $sendtobcc = '';
  2538. $actioncode = 'EMAIL';
  2539. $extraparams = '';
  2540. $actionmsg = '';
  2541. $actionmsg2 = $langs->transnoentities('MailSentBy').' '.CMailFile::getValidAddress($from, 4, 0, 1).' '.$langs->transnoentities('To').' '.
  2542. CMailFile::getValidAddress($sendto, 4, 0, 1);
  2543. if ($message) {
  2544. $actionmsg = $langs->transnoentities('MailFrom').': '.dol_escape_htmltag($from);
  2545. $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('MailTo').': '.dol_escape_htmltag($sendto));
  2546. if ($sendtocc) {
  2547. $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('Bcc').": ".dol_escape_htmltag($sendtocc));
  2548. }
  2549. $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('MailTopic').": ".$subject);
  2550. $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('TextUsedInTheMessageBody').":");
  2551. $actionmsg = dol_concatdesc($actionmsg, $message);
  2552. }
  2553. require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
  2554. // Insert record of emails sent
  2555. $actioncomm = new ActionComm($this->db);
  2556. $actioncomm->type_code = 'AC_OTH_AUTO'; // Type of event ('AC_OTH', 'AC_OTH_AUTO', 'AC_XXX'...)
  2557. $actioncomm->code = 'AC_'.$actioncode;
  2558. $actioncomm->label = $actionmsg2;
  2559. $actioncomm->note_private = $actionmsg;
  2560. $actioncomm->fk_project = 0;
  2561. $actioncomm->datep = $now;
  2562. $actioncomm->datef = $now;
  2563. $actioncomm->percentage = -1; // Not applicable
  2564. $actioncomm->socid = $adherent->thirdparty->id;
  2565. $actioncomm->contact_id = 0;
  2566. $actioncomm->authorid = $user->id; // User saving action
  2567. $actioncomm->userownerid = $user->id; // Owner of action
  2568. // Fields when action is en email (content should be added into note)
  2569. $actioncomm->email_msgid = $cmail->msgid;
  2570. $actioncomm->email_from = $from;
  2571. $actioncomm->email_sender = '';
  2572. $actioncomm->email_to = $to;
  2573. $actioncomm->email_tocc = $sendtocc;
  2574. $actioncomm->email_tobcc = $sendtobcc;
  2575. $actioncomm->email_subject = $subject;
  2576. $actioncomm->errors_to = '';
  2577. $actioncomm->fk_element = $adherent->id;
  2578. $actioncomm->elementtype = $adherent->element;
  2579. $actioncomm->extraparams = $extraparams;
  2580. $actioncomm->create($user);
  2581. }
  2582. } else {
  2583. $blockingerrormsg = "Can't find email template, defined into member module setup, to use for reminding";
  2584. $nbko++;
  2585. $listofmembersko[$adherent->id] = $adherent->id;
  2586. break;
  2587. }
  2588. }
  2589. $i++;
  2590. }
  2591. } else {
  2592. $this->error = $this->db->lasterror();
  2593. return 1;
  2594. }
  2595. }
  2596. if ($blockingerrormsg) {
  2597. $this->error = $blockingerrormsg;
  2598. return 1;
  2599. } else {
  2600. $this->output = 'Found '.($nbok + $nbko).' members to send reminder to.';
  2601. $this->output .= ' Send email successfuly to '.$nbok.' members';
  2602. if (is_array($listofmembersok)) {
  2603. $listofids = '';
  2604. $i = 0;
  2605. foreach ($listofmembersok as $idmember) {
  2606. if ($i > 100) {
  2607. $listofids .= ', ...';
  2608. break;
  2609. }
  2610. if (empty($listofids)) {
  2611. $listofids .= ' [';
  2612. } else {
  2613. $listofids .= ', ';
  2614. }
  2615. $listofids .= $idmember;
  2616. $i++;
  2617. }
  2618. if ($listofids) {
  2619. $listofids .= ']';
  2620. }
  2621. $this->output .= $listofids;
  2622. }
  2623. if ($nbko) {
  2624. $this->output .= ' - Canceled for '.$nbko.' member (no email or email sending error)';
  2625. if (is_array($listofmembersko)) {
  2626. $listofids = '';
  2627. $i = 0;
  2628. foreach ($listofmembersko as $idmember) {
  2629. if ($i > 100) {
  2630. $listofids .= ', ...';
  2631. break;
  2632. }
  2633. if (empty($listofids)) {
  2634. $listofids .= ' [';
  2635. } else {
  2636. $listofids .= ', ';
  2637. }
  2638. $listofids .= $idmember;
  2639. $i++;
  2640. }
  2641. if ($listofids) {
  2642. $listofids .= ']';
  2643. }
  2644. $this->output .= $listofids;
  2645. }
  2646. }
  2647. }
  2648. return 0;
  2649. }
  2650. }