<?php
    include_once("CUserSession.php");
    include_once("helper.php");
    include_once("CUtil.php");
    include_once("CEnums.php");
    include_once("CProjectSolutions.php");

    class CNotificationUserSetting{

        public $sl_no;
        public $major;
        public $minor;
        public $email;
        public $web_page;
        public $web_page_name;

        
        function populateFromDBRow( $row ){
            $this->sl_no = $row["sl_no"];
            $this->major = $row["major"];
            $this->minor = $row["minor"];
            $this->email = $row["email"];
            $this->web_page = $row["web_page"];
            $this->web_page_name = $row["web_page_name"];
        }

        function store_to_db()
        {
            //$user_id = $_SESSION['user']->_id;
            $user_id = "avishkar-f6dd5d0c2b";
            $sql = "REPLACE into Notifications_user_settings(user_id, notification_type_id, email, web_page) values('".$user_id."',".$this->sl_no.",'".$this->email."','".$this->web_page."') ";
            return write_db($sql);
        }
    }

    function get_notification_user_settings(){
        $sql_conn = mysqli_connection();
        $ret["success"] = true;

        //$user_id = $_SESSION['user']->_id;
        $user_id = "avishkar-f6dd5d0c2b";
        //$sql = "SELECT * FROM  `UD_comments` WHERE (conversation_id like '".$conversation_id."' AND sub_id like '".$sub_id."' ) AND ( visibility='all' OR user_id like '".$user_id."' ) ORDER BY timestamp";
        $sql = "SELECT d.sl_no, d.major, d.minor, d.web_page_name,
                case 
                    when cus.email IS NULL then d.email 
                    else cus.email 
                end as email, 
                case 
                    when cus.web_page is NULL then d.web_page 
                    else cus.web_page 
                end as web_page
                FROM `Notification_types` as d left join Notifications_user_settings as cus on sl_no = Notification_type_id and user_id like '".$user_id."'";
        
        try
        {
            $result = mysqli_query($sql_conn, $sql);
            //$res = array();
            while($row = mysqli_fetch_array($result)) {
                $setting = new CNotificationUserSetting();
                $setting->populateFromDBRow($row);
                $res[$setting->major][$setting->minor]["email"] = $setting->email;
                $res[$setting->major][$setting->minor]["web_page"] = $setting->web_page;
                $res[$setting->major][$setting->minor]["web_page_name"] = $setting->web_page_name;
                $res[$setting->major][$setting->minor]["sl_no"] = $setting->sl_no;
            }
        }
        catch (Exception $e){
            $ret["success"] = false;
            $ret['message'] = "Error : ".$e->getMessage();
        }

        $ret["notification_user_settings"] = $res;
        mysqli_close($sql_conn);
        return $ret;
    }

    function update_notification_user_setting($sl_no, $email, $web_page)
    {
        $setting = new CNotificationUserSetting();
        $setting->sl_no = $sl_no;
        $setting->email = $email;
        $setting->web_page = $web_page;
        return $setting->store_to_db();
    }

/* 
// get user specific settings for the notifications
SELECT d.sl_no, d.major, d.minor, d.web_page_name,
case 
	when cus.email IS NULL then d.email 
    else cus.email 
end as email, 
case 
	when cus.web_page is NULL then d.web_page 
    else cus.web_page 
end as web_page
FROM `Notification_types` as d left join Notifications_user_settings as cus on sl_no = Notification_type_id and user_id = 'avishkar-f6dd5d0c2b'

// update notification_user_setting
REPLACE into Notifications_user_settings(user_id, notification_type_id, email, web_page) values('avishkar-f6dd5d0c2b',3,'no','no');
*/

?>