Updated database structure

This commit is contained in:
Pierre HUBERT 2018-08-27 10:42:38 +02:00
parent 90ecca7101
commit 01b66f5026
2 changed files with 62 additions and 64 deletions

View File

@ -28,8 +28,8 @@ class Conversations {
$tablesName = self::LIST_TABLE.", ".self::USERS_TABLE; $tablesName = self::LIST_TABLE.", ".self::USERS_TABLE;
//Prepare conditions //Prepare conditions
$tableJoinCondition = self::LIST_TABLE.".ID = ".self::USERS_TABLE.".ID_".self::LIST_TABLE.""; $tableJoinCondition = self::LIST_TABLE.".id = ".self::USERS_TABLE.".conv_id";
$userCondition = self::USERS_TABLE.".ID_utilisateurs = ?"; $userCondition = self::USERS_TABLE.".user_id = ?";
$orderResults = "ORDER BY ".self::LIST_TABLE.".last_active DESC"; $orderResults = "ORDER BY ".self::LIST_TABLE.".last_active DESC";
//Specify conditions values //Specify conditions values
@ -37,7 +37,7 @@ class Conversations {
//Check if we have to get informations about just one conversation //Check if we have to get informations about just one conversation
if($conversationID != 0){ if($conversationID != 0){
$specificConditions = "AND ".self::LIST_TABLE.".ID = ?"; $specificConditions = "AND ".self::LIST_TABLE.".id = ?";
$conditionsValues[] = $conversationID; $conditionsValues[] = $conversationID;
} }
else else
@ -48,10 +48,10 @@ class Conversations {
//Fields list //Fields list
$requiredFields = array( $requiredFields = array(
self::LIST_TABLE.".ID", self::LIST_TABLE.".id",
self::LIST_TABLE.".last_active", self::LIST_TABLE.".last_active",
self::LIST_TABLE.".name", self::LIST_TABLE.".name",
self::LIST_TABLE.".ID_utilisateurs AS ID_owner", self::LIST_TABLE.".user_id AS owner_id",
self::USERS_TABLE.".following", self::USERS_TABLE.".following",
self::USERS_TABLE.".saw_last_message", self::USERS_TABLE.".saw_last_message",
); );
@ -84,9 +84,9 @@ class Conversations {
//Perform a request on the database //Perform a request on the database
$tableName = self::USERS_TABLE; $tableName = self::USERS_TABLE;
$conditions = "WHERE ID_".self::LIST_TABLE." = ?"; $conditions = "WHERE conv_id = ?";
$conditionsValues = array($conversationID*1); $conditionsValues = array($conversationID*1);
$getFields = array("ID_utilisateurs as userID"); $getFields = array("user_id");
//Perform the request //Perform the request
$results = CS::get()->db->select($tableName, $conditions, $conditionsValues, $getFields); $results = CS::get()->db->select($tableName, $conditions, $conditionsValues, $getFields);
@ -98,7 +98,7 @@ class Conversations {
$membersList = array(); $membersList = array();
foreach($results as $processUser) foreach($results as $processUser)
$membersList[] = $processUser["userID"]; $membersList[] = $processUser["user_id"];
//Return result //Return result
return $membersList; return $membersList;
@ -113,7 +113,7 @@ class Conversations {
public function create(ConversationInfo $conv) : int{ public function create(ConversationInfo $conv) : int{
$mainInformations = array( $mainInformations = array(
"ID_utilisateurs" => $conv->get_id_owner(), "user_id" => $conv->get_id_owner(),
"name" => ($conv->has_name() ? $conv->get_name() : ""), "name" => ($conv->has_name() ? $conv->get_name() : ""),
"last_active" => time(), "last_active" => time(),
"creation_time" => time() "creation_time" => time()
@ -124,7 +124,7 @@ class Conversations {
return 0; //An error occured return 0; //An error occured
//Get the last inserted ID //Get the last inserted ID
$conversationID = CS::get()->db->getLastInsertedID(); $conversationID = db()->getLastInsertedID();
//Check for errors //Check for errors
if($conversationID == 0) if($conversationID == 0)
@ -158,7 +158,7 @@ class Conversations {
//Prepare a request on the database //Prepare a request on the database
$tableName = self::USERS_TABLE; $tableName = self::USERS_TABLE;
$conditions = "WHERE ID_".self::LIST_TABLE." = ? AND ID_utilisateurs = ?"; $conditions = "WHERE conv_id = ? AND user_id = ?";
$values = array( $values = array(
$conversationID, $conversationID,
$userID $userID
@ -187,7 +187,7 @@ class Conversations {
//Prepare the request on the database //Prepare the request on the database
$tableName = self::USERS_TABLE; $tableName = self::USERS_TABLE;
$conditions = "ID_".self::LIST_TABLE." = ? AND ID_utilisateurs = ?"; $conditions = "conv_id = ? AND user_id = ?";
$condVals = array( $condVals = array(
$conversationID, $conversationID,
$userID $userID
@ -216,7 +216,7 @@ class Conversations {
public function changeName(int $conversationID, string $conversationName) : bool{ public function changeName(int $conversationID, string $conversationName) : bool{
//Prepare database request //Prepare database request
$tableName = self::LIST_TABLE; $tableName = self::LIST_TABLE;
$conditions = "ID = ?"; $conditions = "id = ?";
$condVals = array($conversationID); $condVals = array($conversationID);
//Changes //Changes
@ -279,8 +279,8 @@ class Conversations {
//Prepare database request //Prepare database request
$tableName = self::USERS_TABLE; $tableName = self::USERS_TABLE;
$values = array( $values = array(
"ID_".self::LIST_TABLE => $conversationID, "conv_id" => $conversationID,
"ID_utilisateurs" => $userID, "user_id" => $userID,
"time_add" => time(), "time_add" => time(),
"following" => $follow ? 1 : 0, "following" => $follow ? 1 : 0,
"saw_last_message" => 1 "saw_last_message" => 1
@ -300,7 +300,7 @@ class Conversations {
private function removeMember(int $conversationID, int $userID) : bool { private function removeMember(int $conversationID, int $userID) : bool {
//Prepare database request //Prepare database request
$tableName = self::USERS_TABLE; $tableName = self::USERS_TABLE;
$conditions = "ID_".self::LIST_TABLE." = ? AND ID_utilisateurs = ?"; $conditions = "conv_id = ? AND user_id = ?";
$values = array( $values = array(
$conversationID, $conversationID,
$userID $userID
@ -320,10 +320,10 @@ class Conversations {
public function userIsModerator(int $userID, int $conversationID) : bool { public function userIsModerator(int $userID, int $conversationID) : bool {
//Prepare database request //Prepare database request
$tableName = self::LIST_TABLE; $tableName = self::LIST_TABLE;
$conditions = "WHERE ID = ?"; $conditions = "WHERE id = ?";
$values = array($conversationID); $values = array($conversationID);
$requiredFields = array( $requiredFields = array(
"ID_utilisateurs" "user_id"
); );
//Peform a request on the database //Peform a request on the database
@ -338,7 +338,7 @@ class Conversations {
return false; return false;
//Check the first result only //Check the first result only
return $results[0]["ID_utilisateurs"] == $userID; return $results[0]["user_id"] == $userID;
} }
/** /**
@ -349,13 +349,11 @@ class Conversations {
* @return bool TRUE if the user is the owner of the conversation / FALSE else * @return bool TRUE if the user is the owner of the conversation / FALSE else
*/ */
public function isOwnerMessage(int $userID, int $messageID) : bool { public function isOwnerMessage(int $userID, int $messageID) : bool {
return db()->count( return db()->count(
self::MESSAGES_TABLE, self::MESSAGES_TABLE,
"WHERE ID = ? AND ID_utilisateurs = ?", "WHERE id = ? AND user_id = ?",
array($messageID, $userID) array($messageID, $userID)
) > 0; ) > 0;
} }
/** /**
@ -373,17 +371,17 @@ class Conversations {
self::USERS_TABLE." AS table3"; self::USERS_TABLE." AS table3";
//Prepare conditions //Prepare conditions
$joinCondition = "(table1.ID_".self::LIST_TABLE." = table2.ID_".self::LIST_TABLE.")". $joinCondition = "(table1.conv_id = table2.conv_id)".
"AND (table1.ID_".self::LIST_TABLE." = table3.ID_".self::LIST_TABLE.")"; "AND (table1.conv_id = table3.conv_id)";
$whereConditions = "table1.ID_utilisateurs = ? AND table2.ID_utilisateurs = ?"; $whereConditions = "table1.user_id = ? AND table2.user_id = ?";
$groupCondition = "table1.ID_".self::LIST_TABLE." having count(*) = 2"; $groupCondition = "table1.conv_id having count(*) = 2";
//Conditions values //Conditions values
$condValues = array($user1, $user2); $condValues = array($user1, $user2);
//Required fields //Required fields
$requiredFields = array( $requiredFields = array(
"table1.ID_".self::LIST_TABLE." as conversationID", "table1.conv_id as conversationID",
); );
//Build conditions //Build conditions
@ -416,8 +414,8 @@ class Conversations {
//Prepare values //Prepare values
$tableName = self::MESSAGES_TABLE; $tableName = self::MESSAGES_TABLE;
$values = array( $values = array(
"ID_".self::LIST_TABLE => $message->get_conversationID(), "conv_id" => $message->get_conversationID(),
"ID_utilisateurs" => $message->get_userID(), "user_id" => $message->get_userID(),
"time_insert" => time(), "time_insert" => time(),
"message" => $message->has_message() ? $message->get_message() : "" "message" => $message->has_message() ? $message->get_message() : ""
); );
@ -445,7 +443,7 @@ class Conversations {
//Perform a request on the database //Perform a request on the database
$tableName = self::LIST_TABLE; $tableName = self::LIST_TABLE;
$conditions = "ID = ?"; $conditions = "id = ?";
$condVals = array($conversationID); $condVals = array($conversationID);
//Set new values //Set new values
@ -472,12 +470,12 @@ class Conversations {
//Prepare request //Prepare request
$tableName = self::USERS_TABLE; $tableName = self::USERS_TABLE;
$conditions = "ID_".self::LIST_TABLE." = ?"; $conditions = "conv_id = ?";
$condVals = array($conversationID); $condVals = array($conversationID);
//Remove users exceptions //Remove users exceptions
foreach($exceptions as $userID){ foreach($exceptions as $userID){
$conditions.= " AND ID_utilisateurs != ?"; $conditions.= " AND user_id != ?";
$condVals[] = $userID; $condVals[] = $userID;
} }
@ -505,7 +503,7 @@ class Conversations {
//Prepare database request //Prepare database request
$tableName = self::USERS_TABLE; $tableName = self::USERS_TABLE;
$conditions = "ID_".self::LIST_TABLE." = ? AND ID_utilisateurs = ?"; $conditions = "conv_id = ? AND user_id = ?";
$condVals = array( $condVals = array(
$conversationID, $conversationID,
$userID $userID
@ -561,7 +559,7 @@ class Conversations {
public function getLastMessages(int $conversationID, int $numberOfMessages) : array { public function getLastMessages(int $conversationID, int $numberOfMessages) : array {
//Define conditions //Define conditions
$conditions = "WHERE ID_".self::LIST_TABLE." = ? ORDER BY ID DESC LIMIT ".($numberOfMessages*1); $conditions = "WHERE conv_id = ? ORDER BY id DESC LIMIT ".($numberOfMessages*1);
$condVals = array( $condVals = array(
$conversationID $conversationID
); );
@ -586,7 +584,7 @@ class Conversations {
public function getNewMessages(int $conversationID, int $lastMessageID) : array { public function getNewMessages(int $conversationID, int $lastMessageID) : array {
//Define conditions //Define conditions
$conditions = "WHERE ID_".self::LIST_TABLE." = ? AND ID > ? ORDER BY ID"; $conditions = "WHERE conv_id = ? AND ID > ? ORDER BY id";
$condVals = array( $condVals = array(
$conversationID, $conversationID,
$lastMessageID $lastMessageID
@ -610,7 +608,7 @@ class Conversations {
public function getOlderMessages(int $conversationID, int $startID, int $limit) : array { public function getOlderMessages(int $conversationID, int $startID, int $limit) : array {
//Define conditions //Define conditions
$conditions = "WHERE ID_".self::LIST_TABLE." = ? AND ID < ? ORDER BY ID DESC LIMIT ".($limit); $conditions = "WHERE conv_id = ? AND ID < ? ORDER BY id DESC LIMIT ".($limit);
$condVals = array( $condVals = array(
$conversationID, $conversationID,
$startID + 1 $startID + 1
@ -636,7 +634,7 @@ class Conversations {
public function getAllMessages(int $conversationID) : array { public function getAllMessages(int $conversationID) : array {
//Define conditions //Define conditions
$conditions = "WHERE ID_".self::LIST_TABLE." = ? ORDER BY ID"; $conditions = "WHERE conv_id = ? ORDER BY id";
$condVals = array( $condVals = array(
$conversationID $conversationID
); );
@ -659,7 +657,7 @@ class Conversations {
//Perform a request on the database //Perform a request on the database
$tableName = self::LIST_TABLE; $tableName = self::LIST_TABLE;
return CS::get()->db->count($tableName, "WHERE ID = ?", array($convID)) > 0; return CS::get()->db->count($tableName, "WHERE id = ?", array($convID)) > 0;
} }
@ -697,7 +695,7 @@ class Conversations {
public function delete_conversation(int $convID) : bool { public function delete_conversation(int $convID) : bool {
//Get all the messages of the conversation //Get all the messages of the conversation
$messages = $this->getMessages("WHERE ID_".self::LIST_TABLE." = ?", array($convID)); $messages = $this->getMessages("WHERE conv_id = ?", array($convID));
//Delete each message //Delete each message
foreach($messages as $message){ foreach($messages as $message){
@ -732,7 +730,7 @@ class Conversations {
//Get all the messages of member the conversation //Get all the messages of member the conversation
$messages = $this->getMessages( $messages = $this->getMessages(
"WHERE ID_".self::LIST_TABLE." = ? AND ID_utilisateurs = ?", "WHERE conv_id = ? AND user_id = ?",
array($convID, $memberID)); array($convID, $memberID));
//Delete each message //Delete each message
@ -758,7 +756,7 @@ class Conversations {
public function deleteConversationMessage(int $messageID) : bool { public function deleteConversationMessage(int $messageID) : bool {
//Get information about the message //Get information about the message
$messages = $this->getMessages("WHERE ID = ?", array($messageID)); $messages = $this->getMessages("WHERE id = ?", array($messageID));
if(count($messages) < 1) if(count($messages) < 1)
return FALSE; //Message not found return FALSE; //Message not found
@ -802,7 +800,7 @@ class Conversations {
private function delete_all_members(int $convID) : bool { private function delete_all_members(int $convID) : bool {
//Prepare request on the database //Prepare request on the database
$conditions = "ID_".self::LIST_TABLE." = ?"; $conditions = "conv_id = ?";
$values = array( $values = array(
$convID $convID
); );
@ -819,7 +817,7 @@ class Conversations {
* @return bool True in case of success / false else * @return bool True in case of success / false else
*/ */
private function delete_conversation_entry(int $convID) : bool { private function delete_conversation_entry(int $convID) : bool {
return CS::get()->db->deleteEntry(self::LIST_TABLE, "ID = ?", array($convID)); return CS::get()->db->deleteEntry(self::LIST_TABLE, "id = ?", array($convID));
} }
/** /**
@ -832,7 +830,7 @@ class Conversations {
//Prepare database request //Prepare database request
$tableName = self::USERS_TABLE; $tableName = self::USERS_TABLE;
$conditions = "WHERE ID_utilisateurs = ? AND saw_last_message = 0 AND following = 1"; $conditions = "WHERE user_id = ? AND saw_last_message = 0 AND following = 1";
$values = array($userID); $values = array($userID);
//Perform request and return result //Perform request and return result
@ -850,8 +848,8 @@ class Conversations {
//Perform the request on the server //Perform the request on the server
$tablesName = self::USERS_TABLE." as users, ".self::LIST_TABLE." as list, ".self::MESSAGES_TABLE." as messages"; $tablesName = self::USERS_TABLE." as users, ".self::LIST_TABLE." as list, ".self::MESSAGES_TABLE." as messages";
$conditions = "WHERE users.ID_utilisateurs = ? AND users.following = 1 AND users.saw_last_message = 0 AND users.ID_comunic_conversations_list = list.ID $conditions = "WHERE users.user_id = ? AND users.following = 1 AND users.saw_last_message = 0 AND users.conv_id = list.id
AND list.ID = messages.ID_comunic_conversations_list AND list.last_active = messages.time_insert"; AND list.id = messages.conv_id AND list.last_active = messages.time_insert";
$values = array($userID); $values = array($userID);
//Perform the request //Perform the request
@ -879,7 +877,7 @@ class Conversations {
public function getAllUserMessages(int $userID) : array { public function getAllUserMessages(int $userID) : array {
//Define conditions //Define conditions
$conditions = "WHERE ID_utilisateurs = ? "; $conditions = "WHERE user_id = ? ";
$condVals = array( $condVals = array(
$userID $userID
); );
@ -948,8 +946,8 @@ class Conversations {
//Define required fields //Define required fields
$requiredFields = array( $requiredFields = array(
"ID", "id",
"ID_utilisateurs AS ID_user", "user_id",
"image_path", "image_path",
"message", "message",
"time_insert" "time_insert"
@ -976,14 +974,14 @@ class Conversations {
$conv = new ConversationInfo(); $conv = new ConversationInfo();
$conv->set_id($entry["ID"]); $conv->set_id($entry["id"]);
$conv->set_id_owner($entry["ID_owner"]); $conv->set_id_owner($entry["owner_id"]);
$conv->set_last_active($entry["last_active"]); $conv->set_last_active($entry["last_active"]);
if($entry["name"] != null) if($entry["name"] != null)
$conv->set_name($entry["name"]); $conv->set_name($entry["name"]);
$conv->set_following($entry["following"] == 1); $conv->set_following($entry["following"] == 1);
$conv->set_saw_last_message($entry["saw_last_message"] == 1); $conv->set_saw_last_message($entry["saw_last_message"] == 1);
$conv->set_members($this->getConversationMembers($entry["ID"])); $conv->set_members($this->getConversationMembers($entry["id"]));
return $conv; return $conv;
@ -999,8 +997,8 @@ class Conversations {
$message = new ConversationMessage(); $message = new ConversationMessage();
$message->set_id($entry["ID"]); $message->set_id($entry["id"]);
$message->set_userID($entry["ID_user"]); $message->set_userID($entry["user_id"]);
$message->set_time_sent($entry["time_insert"]); $message->set_time_sent($entry["time_insert"]);
if($entry["image_path"] != null) if($entry["image_path"] != null)
$message->set_image_path($entry["image_path"]); $message->set_image_path($entry["image_path"]);
@ -1021,11 +1019,11 @@ class Conversations {
$conversation = new UnreadConversation(); $conversation = new UnreadConversation();
$conversation->set_id($entry["ID_comunic_conversations_list"]); $conversation->set_id($entry["conv_id"]);
if($entry["name"] != null) if($entry["name"] != null)
$conversation->set_conv_name($entry["name"]); $conversation->set_conv_name($entry["name"]);
$conversation->set_last_active($entry["last_active"]); $conversation->set_last_active($entry["last_active"]);
$conversation->set_userID($entry["ID_utilisateurs"]); $conversation->set_userID($entry["user_id"]);
if($entry["message"] != null) if($entry["message"] != null)
$conversation->set_message($entry["message"]); $conversation->set_message($entry["message"]);

View File

@ -105,8 +105,8 @@ CREATE TABLE `comunic_API_userLoginToken` (
DROP TABLE IF EXISTS `comunic_conversations_list`; DROP TABLE IF EXISTS `comunic_conversations_list`;
CREATE TABLE `comunic_conversations_list` ( CREATE TABLE `comunic_conversations_list` (
`ID` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
`ID_utilisateurs` int(11) DEFAULT NULL, `user_id` int(11) DEFAULT NULL,
`name` varchar(50) DEFAULT NULL, `name` varchar(50) DEFAULT NULL,
`last_active` int(11) DEFAULT NULL, `last_active` int(11) DEFAULT NULL,
`creation_time` int(11) DEFAULT NULL, `creation_time` int(11) DEFAULT NULL,
@ -116,9 +116,9 @@ CREATE TABLE `comunic_conversations_list` (
DROP TABLE IF EXISTS `comunic_conversations_messages`; DROP TABLE IF EXISTS `comunic_conversations_messages`;
CREATE TABLE `comunic_conversations_messages` ( CREATE TABLE `comunic_conversations_messages` (
`ID` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
`ID_comunic_conversations_list` int(11) DEFAULT NULL, `conv_id` int(11) DEFAULT NULL,
`ID_utilisateurs` int(11) DEFAULT NULL, `user_id` int(11) DEFAULT NULL,
`time_insert` int(11) DEFAULT NULL, `time_insert` int(11) DEFAULT NULL,
`message` varchar(200) DEFAULT NULL, `message` varchar(200) DEFAULT NULL,
`image_path` varchar(100) DEFAULT NULL, `image_path` varchar(100) DEFAULT NULL,
@ -128,9 +128,9 @@ CREATE TABLE `comunic_conversations_messages` (
DROP TABLE IF EXISTS `comunic_conversations_users`; DROP TABLE IF EXISTS `comunic_conversations_users`;
CREATE TABLE `comunic_conversations_users` ( CREATE TABLE `comunic_conversations_users` (
`ID` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
`ID_comunic_conversations_list` int(11) DEFAULT NULL, `conv_id` int(11) DEFAULT NULL,
`ID_utilisateurs` int(11) DEFAULT NULL, `user_id` int(11) DEFAULT NULL,
`time_add` int(11) DEFAULT NULL, `time_add` int(11) DEFAULT NULL,
`following` int(1) DEFAULT '0', `following` int(1) DEFAULT '0',
`saw_last_message` int(1) DEFAULT NULL, `saw_last_message` int(1) DEFAULT NULL,