| Conditions | 35 |
| Paths | > 20000 |
| Total Lines | 187 |
| Code Lines | 96 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 6 | ||
| Bugs | 1 | Features | 1 |
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 |
||
| 67 | public function setup($parameters, $id) |
||
| 68 | { |
||
| 69 | global $scripturl, $txt, $settings, $context; |
||
| 70 | |||
| 71 | // Break out / sanitize all the parameters |
||
| 72 | $board = !empty($parameters['board']) ? explode('|', $parameters['board']) : null; |
||
| 73 | $limit = !empty($parameters['limit']) ? (int) $parameters['limit'] : 5; |
||
| 74 | $start = !empty($parameters['start']) ? (int) $parameters['start'] : 0; |
||
| 75 | $length = isset($parameters['length']) ? (int) $parameters['length'] : 250; |
||
| 76 | $avatars = !empty($parameters['avatar']); |
||
| 77 | $attachments = !empty($parameters['attachment']); |
||
| 78 | $per_page = !empty($parameters['per_page']) ? (int) $parameters['per_page'] : 0; |
||
| 79 | |||
| 80 | $limit = max(0, $limit); |
||
| 81 | $start = max(0, $start); |
||
| 82 | |||
| 83 | loadLanguage('Stats'); |
||
| 84 | |||
| 85 | // Common message icons |
||
| 86 | $this->_stable_icons(); |
||
| 87 | |||
| 88 | // Load the topics they can see |
||
| 89 | $request = $this->_db->query('', ' |
||
| 90 | SELECT |
||
| 91 | t.id_first_msg |
||
| 92 | FROM {db_prefix}topics AS t |
||
| 93 | INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board) |
||
| 94 | INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg) |
||
| 95 | WHERE {query_see_board} |
||
| 96 | AND ' . (empty($board) ? 't.id_first_msg >= {int:min_msg_id}' : 't.id_board IN ({array_int:current_board})') . ($this->_modSettings['postmod_active'] ? ' |
||
| 97 | AND t.approved = {int:is_approved}' : '') . ' |
||
| 98 | AND (t.locked != {int:locked} OR m.icon != {string:icon}) |
||
| 99 | ORDER BY t.id_first_msg DESC |
||
| 100 | LIMIT {int:limit}', |
||
| 101 | array( |
||
| 102 | 'current_board' => $board, |
||
| 103 | 'min_msg_id' => $this->_modSettings['maxMsgID'] - 45 * min($limit, 5), |
||
| 104 | 'is_approved' => 1, |
||
| 105 | 'locked' => 1, |
||
| 106 | 'icon' => 'moved', |
||
| 107 | 'limit' => $limit, |
||
| 108 | ) |
||
| 109 | ); |
||
| 110 | $posts = array(); |
||
| 111 | while ($row = $this->_db->fetch_assoc($request)) |
||
| 112 | { |
||
| 113 | $posts[] = $row['id_first_msg']; |
||
| 114 | } |
||
| 115 | $this->_db->free_result($request); |
||
| 116 | |||
| 117 | // No posts, basic error message it is |
||
| 118 | $current_url = ''; |
||
| 119 | if (empty($posts)) |
||
| 120 | { |
||
| 121 | $this->setTemplate('template_sp_boardNews_error'); |
||
| 122 | $this->data['error_msg'] = $txt['error_sp_no_posts_found']; |
||
| 123 | |||
| 124 | return; |
||
| 125 | } |
||
| 126 | // Posts and page limits? |
||
| 127 | elseif (!empty($per_page)) |
||
| 128 | { |
||
| 129 | $limit = count($posts); |
||
| 130 | $start = !empty($_REQUEST['news' . $id]) ? (int) $_REQUEST['news' . $id] : 0; |
||
| 131 | |||
| 132 | $clean_url = str_replace('%', '%%', preg_replace('~news' . $id . '=[^;]+;?~', '', $_SERVER['REQUEST_URL'])); |
||
| 133 | $current_url = $clean_url . (strpos($clean_url, '?') !== false ? (in_array(substr($clean_url, -1), array(';', '?')) ? '' : ';') : '?'); |
||
| 134 | } |
||
| 135 | |||
| 136 | // Load the actual post details for the topics |
||
| 137 | $request = $this->_db->query('', ' |
||
| 138 | SELECT |
||
| 139 | m.icon, m.subject, m.body, IFNULL(mem.real_name, m.poster_name) AS poster_name, m.poster_time, |
||
| 140 | t.num_replies, t.id_topic, m.id_member, m.smileys_enabled, m.id_msg, t.locked, mem.avatar, mem.email_address, |
||
| 141 | a.id_attach, a.attachment_type, a.filename, t.num_views |
||
| 142 | FROM {db_prefix}topics AS t |
||
| 143 | INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg) |
||
| 144 | LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member) |
||
| 145 | LEFT JOIN {db_prefix}attachments AS a ON (a.id_member = mem.id_member) |
||
| 146 | WHERE t.id_first_msg IN ({array_int:post_list}) |
||
| 147 | ORDER BY t.id_first_msg DESC |
||
| 148 | LIMIT ' . (!empty($per_page) ? '{int:start}, ' : '') . '{int:limit}', |
||
| 149 | array( |
||
| 150 | 'post_list' => $posts, |
||
| 151 | 'start' => $start, |
||
| 152 | 'limit' => !empty($per_page) ? $per_page : $limit, |
||
| 153 | ) |
||
| 154 | ); |
||
| 155 | |||
| 156 | // Get the first attachment for each post for this group |
||
| 157 | if (!empty($attachments)) |
||
| 158 | { |
||
| 159 | $this->loadAttachments($posts); |
||
| 160 | } |
||
| 161 | |||
| 162 | $this->data['news'] = array(); |
||
| 163 | |||
| 164 | while ($row = $this->_db->fetch_assoc($request)) |
||
| 165 | { |
||
| 166 | // Good time to do this is ... now |
||
| 167 | censor($row['subject']); |
||
| 168 | censor($row['body']); |
||
| 169 | |||
| 170 | $attach = !empty($attachments) ? $this->getMessageAttach($row['id_msg'], $row['id_topic'], $row['body']) : ''; |
||
| 171 | $limited = sportal_parse_cutoff_content($row['body'], 'bbc', $length); |
||
| 172 | |||
| 173 | // Link the ellipsis if the body has been shortened. |
||
| 174 | if ($limited) |
||
| 175 | { |
||
| 176 | $row['body'] .= '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0" title="' . $row['subject'] . '">…</a>'; |
||
| 177 | } |
||
| 178 | |||
| 179 | if (empty($this->_modSettings['messageIconChecks_disable']) && !isset($this->icon_sources[$row['icon']])) |
||
| 180 | { |
||
| 181 | $this->icon_sources[$row['icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['icon'] . '.png') ? 'images_url' : 'default_images_url'; |
||
| 182 | } |
||
| 183 | |||
| 184 | if ($this->_modSettings['sp_resize_images']) |
||
| 185 | { |
||
| 186 | $row['body'] = str_ireplace('class="bbc_img', 'class="bbc_img sp_article', $row['body']); |
||
| 187 | } |
||
| 188 | |||
| 189 | if (!empty($row['id_member'])) |
||
| 190 | { |
||
| 191 | $this->color_ids[$row['id_member']] = $row['id_member']; |
||
| 192 | } |
||
| 193 | |||
| 194 | // If ILA is enable and what we rendered has ILA tags, assume they don't need any further attachment help |
||
| 195 | if (!empty($this->_modSettings['attachment_inline_enabled'])) |
||
| 196 | { |
||
| 197 | if (strpos($row['body'], '<img src="' . $scripturl . '?action=dlattach;attach=') !== false) |
||
| 198 | { |
||
| 199 | $attach = array(); |
||
| 200 | } |
||
| 201 | } |
||
| 202 | |||
| 203 | // Build an array of message information for output |
||
| 204 | $this->data['news'][] = array( |
||
| 205 | 'id' => $row['id_topic'], |
||
| 206 | 'message_id' => $row['id_msg'], |
||
| 207 | 'icon' => '<img src="' . $settings[$this->icon_sources[$row['icon']]] . '/post/' . $row['icon'] . '.png" class="icon" alt="' . $row['icon'] . '" />', |
||
| 208 | 'icon_name' => $row['icon'], |
||
| 209 | 'subject' => $row['subject'], |
||
| 210 | 'time' => standardTime($row['poster_time']), |
||
| 211 | 'views' => $row['num_views'], |
||
| 212 | 'body' => $row['body'], |
||
| 213 | 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.0', |
||
| 214 | 'link' => '<a class="linkbutton" href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $txt['sp_read_more'] . '</a>', |
||
| 215 | 'replies' => $row['num_replies'], |
||
| 216 | 'comment_href' => !empty($row['locked']) ? '' : $scripturl . '?action=post;topic=' . $row['id_topic'] . '.' . $row['num_replies'] . ';num_replies=' . $row['num_replies'], |
||
| 217 | 'comment_link' => !empty($row['locked']) ? '' : '<a class="linkbutton" href="' . $scripturl . '?action=post;topic=' . $row['id_topic'] . '.' . $row['num_replies'] . ';num_replies=' . $row['num_replies'] . '">' . $txt['reply'] . '</a>', |
||
| 218 | 'new_comment' => !empty($row['locked']) ? '' : '<a class="linkbutton" href="' . $scripturl . '?action=post;topic=' . $row['id_topic'] . '.' . $row['num_replies'] . '">' . $txt['reply'] . '</a>', |
||
| 219 | 'poster' => array( |
||
| 220 | 'id' => $row['id_member'], |
||
| 221 | 'name' => $row['poster_name'], |
||
| 222 | 'href' => !empty($row['id_member']) ? $scripturl . '?action=profile;u=' . $row['id_member'] : '', |
||
| 223 | 'link' => !empty($row['id_member']) ? '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>' : $row['poster_name'] |
||
| 224 | ), |
||
| 225 | 'locked' => !empty($row['locked']), |
||
| 226 | 'is_last' => false, |
||
| 227 | 'avatar' => $avatars ? determineAvatar($row) : array(), |
||
| 228 | 'attachment' => $attach, |
||
| 229 | ); |
||
| 230 | } |
||
| 231 | $this->_db->free_result($request); |
||
| 232 | |||
| 233 | // Nothing found, set the message and return |
||
| 234 | if (empty($this->data['news'])) |
||
| 235 | { |
||
| 236 | $this->setTemplate('template_sp_boardNews_error'); |
||
| 237 | $this->data['error_msg'] = $txt['error_sp_no_posts_found']; |
||
| 238 | |||
| 239 | return; |
||
| 240 | } |
||
| 241 | |||
| 242 | $this->data['news'][count($this->data['news']) - 1]['is_last'] = true; |
||
| 243 | $this->setTemplate('template_sp_boardNews'); |
||
| 244 | |||
| 245 | // If we want color id's then lets add them in |
||
| 246 | $this->_color_ids(); |
||
| 247 | |||
| 248 | // Account for embedded videos |
||
| 249 | $this->data['embed_videos'] = !empty($this->_modSettings['enableVideoEmbeding']); |
||
| 250 | |||
| 251 | if (!empty($per_page)) |
||
| 252 | { |
||
| 253 | $context['sp_boardNews_page_index'] = constructPageIndex($current_url . 'news' . $id . '=%1$d', $start, $limit, $per_page, true); |
||
| 254 | } |
||
| 473 |