<?php
class Race
{
    private $id;
    private $name;
    private $dtCreation;
    private $dtEdition;
    private $state;
    private $IDWPUser_RL;
    private $IDWPUser_Deputy;
    private $placeName;
    private $placeLocality;
    private $placeLatitude;
    private $placeLongitude;
    private $start;
    private $end;
    private $subscriptionEnable;
    private  $subscriptionStart;
    private $subscriptionEnd;
    private $IDLevel;
    private $meetingPlaceName;
    private $meetingPlaceLongitude;
    private $meetingPlaceLatitude;
    private $participantMin;
    private $participantMax;
    private $descriptionPublic;
    private $descriptionMemberSubscribed;
    private $formType;
    private $comment;
    private $IDType;
    private $IDGroup;

    private $IDSeason;

    public function loadFromId($id)
    {
        global $wpdb;
        $sql = "
        SELECT IDRace, name, dtCreation, dtEdition, state, IDWPUser_RL,IDWPUser_Deputy, placeName, placeLocality, placeLatitude, placeLongitude, start, end, subscriptionEnable, subscriptionStart, subscriptionEnd, IDLevel, meetingPlaceName, meetingPlaceLongitude, meetingPlaceLatitude, participantMin, participantMax, descriptionPublic, descriptionMemberSubscribed, formType, comment, IDType, IDGroup, IDSeason 
        FROM cass_race
        WHERE IDRace = %d;
        ";
        $result = $wpdb->get_row($wpdb->prepare($sql, $id));
     
        if ($result) {
            $this->setIDRace($result->id);
            $this->setName($result->name);
            $this->setDtCreation($result->dtCreation);
            $this->setDtEdition($result->dtEdition);
            $this->setState($result->state);
            $this->setIDWPUser_RL($result->IDWPUser_RL);
            $this->setIDWPUser_Deputy($result->IDWPUser_Deputy);
            $this->setPlaceName($result->placeName);
            $this->setPlaceLocality($result->placeLocality);
            $this->setPlaceLatitude($result->placeLatitude);
            $this->setPlaceLongitude($result->placeLongitude);
            $this->setStart($result->start);
            $this->setEnd($result->end);
            $this->setSubscriptionEnable($result->subscriptionEnable);
            $this->setSubscriptionStart($result->subscriptionStart);
            $this->setSubscriptionEnd($result->subscriptionEnd);
            $this->setIDLevel($result->IDLevel);
            $this->setMeetingPlaceName($result->meetingPlaceName);
            $this->setMeetingPlaceLongitude($result->meetingPlaceLongitude);
            $this->setMeetingPlaceLatitude($result->meetingPlaceLatitude);
            $this->setParticipantMin($result->participantMin);
            $this->setParticipantMax($result->participantMax);
            $this->setDescriptionPublic($result->descriptionPublic);
            $this->setDescriptionMemberSubscribed($result->descriptionMemberSubscribed);
            $this->setFormType($result->formType);
            $this->setComment($result->comment);
            $this->setIDType($result->IDType);
            $this->setIDGroup($result->IDGroup);
            $this->setIDSeason($result->IDSeason);
            return true;
        } else {
            return false;
        }
    }

    //Update material
    public function update()
    {
        global $wpdb;

        $sql = "
        UPDATE cass_race
        SET name = %s, dtCreation = %s, dtEdition = %s, state = %d, IDWPUser_RL = %d, IDWPUser_Deputy = %d, placeName = %s, placeLocality = %s, placeLatitude = %s, placeLongitude = %s, start = %s, end = %s,subscriptionEnable = %d, subscriptionStart = %s, subscriptionEnd = %s, IDLevel = %d, meetingPlaceName = %s, meetingPlaceLongitude = %s, meetingPlaceLatitude = %s, participantMin = %d, participantMax = %d, descriptionPublic = %s, descriptionMemberSubscribed = %s, formType = %d, comment = %s, IDType = %d, IDGroup = %d, IDSeason = %d
        WHERE IDRace = %d;
         ";
         
        $wpdb->query($wpdb->prepare(
            $sql,
            $this->getName(),
            $this->getDtCreation(),
            $this->getDtEdition(),
            $this->getState(),
            $this->getIDWPUser_RL(),
            $this->getIDWPUser_Deputy(),
            $this->getPlaceName(),
            $this->getPlaceLocality(),
            $this->getPlaceLatitude(),
            $this->getPlaceLongitude(),
            $this->getStart(),
            $this->getEnd(),
            $this->getSubscriptionEnable(),
            $this->getSubscriptionStart(),
            $this->getSubscriptionEnd(),
            $this->getIDLevel(),
            $this->getMeetingPlaceName(),
            $this->getMeetingPlaceLongitude(),
            $this->getMeetingPlaceLatitude(),
            $this->getParticipantMin(),
            $this->getParticipantMax(),
            $this->getDescriptionPublic(),
            $this->getDescriptionMemberSubscribed(),
            $this->getFormType(),
            $this->getComment(),
            $this->getIDType(),
            $this->getIDGroup(),
            $this->getIDSeason(),
            $this->getIDRace()
        )
        );
    }

    //Delete material
    public function delete()
    {
        global $wpdb;

        $sql = "
            DELETE FROM cass_race
            WHERE IDRace = %d;
         ";
        $wpdb->query($wpdb->prepare($sql, $this->getIDRace()));
    }

    //Insert material
    public function create()
    {
        global $wpdb;

        $sql = "
        INSERT INTO cass_race (name, dtCreation, dtEdition, state, IDWPUser_RL, IDWPUser_Deputy, placeName, placeLocality, placeLatitude, placeLongitude, start, end, subscriptionEnable, subscriptionStart, subscriptionEnd,  IDLevel, meetingPlaceName, meetingPlaceLongitude, meetingPlaceLatitude, participantMin, participantMax, descriptionPublic, descriptionMemberSubscribed, formType, comment, IDType, IDGroup, IDSeason)
        VALUES(%s, %s, %s, %d, %d, %d, %s, %s, %s, %s, %s, %s, %d, %s, %s, %s, %s, %d, %d, %d, %d, %s, %s, %d, %s, %d, %d, %d);
         ";

        $wpdb->query($wpdb->prepare(
            $sql,
            $this->getName(),
            $this->getDtCreation(),
            $this->getDtEdition(),
            $this->getState(),
            $this->getIDWPUser_RL(),
            $this->getIDWPUser_Deputy(),
            $this->getPlaceName(),
            $this->getPlaceLocality(),
            $this->getPlaceLatitude(),
            $this->getPlaceLongitude(),
            $this->getStart(),
            $this->getEnd(),
            $this->getSubscriptionEnable(),
            $this->getSubscriptionStart(),
            $this->getSubscriptionEnd(),
            $this->getIDLevel(),
            $this->getMeetingPlaceName(),
            $this->getMeetingPlaceLongitude(),
            $this->getMeetingPlaceLatitude(),
            $this->getParticipantMin(),
            $this->getParticipantMax(),
            $this->getDescriptionPublic(),
            $this->getDescriptionMemberSubscribed(),
            $this->getFormType(),
            $this->getComment(),
            $this->getIDType(),
            $this->getIDGroup(),
            $this->getIDSeason()
        ));
    }

    public static function selectAllStructured(){
        global $wpdb;

        $sql = "
            SELECT cass_race.IDRace, wp_users_rl.display_name AS nameRL, wp_users_deputy.display_name AS nameDeputy, GROUP_CONCAT(cass_material.name) AS materials, cass_race.name AS name, state, placeName, placeLocality, placeLatitude, placeLongitude, start, end, subscriptionEnable, subscriptionStart, subscriptionEnd, cass_race.IDLevel, meetingPlaceName, meetingPlaceLatitude, meetingPlaceLongitude, participantMin, participantMax, descriptionPublic, descriptionMemberSubscribed, comment, cass_level.name AS level, cass_type.name AS type, cass_group.name AS 'group', cass_race.IDGroup, cass_season.name AS season FROM cass_race
            INNER JOIN cass_level ON cass_level.IDLevel = cass_race.IDLevel
            INNER JOIN cass_group ON cass_group.IDGroup = cass_race.IDGroup
            INNER JOIN cass_type ON cass_type.IDType = cass_race.IDType
            INNER JOIN cass_season ON cass_season.IDSeason = cass_race.IDSeason
            INNER JOIN ".$wpdb->prefix."users AS wp_users_rl ON wp_users_rl.ID = cass_race.IDWPUser_RL 
            LEFT JOIN ".$wpdb->prefix."users AS wp_users_deputy ON wp_users_deputy.ID = cass_race.IDWPUser_Deputy
            LEFT JOIN cass_race_material ON cass_race_material.IDRace = cass_race.IDRace
            LEFT JOIN cass_material ON cass_material.IDMaterial = cass_race_material.IDMaterial
            GROUP BY cass_race.IDRace;
            ;
        ";
    
        return $wpdb->get_results($sql);
    }

    public static function selectOneStructured($IDRace){
        global $wpdb;

        $sql = "
            SELECT cass_race.IDRace, wp_users_rl.display_name AS nameRL, wp_users_deputy.display_name AS nameDeputy, GROUP_CONCAT(cass_material.name) AS materials, cass_race.name AS name, state, placeName, placeLocality, placeLatitude, placeLongitude, start, end, subscriptionEnable, subscriptionStart, subscriptionEnd, cass_race.IDLevel, meetingPlaceName, meetingPlaceLatitude, meetingPlaceLongitude, participantMin, participantMax, descriptionPublic, descriptionMemberSubscribed, comment, cass_level.name AS level, cass_type.name AS type, cass_group.name AS 'group', cass_race.IDGroup, cass_season.name AS season FROM cass_race
            INNER JOIN cass_level ON cass_level.IDLevel = cass_race.IDLevel
            INNER JOIN cass_group ON cass_group.IDGroup = cass_race.IDGroup
            INNER JOIN cass_type ON cass_type.IDType = cass_race.IDType
            INNER JOIN cass_season ON cass_season.IDSeason = cass_race.IDSeason
            INNER JOIN ".$wpdb->prefix."users AS wp_users_rl ON wp_users_rl.ID = cass_race.IDWPUser_RL 
            LEFT JOIN ".$wpdb->prefix."users AS wp_users_deputy ON wp_users_deputy.ID = cass_race.IDWPUser_Deputy
            LEFT JOIN cass_race_material ON cass_race_material.IDRace = cass_race.IDRace
            LEFT JOIN cass_material ON cass_material.IDMaterial = cass_race_material.IDMaterial
            WHERE cass_race.IDRace=%d
            GROUP BY cass_race.IDRace;
            ;
        ";
    
        $sql = $wpdb->prepare($sql, $IDRace);
        return $wpdb->get_results($sql);
    }

    public function sqlWPTable()
    {
        global $wpdb;

        $sql = "
        SELECT IDRace, name, dtCreation, dtEdition, state, IDWPUser_RL, IDWPUser_Deputy, placeName, placeLocality, placeLatitude, placeLongitude, start, end,subscriptionEnable, subscriptionStart,subscriptionEnd, IDLevel, meetingPlaceName, meetingPlaceLongitude, meetingPlaceLatitude, participantMin, participantMax, descriptionPublic, descriptionMemberSubscribed, formType, comment, IDType, IDGroup, IDSeason 
        FROM cass_race
        WHERE IDRace = %d;
        ";

        return $wpdb->get_results(
            $sql,
            ARRAY_A
        );
    }

    public static function selectAll() {
        global $wpdb;

        $sql = "
            SELECT IDRace, name, dtCreation, dtEdition, state, IDWPUser_RL, placeName, placeLocality, placeLatitude, placeLongitude, start, end,subscriptionStart,subscriptionEnd, IDLevel, meetingPlaceName, meetingPlaceLongitude, meetingPlaceLatitude, participantMin, participantMax, descriptionPublic, descriptionMemberSubscribed, formType, comment, IDType, IDGroup, IDSeason IDRace, name, dtCreation, dtEdition, state, IDWPUser_RL, placeName, placeLocality, placeLatitude, placeLongitude, start, end,subscriptionStart,subscriptionEnd, IDLevel, meetingPlaceName, meetingPlaceLongitude, meetingPlaceLatitude, participantMin, participantMax, descriptionPublic, descriptionMemberSubscribed, formType, comment, IDType, IDGroup, IDSeason 
            FROM cass_race;
             ";

        return $wpdb->get_results($sql);
    }


    // Getters
    public function getIDRace()
    {
        return $this->id;
    }

    public function getName()
    {
        return $this->name;
    }

    public function getDtCreation()
    {
        return $this->dtCreation;
    }

    public function getDtEdition()
    {
        return $this->dtEdition;
    }

    public function getState()
    {
        return $this->state;
    }

    public function getIDWPUser_RL()
    {
        return $this->IDWPUser_RL;
    }

    public function getIDWPUser_Deputy()
    {
        return $this->IDWPUser_Deputy;
    }

    public function getPlaceName()
    {
        return $this->placeName;
    }

    public function getPlaceLocality()
    {
        return $this->placeLocality;
    }

    public function getPlaceLatitude()
    {
        return $this->placeLatitude;
    }

    public function getPlaceLongitude()
    {
        return $this->placeLongitude;
    }

    public function getStart()
    {
        return $this->start;
    }

    public function getEnd()
    {
        return $this->end;
    }

    public function getSubscriptionEnable()
    {
        return $this->subscriptionEnable;
    }
    public function getSubscriptionStart()
    {
        return $this->subscriptionStart;
    }

    public function getSubscriptionEnd()
    {
        return $this->subscriptionEnd;
    }
    public function getIDLevel()
    {
        return $this->IDLevel;
    }

    public function getMeetingPlaceName()
    {
        return $this->meetingPlaceName;
    }

    public function getMeetingPlaceLongitude()
    {
        return $this->meetingPlaceLongitude;
    }

    public function getMeetingPlaceLatitude()
    {
        return $this->meetingPlaceLatitude;
    }

    public function getParticipantMin()
    {
        return $this->participantMin;
    }

    public function getParticipantMax()
    {
        return $this->participantMax;
    }

    public function getDescriptionPublic()
    {
        return $this->descriptionPublic;
    }

    public function getDescriptionMemberSubscribed()
    {
        return $this->descriptionMemberSubscribed;
    }

    public function getFormType()
    {
        return $this->formType;
    }

    public function getComment()
    {
        return $this->comment;
    }

    public function getIDType()
    {
        return $this->IDType;
    }

    public function getIDGroup()
    {
        return $this->IDGroup;
    }

    public function getIDSeason()
    {
        return $this->IDSeason;
    }

    // Setters
    public function setIDRace($IDRace)
    {
        $this->id = $IDRace;
    }

    public function setName($name)
    {
        $this->name = $name;
    }

    public function setDtCreation($dtCreation)
    {
        $this->dtCreation = $dtCreation;
    }

    public function setDtEdition($dtEdition)
    {
        $this->dtEdition = $dtEdition;
    }

    public function setState($state)
    {
        $this->state = $state;
    }

    public function setIDWPUser_RL($IDWPUser_RL)
    {
        $this->IDWPUser_RL = $IDWPUser_RL;
    }

    public function setIDWPUser_Deputy($IDWPUser_Deputy)
    {
        $this->IDWPUser_Deputy = $IDWPUser_Deputy;
    }

    public function setPlaceName($placeName)
    {
        $this->placeName = $placeName;
    }

    public function setPlaceLocality($placeLocality)
    {
        $this->placeLocality = $placeLocality;
    }

    public function setPlaceLatitude($placeLatitude)
    {
        $this->placeLatitude = $placeLatitude;
    }

    public function setPlaceLongitude($placeLongitude)
    {
        $this->placeLongitude = $placeLongitude;
    }

    public function setStart($start)
    {
        $this->start = $start;
    }

    public function setEnd($end)
    {
        $this->end = $end;
    }

    public function setSubscriptionEnable($subscriptionEnable)
    {
        $this->subscriptionEnable = $subscriptionEnable;
    }

    public function setSubscriptionStart($subscriptionStart)
    {
        $this->subscriptionStart = $subscriptionStart;
    }

    public function setSubscriptionEnd($subscriptionEnd)
    {
        $this->subscriptionEnd = $subscriptionEnd;
    }

    public function setIDLevel($IDLevel)
    {
        $this->IDLevel = $IDLevel;
    }

    public function setMeetingPlaceName($meetingPlaceName)
    {
        $this->meetingPlaceName = $meetingPlaceName;
    }

    public function setMeetingPlaceLongitude($meetingPlaceLongitude)
    {
        $this->meetingPlaceLongitude = $meetingPlaceLongitude;
    }

    public function setMeetingPlaceLatitude($meetingPlaceLatitude)
    {
        $this->meetingPlaceLatitude = $meetingPlaceLatitude;
    }

    public function setParticipantMin($participantMin)
    {
        $this->participantMin = $participantMin;
    }

    public function setParticipantMax($participantMax)
    {
        $this->participantMax = $participantMax;
    }

    public function setDescriptionPublic($descriptionPublic)
    {
        $this->descriptionPublic = $descriptionPublic;
    }

    public function setDescriptionMemberSubscribed($descriptionMemberSubscribed)
    {
        $this->descriptionMemberSubscribed = $descriptionMemberSubscribed;
    }

    public function setFormType($formType)
    {
        $this->formType = $formType;
    }

    public function setComment($comment)
    {
        $this->comment = $comment;
    }

    public function setIDType($IDType)
    {
        $this->IDType = $IDType;
    }

    public function setIDGroup($IDGroup)
    {
        $this->IDGroup = $IDGroup;
    }

    public function setIDSeason($IDSeason)
    {
        $this->IDSeason = $IDSeason;
    }

}