<?php 

require("config/conn.php");


function getNoData($name, $filter, $filtervalue){

    global $conn;

    if ($filter == "") {
        $sql = "SELECT * FROM `$name`";

        $query_run = mysqli_query($conn, $sql);

        if ($query_run) {
           $data = mysqli_num_rows($query_run);
        } else{
            $data = "Error Occured";
        }
        
    } else{
        $sql = "SELECT * FROM `$name` WHERE `$filter`='$filtervalue'";

        $query_run = mysqli_query($conn, $sql);

        if ($query_run) {
           $data = mysqli_num_rows($query_run);
        } else{
            $data = "Error Occured";
        }
    }

    return $data;
}


function filter($data){
    global $conn;
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);

    $data = mysqli_real_escape_string($conn, $data);
  
 
  
    return $data;
  }


  function getDailyQuizSetting($data){

    global $conn;
    $sql = "SELECT * FROM `dailyquizsettings` WHERE `name`='$data' LIMIT 1";

    $query_run  = mysqli_query($conn, $sql);

    if ($query_run) {
        if (mysqli_num_rows($query_run) > 0) {
            foreach ($query_run as $row) {
                $data = $row['value'];
            } 
        } else {
            $data = "Make Setting First!";
        }
    } else {
        $data = "ERROR!";
    }
    
    return $data;
  }

  function getSettingValue($table, $data){

    global $conn;
    $sql = "SELECT * FROM `$table` WHERE `name`='$data' LIMIT 1";

    $query_run  = mysqli_query($conn, $sql);

    if ($query_run) {
        if (mysqli_num_rows($query_run) > 0) {
            foreach ($query_run as $row) {
                $data = $row['value'];
            } 
        } else {
            $data = "Some Error";
        }
    } else {
        $data = "ERROR!";
    }
    
    return $data;
  }
  function getSettingADValue($table, $data){

    global $conn;
    $sql = "SELECT * FROM `$table` WHERE `name`='$data' LIMIT 1";

    $query_run  = mysqli_query($conn, $sql);

    if ($query_run) {
        if (mysqli_num_rows($query_run) > 0) {
            foreach ($query_run as $row) {
                $data = $row['adId'];
            } 
        } else {
            $data = "Some Error";
        }
    } else {
        $data = "ERROR!";
    }
    
    return $data;
  }









  function getAdminLoginDetails($table, $value){

    global $conn;
    $sql = "SELECT * FROM `$table` LIMIT 1";

    $query_run  = mysqli_query($conn, $sql);

    if ($query_run) {
        if (mysqli_num_rows($query_run) > 0) {
            foreach ($query_run as $row) {
                $data = $row["$value"];
            } 
        } else {
            $data = "Some Error";
        }
    } else {
        $data = "ERROR!";
    }
    
    return $data;
  }








  
function sendNotification($to, $notif){
global $conn;

$feilds = array('to'=>$to, 'notification'=>$notif);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($feilds));

$fetchKey="SELECT * FROM `basicsettings` WHERE `name`='Firebase Key'";
$fetch=mysqli_query($conn,$fetchKey);

if ($fetch) {
    foreach ($fetch as $row) {
        $key = $row['value'];
      }
} else{
    $_SESSION['status'] = "Error";
    $_SESSION['message'] = "Some Error Occured !";
    $_SESSION['icon'] =  'error';
    header('Location: notifications.php');
}



$headers = array();
$headers[] = 'Authorization: Key='.$key.'';
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);


return 'Success';
}


function getAppInfo($table, $data){

    global $conn;
    $sql = "SELECT * FROM `$table` WHERE `name`='$data' LIMIT 1";

    $query_run  = mysqli_query($conn, $sql);

    if ($query_run) {
        if (mysqli_num_rows($query_run) > 0) {
            foreach ($query_run as $row) {
                $data = $row['description'];
            } 
        } else {
            $data = "Some Error";
        }
    } else {
        $data = "ERROR!";
    }
    
    return $data;
  }


?>