actions_multicompany.class.php 216 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916
  1. <?php
  2. /* Copyright (C) 2009-2023 Regis Houssin <regis.houssin@inodbox.com>
  3. * Copyright (C) 2011 Herve Prot <herve.prot@symeos.com>
  4. * Copyright (C) 2014 Philippe Grand <philippe.grand@atoo-net.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. /**
  21. * \file /multicompany/class/actions_multicompany.class.php
  22. * \ingroup multicompany
  23. * \brief File Class multicompany
  24. */
  25. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  26. dol_include_once('/multicompany/class/dao_multicompany.class.php', 'DaoMulticompany');
  27. dol_include_once('/multicompany/lib/multicompany.lib.php');
  28. /**
  29. * Class Actions of the module multicompany
  30. */
  31. class ActionsMulticompany
  32. {
  33. /** @var DoliDB */
  34. var $db;
  35. /** @var DaoMulticompany */
  36. var $dao;
  37. var $mesg;
  38. var $error;
  39. var $errors=array();
  40. //! Numero de l'erreur
  41. var $errno = 0;
  42. var $template_dir;
  43. var $template;
  44. var $label;
  45. var $description;
  46. var $referent;
  47. var $sharings=array();
  48. var $options=array();
  49. var $entities=array();
  50. var $dict=array();
  51. var $tpl=array();
  52. var $addzero=array();
  53. var $sharingelements=array();
  54. var $sharingobjects=array();
  55. var $customdicts=array();
  56. private $config=array();
  57. // For Hookmanager return
  58. var $resprints;
  59. var $results=array();
  60. /**
  61. * Constructor
  62. *
  63. * @param DoliDB $db Database handler
  64. */
  65. public function __construct($db)
  66. {
  67. global $conf;
  68. $this->db = $db;
  69. $this->addzero = array(
  70. 'user',
  71. 'usergroup',
  72. 'c_email_templates',
  73. 'email_template',
  74. 'default_values',
  75. 'overwrite_trans'
  76. );
  77. // Default sharing elements
  78. $this->sharingelements = array(
  79. 'thirdparty' => array(
  80. 'type' => 'element',
  81. 'icon' => 'building',
  82. 'active' => true, // for setEntity() function
  83. 'disable' => true, // disable in options tab
  84. ),
  85. 'member' => array(
  86. 'type' => 'element',
  87. 'icon' => 'address-card',
  88. 'input' => array(
  89. 'global' => array(
  90. 'showhide' => true,
  91. 'hide' => true,
  92. 'del' => true
  93. )
  94. )
  95. ),
  96. 'member_type' => array(
  97. 'type' => 'element',
  98. 'icon' => 'address-card',
  99. 'display' => '!empty($conf->global->MULTICOMPANY_MEMBER_SHARING_ENABLED)',
  100. 'input' => array(
  101. 'global' => array(
  102. 'hide' => true,
  103. 'del' => true
  104. ),
  105. 'member' => array(
  106. 'showhide' => true,
  107. 'del' => true
  108. )
  109. )
  110. ),
  111. 'product' => array(
  112. 'type' => 'element',
  113. 'icon' => 'cube',
  114. 'tooltip' => 'ProductSharingInfo',
  115. 'enable' => '(!empty($conf->product->enabled) || !empty($conf->service->enabled))',
  116. 'input' => array(
  117. 'global' => array(
  118. 'showhide' => true,
  119. 'hide' => true,
  120. 'del' => true
  121. )
  122. ),
  123. 'active' => true
  124. ),
  125. 'productprice' => array(
  126. 'type' => 'element',
  127. 'icon' => 'money',
  128. 'tooltip' => 'ProductPriceSharingInfo',
  129. 'enable' => '(!empty($conf->product->enabled) || !empty($conf->service->enabled))',
  130. 'display' => '!empty($conf->global->MULTICOMPANY_PRODUCT_SHARING_ENABLED)',
  131. 'input' => array(
  132. 'global' => array(
  133. 'hide' => true,
  134. 'del' => true
  135. ),
  136. 'product' => array(
  137. 'showhide' => true,
  138. 'del' => true
  139. )
  140. )
  141. ),
  142. 'productsupplierprice' => array(
  143. 'type' => 'element',
  144. 'icon' => 'money',
  145. 'enable' => '((!empty($conf->product->enabled) || !empty($conf->service->enabled)) && !empty($conf->fournisseur->enabled))',
  146. 'display' => '!empty($conf->global->MULTICOMPANY_PRODUCT_SHARING_ENABLED)',
  147. 'input' => array(
  148. 'global' => array(
  149. 'hide' => true,
  150. 'del' => true
  151. ),
  152. 'product' => array(
  153. 'showhide' => true,
  154. 'del' => true
  155. )
  156. ),
  157. ),
  158. 'stock' => array(
  159. 'type' => 'element',
  160. 'icon' => 'cubes',
  161. //'tooltip' => 'SharingStockInfo',
  162. 'enable' => '(!empty($conf->stock->enabled) && (!empty($conf->product->enabled) || !empty($conf->service->enabled)))',
  163. 'display' => '!empty($conf->global->MULTICOMPANY_PRODUCT_SHARING_ENABLED)',
  164. 'input' => array(
  165. 'global' => array(
  166. 'hide' => true,
  167. 'del' => true
  168. ),
  169. 'product' => array(
  170. 'showhide' => true,
  171. 'del' => true
  172. )
  173. )
  174. ),
  175. 'category' => array(
  176. 'type' => 'element',
  177. 'icon' => 'paperclip',
  178. 'input' => array(
  179. 'global' => array(
  180. 'showhide' => true,
  181. 'hide' => true,
  182. 'del' => true
  183. )
  184. )
  185. ),
  186. 'agenda' => array(
  187. 'type' => 'element',
  188. 'icon' => 'calendar',
  189. 'input' => array(
  190. 'global' => array(
  191. 'showhide' => true,
  192. 'hide' => true,
  193. 'del' => true
  194. )
  195. )
  196. ),
  197. 'bankaccount' => array(
  198. 'type' => 'element',
  199. 'icon' => 'bank',
  200. 'input' => array(
  201. 'global' => array(
  202. 'showhide' => true,
  203. 'hide' => true,
  204. 'del' => true
  205. )
  206. )
  207. ),
  208. 'expensereport' => array(
  209. 'type' => 'element',
  210. 'icon' => 'edit',
  211. 'input' => array(
  212. 'global' => array(
  213. 'showhide' => true,
  214. 'hide' => true,
  215. 'del' => true
  216. )
  217. )
  218. ),
  219. 'holiday' => array(
  220. 'type' => 'element',
  221. 'icon' => 'paper-plane-o',
  222. 'input' => array(
  223. 'global' => array(
  224. 'showhide' => true,
  225. 'hide' => true,
  226. 'del' => true
  227. )
  228. )
  229. ),
  230. 'project' => array(
  231. 'type' => 'element',
  232. 'icon' => 'code-fork',
  233. 'input' => array(
  234. 'global' => array(
  235. 'showhide' => true,
  236. 'hide' => true,
  237. 'del' => true
  238. )
  239. )
  240. ),
  241. 'ticket' => array(
  242. 'type' => 'element',
  243. 'icon' => 'ticket-alt',
  244. 'input' => array(
  245. 'global' => array(
  246. 'showhide' => true,
  247. 'hide' => true,
  248. 'del' => true
  249. )
  250. )
  251. ),
  252. // Object
  253. 'proposal' => array(
  254. 'type' => 'object',
  255. 'icon' => 'file-pdf-o',
  256. 'mandatory' => 'thirdparty',
  257. 'enable' => '(!empty($conf->propal->enabled) && !empty($conf->societe->enabled))',
  258. 'display' => '!empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_ENABLED)',
  259. 'input' => array(
  260. 'global' => array(
  261. 'hide' => true,
  262. 'del' => true
  263. ),
  264. 'thirdparty' => array(
  265. 'showhide' => true,
  266. 'hide' => true,
  267. 'del' => true
  268. )
  269. ),
  270. 'active' => true
  271. ),
  272. 'proposalnumber' => array(
  273. 'type' => 'objectnumber',
  274. 'icon' => 'cogs',
  275. //'mandatory' => 'thirdparty',
  276. 'tooltip' => 'ElementNumberSharingInfo',
  277. 'enable' => '(!empty($conf->propal->enabled) && !empty($conf->societe->enabled))',
  278. //'display' => '!empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_ENABLED)',
  279. 'constants' => array(
  280. 'PROPALE_ADDON',
  281. 'PROPALE_SAPHIR_MASK',
  282. 'PROPALE_ADDON_PDF',
  283. 'PROPALE_ADDON_PDF_ODT_PATH'
  284. ),
  285. 'input' => array(
  286. 'global' => array(
  287. 'hide' => true,
  288. 'del' => true
  289. ),
  290. /*'thirdparty' => array(
  291. 'showhide' => true,
  292. 'hide' => true,
  293. 'del' => true
  294. )*/
  295. )
  296. ),
  297. 'order' => array(
  298. 'type' => 'object',
  299. 'icon' => 'file-pdf-o', // Font Awesome icon
  300. 'mandatory' => 'thirdparty',
  301. 'enable' => '(!empty($conf->commande->enabled) && !empty($conf->societe->enabled))',
  302. 'display' => '!empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_ENABLED)',
  303. 'input' => array(
  304. 'global' => array(
  305. 'hide' => true,
  306. 'del' => true
  307. ),
  308. 'thirdparty' => array(
  309. 'showhide' => true,
  310. 'hide' => true,
  311. 'del' => true
  312. )
  313. ),
  314. 'active' => true,
  315. //'disable' => true // Disable if not stable
  316. ),
  317. 'ordernumber' => array(
  318. 'type' => 'objectnumber',
  319. 'icon' => 'cogs',
  320. //'mandatory' => 'thirdparty',
  321. 'tooltip' => 'ElementNumberSharingInfo',
  322. 'enable' => '(!empty($conf->commande->enabled) && !empty($conf->societe->enabled))',
  323. //'display' => '!empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_ENABLED)',
  324. 'constants' => array(
  325. 'COMMANDE_ADDON',
  326. 'COMMANDE_SAPHIR_MASK',
  327. 'COMMANDE_ADDON_PDF',
  328. 'COMMANDE_ADDON_PDF_ODT_PATH'
  329. ),
  330. 'input' => array(
  331. 'global' => array(
  332. 'hide' => true,
  333. 'del' => true
  334. ),
  335. /*'thirdparty' => array(
  336. 'showhide' => true,
  337. 'hide' => true,
  338. 'del' => true
  339. )*/
  340. ),
  341. //'disable' => true
  342. ),
  343. 'invoice' => array(
  344. 'type' => 'object',
  345. 'icon' => 'file-pdf-o',
  346. 'mandatory' => 'thirdparty',
  347. 'enable' => '(!empty($conf->facture->enabled) && !empty($conf->societe->enabled))',
  348. 'display' => '!empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_ENABLED)',
  349. 'input' => array(
  350. 'global' => array(
  351. 'hide' => true,
  352. 'del' => true
  353. ),
  354. 'thirdparty' => array(
  355. 'showhide' => true,
  356. 'hide' => true,
  357. 'del' => true
  358. )
  359. ),
  360. 'active' => true
  361. ),
  362. 'invoicenumber' => array(
  363. 'type' => 'objectnumber',
  364. 'icon' => 'cogs',
  365. //'mandatory' => 'thirdparty',
  366. 'tooltip' => 'ElementNumberSharingInfo',
  367. 'enable' => '(!empty($conf->facture->enabled) && !empty($conf->societe->enabled))',
  368. //'display' => '!empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_ENABLED)',
  369. 'constants' => array(
  370. 'FACTURE_ADDON',
  371. 'FACTURE_MERCURE_MASK_INVOICE',
  372. 'FACTURE_MERCURE_MASK_CREDIT',
  373. 'FACTURE_MERCURE_MASK_DEPOSIT',
  374. 'FACTURE_MERCURE_MASK_REPLACEMENT',
  375. 'FACTURE_ADDON_PDF',
  376. 'FACTURE_ADDON_PDF_ODT_PATH'
  377. ),
  378. 'input' => array(
  379. 'global' => array(
  380. 'hide' => true,
  381. 'del' => true
  382. ),
  383. /*'thirdparty' => array(
  384. 'showhide' => true,
  385. 'hide' => true,
  386. 'del' => true
  387. )*/
  388. )
  389. ),
  390. 'supplier_proposal' => array(
  391. 'type' => 'object',
  392. 'icon' => 'file-pdf-o',
  393. 'input' => array(
  394. 'global' => array(
  395. 'hide' => true,
  396. 'del' => true
  397. )
  398. ),
  399. 'disable' => true
  400. ),
  401. 'supplier_order' => array(
  402. 'type' => 'object',
  403. 'icon' => 'file-pdf-o',
  404. 'input' => array(
  405. 'global' => array(
  406. 'hide' => true,
  407. 'del' => true
  408. )
  409. ),
  410. 'disable' => true
  411. ),
  412. 'supplier_invoice' => array(
  413. 'type' => 'object',
  414. 'icon' => 'file-pdf-o',
  415. 'input' => array(
  416. 'global' => array(
  417. 'hide' => true,
  418. 'del' => true
  419. )
  420. ),
  421. 'disable' => true
  422. ),
  423. 'contract' => array(
  424. 'type' => 'object',
  425. 'mandatory' => 'thirdparty',
  426. 'icon' => 'file-pdf-o',
  427. 'enable' => '(!empty($conf->societe->enabled) && !empty($conf->societe->enabled))',
  428. 'display' => '!empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_ENABLED)',
  429. 'input' => array(
  430. 'global' => array(
  431. 'hide' => true,
  432. 'del' => true
  433. ),
  434. 'thirdparty' => array(
  435. 'showhide' => true,
  436. 'del' => true
  437. )
  438. )
  439. ),
  440. 'projectnumber' => array(
  441. 'type' => 'objectnumber',
  442. 'icon' => 'cogs',
  443. //'mandatory' => 'project',
  444. 'tooltip' => 'ElementNumberSharingInfo',
  445. 'enable' => '!empty($conf->project->enabled)',
  446. //'display' => '!empty($conf->global->MULTICOMPANY_PROJECT_SHARING_ENABLED)',
  447. 'constants' => array(
  448. 'PROJECT_ADDON',
  449. 'PROJECT_UNIVERSAL_MASK',
  450. 'PROJECT_ADDON_PDF',
  451. 'PROJECT_ADDON_PDF_ODT_PATH'
  452. ),
  453. 'input' => array(
  454. 'global' => array(
  455. 'hide' => true,
  456. 'del' => true
  457. ),
  458. /*'project' => array(
  459. 'showhide' => true,
  460. 'hide' => true,
  461. 'del' => true
  462. )*/
  463. )
  464. ),
  465. 'ticketnumber' => array(
  466. 'type' => 'objectnumber',
  467. 'icon' => 'cogs',
  468. //'mandatory' => 'ticket',
  469. 'tooltip' => 'ElementNumberSharingInfo',
  470. 'enable' => '!empty($conf->ticket->enabled)',
  471. //'display' => '!empty($conf->global->MULTICOMPANY_TICKET_SHARING_ENABLED)',
  472. 'input' => array(
  473. 'global' => array(
  474. 'hide' => true,
  475. 'del' => true
  476. ),
  477. /*'ticket' => array(
  478. 'showhide' => true,
  479. 'hide' => true,
  480. 'del' => true
  481. )*/
  482. )
  483. ),
  484. 'intervention' => array(
  485. 'type' => 'object',
  486. 'icon' => 'wrench',
  487. 'input' => array(
  488. 'global' => array(
  489. 'hide' => true,
  490. 'del' => true
  491. )
  492. ),
  493. 'disable' => true
  494. )
  495. );
  496. // Module name if different of object name (eg $conf->commande->enabled)
  497. $this->sharingmodulename = array(
  498. 'thirdparty' => 'societe',
  499. 'proposal' => 'propal',
  500. 'proposalnumber' => 'propal',
  501. 'order' => 'commande',
  502. 'ordernumber' => 'commande',
  503. 'invoice' => 'facture',
  504. 'invoicenumber' => 'facture',
  505. 'product' => (empty($conf->product->enabled) && !empty($conf->service->enabled) ? 'service' : 'product'),
  506. 'productprice' => 'product',
  507. 'productsupplierprice' => 'product',
  508. 'project' => 'projet',
  509. 'projectnumber' => 'projet',
  510. 'ticketnumber' => 'ticket',
  511. 'member' => 'adherent',
  512. 'membertype' => 'adherent',
  513. 'member_type' => 'adherent', // deprecated
  514. //'membertype' => 'member_type', // deprecated
  515. 'intervention' => 'ficheinter',
  516. 'category' => 'categorie',
  517. 'bankaccount' => 'banque',
  518. 'bank_account' => 'banque', // deprecated
  519. //'bankaccount' => 'banque_account', // deprecated
  520. );
  521. // If getEntity value is different of sharing name
  522. $this->sharingnamecompatibility = array(
  523. 'societe' => 'thirdparty',
  524. 'socpeople' => 'thirdparty',
  525. 'contact' => 'thirdparty',
  526. 'adherent' => 'member',
  527. 'bank_account' => 'bankaccount',
  528. 'adherent_type' => 'member_type',
  529. 'categorie' => 'category',
  530. 'propal' => 'proposal',
  531. 'commande' => 'order',
  532. 'facture' => 'invoice',
  533. 'contrat' => 'contract',
  534. 'entrepot' => 'stock'
  535. );
  536. $this->customdicts = array(
  537. 'accounting_journal' => array(
  538. 'icon' => 'search-dollar',
  539. 'transkey' => 'AccountingJournal',
  540. 'foreignkeys' => array()
  541. ),
  542. 'c_accounting_category' => array(
  543. 'icon' => 'search-dollar',
  544. 'transkey' => 'AccountingCategory',
  545. 'foreignkeys' => array()
  546. ),
  547. 'c_socialnetworks' => array(
  548. 'icon' => 'project-diagram',
  549. 'transkey' => 'SocialNetworks',
  550. 'foreignkeys' => array()
  551. ),
  552. 'c_type_container' => array(
  553. 'icon' => 'globe-americas',
  554. 'transkey' => 'TypeContainer',
  555. 'foreignkeys' => array()
  556. ),
  557. 'c_paiement' => array(
  558. 'disabled' => true,
  559. 'transkey' => 'Paiement',
  560. 'foreignkeys' => array(
  561. 'societe' => array(
  562. 'mode_reglement',
  563. 'mode_reglement_supplier'
  564. ),
  565. 'propal' => 'fk_mode_reglement',
  566. 'commande' => 'fk_mode_reglement',
  567. 'facture' => 'fk_mode_reglement',
  568. 'facture_rec' => 'fk_mode_reglement',
  569. 'commande_fournisseur' => 'fk_mode_reglement',
  570. 'facture_fourn' => 'fk_mode_reglement',
  571. 'supplier_proposal' => 'fk_mode_reglement',
  572. 'chargesociales' => 'fk_mode_reglement',
  573. 'don' => 'fk_payment',
  574. 'paiement' => 'fk_paiement',
  575. 'paiementfourn' => 'fk_paiement',
  576. 'paiement_facture' => 'fk_paiement',
  577. 'expensereport' => 'fk_c_paiement',
  578. 'paiementcharge' => 'fk_typepaiement',
  579. 'tva' => 'fk_typepayment',
  580. 'payment_various' => 'fk_typepayment',
  581. 'payment_salary' => 'fk_typepayment',
  582. 'payment_expensereport' => 'fk_typepayment',
  583. 'payment_donation' => 'fk_typepayment',
  584. 'loan_schedule' => 'fk_typepayment',
  585. 'payment_loan' => 'fk_typepayment'
  586. )
  587. ),
  588. 'c_payment_term' => array(
  589. 'disabled' => true,
  590. 'transkey' => 'PaymentTerm',
  591. 'foreignkeys' => array(
  592. 'societe' => array(
  593. 'cond_reglement',
  594. 'cond_reglement_supplier'
  595. ),
  596. 'propal' => 'fk_cond_reglement',
  597. 'commande' => 'fk_cond_reglement',
  598. 'facture' => 'fk_cond_reglement',
  599. 'facture_rec' => 'fk_cond_reglement',
  600. 'commande_fournisseur' => 'fk_cond_reglement',
  601. 'facture_fourn' => 'fk_cond_reglement',
  602. 'supplier_proposal' => 'fk_cond_reglement'
  603. )
  604. )
  605. );
  606. if (!empty($conf->global->MULTICOMPANY_EXTERNAL_MODULES_SHARING)) {
  607. $externalmodules = json_decode($conf->global->MULTICOMPANY_EXTERNAL_MODULES_SHARING, true);
  608. if (is_array($externalmodules) && !empty($externalmodules)) {
  609. foreach($externalmodules as $params) {
  610. if (is_array($params) && !empty($params)) {
  611. if (is_array($params['addzero']) && !empty($params['addzero'])) {
  612. array_push($this->addzero, $params['addzero']);
  613. }
  614. if (is_array($params['sharingelements']) && !empty($params['sharingelements'])) {
  615. $this->sharingelements = array_merge($this->sharingelements, $params['sharingelements']);
  616. }
  617. if (is_array($params['sharingmodulename']) && !empty($params['sharingmodulename'])) {
  618. $this->sharingmodulename = array_merge($this->sharingmodulename, $params['sharingmodulename']);
  619. }
  620. }
  621. }
  622. }
  623. }
  624. }
  625. /**
  626. * Instantiation of DAO class
  627. *
  628. * @return void
  629. */
  630. private function getInstanceDao()
  631. {
  632. if (!is_object($this->dao)){
  633. $this->dao = new DaoMulticompany($this->db);
  634. }
  635. }
  636. /**
  637. * setHtmlTitle
  638. *
  639. * @param array $parameters Hook metadatas (context, etc...)
  640. */
  641. public function setHtmlTitle($parameters=false)
  642. {
  643. global $conf;
  644. if (empty($conf->global->MULTICOMPANY_HIDE_HTML_TITLE)) {
  645. $this->resprints = ' + multicompany';
  646. }
  647. return 0;
  648. }
  649. /**
  650. *
  651. * @param boolean $parameters
  652. * @param unknown $object
  653. * @param string $action
  654. * @return number
  655. */
  656. public function doUpgradeBefore($parameters=false, &$object, &$action='')
  657. {
  658. global $conf, $mc;
  659. if (empty($conf->multicompany->enabled)) {
  660. return 0;
  661. }
  662. if (is_array($parameters) && !empty($parameters)) {
  663. foreach($parameters as $key => $value) {
  664. $$key = $value;
  665. }
  666. }
  667. $currentcontext = explode(':', $parameters['context']);
  668. if ($action == 'upgrade') {
  669. if (version_compare($versionto, '15.0.9', '>')) {
  670. $this->results = array_keys($mc->getEntitiesList(true, false, false, true));
  671. }
  672. }
  673. return 0;
  674. }
  675. /**
  676. *
  677. * @param boolean $parameters
  678. * @param unknown $object
  679. * @param string $action
  680. * @return number
  681. */
  682. public function doUpgradeAfterDB($parameters=false, &$object, &$action='')
  683. {
  684. global $conf, $langs, $user;
  685. global $mc;
  686. if (empty($conf->multicompany->enabled)) {
  687. return 0;
  688. }
  689. if (is_array($parameters) && !empty($parameters)) {
  690. foreach($parameters as $key => $value) {
  691. $$key = $value;
  692. }
  693. }
  694. if (version_compare($versionto, '15.0.9', '>') && $conf->entity != 1) {
  695. return 0;
  696. }
  697. $langs->loadLangs(array('multicompany@multicompany'));
  698. $currentcontext = explode(':', $parameters['context']);
  699. if ($action == 'upgrade') {
  700. if (version_compare($versionto, '13.0.9', '>')) {
  701. $entities = $mc->getEntitiesList(true, false, false, true);
  702. /*
  703. * Update missing user boxes
  704. */
  705. print '<tr class="trforrunsql"><td colspan="4">';
  706. print '<b>'.$langs->trans('MulticompanyUpdateMissingBoxesPerEntity')."</b><br>\n";
  707. $err=0;
  708. $boxes = array('box_lastlogin.php', 'box_birthdays.php', 'box_dolibarr_state_board.php');
  709. foreach ($entities as $key => $label) {
  710. echo $label;
  711. foreach ($boxes as $file) {
  712. echo ' '.$file;
  713. // Search if boxes def already present
  714. $sql = "SELECT count(*) as nb FROM ".MAIN_DB_PREFIX."boxes_def";
  715. $sql.= " WHERE file = '".$this->db->escape($file)."'";
  716. $sql.= " AND entity = ".$key;
  717. $result = $this->db->query($sql);
  718. if ($result) {
  719. $obj = $this->db->fetch_object($result);
  720. if ($obj->nb == 0) {
  721. $this->db->begin();
  722. $sql = "INSERT INTO ".MAIN_DB_PREFIX."boxes_def (file, entity)";
  723. $sql .= " VALUES ('".$this->db->escape($file)."', ";
  724. $sql .= ((int) $key);
  725. $sql .= ")";
  726. $resql = $this->db->query($sql);
  727. if (!$resql) {
  728. $err++;
  729. }
  730. if (!$err) {
  731. $this->db->commit();
  732. } else {
  733. $this->error = $this->db->lasterror();
  734. $this->db->rollback();
  735. }
  736. } else {
  737. print ": ".$langs->trans("NothingToDo")."<br>\n";
  738. }
  739. } else {
  740. $this->error = $this->db->lasterror();
  741. return -1;
  742. }
  743. }
  744. print "<br>\n";
  745. }
  746. print '</td></tr>';
  747. /*
  748. * Update llx_product_perentity
  749. */
  750. print '<tr class="trforrunsql"><td colspan="4">';
  751. print '<b>'.$langs->trans('MulticompanyUpdateProductPMPPerEntity')."</b><br>\n";
  752. foreach ($entities as $key => $label) {
  753. echo $label.' ';
  754. $sql = "SELECT p.rowid, p.pmp";
  755. $sql.= " FROM ".MAIN_DB_PREFIX."product as p";
  756. $sql.= " WHERE p.entity = ".$key;
  757. $sql.= " AND NOT EXISTS (";
  758. $sql.= " SELECT pe.fk_product";
  759. $sql.= " FROM ".MAIN_DB_PREFIX."product_perentity as pe";
  760. $sql.= " WHERE pe.entity = ".$key;
  761. $sql.= " AND p.rowid = pe.fk_product)";
  762. $resql = $this->db->query($sql);
  763. if ($resql) {
  764. $num = $this->db->num_rows($resql);
  765. if ($num) {
  766. $i = 0;
  767. while ($i < $num) {
  768. $obj = $this->db->fetch_object($resql);
  769. $sql2 = "INSERT INTO ".MAIN_DB_PREFIX."product_perentity (";
  770. $sql2.= "entity";
  771. $sql2.= ", fk_product";
  772. $sql2.= ", pmp";
  773. $sql2.= ") VALUES (";
  774. $sql2.= $key;
  775. $sql2.= ", ".$obj->rowid;
  776. $sql2.= ", ".$obj->pmp;
  777. $sql2.= ")";
  778. $resql2 = $this->db->query($sql2);
  779. if (empty($resql2)) {
  780. $this->error = $this->db->lasterror;
  781. return -1;
  782. }
  783. print ". ";
  784. $i++;
  785. }
  786. print "<br>\n";
  787. } else {
  788. print ": ".$langs->trans("NothingToDo")."<br>\n";
  789. }
  790. $this->db->free($resql);
  791. } else {
  792. $this->error = $this->db->lasterror;
  793. return -1;
  794. }
  795. }
  796. print '</td></tr>';
  797. /*
  798. * Update dictionaries by entities
  799. */
  800. if (!empty($this->customdicts)) {
  801. foreach($this->customdicts as $dict => $params) {
  802. if (empty($params['disabled'])) {
  803. $transkey = 'MulticompanyUpdate'.(!empty($params['transkey']) ? $params['transkey'] : ucfirst($dict)).'PerEntity';
  804. print '<tr class="trforrunsql"><td colspan="4">';
  805. print '<b>'.$langs->trans($transkey)."</b>\n";
  806. $fullpath = getSqlInitFilePath($dict);
  807. if (file_exists($fullpath)) {
  808. $i=0;
  809. foreach ($entities as $key => $label) {
  810. echo ($i == 0 ? '<br>' : '').$label.' ';
  811. $sql = "SELECT rowid";
  812. $sql.= " FROM ".MAIN_DB_PREFIX.$dict;
  813. $sql.= " WHERE entity = ".$key;
  814. $resql = $this->db->query($sql);
  815. if ($resql) {
  816. $num = $this->db->num_rows($resql);
  817. if ($num) {
  818. print ": ".$langs->trans("NothingToDo")."<br>\n";
  819. } else {
  820. $result = run_sql($fullpath, 1, $key);
  821. print ': <span class="ok">OK</span><br>'."\n";
  822. }
  823. $this->db->free($resql);
  824. } else {
  825. $this->error = $this->db->lasterror;
  826. return -1;
  827. }
  828. $i++;
  829. }
  830. } else {
  831. print ' <span class="error">KO</span><br>'."\n";
  832. }
  833. print '</td></tr>';
  834. }
  835. }
  836. }
  837. }
  838. }
  839. return 1;
  840. }
  841. /**
  842. *
  843. * @param boolean $parameters
  844. * @param unknown $object
  845. * @param string $action
  846. * @return number
  847. */
  848. public function doUpgrade2($parameters=false, &$object, &$action='')
  849. {
  850. return $this->doUpgradeAfterDB($parameters, $object, $action);
  851. }
  852. /**
  853. *
  854. * @param boolean $parameters
  855. * @param unknown $object
  856. * @param string $action
  857. */
  858. public function restrictedArea($parameters=false, &$object, &$action='')
  859. {
  860. global $conf;
  861. if (empty($conf->multicompany->enabled) || empty($conf->global->MULTICOMPANY_SHARINGS_ENABLED) || empty($conf->global->MULTICOMPANY_SHARING_BYELEMENT_ENABLED)) {
  862. return 0;
  863. }
  864. if (is_array($parameters) && !empty($parameters)) {
  865. foreach($parameters as $key=>$value) {
  866. $$key=$value;
  867. }
  868. }
  869. // Features/modules to check
  870. $featuresarray = array($features);
  871. if (preg_match('/&/', $features)) {
  872. $featuresarray = explode("&", $features);
  873. } elseif (preg_match('/\|/', $features)) {
  874. $featuresarray = explode("|", $features);
  875. }
  876. if (!empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_ENABLED)
  877. && !empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_BYELEMENT_ENABLED)
  878. && in_array('societe', $featuresarray) && !empty($objectid)) {
  879. $staticobject = new Societe($this->db);
  880. $ret = $staticobject->fetch($objectid);
  881. if ($ret > 0) {
  882. // check only if entity of element is different of current entity
  883. if ($staticobject->entity != $conf->entity) {
  884. $this->getSharingsByElement('thirdparty', $staticobject->id); // Populate $this->config['sharedwith']
  885. if (!empty($this->config['sharedwith']['thirdparty'][$staticobject->id])
  886. && in_array($conf->entity, $this->config['sharedwith']['thirdparty'][$staticobject->id])) {
  887. $this->results['result'] = 1;
  888. } else {
  889. $this->results['result'] = 0;
  890. }
  891. }
  892. }
  893. } elseif (!empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_ENABLED)
  894. && !empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_BYELEMENT_ENABLED)
  895. && !empty($conf->global->MULTICOMPANY_CONTACT_SHARING_BYELEMENT_ENABLED)
  896. && in_array('contact', $featuresarray) && !empty($objectid)) {
  897. $staticobject = new Contact($this->db);
  898. $ret = $staticobject->fetch($objectid);
  899. if ($ret > 0) {
  900. // check only if entity of element is different of current entity
  901. if ($staticobject->entity != $conf->entity) {
  902. $this->getSharingsByElement($staticobject->element, $staticobject->id); // Populate $this->config['sharedwith']
  903. if (!empty($this->config['sharedwith'][$staticobject->element][$staticobject->id])
  904. && in_array($conf->entity, $this->config['sharedwith'][$staticobject->element][$staticobject->id])) {
  905. $this->results['result'] = 1;
  906. } else {
  907. $this->results['result'] = 0;
  908. }
  909. }
  910. }
  911. } elseif (!empty($conf->global->MULTICOMPANY_PRODUCT_SHARING_ENABLED)
  912. && !empty($conf->global->MULTICOMPANY_PRODUCT_SHARING_BYELEMENT_ENABLED)
  913. && (in_array('produit', $featuresarray) || in_array('service', $featuresarray)) && !empty($objectid)) {
  914. $staticobject = new Product($this->db);
  915. $ret = $staticobject->fetch($objectid);
  916. if ($ret > 0) {
  917. // check only if entity of element is different of current entity
  918. if ($staticobject->entity != $conf->entity) {
  919. $this->getSharingsByElement($staticobject->element, $staticobject->id); // Populate $this->config['sharedwith']
  920. if (!empty($this->config['sharedwith'][$staticobject->element][$staticobject->id])
  921. && in_array($conf->entity, $this->config['sharedwith'][$staticobject->element][$staticobject->id])) {
  922. $this->results['result'] = 1;
  923. } else {
  924. $this->results['result'] = 0;
  925. }
  926. }
  927. }
  928. }
  929. return 0;
  930. }
  931. /**
  932. * Enter description here ...
  933. *
  934. * @param string $action Action type
  935. */
  936. public function doAdminActions(&$action='')
  937. {
  938. global $conf, $user, $langs;
  939. $this->getInstanceDao();
  940. $id = GETPOST('id','int');
  941. $label = GETPOST('label','alpha');
  942. $name = GETPOST('name','alpha');
  943. $description = GETPOST('description','alpha');
  944. $cancel = GETPOST('cancel', 'alpha');
  945. $addandstay = GETPOST('addandstay', 'alpha');
  946. $cancelandstay = GETPOST('cancelandstay', 'alpha');
  947. $updateandstay = GETPOST('updateandstay', 'alpha');
  948. $template = (empty($conf->global->MULTICOMPANY_TEMPLATE_MANAGEMENT) ? null : GETPOST('template', 'int'));
  949. $usetemplate = (empty($conf->global->MULTICOMPANY_TEMPLATE_MANAGEMENT) ? null : GETPOST('usetemplate', 'int'));
  950. $visible = GETPOST('visible', 'int');
  951. $active = GETPOST('active', 'int');
  952. if ($action === 'add' && empty($cancel) && !empty($user->admin) && empty($user->entity)) {
  953. $error=0;
  954. if (empty($label)) {
  955. $error++;
  956. setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentities("Label")), 'errors');
  957. $action = 'create';
  958. }
  959. else if (empty($name)) {
  960. $error++;
  961. setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentities("CompanyName")), 'errors');
  962. $action = 'create';
  963. }
  964. // Verify if label already exist in database
  965. if (empty($error)) {
  966. $this->dao->getEntities();
  967. if (!empty($this->dao->entities)) {
  968. foreach($this->dao->entities as $entity) {
  969. if (strtolower($entity->label) == strtolower($label)) $error++;
  970. }
  971. if (!empty($error)) {
  972. setEventMessage($langs->trans("ErrorEntityLabelAlreadyExist"), 'errors');
  973. $action = 'create';
  974. }
  975. }
  976. }
  977. if (empty($error)) {
  978. $this->db->begin();
  979. $this->dao->label = $label;
  980. $this->dao->description = $description;
  981. $this->dao->visible = ((!empty($template) && empty($usetemplate)) ? 2 : ((!empty($visible) || !empty($conf->global->MULTICOMPANY_VISIBLE_BY_DEFAULT)) ? 1 : 0));
  982. $this->dao->active = ((!empty($active) || !empty($conf->global->MULTICOMPANY_ACTIVE_BY_DEFAULT))?1:0);
  983. if (!empty($conf->global->MULTICOMPANY_SHARINGS_ENABLED)) {
  984. foreach ($this->sharingelements as $element => $params) {
  985. if (($params['type'] === 'object' && !isset($params['disable']))
  986. && (empty($conf->societe->enabled) || empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_ENABLED))) {
  987. continue;
  988. }
  989. $uppername = strtoupper($element);
  990. $constname = 'MULTICOMPANY_' . $uppername . '_SHARING_ENABLED';
  991. if (!empty($conf->global->$constname)) {
  992. $shareentities = GETPOST($element.'_to', 'array');
  993. $shareentities = array_unique($shareentities); sort($shareentities);
  994. $this->dao->options['sharings'][$element] = (!empty($shareentities) ? $shareentities : null);
  995. if (!empty($conf->global->MULTICOMPANY_TEMPLATE_MANAGEMENT)) {
  996. $this->dao->options['addtoallother'][$element] = GETPOST('addtoallother_'.$element, 'int');
  997. }
  998. if ($params['type'] === 'objectnumber') {
  999. $this->dao->options[$element.'_referent'] = (GETPOSTISSET($element.'_referring_entity') ? GETPOSTINT($element.'_referring_entity') : null);
  1000. }
  1001. }
  1002. }
  1003. }
  1004. if (!empty($conf->accounting->enabled) && GETPOSTISSET('cloneaccountingaccount')) {
  1005. $this->dao->options['cloneaccountingaccount'] = GETPOSTINT('cloneaccountingaccount');
  1006. }
  1007. $extrafields = new ExtraFields($this->db);
  1008. $extralabels = $extrafields->fetch_name_optionals_label($this->dao->table_element, true);
  1009. $extrafields->setOptionalsFromPost($extralabels, $this->dao);
  1010. $id = $this->dao->create($user);
  1011. if ($id <= 0) {
  1012. $error++;
  1013. $errors=($this->dao->error ? array($this->dao->error) : $this->dao->errors);
  1014. setEventMessage($errors, 'errors');
  1015. $action = 'create';
  1016. }
  1017. if (empty($error) && $id > 0) {
  1018. if (!empty($usetemplate) && is_numeric($usetemplate)) {
  1019. $const = $this->dao->getEntityConfig($usetemplate);
  1020. if (!empty($const)) {
  1021. // Reload modules
  1022. foreach ($const as $key => $value) {
  1023. if (preg_match('/^MAIN\_MODULE\_([0-9A-Z]+)$/', $key, $reg)) {
  1024. if (!empty($reg[1])) {
  1025. $classname = 'mod' . ucfirst(strtolower($reg[1]));
  1026. $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/'.$classname.'.class.php';
  1027. if ($res) {
  1028. dol_syslog(get_class($this)."::reloadModules template=".$usetemplate." module=".$key." classname=".$classname, LOG_DEBUG);
  1029. $mod=new $classname($this->db);
  1030. $mod->init('forceactivate', $id);
  1031. } else {
  1032. dol_syslog(get_class($this)."::reloadModules template=".$usetemplate." module=".$key." classname=".$classname, LOG_ERR);
  1033. }
  1034. }
  1035. }
  1036. }
  1037. // Overwrite with template
  1038. foreach ($const as $key => $value) {
  1039. dolibarr_set_const($this->db, $key, $value, 'chaine', 0, '', $id);
  1040. }
  1041. }
  1042. }
  1043. $country_id = GETPOST('country_id', 'int');
  1044. $country = getCountry($country_id, 'all');
  1045. $country_code = $country['code'];
  1046. $country_label = $country['label'];
  1047. dolibarr_set_const($this->db, "MAIN_INFO_SOCIETE_COUNTRY", $country_id.':'.$country_code.':'.$country_label,'chaine',0,'',$id);
  1048. dolibarr_set_const($this->db, "MAIN_INFO_SOCIETE_NOM",$name,'chaine',0,'',$id);
  1049. dolibarr_set_const($this->db, "MAIN_INFO_SOCIETE_ADDRESS",GETPOST('address', 'alpha'),'chaine',0,'',$id);
  1050. dolibarr_set_const($this->db, "MAIN_INFO_SOCIETE_TOWN",GETPOST('town', 'alpha'),'chaine',0,'',$id);
  1051. dolibarr_set_const($this->db, "MAIN_INFO_SOCIETE_ZIP",GETPOST('zipcode', 'alpha'),'chaine',0,'',$id);
  1052. dolibarr_set_const($this->db, "MAIN_INFO_SOCIETE_STATE",GETPOST('departement_id', 'int'),'chaine',0,'',$id);
  1053. dolibarr_set_const($this->db, "MAIN_MONNAIE",GETPOST('currency_code', 'alpha'),'chaine',0,'',$id);
  1054. dolibarr_set_const($this->db, "MAIN_LANG_DEFAULT",GETPOST('main_lang_default', 'alpha'),'chaine',0,'',$id);
  1055. dolibarr_set_const($this->db, "MAIN_CSS_COLORS",GETPOST('options_company_colors', 'alpha'),'chaine',0,'',$id);
  1056. if (empty($usetemplate)) {
  1057. // Load llx_const.sql
  1058. $fullpath = getSqlInitFilePath('const');
  1059. if (file_exists($fullpath)) {
  1060. $result=run_sql($fullpath, 1, $id);
  1061. }
  1062. // Load llx_boxes_def.sql
  1063. $fullpath = getSqlInitFilePath('boxes_def');
  1064. if (file_exists($fullpath)) {
  1065. $result=run_sql($fullpath, 1, $id);
  1066. }
  1067. // Load llx_*.sql declared in $this->customdicts
  1068. foreach($this->customdicts as $dict => $data) {
  1069. $fullpath = getSqlInitFilePath($dict);
  1070. if (file_exists($fullpath)) {
  1071. $result = run_sql($fullpath, 1, $id);
  1072. }
  1073. }
  1074. } else {
  1075. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  1076. $modulesdir = dolGetModulesDirs();
  1077. foreach ($modulesdir as $dir) {
  1078. // Load modules attributes in arrays (name, numero, orders) from dir directory
  1079. //print $dir."\n<br>";
  1080. $handle=@opendir(dol_osencode($dir));
  1081. if (is_resource($handle)) {
  1082. while (($file = readdir($handle))!==false) {
  1083. if (is_readable($dir.$file) && substr($file, 0, 3) == 'mod' && substr($file, dol_strlen($file) - 10) == '.class.php') {
  1084. $modName = substr($file, 0, dol_strlen($file) - 10);
  1085. if ($modName) {
  1086. include_once $dir."/".$file;
  1087. $objMod = new $modName($this->db);
  1088. // Load all lang files of module
  1089. if (isset($objMod->langfiles) && is_array($objMod->langfiles)) {
  1090. foreach($objMod->langfiles as $domain) {
  1091. $langs->load($domain);
  1092. }
  1093. }
  1094. // Load all permissions
  1095. if ($objMod->rights_class) {
  1096. $ret=$objMod->insert_permissions(0, $id);
  1097. }
  1098. }
  1099. }
  1100. }
  1101. }
  1102. }
  1103. // Add entity to all others entities
  1104. if (!empty($this->dao->options['addtoallother'])) {
  1105. $this->dao->getEntities(true, array($id), false);
  1106. foreach ($this->dao->entities as $entity) {
  1107. if (!is_array($entity->options)) $entity->options = array();
  1108. if (!is_array($entity->options['sharings'])) $entity->options['sharings'] = array();
  1109. foreach($this->dao->options['sharings'] as $element => $values) {
  1110. if (!is_array($entity->options['sharings'][$element])) {
  1111. $entity->options['sharings'][$element] = array();
  1112. }
  1113. if ($this->dao->options['addtoallother'][$element] == '1') {
  1114. array_push($entity->options['sharings'][$element], (string) $id);
  1115. $entity->update($entity->id, $user);
  1116. }
  1117. }
  1118. }
  1119. }
  1120. $listofgroups = $this->dao->getListOfGroups();
  1121. if (!empty($listofgroups)) {
  1122. foreach($listofgroups as $groupid) {
  1123. // Duplicate usergroup rights
  1124. $ret = $this->duplicateUserGroupRights($groupid, $usetemplate, array($id));
  1125. if ($ret < 0) {
  1126. $error++;
  1127. }
  1128. // Add users to groups if linked with template
  1129. $userslist = $this->dao->getListOfUsersInGroupByTemplate($groupid, $usetemplate);
  1130. if ($userslist < 0) {
  1131. $error++;
  1132. } elseif (!empty($userslist)) {
  1133. foreach($userslist as $usertemplate) {
  1134. $result = $usertemplate->SetInGroup($groupid, $id);
  1135. if ($result < 0) {
  1136. $error++;
  1137. break;
  1138. }
  1139. }
  1140. }
  1141. }
  1142. }
  1143. }
  1144. if (empty($error)) {
  1145. $this->db->commit();
  1146. } else {
  1147. $this->db->rollback();
  1148. }
  1149. if (!empty($addandstay)) {
  1150. header("Location: " . $_SERVER['PHP_SELF'].'?action=edit&id='.$id);
  1151. exit;
  1152. }
  1153. } else {
  1154. $this->db->rollback();
  1155. }
  1156. }
  1157. } else if ($action === 'edit' && !empty($user->admin) && empty($user->entity)) {
  1158. $error=0;
  1159. if (!empty($cancel)) {
  1160. header("Location: " . $_SERVER['PHP_SELF']);
  1161. exit;
  1162. }
  1163. if ($this->dao->fetch($id) < 0) {
  1164. $error++;
  1165. setEventMessage($langs->trans("ErrorEntityIsNotValid"), 'errors');
  1166. $action = '';
  1167. }
  1168. } else if ($action === 'update' && empty($cancel) && $id > 0 && !empty($user->admin) && empty($user->entity)) {
  1169. $error=0;
  1170. if (!empty($cancelandstay)) {
  1171. $action = 'edit';
  1172. return;
  1173. }
  1174. $ret = $this->dao->fetch($id);
  1175. if ($ret < 0) {
  1176. $error++;
  1177. setEventMessage($langs->trans("ErrorEntityIsNotValid"), 'errors');
  1178. $action = '';
  1179. } else if (empty($label)) {
  1180. $error++;
  1181. setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentities("Label")), 'errors');
  1182. $action = 'edit';
  1183. } else if (empty($name)) {
  1184. $error++;
  1185. setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentities("CompanyName")), 'errors');
  1186. $action = 'edit';
  1187. }
  1188. // Verify if label already exist in database
  1189. if (empty($error)) {
  1190. $this->dao->getEntities();
  1191. if (!empty($this->dao->entities)) {
  1192. foreach($this->dao->entities as $entity) {
  1193. if ($entity->id == $this->dao->id) continue;
  1194. if (strtolower($entity->label) == strtolower($label)) $error++;
  1195. }
  1196. if ($error) {
  1197. setEventMessage($langs->trans("ErrorEntityLabelAlreadyExist"), 'errors');
  1198. $action = 'edit';
  1199. }
  1200. }
  1201. }
  1202. if (empty($error)) {
  1203. $this->db->begin();
  1204. $this->dao->label = $label;
  1205. $this->dao->description = $description;
  1206. if (!empty($conf->global->MULTICOMPANY_SHARINGS_ENABLED)) {
  1207. foreach ($this->sharingelements as $element => $params) {
  1208. if (($params['type'] === 'object' && !isset($params['disable']))
  1209. && (empty($conf->societe->enabled) || empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_ENABLED))) {
  1210. continue;
  1211. }
  1212. $uppername = strtoupper($element);
  1213. $constname = 'MULTICOMPANY_' . $uppername . '_SHARING_ENABLED';
  1214. if (!empty($conf->global->$constname)) {
  1215. $shareentities = GETPOST($element.'_to', 'array');
  1216. $shareentities = array_unique($shareentities); sort($shareentities);
  1217. $this->dao->options['sharings'][$element] = (!empty($shareentities) ? $shareentities : null);
  1218. if (!empty($conf->global->MULTICOMPANY_TEMPLATE_MANAGEMENT)) {
  1219. $this->dao->options['addtoallother'][$element] = GETPOST('addtoallother_'.$element, 'int');
  1220. }
  1221. if ($params['type'] === 'objectnumber') {
  1222. $this->dao->options[$element.'_referent'] = (GETPOSTISSET($element.'_referring_entity') ? GETPOSTINT($element.'_referring_entity') : null);
  1223. }
  1224. }
  1225. }
  1226. }
  1227. if (!empty($conf->accounting->enabled) && GETPOSTISSET('cloneaccountingaccount')) {
  1228. $this->dao->options['cloneaccountingaccount'] = GETPOSTINT('cloneaccountingaccount');
  1229. }
  1230. $extrafields = new ExtraFields($this->db);
  1231. $extralabels = $extrafields->fetch_name_optionals_label($this->dao->table_element, true);
  1232. $extrafields->setOptionalsFromPost($extralabels, $this->dao);
  1233. $ret = $this->dao->update($this->dao->id, $user);
  1234. if ($ret <= 0) {
  1235. $error++;
  1236. $errors=($this->dao->error ? array($this->dao->error) : $this->dao->errors);
  1237. setEventMessage($errors, 'errors');
  1238. $action = 'edit';
  1239. }
  1240. if (empty($error) && $ret > 0) {
  1241. $country_id = GETPOST('country_id', 'int');
  1242. $country = getCountry($country_id, 'all');
  1243. $country_code = $country['code'];
  1244. $country_label = $country['label'];
  1245. dolibarr_set_const($this->db, "MAIN_INFO_SOCIETE_COUNTRY", $country_id.':'.$country_code.':'.$country_label,'chaine',0,'',$this->dao->id);
  1246. dolibarr_set_const($this->db, "MAIN_INFO_SOCIETE_NOM",$name,'chaine',0,'',$this->dao->id);
  1247. dolibarr_set_const($this->db, "MAIN_INFO_SOCIETE_ADDRESS",GETPOST('address', 'alpha'),'chaine',0,'',$this->dao->id);
  1248. dolibarr_set_const($this->db, "MAIN_INFO_SOCIETE_TOWN",GETPOST('town', 'alpha'),'chaine',0,'',$this->dao->id);
  1249. dolibarr_set_const($this->db, "MAIN_INFO_SOCIETE_ZIP",GETPOST('zipcode', 'alpha'),'chaine',0,'',$this->dao->id);
  1250. dolibarr_set_const($this->db, "MAIN_INFO_SOCIETE_STATE",GETPOST('departement_id', 'int'),'chaine',0,'',$this->dao->id);
  1251. dolibarr_set_const($this->db, "MAIN_MONNAIE",GETPOST('currency_code', 'alpha'),'chaine',0,'',$this->dao->id);
  1252. dolibarr_set_const($this->db, "MAIN_LANG_DEFAULT",GETPOST('main_lang_default', 'alpha'),'chaine',0,'',$this->dao->id);
  1253. dolibarr_set_const($this->db, "MAIN_CSS_COLORS",GETPOST('options_company_colors', 'alpha'),'chaine',0,'',$this->dao->id);
  1254. $this->db->commit();
  1255. if (!empty($updateandstay)) {
  1256. $action = 'edit';
  1257. }
  1258. } else {
  1259. $this->db->rollback();
  1260. }
  1261. }
  1262. }
  1263. }
  1264. /**
  1265. * Return action of hook
  1266. * @param object Linked object
  1267. */
  1268. public function doActions($parameters=false, &$object, &$action='')
  1269. {
  1270. global $conf, $langs, $user;
  1271. global $mc;
  1272. if (empty($conf->multicompany->enabled)) {
  1273. return 0;
  1274. }
  1275. $langs->loadLangs(array('multicompany@multicompany'));
  1276. if (is_array($parameters) && !empty($parameters)) {
  1277. foreach($parameters as $key=>$value) {
  1278. $$key=$value;
  1279. }
  1280. }
  1281. $currentcontext = explode(':', $parameters['context']);
  1282. // Clear constants cache after company infos update
  1283. if (is_array($currentcontext)) {
  1284. if ((in_array('admincompany', $currentcontext) || in_array('adminihm', $currentcontext)) && ($action == 'update' || $action == 'updateedit')) {
  1285. clearCache('mc_entity_' . $conf->entity);
  1286. clearCache('mc_constants_' . $conf->entity);
  1287. } else if ((in_array('groupcard', $currentcontext) || in_array('groupperms', $currentcontext)) && $object->element == 'usergroup') {
  1288. global $entity;
  1289. // Users/Groups management only in master entity if transverse mode
  1290. if (!empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE) && ($conf->entity > 1 || ($conf->entity == 1 && !empty($user->entity)))) {
  1291. accessforbidden($langs->trans('ErrorOnlySuperadminCanManageUserAdnGroup'), 1, 1, 1);
  1292. }
  1293. if (!empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE)) {
  1294. $entity=(GETPOST('entity','int') ? GETPOST('entity','int') : $conf->entity);
  1295. } else {
  1296. $entity=(!empty($object->entity) ? $object->entity : $conf->entity);
  1297. }
  1298. // Add/Remove user into group
  1299. if (!empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE)
  1300. && in_array('groupcard', $currentcontext)
  1301. && ($action == 'adduser' || $action =='removeuser')
  1302. && (!empty($userid) && $userid > 0) && $caneditperms) {
  1303. if ($action == 'adduser') {
  1304. $entities = GETPOST("entities", "array", 3);
  1305. if (is_array($entities) && !empty($entities)) {
  1306. $error=0;
  1307. foreach ($entities as $entity_id) {
  1308. $object->fetch($id);
  1309. $object->oldcopy = clone $object;
  1310. $edituser = new User($this->db);
  1311. $edituser->fetch($userid);
  1312. $result=$edituser->SetInGroup($object->id, $entity_id);
  1313. if ($result < 0) {
  1314. $error++;
  1315. break;
  1316. }
  1317. }
  1318. if (empty($error)) {
  1319. header("Location: " . $_SERVER['PHP_SELF'] . '?id=' . $object->id);
  1320. exit;
  1321. } else {
  1322. $this->error = $edituser->error;
  1323. $this->errors = $edituser->errors;
  1324. return -1;
  1325. }
  1326. }
  1327. } elseif ($action == 'removeuser') {
  1328. $object->fetch($id);
  1329. $object->oldcopy = clone $object;
  1330. $edituser = new User($this->db);
  1331. $edituser->fetch($userid);
  1332. $result=$edituser->RemoveFromGroup($object->id, $entity);
  1333. if ($result > 0) {
  1334. header("Location: " . $_SERVER['PHP_SELF'] . "?id=" . $object->id);
  1335. exit;
  1336. } else {
  1337. $this->error = $object->error;
  1338. $this->errors = $object->errors;
  1339. return -1;
  1340. }
  1341. }
  1342. return 1;
  1343. }
  1344. } elseif ((in_array('usercard', $currentcontext) || in_array('userperms', $currentcontext)) && $object->element == 'user') {
  1345. global $entity, $caneditperms;
  1346. // With transverse mode, only superadmin can create Users/Groups in master entity
  1347. if (!empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE) && ($conf->entity > 1 || ($conf->entity == 1 && !empty($user->entity)))) {
  1348. if (in_array('usercard', $currentcontext) && ($action == 'create' || $action == 'adduserldap')) {
  1349. accessforbidden($langs->trans('ErrorOnlySuperadminCanManageUserAdnGroup'), 1, 1, 1);
  1350. } elseif (in_array('userperms', $currentcontext)) {
  1351. $caneditperms = false;
  1352. }
  1353. }
  1354. if (!empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE)) {
  1355. if (GETPOSTISSET('entity')) {
  1356. $entity = GETPOST('entity','int');
  1357. } else {
  1358. $entity = $conf->entity;
  1359. // Check usergroup if user not in master entity
  1360. if (in_array('userperms', $currentcontext) && !empty($user->admin) && empty($user->entity) && $conf->entity == 1) {
  1361. require_once DOL_DOCUMENT_ROOT.'/user/class/usergroup.class.php';
  1362. $group = new UserGroup($this->db);
  1363. $ret = $group->listGroupsForUser($object->id, false);
  1364. if (!empty(current($ret)->usergroup_entity)) {
  1365. sort(current($ret)->usergroup_entity);
  1366. if (current($ret)->usergroup_entity[0] > 1) {
  1367. $entity = current($ret)->usergroup_entity[0];
  1368. }
  1369. }
  1370. }
  1371. }
  1372. } else {
  1373. $entity=(!empty($object->entity) ? $object->entity : $conf->entity);
  1374. }
  1375. // Action add usergroup
  1376. if (!empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE)
  1377. && in_array('usercard', $currentcontext)
  1378. && ($action == 'addgroup' || $action == 'removegroup')
  1379. && (!empty($group) && $group > 0) && $caneditgroup) {
  1380. if ($action == 'addgroup') {
  1381. $entities = GETPOST("entities", "array", 3);
  1382. if (is_array($entities) && !empty($entities)) {
  1383. $error=0;
  1384. foreach ($entities as $entity_id) {
  1385. $object->fetch($id);
  1386. $result = $object->SetInGroup($group, $entity_id);
  1387. if ($result < 0) {
  1388. $error++;
  1389. break;
  1390. }
  1391. }
  1392. if ($error) {
  1393. $this->error = $object->error;
  1394. $this->errors = $object->errors;
  1395. return -1;
  1396. } else {
  1397. header("Location: " . $_SERVER['PHP_SELF'] . '?id=' . $object->id);
  1398. exit;
  1399. }
  1400. }
  1401. } else if ($action == 'removegroup') {
  1402. $object->fetch($id);
  1403. $result = $object->RemoveFromGroup($group, $entity);
  1404. if ($result > 0) {
  1405. header("Location: " . $_SERVER['PHP_SELF'] . '?id=' . $object->id);
  1406. exit;
  1407. } else {
  1408. $this->error = $object->error;
  1409. $this->errors = $object->errors;
  1410. return -1;
  1411. }
  1412. }
  1413. return 1;
  1414. }
  1415. } elseif (in_array('productcard', $currentcontext) && ($object->element == 'product' || $object->element == 'service')) {
  1416. if ($action != 'create' && $action != 'add') {
  1417. if (!empty($object->entity) && $object->entity != $conf->entity) {
  1418. global $usercanread, $usercancreate, $usercandelete;
  1419. /*if (empty($user->rights->multicompany->product->read)) {
  1420. $usercanread = false;
  1421. }*/
  1422. if (empty($user->rights->multicompany->product->write)) {
  1423. $usercancreate = false;
  1424. }
  1425. if (empty($user->rights->multicompany->product->delete)) {
  1426. $usercandelete = false;
  1427. }
  1428. }
  1429. }
  1430. } elseif (!empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_ENABLED)
  1431. && !empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_BYELEMENT_ENABLED)
  1432. && in_array('thirdpartylist', $currentcontext)
  1433. && $this->checkIfElementIsSharedByEntity('thirdparty') === true
  1434. && $action == 'confirmsharebyelement'
  1435. && !empty(GETPOST('toselect', 'array', 2))
  1436. && GETPOST('confirm', 'aZ09') == 'yes') {
  1437. $objEntity = new DaoMulticompany($this->db);
  1438. if (empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARE_ALL_BY_DEFAULT)) {
  1439. $entities = GETPOST('thirdparty_to', 'array', 2); // limit to POST
  1440. } else {
  1441. $entities = GETPOST('from', 'array', 2); // limit to POST
  1442. }
  1443. $elementlist = GETPOST('toselect', 'array', 2); // limit to POST
  1444. $error = 0;
  1445. foreach ($elementlist as $elementid) {
  1446. $ret = $object->fetch($elementid);
  1447. if ($ret > 0) {
  1448. $contactlist = $object->contact_array();
  1449. if (!empty($contactlist)) {
  1450. foreach($contactlist as $key => $name) {
  1451. $ret = $objEntity->setSharingsByElement('contact', $key, $entities);
  1452. if ($ret < 0) {
  1453. $this->error = $ret->error;
  1454. $error++;
  1455. break;
  1456. }
  1457. }
  1458. }
  1459. if (!$error) {
  1460. $retupdate = $objEntity->setSharingsByElement('thirdparty', $object->id, $entities);
  1461. if ($retupdate < 0) {
  1462. $this->error = $retupdate->error;
  1463. $error++;
  1464. break;
  1465. }
  1466. }
  1467. }
  1468. }
  1469. if (!$error) {
  1470. setEventMessage('MassActionThirdpartySharingByElementIsOk');
  1471. return 1;
  1472. } else {
  1473. return -1;
  1474. }
  1475. } elseif (!empty($conf->global->MULTICOMPANY_PRODUCT_SHARING_ENABLED)
  1476. && !empty($conf->global->MULTICOMPANY_PRODUCT_SHARING_BYELEMENT_ENABLED)
  1477. && in_array('productservicelist', $currentcontext)
  1478. && $this->checkIfElementIsSharedByEntity('product') === true
  1479. && $action == 'confirmsharebyelement'
  1480. && !empty(GETPOST('toselect', 'array'))
  1481. && GETPOST('confirm', 'aZ09') == 'yes') {
  1482. $objEntity = new DaoMulticompany($this->db);
  1483. if (empty($conf->global->MULTICOMPANY_PRODUCT_SHARE_ALL_BY_DEFAULT)) {
  1484. $entities = GETPOST('product_to', 'array', 2); // limit to POST
  1485. } else {
  1486. $entities = GETPOST('from', 'array', 2); // limit to POST
  1487. }
  1488. $elementlist = GETPOST('toselect', 'array', 2); // limit to POST
  1489. foreach ($elementlist as $elementid) {
  1490. $ret = $object->fetch($elementid);
  1491. if ($ret > 0) {
  1492. $retupdate = $objEntity->setSharingsByElement('product', $object->id, $entities);
  1493. if ($retupdate < 0) {
  1494. $this->error = $retupdate->error;
  1495. $error++;
  1496. break;
  1497. }
  1498. }
  1499. }
  1500. if (!$error) {
  1501. setEventMessage('MassActionProductSharingByElementIsOk');
  1502. return 1;
  1503. } else {
  1504. return -1;
  1505. }
  1506. } elseif (in_array('accountancyadminaccount', $currentcontext) && $object->element == 'accounting_account' && !empty($permissiontoadd)) {
  1507. $this->getInfo($conf->entity);
  1508. if ($this->options['cloneaccountingaccount'] == 1 && GETPOST('valid_change_chart', 'alpha') && $chartofaccounts > 0) {
  1509. // Get language code for this $chartofaccounts
  1510. $sql = 'SELECT c.code, a.pcg_version FROM '.MAIN_DB_PREFIX.'c_country as c, '.MAIN_DB_PREFIX.'accounting_system as a';
  1511. $sql .= ' WHERE c.rowid = a.fk_country AND a.rowid = '.(int) $chartofaccounts;
  1512. $resql = $this->db->query($sql);
  1513. if ($resql) {
  1514. $obj = $this->db->fetch_object($resql);
  1515. $country_code = $obj->code;
  1516. $pcg_version = $obj->pcg_version;
  1517. } else {
  1518. dol_print_error($this->db);
  1519. }
  1520. // Try to load sql file
  1521. if (!empty($country_code)) {
  1522. $sqlfile = DOL_DOCUMENT_ROOT.'/install/mysql/data/llx_accounting_account_'.strtolower($country_code).'.sql';
  1523. $offsetforchartofaccount = 0;
  1524. $offsetforchartofaccounttoclone = 0;
  1525. // Get the comment line '-- ADD CCCNNNNN to rowid...' to find CCCNNNNN (CCC is country num, NNNNN is id of accounting account)
  1526. // and pass CCCNNNNN + (num of company * 100 000 000) as offset to the run_sql as a new parameter to say to update sql on the fly to add offset to rowid and account_parent value.
  1527. // This is to be sure there is no conflict for each chart of account, whatever is country, whatever is company when multicompany is used.
  1528. $tmp = file_get_contents($sqlfile);
  1529. $reg = array();
  1530. if (preg_match('/-- ADD (\d+) to rowid/ims', $tmp, $reg)) {
  1531. $offsetforchartofaccount += $reg[1];
  1532. $offsetforchartofaccounttoclone += $reg[1];
  1533. }
  1534. $offsetforchartofaccount += ($conf->entity * 100000000);
  1535. $offsetforchartofaccounttoclone += (GETPOST('entitytoclone', 'int') * 100000000);
  1536. $sqldel = "DELETE FROM ".MAIN_DB_PREFIX."accounting_account WHERE entity = ".$conf->entity." AND fk_pcg_version = '".$pcg_version."'";
  1537. $resqldel = $this->db->query($sqldel);
  1538. $sql = "SELECT * FROM ".MAIN_DB_PREFIX."accounting_account WHERE entity = ".GETPOST('entitytoclone', 'int')." AND fk_pcg_version = '".$pcg_version."'";
  1539. $resql = $this->db->query($sql);
  1540. if (!empty($resql)) {
  1541. $i = 0;
  1542. $num_rows = $this->db->num_rows($resql);
  1543. while ($i < $num_rows) {
  1544. $array = $this->db->fetch_array($resql);
  1545. foreach($array as $key => $value){
  1546. unset($array['tms']);
  1547. if (is_int($key) || empty($value)) {
  1548. unset($array[$key]);
  1549. }
  1550. }
  1551. $array['entity'] = $conf->entity;
  1552. $array['rowid'] -= $offsetforchartofaccounttoclone;
  1553. $array['rowid'] += $offsetforchartofaccount;
  1554. if ($array['account_parent'] > 0) {
  1555. $array['account_parent'] -= $offsetforchartofaccounttoclone;
  1556. $array['account_parent'] += $offsetforchartofaccount;
  1557. }
  1558. $sqladd = "INSERT INTO ".MAIN_DB_PREFIX."accounting_account (";
  1559. $sqladd.= implode(',', array_keys($array));
  1560. $sqladd.= ") VALUES (";
  1561. foreach($array as $key => $value) {
  1562. if (is_string($value)) {
  1563. $sqladd.= "'".$this->db->escape($value)."'";
  1564. } else {
  1565. $sqladd.= (int) $value;
  1566. }
  1567. if (end(array_keys($array)) != $key) {
  1568. $sqladd.= ",";
  1569. }
  1570. }
  1571. $sqladd.= ")";
  1572. //echo $sqladd.'<br>';
  1573. if (!$this->db->query($sqladd)) {
  1574. $error++;
  1575. $this->error.= $this->db->lasterror();
  1576. dol_syslog(get_class($this)."::Add accounting_account erreur -1 ".$this->error, LOG_ERR);
  1577. }
  1578. $i++;
  1579. }
  1580. }
  1581. if (!$error) {
  1582. setEventMessages($langs->trans("ChartLoaded"), null, 'mesgs');
  1583. } else {
  1584. setEventMessages($langs->trans("ErrorDuringChartLoad"), null, 'warnings');
  1585. }
  1586. }
  1587. if (!dolibarr_set_const($this->db, 'CHARTOFACCOUNTS', $chartofaccounts, 'chaine', 0, '', $conf->entity)) {
  1588. $error++;
  1589. }
  1590. return 1;
  1591. }
  1592. } else {
  1593. if (!empty($object->entity) && $object->entity != $conf->entity && in_array('globalcard', $currentcontext)) {
  1594. foreach ($this->sharingelements as $element => $params) {
  1595. if ($params['type'] === 'objectnumber' && empty($params['disable']) && $object->element == $this->sharingmodulename[$element]) {
  1596. if (is_array($params['constants']) && !empty($params['constants'])) {
  1597. $this->getInstanceDao();
  1598. // Check if current entity not use the objectnumber sharing
  1599. $constnumber = 'MULTICOMPANY_'.strtoupper($element).'_SHARING_ENABLED';
  1600. if (empty($conf->global->$constnumber) || empty($mc->sharings[$element])) {
  1601. foreach ($params['constants'] as $constname) {
  1602. $res = $this->dao->getEntityConfig($object->entity, $constname);
  1603. if (!empty($res[$constname])) {
  1604. $conf->global->$constname = $res[$constname]; // override current entity config with object entity config
  1605. }
  1606. }
  1607. } else {
  1608. $varname = $element.'_referent';
  1609. $referent = (!empty($mc->$varname) ? $mc->$varname : 1);
  1610. foreach ($params['constants'] as $constname) {
  1611. $res = $this->dao->getEntityConfig($referent, $constname);
  1612. if (!empty($res[$constname])) {
  1613. $conf->global->$constname = $res[$constname]; // override current entity config with referent config
  1614. }
  1615. }
  1616. }
  1617. }
  1618. } elseif ($params['type'] === 'object' && empty($params['disable'])
  1619. && ((isset($this->sharingmodulename[$element]) && $object->element == $this->sharingmodulename[$element]) || $object->element == $element)) {
  1620. $templatepath = dol_buildpath('/multicompany/core/tpl/override_'.$element.'_userrights.tpl.php', 0, 1);
  1621. if (!empty($templatepath)) {
  1622. include $templatepath;
  1623. }
  1624. }
  1625. }
  1626. }
  1627. }
  1628. }
  1629. //var_dump($_POST);
  1630. /*
  1631. if (empty($_SESSION['dol_tables_list_fk_soc']))
  1632. {
  1633. $_SESSION['dol_tables_list_fk_soc'] = getTablesWithField('fk_soc', array());
  1634. }
  1635. var_dump($_SESSION['dol_tables_list_fk_soc']);
  1636. */
  1637. //$include=false;
  1638. //$exclude=false;
  1639. /*
  1640. $exclude = array(
  1641. MAIN_DB_PREFIX . 'user',
  1642. MAIN_DB_PREFIX . 'user_employment',
  1643. MAIN_DB_PREFIX . 'user_param',
  1644. MAIN_DB_PREFIX . 'user_rib',
  1645. MAIN_DB_PREFIX . 'user_rights',
  1646. MAIN_DB_PREFIX . 'usergroup',
  1647. MAIN_DB_PREFIX . 'usergroup_rights',
  1648. MAIN_DB_PREFIX . 'usergroup_user',
  1649. MAIN_DB_PREFIX . 'rights_def',
  1650. );
  1651. */
  1652. //$exclude = '/(const|user|rights\_def)+/';
  1653. //$include = '/(const|user|rights\_def)+/';
  1654. //if (empty($_SESSION['dol_tables_list_entity']))
  1655. /* {
  1656. $_SESSION['dol_tables_list_entity'] = getTablesWithField('entity', $exclude, $include);
  1657. }
  1658. var_dump($_SESSION['dol_tables_list_entity']);
  1659. */
  1660. /*
  1661. if (!empty($conf->global->MULTICOMPANY_SHARINGS_ENABLED) && !empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_ENABLED) && !empty($mc->sharings['thirdparty']))
  1662. {
  1663. if (in_array($contextpage, $this->thirdpartycontextlist) || in_array($contextpage, $this->contactcontextlist))
  1664. {
  1665. if (GETPOST('confirmmassaction') && GETPOST('massaction') == 'modify_entity')
  1666. {
  1667. var_dump($_POST['toselect']);
  1668. }
  1669. }
  1670. }
  1671. */
  1672. return 0;
  1673. }
  1674. /**
  1675. *
  1676. * @param boolean $parameters
  1677. * @param unknown $object
  1678. * @param string $action
  1679. * @return number
  1680. */
  1681. public function commonGenerateDocument($parameters=false, &$object, &$action='')
  1682. {
  1683. global $conf, $mysoc;
  1684. if (empty($conf->multicompany->enabled)) {
  1685. return 0;
  1686. }
  1687. if (!is_object($object)) {
  1688. return 0;
  1689. }
  1690. // Override $mysoc with data of object entity
  1691. if (!empty($object->entity) && $object->entity != $conf->entity) {
  1692. foreach ($this->sharingelements as $element => $params) {
  1693. if ($params['type'] === 'object' && empty($params['disable']) && ($object->element == $this->sharingmodulename[$element] || $object->element == $element )) {
  1694. $this->getInstanceDao();
  1695. $current = $this->dao->getEntityConfig($conf->entity, 'MAIN_INFO_*');
  1696. if (!empty($current)) {
  1697. foreach($current as $constname => $value) {
  1698. unset($conf->global->$constname);
  1699. }
  1700. }
  1701. $other = $this->dao->getEntityConfig($object->entity, 'MAIN_INFO_*');
  1702. if (!empty($other)) {
  1703. foreach($other as $constname => $value) {
  1704. $conf->global->$constname = $value; // override current entity config with referent config
  1705. }
  1706. }
  1707. $mysoc->setMysoc($conf);
  1708. break;
  1709. }
  1710. }
  1711. }
  1712. return 0;
  1713. }
  1714. /**
  1715. *
  1716. * @param boolean $parameters
  1717. * @param unknown $object
  1718. * @param string $action
  1719. * @return number
  1720. */
  1721. public function showLinkedObjectBlock($parameters=false, &$object, &$action='')
  1722. {
  1723. global $conf, $user, $langs;
  1724. global $mc;
  1725. if (empty($conf->multicompany->enabled)) {
  1726. return 0;
  1727. }
  1728. if (is_array($parameters) && !empty($parameters)) {
  1729. foreach($parameters as $key=>$value) {
  1730. $$key=$value;
  1731. }
  1732. }
  1733. $currentcontext = explode(':', $parameters['context']);
  1734. //var_dump($object->linkedObjects);
  1735. foreach($object->linkedObjects as $objecttype => $objects) {
  1736. foreach($objects as $key => $tmpobj) {
  1737. if (empty($tmpobj->entity)) {
  1738. continue; // for debug
  1739. }
  1740. if ($tmpobj->entity != $conf->entity) {
  1741. $element = $objecttype;
  1742. if ($objecttype == 'propal') {
  1743. $element = 'proposal';
  1744. } elseif ($objecttype == 'commande') {
  1745. $element = 'order';
  1746. } elseif ($objecttype == 'facture') {
  1747. $element = 'invoice';
  1748. }
  1749. //var_dump($element);var_dump($mc->sharings[$element]);
  1750. //var_dump($object->linkedObjects[$objecttype][$key]);
  1751. if (!empty($mc->sharings[$element]) && in_array($tmpobj->entity, $mc->sharings[$element])) {
  1752. //nothing
  1753. } else {
  1754. unset($object->linkedObjects[$objecttype][$key]);
  1755. }
  1756. }
  1757. }
  1758. }
  1759. return 0;
  1760. }
  1761. /**
  1762. *
  1763. * @param boolean $parameters
  1764. * @param unknown $object
  1765. * @param string $action
  1766. * @return number
  1767. */
  1768. public function showLinkToObjectBlock($parameters=false, &$object, &$action='')
  1769. {
  1770. global $conf, $user;
  1771. if (empty($conf->multicompany->enabled)) {
  1772. return 0;
  1773. }
  1774. if (is_array($parameters) && !empty($parameters)) {
  1775. foreach($parameters as $key=>$value) {
  1776. $$key=$value;
  1777. }
  1778. }
  1779. $currentcontext = explode(':', $parameters['context']);
  1780. if (!empty($object->entity) && $object->entity != $conf->entity) {
  1781. if (empty($possiblelinks)) { // old method
  1782. $propalperms = 1;
  1783. $orderperms = 1;
  1784. $invoiceperms = 1;
  1785. if (in_array('propalcard', $currentcontext) && $object->element == 'propal') {
  1786. $propalperms = !empty($user->rights->multicompany->propal->write);
  1787. } elseif (in_array('ordercard', $currentcontext) && $object->element == 'commande') {
  1788. $orderperms = !empty($user->rights->multicompany->order->write);
  1789. } elseif (in_array('invoicecard', $currentcontext) && $object->element == 'facture') {
  1790. $invoiceperms = !empty($user->rights->multicompany->invoice->write);
  1791. }
  1792. $this->results = array(
  1793. 'propal' => array(
  1794. 'enabled' => $conf->propal->enabled,
  1795. 'perms' => $propalperms,
  1796. 'label' => 'LinkToProposal',
  1797. 'sql' => "SELECT s.rowid as socid, s.nom as name, s.client, t.rowid, t.ref, t.ref_client, t.total_ht FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."propal as t WHERE t.fk_soc = s.rowid AND t.fk_soc IN (".$listofidcompanytoscan.') AND t.entity IN ('.getEntity('propal').')'),
  1798. 'order' => array(
  1799. 'enabled' => $conf->commande->enabled,
  1800. 'perms' => $orderperms,
  1801. 'label' => 'LinkToOrder',
  1802. 'sql' => "SELECT s.rowid as socid, s.nom as name, s.client, t.rowid, t.ref, t.ref_client, t.total_ht FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."commande as t WHERE t.fk_soc = s.rowid AND t.fk_soc IN (".$listofidcompanytoscan.') AND t.entity IN ('.getEntity('commande').')'),
  1803. 'invoice' => array(
  1804. 'enabled' => $conf->facture->enabled,
  1805. 'perms' => $invoiceperms,
  1806. 'label' => 'LinkToInvoice',
  1807. 'sql' => "SELECT s.rowid as socid, s.nom as name, s.client, t.rowid, t.ref, t.ref_client, t.total_ht FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."facture as t WHERE t.fk_soc = s.rowid AND t.fk_soc IN (".$listofidcompanytoscan.') AND t.entity IN ('.getEntity('invoice').')')
  1808. );
  1809. return 0;
  1810. } else {
  1811. if (!empty($conf->global->MULTICOMPANY_PROPOSAL_SHARING_ENABLED) && in_array('propalcard', $currentcontext) && $object->element == 'propal') {
  1812. $possiblelinks['propal']['perms'] = !empty($user->rights->multicompany->propal->write);
  1813. } elseif (!empty($conf->global->MULTICOMPANY_ORDER_SHARING_ENABLED) && in_array('ordercard', $currentcontext) && $object->element == 'commande') {
  1814. $possiblelinks['order']['perms'] = !empty($user->rights->multicompany->order->write);
  1815. } elseif (!empty($conf->global->MULTICOMPANY_INVOICE_SHARING_ENABLED) && in_array('invoicecard', $currentcontext) && $object->element == 'facture') {
  1816. $possiblelinks['invoice']['perms'] = !empty($user->rights->multicompany->invoice->write);
  1817. }
  1818. $this->results = $possiblelinks;
  1819. return 1;
  1820. }
  1821. }
  1822. return 0;
  1823. }
  1824. /**
  1825. *
  1826. * @param boolean $parameters
  1827. * @param unknown $object
  1828. * @param string $action
  1829. * @return number
  1830. */
  1831. public function addMoreActionsButtons($parameters=false, &$object, &$action='')
  1832. {
  1833. global $conf, $user, $langs;
  1834. if (empty($conf->multicompany->enabled)) {
  1835. return 0;
  1836. }
  1837. if (is_array($parameters) && !empty($parameters)) {
  1838. foreach($parameters as $key=>$value) {
  1839. $$key=$value;
  1840. }
  1841. }
  1842. $currentcontext = explode(':', $parameters['context']);
  1843. /*if (in_array('productcard', $currentcontext) && ($object->element == 'product' || $object->element == 'service'))
  1844. {
  1845. if ($object->entity != $conf->entity)
  1846. {
  1847. $user->rights->produit->creer = 0;
  1848. $user->rights->produit->supprimer = 0;
  1849. $user->rights->service->creer = 0;
  1850. $user->rights->service->supprimer = 0;
  1851. //return 1;
  1852. }
  1853. }*/
  1854. return 0;
  1855. }
  1856. /**
  1857. *
  1858. * @param boolean $parameters
  1859. * @param unknown $object
  1860. * @param string $action
  1861. * @return number
  1862. */
  1863. public function printUserPasswordField($parameters=false, &$object, &$action='')
  1864. {
  1865. global $conf, $user, $langs;
  1866. if (empty($conf->multicompany->enabled)) {
  1867. return 0;
  1868. }
  1869. $langs->load('multicompany@multicompany');
  1870. if (is_array($parameters) && !empty($parameters)) {
  1871. foreach($parameters as $key=>$value) {
  1872. $$key=$value;
  1873. }
  1874. }
  1875. $currentcontext = explode(':', $parameters['context']);
  1876. $this->resprints = "\n".'<!-- BEGIN multicompany printUserPasswordField -->'."\n";
  1877. if (in_array('usercard', $currentcontext) && $object->element == 'user' && !empty($conf->global->MULTICOMPANY_HIDE_LOGIN_COMBOBOX) && checkMulticompanyAutentication()) {
  1878. if ($action == 'create') {
  1879. $this->resprints.= '<input size="30" maxsize="32" type="text" name="password" value="" autocomplete="new-password">';
  1880. } else if ($action == 'edit') {
  1881. if (!empty($caneditpassword)) {
  1882. $this->resprints.= '<input size="30" maxlength="32" type="password" class="flat" name="password" value="'.$object->pass.'" autocomplete="new-password">';
  1883. } else {
  1884. $this->resprints.= preg_replace('/./i','*',$object->pass);
  1885. }
  1886. } else {
  1887. if (!empty($object->pass)) {
  1888. $this->resprints.= preg_replace('/./i','*',$object->pass);
  1889. } else {
  1890. if (!empty($user->admin)) {
  1891. $this->resprints.= ($valuetoshow?(' '.$langs->trans("or").' '):'').$langs->trans("Crypted").': '.$object->pass_indatabase_crypted;
  1892. } else {
  1893. $this->resprints.= $langs->trans("Hidden");
  1894. }
  1895. }
  1896. }
  1897. }
  1898. $this->resprints.= '<!-- END multicompany printUserPasswordField -->'."\n";
  1899. return 0;
  1900. }
  1901. /**
  1902. *
  1903. * @param boolean $parameters
  1904. * @param unknown $object
  1905. * @param string $action
  1906. * @return number
  1907. */
  1908. public function formConfirm($parameters=false, &$object, &$action='')
  1909. {
  1910. global $conf, $langs;
  1911. global $mc, $form;
  1912. if (empty($conf->multicompany->enabled)) {
  1913. return 0;
  1914. }
  1915. $langs->load('multicompany@multicompany');
  1916. if (is_array($parameters) && !empty($parameters)) {
  1917. foreach($parameters as $key=>$value) {
  1918. $$key=$value;
  1919. }
  1920. }
  1921. $return = 0;
  1922. $currentcontext = explode(':', $parameters['context']);
  1923. $this->resprints = "\n".'<!-- BEGIN multicompany formConfirm -->'."\n";
  1924. if (in_array('propalcard', $currentcontext) && $object->element == 'propal') {
  1925. if ($action == 'clone' && !empty($mc->entities['proposal'])) {
  1926. // Create an array for form
  1927. $formquestion = array(
  1928. array('type' => 'other', 'name' => 'socid', 'label' => $langs->trans("SelectThirdParty"), 'value' => $form->select_company(GETPOST('socid', 'int'), 'socid', '(s.client=1 OR s.client=2 OR s.client=3)', '', 0, 0, null, 0, 'maxwidth300')),
  1929. array('type' => 'other', 'name' => 'entity', 'label' => $langs->trans("SelectEntity"), 'value' => $mc->select_entities($conf->entity, 'entity', '', false, false, false, explode(',', $mc->entities['proposal']))),
  1930. array('type' => 'checkbox', 'name' => 'update_prices', 'label' => $langs->trans('PuttingPricesUpToDate'), 'value' => (!empty($conf->global->PROPOSAL_CLONE_UPDATE_PRICES) ? 1 : 0)),
  1931. );
  1932. if (!empty($conf->global->PROPAL_CLONE_DATE_DELIVERY) && !empty($object->delivery_date)) {
  1933. $formquestion[] = array('type' => 'date', 'name' => 'date_delivery', 'label' => $langs->trans("DeliveryDate"), 'value' => $object->delivery_date);
  1934. }
  1935. $this->resprints = "\n".'<!-- BEGIN multicompany formConfirm -->'."\n"; // to avoid conflict with hook of selectThirdpartyListWhere
  1936. // Incomplete payment. We ask if reason = discount or other
  1937. $this->resprints.= $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('ToClone'), $langs->trans('ConfirmClonePropal', $object->ref), 'confirm_clone', $formquestion, 'yes', 1, 0, 600);
  1938. $return++;
  1939. }
  1940. } /*elseif (in_array('invoicecard', $currentcontext) && $object->element == 'facture') {
  1941. if ($action == 'clone' && !empty($mc->entities['invoice'])) {
  1942. // Create an array for form
  1943. $formquestion = array(
  1944. array('type' => 'other', 'name' => 'socid', 'label' => $langs->trans("SelectThirdParty"), 'value' => $form->select_company(GETPOST('socid', 'int'), 'socid', '(s.client=1 OR s.client=2 OR s.client=3)')),
  1945. array('type' => 'other', 'name' => 'entity', 'label' => $langs->trans("SelectEntity"), 'value' => $mc->select_entities($conf->entity, 'entity', '', false, false, false, explode(',', $mc->entities['invoice']))),
  1946. array('type' => 'date', 'name' => 'newdate', 'label' => $langs->trans("Date"), 'value' => dol_now())
  1947. );
  1948. $this->resprints = "\n".'<!-- BEGIN multicompany formConfirm -->'."\n"; // to avoid conflict with hook of selectThirdpartyListWhere
  1949. // Incomplete payment. We ask if reason = discount or other
  1950. $this->resprints.= $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('ToClone'), $langs->trans('ConfirmCloneInvoice', $object->ref), 'confirm_clone', $formquestion, 'yes', 1);
  1951. $return++;
  1952. }
  1953. }*/
  1954. $this->resprints.= '<!-- END multicompany formConfirm -->'."\n";
  1955. return $return;
  1956. }
  1957. /**
  1958. *
  1959. * @param boolean $parameters
  1960. * @param unknown $object
  1961. * @param string $action
  1962. * @return number
  1963. */
  1964. public function formObjectOptions($parameters=false, &$object, &$action='')
  1965. {
  1966. global $conf, $user, $langs;
  1967. global $form;
  1968. if (empty($conf->multicompany->enabled)) {
  1969. return 0;
  1970. }
  1971. $langs->load('multicompany@multicompany');
  1972. if (is_array($parameters) && !empty($parameters)) {
  1973. foreach($parameters as $key=>$value) {
  1974. $$key=$value;
  1975. }
  1976. }
  1977. $currentcontext = explode(':', $parameters['context']);
  1978. $this->resprints = "\n".'<!-- BEGIN multicompany formObjectOptions -->'."\n";
  1979. if (in_array('usercard', $currentcontext) && $object->element == 'user') {
  1980. if ($action == 'edit') {
  1981. // TODO check if user not linked with the current entity before change entity (thirdparty, invoice, etc.) !!
  1982. if (empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE) && $conf->entity == 1 && !empty($user->admin) && empty($user->entity)) {
  1983. $this->resprints.= '<tr><td>'.$langs->trans("Entity").'</td>';
  1984. $this->resprints.= '<td>'.$this->select_entities($object->entity);
  1985. $this->resprints.= "</td></tr>\n";
  1986. } else {
  1987. $this->resprints.= '<input type="hidden" name="entity" value="'.$conf->entity.'" />';
  1988. }
  1989. } elseif ($action == 'create') {
  1990. if (empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE) && $conf->entity == 1 && !empty($user->admin) && empty($user->entity)) {
  1991. $this->resprints.= '<tr><td>'.$langs->trans("Entity").'</td>';
  1992. $this->resprints.= '<td>'.$this->select_entities($conf->entity);
  1993. $this->resprints.= "</td></tr>\n";
  1994. } else {
  1995. $this->resprints.= '<input type="hidden" name="entity" value="'.$conf->entity.'" />';
  1996. }
  1997. } elseif ($action != 'adduserldap') {
  1998. if (empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE) && $conf->entity == 1 && !empty($user->admin) && empty($user->entity)) {
  1999. $this->resprints.= '<tr><td>'.$langs->trans("Entity").'</td><td>';
  2000. if (empty($object->entity)) {
  2001. $this->resprints.= $langs->trans("AllEntities");
  2002. } else {
  2003. $staticentity = new self($this->db);
  2004. $staticentity->getInfo($object->entity);
  2005. $this->resprints.= $staticentity->label;
  2006. }
  2007. $this->resprints.= "</td></tr>\n";
  2008. }
  2009. }
  2010. } elseif (!empty($conf->global->MULTICOMPANY_SHARINGS_ENABLED)) {
  2011. if (!empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_ENABLED)
  2012. && in_array('thirdpartycard', $currentcontext) && $object->element == 'societe'
  2013. && !empty($user->admin) && empty($user->entity)) {
  2014. if (empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_BYELEMENT_ENABLED)) {
  2015. if ($action == 'create') {
  2016. $this->resprints.= '<tr><td>'.fieldLabel('Entity','entity').'</td><td colspan="3" class="maxwidthonsmartphone">';
  2017. $s = $this->select_entities($conf->entity);
  2018. $this->resprints.= $form->textwithpicto($s,$langs->trans("ThirdpartyEntityDesc"),1);
  2019. $this->resprints.= '</td></tr>'."\n";
  2020. }
  2021. } elseif (!empty($conf->global->MULTICOMPANY_SHARING_BYELEMENT_ENABLED) && $this->checkIfElementIsSharedByEntity('thirdparty') === true) {
  2022. if ($action == 'create' || $action == 'edit') {
  2023. if ($action == 'edit') {
  2024. // We can define the granularity of the sharing of the element only in the origin entity of this element
  2025. if (!empty($object->entity) && $object->entity != $conf->entity) {
  2026. return 0;
  2027. }
  2028. }
  2029. $this->resprints.= '<tr><td>'.fieldLabel('MulticompanySharingElementByEntity','entity').'</td><td colspan="3" class="maxwidthonsmartphone">'."\n";
  2030. $this->resprints.= $this->formSelectSharingByElement($object, 'thirdparty'); // "thirdparty" is used instead element name "societe" (for compatibility)
  2031. $this->resprints.= '</td></tr>'."\n";
  2032. } else {
  2033. // We can define the granularity of the sharing of the element only in the origin entity of this element
  2034. if (!empty($object->entity) && $object->entity != $conf->entity) {
  2035. return 0;
  2036. }
  2037. $this->getSharingsByElement('thirdparty', $object->id); // Populate $this->config['sharedwith']
  2038. if (!empty($this->config['sharedwith']['thirdparty'][$object->id])) {
  2039. $this->resprints.= '<tr><td>'.$langs->trans("MulticompanySharedWithThisEntities").'</td><td>';
  2040. foreach ($this->config['sharedwith']['thirdparty'][$object->id] as $sharedwithentity) {
  2041. if ($sharedwithentity == $object->entity) {
  2042. continue;
  2043. }
  2044. $otherentity = new self($this->db);
  2045. $otherentity->getInfo($sharedwithentity);
  2046. $this->resprints.= '<div class="refidno multicompany-entity-card-container">';
  2047. $this->resprints.= '<span class="fa fa-globe"></span><span class="multiselect-selected-title-text">'.$otherentity->label.'</span>';
  2048. $this->resprints.= "</div>\n";
  2049. }
  2050. $this->resprints.= "</td></tr>\n";
  2051. }
  2052. }
  2053. }
  2054. } elseif (!empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_ENABLED)
  2055. && !empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_BYELEMENT_ENABLED)
  2056. && in_array('contactcard', $currentcontext) && $object->element == 'contact'
  2057. && !empty($user->admin) && empty($user->entity)) {
  2058. if (empty($conf->global->MULTICOMPANY_CONTACT_SHARING_BYELEMENT_ENABLED)) {
  2059. if ($action == 'create' || $action == 'edit') { // Action create is managed by the trigger
  2060. if ($action == 'edit') {
  2061. // We can define the granularity of the sharing of the element only in the origin entity of this element
  2062. if (!empty($object->entity) && $object->entity != $conf->entity) {
  2063. return 0;
  2064. }
  2065. $this->getSharingsByElement($object->element, $object->id); // Populate $this->config['sharedwith']
  2066. if (!empty($this->config['sharedwith'][$object->element][$object->id])) {
  2067. $this->resprints.= '<tr><td colspan="4">';
  2068. $this->resprints.= '<input type="hidden" name="oldsharedwith" value="' . base64_encode(json_encode($this->config['sharedwith'][$object->element][$object->id])) . '">';
  2069. $this->resprints.= '</td></tr>'."\n";
  2070. }
  2071. }
  2072. } else {
  2073. $this->getSharingsByElement($object->element, $object->id); // Populate $this->config['sharedwith']
  2074. if (!empty($this->config['sharedwith'][$object->element][$object->id])) {
  2075. $this->resprints.= '<tr><td>'.$langs->trans("MulticompanySharedWithThisEntities").'</td><td>';
  2076. foreach ($this->config['sharedwith'][$object->element][$object->id] as $sharedwithentity) {
  2077. if ($sharedwithentity == $object->entity) {
  2078. continue;
  2079. }
  2080. $otherentity = new self($this->db);
  2081. $otherentity->getInfo($sharedwithentity);
  2082. $this->resprints.= '<div class="refidno multicompany-entity-card-container">';
  2083. $this->resprints.= '<span class="fa fa-globe"></span><span class="multiselect-selected-title-text">'.$otherentity->label.'</span>';
  2084. $this->resprints.= "</div>\n";
  2085. }
  2086. $this->resprints.= "</td></tr>\n";
  2087. }
  2088. }
  2089. } elseif (!empty($conf->global->MULTICOMPANY_SHARING_BYELEMENT_ENABLED) && $this->checkIfElementIsSharedByEntity('thirdparty') === true) {
  2090. if ($action == 'create' || $action == 'edit') {
  2091. if ($action == 'edit') {
  2092. // We can define the granularity of the sharing of the element only in the origin entity of this element
  2093. if (!empty($object->entity) && $object->entity != $conf->entity) {
  2094. return 0;
  2095. }
  2096. }
  2097. $socid = null;
  2098. $parentgranularity = null;
  2099. if ($action == 'create' && is_object($objsoc) && !empty($objsoc->id)) {
  2100. $socid = $objsoc->id;
  2101. } elseif ($action == 'edit' && is_object($object) && !empty($object->socid)) {
  2102. $socid = $object->socid;
  2103. }
  2104. if (!empty($socid)) {
  2105. // Check if parents element have sharings
  2106. $parentgranularity = $this->dao->getListOfSharingsByElement('thirdparty', $socid);
  2107. }
  2108. $visible = ((((isset($objsoc) && is_object($objsoc) && !empty($objsoc->id)) || !empty($object->socid)) && !empty($parentgranularity)) ? true : false);
  2109. $this->resprints.= '<tr id="formselectsharingbyelement"'.($visible ? '' : ' style="display:none;"').'>';
  2110. $this->resprints.= '<td>'.fieldLabel('MulticompanySharingElementByEntity','entity').'</td><td colspan="3" class="maxwidthonsmartphone">'."\n";
  2111. $this->resprints.= $this->formSelectSharingByElement($object, null, 'thirdparty', $socid);
  2112. $this->resprints.= '</td></tr>'."\n";
  2113. } else {
  2114. // We can define the granularity of the sharing of the element only in the origin entity of this element
  2115. if (!empty($object->entity) && $object->entity != $conf->entity) {
  2116. return 0;
  2117. }
  2118. $this->getSharingsByElement($object->element, $object->id); // Populate $this->config['sharedwith']
  2119. if (!empty($this->config['sharedwith'][$object->element][$object->id])) {
  2120. $this->resprints.= '<tr><td>'.$langs->trans("MulticompanySharedWithThisEntities").'</td><td>';
  2121. foreach ($this->config['sharedwith'][$object->element][$object->id] as $sharedwithentity) {
  2122. if ($sharedwithentity == $object->entity) {
  2123. continue;
  2124. }
  2125. $otherentity = new self($this->db);
  2126. $otherentity->getInfo($sharedwithentity);
  2127. $this->resprints.= '<div class="refidno multicompany-entity-card-container">';
  2128. $this->resprints.= '<span class="fa fa-globe"></span><span class="multiselect-selected-title-text">'.$otherentity->label.'</span>';
  2129. $this->resprints.= "</div>\n";
  2130. }
  2131. $this->resprints.= "</td></tr>\n";
  2132. }
  2133. }
  2134. }
  2135. } elseif (!empty($conf->global->MULTICOMPANY_PRODUCT_SHARING_ENABLED)
  2136. && in_array('productcard', $currentcontext) && $object->element == 'product'
  2137. && !empty($user->admin) && empty($user->entity)) {
  2138. if (empty($conf->global->MULTICOMPANY_PRODUCT_SHARING_BYELEMENT_ENABLED)) {
  2139. if ($action == 'create') {
  2140. $this->resprints.= '<tr><td>'.fieldLabel('Entity','entity').'</td><td colspan="3" class="maxwidthonsmartphone">';
  2141. $s = $this->select_entities($conf->entity);
  2142. $this->resprints.= $form->textwithpicto($s, $langs->trans("ProductEntityDesc"), 1);
  2143. $this->resprints.= '</td></tr>'."\n";
  2144. }
  2145. } elseif (!empty($conf->global->MULTICOMPANY_SHARING_BYELEMENT_ENABLED) && $this->checkIfElementIsSharedByEntity($object->element) === true) {
  2146. if ($action == 'create' || $action == 'edit') {
  2147. if ($action == 'edit') {
  2148. // We can define the granularity of the sharing of the element only in the origin entity of this element
  2149. if (!empty($object->entity) && $object->entity != $conf->entity) {
  2150. return 0;
  2151. }
  2152. }
  2153. $this->resprints.= '<tr><td>'.fieldLabel('MulticompanySharingElementByEntity','entity').'</td><td colspan="3" class="maxwidthonsmartphone">'."\n";
  2154. $this->resprints.= $this->formSelectSharingByElement($object);
  2155. $this->resprints.= '</td></tr>'."\n";
  2156. } else {
  2157. // We can define the granularity of the sharing of the element only in the origin entity of this element
  2158. if (!empty($object->entity) && $object->entity != $conf->entity) {
  2159. return 0;
  2160. }
  2161. $this->getSharingsByElement($object->element, $object->id); // Populate $this->config['sharedwith']
  2162. if (!empty($this->config['sharedwith'][$object->element][$object->id])) {
  2163. $this->resprints.= '<tr><td>'.$langs->trans("MulticompanySharedWithThisEntities").'</td><td>';
  2164. foreach ($this->config['sharedwith'][$object->element][$object->id] as $sharedwithentity) {
  2165. if ($sharedwithentity == $object->entity) {
  2166. continue;
  2167. }
  2168. $otherentity = new self($this->db);
  2169. $otherentity->getInfo($sharedwithentity);
  2170. $this->resprints.= '<div class="refidno multicompany-entity-card-container">';
  2171. $this->resprints.= '<span class="fa fa-globe"></span><span class="multiselect-selected-title-text">'.$otherentity->label.'</span>';
  2172. $this->resprints.= "</div>\n";
  2173. }
  2174. $this->resprints.= "</td></tr>\n";
  2175. }
  2176. }
  2177. }
  2178. } elseif (!empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_ENABLED)
  2179. && ((!empty($conf->global->MULTICOMPANY_PROPOSAL_SHARING_ENABLED) && in_array('propalcard', $currentcontext) && $object->element == 'propal')
  2180. || (!empty($conf->global->MULTICOMPANY_ORDER_SHARING_ENABLED) && in_array('ordercard', $currentcontext) && $object->element == 'commande')
  2181. || (!empty($conf->global->MULTICOMPANY_INVOICE_SHARING_ENABLED) && in_array('invoicecard', $currentcontext) && $object->element == 'facture'))) {
  2182. if ($action == 'create') {
  2183. $this->resprints.= '<tr><td>'.$langs->trans("Entity").'</td>';
  2184. $this->resprints.= '<td>'.$this->select_entities($conf->entity);
  2185. $this->resprints.= "</td></tr>\n";
  2186. }
  2187. } elseif (in_array('accountancyadminaccount', $currentcontext) && $object->element == 'accounting_account' && !empty($permissiontoadd)) {
  2188. $this->getInfo($conf->entity);
  2189. if ($this->options['cloneaccountingaccount'] == 1) {
  2190. $this->resprints.= $langs->trans("SelectEntityToCloneAccounts");
  2191. $this->resprints.= $this->select_entities('', 'entitytoclone', '', false, array($conf->entity), true, '', '', 'minwidth200imp');
  2192. $this->resprints.= '
  2193. <script type="text/javascript">
  2194. $(document).ready(function() {
  2195. $("#chartofaccounts").prop("disabled", true);
  2196. $("#change_chart").prop("disabled", true).val("'.$langs->trans("Duplicate").'");
  2197. $( "#searchFormList" ).on("change", "#entitytoclone", function() {
  2198. if ($(this).val() > 0) {
  2199. $.post( "'.dol_buildpath('/multicompany/core/ajax/functions.php',1).'", {
  2200. "action": "getChartOfAccountsOfEntity",
  2201. "id": $(this).val(),
  2202. "token": "'.currentToken().'"
  2203. },
  2204. function (result) {
  2205. if (result.status == "success") {
  2206. $("#chartofaccounts").val(result.chartofaccounts).change();
  2207. $("#change_chart").prop("disabled", false)
  2208. }
  2209. }
  2210. );
  2211. } else {
  2212. $("#chartofaccounts").val(-1).change();
  2213. $("#change_chart").prop("disabled", true);
  2214. }
  2215. });
  2216. $("#change_chart").on("click", function (e) {
  2217. console.log("chartofaccounts seleted = "+$("#chartofaccounts").val());
  2218. // reload page
  2219. window.location.href = "'.$_SERVER["PHP_SELF"].'?valid_change_chart=1&chartofaccounts="+$("#chartofaccounts").val()+"&entitytoclone="+$("#entitytoclone").val();
  2220. });
  2221. });
  2222. </script>
  2223. ';
  2224. }
  2225. }
  2226. }
  2227. $this->resprints.= '<br>';
  2228. $this->resprints.= '<!-- END multicompany formObjectOptions -->'."\n";
  2229. return 0;
  2230. }
  2231. /**
  2232. *
  2233. * @param boolean $parameters
  2234. * @param unknown $object
  2235. * @param string $action
  2236. * @return number
  2237. */
  2238. public function formCreateThirdpartyOptions($parameters=false, &$object, &$action='')
  2239. {
  2240. global $conf, $user, $langs;
  2241. global $form;
  2242. if (empty($conf->multicompany->enabled)) {
  2243. return 0;
  2244. }
  2245. $langs->load('multicompany@multicompany');
  2246. if (is_array($parameters) && !empty($parameters)) {
  2247. foreach($parameters as $key=>$value) {
  2248. $$key=$value;
  2249. }
  2250. }
  2251. //echo 'OK';
  2252. //$this->resprints = 'OK';
  2253. return 0;
  2254. }
  2255. /**
  2256. *
  2257. * @param boolean $parameters
  2258. * @param unknown $object
  2259. * @param string $action
  2260. * @return number
  2261. */
  2262. public function formAddUserToGroup($parameters=false, &$object, &$action='')
  2263. {
  2264. global $conf, $user, $langs;
  2265. global $form, $mc, $exclude;
  2266. if (empty($conf->multicompany->enabled)) {
  2267. return 0;
  2268. }
  2269. $langs->load('multicompany@multicompany');
  2270. if (is_array($parameters) && !empty($parameters)) {
  2271. foreach($parameters as $key=>$value) {
  2272. $$key=$value;
  2273. }
  2274. }
  2275. $currentcontext = explode(':', $parameters['context']);
  2276. $this->resprints = '';
  2277. if (is_array($currentcontext)) {
  2278. if (in_array('usercard', $currentcontext) && $object->element == 'user') {
  2279. if ($action != 'edit' && $action != 'presend' && !empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE)) {
  2280. $this->resprints = "\n".'<!-- BEGIN multicompany formAddUserToGroup -->'."\n";
  2281. if (!empty($groupslist)) {
  2282. $exclude=array();
  2283. }
  2284. if ($caneditgroup) {
  2285. $this->resprints.= '<form action="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'" method="POST">'."\n";
  2286. $this->resprints.= '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'" />';
  2287. $this->resprints.= '<input type="hidden" name="action" value="addgroup" />';
  2288. }
  2289. $this->resprints.= '<table class="noborder centpercent">'."\n";
  2290. $this->resprints.= '<tr class="liste_titre"><th class="liste_titre">'.$langs->trans("Groups").'</th>'."\n";
  2291. if (!empty($user->admin)) {
  2292. $this->resprints.= '<th class="liste_titre">'.$langs->trans("Entity").'</th>';
  2293. }
  2294. $this->resprints.= '<th class="liste_titre" align="right">';
  2295. if ($caneditgroup && empty($user->entity)) {
  2296. // Users/Groups management only in master entity if transverse mode
  2297. if ($conf->entity == 1) {
  2298. $this->resprints.= $form->select_dolgroups('', 'group', 1, $exclude, 0, '', '', $object->entity);
  2299. $this->resprints.= ' &nbsp; ';
  2300. if ($conf->entity == 1) {
  2301. $entities = $this->getEntitiesList();
  2302. $this->resprints.= $form->multiselectarray('entities', $entities, GETPOST('entities', 'array'), '', 0, '', 0, '20%');
  2303. } else {
  2304. $this->resprints.= '<input type="hidden" name="entity" value="'.$conf->entity.'" />';
  2305. }
  2306. $this->resprints.= '<input type="submit" class="button" value="'.$langs->trans("Add").'" />';
  2307. }
  2308. }
  2309. $this->resprints.= '</th></tr>'."\n";
  2310. /*
  2311. * Groups assigned to user
  2312. */
  2313. if (!empty($groupslist)) {
  2314. foreach($groupslist as $group) {
  2315. $this->resprints.= '<tr class="oddeven">';
  2316. $this->resprints.= '<td>';
  2317. if ($caneditgroup) {
  2318. $this->resprints.= '<a href="'.DOL_URL_ROOT.'/user/group/card.php?id='.$group->id.'">'.img_object($langs->trans("ShowGroup"),"group").' '.$group->name.'</a>';
  2319. } else {
  2320. $this->resprints.= img_object($langs->trans("ShowGroup"),"group").' '.$group->name;
  2321. }
  2322. $this->resprints.= '</td>';
  2323. if (!empty($user->admin)) {
  2324. $this->resprints.= '<td class="valeur">';
  2325. if (!empty($group->usergroup_entity)) {
  2326. sort($group->usergroup_entity, SORT_NUMERIC); // sort by entity id
  2327. foreach($group->usergroup_entity as $group_entity) {
  2328. $mc->getInfo($group_entity);
  2329. if (empty($conf->global->MULTICOMPANY_TEMPLATE_MANAGEMENT) && $mc->visible == 2) continue;
  2330. $this->resprints.= '<span class="multicompany-entity-container">';
  2331. if ($mc->visible == 2) {
  2332. $this->resprints.= '<span id="template_' . $mc->id . '" class="fas fa-clone multicompany-button-template" title="'.$langs->trans("TemplateOfEntity").'"></span>';
  2333. } else {
  2334. $this->resprints.= '<span id="template_' . $mc->id . '" class="fas fa-globe multicompany-button-template" title="'.$langs->trans("Entity").'"></span>';
  2335. }
  2336. $this->resprints.= $mc->label . (empty($mc->active) ? ' ('.$langs->trans('Disabled').')' : ($mc->visible == 2 ? ' ('.$langs->trans('Template').')' : (empty($mc->visible) ? ' ('.$langs->trans('Hidden').')' : '')) );
  2337. if ($conf->entity == 1 && empty($user->entity)) {
  2338. $this->resprints.= '<a href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&action=removegroup&group='.$group->id.'&entity='.$group_entity.'&token='.currentToken().'">';
  2339. $this->resprints.= img_picto($langs->trans("RemoveFromGroup"), 'unlink', 'class="multicompany-remove-from-link"');
  2340. $this->resprints.= '</a>';
  2341. }
  2342. $this->resprints.= '</span>';
  2343. }
  2344. }
  2345. }
  2346. $this->resprints.= '<td align="right">&nbsp;</td></tr>'."\n";
  2347. }
  2348. } else {
  2349. $this->resprints.= '<tr class="oddeven"><td colspan="3" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
  2350. }
  2351. $this->resprints.= "</table>";
  2352. if (!empty($caneditgroup)) {
  2353. $this->resprints.= '</form>';
  2354. }
  2355. $this->resprints.= '<!-- END multicompany formAddUserToGroup -->'."\n";
  2356. return 1;
  2357. }
  2358. } else if (in_array('groupcard', $currentcontext) && $object->element == 'usergroup') {
  2359. if ($action != 'edit' && !empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE)) {
  2360. $this->resprints = "\n".'<!-- BEGIN multicompany formAddUserToGroup -->'."\n";
  2361. if (!empty($object->members)) {
  2362. $exclude=array();
  2363. }
  2364. if ($caneditperms && empty($user->entity)) {
  2365. $this->resprints.= '<form action="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'" method="POST">'."\n";
  2366. $this->resprints.= '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  2367. $this->resprints.= '<input type="hidden" name="action" value="adduser">';
  2368. $this->resprints.= '<table class="noborder" width="100%">'."\n";
  2369. $this->resprints.= '<tr class="liste_titre"><td class="titlefield liste_titre">'.$langs->trans("NonAffectedUsers").'</td>'."\n";
  2370. $this->resprints.= '<th class="liste_titre">';
  2371. $this->resprints.= $form->select_dolusers('', 'user', 1, $exclude, 0, '', '', $object->entity);
  2372. $this->resprints.= ' &nbsp; ';
  2373. if ($conf->entity == 1) {
  2374. $entities = $this->getEntitiesList(false, false, false, (empty($conf->global->MULTICOMPANY_TEMPLATE_MANAGEMENT) ? false : true));
  2375. $this->resprints.= $form->multiselectarray('entities', $entities, GETPOST('entities', 'array'), '', 0, '', 0, '20%');
  2376. } else {
  2377. $this->resprints.= '<input type="hidden" name="entity" value="'.$conf->entity.'" />';
  2378. }
  2379. $this->resprints.= '<input type="submit" class="button" value="'.$langs->trans("Add").'">';
  2380. $this->resprints.= '</th></tr>'."\n";
  2381. $this->resprints.= '</table></form>'."\n";
  2382. $this->resprints.= '<br>';
  2383. }
  2384. /*
  2385. * Group members
  2386. */
  2387. $this->resprints.= '<table class="noborder" width="100%">';
  2388. $this->resprints.= '<tr class="liste_titre">';
  2389. $this->resprints.= '<td class="liste_titre">'.$langs->trans("Login").'</td>';
  2390. $this->resprints.= '<td class="liste_titre">'.$langs->trans("Lastname").'</td>';
  2391. $this->resprints.= '<td class="liste_titre">'.$langs->trans("Firstname").'</td>';
  2392. if ($conf->entity == 1) {
  2393. $this->resprints.= '<td class="liste_titre">'.$langs->trans("Entity").'</td>';
  2394. }
  2395. $this->resprints.= '<td class="liste_titre" width="5" align="center">'.$langs->trans("Status").'</td>';
  2396. $this->resprints.= '<td class="liste_titre" width="5" align="right">&nbsp;</td>';
  2397. $this->resprints.= "</tr>\n";
  2398. if (!empty($object->members)) {
  2399. foreach($object->members as $useringroup) {
  2400. $this->resprints.= '<tr class="oddeven">';
  2401. $this->resprints.= '<td>';
  2402. $this->resprints.= $useringroup->getNomUrl(-1, '', 0, 0, 24, 0, 'login');
  2403. if (!empty($useringroup->admin) && empty($useringroup->entity)) {
  2404. $this->resprints.= img_picto($langs->trans("SuperAdministrator"),'redstar');
  2405. } elseif (!empty($useringroup->admin)) {
  2406. $this->resprints.= img_picto($langs->trans("Administrator"),'star');
  2407. }
  2408. $this->resprints.= '</td>';
  2409. $this->resprints.= '<td>'.$useringroup->lastname.'</td>';
  2410. $this->resprints.= '<td>'.$useringroup->firstname.'</td>';
  2411. if ($conf->entity == 1 && !empty($user->admin)) {
  2412. $this->resprints.= '<td class="valeur">';
  2413. if (!empty($useringroup->usergroup_entity)) {
  2414. sort($useringroup->usergroup_entity, SORT_NUMERIC); // sort by entity id
  2415. foreach($useringroup->usergroup_entity as $group_entity) {
  2416. $mc->getInfo($group_entity);
  2417. if (empty($conf->global->MULTICOMPANY_TEMPLATE_MANAGEMENT) && $mc->visible == 2) {
  2418. continue;
  2419. }
  2420. $this->resprints.= '<span class="multicompany-entity-container">';
  2421. if ($mc->visible == 2) {
  2422. $this->resprints.= '<span id="template_' . $mc->id . '" class="fas fa-clone multicompany-button-template" title="'.$langs->trans("TemplateOfEntity").'"></span>';
  2423. } else {
  2424. $this->resprints.= '<span id="template_' . $mc->id . '" class="fas fa-globe multicompany-button-template" title="'.$langs->trans("Entity").'"></span>';
  2425. }
  2426. $this->resprints.= $mc->label . (empty($mc->active) ? ' ('.$langs->trans('Disabled').')' : ($mc->visible == 2 ? ' ('.$langs->trans('Template').')' : (empty($mc->visible) ? ' ('.$langs->trans('Hidden').')' : '')) );
  2427. if (empty($user->entity)) {
  2428. $this->resprints.= '<a href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&action=removeuser&user='.$useringroup->id.'&entity='.$group_entity.'&token='.currentToken().'">';
  2429. $this->resprints.= img_picto($langs->trans("RemoveFromGroup"), 'unlink', 'class="multicompany-remove-from-link"');
  2430. $this->resprints.= '</a>';
  2431. }
  2432. $this->resprints.= '</span>';
  2433. }
  2434. }
  2435. $this->resprints.= '</td>';
  2436. }
  2437. $this->resprints.= '<td align="center">'.$useringroup->getLibStatut(3).'</td>';
  2438. $this->resprints.= '<td align="right">';
  2439. $this->resprints.= "-";
  2440. $this->resprints.= "</td></tr>\n";
  2441. }
  2442. } else {
  2443. $this->resprints.= '<tr><td colspan="6" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
  2444. }
  2445. $this->resprints.= "</table>";
  2446. $this->resprints.= '<!-- END multicompany formAddUserToGroup -->'."\n";
  2447. return 1;
  2448. }
  2449. }
  2450. }
  2451. return 0;
  2452. }
  2453. /**
  2454. *
  2455. * @param boolean $parameters
  2456. * @param unknown $object
  2457. * @param string $action
  2458. * @return number
  2459. */
  2460. public function moreHtmlRef($parameters=false, &$object, &$action='')
  2461. {
  2462. global $conf, $user, $langs;
  2463. global $mc;
  2464. if (empty($conf->multicompany->enabled)) {
  2465. return 0;
  2466. }
  2467. if (is_array($parameters) && !empty($parameters)) {
  2468. foreach($parameters as $key=>$value) {
  2469. $$key=$value;
  2470. }
  2471. }
  2472. $currentcontext = explode(':', $parameters['context']);
  2473. $this->resprints = "\n".'<!-- BEGIN multicompany moreHtmlRef -->'."\n";
  2474. if ($object->element == 'user' || $object->element == 'usergroup') {
  2475. if (empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE) && $conf->entity == 1 && !empty($user->admin) && empty($user->entity)) {
  2476. $this->getInfo($object->entity);
  2477. $this->resprints.= '<br><div class="refidno multicompany-entity-card-container">';
  2478. $this->resprints.= '<span class="fa fa-globe"></span><span class="multiselect-selected-title-text">'.$this->label.'</span>';
  2479. $this->resprints.= '</div>';
  2480. }
  2481. }
  2482. // if global sharings is enabled
  2483. if (!empty($conf->global->MULTICOMPANY_SHARINGS_ENABLED)) {
  2484. $this->getInfo(!empty($object->entity) ? $object->entity : $conf->entity);
  2485. // if third party sharing is enabled (is mandatory for some sharings)
  2486. if (!empty($conf->societe->enabled) && !empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_ENABLED)) {
  2487. if ($object->element == 'societe') {
  2488. if (empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_BYELEMENT_ENABLED)) {
  2489. if (!empty($mc->sharings['thirdparty'])) {
  2490. if (in_array('thirdpartycard', $currentcontext)) {
  2491. if ($action != 'create' && $action != 'edit') {
  2492. if ($object->isObjectUsed($object->id) === 0
  2493. && ((!empty($user->admin) && empty($user->entity)) || !empty($user->rights->multicompany->thirdparty->write))) {
  2494. $selectEntities = $this->getModifyEntityDialog('thirdparty', 'modifyEntity', $object);
  2495. if (!empty($selectEntities)) {
  2496. $this->resprints.= '<div id="modify-entity-thirdparty" class="refidno modify-entity multicompany-entity-card-container" data-tooltip="'.$langs->trans('ThirdpartyModifyEntity').'" data-tooltip-position="bottom">';
  2497. $this->resprints.= '<span class="fa fa-globe"></span><span class="multiselect-selected-title-text">'.$this->label.'</span>';
  2498. $this->resprints.= '</div>';
  2499. $this->resprints.= $selectEntities;
  2500. } else {
  2501. $this->resprints.= '<div class="refidno modify-entity-disabled multicompany-entity-card-container" data-tooltip="'.$langs->trans("NoOtherEntityAvailable").'" data-tooltip-position="bottom">';
  2502. $this->resprints.= '<span class="fa fa-globe"></span><span class="multiselect-selected-title-text">'.$this->label.'</span>';
  2503. $this->resprints.= '</div>';
  2504. }
  2505. } else {
  2506. $this->resprints.= '<div class="refidno modify-entity-disabled multicompany-entity-card-container" data-tooltip="'.$langs->trans("ModifyEntityNotAllowed").'" data-tooltip-position="bottom">';
  2507. $this->resprints.= '<span class="fa fa-globe"></span><span class="multiselect-selected-title-text">'.$this->label.'</span>';
  2508. $this->resprints.= '</div>';
  2509. }
  2510. }
  2511. } else {
  2512. $this->resprints.= '<div class="refidno multicompany-entity-card-container">';
  2513. $this->resprints.= '<span class="fa fa-globe"></span><span class="multiselect-selected-title-text">'.$this->label.'</span>';
  2514. $this->resprints.= '</div>';
  2515. }
  2516. }
  2517. } else {
  2518. if ($this->id != $conf->entity && !empty($mc->sharings['thirdparty'])) {
  2519. $granularity = $this->dao->getListOfSharingsByElement('thirdparty', $object->id);
  2520. if (!empty($granularity) && in_array($conf->entity, $granularity[$object->id])) {
  2521. $this->resprints.= '<div class="refidno multicompany-entity-card-container" data-tooltip="'.$langs->trans("MulticompanyOriginatingEntity").'" data-tooltip-position="bottom">';
  2522. $this->resprints.= '<span class="fa fa-globe"></span><span class="multiselect-selected-title-text">'.$this->label;
  2523. $this->resprints.= '</span>';
  2524. $this->resprints.= '</div>';
  2525. }
  2526. } else {
  2527. if (1==2 && $this->checkIfElementIsSharedByEntity('thirdparty') === true) {
  2528. $this->getSharingsByElement('thirdparty', $object->id); // Populate $this->config['sharedwith']
  2529. if (!empty($this->config['sharedwith']['thirdparty'][$object->id])) {
  2530. foreach ($this->config['sharedwith']['thirdparty'][$object->id] as $sharedwithentity) {
  2531. $otherentity = new self($this->db);
  2532. $otherentity->getInfo($sharedwithentity);
  2533. $this->resprints.= '<div class="refidno multicompany-entity-card-container multicompany-margin-right-5" data-tooltip="'.$langs->trans("MulticompanySharedWithThisEntity").'" data-tooltip-position="bottom">';
  2534. $this->resprints.= '<span class="fa fa-globe"></span><span class="multiselect-selected-title-text">'.$otherentity->label.'</span>';
  2535. $this->resprints.= '</div>';
  2536. }
  2537. }
  2538. }
  2539. }
  2540. }
  2541. } elseif ($object->element == 'contact') {
  2542. if (empty($conf->global->MULTICOMPANY_CONTACT_SHARING_BYELEMENT_ENABLED)) {
  2543. if (empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_BYELEMENT_ENABLED)) {
  2544. if (!empty($mc->sharings['thirdparty'])) {
  2545. if (in_array('contactcard', $currentcontext)) {
  2546. if ($action != 'create' && $action != 'edit') {
  2547. if (empty($object->socid) && ((!empty($user->admin) && empty($user->entity)) || !empty($user->rights->multicompany->contact->write))) {
  2548. $selectEntities = $this->getModifyEntityDialog($object->element, 'modifyEntity', $object);
  2549. if (!empty($selectEntities)) {
  2550. $this->resprints.= '<div id="modify-entity-contact" class="refidno modify-entity multicompany-entity-card-container" data-tooltip="'.$langs->trans('ContactModifyEntity').'" data-tooltip-position="bottom">';
  2551. $this->resprints.= '<span class="fa fa-globe"></span><span class="multiselect-selected-title-text">'.$this->label.'</span>';
  2552. $this->resprints.= '</div>';
  2553. $this->resprints.= $selectEntities;
  2554. } else {
  2555. $this->resprints.= '<div class="refidno modify-entity-disabled multicompany-entity-card-container" data-tooltip="'.$langs->trans("NoOtherEntityAvailable").'" data-tooltip-position="bottom">';
  2556. $this->resprints.= '<span class="fa fa-globe"></span><span class="multiselect-selected-title-text">'.$this->label.'</span>';
  2557. $this->resprints.= '</div>';
  2558. }
  2559. } else {
  2560. $this->resprints.= '<div class="refidno modify-entity-disabled multicompany-entity-card-container" data-tooltip="'.$langs->trans("ModifyEntityNotAllowed").'" data-tooltip-position="bottom">';
  2561. $this->resprints.= '<span class="fa fa-globe"></span><span class="multiselect-selected-title-text">'.$this->label.'</span>';
  2562. $this->resprints.= '</div>';
  2563. }
  2564. }
  2565. } else {
  2566. $this->resprints.= '<div class="refidno multicompany-entity-card-container">';
  2567. $this->resprints.= '<span class="fa fa-globe"></span><span class="multiselect-selected-title-text">'.$this->label.'</span>';
  2568. $this->resprints.= '</div>';
  2569. }
  2570. }
  2571. } else {
  2572. if (1==2 && $this->checkIfElementIsSharedByEntity('thirdparty') === true) {
  2573. $this->getSharingsByElement($object->element, $object->id); // Populate $this->config['sharedwith']
  2574. if (!empty($this->config['sharedwith'][$object->element][$object->id])) {
  2575. foreach ($this->config['sharedwith'][$object->element][$object->id] as $sharedwithentity) {
  2576. $otherentity = new self($this->db);
  2577. $otherentity->getInfo($sharedwithentity);
  2578. $this->resprints.= '<div class="refidno multicompany-entity-card-container multicompany-margin-right-5" data-tooltip="'.$langs->trans("MulticompanySharedWithThisEntity").'" data-tooltip-position="bottom">';
  2579. $this->resprints.= '<span class="fa fa-globe"></span><span class="multiselect-selected-title-text">'.$otherentity->label.'</span>';
  2580. $this->resprints.= '</div>';
  2581. }
  2582. }
  2583. }
  2584. }
  2585. } else {
  2586. if ($this->id != $conf->entity && !empty($mc->sharings['thirdparty'])) {
  2587. $granularity = $this->dao->getListOfSharingsByElement($object->element, $object->id);
  2588. if (!empty($granularity) && in_array($conf->entity, $granularity[$object->id])) {
  2589. $this->resprints.= '<div class="refidno multicompany-entity-card-container" data-tooltip="'.$langs->trans("MulticompanyOriginatingEntity").'" data-tooltip-position="bottom">';
  2590. $this->resprints.= '<span class="fa fa-globe"></span><span class="multiselect-selected-title-text">'.$this->label;
  2591. $this->resprints.= '</span>';
  2592. $this->resprints.= '</div>';
  2593. }
  2594. } else {
  2595. if (1==2 && $this->checkIfElementIsSharedByEntity('thirdparty') === true) {
  2596. $this->getSharingsByElement($object->element, $object->id); // Populate $this->config['sharedwith']
  2597. if (!empty($this->config['sharedwith'][$object->element][$object->id])) {
  2598. foreach ($this->config['sharedwith'][$object->element][$object->id] as $sharedwithentity) {
  2599. $otherentity = new self($this->db);
  2600. $otherentity->getInfo($sharedwithentity);
  2601. $this->resprints.= '<div class="refidno multicompany-entity-card-container multicompany-margin-right-5" data-tooltip="'.$langs->trans("MulticompanySharedWithThisEntity").'" data-tooltip-position="bottom">';
  2602. $this->resprints.= '<span class="fa fa-globe"></span><span class="multiselect-selected-title-text">'.$otherentity->label.'</span>';
  2603. $this->resprints.= '</div>';
  2604. }
  2605. }
  2606. }
  2607. }
  2608. }
  2609. }
  2610. }
  2611. if ((!empty($conf->product->enabled) || !empty($conf->service->enabled)) && $object->element == 'product' && !empty($conf->global->MULTICOMPANY_PRODUCT_SHARING_ENABLED)) {
  2612. if ($action != 'create' && $action != 'edit') {
  2613. if (empty($conf->global->MULTICOMPANY_PRODUCT_SHARING_BYELEMENT_ENABLED)) {
  2614. if ($this->id != $conf->entity && !empty($mc->sharings[$object->element])) {
  2615. $this->resprints.= '<div class="refidno multicompany-entity-card-container" data-tooltip="'.$langs->trans("MulticompanyOriginatingEntity").'" data-tooltip-position="bottom">';
  2616. $this->resprints.= '<span class="fa fa-globe"></span><span class="multiselect-selected-title-text">'.$this->label;
  2617. $this->resprints.= '</span>';
  2618. $this->resprints.= '</div>';
  2619. }
  2620. } else {
  2621. if ($this->id != $conf->entity && !empty($mc->sharings[$object->element])) {
  2622. $granularity = $this->dao->getListOfSharingsByElement($object->element, $object->id);
  2623. if (!empty($granularity) && in_array($conf->entity, $granularity[$object->id])) {
  2624. $this->resprints.= '<div class="refidno multicompany-entity-card-container" data-tooltip="'.$langs->trans("MulticompanyOriginatingEntity").'" data-tooltip-position="bottom">';
  2625. $this->resprints.= '<span class="fa fa-globe"></span><span class="multiselect-selected-title-text">'.$this->label;
  2626. $this->resprints.= '</span>';
  2627. $this->resprints.= '</div>';
  2628. }
  2629. } else {
  2630. if (1==2 && $this->checkIfElementIsSharedByEntity($object->element) === true) {
  2631. $this->getSharingsByElement($object->element, $object->id); // Populate $this->config['sharedwith']
  2632. if (!empty($this->config['sharedwith'][$object->element][$object->id])) {
  2633. foreach ($this->config['sharedwith'][$object->element][$object->id] as $sharedwithentity) {
  2634. $otherentity = new self($this->db);
  2635. $otherentity->getInfo($sharedwithentity);
  2636. $this->resprints.= '<div class="refidno multicompany-entity-card-container multicompany-margin-right-5" data-tooltip="'.$langs->trans("MulticompanySharedWithThisEntity").'" data-tooltip-position="bottom">';
  2637. $this->resprints.= '<span class="fa fa-globe"></span><span class="multiselect-selected-title-text">'.$otherentity->label.'</span>';
  2638. $this->resprints.= '</div>';
  2639. }
  2640. }
  2641. }
  2642. }
  2643. }
  2644. }
  2645. } elseif (!empty($conf->projet->enabled) && $object->element == 'project' && !empty($conf->global->MULTICOMPANY_PROJECT_SHARING_ENABLED) && !empty($mc->sharings['project'])) {
  2646. if (in_array('projectcard', $currentcontext)) {
  2647. if ($action != 'create' && $action != 'edit') {
  2648. if (empty($object->socid) && ((!empty($user->admin) && empty($user->entity)) || !empty($user->rights->projet->creer))) {
  2649. $selectEntities = $this->getModifyEntityDialog('project', 'modifyEntity', $object);
  2650. if (!empty($selectEntities)) {
  2651. $this->resprints.= '<div id="modify-entity-project" class="refidno modify-entity multicompany-entity-card-container" data-tooltip="'.$langs->trans('ProjectModifyEntity').'" data-tooltip-position="bottom">';
  2652. $this->resprints.= '<span class="fa fa-globe"></span><span class="multiselect-selected-title-text">'.$this->label.'</span>';
  2653. $this->resprints.= '</div>';
  2654. $this->resprints.= $selectEntities;
  2655. } else {
  2656. $this->resprints.= '<div class="refidno modify-entity-disabled multicompany-entity-card-container" data-tooltip="'.$langs->trans("NoOtherEntityAvailable").'" data-tooltip-position="bottom">';
  2657. $this->resprints.= '<span class="fa fa-globe"></span><span class="multiselect-selected-title-text">'.$this->label.'</span>';
  2658. $this->resprints.= '</div>';
  2659. }
  2660. } else {
  2661. $this->resprints.= '<div class="refidno modify-entity-disabled multicompany-entity-card-container" data-tooltip="'.$langs->trans("ModifyEntityNotAllowed").'" data-tooltip-position="bottom">';
  2662. $this->resprints.= '<span class="fa fa-globe"></span><span class="multiselect-selected-title-text">'.$this->label.'</span>';
  2663. $this->resprints.= '</div>';
  2664. }
  2665. }
  2666. } else {
  2667. $this->resprints.= '<div class="refidno multicompany-entity-card-container">';
  2668. $this->resprints.= '<span class="fa fa-globe"></span><span class="multiselect-selected-title-text">'.$this->label.'</span>';
  2669. $this->resprints.= '</div>';
  2670. }
  2671. } elseif ((!empty($conf->propal->enabled) && $object->element == 'propal' && in_array('propalcard', $currentcontext) && !empty($conf->global->MULTICOMPANY_PROPOSAL_SHARING_ENABLED) && !empty($mc->sharings['proposal']))
  2672. || (!empty($conf->commande->enabled) && $object->element == 'commande' && in_array('ordercard', $currentcontext) && !empty($conf->global->MULTICOMPANY_ORDER_SHARING_ENABLED) && !empty($mc->sharings['order']))
  2673. || (!empty($conf->facture->enabled) && $object->element == 'facture' && in_array('invoicecard', $currentcontext) && !empty($conf->global->MULTICOMPANY_INVOICE_SHARING_ENABLED) && !empty($mc->sharings['invoice']))
  2674. || (!empty($conf->stock->enabled) && $object->element == 'stock' && in_array('warehousecard', $currentcontext) && !empty($conf->global->MULTICOMPANY_STOCK_SHARING_ENABLED) && !empty($mc->sharings['stock']))
  2675. || (!empty($conf->adherent->enabled) && $object->element == 'member' && in_array('membercard', $currentcontext) && !empty($conf->global->MULTICOMPANY_MEMBER_SHARING_ENABLED) && !empty($mc->sharings['member']))) {
  2676. $this->resprints.= '<div class="refidno multicompany-entity-card-container">';
  2677. $this->resprints.= '<span class="fa fa-globe"></span><span class="multiselect-selected-title-text">'.$this->label.'</span>';
  2678. $this->resprints.= '</div>';
  2679. }
  2680. }
  2681. $this->resprints.= '<!-- END multicompany moreHtmlRef -->'."\n";
  2682. return 0;
  2683. }
  2684. /**
  2685. *
  2686. * @param boolean $parameters
  2687. * @param unknown $object
  2688. * @param string $action
  2689. * @return number
  2690. */
  2691. public function moreHtmlStatus($parameters=false, &$object, &$action='')
  2692. {
  2693. global $conf;
  2694. /*
  2695. if (empty($conf->multicompany->enabled)) return 0;
  2696. if (is_array($parameters) && !empty($parameters)) {
  2697. foreach($parameters as $key=>$value) {
  2698. $$key=$value;
  2699. }
  2700. }
  2701. $this->resprints.= 'OK';
  2702. */
  2703. return 0;
  2704. }
  2705. /**
  2706. *
  2707. * @param boolean $parameters
  2708. * @return number
  2709. */
  2710. public function printUserListWhere($parameters=false)
  2711. {
  2712. global $conf, $user;
  2713. if (empty($conf->multicompany->enabled)) {
  2714. return 0;
  2715. }
  2716. /*if (is_array($parameters) && !empty($parameters)) {
  2717. foreach($parameters as $key=>$value) {
  2718. $$key=$value;
  2719. }
  2720. }*/
  2721. $this->resprints = '';
  2722. if (!empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE)) {
  2723. if (!empty($user->admin) && empty($user->entity) && $conf->entity == 1) {
  2724. $this->resprints.= " WHERE u.entity IS NOT NULL"; // Show all users
  2725. } else {
  2726. $this->resprints.= ",".MAIN_DB_PREFIX."usergroup_user as ug";
  2727. $this->resprints.= " WHERE ((ug.fk_user = u.rowid";
  2728. $this->resprints.= " AND ug.entity IN (".getEntity('usergroup')."))";
  2729. $this->resprints.= " OR u.entity = 0)"; // Show always superadmin
  2730. }
  2731. return 1;
  2732. }
  2733. return 0;
  2734. }
  2735. /**
  2736. *
  2737. * @param boolean $parameters
  2738. * @return number
  2739. */
  2740. public function addMoreMassActions($parameters=false)
  2741. {
  2742. global $conf, $langs;
  2743. global $mc;
  2744. if (empty($conf->multicompany->enabled) || empty($conf->global->MULTICOMPANY_SHARINGS_ENABLED)) {
  2745. return 0;
  2746. }
  2747. $langs->load('multicompany@multicompany');
  2748. if (is_array($parameters) && !empty($parameters)) {
  2749. foreach($parameters as $key=>$value) {
  2750. $$key=$value;
  2751. }
  2752. }
  2753. $currentcontext = explode(':', $parameters['context']);
  2754. if (empty($conf->global->MULTICOMPANY_SHARING_BYELEMENT_ENABLED)) {
  2755. if (!empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_ENABLED)
  2756. && (in_array('thirdpartylist', $currentcontext) || in_array('contactlist', $currentcontext))
  2757. && !empty($mc->sharings['thirdparty'])) {
  2758. $label = img_picto('', 'website', 'class="pictofixedwidth"').$langs->trans('ModifyEntity');
  2759. $this->resprints = '<option value="modify_entity" disabled="disabled" data-html="'.dol_escape_htmltag($label).'">'.$label.'</option>';
  2760. }
  2761. } else {
  2762. if (!empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_ENABLED)
  2763. && !empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_BYELEMENT_ENABLED)
  2764. && in_array('thirdpartylist', $currentcontext)
  2765. && $this->checkIfElementIsSharedByEntity('thirdparty') === true) {
  2766. $label = img_picto('', 'website', 'class="pictofixedwidth"').$langs->trans('ShareByElement');
  2767. $this->resprints = '<option value="sharebyelement" data-html="'.dol_escape_htmltag($label).'">'.$label.'</option>';
  2768. } elseif (!empty($conf->global->MULTICOMPANY_PRODUCT_SHARING_ENABLED)
  2769. && !empty($conf->global->MULTICOMPANY_PRODUCT_SHARING_BYELEMENT_ENABLED)
  2770. && in_array('productservicelist', $currentcontext)
  2771. && $this->checkIfElementIsSharedByEntity('product') === true) {
  2772. $label = img_picto('', 'website', 'class="pictofixedwidth"').$langs->trans('ShareByElement');
  2773. $this->resprints = '<option value="sharebyelement" data-html="'.dol_escape_htmltag($label).'">'.$label.'</option>';
  2774. }
  2775. }
  2776. return 0;
  2777. }
  2778. /**
  2779. *
  2780. * @param boolean $parameters
  2781. * @param unknown $object
  2782. * @param unknown $action
  2783. * @return number
  2784. */
  2785. public function doPreMassActions($parameters=false, &$object, &$action)
  2786. {
  2787. global $conf, $langs;
  2788. global $form, $massaction, $mc;
  2789. if (empty($conf->multicompany->enabled) || empty($conf->global->MULTICOMPANY_SHARINGS_ENABLED)) {
  2790. return 0;
  2791. }
  2792. $langs->load('multicompany@multicompany');
  2793. if (is_array($parameters) && !empty($parameters)) {
  2794. foreach($parameters as $key=>$value) {
  2795. $$key=$value;
  2796. }
  2797. }
  2798. $currentcontext = explode(':', $parameters['context']);
  2799. if (!empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_ENABLED)
  2800. && !empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_BYELEMENT_ENABLED)
  2801. && in_array('thirdpartylist', $currentcontext)
  2802. && $this->checkIfElementIsSharedByEntity('thirdparty') === true
  2803. && $massaction == 'sharebyelement') {
  2804. $formquestion[] = array('type' => 'other',
  2805. 'name' => 'sharebyelement',
  2806. 'value' => '<br>'.$this->formSelectSharingByElement($object, 'thirdparty').'<br>'
  2807. );
  2808. $this->resprints = $form->formconfirm($_SERVER["PHP_SELF"], $langs->trans("ConfirmMassThirdpartySharingByElement"), $langs->trans("ConfirmMassThirdpartySharingByElementQuestion", count($toselect)), "confirmsharebyelement", $formquestion, '', 0, 200, 500, 1);
  2809. } elseif (!empty($conf->global->MULTICOMPANY_PRODUCT_SHARING_ENABLED)
  2810. && !empty($conf->global->MULTICOMPANY_PRODUCT_SHARING_BYELEMENT_ENABLED)
  2811. && in_array('productservicelist', $currentcontext)
  2812. && $this->checkIfElementIsSharedByEntity('product') === true
  2813. && $massaction == 'sharebyelement') {
  2814. $formquestion[] = array('type' => 'other',
  2815. 'name' => 'sharebyelement',
  2816. 'value' => '<br>'.$this->formSelectSharingByElement($object, 'product').'<br>'
  2817. );
  2818. $this->resprints = $form->formconfirm($_SERVER["PHP_SELF"], $langs->trans("ConfirmMassProductSharingByElement"), $langs->trans("ConfirmMassProductSharingByElementQuestion", count($toselect)), "confirmsharebyelement", $formquestion, '', 0, 200, 500, 1);
  2819. }
  2820. }
  2821. /**
  2822. *
  2823. * @param boolean $parameters
  2824. * @return number
  2825. */
  2826. public function printFieldListSelect($parameters=false)
  2827. {
  2828. global $conf;
  2829. global $mc;
  2830. if (empty($conf->multicompany->enabled)) {
  2831. return 0;
  2832. }
  2833. if (is_array($parameters) && !empty($parameters)) {
  2834. foreach($parameters as $key=>$value) {
  2835. $$key=$value;
  2836. }
  2837. }
  2838. $currentcontext = explode(':', $parameters['context']);
  2839. if (!empty($conf->global->MULTICOMPANY_SHARINGS_ENABLED)) {
  2840. // Thirdparty sharing is mandatory to share document (propal, etc...)
  2841. if (!empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_ENABLED) && !empty($mc->sharings['thirdparty'])) {
  2842. if (in_array('thirdpartylist', $currentcontext)) {
  2843. //if (!empty($arrayfields['s.entity']['checked']))
  2844. {
  2845. $this->resprints = ", s.entity";
  2846. }
  2847. } elseif (in_array('contactlist', $currentcontext)) {
  2848. //if (!empty($arrayfields['p.entity']['checked']))
  2849. {
  2850. $this->resprints = ", p.entity";
  2851. }
  2852. } elseif (in_array('propallist', $currentcontext) && !empty($conf->global->MULTICOMPANY_PROPOSAL_SHARING_ENABLED) && !empty($mc->sharings['proposal'])) {
  2853. //if (!empty($arrayfields['p.entity']['checked']))
  2854. {
  2855. $this->resprints = ", p.entity";
  2856. }
  2857. } elseif (in_array('orderlist', $currentcontext) && !empty($conf->global->MULTICOMPANY_ORDER_SHARING_ENABLED) && !empty($mc->sharings['order'])) {
  2858. //if (!empty($arrayfields['p.entity']['checked']))
  2859. {
  2860. $this->resprints = ", c.entity";
  2861. }
  2862. } elseif (in_array('invoicelist', $currentcontext) && !empty($conf->global->MULTICOMPANY_INVOICE_SHARING_ENABLED) && !empty($mc->sharings['invoice'])) {
  2863. //if (!empty($arrayfields['p.entity']['checked']))
  2864. {
  2865. $this->resprints = ", f.entity";
  2866. }
  2867. }
  2868. }
  2869. if (in_array('memberlist', $currentcontext) && !empty($conf->global->MULTICOMPANY_MEMBER_SHARING_ENABLED) && !empty($mc->sharings['member'])) {
  2870. //if (!empty($arrayfields['p.entity']['checked']))
  2871. {
  2872. $this->resprints = ", d.entity";
  2873. }
  2874. } elseif (in_array('productservicelist', $currentcontext) && !empty($conf->global->MULTICOMPANY_PRODUCT_SHARING_ENABLED) && !empty($mc->sharings['product'])) {
  2875. //if (!empty($arrayfields['p.entity']['checked']))
  2876. {
  2877. $this->resprints = ", p.entity";
  2878. }
  2879. } elseif (in_array('projectlist', $currentcontext) && !empty($conf->global->MULTICOMPANY_PROJECT_SHARING_ENABLED) && !empty($mc->sharings['project'])) {
  2880. //if (!empty($arrayfields['p.entity']['checked']))
  2881. {
  2882. $this->resprints = " , p.entity";
  2883. }
  2884. }
  2885. }
  2886. return 0;
  2887. }
  2888. /**
  2889. *
  2890. * @param boolean $parameters
  2891. * @param unknown $object
  2892. * @return number
  2893. */
  2894. public function printFieldListFrom($parameters=false, &$object)
  2895. {
  2896. global $conf;
  2897. if (empty($conf->multicompany->enabled)) {
  2898. return 0;
  2899. }
  2900. if (is_array($parameters) && !empty($parameters)) {
  2901. foreach($parameters as $key=>$value) {
  2902. $$key=$value;
  2903. }
  2904. }
  2905. $currentcontext = explode(':', $parameters['context']);
  2906. if (!empty($conf->global->MULTICOMPANY_SHARINGS_ENABLED)) {
  2907. if (!empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_ENABLED)
  2908. && !empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_BYELEMENT_ENABLED)
  2909. && !empty($conf->global->MULTICOMPANY_CONTACT_SHARING_BYELEMENT_ENABLED)
  2910. && $object->element == 'societe' && !empty($contacttab)) {
  2911. $this->resprints.= ", ".MAIN_DB_PREFIX."societe as s";
  2912. }
  2913. }
  2914. }
  2915. /**
  2916. *
  2917. * @param boolean $parameters
  2918. * @param unknown $object
  2919. * @return number
  2920. */
  2921. public function printFieldListWhere($parameters=false, &$object)
  2922. {
  2923. global $conf;
  2924. global $mc;
  2925. if (empty($conf->multicompany->enabled)) {
  2926. return 0;
  2927. }
  2928. if (is_array($parameters) && !empty($parameters)) {
  2929. foreach($parameters as $key=>$value) {
  2930. $$key=$value;
  2931. }
  2932. }
  2933. $currentcontext = explode(':', $parameters['context']);
  2934. $this->resprints = '';
  2935. if (!empty($conf->global->MULTICOMPANY_SHARINGS_ENABLED)) {
  2936. $search_entity = GETPOST('search_entity','int');
  2937. // All tests are required to be compatible with all browsers
  2938. if (GETPOST('button_removefilter_x','alpha') || GETPOST('button_removefilter.x','alpha') || GETPOST('button_removefilter','alpha')) {
  2939. $search_entity = '';
  2940. }
  2941. if (!empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_ENABLED)) {
  2942. // for element next/previous function for thirdparty element
  2943. if (!empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_BYELEMENT_ENABLED)
  2944. && is_object($object) && $object->element == 'societe' && !empty($object->id)
  2945. //&& !in_array('commercialindex', $currentcontext)
  2946. //&& !in_array('thirdpartycontact', $currentcontext)
  2947. && !empty($showrefnav)) {
  2948. $this->getInstanceDao();
  2949. $granularity = $this->dao->getListOfSharingsByElement('thirdparty', null, $conf->entity);
  2950. if (is_array($granularity) && !empty($granularity)) {
  2951. $method = 'IN';
  2952. if (isSharingAllByDefault('thirdparty')) {
  2953. $method = 'NOT IN';
  2954. }
  2955. $this->resprints.= " AND (te.rowid ".$method." (" . implode(",", array_keys($granularity)) . ") OR te.entity = " . $conf->entity . ")";
  2956. }
  2957. // for element next/previous function for contact element
  2958. } elseif (!empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_BYELEMENT_ENABLED)
  2959. && is_object($object) && $object->element == 'contact' && !empty($object->id) && !empty($showrefnav)) {
  2960. $this->getInstanceDao();
  2961. $granularity = $this->dao->getListOfSharingsByElement($object->element, null, $conf->entity);
  2962. if (is_array($granularity) && !empty($granularity)) {
  2963. $method = 'IN';
  2964. if (isSharingAllByDefault($object->element)) {
  2965. $method = 'NOT IN';
  2966. }
  2967. $this->resprints.= " AND (te.rowid ".$method." (" . implode(",", array_keys($granularity)) . ") OR te.entity = " . $conf->entity . ")";
  2968. }
  2969. // for element list, load_state_board and boxes
  2970. } elseif (!empty($mc->sharings['thirdparty'])) {
  2971. if ((in_array('thirdpartylist', $currentcontext)
  2972. || in_array('thirdpartiesindex', $currentcontext)
  2973. || in_array('commercialindex', $currentcontext)
  2974. || in_array('index', $currentcontext)
  2975. || (!empty($boxcode) && ($boxcode == 'lastprospects' || $boxcode == 'lastcustomers' || $boxcode == 'lastsuppliers')))
  2976. && $object->element == 'societe') {
  2977. if (!empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_BYELEMENT_ENABLED)) {
  2978. $this->getInstanceDao();
  2979. $granularity = $this->dao->getListOfSharingsByElement('thirdparty', null, $conf->entity);
  2980. if (is_array($granularity) && !empty($granularity)) {
  2981. $method = 'IN';
  2982. if (isSharingAllByDefault('thirdparty')) {
  2983. $method = 'NOT IN';
  2984. }
  2985. $this->resprints.= " AND (s.rowid ".$method." (" . implode(",", array_keys($granularity)) . ") OR s.entity = " . $conf->entity . ")";
  2986. }
  2987. }
  2988. //if (!empty($arrayfields['s.entity']['checked']))
  2989. {
  2990. if ($search_entity > 0) {
  2991. $this->resprints.= " AND s.entity = " . $search_entity;
  2992. }
  2993. }
  2994. } elseif (in_array('contactlist', $currentcontext)) {
  2995. if (!empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_BYELEMENT_ENABLED)) {
  2996. $this->getInstanceDao();
  2997. $granularity = $this->dao->getListOfSharingsByElement('contact', null, $conf->entity);
  2998. if (is_array($granularity) && !empty($granularity)) {
  2999. $method = 'IN';
  3000. if (isSharingAllByDefault('contact')) {
  3001. $method = 'NOT IN';
  3002. }
  3003. $this->resprints.= " AND (p.rowid ".$method." (" . implode(",", array_keys($granularity)) . ") OR p.entity = " . $conf->entity . ")";
  3004. }
  3005. }
  3006. //if (!empty($arrayfields['p.entity']['checked']))
  3007. {
  3008. if ($search_entity > 0) {
  3009. $this->resprints = " AND p.entity = " . $search_entity;
  3010. }
  3011. }
  3012. } elseif ($object->element == 'societe' && !empty($contacttab)) {
  3013. if (!empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_BYELEMENT_ENABLED)) {
  3014. $this->getInstanceDao();
  3015. $granularity = $this->dao->getListOfSharingsByElement('contact', null, $conf->entity);
  3016. $this->resprints.= " AND p.fk_soc = s.rowid";
  3017. if (is_array($granularity) && !empty($granularity)) {
  3018. $method = 'IN';
  3019. if (isSharingAllByDefault('contact')) {
  3020. $method = 'NOT IN';
  3021. }
  3022. $this->resprints.= " AND (p.rowid ".$method." (" . implode(",", array_keys($granularity)) . ") OR s.entity = " . $conf->entity . ")";
  3023. }
  3024. }
  3025. // For contact load_state_board and boxes
  3026. } elseif ((in_array('index', $currentcontext) || (!empty($boxcode) && $boxcode == 'lastcontacts'))
  3027. && $object->element == 'contact'
  3028. && !empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_BYELEMENT_ENABLED)) {
  3029. $this->getInstanceDao();
  3030. $granularity = $this->dao->getListOfSharingsByElement($object->element, null, $conf->entity);
  3031. if (is_array($granularity) && !empty($granularity)) {
  3032. $method = 'IN';
  3033. if (isSharingAllByDefault($object->element)) {
  3034. $method = 'NOT IN';
  3035. }
  3036. $this->resprints.= " AND (sp.rowid ".$method." (" . implode(",", array_keys($granularity)) . ") OR sp.entity = " . $conf->entity . ")";
  3037. }
  3038. // for thirdparty contact list tab
  3039. } elseif (in_array('thirdpartycontact', $currentcontext) && $object->element == 'societe') {
  3040. if (!empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_BYELEMENT_ENABLED)) {
  3041. $this->getInstanceDao();
  3042. $granularity = $this->dao->getListOfSharingsByElement('contact', null, $conf->entity);
  3043. if (is_array($granularity) && !empty($granularity)) {
  3044. $method = 'IN';
  3045. if (isSharingAllByDefault('contact')) {
  3046. $method = 'NOT IN';
  3047. }
  3048. $this->resprints.= " AND (t.rowid ".$method." (" . implode(",", array_keys($granularity)) . ") OR t.entity = " . $conf->entity . ")";
  3049. }
  3050. }
  3051. } elseif (in_array('propallist', $currentcontext) && !empty($conf->global->MULTICOMPANY_PROPOSAL_SHARING_ENABLED) && !empty($mc->sharings['proposal'])) {
  3052. //if (!empty($arrayfields['p.entity']['checked']))
  3053. {
  3054. if ($search_entity > 0) {
  3055. $this->resprints = " AND p.entity = " . $search_entity;
  3056. }
  3057. }
  3058. } elseif (in_array('orderlist', $currentcontext) && !empty($conf->global->MULTICOMPANY_ORDER_SHARING_ENABLED) && !empty($mc->sharings['order'])) {
  3059. //if (!empty($arrayfields['p.entity']['checked']))
  3060. {
  3061. if ($search_entity > 0) {
  3062. $this->resprints = " AND c.entity = " . $search_entity;
  3063. }
  3064. }
  3065. } elseif (in_array('invoicelist', $currentcontext) && !empty($conf->global->MULTICOMPANY_INVOICE_SHARING_ENABLED) && !empty($mc->sharings['invoice'])) {
  3066. //if (!empty($arrayfields['f.entity']['checked']))
  3067. {
  3068. if ($search_entity > 0) {
  3069. $this->resprints = " AND f.entity = " . $search_entity;
  3070. }
  3071. }
  3072. }
  3073. }
  3074. }
  3075. // for product/service next/previous function
  3076. if (!empty($conf->global->MULTICOMPANY_PRODUCT_SHARING_ENABLED)
  3077. && !empty($conf->global->MULTICOMPANY_PRODUCT_SHARING_BYELEMENT_ENABLED)
  3078. && is_object($object) && $object->element == 'product' && !empty($object->id)) {
  3079. $this->getInstanceDao();
  3080. $granularity = $this->dao->getListOfSharingsByElement($object->element, null, $conf->entity);
  3081. if (is_array($granularity) && !empty($granularity)) {
  3082. $method = 'IN';
  3083. if (isSharingAllByDefault($object->element)) {
  3084. $method = 'NOT IN';
  3085. }
  3086. $this->resprints.= " AND (te.rowid ".$method." (" . implode(",", array_keys($granularity)) . ") OR te.entity = " . $conf->entity . ")";
  3087. }
  3088. // for product/service list, load_state_board and boxes
  3089. } elseif ((in_array('productservicelist', $currentcontext)
  3090. || in_array('productreassortlist', $currentcontext)
  3091. || in_array('productindex', $currentcontext)
  3092. || (in_array('index', $currentcontext) && $object->element == 'product')
  3093. ||(!empty($boxcode) && ($boxcode == 'lastproducts' || $boxcode == 'productsalertstock')))
  3094. && !empty($conf->global->MULTICOMPANY_PRODUCT_SHARING_ENABLED) && !empty($mc->sharings['product'])) {
  3095. if (!empty($conf->global->MULTICOMPANY_PRODUCT_SHARING_BYELEMENT_ENABLED)) {
  3096. $this->getInstanceDao();
  3097. $granularity = $this->dao->getListOfSharingsByElement('product', null, $conf->entity);
  3098. if (is_array($granularity) && !empty($granularity)) {
  3099. $method = 'IN';
  3100. if (isSharingAllByDefault('product')) {
  3101. $method = 'NOT IN';
  3102. }
  3103. $this->resprints.= " AND (p.rowid ".$method." (" . implode(",", array_keys($granularity)) . ") OR p.entity = " . $conf->entity . ")";
  3104. }
  3105. }
  3106. //if (!empty($arrayfields['p.entity']['checked']))
  3107. {
  3108. if ($search_entity > 0) {
  3109. $this->resprints = " AND p.entity = " . $search_entity;
  3110. }
  3111. }
  3112. } elseif (in_array('memberlist', $currentcontext) && !empty($conf->global->MULTICOMPANY_MEMBER_SHARING_ENABLED) && !empty($mc->sharings['member'])) {
  3113. //if (!empty($arrayfields['p.entity']['checked']))
  3114. {
  3115. if ($search_entity > 0) {
  3116. $this->resprints = " AND d.entity = " . $search_entity;
  3117. }
  3118. }
  3119. } elseif (in_array('stocklist', $currentcontext) && !empty($conf->global->MULTICOMPANY_STOCK_SHARING_ENABLED) && !empty($mc->sharings['stock'])) {
  3120. //if (!empty($arrayfields['p.entity']['checked']))
  3121. {
  3122. if ($search_entity > 0) {
  3123. $this->resprints = " AND t.entity = " . $search_entity;
  3124. }
  3125. }
  3126. } elseif (in_array('projectlist', $currentcontext) && !empty($conf->global->MULTICOMPANY_PROJECT_SHARING_ENABLED) && !empty($mc->sharings['project'])) {
  3127. //if (!empty($arrayfields['p.entity']['checked']))
  3128. {
  3129. if ($search_entity > 0) {
  3130. $this->resprints = " AND p.entity = " . $search_entity;
  3131. }
  3132. }
  3133. }
  3134. }
  3135. return 0;
  3136. }
  3137. /**
  3138. *
  3139. * @param boolean $parameters
  3140. * @return number
  3141. */
  3142. public function printFieldListOption($parameters=false)
  3143. {
  3144. global $conf;
  3145. global $mc;
  3146. if (empty($conf->multicompany->enabled)) {
  3147. return 0;
  3148. }
  3149. if (is_array($parameters) && !empty($parameters)) {
  3150. foreach($parameters as $key=>$value) {
  3151. $$key=$value;
  3152. }
  3153. }
  3154. $currentcontext = explode(':', $parameters['context']);
  3155. if (!empty($conf->global->MULTICOMPANY_SHARINGS_ENABLED)) {
  3156. $search_entity = GETPOST('search_entity','int');
  3157. // All tests are required to be compatible with all browsers
  3158. if (GETPOST('button_removefilter_x','alpha') || GETPOST('button_removefilter.x','alpha') || GETPOST('button_removefilter','alpha')) {
  3159. $search_entity = '';
  3160. }
  3161. if (!empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_ENABLED) && !empty($mc->sharings['thirdparty'])) {
  3162. if (in_array('thirdpartylist', $currentcontext) || in_array('contactlist', $currentcontext)) {
  3163. //if (!empty($arrayfields['s.entity']['checked']) || !empty($arrayfields['p.entity']['checked']))
  3164. {
  3165. // Entity
  3166. $this->resprints = '<td class="liste_titre maxwidthonsmartphone" align="center">';
  3167. $this->resprints.= $this->select_entities($search_entity,'search_entity','',false,false,true,explode(",", $mc->entities['thirdparty']),'','minwidth100imp');
  3168. $this->resprints.= '</td>';
  3169. }
  3170. } elseif (in_array('propallist', $currentcontext) && !empty($conf->global->MULTICOMPANY_PROPOSAL_SHARING_ENABLED) && !empty($mc->sharings['proposal'])) {
  3171. //if (!empty($arrayfields['p.entity']['checked']))
  3172. {
  3173. // Entity
  3174. $this->resprints = '<td class="liste_titre maxwidthonsmartphone" align="center">';
  3175. $this->resprints.= $this->select_entities($search_entity,'search_entity','',false,false,true,explode(",", $mc->entities['proposal']),'','minwidth100imp');
  3176. $this->resprints.= '</td>';
  3177. }
  3178. } elseif (in_array('orderlist', $currentcontext) && !empty($conf->global->MULTICOMPANY_ORDER_SHARING_ENABLED) && !empty($mc->sharings['order'])) {
  3179. //if (!empty($arrayfields['p.entity']['checked']))
  3180. {
  3181. // Entity
  3182. $this->resprints = '<td class="liste_titre maxwidthonsmartphone" align="center">';
  3183. $this->resprints.= $this->select_entities($search_entity,'search_entity','',false,false,true,explode(",", $mc->entities['order']),'','minwidth100imp');
  3184. $this->resprints.= '</td>';
  3185. }
  3186. } elseif (in_array('invoicelist', $currentcontext) && !empty($conf->global->MULTICOMPANY_INVOICE_SHARING_ENABLED) && !empty($mc->sharings['invoice'])) {
  3187. //if (!empty($arrayfields['f.entity']['checked']))
  3188. {
  3189. // Entity
  3190. $this->resprints = '<td class="liste_titre maxwidthonsmartphone" align="center">';
  3191. $this->resprints.= $this->select_entities($search_entity,'search_entity','',false,false,true,explode(",", $mc->entities['invoice']),'','minwidth100imp');
  3192. $this->resprints.= '</td>';
  3193. }
  3194. }
  3195. }
  3196. if (in_array('memberlist', $currentcontext) && !empty($conf->global->MULTICOMPANY_MEMBER_SHARING_ENABLED) && !empty($mc->sharings['member'])) {
  3197. //if (!empty($arrayfields['p.entity']['checked']))
  3198. {
  3199. // Entity
  3200. $this->resprints = '<td class="liste_titre maxwidthonsmartphone" align="center">';
  3201. $this->resprints.= $this->select_entities($search_entity,'search_entity','',false,false,true,explode(",", $mc->entities['member']),'','minwidth100imp');
  3202. $this->resprints.= '</td>';
  3203. }
  3204. } elseif (in_array('productservicelist', $currentcontext) && !empty($conf->global->MULTICOMPANY_PRODUCT_SHARING_ENABLED) && !empty($mc->sharings['product'])){
  3205. //if (!empty($arrayfields['p.entity']['checked']))
  3206. {
  3207. // Entity
  3208. $this->resprints = '<td class="liste_titre maxwidthonsmartphone" align="center">';
  3209. $this->resprints.= $this->select_entities($search_entity,'search_entity','',false,false,true,explode(",", $mc->entities['product']),'','minwidth100imp');
  3210. $this->resprints.= '</td>';
  3211. }
  3212. } elseif (in_array('stocklist', $currentcontext) && !empty($conf->global->MULTICOMPANY_STOCK_SHARING_ENABLED) && !empty($mc->sharings['stock'])) {
  3213. //if (!empty($arrayfields['p.entity']['checked']))
  3214. {
  3215. // Entity
  3216. $this->resprints = '<td class="liste_titre maxwidthonsmartphone" align="center">';
  3217. $this->resprints.= $this->select_entities($search_entity,'search_entity','',false,false,true,explode(",", $mc->entities['stock']),'','minwidth100imp');
  3218. $this->resprints.= '</td>';
  3219. }
  3220. } elseif (in_array('projectlist', $currentcontext) && !empty($conf->global->MULTICOMPANY_PROJECT_SHARING_ENABLED) && !empty($mc->sharings['project'])) {
  3221. //if (!empty($arrayfields['p.entity']['checked']))
  3222. {
  3223. // Entity
  3224. $this->resprints = '<td class="liste_titre maxwidthonsmartphone" align="center">';
  3225. $this->resprints.= $this->select_entities($search_entity,'search_entity','',false,false,true,explode(",", $mc->entities['project']),'','minwidth100imp');
  3226. $this->resprints.= '</td>';
  3227. }
  3228. }
  3229. }
  3230. return 0;
  3231. }
  3232. /**
  3233. *
  3234. * @param boolean $parameters
  3235. * @return number
  3236. */
  3237. public function printFieldListTitle($parameters=false)
  3238. {
  3239. global $conf;
  3240. global $mc;
  3241. if (empty($conf->multicompany->enabled)) {
  3242. return 0;
  3243. }
  3244. if (is_array($parameters) && !empty($parameters)) {
  3245. foreach($parameters as $key=>$value) {
  3246. $$key=$value;
  3247. }
  3248. }
  3249. $currentcontext = explode(':', $parameters['context']);
  3250. if (!empty($conf->global->MULTICOMPANY_SHARINGS_ENABLED)) {
  3251. if (!empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_ENABLED) && !empty($mc->sharings['thirdparty'])) {
  3252. if (in_array('thirdpartylist', $currentcontext)) {
  3253. //if (!empty($arrayfields['s.entity']['checked']))
  3254. {
  3255. $this->resprints = getTitleFieldOfList('Entity',0,$_SERVER["PHP_SELF"],"s.entity","",$param,'align="center"',$sortfield,$sortorder);
  3256. }
  3257. } elseif (in_array('contactlist', $currentcontext)) {
  3258. //if (!empty($arrayfields['p.entity']['checked']))
  3259. {
  3260. $this->resprints = getTitleFieldOfList('Entity',0,$_SERVER["PHP_SELF"],"p.entity","",$param,'align="center"',$sortfield,$sortorder);
  3261. }
  3262. } elseif (in_array('propallist', $currentcontext) && !empty($conf->global->MULTICOMPANY_PROPOSAL_SHARING_ENABLED) && !empty($mc->sharings['proposal'])) {
  3263. //if (!empty($arrayfields['p.entity']['checked']))
  3264. {
  3265. $this->resprints = getTitleFieldOfList('Entity',0,$_SERVER["PHP_SELF"],"p.entity","",$param,'align="center"',$sortfield,$sortorder);
  3266. }
  3267. } elseif (in_array('orderlist', $currentcontext) && !empty($conf->global->MULTICOMPANY_ORDER_SHARING_ENABLED) && !empty($mc->sharings['order'])) {
  3268. //if (!empty($arrayfields['p.entity']['checked']))
  3269. {
  3270. $this->resprints = getTitleFieldOfList('Entity',0,$_SERVER["PHP_SELF"],"c.entity","",$param,'align="center"',$sortfield,$sortorder);
  3271. }
  3272. } elseif (in_array('invoicelist', $currentcontext) && !empty($conf->global->MULTICOMPANY_INVOICE_SHARING_ENABLED) && !empty($mc->sharings['invoice'])) {
  3273. //if (!empty($arrayfields['f.entity']['checked']))
  3274. {
  3275. $this->resprints = getTitleFieldOfList('Entity',0,$_SERVER["PHP_SELF"],"f.entity","",$param,'align="center"',$sortfield,$sortorder);
  3276. }
  3277. }
  3278. }
  3279. if (in_array('memberlist', $currentcontext) && !empty($conf->global->MULTICOMPANY_MEMBER_SHARING_ENABLED) && !empty($mc->sharings['member'])) {
  3280. //if (!empty($arrayfields['p.entity']['checked']))
  3281. {
  3282. $this->resprints = getTitleFieldOfList('Entity',0,$_SERVER["PHP_SELF"],"p.entity","",$param,'align="center"',$sortfield,$sortorder);
  3283. }
  3284. } elseif (in_array('productservicelist', $currentcontext) && !empty($conf->global->MULTICOMPANY_PRODUCT_SHARING_ENABLED) && !empty($mc->sharings['product'])) {
  3285. //if (!empty($arrayfields['p.entity']['checked']))
  3286. {
  3287. $this->resprints = getTitleFieldOfList('Entity',0,$_SERVER["PHP_SELF"],"p.entity","",$param,'align="center"',$sortfield,$sortorder);
  3288. }
  3289. } elseif (in_array('stocklist', $currentcontext) && !empty($conf->global->MULTICOMPANY_STOCK_SHARING_ENABLED) && !empty($mc->sharings['stock'])) {
  3290. //if (!empty($arrayfields['p.entity']['checked']))
  3291. {
  3292. $this->resprints = getTitleFieldOfList('Entity',0,$_SERVER["PHP_SELF"],"t.entity","",$param,'align="center"',$sortfield,$sortorder);
  3293. }
  3294. } elseif (in_array('projectlist', $currentcontext) && !empty($conf->global->MULTICOMPANY_PROJECT_SHARING_ENABLED) && !empty($mc->sharings['project'])) {
  3295. //if (!empty($arrayfields['p.entity']['checked']))
  3296. {
  3297. $this->resprints = getTitleFieldOfList('Entity',0,$_SERVER["PHP_SELF"],"p.entity","",$param,'align="center"',$sortfield,$sortorder);
  3298. }
  3299. }
  3300. }
  3301. return 0;
  3302. }
  3303. /**
  3304. *
  3305. * @param boolean $parameters
  3306. * @return number
  3307. */
  3308. public function printFieldListValue($parameters=false)
  3309. {
  3310. global $conf;
  3311. global $totalarray;
  3312. global $mc;
  3313. if (empty($conf->multicompany->enabled)) {
  3314. return 0;
  3315. }
  3316. if (is_array($parameters) && !empty($parameters)) {
  3317. foreach($parameters as $key=>$value) {
  3318. $$key=$value;
  3319. }
  3320. }
  3321. $currentcontext = explode(':', $parameters['context']);
  3322. $this->resprints = '';
  3323. if (!empty($conf->global->MULTICOMPANY_SHARINGS_ENABLED)) {
  3324. if (!empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_ENABLED) && !empty($mc->sharings['thirdparty'])) {
  3325. if (in_array('thirdpartylist', $currentcontext) || in_array('contactlist', $currentcontext)) {
  3326. $this->getInfo($obj->entity);
  3327. //if (!empty($arrayfields['s.entity']['checked']) || !empty($arrayfields['p.entity']['checked']))
  3328. {
  3329. $this->resprints = '<td align="center">'."\n";
  3330. $this->resprints.= '<div class="refidno multicompany-entity-card-container">';
  3331. $this->resprints.= '<span class="fa fa-globe"></span><span class="multiselect-selected-title-text">'.$this->label.'</span>';
  3332. $this->resprints.= "</div></td>\n";
  3333. }
  3334. } elseif (in_array('propallist', $currentcontext) && !empty($conf->global->MULTICOMPANY_PROPOSAL_SHARING_ENABLED) && !empty($mc->sharings['proposal'])) {
  3335. $this->getInfo($obj->entity);
  3336. //if (!empty($arrayfields['p.entity']['checked']))
  3337. {
  3338. $this->resprints = '<td align="center">'."\n";
  3339. $this->resprints.= '<div class="refidno multicompany-entity-card-container">';
  3340. $this->resprints.= '<span class="fa fa-globe"></span><span class="multiselect-selected-title-text">'.$this->label.'</span>';
  3341. $this->resprints.= "</div></td>\n";
  3342. if (empty($i)) {
  3343. $totalarray['nbfield']++;
  3344. }
  3345. }
  3346. } elseif (in_array('orderlist', $currentcontext) && !empty($conf->global->MULTICOMPANY_ORDER_SHARING_ENABLED) && !empty($mc->sharings['order'])) {
  3347. $this->getInfo($obj->entity);
  3348. //if (!empty($arrayfields['p.entity']['checked']))
  3349. {
  3350. $this->resprints = '<td align="center">'."\n";
  3351. $this->resprints.= '<div class="refidno multicompany-entity-card-container">';
  3352. $this->resprints.= '<span class="fa fa-globe"></span><span class="multiselect-selected-title-text">'.$this->label.'</span>';
  3353. $this->resprints.= "</div></td>\n";
  3354. if (empty($i)) {
  3355. $totalarray['nbfield']++;
  3356. }
  3357. }
  3358. } elseif (in_array('invoicelist', $currentcontext) && !empty($conf->global->MULTICOMPANY_INVOICE_SHARING_ENABLED) && !empty($mc->sharings['invoice'])) {
  3359. $this->getInfo($obj->entity);
  3360. //if (!empty($arrayfields['f.entity']['checked']))
  3361. {
  3362. $this->resprints = '<td align="center">'."\n";
  3363. $this->resprints.= '<div class="refidno multicompany-entity-card-container">';
  3364. $this->resprints.= '<span class="fa fa-globe"></span><span class="multiselect-selected-title-text">'.$this->label.'</span>';
  3365. $this->resprints.= "</div></td>\n";
  3366. if (empty($i)) {
  3367. $totalarray['nbfield']++;
  3368. }
  3369. }
  3370. }
  3371. }
  3372. if (in_array('productservicelist', $currentcontext) && !empty($conf->global->MULTICOMPANY_PRODUCT_SHARING_ENABLED) && !empty($mc->sharings['product'])) {
  3373. $this->getInfo($obj->entity);
  3374. //if (!empty($arrayfields['p.entity']['checked']))
  3375. {
  3376. $this->resprints = '<td align="center">';
  3377. $this->resprints.= '<div class="refidno multicompany-entity-card-container">';
  3378. $this->resprints.= '<span class="fa fa-globe"></span><span class="multiselect-selected-title-text">'.$this->label.'</span>';
  3379. $this->resprints.= '</div></td>';
  3380. }
  3381. } elseif (in_array('stocklist', $currentcontext) && !empty($conf->global->MULTICOMPANY_STOCK_SHARING_ENABLED) && !empty($mc->sharings['stock'])) {
  3382. $this->getInfo($obj->entity);
  3383. //if (!empty($arrayfields['p.entity']['checked']))
  3384. {
  3385. $this->resprints = '<td align="center">';
  3386. $this->resprints.= '<div class="refidno multicompany-entity-card-container">';
  3387. $this->resprints.= '<span class="fa fa-globe"></span><span class="multiselect-selected-title-text">'.$this->label.'</span>';
  3388. $this->resprints.= '</div></td>';
  3389. }
  3390. } elseif (in_array('memberlist', $currentcontext) && !empty($conf->global->MULTICOMPANY_MEMBER_SHARING_ENABLED) && !empty($mc->sharings['member'])) {
  3391. $this->getInfo($obj->entity);
  3392. //if (!empty($arrayfields['p.entity']['checked']))
  3393. {
  3394. $this->resprints = '<td align="center">';
  3395. $this->resprints.= '<div class="refidno multicompany-entity-card-container">';
  3396. $this->resprints.= '<span class="fa fa-globe"></span><span class="multiselect-selected-title-text">'.$this->label.'</span>';
  3397. $this->resprints.= '</div></td>';
  3398. }
  3399. } elseif (in_array('projectlist', $currentcontext) && !empty($conf->global->MULTICOMPANY_PROJECT_SHARING_ENABLED) && !empty($mc->sharings['project'])) {
  3400. $this->getInfo($obj->entity);
  3401. //if (!empty($arrayfields['p.entity']['checked']))
  3402. {
  3403. $this->resprints = '<td align="center">';
  3404. $this->resprints.= '<div class="refidno multicompany-entity-card-container">';
  3405. $this->resprints.= '<span class="fa fa-globe"></span><span class="multiselect-selected-title-text">'.$this->label.'</span>';
  3406. $this->resprints.= '</div></td>';
  3407. }
  3408. }
  3409. }
  3410. return 0;
  3411. }
  3412. /**
  3413. *
  3414. * @param boolean $parameters
  3415. * @return number
  3416. */
  3417. public function selectThirdpartyListWhere($parameters=false)
  3418. {
  3419. global $conf;
  3420. if (empty($conf->multicompany->enabled) && !empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_ENABLED)) {
  3421. return 0;
  3422. }
  3423. if (is_array($parameters) && !empty($parameters)) {
  3424. foreach($parameters as $key=>$value) {
  3425. $$key=$value;
  3426. }
  3427. }
  3428. $currentcontext = explode(':', $parameters['context']);
  3429. $this->resprints = '';
  3430. if (!empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_BYELEMENT_ENABLED)) {
  3431. $this->getInstanceDao();
  3432. $granularity = $this->dao->getListOfSharingsByElement('thirdparty', null, $conf->entity);
  3433. if (is_array($granularity) && !empty($granularity)) {
  3434. $method = 'IN';
  3435. if (isSharingAllByDefault('thirdparty')) {
  3436. $method = 'NOT IN';
  3437. }
  3438. $this->resprints.= " AND (s.rowid ".$method." (" . implode(",", array_keys($granularity)) . ") OR s.entity = " . $conf->entity . ")";
  3439. }
  3440. }
  3441. return 0;
  3442. }
  3443. /**
  3444. *
  3445. * @param boolean $parameters
  3446. * @return number
  3447. */
  3448. public function selectContactListWhere($parameters=false)
  3449. {
  3450. global $conf;
  3451. if (empty($conf->multicompany->enabled) && !empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_ENABLED)) {
  3452. return 0;
  3453. }
  3454. if (is_array($parameters) && !empty($parameters)) {
  3455. foreach($parameters as $key=>$value) {
  3456. $$key=$value;
  3457. }
  3458. }
  3459. $currentcontext = explode(':', $parameters['context']);
  3460. $this->resprints = '';
  3461. if (!empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_BYELEMENT_ENABLED)) {
  3462. $this->getInstanceDao();
  3463. $granularity = $this->dao->getListOfSharingsByElement('contact', null, $conf->entity);
  3464. if (is_array($granularity) && !empty($granularity)) {
  3465. $method = 'IN';
  3466. if (isSharingAllByDefault('contact')) {
  3467. $method = 'NOT IN';
  3468. }
  3469. $this->resprints.= " AND (sp.rowid ".$method." (" . implode(",", array_keys($granularity)) . ") OR sp.entity = " . $conf->entity . ")";
  3470. }
  3471. }
  3472. return 0;
  3473. }
  3474. /**
  3475. *
  3476. * @param boolean $parameters
  3477. * @return number
  3478. */
  3479. public function selectCompaniesForNewContactListWhere($parameters=false)
  3480. {
  3481. global $conf;
  3482. if (empty($conf->multicompany->enabled) && !empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_ENABLED)) {
  3483. return 0;
  3484. }
  3485. if (is_array($parameters) && !empty($parameters)) {
  3486. foreach($parameters as $key=>$value) {
  3487. $$key=$value;
  3488. }
  3489. }
  3490. $currentcontext = explode(':', $parameters['context']);
  3491. $this->resprints = '';
  3492. if (!empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_BYELEMENT_ENABLED)) {
  3493. $this->getInstanceDao();
  3494. $granularity = $this->dao->getListOfSharingsByElement('thirdparty', null, $conf->entity);
  3495. if (is_array($granularity) && !empty($granularity)) {
  3496. $method = 'IN';
  3497. if (isSharingAllByDefault('thirdparty')) {
  3498. $method = 'NOT IN';
  3499. }
  3500. $this->resprints.= " AND (s.rowid ".$method." (" . implode(",", array_keys($granularity)) . ") OR s.entity = " . $conf->entity . ")";
  3501. }
  3502. }
  3503. return 0;
  3504. }
  3505. /**
  3506. *
  3507. * @param boolean $parameters
  3508. * @return number
  3509. */
  3510. public function selectProductsListWhere($parameters=false)
  3511. {
  3512. global $conf;
  3513. if (empty($conf->multicompany->enabled) && !empty($conf->global->MULTICOMPANY_PRODUCT_SHARING_ENABLED)) {
  3514. return 0;
  3515. }
  3516. if (is_array($parameters) && !empty($parameters)) {
  3517. foreach($parameters as $key=>$value) {
  3518. $$key=$value;
  3519. }
  3520. }
  3521. $currentcontext = explode(':', $parameters['context']);
  3522. $this->resprints = '';
  3523. if (!empty($conf->global->MULTICOMPANY_PRODUCT_SHARING_BYELEMENT_ENABLED)) {
  3524. $this->getInstanceDao();
  3525. $granularity = $this->dao->getListOfSharingsByElement('product', null, $conf->entity);
  3526. if (is_array($granularity) && !empty($granularity)) {
  3527. $method = 'IN';
  3528. if (isSharingAllByDefault('product')) {
  3529. $method = 'NOT IN';
  3530. }
  3531. $this->resprints.= " AND (p.rowid ".$method." (" . implode(",", array_keys($granularity)) . ") OR p.entity = " . $conf->entity . ")";
  3532. }
  3533. }
  3534. return 0;
  3535. }
  3536. /**
  3537. *
  3538. * @param boolean $parameters
  3539. * @param unknown $object
  3540. * @param string $action
  3541. * @return number
  3542. */
  3543. public function insertExtraHeader($parameters=false, &$object, &$action='')
  3544. {
  3545. global $conf, $user, $langs;
  3546. if (empty($conf->multicompany->enabled)) {
  3547. return 0;
  3548. }
  3549. if (is_array($parameters) && !empty($parameters)) {
  3550. foreach($parameters as $key=>$value) {
  3551. $$key=$value;
  3552. }
  3553. }
  3554. $currentcontext = explode(':', $parameters['context']);
  3555. if (!empty($user->admin) && empty($user->entity) && $conf->entity == 1 && !empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE)) {
  3556. if (in_array('userperms', $currentcontext) || in_array('groupperms', $currentcontext)) {
  3557. $this->getInstanceDao();
  3558. if ($object->element == 'user') {
  3559. $aEntities=array_keys($permsgroupbyentity);
  3560. // Check usergroup if user not in master entity
  3561. if (empty($aEntities) || !array_key_exists(1, $permsgroupbyentity)) {
  3562. require_once DOL_DOCUMENT_ROOT.'/user/class/usergroup.class.php';
  3563. $aEntities=array();
  3564. $group = new UserGroup($this->db);
  3565. $ret = $group->listGroupsForUser($object->id, false);
  3566. if (!empty($ret)) {
  3567. foreach($ret as $groupid => $data) {
  3568. $aEntities = array_merge($aEntities, $data->usergroup_entity);
  3569. }
  3570. sort($aEntities);
  3571. }
  3572. }
  3573. if (!empty($aEntities)) {
  3574. $entity = (GETPOST('entity', 'int')?GETPOST('entity', 'int'):$aEntities[0]);
  3575. $head = entity_prepare_head($object, $aEntities);
  3576. $title = $langs->trans("Entities");
  3577. dol_fiche_head($head, $entity, $title, 1, 'multicompany@multicompany');
  3578. } else {
  3579. print get_htmloutput_mesg(img_warning('default') . ' ' . $langs->trans("ErrorLinkUserGroupEntity"), '', 'mc-upgrade-alert', 1);
  3580. }
  3581. } else if ($object->element == 'usergroup') {
  3582. $this->dao->getEntities();
  3583. $aEntities=array();
  3584. foreach ($this->dao->entities as $objEntity) {
  3585. $aEntities[] = $objEntity->id;
  3586. }
  3587. $entity = (GETPOST('entity', 'int')?GETPOST('entity', 'int'):$aEntities[0]);
  3588. $head = entity_prepare_head($object, $aEntities);
  3589. $title = $langs->trans("Entities");
  3590. dol_fiche_head($head, $entity, $title, 1, 'multicompany@multicompany');
  3591. if (!empty($conf->global->MULTICOMPANY_TEMPLATE_MANAGEMENT)) {
  3592. print '
  3593. <div id="dialog-duplicate" title="'.$langs->trans('DuplicateRights').'" class="hideobject">
  3594. <p>'.img_info().' '.$langs->transnoentities('DuplicateRightsInfo').'</p>
  3595. <p>'.$langs->transnoentities('SelectRightsOfEntityToDuplicate').' '.$this->select_entities('', 'template', '', false, false, false, false, '', 'minwidth200imp', true, true).'</p>
  3596. <p>'.$langs->transnoentities('SelectEntitiesToOverride').'</p>
  3597. <p><div class="bootstrap-iso">
  3598. <div class="row">
  3599. <div class="col-sm-5">
  3600. <div class="multiselect-selected-title"><span class="fa fa-globe"></span><span class="multiselect-selected-title-text">'.$langs->transnoentities("EntitiesSelected").'</span></div>
  3601. '.$this->multiselectEntitiesToOverride('overrideentities', true).'
  3602. </div>
  3603. <div class="col-xs-2 multiselect-menu">
  3604. <!-- <button type="button" id="multiselect_overrideentities_undo" class="btn btn-primary btn-block">'.$langs->trans("Undo").'</button> -->
  3605. <button type="button" id="multiselect_overrideentities_leftAll" class="btn btn-block multiselect-menu-btn-color"><i class="glyphicon glyphicon-backward"></i></button>
  3606. <button type="button" id="multiselect_overrideentities_leftSelected" class="btn btn-block multiselect-menu-btn-color"><i class="glyphicon glyphicon-chevron-left"></i></button>
  3607. <button type="button" id="multiselect_overrideentities_rightSelected" class="btn btn-block multiselect-menu-btn-color"><i class="glyphicon glyphicon-chevron-right"></i></button>
  3608. <button type="button" id="multiselect_overrideentities_rightAll" class="btn btn-block multiselect-menu-btn-color"><i class="glyphicon glyphicon-forward"></i></button>
  3609. <!-- <button type="button" id="multiselect_overrideentities_redo" class="btn btn-warning btn-block">'.$langs->trans("Redo").'</button> -->
  3610. </div>
  3611. <div class="col-xs-5">
  3612. <div class="multiselect-available-title"><span class="fa fa-globe"></span><span class="multiselect-available-title-text">'.$langs->transnoentities("EntitiesAvailable").'</span></div>
  3613. '.$this->multiselectEntitiesToOverride('overrideentities', false).'
  3614. </div>
  3615. </div>
  3616. </div></p>
  3617. </div>
  3618. <script type="text/javascript">
  3619. (function(){
  3620. var newlink = document.createElement("link");
  3621. newlink.type = "text/css";
  3622. newlink.rel = "stylesheet";
  3623. newlink.href = "'.dol_buildpath("/multicompany/inc/multiselect/css/bootstrap-iso.min.css", 1).'";
  3624. (document.getElementsByTagName("head")[0]||document.getElementsByTagName("body")[0]).appendChild(newlink);
  3625. var newscript = document.createElement("script");
  3626. newscript.type = "text/javascript";
  3627. newscript.async = true;
  3628. newscript.src = "'.dol_buildpath("/multicompany/inc/multiselect/js/multiselect.min.js", 1).'";
  3629. (document.getElementsByTagName("head")[0]||document.getElementsByTagName("body")[0]).appendChild(newscript);
  3630. })();
  3631. $(document).ready(function() {
  3632. $( "span.multicompany-button-clonerights" ).parent().css( "background", "none" ).css( "border", "none" );
  3633. $( "span.multicompany-button-clonerights" ).parent().parent().css( "float", "right" ).css( "padding-top", "15px" );
  3634. $(".fichecenter").on("click", "#clonerights", function() {
  3635. $("#dialog-duplicate").dialog({
  3636. resizable: false,
  3637. height: 450,
  3638. width: 700,
  3639. modal: true,
  3640. open: function() {
  3641. $(".ui-dialog-buttonset > button:first").button("disable");
  3642. $(".ui-dialog-buttonset > button:last").focus();
  3643. },
  3644. buttons: {
  3645. "'.$langs->transnoentities('Validate').'": function() {
  3646. $(this).dialog("close");
  3647. var selections = [];
  3648. $("#multiselect_overrideentities_to option").each(function() {
  3649. selections.push( $(this).val() );
  3650. });
  3651. //console.log(selections);
  3652. $.post( "'.dol_buildpath('/multicompany/core/ajax/functions.php',1).'", {
  3653. "action": "duplicateUserGroupRights",
  3654. "id": "'.$object->id.'",
  3655. "template": $("#template").val(),
  3656. "entities": JSON.stringify(selections),
  3657. "token": "'.currentToken().'"
  3658. },
  3659. function (result) {
  3660. if (result.status == "success") {
  3661. $.jnotify("'.$langs->transnoentities("ConfirmedDuplicateRights").'", "ok");
  3662. } else {
  3663. $.jnotify("'.$langs->transnoentities("ErrorDuplicateRights").'", "error", true);
  3664. }
  3665. }
  3666. );
  3667. },
  3668. "'.$langs->transnoentities('Cancel').'": function() {
  3669. $(this).dialog("close");
  3670. }
  3671. }
  3672. });
  3673. });
  3674. $("#multiselect_overrideentities").multiselect({
  3675. keepRenderingSort: true,
  3676. right: "#multiselect_to_overrideentities",
  3677. rightAll: "#multiselect_overrideentities_leftAll",
  3678. rightSelected: "#multiselect_overrideentities_leftSelected",
  3679. leftSelected: "#multiselect_overrideentities_rightSelected",
  3680. leftAll: "#multiselect_overrideentities_rightAll",
  3681. search: {
  3682. left: \'<input type="text" name="q" class="form-control" placeholder="'.$langs->transnoentities("Search")."...".'" />\',
  3683. right: \'<input type="text" name="q" class="form-control" placeholder="'.$langs->transnoentities("Search")."...".'" />\',
  3684. },
  3685. fireSearch: function(value) {
  3686. return value.length > 2;
  3687. },
  3688. afterMoveToRight: function($left, $right, $options) {
  3689. $(".ui-dialog-buttonset > button:first").button("enable");
  3690. $("#multiselect_overrideentities").html( $("#multiselect_overrideentities option").sort(function(x, y) {
  3691. return $(x).val() < $(y).val() ? -1 : 1;
  3692. }));
  3693. },
  3694. afterMoveToLeft: function($left, $right, $options) {
  3695. if ($("#multiselect_overrideentities_to option").length == 0) {
  3696. $(".ui-dialog-buttonset > button:first").button("disable");
  3697. }
  3698. $("#multiselect_overrideentities").html( $("#multiselect_overrideentities option").sort(function(x, y) {
  3699. return $(x).val() < $(y).val() ? -1 : 1;
  3700. }));
  3701. }
  3702. });
  3703. });
  3704. </script>
  3705. ';
  3706. }
  3707. }
  3708. // Check if advanced perms is enabled for current object entity
  3709. $res = $this->dao->getEntityConfig($entity, 'MAIN_USE_ADVANCED_PERMS');
  3710. if (empty($res['MAIN_USE_ADVANCED_PERMS'])) {
  3711. unset($conf->global->MAIN_USE_ADVANCED_PERMS);
  3712. }
  3713. }
  3714. }
  3715. return 0;
  3716. }
  3717. /**
  3718. *
  3719. * @param boolean $parameters
  3720. * @param unknown $object
  3721. * @param string $action
  3722. * @return number
  3723. */
  3724. public function insertExtraFooter($parameters=false, &$object, &$action='')
  3725. {
  3726. global $conf, $user;
  3727. if (empty($conf->multicompany->enabled)) {
  3728. return 0;
  3729. }
  3730. if (is_array($parameters) && !empty($parameters)) {
  3731. foreach($parameters as $key=>$value) {
  3732. $$key=$value;
  3733. }
  3734. }
  3735. $currentcontext = explode(':', $parameters['context']);
  3736. if (!empty($user->admin) && empty($user->entity) && $conf->entity == 1 && !empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE)) {
  3737. if (in_array('userperms', $currentcontext) || in_array('groupperms', $currentcontext)) {
  3738. // Restore advanced perms if enabled for current entity
  3739. $this->getInstanceDao();
  3740. $res = $this->dao->getEntityConfig($conf->entity, 'MAIN_USE_ADVANCED_PERMS');
  3741. if (!empty($res['MAIN_USE_ADVANCED_PERMS'])) {
  3742. $conf->global->MAIN_USE_ADVANCED_PERMS = $res['MAIN_USE_ADVANCED_PERMS'];
  3743. }
  3744. }
  3745. }
  3746. return 0;
  3747. }
  3748. /**
  3749. * Return combo list of entities.
  3750. *
  3751. * @param int $selected Preselected entity
  3752. * @param int $htmlname Name
  3753. * @param string $option Option
  3754. * @param boolean $login If use in login page or not
  3755. * @param array $exclude Exclude
  3756. * @param boolean $emptyvalue Emptyvalue
  3757. * @param boolean $only Only
  3758. * @param string $all Add 'All entities' value in combo list
  3759. * @param string $cssclass specific css class. eg 'minwidth150imp mycssclass'
  3760. * @param bool $ajax Enable ajax combobox
  3761. * @param bool $template Show template of entities
  3762. * @return string
  3763. */
  3764. public function select_entities($selected = '', $htmlname = 'entity', $option = '', $login = false, $exclude = false, $emptyvalue = false, $only = false, $all = '', $cssclass = 'minwidth150imp', $ajax = true, $template = false)
  3765. {
  3766. global $conf, $user, $langs;
  3767. $this->getInstanceDao();
  3768. $this->dao->getEntities($login, $exclude);
  3769. $out='';
  3770. $entities=array();
  3771. foreach ($this->dao->entities as $entity) {
  3772. if (empty($conf->global->MULTICOMPANY_TEMPLATE_MANAGEMENT) && $entity->visible == 2) {
  3773. continue;
  3774. }
  3775. if ($template === 'only' && $entity->visible != 2) {
  3776. continue;
  3777. }
  3778. if (empty($template) && $entity->visible == 2) {
  3779. continue;
  3780. }
  3781. if (empty($entity->active)) {
  3782. continue;
  3783. }
  3784. $entities[] = $entity;
  3785. }
  3786. if (!empty($entities)) {
  3787. $out.= '<select class="flat maxwidth200 multicompany_select '.$cssclass.'" id="'.$htmlname.'" name="'.$htmlname.'"'.$option.'>';
  3788. if ($emptyvalue) {
  3789. $out.= '<option value="-1">&nbsp;</option>';
  3790. }
  3791. if ($all) {
  3792. $out.= '<option value="0">'.$langs->trans("AllEntities").'</option>';
  3793. }
  3794. foreach ($entities as $entity) {
  3795. if ($entity->active == 1 && ($entity->visible == 1 || (!empty($user->admin) && empty($user->entity)))) {
  3796. if (is_array($only) && !empty($only) && !in_array($entity->id, $only)) {
  3797. continue;
  3798. }
  3799. if (!empty($user->login) && !empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE)
  3800. && !empty($user->entity) && $this->checkRight($user->id, $entity->id) < 0) {
  3801. continue;
  3802. }
  3803. $out.= '<option value="'.$entity->id.'"';
  3804. if ($selected == $entity->id) {
  3805. $out.= ' selected="selected"';
  3806. }
  3807. $out.= '>';
  3808. $out.= $entity->label;
  3809. if (empty($entity->visible)) {
  3810. $out.= ' ('.$langs->trans('Hidden').')';
  3811. } elseif ($entity->visible == 2) {
  3812. $out.= ' ('.$langs->trans('Template').')';
  3813. }
  3814. $out.= '</option>';
  3815. }
  3816. }
  3817. $out.= '</select>';
  3818. // Make select dynamic
  3819. if ($ajax) {
  3820. include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php';
  3821. $out.= ajax_combobox($htmlname);
  3822. }
  3823. }
  3824. return $out;
  3825. }
  3826. /**
  3827. * Return multiselect list of entities.
  3828. *
  3829. * @param string $htmlname Name of select
  3830. * @param DaoMulticompany $current Current entity to manage
  3831. * @param bool $onlyselected true: show only selected, false: hide only selected
  3832. * @return string
  3833. */
  3834. public function multiselect_entities($htmlname, $current, $onlyselected=false)
  3835. {
  3836. global $langs;
  3837. $this->getInstanceDao();
  3838. $this->dao->getEntities();
  3839. $selectname = ($onlyselected ? $htmlname.'_to[]' : 'from[]');
  3840. $selectid = ($onlyselected ? 'multiselect_shared_'.$htmlname.'_to' : 'multiselect_shared_'.$htmlname);
  3841. $return = '<select name="'.$selectname.'" id="'.$selectid.'" class="form-control multiselect-select multiselect-min-height" size="6" multiple="multiple">';
  3842. if (is_array($this->dao->entities)) {
  3843. foreach ($this->dao->entities as $entity) {
  3844. if ($entity->visible == 2) continue;
  3845. if (is_object($current) && $current->id != $entity->id && $entity->active == 1) {
  3846. $sharings = ((!empty($current->options['sharings'][$htmlname]) && is_array($current->options['sharings'][$htmlname])) ? $current->options['sharings'][$htmlname] : null);
  3847. if ((empty($onlyselected) && (empty($sharings) || ! in_array($entity->id, $sharings))) // All unselected
  3848. || ($onlyselected && !empty($sharings) && in_array($entity->id, $sharings))) { // All selected
  3849. $return.= '<option class="oddeven multiselect-option" value="'.$entity->id.'">';
  3850. $return.= $entity->label;
  3851. if (empty($entity->visible)) {
  3852. $return.= ' ('.$langs->trans('Hidden').')';
  3853. }
  3854. $return.= '</option>';
  3855. }
  3856. }
  3857. }
  3858. }
  3859. $return.= '</select>';
  3860. return $return;
  3861. }
  3862. /**
  3863. * Return form for ganularity of sharings by element.
  3864. *
  3865. * @param object $object Object of element
  3866. * @param string $element To force element name for compatibility
  3867. * @param string $parentelement To force element name to check sharings of parents element (eg thirdparty for contact element)
  3868. * @param string $parentelementid Id of parents element to preload shared values
  3869. * @return string
  3870. */
  3871. public function formSelectSharingByElement($object, $element = null, $parentelement = null, $parentelementid = null)
  3872. {
  3873. global $langs;
  3874. global $mc;
  3875. $selected = null;
  3876. $element = (empty($element) ? $object->element : $element);
  3877. $elementid = (!empty($object->id) ? $object->id : null);
  3878. $action = GETPOST('action', 'alpha');
  3879. // To avoid loose preselected values during create and edit
  3880. if (($action == 'add' || $action == 'update' || $action == 'create')) {
  3881. $preselected = array();
  3882. if ($action == 'create' && isSharingAllByDefault($element) && !empty($mc->sharings[$element])) {
  3883. $preselected = $mc->sharings[$element];
  3884. }
  3885. $selected = (GETPOSTISSET($parentelement.'_to') ? GETPOST($parentelement.'_to', 'array', 2) : (GETPOSTISSET($element.'_to') ? GETPOST($element.'_to', 'array', 2) : $preselected));
  3886. }
  3887. $out = '<div class="bootstrap-iso"><div class="row">'."\n";
  3888. $out.= '<div class="col-sm-5">'."\n";
  3889. $out.= '<div class="multiselect-selected-title"><span class="fa fa-globe"></span>'."\n";
  3890. $out.= '<span class="multiselect-selected-title-text">'.$langs->trans("EntitiesSelected").'</span></div>'."\n";
  3891. $out.= $this->multiselectEntitiesForGranularity($element, $elementid, true, $parentelement, $parentelementid, $selected);
  3892. $out.= '</div>'."\n";
  3893. $out.= '<div class="col-xs-2 multiselect-menu">'."\n";
  3894. $out.= '<button type="button" id="multiselect_shared_'.$element.'_undo" class="btn btn-primary btn-block">'.$langs->trans("Undo").'</button>'."\n";
  3895. $out.= '<button type="button" id="multiselect_shared_'.$element.'_leftAll" class="btn btn-block multiselect-menu-btn-color"><i class="glyphicon glyphicon-backward"></i></button>'."\n";
  3896. $out.= '<button type="button" id="multiselect_shared_'.$element.'_leftSelected" class="btn btn-block multiselect-menu-btn-color"><i class="glyphicon glyphicon-chevron-left"></i></button>'."\n";
  3897. $out.= '<button type="button" id="multiselect_shared_'.$element.'_rightSelected" class="btn btn-block multiselect-menu-btn-color"><i class="glyphicon glyphicon-chevron-right"></i></button>'."\n";
  3898. $out.= '<button type="button" id="multiselect_shared_'.$element.'_rightAll" class="btn btn-block multiselect-menu-btn-color"><i class="glyphicon glyphicon-forward"></i></button>'."\n";
  3899. $out.= '<button type="button" id="multiselect_shared_'.$element.'_redo" class="btn btn-warning btn-block">'.$langs->trans("Redo").'</button>'."\n";
  3900. $out.= '</div>'."\n";
  3901. $out.= '<div class="col-xs-5">'."\n";
  3902. $out.= '<div class="multiselect-available-title"><span class="fa fa-globe"></span>'."\n";
  3903. $out.= '<span class="multiselect-available-title-text">'.$langs->trans("EntitiesAvailable").'</span></div>'."\n";
  3904. $out.= $this->multiselectEntitiesForGranularity($element, $elementid, false, $parentelement, $parentelementid, $selected);
  3905. $out.= '</div></div></div>'."\n";
  3906. if (!empty($elementid)) {
  3907. $this->getSharingsByElement($element, $elementid); // Populate $this->config['sharedwith']
  3908. if (!empty($this->config['sharedwith'][$element][$elementid])) {
  3909. $out.= '<input type="hidden" name="oldsharedwith" value="' . base64_encode(json_encode($this->config['sharedwith'][$element][$elementid])) . '">';
  3910. }
  3911. }
  3912. $out.='
  3913. <script type="text/javascript">
  3914. (function(){
  3915. var newlink = document.createElement("link");
  3916. newlink.type = "text/css";
  3917. newlink.rel = "stylesheet";
  3918. newlink.href = "'.dol_buildpath("/multicompany/inc/multiselect/css/bootstrap-iso.min.css", 1).'";
  3919. //console.log(newlink);
  3920. (document.getElementsByTagName("head")[0]||document.getElementsByTagName("body")[0]).appendChild(newlink);
  3921. // var newscript = document.createElement("script");
  3922. // newscript.type = "text/javascript";
  3923. // newscript.async = true;
  3924. // newscript.src = "'.dol_buildpath("/multicompany/inc/multiselect/js/multiselect.min.js", 1).'";
  3925. //console.log(newscript);
  3926. //(document.getElementsByTagName("head")[0]||document.getElementsByTagName("body")[0]).appendChild(newscript);
  3927. // document.getElementsByTagName("head")[0].appendChild(newscript);
  3928. })();
  3929. $(document).ready(function () {
  3930. $("#multiselect_shared_'.$element.'").multiselect({
  3931. keepRenderingSort: true,
  3932. right: "#multiselect_to_'.$element.'",
  3933. rightAll: "#multiselect_shared_'.$element.'_leftAll",
  3934. rightSelected: "#multiselect_shared_'.$element.'_leftSelected",
  3935. leftSelected: "#multiselect_shared_'.$element.'_rightSelected",
  3936. leftAll: "#multiselect_shared_'.$element.'_rightAll",
  3937. search: {
  3938. left: \'<input type="text" name="q" class="form-control" placeholder="'.$langs->trans("Search").'..." />\',
  3939. right: \'<input type="text" name="q" class="form-control" placeholder="'.$langs->trans("Search").'..." />\',
  3940. },
  3941. fireSearch: function(value) {
  3942. return value.length > 2;
  3943. },
  3944. ';
  3945. // ask if update sharing of contacts
  3946. if (!empty($elementid) && $element == 'thirdparty' && $action == 'update') { // ask if update sharing of contacts
  3947. $out.='
  3948. beforeMoveToRight: function($left, $right, $options) {
  3949. return true;
  3950. },
  3951. ';
  3952. }
  3953. $out.='
  3954. afterMoveToRight: function($left, $right, $options) {
  3955. $("#multiselect_shared_'.$element.'_to").html($("#multiselect_shared_'.$element.'_to option").sort(function(x, y) {
  3956. return $(x).val() < $(y).val() ? -1 : 1;
  3957. }));
  3958. },
  3959. ';
  3960. // "contact" element have no child tables
  3961. if (!empty($elementid) && $element != 'contact') {
  3962. $out.='
  3963. beforeMoveToLeft: function($left, $right, $options) {
  3964. var entities = [];
  3965. var bool = false;
  3966. $.each($options, function( key, entity ) {
  3967. entities.push(entity.value);
  3968. });
  3969. //console.log(entities);
  3970. $.ajax({
  3971. type: "POST",
  3972. async: false,
  3973. url: "'.dol_buildpath('/multicompany/core/ajax/functions.php', 1).'",
  3974. data: {
  3975. "action": "checkIfElementIsUsed",
  3976. "id": "'.$elementid.'",
  3977. "element": "'.$element.'",
  3978. "entities": JSON.stringify(entities),
  3979. "token": "'.currentToken().'"
  3980. },
  3981. success: function(result) {
  3982. if (result.status == "success") {
  3983. bool = true;
  3984. if (result.element == "thirdparty") {
  3985. $.jnotify("'.$langs->trans("InfoRemoveThisShareFromContacts").'", "ok");
  3986. }
  3987. } else {
  3988. $.jnotify(result.error, "error", true);
  3989. }
  3990. }
  3991. });
  3992. return bool;
  3993. },
  3994. ';
  3995. }
  3996. $out.='
  3997. afterMoveToLeft: function($left, $right, $options) {
  3998. $("#multiselect_shared_'.$element.'").html($("#multiselect_shared_'.$element.' option").sort(function(x, y) {
  3999. return $(x).val() < $(y).val() ? -1 : 1;
  4000. }));
  4001. }
  4002. });
  4003. });
  4004. </script>
  4005. '."\n";
  4006. return $out;
  4007. }
  4008. /**
  4009. * Return multiselect list of entities for ganularity of sharings by element.
  4010. *
  4011. * @param string $element Name of element (thirdparty, product, ...)
  4012. * @param int $elementid Id of current element for sharing granularity
  4013. * @param bool $onlyselected true: show only selected, false: hide only selected
  4014. * @param string $parentelement To force element name to check sharings of parents element (eg thirdparty for contact element)
  4015. * @param string $parentelementid Id of parents element to preload shared values
  4016. * @param array $selected For avoid loose selected values during create/update
  4017. * @return string
  4018. */
  4019. public function multiselectEntitiesForGranularity($element, $elementid = null, $onlyselected = false, $parentelement = null, $parentelementid = null, $selected = null)
  4020. {
  4021. global $conf, $langs;
  4022. $parentelement = (empty($parentelement) ? $element : $parentelement);
  4023. $currententity = new self($this->db);
  4024. $currententity->getInfo($conf->entity);
  4025. $this->getInstanceDao();
  4026. $this->dao->getEntities(false, array($currententity->id), true);
  4027. if (!empty($parentelementid) && $parentelement == 'thirdparty') {
  4028. $parentgranularity = $this->dao->getListOfSharingsByElement('thirdparty', $parentelementid);
  4029. }
  4030. $granularity = array();
  4031. if (is_null($selected)) {
  4032. if (!empty($elementid)) {
  4033. $granularity = $this->dao->getListOfSharingsByElement($element, $elementid);
  4034. } elseif (!empty($parentelementid) && $parentelement == 'thirdparty') {
  4035. $elementid = $parentelementid;
  4036. $granularity = $parentgranularity;
  4037. }
  4038. } else {
  4039. $elementid = (empty($elementid) ? 0 : $elementid);
  4040. if (!empty($onlyselected) && !empty($selected)) {
  4041. foreach ($selected as $shareid) {
  4042. $granularity[$elementid][] = $shareid;
  4043. }
  4044. }
  4045. }
  4046. $selectname = ($onlyselected ? $element.'_to[]' : 'from[]');
  4047. $selectid = ($onlyselected ? 'multiselect_shared_'.$element.'_to' : 'multiselect_shared_'.$element);
  4048. $return = '<select name="'.$selectname.'" id="'.$selectid.'" class="form-control multiselect-select multiselect-byelement-min-height" size="6" multiple="multiple">';
  4049. if (is_array($this->dao->entities)) {
  4050. foreach ($this->dao->entities as $otherentity) {
  4051. if ($otherentity->visible == 2) {
  4052. continue; // hide templates of entity
  4053. }
  4054. if ($currententity->id == $otherentity->id) {
  4055. continue; // hide current entity
  4056. }
  4057. $sharings = ((!empty($otherentity->options['sharings'][$parentelement]) && is_array($otherentity->options['sharings'][$parentelement])) ? $otherentity->options['sharings'][$parentelement] : array());
  4058. if (!in_array($currententity->id, $sharings)) {
  4059. continue; // Only if current entity share element
  4060. }
  4061. if (empty($onlyselected) && !empty($parentelementid) && $parentelement == 'thirdparty' && !empty($parentgranularity[$parentelementid]) && !in_array($otherentity->id, $parentgranularity[$parentelementid])) {
  4062. continue; // Hide entity if not shared by parents element
  4063. }
  4064. if (empty($onlyselected) && (empty($granularity) || !in_array($otherentity->id, $granularity[$elementid])) // All unselected
  4065. || (!empty($onlyselected) && (!empty($granularity) && in_array($otherentity->id, $granularity[$elementid])))) { // All selected
  4066. $return.= '<option class="oddeven multiselect-option" value="'.$otherentity->id.'">';
  4067. $return.= $otherentity->label;
  4068. if (empty($otherentity->visible)) {
  4069. $return.= ' ('.$langs->trans('Hidden').')';
  4070. }
  4071. $return.= '</option>';
  4072. }
  4073. }
  4074. }
  4075. $return.= '</select>';
  4076. return $return;
  4077. }
  4078. /**
  4079. * Check if element is shared globally at least once between entities
  4080. *
  4081. * @param string $element Name of element (thirdparty, product, ...)
  4082. * return bool
  4083. */
  4084. private function checkIfElementIsSharedByEntity($element)
  4085. {
  4086. if (!empty($this->config['validforsharing'][$element]) && $this->config['validforsharing'][$element] === true) {
  4087. return true;
  4088. }
  4089. global $conf;
  4090. $this->getInstanceDao();
  4091. $this->dao->getEntities(false, array($conf->entity), true);
  4092. $validforsharing = false;
  4093. if (is_array($this->dao->entities) && !empty($this->dao->entities)) {
  4094. foreach ($this->dao->entities as $otherentity) {
  4095. if ($otherentity->visible == 2) {
  4096. continue; // hide templates of entity
  4097. }
  4098. if ($conf->entity == $otherentity->id) {
  4099. continue; // hide current entity
  4100. }
  4101. $sharings = ((is_array($otherentity->options['sharings'][$element]) && !empty($otherentity->options['sharings'][$element])) ? $otherentity->options['sharings'][$element] : array());
  4102. if (!empty($sharings) && in_array($conf->entity, $sharings)) { // Only if current entity sharing element with at least one entity
  4103. $this->config['validforsharing'][$element] = true;
  4104. $validforsharing = true;
  4105. break;
  4106. }
  4107. }
  4108. }
  4109. return $validforsharing;
  4110. }
  4111. /**
  4112. * Return multiselect list of entities to override.
  4113. *
  4114. * @param string $htmlname Name of select
  4115. * @param bool $onlyselected true: show only selected, false: hide only selected
  4116. * @return string
  4117. */
  4118. public function multiselectEntitiesToOverride($htmlname, $onlyselected=false)
  4119. {
  4120. global $langs;
  4121. $this->getInstanceDao();
  4122. $this->dao->getEntities();
  4123. $selectname = ($onlyselected ? $htmlname.'_to[]' : 'from[]');
  4124. $selectid = ($onlyselected ? 'multiselect_'.$htmlname.'_to' : 'multiselect_'.$htmlname);
  4125. $out = '<select name="'.$selectname.'" id="'.$selectid.'" class="form-control multiselect-select multiselect-min-height" size="6" multiple="multiple">';
  4126. if (empty($onlyselected) && is_array($this->dao->entities)) {
  4127. foreach ($this->dao->entities as $entity) {
  4128. if ($entity->active == 1) {
  4129. $out.= '<option class="oddeven multiselect-option" value="'.$entity->id.'">';
  4130. $out.= $entity->label;
  4131. if (empty($entity->visible)) {
  4132. $out.= ' ('.$langs->trans('Hidden').')';
  4133. } elseif ($entity->visible == 2) {
  4134. $out.= ' ('.$langs->trans('Template').')';
  4135. }
  4136. $out.= '</option>';
  4137. }
  4138. }
  4139. }
  4140. $out.= '</select>';
  4141. return $out;
  4142. }
  4143. /**
  4144. * Return multiselect list of entities.
  4145. *
  4146. * @param string $htmlname Name of select
  4147. * @param array $selected Entities already selected
  4148. * @param string $option Option
  4149. * @return string
  4150. */
  4151. public function multiSelectEntities($htmlname, $selected=null, $option=null)
  4152. {
  4153. global $langs;
  4154. $this->getInstanceDao();
  4155. $this->dao->getEntities();
  4156. $return = '<select id="'.$htmlname.'" class="multiselect" multiple="multiple" name="'.$htmlname.'[]" '.$option.'>';
  4157. if (is_array($this->dao->entities)) {
  4158. foreach ($this->dao->entities as $entity) {
  4159. if ($entity->visible == 2) {
  4160. continue;
  4161. }
  4162. $return.= '<option value="'.$entity->id.'" ';
  4163. if (is_array($selected) && in_array($entity->id, $selected)) {
  4164. $return.= 'selected="selected"';
  4165. }
  4166. $return.= '>';
  4167. $return.= $entity->label;
  4168. if (empty($entity->visible)) {
  4169. $return.= ' ('.$langs->trans('Hidden').')';
  4170. }
  4171. $return.= '</option>';
  4172. }
  4173. }
  4174. $return.= '</select>';
  4175. return $return;
  4176. }
  4177. /**
  4178. * Switch to another entity.
  4179. *
  4180. * @param int $id User id
  4181. * @param int $entity Entity id
  4182. * @return int
  4183. */
  4184. public function checkRight($id, $entity)
  4185. {
  4186. global $user;
  4187. $this->getInstanceDao();
  4188. if ($this->dao->fetch($entity) > 0) {
  4189. // Controle des droits sur le changement
  4190. if ($this->dao->verifyRight($entity, $id) || !empty($user->admin)) {
  4191. return 1;
  4192. } else {
  4193. return -2;
  4194. }
  4195. } else {
  4196. return -1;
  4197. }
  4198. }
  4199. /**
  4200. * Switch to another entity.
  4201. * @param int $id Id of the destination entity
  4202. * @param int $userid
  4203. * @return int
  4204. */
  4205. public function switchEntity($id, $userid=null)
  4206. {
  4207. global $conf, $user;
  4208. $this->getInstanceDao();
  4209. if (!empty($userid)) {
  4210. $user=new User($this->db);
  4211. $user->fetch($userid);
  4212. }
  4213. if ($this->dao->fetch($id) > 0 && !empty($this->dao->active)) { // check if the entity is still active
  4214. // Controle des droits sur le changement
  4215. if (!empty($conf->global->MULTICOMPANY_HIDE_LOGIN_COMBOBOX)
  4216. || (!empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE) && $this->dao->verifyRight($id, $user->id))
  4217. || !empty($user->admin)) {
  4218. $_SESSION['dol_entity'] = $id;
  4219. //$conf = new Conf(); FIXME some constants disappear
  4220. $conf->entity = $id;
  4221. $conf->setValues($this->db);
  4222. return 1;
  4223. } else {
  4224. //var_dump($conf->global->MULTICOMPANY_HIDE_LOGIN_COMBOBOX);
  4225. //var_dump($conf->global->MULTICOMPANY_TRANSVERSE_MODE);
  4226. //var_dump($this->dao->verifyRight($id, $user->id));
  4227. return -2;
  4228. }
  4229. } else {
  4230. return -1;
  4231. }
  4232. }
  4233. /**
  4234. * Get entity info
  4235. * @param int $id Entity id
  4236. * @param boolean $getconfig Get entity config (constants) true or false
  4237. * @param string $constname Get specific contant
  4238. */
  4239. public function getInfo($id, $getconfig = false, $constname = null)
  4240. {
  4241. $this->getInstanceDao();
  4242. $this->dao->fetch($id);
  4243. $this->id = $this->dao->id;
  4244. $this->label = $this->dao->label;
  4245. $this->country_id = $this->dao->country_id;
  4246. $this->country_code = $this->dao->country_code;
  4247. $this->currency_code = $this->dao->currency_code;
  4248. $this->language_code = $this->dao->language_code;
  4249. $this->description = $this->dao->description;
  4250. $this->options = $this->dao->options;
  4251. $this->active = $this->dao->active;
  4252. $this->visible = $this->dao->visible;
  4253. $this->constants = ($getconfig === true ? $this->dao->getEntityConfig($id, $constname) : array());
  4254. }
  4255. /**
  4256. * Get sharings by element
  4257. *
  4258. * @param string $element Name of element (thirdparty, product, ...)
  4259. * @param int $elementid Id of current element for check sharing granularity
  4260. */
  4261. public function getSharingsByElement($element, $elementid = null)
  4262. {
  4263. $this->getInstanceDao();
  4264. $entities = $this->dao->getListOfSharingsByElement($element, $elementid);
  4265. if (is_array($entities)) {
  4266. $this->config['sharedwith'][$element] = $entities;
  4267. }
  4268. }
  4269. /**
  4270. * Get action title
  4271. * @param string $action Type of action
  4272. * @return string
  4273. */
  4274. public function getTitle($action='')
  4275. {
  4276. global $langs;
  4277. if ($action == 'create') {
  4278. return $langs->trans("AddEntity");
  4279. } elseif ($action == 'edit') {
  4280. return $langs->trans("EditEntity");
  4281. } else {
  4282. return $langs->trans("EntitiesManagement");
  4283. }
  4284. }
  4285. /**
  4286. * Assigne les valeurs pour les templates
  4287. * @param string $action Type of action
  4288. */
  4289. public function assign_values($action='view')
  4290. {
  4291. global $conf, $langs, $user;
  4292. global $form, $formcompany, $formadmin;
  4293. require_once DOL_DOCUMENT_ROOT . '/core/class/extrafields.class.php';
  4294. $this->tpl['extrafields'] = new ExtraFields($this->db);
  4295. // fetch optionals attributes and labels
  4296. $this->tpl['extralabels'] = $this->tpl['extrafields']->fetch_name_optionals_label('entity');
  4297. $this->getInstanceDao();
  4298. $this->template_dir = dol_buildpath('/multicompany/admin/tpl/');
  4299. $this->template = 'list.tpl.php';
  4300. if ($action == 'create' || $action == 'edit') {
  4301. $this->template = 'card.tpl.php';
  4302. if ($action == 'edit' && GETPOSTISSET('id')) {
  4303. $ret = $this->dao->fetch(GETPOST('id', 'int'));
  4304. }
  4305. if (!empty($conf->global->MULTICOMPANY_TEMPLATE_MANAGEMENT)) {
  4306. $templatevalue = (GETPOSTISSET('template') ? GETPOST('template', 'int') : ($this->dao->visible === '2' ? 1 : 0));
  4307. if ($action == 'create') {
  4308. $this->tpl['template'] = $form->selectyesno('template', $templatevalue, 1);
  4309. $this->tpl['select_template'] = $this->select_entities('','usetemplate','',false,false,true,'','','minwidth200imp',true,'only');
  4310. } elseif ($templatevalue === 1) {
  4311. $this->tpl['template'] = $templatevalue;
  4312. }
  4313. }
  4314. // action
  4315. $this->tpl['action'] = $action;
  4316. // id
  4317. $this->tpl['id'] = (GETPOSTISSET('id')?GETPOST('id', 'int'):null);
  4318. // Label
  4319. $this->tpl['label'] = (GETPOSTISSET('label')?GETPOST('label', 'alpha'):$this->dao->label);
  4320. // Description
  4321. $this->tpl['description'] = (GETPOSTISSET('description')?GETPOST('description', 'alpha'):$this->dao->description);
  4322. // Company name
  4323. $this->tpl['name'] = (GETPOSTISSET('name')?GETPOST('name', 'alpha'):$this->dao->name);
  4324. // Address
  4325. $this->tpl['address'] = (GETPOSTISSET('address')?GETPOST('address', 'alpha'):$this->dao->address);
  4326. // Zip
  4327. $this->tpl['select_zip'] = $formcompany->select_ziptown((GETPOSTISSET('zipcode')?GETPOST('zipcode', 'alpha'):$this->dao->zip),'zipcode',array('town','selectcountry_id','departement_id'),6);
  4328. // Town
  4329. $this->tpl['select_town'] = $formcompany->select_ziptown((GETPOSTISSET('town')?GETPOST('town', 'alpha'):$this->dao->town),'town',array('zipcode','selectcountry_id','departement_id'),40);
  4330. if (!empty($user->admin)) $this->tpl['info_admin'] = info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"),1);
  4331. // Option to add possibility to clone accounting account from another entity
  4332. if (!empty($conf->accounting->enabled)) {
  4333. $this->tpl['cloneaccountingaccount'] = $form->selectyesno('cloneaccountingaccount', $this->dao->options['cloneaccountingaccount'], 1);
  4334. }
  4335. // We define country_id
  4336. if (GETPOSTISSET('country_id')) {
  4337. $country_id = GETPOST('country_id', 'int');
  4338. } elseif (!empty($this->dao->country_id)) {
  4339. $country_id = $this->dao->country_id;
  4340. } else if (!empty($conf->global->MAIN_INFO_SOCIETE_COUNTRY)) {
  4341. $tmp = explode(':', $conf->global->MAIN_INFO_SOCIETE_COUNTRY);
  4342. $country_id = $tmp[0];
  4343. } else {
  4344. $country_id = 0;
  4345. }
  4346. $this->tpl['select_country'] = $form->select_country($country_id,'country_id');
  4347. $this->tpl['select_state'] = $formcompany->select_state((GETPOSTISSET('departement_id')?GETPOST('departement_id', 'int'):$this->dao->state_id),$country_id,'departement_id');
  4348. $this->tpl['select_currency'] = $form->selectCurrency((GETPOSTISSET('currency_code')?GETPOST('currency_code', 'alpha'):($this->dao->currency_code?$this->dao->currency_code:$conf->currency)),"currency_code");
  4349. $this->tpl['select_language'] = $formadmin->select_language((GETPOSTISSET('main_lang_default')?GETPOST('main_lang_default', 'alpha'):($this->dao->language_code?$this->dao->language_code:$conf->global->MAIN_LANG_DEFAULT)),'main_lang_default',1);
  4350. if (!empty($conf->global->MULTICOMPANY_SHARINGS_ENABLED)) {
  4351. foreach ($this->sharingelements as $element => $params) {
  4352. if (($params['type'] === 'object' && !isset($params['disable']))
  4353. && (empty($conf->societe->enabled) || empty($conf->global->MULTICOMPANY_THIRDPARTY_SHARING_ENABLED))) {
  4354. continue;
  4355. }
  4356. $uppername = strtoupper($element);
  4357. $constname = 'MULTICOMPANY_' . $uppername . '_SHARING_ENABLED';
  4358. if (!empty($conf->global->$constname)) {
  4359. $this->tpl['multiselect_from_' . $element] = $this->multiselect_entities($element, $this->dao, false);
  4360. $this->tpl['multiselect_to_' . $element] = $this->multiselect_entities($element, $this->dao, true);
  4361. if ($params['type'] === 'objectnumber') {
  4362. $this->tpl['select_'.$element.'_entity'] = $this->select_entities($this->dao->options[$element.'_referent'], $element.'_referring_entity');
  4363. }
  4364. $addtoallother = (GETPOSTISSET('addtoallother_' . $element) ? GETPOST('addtoallother_' . $element, 'int') : (!empty($this->dao->options['addtoallother']) ? $this->dao->options['addtoallother'][$element] : 0));
  4365. $this->tpl['addtoallother_' . $element] = $form->selectyesno('addtoallother_' . $element, $addtoallother, 1);
  4366. }
  4367. }
  4368. }
  4369. }
  4370. }
  4371. /**
  4372. * Display the template
  4373. */
  4374. public function display()
  4375. {
  4376. global $conf, $langs;
  4377. global $form, $object;
  4378. include $this->template_dir.$this->template;
  4379. }
  4380. /**
  4381. * Set values of global conf for multicompany
  4382. *
  4383. * @param Conf $conf Object conf
  4384. * @return void
  4385. */
  4386. public function setValues(&$conf)
  4387. {
  4388. if (!empty($conf->global->MULTICOMPANY_SHARINGS_ENABLED)) {
  4389. $this->getInstanceDao();
  4390. $this->dao->fetch($conf->entity);
  4391. $this->sharings = (isset($this->dao->options['sharings'])?$this->dao->options['sharings']:'');
  4392. foreach ($this->sharingelements as $element => $params) {
  4393. if ($params['type'] === 'objectnumber' && !isset($params['disable'])) {
  4394. $varname = $element.'_referent';
  4395. $this->$varname = (isset($this->dao->options[$varname]) ? $this->dao->options[$varname] : '');
  4396. }
  4397. }
  4398. // Load shared elements
  4399. $this->loadSharedElements();
  4400. // Define output dir for others entities
  4401. $this->setMultiOutputDir($conf);
  4402. }
  4403. if (!empty($this->customdicts)) {
  4404. foreach($this->customdicts as $dict => $params) {
  4405. $constname = 'MULTICOMPANY_'.strtoupper($dict).'_CUSTOM_ENABLED';
  4406. if (!empty($conf->global->$constname)) {
  4407. $this->dict[$dict] = true;
  4408. }
  4409. }
  4410. }
  4411. }
  4412. /**
  4413. * Set status of an entity
  4414. *
  4415. * @param int $id Id of entity
  4416. * @param string $type Type of status (visible or active)
  4417. * @param string $value Value of status (0: disable, 1: enable)
  4418. * @return int
  4419. */
  4420. public function setStatus($id, $type='active', $value)
  4421. {
  4422. global $user;
  4423. if (!empty($user->admin) && empty($user->entity)) {
  4424. $this->getInstanceDao();
  4425. return $this->dao->setEntity($id, $type, $value);
  4426. } else {
  4427. return -1;
  4428. }
  4429. }
  4430. /**
  4431. * Delete an entity
  4432. *
  4433. * @param int $id Id of entity
  4434. * @return int
  4435. */
  4436. public function deleteEntity($id)
  4437. {
  4438. global $user;
  4439. if (!empty($user->admin) && empty($user->entity) && $id != 1) {
  4440. $this->getInstanceDao();
  4441. return $this->dao->delete($id);
  4442. }
  4443. else {
  4444. return -1;
  4445. }
  4446. }
  4447. /**
  4448. * Get list of entity id to use.
  4449. *
  4450. * @param string $element Current element
  4451. * 'societe', 'socpeople', 'actioncomm', 'agenda', 'resource',
  4452. * 'product', 'productprice', 'stock',
  4453. * 'propal', 'supplier_proposal', 'invoice', 'facture_fourn', 'payment_various',
  4454. * 'categorie', 'bank_account', 'bank_account', 'adherent', 'user',
  4455. * 'commande', 'commande_fournisseur', 'expedition', 'intervention', 'survey',
  4456. * 'contract', 'tax', 'expensereport', 'holiday', 'multicurrency', 'project',
  4457. * 'email_template', 'event', 'donation'
  4458. * 'c_paiement', 'c_payment_term', ...
  4459. * @param int $shared 0=Return id of current entity only,
  4460. * 1=Return id of current entity + shared entities (default)
  4461. * @param object $currentobject Current object if needed
  4462. * @return mixed Entity id(s) to use ( eg. entity IN ('.getEntity(elementname).')' )
  4463. */
  4464. public function getEntity($element=false, $shared=1, $currentobject=null)
  4465. {
  4466. global $conf, $user;
  4467. $element = str_replace(MAIN_DB_PREFIX, '', $element);
  4468. if (in_array($element, $this->addzero)) {
  4469. if (($element == 'user') && !empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE)) {
  4470. return '0,1'; // In transverse mode all users except superadmin and groups are in entity 1
  4471. } else {
  4472. if ($element == 'usergroup' && $conf->entity == 1 && empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE) && !empty($user->admin) && empty($user->entity)) {
  4473. return '0,'.implode(',', array_keys($this->getEntitiesList(true, false, true))); // Superadmin can always edit groups of another entities
  4474. }
  4475. return '0,'.$conf->entity;
  4476. }
  4477. }
  4478. // Custom dictionaries
  4479. if (array_key_exists($element, $this->customdicts)) {
  4480. if (!empty($this->dict[$element])) {
  4481. return $conf->entity;
  4482. } else {
  4483. return 1; // Master entity
  4484. }
  4485. }
  4486. // Check object entity when number sharing is disabled
  4487. if (is_object($currentobject)) {
  4488. foreach ($this->sharingelements as $elementname => $params) {
  4489. if ($params['type'] === 'objectnumber' && !isset($params['disable'])) {
  4490. $constname = 'MULTICOMPANY_'.strtoupper($element).'_SHARING_ENABLED';
  4491. if ($element === $elementname && empty($conf->global->$constname)) {
  4492. // Use object entity ID
  4493. $entity = ((isset($currentobject->entity) && is_numeric($currentobject->entity)) ? $currentobject->entity : $conf->entity);
  4494. return $entity;
  4495. }
  4496. }
  4497. }
  4498. }
  4499. if (!empty($element)) {
  4500. if (!empty($this->sharingnamecompatibility[$element])) {
  4501. $element = $this->sharingnamecompatibility[$element];
  4502. }
  4503. if (!empty($this->entities[$element])) {
  4504. if (!empty($shared)) {
  4505. return $this->entities[$element];
  4506. }
  4507. }
  4508. }
  4509. return $conf->entity;
  4510. }
  4511. /**
  4512. * Set entity id to use when to create an object
  4513. *
  4514. * @param object $currentobject Current object
  4515. * @return int Entity id to use
  4516. */
  4517. public function setEntity($currentobject)
  4518. {
  4519. global $conf;
  4520. $entity = $conf->entity;
  4521. if (is_object($currentobject) && !empty($currentobject->element)) {
  4522. $element = array_search($currentobject->element, $this->sharingmodulename);
  4523. $element = (!empty($element) ? $element : $currentobject->element);
  4524. $constname = 'MULTICOMPANY_'.strtoupper($element).'_SHARING_ENABLED';
  4525. $newentity = $entity;
  4526. if (isset($this->sharingelements[$element]) && isset($this->sharingelements[$element]['active']) && !empty($conf->global->$constname)) {
  4527. if (GETPOSTISSET('entity') && GETPOST('entity', 'int', 2)) {
  4528. $newentity = GETPOST('entity', 'int', 2);
  4529. } elseif (isset($currentobject->entity) && is_numeric($currentobject->entity)) {
  4530. $newentity = $currentobject->entity;
  4531. }
  4532. if (isset($this->sharings[$element]) && in_array($newentity, $this->sharings[$element])) {
  4533. $entity = $newentity;
  4534. }
  4535. } else {
  4536. $entity = (($currentobject->id > 0 && $currentobject->entity > 0) ? $currentobject->entity : $conf->entity);
  4537. }
  4538. }
  4539. return $entity;
  4540. }
  4541. /**
  4542. * Get entities list
  4543. *
  4544. * @param int $login If use in login page or not
  4545. * @param array $exclude Entity ids to exclude
  4546. * @param bool $onlyactive sort only active entities
  4547. * @param bool $showtemplate Show or not templates
  4548. * @return array Array of entities (id => label)
  4549. */
  4550. public function getEntitiesList($login = false, $exclude = false, $onlyactive = false, $showtemplate = false)
  4551. {
  4552. global $langs;
  4553. $this->getInstanceDao();
  4554. $this->dao->getEntities($login, $exclude, $onlyactive);
  4555. $entities=array();
  4556. foreach ($this->dao->entities as $entity) {
  4557. if (empty($showtemplate) && $entity->visible == 2) {
  4558. continue;
  4559. }
  4560. $info = (empty($entity->active) ? ' ('.$langs->transnoentities('Disabled').')' : ($entity->visible == 2 ? ' ('.$langs->transnoentities('Template').')' : (empty($entity->visible) ? ' ('.$langs->transnoentities('Hidden').')' : '')));
  4561. $entities[$entity->id] = dol_html_entity_decode($entity->label, null) . $info;
  4562. }
  4563. return $entities;
  4564. }
  4565. /**
  4566. * Set object documents directory to use
  4567. *
  4568. * @param Conf $conf Object Conf
  4569. * @return void
  4570. */
  4571. public function setMultiOutputDir(&$conf)
  4572. {
  4573. if (!empty($this->entities)) {
  4574. foreach($this->entities as $element => $shares) {
  4575. if ($element == 'thirdparty') {
  4576. $element = 'societe';
  4577. } elseif ($element == 'member') {
  4578. $element = 'adherent';
  4579. } elseif ($element == 'proposal') {
  4580. $element = 'propal';
  4581. } elseif ($element == 'order') {
  4582. $element = 'commande';
  4583. } elseif ($element == 'invoice') {
  4584. $element = 'facture';
  4585. } elseif ($element == 'intervention') {
  4586. $element = 'ficheinter';
  4587. }
  4588. if (!empty($conf->$element->enabled) && isset($conf->$element->multidir_output) && isset($conf->$element->multidir_temp)) {
  4589. $elementpath = $element;
  4590. if ($element == 'product') {
  4591. $elementpath = 'produit';
  4592. } elseif ($element == 'category') {
  4593. $elementpath = 'categorie';
  4594. } elseif ($element == 'propal') {
  4595. $elementpath = 'propale';
  4596. }
  4597. $entities = explode(",", $shares);
  4598. $dir_output = array();
  4599. $dir_temp = array();
  4600. foreach($entities as $entity) {
  4601. if (!array_key_exists($entity, $conf->$element->multidir_output)) {
  4602. $path = ($entity > 1 ? "/".$entity : '');
  4603. $dir_output[$entity] = DOL_DATA_ROOT.$path."/".$elementpath;
  4604. $dir_temp[$entity] = DOL_DATA_ROOT.$path."/".$elementpath."/temp";
  4605. $conf->$element->multidir_output += $dir_output;
  4606. $conf->$element->multidir_temp += $dir_temp;
  4607. }
  4608. if (in_array($element, array('propal', 'commande', 'facture'))) {
  4609. if (!isset($conf->mycompany->multidir_output) || !is_array($conf->mycompany->multidir_output)) {
  4610. $conf->mycompany->multidir_output = array();
  4611. }
  4612. if (!isset($conf->mycompany->multidir_temp) || !is_array($conf->mycompany->multidir_temp)) {
  4613. $conf->mycompany->multidir_temp = array();
  4614. }
  4615. if (!array_key_exists($entity, $conf->mycompany->multidir_output)) {
  4616. $path = ($entity > 1 ? "/".$entity : '');
  4617. $dir_output[$entity] = DOL_DATA_ROOT.$path."/mycompany";
  4618. $dir_temp[$entity] = DOL_DATA_ROOT.$path."/mycompany/temp";
  4619. $conf->mycompany->multidir_output += $dir_output;
  4620. $conf->mycompany->multidir_temp += $dir_temp;
  4621. }
  4622. }
  4623. }
  4624. }
  4625. }
  4626. }
  4627. }
  4628. /**
  4629. * @param bool $parameters
  4630. * @return int
  4631. */
  4632. public function printTopRightMenu($parameters = false)
  4633. {
  4634. echo $this->getTopRightMenu();
  4635. return 0;
  4636. }
  4637. /**
  4638. *
  4639. */
  4640. public function printBugtrackInfo($parameters = false)
  4641. {
  4642. global $conf;
  4643. if (!empty($conf->multicompany->enabled) && !empty($conf->global->MAIN_BUGTRACK_ENABLELINK) && $conf->global->MAIN_BUGTRACK_ENABLELINK == "github") {
  4644. $this->resprints = urlencode("- **Multicompany**: " . $conf->global->MULTICOMPANY_MAIN_VERSION . "\n");
  4645. }
  4646. return 0;
  4647. }
  4648. /**
  4649. * @param bool $parameters
  4650. * @return int
  4651. */
  4652. public function updateSession($parameters = false)
  4653. {
  4654. global $conf;
  4655. if (empty($conf->multicompany->enabled)) {
  4656. return 0;
  4657. }
  4658. // Switch to another entity
  4659. if (GETPOST('action','aZ') == 'switchentity') {
  4660. if ($this->switchEntity(GETPOST('entity','int')) > 0) {
  4661. $requesturi = '/';
  4662. if (!empty($conf->global->MULTICOMPANY_SWITCHENTITY_ACTION_KEPT_REQUEST_URI)) {
  4663. $requesturi = removeParam($_SERVER['REQUEST_URI'], 'action');
  4664. }
  4665. header("Location: ".DOL_URL_ROOT.$requesturi);
  4666. exit;
  4667. }
  4668. } elseif (GETPOSTISSET('switchentity')) {
  4669. if ($this->switchEntity(GETPOST('switchentity','int')) > 0) {
  4670. header("Location: ".DOL_URL_ROOT.removeParam($_SERVER['REQUEST_URI'], 'switchentity'));
  4671. exit;
  4672. }
  4673. }
  4674. }
  4675. /**
  4676. *
  4677. */
  4678. public function getLoginPageOptions($parameters = false)
  4679. {
  4680. global $conf, $langs;
  4681. if (empty($conf->multicompany->enabled)) {
  4682. return 0;
  4683. }
  4684. if (is_array($parameters) && !empty($parameters)) {
  4685. foreach($parameters as $key => $value) {
  4686. $$key = $value;
  4687. }
  4688. }
  4689. $currentcontext = explode(':', $parameters['context']);
  4690. if ((in_array('login', $currentcontext) || in_array('cashdeskloginpage', $currentcontext)) && !empty($conf->global->MULTICOMPANY_HIDE_LOGIN_COMBOBOX)) {
  4691. return 0;
  4692. }
  4693. if (in_array('passwordforgottenpage', $currentcontext) && !empty($conf->global->MULTICOMPANY_FORCE_PASSWORDFORGOTTEN_PAGE_ENABLED)) {
  4694. return 0;
  4695. }
  4696. // Entity combobox
  4697. /*if (empty($entity)) {
  4698. $entity = 1;
  4699. }*/
  4700. $lastentity = (!empty($conf->global->MULTICOMPANY_FORCE_ENTITY) ? $conf->global->MULTICOMPANY_FORCE_ENTITY : $entity);
  4701. if (in_array('cashdeskloginpage', $currentcontext)) {
  4702. $select_entity = $this->select_entities($lastentity, 'entity', ' tabindex="3"', true, false, false, false, '', 'minwidth100imp');
  4703. $tableformat = '<tr>';
  4704. $tableformat.= '<td class="label1">'.$langs->trans("Entity").'</td>';
  4705. $tableformat.= '<td>'.$select_entity.'</td>';
  4706. $tableformat.= '</tr>';
  4707. $this->resprints = $tableformat;
  4708. } else {
  4709. $select_entity = $this->select_entities($lastentity, 'entity', ' tabindex="3"', true, false, false, false, '', 'login-entity minwidth180');
  4710. $divformat = "\n".'<div class="trinputlogin multicompany-trinputlogin">'."\n";
  4711. $divformat.= '<div class="tagtd nowrap text-align-left valignmiddle tdinputlogin">'."\n";
  4712. $divformat.= '<span class="fa fa-globe">'.$select_entity.'</span>'."\n";
  4713. $divformat.= '</div></div>';
  4714. if (!empty($conf->global->MULTICOMPANY_LOGIN_LOGO_BY_ENTITY)) {
  4715. $divformat.= '
  4716. <script type="text/javascript">
  4717. $(document).ready(function() {
  4718. var entityId = $("#entity").val();
  4719. if (entityId > 1) {
  4720. checkEntity();
  4721. }
  4722. $("#login_right").on("change", "#entity", function() {
  4723. checkEntity();
  4724. });
  4725. function checkEntity() {
  4726. $.post( "'.dol_buildpath('/multicompany/core/ajax/functions.php', 1).'", {
  4727. action: \'getEntityLogo\',
  4728. id: $("#entity").val(),
  4729. token: "'.newToken().'"
  4730. },
  4731. function(result) {
  4732. if (result.status == "success") {
  4733. if (result.unsplashimg != "") {
  4734. var xhr;
  4735. var _orgAjax = jQuery.ajaxSettings.xhr;
  4736. jQuery.ajaxSettings.xhr = function () {
  4737. xhr = _orgAjax();
  4738. return xhr;
  4739. };
  4740. $.ajax(result.unsplashimg, {
  4741. success: function() {
  4742. $("body").css("background-color", "#ccc");
  4743. changeImgBg(xhr.responseURL);
  4744. changeImgLogo(result.urllogo);
  4745. },
  4746. error: function() {
  4747. if (result.bgimg != "") {
  4748. changeImgBg(result.bgimg);
  4749. }
  4750. changeImgLogo(result.urllogo);
  4751. }
  4752. });
  4753. } else if (result.bgimg != "") {
  4754. changeImgBg(result.bgimg);
  4755. changeImgLogo(result.urllogo);
  4756. } else {
  4757. changeImgLogo(result.urllogo);
  4758. }
  4759. }
  4760. });
  4761. };
  4762. function changeImgBg(bgimg) {
  4763. $("body").fadeTo(300,0.01, function() {
  4764. $("body").css("background-image", "url(" + bgimg + ")");
  4765. }).fadeTo(300,1);
  4766. };
  4767. function changeImgLogo(urllogo) {
  4768. $("#img_logo").fadeTo(400,0.01, function() {
  4769. $("#img_logo").attr("src", urllogo);
  4770. }).fadeTo(400,1);
  4771. };
  4772. });
  4773. </script>';
  4774. }
  4775. $this->resprints = $divformat;
  4776. }
  4777. return 0;
  4778. }
  4779. /**
  4780. *
  4781. * @param boolean $parameters
  4782. * @return number
  4783. */
  4784. public function getPasswordForgottenPageOptions($parameters = false)
  4785. {
  4786. return $this->getLoginPageOptions($parameters);
  4787. }
  4788. /**
  4789. *
  4790. * @param boolean $parameters
  4791. * @return number
  4792. */
  4793. public function getPasswordForgottenPageExtraOptions($parameters = false)
  4794. {
  4795. global $conf, $template_dir;
  4796. if (empty($conf->multicompany->enabled)) {
  4797. return 0;
  4798. } elseif (empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE)) {
  4799. return 0;
  4800. } elseif (empty($conf->global->MULTICOMPANY_FORCE_PASSWORDFORGOTTEN_PAGE_ENABLED)) {
  4801. return 0;
  4802. }
  4803. if (file_exists(dol_buildpath("/multicompany/core/tpl/passwordforgotten.tpl.php"))) {
  4804. $template_dir = dol_buildpath("/multicompany/core/tpl/");
  4805. }
  4806. }
  4807. /**
  4808. * Add all entities default dictionnaries in database
  4809. */
  4810. public function addAllEntitiesDefaultDicts()
  4811. {
  4812. if (!empty($this->customdicts)) {
  4813. $this->getInstanceDao();
  4814. $this->dao->getEntities();
  4815. $dir = "/multicompany/sql/init/";
  4816. foreach($this->customdicts as $dict => $data) {
  4817. // Load sql llx_*.sql file
  4818. $file = 'llx_'.$dict.'.sql';
  4819. $fullpath = dol_buildpath($dir.$file);
  4820. if (file_exists($fullpath)) {
  4821. foreach ($this->dao->entities as $entity) {
  4822. if ($entity->id == 1) {
  4823. continue;
  4824. }
  4825. $result = run_sql($fullpath, 1, $entity->id);
  4826. }
  4827. }
  4828. }
  4829. }
  4830. }
  4831. /**
  4832. * Load shared elements
  4833. *
  4834. * @return void
  4835. */
  4836. private function loadSharedElements()
  4837. {
  4838. global $conf;
  4839. if (!empty($this->sharings)) {
  4840. $this->getInstanceDao();
  4841. foreach($this->sharings as $element => $ids) {
  4842. $modulesharingenabled = 'MULTICOMPANY_'.strtoupper($element).'_SHARING_ENABLED';
  4843. $module = ((isset($this->sharingmodulename[$element]) && !empty($this->sharingmodulename[$element])) ? $this->sharingmodulename[$element] : $element);
  4844. if (!empty($conf->$module->enabled) && !empty($conf->global->$modulesharingenabled)) {
  4845. $entities=array();
  4846. if (!empty($ids)) {
  4847. foreach ($ids as $id) {
  4848. $ret=$this->dao->fetch($id);
  4849. if ($ret > 0 && $this->dao->active) {
  4850. $entities[] = $id;
  4851. }
  4852. }
  4853. $this->entities[$element] = (!empty($entities) ? implode(",", $entities) : 0);
  4854. $this->entities[$element].= ','.$conf->entity;
  4855. }
  4856. }
  4857. }
  4858. }
  4859. //var_dump($this->entities);
  4860. }
  4861. /**
  4862. * Get modify entity dialog
  4863. */
  4864. private function getModifyEntityDialog($htmlname, $action, $object)
  4865. {
  4866. global $langs;
  4867. $langs->loadLangs(array('errors','multicompany@multicompany'));
  4868. $selectEntities = $this->select_entities('', 'entity' . $htmlname, '', false, array($object->entity));
  4869. $out = '';
  4870. if (!empty($selectEntities)) {
  4871. $out.= '<!-- BEGIN MULTICOMPANY AJAX TEMPLATE -->';
  4872. $out.= '
  4873. <script type="text/javascript">
  4874. $(document).ready(function() {
  4875. $( "#modify-entity-'.$htmlname.'" ).click(function() {
  4876. $( "#dialog-modify-'.$htmlname.'" ).dialog({
  4877. modal: true,
  4878. resizable: false,
  4879. width: 400,
  4880. height: 200,
  4881. open: function() {
  4882. $(".ui-dialog-buttonset > button:last").focus();
  4883. },
  4884. buttons: {
  4885. \''.$langs->trans('Validate').'\': function() {
  4886. $.post( "'.dol_buildpath('/multicompany/core/ajax/functions.php',1).'", {
  4887. action: \''.$action.'\',
  4888. element: \''.$object->element.'\',
  4889. fk_element: \''.$object->id.'\',
  4890. id: $( "#entity'.$htmlname.'" ).val(),
  4891. token: "'.currentToken().'"
  4892. },
  4893. function(result) {
  4894. if (result.status == "success") {
  4895. $.jnotify("'.$langs->trans(ucfirst($htmlname) . "ModifyEntitySuccess").'", "ok");
  4896. $( "#dialog-modify-'.$htmlname.'" ).dialog( "close" );
  4897. window.location.href = "'.$_SERVER["PHP_SELF"].'?'.($htmlname == 'thirdparty'?'socid':'id').'='.$object->id.'";
  4898. } else {
  4899. $.jnotify("'.$langs->trans("Error" . ucfirst($htmlname) . "ModifyEntity").'", "error", true);
  4900. if (result.error) {
  4901. if (result.error == "ErrorCustomerCodeAlreadyUsed") {
  4902. $.jnotify("'.$langs->trans("ErrorCustomerCodeAlreadyUsed").'", "error", true);
  4903. }
  4904. }
  4905. }
  4906. });
  4907. },
  4908. \''.$langs->trans('Cancel').'\': function() {
  4909. $(this).dialog( "close" );
  4910. }
  4911. }
  4912. });
  4913. });
  4914. });
  4915. </script>';
  4916. $out.= '<div id="dialog-modify-' . $htmlname . '" class="hideobject" title="' . $langs->trans(ucfirst($htmlname) . 'ModifyEntity') . '">'."\n";
  4917. $out.= '<p>' . img_warning() . ' ' . $langs->trans(ucfirst($htmlname) . 'ModifyEntityDescription') . '</p>'."\n";
  4918. $out.= '<div>' . $langs->trans('SelectAnEntity');
  4919. $out.= $selectEntities . '</div>'."\n";
  4920. $out.= '</div>'."\n";
  4921. $out.= '<!-- END MULTICOMPANY AJAX TEMPLATE -->';
  4922. }
  4923. return $out;
  4924. }
  4925. /**
  4926. * Show entity info
  4927. */
  4928. private function getTopRightMenu()
  4929. {
  4930. global $conf, $user, $langs;
  4931. $langs->loadLangs(array('languages','admin','multicompany@multicompany'));
  4932. $out='';
  4933. if (!empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE) || !empty($user->admin)) {
  4934. $usedropdownmenu = false;
  4935. if (empty($conf->global->MULTICOMPANY_DROPDOWN_MENU_DISABLED)) {
  4936. // For theme test link
  4937. if (GETPOSTISSET('theme')) {
  4938. if (GETPOST('theme', 'aZ', 1) === 'eldy') {
  4939. $usedropdownmenu = true;
  4940. }
  4941. } else {
  4942. // For user theme
  4943. if (!empty($user->conf->MAIN_THEME)) {
  4944. if ($user->conf->MAIN_THEME === 'eldy') {
  4945. $usedropdownmenu = true;
  4946. }
  4947. // For global theme
  4948. } elseif ($conf->global->MAIN_THEME === 'eldy') {
  4949. $usedropdownmenu = true;
  4950. }
  4951. }
  4952. }
  4953. if ($usedropdownmenu) {
  4954. $out.= $this->getDropdownMenu();
  4955. } else {
  4956. $form=new Form($this->db);
  4957. $this->getInfo($conf->entity);
  4958. $selected = $conf->entity;
  4959. $exclude = false;
  4960. if (getDolGlobalInt('MULTICOMPANY_HIDE_CURRENT_ENTITY_IN_COMBOBOX')) {
  4961. $selected = '';
  4962. $exclude = array($conf->entity);
  4963. }
  4964. $selectEntities = $this->select_entities($selected, 'changeentity', '', false, $exclude, false, false, '', 'minwidth200imp', true, true);
  4965. $eldytheme = false;
  4966. // For theme test link
  4967. if (GETPOSTISSET('theme')) {
  4968. if (GETPOST('theme', 'aZ', 1) === 'eldy') {
  4969. $eldytheme = true;
  4970. }
  4971. } else {
  4972. // For user theme
  4973. if (!empty($user->conf->MAIN_THEME)) {
  4974. if ($user->conf->MAIN_THEME === 'eldy') {
  4975. $eldytheme = true;
  4976. }
  4977. // For global theme
  4978. } elseif ($conf->global->MAIN_THEME === 'eldy') {
  4979. $eldytheme = true;
  4980. }
  4981. }
  4982. $text ='<span class="fa fa-globe atoplogin switchentity'.($eldytheme ? '' : ' padding-top3').'">';
  4983. if ($eldytheme && empty($conf->global->MULTICOMPANY_NO_TOP_MENU_ENTITY_LABEL)) {
  4984. $text.= '<span class="topmenu-mc-label">'.$this->label.'</span>';
  4985. }
  4986. $text.= '</span>';
  4987. if ($cache = getCache('mc_country_' . $this->country_id)) {
  4988. $country = $cache;
  4989. } else {
  4990. $country = getCountry($this->country_id);
  4991. setCache('mc_country_' . $this->country_id, $country);
  4992. }
  4993. $imgCountry=picto_from_langcode($this->country_code, 'class="multicompany-flag-country"');
  4994. $imgLang=picto_from_langcode($this->language_code, 'class="multicompany-flag-language"');
  4995. $htmltext ='<u>'.$langs->trans("Entity").'</u>'."\n";
  4996. $htmltext.= '<br>';
  4997. $htmltext.='<br><b>'.$langs->trans("Label").'</b>: '.$this->label."\n";
  4998. $htmltext.='<br><b>'.$langs->trans("Country").'</b>: '. ($imgCountry?$imgCountry.' ':'') . $country."\n";
  4999. $htmltext.='<br><b>'.$langs->trans("Currency").'</b>: '. currency_name($this->currency_code) . ' (' . $langs->getCurrencySymbol($this->currency_code) . ')'."\n";
  5000. $htmltext.='<br><b>'.$langs->trans("Language").'</b>: '. ($imgLang?$imgLang.' ':'') . ($this->language_code=='auto'?$langs->trans("AutoDetectLang"):$langs->trans("Language_".$this->language_code));
  5001. if (!empty($this->description)) {
  5002. $htmltext.='<br><b>'.$langs->trans("Description").'</b>: '.$this->description."\n";
  5003. }
  5004. $htmltext.= '<br><br>';
  5005. $out.= '<div class="inline-block nowrap">';
  5006. $out.= $form->textwithtooltip('', $htmltext, 2, 1, $text, 'login_block_elem multicompany_block', 2);
  5007. $out .= '</div>';
  5008. if (!empty($selectEntities)) {
  5009. $out.= '
  5010. <script type="text/javascript">
  5011. $(document).ready(function() {
  5012. $(".switchentity").click(function() {
  5013. $( "#dialog-switchentity" ).dialog({
  5014. modal: true,
  5015. width: '.($conf->dol_optimize_smallscreen ? 300 : 400).',
  5016. buttons: {
  5017. \''.$langs->trans('Ok').'\': function() {
  5018. $.post("'.dol_buildpath('/multicompany/core/ajax/functions.php', 1).'", {
  5019. action: \'switchEntity\',
  5020. id: $( "#changeentity" ).val(),
  5021. token: "'.currentToken().'"
  5022. },
  5023. function(content) {
  5024. $("#dialog-switchentity").dialog( "close" );
  5025. var url = window.location.pathname;
  5026. var queryString = window.location.href.split("?")[1];
  5027. if (queryString) {
  5028. var params = parseQueryString(queryString);
  5029. delete params.action;
  5030. delete params.switchentityautoopen;
  5031. delete params.valid_change_chart;
  5032. delete params.chartofaccounts;
  5033. delete params.entitytoclone;
  5034. url = url + "?" + jQuery.param(params);
  5035. }
  5036. location.href=url;
  5037. });
  5038. },
  5039. \''.$langs->trans('Cancel').'\': function() {
  5040. $(this).dialog( "close" );
  5041. }
  5042. }
  5043. });
  5044. });
  5045. var parseQueryString = function( queryString ) {
  5046. var params = {}, queries, temp, i, l;
  5047. // Split into key/value pairs
  5048. queries = queryString.split("&");
  5049. // Convert the array of strings into an object
  5050. for ( i = 0, l = queries.length; i < l; i++ ) {
  5051. temp = queries[i].split("=");
  5052. temp[1] = temp[1].replace(/#builddoc/gi, "");
  5053. params[temp[0]] = temp[1];
  5054. }
  5055. return params;
  5056. };
  5057. ';
  5058. if (GETPOST('switchentityautoopen','int')) {
  5059. $out.='$(".switchentity").click();'."\n";
  5060. }
  5061. $out.= '
  5062. });
  5063. </script>';
  5064. $out.= '<div id="dialog-switchentity" class="hideobject" title="'.$langs->trans('SwitchToAnotherEntity').'">'."\n";
  5065. $out.= '<br>'.$langs->trans('SelectAnEntity');
  5066. $out.= $selectEntities;
  5067. $out.= '</div>'."\n";
  5068. } else {
  5069. }
  5070. }
  5071. }
  5072. if (($level = checkMultiCompanyVersion()) === -2) {
  5073. $msg = get_htmloutput_mesg(img_warning('default') . ' ' . $langs->trans("MultiCompanyUpgradeIsNeeded"), '', 'mc-upgrade-alert', 1);
  5074. $out.= '
  5075. <script type="text/javascript">
  5076. $(document).ready(function() {
  5077. $( "#id-right .fiche" ).before( \'' . $msg . '\' );
  5078. });
  5079. </script>';
  5080. }
  5081. $this->resprints = $out;
  5082. }
  5083. /**
  5084. *
  5085. * @return string
  5086. */
  5087. private function getDropdownMenu()
  5088. {
  5089. global $conf, $user, $langs;
  5090. $this->getInfo($conf->entity);
  5091. if ($cache = getCache('mc_country_' . $this->country_id)) {
  5092. $country = $cache;
  5093. } else {
  5094. $country = getCountry($this->country_id);
  5095. setCache('mc_country_' . $this->country_id, $country);
  5096. }
  5097. $imgCountry=picto_from_langcode($this->country_code, 'class="multicompany-flag-country"');
  5098. $imgLang=picto_from_langcode($this->language_code, 'class="multicompany-flag-language"');
  5099. $dropdownBody = '';
  5100. $dropdownBody.= '<span id="topmenumcmoreinfo-btn"><i class="fa fa-caret-right"></i> '.$langs->trans("ShowMoreInfos").'</span>';
  5101. $dropdownBody.= '<div id="topmenumcmoreinfo" >';
  5102. $dropdownBody.= '<br><u>'.$langs->trans("Entity").'</u>'."\n";
  5103. $dropdownBody.= '<br><b>'.$langs->trans("Label").'</b>: '.$this->label."\n";
  5104. $dropdownBody.= '<br><b>'.$langs->trans("Country").'</b>: '. ($imgCountry?$imgCountry.' ':'') . $country."\n";
  5105. $dropdownBody.= '<br><b>'.$langs->trans("Currency").'</b>: '. currency_name($this->currency_code) . ' (' . $langs->getCurrencySymbol($this->currency_code) . ')'."\n";
  5106. $dropdownBody.= '<br><b>'.$langs->trans("Language").'</b>: '. ($imgLang?$imgLang.' ':'') . ($this->language_code=='auto'?$langs->trans("AutoDetectLang"):$langs->trans("Language_".$this->language_code));
  5107. if (!empty($this->description)) {
  5108. $dropdownBody.= '<br><b>'.$langs->trans("Description").'</b>: '.$this->description."\n";
  5109. }
  5110. $dropdownBody.= '</div>';
  5111. $selected = $conf->entity;
  5112. $exclude = false;
  5113. if (getDolGlobalInt('MULTICOMPANY_HIDE_CURRENT_ENTITY_IN_COMBOBOX')) {
  5114. $selected = '';
  5115. $exclude = array($conf->entity);
  5116. }
  5117. $selectEntities = $this->select_entities($selected, 'changeentity', '', false, $exclude, false, false, '', 'minwidth200imp', true, true);
  5118. $entitySwitchLink ='<div id="switchentity-menu" class="button-top-menu-dropdown'.(empty($selectEntities) ? ' button-not-allowed' : '').'"><i class="fa fa-random"></i> '.$langs->trans("SwitchEntity").'</div>';
  5119. $entityConfigLink ='<a class="button-top-menu-dropdown" href="'.dol_buildpath('multicompany/admin/multicompany.php', 1).'?action=edit&id='.$conf->entity.'"><i class="fa fa-cogs"></i> '.$langs->trans("Setup").'</a>';
  5120. $out = '<div class="inline-block nowrap">';
  5121. $out.= '<div class="inline-block login_block_elem login_block_elem_name float-left" style="padding: 0px;">';
  5122. $out.= '<div id="topmenu-mc-dropdown" class="atoplogin mcdropdown mc-menu">';
  5123. $out.= '<span class="fa fa-globe atoplogin mc-dropdown-toggle" data-toggle="mcdropdown" id="mc-dropdown-icon" title="'.$this->label.'">';
  5124. if (empty($conf->global->MULTICOMPANY_NO_TOP_MENU_ENTITY_LABEL)) {
  5125. $out .= '<span class="topmenu-mc-label">' . $this->label . '</span>';
  5126. }
  5127. $out.= '<span class="fa fa-chevron-down padding-left5" id="mc-dropdown-icon-down"></span>';
  5128. $out.= '<span class="fa fa-chevron-up padding-left5 hidden" id="mc-dropdown-icon-up"></span>';
  5129. $out.= '</span>';
  5130. $out.= '<div class="mc-dropdown-menu">';
  5131. $out.= '<div class="mc-header">';
  5132. $out.= '<div class="fa fa-globe dropdown-mc-image"></div>';
  5133. if (getDolGlobalInt('MULTICOMPANY_NO_TOP_MENU_ENTITY_LABEL')) {
  5134. $out .= '<br><span class="topmenu-mc-header-label">'.$this->label.'</span>';
  5135. }
  5136. if (!empty($selectEntities)) {
  5137. $out.= '<br><br>'.$langs->trans('SelectAnEntity');
  5138. $out.= $selectEntities;
  5139. } else {
  5140. $out.= '<br><br>'.$langs->trans('NoOtherEntityAvailable');
  5141. }
  5142. $out.= '</div>';
  5143. $out.= '<div class="mc-body">'.$dropdownBody.'</div>';
  5144. $out.= '<div class="mc-footer">';
  5145. $out.= '<div class="pull-left">';
  5146. if (!empty($user->admin) && empty($user->entity)) {
  5147. $out.= $entityConfigLink;
  5148. }
  5149. $out.= '</div>';
  5150. $out.= '<div class="pull-right">';
  5151. $out.= $entitySwitchLink;
  5152. $out.= '</div>';
  5153. $out.= '<div style="clear:both;"></div>';
  5154. $out.= '</div>';
  5155. $out.= '</div>';
  5156. $out.= '</div>';
  5157. $out.= '</div></div>';
  5158. $out.= '
  5159. <script type="text/javascript">
  5160. $(document).ready(function() {
  5161. $(document).on("click", function(event) {
  5162. if (!$(event.target).closest("#topmenu-mc-dropdown").length) {
  5163. // Hide the menus.
  5164. $("#topmenu-mc-dropdown").removeClass("open");
  5165. $("#mc-dropdown-icon-down").show();
  5166. $("#mc-dropdown-icon-up").hide();
  5167. }
  5168. });
  5169. $("#tmenu_tooltip").css("padding-right",parseFloat($("#tmenu_tooltip").css("padding-right")) + $("#topmenu-mc-dropdown").width());
  5170. $("#topmenu-mc-dropdown .mc-dropdown-toggle").on("click", function(event) {
  5171. $("#topmenu-mc-dropdown").toggleClass("open");
  5172. $("#mc-dropdown-icon-down").toggle();
  5173. $("#mc-dropdown-icon-up").toggle();
  5174. });
  5175. $("#topmenumcmoreinfo-btn").on("click", function() {
  5176. $("#topmenumcmoreinfo").slideToggle();
  5177. });
  5178. $("#switchentity-menu").on("click",function() {
  5179. $.post("'.dol_buildpath('/multicompany/core/ajax/functions.php', 1).'", {
  5180. action: \'switchEntity\',
  5181. id: $("#changeentity").val(),
  5182. token: "'.currentToken().'"
  5183. },
  5184. function(content) {
  5185. var url = window.location.pathname;
  5186. var queryString = window.location.href.split("?")[1];
  5187. if (queryString) {
  5188. var params = parseQueryString(queryString);
  5189. delete params.action;
  5190. delete params.switchentityautoopen;
  5191. delete params.valid_change_chart;
  5192. delete params.chartofaccounts;
  5193. delete params.entitytoclone;
  5194. url = url + "?" + jQuery.param(params);
  5195. }
  5196. location.href=url;
  5197. });
  5198. });
  5199. var parseQueryString = function( queryString ) {
  5200. var params = {}, queries, temp, i, l;
  5201. // Split into key/value pairs
  5202. queries = queryString.split("&");
  5203. // Convert the array of strings into an object
  5204. for ( i = 0, l = queries.length; i < l; i++ ) {
  5205. temp = queries[i].split("=");
  5206. temp[1] = temp[1].replace(/#builddoc/gi, "");
  5207. params[temp[0]] = temp[1];
  5208. }
  5209. return params;
  5210. };
  5211. ';
  5212. if (GETPOST('switchentityautoopen','int')) {
  5213. $out.='$("#switchentity").click();'."\n";
  5214. }
  5215. $out.= '
  5216. });
  5217. </script>';
  5218. return $out;
  5219. }
  5220. /**
  5221. * Set parameters with referent entity
  5222. *
  5223. * @param Conf $conf
  5224. * @param string $element
  5225. * @deprecated
  5226. */
  5227. public function setConstant(&$conf, $element)
  5228. {
  5229. if (!empty($this->config)) {
  5230. $constants=array();
  5231. if ($element == 'thirdparty') {
  5232. $constants = array(
  5233. 'SOCIETE_CODECLIENT_ADDON',
  5234. 'COMPANY_ELEPHANT_MASK_CUSTOMER',
  5235. 'COMPANY_ELEPHANT_MASK_SUPPLIER',
  5236. 'SOCIETE_IDPROF1_UNIQUE',
  5237. 'SOCIETE_IDPROF2_UNIQUE',
  5238. 'SOCIETE_IDPROF3_UNIQUE',
  5239. 'SOCIETE_IDPROF4_UNIQUE'
  5240. );
  5241. }
  5242. if (!empty($constants)) {
  5243. foreach($constants as $name) {
  5244. if (!empty($this->config[$name])) {
  5245. $conf->global->$name = $this->config[$name];
  5246. }
  5247. }
  5248. }
  5249. }
  5250. }
  5251. /**
  5252. *
  5253. * @param int $groupid
  5254. * @param int $template
  5255. * @param array $entities
  5256. * @return number
  5257. */
  5258. public function duplicateUserGroupRights($groupid, $template, $entities)
  5259. {
  5260. require_once DOL_DOCUMENT_ROOT.'/user/class/usergroup.class.php';
  5261. dol_syslog(get_class($this)."::duplicateUserGroupRights groupid=".$groupid." template=".$template." entities=".implode(",", $entities), LOG_DEBUG);
  5262. $error=0;
  5263. $groupstatic = new UserGroup($this->db);
  5264. $ret = $groupstatic->fetch($groupid, '', false);
  5265. if ($ret > 0) {
  5266. $this->getInstanceDao();
  5267. $permsgroupbyentity = $this->dao->getGroupRightsByEntity($groupid, $template);
  5268. if (!empty($entities)) {
  5269. foreach($entities as $entity) {
  5270. if ($error > 0) {
  5271. break;
  5272. }
  5273. $ret = $groupstatic->delrights('', 'allmodules', '', $entity);
  5274. if ($ret < 0) {
  5275. $error++;
  5276. break;
  5277. }
  5278. foreach($permsgroupbyentity as $rid) {
  5279. $ret = $groupstatic->addrights($rid, '', '', $entity);
  5280. if ($ret < 0) {
  5281. $error++;
  5282. break;
  5283. }
  5284. }
  5285. }
  5286. if (empty($error)) {
  5287. return 1;
  5288. } else {
  5289. return -1;
  5290. }
  5291. } else {
  5292. return -2;
  5293. }
  5294. } else {
  5295. return -3;
  5296. }
  5297. }
  5298. }