<?php
/*
 * dani - irontect
* */
    require_once ('../../../../../wp-load.php');
    global $bp, $wpdb;
    
    ob_end_clean();
    ob_clean();
    
    $eventParticipation = $wpdb->get_row('select * from wp_jet_events_eventmeta WHERE meta_key = "total_member_count" AND event_id = "' . $_POST['eventId'] . '"');
    
    if ($_GET['join-event']) {
        
        $date = date('Y-m-t H-i-s');
        
        $invitedTrue = $wpdb->get_row('select * from wp_jet_events_members where event_id = "' . $_POST['eventId'] . '" and user_id = "' . $_POST['userId'] . '"');
        
        if ($invitedTrue) {
            $invited = '1';
            
            $dataTrue = array(
                            'date_modified' => $date,
                            'is_confirmed'  => '1',
                            'invite_sent'   => '0'
            );
            
            $whereTrue = array(
                            'user_id'  => $_POST['userId'],
                            'event_id' => $_POST['eventId']
            );
            
            $resultTrue = $wpdb->update( 'wp_jet_events_members', $dataTrue, $whereTrue );
            
            if ( is_wp_error( $resultTrue ) ) {
                $error_string = $resultTrue->get_error_message();
                echo '<div id="message" class="error"><p>' . $error_string . '</p></div>';
            }
            
        } else {
            
            $dataFalse = array(
                            'event_id'      => $_POST['eventId'],
                            'user_id'       => $_POST['userId'],
                            'inviter_id'    => '0',
                            'is_admin'      => '0',
                            'is_mod'        => '',
                            'user_title'    => '',
                            'date_modified' => $date,
                            'comments'      => '',
                            'is_confirmed'  => '1',
                            'is_banned'     => '0',
                            'invite_sent'   => '0'
            
            );
            
            $resultFalse = $wpdb->insert( 'wp_jet_events_members', $dataFalse );
            
            if ( is_wp_error( $resultFalse ) ) {
                $error_string = $resultFalse->get_error_message();
                echo '<div id="message" class="error"><p>' . $error_string . '</p></div>';
            }
        }
        
        $dataPartPosi  = array(
                        'meta_value' => $eventParticipation->meta_value+1);
        $wherePartPosi = array(
                        'event_id' => $_POST['eventId']
                        );
        
        $ResultPartPosi = $wpdb->update( 'wp_jet_events_eventmeta', $dataPartPosi, $wherePartPosi );
        
    }
    
    if ($_GET['leave-event']) {
    
            $whereDelete = array(
                            'user_id'  => $_POST['userId'],
                            'event_id' => $_POST['eventId']
            );
    
            $resultDelete = $wpdb->delete( 'wp_jet_events_members', $whereDelete );
    
            if ( is_wp_error( $resultDelete ) ) {
                $error_string = $resultDelete->get_error_message();
                echo '<div id="message" class="error"><p>' . $error_string . '</p></div>';
            }
            
            $dataPartNega  = array(
                            'meta_value' => $eventParticipation->meta_value-1);
            $wherePartNega = array(
                            'event_id' => $_POST['eventId']
            );
            
            $ResultPartPosi = $wpdb->update( 'wp_jet_events_eventmeta', $dataPartNega, $wherePartNega );
    }
    
?>