/**
 * Email the manual payment confirmation to the client from a customisable email template.
 *
 * @since	1.3
 * @param	int		$event_id		The event ID
 * @return	void
 */
function mdjm_email_manual_payment_confirmation($event_id)
{
    if (!mdjm_get_option('manual_payment_cfm_template')) {
        return;
    }
    $mdjm_event = mdjm_get_event($event_id);
    $from_name = mdjm_email_set_from_name('manual_payment', $mdjm_event);
    $from_name = apply_filters('mdjm_email_from_name', $from_name, 'manual_payment', $mdjm_event);
    $from_email = mdjm_email_set_from_address('manual_payment', $mdjm_event);
    $from_email = apply_filters('mdjm_email_from_address', $from_email, 'manual_payment', $mdjm_event);
    $client = get_userdata($mdjm_event->client);
    $to_email = $client->user_email;
    $subject = mdjm_email_set_subject(mdjm_get_option('manual_payment_cfm_template', false));
    $subject = apply_filters('mdjm_manual_payment_subject', wp_strip_all_tags($subject));
    $subject = mdjm_do_content_tags($subject, $event_id, $mdjm_event->client);
    $attachments = apply_filters('mdjm_manual_payment_attachments', array(), $mdjm_event);
    $message = mdjm_get_email_template_content(mdjm_get_option('manual_payment_cfm_template', false));
    $message = mdjm_do_content_tags($message, $event_id, $mdjm_event->client);
    $emails = MDJM()->emails;
    $emails->__set('event_id', $mdjm_event->ID);
    $emails->__set('from_name', $from_name);
    $emails->__set('from_address', $from_email);
    $headers = apply_filters('mdjm_manual_payment_headers', $emails->get_headers(), $event_id, $mdjm_event->client);
    $emails->__set('headers', $headers);
    $emails->__set('track', apply_filters('mdjm_track_email_manual_payment', mdjm_get_option('track_client_emails')));
    if (mdjm_get_option('bcc_admin_to_client')) {
        $emails->__set('copy_to', array(mdjm_get_option('system_email')));
    }
    $emails->send($to_email, $subject, $message, $attachments, sprintf(__('Payment received confirmation for %s', 'mobile-dj-manager'), mdjm_get_label_singular()));
}
/**
 * Update the email content field with the selected template.
 *
 * @since	1.3
 * @return	void
 */
function mdjm_set_email_content_ajax()
{
    if (empty($_POST['template'])) {
        $result['type'] = 'success';
        $result['updated_content'] = '';
    } else {
        $content = mdjm_get_email_template_content($_POST['template']);
        if (!$content) {
            $result['type'] = 'error';
            $result['msg'] = __('Unable to retrieve template content', 'mobile-dj-manager');
        } else {
            $result['type'] = 'success';
            $result['updated_content'] = $content;
            $result['updated_subject'] = html_entity_decode(get_the_title($_POST['template']));
        }
    }
    $result = json_encode($result);
    echo $result;
    die;
}
/**
 * Display the send email form on the communications page.
 *
 * @since	1.3
 * @param
 * @return	Outputs the page content
 */
function mdjm_comms_page()
{
    if (!mdjm_employee_can('send_comms')) {
        wp_die('<h1>' . __('Cheatin&#8217; uh?') . '</h1>' . '<p>' . __('You do not have permission to access this page.', 'mobile-dj-manager') . '</p>', 403);
    }
    global $current_user;
    if (mdjm_employee_can('list_all_clients')) {
        $clients = mdjm_get_clients();
    } else {
        $clients = mdjm_get_employee_clients();
    }
    if (mdjm_employee_can('mdjm_employee_edit')) {
        $employees = mdjm_get_employees();
    }
    ?>
    <div class="wrap">
		<h1><?php 
    _e('Client and Employee Communications', 'mobile-dj-manager');
    ?>
</h1>
        <form name="mdjm_form_send_comms" id="mdjm_form_send_comms" method="post" enctype="multipart/form-data">
        	<?php 
    wp_nonce_field('send_comm_email', 'mdjm_nonce', true, true);
    ?>
			<?php 
    mdjm_admin_action_field('send_comm_email');
    ?>
            <input type="hidden" name="mdjm_email_from_address" id="mdjm_email_from_address" value="<?php 
    echo $current_user->user_email;
    ?>
" />
            <input type="hidden" name="mdjm_email_from_name" id="mdjm_email_from_name" value="<?php 
    echo $current_user->display_name;
    ?>
" />
            <?php 
    do_action('mdjm_pre_comms_table');
    ?>
            <table class="form-table">
                <?php 
    do_action('mdjm_add_comms_fields_before_recipient');
    ?>
                <tr>
                	<th scope="row"><label for="mdjm_email_to"><?php 
    _e('Select a Recipient', 'mobile-dj-manager');
    ?>
</label></th>
                    <td>
                    	<select name="mdjm_email_to" id="mdjm_email_to">
                        	<option value=""><?php 
    _e('Select a Recipient', 'mobile-dj-manager');
    ?>
</option>
                        	<optgroup label="<?php 
    _e('Clients', 'mobile-dj-manager');
    ?>
">
                            	<?php 
    if (empty($clients)) {
        echo '<option disabled="disabled">' . __('No Clients Found', 'mobile-dj-manager') . '</option>';
    } else {
        foreach ($clients as $client) {
            echo '<option value="' . $client->ID . '">' . $client->display_name . '</option>';
        }
    }
    ?>
                            </optgroup>
                            <?php 
    if (!empty($employees)) {
        echo '<optgroup label="' . __('Employees', 'mobile-dj-manager') . '">';
        foreach ($employees as $employee) {
            echo '<option value="' . $employee->ID . '">' . $employee->display_name . '</option>';
        }
        echo '</optgroup>';
    }
    ?>
                        </select>
                    </td>
                </tr>
                <?php 
    do_action('mdjm_add_comms_fields_before_subject');
    ?>
                <tr>
                	<th scope="row"><label for="mdjm_email_subject"><?php 
    _e('Subject', 'mobile-dj-manager');
    ?>
</label></th>
                    <td><input type="text" name="mdjm_email_subject" id="mdjm_email_subject" class="regular-text" value="<?php 
    echo isset($_GET['template']) ? esc_attr(get_the_title($_GET['template'])) : '';
    ?>
" /></td>
                </tr>
                <tr>
                	<th scope="row"><label for="mdjm_email_copy_to"><?php 
    _e('Copy Yourself?', 'mobile-dj-manager');
    ?>
</label></th>
                    <td><input type="checkbox" name="mdjm_email_copy_to" id="mdjm_email_copy_to" value="<?php 
    echo $current_user->user_email;
    ?>
" /> <span class="description"><?php 
    _e('Settings may dictate that additional email copies are also sent', 'mobile-dj-manager');
    ?>
</span></td>
                </tr>
                <?php 
    do_action('mdjm_add_comms_fields_before_template');
    ?>
            	<tr>
                	<th scope="row"><label for="mdjm_email_template"><?php 
    _e('Select a Template', 'mobile-dj-manager');
    ?>
</label></th>
                    <td>
                    	<select name="mdjm_email_template" id="mdjm_email_template">
                        	<option value="0"><?php 
    _e('No Template', 'mobile-dj-manager');
    ?>
</option>
                            <?php 
    $template = isset($_GET['template']) ? $_GET['template'] : 0;
    ?>
                        	<?php 
    echo mdjm_comms_template_options($template);
    ?>
                        </select>
                    </td>
                </tr>
                <?php 
    do_action('mdjm_add_comms_fields_before_event');
    ?>
                <tr>
                    <th scope="row"><label for="mdjm_email_event"><?php 
    printf(__('Associated %s', 'mobile-dj-manager'), mdjm_get_label_singular());
    ?>
</label></th>
                    <td>
                    	<?php 
    if (isset($_GET['event_id']) || isset($_GET['mdjm-action']) && $_GET['mdjm-action'] == 'respond_unavailable') {
        ?>
                        	<?php 
        $value = mdjm_get_event_date($_GET['event_id']) . ' ';
        $value .= __('from', 'mobile-dj-manager') . ' ';
        $value .= mdjm_get_event_start($_GET['event_id']) . ' ';
        $value .= '(' . mdjm_get_event_status($_GET['event_id']) . ')';
        ?>
							<input type="text" name="mdjm_email_event_show" id="mdjm_email_event_show" value="<?php 
        echo $value;
        ?>
" readonly="readonly" size="50" />
                            <input type="hidden" name="mdjm_email_event" id="mdjm_email_event" value="<?php 
        echo $_GET['event_id'];
        ?>
" />
                        <?php 
    } else {
        ?>
                            <select name="mdjm_email_event" id="mdjm_email_event">
                            <option value="0"><?php 
        _e('Select an Event', 'mobile-dj-manager');
        ?>
</option>
                            </select>
                        <?php 
    }
    ?>
                        <p class="description"><?php 
    printf(__('If no %s is selected <code>{event_*}</code> content tags may not be used', 'mobile-dj-manager'), mdjm_get_label_singular(true));
    ?>
</p>
                    </td>
                </tr>
                <?php 
    do_action('mdjm_add_comms_fields_before_file');
    ?>
                <tr>
                	<th scope="row"><label for="mdjm_email_upload_file"><?php 
    _e('Attach a File', 'mobile-dj-manager');
    ?>
</label></th>
                    <td><input type="file" name="mdjm_email_upload_file" id="mdjm_email_upload_file" class="regular-text" value="" />
                    	<p class="description"><?php 
    printf(__('Max file size %dMB. Change php.ini <code>post_max_size</code> to increase', 'mobile-dj-manager'), ini_get('post_max_size'));
    ?>
</p>
                    </td>
                </tr>
                <?php 
    do_action('mdjm_add_comms_fields_before_content');
    ?>
                <tr>
                    <td colspan="2">
                        <?php 
    $content = isset($_GET['template']) ? mdjm_get_email_template_content($_GET['template']) : '';
    wp_editor($content, 'mdjm_email_content', array('media_buttons' => true, 'textarea_rows' => '10', 'editor_class' => 'required'));
    ?>
                    </td>
                </tr>
            </table>
            <?php 
    do_action('mdjm_post_comms_table');
    ?>
            <?php 
    submit_button(__('Send Email', 'mobile-dj-manager'), 'primary', 'submit', true);
    ?>
        </form>
    </div>
    <?php 
}