| Conditions | 17 |
| Paths | 1668 |
| Total Lines | 120 |
| Code Lines | 59 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 1 | Features | 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 |
||
| 57 | public function action_sportal_shoutbox() |
||
| 58 | { |
||
| 59 | global $context, $scripturl, $user_info; |
||
| 60 | |||
| 61 | // ID of the shoutbox we are working on and timestamp |
||
| 62 | $shoutbox_id = !empty($_REQUEST['shoutbox_id']) ? (int) $_REQUEST['shoutbox_id'] : 0; |
||
| 63 | $request_time = !empty($_REQUEST['time']) ? (int) $_REQUEST['time'] : 0; |
||
| 64 | |||
| 65 | // We need to know which shoutbox this is for/from |
||
| 66 | $context['SPortal']['shoutbox'] = sportal_get_shoutbox($shoutbox_id, true, true); |
||
| 67 | |||
| 68 | // Shouting but no one is there to here you |
||
| 69 | if (empty($context['SPortal']['shoutbox'])) |
||
| 70 | { |
||
| 71 | if (isset($_REQUEST['xml'])) |
||
| 72 | { |
||
| 73 | obExit(false, false); |
||
| 74 | } |
||
| 75 | else |
||
| 76 | { |
||
| 77 | throw new Elk_Exception('error_sp_shoutbox_not_exist', false); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | // Any warning title for the shoutbox, like Not For Support ;P |
||
| 82 | $parser = ParserWrapper::instance(); |
||
| 83 | $context['SPortal']['shoutbox']['warning'] = $parser->parseMessage($context['SPortal']['shoutbox']['warning'], true); |
||
| 84 | |||
| 85 | $can_moderate = allowedTo('sp_admin') || allowedTo('sp_manage_shoutbox'); |
||
| 86 | if (!$can_moderate && !empty($context['SPortal']['shoutbox']['moderator_groups'])) |
||
| 87 | { |
||
| 88 | $can_moderate = count(array_intersect($user_info['groups'], $context['SPortal']['shoutbox']['moderator_groups'])) > 0; |
||
| 89 | } |
||
| 90 | |||
| 91 | // Adding a shout |
||
| 92 | if (!empty($_REQUEST['shout'])) |
||
| 93 | { |
||
| 94 | // Pretty basic |
||
| 95 | is_not_guest(); |
||
| 96 | checkSession('request'); |
||
| 97 | |||
| 98 | // If you are not flooding the system, add the shout to the box |
||
| 99 | if (!($flood = sp_prevent_flood('spsbp', false))) |
||
| 100 | { |
||
| 101 | require_once(SUBSDIR . '/Post.subs.php'); |
||
| 102 | |||
| 103 | $_REQUEST['shout'] = Util::htmlspecialchars(trim($_REQUEST['shout'])); |
||
| 104 | $preparse = PreparseCode::instance(); |
||
| 105 | $preparse->preparsecode($_REQUEST['shout'], false); |
||
| 106 | |||
| 107 | if (!empty($_REQUEST['shout'])) |
||
| 108 | { |
||
| 109 | sportal_create_shout($context['SPortal']['shoutbox'], $_REQUEST['shout']); |
||
| 110 | } |
||
| 111 | } |
||
| 112 | else |
||
| 113 | { |
||
| 114 | $context['SPortal']['shoutbox']['warning'] = $flood; |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | // Removing a shout, regret saying that do you :P |
||
| 119 | if (!empty($_REQUEST['delete'])) |
||
| 120 | { |
||
| 121 | checkSession('request'); |
||
| 122 | |||
| 123 | if (!$can_moderate) |
||
| 124 | { |
||
| 125 | throw new Elk_Exception('error_sp_cannot_shoutbox_moderate', false); |
||
| 126 | } |
||
| 127 | |||
| 128 | $delete = (int) $_REQUEST['delete']; |
||
| 129 | |||
| 130 | if (!empty($delete)) |
||
| 131 | { |
||
| 132 | sportal_delete_shout($shoutbox_id, $delete); |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | // Responding to an ajax request |
||
| 137 | if (isset($_REQUEST['xml'])) |
||
| 138 | { |
||
| 139 | $shout_parameters = array( |
||
| 140 | 'limit' => $context['SPortal']['shoutbox']['num_show'], |
||
| 141 | 'bbc' => $context['SPortal']['shoutbox']['allowed_bbc'], |
||
| 142 | 'reverse' => $context['SPortal']['shoutbox']['reverse'], |
||
| 143 | 'cache' => $context['SPortal']['shoutbox']['caching'], |
||
| 144 | 'can_moderate' => $can_moderate, |
||
| 145 | ); |
||
| 146 | |||
| 147 | // Get all the shouts for this box |
||
| 148 | $context['SPortal']['shouts'] = sportal_get_shouts($shoutbox_id, $shout_parameters); |
||
| 149 | |||
| 150 | // Return a clean xml response |
||
| 151 | Template_Layers::instance()->removeAll(); |
||
| 152 | $context['sub_template'] = 'shoutbox_xml'; |
||
| 153 | $context['SPortal']['updated'] = empty($context['SPortal']['shoutbox']['last_update']) || $context['SPortal']['shoutbox']['last_update'] > $request_time; |
||
| 154 | |||
| 155 | return; |
||
| 156 | } |
||
| 157 | |||
| 158 | // Show all the shouts in this box |
||
| 159 | $total_shouts = sportal_get_shoutbox_count($shoutbox_id); |
||
| 160 | |||
| 161 | $context['per_page'] = $context['SPortal']['shoutbox']['num_show']; |
||
| 162 | $context['start'] = !empty($_REQUEST['start']) ? (int) $_REQUEST['start'] : 0; |
||
| 163 | $context['page_index'] = constructPageIndex($scripturl . '?action=shoutbox;shoutbox_id=' . $shoutbox_id, $context['start'], $total_shouts, $context['per_page']); |
||
| 164 | |||
| 165 | $shout_parameters = array( |
||
| 166 | 'start' => $context['start'], |
||
| 167 | 'limit' => $context['per_page'], |
||
| 168 | 'bbc' => $context['SPortal']['shoutbox']['allowed_bbc'], |
||
| 169 | 'cache' => $context['SPortal']['shoutbox']['caching'], |
||
| 170 | 'can_moderate' => $can_moderate, |
||
| 171 | ); |
||
| 172 | |||
| 173 | $context['SPortal']['shouts_history'] = sportal_get_shouts($shoutbox_id, $shout_parameters); |
||
| 174 | $context['SPortal']['shoutbox_id'] = $shoutbox_id; |
||
| 175 | $context['sub_template'] = 'shoutbox_all'; |
||
| 176 | $context['page_title'] = $context['SPortal']['shoutbox']['name']; |
||
| 177 | } |
||
| 179 |