adherent.class.php 106 KB

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