I am creating a recurring calendar event feature since espo doesn't provide it out of the box, but I ran into a problem where all my calendar events I create makes 2 on each day on want there to be an event instead of just one. the day I first start this recurring calendar event is fine, but I get 2 anytime it is duplicated. How can I fix this? I'll share a picture example of what my process looks like, as well as the code I made and where I placed it:
/var/www/espocrm/data/espocrm/custom/Espo/Custom/Hooks/Common/RecurringBase.php:
/var/www/espocrm/data/espocrm/custom/Espo/Custom/Hooks/Meeting/Recurring.php:
And pictures showing the process for my recurring calendar items and the issue I'm having are attached. Any help would be appreciated.
/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;
}
}
}
/var/www/espocrm/data/espocrm/custom/Espo/Custom/Hooks/Meeting/Recurring.php:
PHP Code:
<?php
namespace Espo\Custom\Hooks\Meeting;
use Espo\Custom\Hooks\Common\RecurringBase;
class Recurring extends RecurringBase {}