adherent.class.php 104 KB

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