/** * Extracts all matching contacts with email address and name * * @param string $term * @return array */ public function addBirthdays($userid, $calId) { if (!$this->contactsManager->isEnabled()) { return array(); } $addrBooks = $this->contactsManager->getAddressBooks(); $aContacts = array(); foreach ($addrBooks as $key => $addrBook) { if (is_integer($key)) { $bContacts = \OCA\ContactsPlus\VCard::all($key); $aContacts = array_merge($aContacts, $bContacts); } } $sDateFormat = $this->configInfo->getUserValue($this->userId, $this->appName, 'dateformat', 'd-m-Y'); if ($sDateFormat === 'd-m-Y') { $sDateFormat = 'd.m.Y'; } $aResult = array(); foreach ($aContacts as $contact) { $vcard = \Sabre\VObject\Reader::read($contact['carddata']); if (isset($vcard->BDAY)) { $Birthday = new \DateTime((string) $vcard->BDAY); $checkForm = $Birthday->format('d-m-Y'); $temp = explode('-', $checkForm); $getAge = $this->getAge($temp[2], $temp[1], $temp[0]); $title = $contact['fullname']; $birthdayOutput = $Birthday->format($sDateFormat); $aktYear = $Birthday->format('d-m'); $aktYear = $aktYear . date('-Y'); $start = new \DateTime($aktYear); $end = new \DateTime($aktYear . ' +1 day'); $vcalendar = new VObject('VCALENDAR'); $vcalendar->add('PRODID', 'ownCloud Calendar'); $vcalendar->add('VERSION', '2.0'); $vevent = new VObject('VEVENT'); $vcalendar->add($vevent); $vevent->setDateTime('CREATED', 'now'); $vevent->add('DTSTART'); $vevent->DTSTART->setDateTime($start); $vevent->add('DTEND'); $vevent->DTEND->setDateTime($end); $vevent->DTSTART['VALUE'] = 'date'; $vevent->DTEND['VALUE'] = 'date'; $vevent->{'RRULE'} = 'FREQ=YEARLY;INTERVAL=1'; $vevent->{'TRANSP'} = 'TRANSPARENT'; $vevent->{'SUMMARY'} = (string) $title . ' (' . $getAge . ')'; $description = (string) $this->l10n->t("Happy Birthday! Born on: ") . $birthdayOutput; $vevent->setString('DESCRIPTION', $description); $vevent->{'UID'} = substr(md5(rand() . time()), 0, 10); $insertid = Object::add($calId, $vcalendar->serialize()); if ($this->isDuplicate($insertid)) { Object::delete($insertid); } } } }
public static function renderOutput($addrbookId, $grpId = 'all') { if ($grpId === 'all') { $contacts_alphabet = VCard::all($addrbookId, null, null, array(), true); } elseif ($grpId === 'fav') { $contacts_alphabet = VCard::allByFavourite(); } else { $contacts_alphabet = VCard::getCardsByGroups($addrbookId, $grpId, null, null, true); } $addressBookPerm = Addressbook::find($addrbookId); //Favourites $favorites = \OC::$server->getTagManager()->load(self::$appname)->getFavorites(); $aFavourites = array(); if (is_array($favorites)) { foreach ($favorites as $fav) { $aFavourites[$fav] = 1; } } $contacts = array(); $counterAlle = 0; $aLetter = []; if ($contacts_alphabet) { $oldLetter = ''; foreach ($contacts_alphabet as $contact) { try { $vcard = VObject\Reader::read($contact['carddata']); $details = VCard::structureContact($vcard); $imgBuild = ''; if ($vcard->PHOTO) { $image = new \OCP\Image(); $image->loadFromData((string) $vcard->PHOTO); $imgSrc = $image->__toString(); $imgBuild = 'data:' . $image->mimeType() . ';base64,' . $imgSrc; } $sLetter = strtoupper(mb_substr($contact['sortFullname'], 0, 1, "UTF-8")); if ($sLetter !== $oldLetter) { $aLetter[] = $sLetter; } //\OCP\Util::writeLog(self::$appname,'LETTER: '. $sLetter.':'.$contact['fullname'], \OCP\Util::DEBUG); $contacts[$sLetter][] = array('id' => $contact['id'], 'aid' => $contact['addressbookid'], 'letter' => $sLetter, 'photo' => $imgBuild, 'component' => $contact['component'], 'fullname' => $contact['fullname'], 'surename' => $contact['surename'], 'lastname' => $contact['lastname'], 'organization' => $contact['organization'], 'bcompany' => $contact['bcompany'], 'data' => $details); $oldLetter = $sLetter; $counterAlle++; } catch (Exception $e) { continue; } } $oldLetter = ''; $buildingOutput = '<ul>'; foreach ($aLetter as $letterInfo) { $bFound = false; $ContactsOutput = ''; if (isset($contacts[$letterInfo])) { foreach ($contacts[$letterInfo] as $contactInfo) { $bFound = true; $CONTACTDATA = $contactInfo['data']; $prepareOutput = self::renderSingleCard($CONTACTDATA, $contactInfo, $addressBookPerm, $aFavourites); $ContactsOutput .= '<li class="contactsrow visible">' . $prepareOutput . '</li>'; } } if ($bFound === true) { $buildingOutput .= '<li class="letter" data-scroll="' . $letterInfo . '"><span>' . $letterInfo . '</span></li>' . $ContactsOutput; } else { $buildingOutput .= '<li class="letter hidden" data-scroll="' . $letterInfo . '"><span>' . $letterInfo . '</span></li>'; } } $buildingOutput .= '<li><span class="noitem hidden">' . (string) self::$l10n->t('No Cards found!') . '</span></li>'; $buildingOutput .= '</ul>'; } else { $buildingOutput = '<ul>'; foreach ($aLetter as $letterInfo) { $buildingOutput .= '<li class="letter hidden" data-scroll="' . $letterInfo . '"><span>' . $letterInfo . '</span></li>'; } $buildingOutput .= '<li><span class="noitem">' . (string) self::$l10n->t('Add a new contact or import existing contacts from a file (VCF) per Drag & Drop.') . ' <i id="importAddrStart" title="' . (string) self::$l10n->t('Import addressbook per Drag & Drop') . '" class="toolTip ioc ioc-upload"></i></span></li>'; $buildingOutput .= '</ul>'; } return $buildingOutput; }
public static function renderOutput($addrbookId, $grpId = 'all') { if ($grpId === 'all') { $contacts_alphabet = VCard::all($addrbookId, null, null, array(), true); } elseif ($grpId === 'fav') { $contacts_alphabet = VCard::allByFavourite(); } else { $contacts_alphabet = VCard::getCardsByGroups($addrbookId, $grpId, null, null, true); } $addressBookPerm = Addressbook::find($addrbookId); //Favourites $favorites = \OC::$server->getTagManager()->load(self::$appname)->getFavorites(); $aFavourites = array(); if (is_array($favorites)) { foreach ($favorites as $fav) { $aFavourites[$fav] = 1; } } $contacts = array(); $counterAlle = 0; $aLetter = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'); if ($contacts_alphabet) { foreach ($contacts_alphabet as $contact) { try { $vcard = VObject\Reader::read($contact['carddata']); $details = VCard::structureContact($vcard); $imgBuild = ''; if ($vcard->PHOTO) { $image = new \OCP\Image(); $image->loadFromData((string) $vcard->PHOTO); $imgSrc = $image->__toString(); $imgBuild = 'data:' . $image->mimeType() . ';base64,' . $imgSrc; } $sLetter = strtoupper(substr($contact['fullname'], 0, 1)); //\OCP\Util::writeLog(self::$appname,'LETTER: '. $sLetter.':'.$contact['fullname'], \OCP\Util::DEBUG); $contacts[$sLetter][] = array('id' => $contact['id'], 'aid' => $contact['addressbookid'], 'letter' => $sLetter, 'photo' => $imgBuild, 'component' => $contact['component'], 'fullname' => $contact['fullname'], 'surename' => $contact['surename'], 'lastname' => $contact['lastname'], 'data' => $details); $counterAlle++; } catch (Exception $e) { continue; } } $oldLetter = ''; $buildingOutput = '<ul>'; foreach ($aLetter as $letterInfo) { $bFound = false; $ContactsOutput = ''; if (isset($contacts[$letterInfo])) { foreach ($contacts[$letterInfo] as $contactInfo) { $bFound = true; $CONTACTDATA = $contactInfo['data']; $prepareOutput = self::renderSingleCard($CONTACTDATA, $contactInfo, $addressBookPerm, $aFavourites); $ContactsOutput .= '<li class="contactsrow">' . $prepareOutput . '</li>'; } } if ($bFound === true) { $buildingOutput .= '<li class="letter" data-scroll="' . $letterInfo . '"><span>' . $letterInfo . '</span></li>' . $ContactsOutput; } else { $buildingOutput .= '<li class="letter hidden" data-scroll="' . $letterInfo . '"><span>' . $letterInfo . '</span></li>'; } } $buildingOutput .= '</ul>'; } else { $buildingOutput = '<ul>'; foreach ($aLetter as $letterInfo) { $buildingOutput .= '<li class="letter hidden" data-scroll="' . $letterInfo . '"><span>' . $letterInfo . '</span></li>'; } $buildingOutput .= '<li><span class="noitem">' . (string) self::$l10n->t('No Cards found!') . '</span></li>'; $buildingOutput .= '</ul>'; } return $buildingOutput; }
/** * @NoAdminRequired * @NoCSRFRequired */ public function exportBirthdays() { $bookid = $this->params('aid'); $bookid = isset($bookid) ? $bookid : null; if (!is_null($bookid)) { $addressbook = Addressbook::find($bookid); $aDefNArray = array('0' => 'fname', '1' => 'lname', '3' => 'title', '4' => ''); foreach (VCard::all($bookid) as $contact) { try { $vcard = VObject\Reader::read($contact['carddata']); } catch (Exception $e) { continue; } $birthday = $vcard->BDAY; if ((string) $birthday) { $details = VCard::structureContact($vcard); $BirthdayTemp = new \DateTime($birthday); $checkForm = $BirthdayTemp->format('d-m-Y'); $temp = explode('-', $checkForm); $getAge = $this->getAgeCalc($temp[2], $temp[1], $temp[0]); //$getAge=$BirthdayTemp->format('d-m-Y'); $title = isset($vcard->FN) ? strtr($vcard->FN->getValue(), array('\\,' => ',', '\\;' => ';')) : ''; $sNameOutput = ''; if (isset($details['N'][0]['value']) && count($details['N'][0]['value']) > 0) { foreach ($details['N'][0]['value'] as $key => $val) { if ($val != '') { $aNameOutput[$aDefNArray[$key]] = $val; } } //$sNameOutput=isset($aNameOutput['title'])?$aNameOutput['title'].' ':''; $sNameOutput .= isset($aNameOutput['lname']) ? $aNameOutput['lname'] . ' ' : ''; $sNameOutput .= isset($aNameOutput['fname']) ? $aNameOutput['fname'] . ' ' : ''; unset($aNameOutput); } if ($sNameOutput == '') { $sNameOutput = $title; } $sTitle1 = (string) $this->l10n->t('%1$s (%2$s)', array($sNameOutput, $getAge)); $aktYear = $BirthdayTemp->format('d-m'); $aktYear = $aktYear . date('-Y'); $start = new \DateTime($aktYear); $end = new \DateTime($aktYear . ' +1 day'); $vcalendar = new VObject\Component\VCalendar(); $vevent = $vcalendar->createComponent('VEVENT'); $vevent->add('DTSTART'); $vevent->DTSTART->setDateTime($start); $vevent->DTSTART['VALUE'] = 'date'; $vevent->add('DTEND'); $vevent->DTEND->setDateTime($end); $vevent->DTEND['VALUE'] = 'date'; $vevent->{'SUMMARY'} = (string) $sTitle1; $vevent->{'UID'} = substr(md5(rand() . time()), 0, 10); $params['events'][] = $vevent->serialize(); } } if (is_array($params['events'])) { $return = "BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:ownCloud Calendar " . \OCP\App::getAppVersion('calendar') . "\nX-WR-CALNAME: export-bday-" . $bookid . "\n"; foreach ($params['events'] as $event) { $return .= $event; } $return .= "END:VCALENDAR"; $name = str_replace(' ', '_', $addressbook['displayname']) . '_birthdays' . '.ics'; $response = new DataDownloadResponse($return, $name, 'text/calendar'); return $response; } } }
/** * Returns all cards for a specific addressbook id. * * @param mixed $addressbookid * @return array */ public function getCards($addressbookid) { $data = VCard::all($addressbookid); $cards = array(); foreach ($data as $i) { //OCP\Util::writeLog('contacts', __METHOD__.', uri: ' . $i['uri'], OCP\Util::DEBUG); $cards[] = array('id' => $i['id'], 'size' => strlen($i['carddata']), 'etag' => '"' . md5($i['carddata']) . '"', 'uri' => $i['uri'], 'lastmodified' => $i['lastmodified']); } return $cards; }
public static function getBirthdayEvents($params) { if (\OCP\Config::getUserValue(\OCP\USER::getUser(), 'calendarplus', 'calendar_birthday_' . \OCP\USER::getUser())) { $name = $params['calendar_id']; if (strpos($name, 'birthday_') != 0) { return; } $info = explode('_', $name); $aid = $info[1]; $aDefNArray = array('0' => 'fname', '1' => 'lname', '3' => 'title', '4' => ''); foreach (Addressbook::all($aid) as $addressbook) { foreach (VCard::all($addressbook['id']) as $contact) { try { $vcard = VObject\Reader::read($contact['carddata']); } catch (Exception $e) { continue; } $birthday = $vcard->BDAY; if ((string) $birthday) { $details = VCard::structureContact($vcard); $BirthdayTemp = new \DateTime($birthday); $checkForm = $BirthdayTemp->format('d-m-Y'); $temp = explode('-', $checkForm); $getAge = self::getAge($temp[2], $temp[1], $temp[0]); //$getAge=$BirthdayTemp->format('d-m-Y'); $title = isset($vcard->FN) ? strtr($vcard->FN->getValue(), array('\\,' => ',', '\\;' => ';')) : ''; $sNameOutput = ''; if (isset($details['N'][0]['value']) && count($details['N'][0]['value']) > 0) { foreach ($details['N'][0]['value'] as $key => $val) { if ($val != '') { $aNameOutput[$aDefNArray[$key]] = $val; } } //$sNameOutput=isset($aNameOutput['title'])?$aNameOutput['title'].' ':''; $sNameOutput .= isset($aNameOutput['lname']) ? $aNameOutput['lname'] . ' ' : ''; $sNameOutput .= isset($aNameOutput['fname']) ? $aNameOutput['fname'] . ' ' : ''; unset($aNameOutput); } if ($sNameOutput == '') { $sNameOutput = $title; } $sTitle1 = (string) App::$l10n->t('%1$s (%2$s)', array($sNameOutput, $getAge)); $aktYear = $BirthdayTemp->format('d-m'); $aktYear = $aktYear . date('-Y'); $start = new \DateTime($aktYear); $end = new \DateTime($aktYear . ' +1 day'); $vcalendar = new VObject\Component\VCalendar(); $vevent = $vcalendar->createComponent('VEVENT'); $vevent->add('DTSTART'); $vevent->DTSTART->setDateTime($start); $vevent->DTSTART['VALUE'] = 'date'; $vevent->add('DTEND'); $vevent->DTEND->setDateTime($end); $vevent->DTEND['VALUE'] = 'date'; $vevent->{'SUMMARY'} = (string) $sTitle1; $vevent->{'UID'} = substr(md5(rand() . time()), 0, 10); // DESCRIPTION? $aktYear1 = $BirthdayTemp->format('-m-d'); $aktYear1 = date('Y') . $aktYear1; $params['events'][] = array('id' => 0, 'vevent' => $vevent, 'repeating' => true, 'calendarid' => $params['calendar_id'], 'privat' => false, 'bday' => true, 'shared' => false, 'isalarm' => false, 'summary' => $sTitle1, 'start' => $aktYear1, 'allDay' => true, 'startlist' => $aktYear1, 'editable' => false, 'className' => 'birthdayevent', 'startEditable ' => false, 'durationEditable ' => false); } } } } return true; }