CRM-16666 - Custom participant statuses do not print in email confirmation
[civicrm-core.git] / CRM / Activity / BAO / ICalendar.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * Generate ical invites for activities
38 *
39 */
40class CRM_Activity_BAO_ICalendar {
41
42 /**
43 * @var object The activity for which we're generating ical.
44 */
45 protected $activity;
46
47 /**
fe482240 48 * Constructor.
6a488035 49 *
041ab3d1
TO
50 * @param object $act
51 * Reference to an activity object.
6a488035 52 *
fd31fa4c 53 * @return \CRM_Activity_BAO_ICalendar
6a488035 54 */
481a74f4 55 public function __construct(&$act) {
6a488035
TO
56 $this->activity = $act;
57 }
f813f78e 58
6a488035 59 /**
fe482240 60 * Add an ics attachment to the input array.
6a488035 61 *
041ab3d1
TO
62 * @param array $attachments
63 * Reference to array in same format returned from CRM_Core_BAO_File::getEntityFile().
64 * @param array $contacts
65 * Array of contacts (attendees).
6a488035 66 *
d60f50a8
CW
67 * @return string|null
68 * Array index of the added attachment in the $attachments array, else NULL.
6a488035 69 */
481a74f4 70 public function addAttachment(&$attachments, $contacts) {
6a488035 71 // Check preferences setting
481a74f4 72 if (CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'activity_assignee_notification_ics')) {
6a488035 73 $config = &CRM_Core_Config::singleton();
481a74f4
TO
74 $this->icsfile = tempnam($config->customFileUploadDir, 'ics');
75 if ($this->icsfile !== FALSE) {
76 rename($this->icsfile, $this->icsfile . '.ics');
6a488035 77 $this->icsfile .= '.ics';
481a74f4 78 $icsFileName = basename($this->icsfile);
f813f78e 79
6a488035
TO
80 // get logged in user's primary email
81 // TODO: Is there a better way to do this?
82 $organizer = $this->getPrimaryEmail();
f813f78e 83
6a488035
TO
84 $template = CRM_Core_Smarty::singleton();
85 $template->assign('activity', $this->activity);
86 $template->assign('organizer', $organizer);
87 $template->assign('contacts', $contacts);
88 $template->assign('timezone', date_default_timezone_get());
89 $calendar = $template->fetch('CRM/Activity/Calendar/ICal.tpl');
481a74f4
TO
90 if (file_put_contents($this->icsfile, $calendar) !== FALSE) {
91 if (empty($attachments)) {
6a488035
TO
92 $attachments = array();
93 }
94 $attachments['activity_ics'] = array(
95 'mime_type' => 'text/calendar',
96 'fileName' => $icsFileName,
97 'cleanName' => $icsFileName,
98 'fullPath' => $this->icsfile,
353ffa53 99 );
6a488035
TO
100 return 'activity_ics';
101 }
102 }
103 }
e60f24eb 104 return NULL;
6a488035 105 }
f813f78e 106
d60f50a8 107 /**
fe482240 108 * Remove temp file.
d60f50a8 109 */
00be9182 110 public function cleanup() {
481a74f4
TO
111 if (!empty ($this->icsfile)) {
112 @unlink($this->icsfile);
6a488035
TO
113 }
114 }
f813f78e 115
ffd93213 116 /**
d60f50a8 117 * TODO: Is there a better way to do this?
ffd93213
EM
118 * @return string
119 */
6a488035
TO
120 private function getPrimaryEmail() {
121 $session = &CRM_Core_Session::singleton();
122 $uid = $session->get('userID');
123 $primary = '';
481a74f4
TO
124 $emails = CRM_Core_BAO_Email::allEmails($uid);
125 foreach ($emails as $eid => $e) {
126 if ($e['is_primary']) {
127 if ($e['email']) {
6a488035
TO
128 $primary = $e['email'];
129 break;
130 }
131 }
f813f78e 132
481a74f4 133 if (count($emails) == 1) {
6a488035
TO
134 $primary = $e['email'];
135 break;
136 }
137 }
138 return $primary;
139 }
96025800 140
6a488035 141}