@ -20,10 +20,10 @@ class Connection {
class Model extends Connection {
class Model extends Connection {
public function getUsers($id){
public function getUsers($id){
include "../../creds.php";
//$sql="SELECT * FROM users WHERE user_id != ?";
//$sql="SELECT * FROM users WHERE user_id != ?";
$url = (isset($_SERVER['HTTPS']) & & $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://".$_SERVER['HTTP_HOST']."/paredu";
$pdo = $this->connect();
$pdo = $this->connect();
$pdo->exec('USE paredu ');
$pdo->exec('USE '.$dbname );
$sql = "SELECT * FROM `auth` WHERE `id` != ?";
$sql = "SELECT * FROM `auth` WHERE `id` != ?";
$stmt=$pdo->prepare($sql);
$stmt=$pdo->prepare($sql);
$stmt->execute([$id]);
$stmt->execute([$id]);
@ -33,20 +33,51 @@ class Model extends Connection {
for($i = 0; $i< count ( $ fetch ) ; $ i + + ) {
for($i = 0; $i< count ( $ fetch ) ; $ i + + ) {
$sndarray = array();
$sndarray = array();
$sndarray["id"] = $fetch[$i]["id"];
$sndarray["id"] = $fetch[$i]["id"];
if($fetch[$i]["fullname"] != 0 ){
if($fetch[$i]["fullname"] != "" ){
$sndarray["name"] = $fetch[$i]["fullname"];
$sndarray["name"] = $fetch[$i]["fullname"];
}else{
}else{
$sndarray["name"] = $fetch[$i]["username"];
$sndarray["name"] = $fetch[$i]["username"];
}
}
$sndarray["userimg"] = $url."/API/request.php?type=image&subtype=user&id=".$fetch[$i]["id"];
$sndarray["userimg"] = $siteurl."/API/request.php?type=image&subtype=user&id=".$fetch[$i]["id"];
$pdo->exec('USE '.$dbname."_chat");
$sql = "SELECT * FROM notifications WHERE userid = ? AND fromuserid = ?; ";
$stmtcheck=$pdo->prepare($sql);
$stmtcheck->execute([$id, $fetch[$i]["id"]]);
$resultcheck = $stmtcheck->fetchAll();
if($stmtcheck & & $stmtcheck->rowCount() > 0){
$sndarray["unread"] = $resultcheck[0]["unread"];
}else{
$sndarray["unread"] = 0;
}
$result[$i] = $sndarray;
$result[$i] = $sndarray;
}
}
return $result;
return $result;
}
}
public function insertChat($id,$user2_id, $message, $time){
public function insertChat($id,$user2_id, $message, $time){
$conn = $this->connect();
$sql = "SELECT * FROM notifications WHERE userid = ? AND fromuserid = ?; ";
$stmtcheck=$conn->prepare($sql);
$stmtcheck->execute([$user2_id, $id]);
$result = $stmtcheck->fetchAll();
if($stmtcheck & & $stmtcheck->rowCount() > 0){
$unread = $result[0]["unread"];
}
if(isset($unread)){
$sql = "UPDATE notifications SET unread = ? WHERE userid = ? AND fromuserid = ?;";
$stmtinsert=$conn->prepare($sql);
$stmtinsert->execute([($unread+1), $user2_id, $id]);
}else{
$sql="INSERT INTO notifications (userid, fromuserid, unread) VALUES (?, ?, 1);";
$stmtinsert=$conn->prepare($sql);
$stmtinsert->execute([$user2_id, $id]);
}
$sql="INSERT INTO chats (chat_user_id, chat_user2_id, chat_message, chat_sent_by, chat_date) VALUES(?,?,?,?,?)";
$sql="INSERT INTO chats (chat_user_id, chat_user2_id, chat_message, chat_sent_by, chat_date) VALUES(?,?,?,?,?)";
$stmt=$this->connect()->prepare($sql);
$stmt=$conn->prepare($sql);
$stmt->execute([$id,$user2_id, $message, $id, $time]);
$stmt->execute([$id,$user2_id, $message, $id, $time]);
$result= $stmt->fetchAll();
$result= $stmt->fetchAll();
return $result;
return $result;
@ -61,8 +92,19 @@ class Model extends Connection {
}
}
public function getChats($id1, $id2){
public function getChats($id1, $id2){
$conn = $this->connect();
$sql = "SELECT * FROM notifications WHERE userid = ? AND fromuserid = ?";
$stmtcheck=$conn->prepare($sql);
$stmtcheck->execute([$id1, $id2]);
$result = $stmtcheck->fetchAll();
if($stmtcheck->rowCount() > 0){
$sql = "DELETE FROM notifications WHERE userid = ? AND fromuserid = ?";
$stmtinsert=$conn->prepare($sql);
$stmtinsert->execute([$id1, $id2]);
}
$sql="SELECT * FROM chats WHERE chat_user_id = ? and chat_user2_id=? OR chat_user_id=? and chat_user2_id=?";
$sql="SELECT * FROM chats WHERE chat_user_id = ? and chat_user2_id=? OR chat_user_id=? and chat_user2_id=?";
$stmt=$this->connect()->prepare($sql);
$stmt=$conn->prepare($sql);
$stmt->execute([$id1, $id2, $id2, $id1]);
$stmt->execute([$id1, $id2, $id2, $id1]);
$result=$stmt->fetchAll();
$result=$stmt->fetchAll();
return $result;
return $result;