Merge pull request #17698 from agh1/checkwpbasepage
[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 \CRM_Activity_BAO_ICalendar
25 * The activity for which we're generating ical.
26 */
27 protected $activity;
28
29 /**
30 * Constructor.
31 *
32 * @param object $act
33 * Reference to an activity object.
34 *
35 * @return \CRM_Activity_BAO_ICalendar
36 */
37 public function __construct(&$act) {
38 $this->activity = $act;
39 }
40
41 /**
42 * Add an ics attachment to the input array.
43 *
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).
48 *
49 * @return string|null
50 * Array index of the added attachment in the $attachments array, else NULL.
51 */
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);
60
61 // get logged in user's primary email
62 // TODO: Is there a better way to do this?
63 $organizer = $this->getPrimaryEmail();
64
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)) {
73 $attachments = [];
74 }
75 $attachments['activity_ics'] = [
76 'mime_type' => 'text/calendar',
77 'fileName' => $icsFileName,
78 'cleanName' => $icsFileName,
79 'fullPath' => $this->icsfile,
80 ];
81 return 'activity_ics';
82 }
83 }
84 }
85 return NULL;
86 }
87
88 /**
89 * Remove temp file.
90 */
91 public function cleanup() {
92 if (!empty($this->icsfile)) {
93 @unlink($this->icsfile);
94 }
95 }
96
97 /**
98 * @todo Is there a better way to do this?
99 * @return string
100 */
101 private function getPrimaryEmail() {
102 $uid = CRM_Core_Session::getLoggedInContactID();
103 $primary = '';
104 $emails = CRM_Core_BAO_Email::allEmails($uid);
105 foreach ($emails as $eid => $e) {
106 if ($e['is_primary']) {
107 if ($e['email']) {
108 $primary = $e['email'];
109 break;
110 }
111 }
112
113 if (count($emails) == 1) {
114 $primary = $e['email'];
115 break;
116 }
117 }
118 return $primary;
119 }
120
121 }