Merge pull request #14326 from civicrm/5.14
[civicrm-core.git] / CRM / Activity / BAO / ICalendar.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33
34 /**
35 * Generate ical invites for activities.
36 */
37 class CRM_Activity_BAO_ICalendar {
38
39 /**
40 * @var object The activity for which we're generating ical.
41 */
42 protected $activity;
43
44 /**
45 * Constructor.
46 *
47 * @param object $act
48 * Reference to an activity object.
49 *
50 * @return \CRM_Activity_BAO_ICalendar
51 */
52 public function __construct(&$act) {
53 $this->activity = $act;
54 }
55
56 /**
57 * Add an ics attachment to the input array.
58 *
59 * @param array $attachments
60 * Reference to array in same format returned from CRM_Core_BAO_File::getEntityFile().
61 * @param array $contacts
62 * Array of contacts (attendees).
63 *
64 * @return string|null
65 * Array index of the added attachment in the $attachments array, else NULL.
66 */
67 public function addAttachment(&$attachments, $contacts) {
68 // Check preferences setting
69 if (Civi::settings()->get('activity_assignee_notification_ics')) {
70 $this->icsfile = tempnam(CRM_Core_Config::singleton()->customFileUploadDir, 'ics');
71 if ($this->icsfile !== FALSE) {
72 rename($this->icsfile, $this->icsfile . '.ics');
73 $this->icsfile .= '.ics';
74 $icsFileName = basename($this->icsfile);
75
76 // get logged in user's primary email
77 // TODO: Is there a better way to do this?
78 $organizer = $this->getPrimaryEmail();
79
80 $template = CRM_Core_Smarty::singleton();
81 $template->assign('activity', $this->activity);
82 $template->assign('organizer', $organizer);
83 $template->assign('contacts', $contacts);
84 $template->assign('timezone', date_default_timezone_get());
85 $calendar = $template->fetch('CRM/Activity/Calendar/ICal.tpl');
86 if (file_put_contents($this->icsfile, $calendar) !== FALSE) {
87 if (empty($attachments)) {
88 $attachments = [];
89 }
90 $attachments['activity_ics'] = [
91 'mime_type' => 'text/calendar',
92 'fileName' => $icsFileName,
93 'cleanName' => $icsFileName,
94 'fullPath' => $this->icsfile,
95 ];
96 return 'activity_ics';
97 }
98 }
99 }
100 return NULL;
101 }
102
103 /**
104 * Remove temp file.
105 */
106 public function cleanup() {
107 if (!empty($this->icsfile)) {
108 @unlink($this->icsfile);
109 }
110 }
111
112 /**
113 * @todo Is there a better way to do this?
114 * @return string
115 */
116 private function getPrimaryEmail() {
117 $uid = CRM_Core_Session::getLoggedInContactID();
118 $primary = '';
119 $emails = CRM_Core_BAO_Email::allEmails($uid);
120 foreach ($emails as $eid => $e) {
121 if ($e['is_primary']) {
122 if ($e['email']) {
123 $primary = $e['email'];
124 break;
125 }
126 }
127
128 if (count($emails) == 1) {
129 $primary = $e['email'];
130 break;
131 }
132 }
133 return $primary;
134 }
135
136 }