template_view_article()   F
last analyzed

Complexity

Conditions 24
Paths 576

Size

Total Lines 137
Code Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
cc 24
eloc 60
nc 576
nop 0
dl 0
loc 137
rs 0.5888
c 3
b 0
f 2

How to fix   Long Method    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 ElkArte
5
 *
6
 * @author SimplePortal Team
7
 * @copyright 2015-2021 SimplePortal Team
8
 * @license BSD 3-clause
9
 * @version 1.0.0
10
 */
11
12
/**
13
 * Used to view articles on the portal
14
 */
15
function template_view_articles()
16
{
17
	global $context, $txt;
18
19
	echo '
20
	<div id="sp_view_articles">
21
		<h3 class="category_header">
22
			', $context['page_title'], '
23
		</h3>';
24
25
	if (empty($context['articles']))
26
	{
27
		echo '
28
		<div class="infobox">',
29
			$txt['error_sp_no_articles'], '
30
		</div>';
31
	}
32
33
	foreach ($context['articles'] as $id => $article)
34
	{
35
		echo '
36
		<div class="sp_content_padding">
37
			<div class="sp_article_detail">';
38
39
		// Start off with the avatar
40
		if (!empty($article['author']['avatar']['image']))
41
		{
42
			echo $article['author']['avatar']['image'];
43
		}
44
45
		// And now the article
46
		echo '
47
				<span class="sp_article_latest">
48
					', sprintf(!empty($context['using_relative_time']) ? $txt['sp_posted_on_in_by'] : $txt['sp_posted_in_on_by'], $article['category']['link'], $article['date'], $article['author']['link']), '
49
					<br />
50
					', sprintf($article['views'] == 1 ? $txt['sp_viewed_time'] : $txt['sp_viewed_times'], $article['views']), ', ', sprintf($article['comments'] == 1 ? $txt['sp_commented_on_time'] : $txt['sp_commented_on_times'], $article['comments']), '
51
				</span>
52
				<h4>', $article['link'], '</h4>
53
			</div>
54
			<div id="msg_', $id, '" class="inner sp_inner">', $article['preview'], '<a href="', $article['href'], '">...</a></div>
55
			<div class="sp_article_extra clear">
56
				<a class="linkbutton" href="', $article['href'], '">', $txt['sp_read_more'], '</a>
57
				<a class="linkbutton" href="', $article['href'], '#sp_view_comments">', $txt['sp_write_comment'], '</a>
58
			</div>
59
		</div>';
60
	}
61
62
	echo '
63
	</div>';
64
65
	// Pages as well?
66
	if (!empty($context['page_index']))
67
		template_pagesection();
68
69
	if (!empty($context['using_relative_time']))
70
		addInlineJavascript('$(\'.sp_article_latest\').addClass(\'relative\');', true);
71
}
72
73
/**
74
 * Template for viewing a specific article in the system
75
 * Also used to preview an article from the new article form
76
 */
77
function template_view_article()
78
{
79
	global $context, $txt;
80
81
	echo '
82
	<article id="sp_view_article">';
83
84
	if (empty($context['article']['style']['no_title']))
85
	{
86
		echo '
87
		<h1', strpos($context['article']['style']['title']['class'], 'custom') === false ? ' class="' . $context['article']['style']['title']['class'] . '"' : '', !empty($context['article']['style']['title']['style']) ? ' style="' . $context['article']['style']['title']['style'] . '"' : '', '>
88
			', $context['article']['title'], '
89
		</h1>';
90
	}
91
92
	echo '
93
		<div class="sp_content_padding ', $context['article']['style']['body']['class'], '"', !empty($context['article']['style']['body']['style']) ? ' style="' . $context['article']['style']['body']['style'] . '"' : '', '>
94
			<div class="sp_article_detail">';
95
96
	if (!empty($context['article']['author']['avatar']['image']))
97
		echo $context['article']['author']['avatar']['image'];
98
99
	echo '
100
				<span class="sp_article_latest">
101
					', sprintf(!empty($context['using_relative_time']) ? $txt['sp_posted_on_in_by'] : $txt['sp_posted_in_on_by'], $context['article']['category']['link'], $context['article']['date'], $context['article']['author']['link']);
102
103
	if (!empty($context['article']['author']['avatar']['image']))
104
		echo '
105
					<br />';
106
	else
107
		echo '
108
				</span>
109
				<br />
110
				<span class="floatright">';
111
112
	echo '
113
				', sprintf($context['article']['view_count'] == 1 ? $txt['sp_viewed_time'] : $txt['sp_viewed_times'], $context['article']['view_count']), ', ',
114
				sprintf($context['article']['comment_count'] == 1 ? $txt['sp_commented_on_time'] : $txt['sp_commented_on_times'], $context['article']['comment_count']), '
115
				</span>
116
			</div>
117
			<div id="msg_', $context['article']['id'], '" class="messageContent inner sp_inner">' ,
118
				$context['article']['body'];
119
120
	if ($context['article']['can_moderate'] && empty($context['preview']))
121
		echo '
122
				<div class="submitbutton">
123
					<a class="linkbutton" href="?action=admin;area=portalarticles;sa=edit;article_id=' . $context['article']['id'] . ';' . $context['session_var'] . '=' . $context['session_id'] . '" accesskey="e">' . $txt['edit'] . '</a>
124
					<a class="linkbutton" href="?action=admin;area=portalarticles;sa=delete;article_id=' . $context['article']['id'] . ';' . $context['session_var'] . '=' . $context['session_id'] . '" onclick="return confirm(' . JavaScriptEscape($txt['quickmod_confirm']) . ') && submitThisOnce(this);" accesskey="d">' . $txt['delete'] . '</a>
125
				</div>';
126
127
	// Assuming there are attachments...
128
	if (!empty($context['article']['attachment']))
129
	{
130
		template_sp_display_attachments($context['article'], false);
131
	}
132
133
	echo '		
134
			</div>
135
		</div>';
136
137
	// Not just previewing the new article, then show comments etc
138
	if (empty($context['preview']))
139
	{
140
		echo '
141
		<section id="sp_view_comments">
142
			<h3 class="category_header">
143
				', $txt['sp-comments'], '
144
			</h3>';
145
146
		if (empty($context['article']['comments']))
147
		{
148
			echo '
149
			<div class="infobox">
150
					', $txt['error_sp_no_comments'], '
151
			</div>';
152
		}
153
154
		foreach ($context['article']['comments'] as $comment)
155
		{
156
			echo '
157
			<div id="comment', $comment['id'], '" class="content">
158
				<div class="sp_content_padding flow_auto">
159
					<div class="sp_comment_detail">';
160
161
			if (!empty($comment['author']['avatar']['image']))
162
				echo $comment['author']['avatar']['image'];
163
164
			// Show the edit icons if they are allowed
165
			if ($comment['can_moderate'])
166
				echo '
167
						<div class="floatright">
168
							<a href="', $context['article']['href'], ';modify=', $comment['id'], ';', $context['session_var'], '=', $context['session_id'], '#sp_comment">', sp_embed_image('modify'), '</a>
169
							<a href="', $context['article']['href'], ';delete=', $comment['id'], ';', $context['session_var'], '=', $context['session_id'], '">', sp_embed_image('delete'), '</a>
170
						</div>';
171
172
			echo '
173
						<span class="sp_article_latest">', sprintf($txt['sp_posted_by'], $comment['time'], $comment['author']['link']), '</span>
174
					</div>
175
					<hr />
176
					<p class="sp_comment_body">
177
						', $comment['body'], '
178
					</p>
179
				</div>
180
			</div>';
181
		}
182
183
		// Pages as well?
184
		if (!empty($context['page_index']))
185
			template_pagesection();
186
187
		// Show the comment box
188
		if ($context['article']['can_comment'])
189
		{
190
			echo '
191
			<section id="sp_comment" class="sp_content_padding">
192
					<form action="', $context['article']['href'], '" method="post" accept-charset="UTF-8">
193
					<textarea name="body" rows="5" cols="50" style="width: 100%;padding: 0.1em 0.2em" tabindex="', $context['tabindex']++, '">', !empty($context['article']['comment']['body']) ? $context['article']['comment']['body'] : '', '</textarea>
194
					<div class="submitbutton">
195
						<input type="submit" name="submit" value="', !empty($context['article']['comment']) ? $txt['sp_modify'] : $txt['sp_submit'], '" class="right_submit" />
196
						<input type="hidden" name="comment" value="', !empty($context['article']['comment']['id']) ? $context['article']['comment']['id'] : 0, '" />
197
						<input type="hidden" name="', $context['session_var'], '" value="', $context['session_id'], '" />
198
					</div>
199
				</form>
200
			</section>';
201
		}
202
203
		echo '
204
		</section>';
205
	}
206
207
	echo '
208
	</article>';
209
210
	template_article_schema_script();
211
212
	if (!empty($context['using_relative_time']))
213
		addInlineJavascript('$(\'.sp_article_latest\').addClass(\'relative\');', true);
214
}
215
216
/**
217
 * Output schema.org data as ld+json
218
 */
219
function template_article_schema_script()
220
{
221
	global $context, $boardurl;
222
223
	$post = trim(preg_replace('~<[^>]+>~', ' ', $context['article']['body']));
224
	$description = Util::shorten_text(preg_replace('~\s\s+~', ' ', $post));
225
226
	$smd = array(
227
		'@context' => 'https://schema.org',
228
		'@type' => 'Article',
229
		'headline' => $context['article']['title'],
230
		'author' => array(
231
			'@type' => 'Person',
232
			'name' => $context['article']['author']['name'],
233
		),
234
		'url' => $context['article']['href'],
235
		'commentCount' => $context['article']['comment_count'],
236
		'datePublished' => standardTime($context['article']['time']),
237
		'description' => $description,
238
		'wordCount' => str_word_count($post),
239
		'publisher' => array(
240
			'@type' => 'Organization',
241
			'name' => $context['forum_name'],
242
			'logo' => array(
243
				'@type' => 'ImageObject',
244
				'url' => $context['header_logo_url_html_safe'],
245
				'width' => 120,
246
				'height' => 60,
247
			),
248
		),
249
		'mainEntityOfPage' => array(
250
			'@type' => 'WebPage',
251
			'@id' => !empty($context['canonical_url']) ? $context['canonical_url'] : $boardurl,
252
		),
253
	);
254
255
	// If there are attachments, use the first as the image
256
	if (!empty($context['article']['attachment']))
257
	{
258
		foreach ($context['article']['attachment'] as $attachment)
259
		{
260
			$smd['image'] = array(
261
				'@type' => 'ImageObject',
262
				'url' => $attachment['href'],
263
				'width' => $attachment['real_width'],
264
				'height' => $attachment['real_height']
265
			);
266
267
			break;
268
		}
269
	}
270
271
	echo '
272
	<script type="application/ld+json">
273
    ', json_encode($smd, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), '
274
    </script>';
275
}
276
277
/**
278
 * Used to display attachments below an article body
279
 *
280
 * @param array $article
281
 * @param bool $ignoring
282
 */
283
function template_sp_display_attachments($article, $ignoring)
284
{
285
	global $context;
286
287
	echo '
288
							<div id="msg_', $article['id'], '_footer" class="attachments clear"', $ignoring ? ' style="display:none;"' : '', '>';
289
290
	foreach ($article['attachment'] as $attachment)
291
	{
292
		if (!empty($context['ila_dont_show_attach_below'])
293
			&& in_array($attachment['id'], $context['ila_dont_show_attach_below']))
294
		{
295
			continue;
296
		}
297
298
		echo '
299
								<figure class="attachment_block">';
300
301
		if ($attachment['is_image'])
302
		{
303
			if ($attachment['thumbnail']['has_thumb'])
304
				echo '
305
										<a href="', $attachment['href'], ';image" id="link_', $attachment['id'], '" ', $attachment['thumbnail']['lightbox'], '>
306
											<img class="attachment_image" src="', $attachment['thumbnail']['href'], '" alt="" id="thumb_', $attachment['id'], '" />
307
										</a>';
308
			else
309
				echo '
310
										<img class="attachment_image" src="', $attachment['href'], ';image" alt="" style="max-width:100%; max-height:' . $attachment['height'] . 'px;" />';
311
		}
312
313
		echo '
314
										<figcaption>
315
											<a href="', $attachment['href'], '" class="attachment_name">',
316
												$attachment['name'], '
317
											</a>
318
											<span class="attachment_details">', $attachment['size'], ($attachment['is_image'] ? ' / ' . $attachment['real_width'] . 'x' . $attachment['real_height'] : ''), '</span>
319
										</figcaption>
320
								</figure>';
321
	}
322
323
	echo '
324
							</div>';
325
}
326