1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DBStatic; |
4
|
|
|
use classSupernova; |
5
|
|
|
use Exception; |
6
|
|
|
use mysqli_result; |
7
|
|
|
|
8
|
|
|
class DBStaticNote { |
9
|
|
|
|
10
|
|
|
public static function db_note_get_id_and_owner($note_id_edit) { |
11
|
|
|
return classSupernova::$db->doSelectFetch("SELECT `id`, `owner` FROM {{notes}} WHERE `id` = {$note_id_edit} LIMIT 1 FOR UPDATE"); |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @param $user_id |
16
|
|
|
* @param bool $sticky |
17
|
|
|
* |
18
|
|
|
* @return array|bool|mysqli_result|null |
19
|
|
|
*/ |
20
|
|
|
public static function db_note_list_by_owner($user_id, $sticky = false) { |
21
|
|
|
$sticky = $sticky ? ' AND `sticky` = 1' : ''; |
22
|
|
|
$extra_sort = $sticky ? ' `galaxy` ASC, `system` ASC, `planet` ASC, `planet_type` ASC,' : ''; |
23
|
|
|
$notes_query = classSupernova::$db->doSelect("SELECT * FROM `{{notes}}` WHERE `owner` = {$user_id} {$sticky} ORDER BY `priority` DESC, {$extra_sort} `time` DESC"); |
24
|
|
|
|
25
|
|
|
return $notes_query; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param $note_priority |
30
|
|
|
* @param $note_title_unsafe |
31
|
|
|
* @param $note_text_unsafe |
32
|
|
|
* @param $note_galaxy |
33
|
|
|
* @param $note_system |
34
|
|
|
* @param $note_planet |
35
|
|
|
* @param $note_planet_type |
36
|
|
|
* @param $note_sticky |
37
|
|
|
* @param $note_id_edit |
38
|
|
|
*/ |
39
|
|
|
public static function db_note_update_by_id($note_priority, $note_title_unsafe, $note_text_unsafe, $note_galaxy, $note_system, $note_planet, $note_planet_type, $note_sticky, $note_id_edit) { |
40
|
|
|
classSupernova::$db->doUpdateRowSet( |
41
|
|
|
TABLE_NOTES, |
42
|
|
|
array( |
43
|
|
|
'time' => SN_TIME_NOW, |
44
|
|
|
'priority' => $note_priority, |
45
|
|
|
'title' => $note_title_unsafe, |
46
|
|
|
'text' => $note_text_unsafe, |
47
|
|
|
'galaxy' => $note_galaxy, |
48
|
|
|
'system' => $note_system, |
49
|
|
|
'planet' => $note_planet, |
50
|
|
|
'planet_type' => $note_planet_type, |
51
|
|
|
'sticky' => $note_sticky, |
52
|
|
|
), |
53
|
|
|
array( |
54
|
|
|
'id' => $note_id_edit, |
55
|
|
|
) |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param $userId |
61
|
|
|
* @param $note_priority |
62
|
|
|
* @param $note_title_unsafe |
63
|
|
|
* @param $note_text_unsafe |
64
|
|
|
* @param $note_galaxy |
65
|
|
|
* @param $note_system |
66
|
|
|
* @param $note_planet |
67
|
|
|
* @param $note_planet_type |
68
|
|
|
* @param $note_sticky |
69
|
|
|
*/ |
70
|
|
View Code Duplication |
public static function db_note_insert( |
|
|
|
|
71
|
|
|
$userId, $note_priority, $note_title_unsafe, $note_text_unsafe, $note_galaxy, $note_system, $note_planet, $note_planet_type, $note_sticky) { |
72
|
|
|
classSupernova::$db->doInsertSet(TABLE_NOTES, array( |
73
|
|
|
'owner' => $userId, |
74
|
|
|
'time' => SN_TIME_NOW, |
75
|
|
|
'priority' => $note_priority, |
76
|
|
|
'title' => $note_title_unsafe, |
77
|
|
|
'text' => $note_text_unsafe, |
78
|
|
|
'galaxy' => $note_galaxy, |
79
|
|
|
'system' => $note_system, |
80
|
|
|
'planet' => $note_planet, |
81
|
|
|
'planet_type' => $note_planet_type, |
82
|
|
|
'sticky' => $note_sticky, |
83
|
|
|
)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param $user |
89
|
|
|
* |
90
|
|
|
* @return array|bool|mysqli_result|null |
91
|
|
|
*/ |
92
|
|
|
public static function db_note_list_select_by_owner_and_planet($user) { |
93
|
|
|
$query = classSupernova::$db->doSelect("SELECT * FROM {{notes}} WHERE `owner` = {$user['id']} AND `galaxy` <> 0 AND `system` <> 0 AND `planet` <> 0 ORDER BY `priority` DESC, `galaxy`, `system`, `planet`, `planet_type`;"); |
94
|
|
|
|
95
|
|
|
return $query; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param array $user |
100
|
|
|
* @param int $note_id_edit |
101
|
|
|
* |
102
|
|
|
* @throws Exception |
103
|
|
|
*/ |
104
|
|
|
public static function processDelete($user, $note_id_edit) { |
105
|
|
|
$not = ''; |
106
|
|
|
$whereDanger = array(); |
107
|
|
|
switch (sys_get_param_str('note_delete_range')) { |
108
|
|
|
case 'all': |
109
|
|
|
break; |
110
|
|
|
|
111
|
|
|
/** @noinspection PhpMissingBreakStatementInspection */ |
112
|
|
|
case 'marked_not': |
113
|
|
|
$not = 'NOT'; |
114
|
|
|
case 'marked': |
115
|
|
|
if (!is_array($notes_marked = sys_get_param('note'))) { |
116
|
|
|
throw new Exception('note_err_none_selected', ERR_WARNING); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$notes_marked_filtered = array(); |
120
|
|
|
foreach ($notes_marked as $note_id => $note_select) { |
121
|
|
|
if ($note_select == 'on' && $note_id = idval($note_id)) { |
122
|
|
|
$notes_marked_filtered[] = $note_id; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if (empty($notes_marked_filtered)) { |
127
|
|
|
throw new Exception('note_err_none_selected', ERR_WARNING); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$notes_marked_filtered = implode(',', $notes_marked_filtered); |
131
|
|
|
$whereDanger[] = "`id` {$not} IN ({$notes_marked_filtered})"; |
132
|
|
|
break; |
133
|
|
|
|
134
|
|
|
default: |
135
|
|
|
throw new Exception('note_warn_no_range', ERR_WARNING); |
136
|
|
|
break; |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
sn_db_transaction_start(); |
140
|
|
|
|
141
|
|
|
classSupernova::$gc->db->doDeleteDanger( |
|
|
|
|
142
|
|
|
TABLE_NOTES, |
143
|
|
|
array( |
144
|
|
|
'owner' => $user['id'], |
145
|
|
|
), |
146
|
|
|
$whereDanger |
147
|
|
|
); |
148
|
|
|
sn_db_transaction_commit(); |
149
|
|
|
|
150
|
|
|
throw new Exception($note_id_edit ? 'note_err_none_changed' : 'note_err_none_added', ERR_NONE); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
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.