Passed
Push — myDevel ( d43be7...574062 )
by Spuds
05:08
created

Articles_Block::setup()   D

Complexity

Conditions 12
Paths 320

Size

Total Lines 44
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 12
eloc 20
nc 320
nop 2
dl 0
loc 44
rs 4.6333
c 3
b 0
f 1

How to fix   Complexity   

Long Method

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:

1
<?php
2
3
/**
4
 * @package SimplePortal
5
 *
6
 * @author SimplePortal Team
7
 * @copyright 2015-2021 SimplePortal Team
8
 * @license BSD 3-clause
9
 * @version 1.0.0 RC2
10
 */
11
12
13
/**
14
 * Article Block, show the list of articles in the system
15
 *
16
 * @param mixed[] $parameters
17
 * 		'category' => list of categories to choose article from
18
 * 		'limit' => number of articles to show
19
 * 		'type' => 0 latest 1 random
20
 * 		'view' => 0 compact 1 full
21
 * 		'length' => length for the body text preview
22
 * 		'avatar' => whether to show the author avatar or not
23
 * 		'attachment' => Show the first attachment as "blog" image *if* no ILA tags found
24
 *
25
 * @param int $id - not used in this block
26
 * @param boolean $return_parameters if true returns the configuration options for the block
27
 */
28
class Articles_Block extends SP_Abstract_Block
29
{
30
	/**
31
	 * @var array
32
	 */
33
	protected $attachments = array();
34
35
	/**
36
	 * Constructor, used to define block parameters
37
	 *
38
	 * @param Database|null $db
39
	 */
40
	public function __construct($db = null)
41
	{
42
		require_once(SUBSDIR . '/PortalArticle.subs.php');
43
44
		$this->block_parameters = array(
45
			'category' => array(),
46
			'limit' => 'int',
47
			'type' => 'select',
48
			'view' => 'select',
49
			'length' => 'int',
50
			'avatar' => 'check',
51
			'attachment' => 'check',
52
		);
53
54
		parent::__construct($db);
55
	}
56
57
	/**
58
	 * Sets / loads optional parameters for a block
59
	 *
60
	 * - Called from PortalAdminBlocks.controller as part of block add/save/edit
61
	 */
62
	public function parameters()
63
	{
64
		global $txt;
65
66
		require_once(SUBSDIR . '/PortalAdmin.subs.php');
67
68
		// Load the sp categories for selection in the block
69
		$categories = sp_load_categories();
70
71
		$this->block_parameters['category'][0] = $txt['sp_all'];
72
		foreach ($categories as $category)
73
		{
74
			$this->block_parameters['category'][$category['id']] = $category['name'];
75
		}
76
77
		return $this->block_parameters;
78
	}
79
80
	/**
81
	 * Initializes the block for use.
82
	 *
83
	 * - Called from portal.subs as part of the sportal_load_blocks process
84
	 *
85
	 * @param mixed[] $parameters
86
	 * @param int $id
87
	 */
88
	public function setup($parameters, $id)
89
	{
90
		global $txt;
91
92
		require_once(SUBSDIR . '/Post.subs.php');
93
94
		// Set up for the query
95
		$category = empty($parameters['category']) ? 0 : (int) $parameters['category'];
96
		$limit = empty($parameters['limit']) ? 5 : (int) $parameters['limit'];
97
		$type = empty($parameters['type']) ? 0 : 1;
98
		$attachments = !empty($parameters['attachment']);
99
		$this->data['view'] = empty($parameters['view']) ? 0 : 1;
100
		$this->data['length'] = isset($parameters['length']) ? (int) $parameters['length'] : 250;
101
		$this->data['avatar'] = empty($parameters['avatar']) ? 0 : (int) $parameters['avatar'];
102
103
		// Fetch some articles
104
		$this->data['articles'] = sportal_get_articles(null, true, true, $type ? 'RAND()' : 'spa.date DESC', $category, $limit);
105
106
		// No articles in the system or none they can see
107
		if (empty($this->data['articles']))
108
		{
109
			$this->data['error_msg'] = $txt['error_sp_no_articles_found'];
110
			$this->setTemplate('template_sp_articles_error');
111
112
			return;
113
		}
114
115
		// Get the first image attachment for each article for this group
116
		if (!empty($attachments) && !empty($this->data['view']))
117
		{
118
			$this->data['articles'] = setBlogAttachments(getBlogAttachments($this->data['articles']));
119
		}
120
121
		// Doing the color thing
122
		$this->_color_ids();
123
124
		// And prepare the body text
125
		if (!empty($this->data['view']))
126
		{
127
			$this->prepare_view();
128
		}
129
130
		// Set the template name
131
		$this->setTemplate('template_sp_articles');
132
	}
133
134
	/**
135
	 * Prepares the body text when using the full view
136
	 */
137
	private function prepare_view()
138
	{
139
		global $context, $scripturl;
140
141
		// Needed for basic Lightbox functionality
142
		loadJavascriptFile('topic.js');
143
144
		foreach ($this->data['articles'] as $aid => $article)
145
		{
146
			// Good time to do this is ... now
147
			censor($article['title']);
148
			censor($article['body']);
149
150
			// Parse and optionally shorten the result
151
			$context['article']['id'] = $article['id'];
152
			$article['cut'] = sportal_parse_cutoff_content($article['body'], $article['type'], $this->_modSettings['sp_articles_length'], $article['article_id']);
0 ignored issues
show
Bug introduced by
It seems like $this->_modSettings['sp_articles_length'] can also be of type array<mixed,mixed>; however, parameter $length of sportal_parse_cutoff_content() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

152
			$article['cut'] = sportal_parse_cutoff_content($article['body'], $article['type'], /** @scrutinizer ignore-type */ $this->_modSettings['sp_articles_length'], $article['article_id']);
Loading history...
153
154
			if ($this->_modSettings['sp_resize_images'])
155
			{
156
				$article['body'] = str_ireplace('class="bbc_img', 'class="bbc_img sp_article', $article['body']);
157
			}
158
159
			// Check if we need the blog attachment, no if we rendered any ILA/[sp attach] tags
160
			if (strpos($article['body'], '<img src="' . $scripturl . '?action=portal;sa=spattach;article=') !== false)
161
			{
162
				$article['attachments'] = array();
163
			}
164
165
			// Account for embedded videos
166
			$this->data['embed_videos'] = !empty($this->_modSettings['enableVideoEmbeding']);
167
168
			// Add / replace the data
169
			$this->data['articles'][$aid] = array_merge($this->data['articles'][$aid], $article);
170
		}
171
	}
172
173
	/**
174
	 * Provide the color profile id's
175
	 */
176
	private function _color_ids()
177
	{
178
		global $color_profile;
179
180
		$color_ids = array();
181
		foreach ($this->data['articles'] as $article)
182
		{
183
			if (!empty($article['author']['id']))
184
			{
185
				$color_ids[$article['author']['id']] = $article['author']['id'];
186
			}
187
		}
188
189
		if (sp_loadColors($color_ids) !== false)
0 ignored issues
show
introduced by
The condition sp_loadColors($color_ids) !== false is always true.
Loading history...
190
		{
191
			foreach ($this->data['articles'] as $k => $p)
192
			{
193
				if (!empty($color_profile[$p['author']['id']]['link']))
194
				{
195
					$this->data['articles'][$k]['author']['link'] = $color_profile[$p['author']['id']]['link'];
196
				}
197
			}
198
		}
199
	}
200
}
201
202
/**
203
 * Error template
204
 *
205
 * @param mixed[] $data
206
 */
207
function template_sp_articles_error($data)
208
{
209
	echo $data['error_msg'];
210
}
211
212
/**
213
 * Main template
214
 *
215
 * @param mixed[] $data
216
 */
217
function template_sp_articles($data)
218
{
219
	global $scripturl, $txt, $context;
220
221
	// Not showing avatars, just use a compact link view
222
	if (empty($data['view']))
223
	{
224
		echo '
225
			<ul class="sp_list">';
226
227
		foreach ($data['articles'] as $article)
228
		{
229
			echo '
230
				<li>', sp_embed_image('topic'), ' ', $article['link'], '</li>';
231
		}
232
233
		echo '
234
			</ul>';
235
	}
236
	// Or the full monty!
237
	else
238
	{
239
		// Auto video embedding enabled?
240
		if ($data['embed_videos'])
241
		{
242
			addInlineJavascript('
243
			$(document).ready(function() {
244
				$().linkifyvideo(oEmbedtext);
245
			});', true);
246
		}
247
248
		// Relative times?
249
		if (!empty($context['using_relative_time']))
250
		{
251
			addInlineJavascript('$(\'.sp_article_latest\').addClass(\'relative\');', true);
252
		}
253
254
		// Show the articles.
255
		foreach ($data['articles'] as $article)
256
		{
257
			echo '
258
			<h3 class="secondary_header">',
259
				$article['title'], '
260
			</h3>
261
			<div id="msg_', $article['article_id'], '" class="sp_article_content">
262
				<div class="sp_content_padding">';
263
264
			if (!empty($article['author']['avatar']['href']) && $data['avatar'])
265
			{
266
				echo '
267
					<a href="', $scripturl, '?action=profile;u=', $article['author']['id'], '">
268
						<img src="', $article['author']['avatar']['href'], '" alt="', $article['author']['name'], '" style="max-width:40px" class="floatright" />
269
					</a>
270
					<span>
271
						', sprintf(!empty($context['using_relative_time']) ? $txt['sp_posted_on_in_by'] : $txt['sp_posted_in_on_by'], $article['category']['link'], htmlTime($article['date']), $article['author']['link']), '
272
						<br />
273
						', sprintf($article['view_count'] == 1 ? $txt['sp_viewed_time'] : $txt['sp_viewed_times'], $article['view_count']), ', ', sprintf($article['comment_count'] == 1 ? $txt['sp_commented_on_time'] : $txt['sp_commented_on_times'], $article['comment_count']), '
274
					</span>';
275
			}
276
			else
277
			{
278
				echo '
279
					<span>
280
						', sprintf(!empty($context['using_relative_time']) ? $txt['sp_posted_on_in_by'] : $txt['sp_posted_in_on_by'], $article['category']['link'], htmlTime($article['date']), $article['author']['link']), '
281
						<br />
282
						', sprintf($article['view_count'] == 1 ? $txt['sp_viewed_time'] : $txt['sp_viewed_times'], $article['view_count']), ', ', sprintf($article['comment_count'] == 1 ? $txt['sp_commented_on_time'] : $txt['sp_commented_on_times'], $article['comment_count']), '
283
					</span>';
284
			}
285
286
			echo '
287
					<div class="post inner sp_inner">';
288
289
			if (!empty($article['attachments']))
290
			{
291
				echo '
292
						<div class="sp_attachment_thumb">';
293
294
				// If you want Fancybox to tag this, remove nfb_ from the id
295
				echo '
296
							<a href="', $article['href'], '" id="nfb_link_', $article['attachments']['id'], '">
297
								<img src="', $article['attachments']['href'], '" alt="" title="', $article['attachments']['name'], '" id="thumb_', $article['attachments']['id'], '" />
298
							</a>';
299
300
				echo '
301
						</div>';
302
			}
303
304
			echo '
305
					<div class="sp_article_block">',
306
			$article['body'], '
307
					</div>
308
				
309
				<div class="submitbutton">
310
					<a class="linkbutton" href="', $article['href'], '">', $txt['sp_read_more'], '</a>
311
					<a class="linkbutton" href="', $article['href'], '#sp_view_comments">', $txt['sp_write_comment'], '</a>
312
				</div>
313
				</div>
314
			</div>
315
		</div>';
316
		}
317
	}
318
}
319