1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file contains a class to quickly generate database queries for message |
4
|
|
|
* |
5
|
|
|
* @package BZiON\Models\QueryBuilder |
6
|
|
|
* @license https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* This class can be used to search for messages with specific characteristics |
11
|
|
|
* in the database. |
12
|
|
|
* |
13
|
|
|
* @package BZiON\Models\QueryBuilder |
14
|
|
|
*/ |
15
|
|
|
class MessageQueryBuilder extends QueryBuilder |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Only include messages |
19
|
|
|
* |
20
|
|
|
* @return self |
21
|
|
|
*/ |
22
|
1 |
|
public function messagesOnly() |
23
|
|
|
{ |
24
|
1 |
|
$this->whereConditions[] = 'event_type IS NULL'; |
25
|
|
|
|
26
|
1 |
|
return $this; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Only include conversation events |
31
|
|
|
* |
32
|
|
|
* @return self |
33
|
|
|
*/ |
34
|
|
|
public function eventsOnly() |
35
|
|
|
{ |
36
|
|
|
$this->whereConditions[] = 'event_type IS NOT NULL'; |
37
|
|
|
|
38
|
|
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Only return messages that are sent from/to a specific player |
43
|
|
|
* |
44
|
|
|
* @param Player $player The player related to the messages |
45
|
|
|
* @return self |
46
|
|
|
*/ |
47
|
1 |
View Code Duplication |
public function forPlayer($player) |
|
|
|
|
48
|
|
|
{ |
49
|
1 |
|
$this->extras .= ' |
50
|
|
|
LEFT JOIN `conversations` ON conversations.id = messages.conversation_to |
51
|
|
|
LEFT JOIN `player_conversations` ON player_conversations.conversation=conversations.id |
52
|
|
|
'; |
53
|
|
|
|
54
|
1 |
|
$this->column('player_conversations.player')->is($player); |
|
|
|
|
55
|
|
|
$this->column('conversations.status')->isOneOf(Conversation::getActiveStatuses()); |
|
|
|
|
56
|
|
|
|
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/* |
61
|
|
|
* Locate messages that contain keywords in a search string |
62
|
|
|
* |
63
|
|
|
* @param string $query The search query |
64
|
|
|
* @return self |
65
|
|
|
*/ |
66
|
1 |
|
public function search($query) |
67
|
|
|
{ |
68
|
1 |
|
$keywords = preg_split('/\s+/', trim($query)); |
69
|
|
|
|
70
|
1 |
|
$query = ""; |
71
|
|
|
|
72
|
1 |
|
$first = true; |
73
|
1 |
|
foreach ($keywords as $keyword) { |
74
|
1 |
|
if (!$first) { |
75
|
|
|
$query .= ' AND '; |
76
|
|
|
} else { |
77
|
1 |
|
$first = false; |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
$query .= "(message LIKE CONCAT('%', ?, '%'))"; |
81
|
1 |
|
$this->parameters[] = $keyword; |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
$this->whereConditions[] = $query; |
85
|
|
|
|
86
|
1 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
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.