diff options
author | redmatrix <git@macgirvin.com> | 2016-05-10 17:26:44 -0700 |
---|---|---|
committer | redmatrix <git@macgirvin.com> | 2016-05-10 17:26:44 -0700 |
commit | 0b02a6d123b2014705998c94ddf3d460948d3eac (patch) | |
tree | 78ff2cab9944a4f5ab3f80ec93cbe1120de90bb2 /vendor/sabre/vobject/lib/BirthdayCalendarGenerator.php | |
parent | 40b5b6e9d2da7ab65c8b4d38cdceac83a4d78deb (diff) | |
download | volse-hubzilla-0b02a6d123b2014705998c94ddf3d460948d3eac.tar.gz volse-hubzilla-0b02a6d123b2014705998c94ddf3d460948d3eac.tar.bz2 volse-hubzilla-0b02a6d123b2014705998c94ddf3d460948d3eac.zip |
initial sabre upgrade (needs lots of work - to wit: authentication, redo the browser interface, and rework event export/import)
Diffstat (limited to 'vendor/sabre/vobject/lib/BirthdayCalendarGenerator.php')
-rw-r--r-- | vendor/sabre/vobject/lib/BirthdayCalendarGenerator.php | 191 |
1 files changed, 191 insertions, 0 deletions
diff --git a/vendor/sabre/vobject/lib/BirthdayCalendarGenerator.php b/vendor/sabre/vobject/lib/BirthdayCalendarGenerator.php new file mode 100644 index 000000000..afa41ab1c --- /dev/null +++ b/vendor/sabre/vobject/lib/BirthdayCalendarGenerator.php @@ -0,0 +1,191 @@ +<?php + +namespace Sabre\VObject; + +use Sabre\VObject\Component\VCalendar; + +/** + * This class generates birthday calendars. + * + * @copyright Copyright (C) fruux GmbH (https://fruux.com/) + * @author Dominik Tobschall (http://tobschall.de/) + * @license http://sabre.io/license/ Modified BSD License + */ +class BirthdayCalendarGenerator { + + /** + * Input objects. + * + * @var array + */ + protected $objects = []; + + /** + * Default year. + * Used for dates without a year. + */ + const DEFAULT_YEAR = 2000; + + /** + * Output format for the SUMMARY. + * + * @var string + */ + protected $format = '%1$s\'s Birthday'; + + /** + * Creates the generator. + * + * Check the setTimeRange and setObjects methods for details about the + * arguments. + * + * @param mixed $objects + */ + function __construct($objects = null) { + + if ($objects) { + $this->setObjects($objects); + } + + } + + /** + * Sets the input objects. + * + * You must either supply a vCard as a string or as a Component/VCard object. + * It's also possible to supply an array of strings or objects. + * + * @param mixed $objects + * + * @return void + */ + function setObjects($objects) { + + if (!is_array($objects)) { + $objects = [$objects]; + } + + $this->objects = []; + foreach ($objects as $object) { + + if (is_string($object)) { + + $vObj = Reader::read($object); + if (!$vObj instanceof Component\VCard) { + throw new \InvalidArgumentException('String could not be parsed as \\Sabre\\VObject\\Component\\VCard by setObjects'); + } + + $this->objects[] = $vObj; + + } elseif ($object instanceof Component\VCard) { + + $this->objects[] = $object; + + } else { + + throw new \InvalidArgumentException('You can only pass strings or \\Sabre\\VObject\\Component\\VCard arguments to setObjects'); + + } + + } + + } + + /** + * Sets the output format for the SUMMARY + * + * @param string $format + * + * @return void + */ + function setFormat($format) { + + $this->format = $format; + + } + + /** + * Parses the input data and returns a VCALENDAR. + * + * @return Component/VCalendar + */ + function getResult() { + + $calendar = new VCalendar(); + + foreach ($this->objects as $object) { + + // Skip if there is no BDAY property. + if (!$object->select('BDAY')) { + continue; + } + + // We've seen clients (ez-vcard) putting "BDAY:" properties + // without a value into vCards. If we come across those, we'll + // skip them. + if (empty($object->BDAY->getValue())) { + continue; + } + + // We're always converting to vCard 4.0 so we can rely on the + // VCardConverter handling the X-APPLE-OMIT-YEAR property for us. + $object = $object->convert(Document::VCARD40); + + // Skip if the card has no FN property. + if (!isset($object->FN)) { + continue; + } + + // Skip if the BDAY property is not of the right type. + if (!$object->BDAY instanceof Property\VCard\DateAndOrTime) { + continue; + } + + // Skip if we can't parse the BDAY value. + try { + $dateParts = DateTimeParser::parseVCardDateTime($object->BDAY->getValue()); + } catch (InvalidDataException $e) { + continue; + } + + // Set a year if it's not set. + $unknownYear = false; + + if (!$dateParts['year']) { + $object->BDAY = self::DEFAULT_YEAR . '-' . $dateParts['month'] . '-' . $dateParts['date']; + + $unknownYear = true; + } + + // Create event. + $event = $calendar->add('VEVENT', [ + 'SUMMARY' => sprintf($this->format, $object->FN->getValue()), + 'DTSTART' => new \DateTime($object->BDAY->getValue()), + 'RRULE' => 'FREQ=YEARLY', + 'TRANSP' => 'TRANSPARENT', + ]); + + // add VALUE=date + $event->DTSTART['VALUE'] = 'DATE'; + + // Add X-SABRE-BDAY property. + if ($unknownYear) { + $event->add('X-SABRE-BDAY', 'BDAY', [ + 'X-SABRE-VCARD-UID' => $object->UID->getValue(), + 'X-SABRE-VCARD-FN' => $object->FN->getValue(), + 'X-SABRE-OMIT-YEAR' => self::DEFAULT_YEAR, + ]); + } else { + $event->add('X-SABRE-BDAY', 'BDAY', [ + 'X-SABRE-VCARD-UID' => $object->UID->getValue(), + 'X-SABRE-VCARD-FN' => $object->FN->getValue(), + ]); + } + + } + + return $calendar; + + } + +} |