Use constants to define tables name

This commit is contained in:
Pierre HUBERT 2018-08-27 10:18:44 +02:00
parent 382f816ad8
commit 90ecca7101

View File

@ -8,29 +8,11 @@
class Conversations { class Conversations {
/** /**
* @var String $conversationsListTable Name of the conversation list table * Tables name definition
*/ */
private $conversationsListTable; const LIST_TABLE = DBprefix."conversations_list";
const USERS_TABLE = DBprefix."conversations_users";
/** const MESSAGES_TABLE = DBprefix."conversations_messages";
* @var String $conversationsUsersTable Name of the conversation users table
*/
private $conversationsUsersTable;
/**
* @var String $conversationMessagesTabel Name of the conversation messages table
*/
private $conversationsMessagesTable;
/**
* Public constructor
*/
public function __construct(){
$this->conversationsListTable = CS::get()->config->get("dbprefix")."conversations_list";
$this->conversationsUsersTable = CS::get()->config->get("dbprefix")."conversations_users";
$this->conversationsMessagesTable = CS::get()->config->get("dbprefix")."conversations_messages";
}
/** /**
* Get the conversations list of a specified user * Get the conversations list of a specified user
@ -43,19 +25,19 @@ class Conversations {
public function getList(int $userID, int $conversationID = 0){ public function getList(int $userID, int $conversationID = 0){
//Prepare database request //Prepare database request
$tablesName = $this->conversationsListTable.", ".$this->conversationsUsersTable; $tablesName = self::LIST_TABLE.", ".self::USERS_TABLE;
//Prepare conditions //Prepare conditions
$tableJoinCondition = $this->conversationsListTable.".ID = ".$this->conversationsUsersTable.".ID_".$this->conversationsListTable.""; $tableJoinCondition = self::LIST_TABLE.".ID = ".self::USERS_TABLE.".ID_".self::LIST_TABLE."";
$userCondition = $this->conversationsUsersTable.".ID_utilisateurs = ?"; $userCondition = self::USERS_TABLE.".ID_utilisateurs = ?";
$orderResults = "ORDER BY ".$this->conversationsListTable.".last_active DESC"; $orderResults = "ORDER BY ".self::LIST_TABLE.".last_active DESC";
//Specify conditions values //Specify conditions values
$conditionsValues = array($userID); $conditionsValues = array($userID);
//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 ".$this->conversationsListTable.".ID = ?"; $specificConditions = "AND ".self::LIST_TABLE.".ID = ?";
$conditionsValues[] = $conversationID; $conditionsValues[] = $conversationID;
} }
else else
@ -66,12 +48,12 @@ class Conversations {
//Fields list //Fields list
$requiredFields = array( $requiredFields = array(
$this->conversationsListTable.".ID", self::LIST_TABLE.".ID",
$this->conversationsListTable.".last_active", self::LIST_TABLE.".last_active",
$this->conversationsListTable.".name", self::LIST_TABLE.".name",
$this->conversationsListTable.".ID_utilisateurs AS ID_owner", self::LIST_TABLE.".ID_utilisateurs AS ID_owner",
$this->conversationsUsersTable.".following", self::USERS_TABLE.".following",
$this->conversationsUsersTable.".saw_last_message", self::USERS_TABLE.".saw_last_message",
); );
//Perform database request //Perform database request
@ -101,8 +83,8 @@ class Conversations {
public function getConversationMembers(int $conversationID) : array { public function getConversationMembers(int $conversationID) : array {
//Perform a request on the database //Perform a request on the database
$tableName = $this->conversationsUsersTable; $tableName = self::USERS_TABLE;
$conditions = "WHERE ID_".$this->conversationsListTable." = ?"; $conditions = "WHERE ID_".self::LIST_TABLE." = ?";
$conditionsValues = array($conversationID*1); $conditionsValues = array($conversationID*1);
$getFields = array("ID_utilisateurs as userID"); $getFields = array("ID_utilisateurs as userID");
@ -138,7 +120,7 @@ class Conversations {
); );
//First, insert the conversation in the main table //First, insert the conversation in the main table
if(!CS::get()->db->addLine($this->conversationsListTable, $mainInformations)) if(!CS::get()->db->addLine(self::LIST_TABLE, $mainInformations))
return 0; //An error occured return 0; //An error occured
//Get the last inserted ID //Get the last inserted ID
@ -175,8 +157,8 @@ class Conversations {
public function userBelongsTo(int $userID, int $conversationID) : bool { public function userBelongsTo(int $userID, int $conversationID) : bool {
//Prepare a request on the database //Prepare a request on the database
$tableName = $this->conversationsUsersTable; $tableName = self::USERS_TABLE;
$conditions = "WHERE ID_".$this->conversationsListTable." = ? AND ID_utilisateurs = ?"; $conditions = "WHERE ID_".self::LIST_TABLE." = ? AND ID_utilisateurs = ?";
$values = array( $values = array(
$conversationID, $conversationID,
$userID $userID
@ -204,8 +186,8 @@ class Conversations {
public function changeFollowState(int $userID, int $conversationID, bool $follow) : bool{ public function changeFollowState(int $userID, int $conversationID, bool $follow) : bool{
//Prepare the request on the database //Prepare the request on the database
$tableName = $this->conversationsUsersTable; $tableName = self::USERS_TABLE;
$conditions = "ID_".$this->conversationsListTable." = ? AND ID_utilisateurs = ?"; $conditions = "ID_".self::LIST_TABLE." = ? AND ID_utilisateurs = ?";
$condVals = array( $condVals = array(
$conversationID, $conversationID,
$userID $userID
@ -233,7 +215,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 = $this->conversationsListTable; $tableName = self::LIST_TABLE;
$conditions = "ID = ?"; $conditions = "ID = ?";
$condVals = array($conversationID); $condVals = array($conversationID);
@ -295,9 +277,9 @@ class Conversations {
private function addMember(int $conversationID, int $userID, bool $follow = false) : bool { private function addMember(int $conversationID, int $userID, bool $follow = false) : bool {
//Prepare database request //Prepare database request
$tableName = $this->conversationsUsersTable; $tableName = self::USERS_TABLE;
$values = array( $values = array(
"ID_".$this->conversationsListTable => $conversationID, "ID_".self::LIST_TABLE => $conversationID,
"ID_utilisateurs" => $userID, "ID_utilisateurs" => $userID,
"time_add" => time(), "time_add" => time(),
"following" => $follow ? 1 : 0, "following" => $follow ? 1 : 0,
@ -317,8 +299,8 @@ 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 = $this->conversationsUsersTable; $tableName = self::USERS_TABLE;
$conditions = "ID_".$this->conversationsListTable." = ? AND ID_utilisateurs = ?"; $conditions = "ID_".self::LIST_TABLE." = ? AND ID_utilisateurs = ?";
$values = array( $values = array(
$conversationID, $conversationID,
$userID $userID
@ -337,7 +319,7 @@ 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 = $this->conversationsListTable; $tableName = self::LIST_TABLE;
$conditions = "WHERE ID = ?"; $conditions = "WHERE ID = ?";
$values = array($conversationID); $values = array($conversationID);
$requiredFields = array( $requiredFields = array(
@ -369,7 +351,7 @@ class Conversations {
public function isOwnerMessage(int $userID, int $messageID) : bool { public function isOwnerMessage(int $userID, int $messageID) : bool {
return db()->count( return db()->count(
$this->conversationsMessagesTable, self::MESSAGES_TABLE,
"WHERE ID = ? AND ID_utilisateurs = ?", "WHERE ID = ? AND ID_utilisateurs = ?",
array($messageID, $userID) array($messageID, $userID)
) > 0; ) > 0;
@ -386,22 +368,22 @@ class Conversations {
public function findPrivate(int $user1, int $user2) : array{ public function findPrivate(int $user1, int $user2) : array{
//Prepare database request //Prepare database request
$tableName = $this->conversationsUsersTable." AS table1 JOIN ". $tableName = self::USERS_TABLE." AS table1 JOIN ".
$this->conversationsUsersTable." AS table2 JOIN ". self::USERS_TABLE." AS table2 JOIN ".
$this->conversationsUsersTable." AS table3"; self::USERS_TABLE." AS table3";
//Prepare conditions //Prepare conditions
$joinCondition = "(table1.ID_".$this->conversationsListTable." = table2.ID_".$this->conversationsListTable.")". $joinCondition = "(table1.ID_".self::LIST_TABLE." = table2.ID_".self::LIST_TABLE.")".
"AND (table1.ID_".$this->conversationsListTable." = table3.ID_".$this->conversationsListTable.")"; "AND (table1.ID_".self::LIST_TABLE." = table3.ID_".self::LIST_TABLE.")";
$whereConditions = "table1.ID_utilisateurs = ? AND table2.ID_utilisateurs = ?"; $whereConditions = "table1.ID_utilisateurs = ? AND table2.ID_utilisateurs = ?";
$groupCondition = "table1.ID_".$this->conversationsListTable." having count(*) = 2"; $groupCondition = "table1.ID_".self::LIST_TABLE." 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_".$this->conversationsListTable." as conversationID", "table1.ID_".self::LIST_TABLE." as conversationID",
); );
//Build conditions //Build conditions
@ -432,9 +414,9 @@ class Conversations {
private function insertMessage(NewConversationMessage $message) : bool { private function insertMessage(NewConversationMessage $message) : bool {
//Prepare values //Prepare values
$tableName = $this->conversationsMessagesTable; $tableName = self::MESSAGES_TABLE;
$values = array( $values = array(
"ID_".$this->conversationsListTable => $message->get_conversationID(), "ID_".self::LIST_TABLE => $message->get_conversationID(),
"ID_utilisateurs" => $message->get_userID(), "ID_utilisateurs" => $message->get_userID(),
"time_insert" => time(), "time_insert" => time(),
"message" => $message->has_message() ? $message->get_message() : "" "message" => $message->has_message() ? $message->get_message() : ""
@ -462,7 +444,7 @@ class Conversations {
private function updateLastActive(int $conversationID, int $time) : bool{ private function updateLastActive(int $conversationID, int $time) : bool{
//Perform a request on the database //Perform a request on the database
$tableName = $this->conversationsListTable; $tableName = self::LIST_TABLE;
$conditions = "ID = ?"; $conditions = "ID = ?";
$condVals = array($conversationID); $condVals = array($conversationID);
@ -489,8 +471,8 @@ class Conversations {
private function allUsersAsUnread(int $conversationID, array $exceptions) : bool{ private function allUsersAsUnread(int $conversationID, array $exceptions) : bool{
//Prepare request //Prepare request
$tableName = $this->conversationsUsersTable; $tableName = self::USERS_TABLE;
$conditions = "ID_".$this->conversationsListTable." = ?"; $conditions = "ID_".self::LIST_TABLE." = ?";
$condVals = array($conversationID); $condVals = array($conversationID);
//Remove users exceptions //Remove users exceptions
@ -522,8 +504,8 @@ class Conversations {
public function markUserAsRead(int $userID, int $conversationID) : bool { public function markUserAsRead(int $userID, int $conversationID) : bool {
//Prepare database request //Prepare database request
$tableName = $this->conversationsUsersTable; $tableName = self::USERS_TABLE;
$conditions = "ID_".$this->conversationsListTable." = ? AND ID_utilisateurs = ?"; $conditions = "ID_".self::LIST_TABLE." = ? AND ID_utilisateurs = ?";
$condVals = array( $condVals = array(
$conversationID, $conversationID,
$userID $userID
@ -579,7 +561,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_".$this->conversationsListTable." = ? ORDER BY ID DESC LIMIT ".($numberOfMessages*1); $conditions = "WHERE ID_".self::LIST_TABLE." = ? ORDER BY ID DESC LIMIT ".($numberOfMessages*1);
$condVals = array( $condVals = array(
$conversationID $conversationID
); );
@ -604,7 +586,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_".$this->conversationsListTable." = ? AND ID > ? ORDER BY ID"; $conditions = "WHERE ID_".self::LIST_TABLE." = ? AND ID > ? ORDER BY ID";
$condVals = array( $condVals = array(
$conversationID, $conversationID,
$lastMessageID $lastMessageID
@ -628,7 +610,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_".$this->conversationsListTable." = ? AND ID < ? ORDER BY ID DESC LIMIT ".($limit); $conditions = "WHERE ID_".self::LIST_TABLE." = ? AND ID < ? ORDER BY ID DESC LIMIT ".($limit);
$condVals = array( $condVals = array(
$conversationID, $conversationID,
$startID + 1 $startID + 1
@ -654,7 +636,7 @@ class Conversations {
public function getAllMessages(int $conversationID) : array { public function getAllMessages(int $conversationID) : array {
//Define conditions //Define conditions
$conditions = "WHERE ID_".$this->conversationsListTable." = ? ORDER BY ID"; $conditions = "WHERE ID_".self::LIST_TABLE." = ? ORDER BY ID";
$condVals = array( $condVals = array(
$conversationID $conversationID
); );
@ -675,7 +657,7 @@ class Conversations {
public function exist(int $convID) : bool { public function exist(int $convID) : bool {
//Perform a request on the database //Perform a request on the database
$tableName = $this->conversationsListTable; $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;
@ -715,7 +697,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_".$this->conversationsListTable." = ?", array($convID)); $messages = $this->getMessages("WHERE ID_".self::LIST_TABLE." = ?", array($convID));
//Delete each message //Delete each message
foreach($messages as $message){ foreach($messages as $message){
@ -750,7 +732,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_".$this->conversationsListTable." = ? AND ID_utilisateurs = ?", "WHERE ID_".self::LIST_TABLE." = ? AND ID_utilisateurs = ?",
array($convID, $memberID)); array($convID, $memberID));
//Delete each message //Delete each message
@ -807,7 +789,7 @@ class Conversations {
//Delete message from the database //Delete message from the database
$conditions = "ID = ?"; $conditions = "ID = ?";
$condValues = array($message->get_id()); $condValues = array($message->get_id());
return CS::get()->db->deleteEntry($this->conversationsMessagesTable, $conditions, $condValues); return CS::get()->db->deleteEntry(self::MESSAGES_TABLE, $conditions, $condValues);
} }
@ -820,13 +802,13 @@ 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_".$this->conversationsListTable." = ?"; $conditions = "ID_".self::LIST_TABLE." = ?";
$values = array( $values = array(
$convID $convID
); );
//Try to perform request //Try to perform request
return CS::get()->db->deleteEntry($this->conversationsUsersTable, $conditions, $values); return CS::get()->db->deleteEntry(self::USERS_TABLE, $conditions, $values);
} }
@ -837,7 +819,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($this->conversationsListTable, "ID = ?", array($convID)); return CS::get()->db->deleteEntry(self::LIST_TABLE, "ID = ?", array($convID));
} }
/** /**
@ -849,7 +831,7 @@ class Conversations {
public function number_user_unread(int $userID) : int { public function number_user_unread(int $userID) : int {
//Prepare database request //Prepare database request
$tableName = $this->conversationsUsersTable; $tableName = self::USERS_TABLE;
$conditions = "WHERE ID_utilisateurs = ? AND saw_last_message = 0 AND following = 1"; $conditions = "WHERE ID_utilisateurs = ? AND saw_last_message = 0 AND following = 1";
$values = array($userID); $values = array($userID);
@ -867,7 +849,7 @@ class Conversations {
public function get_list_unread(int $userID) : array { public function get_list_unread(int $userID) : array {
//Perform the request on the server //Perform the request on the server
$tablesName = $this->conversationsUsersTable." as users, ".$this->conversationsListTable." as list, ".$this->conversationsMessagesTable." 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.ID_utilisateurs = ? AND users.following = 1 AND users.saw_last_message = 0 AND users.ID_comunic_conversations_list = list.ID
AND list.ID = messages.ID_comunic_conversations_list AND list.last_active = messages.time_insert"; AND list.ID = messages.ID_comunic_conversations_list AND list.last_active = messages.time_insert";
$values = array($userID); $values = array($userID);
@ -962,7 +944,7 @@ class Conversations {
private function getMessages(string $conditions, array $conditionsValues = array()) : array{ private function getMessages(string $conditions, array $conditionsValues = array()) : array{
//Prepare database request //Prepare database request
$tableName = $this->conversationsMessagesTable; $tableName = self::MESSAGES_TABLE;
//Define required fields //Define required fields
$requiredFields = array( $requiredFields = array(