/** * Given a dav_id and an original vCalendar, pull out each of the VALARMs * and write the values into the calendar_alarm table. * * @param int $dav_id The dav_id of the caldav_data we're processing * @param vComponent The VEVENT or VTODO containing the VALARM * @return null */ function write_alarms($dav_id, vComponent $ical) { $qry = new AwlQuery('DELETE FROM calendar_alarm WHERE dav_id = ' . $dav_id); $qry->Exec('PUT', __LINE__, __FILE__); $alarms = $ical->GetComponents('VALARM'); if (count($alarms) < 1) { return; } $qry->SetSql('INSERT INTO calendar_alarm ( dav_id, action, trigger, summary, description, component, next_trigger ) VALUES( ' . $dav_id . ', :action, :trigger, :summary, :description, :component, :related::timestamp with time zone + :related_trigger::interval )'); $qry->Prepare(); foreach ($alarms as $v) { $trigger = array_merge($v->GetProperties('TRIGGER')); if ($trigger == null) { continue; } // Bogus data. $trigger = $trigger[0]; $related = null; $related_trigger = '0M'; $trigger_type = $trigger->GetParameterValue('VALUE'); if (!isset($trigger_type) || $trigger_type == 'DURATION') { switch ($trigger->GetParameterValue('RELATED')) { case 'DTEND': $related = $ical->GetProperty('DTEND'); break; case 'DUE': $related = $ical->GetProperty('DUE'); break; default: $related = $ical->GetProperty('DTSTART'); } $duration = $trigger->Value(); if (!preg_match('{^-?P(:?\\d+W)?(:?\\d+D)?(:?T(:?\\d+H)?(:?\\d+M)?(:?\\d+S)?)?$}', $duration)) { continue; } $minus = substr($duration, 0, 1) == '-'; $related_trigger = trim(preg_replace('#[PT-]#', ' ', $duration)); if ($minus) { $related_trigger = preg_replace('{(\\d+[WDHMS])}', '-$1 ', $related_trigger); } else { $related_trigger = preg_replace('{(\\d+[WDHMS])}', '$1 ', $related_trigger); } } else { if ($trigger_type == 'DATE-TIME') { $related = $trigger; } else { if (false === strtotime($trigger->Value())) { continue; } // Invalid date. $related = $trigger; } } $related_date = new RepeatRuleDateTime($related); $qry->Bind(':action', $v->GetPValue('ACTION')); $qry->Bind(':trigger', $trigger->Render()); $qry->Bind(':summary', $v->GetPValue('SUMMARY')); $qry->Bind(':description', $v->GetPValue('DESCRIPTION')); $qry->Bind(':component', $v->Render()); $qry->Bind(':related', $related_date->UTC()); $qry->Bind(':related_trigger', $related_trigger); $qry->Exec('PUT', __LINE__, __FILE__); } }
/** * Parse out the attendee property and write a row to the * calendar_attendee table for each one. * @param int $dav_id The dav_id of the caldav_data we're processing * @param iCalComponent The VEVENT or VTODO containing the ATTENDEEs * @return null */ function write_attendees($dav_id, $ical) { $qry = new AwlQuery('DELETE FROM calendar_attendee WHERE dav_id = ' . $dav_id); $qry->Exec('PUT', __LINE__, __FILE__); $attendees = $ical->GetProperties('ATTENDEE'); if (count($attendees) < 1) { return; } $qry->SetSql('INSERT INTO calendar_attendee ( dav_id, status, partstat, cn, attendee, role, rsvp, property ) VALUES( ' . $dav_id . ', :status, :partstat, :cn, :attendee, :role, :rsvp, :property )'); $qry->Prepare(); $processed = array(); foreach ($attendees as $v) { $attendee = $v->Value(); if (isset($processed[$attendee])) { dbg_error_log('LOG', 'Duplicate attendee "%s" in resource "%d"', $attendee, $dav_id); dbg_error_log('LOG', 'Original: "%s"', $processed[$attendee]); dbg_error_log('LOG', 'Duplicate: "%s"', $v->Render()); continue; /** @TODO: work out why we get duplicate ATTENDEE on one VEVENT */ } $qry->Bind(':attendee', $attendee); $qry->Bind(':status', $v->GetParameterValue('STATUS')); $qry->Bind(':partstat', $v->GetParameterValue('PARTSTAT')); $qry->Bind(':cn', $v->GetParameterValue('CN')); $qry->Bind(':role', $v->GetParameterValue('ROLE')); $qry->Bind(':rsvp', $v->GetParameterValue('RSVP')); $qry->Bind(':property', $v->Render()); $qry->Exec('PUT', __LINE__, __FILE__); $processed[$attendee] = $v->Render(); } }