Total Complexity | 41 |
Total Lines | 409 |
Duplicated Lines | 0 % |
Changes | 19 | ||
Bugs | 0 | Features | 0 |
Complex classes like admin_controller often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use admin_controller, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class admin_controller |
||
23 | { |
||
24 | /* @var \sylver35\smiliescat\core\category */ |
||
25 | protected $category; |
||
26 | |||
27 | /** @var \phpbb\config\config */ |
||
28 | protected $config; |
||
29 | |||
30 | /** @var \phpbb\db\driver\driver_interface */ |
||
31 | protected $db; |
||
32 | |||
33 | /** @var \phpbb\pagination */ |
||
34 | protected $pagination; |
||
35 | |||
36 | /** @var \phpbb\request\request */ |
||
37 | protected $request; |
||
38 | |||
39 | /** @var \phpbb\template\template */ |
||
40 | protected $template; |
||
41 | |||
42 | /** @var \phpbb\user */ |
||
43 | protected $user; |
||
44 | |||
45 | /** @var \phpbb\language\language */ |
||
46 | protected $language; |
||
47 | |||
48 | /** @var \phpbb\log\log */ |
||
49 | protected $log; |
||
50 | |||
51 | /** @var string phpBB root path */ |
||
52 | protected $root_path; |
||
53 | |||
54 | /** @var string Custom form action */ |
||
55 | protected $u_action; |
||
56 | |||
57 | /** |
||
58 | * The database tables |
||
59 | * |
||
60 | * @var string */ |
||
61 | protected $smilies_category_table; |
||
62 | |||
63 | /** |
||
64 | * Constructor |
||
65 | */ |
||
66 | public function __construct(category $category, config $config, db $db, pagination $pagination, request $request, template $template, user $user, language $language, log $log, $root_path, $smilies_category_table) |
||
79 | } |
||
80 | |||
81 | public function acp_smilies_category() |
||
82 | { |
||
83 | $this->language->add_lang('acp/posting'); |
||
84 | $action = (string) $this->request->variable('action', ''); |
||
85 | $start = (int) $this->request->variable('start', 0); |
||
86 | $select = (int) $this->request->variable('select', -1); |
||
87 | $id = (int) $this->request->variable('id', -1); |
||
88 | $form_key = 'sylver35/smiliescat'; |
||
89 | add_form_key($form_key); |
||
90 | |||
91 | if ($action) |
||
92 | { |
||
93 | switch ($action) |
||
94 | { |
||
95 | case 'edit': |
||
96 | $this->category->adm_edit_smiley($id, $this->u_action, $start); |
||
97 | break; |
||
98 | |||
99 | case 'modify': |
||
100 | if (!check_form_key($form_key)) |
||
101 | { |
||
102 | trigger_error($this->language->lang('FORM_INVALID') . adm_back_link($this->u_action), E_USER_WARNING); |
||
103 | } |
||
104 | |||
105 | $this->modify_smiley($id, (int) $this->request->variable('cat_id', 0), (int) $this->request->variable('ex_cat', 0), $start); |
||
106 | break; |
||
107 | } |
||
108 | |||
109 | $this->template->assign_vars(array( |
||
110 | 'IN_ACTION' => true, |
||
111 | )); |
||
112 | } |
||
113 | else |
||
114 | { |
||
115 | $this->extract_list_smilies($select, $start); |
||
116 | |||
117 | $this->template->assign_vars(array( |
||
118 | 'LIST_CATEGORY' => $this->category->select_categories($select, true), |
||
119 | 'U_SELECT_CAT' => $this->u_action . '&select=' . $select, |
||
120 | 'U_BACK' => ($select) ? $this->u_action : '', |
||
121 | )); |
||
122 | } |
||
123 | |||
124 | $this->template->assign_vars(array( |
||
125 | 'CATEGORIE_SMILIES' => true, |
||
126 | )); |
||
127 | } |
||
128 | |||
129 | public function acp_categories_config() |
||
130 | { |
||
131 | $this->language->add_lang('acp/language'); |
||
132 | $mode = (string) $this->request->variable('mode', ''); |
||
133 | $action = (string) $this->request->variable('action', ''); |
||
134 | $id = (int) $this->request->variable('id', 0); |
||
135 | $form_key = 'sylver35/smiliescat'; |
||
136 | add_form_key($form_key); |
||
137 | |||
138 | if ($action) |
||
139 | { |
||
140 | if (in_array($action, array('config_cat', 'add_cat', 'edit_cat')) && !check_form_key($form_key)) |
||
141 | { |
||
142 | trigger_error($this->language->lang('FORM_INVALID') . adm_back_link($this->u_action), E_USER_WARNING); |
||
143 | } |
||
144 | |||
145 | switch ($action) |
||
146 | { |
||
147 | case 'config_cat': |
||
148 | $this->config->set('smilies_per_page_cat', (int) $this->request->variable('smilies_per_page_cat', 15)); |
||
149 | |||
150 | $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_CONFIG', time()); |
||
151 | trigger_error($this->language->lang('CONFIG_UPDATED') . adm_back_link($this->u_action)); |
||
152 | break; |
||
153 | |||
154 | case 'add': |
||
155 | $this->category->adm_add_cat($this->u_action); |
||
156 | break; |
||
157 | |||
158 | case 'add_cat': |
||
159 | $this->add_category(); |
||
160 | break; |
||
161 | |||
162 | case 'edit': |
||
163 | $this->category->adm_edit_cat($id, $this->u_action); |
||
164 | break; |
||
165 | |||
166 | case 'edit_cat': |
||
167 | $this->edit_category($id); |
||
168 | break; |
||
169 | |||
170 | case 'delete': |
||
171 | if (confirm_box(true)) |
||
172 | { |
||
173 | $this->delete_cat($id); |
||
174 | } |
||
175 | else |
||
176 | { |
||
177 | confirm_box(false, $this->language->lang('CONFIRM_OPERATION'), build_hidden_fields(array( |
||
178 | 'mode' => $mode, |
||
179 | 'id' => $id, |
||
180 | 'action' => $action, |
||
181 | ))); |
||
182 | } |
||
183 | break; |
||
184 | |||
185 | } |
||
186 | |||
187 | $this->template->assign_vars(array( |
||
188 | 'IN_ACTION' => true, |
||
189 | )); |
||
190 | } |
||
191 | else |
||
192 | { |
||
193 | $this->category->adm_list_cat($this->u_action); |
||
194 | } |
||
195 | |||
196 | $this->template->assign_vars(array( |
||
197 | 'CATEGORIE_CONFIG' => true, |
||
198 | 'SMILIES_PER_PAGE_CAT' => $this->config['smilies_per_page_cat'], |
||
199 | 'U_ACTION_CONFIG' => $this->u_action . '&action=config_cat', |
||
200 | 'U_ADD' => $this->u_action . '&action=add', |
||
201 | )); |
||
202 | } |
||
203 | |||
204 | private function modify_smiley($id, $cat_id, $ex_cat, $start) |
||
223 | } |
||
224 | |||
225 | private function extract_list_smilies($select, $start) |
||
226 | { |
||
227 | $i = 0; |
||
228 | $cat = -1; |
||
229 | $lang = $this->user->lang_name; |
||
230 | $smilies_count = (int) $this->category->smilies_count($select); |
||
231 | |||
232 | if ($select === 0) |
||
233 | { |
||
234 | $sql = $this->db->sql_build_query('SELECT', array( |
||
235 | 'SELECT' => '*', |
||
236 | 'FROM' => array(SMILIES_TABLE => ''), |
||
237 | 'WHERE' => 'category = 0', |
||
238 | 'ORDER_BY' => 'smiley_order ASC', |
||
239 | )); |
||
240 | } |
||
241 | else |
||
242 | { |
||
243 | $sql = $this->db->sql_build_query('SELECT', array( |
||
244 | 'SELECT' => 's.*, c.*', |
||
245 | 'FROM' => array(SMILIES_TABLE => 's'), |
||
246 | 'LEFT_JOIN' => array( |
||
247 | array( |
||
248 | 'FROM' => array($this->smilies_category_table => 'c'), |
||
249 | 'ON' => "cat_id = category AND cat_lang = '$lang'", |
||
250 | ), |
||
251 | ), |
||
252 | 'WHERE' => ($select == -1) ? "code <> ''" : "cat_id = $select AND code <> ''", |
||
253 | 'ORDER_BY' => 'cat_order ASC, smiley_order ASC', |
||
254 | )); |
||
255 | } |
||
256 | $result = $this->db->sql_query_limit($sql, (int) $this->config['smilies_per_page_cat'], $start); |
||
257 | while ($row = $this->db->sql_fetchrow($result)) |
||
258 | { |
||
259 | $row['category'] = isset($row['category']) ? $row['category'] : 0; |
||
260 | $row['cat_name'] = ($row['category']) ? $row['cat_name'] : $this->language->lang('SC_CATEGORY_DEFAUT'); |
||
261 | |||
262 | $this->template->assign_block_vars('items', array( |
||
263 | 'SPACER_CAT' => ($cat !== (int) $row['category']) ? $this->language->lang('SC_CATEGORY_IN', $row['cat_name']) : '', |
||
264 | 'IMG_SRC' => $row['smiley_url'], |
||
265 | 'WIDTH' => $row['smiley_width'], |
||
266 | 'HEIGHT' => $row['smiley_height'], |
||
267 | 'CODE' => $row['code'], |
||
268 | 'EMOTION' => $row['emotion'], |
||
269 | 'CATEGORY' => $row['cat_name'], |
||
270 | 'U_EDIT' => $this->u_action . '&action=edit&id=' . $row['smiley_id'] . '&start=' . $start, |
||
271 | )); |
||
272 | $i++; |
||
273 | |||
274 | // Keep this value in memory |
||
275 | $cat = (int) $row['category']; |
||
276 | } |
||
277 | $this->db->sql_freeresult($result); |
||
278 | |||
279 | $this->template->assign_vars(array( |
||
280 | 'NB_SMILIES' => $smilies_count, |
||
281 | 'U_SMILIES' => $this->root_path . $this->config['smilies_path'] . '/', |
||
282 | )); |
||
283 | |||
284 | $this->pagination->generate_template_pagination($this->u_action . '&select=' . $select, 'pagination', 'start', $smilies_count, (int) $this->config['smilies_per_page_cat'], $start); |
||
285 | } |
||
286 | |||
287 | private function delete_cat($id) |
||
288 | { |
||
289 | $id = (int) $id; |
||
290 | $sql = 'SELECT cat_title, cat_order |
||
291 | FROM ' . $this->smilies_category_table . ' |
||
292 | WHERE cat_id = ' . $id; |
||
293 | $result = $this->db->sql_query($sql); |
||
294 | $row = $this->db->sql_fetchrow($result); |
||
295 | $title = $row['cat_title']; |
||
296 | $order = $row['cat_order']; |
||
297 | $this->db->sql_freeresult($result); |
||
298 | |||
299 | $sql_delete = 'DELETE FROM ' . $this->smilies_category_table . ' WHERE cat_id = ' . $id; |
||
300 | $this->db->sql_query($sql_delete); |
||
301 | |||
302 | // Decrement orders if needed |
||
303 | $sql_decrement = 'SELECT cat_id, cat_order |
||
304 | FROM ' . $this->smilies_category_table . ' |
||
305 | WHERE cat_order > ' . (int) $order; |
||
306 | $result = $this->db->sql_query($sql_decrement); |
||
307 | while ($row = $this->db->sql_fetchrow($result)) |
||
308 | { |
||
309 | $new_order = (int) $row['cat_order'] - 1; |
||
310 | $sql_order = 'UPDATE ' . $this->smilies_category_table . ' |
||
311 | SET cat_order = ' . $new_order . ' |
||
312 | WHERE cat_id = ' . $row['cat_id'] . ' AND cat_order = ' . $row['cat_order']; |
||
313 | $this->db->sql_query($sql_order); |
||
314 | } |
||
315 | $this->db->sql_freeresult($result); |
||
316 | |||
317 | // Reset appropriate smilies category id |
||
318 | $sql_update = 'UPDATE ' . SMILIES_TABLE . ' SET category = 0 WHERE category = ' . $id; |
||
319 | $this->db->sql_query($sql_update); |
||
320 | |||
321 | $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_SC_DELETE_CAT', time(), array($title)); |
||
322 | trigger_error($this->language->lang('SC_DELETE_SUCCESS') . adm_back_link($this->u_action)); |
||
323 | } |
||
324 | |||
325 | private function add_category() |
||
371 | } |
||
372 | |||
373 | private function edit_category($id) |
||
374 | { |
||
419 | } |
||
420 | |||
421 | /** |
||
422 | * Set page url |
||
423 | * |
||
424 | * @param string $u_action Custom form action |
||
425 | * @return null |
||
426 | * @access public |
||
427 | */ |
||
428 | public function set_page_url($u_action) |
||
431 | } |
||
432 | } |
||
433 |