actions_multicompany.class.php 216 KB

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