Passed
Push — myDevel ( 574062...1cfc54 )
by Spuds
02:45
created

Articles_Block::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 10
nc 1
nop 1
dl 0
loc 15
rs 9.9332
c 2
b 0
f 1
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
		// Prepare what we have for the template
116
		$this->prepare_data($attachments);
117
118
		// Set the template name
119
		$this->setTemplate('template_sp_articles');
120
	}
121
122
	/**
123
	 * Fetches attachments, colors ID's, censors text, runs the parsers on the data
124
	 *
125
	 * @param bool $attachments
126
	 */
127
	private function prepare_data($attachments)
128
	{
129
		// Get the first image attachment for each article for this group
130
		if (!empty($attachments) && !empty($this->data['view']))
131
		{
132
			$this->data['articles'] = setBlogAttachments(getBlogAttachments($this->data['articles']));
133
		}
134
135
		// Doing the color thing
136
		$this->_color_ids();
137
138
		// And prepare the body text
139
		if (!empty($this->data['view']))
140
		{
141
			$this->prepare_view();
142
		}
143
	}
144
145
	/**
146
	 * Prepares the body text when using the full view
147
	 */
148
	private function prepare_view()
149
	{
150
		global $context, $scripturl;
151
152
		// Needed for basic Lightbox functionality
153
		loadJavascriptFile('topic.js');
154
155
		foreach ($this->data['articles'] as $aid => $article)
156
		{
157
			// Good time to do this is ... now
158
			censor($article['title']);
159
			censor($article['body']);
160
161
			// Parse and optionally shorten the result
162
			$context['article']['id'] = $article['id'];
163
			$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

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