Merge pull request #15987 from seamuslee001/master_metadata
[civicrm-core.git] / CRM / Activity / BAO / ICalendar.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Generate ical invites for activities.
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 /**
29 * Constructor.
30 *
31 * @param object $act
32 * Reference to an activity object.
33 *
34 * @return \CRM_Activity_BAO_ICalendar
35 */
36 public function __construct(&$act) {
37 $this->activity = $act;
38 }
39
40 /**
41 * Add an ics attachment to the input array.
42 *
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).
47 *
48 * @return string|null
49 * Array index of the added attachment in the $attachments array, else NULL.
50 */
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);
59
60 // get logged in user's primary email
61 // TODO: Is there a better way to do this?
62 $organizer = $this->getPrimaryEmail();
63
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)) {
72 $attachments = [];
73 }
74 $attachments['activity_ics'] = [
75 'mime_type' => 'text/calendar',
76 'fileName' => $icsFileName,
77 'cleanName' => $icsFileName,
78 'fullPath' => $this->icsfile,
79 ];
80 return 'activity_ics';
81 }
82 }
83 }
84 return NULL;
85 }
86
87 /**
88 * Remove temp file.
89 */
90 public function cleanup() {
91 if (!empty($this->icsfile)) {
92 @unlink($this->icsfile);
93 }
94 }
95
96 /**
97 * @todo Is there a better way to do this?
98 * @return string
99 */
100 private function getPrimaryEmail() {
101 $uid = CRM_Core_Session::getLoggedInContactID();
102 $primary = '';
103 $emails = CRM_Core_BAO_Email::allEmails($uid);
104 foreach ($emails as $eid => $e) {
105 if ($e['is_primary']) {
106 if ($e['email']) {
107 $primary = $e['email'];
108 break;
109 }
110 }
111
112 if (count($emails) == 1) {
113 $primary = $e['email'];
114 break;
115 }
116 }
117 return $primary;
118 }
119
120 }