Completed
Pull Request — master (#10)
by mw
02:46
created

EchoNotificationsManager::addDefaultOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace SMW\Notifications;
4
5
use EchoEvent;
6
use EchoAttributeManager;
7
use SMW\Notifications\ValueChange\ChangeNotifications;
8
use SMW\Notifications\ValueChange\ChangeNotificationFormatter;
9
use SMW\Notifications\ValueChange\ChangeNotificationPresentationModel;
10
11
/**
12
 * @license GNU GPL v2+
13
 * @since 1.0
14
 *
15
 * @author mwjames
16
 */
17
class EchoNotificationsManager {
18
19
	/**
20
	 * @see Echo::BeforeCreateEchoEvent
21
	 *
22
	 * @since 1.0
23
	 *
24
	 * @param array &$notifications
25
	 * @param array &$notificationCategories
26
	 * @param array &$icons
27
	 */
28 2
	public function addNotificationsDefinitions( array &$notifications, array &$notificationCategories, array &$icons ) {
29
30 1
		$valueChangeNotificationType = ChangeNotifications::VALUE_CHANGE;
31 1
		$specificationChangeNotificationType = ChangeNotifications::SPECIFICATION_CHANGE;
32
33 1
		$notificationCategories[$valueChangeNotificationType] = array(
34 1
			'priority' => 5,
35 1
			'tooltip' => 'echo-pref-tooltip-smw-value-change',
36
		);
37
38 1
		$notificationCategories[$specificationChangeNotificationType] = array(
39 1
			'priority' => 5,
40 1
			'tooltip' => 'echo-pref-tooltip-smw-specification-change',
41
		);
42
43 1
		$notifications[$valueChangeNotificationType] = array(
44 1
			EchoAttributeManager::ATTR_LOCATORS => [
45
				'\SMW\Notifications\ValueChange\UserLocator::doLocateEventSubscribers'
46 1
			],
47 1
			'category' => 'smw-value-change',
48 1
			'group' => 'neutral',
49 1
			'section' => 'alert',
50 1
			'presentation-model' => ChangeNotificationPresentationModel::class,
51 1
			'formatter-class' => ChangeNotificationFormatter::class,
52 1
			'title-message' => "notification-{$valueChangeNotificationType}",
53
			'title-params' => array(
54 1
				'agent', 'difflink', 'title'
55 1
			),
56 1
			'flyout-message' => "notification-{$valueChangeNotificationType}-flyout",
57
			'flyout-params' => array(
58 1
				'agent', 'difflink', 'title'
59 1
			),
60 1
			'payload' => array( 'extra' ),
61
			'bundle' => array(
62 1
				'web' => true,
63
				'email' => false
64 1
			),
65 1
			'email-subject-message' => "notification-{$valueChangeNotificationType}-email-subject",
66
			'email-subject-params' => array(
67 1
				'user',
68 1
				'agent',
69
				'page-count'
70 1
			),
71 1
			'email-body-batch-message' => "notification-{$valueChangeNotificationType}-email-batch-body",
72
			'email-body-batch-params' => array(
73 1
				'title',
74 1
				'agent',
75
				'item'
76 1
			),
77 1
			'icon' => $valueChangeNotificationType,
78
		);
79
80 1
		$notifications[$specificationChangeNotificationType] = array(
81 1
			EchoAttributeManager::ATTR_LOCATORS => [
82
				'\SMW\Notifications\ValueChange\UserLocator::doLocateEventSubscribers'
83 1
			],
84 1
			'category' => 'smw-specification-change',
85 1
			'group' => 'neutral',
86 1
			'section' => 'alert',
87 1
			'presentation-model' => ChangeNotificationPresentationModel::class,
88 1
			'formatter-class' => ChangeNotificationFormatter::class,
89 1
			'title-message' => "notification-{$valueChangeNotificationType}",
90
			'title-params' => array(
91 2
				'agent', 'difflink', 'title'
92 1
			),
93 1
			'flyout-message' => "notification-{$valueChangeNotificationType}-flyout",
94
			'flyout-params' => array(
95 1
				'agent', 'difflink', 'title'
96 1
			),
97 1
			'payload' => array( 'extra' ),
98
			'bundle' => array(
99 1
				'web' => true,
100
				'email' => false
101 1
			),
102 1
			'email-subject-message' => "notification-{$valueChangeNotificationType}-email-subject",
103
			'email-subject-params' => array(
104 1
				'user',
105 1
				'agent',
106
				'page-count'
107 1
			),
108 1
			'email-body-batch-message' => "notification-{$valueChangeNotificationType}-email-batch-body",
109
			'email-body-batch-params' => array(
110 1
				'title',
111 1
				'agent',
112
				'item'
113 1
			)
114 1
		);
115
116 1
		$icons[$valueChangeNotificationType] = array(
117
			'path' => "SemanticNotifications/res/smw-entity-change-yellow.png"
118 1
		);
119
120
		// Resolved in ChangeNotificationPresentationModel::getIconType
121 1
		$icons[$specificationChangeNotificationType . '-property'] = array(
122
			'path' => "SemanticNotifications/res/smw-entity-change-blue.png"
123 1
		);
124
125 1
		$icons[$specificationChangeNotificationType . '-category'] = array(
126
			'path' => "SemanticNotifications/res/smw-entity-change-green.png"
127 1
		);
128 1
	}
129
130
	/**
131
	 * @see https://www.mediawiki.org/wiki/Notifications/Developer_guide#Conventions
132
	 *
133
	 * @since 1.0
134
	 *
135
	 * @param array &$defaultOptions
136
	 *
137
	 */
138 1
	public function addDefaultOptions( array &$defaultOptions ) {
139 1
		$defaultOptions['echo-subscriptions-web-' . ChangeNotifications::VALUE_CHANGE] = true;
140 1
		return true;
141
	}
142
143
	/**
144
	 * @see https://www.mediawiki.org/wiki/Notifications/Developer_guide#Bundled_notification
145
	 *
146
	 * @since 1.0
147
	 *
148
	 * @param EchoEvent $event
149
	 * @param string &$bundleString
150
	 */
151 1
	public function getNotificationsBundle( EchoEvent $event, &$bundleString ) {
152
153 1
		$extra = $event->getExtra();
154
155 1
		if (
156 1
			$event->getType() === ChangeNotifications::VALUE_CHANGE ||
157 1
			$event->getType() === ChangeNotifications::SPECIFICATION_CHANGE ) {
158 1
			$bundleString = $extra['subject']->getHash() . $extra['revid'];
159 1
		}
160 1
	}
161
162
	/**
163
	 * @since 1.0
164
	 *
165
	 * @param array $event
166
	 *
167
	 * @return EchoEvent
168
	 */
169 1
	public function createEvent( array $event ) {
170
171 1
		if ( $event === array() ) {
172
			return;
173
		}
174
175 1
		wfDebugLog( 'smw', 'EchoEvent triggered' );
176
177 1
		return EchoEvent::create( $event );
178
	}
179
180
	/**
181
	 * @see https://www.mediawiki.org/wiki/Notifications/Developer_guide#Create_the_notification_via_the_Echo_extension
182
	 *
183
	 * @since 1.0
184
	 *
185
	 * @param EchoEvent $event
186
	 * @param array &$users
187
	 */
188 1
	public function getDefaultRecipientsByType( EchoEvent $event, &$users ) {
189
190 1
		$extra = $event->getExtra();
191
192 1
		if ( $event->getType() === ChangeNotifications::VALUE_CHANGE ) {
193 1
			if ( isset( $extra['recipients'] ) ) {
194
				foreach ( $extra['recipients'] as $key => $recipient ) {
195
					$users[] = \User::newFromName( $recipient, false );
196
				}
197
			}
198 1
		}
199 1
	}
200
201
}
202