UTF8 encoding
[TYPO3CMS/Extensions/caldav.git] / Classes / Service / IcsGenerator.php
1 <?php
2 namespace TYPO3\CMS\Caldav\Service;
3
4 /**
5 * *************************************************************
6 * Copyright notice
7 *
8 * (c) 2010-2015 Mario Matzulla (mario(at)matzullas.de)
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 * *************************************************************
27 */
28 use TYPO3\CMS\Backend\Utility\BackendUtility;
29 use TYPO3\CMS\Core\Utility\GeneralUtility;
30
31 class IcsGenerator {
32 var $info = '';
33 var $pageIDForPlugin;
34 var $extConf;
35 var $table = 'tx_cal_event,tx_cal_calendar';
36 var $where = 'tx_cal_event.calendar_id = tx_cal_calendar.uid and tx_cal_calendar.type = 0 and tx_cal_calendar.nearby = 0 and tx_cal_event.type in (0,1,2,3) and tx_cal_event.deleted = 0 and tx_cal_event.hidden = 0 and tx_cal_calendar.deleted = 0 and tx_cal_calendar.hidden = 0 and ((tx_cal_event.tx_caldav_data is null) or (tx_cal_event.tx_caldav_data like "%no event model template file found%" OR tx_cal_event.tx_caldav_data like "%no event model template file found:%" OR tx_cal_event.tx_caldav_data like "%could not find%"))';
37 function __construct($pageIDForPlugin) {
38 $this->extConf = unserialize ( $GLOBALS ['TYPO3_CONF_VARS'] ['EXT'] ['extConf'] ['cal'] );
39 $this->pageIDForPlugin = $pageIDForPlugin;
40 }
41 function getInfo() {
42 return $this->info;
43 }
44 function check() {
45 $select = 'tx_cal_event.*';
46 $table = $this->table;
47 $where = $this->where;
48
49 $return = '';
50
51 $results = $GLOBALS ['TYPO3_DB']->exec_SELECTquery ( $select, $table, $where );
52 if ($results) {
53 while ( $row = $GLOBALS ['TYPO3_DB']->sql_fetch_assoc ( $results ) ) {
54 $return .= '<p>[UID:' . $row ['uid'] . '; PID:' . $row ['pid'] . '] ' . $row ['title'] . ' -> ' . $row ['tx_caldav_data'] . '</p><br />';
55 }
56 $GLOBALS ['TYPO3_DB']->sql_free_result ( $results );
57 }
58 if ($return == '') {
59 $return = 'no problems found';
60 }
61 return $return;
62 }
63 function countEventsWithoutIcs($eventPage = 0) {
64 $count = 0;
65 $select = 'count(*)';
66 $table = $this->table;
67 $where = $this->where;
68 if ($eventPage > 0) {
69 $where = 'tx_cal_event.pid = ' . $eventPage . ' AND ' . $where;
70 }
71 $results = $GLOBALS ['TYPO3_DB']->exec_SELECTquery ( $select, $table, $where );
72 if ($results) {
73 while ( $row = $GLOBALS ['TYPO3_DB']->sql_fetch_assoc ( $results ) ) {
74 $count = $row ['count(*)'];
75 }
76 $GLOBALS ['TYPO3_DB']->sql_free_result ( $results );
77 }
78
79 return $count;
80 }
81 function generateIcs($eventPage = 0) {
82 $select = 'tx_cal_event.*';
83 $table = $this->table;
84 $where = $this->where;
85 if ($eventPage > 0) {
86 $where = 'tx_cal_event.pid = ' . $eventPage . ' AND ' . $where;
87 }
88
89 $results = $GLOBALS ['TYPO3_DB']->exec_SELECTquery ( $select, $table, $where );
90 if ($results) {
91 $eventArray = Array();
92 while ( $event = $GLOBALS ['TYPO3_DB']->sql_fetch_assoc ( $results ) ) {
93 $eventArray [] = $event;
94 }
95 $GLOBALS ['TYPO3_DB']->sql_free_result ( $results );
96
97 $page = BackendUtility::getRecord ( 'pages', intval ( $this->pageIDForPlugin ), 'doktype' );
98 if ($page ['doktype'] != 254) {
99 $calAPI = new \TYPO3\CMS\Cal\Controller\Api ();
100 $calAPI = &$calAPI->tx_cal_api_without ( $this->pageIDForPlugin );
101
102 foreach ( $eventArray as $event ) {
103
104 if ($event ['pid'] > 0) {
105 /* Check Page TSConfig for a preview page that we should use */
106 $eventObject = $calAPI->modelObj->findEvent ( $event ['uid'], 'tx_cal_phpicalendar', $event ['pid'], false, false, false, true, true );
107 if (is_object ( $eventObject )) {
108 if ($eventObject->conf ['view.'] ['event.'] ['phpicalendarEventTemplate']) {
109 $oldPath = &$eventObject->conf ['view.'] ['event.'] ['phpicalendarEventTemplate'];
110 } else {
111 $oldPath = &$eventObject->conf ['view.'] ['event.'] ['eventModelTemplate'];
112 }
113
114 $oldView = $eventObject->conf ['view'];
115 $eventObject->conf ['view'] = 'single_ics';
116 $extPath = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath ( 'cal' );
117
118 $oldPath = 'EXT:cal/Resources/Private/Templates/v2/event_model.tmpl';
119 $oldPath = str_replace ( 'EXT:cal/', $extPath, $oldPath );
120 // $oldPath = str_replace(PATH_site, '', $oldPath);
121 $eventObject->conf ['view.'] ['event.'] ['phpicalendarEventTemplate'] = $oldPath;
122 $eventObject->conf ['view.'] ['event.'] ['eventModelTemplate'] = $oldPath;
123 $oldBackPath = $GLOBALS ['TSFE']->tmpl->getFileName_backPath;
124 $GLOBALS ['TSFE']->tmpl->getFileName_backPath = '';
125 $fileInfo = GeneralUtility::split_fileref ( $oldPath );
126 $GLOBALS ['TSFE']->tmpl->allowedPaths [] = $fileInfo ['path'];
127
128 $viewObj = &\TYPO3\CMS\cal\Utility\Registry::Registry ( 'basic', 'viewcontroller' );
129
130 $masterArray = Array (
131 $eventObject
132 );
133 $drawnIcs = $viewObj->drawIcs ( $masterArray, '', false );
134
135 $table = 'tx_cal_event';
136 $where = 'uid = ' . $event ['uid'];
137 $eventData = Array (
138 'tx_caldav_data' => rtrim(utf8_encode($drawnIcs))
139 );
140 if ($event ['tx_caldav_uid'] == null || $event ['tx_caldav_uid'] == '') {
141
142 $eventData ['tx_caldav_uid'] = $calAPI->conf ['view.'] ['ics.'] ['eventUidPrefix'] . '_' . $event ['calendar_id'] . '_' . $event ['uid'];
143 }
144 $result = $GLOBALS ['TYPO3_DB']->exec_UPDATEquery ( $table, $where, $eventData );
145
146 $table = 'tx_cal_calendar';
147 $where = 'tx_cal_calendar.uid = ' . $event ['calendar_id'];
148 $eventData = Array (
149 'tstamp' => time ()
150 );
151 $result = $GLOBALS ['TYPO3_DB']->exec_UPDATEquery ( $table, $where, $eventData );
152
153 $GLOBALS ['TSFE']->tmpl->getFileName_backPath = $oldBackPath;
154 $eventObject->conf ['view'] = $oldView;
155 }
156 }
157 }
158 }
159 }
160
161 $this->info = 'Done.';
162 }
163 }
164
165 if (defined ( 'TYPO3_MODE' ) && $TYPO3_CONF_VARS [TYPO3_MODE] ['XCLASS'] ['ext/caldav/mod1/class.tx_cal_ics_generator.php']) {
166 require_once ($TYPO3_CONF_VARS [TYPO3_MODE] ['XCLASS'] ['ext/caldav/mod1/class.tx_cal_ics_generator.php']);
167 }
168
169 ?>