1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace ChatApp; |
4
|
|
|
require_once (dirname(__DIR__) . '/config/database.php'); |
5
|
|
|
use ChatApp\Time; |
6
|
|
|
use Dotenv\Dotenv; |
7
|
|
|
$dotenv = new Dotenv(dirname(__DIR__)); |
8
|
|
|
$dotenv->load(); |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Fetching the sidebar results |
13
|
|
|
*/ |
14
|
|
|
class SideBar |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
protected $obTime; |
18
|
|
|
protected $array; |
19
|
|
|
protected $connect; |
20
|
|
|
|
21
|
|
|
public function __construct() |
22
|
|
|
{ |
23
|
|
|
$this->connect = mysqli_connect( |
24
|
|
|
getenv('DB_HOST'), |
25
|
|
|
getenv('DB_USER'), |
26
|
|
|
getenv('DB_PASSWORD'), |
27
|
|
|
getenv('DB_NAME') |
28
|
|
|
); |
29
|
|
|
$this->obTime = new Time(); |
30
|
|
|
$this->array = array(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function loadSideBar($userId) |
34
|
|
|
{ |
35
|
|
|
if(isset($userId)) |
36
|
|
|
{ |
37
|
|
|
$query = "SELECT * FROM total_message WHERE user1='$userId' or user2='$userId' ORDER BY id DESC"; |
38
|
|
|
if($result = $this->connect->query($query)) |
39
|
|
|
{ |
40
|
|
|
if ($result->num_rows > 0) |
41
|
|
|
{ |
42
|
|
|
$length = strlen($userId); |
43
|
|
|
while($row = $result->fetch_assoc()) |
44
|
|
|
{ |
45
|
|
|
$identifier = $row['identifier']; |
46
|
|
|
$substring = substr($identifier, 0, $length); |
47
|
|
|
if($substring != $userId) |
48
|
|
|
{ |
49
|
|
|
$this->data($substring, $row); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
else |
53
|
|
|
{ |
54
|
|
|
$substring = substr($identifier, $length+1); |
55
|
|
|
$this->Data($substring, $row); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
return json_encode($this->array); |
59
|
|
|
} |
60
|
|
|
else |
61
|
|
|
{ |
62
|
|
|
return json_encode(null); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
else |
66
|
|
|
{ |
67
|
|
|
echo "Query Failed"; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
else |
71
|
|
|
{ |
72
|
|
|
header('Location:../login.php'); |
73
|
|
|
} |
74
|
|
|
$this->connect->close(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function data($userId, $row) |
78
|
|
|
{ |
79
|
|
|
$query = "SELECT username, name, login_status, login_id from login where login_id = '$userId'"; |
80
|
|
|
if($result = $this->connect->query($query)) |
81
|
|
|
{ |
82
|
|
|
if($result->num_rows > 0) |
83
|
|
|
{ |
84
|
|
|
$fetch = $result->fetch_assoc(); |
85
|
|
|
$row['time'] = $this->obTime->timeConversion($row['time']); |
86
|
|
|
$fetch = array_merge($fetch, ['time' => $row['time']]); |
87
|
|
|
$this->array = array_merge($this->array, [$fetch]); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.