/**
 * Append a link to download the iCalendar file of the rendez-vous
 *
 * If for some reason, the dates/hours are not consistent, simply use
 * remove_filter( 'rendez_vous_single_get_the_date', 'rendez_vous_append_ical_link' );
 * till i'll fix the issue ;)
 *
 * @package Rendez Vous
 * @subpackage Filters
 *
 * @since Rendez Vous (1.1.0)
 *
 * @param  string $output      the definitive date output for the rendez-vous
 * @param  Rendez_Vous_Item $rendez_vous the rendez-vous object
 * @return string              HTML Output
 */
function rendez_vous_append_ical_link($output = '', $rendez_vous = null)
{
    if (empty($output) || empty($rendez_vous)) {
        return $output;
    }
    if (bp_loggedin_user_id() != $rendez_vous->organizer && !in_array(bp_loggedin_user_id(), $rendez_vous->attendees)) {
        return $output;
    }
    $output .= ' <a href="' . esc_url(rendez_vous_get_ical_link($rendez_vous->id, $rendez_vous->organizer)) . '" title="' . esc_attr__('Download the iCal file', 'rendez-vous') . '" class="ical-link"><span></span></a>';
    return $output;
}
/**
 * Send a screen notifications & email one when a rendez vous is updated
 *
 * Date set or report created
 *
 * @package Rendez Vous
 * @subpackage Notifications
 *
 * @since Rendez Vous (1.0.0)
 */
function rendez_vous_updated_notification($id = 0, $args = array(), $notify = 0)
{
    $bp = buddypress();
    $rdv = rendez_vous();
    if (empty($id) || empty($notify) || empty($rdv->item) || $id != $rdv->item->id || empty($args['message'])) {
        return;
    }
    $has_updated = !empty($rdv->item->date_fixed) || !empty($rdv->item->report_created) ? true : false;
    if (empty($has_updated) && empty($args['message'])) {
        return;
    }
    // Only allow 1 message per day.
    if (empty($has_updated) && 1 == get_transient('rendez_vous_last_message_' . $id)) {
        return;
    }
    $rendez_vous = $rdv->item;
    $attendees = $rendez_vous->attendees;
    if (empty($attendees)) {
        return;
    }
    // Remove the organizer
    if (in_array($rendez_vous->organizer, $attendees)) {
        $attendees = array_diff($attendees, array($rendez_vous->organizer));
    }
    $organizer_name = bp_core_get_user_displayname($rendez_vous->organizer);
    $rendez_vous_link = rendez_vous_get_single_link($id, $rendez_vous->organizer);
    $rendez_vous_content = stripslashes($rendez_vous->title);
    $rdv_updated_action = false;
    $component_action = false;
    if (!empty($rdv->item->date_fixed)) {
        if (is_numeric($rdv->item->date_fixed)) {
            $rendez_vous_content .= "\n\n" . sprintf(__('Date fixed to %1$s at %2$s', 'rendez-vous'), date_i18n(get_option('date_format'), $rdv->item->date_fixed), date_i18n(get_option('time_format'), $rdv->item->date_fixed));
            $rendez_vous_content .= "\n\n" . sprintf(__('Download the Calendar file %s', 'rendez-vous'), rendez_vous_get_ical_link($id, $rendez_vous->organizer));
        } else {
            $rendez_vous_content .= "\n\n" . sprintf(__('Date fixed to %s', 'rendez-vous'), esc_html($rdv->item->date_fixed));
        }
        $rdv_updated_action = __('fixed the date', 'rendez-vous');
        $component_action = 'rendez_vous_fixed';
    } else {
        if (!empty($rdv->item->report_created)) {
            $rdv_updated_action = __('created the report', 'rendez-vous');
            $component_action = 'rendez_vous_report';
        } else {
            if (!empty($args['message'])) {
                $rdv_updated_action = __('sent a message', 'rendez-vous');
                $component_action = 'rendez_vous_message';
                set_transient('rendez_vous_last_message_' . $id, 1, 24 * HOUR_IN_SECONDS);
            }
        }
    }
    $organizer_name = stripslashes($organizer_name);
    // Append the custom message, if any
    if (!empty($args['message'])) {
        $rendez_vous_content .= "\n\n" . $args['message'];
    }
    $rendez_vous_content = wp_kses($rendez_vous_content, array());
    $message = sprintf(__("%s %s for the rendez-vous: %s\n\nTo view details, log in and visit: %s\n\n---------------------\n", 'rendez-vous'), $organizer_name, $rdv_updated_action, $rendez_vous_content, esc_url($rendez_vous_link));
    $subject = bp_get_email_subject(array('text' => sprintf(__('%s updated a rendez-vous', 'rendez-vous'), $organizer_name)));
    // This way we'll have all needed users data at one and we will avoid spam users
    $users = bp_core_get_users(array('type' => 'alphabetical', 'include' => implode(',', $attendees)));
    foreach ($users['users'] as $attendee) {
        $mail_content = $message;
        if ('rendez_vous_message' != $component_action) {
            // Screen Notification
            bp_notifications_add_notification(array('user_id' => $attendee->ID, 'item_id' => $id, 'secondary_item_id' => $rendez_vous->organizer, 'component_name' => $bp->rendez_vous->id, 'component_action' => $component_action));
        }
        // Sending Emails
        if ('no' == get_user_meta($attendee->ID, 'notification_rendez_vous_attend', true)) {
            continue;
        }
        $user_settings = false;
        $user_fullname = !empty($attendee->fullname) ? $attendee->fullname : $attendee->display_name;
        $user_profile = bp_core_get_user_domain($attendee->ID, $attendee->user_nicename, $attendee->user_login);
        $user_email = $attendee->user_email;
        // Set up and send the message
        $to = $attendee->user_email;
        // Only show the disable notifications line if the settings component is enabled
        if (bp_is_active('settings')) {
            $user_settings = trailingslashit($user_profile . bp_get_settings_slug() . '/notifications');
            $mail_content .= sprintf(__('To disable these notifications please log in and go to: %s', 'rendez-vous'), esc_url($user_settings));
        }
        /* Send the message */
        $to = apply_filters('rendez_vous_updated_notify_attendee', $to);
        $subject = apply_filters('rendez_vous_updated_notify_subject', $subject, $organizer_name);
        $mail_content = apply_filters('rendez_vous_updated_notify_message', $mail_content, $rendez_vous, $organizer_name, $rendez_vous_content, $rendez_vous_link, $user_settings);
        wp_mail($to, $subject, $mail_content);
    }
}