/**
  * @NoAdminRequired
  */
 public function getEditFormContact()
 {
     $pId = $this->params('id');
     $vcard = ContactsApp::getContactVCard($pId);
     $active_addressbooks = Addressbook::active($this->userId);
     $editInfoCard = VCard::structureContact($vcard);
     $aDefNArray = array('0' => 'lname', '1' => 'fname', '2' => 'anrede', '3' => 'title');
     $aN = '';
     if (isset($editInfoCard['N'][0]['value']) && count($editInfoCard['N'][0]['value']) > 0) {
         foreach ($editInfoCard['N'][0]['value'] as $key => $val) {
             if ($val != '') {
                 $aN[$aDefNArray[$key]] = $val;
             }
         }
     }
     $aOrgDef = array('0' => 'firm', '1' => 'department');
     $aOrg = array();
     if (isset($editInfoCard['ORG'][0]['value']) && count($editInfoCard['ORG'][0]['value']) > 0) {
         foreach ($editInfoCard['ORG'][0]['value'] as $key => $val) {
             if ($val != '') {
                 $aOrg[$aOrgDef[$key]] = $val;
             }
         }
     }
     $aUrl = array();
     if (isset($editInfoCard['URL']) && count($editInfoCard['URL']) > 0) {
         $iUrlCount = 0;
         foreach ($editInfoCard['URL'] as $urlInfo) {
             $aUrl[$iUrlCount]['val'] = $urlInfo['value'];
             if (array_key_exists('PREF', $urlInfo['parameters'])) {
                 $aUrl[$iUrlCount]['pref'] = 1;
             }
             if (array_key_exists('TYPE', $urlInfo['parameters'])) {
                 foreach ($urlInfo['parameters']['TYPE'] as $typeInfo) {
                     if ($typeInfo !== '' && strtoupper($typeInfo) != 'PREF') {
                         $aUrl[$iUrlCount]['type'] = strtoupper($typeInfo);
                     }
                 }
             }
             $iUrlCount++;
         }
     } else {
         $aUrl[0]['val'] = '';
         $aUrl[0]['type'] = 'INTERNET';
     }
     $sBday = '';
     if (isset($editInfoCard['BDAY'][0]['value']) && !empty($editInfoCard['BDAY'][0]['value'])) {
         $sBday = $editInfoCard['BDAY'][0]['value'];
         $date = new \DateTime($sBday);
         $sBday = $date->format('d.m.Y');
     }
     $sNotice = '';
     if (isset($editInfoCard['NOTE'][0]['value']) && !empty($editInfoCard['NOTE'][0]['value'])) {
         $sNotice = $editInfoCard['NOTE'][0]['value'];
     }
     $sNickname = '';
     if (isset($editInfoCard['NICKNAME'][0]['value']) && !empty($editInfoCard['NICKNAME'][0]['value'])) {
         $sNickname = $editInfoCard['NICKNAME'][0]['value'];
     }
     $sPosition = '';
     if (isset($editInfoCard['TITLE'][0]['value']) && !empty($editInfoCard['TITLE'][0]['value'])) {
         $sPosition = $editInfoCard['TITLE'][0]['value'];
     }
     $addressDefArray = array('0' => '', '1' => '', '2' => 'street', '3' => 'city', '4' => 'state', '5' => 'postalcode', '6' => 'country');
     $aAddr = array();
     if (isset($editInfoCard['ADR']) && count($editInfoCard['ADR']) > 0) {
         $iACount = 0;
         foreach ($editInfoCard['ADR'] as $addrInfo) {
             foreach ($addrInfo['value'] as $key => $val) {
                 $aAddr[$iACount]['val'][$addressDefArray[$key]] = $val;
             }
             if (array_key_exists('PREF', $addrInfo['parameters'])) {
                 $aAddr[$iACount]['pref'] = 1;
             }
             if (array_key_exists('TYPE', $addrInfo['parameters'])) {
                 $temp_sel = '';
                 foreach ($addrInfo['parameters']['TYPE'] as $typeInfo) {
                     if ($typeInfo !== '' && strtoupper($typeInfo) != 'PREF') {
                         if ($temp_sel == '') {
                             $temp_sel = strtoupper($typeInfo);
                         } else {
                             $temp_sel .= '#' . strtoupper($typeInfo);
                         }
                     }
                 }
                 $aAddr[$iACount]['type'] = $temp_sel;
                 if ($aAddr[$iACount]['type'] === '') {
                     $aAddr[$iACount]['type'] = 'WORK';
                 }
             }
             $iACount++;
         }
     } else {
         $aAddr[0]['val'] = array('street' => '', 'postalcode' => '', 'city' => '', 'state' => '', 'country' => '');
         $aAddr[0]['type'] = 'WORK';
     }
     /*TEL;TYPE=pref:0151 50114479
     		TEL;TYPE=WORK,VOICE:0824 7254
     		TEL;TYPE=WORK,FAX:08224 78335
     		TEL;TYPE=CELL,VOICE:0160 8042056*/
     $aTel = array();
     if (isset($editInfoCard['TEL']) && count($editInfoCard['TEL']) > 0) {
         $iCount = 0;
         foreach ($editInfoCard['TEL'] as $telInfo) {
             $aTel[$iCount]['val'] = $telInfo['value'];
             if (array_key_exists('PREF', $telInfo['parameters'])) {
                 $aTel[$iCount]['pref'] = 1;
             }
             if (array_key_exists('TYPE', $telInfo['parameters'])) {
                 $testType = strtoupper(implode('_', $telInfo['parameters']['TYPE']));
                 if (stristr($testType, '_PREF')) {
                     $aTel[$iCount]['pref'] = 1;
                     $testType = str_replace('_PREF', '', $testType);
                 }
                 if (stristr($testType, '_VOICE')) {
                     $sTypeTemp = explode('_', $testType);
                     $aTel[$iCount]['type'] = $sTypeTemp[0];
                 } else {
                     $aTel[$iCount]['type'] = $testType;
                 }
                 if ($aTel[$iCount]['type'] === '') {
                     $aTel[$iCount]['type'] = 'OTHER';
                 }
                 //\OCP\Util::writeLog($this->appName,'TYPE OF TEL '. strtoupper($test), \OCP\Util::DEBUG);
             }
             $iCount++;
         }
     } else {
         $aTel[0]['val'] = '';
         $aTel[0]['type'] = 'WORK';
     }
     //FIXME
     $aMessenger = array();
     if (isset($editInfoCard['IMPP']) && count($editInfoCard['IMPP']) > 0) {
         $iMCount = 0;
         foreach ($editInfoCard['IMPP'] as $messengerInfo) {
             if (array_key_exists('PREF', $messengerInfo['parameters'])) {
                 $aMessenger[$iMCount]['pref'] = 1;
             }
             $aMessenger[$iMCount]['val'] = $messengerInfo['value'];
             $aMessenger[$iMCount]['type'] = $messengerInfo['parameters']['X-SERVICE-TYPE'];
             $iMCount++;
         }
     } else {
         $aMessenger[0]['val'] = '';
         $aMessenger[0]['type'] = 'facebook';
     }
     $aCloud = array();
     if (isset($editInfoCard['CLOUD']) && count($editInfoCard['CLOUD']) > 0) {
         $iClCount = 0;
         foreach ($editInfoCard['CLOUD'] as $cloudInfo) {
             if (array_key_exists('PREF', $cloudInfo['parameters'])) {
                 $aCloud[$iClCount]['pref'] = 1;
             }
             $aCloud[$iClCount]['val'] = $cloudInfo['value'];
             if (array_key_exists('TYPE', $cloudInfo['parameters'])) {
                 foreach ($cloudInfo['parameters']['TYPE'] as $typeInfo) {
                     $aCloud[$iClCount]['type'] = $typeInfo;
                 }
             }
             $iClCount++;
         }
     } else {
         $aCloud[0]['val'] = '';
         $aCloud[0]['type'] = 'WORK';
     }
     $aEmail = array();
     if (isset($editInfoCard['EMAIL']) && count($editInfoCard['EMAIL']) > 0) {
         $iECount = 0;
         foreach ($editInfoCard['EMAIL'] as $emailInfo) {
             $aEmail[$iECount]['val'] = $emailInfo['value'];
             if (array_key_exists('PREF', $emailInfo['parameters'])) {
                 $aEmail[$iECount]['pref'] = $emailInfo['parameters']['PREF'];
             }
             if (array_key_exists('TYPE', $emailInfo['parameters'])) {
                 $temp_sel = '';
                 foreach ($emailInfo['parameters']['TYPE'] as $typeInfo) {
                     if ($typeInfo !== '' && strtoupper($typeInfo) != 'PREF') {
                         if ($temp_sel == '') {
                             $temp_sel = strtoupper($typeInfo);
                         } else {
                             $temp_sel .= '#' . strtoupper($typeInfo);
                         }
                     }
                     if (stristr($temp_sel, 'INTERNET')) {
                         $sTypeTemp = explode('#', $temp_sel);
                         if (count($sTypeTemp) > 1) {
                             $aEmail[$iECount]['type'] = $sTypeTemp[1];
                         } else {
                             $aEmail[$iECount]['type'] = $temp_sel;
                         }
                     } else {
                         $aEmail[$iECount]['type'] = $temp_sel;
                     }
                     if ($aEmail[$iECount]['type'] === '') {
                         $aEmail[$iECount]['type'] = 'WORK';
                     }
                 }
             }
             $iECount++;
         }
     } else {
         $aEmail[0]['val'] = '';
         $aEmail[0]['type'] = 'WORK';
     }
     $bPhoto = 0;
     $imgSrc = '';
     $imgMimeType = '';
     $thumb = '<div id="noimage" class="ioc ioc-user"></div>';
     if (isset($vcard->PHOTO)) {
         $bPhoto = 1;
         $thumb = '';
         $image = new \OCP\Image();
         $image->loadFromData((string) $vcard->PHOTO);
         $imgSrc = $image->__toString();
         $imgMimeType = $image->mimeType();
         \OC::$server->getCache()->remove('editphoto');
         \OC::$server->getCache()->set('editphoto', $image->data(), 600);
     }
     $TELTYPE = ContactsApp::getTypesOfProperty('TEL');
     $EMAILTYPE = ContactsApp::getTypesOfProperty('EMAIL');
     $URLTYPE = ContactsApp::getTypesOfProperty('URL');
     $ADRTYPE = ContactsApp::getTypesOfProperty('ADR');
     $IMTYPE = ContactsApp::getIMOptions();
     $ADDFIELDS = ContactsApp::getAdditionalFields();
     $oldaddressbookid = VCard::getAddressbookid($pId);
     $addressBookPerm = Addressbook::find($oldaddressbookid);
     $maxUploadFilesize = \OCP\Util::maxUploadFilesize('/');
     $bCompany = isset($vcard->{'X-ABSHOWAS'}) ? true : false;
     //FIXME
     $params = ['id' => $pId, 'uploadMaxHumanFilesize' => \OCP\Util::humanFileSize($maxUploadFilesize), 'oldaddressbookid' => $oldaddressbookid, 'addressbooks' => $active_addressbooks, 'addressbooksPerm' => $addressBookPerm, 'tmpkey' => 'editphoto', 'isPhoto' => $bPhoto, 'bCompany' => $bCompany, 'thumbnail' => $thumb, 'imgsrc' => $imgSrc, 'imgMimeType' => $imgMimeType, 'anrede' => isset($aN['title']) ? $aN['title'] : '', 'fname' => isset($aN['fname']) ? $aN['fname'] : '', 'lname' => isset($aN['lname']) ? $aN['lname'] : '', 'firm' => isset($aOrg['firm']) ? $aOrg['firm'] : '', 'department' => isset($aOrg['department']) ? $aOrg['department'] : '', 'aTel' => isset($aTel) ? $aTel : '', 'aEmail' => isset($aEmail) ? $aEmail : '', 'aAddr' => isset($aAddr) ? $aAddr : '', 'aUrl' => isset($aUrl) ? $aUrl : '', 'aCloud' => isset($aCloud) ? $aCloud : '', 'aMessenger' => isset($aMessenger) ? $aMessenger : '', 'sBday' => isset($sBday) ? $sBday : '', 'nickname' => isset($sNickname) ? $sNickname : '', 'position' => isset($sPosition) ? $sPosition : '', 'sNotice' => isset($sNotice) ? $sNotice : '', 'TELTYPE' => $TELTYPE, 'EMAILTYPE' => $EMAILTYPE, 'URLTYPE' => $URLTYPE, 'ADRTYPE' => $ADRTYPE, 'IMTYPE' => $IMTYPE, 'ADDFIELDS' => $ADDFIELDS];
     $response = new TemplateResponse($this->appName, 'contact.edit', $params, '');
     return $response;
 }
 /**
  * @NoAdminRequired
  */
 public function getEditFormContact()
 {
     $pId = $this->params('id');
     $vcard = ContactsApp::getContactVCard($pId);
     $active_addressbooks = Addressbook::active($this->userId);
     $editInfoCard = VCard::structureContact($vcard);
     $aDefNArray = array('0' => 'lname', '1' => 'fname', '2' => 'anrede', '3' => 'title');
     $aN = '';
     if (isset($editInfoCard['N'][0]['value']) && count($editInfoCard['N'][0]['value']) > 0) {
         foreach ($editInfoCard['N'][0]['value'] as $key => $val) {
             if ($val != '') {
                 $aN[$aDefNArray[$key]] = $val;
             }
         }
     }
     $aOrgDef = array('0' => 'firm', '1' => 'department');
     $aOrg = array();
     if (isset($editInfoCard['ORG'][0]['value']) && count($editInfoCard['ORG'][0]['value']) > 0) {
         foreach ($editInfoCard['ORG'][0]['value'] as $key => $val) {
             if ($val != '') {
                 $aOrg[$aOrgDef[$key]] = $val;
             }
         }
     }
     $aUrl = array('val' => '', 'type' => '');
     if (isset($editInfoCard['URL'][0]['value']) && !empty($editInfoCard['URL'][0]['value'])) {
         $aUrl['val'] = $editInfoCard['URL'][0]['value'];
         if (array_key_exists('TYPE', $editInfoCard['URL'][0]['parameters'])) {
             foreach ($editInfoCard['URL'][0]['parameters']['TYPE'] as $typeInfo) {
                 if (strtoupper($typeInfo) != 'PREF' && $typeInfo !== '') {
                     $aUrl['type'] = $typeInfo;
                 }
             }
             if ($aUrl['type'] === '') {
                 $aUrl['type'] = 'INTERNET';
             }
         } else {
             $aUrl['type'] = 'INTERNET';
         }
     } else {
         $aUrl['val'] = '';
         $aUrl['type'] = 'INTERNET';
     }
     $sBday = '';
     if (isset($editInfoCard['BDAY'][0]['value']) && !empty($editInfoCard['BDAY'][0]['value'])) {
         $sBday = $editInfoCard['BDAY'][0]['value'];
         $date = new \DateTime($sBday);
         $sBday = $date->format('d.m.Y');
     }
     $sNotice = '';
     if (isset($editInfoCard['NOTE'][0]['value']) && !empty($editInfoCard['NOTE'][0]['value'])) {
         $sNotice = $editInfoCard['NOTE'][0]['value'];
     }
     $sNickname = '';
     if (isset($editInfoCard['NICKNAME'][0]['value']) && !empty($editInfoCard['NICKNAME'][0]['value'])) {
         $sNickname = $editInfoCard['NICKNAME'][0]['value'];
     }
     $sPosition = '';
     if (isset($editInfoCard['TITLE'][0]['value']) && !empty($editInfoCard['TITLE'][0]['value'])) {
         $sPosition = $editInfoCard['TITLE'][0]['value'];
     }
     $addressDefArray = array('0' => '', '1' => '', '2' => 'street', '3' => 'city', '4' => '', '5' => 'postalcode', '6' => 'country');
     $aAddr = array();
     if (isset($editInfoCard['ADR']) && count($editInfoCard['ADR']) > 0) {
         $iACount = 0;
         foreach ($editInfoCard['ADR'] as $addrInfo) {
             foreach ($addrInfo['value'] as $key => $val) {
                 $aAddr[$iACount]['val'][$addressDefArray[$key]] = $val;
             }
             if (array_key_exists('TYPE', $addrInfo['parameters'])) {
                 $temp_sel = '';
                 foreach ($addrInfo['parameters']['TYPE'] as $typeInfo) {
                     if ($typeInfo !== '' && strtoupper($typeInfo) != 'PREF') {
                         if ($temp_sel == '') {
                             $temp_sel = $typeInfo;
                         } else {
                             $temp_sel .= '#' . $typeInfo;
                         }
                     }
                 }
                 $aAddr[$iACount]['type'] = $temp_sel;
                 if ($aAddr[$iACount]['type'] === '') {
                     $aAddr[$iACount]['type'] = 'WORK';
                 }
             }
             $iACount++;
         }
         $aAddr[$iACount]['val'] = array('street' => '', 'postalcode' => '', 'city' => '', 'country' => '');
         $aAddr[$iACount]['type'] = 'HOME';
     } else {
         $aAddr[0]['val'] = array('street' => '', 'postalcode' => '', 'city' => '', 'country' => '');
         $aAddr[0]['type'] = 'WORK';
     }
     $aTel = array();
     if (isset($editInfoCard['TEL']) && count($editInfoCard['TEL']) > 0) {
         $iCount = 0;
         foreach ($editInfoCard['TEL'] as $telInfo) {
             $aTel[$iCount]['val'] = $telInfo['value'];
             if (array_key_exists('PREF', $telInfo['parameters'])) {
                 $aTel[$iCount]['pref'] = $telInfo['parameters']['PREF'];
             }
             if (array_key_exists('TYPE', $telInfo['parameters'])) {
                 $temp_sel = '';
                 foreach ($telInfo['parameters']['TYPE'] as $typeInfo) {
                     if ($typeInfo !== '' && strtoupper($typeInfo) != 'PREF') {
                         if ($temp_sel == '') {
                             $temp_sel = $typeInfo;
                         } else {
                             $temp_sel .= '#' . $typeInfo;
                         }
                     }
                 }
                 if (stristr($temp_sel, 'VOICE')) {
                     $sTypeTemp = explode('#', $temp_sel);
                     $aTel[$iCount]['type'] = $sTypeTemp[0];
                 }
                 if (stristr($temp_sel, 'FAX')) {
                     $sTypeTemp = explode('#', $temp_sel);
                     $aTel[$iCount]['type'] = $sTypeTemp[1] . '_' . $sTypeTemp[0];
                 }
                 if ($aTel[$iCount]['type'] === '') {
                     $aTel[$iCount]['type'] = 'WORK';
                 }
             }
             $iCount++;
         }
         $aTel[$iCount]['val'] = '';
         $aTel[$iCount]['type'] = 'WORK';
     } else {
         $aTel[0]['val'] = '';
         $aTel[0]['type'] = 'WORK';
     }
     $aEmail = array();
     if (isset($editInfoCard['EMAIL']) && count($editInfoCard['EMAIL']) > 0) {
         $iECount = 0;
         foreach ($editInfoCard['EMAIL'] as $emailInfo) {
             $aEmail[$iECount]['val'] = $emailInfo['value'];
             if (array_key_exists('PREF', $emailInfo['parameters'])) {
                 $aEmail[$iECount]['pref'] = $emailInfo['parameters']['PREF'];
             }
             if (array_key_exists('TYPE', $emailInfo['parameters'])) {
                 $temp_sel = '';
                 foreach ($emailInfo['parameters']['TYPE'] as $typeInfo) {
                     if ($typeInfo !== '' && strtoupper($typeInfo) != 'PREF') {
                         if ($temp_sel == '') {
                             $temp_sel = $typeInfo;
                         } else {
                             $temp_sel .= '#' . $typeInfo;
                         }
                     }
                     if (stristr($temp_sel, 'INTERNET')) {
                         $sTypeTemp = explode('#', $temp_sel);
                         if (count($sTypeTemp) > 1) {
                             $aEmail[$iECount]['type'] = $sTypeTemp[1];
                         } else {
                             $aEmail[$iECount]['type'] = $temp_sel;
                         }
                     } else {
                         $aEmail[$iECount]['type'] = $temp_sel;
                     }
                     if ($aEmail[$iECount]['type'] === '') {
                         $aEmail[$iECount]['type'] = 'WORK';
                     }
                 }
             }
             $iECount++;
         }
         $aEmail[$iECount]['val'] = '';
         $aEmail[$iECount]['type'] = 'WORK';
     } else {
         $aEmail[0]['val'] = '';
         $aEmail[0]['type'] = 'WORK';
     }
     $bPhoto = 0;
     $imgSrc = '';
     $imgMimeType = '';
     $thumb = '<div id="noimage" class="ioc ioc-user"></div>';
     if (isset($vcard->PHOTO)) {
         $bPhoto = 1;
         $thumb = '';
         $image = new \OCP\Image();
         $image->loadFromData((string) $vcard->PHOTO);
         $imgSrc = $image->__toString();
         $imgMimeType = $image->mimeType();
         \OC::$server->getCache()->set('edit-contacts-foto-' . $pId, $image->data(), 600);
     }
     $TELTYPE = ContactsApp::getTypesOfProperty('TEL');
     $EMAILTYPE = ContactsApp::getTypesOfProperty('EMAIL');
     $URLTYPE = ContactsApp::getTypesOfProperty('URL');
     $ADRTYPE = ContactsApp::getTypesOfProperty('ADR');
     $ADDFIELDS = ContactsApp::getAdditionalFields();
     $oldaddressbookid = VCard::getAddressbookid($pId);
     $addressBookPerm = Addressbook::find($oldaddressbookid);
     $maxUploadFilesize = \OCP\Util::maxUploadFilesize('/');
     $params = ['id' => $pId, 'uploadMaxHumanFilesize' => \OCP\Util::humanFileSize($maxUploadFilesize), 'oldaddressbookid' => $oldaddressbookid, 'addressbooks' => $active_addressbooks, 'addressbooksPerm' => $addressBookPerm, 'tmpkey' => 'edit-contacts-foto-' . $pId, 'isPhoto' => $bPhoto, 'thumbnail' => $thumb, 'imgsrc' => $imgSrc, 'imgMimeType' => $imgMimeType, 'anrede' => isset($aN['title']) ? $aN['title'] : '', 'fname' => isset($aN['fname']) ? $aN['fname'] : '', 'lname' => isset($aN['lname']) ? $aN['lname'] : '', 'firm' => isset($aOrg['firm']) ? $aOrg['firm'] : '', 'department' => isset($aOrg['department']) ? $aOrg['department'] : '', 'aTel' => isset($aTel) ? $aTel : '', 'aEmail' => isset($aEmail) ? $aEmail : '', 'aAddr' => isset($aAddr) ? $aAddr : '', 'aUrl' => isset($aUrl) ? $aUrl : '', 'sBday' => isset($sBday) ? $sBday : '', 'nickname' => isset($sNickname) ? $sNickname : '', 'position' => isset($sPosition) ? $sPosition : '', 'sNotice' => isset($sNotice) ? $sNotice : '', 'TELTYPE' => $TELTYPE, 'EMAILTYPE' => $EMAILTYPE, 'URLTYPE' => $URLTYPE, 'ADRTYPE' => $ADRTYPE, 'ADDFIELDS' => $ADDFIELDS];
     $response = new TemplateResponse($this->appName, 'contact.edit', $params, '');
     return $response;
 }