public function save()
 {
     $values = $this->getValues();
     $list = $this->getOption('list');
     $opGoogleCalendarOAuth = $this->getOption('opGoogleCalendarOAuth');
     $entry = $list[$values['choice']];
     $result = $opGoogleCalendarOAuth->getContents(str_replace(opGoogleCalendarOAuth::SCOPE, '', $entry['contents']['src']), 'opCalendarApiResultsJsonEvents', opCalendarApiHandler::GET, array('start-min' => sprintf('%04d-%02d-01T00:00:00', date('Y'), $values['months']), 'start-max' => sprintf('%04d-%02d-%02dT23:59:59', date('Y'), $values['months'], opCalendarPluginToolkit::getLastDay($values['months'])), 'alt' => 'jsonc'));
     if (!$result) {
         return false;
     }
     opCalendarPluginToolkit::updateGoogleCalendarCronFlags($entry['contents']['src'], $values['google_cron_update'][0], $values['public_flag'], $this->member);
     return opCalendarPluginToolkit::insertSchedules($result->toArray(), $values['public_flag'], true);
 }
 public function save()
 {
     $values = $this->getValues();
     $id = $this->getOption('id');
     $googleCronUpdate = isset($values['google_cron_update']) && (bool) $values['google_cron_update'];
     $publicFlag = $values['public_flag'];
     $calendar = $this->getOption('googleCalendar');
     $lastDay = opCalendarPluginToolkit::getLastDay($values['months']);
     $yearMonth = sprintf('%04d-%02d', date('Y'), $values['months']);
     $events = $calendar->events->listEvents($id, array('timeMin' => date('c', strtotime(sprintf('%s-01 00:00:00', $yearMonth))), 'timeMax' => date('c', strtotime(sprintf('%s-%02d 23:59:59', $yearMonth, $lastDay))), 'showDeleted' => true));
     if (!$events) {
         return false;
     }
     opCalendarPluginToolkit::updateGoogleCalendarCronFlags($googleCronUpdate, $publicFlag, $this->member);
     return opCalendarPluginToolkit::insertSchedules($events, $publicFlag);
 }
 protected function updateMemberSchedule($cron, $interval)
 {
     $memberId = $cron['member_id'];
     $member = Doctrine_Core::getTable('Member')->find($memberId);
     if (!$member || !($calendar = $this->opCalendarOAuth->getCalendar($member))) {
         return false;
     }
     $id = $calendar->calendars->get('primary')->id;
     $publicFlag = $cron['public_flag'];
     $this->logSection('prepare', sprintf('update member_id: %d, id: %s', $memberId, $id));
     if (!($events = $this->getContents($id, $calendar))) {
         $this->logSection('result', 'skipped');
         continue;
     }
     $isSuccess = opCalendarPluginToolkit::insertSchedules($events, $publicFlag, $member);
     $this->logSection('result', $isSuccess ? 'success' : 'failed');
     if ($interval) {
         $this->logSection('sleep', 'interval: ' . $interval . ' second');
         sleep($interval);
     }
 }
 protected function execute($arguments = array(), $options = array())
 {
     // initialize the database connection
     $databaseManager = new sfDatabaseManager($this->configuration);
     $connection = $databaseManager->getDatabase($options['connection'])->getConnection();
     sfContext::createInstance($this->createConfiguration($options['application'], $options['env']), $options['application']);
     if (!Doctrine_Core::getTable('SnsConfig')->get('op_calendar_google_data_api_auto_update', false)) {
         throw new sfException("This task is not allowed. \nPlease allow from 'pc_backend.php/opCalendarPlugin' setting.");
     }
     $crons = opCalendarPluginToolkit::getAllGoogleCalendarCronConfig();
     if (!count($crons)) {
         $this->logSection('end', 'No cron data');
         exit;
     }
     $this->consumer = new OAuthConsumer(opConfig::get('op_calendar_google_data_api_key', 'anonymous'), opConfig::get('op_calendar_google_data_api_secret', 'anonymous'));
     $this->logSection('prepared', 'start update');
     foreach ($crons as $cron) {
         $member = Doctrine_Core::getTable('Member')->find($cron['member_id']);
         if (!$member) {
             continue;
         }
         $cron_config = unserialize($cron['serial']);
         foreach ($cron_config['src'] as $src) {
             $this->logSection('start', 'update member_id: ' . $cron['member_id'] . "\nscope: " . $src);
             $result = $this->getContents($src, $cron);
             if (!$result) {
                 continue;
             }
             if (opCalendarPluginToolkit::insertSchedules($result->toArray(), $cron_config['public_flag'], true, $member)) {
                 $this->logSection('end', 'updated success member_id: ' . $cron['member_id'] . "\nscope: " . $src);
             } else {
                 $this->logSection('end', 'updated failed member_id: ' . $cron['member_id'] . "\nscope: " . $src);
             }
             if ($options['interval']) {
                 $this->logSection('sleep', 'set interval: ' . $options['interval'] . ' second');
                 sleep((int) $options['interval']);
             }
         }
     }
 }