<?php
/*
Template Name: Dashboard Petition
*/

/**
 * @package WordPress
 * @subpackage Campoal
 */

global $campoal_options;
global $post;
global $current_user;

// Check user is loggedin or not, if not redirect to login page
conikal_redirect_login();

// common donation form 
$common_donation_form  	= isset($campoal_options['conikal_common_donation_form_field']) ? $campoal_options['conikal_common_donation_form_field'] : 0;
$common_form_id     	= isset($campoal_options['conikal_common_form_id_field']) ? $campoal_options['conikal_common_form_id_field'] : 0;

// Export signatures file
$export_id = isset($_GET['export_id']) ? $_GET['export_id'] : '';
$type = isset($_GET['download']) ? sanitize_text_field($_GET['download']) : 'pdf';
$reason = isset($_GET['reason']) ? sanitize_text_field($_GET['reason']) : 'yes';

if (!empty($export_id) && ($type == 'pdf' || $type == 'csv')) {
    $post_author_id = get_post_field( 'post_author', $export_id );
    $co_authors = get_post_meta($export_id, 'petition_authors', true);
    if ($post_author_id == $current_user->ID || current_user_can('editor') || current_user_can('administrator') || ($co_authors && in_array($current_user->ID, $co_authors) ) ) {
        conikal_signatures_export($export_id, $type, $reason);
        exit;
    }
}

// get params
$section = isset($_GET['section']) ? sanitize_text_field($_GET['section']) : 'dashboard';
$edit_id = isset($_GET['id']) ? sanitize_text_field($_GET['id']) : '';
$form_id = isset($_GET['form_id']) ? sanitize_text_field($_GET['form_id']) : '';
$update_id = isset($_GET['update_id']) ? sanitize_text_field($_GET['update_id']) : '';

// Validate and save the submitted form data created by the acf_form() function
if (class_exists('ACF') && $section == 'edit_petition') {
   acf_form_head(); 
}

get_header();

$number_sign_change_goal = isset($campoal_options['conikal_number_sign_change_field']) ? $campoal_options['conikal_number_sign_change_field'] : 0;
$auto_increase_goal = isset($campoal_options['conikal_auto_increase_goal_field']) ? $campoal_options['conikal_auto_increase_goal_field'] : 0;
$auto_increase_goal_default = isset($campoal_options['conikal_auto_increase_goal_d_field']) ? $campoal_options['conikal_auto_increase_goal_d_field'] : '';
$page_dashboard_petition = isset($campoal_options['conikal_page_dashboard_petition_field']) ? $campoal_options['conikal_page_dashboard_petition_field'] : 0;
$page_dashboard_petition = $page_dashboard_petition ? get_permalink($page_dashboard_petition) : '';

if (!empty($edit_id)) {
    $link = get_permalink($edit_id);
    $post_type = get_post_type($edit_id);
} else {
    $edit_id = get_post_meta($update_id, 'update_post_id', true);
    $link = get_permalink($edit_id);
}
$post_author_id = get_post_field( 'post_author', $edit_id );

// get form id
$form_id = get_post_meta($edit_id, 'petition_contribute', true);
$contribute_approve = get_post_status($form_id);
if ($contribute_approve === 'pending') {
    $form_id = '';
}

?>
<?php if ( $edit_id != '' || $update_id != '' && $form_id != '' && ($post_author_id == $current_user->ID || current_user_can('editor') || current_user_can('administrator') || conikal_co_author_permission($edit_id)) ) { ?>
    <div id="wrapper" class="wrapper petition">
        <div class="color silver">
            <div class="ui large secondary pointing menu" id="control-menu">
                <div class="ui container">
                        <!-- Campaign link -->
                        <a href="<?php echo get_permalink($edit_id) ?>" class="item" data-bjax><?php esc_html_e('Campaign', 'campoal') ?></a>

                        <!-- Dashboard link -->
                        <?php if ($page_dashboard_petition) {
                            $page_dashboard = add_query_arg(array('section' => 'dashboard', 'id' => $edit_id), $page_dashboard_petition); ?>
                            <a class="<?php echo esc_attr($section == 'dashboard' ? 'active ' : '') ?>item" href="<?php echo esc_url($page_dashboard); ?>" data-bjax><?php esc_html_e('Dashboard', 'campoal') ?></a>
                        <?php } ?>

                        <!-- Supporters list -->
                        <?php if ($page_dashboard_petition && $post_type == 'petition') {
                            $page_dashboard = add_query_arg(array('section' => 'supporters', 'id' => $edit_id, 'type' => 'supporter'), $page_dashboard_petition); ?>
                            <a class="<?php echo esc_attr($section == 'supporters' ? 'active ' : '') ?>item" href="<?php echo esc_url($page_dashboard); ?>" data-bjax><?php esc_html_e('Supporters', 'campoal') ?></a>
                        <?php } ?>

                        <!-- Donors list -->
                        <?php if (class_exists('Give') && empty($common_donation_form) && empty($common_form_id) && !empty($form_id) || $post_type == 'give_forms') {
                            $page_dashboard = add_query_arg(array('section' => 'donors', 'id' => $edit_id, 'type' => 'donor'), $page_dashboard_petition); ?>
                            <a class="<?php echo esc_attr($section == 'donors' ? 'active ' : '') ?>item" href="<?php echo esc_url($page_dashboard); ?>" data-bjax><?php esc_html_e('Donors', 'campoal') ?></a>
                        <?php } ?>

                        <!-- Update petition link -->
                        <?php if ($page_dashboard_petition) {
                            $page_add_update = add_query_arg(array('section' => 'add_update', 'id' => $edit_id, 'type' => 'update'), $page_dashboard_petition); ?>
                            <a class="<?php echo esc_attr($section == 'add_update' ? 'active ' : '') ?>item" href="<?php echo esc_url($page_add_update); ?>" data-bjax><?php esc_html_e('Add Update', 'campoal') ?></a>
                        <?php } ?>

                        <!-- Contribute link -->
                        <?php if (class_exists('Give') && empty($common_donation_form) && empty($common_form_id)) {
                            $page_contribute = add_query_arg(array('section' => 'contribute', 'id' => $edit_id, 'form_id' => ($post_type != 'petition' ? $edit_id : '')), $page_dashboard_petition); ?>
                            <a class="<?php echo esc_attr($section == 'contribute' ? 'active ' : '') ?>item" href="<?php echo esc_url($page_contribute); ?>" data-bjax><?php esc_html_e('Fundraising', 'campoal') ?></a>
                        <?php } ?>

                        <!-- Edit petition link -->
                        <?php if ($page_dashboard_petition && $post_type == 'petition') {
                            $page_edit_petition = add_query_arg(array('section' => 'edit_petition', 'id' => $edit_id), $page_dashboard_petition); ?>
                            <a class="<?php echo esc_attr($section == 'edit_petition' ? 'active ' : '') ?>item" href="<?php echo esc_url($page_edit_petition); ?>" data-bjax><?php esc_html_e('Edit Petition', 'campoal') ?></a>
                        <?php } ?>
                </div>
            </div>
        </div>

        <?php
            switch ($section) {
                case 'add_update':
                    get_template_part('templates/dashboard/add-update');
                    break;

                case 'edit_petition':
                    get_template_part('templates/dashboard/edit-petition');
                    break;

                case 'contribute':
                    get_template_part('templates/dashboard/submit-contribute');
                    break;

                case 'dashboard':
                    get_template_part('templates/dashboard/dashboard-petition');
                    break;

                case 'supporters':
                    get_template_part('templates/dashboard/supporters-list');
                    break;

                case 'donors':
                    get_template_part('templates/dashboard/supporters-list');
                    break;
                
                default:
                    get_template_part('templates/dashboard/dashboard-petition');
                    break;
            }
        ?>
    </div>
<?php } else { ?>
    <?php get_template_part('404') ?>
<?php } ?>
<?php get_footer(); ?>