Conditions | 62 |
Paths | > 20000 |
Total Lines | 258 |
Code Lines | 137 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
53 | function UnapprovedPosts() |
||
54 | { |
||
55 | global $txt, $scripturl, $context, $user_info, $smcFunc, $options, $modSettings; |
||
56 | |||
57 | $context['current_view'] = isset($_GET['sa']) && $_GET['sa'] == 'topics' ? 'topics' : 'replies'; |
||
58 | $context['page_title'] = $txt['mc_unapproved_posts']; |
||
59 | |||
60 | // Work out what boards we can work in! |
||
61 | $approve_boards = boardsAllowedTo('approve_posts'); |
||
62 | |||
63 | // If we filtered by board remove ones outside of this board. |
||
64 | // @todo Put a message saying we're filtered? |
||
65 | if (isset($_REQUEST['brd'])) |
||
66 | { |
||
67 | $filter_board = array((int) $_REQUEST['brd']); |
||
68 | $approve_boards = $approve_boards == array(0) ? $filter_board : array_intersect($approve_boards, $filter_board); |
||
69 | } |
||
70 | |||
71 | if ($approve_boards == array(0)) |
||
72 | $approve_query = ''; |
||
73 | elseif (!empty($approve_boards)) |
||
74 | $approve_query = ' AND m.id_board IN (' . implode(',', $approve_boards) . ')'; |
||
75 | // Nada, zip, etc... |
||
76 | else |
||
77 | $approve_query = ' AND 1=0'; |
||
78 | |||
79 | // We also need to know where we can delete topics and/or replies to. |
||
80 | $boards_can = boardsAllowedTo(array('remove_any', 'remove_own', 'delete_own', 'delete_any', 'delete_own_replies'), true, false); |
||
81 | if ($context['current_view'] == 'topics') |
||
82 | { |
||
83 | $delete_own_boards = $boards_can['remove_own']; |
||
84 | $delete_any_boards = $boards_can['remove_any']; |
||
85 | $delete_own_replies = array(); |
||
86 | } |
||
87 | else |
||
88 | { |
||
89 | $delete_own_boards = $boards_can['delete_own']; |
||
90 | $delete_any_boards = $boards_can['delete_any']; |
||
91 | $delete_own_replies = $boards_can['delete_own_replies']; |
||
92 | } |
||
93 | |||
94 | $toAction = array(); |
||
95 | // Check if we have something to do? |
||
96 | if (isset($_GET['approve'])) |
||
97 | $toAction[] = (int) $_GET['approve']; |
||
98 | // Just a deletion? |
||
99 | elseif (isset($_GET['delete'])) |
||
100 | $toAction[] = (int) $_GET['delete']; |
||
101 | // Lots of approvals? |
||
102 | elseif (isset($_POST['item'])) |
||
103 | foreach ($_POST['item'] as $item) |
||
104 | $toAction[] = (int) $item; |
||
105 | |||
106 | // What are we actually doing. |
||
107 | if (isset($_GET['approve']) || (isset($_POST['do']) && $_POST['do'] == 'approve')) |
||
108 | $curAction = 'approve'; |
||
109 | elseif (isset($_GET['delete']) || (isset($_POST['do']) && $_POST['do'] == 'delete')) |
||
110 | $curAction = 'delete'; |
||
111 | |||
112 | // Right, so we have something to do? |
||
113 | if (!empty($toAction) && isset($curAction)) |
||
114 | { |
||
115 | checkSession('request'); |
||
116 | |||
117 | // Handy shortcut. |
||
118 | $any_array = $curAction == 'approve' ? $approve_boards : $delete_any_boards; |
||
119 | |||
120 | // Now for each message work out whether it's actually a topic, and what board it's on. |
||
121 | $request = $smcFunc['db_query']('', ' |
||
122 | SELECT m.id_msg, m.id_member, m.id_board, m.subject, t.id_topic, t.id_first_msg, t.id_member_started |
||
123 | FROM {db_prefix}messages AS m |
||
124 | INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic) |
||
125 | WHERE m.id_msg IN ({array_int:message_list}) |
||
126 | AND m.approved = {int:not_approved} |
||
127 | AND {query_see_message_board}', |
||
128 | array( |
||
129 | 'message_list' => $toAction, |
||
130 | 'not_approved' => 0, |
||
131 | ) |
||
132 | ); |
||
133 | $toAction = array(); |
||
134 | $details = array(); |
||
135 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
136 | { |
||
137 | // If it's not within what our view is ignore it... |
||
138 | if (($row['id_msg'] == $row['id_first_msg'] && $context['current_view'] != 'topics') || ($row['id_msg'] != $row['id_first_msg'] && $context['current_view'] != 'replies')) |
||
139 | continue; |
||
140 | |||
141 | $can_add = false; |
||
142 | // If we're approving this is simple. |
||
143 | if ($curAction == 'approve' && ($any_array == array(0) || in_array($row['id_board'], $any_array))) |
||
144 | { |
||
145 | $can_add = true; |
||
146 | } |
||
147 | // Delete requires more permission checks... |
||
148 | elseif ($curAction == 'delete') |
||
149 | { |
||
150 | // Own post is easy! |
||
151 | if ($row['id_member'] == $user_info['id'] && ($delete_own_boards == array(0) || in_array($row['id_board'], $delete_own_boards))) |
||
152 | $can_add = true; |
||
153 | // Is it a reply to their own topic? |
||
154 | elseif ($row['id_member'] == $row['id_member_started'] && $row['id_msg'] != $row['id_first_msg'] && ($delete_own_replies == array(0) || in_array($row['id_board'], $delete_own_replies))) |
||
155 | $can_add = true; |
||
156 | // Someone elses? |
||
157 | elseif ($row['id_member'] != $user_info['id'] && ($delete_any_boards == array(0) || in_array($row['id_board'], $delete_any_boards))) |
||
158 | $can_add = true; |
||
159 | } |
||
160 | |||
161 | if ($can_add) |
||
162 | $anItem = $context['current_view'] == 'topics' ? $row['id_topic'] : $row['id_msg']; |
||
163 | $toAction[] = $anItem; |
||
|
|||
164 | |||
165 | // All clear. What have we got now, what, what? |
||
166 | $details[$anItem] = array(); |
||
167 | $details[$anItem]["subject"] = $row['subject']; |
||
168 | $details[$anItem]["topic"] = $row['id_topic']; |
||
169 | $details[$anItem]["member"] = ($context['current_view'] == 'topics') ? $row['id_member_started'] : $row['id_member']; |
||
170 | $details[$anItem]["board"] = $row['id_board']; |
||
171 | } |
||
172 | $smcFunc['db_free_result']($request); |
||
173 | |||
174 | // If we have anything left we can actually do the approving (etc). |
||
175 | if (!empty($toAction)) |
||
176 | { |
||
177 | if ($curAction == 'approve') |
||
178 | { |
||
179 | approveMessages($toAction, $details, $context['current_view']); |
||
180 | } |
||
181 | else |
||
182 | { |
||
183 | removeMessages($toAction, $details, $context['current_view']); |
||
184 | } |
||
185 | } |
||
186 | } |
||
187 | |||
188 | // How many unapproved posts are there? |
||
189 | $request = $smcFunc['db_query']('', ' |
||
190 | SELECT COUNT(*) |
||
191 | FROM {db_prefix}messages AS m |
||
192 | INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic AND t.id_first_msg != m.id_msg) |
||
193 | INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board) |
||
194 | WHERE m.approved = {int:not_approved} |
||
195 | AND {query_see_board} |
||
196 | ' . $approve_query, |
||
197 | array( |
||
198 | 'not_approved' => 0, |
||
199 | ) |
||
200 | ); |
||
201 | list ($context['total_unapproved_posts']) = $smcFunc['db_fetch_row']($request); |
||
202 | $smcFunc['db_free_result']($request); |
||
203 | |||
204 | // What about topics? Normally we'd use the table alias t for topics but lets use m so we don't have to redo our approve query. |
||
205 | $request = $smcFunc['db_query']('', ' |
||
206 | SELECT COUNT(*) |
||
207 | FROM {db_prefix}topics AS m |
||
208 | WHERE m.approved = {int:not_approved} |
||
209 | AND {query_see_message_board} |
||
210 | ' . $approve_query, |
||
211 | array( |
||
212 | 'not_approved' => 0, |
||
213 | ) |
||
214 | ); |
||
215 | list ($context['total_unapproved_topics']) = $smcFunc['db_fetch_row']($request); |
||
216 | $smcFunc['db_free_result']($request); |
||
217 | |||
218 | // Limit to how many? (obey the user setting) |
||
219 | $limit = !empty($options['messages_per_page']) ? $options['messages_per_page'] : $modSettings['defaultMaxMessages']; |
||
220 | |||
221 | $context['page_index'] = constructPageIndex($scripturl . '?action=moderate;area=postmod;sa=' . $context['current_view'] . (isset($_REQUEST['brd']) ? ';brd=' . (int) $_REQUEST['brd'] : ''), $_GET['start'], $context['current_view'] == 'topics' ? $context['total_unapproved_topics'] : $context['total_unapproved_posts'], $limit); |
||
222 | $context['start'] = $_GET['start']; |
||
223 | |||
224 | // We have enough to make some pretty tabs! |
||
225 | $context[$context['moderation_menu_name']]['tab_data'] = array( |
||
226 | 'title' => $txt['mc_unapproved_posts'], |
||
227 | 'help' => 'postmod', |
||
228 | 'description' => $txt['mc_unapproved_posts_desc'], |
||
229 | ); |
||
230 | |||
231 | // Update the tabs with the correct number of posts. |
||
232 | $context['menu_data_' . $context['moderation_menu_id']]['sections']['posts']['areas']['postmod']['subsections']['posts']['label'] .= ' (' . $context['total_unapproved_posts'] . ')'; |
||
233 | $context['menu_data_' . $context['moderation_menu_id']]['sections']['posts']['areas']['postmod']['subsections']['topics']['label'] .= ' (' . $context['total_unapproved_topics'] . ')'; |
||
234 | |||
235 | // If we are filtering some boards out then make sure to send that along with the links. |
||
236 | if (isset($_REQUEST['brd'])) |
||
237 | { |
||
238 | $context['menu_data_' . $context['moderation_menu_id']]['sections']['posts']['areas']['postmod']['subsections']['posts']['add_params'] = ';brd=' . (int) $_REQUEST['brd']; |
||
239 | $context['menu_data_' . $context['moderation_menu_id']]['sections']['posts']['areas']['postmod']['subsections']['topics']['add_params'] = ';brd=' . (int) $_REQUEST['brd']; |
||
240 | } |
||
241 | |||
242 | // Get all unapproved posts. |
||
243 | $request = $smcFunc['db_query']('', ' |
||
244 | SELECT m.id_msg, m.id_topic, m.id_board, m.subject, m.body, m.id_member, |
||
245 | COALESCE(mem.real_name, m.poster_name) AS poster_name, m.poster_time, m.smileys_enabled, |
||
246 | t.id_member_started, t.id_first_msg, b.name AS board_name, c.id_cat, c.name AS cat_name |
||
247 | FROM {db_prefix}messages AS m |
||
248 | INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic) |
||
249 | INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board) |
||
250 | LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member) |
||
251 | LEFT JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat) |
||
252 | WHERE m.approved = {int:not_approved} |
||
253 | AND t.id_first_msg ' . ($context['current_view'] == 'topics' ? '=' : '!=') . ' m.id_msg |
||
254 | AND {query_see_board} |
||
255 | ' . $approve_query . ' |
||
256 | LIMIT {int:start}, {int:limit}', |
||
257 | array( |
||
258 | 'not_approved' => 0, |
||
259 | 'start' => $context['start'], |
||
260 | 'limit' => $limit, |
||
261 | ) |
||
262 | ); |
||
263 | $context['unapproved_items'] = array(); |
||
264 | for ($i = 1; $row = $smcFunc['db_fetch_assoc']($request); $i++) |
||
265 | { |
||
266 | // Can delete is complicated, let's solve it first... is it their own post? |
||
267 | if ($row['id_member'] == $user_info['id'] && ($delete_own_boards == array(0) || in_array($row['id_board'], $delete_own_boards))) |
||
268 | $can_delete = true; |
||
269 | // Is it a reply to their own topic? |
||
270 | elseif ($row['id_member'] == $row['id_member_started'] && $row['id_msg'] != $row['id_first_msg'] && ($delete_own_replies == array(0) || in_array($row['id_board'], $delete_own_replies))) |
||
271 | $can_delete = true; |
||
272 | // Someone elses? |
||
273 | elseif ($row['id_member'] != $user_info['id'] && ($delete_any_boards == array(0) || in_array($row['id_board'], $delete_any_boards))) |
||
274 | $can_delete = true; |
||
275 | else |
||
276 | $can_delete = false; |
||
277 | |||
278 | $context['unapproved_items'][] = array( |
||
279 | 'id' => $row['id_msg'], |
||
280 | 'counter' => $context['start'] + $i, |
||
281 | 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'], |
||
282 | 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'] . '">' . $row['subject'] . '</a>', |
||
283 | 'subject' => $row['subject'], |
||
284 | 'body' => parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']), |
||
285 | 'time' => timeformat($row['poster_time']), |
||
286 | 'poster' => array( |
||
287 | 'id' => $row['id_member'], |
||
288 | 'name' => $row['poster_name'], |
||
289 | 'link' => $row['id_member'] ? '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>' : $row['poster_name'], |
||
290 | 'href' => $scripturl . '?action=profile;u=' . $row['id_member'], |
||
291 | ), |
||
292 | 'topic' => array( |
||
293 | 'id' => $row['id_topic'], |
||
294 | ), |
||
295 | 'board' => array( |
||
296 | 'id' => $row['id_board'], |
||
297 | 'name' => $row['board_name'], |
||
298 | 'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['board_name'] . '</a>', |
||
299 | ), |
||
300 | 'category' => array( |
||
301 | 'id' => $row['id_cat'], |
||
302 | 'name' => $row['cat_name'], |
||
303 | 'link' => '<a href="' . $scripturl . '#c' . $row['id_cat'] . '">' . $row['cat_name'] . '</a>', |
||
304 | ), |
||
305 | 'can_delete' => $can_delete, |
||
306 | ); |
||
307 | } |
||
308 | $smcFunc['db_free_result']($request); |
||
309 | |||
310 | $context['sub_template'] = 'unapproved_posts'; |
||
311 | } |
||
810 | ?> |