<?php
/*
 * cass_shortcode_course_info - Action for course info
 *
 * This file is the treatment for course info
 */

//Unscription
add_action( 'admin_post_course_info_unscription', 'prefix_admin_course_info_unscription_callback' );
add_action( 'admin_post_nopriv_ourse_info_unscription', 'prefix_admin_course_info_unscription_callback' );

function prefix_admin_course_info_unscription_callback() {   
    if(intval($_POST["IDRace"] > 0)) {
        global $wpdb;

        //Select user_race
        $sql = "
            SELECT IDRace_user FROM cass_race_user WHERE IDRace=%d AND IDWPUser=%d;
        ";

        $sql = $wpdb->prepare($sql, intval($_POST["IDRace"]), get_current_user_id());

        $result = $wpdb->get_row($sql);

        $race_user = new Race_User();

        $race_user = new Race_User();

        $race_user->loadFromId(intval($result->IDRace_user));

        $race_user->setCommentUnscribe(SantizeGlobal($_POST["commentUnscription"]));
        $race_user->update();
        $race_user->updateState(3);

        $race = new Race();
        $race->loadFromId($race_user->getIDRace());
        
        $member_info = get_userdata(intval($race_user->getIDWPUser()));
        $member_mail = $member_info->user_email;
        $member_first_name = $member_info->first_name;
        $member_last_name = $member_info->last_name;
        $member_no = "OK";

        $rl_info = get_userdata(intval($race->getIDWPUser_RL()));
        $rl_mail = $rl_info->user_email;
        $rl_first_name = $rl_info->first_name;
        $rl_last_name = $rl_info->last_name;

        //Send mail
        $mailer = new Mailer("",$rl_mail,ReadGlobal(Mailer::getTemplate("mailContentParticipiantExcusationToRLObject")),ReadGlobal(Mailer::getTemplate("mailContentParticipiantExcusationToRL")),$member_first_name,$member_last_name,$member_no,ReadGlobal($race->getName()),"");
        $mailer->setMailMessage(nl2br($mailer->getMailMessage()."\r\n \r\n commentaire: \r\n".$_POST["commentUnscription"]));
        $mailer->sendMail();

    }

    if(isset($_POST["returnURLPassed"])) {
        echo "
        <script>
            location.href = '" . $_POST["pageUrl"] . "&returnUrl=" . $_POST["returnURLPassed"] . "';
        </script>
        ";
    } else {
        echo "
        <script>
            location.href ='". $_POST["pageUrl"]."';
        </script>
        ";
    }
  
}

//Inscription
add_action( 'admin_post_course_info_inscription', 'prefix_admin_course_info_inscription_callback' );
add_action( 'admin_post_nopriv_course_info_inscription', 'prefix_admin_course_info_inscription_callback' );

function prefix_admin_course_info_inscription_callback() {   
    if(intval($_POST["IDRace"] > 0)) {
        global $wpdb;

        //Select user_race
        $sql = "
            SELECT IDRace_user FROM cass_race_user WHERE IDRace=%d AND IDWPUser=%d;
        ";

        $sql = $wpdb->prepare($sql, intval($_POST["IDRace"]), get_current_user_id());

        $result = $wpdb->get_row($sql);

        $race_user = new Race_User();
        
        if($result) {
            $race_user->loadFromId($result->IDRace_user);
            $race_user->setCommentUnscribe(SantizeGlobal($_POST["commentInscription"]));
            $race_user->update(); 
            $race_user->updateState(1);
        } else {
            $race_user->setIDWPUser(get_current_user_id());
            $race_user->setIDRace(intval($_POST["IDRace"]));
            $race_user->setCommentInscription(SantizeGlobal($_POST["commentInscription"]));
            $race_user->setState(1);
            $race_user->create();
        }

        $race = new Race();
        $race->loadFromId(intval($_POST["IDRace"]));
        
        $member_info = get_userdata(intval(get_current_user_id()));
        $member_mail = $member_info->user_email;
        $member_first_name = $member_info->first_name;
        $member_last_name = $member_info->last_name;
        $member_no = get_user_meta(get_current_user_id(), 'cassMemberNo', true);

        $rl_info = get_userdata(intval($race->getIDWPUser_RL()));
        $rl_mail = $rl_info->user_email;
        $rl_first_name = $rl_info->first_name;
        $rl_last_name = $rl_info->last_name;
        
        //Send mail
        $mailer = new Mailer("",$rl_mail,ReadGlobal(Mailer::getTemplate("mailContentParticipiantValidationToRLObject")),ReadGlobal(Mailer::getTemplate("mailContentParticipiantValidationToRL")),$member_first_name,$member_last_name,$member_no,ReadGlobal($race->getName()),"");
        $mailer->setMailMessage(nl2br($mailer->getMailMessage()."\r\n \r\n commentaire: \r\n".$_POST["commentInscription"]));
        $mailer->sendMail();

    }

    

    if(isset($_POST["returnURLPassed"])) {
        echo "
        <script>
            location.href = '" . $_POST["pageUrl"] . "&returnUrl=" . $_POST["returnURLPassed"] . "';
        </script>
        ";
    } else {
        echo "
        <script>
            location.href = '" . $_POST["pageUrl"] . "';
        </script>
        ";
    }
}


add_action( 'admin_post_inscription', 'prefix_admin_inscription_callback' );

/*function prefix_admin_inscription_callback() {

    $race_user = new Race_User;

    // Verification if the values are set

    if(isset($_POST['user_id']) && isset($_POST['state']) && isset($_POST['inscription_comment'])  && isset($_POST['race_id'])){
        
        if(Race_User::isParticipiantAlreadyInCourse(intval($_POST["race_id"]),intval($_POST["user_id"]))){
            $race_user->loadFromId(intval($_POST['user_id']));
            $race_user->setState(1);
        }

        //Retrieve value of post and validating data
        if (intval($_POST["user_id"]) != 0) {
            $race_user->setIDWPUser(intval($_POST["user_id"]));
        }

        if ($_POST["inscription_comment"] != "") {
            $race_user->setCommentInscription(SantizeGlobal($_POST["inscription_comment"]));
        }

        if (intval($_POST["race_id"]) != 0 ){
            $race_user->setIDRace(intval($_POST["race_id"]));
        }

        // Registration of the user in a race
        if(Race_User::isParticipiantAlreadyInCourse($_POST["race_id"],$_POST["user_id"])){
            $race_user->update();
        } else {
            $race_user->create();
        }


        $race = new Race();
        $race->loadFromId(intval($_POST["race_id"]));
        
        $member_info = get_userdata(intval($_POST["user_id"]));
        $member_mail = $member_info->user_email;
        $member_first_name = $member_info->first_name;
        $member_last_name = $member_info->last_name;
        $member_no = $member_info->cassMemberNo;

        $rl_info = get_userdata(intval($race->getIDWPUser_RL()));
        $rl_mail = $rl_info->user_email;
        $rl_first_name = $rl_info->first_name;
        $rl_last_name = $rl_info->last_name;
        //Send mail
  

        $mailer = new Mailer("",$rl_mail,ReadGlobal(Mailer::getTemplate("mailContentParticipiantValidationToRLObject")),ReadGlobal(Mailer::getTemplate("mailContentParticipiantValidationToRL")),$member_first_name,$member_last_name,$member_no,ReadGlobal($race->getName()),"");
        $mailer->setMailMessage(nl2br($mailer->getMailMessage()."\r\n \r\n commentaire: \r\n".$_POST["inscription_comment"]));
        $mailer->sendMail();
        // Reload the current page
        wp_safe_redirect(wp_get_referer());


    }else {

        echo "
          <script>
              alert('Les données ne sont pas valides');
              location.reload() ;
          </script>
      ";
    }

}


add_action( 'admin_post_memberCancelCourse', 'prefix_admin_memberCancelCourse_callback' );
add_action( 'admin_post_nopriv_memberCancelCourse', 'prefix_admin_memberCancelCourse_callback' );


function prefix_admin_memberCancelCourse_callback() {   
    if(intval($_POST["memberCancelCourseNameIDRace_user"] > 0)) {
        $race_user = new Race_User();
        $race_user->loadFromId(intval($_POST["memberCancelCourseNameIDRace_user"]));
        $race_user->setCommentUnscribe(SantizeGlobal($_POST["memberCancelCourseReason"]));
        $race_user->update();
        $race_user->updateState(3);

        $race = new Race();
        $race->loadFromId($race_user->getIDRace());
        
        $member_info = get_userdata(intval($race_user->getIDWPUser()));
        $member_mail = $member_info->user_email;
        $member_first_name = $member_info->first_name;
        $member_last_name = $member_info->last_name;
        $member_no = $member_info->cassMemberNo;

        $rl_info = get_userdata(intval($race->getIDWPUser_RL()));
        $rl_mail = $rl_info->user_email;
        $rl_first_name = $rl_info->first_name;
        $rl_last_name = $rl_info->last_name;

        //Send mail
        $mailer = new Mailer("",$rl_mail,ReadGlobal(Mailer::getTemplate("mailContentParticipiantExcusationToRLObject")),ReadGlobal(Mailer::getTemplate("mailContentParticipiantExcusationToRL")),$member_first_name,$member_last_name,$member_no,ReadGlobal($race->getName()),"");
        $mailer->setMailMessage(nl2br($mailer->getMailMessage()."\r\n \r\n commentaire: \r\n".$_POST["memberCancelCourseReason"]));
        $mailer->sendMail();
    }
    $url =  $_POST["pageUrl"];
    echo "
    <script>
        alert('Les informations de votre course ont été mise à jour.');
        location.href = '" . $url . "';
    </script>
    ";
}
*/