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