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

blog_prepare_notification()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 4
dl 0
loc 18
ccs 0
cts 15
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Blogs
4
 *
5
 * @package Blog
6
 */
7
8
/**
9
 * Init blog plugin
10
 *
11
 * @return void
12
 */
13
function blog_init() {
14
15 31
	elgg_register_library('elgg:blog', __DIR__ . '/lib/blog.php');
16
17
	// add a site navigation item
18 31
	elgg_register_menu_item('site', [
19 31
		'name' => 'blog',
20 31
		'text' => elgg_echo('blog:blogs'),
21 31
		'href' => elgg_generate_url('collection:object:blog:all'),
22
	]);
23
24 31
	elgg_extend_view('object/elements/imprint/contents', 'blog/imprint/status');
25
26
	// override the default url to view a blog object
27 31
	elgg_register_plugin_hook_handler('entity:url', 'object', 'blog_set_url');
28
29
	// notifications
30 31
	elgg_register_notification_event('object', 'blog', ['publish']);
31 31
	elgg_register_plugin_hook_handler('prepare', 'notification:publish:object:blog', 'blog_prepare_notification');
32
33
	// add blog link to owner block
34 31
	elgg_register_plugin_hook_handler('register', 'menu:owner_block', 'blog_owner_block_menu');
35
36
	// Add group option
37 31
	add_group_tool_option('blog', elgg_echo('blog:enableblog'), true);
38 31
	elgg_extend_view('groups/tool_latest', 'blog/group_module');
39
40
	// archive menu
41 31
	elgg_register_plugin_hook_handler('register', 'menu:blog_archive', 'blog_archive_menu_setup');
42
43
	// ecml
44 31
	elgg_register_plugin_hook_handler('get_views', 'ecml', 'blog_ecml_views_hook');
45
46
	// allow to be liked
47 31
	elgg_register_plugin_hook_handler('likes:is_likable', 'object:blog', 'Elgg\Values::getTrue');
48
49
	// register database seed
50 31
	elgg_register_plugin_hook_handler('seeds', 'database', 'blog_register_db_seeds');
51 31
}
52
53
/**
54
 * Format and return the URL for blogs.
55
 *
56
 * @param string $hook   'entity:url'
57
 * @param string $type   'object'
58
 * @param string $url    current value
59
 * @param array  $params supplied params
60
 *
61
 * @return string URL of blog
62
 */
63
function blog_set_url($hook, $type, $url, $params) {
64 3
	$entity = elgg_extract('entity', $params);
65 3
	if (!$entity instanceof ElggBlog) {
66 3
		return;
67
	}
68
	
69
	return elgg_generate_url('view:object:blog', [
70
		'guid' => $entity->guid,
71
		'title' => elgg_get_friendly_title($entity->title),
72
	]);
73
}
74
75
/**
76
 * Add a menu item to an ownerblock
77
 *
78
 * @param string         $hook   'register'
79
 * @param string         $type   'menu:owner_block'
80
 * @param ElggMenuItem[] $return current return value
81
 * @param array          $params supplied params
82
 *
83
 * @return ElggMenuItem[]
84
 */
85
function blog_owner_block_menu($hook, $type, $return, $params) {
86
	$entity = elgg_extract('entity', $params);
87
	if ($entity instanceof ElggUser) {
88
		$return[] = ElggMenuItem::factory([
89
					'name' => 'blog',
90
					'text' => elgg_echo('blog'),
91
					'href' => elgg_generate_url('collection:object:blog:owner', [
92
						'username' => $entity->username,
93
					]),
94
		]);
95
	} elseif ($entity instanceof ElggGroup) {
96
		if ($entity->isToolEnabled('blog')) {
97
			$return[] = ElggMenuItem::factory([
98
						'name' => 'blog',
99
						'text' => elgg_echo('blog:group'),
100
						'href' => elgg_generate_url('collection:object:blog:group', [
101
							'group_guid' => $entity->guid,
102
							'subpage' => 'all',
103
						]),
104
			]);
105
		}
106
	}
107
108
	return $return;
109
}
110
111
/**
112
 * Add menu items to the archive menu
113
 *
114
 * @param string         $hook   'register'
115
 * @param string         $type   'menu:blog_archive'
116
 * @param ElggMenuItem[] $return current return value
117
 * @param array          $params supplied params
118
 *
119
 * @return void|ElggMenuItem[]
120
 */
121
function blog_archive_menu_setup($hook, $type, $return, $params) {
122
123
	$page_owner = elgg_get_page_owner_entity();
124
	if (empty($page_owner)) {
125
		return;
126
	}
127
128
	$dates = get_entity_dates('object', 'blog', $page_owner->getGUID());
129
	if (!$dates) {
130
		return;
131
	}
132
133
	$dates = array_reverse($dates);
134
	
135
	$generate_url = function($lower = null, $upper = null) use ($page_owner) {
136
		if ($page_owner instanceof ElggUser) {
137
			$url_segment = elgg_generate_url('collection:object:blog:archive', [
138
				'username' => $page_owner->username,
139
				'lower' => $lower,
140
				'upper' => $upper,
141
			]);
142
		} else {
143
			$url_segment = elgg_generate_url('collection:object:blog:group', [
144
				'group_guid' => $page_owner->guid,
145
				'subpage' => 'archive',
146
				'lower' => $lower,
147
				'upper' => $upper,
148
			]);
149
		}
150
151
		return $url_segment;
152
	};
153
	
154
	$years = [];
155
	foreach ($dates as $date) {
156
		$timestamplow = mktime(0, 0, 0, substr($date, 4, 2), 1, substr($date, 0, 4));
157
		$timestamphigh = mktime(0, 0, 0, ((int) substr($date, 4, 2)) + 1, 1, substr($date, 0, 4));
158
159
		$year = substr($date, 0, 4);
160
		if (!in_array($year, $years)) {
161
			$return[] = ElggMenuItem::factory([
162
				'name' => $year,
163
				'text' => $year,
164
				'href' => '#',
165
				'child_menu' => [
166
					'display' => 'toggle',
167
				]
168
			]);
169
		}
170
171
		$month = trim(elgg_echo('date:month:' . substr($date, 4, 2), ['']));
172
173
		$return[] = ElggMenuItem::factory([
174
			'name' => $date,
175
			'text' => $month,
176
			'href' => $generate_url($timestamplow, $timestamphigh),
177
			'parent_name' => $year,
178
		]);
179
	}
180
181
	return $return;
182
}
183
184
/**
185
 * Prepare a notification message about a published blog
186
 *
187
 * @param string                          $hook         Hook name
188
 * @param string                          $type         Hook type
189
 * @param Elgg\Notifications\Notification $notification The notification to prepare
190
 * @param array                           $params       Hook parameters
191
 * @return Elgg\Notifications\Notification
192
 */
193
function blog_prepare_notification($hook, $type, $notification, $params) {
194
	$entity = $params['event']->getObject();
195
	$owner = $params['event']->getActor();
196
	$recipient = $params['recipient'];
0 ignored issues
show
Unused Code introduced by Cash Costello
The assignment to $recipient is dead and can be removed.
Loading history...
197
	$language = $params['language'];
198
	$method = $params['method'];
0 ignored issues
show
Unused Code introduced by Cash Costello
The assignment to $method is dead and can be removed.
Loading history...
199
200
	$notification->subject = elgg_echo('blog:notify:subject', [$entity->title], $language);
201
	$notification->body = elgg_echo('blog:notify:body', [
202
		$owner->name,
203
		$entity->title,
204
		$entity->getExcerpt(),
205
		$entity->getURL()
206
	], $language);
207
	$notification->summary = elgg_echo('blog:notify:summary', [$entity->title], $language);
208
	$notification->url = $entity->getURL();
209
210
	return $notification;
211
}
212
213
/**
214
 * Register blogs with ECML
215
 *
216
 * @param string $hook         'get_views'
217
 * @param string $type         'ecml'
218
 * @param array  $return_value current return value
219
 * @param array  $params       supplied params
220
 *
221
 * @return array
222
 */
223
function blog_ecml_views_hook($hook, $type, $return_value, $params) {
224
	$return_value['object/blog'] = elgg_echo('blog:blogs');
225
226
	return $return_value;
227
}
228
229
/**
230
 * Register database seed
231
 *
232
 * @elgg_plugin_hook seeds database
233
 *
234
 * @param \Elgg\Hook $hook Hook
235
 * @return array
236
 */
237
function blog_register_db_seeds(\Elgg\Hook $hook) {
238
239
	$seeds = $hook->getValue();
240
241
	$seeds[] = \Elgg\Blog\Seeder::class;
242
243
	return $seeds;
244
}
245
246
return function() {
247 18
	elgg_register_event_handler('init', 'system', 'blog_init');
248
};
249