1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Search Class Doc Comment |
4
|
|
|
* |
5
|
|
|
* PHP version 5 |
6
|
|
|
* |
7
|
|
|
* @category PHP |
8
|
|
|
* @package OpenChat |
9
|
|
|
* @author Ankit Jain <[email protected]> |
10
|
|
|
* @license The MIT License (MIT) |
11
|
|
|
* @link https://github.com/ankitjain28may/openchat |
12
|
|
|
*/ |
13
|
|
|
namespace ChatApp; |
14
|
|
|
|
15
|
|
|
require_once dirname(__DIR__).'/vendor/autoload.php'; |
16
|
|
|
use ChatApp\Time; |
17
|
|
|
use mysqli; |
18
|
|
|
use Dotenv\Dotenv; |
19
|
|
|
$dotenv = new Dotenv(dirname(__DIR__)); |
20
|
|
|
$dotenv->load(); |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Search for the user |
24
|
|
|
* |
25
|
|
|
* @category PHP |
26
|
|
|
* @package OpenChat |
27
|
|
|
* @author Ankit Jain <[email protected]> |
28
|
|
|
* @license The MIT License (MIT) |
29
|
|
|
* @link https://github.com/ankitjain28may/openchat |
30
|
|
|
*/ |
31
|
|
|
class Search |
32
|
|
|
{ |
33
|
|
|
/* |
34
|
|
|
|-------------------------------------------------------------------------- |
35
|
|
|
| Search Class |
36
|
|
|
|-------------------------------------------------------------------------- |
37
|
|
|
| |
38
|
|
|
| Search for the user. |
39
|
|
|
| |
40
|
|
|
*/ |
41
|
|
|
|
42
|
|
|
protected $connect; |
43
|
|
|
protected $array; |
44
|
|
|
protected $obTime; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Create a new class instance. |
48
|
|
|
* |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
|
View Code Duplication |
public function __construct() |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$this->connect = new mysqli( |
54
|
|
|
getenv('DB_HOST'), |
55
|
|
|
getenv('DB_USER'), |
56
|
|
|
getenv('DB_PASSWORD'), |
57
|
|
|
getenv('DB_NAME') |
58
|
|
|
); |
59
|
|
|
$this->obTime = new Time(); |
60
|
|
|
$this->array = array(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Fetch Search Item from the DB |
65
|
|
|
* |
66
|
|
|
* @param object $suggestion To store user id and suggestion value |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
public function searchItem($suggestion) |
71
|
|
|
{ |
72
|
|
|
$userId = $suggestion->userId; |
73
|
|
|
$suggestion = trim($suggestion->value); |
74
|
|
|
$flag = 0; |
75
|
|
|
if (!empty($userId) && !empty($suggestion)) { |
76
|
|
|
$query = "SELECT * FROM login where login_id != '$userId' and |
77
|
|
|
name like '$suggestion%' ORDER BY name DESC"; |
78
|
|
|
if ($result = $this->connect->query($query)) { |
79
|
|
|
if ($result->num_rows > 0) { |
80
|
|
|
while ($row = $result->fetch_assoc()) { |
81
|
|
|
$check_id = $row["login_id"]; |
82
|
|
|
$query = "SELECT * from total_message where ( |
83
|
|
|
user1 = '$check_id' and user2 = '$userId' |
84
|
|
|
) or (user2 = '$check_id' and user1 = '$userId')"; |
85
|
|
|
|
86
|
|
|
if ($result1 = $this->connect->query($query)) { |
87
|
|
|
if ($result1->num_rows > 0) { |
88
|
|
|
$fetch = $result1->fetch_assoc(); |
89
|
|
|
$fetch['time'] = $this->obTime->timeConversion( |
90
|
|
|
$fetch['time'] |
91
|
|
|
); |
92
|
|
|
|
93
|
|
|
$this->array = array_merge( |
94
|
|
|
$this->array, |
95
|
|
|
[[ |
96
|
|
|
'time' => $fetch['time'], |
97
|
|
|
'username' => $row['username'], |
98
|
|
|
'name' => $row['name'], |
99
|
|
|
'login_status' => $row['login_status'] |
100
|
|
|
]] |
101
|
|
|
); |
102
|
|
|
$flag = 1; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
if ($flag != 0) { |
109
|
|
|
$this->array = array_merge([], ["Search" => $this->array]); |
110
|
|
|
return json_encode($this->array); |
111
|
|
|
} |
112
|
|
|
return json_encode(["Search" => "Not Found"]); |
113
|
|
|
} |
114
|
|
|
return json_encode(["Search" => "Not Found"]); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.