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:
| 1 | <?php |
||
| 15 | class welcome extends module_base |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * Allowed columns: Just sum up your options (Exp: left + right = 10) |
||
| 19 | * top 1 |
||
| 20 | * left 2 |
||
| 21 | * center 4 |
||
| 22 | * right 8 |
||
| 23 | * bottom 16 |
||
| 24 | */ |
||
| 25 | public $columns = 21; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Default modulename |
||
| 29 | */ |
||
| 30 | public $name = 'PORTAL_WELCOME'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Default module-image: |
||
| 34 | * file must be in "{T_THEME_PATH}/images/portal/" |
||
| 35 | */ |
||
| 36 | public $image_src = ''; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * module-language file |
||
| 40 | * file must be in "language/{$user->lang}/mods/portal/" |
||
| 41 | */ |
||
| 42 | public $language = 'portal_welcome_module'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * custom acp template |
||
| 46 | * file must be in "adm/style/portal/" |
||
| 47 | */ |
||
| 48 | public $custom_acp_tpl = 'acp_portal_welcome'; |
||
| 49 | |||
| 50 | /** @var \phpbb\config\config */ |
||
| 51 | protected $config; |
||
| 52 | |||
| 53 | /** @var \phpbb\request\request */ |
||
| 54 | protected $request; |
||
| 55 | |||
| 56 | /** @var \phpbb\template */ |
||
| 57 | protected $template; |
||
| 58 | |||
| 59 | /** @var \phpbb\user */ |
||
| 60 | protected $user; |
||
| 61 | |||
| 62 | /** @var string PHP file extension */ |
||
| 63 | protected $php_ext; |
||
| 64 | |||
| 65 | /** @var string phpBB root path */ |
||
| 66 | protected $phpbb_root_path; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Construct a welcome object |
||
| 70 | * |
||
| 71 | * @param \phpbb\config\config $config phpBB config |
||
| 72 | * @param \phpbb\request\request $request phpBB request |
||
| 73 | * @param \phpbb\template $template phpBB template |
||
| 74 | * @param \phpbb\user $user phpBB user |
||
| 75 | * @param string $phpbb_root_path phpBB root path |
||
| 76 | * @param string $phpEx php file extension |
||
| 77 | */ |
||
| 78 | 50 | View Code Duplication | public function __construct($config, $request, $template, $user, $phpbb_root_path, $phpEx) |
| 79 | { |
||
| 80 | 50 | $this->config = $config; |
|
| 81 | 50 | $this->request = $request; |
|
| 82 | 50 | $this->template = $template; |
|
| 83 | 50 | $this->user = $user; |
|
| 84 | 50 | $this->phpbb_root_path = $phpbb_root_path; |
|
| 85 | 50 | $this->php_ext = $phpEx; |
|
| 86 | 50 | } |
|
| 87 | |||
| 88 | /** |
||
| 89 | * {@inheritdoc} |
||
| 90 | */ |
||
| 91 | 1 | public function get_template_center($module_id) |
|
| 92 | { |
||
| 93 | 1 | $portal_config = obtain_portal_config(); |
|
| 94 | |||
| 95 | // Generate text for display and assign template vars |
||
| 96 | 1 | $uid = $this->config['board3_welcome_message_uid_' . $module_id]; |
|
| 97 | 1 | $bitfield = $this->config['board3_welcome_message_bitfield_' . $module_id]; |
|
| 98 | 1 | $bbcode_options = OPTION_FLAG_BBCODE + OPTION_FLAG_SMILIES + OPTION_FLAG_LINKS; |
|
| 99 | 1 | $text = generate_text_for_display($portal_config['board3_welcome_message_' . $module_id], $uid, $bitfield, $bbcode_options); |
|
| 100 | |||
| 101 | 1 | $this->template->assign_vars(array( |
|
| 102 | 1 | 'PORTAL_WELCOME_MSG' => $text, |
|
| 103 | 1 | )); |
|
| 104 | |||
| 105 | 1 | return 'welcome_center.html'; |
|
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * {@inheritdoc} |
||
| 110 | */ |
||
| 111 | 1 | View Code Duplication | public function get_template_acp($module_id) |
| 112 | { |
||
| 113 | return array( |
||
| 114 | 1 | 'title' => 'ACP_PORTAL_WELCOME_SETTINGS', |
|
| 115 | 'vars' => array( |
||
| 116 | 1 | 'legend1' => 'ACP_PORTAL_WELCOME_SETTINGS', |
|
| 117 | 1 | 'board3_welcome_message_' . $module_id => array('lang' => 'PORTAL_WELCOME_INTRO', 'validate' => 'string', 'type' => 'custom', 'method' => 'manage_welcome', 'submit' => 'update_welcome', 'explain' => true), |
|
| 118 | 1 | ), |
|
| 119 | 1 | ); |
|
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * {@inheritdoc} |
||
| 124 | */ |
||
| 125 | 1 | public function install($module_id) |
|
| 126 | { |
||
| 127 | 1 | set_portal_config('board3_welcome_message_' . $module_id, 'Welcome to my Community!'); |
|
| 128 | 1 | $this->config->set('board3_welcome_message_' . $module_id, ''); |
|
| 129 | 1 | $this->config->set('board3_welcome_message_uid_' . $module_id, ''); |
|
| 130 | 1 | $this->config->set('board3_welcome_message_bitfield_' . $module_id, ''); |
|
| 131 | 1 | return true; |
|
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * {@inheritdoc} |
||
| 136 | */ |
||
| 137 | 1 | public function uninstall($module_id, $db) |
|
| 138 | { |
||
| 139 | $del_config = array( |
||
| 140 | 1 | 'board3_welcome_message_' . $module_id, |
|
| 141 | 1 | ); |
|
| 142 | 1 | $sql = 'DELETE FROM ' . PORTAL_CONFIG_TABLE . ' |
|
| 143 | 1 | WHERE ' . $db->sql_in_set('config_name', $del_config); |
|
| 144 | |||
| 145 | 1 | $check = $db->sql_query($sql); |
|
| 146 | |||
| 147 | $del_config = array( |
||
| 148 | 1 | 'board3_welcome_intro_' . $module_id, |
|
| 149 | 1 | 'board3_welcome_message_uid_' . $module_id, |
|
| 150 | 1 | 'board3_welcome_message_bitfield_' . $module_id, |
|
| 151 | 1 | ); |
|
| 152 | 1 | $sql = 'DELETE FROM ' . CONFIG_TABLE . ' |
|
| 153 | 1 | WHERE ' . $db->sql_in_set('config_name', $del_config); |
|
| 154 | 1 | return ((!$check) ? $check : $db->sql_query($sql)); // if something went wrong, make sure we are aware of the first query |
|
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Manage welcome message |
||
| 159 | * |
||
| 160 | * @param mixed $value Value of input |
||
| 161 | * @param string $key Key name |
||
| 162 | * @param int $module_id Module ID |
||
| 163 | * |
||
| 164 | * @return null |
||
| 165 | */ |
||
| 166 | 3 | public function manage_welcome($value, $key, $module_id) |
|
| 167 | { |
||
| 168 | 3 | $action = ($this->request->is_set_post('reset')) ? 'reset' : ''; |
|
| 169 | 3 | $action = ($this->request->is_set_post('submit')) ? 'save' : $action; |
|
| 170 | 3 | $action = ($this->request->is_set_post('preview')) ? 'preview' : $action; |
|
| 171 | |||
| 172 | 3 | $portal_config = obtain_portal_config(); |
|
| 173 | |||
| 174 | 3 | $u_action = append_sid('index.' . $this->php_ext, 'i=-board3-portal-acp-portal_module&mode=config&module_id=' . $module_id); |
|
| 175 | |||
| 176 | switch ($action) |
||
| 177 | { |
||
| 178 | // Save changes |
||
| 179 | 3 | case 'save': |
|
| 180 | 3 | View Code Duplication | if (!check_form_key('acp_portal')) |
| 181 | 3 | { |
|
| 182 | 1 | trigger_error($this->user->lang['FORM_INVALID']. adm_back_link($u_action), E_USER_WARNING); |
|
| 183 | } |
||
| 184 | |||
| 185 | 2 | $welcome_message = $this->request->variable('welcome_message', '', true); |
|
| 186 | 2 | $uid = $bitfield = $flags = ''; |
|
| 187 | 2 | generate_text_for_storage($welcome_message, $uid, $bitfield, $flags, true, true, true); |
|
| 188 | |||
| 189 | // first check for obvious errors, we don't want to waste server resources |
||
| 190 | 2 | View Code Duplication | if (empty($welcome_message)) |
| 191 | 2 | { |
|
| 192 | 1 | trigger_error($this->user->lang['ACP_PORTAL_WELCOME_MESSAGE_SHORT']. adm_back_link($u_action), E_USER_WARNING); |
|
| 193 | } |
||
| 194 | |||
| 195 | // set_portal_config will take care of escaping the welcome message |
||
| 196 | 1 | set_portal_config('board3_welcome_message_' . $module_id, $welcome_message); |
|
| 197 | 1 | $this->config->set('board3_welcome_message_uid_' . $module_id, $uid); |
|
| 198 | 1 | $this->config->set('board3_welcome_message_bitfield_' . $module_id, $bitfield); |
|
| 199 | 1 | break; |
|
| 200 | |||
| 201 | 1 | case 'preview': |
|
| 202 | 1 | $welcome_message = $text = $this->request->variable('welcome_message', '', true); |
|
| 203 | |||
| 204 | 1 | $bbcode_options = OPTION_FLAG_BBCODE + OPTION_FLAG_SMILIES + OPTION_FLAG_LINKS; |
|
| 205 | 1 | $uid = (isset($this->config['board3_welcome_message_uid_' . $module_id])) ? $this->config['board3_welcome_message_uid_' . $module_id] : ''; |
|
| 206 | 1 | $bitfield = (isset($this->config['board3_welcome_message_bitfield_' . $module_id])) ? $this->config['board3_welcome_message_bitfield_' . $module_id] : ''; |
|
| 207 | 1 | $options = OPTION_FLAG_BBCODE + OPTION_FLAG_SMILIES + OPTION_FLAG_LINKS; |
|
| 208 | 1 | generate_text_for_storage($text, $uid, $bitfield, $options, true, true, true); |
|
| 209 | |||
| 210 | 1 | $text = generate_text_for_display($text, $uid, $bitfield, $options); |
|
| 211 | |||
| 212 | 1 | $this->template->assign_vars(array( |
|
| 213 | 1 | 'PREVIEW_TEXT' => $text, |
|
| 214 | 1 | 'S_PREVIEW' => true, |
|
| 215 | 1 | )); |
|
| 216 | |||
| 217 | // Edit or add menu item |
||
| 218 | 1 | case 'reset': |
|
| 219 | 1 | default: |
|
| 220 | 1 | if (!isset($welcome_message)) |
|
| 221 | 1 | { |
|
| 222 | 1 | $welcome_message = generate_text_for_edit($portal_config['board3_welcome_message_' . $module_id], $this->config['board3_welcome_message_uid_' . $module_id], ''); |
|
| 223 | 1 | } |
|
| 224 | |||
| 225 | 1 | $this->template->assign_vars(array( |
|
| 226 | 1 | 'WELCOME_MESSAGE' => (is_array($welcome_message)) ? $welcome_message['text'] : $welcome_message, |
|
| 227 | //'U_BACK' => $u_action, |
||
| 228 | 1 | 'U_ACTION' => $u_action, |
|
| 229 | 1 | 'S_EDIT' => true, |
|
| 230 | 1 | 'S_LINKS_ALLOWED' => true, |
|
| 231 | 1 | 'S_BBCODE_IMG' => true, |
|
| 232 | 1 | 'S_BBCODE_FLASH' => true, |
|
| 233 | 1 | 'S_BBCODE_QUOTE' => true, |
|
| 234 | 1 | 'S_BBCODE_ALLOWED' => true, |
|
| 235 | 1 | 'MAX_FONT_SIZE' => (int) $this->config['max_post_font_size'], |
|
| 236 | 1 | )); |
|
| 237 | |||
| 238 | 1 | if (!function_exists('display_forums')) |
|
| 239 | 1 | { |
|
| 240 | 1 | include($this->phpbb_root_path . 'includes/functions_display.' . $this->php_ext); |
|
| 241 | 1 | } |
|
| 242 | |||
| 243 | // Build custom bbcodes array |
||
| 244 | 1 | display_custom_bbcodes(); |
|
| 245 | 1 | $this->user->add_lang('posting'); |
|
| 246 | |||
| 247 | 1 | break; |
|
| 248 | 1 | } |
|
| 249 | 1 | } |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Update welcome message |
||
| 253 | * |
||
| 254 | * @param string $key Key name |
||
| 255 | * @param int $module_id Module ID |
||
| 256 | * |
||
| 257 | * @return null |
||
| 258 | */ |
||
| 259 | 3 | public function update_welcome($key, $module_id) |
|
| 263 | } |
||
| 264 |