/**
 * @param Sabre\VObject\Component\VAlarm $alarm
 * @param Sabre\VObject\Component\VEvent|Sabre\VObject\Component\VTodo $parent
 * @return DateTime|null
 * @throws Sabre_DAV_Exception
 */
function renderCalDavEntry_calcalarm(&$alarm, &$parent)
{
    $trigger = $alarm->__get("TRIGGER");
    if (!isset($trigger['VALUE']) || strtoupper($trigger['VALUE']) === 'DURATION') {
        $triggerDuration = Sabre\VObject\DateTimeParser::parseDuration($trigger->value);
        $related = isset($trigger['RELATED']) && strtoupper($trigger['RELATED']) == 'END' ? 'END' : 'START';
        if ($related === 'START') {
            /** @var Sabre\VObject\Property\DateTime $dtstart  */
            $dtstart = $parent->__get("DTSTART");
            $effectiveTrigger = $dtstart->getDateTime();
            $effectiveTrigger->add($triggerDuration);
        } else {
            if ($parent->name === 'VTODO') {
                $endProp = 'DUE';
            } else {
                $endProp = 'DTEND';
            }
            /** @var Sabre\VObject\Property\DateTime $dtstart  */
            $dtstart = $parent->__get("DTSTART");
            if (isset($parent->{$endProp})) {
                $effectiveTrigger = clone $parent->{$endProp}->getDateTime();
                $effectiveTrigger->add($triggerDuration);
            } elseif ($parent->__get("DURATION") != "") {
                $effectiveTrigger = clone $dtstart->getDateTime();
                $duration = Sabre\VObject\DateTimeParser::parseDuration($parent->__get("DURATION"));
                $effectiveTrigger->add($duration);
                $effectiveTrigger->add($triggerDuration);
            } else {
                $effectiveTrigger = clone $dtstart->getDateTime();
                $effectiveTrigger->add($triggerDuration);
            }
        }
    } else {
        // ??? @TODO
        $effectiveTrigger = $trigger->getDateTime();
    }
    return $effectiveTrigger;
}
/**
 * @param Sabre\VObject\Component\VEvent $component
 * @param wdcal_local $localization
 * @param string $summary
 * @param int $dtstart
 */
function wdcal_set_component_alerts(&$component, &$localization, $summary, $dtstart)
{
    $a = get_app();
    $prev_alarms = $component->select("VALARM");
    $component->__unset("VALARM");
    foreach ($prev_alarms as $al) {
        /** @var Sabre\VObject\Component\VAlarm $al */
        // @TODO Parse notifications that have been there before; e.g. from Lightning
    }
    foreach (array_keys($_REQUEST["noti_type"]) as $key) {
        if (is_numeric($key) || $key == "new" && $_REQUEST["new_alarm"] == 1) {
            $alarm = new Sabre\VObject\Component\VAlarm("VALARM");
            switch ($_REQUEST["noti_type"][$key]) {
                case "email":
                    $mailtext = str_replace(array("#date#", "#name"), array($localization->date_timestamp2local($dtstart), $summary), t("The event #name# will start at #date"));
                    $alarm->add(new Sabre\VObject\Property("ACTION", "EMAIL"));
                    $alarm->add(new Sabre\VObject\Property("SUMMARY", $summary));
                    $alarm->add(new Sabre\VObject\Property("DESCRIPTION", $mailtext));
                    $alarm->add(new Sabre\VObject\Property("ATTENDEE", "MAILTO:" . $a->user["email"]));
                    break;
                case "display":
                    $alarm->add(new Sabre\VObject\Property("ACTION", "DISPLAY"));
                    $text = str_replace("#name#", $summary, t("#name# is about to begin."));
                    $alarm->add(new Sabre\VObject\Property("DESCRIPTION", $text));
                    break;
                default:
                    continue;
            }
            $trigger_name = "TRIGGER";
            $trigger_val = "";
            // @TODO Bugfix : und ; sind evtl. vertauscht vgl. http://www.kanzaki.com/docs/ical/trigger.html
            if ($_REQUEST["noti_ref"][$key] == "end") {
                $trigger_name .= ";RELATED=END";
            }
            $trigger_val .= "-P";
            if (in_array($_REQUEST["noti_unit"][$key], array("H", "M", "S"))) {
                $trigger_val .= "T";
            }
            $trigger_val .= IntVal($_REQUEST["noti_value"][$key]) . $_REQUEST["noti_unit"][$key];
            $alarm->add(new Sabre\VObject\Property($trigger_name, $trigger_val));
            $component->add($alarm);
        }
    }
}