<?php
global $wpdb;
/**
 * Template Name: Notifications
 * Description: Notifications
 **/

get_header();

    locate_template( array( 'leftsidebar.php' ), true );
?>

    <div id="content">
    
        <div id="iron-encabezado-container">
            <h2 class="iron-encabezado-seccion"><?php echo __('Notificaciones','buddypress'); ?></h2>
        </div>
        
        <div class="padder">
        
        <div id="content-top"></div>
        
            <div id="content-middle">
            
                <div id="content-middle-add">
                
<style>
.imgnotification{
    float: left;
    margin-top: 4px;
    margin-right: 10px;
    width: 22px;
    height: 22px;
} 
.notification{
    color:#8E8E8E;
}
</style>
                <?php
                global $wpdb;
                
                $i    = 0;
                
                //'SELECT * FROM '.$wpdb->prefix.'bp_notifications WHERE user_id ='. bp_loggedin_user_id()
                $queryNots = "SELECT n.*, p.meta_value FROM ".$wpdb->prefix."bp_notifications n left join wp_postmeta p on (n.item_id=p.post_id and p.meta_key = '_wp_page_type') WHERE user_id ='". bp_loggedin_user_id()."' and n.component_name != 'achievements' ";
                
                /**
                 * dani - irontec - quitar hitos
                 * Query Original
                 * $queryNots = "SELECT n.*, p.meta_value FROM ".$wpdb->prefix."bp_notifications n left join wp_postmeta p on (n.item_id=p.post_id and p.meta_key = '_wp_page_type') WHERE user_id ='". bp_loggedin_user_id()."'";
                 **/
                
                $nots = $wpdb->get_results ( $queryNots );
                
                if($nots){
                    
                    foreach ($nots as $not) {
                        
                        $noti_date = $not->date_notified;
                        $type_noti = isset($not->meta_value)? $not->meta_value : $not->component_name;
                        $user_id   = $not->user_id;
                        $item_id   = $not->item_id;
                        $compone   = $not->component_action;
                        
                        $not = BP_Core_Notification::get_all_for_user( wp_get_current_user()->ID );
                        
                        $notifications = bp_core_get_notifications_for_user( bp_loggedin_user_id() );
                        
                        if ($notifications and $not[$i]->component_action == 'jes_event_invite') {
                            
                            if ($not[$i]->component_action == 'jes_event_invite') {
                                
                                $dateEvent = $wpdb->get_row('select * from wp_jet_events where id ="' . $not[$i]->item_id . '"');
                                //miembros/admin/events/invites/
                                
                                $notEvent = '<a href="' . wp_get_current_user()->user_url . 'events/invites/' . '" title="' . $dateEvent->name . '">' . 
                                              __('Tienes una invitción del Evento:', 'buddypress') . ' ' . $dateEvent->name . 
                                              '</a>';
                            }
                            
                            $notificationes = array_merge(array($notEvent),$notifications);
                        } elseif ($not[$i]->component_name == 'ac_notifier') {
                            $notificationes = $notifications;
                        } elseif ($notifications) {
                            
                            $notificationes = $notifications;
                        } elseif ($not[$i]->component_action == 'jes_event_invite') {
                            if ($not[$i]->component_action == 'jes_event_invite') {
                            
                                $dateEvent = $wpdb->get_row('select * from wp_jet_events where id ="' . $not[$i]->item_id . '"');
                            
                                $notEvent = '<a href="' . wp_get_current_user()->user_url . 'events/invites/' . '" title="' . $dateEvent->name . '">' .
                                                __('Tienes una invitción del Evento:', 'buddypress') . ' ' . $dateEvent->name .
                                                '</a>';
                            $notificationes = array($notEvent);
                            }
                        }
                        
                        if (!isset($notificationes[$i])) {
                            continue;
                        }
                        
                        echo '<hr />';
                        echo '<div class="notification">';
                        
                        switch ($type_noti) {
                            case 'friends':
                                $picture = "icon_secc_friends.png";
                                break;
                            case 'messages':
                                $picture = "icon_secc_msg.png";
                                break;
                            case 'pregunta':
                                $picture = "icon_secc_ask.png";
                                break;
                            case 'testimonio':
                                $picture = "icon_secc_testim.png";
                                break;
                            case'jes_events':
                                $picture = "icon_secc_events.png";
                                break;
                            case'groups':
                                $picture = "icon_secc_groups.png";
                                break;
                            case'achievements':
                                $picture = "icon_secc_hitos.png";
                                break;    
                                
                            default:
                                $picture = "icon_secc_friends.png"; //DEFAULT
                                break;
                        } 
                        echo '<img alt="" class="imgnotification" src="' . get_stylesheet_directory_uri() . '/_inc/images/' . $picture . '">';
                        
                        echo str_replace('members','miembros',$notificationes[$i]);
                        echo '<br />';
                        echo  date ( 'd-m-Y h:i:s' , strtotime($noti_date) );
                        echo '<br />';
                        echo '</div>';
                        $i++;
                        
                    }
                    echo '<br />';
                } else {
                    _e ('No tienes ninguna notificaci&oacute;n.', 'buddypress');
                    echo '<br />';
                }
                ?>
                </div> <!-- content-middle-add -->
                
            </div> <!-- content-middle -->
            
        <div id="content-bottom"></div>
        
        </div><!-- .padder -->
    </div><!-- #content -->
    
    <?php locate_template( array( 'sidebar.php' ), true ) ?>
    
<?php get_footer() ?>
