Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Drafts_PersonalMessage_Module 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Drafts_PersonalMessage_Module, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class Drafts_PersonalMessage_Module extends ElkArte\sources\modules\Abstract_Module |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * Autosave enabled |
||
| 27 | * @var bool |
||
| 28 | */ |
||
| 29 | protected static $_autosave_enabled = false; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * How often to autosave if enabled |
||
| 33 | * @var int |
||
| 34 | */ |
||
| 35 | protected static $_autosave_frequency = 30000; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Subject length |
||
| 39 | * @var int |
||
| 40 | */ |
||
| 41 | protected static $_subject_length = 24; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var Event_Manager |
||
| 45 | */ |
||
| 46 | protected static $_eventsManager = null; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var \ElkArte\ValuesContainer |
||
| 50 | */ |
||
| 51 | protected $_loaded_draft; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Add PM draft hooks and events to the system |
||
| 55 | * |
||
| 56 | * {@inheritdoc} |
||
| 57 | */ |
||
| 58 | public static function hooks(\Event_Manager $eventsManager) |
||
| 59 | { |
||
| 60 | global $modSettings, $context; |
||
| 61 | |||
| 62 | loadLanguage('Drafts'); |
||
| 63 | require_once(SUBSDIR . '/Drafts.subs.php'); |
||
| 64 | |||
| 65 | // Are PM drafts enabled? |
||
| 66 | $context['drafts_pm_save'] = !empty($modSettings['drafts_enabled']) && !empty($modSettings['drafts_pm_enabled']) && allowedTo('pm_draft'); |
||
| 67 | $context['drafts_autosave'] = !empty($context['drafts_pm_save']) && !empty($modSettings['drafts_autosave_enabled']) && allowedTo('pm_autosave_draft'); |
||
| 68 | |||
| 69 | if (!empty($modSettings['drafts_enabled']) && !empty($modSettings['drafts_post_enabled'])) |
||
| 70 | { |
||
| 71 | self::$_eventsManager = $eventsManager; |
||
| 72 | self::$_autosave_enabled = !empty($modSettings['drafts_autosave_enabled']); |
||
| 73 | |||
| 74 | add_integration_function('integrate_sa_pm_index', 'Drafts_PersonalMessage_Module::integrate_sa_pm_index', '', false); |
||
| 75 | add_integration_function('integrate_pm_areas', 'Drafts_PersonalMessage_Module::integrate_pm_areas', '', false); |
||
| 76 | |||
| 77 | if (!empty($modSettings['drafts_autosave_frequency'])) |
||
| 78 | { |
||
| 79 | self::$_autosave_frequency = (int) $modSettings['drafts_autosave_frequency'] * 1000; |
||
| 80 | } |
||
| 81 | |||
| 82 | if (!empty($modSettings['draft_subject_length'])) |
||
| 83 | { |
||
| 84 | self::$_subject_length = (int) $modSettings['draft_subject_length']; |
||
| 85 | } |
||
| 86 | |||
| 87 | // Events |
||
| 88 | return array( |
||
| 89 | array('before_set_context', array('Drafts_PersonalMessage_Module', 'before_set_context'), array()), |
||
| 90 | array('prepare_send_context', array('Drafts_PersonalMessage_Module', 'prepare_send_context'), array('editorOptions', 'recipientList')), |
||
| 91 | array('before_sending', array('Drafts_PersonalMessage_Module', 'before_sending'), array('recipientList')), |
||
| 92 | ); |
||
| 93 | } |
||
| 94 | else |
||
| 95 | { |
||
| 96 | return array(); |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Insert the show drafts button in the PM menu |
||
| 102 | * |
||
| 103 | * @param array $pm_areas |
||
| 104 | */ |
||
| 105 | public static function integrate_pm_areas(&$pm_areas) |
||
| 106 | { |
||
| 107 | global $scripturl, $txt; |
||
| 108 | |||
| 109 | $pm_areas['folders']['areas'] = elk_array_insert($pm_areas['folders']['areas'], 'sent', array( |
||
| 110 | 'drafts' => array( |
||
| 111 | 'label' => $txt['drafts_show'], |
||
| 112 | 'custom_url' => $scripturl . '?action=pm;sa=showpmdrafts', |
||
| 113 | 'permission' => 'pm_draft', |
||
| 114 | 'enabled' => true, |
||
| 115 | )), 'after'); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Add the draft controller show drafts to the available subactions |
||
| 120 | * |
||
| 121 | * @param array $subActions |
||
| 122 | */ |
||
| 123 | public static function integrate_sa_pm_index(&$subActions) |
||
| 129 | ); |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Displays a list of available drafts or selects a draft for adding to the editor |
||
| 134 | * |
||
| 135 | * @param int $pmsg |
||
| 136 | * |
||
| 137 | * @throws Pm_Error_Exception |
||
| 138 | */ |
||
| 139 | public function before_set_context($pmsg) |
||
| 140 | { |
||
| 141 | global $context, $user_info; |
||
| 142 | |||
| 143 | // If drafts are enabled, lets generate a list of drafts that they can load in to the editor |
||
| 144 | if (!empty($context['drafts_pm_save'])) |
||
| 145 | { |
||
| 146 | // Has a specific draft has been selected? Load it up if there is not already a message already in the editor |
||
| 147 | if (isset($_REQUEST['id_draft']) && empty($_POST['subject']) && empty($_POST['message'])) |
||
| 148 | { |
||
| 149 | $this->_loadDraft($user_info['id'], (int) $_REQUEST['id_draft']); |
||
| 150 | throw new Pm_Error_Exception($this->_loaded_draft->to_list, $this->_loaded_draft); |
||
|
1 ignored issue
–
show
|
|||
| 151 | } |
||
| 152 | else |
||
| 153 | { |
||
| 154 | $this->_prepareDraftsContext($user_info['id'], $pmsg); |
||
| 155 | } |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Loads in a group of PM drafts for the user. |
||
| 161 | * |
||
| 162 | * What it does: |
||
| 163 | * |
||
| 164 | * - Loads a specific draft for current use in pm editing box if selected. |
||
| 165 | * - Used in the posting screens to allow draft selection |
||
| 166 | * - Will load a draft if selected is supplied via post |
||
| 167 | * |
||
| 168 | * @param int $member_id |
||
| 169 | * @param int $id_draft The draft id |
||
| 170 | * |
||
| 171 | * @return false|null |
||
| 172 | */ |
||
| 173 | protected function _loadDraft($member_id, $id_draft) |
||
| 174 | { |
||
| 175 | // Need a member |
||
| 176 | if (empty($member_id) || empty($id_draft)) |
||
| 177 | { |
||
| 178 | return false; |
||
| 179 | } |
||
| 180 | |||
| 181 | // We haz drafts |
||
| 182 | loadLanguage('Drafts'); |
||
| 183 | require_once(SUBSDIR . '/Drafts.subs.php'); |
||
| 184 | |||
| 185 | // Load the draft and add it to a object container |
||
| 186 | $this->_loaded_draft = new \ElkArte\ValuesContainer(loadDraft($id_draft, 1, true, true)); |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Loads in a group of PM drafts for the user. |
||
| 191 | * |
||
| 192 | * What it does: |
||
| 193 | * |
||
| 194 | * - Loads a specific draft for current use in pm editing box if selected. |
||
| 195 | * - Used in the posting screens to allow draft selection |
||
| 196 | * - Will load a draft if selected is supplied via post |
||
| 197 | * |
||
| 198 | * @param int $member_id |
||
| 199 | * @param int|bool $id_pm = false if set, it will try to load drafts for this id |
||
| 200 | * |
||
| 201 | * @return bool |
||
| 202 | */ |
||
| 203 | protected function _prepareDraftsContext($member_id, $id_pm = false) |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Activates the draft plugin for use in the editor |
||
| 243 | * |
||
| 244 | * @param array $editorOptions |
||
| 245 | */ |
||
| 246 | public function prepare_send_context(&$editorOptions) |
||
| 307 | ); |
||
| 308 | } |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Saves a draft, either in reaction to autosave or pressing of save draft button |
||
| 313 | * |
||
| 314 | * @param array $recipientList |
||
| 315 | * |
||
| 316 | * @throws Controller_Redirect_Exception |
||
| 317 | * @throws Elk_Exception |
||
| 318 | */ |
||
| 319 | public function before_sending($recipientList) |
||
| 320 | { |
||
| 321 | global $context, $user_info, $modSettings; |
||
| 322 | |||
| 323 | // Ajax calling |
||
| 324 | if (!isset($context['drafts_pm_save'])) |
||
| 325 | { |
||
| 326 | $context['drafts_pm_save'] = !empty($modSettings['drafts_enabled']) && !empty($modSettings['drafts_pm_enabled']) && allowedTo('pm_draft'); |
||
| 327 | } |
||
| 328 | |||
| 329 | // Want to save this as a draft and think about it some more? |
||
| 330 | if ($context['drafts_pm_save'] && isset($_POST['save_draft'])) |
||
| 331 | { |
||
| 332 | // PM survey says ... can you stay or must you go |
||
| 333 | if (!empty($context['drafts_pm_save']) && isset($_POST['id_pm_draft'])) |
||
| 334 | { |
||
| 335 | // Prepare the data |
||
| 336 | $draft = array( |
||
| 337 | 'id_pm_draft' => empty($_POST['id_pm_draft']) ? 0 : (int) $_POST['id_pm_draft'], |
||
| 338 | 'reply_id' => empty($_POST['replied_to']) ? 0 : (int) $_POST['replied_to'], |
||
| 339 | 'body' => Util::htmlspecialchars($_POST['message'], ENT_QUOTES, 'UTF-8', true), |
||
| 340 | 'subject' => strtr(Util::htmlspecialchars($_POST['subject']), array("\r" => '', "\n" => '', "\t" => '')), |
||
| 341 | 'id_member' => $user_info['id'], |
||
| 342 | 'is_usersaved' => (int) empty($_REQUEST['autosave']), |
||
| 343 | ); |
||
| 344 | |||
| 345 | if (isset($_REQUEST['xml'])) |
||
| 346 | { |
||
| 347 | $recipientList['to'] = isset($_POST['recipient_to']) ? explode(',', $_POST['recipient_to']) : array(); |
||
| 348 | $recipientList['bcc'] = isset($_POST['recipient_bcc']) ? explode(',', $_POST['recipient_bcc']) : array(); |
||
| 349 | } |
||
| 350 | |||
| 351 | // Trigger any before_savepm_draft events |
||
| 352 | self::$_eventsManager->trigger('before_savepm_draft', array('draft' => &$draft, 'recipientList' => &$recipientList)); |
||
| 353 | |||
| 354 | // Now save the draft |
||
| 355 | savePMDraft($recipientList, $draft, isset($_REQUEST['xml'])); |
||
| 356 | throw new Controller_Redirect_Exception('', ''); |
||
| 357 | } |
||
| 358 | } |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * If it sent, then remove the draft from the system |
||
| 363 | * |
||
| 364 | * @param $failed |
||
| 365 | */ |
||
| 366 | public function message_sent($failed) |
||
| 374 | } |
||
| 375 | } |
||
| 376 | } |
||
| 377 |