/**
  * Perform actions about rendez-vous (insert/edit/delete/save prefs)
  *
  * @package Rendez Vous
  * @subpackage Groups
  *
  * @since Rendez Vous (1.1.0)
  *
  * @uses  Rendez_Vous_Group->is_rendez_vous()   Checks whether we're on a rendez-vous page of a group
  * @uses  rendez_vous()                         to get the plugin's instance
  * @uses  rendez_vous_handle_actions()          to insert/edit/delete/save prefs about a rendez-vous
  * @uses  bp_get_current_group_id()             to get the group id
  * @uses  Rendez_Vous_Group::group_get_option() to get the needed group metas.
  * @uses  groups_is_user_member()               to check the organizer is still a member of the group
  * @uses  delete_post_meta()                    to remove a rendez-vous from a group
  * @uses  rendez_vous_get_single_link()         to get the rendez-vous link
  * @uses  bp_core_add_message()                 to give a feedback to the user
  * @uses  do_action()                           call 'rendez_vous_groups_component_deactivated' or
  *                                                   'rendez_vous_groups_member_removed' to perform custom actions
  * @uses  bp_core_redirect()                    to safely redirect the user
  * @uses  bp_is_current_component()             to check for a BuddyPress component
  * @uses  bp_current_item()                     to make sure a group item is requested
  * @uses  bp_do_404()                           to set the WP Query to a 404.
  */
 public function group_handle_screens()
 {
     if ($this->is_rendez_vous()) {
         $rendez_vous = rendez_vous();
         $this->screen = rendez_vous_handle_actions();
         $rendez_vous->screens->screen = $this->screen;
         $group_id = bp_get_current_group_id();
         /**
          * Should we remove the rendez-vous from the group ?
          *
          * Although, this is already handled in Rendez_Vous_Group->group_rendez_vous_link()
          * an invited user can click on an email he received where the link is a group rendez-vous link.
          * @see rendez_vous_published_notification()
          *
          * Not checking if notifications are active, because there's also an edge case when the activity
          * has not been deleted yet and the user clicks on the activity link.
          */
         if ('single' == $this->screen && !empty($rendez_vous->item->id)) {
             $message = $action = false;
             // The group doesn't support rendez-vous anymore
             if (!self::group_get_option($group_id, '_rendez_vous_group_activate', false)) {
                 $message = __('The Group, the rendez-vous was attached to, does not support rendez-vous anymore', 'rendez-vous');
                 $action = 'rendez_vous_groups_component_deactivated';
                 // The organizer was removed or left the group
             } else {
                 if (!groups_is_user_member($rendez_vous->item->organizer, $group_id)) {
                     $message = sprintf(__('%s is not a member of the group, the rendez-vous was attached to, anymore. As a result, the rendez-vous was removed from the group.', 'rendez-vous'), bp_core_get_user_displayname($rendez_vous->item->organizer));
                     $action = 'rendez_vous_groups_member_removed';
                 }
             }
             // Bail if everything is ok.
             if (empty($message)) {
                 return;
             }
             // Delete the rendez-vous group id meta
             delete_post_meta($rendez_vous->item->id, '_rendez_vous_group_id');
             $redirect = rendez_vous_get_single_link($rendez_vous->item->id, $rendez_vous->item->organizer);
             bp_core_add_message($message, 'error');
             // fire an action to deal with group activities
             do_action($action, $rendez_vous->item->id, $rendez_vous->item);
             // Redirect to organizer's rendez-vous page
             bp_core_redirect($redirect);
         }
     } else {
         if (bp_is_current_component('groups') && bp_is_current_action($this->slug) && bp_current_item()) {
             bp_do_404();
             return;
         }
     }
 }
/**
 * Updated!
 *
 * @package Rendez Vous
 * @subpackage Activity
 *
 * @since Rendez Vous (1.0.0)
 */
function rendez_vous_updated_activity($id = 0, $args = array(), $notify = false, $activity = false)
{
    if (empty($id) || empty($activity)) {
        return;
    }
    $rdv = rendez_vous();
    if (empty($rdv->item->id)) {
        $rendez_vous = rendez_vous_get_item($id);
    } else {
        $rendez_vous = $rdv->item;
    }
    $rendez_vous_url = rendez_vous_get_single_link($id, $rendez_vous->organizer);
    $rendez_vous_link = '<a href="' . esc_url($rendez_vous_url) . '">' . esc_html($rendez_vous->title) . '</a>';
    $user_link = bp_core_get_userlink($rendez_vous->organizer);
    $action_part = __('updated a', 'rendez-vous');
    $action = sprintf(__('%1$s %2$s rendez-vous, %3$s', 'rendez-vous'), $user_link, $action_part, $rendez_vous_link);
    $activity_id = bp_activity_add(apply_filters('rendez_vous_updated_activity_args', array('action' => $action, 'component' => buddypress()->rendez_vous->id, 'type' => 'updated_rendez_vous', 'primary_link' => $rendez_vous_url, 'user_id' => $rendez_vous->organizer, 'item_id' => $rendez_vous->id, 'secondary_item_id' => $rendez_vous->organizer)));
    if (!empty($activity_id)) {
        bp_activity_update_meta($activity_id, 'rendez_vous_title', $rendez_vous->title);
    }
    return true;
}
/**
 * Format screen notifications
 *
 * @package Rendez Vous
 * @subpackage Notifications
 *
 * @since Rendez Vous (1.0.0)
 */
function rendez_vous_format_notifications($action, $item_id, $secondary_item_id, $total_items, $format = 'string')
{
    $bp = buddypress();
    switch ($action) {
        case 'rendez_vous_schedule':
            if ((int) $total_items > 1) {
                $rendez_vous_link = add_query_arg(array('n' => $total_items), trailingslashit(bp_loggedin_user_domain() . $bp->rendez_vous->slug . '/schedule'));
                $title = __('rendez-vous preferences updated', 'rendez-vous');
                $text = sprintf(__('%d rendez-vous preferences updated', 'rendez-vous'), (int) $total_items);
                $filter = 'rendez_vous_multiple_userset_notification';
            } else {
                $rendez_vous_link = add_query_arg(array('n' => 1), rendez_vous_get_single_link($item_id, bp_loggedin_user_id()));
                $user_fullname = bp_core_get_user_displayname($secondary_item_id, false);
                $title = __('View the rendez-vous', 'rendez-vous');
                $text = sprintf(__('%s set his preferences about a rendez-vous', 'rendez-vous'), $user_fullname);
                $filter = 'rendez_vous_single_userset_notification';
            }
            break;
        case 'rendez_vous_attend':
            if ((int) $total_items > 1) {
                $rendez_vous_link = add_query_arg(array('n' => $total_items), trailingslashit(bp_loggedin_user_domain() . $bp->rendez_vous->slug . '/attend'));
                $title = __('rendez-vous sheduled', 'rendez-vous');
                $text = sprintf(__('%d rendez-vous sheduled', 'rendez-vous'), (int) $total_items);
                $filter = 'rendez_vous_multiple_attend_notification';
            } else {
                $rendez_vous_link = add_query_arg(array('n' => 1), rendez_vous_get_single_link($item_id, $secondary_item_id));
                $user_fullname = bp_core_get_user_displayname($secondary_item_id, false);
                $title = __('View the rendez-vous', 'rendez-vous');
                $text = sprintf(__('%s scheduled a rendez-vous', 'rendez-vous'), $user_fullname);
                $filter = 'rendez_vous_single_attend_notification';
            }
            break;
        case 'rendez_vous_fixed':
            if ((int) $total_items > 1) {
                $rendez_vous_link = add_query_arg(array('n' => $total_items), trailingslashit(bp_loggedin_user_domain() . $bp->rendez_vous->slug . '/attend'));
                $title = __('rendez-vous fixed', 'rendez-vous');
                $text = sprintf(__('%d rendez-vous fixed', 'rendez-vous'), (int) $total_items);
                $filter = 'rendez_vous_multiple_fixed_notification';
            } else {
                $rendez_vous_link = add_query_arg(array('n' => 1), rendez_vous_get_single_link($item_id, $secondary_item_id));
                $user_fullname = bp_core_get_user_displayname($secondary_item_id, false);
                $title = __('View the rendez-vous', 'rendez-vous');
                $text = sprintf(__('%s fixed a rendez-vous', 'rendez-vous'), $user_fullname);
                $filter = 'rendez_vous_single_fixed_notification';
            }
            break;
        case 'rendez_vous_report':
            if ((int) $total_items > 1) {
                $rendez_vous_link = add_query_arg(array('n' => $total_items), trailingslashit(bp_loggedin_user_domain() . $bp->rendez_vous->slug . '/attend'));
                $title = __('rendez-vous report created', 'rendez-vous');
                $text = sprintf(__('%d rendez-vous reports created', 'rendez-vous'), (int) $total_items);
                $filter = 'rendez_vous_multiple_report_notification';
            } else {
                $rendez_vous_link = add_query_arg(array('n' => 1), rendez_vous_get_single_link($item_id, $secondary_item_id));
                $user_fullname = bp_core_get_user_displayname($secondary_item_id, false);
                $title = __('View the rendez-vous', 'rendez-vous');
                $text = sprintf(__('%s created a report for a rendez-vous', 'rendez-vous'), $user_fullname);
                $filter = 'rendez_vous_single_report_notification';
            }
            break;
    }
    /**
     * If on notifications read screen remove the n arguments to
     * avoid re runing the mark notification function
     */
    if (bp_is_user_notifications() && bp_is_current_action('read')) {
        $rendez_vous_link = remove_query_arg('n', $rendez_vous_link);
    }
    if ('string' == $format) {
        $return = apply_filters($filter, '<a href="' . esc_url($rendez_vous_link) . '" title="' . esc_attr($title) . '">' . esc_html($text) . '</a>', $rendez_vous_link, (int) $total_items, $item_id, $secondary_item_id);
    } else {
        $return = apply_filters($filter, array('text' => $text, 'link' => esc_url($rendez_vous_link)), $rendez_vous_link, (int) $total_items, $item_id, $secondary_item_id);
    }
    do_action('rendez_vous_format_notifications', $action, $item_id, $secondary_item_id, $total_items);
    return $return;
}
function rendez_vous_single_get_permalink()
{
    $id = rendez_vous_single_get_the_id();
    $organizer = rendez_vous()->item->organizer;
    $link = rendez_vous_get_single_link($id, $organizer);
    return apply_filters('rendez_vous_single_get_permalink', $link, $id, $organizer);
}
/**
 * Generates an iCal file using the rendez-vous datas
 *
 * @package Rendez Vous
 * @subpackage Functions
 *
 * @since Rendez Vous (1.1.0)
 *
 * @return string calendar file
 */
function rendez_vous_download_ical()
{
    $ical_page = array('is' => (bool) bp_is_current_action('schedule') && 'ical' == bp_action_variable(0), 'rdv' => (int) bp_action_variable(1));
    apply_filters('rendez_vous_download_ical', (array) $ical_page);
    if (empty($ical_page['is'])) {
        return;
    }
    $redirect = wp_get_referer();
    $user_attend = trailingslashit(bp_loggedin_user_domain() . buddypress()->rendez_vous->slug . '/attend');
    if (empty($ical_page['rdv'])) {
        bp_core_add_message(__('The rendez-vous was not found.', 'rendez-vous'), 'error');
        bp_core_redirect($redirect);
    }
    $rendez_vous = rendez_vous_get_item($ical_page['rdv']);
    // Redirect the user to the login form
    if (!is_user_logged_in()) {
        bp_core_no_access(array('redirect' => $_SERVER['REQUEST_URI']));
        return;
    }
    // Redirect if no rendez vous found
    if (empty($rendez_vous->organizer) || empty($rendez_vous->attendees)) {
        bp_core_add_message(__('The rendez-vous was not found.', 'rendez-vous'), 'error');
        bp_core_redirect($user_attend);
    }
    // Redirect if not an attendee
    if ($rendez_vous->organizer != bp_loggedin_user_id() && !in_array(bp_loggedin_user_id(), $rendez_vous->attendees)) {
        bp_core_add_message(__('You are not attending this rendez-vous.', 'rendez-vous'), 'error');
        bp_core_redirect($user_attend);
    }
    // Redirect if def date is not set
    if (empty($rendez_vous->def_date)) {
        bp_core_add_message(__('the Rendez-vous is not set yet.', 'rendez-vous'), 'error');
        bp_core_redirect($redirect);
    }
    $hourminutes = explode(':', $rendez_vous->duration);
    // Redirect if can't use the duration
    if (!is_array($hourminutes) && count($hourminutes) < 2) {
        bp_core_add_message(__('the duration is not set the right way.', 'rendez-vous'), 'error');
        bp_core_redirect($redirect);
    }
    $minutes = intval($hourminutes[1]) + intval($hourminutes[0]) * 60;
    $end_date = strtotime('+' . $minutes . ' minutes', $rendez_vous->def_date);
    // Dates are stored as UTC althought values are local, we need to reconvert
    $date_start = date_i18n('Y-m-d H:i:s', $rendez_vous->def_date, true);
    $date_end = date_i18n('Y-m-d H:i:s', $end_date, true);
    $tz_string = get_option('timezone_string');
    if (!empty($tz_string)) {
        date_default_timezone_set($tz_string);
    }
    status_header(200);
    header('Cache-Control: cache, must-revalidate');
    header('Pragma: public');
    header('Content-Description: File Transfer');
    header('Content-Disposition: attachment; filename=rendez_vous_' . $rendez_vous->id . '.ics');
    header('Content-Type: text/calendar');
    ?>
BEGIN:VCALENDAR<?php 
    echo "\n";
    ?>
VERSION:2.0<?php 
    echo "\n";
    ?>
PRODID:-//hacksw/handcal//NONSGML v1.0//EN<?php 
    echo "\n";
    ?>
CALSCALE:GREGORIAN<?php 
    echo "\n";
    ?>
BEGIN:VEVENT<?php 
    echo "\n";
    ?>
DTEND:<?php 
    echo gmdate('Ymd\\THis\\Z', strtotime($date_end));
    echo "\n";
    ?>
UID:<?php 
    echo uniqid();
    echo "\n";
    ?>
DTSTAMP:<?php 
    echo gmdate('Ymd\\THis\\Z', time());
    echo "\n";
    ?>
LOCATION:<?php 
    echo esc_html(preg_replace('/([\\,;])/', '\\\\$1', $rendez_vous->venue));
    echo "\n";
    ?>
DESCRIPTION:<?php 
    echo esc_html(preg_replace('/([\\,;])/', '\\\\$1', $rendez_vous->description));
    echo "\n";
    ?>
URL;VALUE=URI:<?php 
    echo esc_url(rendez_vous_get_single_link($rendez_vous->id, $rendez_vous->organizer));
    echo "\n";
    ?>
SUMMARY:<?php 
    echo esc_html(preg_replace('/([\\,;])/', '\\\\$1', $rendez_vous->title));
    echo "\n";
    ?>
DTSTART:<?php 
    echo gmdate('Ymd\\THis\\Z', strtotime($date_start));
    echo "\n";
    ?>
END:VEVENT<?php 
    echo "\n";
    ?>
END:VCALENDAR<?php 
    echo "\n";
    ?>
	<?php 
    exit;
}