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