Пример #1
0
 /**
  *
  * @param
  *        	array	iCalendar component array
  * @return array
  */
 function insertCalEventsIntoDB($iCalendarComponentArray = array(), $calId, $pid = '', $cruserId = '', $isTemp = 1, $deleteNotUsedCategories = true)
 {
     $insertedOrUpdatedEventUids = array();
     $insertedOrUpdatedCategoryUids = array();
     if (empty($iCalendarComponentArray)) {
         return $insertedOrUpdatedEventUids;
     }
     $offsetArray = array();
     foreach ($iCalendarComponentArray as $component) {
         $table = 'tx_cal_event';
         $insertFields = array();
         $insertFields['isTemp'] = $isTemp;
         $insertFields['tstamp'] = time();
         $insertFields['crdate'] = time();
         $insertFields['pid'] = $pid;
         if (is_a($component, '\\TYPO3\\CMS\\Cal\\Model\\ICalendar\\vevent')) {
             $insertFields['cruser_id'] = $cruserId;
             $insertFields['calendar_id'] = $calId;
             if ($component->getAttribute('DTSTART')) {
                 $startdate = $component->getAttribute('DTSTART');
                 if (is_array($startdate)) {
                     $dateTime = new \TYPO3\CMS\Cal\Model\CalDate($startdate['year'] . $startdate['month'] . $startdate['mday'] . '000000');
                 } else {
                     $dateTime = new \TYPO3\CMS\Cal\Model\CalDate($startdate);
                 }
                 $params = $component->getAttributeParameters('DTSTART');
                 $timezone = $params['TZID'];
                 if ($timezone) {
                     $dateTime->convertTZbyID($timezone);
                 }
                 $insertFields['start_date'] = $dateTime->format('%Y%m%d');
                 $insertFields['start_time'] = $dateTime->hour * 3600 + $dateTime->minute * 60;
             } else {
                 continue;
             }
             if ($component->getAttribute('DTEND')) {
                 $enddate = $component->getAttribute('DTEND');
                 if (is_array($enddate)) {
                     $dateTime = new \TYPO3\CMS\Cal\Model\CalDate($enddate['year'] . $enddate['month'] . $enddate['mday'] . '000000');
                 } else {
                     $dateTime = new \TYPO3\CMS\Cal\Model\CalDate($enddate);
                 }
                 $params = $component->getAttributeParameters('DTEND');
                 $timezone = $params['TZID'];
                 if ($timezone) {
                     $dateTime->convertTZbyID($timezone);
                 }
                 $insertFields['end_date'] = $dateTime->format('%Y%m%d');
                 $insertFields['end_time'] = $dateTime->hour * 3600 + $dateTime->minute * 60;
             }
             if ($component->getAttribute('DURATION')) {
                 $enddate = $insertFields['start_time'] + $component->getAttribute('DURATION');
                 $dateTime = new \TYPO3\CMS\Cal\Model\CalDate($insertFields['start_date']);
                 $dateTime->addSeconds($enddate);
                 $params = $component->getAttributeParameters('DURATION');
                 $timezone = $params['TZID'];
                 if ($timezone) {
                     $dateTime->convertTZbyID($timezone);
                 }
                 $insertFields['end_date'] = $dateTime->format('%Y%m%d');
                 $insertFields['end_time'] = $dateTime->hour * 3600 + $dateTime->minute * 60;
             }
             $insertFields['icsUid'] = $component->getAttribute('UID');
             $insertFields['title'] = $component->getAttribute('SUMMARY');
             if ($component->organizerName()) {
                 $insertFields['organizer'] = str_replace('"', '', $component->organizerName());
             }
             $insertFields['location'] = $component->getAttribute('LOCATION');
             if ($insertFields['location'] == null) {
                 $insertFields['location'] = '';
             }
             $insertFields['description'] = $component->getAttribute('DESCRIPTION');
             $categoryString = $component->getAttribute('CATEGORY');
             if ($categoryString == "") {
                 $categoryString = $component->getAttribute('CATEGORIES');
             }
             $categories = GeneralUtility::trimExplode(',', $categoryString, 1);
             $categoryUids = array();
             foreach ($categories as $category) {
                 $category = trim($category);
                 $categorySelect = '*';
                 $categoryTable = 'tx_cal_category';
                 $categoryWhere = 'calendar_id = ' . intval($calId) . ' AND title =' . $GLOBALS['TYPO3_DB']->fullQuoteStr($category, $categoryTable);
                 $foundCategory = false;
                 $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery($categorySelect, $categoryTable, $categoryWhere);
                 if ($result) {
                     while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) {
                         $foundCategory = true;
                         $categoryUids[] = $row['uid'];
                     }
                     $GLOBALS['TYPO3_DB']->sql_free_result($result);
                 }
                 if (!$foundCategory) {
                     $result = $GLOBALS['TYPO3_DB']->exec_INSERTquery($categoryTable, array('tstamp' => $insertFields['tstamp'], 'crdate' => $insertFields['crdate'], 'pid' => $pid, 'title' => $category, 'calendar_id' => $calId));
                     if (FALSE === $result) {
                         throw new \RuntimeException('Could not write ' . $categoryTable . ' record to database: ' . $GLOBALS['TYPO3_DB']->sql_error(), 1431458143);
                     }
                     $categoryUids[] = $GLOBALS['TYPO3_DB']->sql_insert_id();
                 }
             }
             if ($component->getAttribute('RRULE')) {
                 $rrule = $component->getAttribute('RRULE');
                 $this->insertRuleValues($rrule, $insertFields);
             }
             if ($component->getAttribute('RDATE')) {
                 $rdate = $component->getAttribute('RDATE');
                 if (is_array($rdate)) {
                     $insertFields['rdate'] = implode(',', $rdate);
                 } else {
                     $insertFields['rdate'] = $rdate;
                 }
                 if ($component->getAttributeParameters('RDATE')) {
                     $parameterArray = $component->getAttributeParameters('RDATE');
                     $keys = array_keys($parameterArray);
                     $insertFields['rdate_type'] = strtolower($keys[0]);
                 } else {
                     $insertFields['rdate_type'] = 'date_time';
                 }
             }
             // Fix for allday events
             if ($insertFields['start_time'] == 0 && $insertFields['end_time'] == 0) {
                 $date = new \TYPO3\CMS\Cal\Model\CalDate($insertFields['end_date'] . '000000');
                 $date->setTZbyId('UTC');
                 $date->subtractSeconds(86400);
                 $insertFields['end_date'] = $date->format('%Y%m%d');
             }
             $eventRow = BackendUtility::getRecordRaw('tx_cal_event', 'icsUid="' . $insertFields['icsUid'] . '"');
             if ($eventRow['uid']) {
                 $result = $GLOBALS['TYPO3_DB']->exec_UPDATEquery($table, 'uid=' . $eventRow['uid'], $insertFields);
                 $eventUid = $eventRow['uid'];
             } else {
                 $result = $GLOBALS['TYPO3_DB']->exec_INSERTquery($table, $insertFields);
                 if (FALSE === $result) {
                     throw new \RuntimeException('Could not write ' . $table . ' record to database: ' . $GLOBALS['TYPO3_DB']->sql_error(), 1431458144);
                 }
                 $eventUid = $GLOBALS['TYPO3_DB']->sql_insert_id();
             }
             $extConf = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['cal']);
             if ($component->getAttribute('RECURRENCE-ID')) {
                 $recurrenceIdStart = new \TYPO3\CMS\Cal\Model\CalDate($component->getAttribute('RECURRENCE-ID'));
                 $params = $component->getAttributeParameters('RECURRENCE-ID');
                 $timezone = $params['TZID'];
                 if ($timezone) {
                     $recurrenceIdStart->convertTZbyID($timezone);
                 }
                 $indexEntry = BackendUtility::getRecordRaw('tx_cal_index', 'event_uid="' . $eventUid . '" AND start_datetime="' . $recurrenceIdStart->format('%Y%m%d%H%M%S') . '"');
                 if ($indexEntry) {
                     $origStartDate = new \TYPO3\CMS\Cal\Model\CalDate();
                     $origStartDate = new \TYPO3\CMS\Cal\Model\CalDate();
                     $table = 'tx_cal_event_deviation';
                     $insertFields['parentid'] = $eventUid;
                     $insertFields['orig_start_time'] = $recurrenceIdStart->getHour() * 3600 + $recurrenceIdStart->getMinute() * 60;
                     $recurrenceIdStart->setHour(0);
                     $recurrenceIdStart->setMinute(0);
                     $recurrenceIdStart->setSecond(0);
                     $insertFields['orig_start_date'] = $recurrenceIdStart->getTime();
                     unset($insertFields['calendar_id']);
                     if ($indexEntry['event_deviation_uid'] > 0) {
                         $result = $GLOBALS['TYPO3_DB']->exec_UPDATEquery($table, 'uid=' . $indexEntry['event_deviation_uid'], $insertFields);
                         $eventDeviationUid = $indexEntry['event_deviation_uid'];
                     } else {
                         $result = $GLOBALS['TYPO3_DB']->exec_INSERTquery($table, $insertFields);
                         if (FALSE === $result) {
                             throw new \RuntimeException('Could not write ' . $table . ' record to database: ' . $GLOBALS['TYPO3_DB']->sql_error(), 1431458145);
                         }
                         $eventDeviationUid = $GLOBALS['TYPO3_DB']->sql_insert_id();
                     }
                     $GLOBALS['TYPO3_DB']->exec_UPDATEquery('tx_cal_index', 'uid=' . $indexEntry['uid'], array('event_deviation_uid' => $eventDeviationUid));
                 }
             } else {
                 /* Delete the old exception relations */
                 $exceptionEventUidsToBeDeleted = array();
                 $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery('tx_cal_exception_event.uid', 'tx_cal_exception_event,tx_cal_exception_event_mm', 'tx_cal_exception_event.uid = tx_cal_exception_event_mm.uid_foreign AND tx_cal_exception_event_mm.uid_local=' . $eventUid);
                 if ($result) {
                     while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) {
                         $exceptionEventUidsToBeDeleted[] = $row['uid'];
                     }
                     $GLOBALS['TYPO3_DB']->sql_free_result($result);
                 }
                 if (!empty($exceptionEventUidsToBeDeleted)) {
                     $GLOBALS['TYPO3_DB']->exec_DELETEquery('tx_cal_exception_event', 'uid in (' . implode(',', $exceptionEventUidsToBeDeleted) . ')');
                 }
                 $exceptionEventGroupUidsToBeDeleted = array();
                 $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery('tx_cal_exception_event_group.uid', 'tx_cal_exception_event_group,tx_cal_exception_event_group_mm', 'tx_cal_exception_event_group.uid = tx_cal_exception_event_group_mm.uid_foreign AND tx_cal_exception_event_group_mm.uid_local=' . $eventUid);
                 if ($result) {
                     while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) {
                         $exceptionEventGroupUidsToBeDeleted[] = $row['uid'];
                     }
                     $GLOBALS['TYPO3_DB']->sql_free_result($result);
                 }
                 if (!empty($exceptionEventGroupUidsToBeDeleted)) {
                     $GLOBALS['TYPO3_DB']->exec_DELETEquery('tx_cal_exception_event_group', 'uid in (' . implode(',', $exceptionEventGroupUidsToBeDeleted) . ')');
                 }
                 $where = ' uid_local=' . $eventUid;
                 $GLOBALS['TYPO3_DB']->exec_DELETEquery('tx_cal_exception_event_mm', $where);
                 $GLOBALS['TYPO3_DB']->exec_DELETEquery('tx_cal_exception_event_group_mm', $where);
                 // Exceptions:
                 if ($component->getAttribute('EXDATE')) {
                     if (is_array($component->getAttribute('EXDATE'))) {
                         foreach ($component->getAttribute('EXDATE') as $exceptionDescription) {
                             $this->createException($pid, $cruserId, $eventUid, $exceptionDescription);
                         }
                     } else {
                         $this->createException($pid, $cruserId, $eventUid, $component->getAttribute('EXDATE'));
                     }
                 }
                 if ($component->getAttribute('EXRULE')) {
                     if (is_array($component->getAttribute('EXRULE'))) {
                         foreach ($component->getAttribute('EXRULE') as $exceptionDescription) {
                             $this->createExceptionRule($pid, $cruserId, $eventUid, $exceptionDescription);
                         }
                     } else {
                         $this->createExceptionRule($pid, $cruserId, $eventUid, $component->getAttribute('EXRULE'));
                     }
                 }
                 $pageTSConf = BackendUtility::getPagesTSconfig($pid);
                 if ($pageTSConf['options.']['tx_cal_controller.']['pageIDForPlugin']) {
                     $pageIDForPlugin = $pageTSConf['options.']['tx_cal_controller.']['pageIDForPlugin'];
                 } else {
                     $pageIDForPlugin = $pid;
                 }
                 /** @var \TYPO3\CMS\Cal\Utility\RecurrenceGenerator $rgc */
                 $rgc = GeneralUtility::makeInstance('TYPO3\\CMS\\Cal\\Utility\\RecurrenceGenerator', $pageIDForPlugin);
                 $rgc->generateIndexForUid($eventUid, 'tx_cal_event');
                 if ($this->conf['view.']['event.']['remind']) {
                     /* Schedule reminders for new and changed events */
                     $pageTSConf = BackendUtility::getPagesTSconfig($pid);
                     $offset = is_numeric($pageTSConf['options.']['tx_cal_controller.']['view.']['event.']['remind.']['time']) ? $pageTSConf['options.']['tx_cal_controller.']['view.']['event.']['remind.']['time'] * 60 : 0;
                     $date = new \TYPO3\CMS\Cal\Model\CalDate($insertFields['start_date'] . '000000');
                     $date->setTZbyId('UTC');
                     $reminderTimestamp = $date->getTime() + $insertFields['start_time'] - $offset;
                     $reminderService =& \TYPO3\CMS\Cal\Utility\Functions::getReminderService();
                     $reminderService->scheduleReminder($eventUid);
                 }
                 /* Delete the old category relations */
                 $where = ' uid_local=' . $eventUid;
                 $GLOBALS['TYPO3_DB']->exec_DELETEquery('tx_cal_event_category_mm', $where);
                 foreach ($categoryUids as $uid) {
                     $result = $GLOBALS['TYPO3_DB']->exec_INSERTquery('tx_cal_event_category_mm', array('uid_local' => $eventUid, 'uid_foreign' => $uid));
                     if (FALSE === $result) {
                         throw new \RuntimeException('Could not write tx_cal_event_category_mm record to database: ' . $GLOBALS['TYPO3_DB']->sql_error(), 1431458146);
                     }
                 }
                 $insertedOrUpdatedEventUids[] = $eventUid;
                 $insertedOrUpdatedCategoryUids = array_merge($insertedOrUpdatedCategoryUids, $categoryUids);
                 // Hook: insertCalEventsIntoDB
                 $hookObjectsArr = \TYPO3\CMS\Cal\Utility\Functions::getHookObjectsArray('tx_cal_icalendar_service', 'iCalendarServiceClass', 'service');
                 foreach ($hookObjectsArr as $hookObj) {
                     if (method_exists($hookObj, 'insertCalEventsIntoDB')) {
                         $hookObj->insertCalEventsIntoDB($this, $eventUid, $component);
                     }
                 }
             }
         }
     }
     if ($deleteNotUsedCategories) {
         /* Delete the categories */
         $where = ' calendar_id=' . $calId;
         if (!empty($insertedOrUpdatedCategoryUids)) {
             array_unique($insertedOrUpdatedCategoryUids);
             $where .= ' AND uid NOT IN (' . implode(',', $insertedOrUpdatedCategoryUids) . ')';
         }
         $extConf = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['cal']);
         $GLOBALS['TYPO3_DB']->exec_DELETEquery($extConf['categoryService'], $where);
     }
     return $insertedOrUpdatedEventUids;
 }