| Total Complexity | 50 |
| Total Lines | 373 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Rules 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 Rules, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class Rules extends AbstractController |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * Default action for the class |
||
| 30 | */ |
||
| 31 | public function action_index() |
||
| 32 | { |
||
| 33 | $this->action_manrules(); |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * List and allow adding/entering all man rules |
||
| 38 | * |
||
| 39 | * @uses sub template rules |
||
| 40 | */ |
||
| 41 | public function action_manrules(): void |
||
| 42 | { |
||
| 43 | global $txt, $context; |
||
| 44 | |||
| 45 | require_once(SUBSDIR . '/PersonalMessage.subs.php'); |
||
| 46 | |||
| 47 | // Applying all rules? |
||
| 48 | if ($this->_req->hasQuery('apply')) |
||
| 49 | { |
||
| 50 | $this->action_applyRules(); |
||
| 51 | } |
||
| 52 | |||
| 53 | // Editing a specific rule? |
||
| 54 | if ($this->_req->hasQuery('add')) |
||
| 55 | { |
||
| 56 | $this->action_addRule(); |
||
| 57 | |||
| 58 | return; |
||
| 59 | } |
||
| 60 | |||
| 61 | // Saving? |
||
| 62 | if ($this->_req->hasQuery('save')) |
||
| 63 | { |
||
| 64 | $this->action_saveRule(); |
||
| 65 | } |
||
| 66 | |||
| 67 | // Deleting? |
||
| 68 | if ($this->_req->hasPost('delselected')) |
||
| 69 | { |
||
| 70 | $this->action_deleteRules(); |
||
| 71 | } |
||
| 72 | |||
| 73 | // The link tree - gotta have this :o |
||
| 74 | $context['breadcrumbs'][] = [ |
||
| 75 | 'url' => getUrl('action', ['action' => 'pm', 'sa' => 'manrules']), |
||
| 76 | 'name' => $txt['pm_manage_rules'] |
||
| 77 | ]; |
||
| 78 | |||
| 79 | $context['page_title'] = $txt['pm_manage_rules']; |
||
| 80 | $context['sub_template'] = 'rules'; |
||
| 81 | |||
| 82 | // Load them... load them!! |
||
| 83 | loadRules(); |
||
| 84 | |||
| 85 | // Likely to need all the groups! |
||
| 86 | require_once(SUBSDIR . '/Membergroups.subs.php'); |
||
| 87 | $context['groups'] = accessibleGroups(); |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Apply all rules to the current PMs |
||
| 92 | */ |
||
| 93 | public function action_applyRules(): void |
||
| 94 | { |
||
| 95 | checkSession('get'); |
||
| 96 | |||
| 97 | applyRules(true); |
||
| 98 | redirectexit('action=pm;sa=manrules'); |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Setting up the UI for adding or editing a rule |
||
| 103 | */ |
||
| 104 | public function action_addRule(): void |
||
| 105 | { |
||
| 106 | global $context; |
||
| 107 | |||
| 108 | require_once(SUBSDIR . '/PersonalMessage.subs.php'); |
||
| 109 | loadRules(); |
||
| 110 | |||
| 111 | require_once(SUBSDIR . '/Membergroups.subs.php'); |
||
| 112 | $context['groups'] = accessibleGroups(); |
||
| 113 | |||
| 114 | $rid = $this->_req->getQuery('rid', 'intval', 0); |
||
| 115 | $context['rid'] = isset($context['rules'][$rid]) ? $rid : 0; |
||
| 116 | $context['sub_template'] = 'add_rule'; |
||
| 117 | |||
| 118 | $this->_setupJSVars(); |
||
| 119 | |||
| 120 | // Current rule information... |
||
| 121 | if ($context['rid']) |
||
| 122 | { |
||
| 123 | $context['rule'] = $context['rules'][$context['rid']]; |
||
| 124 | $this->_getCriteriaMemberNames(); |
||
| 125 | } |
||
| 126 | else |
||
| 127 | { |
||
| 128 | $context['rule'] = [ |
||
| 129 | 'id' => '', |
||
| 130 | 'name' => '', |
||
| 131 | 'criteria' => [], |
||
| 132 | 'actions' => [], |
||
| 133 | 'logic' => 'and', |
||
| 134 | ]; |
||
| 135 | } |
||
| 136 | |||
| 137 | // Add a dummy criteria to allow expansion for none js users. |
||
| 138 | $context['rule']['criteria'][] = ['t' => '', 'v' => '']; |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Sets up the JavaScript variables for the PM rule UI |
||
| 143 | */ |
||
| 144 | private function _setupJSVars(): void |
||
| 145 | { |
||
| 146 | global $txt, $context; |
||
| 147 | |||
| 148 | // Any known rule |
||
| 149 | $js_rules = []; |
||
| 150 | foreach ($context['known_rules'] as $rule) |
||
| 151 | { |
||
| 152 | $js_rules[$rule] = $txt['pm_rule_' . $rule]; |
||
| 153 | } |
||
| 154 | |||
| 155 | // Any known label |
||
| 156 | $js_labels = []; |
||
| 157 | foreach ($context['labels'] as $label) |
||
| 158 | { |
||
| 159 | if ($label['id'] !== -1) |
||
| 160 | { |
||
| 161 | $js_labels[$label['id'] + 1] = $label['name']; |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | theme()->addJavascriptVar([ |
||
| 166 | 'criteriaNum' => 0, |
||
| 167 | 'actionNum' => 0, |
||
| 168 | ]); |
||
| 169 | |||
| 170 | // Oh my, we have a lot of text strings for this |
||
| 171 | theme()->addJavascriptVar([ |
||
| 172 | 'groups' => json_encode($context['groups']), |
||
| 173 | 'labels' => json_encode($js_labels), |
||
| 174 | 'rules' => json_encode($js_rules), |
||
| 175 | 'txt_pm_readable_and' => $txt['pm_readable_and'], |
||
| 176 | 'txt_pm_readable_or' => $txt['pm_readable_or'], |
||
| 177 | 'txt_pm_readable_member' => $txt['pm_readable_member'], |
||
| 178 | 'txt_pm_readable_group' => $txt['pm_readable_group'], |
||
| 179 | 'txt_pm_readable_subject ' => $txt['pm_readable_subject'], |
||
| 180 | 'txt_pm_readable_body' => $txt['pm_readable_body'], |
||
| 181 | 'txt_pm_readable_buddy' => $txt['pm_readable_buddy'], |
||
| 182 | 'txt_pm_readable_label' => $txt['pm_readable_label'], |
||
| 183 | 'txt_pm_readable_delete' => $txt['pm_readable_delete'], |
||
| 184 | 'txt_pm_readable_start' => $txt['pm_readable_start'], |
||
| 185 | 'txt_pm_readable_end' => $txt['pm_readable_end'], |
||
| 186 | 'txt_pm_readable_then' => $txt['pm_readable_then'], |
||
| 187 | 'txt_pm_rule_not_defined' => $txt['pm_rule_not_defined'], |
||
| 188 | 'txt_pm_rule_criteria_pick' => $txt['pm_rule_criteria_pick'], |
||
| 189 | 'txt_pm_rule_sel_group' => $txt['pm_rule_sel_group'], |
||
| 190 | 'txt_pm_rule_sel_action' => $txt['pm_rule_sel_action'], |
||
| 191 | 'txt_pm_rule_label' => $txt['pm_rule_label'], |
||
| 192 | 'txt_pm_rule_delete' => $txt['pm_rule_delete'], |
||
| 193 | 'txt_pm_rule_sel_label' => $txt['pm_rule_sel_label'], |
||
| 194 | ], true); |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Gets member names for rule criteria when editing a rule |
||
| 199 | */ |
||
| 200 | private function _getCriteriaMemberNames(): void |
||
| 201 | { |
||
| 202 | global $context; |
||
| 203 | |||
| 204 | $members = []; |
||
| 205 | |||
| 206 | // Need to get member names! |
||
| 207 | foreach ($context['rule']['criteria'] as $k => $criteria) |
||
| 208 | { |
||
| 209 | if ($criteria['t'] !== 'mid' || empty($criteria['v'])) |
||
| 210 | { |
||
| 211 | continue; |
||
| 212 | } |
||
| 213 | |||
| 214 | $members[(int) $criteria['v']] = $k; |
||
| 215 | } |
||
| 216 | |||
| 217 | if (!empty($members)) |
||
| 218 | { |
||
| 219 | require_once(SUBSDIR . '/Members.subs.php'); |
||
| 220 | $result = getBasicMemberData(array_keys($members)); |
||
| 221 | foreach ($result as $row) |
||
| 222 | { |
||
| 223 | $context['rule']['criteria'][$members[$row['id_member']]]['v'] = $row['member_name']; |
||
| 224 | } |
||
| 225 | } |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Saving a PM rule |
||
| 230 | */ |
||
| 231 | public function action_saveRule(): void |
||
| 232 | { |
||
| 233 | global $context; |
||
| 234 | |||
| 235 | checkSession(); |
||
| 236 | |||
| 237 | require_once(SUBSDIR . '/PersonalMessage.subs.php'); |
||
| 238 | loadRules(); |
||
| 239 | |||
| 240 | $rid = $this->_req->getQuery('rid', 'intval', 0); |
||
| 241 | $context['rid'] = isset($context['rules'][$rid]) ? $rid : 0; |
||
| 242 | |||
| 243 | // Name is easy! |
||
| 244 | $ruleName = $this->_req->getPost('rule_name', 'trim|Util::htmlspecialchars', ''); |
||
| 245 | if (empty($ruleName)) |
||
| 246 | { |
||
| 247 | throw new Exception('pm_rule_no_name', false); |
||
| 248 | } |
||
| 249 | |||
| 250 | $criteria = $this->_getCriteria(); |
||
| 251 | $doDelete = 0; |
||
| 252 | $actions = $this->_getActions($doDelete); |
||
| 253 | |||
| 254 | if (empty($criteria) || (empty($actions) && !$doDelete)) |
||
| 255 | { |
||
| 256 | throw new Exception('pm_rule_no_criteria', false); |
||
| 257 | } |
||
| 258 | |||
| 259 | // What are we storing? |
||
| 260 | $criteria = serialize($criteria); |
||
| 261 | $actions = serialize($actions); |
||
| 262 | |||
| 263 | // Logic? |
||
| 264 | $rule_logic = $this->_req->getPost('rule_logic', 'trim|strval', ''); |
||
| 265 | $isOr = $rule_logic === 'or' ? 1 : 0; |
||
| 266 | |||
| 267 | // Create the rule? |
||
| 268 | if (empty($context['rid'])) |
||
| 269 | { |
||
| 270 | addPMRule($this->user->id, $ruleName, $criteria, $actions, $doDelete, $isOr); |
||
| 271 | } |
||
| 272 | else |
||
| 273 | { |
||
| 274 | updatePMRule($this->user->id, $context['rid'], $ruleName, $criteria, $actions, $doDelete, $isOr); |
||
| 275 | } |
||
| 276 | |||
| 277 | redirectexit('action=pm;sa=manrules'); |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Processes and validates rule criteria from the request |
||
| 282 | * |
||
| 283 | * @return array |
||
| 284 | */ |
||
| 285 | private function _getCriteria(): array |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Processes and validates rule actions from the request |
||
| 342 | * |
||
| 343 | * @param int $doDelete |
||
| 344 | * @return array |
||
| 345 | */ |
||
| 346 | private function _getActions(int &$doDelete): array |
||
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Deleting selected PM rules |
||
| 382 | */ |
||
| 383 | public function action_deleteRules(): void |
||
| 399 | } |
||
| 400 | } |
||
| 401 |