Passed
Push — myDevel ( 6b67fa...0991c9 )
by Spuds
04:10
created

Articles_Block::loadAttachments()   B

Complexity

Conditions 8
Paths 21

Size

Total Lines 40
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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

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