I am custom making a repeating calendar events option, but discovered any repeating meetings or jobs I make that exists after November 1st gets shifted from 12am-12pm to 11am-11pm, making the item appear to last 2 days on the calendar. I tried everything to fix it but I just can't figure it out. How can I adjust this?
Here is the code I made in the new hook I made /var/www/espocrm/data/espocrm/custom/Espo/Custom/Hooks/Common/RecurringBase.php:
Here is the code I made in the new hook I made /var/www/espocrm/data/espocrm/custom/Espo/Custom/Hooks/Common/RecurringBase.php:
PHP Code:
<?php
namespace Espo\Custom\Hooks\Common;
use Espo\ORM\Entity;
use Espo\Core\Hook\Hook\AfterSave;
use Espo\ORM\EntityManager;
use Espo\ORM\Repository\Option\SaveOptions;
class RecurringBase implements AfterSave
{
protected EntityManager $em;
public function __construct(EntityManager $em)
{
$this->em = $em;
}
public function afterSave(Entity $entity, SaveOptions $options): void
{
if (!$entity->get('repeat') || $options->get('isRecurringChild') === true) {
return;
}
$startDate = new \DateTime($entity->get('dateStart'));
$endDate = new \DateTime($entity->get('dateEnd'));
$until = $entity->get('repeatUntil') ? new \DateTime($entity->get('repeatUntil')) : null;
$interval = (int) $entity->get('repeatInterval') ?: 1;
$repeat = $entity->get('repeat');
$count = 0;
$max = 200;
while (true) {
$count++;
$newStart = (clone $startDate);
$newEnd = (clone $endDate);
switch ($repeat) {
case 'Daily':
$newStart->modify("+{$interval} days");
$newEnd->modify("+{$interval} days");
break;
case 'Weekly':
$newStart->modify("+{$interval} weeks");
$newEnd->modify("+{$interval} weeks");
break;
case 'Monthly':
$newStart->modify("+{$interval} months");
$newEnd->modify("+{$interval} months");
break;
default:
return;
}
if ($until && $newStart > $until) {
break;
}
$duplicate = $this->em->getEntityFactory()->create($entity->getEntityType());
foreach ([
'name','description','status',
'assignedUserId','teamsIds',
'parentId','parentType',
'allDay','reminders'
] as $f) {
if ($entity->has($f)) {
$duplicate->set($f, $entity->get($f));
}
}
$duplicate->set('dateStart', $newStart->format('Y-m-d H:i:s'));
$duplicate->set('dateEnd', $newEnd->format('Y-m-d H:i:s'));
$duplicate->set('repeat', null);
$duplicate->set('repeatInterval', null);
$duplicate->set('repeatUntil', null);
$this->em->saveEntity($duplicate, ['isRecurringChild' => true]);
if ($count >= $max) {
break;
}
$startDate = $newStart;
$endDate = $newEnd;
}
}
}