3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2018
35 * Generate ical invites for activities.
37 class CRM_Activity_BAO_ICalendar
{
40 * @var object The activity for which we're generating ical.
48 * Reference to an activity object.
50 * @return \CRM_Activity_BAO_ICalendar
52 public function __construct(&$act) {
53 $this->activity
= $act;
57 * Add an ics attachment to the input array.
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).
65 * Array index of the added attachment in the $attachments array, else NULL.
67 public function addAttachment(&$attachments, $contacts) {
68 // Check preferences setting
69 if (Civi
::settings()->get('activity_assignee_notification_ics')) {
70 $config = &CRM_Core_Config
::singleton();
71 $this->icsfile
= tempnam($config->customFileUploadDir
, 'ics');
72 if ($this->icsfile
!== FALSE) {
73 rename($this->icsfile
, $this->icsfile
. '.ics');
74 $this->icsfile
.= '.ics';
75 $icsFileName = basename($this->icsfile
);
77 // get logged in user's primary email
78 // TODO: Is there a better way to do this?
79 $organizer = $this->getPrimaryEmail();
81 $template = CRM_Core_Smarty
::singleton();
82 $template->assign('activity', $this->activity
);
83 $template->assign('organizer', $organizer);
84 $template->assign('contacts', $contacts);
85 $template->assign('timezone', date_default_timezone_get());
86 $calendar = $template->fetch('CRM/Activity/Calendar/ICal.tpl');
87 if (file_put_contents($this->icsfile
, $calendar) !== FALSE) {
88 if (empty($attachments)) {
89 $attachments = array();
91 $attachments['activity_ics'] = array(
92 'mime_type' => 'text/calendar',
93 'fileName' => $icsFileName,
94 'cleanName' => $icsFileName,
95 'fullPath' => $this->icsfile
,
97 return 'activity_ics';
107 public function cleanup() {
108 if (!empty ($this->icsfile
)) {
109 @unlink
($this->icsfile
);
114 * @todo Is there a better way to do this?
117 private function getPrimaryEmail() {
118 $uid = CRM_Core_Session
::getLoggedInContactID();
120 $emails = CRM_Core_BAO_Email
::allEmails($uid);
121 foreach ($emails as $eid => $e) {
122 if ($e['is_primary']) {
124 $primary = $e['email'];
129 if (count($emails) == 1) {
130 $primary = $e['email'];