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 recent 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_RECENT'; |
||
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_recent_module'; |
||
43 | |||
44 | /** |
||
45 | * custom acp template |
||
46 | * file must be in "adm/style/portal/" |
||
47 | */ |
||
48 | public $custom_acp_tpl = ''; |
||
49 | |||
50 | /** @var \phpbb\auth\auth */ |
||
51 | protected $auth; |
||
52 | |||
53 | /** @var \phpbb\config\config */ |
||
54 | protected $config; |
||
55 | |||
56 | /** @var \phpbb\db\driver */ |
||
57 | protected $db; |
||
58 | |||
59 | /** @var \phpbb\request\request */ |
||
60 | protected $request; |
||
61 | |||
62 | /** @var \phpbb\template */ |
||
63 | protected $template; |
||
64 | |||
65 | /** @var string PHP file extension */ |
||
66 | protected $php_ext; |
||
67 | |||
68 | /** @var string phpBB root path */ |
||
69 | protected $phpbb_root_path; |
||
70 | |||
71 | /** |
||
72 | * Construct a recent object |
||
73 | * |
||
74 | * @param \phpbb\auth\auth $auth phpBB auth |
||
75 | * @param \phpbb\config\config $config phpBB config |
||
76 | * @param \phpbb\db\driver $db phpBB db driver |
||
77 | * @param \phpbb\request\request $request phpBB request |
||
78 | * @param \phpbb\template $template phpBB template |
||
79 | * @param string $phpbb_root_path phpBB root path |
||
80 | * @param string $phpEx php file extension |
||
81 | */ |
||
82 | View Code Duplication | public function __construct($auth, $config, $db, $request, $template, $phpbb_root_path, $phpEx) |
|
92 | |||
93 | /** |
||
94 | * {@inheritdoc} |
||
95 | */ |
||
96 | public function get_template_center($module_id) |
||
97 | { |
||
98 | // |
||
99 | // Exclude forums |
||
100 | // |
||
101 | $sql_where = ''; |
||
102 | if ($this->config['board3_recent_forum_' . $module_id] > 0) |
||
103 | { |
||
104 | $exclude_forums = explode(',', $this->config['board3_recent_forum_' . $module_id]); |
||
105 | |||
106 | $sql_where = ' AND ' . $this->db->sql_in_set('forum_id', array_map('intval', $exclude_forums), ($this->config['board3_recent_exclude_forums_' . $module_id]) ? true : false); |
||
107 | } |
||
108 | |||
109 | // Get a list of forums the user cannot read |
||
110 | $forum_ary = array_unique(array_keys($this->auth->acl_getf('!f_read', true))); |
||
111 | |||
112 | // Determine first forum the user is able to read (must not be a category) |
||
113 | $sql = 'SELECT forum_id |
||
114 | FROM ' . FORUMS_TABLE . ' |
||
115 | WHERE forum_type = ' . FORUM_POST; |
||
116 | |||
117 | $forum_sql = ''; |
||
118 | if (sizeof($forum_ary)) |
||
119 | { |
||
120 | $sql .= ' AND ' . $this->db->sql_in_set('forum_id', $forum_ary, true); |
||
121 | $forum_sql = ' AND ' . $this->db->sql_in_set('t.forum_id', $forum_ary, true); |
||
122 | } |
||
123 | |||
124 | $result = $this->db->sql_query_limit($sql, 1, 0, 600); |
||
125 | $g_forum_id = (int) $this->db->sql_fetchfield('forum_id'); |
||
126 | $this->db->sql_freeresult($result); |
||
127 | |||
128 | // |
||
129 | // Recent announcements |
||
130 | // |
||
131 | $sql = 'SELECT topic_title, forum_id, topic_id |
||
132 | FROM ' . TOPICS_TABLE . ' t |
||
133 | WHERE topic_status <> ' . FORUM_LINK . ' |
||
134 | AND topic_visibility = ' . ITEM_APPROVED . ' |
||
135 | AND (topic_type = ' . POST_ANNOUNCE . ' OR topic_type = ' . POST_GLOBAL . ') |
||
136 | AND topic_moved_id = 0 |
||
137 | ' . $sql_where . $forum_sql . ' |
||
138 | ORDER BY topic_time DESC'; |
||
139 | $result = $this->db->sql_query_limit($sql, $this->config['board3_max_topics_' . $module_id], 0 , 30); |
||
140 | |||
141 | View Code Duplication | while (($row = $this->db->sql_fetchrow($result)) && ($row['topic_title'])) |
|
142 | { |
||
143 | // auto auth |
||
144 | if (($this->auth->acl_get('f_read', $row['forum_id'])) || ($row['forum_id'] == '0')) |
||
145 | { |
||
146 | $this->template->assign_block_vars('latest_announcements', array( |
||
147 | 'TITLE' => character_limit($row['topic_title'], $this->config['board3_recent_title_limit_' . $module_id]), |
||
148 | 'FULL_TITLE' => censor_text($row['topic_title']), |
||
149 | 'U_VIEW_TOPIC' => append_sid("{$this->phpbb_root_path}viewtopic.{$this->php_ext}", 'f=' . (($row['forum_id'] == 0) ? $g_forum_id : $row['forum_id']) . '&t=' . $row['topic_id']) |
||
150 | )); |
||
151 | } |
||
152 | } |
||
153 | $this->db->sql_freeresult($result); |
||
154 | |||
155 | // |
||
156 | // Recent hot topics |
||
157 | // |
||
158 | $sql = 'SELECT topic_title, forum_id, topic_id |
||
159 | FROM ' . TOPICS_TABLE . ' t |
||
160 | WHERE topic_visibility = ' . ITEM_APPROVED . ' |
||
161 | AND topic_posts_approved >' . $this->config['hot_threshold'] . ' |
||
162 | AND topic_moved_id = 0 |
||
163 | ' . $sql_where . $forum_sql . ' |
||
164 | ORDER BY topic_time DESC'; |
||
165 | $result = $this->db->sql_query_limit($sql, $this->config['board3_max_topics_' . $module_id], 0, 30); |
||
166 | |||
167 | View Code Duplication | while (($row = $this->db->sql_fetchrow($result)) && ($row['topic_title'])) |
|
168 | { |
||
169 | // auto auth |
||
170 | if (($this->auth->acl_get('f_read', $row['forum_id'])) || ($row['forum_id'] == '0')) |
||
171 | { |
||
172 | $this->template->assign_block_vars('latest_hot_topics', array( |
||
173 | 'TITLE' => character_limit($row['topic_title'], $this->config['board3_recent_title_limit_' . $module_id]), |
||
174 | 'FULL_TITLE' => censor_text($row['topic_title']), |
||
175 | 'U_VIEW_TOPIC' => append_sid("{$this->phpbb_root_path}viewtopic.{$this->php_ext}", 'f=' . (($row['forum_id'] == 0) ? $g_forum_id : $row['forum_id']) . '&t=' . $row['topic_id']) |
||
176 | )); |
||
177 | } |
||
178 | } |
||
179 | $this->db->sql_freeresult($result); |
||
180 | |||
181 | // |
||
182 | // Recent topic (only show normal topic) |
||
183 | // |
||
184 | $sql = 'SELECT topic_title, forum_id, topic_id |
||
185 | FROM ' . TOPICS_TABLE . ' t |
||
186 | WHERE topic_status <> ' . ITEM_MOVED . ' |
||
187 | AND topic_visibility = ' . ITEM_APPROVED . ' |
||
188 | AND topic_type = ' . POST_NORMAL . ' |
||
189 | AND topic_moved_id = 0 |
||
190 | ' . $sql_where . $forum_sql . ' |
||
191 | ORDER BY topic_time DESC'; |
||
192 | $result = $this->db->sql_query_limit($sql, $this->config['board3_max_topics_' . $module_id], 0, 30); |
||
193 | |||
194 | while (($row = $this->db->sql_fetchrow($result)) && ($row['topic_title'])) |
||
195 | { |
||
196 | // auto auth |
||
197 | if (($this->auth->acl_get('f_read', $row['forum_id'])) || ($row['forum_id'] == '0')) |
||
198 | { |
||
199 | $this->template->assign_block_vars('latest_topics', array( |
||
200 | 'TITLE' => character_limit($row['topic_title'], $this->config['board3_recent_title_limit_' . $module_id]), |
||
201 | 'FULL_TITLE' => censor_text($row['topic_title']), |
||
202 | 'U_VIEW_TOPIC' => append_sid("{$this->phpbb_root_path}viewtopic.{$this->php_ext}", 'f=' . $row['forum_id'] . '&t=' . $row['topic_id']) |
||
203 | )); |
||
204 | } |
||
205 | } |
||
206 | $this->db->sql_freeresult($result); |
||
207 | |||
208 | return 'recent_center.html'; |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * {@inheritdoc} |
||
213 | */ |
||
214 | public function get_template_acp($module_id) |
||
227 | |||
228 | /** |
||
229 | * {@inheritdoc} |
||
230 | */ |
||
231 | public function install($module_id) |
||
239 | |||
240 | /** |
||
241 | * {@inheritdoc} |
||
242 | */ |
||
243 | View Code Duplication | public function uninstall($module_id, $db) |
|
255 | } |
||
256 |