Forums › Forums › OroCRM › OroCRM – How do I? Questions › Create Calendar Events from Another Entity's Controller
This topic contains 2 replies, has 2 voices, and was last updated by oro_newbie 6 years, 3 months ago.
Starting from March 1, 2020 the forum has been switched to the read-only mode. Please head to StackOverflow for support.
- CreatorTopic
- January 10, 2018 at 4:50 am #29490
Hi,
I would like to create calendar events from the Case Entity (I know you can add tasks but I have a custom field with the case entity – follow up date & follow up action using which I would like to create a calendar event everytime it’s updated).
I think there are different ways to this?
1. Try to make use of the Calendar Event’s form handler
2. Make use of the workflow action used to create the calendar event (programmatically)
3. Add the calendar event directly to the database – I would prefer this but in this case I do not know how to add the reminders and the context.Can anyone please advise?
Thank you in advance.
- CreatorTopic
- AuthorReplies
- January 10, 2018 at 6:54 am #29491
Hi,
yes you can add a listener which will add calendar event into Db when Case entity’s fields are changed.Please note that CalendarEvent entity implements RemindableInterface which allows you to add reminders invoking setReminders method:
PHP1$calendarEventEntity->setReminders($remindersCollection);To add context you need to use ActivityManager. You can take a look at form handler to figure out how it works while processing the form.
So just invoke this method with the right parameters. In your case it would be something like:PHP1$activityManager->setActivityTargets($calendarEventEntity, [$caseEntity]);January 11, 2018 at 6:37 am #29492@igalayev
Thank you so much! Your advise worked perfectly. Here’s the code if anyone’s still stuck:
1) Create a Listener for your bundle (FollowUpListener in my case):
PHP123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135<?phpnamespace Custom\Bundle\CaseBundle\Listener;use Doctrine\Common\Collections\ArrayCollection;use Doctrine\Common\EventSubscriber;use Doctrine\Common\Persistence\ManagerRegistry;use Doctrine\ORM\Event\LifecycleEventArgs;use Symfony\Component\DependencyInjection\ContainerInterface;use Oro\Bundle\ActivityBundle\Manager\ActivityManager;use OroCRM\Bundle\CaseBundle\Entity\CaseEntity;use Oro\Bundle\CalendarBundle\Entity\CalendarEvent;use Oro\Bundle\ReminderBundle\Entity\Reminder;use Oro\Bundle\ReminderBundle\Model\ReminderInterval;class FollowUpListener implements EventSubscriber{protected $container;protected $registry;protected $activityManager;public function __construct(ContainerInterface $container, ManagerRegistry $registry, ActivityManager $activityManager){$this->container = $container;$this->registry = $registry;$this->activityManager = $activityManager;}public function getSubscribedEvents(){return array('postPersist','postUpdate',);}/*** This method will called on Doctrine postPersist event*/public function postPersist(LifecycleEventArgs $args){$entity = $args->getEntity();$em = $args->getEntityManager();$uow = $em->getUnitOfWork();if($entity instanceof CaseEntity) {$changes = $uow->getEntityChangeSet($entity);//check if follow up date has been changed$followup_changed = isset($changes['followup_date']) ? true : false;if ($followup_changed) {//Create Calendar Event$calendarEvent = new CalendarEvent();$calendarEvent->setStart($entity->getFollowupDate())->setEnd($entity->getFollowupDate())->setTitle($entity->getFollowUp())->setDescription($entity->getDescription())->setCalendar($em->getRepository('OroCalendarBundle:Calendar')->findOneBy(['owner'=>$this->container->get('security.context')->getToken()->getUser()->getId()]))->setAllDay(false);$calendarManager = $this->registry->getManagerForClass('Oro\Bundle\CalendarBundle\Entity\CalendarEvent');$calendarManager->persist($calendarEvent);$calendarManager->flush();// Add Context$this->activityManager->setActivityTargets($calendarEvent, [$entity]);// Add Reminders$reminders = new ArrayCollection();$interval = new ReminderInterval(5,'M'); // Remind 5 minutes in advance$methods = ['email', 'web_socket']; // Remind both by email and in the applicationforeach ($methods as $method){$reminderEntity = new Reminder();$reminderEntity->setSubject($calendarEvent->getTitle())->setExpireAt($calendarEvent->getStart())->setMethod($method)->setInterval($interval)->setRelatedEntityClassName(get_class($calendarEvent))->setRelatedEntityId($calendarEvent->getId())->setRecipient($calendarEvent->getCalendar()->getOwner());$reminderManager = $this->registry->getManagerForClass('Oro\Bundle\ReminderBundle\Entity\Reminder');$reminderManager->persist($reminderEntity);$reminders[] = $reminderEntity;}$calendarEvent->setReminders($reminders);$calendarManager->persist($calendarEvent);$calendarManager->flush();$md = $em->getClassMetadata(get_class($calendarEvent));$uow->computeChangeSet($md, $calendarEvent);}}}/*** This method will called on Doctrine postUpdate event*/public function postUpdate(LifecycleEventArgs $args){$this->postPersist($args);}}2) Add to services.yml:
PHP123456case.listener:class: Custom\Bundle\CaseBundle\Listener\FollowUpListenerarguments: ['@service_container', '@doctrine', @oro_activity.manager]tags:- { name: doctrine.event_listener, event: postUpdate }- { name: doctrine.event_listener, event: postPersist } - AuthorReplies
The forum ‘OroCRM – How do I? Questions’ is closed to new topics and replies.