Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
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 Reference to an activity object | |
51 | * | |
fd31fa4c EM |
52 | * @return \CRM_Activity_BAO_ICalendar |
53 | @access public | |
6a488035 | 54 | */ |
00be9182 | 55 | public function __construct( &$act ) { |
6a488035 TO |
56 | $this->activity = $act; |
57 | } | |
f813f78e | 58 | |
6a488035 TO |
59 | /** |
60 | * Add an ics attachment to the input array | |
61 | * | |
62 | * @param array $attachments Reference to array in same format returned from CRM_Core_BAO_File::getEntityFile() | |
63 | * @param array $contacts Array of contacts (attendees) | |
64 | * | |
65 | * @return string Array index of the added attachment in the $attachments array, or else null. | |
6a488035 | 66 | */ |
00be9182 | 67 | public function addAttachment( &$attachments, $contacts ) { |
6a488035 TO |
68 | // Check preferences setting |
69 | if ( CRM_Core_BAO_Setting::getItem( CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, '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 ); | |
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'); | |
f813f78e | 87 | if ( file_put_contents( $this->icsfile, $calendar ) !== FALSE ) { |
6a488035 TO |
88 | if ( empty( $attachments ) ) { |
89 | $attachments = array(); | |
90 | } | |
91 | $attachments['activity_ics'] = array( | |
92 | 'mime_type' => 'text/calendar', | |
93 | 'fileName' => $icsFileName, | |
94 | 'cleanName' => $icsFileName, | |
95 | 'fullPath' => $this->icsfile, | |
96 | ); | |
97 | return 'activity_ics'; | |
98 | } | |
99 | } | |
100 | } | |
101 | return null; | |
102 | } | |
f813f78e | 103 | |
00be9182 | 104 | public function cleanup() { |
6a488035 TO |
105 | if ( !empty ( $this->icsfile ) ) { |
106 | @unlink( $this->icsfile ); | |
107 | } | |
108 | } | |
f813f78e | 109 | |
6a488035 | 110 | // TODO: Is there a better way to do this? |
ffd93213 EM |
111 | /** |
112 | * @return string | |
113 | */ | |
6a488035 TO |
114 | private function getPrimaryEmail() { |
115 | $session = &CRM_Core_Session::singleton(); | |
116 | $uid = $session->get('userID'); | |
117 | $primary = ''; | |
118 | $emails = CRM_Core_BAO_Email::allEmails( $uid ); | |
119 | foreach ( $emails as $eid => $e ) { | |
120 | if ( $e['is_primary'] ) { | |
121 | if ( $e['email'] ) { | |
122 | $primary = $e['email']; | |
123 | break; | |
124 | } | |
125 | } | |
f813f78e | 126 | |
6a488035 TO |
127 | if ( count($emails) == 1 ) { |
128 | $primary = $e['email']; | |
129 | break; | |
130 | } | |
131 | } | |
132 | return $primary; | |
133 | } | |
134 | } | |
135 |