data_process.js 159 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548
  1. /*
  2. CardDavMATE - the open source CardDAV Web Client
  3. Copyright (C) 2011-2015
  4. Jan Mate <jan.mate@inf-it.com>
  5. Andrej Lezo <andrej.lezo@inf-it.com>
  6. Matej Mihalik <matej.mihalik@inf-it.com>
  7. This program is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU Affero General Public License as
  9. published by the Free Software Foundation, either version 3 of the
  10. License, or (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU Affero General Public License for more details.
  15. You should have received a copy of the GNU Affero General Public License
  16. along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. function dataToVcard(accountUID, inputUID, inputFilterUID, inputEtag)
  19. {
  20. var vCardText='';
  21. var groupCounter=0;
  22. var tmpvCardEditorRef=$('#vCardEditor');
  23. if(typeof globalDisabledContactAttributes=='undefined' || !(globalDisabledContactAttributes instanceof Array))
  24. globalDisabledContactAttributes=[];
  25. // vCard BEGIN (required by RFC)
  26. if(vCard.tplM['begin']!=null && (process_elem=vCard.tplM['begin'][0])!=undefined)
  27. vCardText+=vCard.tplM['begin'][0];
  28. else
  29. {
  30. process_elem=vCard.tplC['begin'];
  31. process_elem=process_elem.replace('##:::##group_wd##:::##','');
  32. vCardText+=process_elem;
  33. }
  34. // VERSION (required by RFC)
  35. if(vCard.tplM['contentline_VERSION']!=null && (process_elem=vCard.tplM['contentline_VERSION'][0])!=undefined)
  36. {
  37. // replace the object and related objects' group names (+ append the related objects after the processed)
  38. parsed=('\r\n'+process_elem).match(RegExp('\r\n((?:'+vCard.re['group']+'\\.)?)','m'));
  39. if(parsed[1]!='') // if group is present, replace the object and related objects' group names
  40. process_elem=('\r\n'+process_elem).replace(RegExp('\r\n'+parsed[1].replace('.','\\.'),'mg'),'\r\nitem'+(groupCounter++)+'.').substring(2);
  41. }
  42. else
  43. {
  44. process_elem=vCard.tplC['contentline_VERSION'];
  45. process_elem=process_elem.replace('##:::##group_wd##:::##', '');
  46. process_elem=process_elem.replace('##:::##version##:::##', '3.0');
  47. }
  48. vCardText+=process_elem;
  49. // UID (required by RFC)
  50. var newUID='';
  51. if(vCard.tplM['contentline_UID']!=null && (process_elem=vCard.tplM['contentline_UID'][0])!=undefined)
  52. {
  53. // replace the object and related objects' group names (+ append the related objects after the processed)
  54. parsed=('\r\n'+process_elem).match(RegExp('\r\n((?:'+vCard.re['group']+'\\.)?)','m'));
  55. if(parsed[1]!='') // if group is present, replace the object and related objects' group names
  56. process_elem=('\r\n'+process_elem).replace(RegExp('\r\n'+parsed[1].replace('.','\\.'),'mg'),'\r\nitem'+(groupCounter++)+'.').substring(2);
  57. }
  58. else
  59. {
  60. process_elem=vCard.tplC['contentline_UID'];
  61. process_elem=process_elem.replace('##:::##group_wd##:::##', '');
  62. process_elem=process_elem.replace('##:::##params_wsc##:::##', '');
  63. newUID=globalAddressbookList.getNewUID();
  64. // it is VERY small probability, that for 2 newly created contacts the same UID is generated (but not impossible :( ...)
  65. process_elem=process_elem.replace('##:::##uid##:::##',newUID);
  66. }
  67. vCardText+=process_elem;
  68. // N (required by RFC)
  69. if(vCard.tplM['contentline_N']!=null && (process_elem=vCard.tplM['contentline_N'][0])!=undefined)
  70. {
  71. // replace the object and related objects' group names (+ append the related objects after the processed)
  72. parsed=('\r\n'+process_elem).match(RegExp('\r\n((?:'+vCard.re['group']+'\\.)?)','m'));
  73. if(parsed[1]!='') // if group is present, replace the object and related objects' group names
  74. process_elem=('\r\n'+process_elem).replace(RegExp('\r\n'+parsed[1].replace('.','\\.'),'mg'),'\r\nitem'+(groupCounter++)+'.').substring(2);
  75. }
  76. else
  77. {
  78. process_elem=vCard.tplC['contentline_N'];
  79. process_elem=process_elem.replace('##:::##group_wd##:::##', '');
  80. process_elem=process_elem.replace('##:::##params_wsc##:::##', '');
  81. }
  82. process_elem=process_elem.replace('##:::##family##:::##',vcardEscapeValue(tmpvCardEditorRef.find('[data-type="family"]').val()));
  83. process_elem=process_elem.replace('##:::##given##:::##',vcardEscapeValue(tmpvCardEditorRef.find('[data-type="given"]').val()));
  84. process_elem=process_elem.replace('##:::##middle##:::##',vcardEscapeValue(tmpvCardEditorRef.find('[data-type="middle"]').val()));
  85. process_elem=process_elem.replace('##:::##prefix##:::##',vcardEscapeValue(tmpvCardEditorRef.find('[data-type="prefix"]').val()));
  86. process_elem=process_elem.replace('##:::##suffix##:::##',vcardEscapeValue(tmpvCardEditorRef.find('[data-type="suffix"]').val()));
  87. vCardText+=process_elem;
  88. // FN (extracted from newly created N [previous "process_elem"], required by RFC)
  89. // parsed (contentline_parse) = [1]->"group.", [2]->"name", [3]->";param;param", [4]->"value"
  90. parsed=('\r\n'+process_elem).match(vCard.pre['contentline_parse']);
  91. // parsed_value = [0]->Family, [1]->Given, [2]->Middle, [3]->Prefix, [4]->Suffix
  92. parsed_value=vcardSplitValue(parsed[4],';');
  93. // XXX toto je blbost, v settingsoch predsa musi byt jednoznacne ci sa uklada format A alebo B
  94. /* backward compatibility for stupid users (remove it in future) */
  95. if(typeof globalSettings.contactstorefn.value=='string')
  96. var tmp=globalSettings.contactstorefn.value.replace(RegExp(',', 'g'),', ').split(',');
  97. else /* new configuration options (arrays) */
  98. var tmp=globalSettings.contactstorefn.value.slice(); // copy the configuration array
  99. var first_found=false;
  100. for(var i=0;i<tmp.length;i++)
  101. {
  102. var tmp_found=false;
  103. if(tmp[i].match(RegExp('surname|lastname|last|family','ig'))!=null)
  104. {
  105. if(parsed_value[0]=='')
  106. tmp[i]='';
  107. else
  108. {
  109. tmp[i]=tmp[i].replace(RegExp((!first_found ? '.*' : '')+'(surname|lastname|last|family)','ig'),parsed_value[0]);
  110. first_found=true;
  111. }
  112. }
  113. if(tmp[i].match(RegExp('firstname|first|given','ig'))!=null)
  114. {
  115. if(parsed_value[1]=='')
  116. tmp[i]='';
  117. else
  118. {
  119. tmp[i]=tmp[i].replace(RegExp((!first_found ? '.*' : '')+'(firstname|first|given)','ig'),parsed_value[1]);
  120. first_found=true;
  121. }
  122. }
  123. if(tmp[i].match(RegExp('middlename|middle','ig'))!=null)
  124. {
  125. if(parsed_value[2]=='')
  126. tmp[i]='';
  127. else
  128. {
  129. tmp[i]=tmp[i].replace(RegExp((!first_found ? '.*' : '')+'(middlename|middle)','ig'),parsed_value[2]);
  130. first_found=true;
  131. }
  132. }
  133. if(tmp[i].match(RegExp('prefix','ig'))!=null)
  134. {
  135. if(parsed_value[3]=='')
  136. tmp[i]='';
  137. else
  138. {
  139. tmp[i]=tmp[i].replace(RegExp((!first_found ? '.*' : '')+'prefix','ig'),parsed_value[3]);
  140. first_found=true;
  141. }
  142. }
  143. if(tmp[i].match(RegExp('suffix','ig'))!=null)
  144. {
  145. if(parsed_value[4]=='')
  146. tmp[i]='';
  147. else
  148. {
  149. tmp[i]=tmp[i].replace(RegExp((!first_found ? '.*' : '')+'suffix','ig'),parsed_value[4]);
  150. first_found=true;
  151. }
  152. }
  153. }
  154. fn_value=tmp.join('');
  155. if(fn_value=='') //empty FN -> we use the company name as FN
  156. fn_value=vcardEscapeValue(tmpvCardEditorRef.find('[data-type="org"]').val());
  157. if(vCard.tplM['contentline_FN']!=null && (process_elem=vCard.tplM['contentline_FN'][0])!=undefined)
  158. {
  159. // replace the object and related objects' group names (+ append the related objects after the processed)
  160. parsed=('\r\n'+process_elem).match(RegExp('\r\n((?:'+vCard.re['group']+'\\.)?)','m'));
  161. if(parsed[1]!='') // if group is present, replace the object and related objects' group names
  162. process_elem=('\r\n'+process_elem).replace(RegExp('\r\n'+parsed[1].replace('.','\\.'),'mg'),'\r\nitem'+(groupCounter++)+'.').substring(2);
  163. }
  164. else
  165. {
  166. process_elem=vCard.tplC['contentline_FN'];
  167. process_elem=process_elem.replace('##:::##group_wd##:::##', '');
  168. process_elem=process_elem.replace('##:::##params_wsc##:::##', '');
  169. }
  170. process_elem=process_elem.replace('##:::##fn##:::##',fn_value);
  171. vCardText+=process_elem;
  172. // CATEGORIES
  173. if(globalDisabledContactAttributes.indexOf('CATEGORIES')==-1 && (value=tmpvCardEditorRef.find('[data-type="\\%categories"]').find('input[data-type="value"]').val())!='')
  174. {
  175. if(vCard.tplM['contentline_CATEGORIES']!=null && (process_elem=vCard.tplM['contentline_CATEGORIES'][0])!=undefined)
  176. {
  177. // replace the object and related objects' group names (+ append the related objects after the processed)
  178. parsed=('\r\n'+process_elem).match(RegExp('\r\n((?:'+vCard.re['group']+'\\.)?)','m'));
  179. if(parsed[1]!='') // if group is present, replace the object and related objects' group names
  180. process_elem=('\r\n'+process_elem).replace(RegExp('\r\n'+parsed[1].replace('.','\\.'),'mg'),'\r\nitem'+(groupCounter++)+'.').substring(2);
  181. }
  182. else
  183. {
  184. process_elem=vCard.tplC['contentline_CATEGORIES'];
  185. process_elem=process_elem.replace('##:::##group_wd##:::##', '');
  186. process_elem=process_elem.replace('##:::##params_wsc##:::##', '');
  187. }
  188. process_elem=process_elem.replace('##:::##value##:::##', value); // we do not need to escape the value here!
  189. vCardText+=process_elem;
  190. }
  191. // NOTE
  192. if(globalDisabledContactAttributes.indexOf('NOTE')==-1 && (value=tmpvCardEditorRef.find('[data-type="\\%note"]').find('textarea').val())!='')
  193. {
  194. if(vCard.tplM['contentline_NOTE']!=null && (process_elem=vCard.tplM['contentline_NOTE'][0])!=undefined)
  195. {
  196. // replace the object and related objects' group names (+ append the related objects after the processed)
  197. parsed=('\r\n'+process_elem).match(RegExp('\r\n((?:'+vCard.re['group']+'\\.)?)','m'));
  198. if(parsed[1]!='') // if group is present, replace the object and related objects' group names
  199. process_elem=('\r\n'+process_elem).replace(RegExp('\r\n'+parsed[1].replace('.','\\.'),'mg'),'\r\nitem'+(groupCounter++)+'.').substring(2);
  200. }
  201. else
  202. {
  203. process_elem=vCard.tplC['contentline_NOTE'];
  204. process_elem=process_elem.replace('##:::##group_wd##:::##', '');
  205. process_elem=process_elem.replace('##:::##params_wsc##:::##', '');
  206. }
  207. process_elem=process_elem.replace('##:::##value##:::##',vcardEscapeValue(value));
  208. vCardText+=process_elem;
  209. }
  210. // REV
  211. if(vCard.tplM['contentline_REV']!=null && (process_elem=vCard.tplM['contentline_REV'][0])!=undefined)
  212. {
  213. // replace the object and related objects' group names (+ append the related objects after the processed)
  214. parsed=('\r\n'+process_elem).match(RegExp('\r\n((?:'+vCard.re['group']+'\\.)?)','m'));
  215. if(parsed[1]!='') // if group is present, replace the object and related objects' group names
  216. process_elem=('\r\n'+process_elem).replace(RegExp('\r\n'+parsed[1].replace('.','\\.'),'mg'),'\r\nitem'+(groupCounter++)+'.').substring(2);
  217. }
  218. else
  219. {
  220. process_elem=vCard.tplC['contentline_REV'];
  221. process_elem=process_elem.replace('##:::##group_wd##:::##', '');
  222. }
  223. process_elem=process_elem.replace('##:::##params_wsc##:::##', '');
  224. var d = new Date();
  225. var utc=d.getUTCFullYear()+(d.getUTCMonth()+1<10 ? '0':'')+(d.getUTCMonth()+1)+(d.getUTCDate()<10 ? '0':'')+d.getUTCDate()+'T'+(d.getUTCHours()<10 ? '0':'')+d.getUTCHours()+(d.getUTCMinutes()<10 ? '0':'')+d.getUTCMinutes()+(d.getUTCSeconds()<10 ? '0':'')+d.getUTCSeconds()+'Z';
  226. process_elem=process_elem.replace('##:::##value##:::##', utc);
  227. vCardText+=process_elem;
  228. // NICKNAME
  229. if(globalDisabledContactAttributes.indexOf('NICKNAME')==-1 && (value=tmpvCardEditorRef.find('[data-type="nickname"]').val())!='')
  230. {
  231. if(vCard.tplM['contentline_NICKNAME']!=null && (process_elem=vCard.tplM['contentline_NICKNAME'][0])!=undefined)
  232. {
  233. // replace the object and related objects' group names (+ append the related objects after the processed)
  234. parsed=('\r\n'+process_elem).match(RegExp('\r\n((?:'+vCard.re['group']+'\\.)?)','m'));
  235. if(parsed[1]!='') // if group is present, replace the object and related objects' group names
  236. process_elem=('\r\n'+process_elem).replace(RegExp('\r\n'+parsed[1].replace('.','\\.'),'mg'),'\r\nitem'+(groupCounter++)+'.').substring(2);
  237. }
  238. else
  239. {
  240. process_elem=vCard.tplC['contentline_NICKNAME'];
  241. process_elem=process_elem.replace('##:::##group_wd##:::##', '');
  242. process_elem=process_elem.replace('##:::##params_wsc##:::##', '');
  243. }
  244. process_elem=process_elem.replace('##:::##value##:::##', vcardEscapeValue(value));
  245. vCardText+=process_elem;
  246. }
  247. // X-PHONETIC-FIRST-NAME
  248. if(globalDisabledContactAttributes.indexOf('X-PHONETIC-FIRST-NAME')==-1 && (value=tmpvCardEditorRef.find('[data-type="ph_firstname"]').val())!='')
  249. {
  250. if(vCard.tplM['contentline_X-PHONETIC-FIRST-NAME']!=null && (process_elem=vCard.tplM['contentline_X-PHONETIC-FIRST-NAME'][0])!=undefined)
  251. {
  252. // replace the object and related objects' group names (+ append the related objects after the processed)
  253. parsed=('\r\n'+process_elem).match(RegExp('\r\n((?:'+vCard.re['group']+'\\.)?)','m'));
  254. if(parsed[1]!='') // if group is present, replace the object and related objects' group names
  255. process_elem=('\r\n'+process_elem).replace(RegExp('\r\n'+parsed[1].replace('.','\\.'),'mg'),'\r\nitem'+(groupCounter++)+'.').substring(2);
  256. }
  257. else
  258. {
  259. process_elem=vCard.tplC['contentline_X-PHONETIC-FIRST-NAME'];
  260. process_elem=process_elem.replace('##:::##group_wd##:::##', '');
  261. process_elem=process_elem.replace('##:::##params_wsc##:::##', '');
  262. }
  263. process_elem=process_elem.replace('##:::##value##:::##',vcardEscapeValue(value));
  264. vCardText+=process_elem;
  265. }
  266. // X-PHONETIC-LAST-NAME
  267. if(globalDisabledContactAttributes.indexOf('X-PHONETIC-LAST-NAME')==-1 && (value=tmpvCardEditorRef.find('[data-type="ph_lastname"]').val())!='')
  268. {
  269. if(vCard.tplM['contentline_X-PHONETIC-LAST-NAME']!=null && (process_elem=vCard.tplM['contentline_X-PHONETIC-LAST-NAME'][0])!=undefined)
  270. {
  271. // replace the object and related objects' group names (+ append the related objects after the processed)
  272. parsed=('\r\n'+process_elem).match(RegExp('\r\n((?:'+vCard.re['group']+'\\.)?)','m'));
  273. if(parsed[1]!='') // if group is present, replace the object and related objects' group names
  274. process_elem=('\r\n'+process_elem).replace(RegExp('\r\n'+parsed[1].replace('.','\\.'),'mg'),'\r\nitem'+(groupCounter++)+'.').substring(2);
  275. }
  276. else
  277. {
  278. process_elem=vCard.tplC['contentline_X-PHONETIC-LAST-NAME'];
  279. process_elem=process_elem.replace('##:::##group_wd##:::##', '');
  280. process_elem=process_elem.replace('##:::##params_wsc##:::##', '');
  281. }
  282. process_elem=process_elem.replace('##:::##value##:::##',vcardEscapeValue(value));
  283. vCardText+=process_elem;
  284. }
  285. // BDAY
  286. if(globalDisabledContactAttributes.indexOf('BDAY')==-1 && (value=tmpvCardEditorRef.find('[data-type="date_bday"]').val())!='')
  287. {
  288. var valid=true;
  289. try {var date=$.datepicker.parseDate(globalSettings.datepickerformat.value, value)}
  290. catch (e) {valid=false}
  291. if(valid==true)
  292. {
  293. if(vCard.tplM['contentline_BDAY']!=null && (process_elem=vCard.tplM['contentline_BDAY'][0])!=undefined)
  294. {
  295. // replace the object and related objects' group names (+ append the related objects after the processed)
  296. parsed=('\r\n'+process_elem).match(RegExp('\r\n((?:'+vCard.re['group']+'\\.)?)','m'));
  297. if(parsed[1]!='') // if group is present, replace the object and related objects' group names
  298. process_elem=('\r\n'+process_elem).replace(RegExp('\r\n'+parsed[1].replace('.','\\.'),'mg'),'\r\nitem'+(groupCounter++)+'.').substring(2);
  299. }
  300. else
  301. {
  302. process_elem=vCard.tplC['contentline_BDAY'];
  303. process_elem=process_elem.replace('##:::##group_wd##:::##', '');
  304. process_elem=process_elem.replace('##:::##params_wsc##:::##', ';VALUE=date');
  305. }
  306. process_elem=process_elem.replace('##:::##value##:::##',vcardEscapeValue($.datepicker.formatDate('yy-mm-dd', date)));
  307. vCardText+=process_elem;
  308. }
  309. }
  310. // X-ABDATE
  311. if(globalDisabledContactAttributes.indexOf('X-ABDATE')==-1)
  312. {
  313. tmpvCardEditorRef.find('[data-type="\\%date"]').each(
  314. function (index,element)
  315. {
  316. if((value=$(element).find('[data-type="date_value"]').val())!='')
  317. {
  318. var valid=true;
  319. try {var date=$.datepicker.parseDate(globalSettings.datepickerformat.value, value)}
  320. catch (e) {valid=false}
  321. if(valid==true)
  322. {
  323. incGroupCounter=false;
  324. if(vCard.tplM['contentline_X-ABDATE']!=null && (process_elem=vCard.tplM['contentline_X-ABDATE'][$(element).attr('data-id')])!=undefined)
  325. {
  326. // replace the object and related objects' group names (+ append the related objects after the processed)
  327. parsed=('\r\n'+process_elem).match(RegExp('\r\n((?:'+vCard.re['group']+'\\.)?)','m'));
  328. if(parsed[1]!='') // if group is present, replace the object and related objects' group names
  329. {
  330. process_elem=('\r\n'+process_elem).replace(RegExp('\r\n'+parsed[1].replace('.','\\.'),'mg'),'\r\nitem'+(groupCounter)+'.').substring(2);
  331. incGroupCounter=true;
  332. }
  333. }
  334. else
  335. {
  336. process_elem=vCard.tplC['contentline_X-ABDATE'];
  337. process_elem=process_elem.replace('##:::##group_wd##:::##', '');
  338. }
  339. var date_value=$.datepicker.formatDate('yy-mm-dd', date);
  340. var tmp_type=$(element).find('[data-type="date_type"] option').filter(':selected').attr('data-type');
  341. /* construct the "custom" type */
  342. if(tmp_type==':custom')
  343. {
  344. var tmp_cust_value=$(element).find('[data-type="custom_value"]').val();
  345. var tmp_cust_value_processed=tmp_cust_value.replace(RegExp('^\\s*|\\s*$','g'),'').replaceAll(' ',' ');
  346. // if a custom type is already defined as standard type, use the standard definition
  347. if((tmp_cust_already_exists=$(element).find('[data-type="date_type"] option').filter(function(){return $(this).html()==tmp_cust_value_processed;}).attr('data-type'))!=undefined)
  348. tmp_type=tmp_cust_already_exists;
  349. else // use custom type
  350. tmp_type=':'+tmp_cust_value+':';
  351. }
  352. params_wsc='';
  353. tmp_normal_types=tmp_type.replace(RegExp(':.*:','g'),',').replaceAll(',,',',').replace(RegExp('^,|,$','g'),'');
  354. if(tmp_normal_types!='')
  355. params_wsc=';TYPE='+vcardEscapeValue(tmp_normal_types).toUpperCase().replace(RegExp('\\\\,','g'),';TYPE=');
  356. process_elem=process_elem.replace('##:::##params_wsc##:::##',params_wsc);
  357. process_elem=process_elem.replace('##:::##value##:::##',vcardEscapeValue(date_value));
  358. my_related='';
  359. tmp_related_type=tmp_type.match(RegExp(':(.*):')); // only one element of related (X-ABLabel) is supported
  360. if(tmp_related_type!=null && tmp_related_type[1]!='')
  361. my_related='X-ABLabel:'+vcardEscapeValue((dataTypes['date_store_as'][tmp_related_type[1]]!=undefined ? dataTypes['date_store_as'][tmp_related_type[1]] : tmp_related_type[1]))+'\r\n';
  362. if(my_related!='')
  363. {
  364. incGroupCounter=true;
  365. parsed=('\r\n'+process_elem).match(vCard.pre['contentline_parse']);
  366. if(parsed[1]!='') // if group is present, we use it, otherwise we create a new group
  367. process_elem+=parsed[1]+my_related;
  368. else
  369. process_elem='item'+groupCounter+'.'+process_elem+'item'+groupCounter+'.'+my_related;
  370. }
  371. if(incGroupCounter) groupCounter++;
  372. if(globalSettings.compatibility.value.anniversaryOutputFormat.indexOf('other')!=-1)
  373. {
  374. // X-ANNIVERSARY
  375. if(tmp_type==':_$!<anniversary>!$_:')
  376. {
  377. if(globalSettings.compatibility.value.anniversaryOutputFormat.indexOf('apple')!=-1)
  378. vCardText+=process_elem;
  379. process_elem='X-ANNIVERSARY;VALUE=date:'+vcardEscapeValue(date_value)+'\r\n';
  380. }
  381. }
  382. vCardText+=process_elem;
  383. }
  384. }
  385. });
  386. }
  387. // TITLE
  388. if(globalDisabledContactAttributes.indexOf('TITLE')==-1 && (value=tmpvCardEditorRef.find('[data-type="title"]').val())!='')
  389. {
  390. if(vCard.tplM['contentline_TITLE']!=null && (process_elem=vCard.tplM['contentline_TITLE'][0])!=undefined)
  391. {
  392. // replace the object and related objects' group names (+ append the related objects after the processed)
  393. parsed=('\r\n'+process_elem).match(RegExp('\r\n((?:'+vCard.re['group']+'\\.)?)','m'));
  394. if(parsed[1]!='') // if group is present, replace the object and related objects' group names
  395. process_elem=('\r\n'+process_elem).replace(RegExp('\r\n'+parsed[1].replace('.','\\.'),'mg'),'\r\nitem'+(groupCounter++)+'.').substring(2);
  396. }
  397. else
  398. {
  399. process_elem=vCard.tplC['contentline_TITLE'];
  400. process_elem=process_elem.replace('##:::##group_wd##:::##', '');
  401. process_elem=process_elem.replace('##:::##params_wsc##:::##', '');
  402. }
  403. process_elem=process_elem.replace('##:::##value##:::##',vcardEscapeValue(value));
  404. vCardText+=process_elem;
  405. }
  406. // ORG
  407. if(globalDisabledContactAttributes.indexOf('ORG')==-1)
  408. {
  409. value=tmpvCardEditorRef.find('[data-type="org"]:visible:not([readonly])').val();
  410. value2=tmpvCardEditorRef.find('[data-type="department"]:visible:not([readonly])').val();
  411. if((value!=undefined && value!='') || (value2!=undefined && value2!=''))
  412. {
  413. if(vCard.tplM['contentline_ORG']!=null && (process_elem=vCard.tplM['contentline_ORG'][0])!=undefined)
  414. {
  415. // replace the object and related objects' group names (+ append the related objects after the processed)
  416. parsed=('\r\n'+process_elem).match(RegExp('\r\n((?:'+vCard.re['group']+'\\.)?)','m'));
  417. if(parsed[1]!='') // if group is present, replace the object and related objects' group names
  418. process_elem=('\r\n'+process_elem).replace(RegExp('\r\n'+parsed[1].replace('.','\\.'),'mg'),'\r\nitem'+(groupCounter++)+'.').substring(2);
  419. }
  420. else
  421. {
  422. process_elem=vCard.tplC['contentline_ORG'];
  423. process_elem=process_elem.replace('##:::##group_wd##:::##', '');
  424. process_elem=process_elem.replace('##:::##params_wsc##:::##', '');
  425. process_elem=process_elem.replace('##:::##units_wsc##:::##', '');
  426. }
  427. process_elem=process_elem.replace('##:::##org##:::##',vcardEscapeValue(value)+(value2!=undefined && value2!='' ? ';'+vcardEscapeValue(value2) : ''));
  428. vCardText+=process_elem;
  429. }
  430. }
  431. // X-ABShowAs
  432. if(globalDisabledContactAttributes.indexOf('X-ABShowAs')==-1 && tmpvCardEditorRef.find('[data-type="isorg"]').prop('checked'))
  433. {
  434. if(vCard.tplM['contentline_X-ABShowAs']!=null && (process_elem=vCard.tplM['contentline_X-ABShowAs'][0])!=undefined)
  435. {
  436. // replace the object and related objects' group names (+ append the related objects after the processed)
  437. parsed=('\r\n'+process_elem).match(RegExp('\r\n((?:'+vCard.re['group']+'\\.)?)','m'));
  438. if(parsed[1]!='') // if group is present, replace the object and related objects' group names
  439. process_elem=('\r\n'+process_elem).replace(RegExp('\r\n'+parsed[1].replace('.','\\.'),'mg'),'\r\nitem'+(groupCounter++)+'.').substring(2);
  440. }
  441. else
  442. {
  443. process_elem=vCard.tplC['contentline_X-ABShowAs'];
  444. process_elem=process_elem.replace('##:::##group_wd##:::##', '');
  445. process_elem=process_elem.replace('##:::##params_wsc##:::##', '');
  446. process_elem=process_elem.replace('##:::##value##:::##', 'COMPANY');
  447. }
  448. vCardText+=process_elem;
  449. }
  450. // PHOTO
  451. if(globalDisabledContactAttributes.indexOf('PHOTO')==-1 && !tmpvCardEditorRef.find('#photo').hasClass('photo_blank'))
  452. {
  453. var value = $('#photoURLHidden').val() || tmpvCardEditorRef.find('#photo').get(0).toDataURL('image/png');
  454. if(vCard.tplM['contentline_PHOTO']!=null && (process_elem=vCard.tplM['contentline_PHOTO'][0])!=undefined)
  455. {
  456. // replace the object and related objects' group names (+ append the related objects after the processed)
  457. parsed=('\r\n'+process_elem).match(RegExp('\r\n((?:'+vCard.re['group']+'\\.)?)','m'));
  458. if(parsed[1]!='') // if group is present, replace the object and related objects' group names
  459. process_elem=('\r\n'+process_elem).replace(RegExp('\r\n'+parsed[1].replace('.','\\.'),'mg'),'\r\nitem'+(groupCounter++)+'.').substring(2);
  460. process_elem=process_elem.replace('##:::##value##:::##',value);
  461. }
  462. else
  463. {
  464. process_elem=vCard.tplC['contentline_PHOTO'];
  465. process_elem=process_elem.replace('##:::##group_wd##:::##', '');
  466. process_elem=process_elem.replace('##:::##value##:::##', value);
  467. }
  468. // Data URL (non-remote) will always be a binary encoded png image
  469. if($('#photoURLHidden').val()==='') {
  470. process_elem=process_elem.replace('##:::##params_wsc##:::##', ';ENCODING=b;TYPE=png');
  471. }
  472. // For remote URL, we can't reliably determine its type, so we just append the VALUE=URI param
  473. else {
  474. process_elem=process_elem.replace('##:::##params_wsc##:::##', ';VALUE=URI');
  475. }
  476. vCardText+=process_elem;
  477. }
  478. // ADR
  479. if(globalDisabledContactAttributes.indexOf('ADR')==-1)
  480. {
  481. tmpvCardEditorRef.find('[data-type="\\%address"]').each(
  482. function (index,element)
  483. {
  484. // if data is present for the selected country's address fields
  485. var found=0;
  486. $(element).find('[data-addr-field]').each(
  487. function(index,element)
  488. {
  489. if($(element).attr('data-addr-field')!='' && $(element).attr('data-addr-field')!='country' && $(element).val()!='')
  490. {
  491. found=1;
  492. return false;
  493. }
  494. }
  495. );
  496. if(found)
  497. {
  498. var incGroupCounter=false;
  499. if(vCard.tplM['contentline_ADR']!=null && (process_elem=vCard.tplM['contentline_ADR'][$(element).attr('data-id')])!=undefined)
  500. {
  501. // replace the object and related objects' group names (+ append the related objects after the processed)
  502. parsed=('\r\n'+process_elem).match(RegExp('\r\n((?:'+vCard.re['group']+'\\.)?)','m'));
  503. if(parsed[1]!='') // if group is present, replace the object and related objects' group names
  504. {
  505. process_elem=('\r\n'+process_elem).replace(RegExp('\r\n'+parsed[1].replace('.','\\.'),'mg'),'\r\nitem'+groupCounter+'.').substring(2);
  506. incGroupCounter=true;
  507. }
  508. }
  509. else
  510. {
  511. process_elem=vCard.tplC['contentline_ADR'];
  512. process_elem=process_elem.replace('##:::##group_wd##:::##', '');
  513. }
  514. tmp_type=$(element).find('[data-type="address_type"] option').filter(':selected').attr('data-type');
  515. /* construct the "custom" type */
  516. if(tmp_type==':custom')
  517. {
  518. var tmp_cust_value=$(element).find('[data-type="custom_value"]').val();
  519. var tmp_cust_value_processed=tmp_cust_value.replace(RegExp('^\\s*|\\s*$','g'),'').replaceAll(' ',' ');
  520. // if a custom type is already defined as standard type, use the standard definition
  521. if((tmp_cust_already_exists=$(element).find('[data-type="address_type"] option').filter(function(){return $(this).html()==tmp_cust_value_processed;}).attr('data-type'))!=undefined)
  522. tmp_type=tmp_cust_already_exists;
  523. else // use custom type
  524. tmp_type=':'+tmp_cust_value+':';
  525. }
  526. params_wsc='';
  527. tmp_normal_types=tmp_type.replace(RegExp(':.*:','g'),',').replaceAll(',,',',').replace(RegExp('^,|,$','g'),'');
  528. if(tmp_normal_types!='')
  529. params_wsc=';TYPE='+vcardEscapeValue(tmp_normal_types).toUpperCase().replace(RegExp('\\\\,','g'),';TYPE=');
  530. var streetVal = $(element).find('[data-addr-field="street"]').map(function() {
  531. var val = $(this).val();
  532. if(val) {
  533. return val;
  534. }
  535. }).get().join('\n');
  536. process_elem=process_elem.replace('##:::##params_wsc##:::##',params_wsc);
  537. process_elem=process_elem.replace('##:::##pobox##:::##',vcardEscapeValue($(element).find('[data-addr-field="pobox"]').val()));
  538. process_elem=process_elem.replace('##:::##extaddr##:::##',vcardEscapeValue($(element).find('[data-addr-field="extaddr"]').val()));
  539. process_elem=process_elem.replace('##:::##street##:::##',vcardEscapeValue(streetVal));
  540. process_elem=process_elem.replace('##:::##locality##:::##',vcardEscapeValue($(element).find('[data-addr-field="locality"]').val()));
  541. process_elem=process_elem.replace('##:::##region##:::##',vcardEscapeValue($(element).find('[data-addr-field="region"]').val()));
  542. process_elem=process_elem.replace('##:::##code##:::##',vcardEscapeValue($(element).find('[data-addr-field="code"]').val()));
  543. process_elem=process_elem.replace('##:::##country##:::##',vcardEscapeValue($(element).find('[data-type="country_type"] option').filter(':selected').attr('data-full-name')));
  544. my_related='X-ABADR:'+vcardEscapeValue($(element).find('[data-type="country_type"] option').filter(':selected').attr('data-type'))+'\r\n';
  545. parsed=('\r\n'+process_elem).match(vCard.pre['contentline_parse']);
  546. if(parsed[1]!='') // if group is present, we use it, otherwise we create a new group
  547. process_elem+=parsed[1]+my_related;
  548. else
  549. process_elem='item'+groupCounter+'.'+process_elem+'item'+groupCounter+'.'+my_related;
  550. incGroupCounter=true; // we always increate the group number, because the X-ABADR is always stored
  551. my_related='';
  552. tmp_related_type=tmp_type.match(RegExp(':(.*):')); // only one element of related (X-ABLabel) is supported
  553. if(tmp_related_type!=null && tmp_related_type[1]!='')
  554. my_related='X-ABLabel:'+vcardEscapeValue((dataTypes['address_type_store_as'][tmp_related_type[1]]!=undefined ? dataTypes['address_type_store_as'][tmp_related_type[1]] : tmp_related_type[1]))+'\r\n';
  555. if(my_related!='')
  556. {
  557. incGroupCounter=true;
  558. parsed=('\r\n'+process_elem).match(vCard.pre['contentline_parse']);
  559. if(parsed[1]!='') // if group is present, we use it, otherwise we create a new group
  560. process_elem+=parsed[1]+my_related;
  561. else
  562. process_elem='item'+groupCounter+'.'+process_elem+'item'+groupCounter+'.'+my_related;
  563. }
  564. if(incGroupCounter) groupCounter++;
  565. vCardText+=process_elem;
  566. }
  567. }
  568. );
  569. }
  570. // TEL
  571. if(globalDisabledContactAttributes.indexOf('TEL')==-1)
  572. {
  573. tmpvCardEditorRef.find('[data-type="\\%phone"]').each(
  574. function (index,element)
  575. {
  576. if((value=$(element).find('[data-type="value"]').val())!='')
  577. {
  578. var incGroupCounter=false;
  579. if(vCard.tplM['contentline_TEL']!=null && (process_elem=vCard.tplM['contentline_TEL'][$(element).attr('data-id')])!=undefined)
  580. {
  581. // replace the object and related objects' group names (+ append the related objects after the processed)
  582. parsed=('\r\n'+process_elem).match(RegExp('\r\n((?:'+vCard.re['group']+'\\.)?)','m'));
  583. if(parsed[1]!='') // if group is present, replace the object and related objects' group names
  584. {
  585. process_elem=('\r\n'+process_elem).replace(RegExp('\r\n'+parsed[1].replace('.','\\.'),'mg'),'\r\nitem'+groupCounter+'.').substring(2);
  586. incGroupCounter=true;
  587. }
  588. }
  589. else
  590. {
  591. process_elem=vCard.tplC['contentline_TEL'];
  592. process_elem=process_elem.replace('##:::##group_wd##:::##', '');
  593. }
  594. tmp_type=$(element).find('[data-type="phone_type"] option').filter(':selected').attr('data-type');
  595. /* construct the "custom" type */
  596. if(tmp_type==':custom')
  597. {
  598. var tmp_cust_value=$(element).find('[data-type="custom_value"]').val();
  599. var tmp_cust_value_processed=tmp_cust_value.replace(RegExp('^\\s*|\\s*$','g'),'').replaceAll(' ',' ');
  600. // if a custom type is already defined as standard type, use the standard definition
  601. if((tmp_cust_already_exists=$(element).find('[data-type="phone_type"] option').filter(function(){return $(this).html()==tmp_cust_value_processed;}).attr('data-type'))!=undefined)
  602. tmp_type=tmp_cust_already_exists;
  603. else // use custom type
  604. tmp_type=':'+tmp_cust_value+':';
  605. }
  606. params_wsc='';
  607. tmp_normal_types=tmp_type.replace(RegExp(':.*:','g'),',').replaceAll(',,',',').replace(RegExp('^,|,$','g'),'');
  608. if(tmp_normal_types!='')
  609. params_wsc=';TYPE='+vcardEscapeValue(tmp_normal_types).toUpperCase().replace(RegExp('\\\\,','g'),';TYPE=');
  610. process_elem=process_elem.replace('##:::##params_wsc##:::##',params_wsc);
  611. process_elem=process_elem.replace('##:::##value##:::##',vcardEscapeValue(value));
  612. my_related='';
  613. tmp_related_type=tmp_type.match(RegExp(':(.*):')); // only one element of related (X-ABLabel) is supported
  614. if(tmp_related_type!=null && tmp_related_type[1]!='')
  615. my_related='X-ABLabel:'+vcardEscapeValue((dataTypes['phone_type_store_as'][tmp_related_type[1]]!=undefined ? dataTypes['phone_type_store_as'][tmp_related_type[1]] : tmp_related_type[1]))+'\r\n';
  616. if(my_related!='')
  617. {
  618. incGroupCounter=true;
  619. parsed=('\r\n'+process_elem).match(vCard.pre['contentline_parse']);
  620. if(parsed[1]!='') // if group is present, we use it, otherwise we create a new group
  621. process_elem+=parsed[1]+my_related;
  622. else
  623. process_elem='item'+groupCounter+'.'+process_elem+'item'+groupCounter+'.'+my_related;
  624. }
  625. if(incGroupCounter) groupCounter++;
  626. vCardText+=process_elem;
  627. }
  628. }
  629. );
  630. }
  631. // EMAIL
  632. if(globalDisabledContactAttributes.indexOf('EMAIL')==-1)
  633. {
  634. tmpvCardEditorRef.find('[data-type="\\%email"]').each(
  635. function (index,element)
  636. {
  637. if((value=$(element).find('[data-type="value"]').val())!='')
  638. {
  639. incGroupCounter=false;
  640. if(vCard.tplM['contentline_EMAIL']!=null && (process_elem=vCard.tplM['contentline_EMAIL'][$(element).attr('data-id')])!=undefined)
  641. {
  642. // replace the object and related objects' group names (+ append the related objects after the processed)
  643. parsed=('\r\n'+process_elem).match(RegExp('\r\n((?:'+vCard.re['group']+'\\.)?)','m'));
  644. if(parsed[1]!='') // if group is present, replace the object and related objects' group names
  645. {
  646. process_elem=('\r\n'+process_elem).replace(RegExp('\r\n'+parsed[1].replace('.','\\.'),'mg'),'\r\nitem'+groupCounter+'.').substring(2);
  647. incGroupCounter=true;
  648. }
  649. }
  650. else
  651. {
  652. process_elem=vCard.tplC['contentline_EMAIL'];
  653. process_elem=process_elem.replace('##:::##group_wd##:::##', '');
  654. }
  655. tmp_type=$(element).find('[data-type="email_type"] option').filter(':selected').attr('data-type');
  656. /* construct the "custom" type */
  657. if(tmp_type==':custom')
  658. {
  659. var tmp_cust_value=$(element).find('[data-type="custom_value"]').val();
  660. var tmp_cust_value_processed=tmp_cust_value.replace(RegExp('^\\s*|\\s*$','g'),'').replaceAll(' ',' ');
  661. // if a custom type is already defined as standard type, use the standard definition
  662. if((tmp_cust_already_exists=$(element).find('[data-type="email_type"] option').filter(function(){return $(this).html()==tmp_cust_value_processed;}).attr('data-type'))!=undefined)
  663. tmp_type=tmp_cust_already_exists;
  664. else // use custom type
  665. tmp_type=':'+tmp_cust_value+':';
  666. }
  667. params_wsc='';
  668. tmp_normal_types=tmp_type.replace(RegExp(':.*:','g'),',').replaceAll(',,',',').replace(RegExp('^,|,$','g'),'');
  669. if(tmp_normal_types!='')
  670. params_wsc=';TYPE='+vcardEscapeValue(tmp_normal_types).toUpperCase().replace(RegExp('\\\\,','g'),';TYPE=');
  671. process_elem=process_elem.replace('##:::##params_wsc##:::##',params_wsc);
  672. process_elem=process_elem.replace('##:::##value##:::##',vcardEscapeValue(value));
  673. my_related='';
  674. tmp_related_type=tmp_type.match(RegExp(':(.*):')); // only one element of related (X-ABLabel) is supported
  675. if(tmp_related_type!=null && tmp_related_type[1]!='')
  676. my_related='X-ABLabel:'+vcardEscapeValue((dataTypes['email_type_store_as'][tmp_related_type[1]]!=undefined ? dataTypes['email_type_store_as'][tmp_related_type[1]] : tmp_related_type[1]))+'\r\n';
  677. if(my_related!='')
  678. {
  679. incGroupCounter=true;
  680. parsed=('\r\n'+process_elem).match(vCard.pre['contentline_parse']);
  681. if(parsed[1]!='') // if group is present, we use it, otherwise we create a new group
  682. process_elem+=parsed[1]+my_related;
  683. else
  684. process_elem='item'+groupCounter+'.'+process_elem+'item'+groupCounter+'.'+my_related;
  685. }
  686. if(incGroupCounter) groupCounter++;
  687. vCardText+=process_elem;
  688. }
  689. }
  690. );
  691. }
  692. // URL
  693. if(globalDisabledContactAttributes.indexOf('URL')==-1)
  694. {
  695. tmpvCardEditorRef.find('[data-type="\\%url"]').each(
  696. function (index,element)
  697. {
  698. if((value=$(element).find('[data-type="value"]').val())!='')
  699. {
  700. incGroupCounter=false;
  701. if(vCard.tplM['contentline_URL']!=null && (process_elem=vCard.tplM['contentline_URL'][$(element).attr('data-id')])!=undefined)
  702. {
  703. // replace the object and related objects' group names (+ append the related objects after the processed)
  704. parsed=('\r\n'+process_elem).match(RegExp('\r\n((?:'+vCard.re['group']+'\\.)?)','m'));
  705. if(parsed[1]!='') // if group is present, replace the object and related objects' group names
  706. {
  707. process_elem=('\r\n'+process_elem).replace(RegExp('\r\n'+parsed[1].replace('.','\\.'),'mg'),'\r\nitem'+groupCounter+'.').substring(2);
  708. incGroupCounter=true;
  709. }
  710. }
  711. else
  712. {
  713. process_elem=vCard.tplC['contentline_URL'];
  714. process_elem=process_elem.replace('##:::##group_wd##:::##', '');
  715. }
  716. tmp_type=$(element).find('[data-type="url_type"] option').filter(':selected').attr('data-type');
  717. /* construct the "custom" type */
  718. if(tmp_type==':custom')
  719. {
  720. var tmp_cust_value=$(element).find('[data-type="custom_value"]').val();
  721. var tmp_cust_value_processed=tmp_cust_value.replace(RegExp('^\\s*|\\s*$','g'),'').replaceAll(' ',' ');
  722. // if a custom type is already defined as standard type, use the standard definition
  723. if((tmp_cust_already_exists=$(element).find('[data-type="url_type"] option').filter(function(){return $(this).html()==tmp_cust_value_processed;}).attr('data-type'))!=undefined)
  724. tmp_type=tmp_cust_already_exists;
  725. else // use custom type
  726. tmp_type=':'+tmp_cust_value+':';
  727. }
  728. params_wsc='';
  729. tmp_normal_types=tmp_type.replace(RegExp(':.*:','g'),',').replaceAll(',,',',').replace(RegExp('^,|,$','g'),'');
  730. if(tmp_normal_types!='')
  731. params_wsc=';TYPE='+vcardEscapeValue(tmp_normal_types).toUpperCase().replace(RegExp('\\\\,','g'),';TYPE=');
  732. process_elem=process_elem.replace('##:::##params_wsc##:::##',params_wsc);
  733. process_elem=process_elem.replace('##:::##value##:::##',vcardEscapeValue(value));
  734. my_related='';
  735. tmp_related_type=tmp_type.match(RegExp(':(.*):')); // only one element of related (X-ABLabel) is supported
  736. if(tmp_related_type!=null && tmp_related_type[1]!='')
  737. my_related='X-ABLabel:'+vcardEscapeValue((dataTypes['url_type_store_as'][tmp_related_type[1]]!=undefined ? dataTypes['url_type_store_as'][tmp_related_type[1]] : tmp_related_type[1]))+'\r\n';
  738. if(my_related!='')
  739. {
  740. incGroupCounter=true;
  741. parsed=('\r\n'+process_elem).match(vCard.pre['contentline_parse']);
  742. if(parsed[1]!='') // if group is present, we use it, otherwise we create a new group
  743. process_elem+=parsed[1]+my_related;
  744. else
  745. process_elem='item'+groupCounter+'.'+process_elem+'item'+groupCounter+'.'+my_related;
  746. }
  747. if(incGroupCounter) groupCounter++;
  748. vCardText+=process_elem;
  749. }
  750. }
  751. );
  752. }
  753. // X-ABRELATEDNAMES
  754. if(globalDisabledContactAttributes.indexOf('X-ABRELATEDNAMES')==-1)
  755. {
  756. tmpvCardEditorRef.find('[data-type="\\%person"]').each(
  757. function (index,element)
  758. {
  759. if((value=$(element).find('[data-type="value"]').val())!='')
  760. {
  761. incGroupCounter=false;
  762. if(vCard.tplM['contentline_X-ABRELATEDNAMES']!=null && (process_elem=vCard.tplM['contentline_X-ABRELATEDNAMES'][$(element).attr('data-id')])!=undefined)
  763. {
  764. // replace the object and related objects' group names (+ append the related objects after the processed)
  765. parsed=('\r\n'+process_elem).match(RegExp('\r\n((?:'+vCard.re['group']+'\\.)?)','m'));
  766. if(parsed[1]!='') // if group is present, replace the object and related objects' group names
  767. {
  768. process_elem=('\r\n'+process_elem).replace(RegExp('\r\n'+parsed[1].replace('.','\\.'),'mg'),'\r\nitem'+groupCounter+'.').substring(2);
  769. incGroupCounter=true;
  770. }
  771. }
  772. else
  773. {
  774. process_elem=vCard.tplC['contentline_X-ABRELATEDNAMES'];
  775. process_elem=process_elem.replace('##:::##group_wd##:::##', '');
  776. }
  777. tmp_type=$(element).find('[data-type="person_type"] option').filter(':selected').attr('data-type');
  778. /* construct the "custom" type */
  779. if(tmp_type==':custom')
  780. {
  781. var tmp_cust_value=$(element).find('[data-type="custom_value"]').val();
  782. var tmp_cust_value_processed=tmp_cust_value.replace(RegExp('^\\s*|\\s*$','g'),'').replaceAll(' ',' ');
  783. // if a custom type is already defined as standard type, use the standard definition
  784. if((tmp_cust_already_exists=$(element).find('[data-type="person_type"] option').filter(function(){return $(this).html()==tmp_cust_value_processed;}).attr('data-type'))!=undefined)
  785. tmp_type=tmp_cust_already_exists;
  786. else // use custom type
  787. tmp_type=':'+tmp_cust_value+':';
  788. }
  789. params_wsc='';
  790. tmp_normal_types=tmp_type.replace(RegExp(':.*:','g'),',').replaceAll(',,',',').replace(RegExp('^,|,$','g'),'');
  791. if(tmp_normal_types!='')
  792. params_wsc=';TYPE='+vcardEscapeValue(tmp_normal_types).toUpperCase().replace(RegExp('\\\\,','g'),';TYPE=');
  793. process_elem=process_elem.replace('##:::##params_wsc##:::##',params_wsc);
  794. process_elem=process_elem.replace('##:::##value##:::##',vcardEscapeValue(value));
  795. my_related='';
  796. tmp_related_type=tmp_type.match(RegExp(':(.*):')); // only one element of related (X-ABLabel) is supported
  797. if(tmp_related_type!=null && tmp_related_type[1]!='')
  798. my_related='X-ABLabel:'+vcardEscapeValue((dataTypes['person_type_store_as'][tmp_related_type[1]]!=undefined ? dataTypes['person_type_store_as'][tmp_related_type[1]] : tmp_related_type[1]))+'\r\n';
  799. if(my_related!='')
  800. {
  801. incGroupCounter=true;
  802. parsed=('\r\n'+process_elem).match(vCard.pre['contentline_parse']);
  803. if(parsed[1]!='') // if group is present, we use it, otherwise we create a new group
  804. process_elem+=parsed[1]+my_related;
  805. else
  806. process_elem='item'+groupCounter+'.'+process_elem+'item'+groupCounter+'.'+my_related;
  807. }
  808. if(incGroupCounter) groupCounter++;
  809. if(tmp_related_type!=null && tmp_related_type[1]!='')
  810. {
  811. // In addition of the X-ABRELATEDNAMES attributes add also the old style X-* attributes
  812. switch(tmp_related_type[1])
  813. {
  814. case '_$!<assistant>!$_':
  815. process_elem+='X-ASSISTANT:'+vcardEscapeValue(value)+'\r\n';
  816. // process_elem+='X-EVOLUTION-ASSISTANT:'+vcardEscapeValue(value)+'\r\n';
  817. break;
  818. case '_$!<manager>!$_':
  819. process_elem+='X-MANAGER:'+vcardEscapeValue(value)+'\r\n';
  820. // process_elem+='X-EVOLUTION-MANAGER:'+vcardEscapeValue(value)+'\r\n';
  821. break;
  822. case '_$!<spouse>!$_':
  823. process_elem+='X-SPOUSE:'+vcardEscapeValue(value)+'\r\n';
  824. // process_elem+='X-EVOLUTION-SPOUSE:'+vcardEscapeValue(value)+'\r\n';
  825. break;
  826. default:
  827. break;
  828. }
  829. }
  830. vCardText+=process_elem;
  831. }
  832. }
  833. );
  834. }
  835. // IMPP
  836. if(globalDisabledContactAttributes.indexOf('IMPP')==-1)
  837. {
  838. tmpvCardEditorRef.find('[data-type="\\%im"]').each(
  839. function (index,element)
  840. {
  841. if((value=$(element).find('[data-type="value"]').val())!='')
  842. {
  843. incGroupCounter=false;
  844. if(vCard.tplM['contentline_IMPP']!=null && (process_elem=vCard.tplM['contentline_IMPP'][$(element).attr('data-id')])!=undefined)
  845. {
  846. // replace the object and related objects' group names (+ append the related objects after the processed)
  847. parsed=('\r\n'+process_elem).match(RegExp('\r\n((?:'+vCard.re['group']+'\\.)?)','m'));
  848. if(parsed[1]!='') // if group is present, replace the object and related objects' group names
  849. {
  850. process_elem=('\r\n'+process_elem).replace(RegExp('\r\n'+parsed[1].replace('.','\\.'),'mg'),'\r\nitem'+groupCounter+'.').substring(2);
  851. incGroupCounter=true;
  852. }
  853. }
  854. else
  855. {
  856. process_elem=vCard.tplC['contentline_IMPP'];
  857. process_elem=process_elem.replace('##:::##group_wd##:::##', '');
  858. }
  859. tmp_type=$(element).find('[data-type="im_type"] option').filter(':selected').attr('data-type');
  860. /* construct the "custom" type */
  861. if(tmp_type==':custom')
  862. {
  863. var tmp_cust_value=$(element).find('[data-type="custom_value"]:first').val();
  864. var tmp_cust_value_processed=tmp_cust_value.replace(RegExp('^\\s*|\\s*$','g'),'').replaceAll(' ',' ');
  865. // if a custom type is already defined as standard type, use the standard definition
  866. if((tmp_cust_already_exists=$(element).find('[data-type="im_type"] option').filter(function(){return $(this).html()==tmp_cust_value_processed;}).attr('data-type'))!=undefined)
  867. tmp_type=tmp_cust_already_exists;
  868. else // use custom type
  869. tmp_type=':'+tmp_cust_value+':';
  870. }
  871. params_wsc=params_wsc_old_repr='';
  872. tmp_normal_types=tmp_type.replace(RegExp(':.*:','g'),',').replaceAll(',,',',').replace(RegExp('^,|,$','g'),'');
  873. if(tmp_normal_types!='')
  874. params_wsc=params_wsc_old_repr=';TYPE='+vcardEscapeValue(tmp_normal_types).toUpperCase().replace(RegExp('\\\\,','g'),';TYPE=');
  875. tmp_service_type=$(element).find('[data-type="im_service_type"] option').filter(':selected').attr('data-type');
  876. /* construct the "custom" type */
  877. if(tmp_service_type==':custom')
  878. {
  879. var tmp_cust_value=$(element).find('[data-type="custom_value"]:last').val();
  880. var tmp_cust_value_processed=tmp_cust_value.replace(RegExp('^\\s*|\\s*$','g'),'').replaceAll(' ',' ');
  881. // if a custom type is already defined as standard type, use the standard definition
  882. if((tmp_cust_already_exists=$(element).find('[data-type="im_service_type"] option').filter(function(){return $(this).html()==tmp_cust_value_processed;}).attr('data-type'))!=undefined)
  883. tmp_service_type=tmp_cust_already_exists;
  884. else // use custom type
  885. tmp_service_type=':'+tmp_cust_value+':';
  886. }
  887. if(dataTypes['im_service_type_store_as'][tmp_service_type]!=undefined)
  888. tmp_service_type=dataTypes['im_service_type_store_as'][tmp_service_type];
  889. params_wsc=';X-SERVICE-TYPE='+vcardEscapeValue(tmp_service_type)+params_wsc;
  890. process_elem=process_elem.replace('##:::##params_wsc##:::##',params_wsc);
  891. switch(tmp_service_type.toLowerCase()) // RFC4770
  892. {
  893. case 'aim':
  894. im_value='aim:'+vcardEscapeValue(value);
  895. break;
  896. case 'facebook':
  897. im_value='xmpp:'+vcardEscapeValue(value);
  898. break;
  899. case 'googletalk':
  900. im_value='xmpp:'+vcardEscapeValue(value);
  901. break;
  902. case 'icq':
  903. im_value='aim:'+vcardEscapeValue(value);
  904. break;
  905. case 'irc':
  906. im_value='irc:'+vcardEscapeValue(value);
  907. break;
  908. case 'jabber':
  909. im_value='xmpp:'+vcardEscapeValue(value);
  910. break;
  911. case 'msn':
  912. im_value='msnim:'+vcardEscapeValue(value);
  913. break;
  914. case 'skype':
  915. im_value='skype:'+vcardEscapeValue(value);
  916. break;
  917. case 'yahoo':
  918. im_value='ymsgr:'+vcardEscapeValue(value);
  919. break;
  920. default: // 'gadugadu', 'qq', ...
  921. im_value='x-apple:'+vcardEscapeValue(value);
  922. break;
  923. }
  924. process_elem=process_elem.replace('##:::##value##:::##',im_value);
  925. my_related='';
  926. tmp_related_type=tmp_type.match(RegExp(':(.*):')); // only one element of related (X-ABLabel) is supported
  927. if(tmp_related_type!=null && tmp_related_type[1]!='')
  928. my_related='X-ABLabel:'+vcardEscapeValue((dataTypes['im_type_store_as'][tmp_related_type[1]]!=undefined ? dataTypes['im_type_store_as'][tmp_related_type[1]] : tmp_related_type[1]))+'\r\n';
  929. if(my_related!='')
  930. {
  931. incGroupCounter=true;
  932. parsed=('\r\n'+process_elem).match(vCard.pre['contentline_parse']);
  933. if(parsed[1]!='') // if group is present, we use it, otherwise we create a new group
  934. process_elem+=parsed[1]+my_related;
  935. else
  936. process_elem='item'+groupCounter+'.'+process_elem+'item'+groupCounter+'.'+my_related;
  937. }
  938. if(incGroupCounter) groupCounter++;
  939. // In addition of the IMPP attributes add also the old style X-* attributes
  940. process_elem_old_repr='';
  941. switch(tmp_service_type.toLowerCase())
  942. {
  943. case 'aim':
  944. new_group_wd='';
  945. if(incGroupCounter)
  946. {
  947. new_group_wd='item'+groupCounter+'.';
  948. process_elem_old_repr=('\r\n'+process_elem).replace(RegExp('\r\nitem'+(groupCounter-1)+'\\.','mg'),'\r\n'+new_group_wd);
  949. groupCounter++;
  950. }
  951. else
  952. process_elem_old_repr='\r\n'+process_elem;
  953. process_elem+=process_elem_old_repr.replace('\r\n'+new_group_wd+'IMPP;X-SERVICE-TYPE='+ vcardEscapeValue(tmp_service_type),new_group_wd+'X-AIM').replace(im_value+'\r\n',vcardEscapeValue(value)+'\r\n');
  954. break;
  955. case 'jabber':
  956. new_group_wd='';
  957. if(incGroupCounter)
  958. {
  959. new_group_wd='item'+groupCounter+'.';
  960. process_elem_old_repr=('\r\n'+process_elem).replace(RegExp('\r\nitem'+(groupCounter-1)+'\\.','mg'),'\r\n'+new_group_wd);
  961. groupCounter++;
  962. }
  963. else
  964. process_elem_old_repr='\r\n'+process_elem;
  965. process_elem+=process_elem_old_repr.replace('\r\n'+new_group_wd+'IMPP;X-SERVICE-TYPE='+ vcardEscapeValue(tmp_service_type),new_group_wd+'X-JABBER').replace(im_value+'\r\n',vcardEscapeValue(value)+'\r\n');
  966. break;
  967. case 'msn':
  968. new_group_wd='';
  969. if(incGroupCounter)
  970. {
  971. new_group_wd='item'+groupCounter+'.';
  972. process_elem_old_repr=('\r\n'+process_elem).replace(RegExp('\r\nitem'+(groupCounter-1)+'\\.','mg'),'\r\n'+new_group_wd);
  973. groupCounter++;
  974. }
  975. else
  976. process_elem_old_repr='\r\n'+process_elem;
  977. process_elem+=process_elem_old_repr.replace('\r\n'+new_group_wd+'IMPP;X-SERVICE-TYPE='+ vcardEscapeValue(tmp_service_type),new_group_wd+'X-MSN').replace(im_value+'\r\n',vcardEscapeValue(value)+'\r\n');
  978. break;
  979. case 'yahoo':
  980. new_group_wd='';
  981. process_elem_tmp=process_elem;
  982. if(incGroupCounter)
  983. {
  984. new_group_wd='item'+groupCounter+'.';
  985. process_elem_old_repr=('\r\n'+process_elem_tmp).replace(RegExp('\r\nitem'+(groupCounter-1)+'\\.','mg'),'\r\n'+new_group_wd);
  986. groupCounter++;
  987. }
  988. else
  989. process_elem_old_repr='\r\n'+process_elem;
  990. process_elem+=process_elem_old_repr.replace('\r\n'+new_group_wd+'IMPP;X-SERVICE-TYPE='+ vcardEscapeValue(tmp_service_type),new_group_wd+'X-YAHOO').replace(im_value+'\r\n',vcardEscapeValue(value)+'\r\n');
  991. new_group_wd='';
  992. if(incGroupCounter)
  993. {
  994. new_group_wd='item'+groupCounter+'.';
  995. process_elem_old_repr=('\r\n'+process_elem_tmp).replace(RegExp('\r\nitem'+(groupCounter-2)+'\\.','mg'),'\r\n'+new_group_wd);
  996. groupCounter++;
  997. }
  998. else
  999. process_elem_old_repr='\r\n'+process_elem;
  1000. process_elem+=process_elem_old_repr.replace('\r\n'+new_group_wd+'IMPP;X-SERVICE-TYPE='+ vcardEscapeValue(tmp_service_type),new_group_wd+'X-YAHOO-ID').replace(im_value+'\r\n',vcardEscapeValue(value)+'\r\n');
  1001. break;
  1002. case 'icq':
  1003. new_group_wd='';
  1004. if(incGroupCounter)
  1005. {
  1006. new_group_wd='item'+groupCounter+'.';
  1007. process_elem_old_repr=('\r\n'+process_elem).replace(RegExp('\r\nitem'+(groupCounter-1)+'\\.','mg'),'\r\n'+new_group_wd);
  1008. groupCounter++;
  1009. }
  1010. else
  1011. process_elem_old_repr='\r\n'+process_elem;
  1012. process_elem+=process_elem_old_repr.replace('\r\n'+new_group_wd+'IMPP;X-SERVICE-TYPE='+ vcardEscapeValue(tmp_service_type),new_group_wd+'X-ICQ').replace(im_value+'\r\n',vcardEscapeValue(value)+'\r\n');
  1013. break;
  1014. default:
  1015. break;
  1016. }
  1017. vCardText+=process_elem;
  1018. }
  1019. }
  1020. );
  1021. }
  1022. // X-SOCIALPROFILE
  1023. if(globalDisabledContactAttributes.indexOf('X-SOCIALPROFILE')==-1)
  1024. {
  1025. tmpvCardEditorRef.find('[data-type="\\%profile"]').each(
  1026. function (index,element)
  1027. {
  1028. if((value=$(element).find('[data-type="value"]').val())!='')
  1029. {
  1030. incGroupCounter=false;
  1031. if(vCard.tplM['contentline_X-SOCIALPROFILE']!=null && (process_elem=vCard.tplM['contentline_X-SOCIALPROFILE'][$(element).attr('data-id')])!=undefined)
  1032. {
  1033. // replace the object and related objects' group names (+ append the related objects after the processed)
  1034. parsed=('\r\n'+process_elem).match(RegExp('\r\n((?:'+vCard.re['group']+'\\.)?)','m'));
  1035. if(parsed[1]!='') // if group is present, replace the object and related objects' group names
  1036. {
  1037. process_elem=('\r\n'+process_elem).replace(RegExp('\r\n'+parsed[1].replace('.','\\.'),'mg'),'\r\nitem'+groupCounter+'.').substring(2);
  1038. incGroupCounter=true;
  1039. }
  1040. }
  1041. else
  1042. {
  1043. process_elem=vCard.tplC['contentline_X-SOCIALPROFILE'];
  1044. process_elem=process_elem.replace('##:::##group_wd##:::##', '');
  1045. }
  1046. tmp_type=$(element).find('[data-type="profile_type"] option').filter(':selected').attr('data-type');
  1047. /* construct the "custom" type */
  1048. if(tmp_type==':custom')
  1049. {
  1050. var tmp_cust_value=$(element).find('[data-type="custom_value"]').val();
  1051. var tmp_cust_value_processed=tmp_cust_value.replace(RegExp('^\\s*|\\s*$','g'),'').replaceAll(' ',' ');
  1052. // if a custom type is already defined as standard type, use the standard definition
  1053. if((tmp_cust_already_exists=$(element).find('[data-type="profile_type"] option').filter(function(){return $(this).html()==tmp_cust_value_processed;}).attr('data-type'))!=undefined)
  1054. tmp_type=tmp_cust_already_exists;
  1055. else // use custom type
  1056. tmp_type=':'+tmp_cust_value+':';
  1057. }
  1058. params_wsc='';
  1059. tmp_normal_types=tmp_type.replace(RegExp(':.*:','g'),',').replaceAll(',,',',').replace(RegExp('^,|,$','g'),'');
  1060. if(tmp_normal_types!='')
  1061. params_wsc=';TYPE='+vcardEscapeValue(tmp_normal_types).toUpperCase().replace(RegExp('\\\\,','g'),';TYPE=')+';x-user='+vcardEscapeValue(tmp_type=='twitter' ? value.replace(/^@+/, '') : value);
  1062. process_elem=process_elem.replace('##:::##params_wsc##:::##',params_wsc);
  1063. process_elem=process_elem.replace('##:::##value##:::##', vcardEscapeValue((globalSettings.urihandlerprofile.value[tmp_type]!=undefined ? globalSettings.urihandlerprofile.value[tmp_type] : 'x-apple:%u').replace('%u', (tmp_type=='twitter' ? value.replace(/^@+/, '') : value))));
  1064. if(incGroupCounter) groupCounter++;
  1065. vCardText+=process_elem;
  1066. }
  1067. }
  1068. );
  1069. }
  1070. // extension hook
  1071. if(typeof(globalContactsExtDataToVcard)=='function')
  1072. vCardText=globalContactsExtDataToVcard(tmpvCardEditorRef, vCardText);
  1073. // PRODID
  1074. vCardText+='PRODID:-//Inf-IT//'+globalAppName+' '+globalVersion+'//EN\r\n';
  1075. if(typeof vCard.tplM['unprocessed_unrelated']!='undefined')
  1076. vCardText+=vCard.tplM['unprocessed_unrelated'].replace(RegExp('^\r\n'),'');
  1077. // vCard END (required by RFC)
  1078. if(vCard.tplM['end']!=null && (process_elem=vCard.tplM['end'][0])!=undefined)
  1079. vCardText+=vCard.tplM['end'][0];
  1080. else
  1081. {
  1082. process_elem=vCard.tplC['end'];
  1083. process_elem=process_elem.replace('##:::##group_wd##:::##', '');
  1084. vCardText+=process_elem;
  1085. }
  1086. // replace unsupported XML characters
  1087. vCardText=vCardText.replace(/[^\u0009\u000A\u000D\u0020-\uD7FF\uE000-\uFFFD]/g, ' ');
  1088. // line folding (RFC2426 - section 2.6) - maximum of 75 octects (and cannot break
  1089. // multi-octet UTF8-characters) allowed on one line, excluding a line break (CRLF)
  1090. vCardText=vObjectLineFolding(vCardText);
  1091. if(typeof(globalContactsExtPutVcardToCollectionOverload)=='function')
  1092. globalContactsExtPutVcardToCollectionOverload(accountUID, inputEtag, newUID, vCardText);
  1093. else
  1094. {
  1095. var selAddr = tmpvCardEditorRef.find('[data-attr-name="_DEST_"]').find('option:selected').attr('data-type')
  1096. //addressbook selectbox was changed
  1097. var orgAddr = $('#vCardEditor').attr('data-url').replace(RegExp('[^/]*$'),'');
  1098. if($('#ExtendedDest').length>0)
  1099. {
  1100. var putGroups=new Array();
  1101. var removeGroups=new Array();
  1102. var myGroups = new Array()
  1103. if(inputEtag!='')
  1104. {
  1105. myGroups=globalAddressbookList.getMyContactGroups($('#vCardEditor').attr('data-url'));
  1106. for(var gi=0; gi<myGroups.length; gi++)
  1107. if($('#ExtendedDest').find('.extended_dest_group').find('input:checked[data-id="'+myGroups[gi]+'"]').length==0)
  1108. removeGroups.push(myGroups[gi]);
  1109. }
  1110. $('#ExtendedDest').find('.extended_dest_group').find('input:checked').each(function(){
  1111. var guid = $(this).attr('data-id');
  1112. if(myGroups.indexOf(guid)==-1)
  1113. putGroups.push(guid);
  1114. });
  1115. }
  1116. if(orgAddr!= selAddr && inputEtag!='')
  1117. {
  1118. var tmp2=globalAddressbookList.getContactByUID($('#vCardEditor').attr('data-url'));
  1119. var vUID = $('#vCardEditor').attr('data-url').match(RegExp('[^/]*$'));
  1120. // here we generate the destination for MOVE (we don't use the old vCard file name to minimalize the possible conflict situations)
  1121. tmp2.vcard=vCardText;
  1122. tmp2.newAccountUID=globalResourceCardDAVList.getCollectionByUID(selAddr).accountUID;
  1123. tmp2.newUid=selAddr;
  1124. tmp2.finalContactUID=tmp2.uid;
  1125. tmp2.orgUID=selAddr+vUID;
  1126. tmp2.addToContactGroupUID=new Array();
  1127. tmp2.removeToContactGroupUID=new Array();
  1128. // we need to store the ui object references for error handling in the GUI
  1129. if($('#ExtendedDest').length>0)
  1130. {
  1131. tmp2.uiObjects={resource:globalRefAddContact.attr('data-filter-url')};
  1132. if(putGroups.length>0)
  1133. tmp2.addToContactGroupUID=putGroups.slice();
  1134. if(removeGroups.length>0)
  1135. tmp2.removeToContactGroupUID=removeGroups.slice();
  1136. }
  1137. tmp2.formSave=true;
  1138. lockAndPerformToCollection(tmp2, globalRefAddContact.attr('data-filter-url'), 'IRM_DELETE');
  1139. }
  1140. else
  1141. {
  1142. if(inputEtag=='')
  1143. inputUID=selAddr;
  1144. if($('#ExtendedDest').length>0 && (putGroups.length>0 || removeGroups.length>0))
  1145. {
  1146. if(inputEtag!='')
  1147. var tmp2=globalAddressbookList.getContactByUID($('#vCardEditor').attr('data-url'));
  1148. else
  1149. var tmp2={accountUID: accountUID, uid: inputUID, etag: inputEtag};
  1150. var vUID = $('#vCardEditor').attr('data-url').match(RegExp('[^/]*$'));
  1151. // here we generate the destination for MOVE (we don't use the old vCard file name to minimalize the possible conflict situations)
  1152. tmp2.vcard=vCardText;
  1153. tmp2.uiObjects={resource:globalRefAddContact.attr('data-filter-url')};
  1154. tmp2.addToContactGroupUID=new Array();
  1155. tmp2.removeToContactGroupUID=new Array();
  1156. if(putGroups.length>0)
  1157. tmp2.addToContactGroupUID=putGroups.slice();
  1158. if(removeGroups.length>0)
  1159. tmp2.removeToContactGroupUID=removeGroups.slice();
  1160. tmp2.formSave=true;
  1161. lockAndPerformToCollection(tmp2, globalRefAddContact.attr('data-filter-url'), 'PUT');
  1162. }
  1163. else
  1164. putVcardToCollectionMain({accountUID: accountUID, uid: inputUID, etag: inputEtag, vcard: vCardText}, inputFilterUID);
  1165. }
  1166. }
  1167. }
  1168. function vcardToData(inputContact, inputIsReadonly, inputIsCompany, inputEditorMode)
  1169. {
  1170. if(typeof globalDebug!='undefined' && globalDebug instanceof Array && globalDebug.indexOf('vcard')!=-1)
  1171. console.time('vcardToData timer');
  1172. if(inputContact.vcard==undefined)
  1173. {
  1174. console.log("Error: '"+inputContact.uid+"': unable to parse vCard");
  1175. return false;
  1176. }
  1177. var tmpvCardEditorRef=CardDAVeditor_cleanup(false, inputIsCompany); // editor initialization
  1178. $('#ABContactColor').css('background-color', inputContact.color);
  1179. if(typeof globalDisabledContactAttributes=='undefined' || !(globalDisabledContactAttributes instanceof Array))
  1180. globalDisabledContactAttributes=[];
  1181. if(inputContact.vcard.match(vCard.pre['vcard']))
  1182. {
  1183. // ------------------------------------------------------------------------------------- //
  1184. // BEGIN and END
  1185. vcard_full=inputContact.vcard.split('\r\n'); // vCard data to array
  1186. // parsed (contentline_parse) = [1]->"group.", [2]->"name", [3]->";param;param", [4]->"value"
  1187. if((parsed=('\r\n'+vcard_full[0]+'\r\n').match(vCard.pre['contentline_parse']))==null)
  1188. {
  1189. console.log("Error: '"+inputContact.uid+"': unable to parse vCard");
  1190. return false;
  1191. }
  1192. // values not directly supported by the editor (old values are kept intact)
  1193. vCard.tplM['begin'][0]=vCard.tplC['begin'].replace('##:::##group_wd##:::##', vcard_begin_group=parsed[1]);
  1194. // parsed (contentline_parse) = [1]->"group.", [2]->"name", [3]->";param;param", [4]->"value"
  1195. if((parsed=('\r\n'+vcard_full[vcard_full.length-2]+'\r\n').match(vCard.pre['contentline_parse']))==null)
  1196. {
  1197. console.log("Error: '"+inputContact.uid+"': unable to parse vCard");
  1198. return false;
  1199. }
  1200. // values not directly supported by the editor (old values are kept intact)
  1201. vCard.tplM['end'][0]=vCard.tplC['end'].replace('##:::##group_wd##:::##', vcard_end_group=parsed[1]);
  1202. if(vcard_begin_group!=vcard_end_group)
  1203. {
  1204. console.log("Error: '"+inputContact.uid+"': unable to parse vCard");
  1205. return false; // the vCard BEGIN and END "group" are different
  1206. }
  1207. // remove the vCard BEGIN and END
  1208. vcard='\r\n'+vcard_full.slice(1, vcard_full.length-2).join('\r\n')+'\r\n';
  1209. //console.time('VERSION timer');
  1210. // ------------------------------------------------------------------------------------- //
  1211. // VERSION -> what to do if present more than once?
  1212. vcard_element=vcard.match(vCard.pre['contentline_VERSION']);
  1213. if(vcard_element!=null && vcard_element.length==1) // if the VERSION attribute is not present exactly once, vCard is considered invalid
  1214. {
  1215. // parsed (contentline_parse) = [1]->"group.", [2]->"name", [3]->";param;param", [4]->"value"
  1216. parsed=vcard_element[0].match(vCard.pre['contentline_parse']);
  1217. if(parsed[3]=='' && parsed[4]=='3.0') // RFC requirement
  1218. {
  1219. // values not directly supported by the editor (old values are kept intact)
  1220. vCard.tplM['contentline_VERSION'][0]=vCard.tplC['contentline_VERSION'];
  1221. vCard.tplM['contentline_VERSION'][0]=vCard.tplM['contentline_VERSION'][0].replace('##:::##group_wd##:::##', parsed[1]);
  1222. vCard.tplM['contentline_VERSION'][0]=vCard.tplM['contentline_VERSION'][0].replace('##:::##version##:::##', parsed[4]);
  1223. // remove the processed parameter
  1224. vcard=vcard.replace(vcard_element[0], '\r\n');
  1225. // find the corresponding group data (if exists)
  1226. if(parsed[1]!='')
  1227. {
  1228. var re=RegExp('\r\n'+parsed[1].replace('.','\\..*')+'\r\n', 'im');
  1229. while((vcard_element_related=vcard.match(re))!=null)
  1230. {
  1231. // append the parameter to its parent
  1232. vCard.tplM['contentline_VERSION'][0]+=vcard_element_related[0].substr(2);
  1233. // remove the processed parameter
  1234. vcard=vcard.replace(vcard_element_related[0],'\r\n');
  1235. }
  1236. }
  1237. }
  1238. else
  1239. {
  1240. console.log("Error: '"+inputContact.uid+"': unable to parse vCard");
  1241. return false; // invalid input for "VERSION" (we support only vCard 3.0)
  1242. }
  1243. }
  1244. else
  1245. {
  1246. console.log("Error: '"+inputContact.uid+"': unable to parse vCard");
  1247. return false; // vcard "VERSION" not present or present more than once
  1248. }
  1249. //console.timeEnd('VERSION timer');
  1250. //console.time('UID timer');
  1251. // ------------------------------------------------------------------------------------- //
  1252. // UID -> TODO: what to do if present more than once?
  1253. vcard_element=vcard.match(vCard.pre['contentline_UID']);
  1254. if(vcard_element!=null && vcard_element.length==1) // if the UID attribute is not present exactly once, vCard is considered invalid
  1255. {
  1256. // parsed (contentline_parse) = [1]->"group.", [2]->"name", [3]->";param;param", [4]->"value"
  1257. parsed=vcard_element[0].match(vCard.pre['contentline_parse']);
  1258. // values not directly supported by the editor (old values are kept intact)
  1259. vCard.tplM['contentline_UID'][0]=vCard.tplC['contentline_UID'];
  1260. vCard.tplM['contentline_UID'][0]=vCard.tplM['contentline_UID'][0].replace('##:::##group_wd##:::##', parsed[1]);
  1261. vCard.tplM['contentline_UID'][0]=vCard.tplM['contentline_UID'][0].replace('##:::##params_wsc##:::##', parsed[3]);
  1262. vCard.tplM['contentline_UID'][0]=vCard.tplM['contentline_UID'][0].replace('##:::##uid##:::##', parsed[4]);
  1263. tmpvCardEditorRef.find('#vCardEditor').attr('data-vcard-uid', parsed[4]); // special hack; usually used by extension hooks
  1264. // remove the processed parameter
  1265. vcard=vcard.replace(vcard_element[0], '\r\n');
  1266. // find the corresponding group data (if exists)
  1267. if(parsed[1]!='')
  1268. {
  1269. var re=RegExp('\r\n'+parsed[1].replace('.','\\..*')+'\r\n', 'im');
  1270. while((vcard_element_related=vcard.match(re))!=null)
  1271. {
  1272. // append the parameter to its parent
  1273. vCard.tplM['contentline_UID'][0]+=vcard_element_related[0].substr(2);
  1274. // remove the processed parameter
  1275. vcard=vcard.replace(vcard_element_related[0],'\r\n');
  1276. }
  1277. }
  1278. }
  1279. // Old not RFC vCards not contain UID - we ignore this error (UID is generated if vCard is changed)
  1280. // else
  1281. // {
  1282. // console.log("Error: '"+inputContact.uid+"': unable to parse vCard");
  1283. // return false; // vcard UID not present or present more than once
  1284. // }
  1285. //console.timeEnd('UID timer');
  1286. //console.time('FN timer');
  1287. // ------------------------------------------------------------------------------------- //
  1288. // FN -> TODO: what to do if present more than once?
  1289. vcard_element=vcard.match(vCard.pre['contentline_FN']);
  1290. if(vcard_element!=null && vcard_element.length==1) // if the FN attribute is not present exactly once, vCard is considered invalid
  1291. {
  1292. // parsed (contentline_parse) = [1]->"group.", [2]->"name", [3]->";param;param", [4]->"value"
  1293. parsed=vcard_element[0].match(vCard.pre['contentline_parse']);
  1294. // values not directly supported by the editor (old values are kept intact)
  1295. vCard.tplM['contentline_FN'][0]=vCard.tplC['contentline_FN'];
  1296. vCard.tplM['contentline_FN'][0]=vCard.tplM['contentline_FN'][0].replace('##:::##group_wd##:::##', parsed[1]);
  1297. vCard.tplM['contentline_FN'][0]=vCard.tplM['contentline_FN'][0].replace('##:::##params_wsc##:::##', parsed[3]);
  1298. // remove the processed parameter
  1299. vcard=vcard.replace(vcard_element[0],'\r\n');
  1300. // find the corresponding group data (if exists)
  1301. if(parsed[1]!='')
  1302. {
  1303. var re=RegExp('\r\n'+parsed[1].replace('.','\\..*')+'\r\n', 'im');
  1304. while((vcard_element_related=vcard.match(re))!=null)
  1305. {
  1306. // append the parameter to its parent
  1307. vCard.tplM['contentline_FN'][0]+=vcard_element_related[0].substr(2);
  1308. // remove the processed parameter
  1309. vcard=vcard.replace(vcard_element_related[0],'\r\n');
  1310. }
  1311. }
  1312. }
  1313. else
  1314. {
  1315. console.log("Error: '"+inputContact.uid+"': unable to parse vCard");
  1316. return false; // vcard FN not present or present more than once
  1317. }
  1318. //console.timeEnd('FN timer');
  1319. //console.time('N timer');
  1320. // ------------------------------------------------------------------------------------- //
  1321. // N -> TODO: what to do if present more than once?
  1322. vcard_element=vcard.match(vCard.pre['contentline_N']);
  1323. if(vcard_element!=null && vcard_element.length==1) // if the N attribute is not present exactly once, vCard is considered invalid
  1324. {
  1325. // parsed (contentline_parse) = [1]->"group.", [2]->"name", [3]->";param;param", [4]->"value"
  1326. parsed=vcard_element[0].match(vCard.pre['contentline_parse']);
  1327. // parsed_value = [0]->Family, [1]->Given, [2]->Middle, [3]->Prefix, [4]->Suffix
  1328. parsed_value=vcardSplitValue(parsed[4],';');
  1329. if(parsed_value[0]!=undefined && parsed_value[0]!='')
  1330. tmpvCardEditorRef.find('[data-type="family"]').val(vcardUnescapeValue(parsed_value[0]));
  1331. if(parsed_value[1]!=undefined && parsed_value[1]!='')
  1332. tmpvCardEditorRef.find('[data-type="given"]').val(vcardUnescapeValue(parsed_value[1]));
  1333. if(parsed_value[2]!=undefined && parsed_value[2]!='')
  1334. tmpvCardEditorRef.find('[data-type="middle"]').val(vcardUnescapeValue(parsed_value[2]));
  1335. if(parsed_value[3]!=undefined && parsed_value[3]!='')
  1336. tmpvCardEditorRef.find('[data-type="prefix"]').val(vcardUnescapeValue(parsed_value[3]));
  1337. if(parsed_value[4]!=undefined && parsed_value[4]!='')
  1338. tmpvCardEditorRef.find('[data-type="suffix"]').val(vcardUnescapeValue(parsed_value[4]));
  1339. // values not directly supported by the editor (old values are kept intact)
  1340. vCard.tplM['contentline_N'][0]=vCard.tplC['contentline_N'];
  1341. vCard.tplM['contentline_N'][0]=vCard.tplM['contentline_N'][0].replace('##:::##group_wd##:::##', parsed[1]);
  1342. vCard.tplM['contentline_N'][0]=vCard.tplM['contentline_N'][0].replace('##:::##params_wsc##:::##', parsed[3]);
  1343. // remove the processed parameter
  1344. vcard=vcard.replace(vcard_element[0],'\r\n');
  1345. // find the corresponding group data (if exists)
  1346. if(parsed[1]!='')
  1347. {
  1348. var re=RegExp('\r\n'+parsed[1].replace('.','\\..*')+'\r\n', 'im');
  1349. while((vcard_element_related=vcard.match(re))!=null)
  1350. {
  1351. // append the parameter to its parent
  1352. vCard.tplM['contentline_N'][0]+=vcard_element_related[0].substr(2);
  1353. // remove the processed parameter
  1354. vcard=vcard.replace(vcard_element_related[0],'\r\n');
  1355. }
  1356. }
  1357. }
  1358. else
  1359. {
  1360. console.log("Error: '"+inputContact.uid+"': unable to parse vCard");
  1361. return false; // vcard N not present or present more than once
  1362. }
  1363. //console.timeEnd('N timer');
  1364. //console.time('CATEGORIES timer');
  1365. // ------------------------------------------------------------------------------------- //
  1366. // CATEGORIES -> present max. once because of the CardDavMATE vCard transformations
  1367. if(globalDisabledContactAttributes.indexOf('CATEGORIES')==-1)
  1368. {
  1369. vcard_element=vcard.match(vCard.pre['contentline_CATEGORIES']);
  1370. if(vcard_element!=null && vcard_element.length==1)
  1371. {
  1372. // parsed (contentline_parse) = [1]->"group.", [2]->"name", [3]->";param;param", [4]->"value"
  1373. parsed=vcard_element[0].match(vCard.pre['contentline_parse']);
  1374. tmpvCardEditorRef.find('#tags').importTags(parsed[4]); // we do not need to unescape the value here!
  1375. // values not directly supported by the editor (old values are kept intact)
  1376. vCard.tplM['contentline_CATEGORIES'][0]=vCard.tplC['contentline_CATEGORIES'];
  1377. vCard.tplM['contentline_CATEGORIES'][0]=vCard.tplM['contentline_CATEGORIES'][0].replace('##:::##group_wd##:::##', parsed[1]);
  1378. vCard.tplM['contentline_CATEGORIES'][0]=vCard.tplM['contentline_CATEGORIES'][0].replace('##:::##params_wsc##:::##', parsed[3]);
  1379. // remove the processed parameter
  1380. vcard=vcard.replace(vcard_element[0],'\r\n');
  1381. // find the corresponding group data (if exists)
  1382. if(parsed[1]!='')
  1383. {
  1384. var re=RegExp('\r\n'+parsed[1].replace('.','\\..*')+'\r\n', 'im');
  1385. while((vcard_element_related=vcard.match(re))!=null)
  1386. {
  1387. // append the parameter to its parent
  1388. vCard.tplM['contentline_CATEGORIES'][0]+=vcard_element_related[0].substr(2);
  1389. // remove the processed parameter
  1390. vcard=vcard.replace(vcard_element_related[0],'\r\n');
  1391. }
  1392. }
  1393. }
  1394. }
  1395. //console.timeEnd('CATEGORIES timer');
  1396. //console.time('NOTE timer');
  1397. // ------------------------------------------------------------------------------------- //
  1398. // NOTE -> TODO: what to do if present more than once?
  1399. if(globalDisabledContactAttributes.indexOf('NOTE')==-1)
  1400. {
  1401. vcard_element=vcard.match(vCard.pre['contentline_NOTE']);
  1402. if(vcard_element!=null)
  1403. {
  1404. if(vcard_element.length==1) // if the NOTE attribute is present exactly once
  1405. {
  1406. // parsed (contentline_parse) = [1]->"group.", [2]->"name", [3]->";param;param", [4]->"value"
  1407. parsed=vcard_element[0].match(vCard.pre['contentline_parse']);
  1408. tmpvCardEditorRef.find('[data-type="\\%note"]').find('textarea').text(vcardUnescapeValue(parsed[4])).trigger('autosize.resize');
  1409. // values not directly supported by the editor (old values are kept intact)
  1410. vCard.tplM['contentline_NOTE'][0]=vCard.tplC['contentline_NOTE'];
  1411. vCard.tplM['contentline_NOTE'][0]=vCard.tplM['contentline_NOTE'][0].replace('##:::##group_wd##:::##', parsed[1]);
  1412. vCard.tplM['contentline_NOTE'][0]=vCard.tplM['contentline_NOTE'][0].replace('##:::##params_wsc##:::##', parsed[3]);
  1413. // remove the processed parameter
  1414. vcard=vcard.replace(vcard_element[0],'\r\n');
  1415. // find the corresponding group data (if exists)
  1416. if(parsed[1]!='')
  1417. {
  1418. var re=RegExp('\r\n'+parsed[1].replace('.','\\..*')+'\r\n', 'im');
  1419. while((vcard_element_related=vcard.match(re))!=null)
  1420. {
  1421. // append the parameter to its parent
  1422. vCard.tplM['contentline_NOTE'][0]+=vcard_element_related[0].substr(2);
  1423. // remove the processed parameter
  1424. vcard=vcard.replace(vcard_element_related[0],'\r\n');
  1425. }
  1426. }
  1427. }
  1428. else
  1429. {
  1430. console.log("Error: '"+inputContact.uid+"': unable to parse vCard");
  1431. return false; // vcard NOTE present more than once
  1432. }
  1433. }
  1434. }
  1435. //console.timeEnd('NOTE timer');
  1436. //console.time('REV timer');
  1437. // ------------------------------------------------------------------------------------- //
  1438. // REV -> what to do if present more than once?
  1439. vcard_element=vcard.match(vCard.pre['contentline_REV']);
  1440. if(vcard_element!=null) // if the REV attribute is exists
  1441. {
  1442. if(vcard_element.length==1) // and is present exactly once
  1443. {
  1444. // parsed (contentline_parse) = [1]->"group.", [2]->"name", [3]->";param;param", [4]->"value"
  1445. parsed=vcard_element[0].match(vCard.pre['contentline_parse']);
  1446. // values not directly supported by the editor (old values are kept intact)
  1447. vCard.tplM['contentline_REV'][0]=vCard.tplC['contentline_REV'];
  1448. vCard.tplM['contentline_REV'][0]=vCard.tplM['contentline_REV'][0].replace('##:::##group_wd##:::##', parsed[1]);
  1449. // remove the processed parameter
  1450. vcard=vcard.replace(vcard_element[0],'\r\n');
  1451. // find the corresponding group data (if exists)
  1452. if(parsed[1]!='')
  1453. {
  1454. var re=RegExp('\r\n'+parsed[1].replace('.','\\..*')+'\r\n', 'im');
  1455. while((vcard_element_related=vcard.match(re))!=null)
  1456. {
  1457. // append the parameter to its parent
  1458. vCard.tplM['contentline_REV'][0]+=vcard_element_related[0].substr(2);
  1459. // remove the processed parameter
  1460. vcard=vcard.replace(vcard_element_related[0],'\r\n');
  1461. }
  1462. }
  1463. }
  1464. else
  1465. {
  1466. console.log("Error: '"+inputContact.uid+"': unable to parse vCard");
  1467. return false; // vcard REV present more than once
  1468. }
  1469. }
  1470. //console.timeEnd('REV timer');
  1471. //console.time('NICKNAME timer');
  1472. // ------------------------------------------------------------------------------------- //
  1473. // NICKNAME -> TODO: what to do if present more than once?
  1474. if(globalDisabledContactAttributes.indexOf('NICKNAME')==-1)
  1475. {
  1476. vcard_element=vcard.match(vCard.pre['contentline_NICKNAME']);
  1477. if(vcard_element!=null)
  1478. {
  1479. if(vcard_element.length!=1) // if the NICKNAME attribute is present more than once, vCard is considered invalid
  1480. {
  1481. console.log("Error: '"+inputContact.uid+"': unable to parse vCard");
  1482. return false;
  1483. }
  1484. // parsed (contentline_parse) = [1]->"group.", [2]->"name", [3]->";param;param", [4]->"value"
  1485. parsed=vcard_element[0].match(vCard.pre['contentline_parse']);
  1486. tmpvCardEditorRef.find('[data-type="nickname"]').val(vcardUnescapeValue(parsed[4]));
  1487. // values not directly supported by the editor (old values are kept intact)
  1488. vCard.tplM['contentline_NICKNAME'][0]=vCard.tplC['contentline_NICKNAME'];
  1489. vCard.tplM['contentline_NICKNAME'][0]=vCard.tplM['contentline_NICKNAME'][0].replace('##:::##group_wd##:::##', parsed[1]);
  1490. vCard.tplM['contentline_NICKNAME'][0]=vCard.tplM['contentline_NICKNAME'][0].replace('##:::##params_wsc##:::##', parsed[3]);
  1491. // remove the processed parameter
  1492. vcard=vcard.replace(vcard_element[0],'\r\n');
  1493. // find the corresponding group data (if exists)
  1494. if(parsed[1]!='')
  1495. {
  1496. var re=RegExp('\r\n'+parsed[1].replace('.','\\..*')+'\r\n', 'im');
  1497. while((vcard_element_related=vcard.match(re))!=null)
  1498. {
  1499. // append the parameter to its parent
  1500. vCard.tplM['contentline_NICKNAME'][0]+=vcard_element_related[0].substr(2);
  1501. // remove the processed parameter
  1502. vcard=vcard.replace(vcard_element_related[0],'\r\n');
  1503. }
  1504. }
  1505. }
  1506. }
  1507. //console.timeEnd('NICKNAME timer');
  1508. //console.time('X-PHONETIC-FIST-NAME timer');
  1509. // ------------------------------------------------------------------------------------- //
  1510. // X-PHONETIC-FIRST-NAME -> TODO: what to do if present more than once?
  1511. if(globalDisabledContactAttributes.indexOf('X-PHONETIC-FIRST-NAME')==-1)
  1512. {
  1513. vcard_element=vcard.match(vCard.pre['contentline_X-PHONETIC-FIRST-NAME']);
  1514. if(vcard_element!=null)
  1515. {
  1516. if(vcard_element.length!=1) // if the X-PHONETIC-FIRST-NAME attribute is present more than once, vCard is considered invalid
  1517. {
  1518. console.log("Error: '"+inputContact.uid+"': unable to parse vCard");
  1519. return false;
  1520. }
  1521. // parsed (contentline_parse) = [1]->"group.", [2]->"name", [3]->";param;param", [4]->"value"
  1522. parsed=vcard_element[0].match(vCard.pre['contentline_parse']);
  1523. tmpvCardEditorRef.find('[data-type="ph_firstname"]').val(vcardUnescapeValue(parsed[4]));
  1524. // values not directly supported by the editor (old values are kept intact)
  1525. vCard.tplM['contentline_X-PHONETIC-FIRST-NAME'][0]=vCard.tplC['contentline_X-PHONETIC-FIRST-NAME'];
  1526. vCard.tplM['contentline_X-PHONETIC-FIRST-NAME'][0]=vCard.tplM['contentline_X-PHONETIC-FIRST-NAME'][0].replace('##:::##group_wd##:::##', parsed[1]);
  1527. vCard.tplM['contentline_X-PHONETIC-FIRST-NAME'][0]=vCard.tplM['contentline_X-PHONETIC-FIRST-NAME'][0].replace('##:::##params_wsc##:::##', parsed[3]);
  1528. // remove the processed parameter
  1529. vcard=vcard.replace(vcard_element[0],'\r\n');
  1530. // find the corresponding group data (if exists)
  1531. if(parsed[1]!='')
  1532. {
  1533. var re=RegExp('\r\n'+parsed[1].replace('.','\\..*')+'\r\n', 'im');
  1534. while((vcard_element_related=vcard.match(re))!=null)
  1535. {
  1536. // append the parameter to its parent
  1537. vCard.tplM['contentline_X-PHONETIC-FIRST-NAME'][0]+=vcard_element_related[0].substr(2);
  1538. // remove the processed parameter
  1539. vcard=vcard.replace(vcard_element_related[0],'\r\n');
  1540. }
  1541. }
  1542. }
  1543. }
  1544. //console.timeEnd('X-PHONETIC-FIST-NAME timer');
  1545. //console.time('X-PHONETIC-LAST-NAME timer');
  1546. // ------------------------------------------------------------------------------------- //
  1547. // X-PHONETIC-LAST-NAME -> TODO: what to do if present more than once?
  1548. if(globalDisabledContactAttributes.indexOf('X-PHONETIC-LAST-NAME')==-1)
  1549. {
  1550. vcard_element=vcard.match(vCard.pre['contentline_X-PHONETIC-LAST-NAME']);
  1551. if(vcard_element!=null)
  1552. {
  1553. if(vcard_element.length!=1) // if the X-PHONETIC-LAST-NAME attribute is present more than once, vCard is considered invalid
  1554. {
  1555. console.log("Error: '"+inputContact.uid+"': unable to parse vCard");
  1556. return false;
  1557. }
  1558. // parsed (contentline_parse) = [1]->"group.", [2]->"name", [3]->";param;param", [4]->"value"
  1559. parsed=vcard_element[0].match(vCard.pre['contentline_parse']);
  1560. tmpvCardEditorRef.find('[data-type="ph_lastname"]').val(vcardUnescapeValue(parsed[4]));
  1561. // values not directly supported by the editor (old values are kept intact)
  1562. vCard.tplM['contentline_X-PHONETIC-LAST-NAME'][0]=vCard.tplC['contentline_X-PHONETIC-LAST-NAME'];
  1563. vCard.tplM['contentline_X-PHONETIC-LAST-NAME'][0]=vCard.tplM['contentline_X-PHONETIC-LAST-NAME'][0].replace('##:::##group_wd##:::##', parsed[1]);
  1564. vCard.tplM['contentline_X-PHONETIC-LAST-NAME'][0]=vCard.tplM['contentline_X-PHONETIC-LAST-NAME'][0].replace('##:::##params_wsc##:::##', parsed[3]);
  1565. // remove the processed parameter
  1566. vcard=vcard.replace(vcard_element[0],'\r\n');
  1567. // find the corresponding group data (if exists)
  1568. if(parsed[1]!='')
  1569. {
  1570. var re=RegExp('\r\n'+parsed[1].replace('.','\\..*')+'\r\n', 'im');
  1571. while((vcard_element_related=vcard.match(re))!=null)
  1572. {
  1573. // append the parameter to its parent
  1574. vCard.tplM['contentline_X-PHONETIC-LAST-NAME'][0]+=vcard_element_related[0].substr(2);
  1575. // remove the processed parameter
  1576. vcard=vcard.replace(vcard_element_related[0],'\r\n');
  1577. }
  1578. }
  1579. }
  1580. }
  1581. //console.timeEnd('X-PHONETIC-LAST-NAME timer');
  1582. //console.time('BDAY timer');
  1583. // ------------------------------------------------------------------------------------- //
  1584. // BDAY
  1585. if(globalDisabledContactAttributes.indexOf('BDAY')==-1)
  1586. {
  1587. vcard_element=vcard.match(vCard.pre['contentline_BDAY']);
  1588. if(vcard_element!=null)
  1589. {
  1590. if(vcard_element.length!=1) // if the BDAY attribute is present more than once, vCard is considered invalid
  1591. {
  1592. console.log("Error: '"+inputContact.uid+"': unable to parse vCard");
  1593. return false;
  1594. }
  1595. // parsed (contentline_parse) = [1]->"group.", [2]->"name", [3]->";param;param", [4]->"value"
  1596. parsed=vcard_element[0].match(vCard.pre['contentline_parse']);
  1597. var valid=true;
  1598. try {var date=$.datepicker.parseDate('yy-mm-dd', parsed[4])}
  1599. catch (e) {valid=false}
  1600. if(valid==true)
  1601. {
  1602. tmpvCardEditorRef.find('[data-type="date_bday"]').val(vcardUnescapeValue($.datepicker.formatDate(globalSettings.datepickerformat.value, date))).change();
  1603. // values not directly supported by the editor (old values are kept intact)
  1604. vCard.tplM['contentline_BDAY'][0]=vCard.tplC['contentline_BDAY'];
  1605. vCard.tplM['contentline_BDAY'][0]=vCard.tplM['contentline_BDAY'][0].replace('##:::##group_wd##:::##', parsed[1]);
  1606. vCard.tplM['contentline_BDAY'][0]=vCard.tplM['contentline_BDAY'][0].replace('##:::##params_wsc##:::##', parsed[3]);
  1607. // remove the processed parameter
  1608. vcard=vcard.replace(vcard_element[0],'\r\n');
  1609. // find the corresponding group data (if exists)
  1610. if(parsed[1]!='')
  1611. {
  1612. var re=RegExp('\r\n'+parsed[1].replace('.','\\..*')+'\r\n', 'im');
  1613. while((vcard_element_related=vcard.match(re))!=null)
  1614. {
  1615. // append the parameter to its parent
  1616. vCard.tplM['contentline_BDAY'][0]+=vcard_element_related[0].substr(2);
  1617. // remove the processed parameter
  1618. vcard=vcard.replace(vcard_element_related[0],'\r\n');
  1619. }
  1620. }
  1621. }
  1622. else
  1623. {
  1624. console.log("Error: '"+inputContact.uid+"': unable to parse vCard");
  1625. return false; // if the date value is invalid, vCard is considered invalid
  1626. }
  1627. }
  1628. }
  1629. //console.timeEnd('BDAY timer');
  1630. //console.time('X-ABDATE timer');
  1631. // ------------------------------------------------------------------------------------- //
  1632. // X-ABDATE
  1633. if(globalDisabledContactAttributes.indexOf('X-ABDATE')==-1)
  1634. {
  1635. var element_i=0;
  1636. while((vcard_element=vcard.match(vCard.pre['contentline_X-ABDATE']))!=null)
  1637. {
  1638. // parsed (contentline_parse) = [1]->"group.", [2]->"name", [3]->";param;param", [4]->"value"
  1639. var parsed=vcard_element[0].match(vCard.pre['contentline_parse']);
  1640. var valid=true;
  1641. try {var date=$.datepicker.parseDate('yy-mm-dd', parsed[4])}
  1642. catch (e) {valid=false}
  1643. if(valid==true)
  1644. {
  1645. // parsed_value = [1..]->X-ABDATE-params
  1646. var parsed_value=vcardSplitParam(parsed[3]);
  1647. // click to "add" button if not enought data rows present
  1648. var tmp_sel=tmpvCardEditorRef.find('[data-type="\\%date"]').last();
  1649. if(tmp_sel.find('[data-type="date_value"]').val()!='')
  1650. tmp_sel.find('[data-type="\\%add"]').find('input[type="image"]').click();
  1651. // get the "TYPE=" values array
  1652. var pref=0; //by default there is no preferred date
  1653. var type_values=Array();
  1654. var j=0;
  1655. for(var i=1; i<parsed_value.length; i++)
  1656. if(parsed_value[i].toLowerCase().indexOf('type=')==0)
  1657. {
  1658. var type_values_tmp=parsed_value[i].substring('type='.length); //case insensitive remove of /^type=/
  1659. // if one value is a comma separated value of parameters
  1660. var type_values_tmp_2=type_values_tmp.split(',');
  1661. var type_value_tmp_2_lower='';
  1662. for(var m=0; m<type_values_tmp_2.length; m++)
  1663. if((type_value_tmp_2_lower=vcardUnescapeValue(type_values_tmp_2[m]).toLowerCase())!='pref')
  1664. type_values[j++]=type_value_tmp_2_lower;
  1665. else
  1666. pref=1;
  1667. }
  1668. if(parsed[1]!='') // APPLE SPECIFIC types: find the corresponding group.X-ABLabel: used by APPLE as "TYPE"
  1669. {
  1670. var vcard_element_related=null;
  1671. var re=RegExp('\r\n'+parsed[1].replace('.','\\.X-ABLabel:(.*)')+'\r\n', 'im');
  1672. while((vcard_element_related=vcard.match(re))!=null)
  1673. {
  1674. // get the X-ABLabel value
  1675. if(type_values.indexOf(vcard_element_related[1].toLowerCase())==-1)
  1676. type_values[j++]=vcardUnescapeValue(':'+vcard_element_related[1].toLowerCase()+':');
  1677. // remove the processed parameter
  1678. vcard=vcard.replace(vcard_element_related[0], '\r\n');
  1679. }
  1680. }
  1681. // get the type value and label
  1682. var type_values_us=type_values.unique().sort();
  1683. var type_values_txt=type_values_us.join(','); // TYPE=INTERNET;TYPE=INTERNET;TYPE=HOME; -> array('HOME','INTERNET') -> 'home,internet'
  1684. var type_values_txt_label=type_values_us.join(' ').replace(vCard.pre['vcardToData_colon_begin_or_end'], ''); // TYPE=INTERNET;TYPE=INTERNET;TYPE=HOME; -> array('HOME','INTERNET') -> 'home internet'
  1685. if(type_values_txt=='') // if no person type defined, we use the 'other' type as default
  1686. type_values_txt=type_values_txt_label='other';
  1687. // get the default available types
  1688. var type_list=new Array();
  1689. tmpvCardEditorRef.find('[data-type="\\%date"]:eq('+element_i+')').find('[data-type="date_type"]').children().each(function(index, element){type_list[type_list.length]=$(element).attr('data-type');});
  1690. // if an existing type regex matches the new type, use the old type
  1691. // and replace the old type definition with new type definition to comforn the server vCard type format
  1692. for(var i=0; i<type_list.length; i++)
  1693. if(dataTypes['date_type'][type_list[i]]!=undefined && type_values_txt.match(dataTypes['date_type'][type_list[i]])!=null)
  1694. {
  1695. tmpvCardEditorRef.find('[data-type="\\%date"]').find('[data-type="date_type"]').find('[data-type="'+type_list[i]+'"]').attr('data-type', type_values_txt);
  1696. break;
  1697. }
  1698. // date type: select or append to existing types and select
  1699. var select_element=tmpvCardEditorRef.find('[data-type="\\%date"]:eq('+element_i+') [data-type="date_type"]').find('[data-type="'+jqueryEscapeSelector(type_values_txt)+'"]');
  1700. if(select_element.length==1)
  1701. select_element.prop('selected', true);
  1702. else if(select_element.length==0)
  1703. {
  1704. // create the missing option
  1705. var new_opt=tmpvCardEditorRef.find('[data-type="date_type"] :first-child').first().clone().attr('data-type',type_values_txt).text(type_values_txt_label);
  1706. // append the option to all element of this type
  1707. tmpvCardEditorRef.find('[data-type="date_type"] :last-child').prev().after(new_opt);
  1708. // select the option on the current type
  1709. tmpvCardEditorRef.find('[data-type="\\%date"]:eq('+element_i+') [data-type="date_type"]').find('[data-type="'+jqueryEscapeSelector(type_values_txt)+'"]').prop('selected', true);
  1710. }
  1711. tmpvCardEditorRef.find('[data-type="\\%date"]:eq('+element_i+') [data-type="date_value"]').val(vcardUnescapeValue($.datepicker.formatDate(globalSettings.datepickerformat.value, date))).change();
  1712. // values not directly supported by the editor (old values are kept intact)
  1713. vCard.tplM['contentline_X-ABDATE'][element_i]=vCard.tplC['contentline_X-ABDATE'];
  1714. vCard.tplM['contentline_X-ABDATE'][element_i]=vCard.tplM['contentline_X-ABDATE'][element_i].replace('##:::##group_wd##:::##', parsed[1]);
  1715. // if the phone person was preferred, we keep it so (we not support preferred person selection directly by editor)
  1716. if(pref==1)
  1717. vCard.tplM['contentline_X-ABDATE'][element_i]=vCard.tplM['contentline_X-ABDATE'][element_i].replace('##:::##params_wsc##:::##', '##:::##params_wsc##:::##;TYPE=PREF');
  1718. // remove the processed parameter
  1719. vcard=vcard.replace(vcard_element[0], '\r\n');
  1720. // find the corresponding group data (if exists)
  1721. if(parsed[1]!='')
  1722. {
  1723. var vcard_element_related=null;
  1724. var re=RegExp('\r\n'+parsed[1].replace('.','\\..*')+'\r\n', 'im');
  1725. while((vcard_element_related=vcard.match(re))!=null)
  1726. {
  1727. // append the parameter to its parent
  1728. vCard.tplM['contentline_X-ABDATE'][element_i]+=vcard_element_related[0].substr(2);
  1729. // remove the processed parameter
  1730. vcard=vcard.replace(vcard_element_related[0], '\r\n');
  1731. }
  1732. }
  1733. element_i++;
  1734. }
  1735. }
  1736. }
  1737. //console.timeEnd('X-ABDATE timer');
  1738. //console.time('TITLE timer');
  1739. // ------------------------------------------------------------------------------------- //
  1740. // TITLE -> TODO: what to do if present more than once?
  1741. if(globalDisabledContactAttributes.indexOf('TITLE')==-1)
  1742. {
  1743. vcard_element=vcard.match(vCard.pre['contentline_TITLE']);
  1744. if(vcard_element!=null)
  1745. {
  1746. if(vcard_element.length!=1) // if the TITLE attribute is present more than once, vCard is considered invalid
  1747. {
  1748. console.log("Error: '"+inputContact.uid+"': unable to parse vCard");
  1749. return false;
  1750. }
  1751. // parsed (contentline_parse) = [1]->"group.", [2]->"name", [3]->";param;param", [4]->"value"
  1752. parsed=vcard_element[0].match(vCard.pre['contentline_parse']);
  1753. tmpvCardEditorRef.find('[data-type="title"]').val(vcardUnescapeValue(parsed[4]));
  1754. // values not directly supported by the editor (old values are kept intact)
  1755. vCard.tplM['contentline_TITLE'][0]=vCard.tplC['contentline_TITLE'];
  1756. vCard.tplM['contentline_TITLE'][0]=vCard.tplM['contentline_TITLE'][0].replace('##:::##group_wd##:::##', parsed[1]);
  1757. vCard.tplM['contentline_TITLE'][0]=vCard.tplM['contentline_TITLE'][0].replace('##:::##params_wsc##:::##', parsed[3]);
  1758. // remove the processed parameter
  1759. vcard=vcard.replace(vcard_element[0],'\r\n');
  1760. // find the corresponding group data (if exists)
  1761. if(parsed[1]!='')
  1762. {
  1763. var re=RegExp('\r\n'+parsed[1].replace('.','\\..*')+'\r\n', 'im');
  1764. while((vcard_element_related=vcard.match(re))!=null)
  1765. {
  1766. // append the parameter to its parent
  1767. vCard.tplM['contentline_TITLE'][0]+=vcard_element_related[0].substr(2);
  1768. // remove the processed parameter
  1769. vcard=vcard.replace(vcard_element_related[0],'\r\n');
  1770. }
  1771. }
  1772. }
  1773. }
  1774. //console.timeEnd('TITLE timer');
  1775. //console.time('ORG timer');
  1776. // ------------------------------------------------------------------------------------- //
  1777. // ORG -> TODO: what to do if present more than once?
  1778. if(globalDisabledContactAttributes.indexOf('ORG')==-1)
  1779. {
  1780. vcard_element=vcard.match(vCard.pre['contentline_ORG']);
  1781. if(vcard_element!=null)
  1782. {
  1783. if(vcard_element.length!=1) // if the ORG attribute is present more than once, vCard is considered invalid
  1784. {
  1785. console.log("Error: '"+inputContact.uid+"': unable to parse vCard");
  1786. return false;
  1787. }
  1788. // parsed (contentline_parse) = [1]->"group.", [2]->"name", [3]->";param;param", [4]->"value"
  1789. parsed=vcard_element[0].match(vCard.pre['contentline_parse']);
  1790. // parsed_value = [0]->Org, [1..]->Org Units
  1791. parsed_value=vcardSplitValue(parsed[4], ';');
  1792. if(parsed_value[0]!=undefined && parsed_value[0]!='')
  1793. tmpvCardEditorRef.find('[data-type="org"]').val(vcardUnescapeValue(parsed_value[0]));
  1794. if(parsed_value[1]!=undefined && parsed_value[1]!='')
  1795. tmpvCardEditorRef.find('[data-type="department"]').val(vcardUnescapeValue(parsed_value[1]));
  1796. // values not directly supported by the editor (old values are kept intact)
  1797. vCard.tplM['contentline_ORG'][0]=vCard.tplC['contentline_ORG'];
  1798. vCard.tplM['contentline_ORG'][0]=vCard.tplM['contentline_ORG'][0].replace('##:::##group_wd##:::##', parsed[1]);
  1799. vCard.tplM['contentline_ORG'][0]=vCard.tplM['contentline_ORG'][0].replace('##:::##params_wsc##:::##', parsed[3]);
  1800. vCard.tplM['contentline_ORG'][0]=vCard.tplM['contentline_ORG'][0].replace('##:::##units_wsc##:::##', (parsed_value[2]==undefined ? '' : ';'+parsed_value.slice(2).join(';')));
  1801. // remove the processed parameter
  1802. vcard=vcard.replace(vcard_element[0],'\r\n');
  1803. // find the corresponding group data (if exists)
  1804. if(parsed[1]!='')
  1805. {
  1806. var re=RegExp('\r\n'+parsed[1].replace('.','\\..*')+'\r\n', 'im');
  1807. while((vcard_element_related=vcard.match(re))!=null)
  1808. {
  1809. // append the parameter to its parent
  1810. vCard.tplM['contentline_ORG'][0]+=vcard_element_related[0].substr(2);
  1811. // remove the processed parameter
  1812. vcard=vcard.replace(vcard_element_related[0],'\r\n');
  1813. }
  1814. }
  1815. }
  1816. }
  1817. //console.timeEnd('ORG timer');
  1818. //console.time('X-ABShowAs timer');
  1819. // ------------------------------------------------------------------------------------- //
  1820. // X-ABShowAs -> TODO: what to do if present more than once?
  1821. var photo_show_org=false;
  1822. if(globalDisabledContactAttributes.indexOf('X-ABShowAs')==-1)
  1823. {
  1824. vcard_element=vcard.match(vCard.pre['X-ABShowAs']);
  1825. if(vcard_element!=null)
  1826. {
  1827. if(vcard_element.length>1) // if the X-ABShowAs attribute is present more than once, vCard is considered invalid
  1828. {
  1829. console.log("Error: '"+inputContact.uid+"': unable to parse vCard");
  1830. return false;
  1831. }
  1832. // parsed (contentline_parse) = [1]->"group.", [2]->"name", [3]->";param;param", [4]->"value"
  1833. parsed=vcard_element[0].match(vCard.pre['contentline_parse']);
  1834. if(vcardUnescapeValue(parsed[4]).match(RegExp('^company$','i')))
  1835. {
  1836. tmpvCardEditorRef.find('[data-type="isorg"]').prop('checked', true);
  1837. photo_show_org=true;
  1838. }
  1839. // values not directly supported by the editor (old values are kept intact)
  1840. vCard.tplM['contentline_X-ABShowAs'][0]=vCard.tplC['contentline_X-ABShowAs'];
  1841. vCard.tplM['contentline_X-ABShowAs'][0]=vCard.tplM['contentline_X-ABShowAs'][0].replace('##:::##group_wd##:::##', parsed[1]);
  1842. vCard.tplM['contentline_X-ABShowAs'][0]=vCard.tplM['contentline_X-ABShowAs'][0].replace('##:::##params_wsc##:::##', parsed[3]);
  1843. vCard.tplM['contentline_X-ABShowAs'][0]=vCard.tplM['contentline_X-ABShowAs'][0].replace('##:::##value##:::##', parsed[4]);
  1844. // remove the processed parameter
  1845. vcard=vcard.replace(vcard_element[0],'\r\n');
  1846. // find the corresponding group data (if exists)
  1847. if(parsed[1]!='')
  1848. {
  1849. var re=RegExp('\r\n'+parsed[1].replace('.','\\..*')+'\r\n', 'im');
  1850. while((vcard_element_related=vcard.match(re))!=null)
  1851. {
  1852. // append the parameter to its parent
  1853. vCard.tplM['contentline_X-ABShowAs'][0]+=vcard_element_related[0].substr(2);
  1854. // remove the processed parameter
  1855. vcard=vcard.replace(vcard_element_related[0],'\r\n');
  1856. }
  1857. }
  1858. }
  1859. }
  1860. //console.timeEnd('X-ABShowAs timer');
  1861. //console.time('PHOTO timer');
  1862. // ------------------------------------------------------------------------------------- //
  1863. // PHOTO -> TODO: what to do if present more than once?
  1864. if(photo_show_org)
  1865. tmpvCardEditorRef.find('#photo').toggleClass('photo_user photo_company');
  1866. if(globalDisabledContactAttributes.indexOf('PHOTO')==-1)
  1867. {
  1868. vcard_element=vcard.match(vCard.pre['contentline_PHOTO']);
  1869. if(vcard_element!=null) // if the PHOTO attribute is present more than once, we use the first value
  1870. {
  1871. // parsed (contentline_parse) = [1]->"group.", [2]->"name", [3]->";param;param", [4]->"value"
  1872. parsed=vcard_element[0].match(vCard.pre['contentline_parse']);
  1873. var img_type = '';
  1874. var custom_params = '';
  1875. var typeRe = RegExp('TYPE=(.*)', 'i');
  1876. var othersRe = RegExp('(?:ENCODING|VALUE)=.*', 'i');
  1877. parsed_value = vcardSplitParam(parsed[3]);
  1878. for(i=1; i<parsed_value.length; i++) {
  1879. if((type_value=parsed_value[i].match(typeRe))!=undefined) {
  1880. img_type=type_value[1].toLowerCase();
  1881. }
  1882. else if(!othersRe.test(parsed_value[i])) {
  1883. custom_params += ';'+parsed_value[i];
  1884. }
  1885. }
  1886. // support also for unknown type of images (stupid clients)
  1887. var photo = parsed[4];
  1888. var isRemote = RegExp('^https?://', 'i').test(photo);
  1889. var newImg = new Image();
  1890. newImg.src = isRemote ? photo : 'data:image'+(img_type!='' ? '/'+img_type : '')+';base64,'+photo.replace(RegExp('^data:(?:image/.*?;)?(?:base64,)?','i'),'');
  1891. newImg.onload = function(){
  1892. loadImage(this);
  1893. };
  1894. if(isRemote) {
  1895. tmpvCardEditorRef.find('#photoURL, #photoURLHidden').val(photo);
  1896. }
  1897. // values not directly supported by the editor (old values are kept intact)
  1898. vCard.tplM['contentline_PHOTO'][0]=vCard.tplC['contentline_PHOTO'];
  1899. vCard.tplM['contentline_PHOTO'][0]=vCard.tplM['contentline_PHOTO'][0].replace('##:::##group_wd##:::##', parsed[1]);
  1900. vCard.tplM['contentline_PHOTO'][0]=vCard.tplM['contentline_PHOTO'][0].replace('##:::##params_wsc##:::##', '##:::##params_wsc##:::##'+custom_params);
  1901. // remove the processed parameter
  1902. vcard=vcard.replace(vcard_element[0],'\r\n');
  1903. // find the corresponding group data (if exists)
  1904. if(parsed[1]!='')
  1905. {
  1906. var re=RegExp('\r\n'+parsed[1].replace('.','\\..*')+'\r\n', 'im');
  1907. while((vcard_element_related=vcard.match(re))!=null)
  1908. {
  1909. // append the parameter to its parent
  1910. vCard.tplM['contentline_PHOTO'][0]+=vcard_element_related[0].substr(2);
  1911. // remove the processed parameter
  1912. vcard=vcard.replace(vcard_element_related[0],'\r\n');
  1913. }
  1914. }
  1915. // // photo URL is used by iCloud but it requires iCloud session cookie :-(
  1916. // if(parsed[4].match(RegExp('^https?://','i'))!=null)
  1917. // tmpvCardEditorRef.find('[data-type="photo"]').attr('src',parsed[4]);
  1918. }
  1919. else // use default icons (see X-ABShowAs above)
  1920. tmpvCardEditorRef.find('#photo').addClass('photo_blank');
  1921. }
  1922. //console.timeEnd('PHOTO timer');
  1923. //console.time('ADR timer');
  1924. // ------------------------------------------------------------------------------------- //
  1925. // ADR
  1926. if(globalDisabledContactAttributes.indexOf('ADR')==-1)
  1927. {
  1928. var element_i=0;
  1929. while((vcard_element=vcard.match(vCard.pre['contentline_ADR']))!=null)
  1930. {
  1931. // parsed (contentline_parse) = [1]->"group.", [2]->"name", [3]->";param;param", [4]->"value"
  1932. var parsed=vcard_element[0].match(vCard.pre['contentline_parse']);
  1933. // parsed_param = [1..]->ADR-params
  1934. var parsed_param=vcardSplitParam(parsed[3]);
  1935. // parsed_value = [1..]->ADR elements
  1936. var parsed_value=vcardSplitValue(parsed[4],';');
  1937. // click to "add" button if not enought data rows present
  1938. var found=0;
  1939. tmpvCardEditorRef.find('[data-type="\\%address"]').last().find('[data-type="value"]').each(
  1940. function(index,element)
  1941. {
  1942. if($(element).val()!='')
  1943. {
  1944. found=1;
  1945. return false;
  1946. }
  1947. }
  1948. );
  1949. if(found)
  1950. tmpvCardEditorRef.find('[data-type="\\%address"]').last().find('[data-type="\\%add"]').find('input[type="image"]').click();
  1951. // get the "TYPE=" values array
  1952. var pref=0; //by default there is no preferred address
  1953. var type_values=Array();
  1954. var j=0;
  1955. for(var i=1; i<parsed_param.length; i++)
  1956. if(parsed_param[i].toLowerCase().indexOf('type=')==0)
  1957. {
  1958. var type_values_tmp=parsed_param[i].substring('type='.length); //case insensitive remove of /^type=/
  1959. // if one value is a comma separated value of parameters
  1960. var type_values_tmp_2=type_values_tmp.split(',');
  1961. var type_value_tmp_2_lower='';
  1962. for(var m=0; m<type_values_tmp_2.length; m++)
  1963. if((type_value_tmp_2_lower=vcardUnescapeValue(type_values_tmp_2[m]).toLowerCase())!='pref')
  1964. type_values[j++]=type_value_tmp_2_lower;
  1965. else
  1966. pref=1;
  1967. }
  1968. if(parsed[1]!='') // APPLE SPECIFIC types: find the corresponding group.X-ABLabel: used by APPLE as "TYPE"
  1969. {
  1970. var vcard_element_related=null;
  1971. var re=RegExp('\r\n'+parsed[1].replace('.','\\.X-ABLabel:(.*)')+'\r\n', 'im');
  1972. while((vcard_element_related=vcard.match(re))!=null)
  1973. {
  1974. // get the X-ABLabel value
  1975. if(type_values.indexOf(vcard_element_related[1].toLowerCase())==-1)
  1976. type_values[j++]=vcardUnescapeValue(':'+vcard_element_related[1].toLowerCase()+':');
  1977. // remove the processed parameter
  1978. vcard=vcard.replace(vcard_element_related[0], '\r\n');
  1979. }
  1980. }
  1981. // find the corresponding group.X-ABADR: used by APPLE as short address country
  1982. var addr_country='';
  1983. if(parsed[1]!='')
  1984. {
  1985. var re=RegExp('\r\n'+parsed[1].replace('.','\\.X-ABADR:(.*)')+'\r\n', 'm');
  1986. if((vcard_element_related=vcard.match(re))!=null)
  1987. {
  1988. // get the X-ABADR value
  1989. addr_country=vcardUnescapeValue(vcard_element_related[1]).toLowerCase();
  1990. // remove the processed parameter
  1991. vcard=vcard.replace(vcard_element_related[0],'\r\n');
  1992. }
  1993. }
  1994. // get the type value and label
  1995. var type_values_us=type_values.unique().sort();
  1996. var type_values_txt=type_values_us.join(','); // TYPE=HOME;TYPE=HOME;TYPE=FAX; -> array('FAX','HOME') -> 'fax,home'
  1997. var type_values_txt_label=type_values_us.join(' ').replace(vCard.pre['vcardToData_colon_begin_or_end'], ''); // TYPE=HOME;TYPE=HOME;TYPE=FAX; -> array('FAX','HOME') -> 'fax home'
  1998. if(type_values_txt=='') // if no address type defined, we use the 'work' type as default
  1999. type_values_txt=type_values_txt_label='work';
  2000. // get the default available types
  2001. var type_list=new Array();
  2002. tmpvCardEditorRef.find('[data-type="\\%address"]:eq('+element_i+')').find('[data-type="address_type"]').children().each(function(index, element){type_list[type_list.length]=$(element).attr('data-type');});
  2003. // if an existing type regex matches the new type, use the old type
  2004. // and replace the old type definition with new type definition to comforn the server vCard type format
  2005. for(var i=0;i<type_list.length;i++)
  2006. if(dataTypes['address_type'][type_list[i]]!=undefined && type_values_txt.match(dataTypes['address_type'][type_list[i]])!=null)
  2007. {
  2008. tmpvCardEditorRef.find('[data-type="\\%address"]').find('[data-type="address_type"]').find('[data-type="'+type_list[i]+'"]').attr('data-type', type_values_txt);
  2009. break;
  2010. }
  2011. // address type: select or append to existing types and select
  2012. var select_element=tmpvCardEditorRef.find('[data-type="\\%address"]:eq('+element_i+') [data-type="address_type"]').find('[data-type="'+jqueryEscapeSelector(type_values_txt)+'"]');
  2013. if(select_element.length==1)
  2014. select_element.prop('selected', true);
  2015. else if(select_element.length==0)
  2016. {
  2017. // create the missing option
  2018. var new_opt=tmpvCardEditorRef.find('[data-type="address_type"] :first-child').first().clone().attr('data-type',type_values_txt).text(type_values_txt_label);
  2019. // append the option to all element of this type
  2020. tmpvCardEditorRef.find('[data-type="address_type"] :last-child').prev().after(new_opt);
  2021. // select the option on the current type
  2022. tmpvCardEditorRef.find('[data-type="\\%address"]:eq('+element_i+') [data-type="address_type"]').find('[data-type="'+jqueryEscapeSelector(type_values_txt)+'"]').prop('selected', true);
  2023. }
  2024. var tmp=tmpvCardEditorRef.find('[data-type="\\%address"]:eq('+element_i+')');
  2025. var found;
  2026. if((found=tmp.find('[data-type="country_type"]').children('[data-type="'+jqueryEscapeSelector(addr_country)+'"]')).length>0 || (found=tmp.find('[data-type="country_type"]').children('[data-full-name="'+jqueryEscapeSelector(parsed_value[6])+'"]')).length>0)
  2027. found.prop('selected', true);
  2028. else if(globalSettings.addresscountryequivalence.value.length>0 && parsed_value[6]!=undefined) // unknown ADR format (country not detected)
  2029. {
  2030. // TODO: move regex object directly into config.js
  2031. for(var i=0; i<globalSettings.addresscountryequivalence.value.length; i++)
  2032. if(parsed_value[6].match(RegExp(globalSettings.addresscountryequivalence.value[i].regex, 'i'))!=null)
  2033. {
  2034. tmp.find('[data-type="country_type"]').children('[data-type="'+jqueryEscapeSelector(globalSettings.addresscountryequivalence.value[i].country)+'"]').prop('selected', true);
  2035. break;
  2036. }
  2037. }
  2038. // Note:
  2039. // if no country detected, the default is used (see globalDefaultAddressCountry in config.js)
  2040. tmp.find('[data-autoselect]').change();
  2041. var streetVals = vcardUnescapeValue(parsed_value[2]).split('\n');
  2042. for(var i=0; i<streetVals.length; i++) {
  2043. var tmp = tmpvCardEditorRef.find('[data-type="\\%address"]:eq('+element_i+') [data-addr-field="street"]').last();
  2044. tmp.val(streetVals[i]);
  2045. if(i<streetVals.length-1) {
  2046. tmp.trigger('keyup.street');
  2047. }
  2048. };
  2049. tmpvCardEditorRef.find('[data-type="\\%address"]:eq('+element_i+') [data-addr-field="pobox"]').val(vcardUnescapeValue(parsed_value[0]));
  2050. tmpvCardEditorRef.find('[data-type="\\%address"]:eq('+element_i+') [data-addr-field="extaddr"]').val(vcardUnescapeValue(parsed_value[1]));
  2051. tmpvCardEditorRef.find('[data-type="\\%address"]:eq('+element_i+') [data-addr-field="locality"]').val(vcardUnescapeValue(parsed_value[3]));
  2052. tmpvCardEditorRef.find('[data-type="\\%address"]:eq('+element_i+') [data-addr-field="region"]').val(vcardUnescapeValue(parsed_value[4]));
  2053. tmpvCardEditorRef.find('[data-type="\\%address"]:eq('+element_i+') [data-addr-field="code"]').val(vcardUnescapeValue(parsed_value[5]));
  2054. // values not directly supported by the editor (old values are kept intact)
  2055. vCard.tplM['contentline_ADR'][element_i]=vCard.tplC['contentline_ADR'];
  2056. vCard.tplM['contentline_ADR'][element_i]=vCard.tplM['contentline_ADR'][element_i].replace('##:::##group_wd##:::##', parsed[1]);
  2057. // if the address was preferred, we keep it so (we not support preferred address selection directly by editor)
  2058. if(pref==1)
  2059. vCard.tplM['contentline_ADR'][element_i]=vCard.tplM['contentline_ADR'][element_i].replace('##:::##params_wsc##:::##', '##:::##params_wsc##:::##;TYPE=PREF');
  2060. // remove the processed parameter
  2061. vcard=vcard.replace(vcard_element[0],'\r\n');
  2062. // find the corresponding group data (if exists)
  2063. if(parsed[1]!='')
  2064. {
  2065. var vcard_element_related=null;
  2066. var re=RegExp('\r\n'+parsed[1].replace('.','\\..*')+'\r\n', 'im');
  2067. while((vcard_element_related=vcard.match(re))!=null)
  2068. {
  2069. // append the parameter to its parent
  2070. vCard.tplM['contentline_ADR'][element_i]+=vcard_element_related[0].substr(2);
  2071. // remove the processed parameter
  2072. vcard=vcard.replace(vcard_element_related[0], '\r\n');
  2073. }
  2074. }
  2075. element_i++;
  2076. }
  2077. }
  2078. //console.timeEnd('ADR timer');
  2079. //console.time('TEL timer');
  2080. // ------------------------------------------------------------------------------------- //
  2081. // TEL
  2082. if(globalDisabledContactAttributes.indexOf('TEL')==-1)
  2083. {
  2084. var element_i=0;
  2085. while((vcard_element=vcard.match(vCard.pre['contentline_TEL']))!=null)
  2086. {
  2087. // parsed (contentline_parse) = [1]->"group.", [2]->"name", [3]->";param;param", [4]->"value"
  2088. var parsed=vcard_element[0].match(vCard.pre['contentline_parse']);
  2089. // parsed_value = [1..]->TEL-params
  2090. var parsed_value=vcardSplitParam(parsed[3]);
  2091. // click to "add" button if not enought data rows present
  2092. var tmp_sel=tmpvCardEditorRef.find('[data-type="\\%phone"]').last();
  2093. if(tmp_sel.find('[data-type="value"]').val()!='')
  2094. tmp_sel.find('[data-type="\\%add"]').find('input[type="image"]').click();
  2095. // get the "TYPE=" values array
  2096. var pref=0; //by default there is no preferred phone number
  2097. var type_values=Array();
  2098. var j=0;
  2099. for(var i=1; i<parsed_value.length; i++)
  2100. if(parsed_value[i].toLowerCase().indexOf('type=')==0)
  2101. {
  2102. var type_values_tmp=parsed_value[i].substring('type='.length); //case insensitive remove of /^type=/
  2103. // if one value is a comma separated value of parameters
  2104. var type_values_tmp_2=type_values_tmp.split(',');
  2105. var type_value_tmp_2_lower='';
  2106. for(var m=0; m<type_values_tmp_2.length; m++)
  2107. if((type_value_tmp_2_lower=vcardUnescapeValue(type_values_tmp_2[m]).toLowerCase())!='pref')
  2108. type_values[j++]=type_value_tmp_2_lower;
  2109. else
  2110. pref=1;
  2111. }
  2112. if(parsed[1]!='') // APPLE SPECIFIC types: find the corresponding group.X-ABLabel: used by APPLE as "TYPE"
  2113. {
  2114. var vcard_element_related=null;
  2115. var re=RegExp('\r\n'+parsed[1].replace('.','\\.X-ABLabel:(.*)')+'\r\n', 'im');
  2116. while((vcard_element_related=vcard.match(re))!=null)
  2117. {
  2118. // get the X-ABLabel value
  2119. if(type_values.indexOf(vcard_element_related[1].toLowerCase())==-1)
  2120. type_values[j++]=vcardUnescapeValue(':'+vcard_element_related[1].toLowerCase()+':');
  2121. // remove the processed parameter
  2122. vcard=vcard.replace(vcard_element_related[0], '\r\n');
  2123. }
  2124. }
  2125. // get the type value and label
  2126. var type_values_us=type_values.unique().sort();
  2127. var type_values_txt=type_values_us.join(','); // TYPE=HOME;TYPE=HOME;TYPE=FAX; -> array('FAX','HOME') -> 'fax,home'
  2128. var type_values_txt_label=type_values_us.join(' ').replace(vCard.pre['vcardToData_colon_begin_or_end'], ''); // TYPE=HOME;TYPE=HOME;TYPE=FAX; -> array('FAX','HOME') -> 'fax home'
  2129. if(type_values_txt=='') // if no phone type defined, we use the 'cell' type as default
  2130. type_values_txt=type_values_txt_label='cell';
  2131. // get the default available types (optimize in future)
  2132. var type_list=new Array();
  2133. tmpvCardEditorRef.find('[data-type="\\%phone"]:eq('+element_i+')').find('[data-type="phone_type"]').children().each(function(index, element){type_list[type_list.length]=$(element).attr('data-type');});
  2134. // if an existing type regex matches the new type, use the old type
  2135. // and replace the old type definition with new type definition to comforn the current vCard type format
  2136. for(var i=0; i<type_list.length; i++)
  2137. if(dataTypes['phone_type'][type_list[i]]!=undefined && type_values_txt.match(dataTypes['phone_type'][type_list[i]])!=null)
  2138. {
  2139. tmpvCardEditorRef.find('[data-type="\\%phone"]').find('[data-type="phone_type"]').find('[data-type="'+type_list[i]+'"]').attr('data-type', type_values_txt);
  2140. break;
  2141. }
  2142. // phone type: select or append to existing types and select
  2143. var select_element=tmpvCardEditorRef.find('[data-type="\\%phone"]:eq('+element_i+') [data-type="phone_type"]').find('[data-type="'+jqueryEscapeSelector(type_values_txt)+'"]');
  2144. if(select_element.length==1)
  2145. select_element.prop('selected', true);
  2146. else if(select_element.length==0)
  2147. {
  2148. // create the missing option
  2149. var new_opt=tmpvCardEditorRef.find('[data-type="phone_type"] :first-child').first().clone().attr('data-type', type_values_txt).text(type_values_txt_label);
  2150. // append the option to all element of this type
  2151. tmpvCardEditorRef.find('[data-type="phone_type"] :last-child').prev().after(new_opt);
  2152. // select the option on the current type
  2153. tmpvCardEditorRef.find('[data-type="\\%phone"]:eq('+element_i+') [data-type="phone_type"]').find('[data-type="'+jqueryEscapeSelector(type_values_txt)+'"]').prop('selected', true);
  2154. }
  2155. tmpvCardEditorRef.find('[data-type="\\%phone"]:eq('+element_i+') [data-type="value"]').val(vcardUnescapeValue(parsed[4]));
  2156. // values not directly supported by the editor (old values are kept intact)
  2157. vCard.tplM['contentline_TEL'][element_i]=vCard.tplC['contentline_TEL'];
  2158. vCard.tplM['contentline_TEL'][element_i]=vCard.tplM['contentline_TEL'][element_i].replace('##:::##group_wd##:::##', parsed[1]);
  2159. // if the phone number was preferred, we keep it so (we not support preferred number selection directly by editor)
  2160. if(pref==1)
  2161. vCard.tplM['contentline_TEL'][element_i]=vCard.tplM['contentline_TEL'][element_i].replace('##:::##params_wsc##:::##', '##:::##params_wsc##:::##;TYPE=PREF');
  2162. // remove the processed parameter
  2163. vcard=vcard.replace(vcard_element[0], '\r\n');
  2164. // find the corresponding group data (if exists)
  2165. if(parsed[1]!='')
  2166. {
  2167. var vcard_element_related=null;
  2168. var re=RegExp('\r\n'+parsed[1].replace('.','\\..*')+'\r\n', 'im');
  2169. while((vcard_element_related=vcard.match(re))!=null)
  2170. {
  2171. // append the parameter to its parent
  2172. vCard.tplM['contentline_TEL'][element_i]+=vcard_element_related[0].substr(2);
  2173. // remove the processed parameter
  2174. vcard=vcard.replace(vcard_element_related[0], '\r\n');
  2175. }
  2176. }
  2177. element_i++;
  2178. }
  2179. }
  2180. //console.timeEnd('TEL timer');
  2181. //console.time('EMAIL timer');
  2182. // ------------------------------------------------------------------------------------- //
  2183. // EMAIL
  2184. if(globalDisabledContactAttributes.indexOf('EMAIL')==-1)
  2185. {
  2186. var element_i=0;
  2187. while((vcard_element=vcard.match(vCard.pre['contentline_EMAIL']))!=null)
  2188. {
  2189. // parsed (contentline_parse) = [1]->"group.", [2]->"name", [3]->";param;param", [4]->"value"
  2190. var parsed=vcard_element[0].match(vCard.pre['contentline_parse']);
  2191. // parsed_value = [1..]->EMAIL-params
  2192. var parsed_value=vcardSplitParam(parsed[3]);
  2193. // click to "add" button if not enought data rows present
  2194. var tmp_sel=tmpvCardEditorRef.find('[data-type="\\%email"]').last();
  2195. if(tmp_sel.find('[data-type="value"]').val()!='')
  2196. tmp_sel.find('[data-type="\\%add"]').find('input[type="image"]').click();
  2197. // get the "TYPE=" values array
  2198. var pref=0; //by default there is no preferred email address
  2199. var type_values=Array();
  2200. var j=0;
  2201. for(var i=1; i<parsed_value.length; i++)
  2202. if(parsed_value[i].toLowerCase().indexOf('type=')==0)
  2203. {
  2204. var type_values_tmp=parsed_value[i].substring('type='.length); //case insensitive remove of /^type=/
  2205. // if one value is a comma separated value of parameters
  2206. var type_values_tmp_2=type_values_tmp.split(',');
  2207. var type_value_tmp_2_lower='';
  2208. for(var m=0; m<type_values_tmp_2.length; m++)
  2209. if((type_value_tmp_2_lower=vcardUnescapeValue(type_values_tmp_2[m]).toLowerCase())!='pref')
  2210. type_values[j++]=type_value_tmp_2_lower;
  2211. else
  2212. pref=1;
  2213. }
  2214. if(parsed[1]!='') // APPLE SPECIFIC types: find the corresponding group.X-ABLabel: used by APPLE as "TYPE"
  2215. {
  2216. var vcard_element_related=null;
  2217. var re=RegExp('\r\n'+parsed[1].replace('.','\\.X-ABLabel:(.*)')+'\r\n', 'im');
  2218. while((vcard_element_related=vcard.match(re))!=null)
  2219. {
  2220. // get the X-ABLabel value
  2221. if(type_values.indexOf(vcard_element_related[1].toLowerCase())==-1)
  2222. type_values[j++]=vcardUnescapeValue(':'+vcard_element_related[1].toLowerCase()+':');
  2223. // remove the processed parameter
  2224. vcard=vcard.replace(vcard_element_related[0], '\r\n');
  2225. }
  2226. }
  2227. // get the type value and label
  2228. var type_values_us=type_values.unique().sort();
  2229. var type_values_txt=type_values_us.join(','); // TYPE=INTERNET;TYPE=INTERNET;TYPE=HOME; -> array('HOME','INTERNET') -> 'home,internet'
  2230. var type_values_txt_label=type_values_us.join(' ').replace(vCard.pre['vcardToData_colon_begin_or_end'], ''); // TYPE=INTERNET;TYPE=INTERNET;TYPE=HOME; -> array('HOME','INTERNET') -> 'home internet'
  2231. if(type_values_txt=='') // if no email type defined, we use the 'home' type as default
  2232. type_values_txt=type_values_txt_label='home,internet';
  2233. // get the default available types
  2234. var type_list=new Array();
  2235. tmpvCardEditorRef.find('[data-type="\\%email"]:eq('+element_i+')').find('[data-type="email_type"]').children().each(function(index, element){type_list[type_list.length]=$(element).attr('data-type');});
  2236. // if an existing type regex matches the new type, use the old type
  2237. // and replace the old type definition with new type definition to comforn the server vCard type format
  2238. for(var i=0; i<type_list.length; i++)
  2239. if(dataTypes['email_type'][type_list[i]]!=undefined && type_values_txt.match(dataTypes['email_type'][type_list[i]])!=null)
  2240. {
  2241. tmpvCardEditorRef.find('[data-type="\\%email"]').find('[data-type="email_type"]').find('[data-type="'+type_list[i]+'"]').attr('data-type', type_values_txt);
  2242. break;
  2243. }
  2244. // email type: select or append to existing types and select
  2245. var select_element=tmpvCardEditorRef.find('[data-type="\\%email"]:eq('+element_i+') [data-type="email_type"]').find('[data-type="'+jqueryEscapeSelector(type_values_txt)+'"]');
  2246. if(select_element.length==1)
  2247. select_element.prop('selected',true);
  2248. else if(select_element.length==0)
  2249. {
  2250. // create the missing option
  2251. new_opt=tmpvCardEditorRef.find('[data-type="email_type"] :first-child').first().clone().attr('data-type',type_values_txt).text(type_values_txt_label);
  2252. // append the option to all element of this type
  2253. tmpvCardEditorRef.find('[data-type="email_type"] :last-child').prev().after(new_opt);
  2254. // select the option on the current type
  2255. tmpvCardEditorRef.find('[data-type="\\%email"]:eq('+element_i+') [data-type="email_type"]').find('[data-type="'+jqueryEscapeSelector(type_values_txt)+'"]').prop('selected',true);
  2256. }
  2257. tmpvCardEditorRef.find('[data-type="\\%email"]:eq('+element_i+') [data-type="value"]').val(vcardUnescapeValue(parsed[4]));
  2258. // values not directly supported by the editor (old values are kept intact)
  2259. vCard.tplM['contentline_EMAIL'][element_i]=vCard.tplC['contentline_EMAIL'];
  2260. vCard.tplM['contentline_EMAIL'][element_i]=vCard.tplM['contentline_EMAIL'][element_i].replace('##:::##group_wd##:::##', parsed[1]);
  2261. // if the phone number was preferred, we keep it so (we not support preferred number selection directly by editor)
  2262. if(pref==1)
  2263. vCard.tplM['contentline_EMAIL'][element_i]=vCard.tplM['contentline_EMAIL'][element_i].replace('##:::##params_wsc##:::##', '##:::##params_wsc##:::##;TYPE=PREF');
  2264. // remove the processed parameter
  2265. vcard=vcard.replace(vcard_element[0], '\r\n');
  2266. // find the corresponding group data (if exists)
  2267. if(parsed[1]!='')
  2268. {
  2269. var vcard_element_related=null;
  2270. var re=RegExp('\r\n'+parsed[1].replace('.','\\..*')+'\r\n', 'im');
  2271. while((vcard_element_related=vcard.match(re))!=null)
  2272. {
  2273. // append the parameter to its parent
  2274. vCard.tplM['contentline_EMAIL'][element_i]+=vcard_element_related[0].substr(2);
  2275. // remove the processed parameter
  2276. vcard=vcard.replace(vcard_element_related[0], '\r\n');
  2277. }
  2278. }
  2279. element_i++;
  2280. }
  2281. }
  2282. //console.timeEnd('EMAIL timer');
  2283. //console.time('X-SOCIALPROFILE timer');
  2284. // ------------------------------------------------------------------------------------- //
  2285. // X-SOCIALPROFILE
  2286. if(globalDisabledContactAttributes.indexOf('X-SOCIALPROFILE')==-1)
  2287. {
  2288. var element_i=0;
  2289. while((vcard_element=vcard.match(vCard.pre['contentline_X-SOCIALPROFILE']))!=null)
  2290. {
  2291. // parsed (contentline_parse) = [1]->"group.", [2]->"name", [3]->";param;param", [4]->"value"
  2292. var parsed=vcard_element[0].match(vCard.pre['contentline_parse']);
  2293. // parsed_value = [1..]->X-SOCIALPROFILE-params
  2294. var parsed_value=vcardSplitParam(parsed[3]);
  2295. // click to "add" button if not enought data rows present
  2296. var tmp_sel=tmpvCardEditorRef.find('[data-type="\\%profile"]').last();
  2297. if(tmp_sel.find('[data-type="value"]').val()!='')
  2298. tmp_sel.find('[data-type="\\%add"]').find('input[type="image"]').click();
  2299. // get the "TYPE=" values array
  2300. var pref=0; //by default there is no preferred X-SOCIALPROFILE
  2301. var type_values=Array();
  2302. var j=0;
  2303. var social_user='';
  2304. for(i=1;i<parsed_value.length;i++)
  2305. if(parsed_value[i].toLowerCase().indexOf('x-user=')==0)
  2306. social_user=parsed_value[i].substring('x-user='.length); //case insensitive remove of /^x-user=/
  2307. else if(parsed_value[i].toLowerCase().indexOf('type=')==0)
  2308. {
  2309. var type_values_tmp=parsed_value[i].substring('type='.length); //case insensitive remove of /^type=/
  2310. // if one value is a comma separated value of parameters
  2311. var type_values_tmp_2=type_values_tmp.split(',');
  2312. var type_value_tmp_2_lower='';
  2313. for(var m=0; m<type_values_tmp_2.length; m++)
  2314. if((type_value_tmp_2_lower=vcardUnescapeValue(type_values_tmp_2[m]).toLowerCase())!='pref')
  2315. type_values[j++]=type_value_tmp_2_lower;
  2316. else
  2317. pref=1;
  2318. }
  2319. // if there is no x-user parameter extract the username from the URL (last part of the URL before '/')
  2320. if(social_user=='')
  2321. social_user=parsed[4].split('/').slice(-2)[0];
  2322. // get the type value and label
  2323. var type_values_us=type_values.unique().sort();
  2324. var type_values_txt=type_values_us.join(','); // TYPE=B;TYPE=A;TYPE=C; -> array('B','A','C') -> 'a,b,c'
  2325. var type_values_txt_label=type_values_us.join(' ').replace(vCard.pre['vcardToData_colon_begin_or_end'], ''); // TYPE=B;TYPE=A;TYPE=C; -> array('B','A','C') -> 'a b c'
  2326. if(type_values_txt=='') // if no X-SOCIALPROFILE type defined, we use the 'twitter' type as default
  2327. type_values_txt=type_values_txt_label='twitter';
  2328. // get the default available types
  2329. var type_list=new Array();
  2330. tmpvCardEditorRef.find('[data-type="\\%profile"]:eq('+element_i+')').find('[data-type="profile_type"]').children().each(function(index, element){type_list[type_list.length]=$(element).attr('data-type');});
  2331. // if an existing type regex matches the new type, use the old type
  2332. // and replace the old type definition with new type definition to comforn the server vCard type format
  2333. for(var i=0; i<type_list.length; i++)
  2334. if(dataTypes['profile_type'][type_list[i]]!=undefined && type_values_txt.match(dataTypes['profile_type'][type_list[i]])!=null)
  2335. {
  2336. tmpvCardEditorRef.find('[data-type="\\%profile"]').find('[data-type="profile_type"]').find('[data-type="'+type_list[i]+'"]').attr('data-type', type_values_txt);
  2337. break;
  2338. }
  2339. // X-SOCIALPROFILE type: select or append to existing types and select
  2340. var select_element=tmpvCardEditorRef.find('[data-type="\\%profile"]:eq('+element_i+') [data-type="profile_type"]').find('[data-type="'+jqueryEscapeSelector(type_values_txt)+'"]');
  2341. if(select_element.length==1)
  2342. select_element.prop('selected',true);
  2343. else if(select_element.length==0)
  2344. {
  2345. // create the missing option
  2346. new_opt=tmpvCardEditorRef.find('[data-type="profile_type"] :first-child').first().clone().attr('data-type',type_values_txt).text(type_values_txt_label);
  2347. // append the option to all element of this type
  2348. tmpvCardEditorRef.find('[data-type="profile_type"] :last-child').prev().after(new_opt);
  2349. // select the option on the current type
  2350. tmpvCardEditorRef.find('[data-type="\\%profile"]:eq('+element_i+') [data-type="profile_type"]').find('[data-type="'+jqueryEscapeSelector(type_values_txt)+'"]').prop('selected', true);
  2351. }
  2352. tmpvCardEditorRef.find('[data-type="\\%profile"]:eq('+element_i+') [data-type="value"]').val(vcardUnescapeValue(type_values_txt=='twitter' ? '@'+social_user : social_user));
  2353. // values not directly supported by the editor (old values are kept intact)
  2354. vCard.tplM['contentline_X-SOCIALPROFILE'][element_i]=vCard.tplC['contentline_X-SOCIALPROFILE'];
  2355. vCard.tplM['contentline_X-SOCIALPROFILE'][element_i]=vCard.tplM['contentline_X-SOCIALPROFILE'][element_i].replace('##:::##group_wd##:::##', parsed[1]);
  2356. // if the X-SOCIALPROFILE was preferred, we keep it so (we not support preferred X-SOCIALPROFILE selection directly by editor)
  2357. if(pref==1)
  2358. vCard.tplM['contentline_X-SOCIALPROFILE'][element_i]=vCard.tplM['contentline_X-SOCIALPROFILE'][element_i].replace('##:::##params_wsc##:::##', '##:::##params_wsc##:::##;TYPE=PREF');
  2359. // remove the processed parameter
  2360. vcard=vcard.replace(vcard_element[0], '\r\n');
  2361. // find the corresponding group data (if exists)
  2362. if(parsed[1]!='')
  2363. {
  2364. var vcard_element_related=null;
  2365. var re=RegExp('\r\n'+parsed[1].replace('.','\\..*')+'\r\n', 'im');
  2366. while((vcard_element_related=vcard.match(re))!=null)
  2367. {
  2368. // append the parameter to its parent
  2369. vCard.tplM['contentline_X-SOCIALPROFILE'][element_i]+=vcard_element_related[0].substr(2);
  2370. // remove the processed parameter
  2371. vcard=vcard.replace(vcard_element_related[0], '\r\n');
  2372. }
  2373. }
  2374. element_i++;
  2375. }
  2376. }
  2377. //console.timeEnd('X-SOCIALPROFILE timer');
  2378. //console.time('URL timer');
  2379. // ------------------------------------------------------------------------------------- //
  2380. // URL
  2381. if(globalDisabledContactAttributes.indexOf('URL')==-1)
  2382. {
  2383. var element_i=0;
  2384. while((vcard_element=vcard.match(vCard.pre['contentline_URL']))!=null)
  2385. {
  2386. // parsed (contentline_parse) = [1]->"group.", [2]->"name", [3]->";param;param", [4]->"value"
  2387. var parsed=vcard_element[0].match(vCard.pre['contentline_parse']);
  2388. // parsed_value = [1..]->URL-params
  2389. var parsed_value=vcardSplitParam(parsed[3]);
  2390. // click to "add" button if not enought data rows present
  2391. var tmp_sel=tmpvCardEditorRef.find('[data-type="\\%url"]').last();
  2392. if(tmp_sel.find('[data-type="value"]').val()!='')
  2393. tmp_sel.find('[data-type="\\%add"]').find('input[type="image"]').click();
  2394. // get the "TYPE=" values array
  2395. var pref=0; //by default there is no preferred url address
  2396. var type_values=Array();
  2397. var j=0;
  2398. for(var i=1; i<parsed_value.length; i++)
  2399. if(parsed_value[i].toLowerCase().indexOf('type=')==0)
  2400. {
  2401. var type_values_tmp=parsed_value[i].substring('type='.length); //case insensitive remove of /^type=/
  2402. // if one value is a comma separated value of parameters
  2403. var type_values_tmp_2=type_values_tmp.split(',');
  2404. var type_value_tmp_2_lower='';
  2405. for(var m=0; m<type_values_tmp_2.length; m++)
  2406. if((type_value_tmp_2_lower=vcardUnescapeValue(type_values_tmp_2[m]).toLowerCase())!='pref')
  2407. type_values[j++]=type_value_tmp_2_lower;
  2408. else
  2409. pref=1;
  2410. }
  2411. if(parsed[1]!='') // APPLE SPECIFIC types: find the corresponding group.X-ABLabel: used by APPLE as "TYPE"
  2412. {
  2413. var vcard_element_related=null;
  2414. var re=RegExp('\r\n'+parsed[1].replace('.','\\.X-ABLabel:(.*)')+'\r\n', 'im');
  2415. while((vcard_element_related=vcard.match(re))!=null)
  2416. {
  2417. // get the X-ABLabel value
  2418. if(type_values.indexOf(vcard_element_related[1].toLowerCase())==-1)
  2419. type_values[j++]=vcardUnescapeValue(':'+vcard_element_related[1].toLowerCase()+':');
  2420. // remove the processed parameter
  2421. vcard=vcard.replace(vcard_element_related[0], '\r\n');
  2422. }
  2423. }
  2424. // get the type value and label
  2425. var type_values_us=type_values.unique().sort();
  2426. var type_values_txt=type_values_us.join(','); // TYPE=WORK;TYPE=WORK;TYPE=HOME; -> array('HOME','WORK') -> 'home,work'
  2427. var type_values_txt_label=type_values_us.join(' ').replace(vCard.pre['vcardToData_colon_begin_or_end'], ''); // TYPE=WORK;TYPE=WORK;TYPE=HOME; -> array('HOME','WORK') -> 'home work'
  2428. if(type_values_txt=='') // if no url type defined, we use the 'homepage' type as default
  2429. type_values_txt=type_values_txt_label='homepage';
  2430. // get the default available types (optimize in future)
  2431. var type_list=new Array();
  2432. tmpvCardEditorRef.find('[data-type="\\%url"]:eq('+element_i+')').find('[data-type="url_type"]').children().each(function(index, element){type_list[type_list.length]=$(element).attr('data-type');});
  2433. // if an existing type regex matches the new type, use the old type
  2434. // and replace the old type definition with new type definition to comforn the server vCard type format
  2435. for(var i=0; i<type_list.length; i++)
  2436. if(dataTypes['url_type'][type_list[i]]!=undefined && type_values_txt.match(dataTypes['url_type'][type_list[i]])!=null)
  2437. {
  2438. tmpvCardEditorRef.find('[data-type="\\%url"]').find('[data-type="url_type"]').find('[data-type="'+type_list[i]+'"]').attr('data-type', type_values_txt);
  2439. break;
  2440. }
  2441. // url type: select or append to existing types and select
  2442. var select_element=tmpvCardEditorRef.find('[data-type="\\%url"]:eq('+element_i+') [data-type="url_type"]').find('[data-type="'+jqueryEscapeSelector(type_values_txt)+'"]');
  2443. if(select_element.length==1)
  2444. select_element.prop('selected', true);
  2445. else if(select_element.length==0)
  2446. {
  2447. // create the missing option
  2448. var new_opt=tmpvCardEditorRef.find('[data-type="url_type"] :first-child').first().clone().attr('data-type',type_values_txt).text(type_values_txt_label);
  2449. // append the option to all element of this type
  2450. tmpvCardEditorRef.find('[data-type="url_type"] :last-child').prev().after(new_opt);
  2451. // select the option on the current type
  2452. tmpvCardEditorRef.find('[data-type="\\%url"]:eq('+element_i+') [data-type="url_type"]').find('[data-type="'+jqueryEscapeSelector(type_values_txt)+'"]').prop('selected', true);
  2453. }
  2454. tmpvCardEditorRef.find('[data-type="\\%url"]:eq('+element_i+') [data-type="value"]').val(vcardUnescapeValue(parsed[4]));
  2455. // values not directly supported by the editor (old values are kept intact)
  2456. vCard.tplM['contentline_URL'][element_i]=vCard.tplC['contentline_URL'];
  2457. vCard.tplM['contentline_URL'][element_i]=vCard.tplM['contentline_URL'][element_i].replace('##:::##group_wd##:::##', parsed[1]);
  2458. // if the URL was preferred, we keep it so (we not support preferred number selection directly by editor)
  2459. if(pref==1)
  2460. vCard.tplM['contentline_URL'][element_i]=vCard.tplM['contentline_URL'][element_i].replace('##:::##params_wsc##:::##', '##:::##params_wsc##:::##;TYPE=PREF');
  2461. // remove the processed parameter
  2462. vcard=vcard.replace(vcard_element[0], '\r\n');
  2463. // find the corresponding group data (if exists)
  2464. if(parsed[1]!='')
  2465. {
  2466. var vcard_element_related=null;
  2467. var re=RegExp('\r\n'+parsed[1].replace('.','\\..*')+'\r\n', 'im');
  2468. while((vcard_element_related=vcard.match(re))!=null)
  2469. {
  2470. // append the parameter to its parent
  2471. vCard.tplM['contentline_URL'][element_i]+=vcard_element_related[0].substr(2);
  2472. // remove the processed parameter
  2473. vcard=vcard.replace(vcard_element_related[0], '\r\n');
  2474. }
  2475. }
  2476. element_i++;
  2477. }
  2478. }
  2479. //console.timeEnd('URL timer');
  2480. //
  2481. //console.time('X-ABRELATEDNAMES timer');
  2482. // ------------------------------------------------------------------------------------- //
  2483. // X-ABRELATEDNAMES
  2484. if(globalDisabledContactAttributes.indexOf('X-ABRELATEDNAMES')==-1)
  2485. {
  2486. var element_i=0;
  2487. while((vcard_element=vcard.match(vCard.pre['contentline_X-ABRELATEDNAMES']))!=null)
  2488. {
  2489. // parsed (contentline_parse) = [1]->"group.", [2]->"name", [3]->";param;param", [4]->"value"
  2490. var parsed=vcard_element[0].match(vCard.pre['contentline_parse']);
  2491. // parsed_value = [1..]->X-ABRELATEDNAMES-params
  2492. var parsed_value=vcardSplitParam(parsed[3]);
  2493. // click to "add" button if not enought data rows present
  2494. var tmp_sel=tmpvCardEditorRef.find('[data-type="\\%person"]').last();
  2495. if(tmp_sel.find('[data-type="value"]').val()!='')
  2496. tmp_sel.find('[data-type="\\%add"]').find('input[type="image"]').click();
  2497. // get the "TYPE=" values array
  2498. var pref=0; //by default there is no preferred person
  2499. var type_values=Array();
  2500. var j=0;
  2501. for(var i=1; i<parsed_value.length; i++)
  2502. if(parsed_value[i].toLowerCase().indexOf('type=')==0)
  2503. {
  2504. var type_values_tmp=parsed_value[i].substring('type='.length); //case insensitive remove of /^type=/
  2505. // if one value is a comma separated value of parameters
  2506. var type_values_tmp_2=type_values_tmp.split(',');
  2507. var type_value_tmp_2_lower='';
  2508. for(var m=0; m<type_values_tmp_2.length; m++)
  2509. if((type_value_tmp_2_lower=vcardUnescapeValue(type_values_tmp_2[m]).toLowerCase())!='pref')
  2510. type_values[j++]=type_value_tmp_2_lower;
  2511. else
  2512. pref=1;
  2513. }
  2514. if(parsed[1]!='') // APPLE SPECIFIC types: find the corresponding group.X-ABLabel: used by APPLE as "TYPE"
  2515. {
  2516. var vcard_element_related=null;
  2517. var re=RegExp('\r\n'+parsed[1].replace('.','\\.X-ABLabel:(.*)')+'\r\n', 'im');
  2518. while((vcard_element_related=vcard.match(re))!=null)
  2519. {
  2520. // get the X-ABLabel value
  2521. if(type_values.indexOf(vcard_element_related[1].toLowerCase())==-1)
  2522. type_values[j++]=vcardUnescapeValue(':'+vcard_element_related[1].toLowerCase()+':');
  2523. // remove the processed parameter
  2524. vcard=vcard.replace(vcard_element_related[0], '\r\n');
  2525. }
  2526. }
  2527. // get the type value and label
  2528. var type_values_us=type_values.unique().sort();
  2529. var type_values_txt=type_values_us.join(','); // TYPE=INTERNET;TYPE=INTERNET;TYPE=HOME; -> array('HOME','INTERNET') -> 'home,internet'
  2530. var type_values_txt_label=type_values_us.join(' ').replace(vCard.pre['vcardToData_colon_begin_or_end'], ''); // TYPE=INTERNET;TYPE=INTERNET;TYPE=HOME; -> array('HOME','INTERNET') -> 'home internet'
  2531. if(type_values_txt=='') // if no person type defined, we use the 'other' type as default
  2532. type_values_txt=type_values_txt_label='other';
  2533. // get the default available types
  2534. var type_list=new Array();
  2535. tmpvCardEditorRef.find('[data-type="\\%person"]:eq('+element_i+')').find('[data-type="person_type"]').children().each(function(index, element){type_list[type_list.length]=$(element).attr('data-type');});
  2536. // if an existing type regex matches the new type, use the old type
  2537. // and replace the old type definition with new type definition to comforn the server vCard type format
  2538. for(var i=0; i<type_list.length; i++)
  2539. if(dataTypes['person_type'][type_list[i]]!=undefined && type_values_txt.match(dataTypes['person_type'][type_list[i]])!=null)
  2540. {
  2541. tmpvCardEditorRef.find('[data-type="\\%person"]').find('[data-type="person_type"]').find('[data-type="'+type_list[i]+'"]').attr('data-type', type_values_txt);
  2542. break;
  2543. }
  2544. // person type: select or append to existing types and select
  2545. var select_element=tmpvCardEditorRef.find('[data-type="\\%person"]:eq('+element_i+') [data-type="person_type"]').find('[data-type="'+jqueryEscapeSelector(type_values_txt)+'"]');
  2546. if(select_element.length==1)
  2547. select_element.prop('selected', true);
  2548. else if(select_element.length==0)
  2549. {
  2550. // create the missing option
  2551. var new_opt=tmpvCardEditorRef.find('[data-type="person_type"] :first-child').first().clone().attr('data-type',type_values_txt).text(type_values_txt_label);
  2552. // append the option to all element of this type
  2553. tmpvCardEditorRef.find('[data-type="person_type"] :last-child').prev().after(new_opt);
  2554. // select the option on the current type
  2555. tmpvCardEditorRef.find('[data-type="\\%person"]:eq('+element_i+') [data-type="person_type"]').find('[data-type="'+jqueryEscapeSelector(type_values_txt)+'"]').prop('selected', true);
  2556. }
  2557. tmpvCardEditorRef.find('[data-type="\\%person"]:eq('+element_i+') [data-type="value"]').val(vcardUnescapeValue(parsed[4]));
  2558. // values not directly supported by the editor (old values are kept intact)
  2559. vCard.tplM['contentline_X-ABRELATEDNAMES'][element_i]=vCard.tplC['contentline_X-ABRELATEDNAMES'];
  2560. vCard.tplM['contentline_X-ABRELATEDNAMES'][element_i]=vCard.tplM['contentline_X-ABRELATEDNAMES'][element_i].replace('##:::##group_wd##:::##', parsed[1]);
  2561. // if the phone person was preferred, we keep it so (we not support preferred person selection directly by editor)
  2562. if(pref==1)
  2563. vCard.tplM['contentline_X-ABRELATEDNAMES'][element_i]=vCard.tplM['contentline_X-ABRELATEDNAMES'][element_i].replace('##:::##params_wsc##:::##', '##:::##params_wsc##:::##;TYPE=PREF');
  2564. // remove the processed parameter
  2565. vcard=vcard.replace(vcard_element[0], '\r\n');
  2566. // find the corresponding group data (if exists)
  2567. if(parsed[1]!='')
  2568. {
  2569. var vcard_element_related=null;
  2570. var re=RegExp('\r\n'+parsed[1].replace('.','\\..*')+'\r\n', 'im');
  2571. while((vcard_element_related=vcard.match(re))!=null)
  2572. {
  2573. // append the parameter to its parent
  2574. vCard.tplM['contentline_X-ABRELATEDNAMES'][element_i]+=vcard_element_related[0].substr(2);
  2575. // remove the processed parameter
  2576. vcard=vcard.replace(vcard_element_related[0], '\r\n');
  2577. }
  2578. }
  2579. element_i++;
  2580. }
  2581. }
  2582. //console.timeEnd('X-ABRELATEDNAMES timer');
  2583. //console.time('IMPP timer');
  2584. // ------------------------------------------------------------------------------------- //
  2585. // IMPP
  2586. if(globalDisabledContactAttributes.indexOf('IMPP')==-1)
  2587. {
  2588. var element_i=0;
  2589. while((vcard_element=vcard.match(vCard.pre['contentline_IMPP']))!=null)
  2590. {
  2591. // parsed (contentline_parse) = [1]->"group.", [2]->"name", [3]->";param;param", [4]->"value"
  2592. var parsed=vcard_element[0].match(vCard.pre['contentline_parse']);
  2593. // parsed_value = [1..]->IMPP-params
  2594. var parsed_value=vcardSplitParam(parsed[3]);
  2595. // click to "add" button if not enought data rows present
  2596. var tmp_sel=tmpvCardEditorRef.find('[data-type="\\%im"]').last();
  2597. if(tmp_sel.find('[data-type="value"]').val()!='')
  2598. tmp_sel.find('[data-type="\\%add"]').find('input[type="image"]').click();
  2599. // get the "TYPE=" & "X-SERVICE-TYPE" values array
  2600. var pref=0; //by default there is no preferred IM
  2601. var type_values=Array();
  2602. var j=0;
  2603. var service_type_value='';
  2604. for(var i=1; i<parsed_value.length; i++)
  2605. if(parsed_value[i].toLowerCase().indexOf('type=')==0)
  2606. {
  2607. var type_values_tmp=parsed_value[i].substring('type='.length); //case insensitive remove of /^type=/
  2608. // if one value is a comma separated value of parameters
  2609. var type_values_tmp_2=type_values_tmp.split(',');
  2610. var type_value_tmp_2_lower='';
  2611. for(var m=0; m<type_values_tmp_2.length; m++)
  2612. if((type_value_tmp_2_lower=vcardUnescapeValue(type_values_tmp_2[m]).toLowerCase())!='pref')
  2613. type_values[j++]=type_value_tmp_2_lower;
  2614. else
  2615. pref=1;
  2616. }
  2617. else if(parsed_value[i].toLowerCase().indexOf('x-service-type=')==0)
  2618. service_type_value=vcardUnescapeValue(parsed_value[i].substring('x-service-type='.length)).toLowerCase(); //case insensitive remove of /^x-service-type=/
  2619. if(parsed[1]!='') // APPLE SPECIFIC types: find the corresponding group.X-ABLabel: used by APPLE as "TYPE"
  2620. {
  2621. var vcard_element_related=null;
  2622. var re=RegExp('\r\n'+parsed[1].replace('.','\\.X-ABLabel:(.*)')+'\r\n', 'im');
  2623. while((vcard_element_related=vcard.match(re))!=null)
  2624. {
  2625. // get the X-ABLabel value
  2626. if(type_values.indexOf(vcard_element_related[1].toLowerCase())==-1)
  2627. type_values[j++]=vcardUnescapeValue(':'+vcard_element_related[1].toLowerCase()+':');
  2628. // remove the processed parameter
  2629. vcard=vcard.replace(vcard_element_related[0], '\r\n');
  2630. }
  2631. }
  2632. // get the type value and label
  2633. var type_values_us=type_values.unique().sort();
  2634. type_values_txt=type_values_us.join(','); // TYPE=INTERNET;TYPE=INTERNET;TYPE=HOME; -> array('HOME','INTERNET') -> 'home,internet'
  2635. type_values_txt_label=type_values_us.join(' ').replace(vCard.pre['vcardToData_colon_begin_or_end'], ''); // TYPE=INTERNET;TYPE=INTERNET;TYPE=HOME; -> array('HOME','INTERNET') -> 'home internet'
  2636. if(type_values_txt=='') // if no IMPP type defined, we use the 'other' type as default
  2637. type_values_txt=type_values_txt_label='other';
  2638. // get the default available types
  2639. var type_list=new Array();
  2640. tmpvCardEditorRef.find('[data-type="\\%im"]:eq('+element_i+')').find('[data-type="im_type"]').children().each(function(index, element){type_list[type_list.length]=$(element).attr('data-type');});
  2641. // if an existing type regex matches the new type, use the old type
  2642. // and replace the old type definition with new type definition to comforn the server vCard type format
  2643. for(var i=0; i<type_list.length; i++)
  2644. if(dataTypes['im_type'][type_list[i]]!=undefined && type_values_txt.match(dataTypes['im_type'][type_list[i]])!=null)
  2645. {
  2646. tmpvCardEditorRef.find('[data-type="\\%im"]').find('[data-type="im_type"]').find('[data-type="'+type_list[i]+'"]').attr('data-type', type_values_txt);
  2647. break;
  2648. }
  2649. // IM type: select or append to existing types and select
  2650. var select_element=tmpvCardEditorRef.find('[data-type="\\%im"]:eq('+element_i+') [data-type="im_type"]').find('[data-type="'+jqueryEscapeSelector(type_values_txt)+'"]');
  2651. if(select_element.length==1)
  2652. select_element.prop('selected',true);
  2653. else if(select_element.length==0)
  2654. {
  2655. // create the missing option
  2656. var new_opt=tmpvCardEditorRef.find('[data-type="im_type"] :first-child').first().clone().attr('data-type',type_values_txt).text(type_values_txt_label);
  2657. // append the option to all element of this type
  2658. tmpvCardEditorRef.find('[data-type="im_type"] :last-child').prev().after(new_opt);
  2659. // select the option on the current type
  2660. tmpvCardEditorRef.find('[data-type="\\%im"]:eq('+element_i+') [data-type="im_type"]').find('[data-type="'+jqueryEscapeSelector(type_values_txt)+'"]').prop('selected', true);
  2661. }
  2662. // IM service type: select or append to existing types and select
  2663. select_element=tmpvCardEditorRef.find('[data-type="\\%im"]:eq('+element_i+') [data-type="im_service_type"]').find('[data-type="'+jqueryEscapeSelector(service_type_value)+'"]');
  2664. if(select_element.length==1)
  2665. select_element.prop('selected',true);
  2666. else if(select_element.length==0)
  2667. {
  2668. // create the missing option
  2669. new_opt=tmpvCardEditorRef.find('[data-type="im_service_type"] :first-child').first().clone().attr('data-type',service_type_value).text(service_type_value);
  2670. // append the option to all element of this type
  2671. tmpvCardEditorRef.find('[data-type="im_service_type"] :last-child').prev().after(new_opt);
  2672. // select the option on the current type
  2673. tmpvCardEditorRef.find('[data-type="\\%im"]:eq('+element_i+') [data-type="im_service_type"]').find('[data-type="'+jqueryEscapeSelector(service_type_value)+'"]').prop('selected', true);
  2674. }
  2675. tmpvCardEditorRef.find('[data-type="\\%im"]:eq('+element_i+') [data-type="value"]').val(vcardUnescapeValue(parsed[4].replace(vCard.pre['vcardToData_before_val'], '')));
  2676. // values not directly supported by the editor (old values are kept intact)
  2677. vCard.tplM['contentline_IMPP'][element_i]=vCard.tplC['contentline_IMPP'];
  2678. vCard.tplM['contentline_IMPP'][element_i]=vCard.tplM['contentline_IMPP'][element_i].replace('##:::##group_wd##:::##', parsed[1]);
  2679. // if the IMPP accound was preferred, we keep it so (we not support preferred person selection directly by editor)
  2680. if(pref==1)
  2681. vCard.tplM['contentline_IMPP'][element_i]=vCard.tplM['contentline_IMPP'][element_i].replace('##:::##params_wsc##:::##', '##:::##params_wsc##:::##;TYPE=PREF');
  2682. // remove the processed parameter
  2683. vcard=vcard.replace(vcard_element[0], '\r\n');
  2684. // find the corresponding group data (if exists)
  2685. if(parsed[1]!='')
  2686. {
  2687. var vcard_element_related=null;
  2688. var re=RegExp('\r\n'+parsed[1].replace('.','\\..*')+'\r\n', 'im');
  2689. while((vcard_element_related=vcard.match(re))!=null)
  2690. {
  2691. // append the parameter to its parent
  2692. vCard.tplM['contentline_IMPP'][element_i]+=vcard_element_related[0].substr(2);
  2693. // remove the processed parameter
  2694. vcard=vcard.replace(vcard_element_related[0], '\r\n');
  2695. }
  2696. }
  2697. element_i++;
  2698. }
  2699. }
  2700. //console.timeEnd('IMPP timer');
  2701. // extension hook
  2702. if(typeof(globalContactsExtVcardToData)=='function')
  2703. vcard=globalContactsExtVcardToData(tmpvCardEditorRef, inputContact, vcard);
  2704. // ------------------------------------------------------------------------------------- //
  2705. // Store the vCard URL to XML
  2706. tmpvCardEditorRef.find('#vCardEditor').attr('data-account-uid', inputContact.accountUID);
  2707. tmpvCardEditorRef.find('#vCardEditor').attr('data-url', inputContact.uid);
  2708. tmpvCardEditorRef.find('#vCardEditor').attr('data-etag', inputContact.etag);
  2709. // UID is stored also in the Cancel button (for Add -> Cancel support /loading the previous active contact/)
  2710. if(inputContact.uid!=undefined) // occurs if loadContactByVcard is used (it also appends the UID of previous contact into 'data-id')
  2711. tmpvCardEditorRef.find('#vCardEditor').find('[data-type="cancel"]').attr('data-id', inputContact.uid);
  2712. processEditorElements(tmpvCardEditorRef, inputEditorMode, inputIsReadonly, inputContact.isCompany);
  2713. var tmp_optionslist=[];
  2714. // create the list of available collections to the interface
  2715. for(var i=0; i<globalResourceCardDAVList.collections.length; i++)
  2716. if(globalResourceCardDAVList.collections[i].headerOnly!==true && globalResourceCardDAVList.collections[i].makeLoaded===true)
  2717. tmp_optionslist[tmp_optionslist.length]=$('<option data-type=""></option>').attr({'data-type': globalResourceCardDAVList.collections[i].uid, 'data-color': globalResourceCardDAVList.collections[i].color}).text(globalResourceCardDAVList.collections[i].displayvalue);
  2718. // add the list of available collections to the interface
  2719. tmpvCardEditorRef.find('[data-attr-name="_DEST_"]').append(tmp_optionslist);
  2720. // bind the change event (color change in the editor)
  2721. tmpvCardEditorRef.find('[data-attr-name="_DEST_"]').change(function(){
  2722. var selColl=globalResourceCardDAVList.getCollectionByUID($(this).find('option:selected').attr('data-type'));
  2723. globalRefAddContact.attr('data-url', selColl.uid.replace(RegExp('[^/]+$'),''));
  2724. globalRefAddContact.attr('data-filter-url',selColl.uid); // Set the current addressbook filter uid
  2725. globalRefAddContact.attr('data-account-uid',selColl.accountUID);
  2726. $('#ABContactColor').css('background-color', $(this).find('option:selected').attr('data-color'));
  2727. });
  2728. var collUID='';
  2729. if(typeof inputContact.uid!='undefined')
  2730. collUID= inputContact.uid.replace(RegExp('[^/]*$'),'');
  2731. else
  2732. collUID = globalRefAddContact.attr('data-url');
  2733. var select_elem=tmpvCardEditorRef.find('[data-attr-name="_DEST_"]').find('[data-type="'+jqueryEscapeSelector(collUID)+'"]');
  2734. if(select_elem.length==1)
  2735. select_elem.prop('selected', true);
  2736. if(typeof globalContactsExtVcardToData!='undefined' && !inputIsCompany)
  2737. tmpvCardEditorRef.find('[data-type="DEST"]').addClass('element_no_display');
  2738. // Unprocessed unrelated vCard elements
  2739. vCard.tplM['unprocessed_unrelated']=vcard;
  2740. if(typeof globalDebug!='undefined' && globalDebug instanceof Array && globalDebug.indexOf('vcard')!=-1)
  2741. {
  2742. console.timeEnd('vcardToData timer');
  2743. if(vcard!='\r\n')
  2744. console.log('Warning: [vCard unprocessed unrelated]: '+vcard);
  2745. }
  2746. //clean error message
  2747. $('#ABMessage').height('0');
  2748. $('#ABContact').empty().append(tmpvCardEditorRef);
  2749. var foundGroup=0;
  2750. for(var adr in globalAddressbookList.vcard_groups)
  2751. {
  2752. if(globalAddressbookList.vcard_groups[adr].length>0)
  2753. {
  2754. foundGroup=1;
  2755. break;
  2756. }
  2757. }
  2758. if(foundGroup)
  2759. {
  2760. if(typeof inputContact.uid!='undefined')
  2761. extendDestSelect();
  2762. else
  2763. {
  2764. var selGroup = $('#ResourceCardDAVList').find('.contact_group').find(':input.resourceCardDAV_selected').attr('data-id');
  2765. extendDestSelect(selGroup);
  2766. if(typeof selGroup!= 'undefined')
  2767. select_elem.text(localization[globalInterfaceLanguage].txtVcardGroupsTextSingle.replace('%coll%',globalResourceCardDAVList.getCollectionByUID(collUID).displayvalue));
  2768. }
  2769. }
  2770. if(typeof inputContact.uid !='undefined')
  2771. checkForVcardGroups(inputContact.uid);
  2772. if(typeof(globalContactsSelectProcess)=='function')
  2773. globalContactsSelectProcess(tmpvCardEditorRef, inputContact);
  2774. return true;
  2775. }
  2776. else
  2777. {
  2778. console.log("Error: '"+inputContact.uid+"': unable to parse vCard");
  2779. return false;
  2780. }
  2781. }
  2782. function basicRFCFixesAndCleanup(vcardString)
  2783. {
  2784. // If vCard contains only '\n' instead of '\r\n' we fix it
  2785. if(vcardString.match(vCard.pre['basicRFCFixesAndCleanup_r-m'])==null)
  2786. vcardString=vcardString.replace(vCard.pre['basicRFCFixesAndCleanup_n-gm'], '\r\n');
  2787. // remove multiple empty lines
  2788. vcardString=vcardString.replace(vCard.pre['basicRFCFixesAndCleanup_rnp-gm'], '\r\n');
  2789. // append '\r\n' to the end of the vCard if missing
  2790. if(vcardString[vcardString.length-1]!='\n')
  2791. vcardString+='\r\n';
  2792. // remove line folding
  2793. vcardString=vcardString.replace(vCard.pre['basicRFCFixesAndCleanup_rnwsp-gm'], '');
  2794. // RFC-obsolete PHOTO fix
  2795. vcardString=vcardString.replace(vCard.pre['basicRFCFixesAndCleanup_photo-gim'], '\r\nPHOTO:');
  2796. // ------------------------------------------------------------------------------------- //
  2797. // begin CATEGORIES merge to one CATEGORIES attribute (sorry for related attributes)
  2798. // note: we cannot do this in additionalRFCFixes or normalizeVcard
  2799. var categoriesArr=[];
  2800. var vcard_element=null;
  2801. var vcard_element_related=null;
  2802. while((vcard_element=vcardString.match(vCard.pre['contentline_CATEGORIES']))!=null)
  2803. {
  2804. // parsed (contentline_parse) = [1]->"group.", [2]->"name", [3]->";param;param", [4]->"value"
  2805. var parsed=vcard_element[0].match(vCard.pre['contentline_parse']);
  2806. categoriesArr[categoriesArr.length]=parsed[4];
  2807. // remove the processed parameter
  2808. vcardString=vcardString.replace(vcard_element[0],'\r\n');
  2809. // find the corresponding group data (if exists)
  2810. if(parsed[1]!='')
  2811. {
  2812. var re=RegExp('\r\n'+parsed[1].replace('.','\\..*')+'\r\n', 'm');
  2813. while((vcard_element_related=vcardString.match(re))!=null)
  2814. // remove the processed parameter
  2815. vcardString=vcardString.replace(vcard_element_related[0],'\r\n');
  2816. }
  2817. }
  2818. var categoriesTxt=categoriesArr.join(',');
  2819. var tmp=vcardString.split('\r\n');
  2820. tmp.splice(tmp.length-2,0,'CATEGORIES:'+categoriesTxt);
  2821. // end CATEGORIES cleanup
  2822. // ------------------------------------------------------------------------------------- //
  2823. // ------------------------------------------------------------------------------------- //
  2824. // begin SoGo fixes (company vCards without N and FN attributes)
  2825. // we must perform vCard fixes here because the N and FN attributes are used in the collection list
  2826. // if N attribute is missing we add it
  2827. if(vcardString.match(vCard.pre['contentline_N'])==null)
  2828. tmp.splice(1,0,'N:;;;;');
  2829. // if FN attribute is missing we add it
  2830. if(vcardString.match(vCard.pre['contentline_FN'])==null)
  2831. {
  2832. var fn_value='';
  2833. var tmp2=null;
  2834. // if there is an ORG attribute defined, we use the company name as fn_value (instead of empty string)
  2835. if((tmp2=vcardString.match(vCard.pre['contentline_ORG']))!=null)
  2836. {
  2837. // parsed (contentline_parse) = [1]->"group.", [2]->"name", [3]->";param;param", [4]->"value"
  2838. var parsed=tmp2[0].match(vCard.pre['contentline_parse']);
  2839. // parsed_value = [0]->Org, [1..]->Org Units
  2840. var parsed_value=vcardSplitValue(parsed[4],';');
  2841. fn_value=parsed_value[0];
  2842. }
  2843. tmp.splice(1,0,'FN:'+fn_value);
  2844. }
  2845. vcardString=tmp.join('\r\n');
  2846. // end SoGo fixes
  2847. // ------------------------------------------------------------------------------------- //
  2848. return {vcard: vcardString, categories: categoriesTxt};
  2849. }
  2850. function additionalRFCFixes(vcardString)
  2851. {
  2852. // ------------------------------------------------------------------------------------- //
  2853. var tmp=vcardString.split('\r\n');
  2854. // update non-RFC attributes (special transformations)
  2855. for(var i=1;i<tmp.length-2;i++)
  2856. {
  2857. // parsed = [1]->"group.", [2]->"name", [3]->";param;param", [4]->"value"
  2858. var parsed=('\r\n'+tmp[i]+'\r\n').match(vCard.pre['contentline_parse']);
  2859. if(parsed!=null)
  2860. {
  2861. switch(parsed[2])
  2862. {
  2863. case 'TEL':
  2864. // remove the non-RFC params (Evolution bug)
  2865. var parsed_value=vcardSplitParam(parsed[3]);
  2866. for(var j=parsed_value.length-1;j>0;j--)
  2867. if(parsed_value[j].match(vCard.pre['additionalRFCFixes_tel-param'])==null)
  2868. parsed_value.splice(j,1);
  2869. parsed[3]=parsed_value.join(';');
  2870. tmp[i]=parsed[1]+parsed[2]+parsed[3]+':'+parsed[4];
  2871. break;
  2872. case 'EMAIL':
  2873. // transform the params separated by ',' to 'TYPE=' params and remove the non-RFC params (Evolution bug)
  2874. var parsed_value=vcardSplitParam(parsed[3]);
  2875. for(var j=parsed_value.length-1;j>0;j--)
  2876. if(parsed_value[j].match(vCard.pre['additionalRFCFixes_email-param'])==null)
  2877. {
  2878. if((transformed=parsed_value[j].replace(vCard.pre['additionalRFCFixes_comma-g'], ';TYPE=')).match(vCard.pre['additionalRFCFixes_email-params'])!=null)
  2879. parsed_value[j]=transformed;
  2880. else
  2881. parsed_value.splice(j,1);
  2882. }
  2883. parsed[3]=parsed_value.join(';');
  2884. // add missing and required "internet" type (Sogo bug)
  2885. if(parsed[3].match(vCard.pre['additionalRFCFixes_type-internet'])==null)
  2886. parsed[3]+=';TYPE=INTERNET';
  2887. tmp[i]=parsed[1]+parsed[2]+parsed[3]+':'+parsed[4];
  2888. break;
  2889. // the upcoming vCard 4.0 allows params for URL and many clients use it also in vCard 3.0
  2890. // case 'URL': // no params allowed for URL (Evolution bug)
  2891. // tmp[i]=parsed[1]+parsed[2]+':'+parsed[4];
  2892. // break;
  2893. default:
  2894. break;
  2895. }
  2896. }
  2897. }
  2898. vcardString=tmp.join('\r\n');
  2899. // ------------------------------------------------------------------------------------- //
  2900. return vcardString;
  2901. }
  2902. // transform the vCard to the editor expected format
  2903. function normalizeVcard(vcardString)
  2904. {
  2905. var parsed=null;
  2906. // remove the PRODID element (unusable for the editor)
  2907. while((parsed=vcardString.match(vCard.pre['contentline_PRODID']))!=null)
  2908. vcardString=vcardString.replace(parsed[0],'\r\n');
  2909. var tmp=vcardString.split('\r\n');
  2910. var vcard_begin=tmp[0].replace(vCard.pre['normalizeVcard_group_w_dot'], 'item.')+'\r\n';
  2911. var vcard_end=tmp[tmp.length-2].replace(vCard.pre['normalizeVcard_group_w_dot'], 'item.')+'\r\n';
  2912. // remove the vCard BEGIN and END and all duplicate entries (usually created by other buggy clients)
  2913. vcardString='\r\n'+tmp.slice(1, tmp.length-2).join('\r\n')+'\r\n';
  2914. var vcard_out_grouped=new Array();
  2915. while((parsed=vcardString.match(vCard.pre['contentline_parse']))!=null)
  2916. {
  2917. var additional_related='';
  2918. var vcard_element_related='';
  2919. var attr_name='';
  2920. var params_swc='';
  2921. var attr_value='';
  2922. // parsed = [1]->"group.", [2]->"name", [3]->";param;param", [4]->"value"
  2923. var params_array=vcardSplitParam(parsed[3]);
  2924. // we transform the old X-* IM attributes to new IMPP (internally used by editor)
  2925. switch(parsed[2])
  2926. {
  2927. case 'X-ABDATE':
  2928. attr_name=parsed[2];
  2929. params_swc=params_array.sort().join(';').toUpperCase(); // we need upper case here to remove duplicate values later
  2930. tmp=parsed[4].match(vCard.pre['normalizeVcard_date']);
  2931. attr_value=tmp[1]+'-'+tmp[2]+'-'+tmp[3]; // sorry, we support only date (no date-time support)
  2932. break;
  2933. case 'X-EVOLUTION-ANNIVERSARY':
  2934. case 'X-ANNIVERSARY':
  2935. attr_name='X-ABDATE';
  2936. params_swc='';
  2937. tmp=parsed[4].match(vCard.pre['normalizeVcard_date']);
  2938. attr_value=tmp[1]+'-'+tmp[2]+'-'+tmp[3]; // sorry, we support only date (no date-time support)
  2939. additional_related='X-ABLabel:_$!<Anniversary>!$_\r\n';
  2940. // check for X-ABDATE attribute with the same value
  2941. var found=false;
  2942. var tmpVcardString=vcardString;
  2943. var tmp_vcard_element=null;
  2944. while((tmp_vcard_element=tmpVcardString.match(vCard.pre['contentline_X-ABDATE']))!=null)
  2945. {
  2946. // parsed (contentline_parse) = [1]->"group.", [2]->"name", [3]->";param;param", [4]->"value"
  2947. var tmp_parsed=tmp_vcard_element[0].match(vCard.pre['contentline_parse']);
  2948. if(tmp_parsed[4]==parsed[4] || tmp_parsed[4]==attr_value)
  2949. {
  2950. found=true;
  2951. break;
  2952. }
  2953. tmpVcardString=tmpVcardString.replace(tmp_vcard_element[0], '\r\n');
  2954. }
  2955. if(found==true)
  2956. {
  2957. // remove the processed element
  2958. vcardString=vcardString.replace(parsed[0], '\r\n');
  2959. // find the corresponding group data (if exists)
  2960. if(parsed[1]!='')
  2961. {
  2962. var re=RegExp('\r\n'+parsed[1].replace('.', '\\..*')+'\r\n', 'm');
  2963. while((vcard_element_related=vcardString.match(re))!=null)
  2964. vcardString=vcardString.replace(vcard_element_related[0], '\r\n'); // remove the processed parameter
  2965. }
  2966. continue;
  2967. }
  2968. break;
  2969. case 'BDAY':
  2970. attr_name=parsed[2];
  2971. params_swc=';VALUE=date';
  2972. tmp=parsed[4].match(vCard.pre['normalizeVcard_date']);
  2973. attr_value=tmp[1]+'-'+tmp[2]+'-'+tmp[3]; // sorry, we support only date (no date-time support)
  2974. break;
  2975. case 'X-AIM':
  2976. case 'X-JABBER':
  2977. case 'X-MSN':
  2978. case 'X-YAHOO':
  2979. case 'X-YAHOO-ID':
  2980. case 'X-ICQ':
  2981. case 'X-SKYPE':
  2982. attr_name='IMPP';
  2983. if(params_array.length==0)
  2984. params_array[0]=''; // after the join it generates ';' after the attribute name
  2985. params_array[params_array.length]='X-SERVICE-TYPE='+parsed[2].replace(vCard.pre['normalizeVcard_xb_or_ide'], ''); // extract the IM type
  2986. params_swc=params_array.sort().join(';');
  2987. attr_value=parsed[4];
  2988. // check for IMPP attribute with the same value
  2989. var found=false;
  2990. var tmpVcardString=vcardString;
  2991. var tmp_vcard_element=null;
  2992. while((tmp_vcard_element=tmpVcardString.match(vCard.pre['contentline_IMPP']))!=null)
  2993. {
  2994. // parsed (contentline_parse) = [1]->"group.", [2]->"name", [3]->";param;param", [4]->"value"
  2995. var tmp_parsed=tmp_vcard_element[0].match(vCard.pre['contentline_parse']);
  2996. if(tmp_parsed[4].replace(vCard.pre['normalizeVcard_before_val'], '')==parsed[4])
  2997. {
  2998. found=true;
  2999. break;
  3000. }
  3001. tmpVcardString=tmpVcardString.replace(tmp_vcard_element[0], '\r\n');
  3002. }
  3003. if(found==true)
  3004. {
  3005. // remove the processed element
  3006. vcardString=vcardString.replace(parsed[0], '\r\n');
  3007. // find the corresponding group data (if exists)
  3008. if(parsed[1]!='')
  3009. {
  3010. var re=RegExp('\r\n'+parsed[1].replace('.', '\\..*')+'\r\n', 'm');
  3011. while((vcard_element_related=vcardString.match(re))!=null)
  3012. vcardString=vcardString.replace(vcard_element_related[0], '\r\n'); // remove the processed parameter
  3013. }
  3014. continue;
  3015. }
  3016. break;
  3017. case 'IMPP':
  3018. attr_name=parsed[2];
  3019. params_swc=params_array.sort().join(';').toUpperCase(); // we need upper case here to remove duplicate values later
  3020. // remove the '*:' from the '*:value'
  3021. // but we add them back during the vcard generation from the interface
  3022. attr_value=vcardSplitValue(parsed[4], ':').splice(1, 1).join('')
  3023. break;
  3024. case 'X-ASSISTANT':
  3025. case 'X-EVOLUTION-ASSISTANT':
  3026. attr_name='X-ABRELATEDNAMES';
  3027. params_swc='';
  3028. attr_value=parsed[4];
  3029. additional_related='X-ABLabel:_$!<Assistant>!$_\r\n';
  3030. // check for X-ABRELATEDNAMES attribute with the same value
  3031. var found=false;
  3032. var tmpVcardString=vcardString;
  3033. var tmp_vcard_element=null;
  3034. while((tmp_vcard_element=tmpVcardString.match(vCard.pre['contentline_X-ABRELATEDNAMES']))!=null)
  3035. {
  3036. // parsed (contentline_parse) = [1]->"group.", [2]->"name", [3]->";param;param", [4]->"value"
  3037. var tmp_parsed=tmp_vcard_element[0].match(vCard.pre['contentline_parse']);
  3038. if(tmp_parsed[4]==parsed[4])
  3039. {
  3040. found=true;
  3041. break;
  3042. }
  3043. tmpVcardString=tmpVcardString.replace(tmp_vcard_element[0], '\r\n');
  3044. }
  3045. if(found==true)
  3046. {
  3047. // remove the processed element
  3048. vcardString=vcardString.replace(parsed[0], '\r\n');
  3049. // find the corresponding group data (if exists)
  3050. if(parsed[1]!='')
  3051. {
  3052. var re=RegExp('\r\n'+parsed[1].replace('.', '\\..*')+'\r\n', 'm');
  3053. while((vcard_element_related=vcardString.match(re))!=null)
  3054. vcardString=vcardString.replace(vcard_element_related[0], '\r\n'); // remove the processed parameter
  3055. }
  3056. continue;
  3057. }
  3058. break;
  3059. case 'X-MANAGER':
  3060. case 'X-EVOLUTION-MANAGER':
  3061. attr_name='X-ABRELATEDNAMES';
  3062. params_swc='';
  3063. attr_value=parsed[4];
  3064. additional_related='X-ABLabel:_$!<Manager>!$_\r\n';
  3065. // check for X-ABRELATEDNAMES attribute with the same value
  3066. var found=false;
  3067. var tmpVcardString=vcardString;
  3068. var tmp_vcard_element=null;
  3069. while((tmp_vcard_element=tmpVcardString.match(vCard.pre['contentline_X-ABRELATEDNAMES']))!=null)
  3070. {
  3071. // parsed (contentline_parse) = [1]->"group.", [2]->"name", [3]->";param;param", [4]->"value"
  3072. var tmp_parsed=tmp_vcard_element[0].match(vCard.pre['contentline_parse']);
  3073. if(tmp_parsed[4]==parsed[4])
  3074. {
  3075. found=true;
  3076. break;
  3077. }
  3078. tmpVcardString=tmpVcardString.replace(tmp_vcard_element[0], '\r\n');
  3079. }
  3080. if(found==true)
  3081. {
  3082. // remove the processed element
  3083. vcardString=vcardString.replace(parsed[0], '\r\n');
  3084. // find the corresponding group data (if exists)
  3085. if(parsed[1]!='')
  3086. {
  3087. var re=RegExp('\r\n'+parsed[1].replace('.', '\\..*')+'\r\n', 'm');
  3088. while((vcard_element_related=vcardString.match(re))!=null)
  3089. vcardString=vcardString.replace(vcard_element_related[0], '\r\n'); // remove the processed parameter
  3090. }
  3091. continue;
  3092. }
  3093. break;
  3094. case 'X-SPOUSE':
  3095. case 'X-EVOLUTION-SPOUSE':
  3096. attr_name='X-ABRELATEDNAMES';
  3097. params_swc='';
  3098. attr_value=parsed[4];
  3099. additional_related='X-ABLabel:_$!<Spouse>!$_\r\n';
  3100. // check for X-ABRELATEDNAMES attribute with the same value
  3101. var found=false;
  3102. var tmpVcardString=vcardString;
  3103. var tmp_vcard_element=null;
  3104. while((tmp_vcard_element=tmpVcardString.match(vCard.pre['contentline_X-ABRELATEDNAMES']))!=null)
  3105. {
  3106. // parsed (contentline_parse) = [1]->"group.", [2]->"name", [3]->";param;param", [4]->"value"
  3107. var tmp_parsed=tmp_vcard_element[0].match(vCard.pre['contentline_parse']);
  3108. if(tmp_parsed[4]==parsed[4])
  3109. {
  3110. found=true;
  3111. break;
  3112. }
  3113. tmpVcardString=tmpVcardString.replace(tmp_vcard_element[0], '\r\n');
  3114. }
  3115. if(found==true)
  3116. {
  3117. // remove the processed element
  3118. vcardString=vcardString.replace(parsed[0], '\r\n');
  3119. // find the corresponding group data (if exists)
  3120. if(parsed[1]!='')
  3121. {
  3122. var re=RegExp('\r\n'+parsed[1].replace('.', '\\..*')+'\r\n', 'm');
  3123. while((vcard_element_related=vcardString.match(re))!=null)
  3124. vcardString=vcardString.replace(vcard_element_related[0], '\r\n'); // remove the processed parameter
  3125. }
  3126. continue;
  3127. }
  3128. break;
  3129. default:
  3130. attr_name=parsed[2];
  3131. params_swc=params_array.sort().join(';');
  3132. attr_value=parsed[4];
  3133. break;
  3134. }
  3135. // remove the processed element
  3136. vcardString=vcardString.replace(parsed[0],'\r\n');
  3137. if(attr_name!='FN' && attr_name!='N' && attr_value=='') // attributes with empty values are not supported and are removed here
  3138. {
  3139. // find the corresponding group data (if exists)
  3140. if(parsed[1]!='')
  3141. {
  3142. var re=RegExp('\r\n'+parsed[1].replace('.','\\..*')+'\r\n', 'm');
  3143. while((vcard_element_related=vcardString.match(re))!=null)
  3144. // remove the processed parameter
  3145. vcardString=vcardString.replace(vcard_element_related[0], '\r\n');
  3146. }
  3147. continue;
  3148. }
  3149. // add the new element to output array (without group)
  3150. grouped_elem=new Array();
  3151. grouped_elem[grouped_elem.length]=attr_name+params_swc+':'+attr_value+'\r\n';
  3152. if(additional_related!='') // used if we manually add related items as a part of transformation
  3153. grouped_elem[grouped_elem.length]=additional_related;
  3154. // find the corresponding group data (if exists)
  3155. if(parsed[1]!='')
  3156. {
  3157. var re=RegExp('\r\n'+parsed[1].replace('.','\\.(.*)')+'\r\n', 'm');
  3158. while((vcard_element_related=vcardString.match(re))!=null)
  3159. {
  3160. // add the related element to array
  3161. grouped_elem[grouped_elem.length]=vcard_element_related[1]+'\r\n';
  3162. // remove the processed parameter
  3163. vcardString=vcardString.replace(vcard_element_related[0], '\r\n');
  3164. }
  3165. }
  3166. // add the new grouped element to output
  3167. vcard_out_grouped[vcard_out_grouped.length]=grouped_elem.sort().join('');
  3168. }
  3169. //
  3170. // after the transformation and grouping we remove all identical elements and preserve sorting
  3171. // (for example X-AIM and IMPP;X-SERVICE-TYPE=AIM, ...)
  3172. for(var i=vcard_out_grouped.length-1;i>=0;i--)
  3173. if(vcard_out_grouped.slice(0,i).indexOf(vcard_out_grouped[i])!=-1)
  3174. vcard_out_grouped.splice(i,1);
  3175. // add new group names ...
  3176. elemCounter=0;
  3177. for(i=0;i<vcard_out_grouped.length;i++)
  3178. if(vcard_out_grouped[i].match(vCard.pre['normalizeVcard_rn-gm']).length>1)
  3179. vcard_out_grouped[i]=(('\r\n'+vcard_out_grouped[i].substring(0, vcard_out_grouped[i].length-2)).replace(vCard.pre['normalizeVcard_rn-gm'], '\r\nitem'+(elemCounter++)+'.')+'\r\n').substring(2);
  3180. vcard_out_grouped.unshift(vcard_begin);
  3181. vcard_out_grouped.push(vcard_end);
  3182. return vcard_out_grouped.join('');
  3183. }