Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
3819f101 | 19 | * Generate ical invites for activities. |
6a488035 TO |
20 | */ |
21 | class CRM_Activity_BAO_ICalendar { | |
22 | ||
23 | /** | |
51dda21e SL |
24 | * @var \CRM_Activity_BAO_ICalendar |
25 | * The activity for which we're generating ical. | |
6a488035 TO |
26 | */ |
27 | protected $activity; | |
28 | ||
29 | /** | |
fe482240 | 30 | * Constructor. |
6a488035 | 31 | * |
041ab3d1 TO |
32 | * @param object $act |
33 | * Reference to an activity object. | |
6a488035 | 34 | * |
fd31fa4c | 35 | * @return \CRM_Activity_BAO_ICalendar |
6a488035 | 36 | */ |
481a74f4 | 37 | public function __construct(&$act) { |
6a488035 TO |
38 | $this->activity = $act; |
39 | } | |
f813f78e | 40 | |
6a488035 | 41 | /** |
fe482240 | 42 | * Add an ics attachment to the input array. |
6a488035 | 43 | * |
041ab3d1 TO |
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). | |
6a488035 | 48 | * |
d60f50a8 CW |
49 | * @return string|null |
50 | * Array index of the added attachment in the $attachments array, else NULL. | |
6a488035 | 51 | */ |
481a74f4 | 52 | public function addAttachment(&$attachments, $contacts) { |
6a488035 | 53 | // Check preferences setting |
aaffa79f | 54 | if (Civi::settings()->get('activity_assignee_notification_ics')) { |
f3a87cf4 | 55 | $this->icsfile = tempnam(CRM_Core_Config::singleton()->customFileUploadDir, 'ics'); |
481a74f4 TO |
56 | if ($this->icsfile !== FALSE) { |
57 | rename($this->icsfile, $this->icsfile . '.ics'); | |
6a488035 | 58 | $this->icsfile .= '.ics'; |
481a74f4 | 59 | $icsFileName = basename($this->icsfile); |
f813f78e | 60 | |
6a488035 TO |
61 | // get logged in user's primary email |
62 | // TODO: Is there a better way to do this? | |
63 | $organizer = $this->getPrimaryEmail(); | |
f813f78e | 64 | |
6a488035 TO |
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'); | |
481a74f4 TO |
71 | if (file_put_contents($this->icsfile, $calendar) !== FALSE) { |
72 | if (empty($attachments)) { | |
96f94695 | 73 | $attachments = []; |
6a488035 | 74 | } |
96f94695 | 75 | $attachments['activity_ics'] = [ |
6a488035 TO |
76 | 'mime_type' => 'text/calendar', |
77 | 'fileName' => $icsFileName, | |
78 | 'cleanName' => $icsFileName, | |
79 | 'fullPath' => $this->icsfile, | |
96f94695 | 80 | ]; |
6a488035 TO |
81 | return 'activity_ics'; |
82 | } | |
83 | } | |
84 | } | |
e60f24eb | 85 | return NULL; |
6a488035 | 86 | } |
f813f78e | 87 | |
d60f50a8 | 88 | /** |
fe482240 | 89 | * Remove temp file. |
d60f50a8 | 90 | */ |
00be9182 | 91 | public function cleanup() { |
96f94695 | 92 | if (!empty($this->icsfile)) { |
481a74f4 | 93 | @unlink($this->icsfile); |
6a488035 TO |
94 | } |
95 | } | |
f813f78e | 96 | |
ffd93213 | 97 | /** |
7808aae6 | 98 | * @todo Is there a better way to do this? |
ffd93213 EM |
99 | * @return string |
100 | */ | |
6a488035 | 101 | private function getPrimaryEmail() { |
3bdcd4ec | 102 | $uid = CRM_Core_Session::getLoggedInContactID(); |
6a488035 | 103 | $primary = ''; |
481a74f4 TO |
104 | $emails = CRM_Core_BAO_Email::allEmails($uid); |
105 | foreach ($emails as $eid => $e) { | |
106 | if ($e['is_primary']) { | |
107 | if ($e['email']) { | |
6a488035 TO |
108 | $primary = $e['email']; |
109 | break; | |
110 | } | |
111 | } | |
f813f78e | 112 | |
481a74f4 | 113 | if (count($emails) == 1) { |
6a488035 TO |
114 | $primary = $e['email']; |
115 | break; | |
116 | } | |
117 | } | |
118 | return $primary; | |
119 | } | |
96025800 | 120 | |
6a488035 | 121 | } |