3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * Generate ical invites for activities.
21 class CRM_Activity_BAO_ICalendar
{
24 * @var \CRM_Activity_BAO_ICalendar
25 * The activity for which we're generating ical.
33 * Reference to an activity object.
35 * @return \CRM_Activity_BAO_ICalendar
37 public function __construct(&$act) {
38 $this->activity
= $act;
42 * Add an ics attachment to the input array.
44 * @param array $attachments
45 * Reference to array in same format returned from CRM_Core_BAO_File::getEntityFile().
46 * @param array $contacts
47 * Array of contacts (attendees).
50 * Array index of the added attachment in the $attachments array, else NULL.
52 public function addAttachment(&$attachments, $contacts) {
53 // Check preferences setting
54 if (Civi
::settings()->get('activity_assignee_notification_ics')) {
55 $this->icsfile
= tempnam(CRM_Core_Config
::singleton()->customFileUploadDir
, 'ics');
56 if ($this->icsfile
!== FALSE) {
57 rename($this->icsfile
, $this->icsfile
. '.ics');
58 $this->icsfile
.= '.ics';
59 $icsFileName = basename($this->icsfile
);
61 // get logged in user's primary email
62 // TODO: Is there a better way to do this?
63 $organizer = $this->getPrimaryEmail();
65 $template = CRM_Core_Smarty
::singleton();
66 $template->assign('activity', $this->activity
);
67 $template->assign('organizer', $organizer);
68 $template->assign('contacts', $contacts);
69 $template->assign('timezone', date_default_timezone_get());
70 $calendar = $template->fetch('CRM/Activity/Calendar/ICal.tpl');
71 if (file_put_contents($this->icsfile
, $calendar) !== FALSE) {
72 if (empty($attachments)) {
75 $attachments['activity_ics'] = [
76 'mime_type' => 'text/calendar',
77 'fileName' => $icsFileName,
78 'cleanName' => $icsFileName,
79 'fullPath' => $this->icsfile
,
81 return 'activity_ics';
91 public function cleanup() {
92 if (!empty($this->icsfile
)) {
93 @unlink
($this->icsfile
);
98 * @todo Is there a better way to do this?
101 private function getPrimaryEmail() {
102 $uid = CRM_Core_Session
::getLoggedInContactID();
104 $emails = CRM_Core_BAO_Email
::allEmails($uid);
105 foreach ($emails as $eid => $e) {
106 if ($e['is_primary']) {
108 $primary = $e['email'];
113 if (count($emails) == 1) {
114 $primary = $e['email'];