adherent.class.php 112 KB

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