activity = $act; } /** * Add an ics attachment to the input array * * @param array $attachments Reference to array in same format returned from CRM_Core_BAO_File::getEntityFile() * @param array $contacts Array of contacts (attendees) * * @return string Array index of the added attachment in the $attachments array, or else null. * @access public */ function addAttachment( &$attachments, $contacts ) { // Check preferences setting if ( CRM_Core_BAO_Setting::getItem( CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'activity_assignee_notification_ics' ) ) { $config = &CRM_Core_Config::singleton(); $this->icsfile = tempnam( $config->customFileUploadDir, 'ics' ); if ( $this->icsfile !== FALSE ) { rename( $this->icsfile, $this->icsfile . '.ics' ); $this->icsfile .= '.ics'; $icsFileName = basename( $this->icsfile ); // get logged in user's primary email // TODO: Is there a better way to do this? $organizer = $this->getPrimaryEmail(); $template = CRM_Core_Smarty::singleton(); $template->assign('activity', $this->activity); $template->assign('organizer', $organizer); $template->assign('contacts', $contacts); $template->assign('timezone', date_default_timezone_get()); $calendar = $template->fetch('CRM/Activity/Calendar/ICal.tpl'); if ( file_put_contents( $this->icsfile, $calendar ) !== FALSE ) { if ( empty( $attachments ) ) { $attachments = array(); } $attachments['activity_ics'] = array( 'mime_type' => 'text/calendar', 'fileName' => $icsFileName, 'cleanName' => $icsFileName, 'fullPath' => $this->icsfile, ); return 'activity_ics'; } } } return null; } function cleanup() { if ( !empty ( $this->icsfile ) ) { @unlink( $this->icsfile ); } } // TODO: Is there a better way to do this? private function getPrimaryEmail() { $session = &CRM_Core_Session::singleton(); $uid = $session->get('userID'); $primary = ''; $emails = CRM_Core_BAO_Email::allEmails( $uid ); foreach ( $emails as $eid => $e ) { if ( $e['is_primary'] ) { if ( $e['email'] ) { $primary = $e['email']; break; } } if ( count($emails) == 1 ) { $primary = $e['email']; break; } } return $primary; } }