12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040 |
- <?php
- // Copyright (C) 2010-2016 Combodo SARL
- //
- // This file is part of iTop.
- //
- // iTop is free software; you can redistribute it and/or modify
- // it under the terms of the GNU Affero General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // iTop is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU Affero General Public License for more details.
- //
- // You should have received a copy of the GNU Affero General Public License
- // along with iTop. If not, see <http://www.gnu.org/licenses/>
- /**
- * Typology for the attributes
- *
- * @copyright Copyright (C) 2010-2016 Combodo SARL
- * @license http://opensource.org/licenses/AGPL-3.0
- */
- require_once('MyHelpers.class.inc.php');
- require_once('ormdocument.class.inc.php');
- require_once('ormstopwatch.class.inc.php');
- require_once('ormpassword.class.inc.php');
- require_once('ormcaselog.class.inc.php');
- require_once('htmlsanitizer.class.inc.php');
- require_once(APPROOT.'sources/autoload.php');
- require_once('customfieldshandler.class.inc.php');
- require_once('ormcustomfieldsvalue.class.inc.php');
- require_once('datetimeformat.class.inc.php');
- // This should be changed to a use when we go full-namespace
- require_once(APPROOT . 'sources/form/validator/validator.class.inc.php');
- require_once(APPROOT . 'sources/form/validator/notemptyextkeyvalidator.class.inc.php');
- /**
- * MissingColumnException - sent if an attribute is being created but the column is missing in the row
- *
- * @package iTopORM
- */
- class MissingColumnException extends Exception
- {}
- /**
- * add some description here...
- *
- * @package iTopORM
- */
- define('EXTKEY_RELATIVE', 1);
- /**
- * add some description here...
- *
- * @package iTopORM
- */
- define('EXTKEY_ABSOLUTE', 2);
- /**
- * Propagation of the deletion through an external key - ask the user to delete the referencing object
- *
- * @package iTopORM
- */
- define('DEL_MANUAL', 1);
- /**
- * Propagation of the deletion through an external key - ask the user to delete the referencing object
- *
- * @package iTopORM
- */
- define('DEL_AUTO', 2);
- /**
- * Fully silent delete... not yet implemented
- */
- define('DEL_SILENT', 2);
- /**
- * For HierarchicalKeys only: move all the children up one level automatically
- */
- define('DEL_MOVEUP', 3);
- /**
- * For Link sets: tracking_level
- *
- * @package iTopORM
- */
- define('ATTRIBUTE_TRACKING_NONE', 0); // Do not track changes of the attribute
- define('ATTRIBUTE_TRACKING_ALL', 3); // Do track all changes of the attribute
- define('LINKSET_TRACKING_NONE', 0); // Do not track changes in the link set
- define('LINKSET_TRACKING_LIST', 1); // Do track added/removed items
- define('LINKSET_TRACKING_DETAILS', 2); // Do track modified items
- define('LINKSET_TRACKING_ALL', 3); // Do track added/removed/modified items
- define('LINKSET_EDITMODE_NONE', 0); // The linkset cannot be edited at all from inside this object
- define('LINKSET_EDITMODE_ADDONLY', 1); // The only possible action is to open a new window to create a new object
- define('LINKSET_EDITMODE_ACTIONS', 2); // Show the usual 'Actions' popup menu
- define('LINKSET_EDITMODE_INPLACE', 3); // The "linked" objects can be created/modified/deleted in place
- define('LINKSET_EDITMODE_ADDREMOVE', 4); // The "linked" objects can be added/removed in place
- /**
- * Attribute definition API, implemented in and many flavours (Int, String, Enum, etc.)
- *
- * @package iTopORM
- */
- abstract class AttributeDefinition
- {
- public function GetType()
- {
- return Dict::S('Core:'.get_class($this));
- }
- public function GetTypeDesc()
- {
- return Dict::S('Core:'.get_class($this).'+');
- }
- abstract public function GetEditClass();
- protected $m_sCode;
- private $m_aParams = array();
- protected $m_sHostClass = '!undefined!';
- public function Get($sParamName) {return $this->m_aParams[$sParamName];}
- protected function IsParam($sParamName) {return (array_key_exists($sParamName, $this->m_aParams));}
- protected function GetOptional($sParamName, $default)
- {
- if (array_key_exists($sParamName, $this->m_aParams))
- {
- return $this->m_aParams[$sParamName];
- }
- else
- {
- return $default;
- }
- }
-
- public function __construct($sCode, $aParams)
- {
- $this->m_sCode = $sCode;
- $this->m_aParams = $aParams;
- $this->ConsistencyCheck();
- }
- public function GetParams()
- {
- return $this->m_aParams;
- }
- public function HasParam($sParam)
- {
- return array_key_exists($sParam, $this->m_aParams);
- }
- public function SetHostClass($sHostClass)
- {
- $this->m_sHostClass = $sHostClass;
- }
- public function GetHostClass()
- {
- return $this->m_sHostClass;
- }
- public function ListSubItems()
- {
- $aSubItems = array();
- foreach(MetaModel::ListAttributeDefs($this->m_sHostClass) as $sAttCode => $oAttDef)
- {
- if ($oAttDef instanceof AttributeSubItem)
- {
- if ($oAttDef->Get('target_attcode') == $this->m_sCode)
- {
- $aSubItems[$sAttCode] = $oAttDef;
- }
- }
- }
- return $aSubItems;
- }
- // Note: I could factorize this code with the parameter management made for the AttributeDef class
- // to be overloaded
- static public function ListExpectedParams()
- {
- return array();
- }
- private function ConsistencyCheck()
- {
- // Check that any mandatory param has been specified
- //
- $aExpectedParams = $this->ListExpectedParams();
- foreach($aExpectedParams as $sParamName)
- {
- if (!array_key_exists($sParamName, $this->m_aParams))
- {
- $aBacktrace = debug_backtrace();
- $sTargetClass = $aBacktrace[2]["class"];
- $sCodeInfo = $aBacktrace[1]["file"]." - ".$aBacktrace[1]["line"];
- throw new Exception("ERROR missing parameter '$sParamName' in ".get_class($this)." declaration for class $sTargetClass ($sCodeInfo)");
- }
- }
- }
- /**
- * Check the validity of the given value
- * @param DBObject $oHostObject
- * @param string An error if any, null otherwise
- */
- public function CheckValue(DBObject $oHostObject, $value)
- {
- // todo: factorize here the cases implemented into DBObject
- return true;
- }
- // table, key field, name field
- public function ListDBJoins()
- {
- return "";
- // e.g: return array("Site", "infrid", "name");
- }
- public function GetFinalAttDef()
- {
- return $this;
- }
- public function IsDirectField() {return false;}
- public function IsScalar() {return false;}
- public function IsLinkSet() {return false;}
- public function IsExternalKey($iType = EXTKEY_RELATIVE) {return false;}
- public function IsHierarchicalKey() {return false;}
- public function IsExternalField() {return false;}
- public function IsWritable() {return false;}
- public function LoadInObject() {return true;}
- public function LoadFromDB() {return true;}
- public function AlwaysLoadInTables() {return $this->GetOptional('always_load_in_tables', false);}
- public function GetValue($oHostObject, $bOriginal = false){return null;} // must return the value if LoadInObject returns false
- public function IsNullAllowed() {return true;}
- public function GetCode() {return $this->m_sCode;}
- public function GetMirrorLinkAttribute() {return null;}
- /**
- * Helper to browse the hierarchy of classes, searching for a label
- */
- protected function SearchLabel($sDictEntrySuffix, $sDefault, $bUserLanguageOnly)
- {
- $sLabel = Dict::S('Class:'.$this->m_sHostClass.$sDictEntrySuffix, '', $bUserLanguageOnly);
- if (strlen($sLabel) == 0)
- {
- // Nothing found: go higher in the hierarchy (if possible)
- //
- $sLabel = $sDefault;
- $sParentClass = MetaModel::GetParentClass($this->m_sHostClass);
- if ($sParentClass)
- {
- if (MetaModel::IsValidAttCode($sParentClass, $this->m_sCode))
- {
- $oAttDef = MetaModel::GetAttributeDef($sParentClass, $this->m_sCode);
- $sLabel = $oAttDef->SearchLabel($sDictEntrySuffix, $sDefault, $bUserLanguageOnly);
- }
- }
- }
- return $sLabel;
- }
- public function GetLabel($sDefault = null)
- {
- $sLabel = $this->SearchLabel('/Attribute:'.$this->m_sCode, null, true /*user lang*/);
- if (is_null($sLabel))
- {
- // If no default value is specified, let's define the most relevant one for developping purposes
- if (is_null($sDefault))
- {
- $sDefault = str_replace('_', ' ', $this->m_sCode);
- }
- // Browse the hierarchy again, accepting default (english) translations
- $sLabel = $this->SearchLabel('/Attribute:'.$this->m_sCode, $sDefault, false);
- }
- return $sLabel;
- }
- /**
- * Get the label corresponding to the given value (in plain text)
- * To be overloaded for localized enums
- */
- public function GetValueLabel($sValue)
- {
- return $sValue;
- }
- /**
- * Get the value from a given string (plain text, CSV import)
- * Return null if no match could be found
- */
- public function MakeValueFromString($sProposedValue, $bLocalizedValue = false, $sSepItem = null, $sSepAttribute = null, $sSepValue = null, $sAttributeQualifier = null)
- {
- return $this->MakeRealValue($sProposedValue, null);
- }
- /**
- * Parses a search string coming from user input
- * @param string $sSearchString
- * @return string
- */
- public function ParseSearchString($sSearchString)
- {
- return $sSearchString;
- }
- public function GetLabel_Obsolete()
- {
- // Written for compatibility with a data model written prior to version 0.9.1
- if (array_key_exists('label', $this->m_aParams))
- {
- return $this->m_aParams['label'];
- }
- else
- {
- return $this->GetLabel();
- }
- }
- public function GetDescription($sDefault = null)
- {
- $sLabel = $this->SearchLabel('/Attribute:'.$this->m_sCode.'+', null, true /*user lang*/);
- if (is_null($sLabel))
- {
- // If no default value is specified, let's define the most relevant one for developping purposes
- if (is_null($sDefault))
- {
- $sDefault = '';
- }
- // Browse the hierarchy again, accepting default (english) translations
- $sLabel = $this->SearchLabel('/Attribute:'.$this->m_sCode.'+', $sDefault, false);
- }
- return $sLabel;
- }
- public function GetHelpOnEdition($sDefault = null)
- {
- $sLabel = $this->SearchLabel('/Attribute:'.$this->m_sCode.'?', null, true /*user lang*/);
- if (is_null($sLabel))
- {
- // If no default value is specified, let's define the most relevant one for developping purposes
- if (is_null($sDefault))
- {
- $sDefault = '';
- }
- // Browse the hierarchy again, accepting default (english) translations
- $sLabel = $this->SearchLabel('/Attribute:'.$this->m_sCode.'?', $sDefault, false);
- }
- return $sLabel;
- }
- public function GetHelpOnSmartSearch()
- {
- $aParents = array_merge(array(get_class($this) => get_class($this)), class_parents($this));
- foreach ($aParents as $sClass)
- {
- $sHelp = Dict::S("Core:$sClass?SmartSearch", '-missing-');
- if ($sHelp != '-missing-')
- {
- return $sHelp;
- }
- }
- return '';
- }
- public function GetDescription_Obsolete()
- {
- // Written for compatibility with a data model written prior to version 0.9.1
- if (array_key_exists('description', $this->m_aParams))
- {
- return $this->m_aParams['description'];
- }
- else
- {
- return $this->GetDescription();
- }
- }
- public function GetTrackingLevel()
- {
- return $this->GetOptional('tracking_level', ATTRIBUTE_TRACKING_ALL);
- }
- public function GetValuesDef() {return null;}
- public function GetPrerequisiteAttributes($sClass = null) {return array();}
- public function GetNullValue() {return null;}
- public function IsNull($proposedValue) {return is_null($proposedValue);}
- public function MakeRealValue($proposedValue, $oHostObj) {return $proposedValue;} // force an allowed value (type conversion and possibly forces a value as mySQL would do upon writing!)
- public function Equals($val1, $val2) {return ($val1 == $val2);}
- public function GetSQLExpressions($sPrefix = '') {return array();} // returns suffix/expression pairs (1 in most of the cases), for READING (Select)
- public function FromSQLToValue($aCols, $sPrefix = '') {return null;} // returns a value out of suffix/value pairs, for SELECT result interpretation
- public function GetSQLColumns($bFullSpec = false) {return array();} // returns column/spec pairs (1 in most of the cases), for STRUCTURING (DB creation)
- public function GetSQLValues($value) {return array();} // returns column/value pairs (1 in most of the cases), for WRITING (Insert, Update)
- public function RequiresIndex() {return false;}
- public function GetOrderBySQLExpressions($sClassAlias)
- {
- // Note: This is the responsibility of this function to place backticks around column aliases
- return array('`'.$sClassAlias.$this->GetCode().'`');
- }
-
- public function GetOrderByHint()
- {
- return '';
- }
- // Import - differs slightly from SQL input, but identical in most cases
- //
- public function GetImportColumns() {return $this->GetSQLColumns();}
- public function FromImportToValue($aCols, $sPrefix = '')
- {
- $aValues = array();
- foreach ($this->GetSQLExpressions($sPrefix) as $sAlias => $sExpr)
- {
- // This is working, based on the assumption that importable fields
- // are not computed fields => the expression is the name of a column
- $aValues[$sPrefix.$sAlias] = $aCols[$sExpr];
- }
- return $this->FromSQLToValue($aValues, $sPrefix);
- }
- public function GetValidationPattern()
- {
- return '';
- }
-
- public function CheckFormat($value)
- {
- return true;
- }
-
- public function GetMaxSize()
- {
- return null;
- }
-
- public function MakeValue()
- {
- $sComputeFunc = $this->Get("compute_func");
- if (empty($sComputeFunc)) return null;
- return call_user_func($sComputeFunc);
- }
-
- abstract public function GetDefaultValue(DBObject $oHostObject = null);
- //
- // To be overloaded in subclasses
- //
-
- abstract public function GetBasicFilterOperators(); // returns an array of "opCode"=>"description"
- abstract public function GetBasicFilterLooseOperator(); // returns an "opCode"
- //abstract protected GetBasicFilterHTMLInput();
- abstract public function GetBasicFilterSQLExpr($sOpCode, $value);
- public function GetFilterDefinitions()
- {
- return array();
- }
- public function GetEditValue($sValue, $oHostObj = null)
- {
- return (string)$sValue;
- }
-
- /**
- * For fields containing a potential markup, return the value without this markup
- * @return string
- */
- public function GetAsPlainText($sValue, $oHostObj = null)
- {
- return (string) $this->GetEditValue($sValue, $oHostObj);
- }
- /**
- * Helper to get a value that will be JSON encoded
- * The operation is the opposite to FromJSONToValue
- */
- public function GetForJSON($value)
- {
- // In most of the cases, that will be the expected behavior...
- return $this->GetEditValue($value);
- }
- /**
- * Helper to form a value, given JSON decoded data
- * The operation is the opposite to GetForJSON
- */
- public function FromJSONToValue($json)
- {
- // Passthrough in most of the cases
- return $json;
- }
- /**
- * Override to display the value in the GUI
- */
- public function GetAsHTML($sValue, $oHostObject = null, $bLocalize = true)
- {
- return Str::pure2html((string)$sValue);
- }
- /**
- * Override to export the value in XML
- */
- public function GetAsXML($sValue, $oHostObject = null, $bLocalize = true)
- {
- return Str::pure2xml((string)$sValue);
- }
- /**
- * Override to escape the value when read by DBObject::GetAsCSV()
- */
- public function GetAsCSV($sValue, $sSeparator = ',', $sTextQualifier = '"', $oHostObject = null, $bLocalize = true, $bConvertToPlainText = false)
- {
- return (string)$sValue;
- }
- /**
- * Override to differentiate a value displayed in the UI or in the history
- */
- public function GetAsHTMLForHistory($sValue, $oHostObject = null, $bLocalize = true)
- {
- return $this->GetAsHTML($sValue, $oHostObject, $bLocalize);
- }
- static public function GetFormFieldClass()
- {
- return '\\Combodo\\iTop\\Form\\Field\\StringField';
- }
- /**
- * Override to specify Field class
- *
- * When called first, $oFormField is null and will be created (eg. Make). Then when the ::parent is called and the $oFormField is passed, MakeFormField behave more like a Prepare.
- */
- public function MakeFormField(DBObject $oObject, $oFormField = null)
- {
- // This is a fallback in case the AttributeDefinition subclass has no overloading of this function.
- if ($oFormField === null)
- {
- $sFormFieldClass = static::GetFormFieldClass();
- $oFormField = new $sFormFieldClass($this->GetCode());
- //$oFormField->SetReadOnly(true);
- }
- $oFormField->SetLabel($this->GetLabel());
- // Attributes flags
- // - Retrieving flags for the current object
- if ($oObject->IsNew())
- {
- $iFlags = $oObject->GetInitialStateAttributeFlags($this->GetCode());
- }
- else
- {
- $iFlags = $oObject->GetAttributeFlags($this->GetCode());
- }
- // - Comparing flags
- if (!$this->IsNullAllowed() || (($iFlags & OPT_ATT_MANDATORY) === OPT_ATT_MANDATORY))
- {
- $oFormField->SetMandatory(true);
- }
- if ((!$oObject->IsNew() || !$oFormField->GetMandatory()) && (($iFlags & OPT_ATT_READONLY) === OPT_ATT_READONLY))
- {
- $oFormField->SetReadOnly(true);
- }
-
- // CurrentValue
- $oFormField->SetCurrentValue($oObject->Get($this->GetCode()));
- // Validation pattern
- if ($this->GetValidationPattern() !== '')
- {
- $oFormField->AddValidator(new \Combodo\iTop\Form\Validator\Validator($this->GetValidationPattern()));
- }
- return $oFormField;
- }
- /**
- * List the available verbs for 'GetForTemplate'
- */
- public function EnumTemplateVerbs()
- {
- return array(
- '' => 'Plain text (unlocalized) representation',
- 'html' => 'HTML representation',
- 'label' => 'Localized representation',
- 'text' => 'Plain text representation (without any markup)',
- );
- }
- /**
- * Get various representations of the value, for insertion into a template (e.g. in Notifications)
- * @param $value mixed The current value of the field
- * @param $sVerb string The verb specifying the representation of the value
- * @param $oHostObject DBObject The object
- * @param $bLocalize bool Whether or not to localize the value
- */
- public function GetForTemplate($value, $sVerb, $oHostObject = null, $bLocalize = true)
- {
- if ($this->IsScalar())
- {
- switch ($sVerb)
- {
- case '':
- return $value;
-
- case 'html':
- return $this->GetAsHtml($value, $oHostObject, $bLocalize);
-
- case 'label':
- return $this->GetEditValue($value);
-
- case 'text':
- return $this->GetAsPlainText($value);
- break;
-
- default:
- throw new Exception("Unknown verb '$sVerb' for attribute ".$this->GetCode().' in class '.get_class($oHostObj));
- }
- }
- return null;
- }
- public function GetAllowedValues($aArgs = array(), $sContains = '')
- {
- $oValSetDef = $this->GetValuesDef();
- if (!$oValSetDef) return null;
- return $oValSetDef->GetValues($aArgs, $sContains);
- }
- /**
- * Explain the change of the attribute (history)
- */
- public function DescribeChangeAsHTML($sOldValue, $sNewValue, $sLabel = null)
- {
- if (is_null($sLabel))
- {
- $sLabel = $this->GetLabel();
- }
- $sNewValueHtml = $this->GetAsHTMLForHistory($sNewValue);
- $sOldValueHtml = $this->GetAsHTMLForHistory($sOldValue);
- if($this->IsExternalKey())
- {
- $sTargetClass = $this->GetTargetClass();
- $sOldValueHtml = (int)$sOldValue ? MetaModel::GetHyperLink($sTargetClass, (int)$sOldValue) : null;
- $sNewValueHtml = (int)$sNewValue ? MetaModel::GetHyperLink($sTargetClass, (int)$sNewValue) : null;
- }
- if ( (($this->GetType() == 'String') || ($this->GetType() == 'Text')) &&
- (strlen($sNewValue) > strlen($sOldValue)) )
- {
- // Check if some text was not appended to the field
- if (substr($sNewValue,0, strlen($sOldValue)) == $sOldValue) // Text added at the end
- {
- $sDelta = $this->GetAsHTML(substr($sNewValue, strlen($sOldValue)));
- $sResult = Dict::Format('Change:Text_AppendedTo_AttName', $sDelta, $sLabel);
- }
- else if (substr($sNewValue, -strlen($sOldValue)) == $sOldValue) // Text added at the beginning
- {
- $sDelta = $this->GetAsHTML(substr($sNewValue, 0, strlen($sNewValue) - strlen($sOldValue)));
- $sResult = Dict::Format('Change:Text_AppendedTo_AttName', $sDelta, $sLabel);
- }
- else
- {
- if (strlen($sOldValue) == 0)
- {
- $sResult = Dict::Format('Change:AttName_SetTo', $sLabel, $sNewValueHtml);
- }
- else
- {
- if (is_null($sNewValue))
- {
- $sNewValueHtml = Dict::S('UI:UndefinedObject');
- }
- $sResult = Dict::Format('Change:AttName_SetTo_NewValue_PreviousValue_OldValue', $sLabel, $sNewValueHtml, $sOldValueHtml);
- }
- }
- }
- else
- {
- if (strlen($sOldValue) == 0)
- {
- $sResult = Dict::Format('Change:AttName_SetTo', $sLabel, $sNewValueHtml);
- }
- else
- {
- if (is_null($sNewValue))
- {
- $sNewValueHtml = Dict::S('UI:UndefinedObject');
- }
- $sResult = Dict::Format('Change:AttName_SetTo_NewValue_PreviousValue_OldValue', $sLabel, $sNewValueHtml, $sOldValueHtml);
- }
- }
- return $sResult;
- }
- /**
- * Parses a string to find some smart search patterns and build the corresponding search/OQL condition
- * Each derived class is reponsible for defining and processing their own smart patterns, the base class
- * does nothing special, and just calls the default (loose) operator
- * @param string $sSearchText The search string to analyze for smart patterns
- * @param FieldExpression The FieldExpression representing the atttribute code in this OQL query
- * @param Hash $aParams Values of the query parameters
- * @return Expression The search condition to be added (AND) to the current search
- */
- public function GetSmartConditionExpression($sSearchText, FieldExpression $oField, &$aParams)
- {
- $sParamName = $oField->GetParent().'_'.$oField->GetName();
- $oRightExpr = new VariableExpression($sParamName);
- $sOperator = $this->GetBasicFilterLooseOperator();
- switch ($sOperator)
- {
- case 'Contains':
- $aParams[$sParamName] = "%$sSearchText%";
- $sSQLOperator = 'LIKE';
- break;
-
- default:
- $sSQLOperator = $sOperator;
- $aParams[$sParamName] = $sSearchText;
- }
- $oNewCondition = new BinaryExpression($oField, $sSQLOperator, $oRightExpr);
- return $oNewCondition;
- }
-
- /**
- * Tells if an attribute is part of the unique fingerprint of the object (used for comparing two objects)
- * All attributes which value is not based on a value from the object itself (like ExternalFields or LinkedSet)
- * must be excluded from the object's signature
- * @return boolean
- */
- public function IsPartOfFingerprint()
- {
- return true;
- }
-
- /**
- * The part of the current attribute in the object's signature, for the supplied value
- * @param unknown $value The value of this attribute for the object
- * @return string The "signature" for this field/attribute
- */
- public function Fingerprint($value)
- {
- return (string)$value;
- }
- }
- /**
- * Set of objects directly linked to an object, and being part of its definition
- *
- * @package iTopORM
- */
- class AttributeLinkedSet extends AttributeDefinition
- {
- static public function ListExpectedParams()
- {
- return array_merge(parent::ListExpectedParams(), array("allowed_values", "depends_on", "linked_class", "ext_key_to_me", "count_min", "count_max"));
- }
- public function GetEditClass() {return "LinkedSet";}
- public function IsWritable() {return true;}
- public function IsLinkSet() {return true;}
- public function IsIndirect() {return false;}
- public function GetValuesDef() {return $this->Get("allowed_values");}
- public function GetPrerequisiteAttributes($sClass = null) {return $this->Get("depends_on");}
- public function GetDefaultValue(DBObject $oHostObject = null)
- {
- return DBObjectSet::FromScratch($this->Get('linked_class'));
- }
- public function GetTrackingLevel()
- {
- return $this->GetOptional('tracking_level', MetaModel::GetConfig()->Get('tracking_level_linked_set_default'));
- }
- public function GetEditMode()
- {
- return $this->GetOptional('edit_mode', LINKSET_EDITMODE_ACTIONS);
- }
-
- public function GetLinkedClass() {return $this->Get('linked_class');}
- public function GetExtKeyToMe() {return $this->Get('ext_key_to_me');}
- public function GetBasicFilterOperators() {return array();}
- public function GetBasicFilterLooseOperator() {return '';}
- public function GetBasicFilterSQLExpr($sOpCode, $value) {return '';}
- public function GetAsHTML($sValue, $oHostObject = null, $bLocalize = true)
- {
- if (is_object($sValue) && ($sValue instanceof DBObjectSet))
- {
- $sValue->Rewind();
- $aItems = array();
- while ($oObj = $sValue->Fetch())
- {
- // Show only relevant information (hide the external key to the current object)
- $aAttributes = array();
- foreach(MetaModel::ListAttributeDefs($this->GetLinkedClass()) as $sAttCode => $oAttDef)
- {
- if ($sAttCode == $this->GetExtKeyToMe()) continue;
- if ($oAttDef->IsExternalField()) continue;
- $sAttValue = $oObj->GetAsHTML($sAttCode);
- if (strlen($sAttValue) > 0)
- {
- $aAttributes[] = $sAttValue;
- }
- }
- $sAttributes = implode(', ', $aAttributes);
- $aItems[] = $sAttributes;
- }
- return implode('<br/>', $aItems);
- }
- return null;
- }
- public function GetAsXML($sValue, $oHostObject = null, $bLocalize = true)
- {
- if (is_object($sValue) && ($sValue instanceof DBObjectSet))
- {
- $sValue->Rewind();
- $sRes = "<Set>\n";
- while ($oObj = $sValue->Fetch())
- {
- $sObjClass = get_class($oObj);
- $sRes .= "<$sObjClass id=\"".$oObj->GetKey()."\">\n";
- // Show only relevant information (hide the external key to the current object)
- $aAttributes = array();
- foreach(MetaModel::ListAttributeDefs($sObjClass) as $sAttCode => $oAttDef)
- {
- if ($sAttCode == 'finalclass')
- {
- if ($sObjClass == $this->GetLinkedClass())
- {
- // Simplify the output if the exact class could be determined implicitely
- continue;
- }
- }
- if ($sAttCode == $this->GetExtKeyToMe()) continue;
- if ($oAttDef->IsExternalField() && ($oAttDef->GetKeyAttCode() == $this->GetExtKeyToMe())) continue;
- if (($oAttDef instanceof AttributeFriendlyName) && ($oAttDef->GetKeyAttCode() == $this->GetExtKeyToMe())) continue;
- if (($oAttDef instanceof AttributeFriendlyName) && ($oAttDef->GetKeyAttCode() == 'id')) continue;
- if (!$oAttDef->IsScalar()) continue;
- $sAttValue = $oObj->GetAsXML($sAttCode, $bLocalize);
- $sRes .= "<$sAttCode>$sAttValue</$sAttCode>\n";
- }
- $sRes .= "</$sObjClass>\n";
- }
- $sRes .= "</Set>\n";
- }
- else
- {
- $sRes = '';
- }
- return $sRes;
- }
- public function GetAsCSV($sValue, $sSeparator = ',', $sTextQualifier = '"', $oHostObject = null, $bLocalize = true, $bConvertToPlainText = false)
- {
- $sSepItem = MetaModel::GetConfig()->Get('link_set_item_separator');
- $sSepAttribute = MetaModel::GetConfig()->Get('link_set_attribute_separator');
- $sSepValue = MetaModel::GetConfig()->Get('link_set_value_separator');
- $sAttributeQualifier = MetaModel::GetConfig()->Get('link_set_attribute_qualifier');
- if (is_object($sValue) && ($sValue instanceof DBObjectSet))
- {
- $sValue->Rewind();
- $aItems = array();
- while ($oObj = $sValue->Fetch())
- {
- $sObjClass = get_class($oObj);
- // Show only relevant information (hide the external key to the current object)
- $aAttributes = array();
- foreach(MetaModel::ListAttributeDefs($sObjClass) as $sAttCode => $oAttDef)
- {
- if ($sAttCode == 'finalclass')
- {
- if ($sObjClass == $this->GetLinkedClass())
- {
- // Simplify the output if the exact class could be determined implicitely
- continue;
- }
- }
- if ($sAttCode == $this->GetExtKeyToMe()) continue;
- if ($oAttDef->IsExternalField()) continue;
- if (!$oAttDef->IsDirectField()) continue;
- if (!$oAttDef->IsScalar()) continue;
- $sAttValue = $oObj->GetAsCSV($sAttCode, $sSepValue, '', $bLocalize);
- if (strlen($sAttValue) > 0)
- {
- $sAttributeData = str_replace($sAttributeQualifier, $sAttributeQualifier.$sAttributeQualifier, $sAttCode.$sSepValue.$sAttValue);
- $aAttributes[] = $sAttributeQualifier.$sAttributeData.$sAttributeQualifier;
- }
- }
- $sAttributes = implode($sSepAttribute, $aAttributes);
- $aItems[] = $sAttributes;
- }
- $sRes = implode($sSepItem, $aItems);
- }
- else
- {
- $sRes = '';
- }
- $sRes = str_replace($sTextQualifier, $sTextQualifier.$sTextQualifier, $sRes);
- $sRes = $sTextQualifier.$sRes.$sTextQualifier;
- return $sRes;
- }
- /**
- * List the available verbs for 'GetForTemplate'
- */
- public function EnumTemplateVerbs()
- {
- return array(
- '' => 'Plain text (unlocalized) representation',
- 'html' => 'HTML representation (unordered list)',
- );
- }
- /**
- * Get various representations of the value, for insertion into a template (e.g. in Notifications)
- * @param $value mixed The current value of the field
- * @param $sVerb string The verb specifying the representation of the value
- * @param $oHostObject DBObject The object
- * @param $bLocalize bool Whether or not to localize the value
- */
- public function GetForTemplate($value, $sVerb, $oHostObject = null, $bLocalize = true)
- {
- $sRemoteName = $this->IsIndirect() ? $this->GetExtKeyToRemote().'_friendlyname' : 'friendlyname';
-
- $oLinkSet = clone $value; // Workaround/Safety net for Trac #887
- $iLimit = MetaModel::GetConfig()->Get('max_linkset_output');
- if ($iLimit > 0)
- {
- $oLinkSet->SetLimit($iLimit);
- }
- $aNames = $oLinkSet->GetColumnAsArray($sRemoteName);
- if ($iLimit > 0)
- {
- $iTotal = $oLinkSet->Count();
- if ($iTotal > count($aNames))
- {
- $aNames[] = '... '.Dict::Format('UI:TruncatedResults', count($aNames), $iTotal);
- }
- }
-
- switch($sVerb)
- {
- case '':
- return implode("\n", $aNames);
-
- case 'html':
- return '<ul><li>'.implode("</li><li>", $aNames).'</li></ul>';
-
- default:
- throw new Exception("Unknown verb '$sVerb' for attribute ".$this->GetCode().' in class '.get_class($oHostObj));
- }
- }
- public function DuplicatesAllowed() {return false;} // No duplicates for 1:n links, never
- public function GetImportColumns()
- {
- $aColumns = array();
- $aColumns[$this->GetCode()] = 'TEXT';
- return $aColumns;
- }
- public function MakeValueFromString($sProposedValue, $bLocalizedValue = false, $sSepItem = null, $sSepAttribute = null, $sSepValue = null, $sAttributeQualifier = null)
- {
- if (is_null($sSepItem) || empty($sSepItem))
- {
- $sSepItem = MetaModel::GetConfig()->Get('link_set_item_separator');
- }
- if (is_null($sSepAttribute) || empty($sSepAttribute))
- {
- $sSepAttribute = MetaModel::GetConfig()->Get('link_set_attribute_separator');
- }
- if (is_null($sSepValue) || empty($sSepValue))
- {
- $sSepValue = MetaModel::GetConfig()->Get('link_set_value_separator');
- }
- if (is_null($sAttributeQualifier) || empty($sAttributeQualifier))
- {
- $sAttributeQualifier = MetaModel::GetConfig()->Get('link_set_attribute_qualifier');
- }
- $sTargetClass = $this->Get('linked_class');
- $sInput = str_replace($sSepItem, "\n", $sProposedValue);
- $oCSVParser = new CSVParser($sInput, $sSepAttribute, $sAttributeQualifier);
- $aInput = $oCSVParser->ToArray(0 /* do not skip lines */);
- $aLinks = array();
- foreach($aInput as $aRow)
- {
- // 1st - get the values, split the extkey->searchkey specs, and eventually get the finalclass value
- $aExtKeys = array();
- $aValues = array();
- foreach($aRow as $sCell)
- {
- $iSepPos = strpos($sCell, $sSepValue);
- if ($iSepPos === false)
- {
- // Houston...
- throw new CoreException('Wrong format for link attribute specification', array('value' => $sCell));
- }
- $sAttCode = trim(substr($sCell, 0, $iSepPos));
- $sValue = substr($sCell, $iSepPos + strlen($sSepValue));
- if (preg_match('/^(.+)->(.+)$/', $sAttCode, $aMatches))
- {
- $sKeyAttCode = $aMatches[1];
- $sRemoteAttCode = $aMatches[2];
- $aExtKeys[$sKeyAttCode][$sRemoteAttCode] = $sValue;
- if (!MetaModel::IsValidAttCode($sTargetClass, $sKeyAttCode))
- {
- throw new CoreException('Wrong attribute code for link attribute specification', array('class' => $sTargetClass, 'attcode' => $sKeyAttCode));
- }
- $oKeyAttDef = MetaModel::GetAttributeDef($sTargetClass, $sKeyAttCode);
- $sRemoteClass = $oKeyAttDef->GetTargetClass();
- if (!MetaModel::IsValidAttCode($sRemoteClass, $sRemoteAttCode))
- {
- throw new CoreException('Wrong attribute code for link attribute specification', array('class' => $sRemoteClass, 'attcode' => $sRemoteAttCode));
- }
- }
- else
- {
- if(!MetaModel::IsValidAttCode($sTargetClass, $sAttCode))
- {
- throw new CoreException('Wrong attribute code for link attribute specification', array('class' => $sTargetClass, 'attcode' => $sAttCode));
- }
- $oAttDef = MetaModel::GetAttributeDef($sTargetClass, $sAttCode);
- $aValues[$sAttCode] = $oAttDef->MakeValueFromString($sValue, $bLocalizedValue, $sSepItem, $sSepAttribute, $sSepValue, $sAttributeQualifier);
- }
- }
- // 2nd - Instanciate the object and set the value
- if (isset($aValues['finalclass']))
- {
- $sLinkClass = $aValues['finalclass'];
- if (!is_subclass_of($sLinkClass, $sTargetClass))
- {
- throw new CoreException('Wrong class for link attribute specification', array('requested_class' => $sLinkClass, 'expected_class' => $sTargetClass));
- }
- }
- elseif (MetaModel::IsAbstract($sTargetClass))
- {
- throw new CoreException('Missing finalclass for link attribute specification');
- }
- else
- {
- $sLinkClass = $sTargetClass;
- }
- $oLink = MetaModel::NewObject($sLinkClass);
- foreach ($aValues as $sAttCode => $sValue)
- {
- $oLink->Set($sAttCode, $sValue);
- }
- // 3rd - Set external keys from search conditions
- foreach ($aExtKeys as $sKeyAttCode => $aReconciliation)
- {
- $oKeyAttDef = MetaModel::GetAttributeDef($sTargetClass, $sKeyAttCode);
- $sKeyClass = $oKeyAttDef->GetTargetClass();
- $oExtKeyFilter = new DBObjectSearch($sKeyClass);
- $aReconciliationDesc = array();
- foreach($aReconciliation as $sRemoteAttCode => $sValue)
- {
- $oExtKeyFilter->AddCondition($sRemoteAttCode, $sValue, '=');
- $aReconciliationDesc[] = "$sRemoteAttCode=$sValue";
- }
- $oExtKeySet = new CMDBObjectSet($oExtKeyFilter);
- switch($oExtKeySet->Count())
- {
- case 0:
- $sReconciliationDesc = implode(', ', $aReconciliationDesc);
- throw new CoreException("Found no match", array('ext_key' => $sKeyAttCode, 'reconciliation' => $sReconciliationDesc));
- break;
- case 1:
- $oRemoteObj = $oExtKeySet->Fetch();
- $oLink->Set($sKeyAttCode, $oRemoteObj->GetKey());
- break;
- default:
- $sReconciliationDesc = implode(', ', $aReconciliationDesc);
- throw new CoreException("Found several matches", array('ext_key' => $sKeyAttCode, 'reconciliation' => $sReconciliationDesc));
- // Found several matches, ambiguous
- }
- }
- // Check (roughly) if such a link is valid
- $aErrors = array();
- foreach(MetaModel::ListAttributeDefs($sTargetClass) as $sAttCode => $oAttDef)
- {
- if ($oAttDef->IsExternalKey())
- {
- if (($oAttDef->GetTargetClass() == $this->GetHostClass()) || (is_subclass_of($this->GetHostClass(), $oAttDef->GetTargetClass())))
- {
- continue; // Don't check the key to self
- }
- }
-
- if ($oAttDef->IsWritable() && $oAttDef->IsNull($oLink->Get($sAttCode)) && !$oAttDef->IsNullAllowed())
- {
- $aErrors[] = $sAttCode;
- }
- }
- if (count($aErrors) > 0)
- {
- throw new CoreException("Missing value for mandatory attribute(s): ".implode(', ', $aErrors));
- }
- $aLinks[] = $oLink;
- }
- $oSet = DBObjectSet::FromArray($sTargetClass, $aLinks);
- return $oSet;
- }
- /**
- * Helper to get a value that will be JSON encoded
- * The operation is the opposite to FromJSONToValue
- */
- public function GetForJSON($value)
- {
- $aRet = array();
- if (is_object($value) && ($value instanceof DBObjectSet))
- {
- $value->Rewind();
- while ($oObj = $value->Fetch())
- {
- $sObjClass = get_class($oObj);
- // Show only relevant information (hide the external key to the current object)
- $aAttributes = array();
- foreach(MetaModel::ListAttributeDefs($sObjClass) as $sAttCode => $oAttDef)
- {
- if ($sAttCode == 'finalclass')
- {
- if ($sObjClass == $this->GetLinkedClass())
- {
- // Simplify the output if the exact class could be determined implicitely
- continue;
- }
- }
- if ($sAttCode == $this->GetExtKeyToMe()) continue;
- if ($oAttDef->IsExternalField()) continue;
- if (!$oAttDef->IsDirectField()) continue;
- if (!$oAttDef->IsScalar()) continue;
- $attValue = $oObj->Get($sAttCode);
- $aAttributes[$sAttCode] = $oAttDef->GetForJSON($attValue);
- }
- $aRet[] = $aAttributes;
- }
- }
- return $aRet;
- }
- /**
- * Helper to form a value, given JSON decoded data
- * The operation is the opposite to GetForJSON
- */
- public function FromJSONToValue($json)
- {
- $sTargetClass = $this->Get('linked_class');
- $aLinks = array();
- foreach($json as $aValues)
- {
- if (isset($aValues['finalclass']))
- {
- $sLinkClass = $aValues['finalclass'];
- if (!is_subclass_of($sLinkClass, $sTargetClass))
- {
- throw new CoreException('Wrong class for link attribute specification', array('requested_class' => $sLinkClass, 'expected_class' => $sTargetClass));
- }
- }
- elseif (MetaModel::IsAbstract($sTargetClass))
- {
- throw new CoreException('Missing finalclass for link attribute specification');
- }
- else
- {
- $sLinkClass = $sTargetClass;
- }
- $oLink = MetaModel::NewObject($sLinkClass);
- foreach ($aValues as $sAttCode => $sValue)
- {
- $oLink->Set($sAttCode, $sValue);
- }
- // Check (roughly) if such a link is valid
- $aErrors = array();
- foreach(MetaModel::ListAttributeDefs($sTargetClass) as $sAttCode => $oAttDef)
- {
- if ($oAttDef->IsExternalKey())
- {
- if (($oAttDef->GetTargetClass() == $this->GetHostClass()) || (is_subclass_of($this->GetHostClass(), $oAttDef->GetTargetClass())))
- {
- continue; // Don't check the key to self
- }
- }
-
- if ($oAttDef->IsWritable() && $oAttDef->IsNull($oLink->Get($sAttCode)) && !$oAttDef->IsNullAllowed())
- {
- $aErrors[] = $sAttCode;
- }
- }
- if (count($aErrors) > 0)
- {
- throw new CoreException("Missing value for mandatory attribute(s): ".implode(', ', $aErrors));
- }
- $aLinks[] = $oLink;
- }
- $oSet = DBObjectSet::FromArray($sTargetClass, $aLinks);
- return $oSet;
- }
- public function Equals($val1, $val2)
- {
- if ($val1 === $val2) return true;
- if (is_object($val1) != is_object($val2))
- {
- return false;
- }
- if (!is_object($val1))
- {
- // string ?
- // todo = implement this case ?
- return false;
- }
- // Note: maintain this algorithm so as to make sure it is strictly equivalent to the one used within DBObject::DBWriteLinks()
- $sExtKeyToMe = $this->GetExtKeyToMe();
- $sAdditionalKey = null;
- if ($this->IsIndirect() && !$this->DuplicatesAllowed())
- {
- $sAdditionalKey = $this->GetExtKeyToRemote();
- }
- $oComparator = new DBObjectSetComparator($val1, $val2, array($sExtKeyToMe), $sAdditionalKey);
- $aChanges = $oComparator->GetDifferences();
- $bAreEquivalent = (count($aChanges['added']) == 0) && (count($aChanges['removed']) == 0) && (count($aChanges['modified']) == 0);
- return $bAreEquivalent;
- }
- /**
- * Find the corresponding "link" attribute on the target class
- *
- * @return string The attribute code on the target class, or null if none has been found
- */
- public function GetMirrorLinkAttribute()
- {
- $oRemoteAtt = MetaModel::GetAttributeDef($this->GetLinkedClass(), $this->GetExtKeyToMe());
- return $oRemoteAtt;
- }
- static public function GetFormFieldClass()
- {
- return '\\Combodo\\iTop\\Form\\Field\\LinkedSetField';
- }
- public function MakeFormField(DBObject $oObject, $oFormField = null)
- {
- if ($oFormField === null)
- {
- $sFormFieldClass = static::GetFormFieldClass();
- $oFormField = new $sFormFieldClass($this->GetCode());
- }
-
- // Setting target class
- if (!$this->IsIndirect())
- {
- $sTargetClass = $this->GetLinkedClass();
- }
- else
- {
- $oRemoteAttDef = MetaModel::GetAttributeDef($this->GetLinkedClass(), $this->GetExtKeyToRemote());
- $sTargetClass = $oRemoteAttDef->GetTargetClass();
- $oFormField->SetExtKeyToRemote($this->GetExtKeyToRemote());
- }
- $oFormField->SetTargetClass($sTargetClass);
- $oFormField->SetIndirect($this->IsIndirect());
- // Setting attcodes to display
- $aAttCodesToDisplay = MetaModel::FlattenZList(MetaModel::GetZListItems($sTargetClass, 'list'));
- // - Adding friendlyname attribute to the list is not already in it
- $sTitleAttCode = MetaModel::GetFriendlyNameAttributeCode($sTargetClass);
- if (($sTitleAttCode !== null) && !in_array($sTitleAttCode, $aAttCodesToDisplay))
- {
- $aAttCodesToDisplay = array_merge(array($sTitleAttCode), $aAttCodesToDisplay);
- }
- // - Adding attribute labels
- $aAttributesToDisplay = array();
- foreach ($aAttCodesToDisplay as $sAttCodeToDisplay)
- {
- $oAttDefToDisplay = MetaModel::GetAttributeDef($sTargetClass, $sAttCodeToDisplay);
- $aAttributesToDisplay[$sAttCodeToDisplay] = $oAttDefToDisplay->GetLabel();
- }
- $oFormField->SetAttributesToDisplay($aAttributesToDisplay);
-
- parent::MakeFormField($oObject, $oFormField);
-
- return $oFormField;
- }
- public function IsPartOfFingerprint() { return false; }
- }
- /**
- * Set of objects linked to an object (n-n), and being part of its definition
- *
- * @package iTopORM
- */
- class AttributeLinkedSetIndirect extends AttributeLinkedSet
- {
- static public function ListExpectedParams()
- {
- return array_merge(parent::ListExpectedParams(), array("ext_key_to_remote"));
- }
- public function IsIndirect() {return true;}
- public function GetExtKeyToRemote() { return $this->Get('ext_key_to_remote'); }
- public function GetEditClass() {return "LinkedSet";}
- public function DuplicatesAllowed() {return $this->GetOptional("duplicates", false);} // The same object may be linked several times... or not...
- public function GetTrackingLevel()
- {
- return $this->GetOptional('tracking_level', MetaModel::GetConfig()->Get('tracking_level_linked_set_indirect_default'));
- }
- /**
- * Find the corresponding "link" attribute on the target class
- *
- * @return string The attribute code on the target class, or null if none has been found
- */
- public function GetMirrorLinkAttribute()
- {
- $oRet = null;
- $oExtKeyToRemote = MetaModel::GetAttributeDef($this->GetLinkedClass(), $this->GetExtKeyToRemote());
- $sRemoteClass = $oExtKeyToRemote->GetTargetClass();
- foreach (MetaModel::ListAttributeDefs($sRemoteClass) as $sRemoteAttCode => $oRemoteAttDef)
- {
- if (!$oRemoteAttDef instanceof AttributeLinkedSetIndirect) continue;
- if ($oRemoteAttDef->GetLinkedClass() != $this->GetLinkedClass()) continue;
- if ($oRemoteAttDef->GetExtKeyToMe() != $this->GetExtKeyToRemote()) continue;
- if ($oRemoteAttDef->GetExtKeyToRemote() != $this->GetExtKeyToMe()) continue;
- $oRet = $oRemoteAttDef;
- break;
- }
- return $oRet;
- }
- }
- /**
- * Abstract class implementing default filters for a DB column
- *
- * @package iTopORM
- */
- class AttributeDBFieldVoid extends AttributeDefinition
- {
- static public function ListExpectedParams()
- {
- return array_merge(parent::ListExpectedParams(), array("allowed_values", "depends_on", "sql"));
- }
- // To be overriden, used in GetSQLColumns
- protected function GetSQLCol($bFullSpec = false)
- {
- return "VARCHAR(255)".($bFullSpec ? $this->GetSQLColSpec() : '');
- }
- protected function GetSQLColSpec()
- {
- $default = $this->ScalarToSQL($this->GetDefaultValue());
- if (is_null($default))
- {
- $sRet = '';
- }
- else
- {
- if (is_numeric($default))
- {
- // Though it is a string in PHP, it will be considered as a numeric value in MySQL
- // Then it must not be quoted here, to preserve the compatibility with the value returned by CMDBSource::GetFieldSpec
- $sRet = " DEFAULT $default";
- }
- else
- {
- $sRet = " DEFAULT ".CMDBSource::Quote($default);
- }
- }
- return $sRet;
- }
- public function GetEditClass() {return "String";}
-
- public function GetValuesDef() {return $this->Get("allowed_values");}
- public function GetPrerequisiteAttributes($sClass = null) {return $this->Get("depends_on");}
- public function IsDirectField() {return true;}
- public function IsScalar() {return true;}
- public function IsWritable() {return true;}
- public function GetSQLExpr()
- {
- return $this->Get("sql");
- }
- public function GetDefaultValue(DBObject $oHostObject = null) {return $this->MakeRealValue("", $oHostObject);}
- public function IsNullAllowed() {return false;}
- //
- protected function ScalarToSQL($value) {return $value;} // format value as a valuable SQL literal (quoted outside)
- public function GetSQLExpressions($sPrefix = '')
- {
- $aColumns = array();
- // Note: to optimize things, the existence of the attribute is determined by the existence of one column with an empty suffix
- $aColumns[''] = $this->Get("sql");
- return $aColumns;
- }
- public function FromSQLToValue($aCols, $sPrefix = '')
- {
- $value = $this->MakeRealValue($aCols[$sPrefix.''], null);
- return $value;
- }
- public function GetSQLValues($value)
- {
- $aValues = array();
- $aValues[$this->Get("sql")] = $this->ScalarToSQL($value);
- return $aValues;
- }
- public function GetSQLColumns($bFullSpec = false)
- {
- $aColumns = array();
- $aColumns[$this->Get("sql")] = $this->GetSQLCol($bFullSpec);
- return $aColumns;
- }
- public function GetFilterDefinitions()
- {
- return array($this->GetCode() => new FilterFromAttribute($this));
- }
- public function GetBasicFilterOperators()
- {
- return array("="=>"equals", "!="=>"differs from");
- }
- public function GetBasicFilterLooseOperator()
- {
- return "=";
- }
- public function GetBasicFilterSQLExpr($sOpCode, $value)
- {
- $sQValue = CMDBSource::Quote($value);
- switch ($sOpCode)
- {
- case '!=':
- return $this->GetSQLExpr()." != $sQValue";
- break;
- case '=':
- default:
- return $this->GetSQLExpr()." = $sQValue";
- }
- }
- }
- /**
- * Base class for all kind of DB attributes, with the exception of external keys
- *
- * @package iTopORM
- */
- class AttributeDBField extends AttributeDBFieldVoid
- {
- static public function ListExpectedParams()
- {
- return array_merge(parent::ListExpectedParams(), array("default_value", "is_null_allowed"));
- }
- public function GetDefaultValue(DBObject $oHostObject = null) {return $this->MakeRealValue($this->Get("default_value"), $oHostObject);}
- public function IsNullAllowed() {return $this->Get("is_null_allowed");}
- }
- /**
- * Map an integer column to an attribute
- *
- * @package iTopORM
- */
- class AttributeInteger extends AttributeDBField
- {
- static public function ListExpectedParams()
- {
- return parent::ListExpectedParams();
- //return array_merge(parent::ListExpectedParams(), array());
- }
- public function GetEditClass() {return "String";}
- protected function GetSQLCol($bFullSpec = false) {return "INT(11)".($bFullSpec ? $this->GetSQLColSpec() : '');}
-
- public function GetValidationPattern()
- {
- return "^[0-9]+$";
- }
- public function GetBasicFilterOperators()
- {
- return array(
- "!="=>"differs from",
- "="=>"equals",
- ">"=>"greater (strict) than",
- ">="=>"greater than",
- "<"=>"less (strict) than",
- "<="=>"less than",
- "in"=>"in"
- );
- }
- public function GetBasicFilterLooseOperator()
- {
- // Unless we implement an "equals approximately..." or "same order of magnitude"
- return "=";
- }
- public function GetBasicFilterSQLExpr($sOpCode, $value)
- {
- $sQValue = CMDBSource::Quote($value);
- switch ($sOpCode)
- {
- case '!=':
- return $this->GetSQLExpr()." != $sQValue";
- break;
- case '>':
- return $this->GetSQLExpr()." > $sQValue";
- break;
- case '>=':
- return $this->GetSQLExpr()." >= $sQValue";
- break;
- case '<':
- return $this->GetSQLExpr()." < $sQValue";
- break;
- case '<=':
- return $this->GetSQLExpr()." <= $sQValue";
- break;
- case 'in':
- if (!is_array($value)) throw new CoreException("Expected an array for argument value (sOpCode='$sOpCode')");
- return $this->GetSQLExpr()." IN ('".implode("', '", $value)."')";
- break;
- case '=':
- default:
- return $this->GetSQLExpr()." = \"$value\"";
- }
- }
- public function GetNullValue()
- {
- return null;
- }
- public function IsNull($proposedValue)
- {
- return is_null($proposedValue);
- }
- public function MakeRealValue($proposedValue, $oHostObj)
- {
- if (is_null($proposedValue)) return null;
- if ($proposedValue === '') return null; // 0 is transformed into '' !
- return (int)$proposedValue;
- }
- public function ScalarToSQL($value)
- {
- assert(is_numeric($value) || is_null($value));
- return $value; // supposed to be an int
- }
- }
- /**
- * An external key for which the class is defined as the value of another attribute
- *
- * @package iTopORM
- */
- class AttributeObjectKey extends AttributeDBFieldVoid
- {
- static public function ListExpectedParams()
- {
- return array_merge(parent::ListExpectedParams(), array('class_attcode', 'is_null_allowed'));
- }
- public function GetEditClass() {return "String";}
- protected function GetSQLCol($bFullSpec = false) {return "INT(11)".($bFullSpec ? " DEFAULT 0" : "");}
- public function GetDefaultValue(DBObject $oHostObject = null) {return 0;}
- public function IsNullAllowed()
- {
- return $this->Get("is_null_allowed");
- }
- public function GetBasicFilterOperators()
- {
- return parent::GetBasicFilterOperators();
- }
- public function GetBasicFilterLooseOperator()
- {
- return parent::GetBasicFilterLooseOperator();
- }
- public function GetBasicFilterSQLExpr($sOpCode, $value)
- {
- return parent::GetBasicFilterSQLExpr($sOpCode, $value);
- }
- public function GetNullValue()
- {
- return 0;
- }
- public function IsNull($proposedValue)
- {
- return ($proposedValue == 0);
- }
- public function MakeRealValue($proposedValue, $oHostObj)
- {
- if (is_null($proposedValue)) return 0;
- if ($proposedValue === '') return 0;
- if (MetaModel::IsValidObject($proposedValue)) return $proposedValue->GetKey();
- return (int)$proposedValue;
- }
- }
- /**
- * Display an integer between 0 and 100 as a percentage / horizontal bar graph
- *
- * @package iTopORM
- */
- class AttributePercentage extends AttributeInteger
- {
- public function GetAsHTML($sValue, $oHostObject = null, $bLocalize = true)
- {
- $iWidth = 5; // Total width of the percentage bar graph, in em...
- $iValue = (int)$sValue;
- if ($iValue > 100)
- {
- $iValue = 100;
- }
- else if ($iValue < 0)
- {
- $iValue = 0;
- }
- if ($iValue > 90)
- {
- $sColor = "#cc3300";
- }
- else if ($iValue > 50)
- {
- $sColor = "#cccc00";
- }
- else
- {
- $sColor = "#33cc00";
- }
- $iPercentWidth = ($iWidth * $iValue) / 100;
- return "<div style=\"width:{$iWidth}em;-moz-border-radius: 3px;-webkit-border-radius: 3px;border-radius: 3px;display:inline-block;border: 1px #ccc solid;\"><div style=\"width:{$iPercentWidth}em; display:inline-block;background-color:$sColor;\"> </div></div> $sValue %";
- }
- }
- /**
- * Map a decimal value column (suitable for financial computations) to an attribute
- * internally in PHP such numbers are represented as string. Should you want to perform
- * a calculation on them, it is recommended to use the BC Math functions in order to
- * retain the precision
- *
- * @package iTopORM
- */
- class AttributeDecimal extends AttributeDBField
- {
- static public function ListExpectedParams()
- {
- return array_merge(parent::ListExpectedParams(), array('digits', 'decimals' /* including precision */));
- }
- public function GetEditClass() {return "String";}
- protected function GetSQLCol($bFullSpec = false)
- {
- return "DECIMAL(".$this->Get('digits').",".$this->Get('decimals').")".($bFullSpec ? $this->GetSQLColSpec() : '');
- }
-
- public function GetValidationPattern()
- {
- $iNbDigits = $this->Get('digits');
- $iPrecision = $this->Get('decimals');
- $iNbIntegerDigits = $iNbDigits - $iPrecision - 1; // -1 because the first digit is treated separately in the pattern below
- return "^[-+]?[0-9]\d{0,$iNbIntegerDigits}(\.\d{0,$iPrecision})?$";
- }
- public function GetBasicFilterOperators()
- {
- return array(
- "!="=>"differs from",
- "="=>"equals",
- ">"=>"greater (strict) than",
- ">="=>"greater than",
- "<"=>"less (strict) than",
- "<="=>"less than",
- "in"=>"in"
- );
- }
- public function GetBasicFilterLooseOperator()
- {
- // Unless we implement an "equals approximately..." or "same order of magnitude"
- return "=";
- }
- public function GetBasicFilterSQLExpr($sOpCode, $value)
- {
- $sQValue = CMDBSource::Quote($value);
- switch ($sOpCode)
- {
- case '!=':
- return $this->GetSQLExpr()." != $sQValue";
- break;
- case '>':
- return $this->GetSQLExpr()." > $sQValue";
- break;
- case '>=':
- return $this->GetSQLExpr()." >= $sQValue";
- break;
- case '<':
- return $this->GetSQLExpr()." < $sQValue";
- break;
- case '<=':
- return $this->GetSQLExpr()." <= $sQValue";
- break;
- case 'in':
- if (!is_array($value)) throw new CoreException("Expected an array for argument value (sOpCode='$sOpCode')");
- return $this->GetSQLExpr()." IN ('".implode("', '", $value)."')";
- break;
- case '=':
- default:
- return $this->GetSQLExpr()." = \"$value\"";
- }
- }
- public function GetNullValue()
- {
- return null;
- }
- public function IsNull($proposedValue)
- {
- return is_null($proposedValue);
- }
- public function MakeRealValue($proposedValue, $oHostObj)
- {
- if (is_null($proposedValue)) return null;
- if ($proposedValue === '') return null;
- return (string)$proposedValue;
- }
- public function ScalarToSQL($value)
- {
- assert(is_null($value) || preg_match('/'.$this->GetValidationPattern().'/', $value));
- return $value; // null or string
- }
- }
- /**
- * Map a boolean column to an attribute
- *
- * @package iTopORM
- */
- class AttributeBoolean extends AttributeInteger
- {
- static public function ListExpectedParams()
- {
- return parent::ListExpectedParams();
- //return array_merge(parent::ListExpectedParams(), array());
- }
- public function GetEditClass() {return "Integer";}
- protected function GetSQLCol($bFullSpec = false) {return "TINYINT(1)".($bFullSpec ? $this->GetSQLColSpec() : '');}
-
- public function MakeRealValue($proposedValue, $oHostObj)
- {
- if (is_null($proposedValue)) return null;
- if ($proposedValue === '') return null;
- if ((int)$proposedValue) return true;
- return false;
- }
- public function ScalarToSQL($value)
- {
- if ($value) return 1;
- return 0;
- }
- public function GetAsXML($sValue, $oHostObject = null, $bLocalize = true)
- {
- return $sValue ? '1' : '0';
- }
- public function GetAsCSV($sValue, $sSeparator = ',', $sTextQualifier = '"', $oHostObject = null, $bLocalize = true, $bConvertToPlainText = false)
- {
- return $sValue ? '1' : '0';
- }
- public function GetAsHTML($sValue, $oHostObject = null, $bLocalize = true)
- {
- return $sValue ? '1' : '0';
- }
- /**
- * Helper to get a value that will be JSON encoded
- * The operation is the opposite to FromJSONToValue
- */
- public function GetForJSON($value)
- {
- return $value ? '1' : '0';
- }
- }
- /**
- * Map a varchar column (size < ?) to an attribute
- *
- * @package iTopORM
- */
- class AttributeString extends AttributeDBField
- {
- static public function ListExpectedParams()
- {
- return parent::ListExpectedParams();
- //return array_merge(parent::ListExpectedParams(), array());
- }
- public function GetEditClass() {return "String";}
- protected function GetSQLCol($bFullSpec = false) {return "VARCHAR(255)".($bFullSpec ? $this->GetSQLColSpec() : '');}
- public function GetValidationPattern()
- {
- $sPattern = $this->GetOptional('validation_pattern', '');
- if (empty($sPattern))
- {
- return parent::GetValidationPattern();
- }
- else
- {
- return $sPattern;
- }
- }
- public function CheckFormat($value)
- {
- $sRegExp = $this->GetValidationPattern();
- if (empty($sRegExp))
- {
- return true;
- }
- else
- {
- $sRegExp = str_replace('/', '\\/', $sRegExp);
- return preg_match("/$sRegExp/", $value);
- }
- }
- public function GetMaxSize()
- {
- return 255;
- }
- public function GetBasicFilterOperators()
- {
- return array(
- "="=>"equals",
- "!="=>"differs from",
- "Like"=>"equals (no case)",
- "NotLike"=>"differs from (no case)",
- "Contains"=>"contains",
- "Begins with"=>"begins with",
- "Finishes with"=>"finishes with"
- );
- }
- public function GetBasicFilterLooseOperator()
- {
- return "Contains";
- }
- public function GetBasicFilterSQLExpr($sOpCode, $value)
- {
- $sQValue = CMDBSource::Quote($value);
- switch ($sOpCode)
- {
- case '=':
- case '!=':
- return $this->GetSQLExpr()." $sOpCode $sQValue";
- case 'Begins with':
- return $this->GetSQLExpr()." LIKE ".CMDBSource::Quote("$value%");
- case 'Finishes with':
- return $this->GetSQLExpr()." LIKE ".CMDBSource::Quote("%$value");
- case 'Contains':
- return $this->GetSQLExpr()." LIKE ".CMDBSource::Quote("%$value%");
- case 'NotLike':
- return $this->GetSQLExpr()." NOT LIKE $sQValue";
- case 'Like':
- default:
- return $this->GetSQLExpr()." LIKE $sQValue";
- }
- }
- public function GetNullValue()
- {
- return '';
- }
- public function IsNull($proposedValue)
- {
- return ($proposedValue == '');
- }
- public function MakeRealValue($proposedValue, $oHostObj)
- {
- if (is_null($proposedValue)) return '';
- return (string)$proposedValue;
- }
- public function ScalarToSQL($value)
- {
- if (!is_string($value) && !is_null($value))
- {
- throw new CoreWarning('Expected the attribute value to be a string', array('found_type' => gettype($value), 'value' => $value, 'class' => $this->GetHostClass(), 'attribute' => $this->GetCode()));
- }
- return $value;
- }
- public function GetAsCSV($sValue, $sSeparator = ',', $sTextQualifier = '"', $oHostObject = null, $bLocalize = true, $bConvertToPlainText = false)
- {
- $sFrom = array("\r\n", $sTextQualifier);
- $sTo = array("\n", $sTextQualifier.$sTextQualifier);
- $sEscaped = str_replace($sFrom, $sTo, (string)$sValue);
- return $sTextQualifier.$sEscaped.$sTextQualifier;
- }
- public function GetDisplayStyle()
- {
- return $this->GetOptional('display_style', 'select');
- }
- static public function GetFormFieldClass()
- {
- return '\\Combodo\\iTop\\Form\\Field\\StringField';
- }
- public function MakeFormField(DBObject $oObject, $oFormField = null)
- {
- if ($oFormField === null)
- {
- $sFormFieldClass = static::GetFormFieldClass();
- $oFormField = new $sFormFieldClass($this->GetCode());
- }
- parent::MakeFormField($oObject, $oFormField);
- return $oFormField;
- }
- }
- /**
- * An attibute that matches an object class
- *
- * @package iTopORM
- */
- class AttributeClass extends AttributeString
- {
- static public function ListExpectedParams()
- {
- return array_merge(parent::ListExpectedParams(), array("class_category", "more_values"));
- }
- public function __construct($sCode, $aParams)
- {
- $this->m_sCode = $sCode;
- $aParams["allowed_values"] = new ValueSetEnumClasses($aParams['class_category'], $aParams['more_values']);
- parent::__construct($sCode, $aParams);
- }
- public function GetDefaultValue(DBObject $oHostObject = null)
- {
- $sDefault = parent::GetDefaultValue($oHostObject);
- if (!$this->IsNullAllowed() && $this->IsNull($sDefault))
- {
- // For this kind of attribute specifying null as default value
- // is authorized even if null is not allowed
-
- // Pick the first one...
- $aClasses = $this->GetAllowedValues();
- $sDefault = key($aClasses);
- }
- return $sDefault;
- }
- public function GetAsHTML($sValue, $oHostObject = null, $bLocalize = true)
- {
- if (empty($sValue)) return '';
- return MetaModel::GetName($sValue);
- }
- public function RequiresIndex()
- {
- return true;
- }
-
- public function GetBasicFilterLooseOperator()
- {
- return '=';
- }
-
- }
- /**
- * An attibute that matches one of the language codes availables in the dictionnary
- *
- * @package iTopORM
- */
- class AttributeApplicationLanguage extends AttributeString
- {
- static public function ListExpectedParams()
- {
- return parent::ListExpectedParams();
- }
- public function __construct($sCode, $aParams)
- {
- $this->m_sCode = $sCode;
- $aAvailableLanguages = Dict::GetLanguages();
- $aLanguageCodes = array();
- foreach($aAvailableLanguages as $sLangCode => $aInfo)
- {
- $aLanguageCodes[$sLangCode] = $aInfo['description'].' ('.$aInfo['localized_description'].')';
- }
- $aParams["allowed_values"] = new ValueSetEnum($aLanguageCodes);
- parent::__construct($sCode, $aParams);
- }
- public function RequiresIndex()
- {
- return true;
- }
-
- public function GetBasicFilterLooseOperator()
- {
- return '=';
- }
- }
- /**
- * The attribute dedicated to the finalclass automatic attribute
- *
- * @package iTopORM
- */
- class AttributeFinalClass extends AttributeString
- {
- public function __construct($sCode, $aParams)
- {
- $this->m_sCode = $sCode;
- $aParams["allowed_values"] = null;
- parent::__construct($sCode, $aParams);
- $this->m_sValue = $this->Get("default_value");
- }
- public function IsWritable()
- {
- return false;
- }
- public function RequiresIndex()
- {
- return true;
- }
- public function SetFixedValue($sValue)
- {
- $this->m_sValue = $sValue;
- }
- public function GetDefaultValue(DBObject $oHostObject = null)
- {
- return $this->m_sValue;
- }
- public function GetAsHTML($sValue, $oHostObject = null, $bLocalize = true)
- {
- if (empty($sValue)) return '';
- if ($bLocalize)
- {
- return MetaModel::GetName($sValue);
- }
- else
- {
- return $sValue;
- }
- }
- // Because this is sometimes used to get a localized/string version of an attribute...
- public function GetEditValue($sValue, $oHostObj = null)
- {
- if (empty($sValue)) return '';
- return MetaModel::GetName($sValue);
- }
- /**
- * Helper to get a value that will be JSON encoded
- * The operation is the opposite to FromJSONToValue
- */
- public function GetForJSON($value)
- {
- // JSON values are NOT localized
- return $value;
- }
-
- public function GetAsCSV($value, $sSeparator = ',', $sTextQualifier = '"', $oHostObject = null, $bLocalize = true, $bConvertToPlainText = false)
- {
- if ($bLocalize && $value != '')
- {
- $sRawValue = MetaModel::GetName($value);
- }
- else
- {
- $sRawValue = $value;
- }
- return parent::GetAsCSV($sRawValue, $sSeparator, $sTextQualifier, null, false, $bConvertToPlainText);
- }
- public function GetAsXML($value, $oHostObject = null, $bLocalize = true)
- {
- if (empty($value)) return '';
- if ($bLocalize)
- {
- $sRawValue = MetaModel::GetName($value);
- }
- else
- {
- $sRawValue = $value;
- }
- return Str::pure2xml($sRawValue);
- }
- public function GetBasicFilterLooseOperator()
- {
- return '=';
- }
-
- public function GetValueLabel($sValue)
- {
- if (empty($sValue)) return '';
- return MetaModel::GetName($sValue);
- }
- public function GetAllowedValues($aArgs = array(), $sContains = '')
- {
- $aRawValues = MetaModel::EnumChildClasses($this->GetHostClass(), ENUM_CHILD_CLASSES_ALL);
- $aLocalizedValues = array();
- foreach ($aRawValues as $sClass)
- {
- $aLocalizedValues[$sClass] = MetaModel::GetName($sClass);
- }
- return $aLocalizedValues;
- }
- }
- /**
- * Map a varchar column (size < ?) to an attribute that must never be shown to the user
- *
- * @package iTopORM
- */
- class AttributePassword extends AttributeString
- {
- static public function ListExpectedParams()
- {
- return parent::ListExpectedParams();
- //return array_merge(parent::ListExpectedParams(), array());
- }
- public function GetEditClass() {return "Password";}
- protected function GetSQLCol($bFullSpec = false) {return "VARCHAR(64)".($bFullSpec ? $this->GetSQLColSpec() : '');}
- public function GetMaxSize()
- {
- return 64;
- }
- public function GetFilterDefinitions()
- {
- // Note: due to this, you will get an error if a password is being declared as a search criteria (see ZLists)
- // not allowed to search on passwords!
- return array();
- }
- public function GetAsHTML($sValue, $oHostObject = null, $bLocalize = true)
- {
- if (strlen($sValue) == 0)
- {
- return '';
- }
- else
- {
- return '******';
- }
- }
-
- public function IsPartOfFingerprint() { return false; } // Cannot reliably compare two encrypted passwords since the same password will be encrypted in diffferent manners depending on the random 'salt'
- }
- /**
- * Map a text column (size < 255) to an attribute that is encrypted in the database
- * The encryption is based on a key set per iTop instance. Thus if you export your
- * database (in SQL) to someone else without providing the key at the same time
- * the encrypted fields will remain encrypted
- *
- * @package iTopORM
- */
- class AttributeEncryptedString extends AttributeString
- {
- static $sKey = null; // Encryption key used for all encrypted fields
- public function __construct($sCode, $aParams)
- {
- parent::__construct($sCode, $aParams);
- if (self::$sKey == null)
- {
- self::$sKey = MetaModel::GetConfig()->GetEncryptionKey();
- }
- }
- /**
- * When the attribute definitions are stored in APC cache:
- * 1) The static class variable $sKey is NOT serialized
- * 2) The object's constructor is NOT called upon wakeup
- * 3) mcrypt may crash the server if passed an empty key !!
- *
- * So let's restore the key (if needed) when waking up
- **/
- public function __wakeup()
- {
- if (self::$sKey == null)
- {
- self::$sKey = MetaModel::GetConfig()->GetEncryptionKey();
- }
- }
-
- protected function GetSQLCol($bFullSpec = false) {return "TINYBLOB";}
- public function GetMaxSize()
- {
- return 255;
- }
- public function GetFilterDefinitions()
- {
- // Note: due to this, you will get an error if a an encrypted field is declared as a search criteria (see ZLists)
- // not allowed to search on encrypted fields !
- return array();
- }
- public function MakeRealValue($proposedValue, $oHostObj)
- {
- if (is_null($proposedValue)) return null;
- return (string)$proposedValue;
- }
- /**
- * Decrypt the value when reading from the database
- */
- public function FromSQLToValue($aCols, $sPrefix = '')
- {
- $oSimpleCrypt = new SimpleCrypt();
- $sValue = $oSimpleCrypt->Decrypt(self::$sKey, $aCols[$sPrefix]);
- return $sValue;
- }
- /**
- * Encrypt the value before storing it in the database
- */
- public function GetSQLValues($value)
- {
- $oSimpleCrypt = new SimpleCrypt();
- $encryptedValue = $oSimpleCrypt->Encrypt(self::$sKey, $value);
- $aValues = array();
- $aValues[$this->Get("sql")] = $encryptedValue;
- return $aValues;
- }
- }
- // Wiki formatting - experimental
- //
- // [[<objClass>:<objName>]]
- // Example: [[Server:db1.tnut.com]]
- define('WIKI_OBJECT_REGEXP', '/\[\[(.+):(.+)\]\]/U');
- /**
- * Map a text column (size > ?) to an attribute
- *
- * @package iTopORM
- */
- class AttributeText extends AttributeString
- {
- public function GetEditClass() {return ($this->GetFormat() == 'text') ? 'Text' : "HTML";}
-
- protected function GetSQLCol($bFullSpec = false) {return "TEXT";}
- public function GetSQLColumns($bFullSpec = false)
- {
- $aColumns = array();
- $aColumns[$this->Get('sql')] = $this->GetSQLCol($bFullSpec);
- if ($this->GetOptional('format', null) != null )
- {
- // Add the extra column only if the property 'format' is specified for the attribute
- $aColumns[$this->Get('sql').'_format'] = "ENUM('text','html')";
- if ($bFullSpec)
- {
- $aColumns[$this->Get('sql').'_format'].= " DEFAULT 'text'"; // default 'text' is for migrating old records
- }
- }
- return $aColumns;
- }
- public function GetSQLExpressions($sPrefix = '')
- {
- if ($sPrefix == '')
- {
- $sPrefix = $this->Get('sql');
- }
- $aColumns = array();
- // Note: to optimize things, the existence of the attribute is determined by the existence of one column with an empty suffix
- $aColumns[''] = $sPrefix;
- if ($this->GetOptional('format', null) != null )
- {
- // Add the extra column only if the property 'format' is specified for the attribute
- $aColumns['_format'] = $sPrefix.'_format';
- }
- return $aColumns;
- }
-
- public function GetMaxSize()
- {
- // Is there a way to know the current limitation for mysql?
- // See mysql_field_len()
- return 65535;
- }
- static public function RenderWikiHtml($sText)
- {
- $sPattern = '/'.str_replace('/', '\/', utils::GetConfig()->Get('url_validation_pattern')).'/i';
- if (preg_match_all($sPattern, $sText, $aAllMatches, PREG_SET_ORDER /* important !*/ |PREG_OFFSET_CAPTURE /* important ! */))
- {
- $aUrls = array();
- $i = count($aAllMatches);
- // Replace the URLs by an actual hyperlink <a href="...">...</a>
- // Let's do it backwards so that the initial positions are not modified by the replacement
- // This works if the matches are captured: in the order they occur in the string AND
- // with their offset (i.e. position) inside the string
- while($i > 0)
- {
- $i--;
- $sUrl = $aAllMatches[$i][0][0]; // String corresponding to the main pattern
- $iPos = $aAllMatches[$i][0][1]; // Position of the main pattern
- $sText = substr_replace($sText, "<a href=\"$sUrl\">$sUrl</a>", $iPos, strlen($sUrl));
-
- }
- }
- if (preg_match_all(WIKI_OBJECT_REGEXP, $sText, $aAllMatches, PREG_SET_ORDER))
- {
- foreach($aAllMatches as $iPos => $aMatches)
- {
- $sClass = trim($aMatches[1]);
- $sName = trim($aMatches[2]);
-
- if (MetaModel::IsValidClass($sClass))
- {
- $oObj = MetaModel::GetObjectByName($sClass, $sName, false /* MustBeFound */);
- if (is_object($oObj))
- {
- // Propose a std link to the object
- $sText = str_replace($aMatches[0], $oObj->GetHyperlink(), $sText);
- }
- else
- {
- // Propose a std link to the object
- $sClassLabel = MetaModel::GetName($sClass);
- $sText = str_replace($aMatches[0], "<span class=\"wiki_broken_link\">$sClassLabel:$sName</span>", $sText);
- // Later: propose a link to create a new object
- // Anyhow... there is no easy way to suggest default values based on the given FRIENDLY name
- //$sText = preg_replace('/\[\[(.+):(.+)\]\]/', '<a href="'.utils::GetAbsoluteUrlAppRoot().'pages/UI.php?operation=new&class='.$sClass.'&default[att1]=xxx&default[att2]=yyy">'.$sName.'</a>', $sText);
- }
- }
- }
- }
- return $sText;
- }
- public function GetAsHTML($sValue, $oHostObject = null, $bLocalize = true)
- {
- $aStyles = array();
- if ($this->GetWidth() != '')
- {
- $aStyles[] = 'width:'.$this->GetWidth();
- }
- if ($this->GetHeight() != '')
- {
- $aStyles[] = 'height:'.$this->GetHeight();
- }
- $sStyle = '';
- if (count($aStyles) > 0)
- {
- $aStyles[] = 'overflow:auto';
- $sStyle = 'style="'.implode(';', $aStyles).'"';
- }
-
- if ($this->GetFormat() == 'text')
- {
- $sValue = parent::GetAsHTML($sValue, $oHostObject, $bLocalize);
- $sValue = self::RenderWikiHtml($sValue);
- return "<div $sStyle>".str_replace("\n", "<br>\n", $sValue).'</div>';
- }
- else
- {
- return "<div class=\"HTML\" $sStyle>".InlineImage::FixUrls($sValue).'</div>';
- }
-
- }
- public function GetEditValue($sValue, $oHostObj = null)
- {
- if ($this->GetFormat() == 'text')
- {
- if (preg_match_all(WIKI_OBJECT_REGEXP, $sValue, $aAllMatches, PREG_SET_ORDER))
- {
- foreach($aAllMatches as $iPos => $aMatches)
- {
- $sClass = $aMatches[1];
- $sName = $aMatches[2];
- if (MetaModel::IsValidClass($sClass))
- {
- $sClassLabel = MetaModel::GetName($sClass);
- $sValue = str_replace($aMatches[0], "[[$sClassLabel:$sName]]", $sValue);
- }
- }
- }
- }
- return $sValue;
- }
- /**
- * For fields containing a potential markup, return the value without this markup
- * @return string
- */
- public function GetAsPlainText($sValue, $oHostObj = null)
- {
- if ($this->GetFormat() == 'html')
- {
- return (string) utils::HtmlToText($this->GetEditValue($sValue, $oHostObj));
- }
- else
- {
- return parent::GetAsPlainText($sValue, $oHostObj);
- }
- }
-
- public function MakeRealValue($proposedValue, $oHostObj)
- {
- $sValue = $proposedValue;
- switch ($this->GetFormat())
- {
- case 'html':
- $sValue = HTMLSanitizer::Sanitize($sValue);
- break;
-
- case 'text':
- default:
- if (preg_match_all(WIKI_OBJECT_REGEXP, $sValue, $aAllMatches, PREG_SET_ORDER))
- {
- foreach($aAllMatches as $iPos => $aMatches)
- {
- $sClassLabel = $aMatches[1];
- $sName = $aMatches[2];
- if (!MetaModel::IsValidClass($sClassLabel))
- {
- $sClass = MetaModel::GetClassFromLabel($sClassLabel);
- if ($sClass)
- {
- $sValue = str_replace($aMatches[0], "[[$sClass:$sName]]", $sValue);
- }
- }
- }
- }
- }
- return $sValue;
- }
- public function GetAsXML($value, $oHostObject = null, $bLocalize = true)
- {
- return Str::pure2xml($value);
- }
-
- public function GetWidth()
- {
- return $this->GetOptional('width', '');
- }
-
- public function GetHeight()
- {
- return $this->GetOptional('height', '');
- }
- static public function GetFormFieldClass()
- {
- return '\\Combodo\\iTop\\Form\\Field\\TextAreaField';
- }
- public function MakeFormField(DBObject $oObject, $oFormField = null)
- {
- if ($oFormField === null)
- {
- $sFormFieldClass = static::GetFormFieldClass();
- $oFormField = new $sFormFieldClass($this->GetCode(), null, $oObject);
- $oFormField->SetFormat($this->GetFormat());
- }
- parent::MakeFormField($oObject, $oFormField);
- return $oFormField;
- }
- /**
- * The actual formatting of the field: either text (=plain text) or html (= text with HTML markup)
- * @return string
- */
- public function GetFormat()
- {
- return $this->GetOptional('format', 'text');
- }
-
- /**
- * Read the value from the row returned by the SQL query and transorms it to the appropriate
- * internal format (either text or html)
- * @see AttributeDBFieldVoid::FromSQLToValue()
- */
- public function FromSQLToValue($aCols, $sPrefix = '')
- {
- $value = $aCols[$sPrefix.''];
- if ($this->GetOptional('format', null) != null )
- {
- // Read from the extra column only if the property 'format' is specified for the attribute
- $sFormat = $aCols[$sPrefix.'_format'];
- }
- else
- {
- $sFormat = $this->GetFormat();
- }
-
- switch($sFormat)
- {
- case 'text':
- if ($this->GetFormat() == 'html')
- {
- $value = utils::TextToHtml($value);
- }
- break;
-
- case 'html':
- if ($this->GetFormat() == 'text')
- {
- $value = utils::HtmlToText($value);
- }
- else
- {
- $value = InlineImage::FixUrls((string)$value);
- }
- break;
-
- default:
- // unknown format ??
- }
- return $value;
- }
-
- public function GetSQLValues($value)
- {
- $aValues = array();
- $aValues[$this->Get("sql")] = $this->ScalarToSQL($value);
- if ($this->GetOptional('format', null) != null )
- {
- // Add the extra column only if the property 'format' is specified for the attribute
- $aValues[$this->Get("sql").'_format'] = $this->GetFormat();
- }
- return $aValues;
- }
-
- public function GetAsCSV($sValue, $sSeparator = ',', $sTextQualifier = '"', $oHostObject = null, $bLocalize = true, $bConvertToPlainText = false)
- {
- switch($this->GetFormat())
- {
- case 'html':
- if ($bConvertToPlainText)
- {
- $sValue = utils::HtmlToText((string)$sValue);
- }
- $sFrom = array("\r\n", $sTextQualifier);
- $sTo = array("\n", $sTextQualifier.$sTextQualifier);
- $sEscaped = str_replace($sFrom, $sTo, (string)$sValue);
- return $sTextQualifier.$sEscaped.$sTextQualifier;
- break;
-
- case 'text':
- default:
- return parent::GetAsCSV($sValue, $sSeparator, $sTextQualifier, $oHostObject, $bLocalize, $bConvertToPlainText);
- }
- }
- }
- /**
- * Map a log to an attribute
- *
- * @package iTopORM
- */
- class AttributeLongText extends AttributeText
- {
- protected function GetSQLCol($bFullSpec = false) {return "LONGTEXT";}
- public function GetMaxSize()
- {
- // Is there a way to know the current limitation for mysql?
- // See mysql_field_len()
- return 65535*1024; // Limited... still 64 Mb!
- }
- }
- /**
- * An attibute that stores a case log (i.e journal)
- *
- * @package iTopORM
- */
- class AttributeCaseLog extends AttributeLongText
- {
- public function GetNullValue()
- {
- return '';
- }
- public function IsNull($proposedValue)
- {
- if (!($proposedValue instanceof ormCaseLog))
- {
- return ($proposedValue == '');
- }
- return ($proposedValue->GetText() == '');
- }
- public function ScalarToSQL($value)
- {
- if (!is_string($value) && !is_null($value))
- {
- throw new CoreWarning('Expected the attribute value to be a string', array('found_type' => gettype($value), 'value' => $value, 'class' => $this->GetCode(), 'attribute' => $this->GetHostClass()));
- }
- return $value;
- }
- public function GetEditClass() {return "CaseLog";}
- public function GetEditValue($sValue, $oHostObj = null)
- {
- if (!($sValue instanceOf ormCaseLog))
- {
- return '';
- }
- return $sValue->GetModifiedEntry();
- }
- /**
- * For fields containing a potential markup, return the value without this markup
- * @return string
- */
- public function GetAsPlainText($value, $oHostObj = null)
- {
- $value = $oObj->Get($sAttCode);
- if ($value instanceOf ormCaseLog)
- {
- return $value->GetAsPlainText();
- }
- else
- {
- return (string) $value;
- }
- }
-
- public function GetDefaultValue(DBObject $oHostObject = null) {return new ormCaseLog();}
- public function Equals($val1, $val2) {return ($val1->GetText() == $val2->GetText());}
-
- // Facilitate things: allow the user to Set the value from a string
- public function MakeRealValue($proposedValue, $oHostObj)
- {
- if ($proposedValue instanceof ormCaseLog)
- {
- // Passthrough
- $ret = $proposedValue;
- }
- else
- {
- // Append the new value if an instance of the object is supplied
- //
- $oPreviousLog = null;
- if ($oHostObj != null)
- {
- $oPreviousLog = $oHostObj->Get($this->GetCode());
- if (!is_object($oPreviousLog))
- {
- $oPreviousLog = $oHostObj->GetOriginal($this->GetCode());;
- }
-
- }
- if (is_object($oPreviousLog))
- {
- $oCaseLog = clone($oPreviousLog);
- }
- else
- {
- $oCaseLog = new ormCaseLog();
- }
- if ($proposedValue instanceof stdClass)
- {
- $oCaseLog->AddLogEntryFromJSON($proposedValue);
- }
- else
- {
- if (strlen($proposedValue) > 0)
- {
- $oCaseLog->AddLogEntry($proposedValue);
- }
- }
- $ret = $oCaseLog;
- }
- return $ret;
- }
- public function GetSQLExpressions($sPrefix = '')
- {
- if ($sPrefix == '')
- {
- $sPrefix = $this->Get('sql');
- }
- $aColumns = array();
- // Note: to optimize things, the existence of the attribute is determined by the existence of one column with an empty suffix
- $aColumns[''] = $sPrefix;
- $aColumns['_index'] = $sPrefix.'_index';
- return $aColumns;
- }
- public function FromSQLToValue($aCols, $sPrefix = '')
- {
- if (!array_key_exists($sPrefix, $aCols))
- {
- $sAvailable = implode(', ', array_keys($aCols));
- throw new MissingColumnException("Missing column '$sPrefix' from {$sAvailable}");
- }
- $sLog = $aCols[$sPrefix];
- if (isset($aCols[$sPrefix.'_index']))
- {
- $sIndex = $aCols[$sPrefix.'_index'];
- }
- else
- {
- // For backward compatibility, allow the current state to be: 1 log, no index
- $sIndex = '';
- }
- if (strlen($sIndex) > 0)
- {
- $aIndex = unserialize($sIndex);
- $value = new ormCaseLog($sLog, $aIndex);
- }
- else
- {
- $value = new ormCaseLog($sLog);
- }
- return $value;
- }
- public function GetSQLValues($value)
- {
- if (!($value instanceOf ormCaseLog))
- {
- $value = new ormCaseLog('');
- }
- $aValues = array();
- $aValues[$this->GetCode()] = $value->GetText();
- $aValues[$this->GetCode().'_index'] = serialize($value->GetIndex());
- return $aValues;
- }
- public function GetSQLColumns($bFullSpec = false)
- {
- $aColumns = array();
- $aColumns[$this->GetCode()] = 'LONGTEXT'; // 2^32 (4 Gb)
- $aColumns[$this->GetCode().'_index'] = 'BLOB';
- return $aColumns;
- }
- public function GetAsHTML($value, $oHostObject = null, $bLocalize = true)
- {
- if ($value instanceOf ormCaseLog)
- {
- $sContent = $value->GetAsHTML(null, false, array(__class__, 'RenderWikiHtml'));
- }
- else
- {
- $sContent = '';
- }
- $aStyles = array();
- if ($this->GetWidth() != '')
- {
- $aStyles[] = 'width:'.$this->GetWidth();
- }
- if ($this->GetHeight() != '')
- {
- $aStyles[] = 'height:'.$this->GetHeight();
- }
- $sStyle = '';
- if (count($aStyles) > 0)
- {
- $sStyle = 'style="'.implode(';', $aStyles).'"';
- }
- return "<div class=\"caselog\" $sStyle>".$sContent.'</div>'; }
- public function GetAsCSV($value, $sSeparator = ',', $sTextQualifier = '"', $oHostObject = null, $bLocalize = true, $bConvertToPlainText = false)
- {
- if ($value instanceOf ormCaseLog)
- {
- return parent::GetAsCSV($value->GetText($bConvertToPlainText), $sSeparator, $sTextQualifier, $oHostObject, $bLocalize, $bConvertToPlainText);
- }
- else
- {
- return '';
- }
- }
-
- public function GetAsXML($value, $oHostObject = null, $bLocalize = true)
- {
- if ($value instanceOf ormCaseLog)
- {
- return parent::GetAsXML($value->GetText(), $oHostObject, $bLocalize);
- }
- else
- {
- return '';
- }
- }
- /**
- * List the available verbs for 'GetForTemplate'
- */
- public function EnumTemplateVerbs()
- {
- return array(
- '' => 'Plain text representation of all the log entries',
- 'head' => 'Plain text representation of the latest entry',
- 'head_html' => 'HTML representation of the latest entry',
- 'html' => 'HTML representation of all the log entries',
- );
- }
- /**
- * Get various representations of the value, for insertion into a template (e.g. in Notifications)
- * @param $value mixed The current value of the field
- * @param $sVerb string The verb specifying the representation of the value
- * @param $oHostObject DBObject The object
- * @param $bLocalize bool Whether or not to localize the value
- */
- public function GetForTemplate($value, $sVerb, $oHostObject = null, $bLocalize = true)
- {
- switch($sVerb)
- {
- case '':
- return $value->GetText();
-
- case 'head':
- return $value->GetLatestEntry();
- case 'head_html':
- return '<div class="caselog_entry">'.str_replace( array( "\r\n", "\n", "\r"), "<br/>", htmlentities($value->GetLatestEntry(), ENT_QUOTES, 'UTF-8')).'</div>';
-
- case 'html':
- return $value->GetAsEmailHtml();
-
- default:
- throw new Exception("Unknown verb '$sVerb' for attribute ".$this->GetCode().' in class '.get_class($oHostObj));
- }
- }
-
- /**
- * Helper to get a value that will be JSON encoded
- * The operation is the opposite to FromJSONToValue
- */
- public function GetForJSON($value)
- {
- return $value->GetForJSON();
- }
- /**
- * Helper to form a value, given JSON decoded data
- * The operation is the opposite to GetForJSON
- */
- public function FromJSONToValue($json)
- {
- if (is_string($json))
- {
- // Will be correctly handled in MakeRealValue
- $ret = $json;
- }
- else
- {
- if (isset($json->add_item))
- {
- // Will be correctly handled in MakeRealValue
- $ret = $json->add_item;
- if (!isset($ret->message))
- {
- throw new Exception("Missing mandatory entry: 'message'");
- }
- }
- else
- {
- $ret = ormCaseLog::FromJSON($json);
- }
- }
- return $ret;
- }
-
- public function Fingerprint($value)
- {
- $sFingerprint = '';
- if ($value instanceOf ormCaseLog)
- {
- $sFingerprint = $value->GetText();
- }
- return $sFingerprint;
- }
-
- /**
- * The actual formatting of the text: either text (=plain text) or html (= text with HTML markup)
- * @return string
- */
- public function GetFormat()
- {
- return $this->GetOptional('format', 'html'); // default format for case logs is now HTML
- }
- static public function GetFormFieldClass()
- {
- return '\\Combodo\\iTop\\Form\\Field\\CaseLogField';
- }
- public function MakeFormField(DBObject $oObject, $oFormField = null)
- {
- // First we call the parent so the field is build
- $oFormField = parent::MakeFormField($oObject, $oFormField);
- // Then only we set the value
- $oFormField->SetCurrentValue($this->GetEditValue($oObject->Get($this->GetCode())));
- // And we set the entries
- $oFormField->SetEntries($oObject->Get($this->GetCode())->GetAsArray());
- return $oFormField;
- }
- }
- /**
- * Map a text column (size > ?), containing HTML code, to an attribute
- *
- * @package iTopORM
- */
- class AttributeHTML extends AttributeLongText
- {
- public function GetSQLColumns($bFullSpec = false)
- {
- $aColumns = array();
- $aColumns[$this->GetCode()] = $this->GetSQLCol();
- if ($this->GetOptional('format', null) != null )
- {
- // Add the extra column only if the property 'format' is specified for the attribute
- $aColumns[$this->Get('sql').'_format'] = "ENUM('text','html')";
- if ($bFullSpec)
- {
- $aColumns[$this->Get('sql').'_format'].= " DEFAULT 'html'"; // default 'html' is for migrating old records
- }
- }
- return $aColumns;
- }
- /**
- * The actual formatting of the text: either text (=plain text) or html (= text with HTML markup)
- * @return string
- */
- public function GetFormat()
- {
- return $this->GetOptional('format', 'html'); // Defaults to HTML
- }
- }
- /**
- * Specialization of a string: email
- *
- * @package iTopORM
- */
- class AttributeEmailAddress extends AttributeString
- {
- public function GetValidationPattern()
- {
- return $this->GetOptional('validation_pattern', '^'.utils::GetConfig()->Get('email_validation_pattern').'$');
- }
- public function GetAsHTML($sValue, $oHostObject = null, $bLocalize = true)
- {
- if (empty($sValue)) return '';
- return '<a class="mailto" href="mailto:'.$sValue.'">'.parent::GetAsHTML($sValue).'</a>';
- }
- }
- /**
- * Specialization of a string: IP address
- *
- * @package iTopORM
- */
- class AttributeIPAddress extends AttributeString
- {
- public function GetValidationPattern()
- {
- $sNum = '(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])';
- return "^($sNum\\.$sNum\\.$sNum\\.$sNum)$";
- }
- public function GetOrderBySQLExpressions($sClassAlias)
- {
- // Note: This is the responsibility of this function to place backticks around column aliases
- return array('INET_ATON(`'.$sClassAlias.$this->GetCode().'`)');
- }
- }
- /**
- * Specialization of a string: OQL expression
- *
- * @package iTopORM
- */
- class AttributeOQL extends AttributeText
- {
- public function GetEditClass() {return "OQLExpression";}
- }
- /**
- * Specialization of a string: template (contains iTop placeholders like $current_contact_id$ or $this->name$)
- *
- * @package iTopORM
- */
- class AttributeTemplateString extends AttributeString
- {
- }
- /**
- * Specialization of a text: template (contains iTop placeholders like $current_contact_id$ or $this->name$)
- *
- * @package iTopORM
- */
- class AttributeTemplateText extends AttributeText
- {
- }
- /**
- * Specialization of a HTML: template (contains iTop placeholders like $current_contact_id$ or $this->name$)
- *
- * @package iTopORM
- */
- class AttributeTemplateHTML extends AttributeText
- {
- public function GetSQLColumns($bFullSpec = false)
- {
- $aColumns = array();
- $aColumns[$this->GetCode()] = $this->GetSQLCol();
- if ($this->GetOptional('format', null) != null )
- {
- // Add the extra column only if the property 'format' is specified for the attribute
- $aColumns[$this->Get('sql').'_format'] = "ENUM('text','html')";
- if ($bFullSpec)
- {
- $aColumns[$this->Get('sql').'_format'].= " DEFAULT 'html'"; // default 'html' is for migrating old records
- }
- }
- return $aColumns;
- }
- /**
- * The actual formatting of the text: either text (=plain text) or html (= text with HTML markup)
- * @return string
- */
- public function GetFormat()
- {
- return $this->GetOptional('format', 'html'); // Defaults to HTML
- }
- }
- /**
- * Map a enum column to an attribute
- *
- * @package iTopORM
- */
- class AttributeEnum extends AttributeString
- {
- static public function ListExpectedParams()
- {
- return parent::ListExpectedParams();
- //return array_merge(parent::ListExpectedParams(), array());
- }
- public function GetEditClass() {return "String";}
- protected function GetSQLCol($bFullSpec = false)
- {
- $oValDef = $this->GetValuesDef();
- if ($oValDef)
- {
- $aValues = CMDBSource::Quote(array_keys($oValDef->GetValues(array(), "")), true);
- }
- else
- {
- $aValues = array();
- }
- if (count($aValues) > 0)
- {
- // The syntax used here do matters
- // In particular, I had to remove unnecessary spaces to
- // make sure that this string will match the field type returned by the DB
- // (used to perform a comparison between the current DB format and the data model)
- return "ENUM(".implode(",", $aValues).")".($bFullSpec ? $this->GetSQLColSpec() : '');
- }
- else
- {
- return "VARCHAR(255)".($bFullSpec ? " DEFAULT ''" : ""); // ENUM() is not an allowed syntax!
- }
- }
-
- protected function GetSQLColSpec()
- {
- $default = $this->ScalarToSQL($this->GetDefaultValue());
- if (is_null($default))
- {
- $sRet = '';
- }
- else
- {
- // ENUMs values are strings so the default value must be a string as well,
- // otherwise MySQL interprets the number as the zero-based index of the value in the list (i.e. the nth value in the list)
- $sRet = " DEFAULT ".CMDBSource::Quote($default);
- }
- return $sRet;
- }
- public function ScalarToSQL($value)
- {
- // Note: for strings, the null value is an empty string and it is recorded as such in the DB
- // but that wasn't working for enums, because '' is NOT one of the allowed values
- // that's why a null value must be forced to a real null
- $value = parent::ScalarToSQL($value);
- if ($this->IsNull($value))
- {
- return null;
- }
- else
- {
- return $value;
- }
- }
- public function RequiresIndex()
- {
- return false;
- }
- public function GetBasicFilterOperators()
- {
- return parent::GetBasicFilterOperators();
- }
- public function GetBasicFilterLooseOperator()
- {
- return '=';
- }
- public function GetBasicFilterSQLExpr($sOpCode, $value)
- {
- return parent::GetBasicFilterSQLExpr($sOpCode, $value);
- }
- public function GetValueLabel($sValue)
- {
- if (is_null($sValue))
- {
- // Unless a specific label is defined for the null value of this enum, use a generic "undefined" label
- $sLabel = Dict::S('Class:'.$this->GetHostClass().'/Attribute:'.$this->GetCode().'/Value:'.$sValue, Dict::S('Enum:Undefined'));
- }
- else
- {
- $sLabel = $this->SearchLabel('/Attribute:'.$this->m_sCode.'/Value:'.$sValue, null, true /*user lang*/);
- if (is_null($sLabel))
- {
- $sDefault = str_replace('_', ' ', $sValue);
- // Browse the hierarchy again, accepting default (english) translations
- $sLabel = $this->SearchLabel('/Attribute:'.$this->m_sCode.'/Value:'.$sValue, $sDefault, false);
- }
- }
- return $sLabel;
- }
- public function GetValueDescription($sValue)
- {
- if (is_null($sValue))
- {
- // Unless a specific label is defined for the null value of this enum, use a generic "undefined" label
- $sDescription = Dict::S('Class:'.$this->GetHostClass().'/Attribute:'.$this->GetCode().'/Value:'.$sValue.'+', Dict::S('Enum:Undefined'));
- }
- else
- {
- $sDescription = Dict::S('Class:'.$this->GetHostClass().'/Attribute:'.$this->GetCode().'/Value:'.$sValue.'+', '', true /* user language only */);
- if (strlen($sDescription) == 0)
- {
- $sParentClass = MetaModel::GetParentClass($this->m_sHostClass);
- if ($sParentClass)
- {
- if (MetaModel::IsValidAttCode($sParentClass, $this->m_sCode))
- {
- $oAttDef = MetaModel::GetAttributeDef($sParentClass, $this->m_sCode);
- $sDescription = $oAttDef->GetValueDescription($sValue);
- }
- }
- }
- }
- return $sDescription;
- }
- public function GetAsHTML($sValue, $oHostObject = null, $bLocalize = true)
- {
- if ($bLocalize)
- {
- $sLabel = $this->GetValueLabel($sValue);
- $sDescription = $this->GetValueDescription($sValue);
- // later, we could imagine a detailed description in the title
- $sRes = "<span title=\"$sDescription\">".parent::GetAsHtml($sLabel)."</span>";
- }
- else
- {
- $sRes = parent::GetAsHtml($sValue, $oHostObject, $bLocalize);
- }
- return $sRes;
- }
- public function GetAsXML($value, $oHostObject = null, $bLocalize = true)
- {
- if (is_null($value))
- {
- $sFinalValue = '';
- }
- elseif ($bLocalize)
- {
- $sFinalValue = $this->GetValueLabel($value);
- }
- else
- {
- $sFinalValue = $value;
- }
- $sRes = parent::GetAsXML($sFinalValue, $oHostObject, $bLocalize);
- return $sRes;
- }
- public function GetAsCSV($sValue, $sSeparator = ',', $sTextQualifier = '"', $oHostObject = null, $bLocalize = true, $bConvertToPlainText = false)
- {
- if (is_null($sValue))
- {
- $sFinalValue = '';
- }
- elseif ($bLocalize)
- {
- $sFinalValue = $this->GetValueLabel($sValue);
- }
- else
- {
- $sFinalValue = $sValue;
- }
- $sRes = parent::GetAsCSV($sFinalValue, $sSeparator, $sTextQualifier, $oHostObject, $bLocalize);
- return $sRes;
- }
- static public function GetFormFieldClass()
- {
- return '\\Combodo\\iTop\\Form\\Field\\SelectField';
- }
- public function MakeFormField(DBObject $oObject, $oFormField = null)
- {
- if ($oFormField === null)
- {
- // TODO : We should check $this->Get('display_style') and create a Radio / Select / ... regarding its value
- $sFormFieldClass = static::GetFormFieldClass();
- $oFormField = new $sFormFieldClass($this->GetCode());
- }
- $oFormField->SetChoices($this->GetAllowedValues($oObject->ToArgsForQuery()));
- parent::MakeFormField($oObject, $oFormField);
- return $oFormField;
- }
- public function GetEditValue($sValue, $oHostObj = null)
- {
- if (is_null($sValue))
- {
- return '';
- }
- else
- {
- return $this->GetValueLabel($sValue);
- }
- }
- /**
- * Helper to get a value that will be JSON encoded
- * The operation is the opposite to FromJSONToValue
- */
- public function GetForJSON($value)
- {
- return $value;
- }
- public function GetAllowedValues($aArgs = array(), $sContains = '')
- {
- $aRawValues = parent::GetAllowedValues($aArgs, $sContains);
- if (is_null($aRawValues)) return null;
- $aLocalizedValues = array();
- foreach ($aRawValues as $sKey => $sValue)
- {
- $aLocalizedValues[$sKey] = Str::pure2html($this->GetValueLabel($sKey));
- }
- return $aLocalizedValues;
- }
-
- /**
- * An enum can be localized
- */
- public function MakeValueFromString($sProposedValue, $bLocalizedValue = false, $sSepItem = null, $sSepAttribute = null, $sSepValue = null, $sAttributeQualifier = null)
- {
- if ($bLocalizedValue)
- {
- // Lookup for the value matching the input
- //
- $sFoundValue = null;
- $aRawValues = parent::GetAllowedValues();
- if (!is_null($aRawValues))
- {
- foreach ($aRawValues as $sKey => $sValue)
- {
- $sRefValue = $this->GetValueLabel($sKey);
- if ($sProposedValue == $sRefValue)
- {
- $sFoundValue = $sKey;
- break;
- }
- }
- }
- if (is_null($sFoundValue))
- {
- return null;
- }
- return $this->MakeRealValue($sFoundValue, null);
- }
- else
- {
- return parent::MakeValueFromString($sProposedValue, $bLocalizedValue, $sSepItem, $sSepAttribute, $sSepValue, $sAttributeQualifier);
- }
- }
- /**
- * Processes the input value to align it with the values supported
- * by this type of attribute. In this case: turns empty strings into nulls
- * @param mixed $proposedValue The value to be set for the attribute
- * @return mixed The actual value that will be set
- */
- public function MakeRealValue($proposedValue, $oHostObj)
- {
- if ($proposedValue == '') return null;
- return parent::MakeRealValue($proposedValue, $oHostObj);
- }
-
- public function GetOrderByHint()
- {
- $aValues = $this->GetAllowedValues();
-
- return Dict::Format('UI:OrderByHint_Values', implode(', ', $aValues));
- }
- }
- /**
- * A meta enum is an aggregation of enum from subclasses into an enum of a base class
- * It has been designed is to cope with the fact that statuses must be defined in leaf classes, while it makes sense to
- * have a superstatus available on the root classe(s)
- *
- * @package iTopORM
- */
- class AttributeMetaEnum extends AttributeEnum
- {
- static public function ListExpectedParams()
- {
- return array('allowed_values', 'sql', 'default_value', 'mapping');
- }
- public function IsWritable()
- {
- return false;
- }
- public function RequiresIndex()
- {
- return true;
- }
- public function GetPrerequisiteAttributes($sClass = null)
- {
- if (is_null($sClass))
- {
- $sClass = $this->GetHostClass();
- }
- $aMappingData = $this->GetMapRule($sClass);
- if ($aMappingData == null)
- {
- $aRet = array();
- }
- else
- {
- $aRet = array($aMappingData['attcode']);
- }
- return $aRet;
- }
- /**
- * Overload the standard so as to leave the data unsorted
- *
- * @param array $aArgs
- * @param string $sContains
- * @return array|null
- */
- public function GetAllowedValues($aArgs = array(), $sContains = '')
- {
- $oValSetDef = $this->GetValuesDef();
- if (!$oValSetDef) return null;
- $aRawValues = $oValSetDef->GetValueList();
- if (is_null($aRawValues)) return null;
- $aLocalizedValues = array();
- foreach ($aRawValues as $sKey => $sValue)
- {
- $aLocalizedValues[$sKey] = Str::pure2html($this->GetValueLabel($sKey));
- }
- return $aLocalizedValues;
- }
- /**
- * Returns the meta value for the given object.
- * See also MetaModel::RebuildMetaEnums() that must be maintained when MapValue changes
- *
- * @param $oObject
- * @return mixed
- * @throws Exception
- */
- public function MapValue($oObject)
- {
- $aMappingData = $this->GetMapRule(get_class($oObject));
- if ($aMappingData == null)
- {
- $sRet = $this->GetDefaultValue();
- }
- else
- {
- $sAttCode = $aMappingData['attcode'];
- $value = $oObject->Get($sAttCode);
- if (array_key_exists($value, $aMappingData['values']))
- {
- $sRet = $aMappingData['values'][$value];
- }
- elseif ($this->GetDefaultValue() != '')
- {
- $sRet = $this->GetDefaultValue();
- }
- else
- {
- throw new Exception('AttributeMetaEnum::MapValue(): mapping not found for value "'.$value.'" in '.get_class($oObject).', on attribute '.MetaModel::GetAttributeOrigin($this->GetHostClass(), $this->GetCode()).'::'.$this->GetCode());
- }
- }
- return $sRet;
- }
- public function GetMapRule($sClass)
- {
- $aMappings = $this->Get('mapping');
- if (array_key_exists($sClass, $aMappings))
- {
- $aMappingData = $aMappings[$sClass];
- }
- else
- {
- $sParent = MetaModel::GetParentClass($sClass);
- $aMappingData = $this->GetMapRule($sParent);
- }
- return $aMappingData;
- }
- }
- /**
- * Map a date+time column to an attribute
- *
- * @package iTopORM
- */
- class AttributeDateTime extends AttributeDBField
- {
- static $oFormat = null;
-
- static public function GetFormat()
- {
- if (self::$oFormat == null)
- {
- static::LoadFormatFromConfig();
- }
- return self::$oFormat;
- }
-
- /**
- * Load the 3 settings: date format, time format and data_time format from the configuration
- */
- protected static function LoadFormatFromConfig()
- {
- $aFormats = MetaModel::GetConfig()->Get('date_and_time_format');
- $sLang = Dict::GetUserLanguage();
- $sDateFormat = isset($aFormats[$sLang]['date']) ? $aFormats[$sLang]['date'] : (isset($aFormats['default']['date']) ? $aFormats['default']['date'] : 'Y-m-d');
- $sTimeFormat = isset($aFormats[$sLang]['time']) ? $aFormats[$sLang]['time'] : (isset($aFormats['default']['time']) ? $aFormats['default']['time'] : 'H:i:s');
- $sDateAndTimeFormat = isset($aFormats[$sLang]['date_time']) ? $aFormats[$sLang]['date_time'] : (isset($aFormats['default']['date_time']) ? $aFormats['default']['date_time'] : '$date $time');
-
- $sFullFormat = str_replace(array('$date', '$time'), array($sDateFormat, $sTimeFormat), $sDateAndTimeFormat);
-
- self::SetFormat(new DateTimeFormat($sFullFormat));
- AttributeDate::SetFormat(new DateTimeFormat($sDateFormat));
- }
-
- /**
- * Returns the format string used for the date & time stored in memory
- * @return string
- */
- static public function GetInternalFormat()
- {
- return 'Y-m-d H:i:s';
- }
- /**
- * Returns the format string used for the date & time written to MySQL
- * @return string
- */
- static public function GetSQLFormat()
- {
- return 'Y-m-d H:i:s';
- }
-
- static public function SetFormat(DateTimeFormat $oDateTimeFormat)
- {
- self::$oFormat = $oDateTimeFormat;
- }
-
- static public function GetSQLTimeFormat()
- {
- return 'H:i:s';
- }
-
- /**
- * Parses a search string coming from user input
- * @param string $sSearchString
- * @return string
- */
- public function ParseSearchString($sSearchString)
- {
- try
- {
- $oDateTime = $this->GetFormat()->Parse($sSearchString);
- $sSearchString = $oDateTime->format($this->GetInternalFormat());
- }
- catch(Exception $e)
- {
- $sFormatString = '!'.(string)AttributeDate::GetFormat(); // BEWARE: ! is needed to set non-parsed fields to zero !!!
- $oDateTime = DateTime::createFromFormat($sFormatString, $sSearchString);
- if ($oDateTime !== false)
- {
- $sSearchString = $oDateTime->format($this->GetInternalFormat());
- }
- }
- return $sSearchString;
- }
-
- static public function GetFormFieldClass()
- {
- return '\\Combodo\\iTop\\Form\\Field\\DateTimeField';
- }
-
- /**
- * Override to specify Field class
- *
- * When called first, $oFormField is null and will be created (eg. Make). Then when the ::parent is called and the $oFormField is passed, MakeFormField behave more like a Prepare.
- */
- public function MakeFormField(DBObject $oObject, $oFormField = null)
- {
- $oFormField = parent::MakeFormField($oObject, $oFormField);
- $oFormField->SetPHPDateTimeFormat((string) $this->GetFormat());
- $oFormField->SetJSDateTimeFormat($this->GetFormat()->ToMomentJS());
- return $oFormField;
- }
- static public function ListExpectedParams()
- {
- return parent::ListExpectedParams();
- //return array_merge(parent::ListExpectedParams(), array());
- }
- public function GetEditClass() {return "DateTime";}
- public function GetEditValue($sValue, $oHostObj = null)
- {
- return (string)static::GetFormat()->format($sValue);
- }
- public function GetValueLabel($sValue, $oHostObj = null)
- {
- return (string)static::GetFormat()->format($sValue);
- }
-
- protected function GetSQLCol($bFullSpec = false) {return "DATETIME";}
- public static function GetAsUnixSeconds($value)
- {
- $oDeadlineDateTime = new DateTime($value);
- $iUnixSeconds = $oDeadlineDateTime->format('U');
- return $iUnixSeconds;
- }
- public function GetDefaultValue(DBObject $oHostObject = null)
- {
- // null value will be replaced by the current date, if not already set, in DoComputeValues
- return $this->GetNullValue();
- }
- public function GetValidationPattern()
- {
- return static::GetFormat()->ToRegExpr();
- }
- public function GetBasicFilterOperators()
- {
- return array(
- "="=>"equals",
- "!="=>"differs from",
- "<"=>"before",
- "<="=>"before",
- ">"=>"after (strictly)",
- ">="=>"after",
- "SameDay"=>"same day (strip time)",
- "SameMonth"=>"same year/month",
- "SameYear"=>"same year",
- "Today"=>"today",
- ">|"=>"after today + N days",
- "<|"=>"before today + N days",
- "=|"=>"equals today + N days",
- );
- }
- public function GetBasicFilterLooseOperator()
- {
- // Unless we implement a "same xxx, depending on given precision" !
- return "=";
- }
- public function GetBasicFilterSQLExpr($sOpCode, $value)
- {
- $sQValue = CMDBSource::Quote($value);
- switch ($sOpCode)
- {
- case '=':
- case '!=':
- case '<':
- case '<=':
- case '>':
- case '>=':
- return $this->GetSQLExpr()." $sOpCode $sQValue";
- case 'SameDay':
- return "DATE(".$this->GetSQLExpr().") = DATE($sQValue)";
- case 'SameMonth':
- return "DATE_FORMAT(".$this->GetSQLExpr().", '%Y-%m') = DATE_FORMAT($sQValue, '%Y-%m')";
- case 'SameYear':
- return "MONTH(".$this->GetSQLExpr().") = MONTH($sQValue)";
- case 'Today':
- return "DATE(".$this->GetSQLExpr().") = CURRENT_DATE()";
- case '>|':
- return "DATE(".$this->GetSQLExpr().") > DATE_ADD(CURRENT_DATE(), INTERVAL $sQValue DAY)";
- case '<|':
- return "DATE(".$this->GetSQLExpr().") < DATE_ADD(CURRENT_DATE(), INTERVAL $sQValue DAY)";
- case '=|':
- return "DATE(".$this->GetSQLExpr().") = DATE_ADD(CURRENT_DATE(), INTERVAL $sQValue DAY)";
- default:
- return $this->GetSQLExpr()." = $sQValue";
- }
- }
-
- public function MakeRealValue($proposedValue, $oHostObj)
- {
- if (is_null($proposedValue))
- {
- return null;
- }
- if (is_string($proposedValue) && ($proposedValue == "") && $this->IsNullAllowed())
- {
- return null;
- }
- if (!is_numeric($proposedValue))
- {
- return $proposedValue;
- }
- return date(static::GetInternalFormat(), $proposedValue);
- }
- public function ScalarToSQL($value)
- {
- if (is_null($value))
- {
- return null;
- }
- elseif (empty($value))
- {
- // Make a valid date for MySQL. TO DO: support NULL as a literal value for fields that can be null.
- return '0000-00-00 00:00:00';
- }
- return $value;
- }
- public function GetAsHTML($value, $oHostObject = null, $bLocalize = true)
- {
- return Str::pure2html(static::GetFormat()->format($value));
- }
- public function GetAsXML($value, $oHostObject = null, $bLocalize = true)
- {
- return Str::pure2xml($value);
- }
- public function GetAsCSV($sValue, $sSeparator = ',', $sTextQualifier = '"', $oHostObject = null, $bLocalize = true, $bConvertToPlainText = false)
- {
- if (empty($sValue) || ($sValue === '0000-00-00 00:00:00') || ($sValue === '0000-00-00'))
- {
- return '';
- }
- else if ((string)static::GetFormat() !== static::GetInternalFormat())
- {
- // Format conversion
- $oDate = new DateTime($sValue);
- if ($oDate !== false)
- {
- $sValue = static::GetFormat()->format($oDate);
- }
- }
- $sFrom = array("\r\n", $sTextQualifier);
- $sTo = array("\n", $sTextQualifier.$sTextQualifier);
- $sEscaped = str_replace($sFrom, $sTo, (string)$sValue);
- return $sTextQualifier.$sEscaped.$sTextQualifier;
- }
-
- /**
- * Parses a string to find some smart search patterns and build the corresponding search/OQL condition
- * Each derived class is reponsible for defining and processing their own smart patterns, the base class
- * does nothing special, and just calls the default (loose) operator
- * @param string $sSearchText The search string to analyze for smart patterns
- * @param FieldExpression The FieldExpression representing the atttribute code in this OQL query
- * @param Hash $aParams Values of the query parameters
- * @return Expression The search condition to be added (AND) to the current search
- */
- public function GetSmartConditionExpression($sSearchText, FieldExpression $oField, &$aParams, $bParseSearchString = false)
- {
- // Possible smart patterns
- $aPatterns = array(
- 'between' => array('pattern' => '/^\[(.*),(.*)\]$/', 'operator' => 'n/a'),
- 'greater than or equal' => array('pattern' => '/^>=(.*)$/', 'operator' => '>='),
- 'greater than' => array('pattern' => '/^>(.*)$/', 'operator' => '>'),
- 'less than or equal' => array('pattern' => '/^<=(.*)$/', 'operator' => '<='),
- 'less than' => array('pattern' => '/^<(.*)$/', 'operator' => '<'),
- );
-
- $sPatternFound = '';
- $aMatches = array();
- foreach($aPatterns as $sPatName => $sPattern)
- {
- if (preg_match($sPattern['pattern'], $sSearchText, $aMatches))
- {
- $sPatternFound = $sPatName;
- break;
- }
- }
-
- switch($sPatternFound)
- {
- case 'between':
-
- $sParamName1 = $oField->GetParent().'_'.$oField->GetName().'_1';
- $oRightExpr = new VariableExpression($sParamName1);
- if ($bParseSearchString)
- {
- $aParams[$sParamName1] = $this->ParseSearchString($aMatches[1]);
- }
- else
- {
- $aParams[$sParamName1] = $aMatches[1];
- }
- $oCondition1 = new BinaryExpression($oField, '>=', $oRightExpr);
- $sParamName2 = $oField->GetParent().'_'.$oField->GetName().'_2';
- $oRightExpr = new VariableExpression($sParamName2);
- $sOperator = $this->GetBasicFilterLooseOperator();
- if ($bParseSearchString)
- {
- $aParams[$sParamName2] = $this->ParseSearchString($aMatches[2]);
- }
- else
- {
- $aParams[$sParamName2] = $aMatches[2];
- }
- $oCondition2 = new BinaryExpression($oField, '<=', $oRightExpr);
-
- $oNewCondition = new BinaryExpression($oCondition1, 'AND', $oCondition2);
- break;
-
- case 'greater than':
- case 'greater than or equal':
- case 'less than':
- case 'less than or equal':
- $sSQLOperator = $aPatterns[$sPatternFound]['operator'];
- $sParamName = $oField->GetParent().'_'.$oField->GetName();
- $oRightExpr = new VariableExpression($sParamName);
- if ($bParseSearchString)
- {
- $aParams[$sParamName] = $this->ParseSearchString($aMatches[1]);
- }
- else
- {
- $aParams[$sParamName] = $aMatches[1];
- }
- $oNewCondition = new BinaryExpression($oField, $sSQLOperator, $oRightExpr);
-
- break;
-
- default:
- $oNewCondition = parent::GetSmartConditionExpression($sSearchText, $oField, $aParams, $bParseSearchString);
- }
- return $oNewCondition;
- }
- public function GetHelpOnSmartSearch()
- {
- $sDict = parent::GetHelpOnSmartSearch();
-
- $oFormat = static::GetFormat();
- $sExample = $oFormat->Format(new DateTime('2015-07-19 18:40:00'));
- return vsprintf($sDict, array($oFormat->ToPlaceholder(), $sExample));
- }
- }
- /**
- * Store a duration as a number of seconds
- *
- * @package iTopORM
- */
- class AttributeDuration extends AttributeInteger
- {
- public function GetEditClass() {return "Duration";}
- protected function GetSQLCol($bFullSpec = false) {return "INT(11) UNSIGNED";}
- public function GetNullValue() {return '0';}
- public function MakeRealValue($proposedValue, $oHostObj)
- {
- if (is_null($proposedValue)) return null;
- if (!is_numeric($proposedValue)) return null;
- if ( ((int)$proposedValue) < 0) return null;
- return (int)$proposedValue;
- }
- public function ScalarToSQL($value)
- {
- if (is_null($value))
- {
- return null;
- }
- return $value;
- }
- public function GetAsHTML($value, $oHostObject = null, $bLocalize = true)
- {
- return Str::pure2html(self::FormatDuration($value));
- }
- public static function FormatDuration($duration)
- {
- $aDuration = self::SplitDuration($duration);
- $sResult = '';
-
- if ($duration < 60)
- {
- // Less than 1 min
- $sResult = Dict::Format('Core:Duration_Seconds', $aDuration['seconds']);
- }
- else if ($duration < 3600)
- {
- // less than 1 hour, display it in minutes/seconds
- $sResult = Dict::Format('Core:Duration_Minutes_Seconds', $aDuration['minutes'], $aDuration['seconds']);
- }
- else if ($duration < 86400)
- {
- // Less than 1 day, display it in hours/minutes/seconds
- $sResult = Dict::Format('Core:Duration_Hours_Minutes_Seconds', $aDuration['hours'], $aDuration['minutes'], $aDuration['seconds']);
- }
- else
- {
- // more than 1 day, display it in days/hours/minutes/seconds
- $sResult = Dict::Format('Core:Duration_Days_Hours_Minutes_Seconds', $aDuration['days'], $aDuration['hours'], $aDuration['minutes'], $aDuration['seconds']);
- }
- return $sResult;
- }
-
- static function SplitDuration($duration)
- {
- $duration = (int) $duration;
- $days = floor($duration / 86400);
- $hours = floor(($duration - (86400*$days)) / 3600);
- $minutes = floor(($duration - (86400*$days + 3600*$hours)) / 60);
- $seconds = ($duration % 60); // modulo
- return array( 'days' => $days, 'hours' => $hours, 'minutes' => $minutes, 'seconds' => $seconds );
- }
- }
- /**
- * Map a date+time column to an attribute
- *
- * @package iTopORM
- */
- class AttributeDate extends AttributeDateTime
- {
- static $oDateFormat = null;
-
- static public function GetFormat()
- {
- if (self::$oDateFormat == null)
- {
- AttributeDateTime::LoadFormatFromConfig();
- }
- return self::$oDateFormat;
- }
- static public function SetFormat(DateTimeFormat $oDateFormat)
- {
- self::$oDateFormat = $oDateFormat;
- }
- /**
- * Returns the format string used for the date & time stored in memory
- * @return string
- */
- static public function GetInternalFormat()
- {
- return 'Y-m-d';
- }
- /**
- * Returns the format string used for the date & time written to MySQL
- * @return string
- */
- static public function GetSQLFormat()
- {
- return 'Y-m-d';
- }
-
- static public function ListExpectedParams()
- {
- return parent::ListExpectedParams();
- //return array_merge(parent::ListExpectedParams(), array());
- }
- public function GetEditClass() {return "Date";}
- protected function GetSQLCol($bFullSpec = false) {return "DATE";}
- }
- /**
- * A dead line stored as a date & time
- * The only difference with the DateTime attribute is the display:
- * relative to the current time
- */
- class AttributeDeadline extends AttributeDateTime
- {
- public function GetAsHTML($value, $oHostObject = null, $bLocalize = true)
- {
- $sResult = self::FormatDeadline($value);
- return $sResult;
- }
- public static function FormatDeadline($value)
- {
- $sResult = '';
- if ($value !== null)
- {
- $iValue = AttributeDateTime::GetAsUnixSeconds($value);
- $sDate = AttributeDateTime::Format($value);
- $difference = $iValue - time();
-
- if ($difference >= 0)
- {
- $sDifference = self::FormatDuration($difference);
- }
- else
- {
- $sDifference = Dict::Format('UI:DeadlineMissedBy_duration', self::FormatDuration(-$difference));
- }
- $sFormat = MetaModel::GetConfig()->Get('deadline_format', '$difference$');
- $sResult = str_replace(array('$date$', '$difference$'), array($sDate, $sDifference), $sFormat);
- }
- return $sResult;
- }
- static function FormatDuration($duration)
- {
- $days = floor($duration / 86400);
- $hours = floor(($duration - (86400*$days)) / 3600);
- $minutes = floor(($duration - (86400*$days + 3600*$hours)) / 60);
- $sResult = '';
-
- if ($duration < 60)
- {
- // Less than 1 min
- $sResult =Dict::S('UI:Deadline_LessThan1Min');
- }
- else if ($duration < 3600)
- {
- // less than 1 hour, display it in minutes
- $sResult =Dict::Format('UI:Deadline_Minutes', $minutes);
- }
- else if ($duration < 86400)
- {
- // Less that 1 day, display it in hours/minutes
- $sResult =Dict::Format('UI:Deadline_Hours_Minutes', $hours, $minutes);
- }
- else
- {
- // Less that 1 day, display it in hours/minutes
- $sResult =Dict::Format('UI:Deadline_Days_Hours_Minutes', $days, $hours, $minutes);
- }
- return $sResult;
- }
- }
- /**
- * Map a foreign key to an attribute
- * AttributeExternalKey and AttributeExternalField may be an external key
- * the difference is that AttributeExternalKey corresponds to a column into the defined table
- * where an AttributeExternalField corresponds to a column into another table (class)
- *
- * @package iTopORM
- */
- class AttributeExternalKey extends AttributeDBFieldVoid
- {
- static public function ListExpectedParams()
- {
- return array_merge(parent::ListExpectedParams(), array("targetclass", "is_null_allowed", "on_target_delete"));
- }
- public function GetEditClass() {return "ExtKey";}
- protected function GetSQLCol($bFullSpec = false) {return "INT(11)".($bFullSpec ? " DEFAULT 0" : "");}
- public function RequiresIndex()
- {
- return true;
- }
- public function IsExternalKey($iType = EXTKEY_RELATIVE) {return true;}
- public function GetTargetClass($iType = EXTKEY_RELATIVE) {return $this->Get("targetclass");}
- public function GetKeyAttDef($iType = EXTKEY_RELATIVE){return $this;}
- public function GetKeyAttCode() {return $this->GetCode();}
- public function GetDisplayStyle() { return $this->GetOptional('display_style', 'select'); }
-
- public function GetDefaultValue(DBObject $oHostObject = null) {return 0;}
- public function IsNullAllowed()
- {
- if (MetaModel::GetConfig()->Get('disable_mandatory_ext_keys'))
- {
- return true;
- }
- return $this->Get("is_null_allowed");
- }
- public function GetBasicFilterOperators()
- {
- return parent::GetBasicFilterOperators();
- }
- public function GetBasicFilterLooseOperator()
- {
- return parent::GetBasicFilterLooseOperator();
- }
- public function GetBasicFilterSQLExpr($sOpCode, $value)
- {
- return parent::GetBasicFilterSQLExpr($sOpCode, $value);
- }
- // overloaded here so that an ext key always have the answer to
- // "what are your possible values?"
- public function GetValuesDef()
- {
- $oValSetDef = $this->Get("allowed_values");
- if (!$oValSetDef)
- {
- // Let's propose every existing value
- $oValSetDef = new ValueSetObjects('SELECT '.$this->GetTargetClass());
- }
- return $oValSetDef;
- }
- public function GetAllowedValues($aArgs = array(), $sContains = '')
- {
- //throw new Exception("GetAllowedValues on ext key has been deprecated");
- try
- {
- return parent::GetAllowedValues($aArgs, $sContains);
- }
- catch (MissingQueryArgument $e)
- {
- // Some required arguments could not be found, enlarge to any existing value
- $oValSetDef = new ValueSetObjects('SELECT '.$this->GetTargetClass());
- return $oValSetDef->GetValues($aArgs, $sContains);
- }
- }
- public function GetAllowedValuesAsObjectSet($aArgs = array(), $sContains = '')
- {
- $oValSetDef = $this->GetValuesDef();
- $oSet = $oValSetDef->ToObjectSet($aArgs, $sContains);
- return $oSet;
- }
- public function GetDeletionPropagationOption()
- {
- return $this->Get("on_target_delete");
- }
- public function GetNullValue()
- {
- return 0;
- }
- public function IsNull($proposedValue)
- {
- return ($proposedValue == 0);
- }
- public function MakeRealValue($proposedValue, $oHostObj)
- {
- if (is_null($proposedValue)) return 0;
- if ($proposedValue === '') return 0;
- if (MetaModel::IsValidObject($proposedValue)) return $proposedValue->GetKey();
- return (int)$proposedValue;
- }
-
- public function GetMaximumComboLength()
- {
- return $this->GetOptional('max_combo_length', MetaModel::GetConfig()->Get('max_combo_length'));
- }
-
- public function GetMinAutoCompleteChars()
- {
- return $this->GetOptional('min_autocomplete_chars', MetaModel::GetConfig()->Get('min_autocomplete_chars'));
- }
-
- public function AllowTargetCreation()
- {
- return $this->GetOptional('allow_target_creation', MetaModel::GetConfig()->Get('allow_target_creation'));
- }
- /**
- * Find the corresponding "link" attribute on the target class
- *
- * @return string The attribute code on the target class, or null if none has been found
- */
- public function GetMirrorLinkAttribute()
- {
- $oRet = null;
- $sRemoteClass = $this->GetTargetClass();
- foreach (MetaModel::ListAttributeDefs($sRemoteClass) as $sRemoteAttCode => $oRemoteAttDef)
- {
- if (!$oRemoteAttDef->IsLinkSet()) continue;
- if (!is_subclass_of($this->GetHostClass(), $oRemoteAttDef->GetLinkedClass()) && $oRemoteAttDef->GetLinkedClass() != $this->GetHostClass()) continue;
- if ($oRemoteAttDef->GetExtKeyToMe() != $this->GetCode()) continue;
- $oRet = $oRemoteAttDef;
- break;
- }
- return $oRet;
- }
- static public function GetFormFieldClass()
- {
- return '\\Combodo\\iTop\\Form\\Field\\SelectObjectField';
- }
- public function MakeFormField(DBObject $oObject, $oFormField = null)
- {
- if ($oFormField === null)
- {
- // TODO : We should check $this->Get('display_style') and create a Radio / Select / ... regarding its value
- $sFormFieldClass = static::GetFormFieldClass();
- $oFormField = new $sFormFieldClass($this->GetCode());
- }
-
- // Setting params
- $oFormField->SetMaximumComboLength($this->GetMaximumComboLength());
- $oFormField->SetMinAutoCompleteChars($this->GetMinAutoCompleteChars());
- $oFormField->SetHierarchical(MetaModel::IsHierarchicalClass($this->GetTargetClass()));
- // Setting choices regarding the field dependencies
- $aFieldDependencies = $this->GetPrerequisiteAttributes();
- if (!empty($aFieldDependencies))
- {
- $oTmpAttDef = $this;
- $oTmpField = $oFormField;
- $oFormField->SetOnFinalizeCallback(function() use ($oTmpField, $oTmpAttDef, $oObject)
- {
- $oSearch = DBSearch::FromOQL($oTmpAttDef->GetValuesDef()->GetFilterExpression());
- $oSearch->SetInternalParams(array('this' => $oObject));
- $oTmpField->SetSearch($oSearch);
- });
- }
- else
- {
- $oSearch = DBSearch::FromOQL($this->GetValuesDef()->GetFilterExpression());
- $oSearch->SetInternalParams(array('this' => $oObject));
- $oFormField->SetSearch($oSearch);
- }
- // If ExtKey is mandatory, we add a validator to ensure that the value 0 is not selected
- if ($oObject->GetAttributeFlags($this->GetCode()) & OPT_ATT_MANDATORY)
- {
- $oFormField->AddValidator(new \Combodo\iTop\Form\Validator\NotEmptyExtKeyValidator());
- }
- parent::MakeFormField($oObject, $oFormField);
- return $oFormField;
- }
- }
- /**
- * Special kind of External Key to manage a hierarchy of objects
- */
- class AttributeHierarchicalKey extends AttributeExternalKey
- {
- protected $m_sTargetClass;
- static public function ListExpectedParams()
- {
- $aParams = parent::ListExpectedParams();
- $idx = array_search('targetclass', $aParams);
- unset($aParams[$idx]);
- $idx = array_search('jointype', $aParams);
- unset($aParams[$idx]);
- return $aParams; // TODO: mettre les bons parametres ici !!
- }
- public function GetEditClass() {return "ExtKey";}
- public function RequiresIndex()
- {
- return true;
- }
- /*
- * The target class is the class for which the attribute has been defined first
- */
- public function SetHostClass($sHostClass)
- {
- if (!isset($this->m_sTargetClass))
- {
- $this->m_sTargetClass = $sHostClass;
- }
- parent::SetHostClass($sHostClass);
- }
- public function IsHierarchicalKey() {return true;}
- public function GetTargetClass($iType = EXTKEY_RELATIVE) {return $this->m_sTargetClass;}
- public function GetKeyAttDef($iType = EXTKEY_RELATIVE){return $this;}
- public function GetKeyAttCode() {return $this->GetCode();}
- public function GetBasicFilterOperators()
- {
- return parent::GetBasicFilterOperators();
- }
- public function GetBasicFilterLooseOperator()
- {
- return parent::GetBasicFilterLooseOperator();
- }
- public function GetSQLColumns($bFullSpec = false)
- {
- $aColumns = array();
- $aColumns[$this->GetCode()] = 'INT(11)'.($bFullSpec ? ' DEFAULT 0' : '');
- $aColumns[$this->GetSQLLeft()] = 'INT(11)'.($bFullSpec ? ' DEFAULT 0' : '');
- $aColumns[$this->GetSQLRight()] = 'INT(11)'.($bFullSpec ? ' DEFAULT 0' : '');
- return $aColumns;
- }
- public function GetSQLRight()
- {
- return $this->GetCode().'_right';
- }
- public function GetSQLLeft()
- {
- return $this->GetCode().'_left';
- }
- public function GetSQLValues($value)
- {
- if (!is_array($value))
- {
- $aValues[$this->GetCode()] = $value;
- }
- else
- {
- $aValues = array();
- $aValues[$this->GetCode()] = $value[$this->GetCode()];
- $aValues[$this->GetSQLRight()] = $value[$this->GetSQLRight()];
- $aValues[$this->GetSQLLeft()] = $value[$this->GetSQLLeft()];
- }
- return $aValues;
- }
- public function GetAllowedValues($aArgs = array(), $sContains = '')
- {
- if (array_key_exists('this', $aArgs))
- {
- // Hierarchical keys have one more constraint: the "parent value" cannot be
- // "under" themselves
- $iRootId = $aArgs['this']->GetKey();
- if ($iRootId > 0) // ignore objects that do no exist in the database...
- {
- $oValSetDef = $this->GetValuesDef();
- $sClass = $this->m_sTargetClass;
- $oFilter = DBObjectSearch::FromOQL("SELECT $sClass AS node JOIN $sClass AS root ON node.".$this->GetCode()." NOT BELOW root.id WHERE root.id = $iRootId");
- $oValSetDef->AddCondition($oFilter);
- }
- }
- else
- {
- return parent::GetAllowedValues($aArgs, $sContains);
- }
- }
- public function GetAllowedValuesAsObjectSet($aArgs = array(), $sContains = '')
- {
- $oValSetDef = $this->GetValuesDef();
- if (array_key_exists('this', $aArgs))
- {
- // Hierarchical keys have one more constraint: the "parent value" cannot be
- // "under" themselves
- $iRootId = $aArgs['this']->GetKey();
- if ($iRootId > 0) // ignore objects that do no exist in the database...
- {
- $aValuesSetDef = $this->GetValuesDef();
- $sClass = $this->m_sTargetClass;
- $oFilter = DBObjectSearch::FromOQL("SELECT $sClass AS node JOIN $sClass AS root ON node.".$this->GetCode()." NOT BELOW root.id WHERE root.id = $iRootId");
- $oValSetDef->AddCondition($oFilter);
- }
- }
- $oSet = $oValSetDef->ToObjectSet($aArgs, $sContains);
- return $oSet;
- }
- /**
- * Find the corresponding "link" attribute on the target class
- *
- * @return string The attribute code on the target class, or null if none has been found
- */
- public function GetMirrorLinkAttribute()
- {
- return null;
- }
- }
- /**
- * An attribute which corresponds to an external key (direct or indirect)
- *
- * @package iTopORM
- */
- class AttributeExternalField extends AttributeDefinition
- {
- static public function ListExpectedParams()
- {
- return array_merge(parent::ListExpectedParams(), array("extkey_attcode", "target_attcode"));
- }
- public function GetEditClass() {return "ExtField";}
- public function GetFinalAttDef()
- {
- $oExtAttDef = $this->GetExtAttDef();
- return $oExtAttDef->GetFinalAttDef();
- }
- protected function GetSQLCol($bFullSpec = false)
- {
- // throw new CoreException("external attribute: does it make any sense to request its type ?");
- $oExtAttDef = $this->GetExtAttDef();
- return $oExtAttDef->GetSQLCol($bFullSpec);
- }
- public function GetSQLExpressions($sPrefix = '')
- {
- if ($sPrefix == '')
- {
- return array('' => $this->GetCode()); // Warning: Use GetCode() since AttributeExternalField does not have any 'sql' property
- }
- else
- {
- return $sPrefix;
- }
- }
- public function GetLabel($sDefault = null)
- {
- $sLabel = parent::GetLabel('');
- if (strlen($sLabel) == 0)
- {
- $oRemoteAtt = $this->GetExtAttDef();
- $sLabel = $oRemoteAtt->GetLabel($this->m_sCode);
- }
- return $sLabel;
- }
- public function GetDescription($sDefault = null)
- {
- $sLabel = parent::GetDescription('');
- if (strlen($sLabel) == 0)
- {
- $oRemoteAtt = $this->GetExtAttDef();
- $sLabel = $oRemoteAtt->GetDescription('');
- }
- return $sLabel;
- }
- public function GetHelpOnEdition($sDefault = null)
- {
- $sLabel = parent::GetHelpOnEdition('');
- if (strlen($sLabel) == 0)
- {
- $oRemoteAtt = $this->GetExtAttDef();
- $sLabel = $oRemoteAtt->GetHelpOnEdition('');
- }
- return $sLabel;
- }
- public function IsExternalKey($iType = EXTKEY_RELATIVE)
- {
- switch($iType)
- {
- case EXTKEY_ABSOLUTE:
- // see further
- $oRemoteAtt = $this->GetExtAttDef();
- return $oRemoteAtt->IsExternalKey($iType);
- case EXTKEY_RELATIVE:
- return false;
- default:
- throw new CoreException("Unexpected value for argument iType: '$iType'");
- }
- }
- public function GetTargetClass($iType = EXTKEY_RELATIVE)
- {
- return $this->GetKeyAttDef($iType)->GetTargetClass();
- }
- public function IsExternalField() {return true;}
- public function GetKeyAttCode() {return $this->Get("extkey_attcode");}
- public function GetExtAttCode() {return $this->Get("target_attcode");}
- public function GetKeyAttDef($iType = EXTKEY_RELATIVE)
- {
- switch($iType)
- {
- case EXTKEY_ABSOLUTE:
- // see further
- $oRemoteAtt = $this->GetExtAttDef();
- if ($oRemoteAtt->IsExternalField())
- {
- return $oRemoteAtt->GetKeyAttDef(EXTKEY_ABSOLUTE);
- }
- else if ($oRemoteAtt->IsExternalKey())
- {
- return $oRemoteAtt;
- }
- return $this->GetKeyAttDef(EXTKEY_RELATIVE); // which corresponds to the code hereafter !
- case EXTKEY_RELATIVE:
- return MetaModel::GetAttributeDef($this->GetHostClass(), $this->Get("extkey_attcode"));
- default:
- throw new CoreException("Unexpected value for argument iType: '$iType'");
- }
- }
-
- public function GetPrerequisiteAttributes($sClass = null)
- {
- return array($this->Get("extkey_attcode"));
- }
-
- public function GetExtAttDef()
- {
- $oKeyAttDef = $this->GetKeyAttDef();
- $oExtAttDef = MetaModel::GetAttributeDef($oKeyAttDef->GetTargetClass(), $this->Get("target_attcode"));
- if (!is_object($oExtAttDef)) throw new CoreException("Invalid external field ".$this->GetCode()." in class ".$this->GetHostClass().". The class ".$oKeyAttDef->GetTargetClass()." has no attribute ".$this->Get("target_attcode"));
- return $oExtAttDef;
- }
- public function GetSQLExpr()
- {
- $oExtAttDef = $this->GetExtAttDef();
- return $oExtAttDef->GetSQLExpr();
- }
- public function GetDefaultValue(DBObject $oHostObject = null)
- {
- $oExtAttDef = $this->GetExtAttDef();
- return $oExtAttDef->GetDefaultValue();
- }
- public function IsNullAllowed()
- {
- $oExtAttDef = $this->GetExtAttDef();
- return $oExtAttDef->IsNullAllowed();
- }
- public function IsScalar()
- {
- $oExtAttDef = $this->GetExtAttDef();
- return $oExtAttDef->IsScalar();
- }
- public function GetFilterDefinitions()
- {
- return array($this->GetCode() => new FilterFromAttribute($this));
- }
- public function GetBasicFilterOperators()
- {
- $oExtAttDef = $this->GetExtAttDef();
- return $oExtAttDef->GetBasicFilterOperators();
- }
- public function GetBasicFilterLooseOperator()
- {
- $oExtAttDef = $this->GetExtAttDef();
- return $oExtAttDef->GetBasicFilterLooseOperator();
- }
- public function GetBasicFilterSQLExpr($sOpCode, $value)
- {
- $oExtAttDef = $this->GetExtAttDef();
- return $oExtAttDef->GetBasicFilterSQLExpr($sOpCode, $value);
- }
- public function GetNullValue()
- {
- $oExtAttDef = $this->GetExtAttDef();
- return $oExtAttDef->GetNullValue();
- }
- public function IsNull($proposedValue)
- {
- $oExtAttDef = $this->GetExtAttDef();
- return $oExtAttDef->IsNull($proposedValue);
- }
- public function MakeRealValue($proposedValue, $oHostObj)
- {
- $oExtAttDef = $this->GetExtAttDef();
- return $oExtAttDef->MakeRealValue($proposedValue, $oHostObj);
- }
- public function ScalarToSQL($value)
- {
- // This one could be used in case of filtering only
- $oExtAttDef = $this->GetExtAttDef();
- return $oExtAttDef->ScalarToSQL($value);
- }
- // Do not overload GetSQLExpression here because this is handled in the joins
- //public function GetSQLExpressions($sPrefix = '') {return array();}
- // Here, we get the data...
- public function FromSQLToValue($aCols, $sPrefix = '')
- {
- $oExtAttDef = $this->GetExtAttDef();
- return $oExtAttDef->FromSQLToValue($aCols, $sPrefix);
- }
- public function GetAsHTML($value, $oHostObject = null, $bLocalize = true)
- {
- $oExtAttDef = $this->GetExtAttDef();
- return $oExtAttDef->GetAsHTML($value, null, $bLocalize);
- }
- public function GetAsXML($value, $oHostObject = null, $bLocalize = true)
- {
- $oExtAttDef = $this->GetExtAttDef();
- return $oExtAttDef->GetAsXML($value, null, $bLocalize);
- }
- public function GetAsCSV($value, $sSeparator = ',', $sTestQualifier = '"', $oHostObject = null, $bLocalize = true, $bConvertToPlainText = false)
- {
- $oExtAttDef = $this->GetExtAttDef();
- return $oExtAttDef->GetAsCSV($value, $sSeparator, $sTestQualifier, null, $bLocalize, $bConvertToPlainText);
- }
- public function IsPartOfFingerprint()
- {
- return false;
- }
- }
- /**
- * Map a varchar column to an URL (formats the ouput in HMTL)
- *
- * @package iTopORM
- */
- class AttributeURL extends AttributeString
- {
- static public function ListExpectedParams()
- {
- //return parent::ListExpectedParams();
- return array_merge(parent::ListExpectedParams(), array("target"));
- }
- public function GetEditClass() {return "String";}
- public function GetAsHTML($sValue, $oHostObject = null, $bLocalize = true)
- {
- $sTarget = $this->Get("target");
- if (empty($sTarget)) $sTarget = "_blank";
- $sLabel = Str::pure2html($sValue);
- if (strlen($sLabel) > 255)
- {
- // Truncate the length to 128 characters, by removing the middle
- $sLabel = substr($sLabel, 0, 100).'.....'.substr($sLabel, -20);
- }
- return "<a target=\"$sTarget\" href=\"$sValue\">$sLabel</a>";
- }
- public function GetValidationPattern()
- {
- return $this->GetOptional('validation_pattern', '^'.utils::GetConfig()->Get('url_validation_pattern').'$');
- }
- }
- /**
- * A blob is an ormDocument, it is stored as several columns in the database
- *
- * @package iTopORM
- */
- class AttributeBlob extends AttributeDefinition
- {
- static public function ListExpectedParams()
- {
- return array_merge(parent::ListExpectedParams(), array("depends_on"));
- }
- public function GetEditClass() {return "Document";}
-
- public function IsDirectField() {return true;}
- public function IsScalar() {return true;}
- public function IsWritable() {return true;}
- public function GetDefaultValue(DBObject $oHostObject = null) {return "";}
- public function IsNullAllowed(DBObject $oHostObject = null) {return $this->GetOptional("is_null_allowed", false);}
- public function GetEditValue($sValue, $oHostObj = null)
- {
- return '';
- }
- // Facilitate things: allow administrators to upload a document
- // from a CSV by specifying its path/URL
- public function MakeRealValue($proposedValue, $oHostObj)
- {
- if (!is_object($proposedValue))
- {
- if (file_exists($proposedValue) && UserRights::IsAdministrator())
- {
- $sContent = file_get_contents($proposedValue);
- $sExtension = strtolower(pathinfo($proposedValue, PATHINFO_EXTENSION));
- $sMimeType = "application/x-octet-stream";
- $aKnownExtensions = array(
- 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
- 'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
- 'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
- 'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
- 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
- 'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide',
- 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
- 'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
- 'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12',
- 'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12.xlsx',
- 'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
- 'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
- 'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
- 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
- 'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide',
- 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
- 'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
- 'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12',
- 'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
- 'jpg' => 'image/jpeg',
- 'jpeg' => 'image/jpeg',
- 'gif' => 'image/gif',
- 'png' => 'image/png',
- 'pdf' => 'application/pdf',
- 'doc' => 'application/msword',
- 'dot' => 'application/msword',
- 'xls' => 'application/vnd.ms-excel',
- 'ppt' => 'application/vnd.ms-powerpoint',
- 'vsd' => 'application/x-visio',
- 'vdx' => 'application/visio.drawing',
- 'odt' => 'application/vnd.oasis.opendocument.text',
- 'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
- 'odp' => 'application/vnd.oasis.opendocument.presentation',
- 'zip' => 'application/zip',
- 'txt' => 'text/plain',
- 'htm' => 'text/html',
- 'html' => 'text/html',
- 'exe' => 'application/octet-stream'
- );
- if (!array_key_exists($sExtension, $aKnownExtensions) && extension_loaded('fileinfo'))
- {
- $finfo = new finfo(FILEINFO_MIME);
- $sMimeType = $finfo->file($proposedValue);
- }
- return new ormDocument($sContent, $sMimeType);
- }
- else
- {
- return new ormDocument($proposedValue, 'text/plain');
- }
- }
- return $proposedValue;
- }
- public function GetSQLExpressions($sPrefix = '')
- {
- if ($sPrefix == '')
- {
- $sPrefix = $this->GetCode();
- }
- $aColumns = array();
- // Note: to optimize things, the existence of the attribute is determined by the existence of one column with an empty suffix
- $aColumns[''] = $sPrefix.'_mimetype';
- $aColumns['_data'] = $sPrefix.'_data';
- $aColumns['_filename'] = $sPrefix.'_filename';
- return $aColumns;
- }
- public function FromSQLToValue($aCols, $sPrefix = '')
- {
- if (!array_key_exists($sPrefix, $aCols))
- {
- $sAvailable = implode(', ', array_keys($aCols));
- throw new MissingColumnException("Missing column '$sPrefix' from {$sAvailable}");
- }
- $sMimeType = isset($aCols[$sPrefix]) ? $aCols[$sPrefix] : '';
- if (!array_key_exists($sPrefix.'_data', $aCols))
- {
- $sAvailable = implode(', ', array_keys($aCols));
- throw new MissingColumnException("Missing column '".$sPrefix."_data' from {$sAvailable}");
- }
- $data = isset($aCols[$sPrefix.'_data']) ? $aCols[$sPrefix.'_data'] : null;
- if (!array_key_exists($sPrefix.'_filename', $aCols))
- {
- $sAvailable = implode(', ', array_keys($aCols));
- throw new MissingColumnException("Missing column '".$sPrefix."_filename' from {$sAvailable}");
- }
- $sFileName = isset($aCols[$sPrefix.'_filename']) ? $aCols[$sPrefix.'_filename'] : '';
- $value = new ormDocument($data, $sMimeType, $sFileName);
- return $value;
- }
- public function GetSQLValues($value)
- {
- // #@# Optimization: do not load blobs anytime
- // As per mySQL doc, selecting blob columns will prevent mySQL from
- // using memory in case a temporary table has to be created
- // (temporary tables created on disk)
- // We will have to remove the blobs from the list of attributes when doing the select
- // then the use of Get() should finalize the load
- if ($value instanceOf ormDocument)
- {
- $aValues = array();
- $aValues[$this->GetCode().'_data'] = $value->GetData();
- $aValues[$this->GetCode().'_mimetype'] = $value->GetMimeType();
- $aValues[$this->GetCode().'_filename'] = $value->GetFileName();
- }
- else
- {
- $aValues = array();
- $aValues[$this->GetCode().'_data'] = '';
- $aValues[$this->GetCode().'_mimetype'] = '';
- $aValues[$this->GetCode().'_filename'] = '';
- }
- return $aValues;
- }
- public function GetSQLColumns($bFullSpec = false)
- {
- $aColumns = array();
- $aColumns[$this->GetCode().'_data'] = 'LONGBLOB'; // 2^32 (4 Gb)
- $aColumns[$this->GetCode().'_mimetype'] = 'VARCHAR(255)';
- $aColumns[$this->GetCode().'_filename'] = 'VARCHAR(255)';
- return $aColumns;
- }
- public function GetFilterDefinitions()
- {
- return array();
- // still not working... see later...
- return array(
- $this->GetCode().'->filename' => new FilterFromAttribute($this, '_filename'),
- $this->GetCode().'_mimetype' => new FilterFromAttribute($this, '_mimetype'),
- $this->GetCode().'_mimetype' => new FilterFromAttribute($this, '_mimetype')
- );
- }
- public function GetBasicFilterOperators()
- {
- return array();
- }
- public function GetBasicFilterLooseOperator()
- {
- return '=';
- }
- public function GetBasicFilterSQLExpr($sOpCode, $value)
- {
- return 'true';
- }
- public function GetAsHTML($value, $oHostObject = null, $bLocalize = true)
- {
- if (is_object($value))
- {
- return $value->GetAsHTML();
- }
- }
- public function GetAsCSV($sValue, $sSeparator = ',', $sTextQualifier = '"', $oHostObject = null, $bLocalize = true, $bConvertToPlainText = false)
- {
- return ''; // Not exportable in CSV !
- }
-
- public function GetAsXML($value, $oHostObject = null, $bLocalize = true)
- {
- return ''; // Not exportable in XML, or as CDATA + some subtags ??
- }
- /**
- * Helper to get a value that will be JSON encoded
- * The operation is the opposite to FromJSONToValue
- */
- public function GetForJSON($value)
- {
- if ($value instanceOf ormDocument)
- {
- $aValues = array();
- $aValues['data'] = base64_encode($value->GetData());
- $aValues['mimetype'] = $value->GetMimeType();
- $aValues['filename'] = $value->GetFileName();
- }
- else
- {
- $aValues = null;
- }
- return $aValues;
- }
- /**
- * Helper to form a value, given JSON decoded data
- * The operation is the opposite to GetForJSON
- */
- public function FromJSONToValue($json)
- {
- if (isset($json->data))
- {
- $data = base64_decode($json->data);
- $value = new ormDocument($data, $json->mimetype, $json->filename);
- }
- else
- {
- $value = null;
- }
- return $value;
- }
-
- public function Fingerprint($value)
- {
- $sFingerprint = '';
- if ($value instanceOf ormDocument)
- {
- $sFingerprint = md5($value->GetData());
- }
- return $sFingerprint;
- }
- }
- /**
- * A stop watch is an ormStopWatch object, it is stored as several columns in the database
- *
- * @package iTopORM
- */
- class AttributeStopWatch extends AttributeDefinition
- {
- static public function ListExpectedParams()
- {
- // The list of thresholds must be an array of iPercent => array of 'option' => value
- return array_merge(parent::ListExpectedParams(), array("states", "goal_computing", "working_time_computing", "thresholds"));
- }
- public function GetEditClass() {return "StopWatch";}
-
- public function IsDirectField() {return true;}
- public function IsScalar() {return true;}
- public function IsWritable() {return false;}
- public function GetDefaultValue(DBObject $oHostObject = null) {return $this->NewStopWatch();}
- public function GetEditValue($value, $oHostObj = null)
- {
- return $value->GetTimeSpent();
- }
- public function GetStates()
- {
- return $this->Get('states');
- }
- public function AlwaysLoadInTables()
- {
- // Each and every stop watch is accessed for computing the highlight code (DBObject::GetHighlightCode())
- return true;
- }
- /**
- * Construct a brand new (but configured) stop watch
- */
- public function NewStopWatch()
- {
- $oSW = new ormStopWatch();
- foreach ($this->ListThresholds() as $iThreshold => $aFoo)
- {
- $oSW->DefineThreshold($iThreshold);
- }
- return $oSW;
- }
- // Facilitate things: allow the user to Set the value from a string
- public function MakeRealValue($proposedValue, $oHostObj)
- {
- if (!$proposedValue instanceof ormStopWatch)
- {
- return $this->NewStopWatch();
- }
- return $proposedValue;
- }
- public function GetSQLExpressions($sPrefix = '')
- {
- if ($sPrefix == '')
- {
- $sPrefix = $this->GetCode(); // Warning: a stopwatch does not have any 'sql' property, so its SQL column is equal to its attribute code !!
- }
- $aColumns = array();
- // Note: to optimize things, the existence of the attribute is determined by the existence of one column with an empty suffix
- $aColumns[''] = $sPrefix.'_timespent';
- $aColumns['_started'] = $sPrefix.'_started';
- $aColumns['_laststart'] = $sPrefix.'_laststart';
- $aColumns['_stopped'] = $sPrefix.'_stopped';
- foreach ($this->ListThresholds() as $iThreshold => $aFoo)
- {
- $sThPrefix = '_'.$iThreshold;
- $aColumns[$sThPrefix.'_deadline'] = $sPrefix.$sThPrefix.'_deadline';
- $aColumns[$sThPrefix.'_passed'] = $sPrefix.$sThPrefix.'_passed';
- $aColumns[$sThPrefix.'_triggered'] = $sPrefix.$sThPrefix.'_triggered';
- $aColumns[$sThPrefix.'_overrun'] = $sPrefix.$sThPrefix.'_overrun';
- }
- return $aColumns;
- }
- public static function DateToSeconds($sDate)
- {
- if (is_null($sDate))
- {
- return null;
- }
- $oDateTime = new DateTime($sDate);
- $iSeconds = $oDateTime->format('U');
- return $iSeconds;
- }
- public static function SecondsToDate($iSeconds)
- {
- if (is_null($iSeconds))
- {
- return null;
- }
- return date("Y-m-d H:i:s", $iSeconds);
- }
- public function FromSQLToValue($aCols, $sPrefix = '')
- {
- $aExpectedCols = array($sPrefix, $sPrefix.'_started', $sPrefix.'_laststart', $sPrefix.'_stopped');
- foreach ($this->ListThresholds() as $iThreshold => $aFoo)
- {
- $sThPrefix = '_'.$iThreshold;
- $aExpectedCols[] = $sPrefix.$sThPrefix.'_deadline';
- $aExpectedCols[] = $sPrefix.$sThPrefix.'_passed';
- $aExpectedCols[] = $sPrefix.$sThPrefix.'_triggered';
- $aExpectedCols[] = $sPrefix.$sThPrefix.'_overrun';
- }
- foreach ($aExpectedCols as $sExpectedCol)
- {
- if (!array_key_exists($sExpectedCol, $aCols))
- {
- $sAvailable = implode(', ', array_keys($aCols));
- throw new MissingColumnException("Missing column '$sExpectedCol' from {$sAvailable}");
- }
- }
- $value = new ormStopWatch(
- $aCols[$sPrefix],
- self::DateToSeconds($aCols[$sPrefix.'_started']),
- self::DateToSeconds($aCols[$sPrefix.'_laststart']),
- self::DateToSeconds($aCols[$sPrefix.'_stopped'])
- );
- $aThresholds = array();
- foreach ($this->ListThresholds() as $iThreshold => $aDefinition)
- {
- $sThPrefix = '_'.$iThreshold;
- $value->DefineThreshold(
- $iThreshold,
- self::DateToSeconds($aCols[$sPrefix.$sThPrefix.'_deadline']),
- (bool)($aCols[$sPrefix.$sThPrefix.'_passed'] == 1),
- (bool)($aCols[$sPrefix.$sThPrefix.'_triggered'] == 1),
- $aCols[$sPrefix.$sThPrefix.'_overrun'],
- array_key_exists('highlight', $aDefinition) ? $aDefinition['highlight'] : null
- );
- }
- return $value;
- }
- public function GetSQLValues($value)
- {
- if ($value instanceOf ormStopWatch)
- {
- $aValues = array();
- $aValues[$this->GetCode().'_timespent'] = $value->GetTimeSpent();
- $aValues[$this->GetCode().'_started'] = self::SecondsToDate($value->GetStartDate());
- $aValues[$this->GetCode().'_laststart'] = self::SecondsToDate($value->GetLastStartDate());
- $aValues[$this->GetCode().'_stopped'] = self::SecondsToDate($value->GetStopDate());
- foreach ($this->ListThresholds() as $iThreshold => $aFoo)
- {
- $sPrefix = $this->GetCode().'_'.$iThreshold;
- $aValues[$sPrefix.'_deadline'] = self::SecondsToDate($value->GetThresholdDate($iThreshold));
- $aValues[$sPrefix.'_passed'] = $value->IsThresholdPassed($iThreshold) ? '1' : '0';
- $aValues[$sPrefix.'_triggered'] = $value->IsThresholdTriggered($iThreshold) ? '1' : '0';
- $aValues[$sPrefix.'_overrun'] = $value->GetOverrun($iThreshold);
- }
- }
- else
- {
- $aValues = array();
- $aValues[$this->GetCode().'_timespent'] = '';
- $aValues[$this->GetCode().'_started'] = '';
- $aValues[$this->GetCode().'_laststart'] = '';
- $aValues[$this->GetCode().'_stopped'] = '';
- }
- return $aValues;
- }
- public function GetSQLColumns($bFullSpec = false)
- {
- $aColumns = array();
- $aColumns[$this->GetCode().'_timespent'] = 'INT(11) UNSIGNED';
- $aColumns[$this->GetCode().'_started'] = 'DATETIME';
- $aColumns[$this->GetCode().'_laststart'] = 'DATETIME';
- $aColumns[$this->GetCode().'_stopped'] = 'DATETIME';
- foreach ($this->ListThresholds() as $iThreshold => $aFoo)
- {
- $sPrefix = $this->GetCode().'_'.$iThreshold;
- $aColumns[$sPrefix.'_deadline'] = 'DATETIME';
- $aColumns[$sPrefix.'_passed'] = 'TINYINT(1) UNSIGNED';
- $aColumns[$sPrefix.'_triggered'] = 'TINYINT(1)';
- $aColumns[$sPrefix.'_overrun'] = 'INT(11) UNSIGNED';
- }
- return $aColumns;
- }
- public function GetFilterDefinitions()
- {
- $aRes = array(
- $this->GetCode() => new FilterFromAttribute($this),
- $this->GetCode().'_started' => new FilterFromAttribute($this, '_started'),
- $this->GetCode().'_laststart' => new FilterFromAttribute($this, '_laststart'),
- $this->GetCode().'_stopped' => new FilterFromAttribute($this, '_stopped')
- );
- foreach ($this->ListThresholds() as $iThreshold => $aFoo)
- {
- $sPrefix = $this->GetCode().'_'.$iThreshold;
- $aRes[$sPrefix.'_deadline'] = new FilterFromAttribute($this, '_deadline');
- $aRes[$sPrefix.'_passed'] = new FilterFromAttribute($this, '_passed');
- $aRes[$sPrefix.'_triggered'] = new FilterFromAttribute($this, '_triggered');
- $aRes[$sPrefix.'_overrun'] = new FilterFromAttribute($this, '_overrun');
- }
- return $aRes;
- }
- public function GetBasicFilterOperators()
- {
- return array();
- }
- public function GetBasicFilterLooseOperator()
- {
- return '=';
- }
- public function GetBasicFilterSQLExpr($sOpCode, $value)
- {
- return 'true';
- }
- public function GetAsHTML($value, $oHostObject = null, $bLocalize = true)
- {
- if (is_object($value))
- {
- return $value->GetAsHTML($this, $oHostObject);
- }
- }
- public function GetAsCSV($value, $sSeparator = ',', $sTextQualifier = '"', $oHostObject = null, $bLocalize = true, $bConvertToPlainText = false)
- {
- return $value->GetTimeSpent();
- }
-
- public function GetAsXML($value, $oHostObject = null, $bLocalize = true)
- {
- return $value->GetTimeSpent();
- }
- public function ListThresholds()
- {
- return $this->Get('thresholds');
- }
-
- public function Fingerprint($value)
- {
- $sFingerprint = '';
- if (is_object($value))
- {
- $sFingerprint = $value->GetAsHTML($this);
- }
- return $sFingerprint;
- }
- /**
- * To expose internal values: Declare an attribute AttributeSubItem
- * and implement the GetSubItemXXXX verbs
- */
- public function GetSubItemSQLExpression($sItemCode)
- {
- $sPrefix = $this->GetCode();
- switch($sItemCode)
- {
- case 'timespent':
- return array('' => $sPrefix.'_timespent');
- case 'started':
- return array('' => $sPrefix.'_started');
- case 'laststart':
- return array('' => $sPrefix.'_laststart');
- case 'stopped':
- return array('' => $sPrefix.'_stopped');
- }
- foreach ($this->ListThresholds() as $iThreshold => $aFoo)
- {
- $sThPrefix = $iThreshold.'_';
- if (substr($sItemCode, 0, strlen($sThPrefix)) == $sThPrefix)
- {
- // The current threshold is concerned
- $sThresholdCode = substr($sItemCode, strlen($sThPrefix));
- switch($sThresholdCode)
- {
- case 'deadline':
- return array('' => $sPrefix.'_'.$iThreshold.'_deadline');
- case 'passed':
- return array('' => $sPrefix.'_'.$iThreshold.'_passed');
- case 'triggered':
- return array('' => $sPrefix.'_'.$iThreshold.'_triggered');
- case 'overrun':
- return array('' => $sPrefix.'_'.$iThreshold.'_overrun');
- }
- }
- }
- throw new CoreException("Unknown item code '$sItemCode' for attribute ".$this->GetHostClass().'::'.$this->GetCode());
- }
- public function GetSubItemValue($sItemCode, $value, $oHostObject = null)
- {
- $oStopWatch = $value;
- switch($sItemCode)
- {
- case 'timespent':
- return $oStopWatch->GetTimeSpent();
- case 'started':
- return $oStopWatch->GetStartDate();
- case 'laststart':
- return $oStopWatch->GetLastStartDate();
- case 'stopped':
- return $oStopWatch->GetStopDate();
- }
- foreach ($this->ListThresholds() as $iThreshold => $aFoo)
- {
- $sThPrefix = $iThreshold.'_';
- if (substr($sItemCode, 0, strlen($sThPrefix)) == $sThPrefix)
- {
- // The current threshold is concerned
- $sThresholdCode = substr($sItemCode, strlen($sThPrefix));
- switch($sThresholdCode)
- {
- case 'deadline':
- return $oStopWatch->GetThresholdDate($iThreshold);
- case 'passed':
- return $oStopWatch->IsThresholdPassed($iThreshold);
- case 'triggered':
- return $oStopWatch->IsThresholdTriggered($iThreshold);
- case 'overrun':
- return $oStopWatch->GetOverrun($iThreshold);
- }
- }
- }
- throw new CoreException("Unknown item code '$sItemCode' for attribute ".$this->GetHostClass().'::'.$this->GetCode());
- }
- protected function GetBooleanLabel($bValue)
- {
- $sDictKey = $bValue ? 'yes' : 'no';
- return Dict::S('BooleanLabel:'.$sDictKey, 'def:'.$sDictKey);
- }
- public function GetSubItemAsHTMLForHistory($sItemCode, $sValue)
- {
- switch($sItemCode)
- {
- case 'timespent':
- $sHtml = (int)$sValue ? Str::pure2html(AttributeDuration::FormatDuration($sValue)) : null;
- break;
- case 'started':
- case 'laststart':
- case 'stopped':
- $sHtml = (int)$sValue ? date(self::GetDateFormat(), (int)$sValue) : null;
- break;
- default:
- foreach ($this->ListThresholds() as $iThreshold => $aFoo)
- {
- $sThPrefix = $iThreshold.'_';
- if (substr($sItemCode, 0, strlen($sThPrefix)) == $sThPrefix)
- {
- // The current threshold is concerned
- $sThresholdCode = substr($sItemCode, strlen($sThPrefix));
- switch($sThresholdCode)
- {
- case 'deadline':
- $sHtml = (int)$sValue ? date(self::GetDateFormat(true /*full*/), (int)$sValue) : null;
- break;
- case 'passed':
- $sHtml = $this->GetBooleanLabel((int)$sValue);
- break;
- case 'triggered':
- $sHtml = $this->GetBooleanLabel((int)$sValue);
- break;
- case 'overrun':
- $sHtml = (int)$sValue > 0 ? Str::pure2html(AttributeDuration::FormatDuration((int)$sValue)) : '';
- }
- }
- }
- }
- return $sHtml;
- }
- static protected function GetDateFormat($bFull = false)
- {
- if ($bFull)
- {
- return "Y-m-d H:i:s";
- }
- else
- {
- return "Y-m-d H:i";
- }
- }
- public function GetSubItemAsHTML($sItemCode, $value)
- {
- $sHtml = $value;
- switch($sItemCode)
- {
- case 'timespent':
- $sHtml = Str::pure2html(AttributeDuration::FormatDuration($value));
- break;
- case 'started':
- case 'laststart':
- case 'stopped':
- if (is_null($value))
- {
- $sHtml = ''; // Undefined
- }
- else
- {
- $sHtml = date(self::GetDateFormat(), $value);
- }
- break;
- default:
- foreach ($this->ListThresholds() as $iThreshold => $aFoo)
- {
- $sThPrefix = $iThreshold.'_';
- if (substr($sItemCode, 0, strlen($sThPrefix)) == $sThPrefix)
- {
- // The current threshold is concerned
- $sThresholdCode = substr($sItemCode, strlen($sThPrefix));
- switch($sThresholdCode)
- {
- case 'deadline':
- if ($value)
- {
- $sDate = date(self::GetDateFormat(true /*full*/), $value);
- $sHtml = Str::pure2html(AttributeDeadline::FormatDeadline($sDate));
- }
- else
- {
- $sHtml = '';
- }
- break;
- case 'passed':
- case 'triggered':
- $sHtml = $this->GetBooleanLabel($value);
- break;
- case 'overrun':
- $sHtml = Str::pure2html(AttributeDuration::FormatDuration($value));
- break;
- }
- }
- }
- }
- return $sHtml;
- }
- public function GetSubItemAsCSV($sItemCode, $value, $sSeparator = ',', $sTextQualifier = '"', $bConvertToPlainText = false)
- {
- $sFrom = array("\r\n", $sTextQualifier);
- $sTo = array("\n", $sTextQualifier.$sTextQualifier);
- $sEscaped = str_replace($sFrom, $sTo, (string)$value);
- $sRet = $sTextQualifier.$sEscaped.$sTextQualifier;
- switch($sItemCode)
- {
- case 'timespent':
- case 'started':
- case 'laststart':
- case 'stopped':
- break;
- default:
- foreach ($this->ListThresholds() as $iThreshold => $aFoo)
- {
- $sThPrefix = $iThreshold.'_';
- if (substr($sItemCode, 0, strlen($sThPrefix)) == $sThPrefix)
- {
- // The current threshold is concerned
- $sThresholdCode = substr($sItemCode, strlen($sThPrefix));
- switch($sThresholdCode)
- {
- case 'deadline':
- if ($value != '')
- {
- $sRet = $sTextQualifier.date(self::GetDateFormat(true /*full*/), $value).$sTextQualifier;
- }
- break;
- case 'passed':
- case 'triggered':
- $sRet = $sTextQualifier.$this->GetBooleanLabel($value).$sTextQualifier;
- break;
- case 'overrun':
- break;
- }
- }
- }
- }
- return $sRet;
- }
- public function GetSubItemAsXML($sItemCode, $value)
- {
- $sRet = Str::pure2xml((string)$value);
- switch($sItemCode)
- {
- case 'timespent':
- case 'started':
- case 'laststart':
- case 'stopped':
- break;
- default:
- foreach ($this->ListThresholds() as $iThreshold => $aFoo)
- {
- $sThPrefix = $iThreshold.'_';
- if (substr($sItemCode, 0, strlen($sThPrefix)) == $sThPrefix)
- {
- // The current threshold is concerned
- $sThresholdCode = substr($sItemCode, strlen($sThPrefix));
- switch($sThresholdCode)
- {
- case 'deadline':
- break;
- case 'passed':
- case 'triggered':
- $sRet = $this->GetBooleanLabel($value);
- break;
- case 'overrun':
- break;
- }
- }
- }
- }
- return $sRet;
- }
- /**
- * Implemented for the HTML spreadsheet format!
- */
- public function GetSubItemAsEditValue($sItemCode, $value)
- {
- $sRet = $value;
- switch($sItemCode)
- {
- case 'timespent':
- break;
- case 'started':
- case 'laststart':
- case 'stopped':
- if (is_null($value))
- {
- $sRet = ''; // Undefined
- }
- else
- {
- $sRet = date(self::GetDateFormat(), $value);
- }
- break;
- default:
- foreach ($this->ListThresholds() as $iThreshold => $aFoo)
- {
- $sThPrefix = $iThreshold.'_';
- if (substr($sItemCode, 0, strlen($sThPrefix)) == $sThPrefix)
- {
- // The current threshold is concerned
- $sThresholdCode = substr($sItemCode, strlen($sThPrefix));
- switch($sThresholdCode)
- {
- case 'deadline':
- if ($value)
- {
- $sRet = date(self::GetDateFormat(true /*full*/), $value);
- }
- else
- {
- $sRet = '';
- }
- break;
- case 'passed':
- case 'triggered':
- $sRet = $this->GetBooleanLabel($value);
- break;
- case 'overrun':
- break;
- }
- }
- }
- }
- return $sRet;
- }
- }
- /**
- * View of a subvalue of another attribute
- * If an attribute implements the verbs GetSubItem.... then it can expose
- * internal values, each of them being an attribute and therefore they
- * can be displayed at different times in the object lifecycle, and used for
- * reporting (as a condition in OQL, or as an additional column in an export)
- * Known usages: Stop Watches can expose threshold statuses
- */
- class AttributeSubItem extends AttributeDefinition
- {
- static public function ListExpectedParams()
- {
- return array_merge(parent::ListExpectedParams(), array('target_attcode', 'item_code'));
- }
- public function GetParentAttCode() {return $this->Get("target_attcode");}
- /**
- * Helper : get the attribute definition to which the execution will be forwarded
- */
- public function GetTargetAttDef()
- {
- $sClass = $this->GetHostClass();
- $oParentAttDef = MetaModel::GetAttributeDef($sClass, $this->Get('target_attcode'));
- return $oParentAttDef;
- }
- public function GetEditClass() {return "";}
-
- public function GetValuesDef() {return null;}
- public function IsDirectField() {return true;}
- public function IsScalar() {return true;}
- public function IsWritable() {return false;}
- public function GetDefaultValue(DBObject $oHostObject = null) {return null;}
- // public function IsNullAllowed() {return false;}
- public function LoadInObject() {return false;} // if this verb returns false, then GetValue must be implemented
- /**
- * Used by DBOBject::Get()
- */
- public function GetValue($oHostObject, $bOriginal = false)
- {
- $oParent = $this->GetTargetAttDef();
- $parentValue = $oHostObject->GetStrict($oParent->GetCode());
- $res = $oParent->GetSubItemValue($this->Get('item_code'), $parentValue, $oHostObject);
- return $res;
- }
- //
- // protected function ScalarToSQL($value) {return $value;} // format value as a valuable SQL literal (quoted outside)
- public function FromSQLToValue($aCols, $sPrefix = '')
- {
- }
- public function GetSQLColumns($bFullSpec = false)
- {
- return array();
- }
- public function GetFilterDefinitions()
- {
- return array($this->GetCode() => new FilterFromAttribute($this));
- }
- public function GetBasicFilterOperators()
- {
- return array();
- }
- public function GetBasicFilterLooseOperator()
- {
- return "=";
- }
- public function GetBasicFilterSQLExpr($sOpCode, $value)
- {
- $sQValue = CMDBSource::Quote($value);
- switch ($sOpCode)
- {
- case '!=':
- return $this->GetSQLExpr()." != $sQValue";
- break;
- case '=':
- default:
- return $this->GetSQLExpr()." = $sQValue";
- }
- }
- public function GetSQLExpressions($sPrefix = '')
- {
- $oParent = $this->GetTargetAttDef();
- $res = $oParent->GetSubItemSQLExpression($this->Get('item_code'));
- return $res;
- }
- public function GetAsHTML($value, $oHostObject = null, $bLocalize = true)
- {
- $oParent = $this->GetTargetAttDef();
- $res = $oParent->GetSubItemAsHTML($this->Get('item_code'), $value);
- return $res;
- }
- public function GetAsHTMLForHistory($value, $oHostObject = null, $bLocalize = true)
- {
- $oParent = $this->GetTargetAttDef();
- $res = $oParent->GetSubItemAsHTMLForHistory($this->Get('item_code'), $value);
- return $res;
- }
- public function GetAsCSV($value, $sSeparator = ',', $sTextQualifier = '"', $oHostObject = null, $bLocalize = true, $bConvertToPlainText = false)
- {
- $oParent = $this->GetTargetAttDef();
- $res = $oParent->GetSubItemAsCSV($this->Get('item_code'), $value, $sSeparator, $sTextQualifier, $bConvertToPlainText);
- return $res;
- }
-
- public function GetAsXML($value, $oHostObject = null, $bLocalize = true)
- {
- $oParent = $this->GetTargetAttDef();
- $res = $oParent->GetSubItemAsXML($this->Get('item_code'), $value);
- return $res;
- }
- /**
- * As of now, this function must be implemented to have the value in spreadsheet format
- */
- public function GetEditValue($value, $oHostObj = null)
- {
- $oParent = $this->GetTargetAttDef();
- $res = $oParent->GetSubItemAsEditValue($this->Get('item_code'), $value);
- return $res;
- }
-
- public function IsPartOfFingerprint() { return false; }
- }
- /**
- * One way encrypted (hashed) password
- */
- class AttributeOneWayPassword extends AttributeDefinition
- {
- static public function ListExpectedParams()
- {
- return array_merge(parent::ListExpectedParams(), array("depends_on"));
- }
- public function GetEditClass() {return "One Way Password";}
-
- public function IsDirectField() {return true;}
- public function IsScalar() {return true;}
- public function IsWritable() {return true;}
- public function GetDefaultValue(DBObject $oHostObject = null) {return "";}
- public function IsNullAllowed() {return $this->GetOptional("is_null_allowed", false);}
- // Facilitate things: allow the user to Set the value from a string or from an ormPassword (already encrypted)
- public function MakeRealValue($proposedValue, $oHostObj)
- {
- $oPassword = $proposedValue;
- if (!is_object($oPassword))
- {
- $oPassword = new ormPassword('', '');
- $oPassword->SetPassword($proposedValue);
- }
- return $oPassword;
- }
- public function GetSQLExpressions($sPrefix = '')
- {
- if ($sPrefix == '')
- {
- $sPrefix = $this->GetCode(); // Warning: AttributeOneWayPassword does not have any sql property so code = sql !
- }
- $aColumns = array();
- // Note: to optimize things, the existence of the attribute is determined by the existence of one column with an empty suffix
- $aColumns[''] = $sPrefix.'_hash';
- $aColumns['_salt'] = $sPrefix.'_salt';
- return $aColumns;
- }
- public function FromSQLToValue($aCols, $sPrefix = '')
- {
- if (!array_key_exists($sPrefix, $aCols))
- {
- $sAvailable = implode(', ', array_keys($aCols));
- throw new MissingColumnException("Missing column '$sPrefix' from {$sAvailable}");
- }
- $hashed = isset($aCols[$sPrefix]) ? $aCols[$sPrefix] : '';
- if (!array_key_exists($sPrefix.'_salt', $aCols))
- {
- $sAvailable = implode(', ', array_keys($aCols));
- throw new MissingColumnException("Missing column '".$sPrefix."_salt' from {$sAvailable}");
- }
- $sSalt = isset($aCols[$sPrefix.'_salt']) ? $aCols[$sPrefix.'_salt'] : '';
- $value = new ormPassword($hashed, $sSalt);
- return $value;
- }
- public function GetSQLValues($value)
- {
- // #@# Optimization: do not load blobs anytime
- // As per mySQL doc, selecting blob columns will prevent mySQL from
- // using memory in case a temporary table has to be created
- // (temporary tables created on disk)
- // We will have to remove the blobs from the list of attributes when doing the select
- // then the use of Get() should finalize the load
- if ($value instanceOf ormPassword)
- {
- $aValues = array();
- $aValues[$this->GetCode().'_hash'] = $value->GetHash();
- $aValues[$this->GetCode().'_salt'] = $value->GetSalt();
- }
- else
- {
- $aValues = array();
- $aValues[$this->GetCode().'_hash'] = '';
- $aValues[$this->GetCode().'_salt'] = '';
- }
- return $aValues;
- }
- public function GetSQLColumns($bFullSpec = false)
- {
- $aColumns = array();
- $aColumns[$this->GetCode().'_hash'] = 'TINYBLOB';
- $aColumns[$this->GetCode().'_salt'] = 'TINYBLOB';
- return $aColumns;
- }
- public function GetImportColumns()
- {
- $aColumns = array();
- $aColumns[$this->GetCode()] = 'TINYTEXT';
- return $aColumns;
- }
- public function FromImportToValue($aCols, $sPrefix = '')
- {
- if (!isset($aCols[$sPrefix]))
- {
- $sAvailable = implode(', ', array_keys($aCols));
- throw new MissingColumnException("Missing column '$sPrefix' from {$sAvailable}");
- }
- $sClearPwd = $aCols[$sPrefix];
- $oPassword = new ormPassword('', '');
- $oPassword->SetPassword($sClearPwd);
- return $oPassword;
- }
- public function GetFilterDefinitions()
- {
- return array();
- // still not working... see later...
- }
- public function GetBasicFilterOperators()
- {
- return array();
- }
- public function GetBasicFilterLooseOperator()
- {
- return '=';
- }
- public function GetBasicFilterSQLExpr($sOpCode, $value)
- {
- return 'true';
- }
- public function GetAsHTML($value, $oHostObject = null, $bLocalize = true)
- {
- if (is_object($value))
- {
- return $value->GetAsHTML();
- }
- }
- public function GetAsCSV($sValue, $sSeparator = ',', $sTextQualifier = '"', $oHostObject = null, $bLocalize = true, $bConvertToPlainText = false)
- {
- return ''; // Not exportable in CSV
- }
-
- public function GetAsXML($value, $oHostObject = null, $bLocalize = true)
- {
- return ''; // Not exportable in XML
- }
- }
- // Indexed array having two dimensions
- class AttributeTable extends AttributeDBField
- {
- public function GetEditClass() {return "Table";}
- protected function GetSQLCol($bFullSpec = false) {return "LONGTEXT";}
- public function GetMaxSize()
- {
- return null;
- }
- public function GetNullValue()
- {
- return array();
- }
- public function IsNull($proposedValue)
- {
- return (count($proposedValue) == 0);
- }
- public function GetEditValue($sValue, $oHostObj = null)
- {
- return '';
- }
- // Facilitate things: allow the user to Set the value from a string
- public function MakeRealValue($proposedValue, $oHostObj)
- {
- if (is_null($proposedValue))
- {
- return array();
- }
- else if (!is_array($proposedValue))
- {
- return array(0 => array(0 => $proposedValue));
- }
- return $proposedValue;
- }
- public function FromSQLToValue($aCols, $sPrefix = '')
- {
- try
- {
- $value = @unserialize($aCols[$sPrefix.'']);
- if ($value === false)
- {
- $value = $this->MakeRealValue($aCols[$sPrefix.''], null);
- }
- }
- catch(Exception $e)
- {
- $value = $this->MakeRealValue($aCols[$sPrefix.''], null);
- }
- return $value;
- }
- public function GetSQLValues($value)
- {
- $aValues = array();
- $aValues[$this->Get("sql")] = serialize($value);
- return $aValues;
- }
- public function GetAsHTML($value, $oHostObject = null, $bLocalize = true)
- {
- if (!is_array($value))
- {
- throw new CoreException('Expecting an array', array('found' => get_class($value)));
- }
- if (count($value) == 0)
- {
- return "";
- }
- $sRes = "<TABLE class=\"listResults\">";
- $sRes .= "<TBODY>";
- foreach($value as $iRow => $aRawData)
- {
- $sRes .= "<TR>";
- foreach ($aRawData as $iCol => $cell)
- {
- $sCell = str_replace("\n", "<br>\n", Str::pure2html((string)$cell));
- $sRes .= "<TD>$sCell</TD>";
- }
- $sRes .= "</TR>";
- }
- $sRes .= "</TBODY>";
- $sRes .= "</TABLE>";
- return $sRes;
- }
- public function GetAsCSV($sValue, $sSeparator = ',', $sTextQualifier = '"', $oHostObject = null, $bLocalize = true, $bConvertToPlainText = false)
- {
- // Not implemented
- return '';
- }
- public function GetAsXML($value, $oHostObject = null, $bLocalize = true)
- {
- if (count($value) == 0)
- {
- return "";
- }
- $sRes = "";
- foreach($value as $iRow => $aRawData)
- {
- $sRes .= "<row>";
- foreach ($aRawData as $iCol => $cell)
- {
- $sCell = Str::pure2xml((string)$cell);
- $sRes .= "<cell icol=\"$iCol\">$sCell</cell>";
- }
- $sRes .= "</row>";
- }
- return $sRes;
- }
- }
- // The PHP value is a hash array, it is stored as a TEXT column
- class AttributePropertySet extends AttributeTable
- {
- public function GetEditClass() {return "PropertySet";}
- // Facilitate things: allow the user to Set the value from a string
- public function MakeRealValue($proposedValue, $oHostObj)
- {
- if (!is_array($proposedValue))
- {
- return array('?' => (string)$proposedValue);
- }
- return $proposedValue;
- }
- public function GetAsHTML($value, $oHostObject = null, $bLocalize = true)
- {
- if (!is_array($value))
- {
- throw new CoreException('Expecting an array', array('found' => get_class($value)));
- }
- if (count($value) == 0)
- {
- return "";
- }
- $sRes = "<TABLE class=\"listResults\">";
- $sRes .= "<TBODY>";
- foreach($value as $sProperty => $sValue)
- {
- if ($sProperty == 'auth_pwd')
- {
- $sValue = '*****';
- }
- $sRes .= "<TR>";
- $sCell = str_replace("\n", "<br>\n", Str::pure2html((string)$sValue));
- $sRes .= "<TD class=\"label\">$sProperty</TD><TD>$sCell</TD>";
- $sRes .= "</TR>";
- }
- $sRes .= "</TBODY>";
- $sRes .= "</TABLE>";
- return $sRes;
- }
- public function GetAsCSV($value, $sSeparator = ',', $sTextQualifier = '"', $oHostObject = null, $bLocalize = true, $bConvertToPlainText = false)
- {
- if (count($value) == 0)
- {
- return "";
- }
- $aRes = array();
- foreach($value as $sProperty => $sValue)
- {
- if ($sProperty == 'auth_pwd')
- {
- $sValue = '*****';
- }
- $sFrom = array(',', '=');
- $sTo = array('\,', '\=');
- $aRes[] = $sProperty.'='.str_replace($sFrom, $sTo, (string)$sValue);
- }
- $sRaw = implode(',', $aRes);
- $sFrom = array("\r\n", $sTextQualifier);
- $sTo = array("\n", $sTextQualifier.$sTextQualifier);
- $sEscaped = str_replace($sFrom, $sTo, $sRaw);
- return $sTextQualifier.$sEscaped.$sTextQualifier;
- }
- public function GetAsXML($value, $oHostObject = null, $bLocalize = true)
- {
- if (count($value) == 0)
- {
- return "";
- }
- $sRes = "";
- foreach($value as $sProperty => $sValue)
- {
- if ($sProperty == 'auth_pwd')
- {
- $sValue = '*****';
- }
- $sRes .= "<property id=\"$sProperty\">";
- $sRes .= Str::pure2xml((string)$sValue);
- $sRes .= "</property>";
- }
- return $sRes;
- }
- }
- /**
- * The attribute dedicated to the friendly name automatic attribute (not written)
- *
- * @package iTopORM
- */
- class AttributeComputedFieldVoid extends AttributeDefinition
- {
- static public function ListExpectedParams()
- {
- return array_merge(parent::ListExpectedParams(), array());
- }
- public function GetEditClass() {return "";}
-
- public function GetValuesDef() {return null;}
- public function GetPrerequisiteAttributes($sClass = null) {return $this->GetOptional("depends_on", array());}
- public function IsDirectField() {return true;}
- public function IsScalar() {return true;}
- public function IsWritable() {return false;}
- public function GetSQLExpr()
- {
- return null;
- }
- public function GetDefaultValue(DBObject $oHostObject = null) {return $this->MakeRealValue("", $oHostObject);}
- public function IsNullAllowed() {return false;}
- //
- // protected function ScalarToSQL($value) {return $value;} // format value as a valuable SQL literal (quoted outside)
- public function GetSQLExpressions($sPrefix = '')
- {
- if ($sPrefix == '')
- {
- $sPrefix = $this->GetCode(); // Warning AttributeComputedFieldVoid does not have any sql property
- }
- return array('' => $sPrefix);
- }
- public function FromSQLToValue($aCols, $sPrefix = '')
- {
- return null;
- }
- public function GetSQLValues($value)
- {
- return array();
- }
- public function GetSQLColumns($bFullSpec = false)
- {
- return array();
- }
- public function GetFilterDefinitions()
- {
- return array($this->GetCode() => new FilterFromAttribute($this));
- }
- public function GetBasicFilterOperators()
- {
- return array("="=>"equals", "!="=>"differs from");
- }
- public function GetBasicFilterLooseOperator()
- {
- return "=";
- }
- public function GetBasicFilterSQLExpr($sOpCode, $value)
- {
- $sQValue = CMDBSource::Quote($value);
- switch ($sOpCode)
- {
- case '!=':
- return $this->GetSQLExpr()." != $sQValue";
- break;
- case '=':
- default:
- return $this->GetSQLExpr()." = $sQValue";
- }
- }
- public function IsPartOfFingerprint() { return false; }
- }
- /**
- * The attribute dedicated to the friendly name automatic attribute (not written)
- *
- * @package iTopORM
- */
- class AttributeFriendlyName extends AttributeComputedFieldVoid
- {
- public function __construct($sCode, $sExtKeyAttCode)
- {
- $this->m_sCode = $sCode;
- $aParams = array();
- // $aParams["is_null_allowed"] = false,
- $aParams["default_value"] = '';
- $aParams["extkey_attcode"] = $sExtKeyAttCode;
- parent::__construct($sCode, $aParams);
- $this->m_sValue = $this->Get("default_value");
- }
- public function GetKeyAttCode() {return $this->Get("extkey_attcode");}
- public function GetExtAttCode() {return 'friendlyname';}
- public function GetLabel($sDefault = null)
- {
- $sLabel = parent::GetLabel('');
- if (strlen($sLabel) == 0)
- {
- $sKeyAttCode = $this->Get("extkey_attcode");
- if ($sKeyAttCode == 'id')
- {
- return Dict::S('Core:FriendlyName-Label');
- }
- else
- {
- $oExtKeyAttDef = MetaModel::GetAttributeDef($this->GetHostClass(), $sKeyAttCode);
- $sLabel = $oExtKeyAttDef->GetLabel($this->m_sCode);
- }
- }
- return $sLabel;
- }
- public function GetDescription($sDefault = null)
- {
- $sLabel = parent::GetDescription('');
- if (strlen($sLabel) == 0)
- {
- $sKeyAttCode = $this->Get("extkey_attcode");
- if ($sKeyAttCode == 'id')
- {
- return Dict::S('Core:FriendlyName-Description');
- }
- else
- {
- $oExtKeyAttDef = MetaModel::GetAttributeDef($this->GetHostClass(), $sKeyAttCode);
- $sLabel = $oExtKeyAttDef->GetDescription('');
- }
- }
- return $sLabel;
- }
- // n/a, the friendly name is made of a complex expression (see GetNameSpec)
- protected function GetSQLCol($bFullSpec = false) {return "";}
- public function FromSQLToValue($aCols, $sPrefix = '')
- {
- $sValue = $aCols[$sPrefix];
- return $sValue;
- }
- /**
- * Encrypt the value before storing it in the database
- */
- public function GetSQLValues($value)
- {
- return array();
- }
- public function IsWritable()
- {
- return false;
- }
- public function IsDirectField()
- {
- return false;
- }
- public function SetFixedValue($sValue)
- {
- $this->m_sValue = $sValue;
- }
- public function GetDefaultValue(DBObject $oHostObject = null)
- {
- return $this->m_sValue;
- }
- public function GetAsHTML($sValue, $oHostObject = null, $bLocalize = true)
- {
- return Str::pure2html((string)$sValue);
- }
- public function GetAsCSV($sValue, $sSeparator = ',', $sTextQualifier = '"', $oHostObject = null, $bLocalize = true, $bConvertToPlainText = false)
- {
- $sFrom = array("\r\n", $sTextQualifier);
- $sTo = array("\n", $sTextQualifier.$sTextQualifier);
- $sEscaped = str_replace($sFrom, $sTo, (string)$sValue);
- return $sTextQualifier.$sEscaped.$sTextQualifier;
- }
- static function GetFormFieldClass()
- {
- return '\\Combodo\\iTop\\Form\\Field\\StringField';
- }
- public function MakeFormField(DBObject $oObject, $oFormField = null)
- {
- if ($oFormField === null)
- {
- $sFormFieldClass = static::GetFormFieldClass();
- $oFormField = new $sFormFieldClass($this->GetCode());
- }
- $oFormField->SetReadOnly(true);
- parent::MakeFormField($oObject, $oFormField);
- return $oFormField;
- }
- // Do not display friendly names in the history of change
- public function DescribeChangeAsHTML($sOldValue, $sNewValue, $sLabel = null)
- {
- return '';
- }
- public function GetBasicFilterLooseOperator()
- {
- return "Contains";
- }
- public function GetBasicFilterSQLExpr($sOpCode, $value)
- {
- $sQValue = CMDBSource::Quote($value);
- switch ($sOpCode)
- {
- case '=':
- case '!=':
- return $this->GetSQLExpr()." $sOpCode $sQValue";
- case 'Contains':
- return $this->GetSQLExpr()." LIKE ".CMDBSource::Quote("%$value%");
- case 'NotLike':
- return $this->GetSQLExpr()." NOT LIKE $sQValue";
- case 'Like':
- default:
- return $this->GetSQLExpr()." LIKE $sQValue";
- }
- }
-
- public function IsPartOfFingerprint() { return false; }
- }
- /**
- * Holds the setting for the redundancy on a specific relation
- * Its value is a string, containing either:
- * - 'disabled'
- * - 'n', where n is a positive integer value giving the minimum count of items upstream
- * - 'n%', where n is a positive integer value, giving the minimum as a percentage of the total count of items upstream
- *
- * @package iTopORM
- */
- class AttributeRedundancySettings extends AttributeDBField
- {
- static public function ListExpectedParams()
- {
- return array('sql', 'relation_code', 'from_class', 'neighbour_id', 'enabled', 'enabled_mode', 'min_up', 'min_up_type', 'min_up_mode');
- }
- public function GetValuesDef() {return null;}
- public function GetPrerequisiteAttributes($sClass = null) {return array();}
- public function GetEditClass() {return "RedundancySetting";}
- protected function GetSQLCol($bFullSpec = false)
- {
- return "VARCHAR(20)".($bFullSpec ? $this->GetSQLColSpec() : '');
- }
- public function GetValidationPattern()
- {
- return "^[0-9]{1,3}|[0-9]{1,2}%|disabled$";
- }
- public function GetMaxSize()
- {
- return 20;
- }
- public function GetDefaultValue(DBObject $oHostObject = null)
- {
- $sRet = 'disabled';
- if ($this->Get('enabled'))
- {
- if ($this->Get('min_up_type') == 'count')
- {
- $sRet = (string) $this->Get('min_up');
- }
- else // percent
- {
- $sRet = $this->Get('min_up').'%';
- }
- }
- return $sRet;
- }
- public function IsNullAllowed()
- {
- return false;
- }
- public function GetNullValue()
- {
- return '';
- }
- public function IsNull($proposedValue)
- {
- return ($proposedValue == '');
- }
- public function MakeRealValue($proposedValue, $oHostObj)
- {
- if (is_null($proposedValue)) return '';
- return (string)$proposedValue;
- }
- public function ScalarToSQL($value)
- {
- if (!is_string($value))
- {
- throw new CoreException('Expected the attribute value to be a string', array('found_type' => gettype($value), 'value' => $value, 'class' => $this->GetHostClass(), 'attribute' => $this->GetCode()));
- }
- return $value;
- }
- public function GetRelationQueryData()
- {
- foreach (MetaModel::EnumRelationQueries($this->GetHostClass(), $this->Get('relation_code'), false) as $sDummy => $aQueryInfo)
- {
- if ($aQueryInfo['sFromClass'] == $this->Get('from_class'))
- {
- if ($aQueryInfo['sNeighbour'] == $this->Get('neighbour_id'))
- {
- return $aQueryInfo;
- }
- }
- }
- }
- /**
- * Find the user option label
- * @param user option : disabled|cout|percent
- */
- public function GetUserOptionFormat($sUserOption, $sDefault = null)
- {
- $sLabel = $this->SearchLabel('/Attribute:'.$this->m_sCode.'/'.$sUserOption, null, true /*user lang*/);
- if (is_null($sLabel))
- {
- // If no default value is specified, let's define the most relevant one for developping purposes
- if (is_null($sDefault))
- {
- $sDefault = str_replace('_', ' ', $this->m_sCode.':'.$sUserOption.'(%1$s)');
- }
- // Browse the hierarchy again, accepting default (english) translations
- $sLabel = $this->SearchLabel('/Attribute:'.$this->m_sCode.'/'.$sUserOption, $sDefault, false);
- }
- return $sLabel;
- }
- /**
- * Override to display the value in the GUI
- */
- public function GetAsHTML($sValue, $oHostObject = null, $bLocalize = true)
- {
- $sCurrentOption = $this->GetCurrentOption($sValue);
- $sClass = $oHostObject ? get_class($oHostObject) : $this->m_sHostClass;
- return sprintf($this->GetUserOptionFormat($sCurrentOption), $this->GetMinUpValue($sValue), MetaModel::GetName($sClass));
- }
- public function GetAsCSV($sValue, $sSeparator = ',', $sTextQualifier = '"', $oHostObject = null, $bLocalize = true, $bConvertToPlainText = false)
- {
- $sFrom = array("\r\n", $sTextQualifier);
- $sTo = array("\n", $sTextQualifier.$sTextQualifier);
- $sEscaped = str_replace($sFrom, $sTo, (string)$sValue);
- return $sTextQualifier.$sEscaped.$sTextQualifier;
- }
- /**
- * Helper to interpret the value, given the current settings and string representation of the attribute
- */
- public function IsEnabled($sValue)
- {
- if ($this->get('enabled_mode') == 'fixed')
- {
- $bRet = $this->get('enabled');
- }
- else
- {
- $bRet = ($sValue != 'disabled');
- }
- return $bRet;
- }
- /**
- * Helper to interpret the value, given the current settings and string representation of the attribute
- */
- public function GetMinUpType($sValue)
- {
- if ($this->get('min_up_mode') == 'fixed')
- {
- $sRet = $this->get('min_up_type');
- }
- else
- {
- $sRet = 'count';
- if (substr(trim($sValue), -1, 1) == '%')
- {
- $sRet = 'percent';
- }
- }
- return $sRet;
- }
- /**
- * Helper to interpret the value, given the current settings and string representation of the attribute
- */
- public function GetMinUpValue($sValue)
- {
- if ($this->get('min_up_mode') == 'fixed')
- {
- $iRet = (int) $this->Get('min_up');
- }
- else
- {
- $sRefValue = $sValue;
- if (substr(trim($sValue), -1, 1) == '%')
- {
- $sRefValue = substr(trim($sValue), 0, -1);
- }
- $iRet = (int) trim($sRefValue);
- }
- return $iRet;
- }
- /**
- * Helper to determine if the redundancy can be viewed/edited by the end-user
- */
- public function IsVisible()
- {
- $bRet = false;
- if ($this->Get('enabled_mode') == 'fixed')
- {
- $bRet = $this->Get('enabled');
- }
- elseif ($this->Get('enabled_mode') == 'user')
- {
- $bRet = true;
- }
- return $bRet;
- }
- public function IsWritable()
- {
- if (($this->Get('enabled_mode') == 'fixed') && ($this->Get('min_up_mode') == 'fixed'))
- {
- return false;
- }
- return true;
- }
- /**
- * Returns an HTML form that can be read by ReadValueFromPostedForm
- */
- public function GetDisplayForm($sCurrentValue, $oPage, $bEditMode = false, $sFormPrefix = '')
- {
- $sRet = '';
- $aUserOptions = $this->GetUserOptions($sCurrentValue);
- if (count($aUserOptions) < 2)
- {
- $bEditOption = false;
- }
- else
- {
- $bEditOption = $bEditMode;
- }
- $sCurrentOption = $this->GetCurrentOption($sCurrentValue);
- foreach($aUserOptions as $sUserOption)
- {
- $bSelected = ($sUserOption == $sCurrentOption);
- $sRet .= '<div>';
- $sRet .= $this->GetDisplayOption($sCurrentValue, $oPage, $sFormPrefix, $bEditOption, $sUserOption, $bSelected);
- $sRet .= '</div>';
- }
- return $sRet;
- }
- const USER_OPTION_DISABLED = 'disabled';
- const USER_OPTION_ENABLED_COUNT = 'count';
- const USER_OPTION_ENABLED_PERCENT = 'percent';
- /**
- * Depending on the xxx_mode parameters, build the list of options that are allowed to the end-user
- */
- protected function GetUserOptions($sValue)
- {
- $aRet = array();
- if ($this->Get('enabled_mode') == 'user')
- {
- $aRet[] = self::USER_OPTION_DISABLED;
- }
-
- if ($this->Get('min_up_mode') == 'user')
- {
- $aRet[] = self::USER_OPTION_ENABLED_COUNT;
- $aRet[] = self::USER_OPTION_ENABLED_PERCENT;
- }
- else
- {
- if ($this->GetMinUpType($sValue) == 'count')
- {
- $aRet[] = self::USER_OPTION_ENABLED_COUNT;
- }
- else
- {
- $aRet[] = self::USER_OPTION_ENABLED_PERCENT;
- }
- }
- return $aRet;
- }
- /**
- * Convert the string representation into one of the existing options
- */
- protected function GetCurrentOption($sValue)
- {
- $sRet = self::USER_OPTION_DISABLED;
- if ($this->IsEnabled($sValue))
- {
- if ($this->GetMinUpType($sValue) == 'count')
- {
- $sRet = self::USER_OPTION_ENABLED_COUNT;
- }
- else
- {
- $sRet = self::USER_OPTION_ENABLED_PERCENT;
- }
- }
- return $sRet;
- }
- /**
- * Display an option (form, or current value)
- */
- protected function GetDisplayOption($sCurrentValue, $oPage, $sFormPrefix, $bEditMode, $sUserOption, $bSelected = true)
- {
- $sRet = '';
- $iCurrentValue = $this->GetMinUpValue($sCurrentValue);
- if ($bEditMode)
- {
- $sHtmlNamesPrefix = 'rddcy_'.$this->Get('relation_code').'_'.$this->Get('from_class').'_'.$this->Get('neighbour_id');
- switch ($sUserOption)
- {
- case self::USER_OPTION_DISABLED:
- $sValue = ''; // Empty placeholder
- break;
-
- case self::USER_OPTION_ENABLED_COUNT:
- if ($bEditMode)
- {
- $sName = $sHtmlNamesPrefix.'_min_up_count';
- $sEditValue = $bSelected ? $iCurrentValue : '';
- $sValue = '<input class="redundancy-min-up-count" type="string" size="3" name="'.$sName.'" value="'.$sEditValue.'">';
- // To fix an issue on Firefox: focus set to the option (because the input is within the label for the option)
- $oPage->add_ready_script("\$('[name=\"$sName\"]').click(function(){var me=this; setTimeout(function(){\$(me).focus();}, 100);});");
- }
- else
- {
- $sValue = $iCurrentValue;
- }
- break;
-
- case self::USER_OPTION_ENABLED_PERCENT:
- if ($bEditMode)
- {
- $sName = $sHtmlNamesPrefix.'_min_up_percent';
- $sEditValue = $bSelected ? $iCurrentValue : '';
- $sValue = '<input class="redundancy-min-up-percent" type="string" size="3" name="'.$sName.'" value="'.$sEditValue.'">';
- // To fix an issue on Firefox: focus set to the option (because the input is within the label for the option)
- $oPage->add_ready_script("\$('[name=\"$sName\"]').click(function(){var me=this; setTimeout(function(){\$(me).focus();}, 100);});");
- }
- else
- {
- $sValue = $iCurrentValue;
- }
- break;
- }
- $sLabel = sprintf($this->GetUserOptionFormat($sUserOption), $sValue, MetaModel::GetName($this->GetHostClass()));
- $sOptionName = $sHtmlNamesPrefix.'_user_option';
- $sOptionId = $sOptionName.'_'.$sUserOption;
- $sChecked = $bSelected ? 'checked' : '';
- $sRet = '<input type="radio" name="'.$sOptionName.'" id="'.$sOptionId.'" value="'.$sUserOption.'"'.$sChecked.'> <label for="'.$sOptionId.'">'.$sLabel.'</label>';
- }
- else
- {
- // Read-only: display only the currently selected option
- if ($bSelected)
- {
- $sRet = sprintf($this->GetUserOptionFormat($sUserOption), $iCurrentValue, MetaModel::GetName($this->GetHostClass()));
- }
- }
- return $sRet;
- }
- /**
- * Makes the string representation out of the values given by the form defined in GetDisplayForm
- */
- public function ReadValueFromPostedForm($sFormPrefix)
- {
- $sHtmlNamesPrefix = 'rddcy_'.$this->Get('relation_code').'_'.$this->Get('from_class').'_'.$this->Get('neighbour_id');
- $iMinUpCount = (int) utils::ReadPostedParam($sHtmlNamesPrefix.'_min_up_count', null, 'raw_data');
- $iMinUpPercent = (int) utils::ReadPostedParam($sHtmlNamesPrefix.'_min_up_percent', null, 'raw_data');
- $sSelectedOption = utils::ReadPostedParam($sHtmlNamesPrefix.'_user_option', null, 'raw_data');
- switch ($sSelectedOption)
- {
- case self::USER_OPTION_ENABLED_COUNT:
- $sRet = $iMinUpCount;
- break;
- case self::USER_OPTION_ENABLED_PERCENT:
- $sRet = $iMinUpPercent.'%';
- break;
- case self::USER_OPTION_DISABLED:
- default:
- $sRet = 'disabled';
- break;
- }
- return $sRet;
- }
- }
- /**
- * Custom fields managed by an external implementation
- *
- * @package iTopORM
- */
- class AttributeCustomFields extends AttributeDefinition
- {
- static public function ListExpectedParams()
- {
- return array_merge(parent::ListExpectedParams(), array("handler_class"));
- }
- public function GetEditClass() {return "CustomFields";}
- public function IsWritable() {return true;}
- public function LoadFromDB() {return false;} // See ReadValue...
- public function GetDefaultValue(DBObject $oHostObject = null)
- {
- return new ormCustomFieldsValue($oHostObject, $this->GetCode());
- }
- public function GetBasicFilterOperators() {return array();}
- public function GetBasicFilterLooseOperator() {return '';}
- public function GetBasicFilterSQLExpr($sOpCode, $value) {return '';}
- /**
- * @param DBObject $oHostObject
- * @param array|null $aValues
- * @return CustomFieldsHandler
- */
- public function GetHandler($aValues = null)
- {
- $sHandlerClass = $this->Get('handler_class');
- $oHandler = new $sHandlerClass($this->GetCode());
- if (!is_null($aValues))
- {
- $oHandler->SetCurrentValues($aValues);
- }
- return $oHandler;
- }
- public function GetPrerequisiteAttributes($sClass = null)
- {
- $sHandlerClass = $this->Get('handler_class');
- return $sHandlerClass::GetPrerequisiteAttributes($sClass);
- }
- public function GetEditValue($sValue, $oHostObj = null)
- {
- return 'GetEditValueNotImplemented for '.get_class($this);
- }
- /**
- * Makes the string representation out of the values given by the form defined in GetDisplayForm
- */
- public function ReadValueFromPostedForm($oHostObject, $sFormPrefix)
- {
- $aRawData = json_decode(utils::ReadPostedParam("attr_{$sFormPrefix}{$this->GetCode()}", '{}', 'raw_data'), true);
- return new ormCustomFieldsValue($oHostObject, $this->GetCode(), $aRawData);
- }
- public function MakeRealValue($proposedValue, $oHostObject)
- {
- if (is_object($proposedValue) && ($proposedValue instanceof ormCustomFieldsValue))
- {
- return $proposedValue;
- }
- elseif (is_string($proposedValue))
- {
- $aValues = json_decode($proposedValue, true);
- return new ormCustomFieldsValue($oHostObject, $this->GetCode(), $aValues);
- }
- elseif (is_array($proposedValue))
- {
- return new ormCustomFieldsValue($oHostObject, $this->GetCode(), $proposedValue);
- }
- throw new Exception('Unexpected type for the value of a custom fields attribute: '.gettype($proposedValue));
- }
- static public function GetFormFieldClass()
- {
- return '\\Combodo\\iTop\\Form\\Field\\SubFormField';
- }
- /**
- * Override to build the relevant form field
- *
- * When called first, $oFormField is null and will be created (eg. Make). Then when the ::parent is called and the $oFormField is passed, MakeFormField behaves more like a Prepare.
- */
- public function MakeFormField(DBObject $oObject, $oFormField = null)
- {
- if ($oFormField === null)
- {
- $sFormFieldClass = static::GetFormFieldClass();
- $oFormField = new $sFormFieldClass($this->GetCode());
- $oFormField->SetForm($this->GetForm($oObject));
- }
- parent::MakeFormField($oObject, $oFormField);
-
- return $oFormField;
- }
- /**
- * @param DBObject $oHostObject
- * @return Combodo\iTop\Form\Form
- */
- public function GetForm(DBObject $oHostObject, $sFormPrefix = null)
- {
- try
- {
- $oValue = $oHostObject->Get($this->GetCode());
- $oHandler = $this->GetHandler($oValue->GetValues());
- $sFormId = is_null($sFormPrefix) ? 'cf_'.$this->GetCode() : $sFormPrefix.'_cf_'.$this->GetCode();
- $oHandler->BuildForm($oHostObject, $sFormId);
- $oForm = $oHandler->GetForm();
- }
- catch (Exception $e)
- {
- $oForm = new \Combodo\iTop\Form\Form('');
- $oField = new \Combodo\iTop\Form\Field\LabelField('');
- $oField->SetLabel('Custom field error: '.$e->getMessage());
- $oForm->AddField($oField);
- $oForm->Finalize();
- }
- return $oForm;
- }
- /**
- * Read the data from where it has been stored. This verb must be implemented as soon as LoadFromDB returns false and LoadInObject returns true
- * @param $oHostObject
- * @return ormCustomFieldsValue
- */
- public function ReadValue($oHostObject)
- {
- try
- {
- $oHandler = $this->GetHandler();
- $aValues = $oHandler->ReadValues($oHostObject);
- $oRet = new ormCustomFieldsValue($oHostObject, $this->GetCode(), $aValues);
- }
- catch (Exception $e)
- {
- $oRet = new ormCustomFieldsValue($oHostObject, $this->GetCode());
- }
- return $oRet;
- }
- /**
- * Record the data (currently in the processing of recording the host object)
- * It is assumed that the data has been checked prior to calling Write()
- * @param DBObject $oHostObject
- * @param ormCustomFieldsValue|null $oValue (null is the default value)
- */
- public function WriteValue(DBObject $oHostObject, ormCustomFieldsValue $oValue = null)
- {
- if (is_null($oValue))
- {
- $oHandler = $this->GetHandler();
- $aValues = array();
- }
- else
- {
- // Pass the values through the form to make sure that they are correct
- $oHandler = $this->GetHandler($oValue->GetValues());
- $oHandler->BuildForm($oHostObject, '');
- $oForm = $oHandler->GetForm();
- $aValues = $oForm->GetCurrentValues();
- }
- return $oHandler->WriteValues($oHostObject, $aValues);
- }
- /**
- * The part of the current attribute in the object's signature, for the supplied value
- * @param $value The value of this attribute for the object
- * @return string The "signature" for this field/attribute
- */
- public function Fingerprint($value)
- {
- $oHandler = $this->GetHandler($value->GetValues());
- return $oHandler->GetValueFingerprint();
- }
- /**
- * Check the validity of the data
- * @param DBObject $oHostObject
- * @param $value
- * @return bool|string true or error message
- */
- public function CheckValue(DBObject $oHostObject, $value)
- {
- try
- {
- $oHandler = $this->GetHandler($value->GetValues());
- $oHandler->BuildForm($oHostObject, '');
- $oForm = $oHandler->GetForm();
- $oForm->Validate();
- if ($oForm->GetValid())
- {
- $ret = true;
- }
- else
- {
- $aMessages = array();
- foreach ($oForm->GetErrorMessages() as $sFieldId => $aFieldMessages)
- {
- $aMessages[] = $sFieldId.': '.implode(', ', $aFieldMessages);
- }
- $ret = 'Invalid value: '.implode(', ', $aMessages);
- }
- }
- catch (Exception $e)
- {
- $ret = $e->getMessage();
- }
- return $ret;
- }
- /**
- * Cleanup data upon object deletion (object id still available here)
- * @param DBObject $oHostObject
- */
- public function DeleteValue(DBObject $oHostObject)
- {
- $oValue = $oHostObject->Get($this->GetCode());
- $oHandler = $this->GetHandler($oValue->GetValues());
- return $oHandler->DeleteValues($oHostObject);
- }
- public function GetAsHTML($value, $oHostObject = null, $bLocalize = true)
- {
- try
- {
- $sRet = $value->GetAsHTML($bLocalize);
- }
- catch (Exception $e)
- {
- $sRet = 'Custom field error: '.htmlentities($e->getMessage(), ENT_QUOTES, 'UTF-8');
- }
- return $sRet;
- }
- public function GetAsXML($value, $oHostObject = null, $bLocalize = true)
- {
- try
- {
- $sRet = $value->GetAsXML($bLocalize);
- }
- catch (Exception $e)
- {
- $sRet = Str::pure2xml('Custom field error: '.$e->getMessage());
- }
- return $sRet;
- }
- public function GetAsCSV($value, $sSeparator = ',', $sTextQualifier = '"', $oHostObject = null, $bLocalize = true, $bConvertToPlainText = false)
- {
- try
- {
- $sRet = $value->GetAsCSV($sSeparator, $sTextQualifier, $bLocalize, $bConvertToPlainText);
- }
- catch (Exception $e)
- {
- $sFrom = array("\r\n", $sTextQualifier);
- $sTo = array("\n", $sTextQualifier.$sTextQualifier);
- $sEscaped = str_replace($sFrom, $sTo, 'Custom field error: '.$e->getMessage());
- $sRet = $sTextQualifier.$sEscaped.$sTextQualifier;
- }
- return $sRet;
- }
- /**
- * List the available verbs for 'GetForTemplate'
- */
- public function EnumTemplateVerbs()
- {
- $sHandlerClass = $this->Get('handler_class');
- return $sHandlerClass::EnumTemplateVerbs();
- }
- /**
- * Get various representations of the value, for insertion into a template (e.g. in Notifications)
- * @param $value mixed The current value of the field
- * @param $sVerb string The verb specifying the representation of the value
- * @param $oHostObject DBObject The object
- * @param $bLocalize bool Whether or not to localize the value
- */
- public function GetForTemplate($value, $sVerb, $oHostObject = null, $bLocalize = true)
- {
- try
- {
- $sRet = $value->GetForTemplate($sVerb, $bLocalize);
- }
- catch (Exception $e)
- {
- $sRet = 'Custom field error: '.$e->getMessage();
- }
- return $sRet;
- }
- public function MakeValueFromString($sProposedValue, $bLocalizedValue = false, $sSepItem = null, $sSepAttribute = null, $sSepValue = null, $sAttributeQualifier = null)
- {
- return null;
- }
- /**
- * Helper to get a value that will be JSON encoded
- * The operation is the opposite to FromJSONToValue
- */
- public function GetForJSON($value)
- {
- return null;
- }
- /**
- * Helper to form a value, given JSON decoded data
- * The operation is the opposite to GetForJSON
- */
- public function FromJSONToValue($json)
- {
- return null;
- }
- public function Equals($val1, $val2)
- {
- try
- {
- $bEquals = $val1->Equals($val2);
- }
- catch (Exception $e)
- {
- false;
- }
- return $bEquals;
- }
- }
|