Passed
Push — master ( ba823b...756562 )
by Jeroen
06:16
created

blog_get_page_content_edit()   C

Complexity

Conditions 9
Paths 6

Size

Total Lines 66
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
cc 9
eloc 43
nc 6
nop 3
dl 0
loc 66
ccs 0
cts 42
cp 0
crap 90
rs 6.4099
c 0
b 0
f 0

How to fix   Long Method   

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
 * Blog helper functions
4
 *
5
 * @package Blog
6
 */
7
8
/**
9
 * Get page components to list a user's or all blogs.
10
 *
11
 * @param int $container_guid The GUID of the page owner or NULL for all blogs
12
 * @return array
13
 */
14
function blog_get_page_content_list($container_guid = null) {
15
16
	$return = [];
17
18
	$return['filter_context'] = $container_guid ? 'mine' : 'all';
19
20
	$options = [
21
		'type' => 'object',
22
		'subtype' => 'blog',
23
		'full_view' => false,
24
		'no_results' => elgg_echo('blog:none'),
25
		'preload_owners' => true,
26
		'distinct' => false,
27
	];
28
29
	$current_user = elgg_get_logged_in_user_entity();
30
31
	if ($container_guid) {
0 ignored issues
show
Bug Best Practice introduced by Cash Costello
The expression $container_guid of type null|integer is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
32
		// access check for closed groups
33
		elgg_group_gatekeeper();
34
35
		$container = get_entity($container_guid);
36
		if ($container instanceof ElggGroup) {
37
			$options['container_guid'] = $container_guid;
38
		} else {
39
			$options['owner_guid'] = $container_guid;
40
		}
41
		$return['title'] = elgg_echo('blog:title:user_blogs', [$container->name]);
42
43
		elgg_push_collection_breadcrumbs('object', 'blog', $container);
44
45
		if ($current_user && ($container_guid == $current_user->guid)) {
46
			$return['filter_context'] = 'mine';
47
		} else if ($container instanceof ElggGroup) {
48
			$return['filter'] = false;
49
		} else {
50
			// do not show button or select a tab when viewing someone else's posts
51
			$return['filter_context'] = 'none';
52
		}
53
	} else {
54
		$options['preload_containers'] = true;
55
		$return['filter_context'] = 'all';
56
		$return['title'] = elgg_echo('blog:title:all_blogs');
57
		elgg_push_collection_breadcrumbs('object', 'blog');
58
	}
59
60
	elgg_register_title_button('blog', 'add', 'object', 'blog');
61
62
	$return['content'] = elgg_list_entities($options);
63
64
	return $return;
65
}
66
67
/**
68
 * Get page components to show blogs with publish dates between $lower and $upper
69
 *
70
 * @param int $owner_guid The GUID of the owner of this page
71
 * @param int $lower      Unix timestamp
72
 * @param int $upper      Unix timestamp
73
 * @return array
74
 */
75
function blog_get_page_content_archive($owner_guid, $lower = 0, $upper = 0) {
76
77
	$owner = get_entity($owner_guid);
78
	elgg_set_page_owner_guid($owner_guid);
79
80
	elgg_push_collection_breadcrumbs('object', 'blog', $owner);
81
82
	elgg_push_breadcrumb(elgg_echo('blog:archives'));
83
84
	if ($lower) {
85
		$lower = (int) $lower;
86
	}
87
88
	if ($upper) {
89
		$upper = (int) $upper;
90
	}
91
92
	$options = [
93
		'type' => 'object',
94
		'subtype' => 'blog',
95
		'full_view' => false,
96
		'no_results' => elgg_echo('blog:none'),
97
		'preload_owners' => true,
98
		'distinct' => false,
99
	];
100
101
	if ($owner instanceof ElggGroup) {
102
		$options['container_guid'] = $owner_guid;
103
	} elseif ($owner instanceof ElggUser) {
104
		$options['owner_guid'] = $owner_guid;
105
	}
106
107
	if ($lower) {
108
		$options['created_time_lower'] = $lower;
109
	}
110
111
	if ($upper) {
112
		$options['created_time_upper'] = $upper;
113
	}
114
115
	$content = elgg_list_entities($options);
116
117
	$title = elgg_echo('date:month:' . date('m', $lower), [date('Y', $lower)]);
118
119
	return [
120
		'content' => $content,
121
		'title' => $title,
122
		'filter' => '',
123
	];
124
}
125
126
/**
127
 * Get page components to edit/create a blog post.
128
 *
129
 * @param string $page     'edit' or 'new'
130
 * @param int    $guid     GUID of blog post or container
131
 * @param int    $revision Annotation id for revision to edit (optional)
132
 * @return array
133
 */
134
function blog_get_page_content_edit($page, $guid = 0, $revision = null) {
135
136
	elgg_require_js('elgg/blog/save_draft');
137
138
	$return = [
139
		'filter' => '',
140
	];
141
142
	$vars = [];
143
	$vars['id'] = 'blog-post-edit';
144
	$vars['class'] = 'elgg-form-alt';
145
146
	$sidebar = '';
147
	if ($page == 'edit') {
148
		$blog = get_entity((int) $guid);
149
150
		$title = elgg_echo('blog:edit');
151
152
		if ($blog instanceof ElggBlog && $blog->canEdit()) {
153
			$vars['entity'] = $blog;
154
155
			$title .= ": \"$blog->title\"";
156
157
			if ($revision) {
0 ignored issues
show
Bug Best Practice introduced by Cash Costello
The expression $revision of type null|integer is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
158
				$revision = elgg_get_annotation_from_id((int) $revision);
159
				$vars['revision'] = $revision;
160
				$title .= ' ' . elgg_echo('blog:edit_revision_notice');
161
162
				if (!$revision || !($revision->entity_guid == $guid)) {
0 ignored issues
show
introduced by Cash Costello
The condition ! $revision || ! $revision->entity_guid == $guid can never be false.
Loading history...
163
					$content = elgg_echo('blog:error:revision_not_found');
164
					$return['content'] = $content;
165
					$return['title'] = $title;
166
					return $return;
167
				}
168
			}
169
170
			$body_vars = blog_prepare_form_vars($blog, $revision);
0 ignored issues
show
Bug introduced by Cash Costello
It seems like $revision can also be of type integer; however, parameter $revision of blog_prepare_form_vars() does only seem to accept ElggAnnotation, 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

170
			$body_vars = blog_prepare_form_vars($blog, /** @scrutinizer ignore-type */ $revision);
Loading history...
171
172
			elgg_push_entity_breadcrumbs($blog);
173
			elgg_push_breadcrumb(elgg_echo('edit'));
174
			
175
			elgg_require_js('elgg/blog/save_draft');
176
177
			$content = elgg_view_form('blog/save', $vars, $body_vars);
178
			$sidebar = elgg_view('blog/sidebar/revisions', $vars);
179
		} else {
180
			$content = elgg_echo('blog:error:cannot_edit_post');
181
		}
182
	} else {
183
		if (!$guid) {
184
			$guid = elgg_get_logged_in_user_guid();
185
		}
186
187
		$container = get_entity($guid) ? : null;
188
		elgg_push_collection_breadcrumbs('object', 'blog', $container);
189
		elgg_push_breadcrumb(elgg_echo('blog:add'));
190
		$body_vars = blog_prepare_form_vars(null);
191
192
		$title = elgg_echo('blog:add');
193
		$content = elgg_view_form('blog/save', $vars, $body_vars);
194
	}
195
196
	$return['title'] = $title;
197
	$return['content'] = $content;
198
	$return['sidebar'] = $sidebar;
199
	return $return;
200
}
201
202
/**
203
 * Pull together blog variables for the save form
204
 *
205
 * @param ElggBlog       $post     blog post being edited
206
 * @param ElggAnnotation $revision a revision from which to edit
207
 * @return array
208
 */
209
function blog_prepare_form_vars($post = null, $revision = null) {
210
211
	// input names => defaults
212
	$values = [
213
		'title' => null,
214
		'description' => null,
215
		'status' => 'published',
216
		'access_id' => ACCESS_DEFAULT,
217
		'comments_on' => 'On',
218
		'excerpt' => null,
219
		'tags' => null,
220
		'container_guid' => null,
221
		'guid' => null,
222
		'entity' => $post,
223
		'draft_warning' => '',
224
	];
225
226
	if ($post) {
227
		foreach (array_keys($values) as $field) {
228
			if (isset($post->$field)) {
229
				$values[$field] = $post->$field;
230
			}
231
		}
232
233
		if ($post->status == 'draft') {
234
			$values['access_id'] = $post->future_access;
235
		}
236
	}
237
238
	if (elgg_is_sticky_form('blog')) {
239
		$sticky_values = elgg_get_sticky_values('blog');
240
		foreach ($sticky_values as $key => $value) {
241
			$values[$key] = $value;
242
		}
243
	}
244
	
245
	elgg_clear_sticky_form('blog');
246
247
	if (!$post) {
248
		return $values;
249
	}
250
251
	// load the revision annotation if requested
252
	if ($revision instanceof ElggAnnotation && $revision->entity_guid == $post->getGUID()) {
253
		$values['revision'] = $revision;
254
		$values['description'] = $revision->value;
255
	}
256
257
	// display a notice if there's an autosaved annotation
258
	// and we're not editing it.
259
	$auto_save_annotations = $post->getAnnotations([
260
		'annotation_name' => 'blog_auto_save',
261
		'limit' => 1,
262
	]);
263
	if ($auto_save_annotations) {
0 ignored issues
show
Bug Best Practice introduced by Cash Costello
The expression $auto_save_annotations of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
introduced by Cash Costello
The condition $auto_save_annotations can never be true.
Loading history...
264
		$auto_save = $auto_save_annotations[0];
265
	} else {
266
		$auto_save = false;
267
	}
268
	/* @var ElggAnnotation|false $auto_save */
269
270
	if ($auto_save && $revision && $auto_save->id != $revision->id) {
0 ignored issues
show
introduced by Steve Clay
The condition $auto_save && $revision ...ve->id != $revision->id can never be true.
Loading history...
271
		$values['draft_warning'] = elgg_echo('blog:messages:warning:draft');
272
	}
273
274
	return $values;
275
}
276