Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 71 additions & 13 deletions classes/CalendarExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ public function exportCalendar($intId)
return;
}

$filename = strlen($objCalendar->ical_alias) ? $objCalendar->ical_alias : 'calendar' . $objCalendar->id;
//$filename = strlen($objCalendar->ical_alias) ? $objCalendar->ical_alias : 'calendar' . $objCalendar->id;

$filename = $this->getICalFilename($objCalendar);

// Delete ics file
if (\Input::get('act') == 'delete')
{
Expand All @@ -62,8 +64,8 @@ public function generateSubscriptions()

while ($objCalendar->next())
{
$filename = strlen($objCalendar->ical_alias) ? $objCalendar->ical_alias : 'calendar' . $objCalendar->id;

//$filename = strlen($objCalendar->ical_alias) ? $objCalendar->ical_alias : 'calendar' . $objCalendar->id;
$filename = $this->getICalFilename($objCalendar);
$this->generateFiles($objCalendar->row());
$this->log('Generated ical subscription "' . $filename . '.ics"', 'CalendarExport generateSubscriptions()', TL_CRON);
}
Expand All @@ -81,14 +83,28 @@ protected function generateFiles($arrArchive)

$startdate = (strlen($arrArchive['ical_start'])) ? $arrArchive['ical_start'] : time();
$enddate = (strlen($arrArchive['ical_end'])) ? $arrArchive['ical_end'] : time()+$GLOBALS['calendar_ical']['endDateTimeDifferenceInDays']*24*3600;
$filename = strlen($arrArchive['ical_alias']) ? $arrArchive['ical_alias'] : 'calendar' . $arrArchive['id'];
$ical = $this->getAllEvents(array($arrArchive['id']), $startdate, $enddate, $arrArchive['title'], $arrArchive['ical_description'], $filename, $arrArchive['ical_prefix']);
//$filename = strlen($arrArchive['ical_alias']) ? $arrArchive['ical_alias'] : 'calendar' . $arrArchive['id'];
$filename = $this->getICalFilename($arrArchive);
$ical = $this->getAllEvents(array($arrArchive['id']), $startdate, $enddate, $arrArchive['title'], $arrArchive['ical_description'], $filename, $arrArchive['ical_prefix']);
$content = $ical->createCalendar();
$objFile = new File($filename . ".ics");
$objFile->write($content);
$objFile->close();
}

private function getICalFilename($archive)
{
$cm_icalBasepath = \Config::get('cm_icalBasepath');
if ($cm_icalBasepath) $cm_icalBasepath.='/';

if (is_array($archive))
{
return $cm_icalBasepath.(strlen($archive['ical_alias']) ? $archive['ical_alias'] : 'calendar' . $archive['id']);
}
return $cm_icalBasepath.(strlen($archive->ical_alias) ? $archive->ical_alias : 'calendar' . $archive->id);

}

/**
* Remove old ics files from the root directory
*/
Expand All @@ -99,16 +115,18 @@ public function removeOldSubscriptions()

while ($objFeeds->next())
{
$arrFeeds[] = strlen($objFeeds->ical_alias) ? $objFeeds->ical_alias : 'calendar' . $objFeeds->id;
//$arrFeeds[] = strlen($objFeeds->ical_alias) ? $objFeeds->ical_alias : 'calendar' . $objFeeds->id;
$arrFeeds[] = $this->getICalFilename($objFeeds);
}

// Make sure dcaconfig.php is loaded
include(TL_ROOT . '/system/config/dcaconfig.php');

// Delete old files
foreach (scan(TL_ROOT) as $file)
$cm_icalBasepath = TL_ROOT.'/'.\Config::get('cm_icalBasepath');

// Delete old files
foreach (scan($cm_icalBasepath) as $file)
{
if (is_dir(TL_ROOT . '/' . $file))
if (is_dir($cm_icalBasepath . '/' . $file))
{
continue;
}
Expand All @@ -127,7 +145,31 @@ public function removeOldSubscriptions()

$objFile->close();
}
return array();
if ($cm_icalBasepath)
{
foreach (scan(TL_ROOT) as $file)
{
if (is_dir(TL_ROOT . '/' . $file))
{
continue;
}

if (is_array($GLOBALS['TL_CONFIG']['rootFiles']) && in_array($file, $GLOBALS['TL_CONFIG']['rootFiles']))
{
continue;
}

$objFile = new \File($file);
if ($objFile->extension == 'ics' && !in_array($objFile->filename, $arrFeeds) && !preg_match('/^sitemap/i', $objFile->filename))
{
$this->log('file ' . $objFile->filename, '', TL_CRON);
$objFile->delete();
}

$objFile->close();
}
}
return array();
}

protected function getAllEvents($arrCalendars, $intStart, $intEnd, $title, $description, $filename = "", $title_prefix)
Expand Down Expand Up @@ -178,7 +220,7 @@ protected function getAllEvents($arrCalendars, $intStart, $intEnd, $title, $desc
}
$vevent->setProperty( 'summary', html_entity_decode((strlen($title_prefix) ? $title_prefix . " " : "") . $objEvents->title, ENT_QUOTES, 'UTF-8'));
$vevent->setProperty( 'description', html_entity_decode(strip_tags(preg_replace('/<br \\/>/', "\n", $this->replaceInsertTags($objEvents->teaser))), ENT_QUOTES, 'UTF-8'));

/*
$strDetails = '';
$objElement = \ContentModel::findPublishedByPidAndTable($objEvents->id, 'tl_calendar_events');
Expand Down Expand Up @@ -228,6 +270,7 @@ protected function getAllEvents($arrCalendars, $intStart, $intEnd, $title, $desc
{
$count = 0;
$arrRepeat = deserialize($objEvents->repeatEach);

$arg = $arrRepeat['value'];
$unit = $arrRepeat['unit'];
if ($arg == 1)
Expand Down Expand Up @@ -282,10 +325,25 @@ protected function getAllEvents($arrCalendars, $intStart, $intEnd, $title, $desc
/*
* end module event_recurrences handling
*/

$vevent = $this->cm_processModifyVEventHook($vevent, $objEvents);

$ical->setComponent ($vevent);
}
}
return $ical;
}

private function cm_processModifyVEventHook($vEvent, $eventData)
{
if (isset($GLOBALS['TL_HOOKS']['cm_modifyVEvent']) && is_array($GLOBALS['TL_HOOKS']['cm_modifyVEvent']))
{
foreach ($GLOBALS['TL_HOOKS']['cm_modifyVEvent'] as $callback)
{
$this->import($callback[0]);
$vEvent = $this->{$callback[0]}->{$callback[1]}($vEvent, $eventData);
}
}
return $vEvent;
}

}
88 changes: 88 additions & 0 deletions dca/tl_settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
/**

* @copyright Christian Muenster 2017
* @author Christian Muenster
* @license LGPL
* @filesource
*/


$GLOBALS['TL_DCA']['tl_settings']['palettes']['default'] .= ';{cm_ical_legend},cm_icalBasepath';

/**
* Add fields
*/
$GLOBALS['TL_DCA']['tl_settings']['fields']['cm_icalBasepath'] = array(
'label' => &$GLOBALS['TL_LANG']['tl_settings']['cm_icalBasepath'],
'exclude' => true,
'inputType' => 'fileTree',
'eval' => array('files'=>false, 'fieldType'=>'radio', 'tl_class'=>'clr w50'),
'save_callback' => array(array('cm_ICalSettings', 'saveFile')),
'load_callback' => array(array('cm_ICalSettings', 'loadFile'))
// 'sql' => "binary(16) NULL"
);

class cm_ICalSettings extends \Backend
{
/**
* Import the back end user object
*/
public function __construct()
{
parent::__construct();
$this->import('BackendUser', 'User');
}

/* public function saveFile($value)
{
if(version_compare(VERSION, '3.2', '>='))
{
$uuid = String::binToUuid($value);
$objFile = FilesModel::findByUuid($uuid);
$value = $objFile->path;
}
return $value;
}

public function loadFile($value)
{
if(version_compare(VERSION, '3.2', '>='))
{
$objFile = FilesModel::findByPath($value);
$value = $objFile->uuid;
}
return $value;
}
*/
public function saveFile($value)
{
//echo strlen($value) == 16 ? \String::binToUuid($value) : $value;
if(version_compare(VERSION, '3.2', '>='))
{
$uuid = String::binToUuid($value);
$objFile = \FilesModel::findByUuid($value);
$value = $objFile->path;
}
return $value;
}

/**
* Save path in file, not a uuid by load dca
*
* @param uuid
*/
public function loadFile($value)
{
//return \String::uuidToBin($value);
if($value && version_compare(VERSION, '3.2', '>='))
{
$objFile = FilesModel::findByPath($value);
if ($objFile->numRows<1) return '';
$value = $objFile->uuid;
}
return $value;
}
}

?>