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 object The activity for which we're generating ical.
32 * Reference to an activity object.
34 * @return \CRM_Activity_BAO_ICalendar
36 public function __construct(&$act) {
37 $this->activity
= $act;
41 * Add an ics attachment to the input array.
43 * @param array $attachments
44 * Reference to array in same format returned from CRM_Core_BAO_File::getEntityFile().
45 * @param array $contacts
46 * Array of contacts (attendees).
49 * Array index of the added attachment in the $attachments array, else NULL.
51 public function addAttachment(&$attachments, $contacts) {
52 // Check preferences setting
53 if (Civi
::settings()->get('activity_assignee_notification_ics')) {
54 $this->icsfile
= tempnam(CRM_Core_Config
::singleton()->customFileUploadDir
, 'ics');
55 if ($this->icsfile
!== FALSE) {
56 rename($this->icsfile
, $this->icsfile
. '.ics');
57 $this->icsfile
.= '.ics';
58 $icsFileName = basename($this->icsfile
);
60 // get logged in user's primary email
61 // TODO: Is there a better way to do this?
62 $organizer = $this->getPrimaryEmail();
64 $template = CRM_Core_Smarty
::singleton();
65 $template->assign('activity', $this->activity
);
66 $template->assign('organizer', $organizer);
67 $template->assign('contacts', $contacts);
68 $template->assign('timezone', date_default_timezone_get());
69 $calendar = $template->fetch('CRM/Activity/Calendar/ICal.tpl');
70 if (file_put_contents($this->icsfile
, $calendar) !== FALSE) {
71 if (empty($attachments)) {
74 $attachments['activity_ics'] = [
75 'mime_type' => 'text/calendar',
76 'fileName' => $icsFileName,
77 'cleanName' => $icsFileName,
78 'fullPath' => $this->icsfile
,
80 return 'activity_ics';
90 public function cleanup() {
91 if (!empty($this->icsfile
)) {
92 @unlink
($this->icsfile
);
97 * @todo Is there a better way to do this?
100 private function getPrimaryEmail() {
101 $uid = CRM_Core_Session
::getLoggedInContactID();
103 $emails = CRM_Core_BAO_Email
::allEmails($uid);
104 foreach ($emails as $eid => $e) {
105 if ($e['is_primary']) {
107 $primary = $e['email'];
112 if (count($emails) == 1) {
113 $primary = $e['email'];