<?php

//Entry point for the notification 
if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

global $db,$app_list_strings,$sugar_config;                     //declaration of global variables
$current_user = $_GET['userid'];
$current_user_name = $_GET['u_name'];

//this action fetches and sends the respective notification data to the calling method
if($_GET['action'] == 'getnotify')
{
    $select_notification = "SELECT  notifications.name,notifications.id,
                                    notifications.assigned_user_id,notifications.is_read,
                                    notifications_cstm.enjay_bean_type_c,notifications_cstm.enjay_bean_id_c,notifications_cstm.enjay_is_notify_c
                            FROM    notifications 
                            INNER JOIN notifications_cstm 
                            ON      notifications.id = notifications_cstm.id_c 
                            WHERE   notifications.assigned_user_id = '$current_user' AND notifications_cstm.enjay_is_notify_c = '0' 
                            ORDER BY `notifications`.`date_entered` 
                            ASC LIMIT 1";

    $sql = $db->query($select_notification);
    $row = $db->fetchByAssoc($sql);


    $notification_isread = "SELECT  notifications.name,notifications.id,
                                    notifications.assigned_user_id,notifications.is_read,
                                    notifications_cstm.enjay_bean_type_c,notifications_cstm.enjay_bean_id_c,notifications_cstm.enjay_is_notify_c
                            FROM    notifications 
                            INNER JOIN notifications_cstm 
                            ON      notifications.id = notifications_cstm.id_c 
                            WHERE   notifications.assigned_user_id = '$current_user' AND notifications.is_read = '0' 
                            ORDER BY `notifications`.`date_entered`"; 

    $sql_notification_isread = $db->query($notification_isread);
    $numrows = $sql_notification_isread->num_rows;
    if($numrows!=0)
    {
        $notify_str_num = $numrows;
    }

    $notify_arr = array();

    while($data = $GLOBALS['db']->fetchByAssoc($sql_notification_isread)) {        
        $iconImageUrl = "themes/{$sugar_config['default_theme']}/images/icon_" . $data['enjay_bean_type_c'] . "_32.png";
        if(file_exists($iconImageUrl))
        {
            $imageURL = $iconImageUrl;
        }
        else
        {
            $imageURL = "themes/{$sugar_config['default_theme']}/images/sugar_icon.png";
        }
        $data['imageURL'] = $imageURL;

        array_push($notify_arr, $data);
    }

    if($row)
    {
        $iconImageUrl = "themes/{$sugar_config['default_theme']}/images/icon_" . $row['enjay_bean_type_c'] . "_32.png";
        if(file_exists($iconImageUrl))
        {
            $imageURL = $iconImageUrl;
        }
        else
        {
            $imageURL = "themes/{$sugar_config['default_theme']}/images/sugar_icon.png";
        }

        $row['icon_url'] = $imageURL;
        $row['u_name'] = $current_user_name;  
        //Bhavin changes
        $update_query = "UPDATE notifications_cstm 
                     SET enjay_is_notify_c = 1 
                     WHERE id_c = '".$row['id']."'";  
        $db->query($update_query);
        //
    }
    $row['notify_num'] = $notify_str_num;
    $row['notify_arr'] = $notify_arr;
    echo json_encode($row);
    die();
}

//this action deletes the notificatio from notifications and notifications_cstm table as soon as it is made visible on the screen
if($_GET['action']== "EnjayDeleteDN")
{
    $id = $_REQUEST['record'];
    $update_query = "UPDATE notifications_cstm 
                     SET enjay_is_notify_c = 1 
                     WHERE id_c = '$id'";
    //echo $update_query;
    //$update_query = "DELETE notifications, notifications_cstm FROM notifications INNER JOIN notifications_cstm ON notifications.id = notifications_cstm.id_c WHERE notifications.id = '$id'";
    $db->query($update_query);
    exit;
}
?>