Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ursa 21.3.0 plat 25002 ics #12960

Open
wants to merge 2 commits into
base: Ursa-21.6.0
Choose a base branch
from
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
46 changes: 46 additions & 0 deletions alpha/apps/kaltura/lib/dateUtils.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,51 @@ public static function kDate (string $format, $timestamp = null)

return date($format, $timestamp);
}

// Format the offset from secs to +hhmm format
public static function formatOffset(int $offset)
{
$hours = floor($offset / 3600);
$minutes = floor(abs($offset) % 3600 / 60);
return sprintf('%+03d%02d', $hours, $minutes);
}

public static function convertWeekDay(int $timestamp)
{
$date = new DateTime('@' . $timestamp);
$dayOfWeek = $date->format('w'); // Get the day of the week (0 for Sunday, 6 for Saturday)
$dayOfMonth = (int) $date->format('j'); // Get the day of the month
$occurrenceOfSpecificDay = ceil($dayOfMonth / 7);

// Find the first day of the month
$firstDayOfMonth = new DateTime($date->format('Y-m-01'));

// Count how many times the same weekday has occurred up to the given day
$occurrenceOfWeekDay = 0;
for ($day = 1; $day <= $dayOfMonth; $day++)
{
$currentDayOfWeek = $firstDayOfMonth->format('w');
if ($currentDayOfWeek == $dayOfWeek)
{
$occurrenceOfWeekDay++;
}
$firstDayOfMonth->modify('+1 day');
}

// Weekday abbreviations
$weekDays = ['SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA'];

// Return the occurrence and the weekday abbreviation, e.g., "1MO" for first Monday
$occurrenceOfSpecificDay = ($occurrenceOfSpecificDay == $occurrenceOfWeekDay) ? -1 : $occurrenceOfSpecificDay;
return $occurrenceOfSpecificDay . $weekDays[$dayOfWeek];
}

public static function getDateOnPreviousYear($timestamp)
{
$date = new DateTime();
$date->setTimestamp($timestamp);
$date->modify('-1 year');
return $date->getTimestamp();
}
}
?>
39 changes: 34 additions & 5 deletions plugins/schedule/base/lib/KalturaICalSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
class KalturaICalSerializer extends KalturaSerializer
{
private $calendar;


protected $timeZoneBlockArray;

public function __construct()
{
$this->calendar = new kSchedulingICalCalendar();
$this->timeZoneBlockArray = array();
}
/**
* {@inheritDoc}
Expand All @@ -26,6 +29,31 @@ public function getHeader()
return $this->calendar->begin();
}

protected function injectTimeZoneBlocks($iCalString)
{
$position = strpos($iCalString, 'BEGIN:' . kSchedulingICal::TYPE_EVENT);
if ($position !== false)
{
$iCalBeforeEvents = substr($iCalString, 0, $position);
$iCalWithEvents = substr($iCalString, $position);
// Clean array from duplicated transitions
$this->timeZoneBlockArray = array_unique($this->timeZoneBlockArray);
// Add BEGIN/END timezone tags
array_unshift($this->timeZoneBlockArray, "BEGIN:VTIMEZONE\r\n");
$this->timeZoneBlockArray[] = "END:VTIMEZONE\r\n";
//Inject to the iCal
$timeZoneBlocksCollection = implode('', $this->timeZoneBlockArray);
return $iCalBeforeEvents . $timeZoneBlocksCollection . $iCalWithEvents;
}
return $iCalString;
}

protected function innerSerialize($object)
{
$event = kSchedulingICalEvent::fromObject($object);
return $event->write($object, $this->timeZoneBlockArray);
}


/**
* {@inheritDoc}
Expand All @@ -35,17 +63,18 @@ public function serialize($object)
{
if($object instanceof KalturaScheduleEvent)
{
$event = kSchedulingICalEvent::fromObject($object);
return $event->write();
$scheduleEventArray = new KalturaScheduleEventArray();
$scheduleEventArray[] = $object;
return $this->serialize($scheduleEventArray);
}
elseif($object instanceof KalturaScheduleEventArray)
{
$ret = '';
foreach($object as $item)
{
$ret .= $this->serialize($item);
$ret .= $this->innerSerialize($item);
}
return $ret;
return $this->injectTimeZoneBlocks($ret);
}
elseif($object instanceof KalturaScheduleEventListResponse)
{
Expand Down
21 changes: 18 additions & 3 deletions plugins/schedule/base/lib/iCal/kSchedulingICal.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
class kSchedulingICal
{
const TIME_FORMAT = 'Ymd\THis\Z';
const TIME_FORMAT_NO_TIME_ZONE = 'Ymd\THis';
const TIME_PARSE = '%Y%m%dT%H%i%sZ';
const TIME_PARSE_NO_TIME_ZONE = '%Y%m%dT%H%i%s';

Expand Down Expand Up @@ -56,11 +57,19 @@ public static function parseComponent($type, array &$lines)
return $component;
}

public static function formatDate($time)
public static function formatDate($time, $timeZoneId = null)
{
$original = date_default_timezone_get();
date_default_timezone_set('UTC');
$date = date(kSchedulingICal::TIME_FORMAT, $time);
if ($timeZoneId)
{
date_default_timezone_set($timeZoneId);
$date = date(kSchedulingICal::TIME_FORMAT_NO_TIME_ZONE, $time);
}
else
{
date_default_timezone_set('UTC');
$date = date(kSchedulingICal::TIME_FORMAT, $time);
}
date_default_timezone_set($original);
return $date;
}
Expand Down Expand Up @@ -207,4 +216,10 @@ public static function loadTzMaps($timezoneMapName)
self::$timezoneMap = array_merge(
include __DIR__ . '/../../../../../infra/general/timezones/' . $timezoneMapName . '.php');
}

// Prepare date format for ICS (e.g. 20240925T115352Z)
public static function formatTransitionDate($time)
{
return gmdate(self::TIME_FORMAT_NO_TIME_ZONE, $time);
}
}
11 changes: 11 additions & 0 deletions plugins/schedule/base/lib/iCal/kSchedulingICalCalendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

class kSchedulingICalCalendar extends kSchedulingICalComponent
{
const VERSION = '2.0';
const PRODID_PREFIX = '-//Kaltura Inc//Kaltura Server ';
const PRODID_POSTFIX = '//EN';
/**
* @param string $data
* @param KalturaScheduleEventType $eventsType
Expand All @@ -20,4 +23,12 @@ protected function getType()
{
return kSchedulingICal::TYPE_CALENDAR;
}

public function begin()
{
$ret = $this->writeField('BEGIN', $this->getType());
$ret .= $this->writeField('PRODID', self::PRODID_PREFIX . mySystemUtils::getVersion() . self::PRODID_POSTFIX);
$ret .= $this->writeField('VERSION', self::VERSION);
return $ret;
}
}
15 changes: 12 additions & 3 deletions plugins/schedule/base/lib/iCal/kSchedulingICalComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ public function getComponents()
return $this->components;
}

public function setField($field, $value)
public function setField($field, $value, $subFieldVal = null)
{
$this->fields[strtoupper($field)] = $value;
$this->fields[strtoupper($field) . $subFieldVal] = $value;
}

public function getField($field)
Expand Down Expand Up @@ -248,14 +248,23 @@ public function end()
return $this->writeField('END', $this->getType());
}

public function write()
public function write($object = null, &$timeZoneBlockArray = null)
{
$ret = '';

$this->addVtimeZoneBlockIfApplicable($object, $timeZoneBlockArray);
$ret .= $this->begin();
$ret .= $this->writeBody();
$ret .= $this->end();

return $ret;
}

protected function addVtimeZoneBlockIfApplicable($object = null, &$timeZoneBlockArray = null): void
{
if ($this->getType() === kSchedulingICal::TYPE_EVENT && $this instanceof kSchedulingICalEvent && $this->getTimeZoneId())
{
$this->addVtimeZoneBlock($object, $timeZoneBlockArray);
}
}
}
Loading