Cleanup deprecated CRM_Core_BAO_Settings calls CRM-17507
[civicrm-core.git] / CRM / Activity / BAO / ICalendar.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
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 */
33
34/**
3819f101 35 * Generate ical invites for activities.
6a488035
TO
36 */
37class CRM_Activity_BAO_ICalendar {
38
39 /**
40 * @var object The activity for which we're generating ical.
41 */
42 protected $activity;
43
44 /**
fe482240 45 * Constructor.
6a488035 46 *
041ab3d1
TO
47 * @param object $act
48 * Reference to an activity object.
6a488035 49 *
fd31fa4c 50 * @return \CRM_Activity_BAO_ICalendar
6a488035 51 */
481a74f4 52 public function __construct(&$act) {
6a488035
TO
53 $this->activity = $act;
54 }
f813f78e 55
6a488035 56 /**
fe482240 57 * Add an ics attachment to the input array.
6a488035 58 *
041ab3d1
TO
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).
6a488035 63 *
d60f50a8
CW
64 * @return string|null
65 * Array index of the added attachment in the $attachments array, else NULL.
6a488035 66 */
481a74f4 67 public function addAttachment(&$attachments, $contacts) {
6a488035 68 // Check preferences setting
aaffa79f 69 if (Civi::settings()->get('activity_assignee_notification_ics')) {
6a488035 70 $config = &CRM_Core_Config::singleton();
481a74f4
TO
71 $this->icsfile = tempnam($config->customFileUploadDir, 'ics');
72 if ($this->icsfile !== FALSE) {
73 rename($this->icsfile, $this->icsfile . '.ics');
6a488035 74 $this->icsfile .= '.ics';
481a74f4 75 $icsFileName = basename($this->icsfile);
f813f78e 76
6a488035
TO
77 // get logged in user's primary email
78 // TODO: Is there a better way to do this?
79 $organizer = $this->getPrimaryEmail();
f813f78e 80
6a488035
TO
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');
481a74f4
TO
87 if (file_put_contents($this->icsfile, $calendar) !== FALSE) {
88 if (empty($attachments)) {
6a488035
TO
89 $attachments = array();
90 }
91 $attachments['activity_ics'] = array(
92 'mime_type' => 'text/calendar',
93 'fileName' => $icsFileName,
94 'cleanName' => $icsFileName,
95 'fullPath' => $this->icsfile,
353ffa53 96 );
6a488035
TO
97 return 'activity_ics';
98 }
99 }
100 }
e60f24eb 101 return NULL;
6a488035 102 }
f813f78e 103
d60f50a8 104 /**
fe482240 105 * Remove temp file.
d60f50a8 106 */
00be9182 107 public function cleanup() {
481a74f4
TO
108 if (!empty ($this->icsfile)) {
109 @unlink($this->icsfile);
6a488035
TO
110 }
111 }
f813f78e 112
ffd93213 113 /**
7808aae6 114 * @todo Is there a better way to do this?
ffd93213
EM
115 * @return string
116 */
6a488035
TO
117 private function getPrimaryEmail() {
118 $session = &CRM_Core_Session::singleton();
119 $uid = $session->get('userID');
120 $primary = '';
481a74f4
TO
121 $emails = CRM_Core_BAO_Email::allEmails($uid);
122 foreach ($emails as $eid => $e) {
123 if ($e['is_primary']) {
124 if ($e['email']) {
6a488035
TO
125 $primary = $e['email'];
126 break;
127 }
128 }
f813f78e 129
481a74f4 130 if (count($emails) == 1) {
6a488035
TO
131 $primary = $e['email'];
132 break;
133 }
134 }
135 return $primary;
136 }
96025800 137
6a488035 138}