Last active
May 24, 2023 10:42
-
-
Save fintanmm/097bd9e34eaa9612cea27e7cf64aa2d0 to your computer and use it in GitHub Desktop.
EventDateHook for EspoCRM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Espo\Custom\Hooks\CaseObj; | |
use Espo\Core\InjectableFactory; | |
use Espo\Core\Utils\Log; | |
use Espo\Modules\Crm\Entities\CaseObj; | |
use Espo\ORM\Entity; | |
use Espo\ORM\EntityManager; | |
// This hook will be executed after the save of the Case entity | |
// The hook belongs to the EspoCRM software | |
class EventDateHook | |
{ | |
public static $order = 1; | |
public function __construct( | |
private EntityManager $entityManager, | |
private InjectableFactory $injectableFactory, | |
private Log $log | |
) { | |
} | |
public function afterSave(Entity $entity, array $options = array()) | |
{ | |
// Check if the case has a contact assigned | |
if (!empty($entity->get('contactId'))) { | |
// Fetch related meetings from the contact for EspoCRM | |
$relatedMeetings = $this->entityManager | |
->getRDBRepositoryByClass(CaseObj::class) | |
->getRelation($entity, 'meetings'); | |
$fineOne = $relatedMeetings->findOne(); | |
if ($fineOne != null) { | |
// Get the date of the meeting | |
$meetingDateStart = $relatedMeetings->findOne()->get('dateStart'); | |
// Update the case with the link to the meeting | |
$entity->set('eventStartDate', $meetingDateStart); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment