<?php

if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
class EnjayDNHookClass
{

	function EnjayDNBeforeSaveFunction($bean, $event, $arguments)
    {
    	require_once "modules/Notifications/Notifications.php";
        if (($bean->object_name != "UpgradeHistory") && (!empty($_REQUEST['module']) && $_REQUEST['module'] != 'ModuleBuilder') && empty($_REQUEST['to_csv']))
        {
	        global $current_user, $db;
	        $saved_modules = array();
	        $query = "SELECT * FROM enjay_dn_modules_list";
	        $result = $db->query($query);

	        while ($row = $db->fetchByAssoc($result))
	        {
	            array_push($saved_modules, $row['module']);
	        }

            if (in_array($bean->module_dir, $saved_modules))
            {
                $sql_upd2 = "DELETE notifications, notifications_cstm
                             FROM notifications
                             INNER JOIN notifications_cstm
                             ON notifications.id = notifications_cstm.id_c
                             WHERE notifications_cstm.enjay_bean_id_c = '$bean->id'
                             AND notifications_cstm.enjay_bean_type_c = '$bean_type'";
                $db->query($sql_upd2);

                //$GLOBALS['log']->fatal("Module is enabled :".$bean->module_dir);

                if (($bean->fetched_row['assigned_user_id'] != $bean->assigned_user_id) && ($current_user->id != $bean->assigned_user_id))
                {
                    //$GLOBALS['log']->fatal("Record to insert in table");

                    $notification = new Notifications();
                    if($bean->name !="")
                    {
                        $notification->name = $bean->name;
                    }
                    else
                    {
                        $notification->name = $bean->last_name;
                    }
                    $notification->assigned_user_id = $bean->assigned_user_id;
                    $notification->enjay_bean_id_c = $bean->id;
                    $notification->enjay_is_notify_c = 0;
                    $notification->is_read = 0;
                    $notification->is_save = true;
                    if ($bean->module_dir == "ProspectLists")
                        $notification->enjay_bean_type_c = "Prospect_Lists";          //since Prospect list has different table name
                    else
                        $notification->enjay_bean_type_c = $bean->module_dir;

                    $notify_id = $notification->save();
                    //$GLOBALS['log']->fatal("Notification table id : ".$notify_id);
                }
                //$GLOBALS['log']->fatal("Notification Logic hook finished");
            }
        }
    }
    function EnjayDNAfterDeleteFunction($bean, $event, $arguments)
    {
        global $db;
        $sql_dele = "DELETE notifications, notifications_cstm
                             FROM notifications
                             INNER JOIN notifications_cstm
                             ON notifications.id = notifications_cstm.id_c
                             WHERE notifications_cstm.enjay_bean_id_c = '$bean->id'";
                $db->query($sql_dele);
    }
    function EnjayDNAfterUIFrameFunction($event, $arguments)
    {
        /*Script to get the notification*/
        global $current_user,$sugar_config,$db;
        $notify_time = $sugar_config['enjay_notify_time'];
        $excludeAction = array('DynamicAction', 'modulelistmenu', 'favorites', 'EmailUIAjax', 'wizard', 'QuickEdit', 'SaveActivity',);
        //put condition for few modules and action
        //For Notification
        if (!in_array($_REQUEST['action'], $excludeAction) && empty($_REQUEST['to_pdf']) && (!empty($_REQUEST['module']) && $_REQUEST['module'] != 'ModuleBuilder') && empty($_REQUEST['to_csv']) && isset($current_user->id) && ($_REQUEST['module'] != 'Calendar'))
        {
           //echo "<script type='text/javascript' src='custom/modules/EnjayDestopNotification/unserialize.js'></script>";
           //echo "<script type='text/javascript' src='custom/modules/EnjayDestopNotification/notification_design.js'></script>";
           echo "<script type='text/javascript' src='custom/modules/EnjayDestopNotification/notification.js'></script>";
           echo "<script>var userid = '$current_user->id'; var u_name = '$current_user->name'; var notify_time = $notify_time</script>";
        }

        //$read_count = $_REQUEST['isread'];
        $bean_id = $_REQUEST['record'];
        $bean_type = $_REQUEST['module'];

        if(!empty($bean_id) && !empty($bean_type))
        {
        	$sql_upd1 = "DELETE notifications, notifications_cstm
                         FROM notifications
                         INNER JOIN notifications_cstm
                         ON notifications.id = notifications_cstm.id_c
                         WHERE notifications_cstm.enjay_bean_id_c = '$bean_id'
                         AND notifications_cstm.enjay_bean_type_c = '$bean_type'
                         AND notifications.assigned_user_id = '$current_user->id'";
        	$db->query($sql_upd1);
        }
    }
}
?>