| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.5 | |
| 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 Reference to an activity object |
| 51 | * |
| 52 | * @return void |
| 53 | * @access public |
| 54 | */ |
| 55 | function __construct( &$act ) { |
| 56 | $this->activity = $act; |
| 57 | } |
| 58 | |
| 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. |
| 66 | * @access public |
| 67 | */ |
| 68 | function addAttachment( &$attachments, $contacts ) { |
| 69 | // Check preferences setting |
| 70 | if ( CRM_Core_BAO_Setting::getItem( CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'activity_assignee_notification_ics' ) ) { |
| 71 | $config = &CRM_Core_Config::singleton(); |
| 72 | $this->icsfile = tempnam( $config->customFileUploadDir, 'ics' ); |
| 73 | if ( $this->icsfile !== FALSE ) { |
| 74 | rename( $this->icsfile, $this->icsfile . '.ics' ); |
| 75 | $this->icsfile .= '.ics'; |
| 76 | $icsFileName = basename( $this->icsfile ); |
| 77 | |
| 78 | // get logged in user's primary email |
| 79 | // TODO: Is there a better way to do this? |
| 80 | $organizer = $this->getPrimaryEmail(); |
| 81 | |
| 82 | $template = CRM_Core_Smarty::singleton(); |
| 83 | $template->assign('activity', $this->activity); |
| 84 | $template->assign('organizer', $organizer); |
| 85 | $template->assign('contacts', $contacts); |
| 86 | $template->assign('timezone', date_default_timezone_get()); |
| 87 | $calendar = $template->fetch('CRM/Activity/Calendar/ICal.tpl'); |
| 88 | if ( file_put_contents( $this->icsfile, $calendar ) !== FALSE ) { |
| 89 | if ( empty( $attachments ) ) { |
| 90 | $attachments = array(); |
| 91 | } |
| 92 | $attachments['activity_ics'] = array( |
| 93 | 'mime_type' => 'text/calendar', |
| 94 | 'fileName' => $icsFileName, |
| 95 | 'cleanName' => $icsFileName, |
| 96 | 'fullPath' => $this->icsfile, |
| 97 | ); |
| 98 | return 'activity_ics'; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | return null; |
| 103 | } |
| 104 | |
| 105 | function cleanup() { |
| 106 | if ( !empty ( $this->icsfile ) ) { |
| 107 | @unlink( $this->icsfile ); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // TODO: Is there a better way to do this? |
| 112 | private function getPrimaryEmail() { |
| 113 | $session = &CRM_Core_Session::singleton(); |
| 114 | $uid = $session->get('userID'); |
| 115 | $primary = ''; |
| 116 | $emails = CRM_Core_BAO_Email::allEmails( $uid ); |
| 117 | foreach ( $emails as $eid => $e ) { |
| 118 | if ( $e['is_primary'] ) { |
| 119 | if ( $e['email'] ) { |
| 120 | $primary = $e['email']; |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | if ( count($emails) == 1 ) { |
| 126 | $primary = $e['email']; |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | return $primary; |
| 131 | } |
| 132 | } |
| 133 | |