doucheng2210 2013-07-09 11:21
浏览 52
已采纳

PHP Google日历Api为重复发生的事件添加提醒

I started my project 1 year ago and never had to post in forums, 1 week ago i started developing with google calendar api and this is my 3rd post :s

I already create sucessfully single and recurring events, reminders for single events but now i need to know how to create reminders for recurring events with the Zend Gdata api.

This is the code i use to create reminders for single events:

function setReminder($client, $eventId, $minutes=15, $methods)
{
    $gc = new Zend_Gdata_Calendar($client);
    $reminder_types = array();
    if ($event = getEvent($client, $eventId)) {
        $times = $event->when;
        var_dump($event);
        foreach ($times as $when) 
        {
            if(in_array('alert',$methods))
            {
                var_dump('alert');
                $reminder_alert = $gc->newReminder();
                $reminder_alert->setMinutes($minutes);
                $reminder_alert->setMethod("alert");
                array_push($reminder_types,$reminder_alert);
            }
            if(in_array('email',$methods))
            {
                var_dump('email');
                $reminder_email = $gc->newReminder();
                $reminder_email->setMinutes($minutes);
                $reminder_email->setMethod("email");
                array_push($reminder_types,$reminder_email);
            }

            $when->reminders = $reminder_types;
        }
        $eventNew = $event->save();
        //var_dump($eventNew);
        return $eventNew;
    } else {
        return null;
    }
}

The problem is, for recurring events $event->when is empty.

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • dongya5893 2013-07-10 10:52
    关注

    Ok after a lot of search and trial and error i found the solution to create recurring events with reminders.

    Austin from Google wrote:

    At the moment, the Zend GData client library does not provide the ability to directly add reminder when it is dealing with recurring event. This exceptional case is caused by the fact that there is no gd:when element within a recurring event entry, and typically a gd:reminder would exist inside a gd:when element. The Zend client library handle the general case and thus does not provide a way to add reminder when there is no gd:when specified.

    But there is an hack :) here it is how i did it:

    function createRecurringEvent ($client, $calendar, $title, $desc, $where, $startDate, $startTime, $endDate, $endTime, $repeat, $endRepeatDate, $endRepeatTime, $tzOffset, $minutes=15, $methods)
    {
        $frequence = "";
        $recurData = null;
        $gc = new Zend_Gdata_Calendar($client);
        $newEntry = $gc->newEventEntry();
        $newEntry->title = $gc->newTitle(trim($title));
        $newEntry->where = array($gc->newWhere($where));
    
        $newEntry->content = $gc->newContent($desc);
        $newEntry->content->type = 'text';
    
    
        switch ($repeat) {
            case "d":
                $frequence = "DAILY";
                break;
            case "w":
                $frequence = "WEEKLY";
                break;
            case "m":
                $frequence = "MONTHLY";
                break;
            case "y":
                $frequence = "YEARLY";
                break;
        }
    
        $startDateTime = getIcalDate($startDate . " " . $startTime);
        $endDateTime = getIcalDate($endDate . " " . $endTime);
        $endRepeatDateTime = getIcalDate($endRepeatDate . " " . $endRepeatTime);
    
        if ($recurData == null) 
        {
            $recurData =
                "DTSTART:{$startDateTime}
    " .
                "DTEND:{$endDateTime}
    " .
                "RRULE:FREQ={$frequence};UNTIL={$endRepeatDateTime}
    ";
    
        }
    
        $newEntry->recurrence = $gc->newRecurrence($recurData);
    
        $event_xml = simplexml_load_string($newEntry->saveXML());
    
        if(in_array('alert',$methods))
            $event_xml->addChild('gd:reminder  xmlns:gd="http://schemas.google.com/g/2005" minutes="15" method="alert"');
        if(in_array('email',$methods))
            $event_xml->addChild('gd:reminder  xmlns:gd="http://schemas.google.com/g/2005" minutes="15" method="email"');
        $xml = str_replace("atom:reminder","gd:reminder",$event_xml->asXML());
    
        $gc->post($xml, $calendar);
    }
    

    The 100 parameters will be replaced by an object this is only for test. I hope this helps someone else since this api is a nightmare. I'm thinking in creating a tutorial about this in the future.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效