Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

mod/blog/lib/blog.php (3 issues)

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
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
		$crumbs_title = $container->name;
44
		elgg_push_breadcrumb($crumbs_title);
45
46
		if ($current_user && ($container_guid == $current_user->guid)) {
47
			$return['filter_context'] = 'mine';
48
		} else if ($container instanceof ElggGroup) {
49
			$return['filter'] = false;
50
		} else {
51
			// do not show button or select a tab when viewing someone else's posts
52
			$return['filter_context'] = 'none';
53
		}
54
	} else {
55
		$options['preload_containers'] = true;
56
		$return['filter_context'] = 'all';
57
		$return['title'] = elgg_echo('blog:title:all_blogs');
58
		elgg_pop_breadcrumb();
59
		elgg_push_breadcrumb(elgg_echo('blog:blogs'));
60
	}
61
62
	elgg_register_title_button('blog', 'add', 'object', 'blog');
63
64
	$return['content'] = elgg_list_entities($options);
65
66
	return $return;
67
}
68
69
/**
70
 * Get page components to show blogs with publish dates between $lower and $upper
71
 *
72
 * @param int $owner_guid The GUID of the owner of this page
73
 * @param int $lower      Unix timestamp
74
 * @param int $upper      Unix timestamp
75
 * @return array
76
 */
77
function blog_get_page_content_archive($owner_guid, $lower = 0, $upper = 0) {
78
79
	$owner = get_entity($owner_guid);
80
	elgg_set_page_owner_guid($owner_guid);
81
82
	$crumbs_title = $owner->name;
83
	if ($owner instanceof ElggUser) {
84
		$url = elgg_generate_url('collection:object:blog:owner', [
85
			'username' => $owner->username,
86
		]);
87
	} else {
88
		$url = elgg_generate_url('collection:object:blog:group', [
89
			'group_guid' => $owner->guid,
90
			'subpage' => 'all',
91
		]);
92
	}
93
	elgg_push_breadcrumb($crumbs_title, $url);
94
	elgg_push_breadcrumb(elgg_echo('blog:archives'));
95
96
	if ($lower) {
97
		$lower = (int) $lower;
98
	}
99
100
	if ($upper) {
101
		$upper = (int) $upper;
102
	}
103
104
	$options = [
105
		'type' => 'object',
106
		'subtype' => 'blog',
107
		'full_view' => false,
108
		'no_results' => elgg_echo('blog:none'),
109
		'preload_owners' => true,
110
		'distinct' => false,
111
	];
112
113
	if ($owner instanceof ElggGroup) {
114
		$options['container_guid'] = $owner_guid;
115
	} elseif ($owner instanceof ElggUser) {
116
		$options['owner_guid'] = $owner_guid;
117
	}
118
119
	if ($lower) {
120
		$options['created_time_lower'] = $lower;
121
	}
122
123
	if ($upper) {
124
		$options['created_time_upper'] = $upper;
125
	}
126
127
	$content = elgg_list_entities($options);
128
129
	$title = elgg_echo('date:month:' . date('m', $lower), [date('Y', $lower)]);
130
131
	return [
132
		'content' => $content,
133
		'title' => $title,
134
		'filter' => '',
135
	];
136
}
137
138
/**
139
 * Get page components to edit/create a blog post.
140
 *
141
 * @param string $page     'edit' or 'new'
142
 * @param int    $guid     GUID of blog post or container
143
 * @param int    $revision Annotation id for revision to edit (optional)
144
 * @return array
145
 */
146
function blog_get_page_content_edit($page, $guid = 0, $revision = null) {
147
148
	elgg_require_js('elgg/blog/save_draft');
149
150
	$return = [
151
		'filter' => '',
152
	];
153
154
	$vars = [];
155
	$vars['id'] = 'blog-post-edit';
156
	$vars['class'] = 'elgg-form-alt';
157
158
	$sidebar = '';
159
	if ($page == 'edit') {
160
		$blog = get_entity((int) $guid);
161
162
		$title = elgg_echo('blog:edit');
163
164
		if ($blog instanceof ElggBlog && $blog->canEdit()) {
165
			$vars['entity'] = $blog;
166
167
			$title .= ": \"$blog->title\"";
168
169
			if ($revision) {
0 ignored issues
show
Bug Best Practice introduced by
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...
170
				$revision = elgg_get_annotation_from_id((int) $revision);
171
				$vars['revision'] = $revision;
172
				$title .= ' ' . elgg_echo('blog:edit_revision_notice');
173
174
				if (!$revision || !($revision->entity_guid == $guid)) {
175
					$content = elgg_echo('blog:error:revision_not_found');
176
					$return['content'] = $content;
177
					$return['title'] = $title;
178
					return $return;
179
				}
180
			}
181
182
			$body_vars = blog_prepare_form_vars($blog, $revision);
183
184
			elgg_push_breadcrumb($blog->title, $blog->getURL());
185
			elgg_push_breadcrumb(elgg_echo('edit'));
186
			
187
			elgg_require_js('elgg/blog/save_draft');
188
189
			$content = elgg_view_form('blog/save', $vars, $body_vars);
190
			$sidebar = elgg_view('blog/sidebar/revisions', $vars);
191
		} else {
192
			$content = elgg_echo('blog:error:cannot_edit_post');
193
		}
194
	} else {
195
		elgg_push_breadcrumb(elgg_echo('blog:add'));
196
		$body_vars = blog_prepare_form_vars(null);
197
198
		$title = elgg_echo('blog:add');
199
		$content = elgg_view_form('blog/save', $vars, $body_vars);
200
	}
201
202
	$return['title'] = $title;
203
	$return['content'] = $content;
204
	$return['sidebar'] = $sidebar;
205
	return $return;
206
}
207
208
/**
209
 * Pull together blog variables for the save form
210
 *
211
 * @param ElggBlog       $post     blog post being edited
212
 * @param ElggAnnotation $revision a revision from which to edit
213
 * @return array
214
 */
215
function blog_prepare_form_vars($post = null, $revision = null) {
216
217
	// input names => defaults
218
	$values = [
219
		'title' => null,
220
		'description' => null,
221
		'status' => 'published',
222
		'access_id' => ACCESS_DEFAULT,
223
		'comments_on' => 'On',
224
		'excerpt' => null,
225
		'tags' => null,
226
		'container_guid' => null,
227
		'guid' => null,
228
		'entity' => $post,
229
		'draft_warning' => '',
230
	];
231
232
	if ($post) {
233
		foreach (array_keys($values) as $field) {
234
			if (isset($post->$field)) {
235
				$values[$field] = $post->$field;
236
			}
237
		}
238
239
		if ($post->status == 'draft') {
240
			$values['access_id'] = $post->future_access;
241
		}
242
	}
243
244
	if (elgg_is_sticky_form('blog')) {
245
		$sticky_values = elgg_get_sticky_values('blog');
246
		foreach ($sticky_values as $key => $value) {
247
			$values[$key] = $value;
248
		}
249
	}
250
	
251
	elgg_clear_sticky_form('blog');
252
253
	if (!$post) {
254
		return $values;
255
	}
256
257
	// load the revision annotation if requested
258
	if ($revision instanceof ElggAnnotation && $revision->entity_guid == $post->getGUID()) {
259
		$values['revision'] = $revision;
260
		$values['description'] = $revision->value;
261
	}
262
263
	// display a notice if there's an autosaved annotation
264
	// and we're not editing it.
265
	$auto_save_annotations = $post->getAnnotations([
266
		'annotation_name' => 'blog_auto_save',
267
		'limit' => 1,
268
	]);
269
	if ($auto_save_annotations) {
0 ignored issues
show
Bug Best Practice introduced by
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...
270
		$auto_save = $auto_save_annotations[0];
271
	} else {
272
		$auto_save = false;
273
	}
274
	/* @var ElggAnnotation|false $auto_save */
275
276
	if ($auto_save && $revision && $auto_save->id != $revision->id) {
277
		$values['draft_warning'] = elgg_echo('blog:messages:warning:draft');
278
	}
279
280
	return $values;
281
}
282