forked from Gizra/message_notify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_notify.module
194 lines (173 loc) · 5.5 KB
/
message_notify.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/**
* @file
* Message notify.
*/
/**
* Process and send a message.
*
* @param $message
* The message entity being used for the notification.
* @param $options
* Array of options to override the plugin's default ones.
* @param $notifier_name
* Optional; The name of the notifier to use. Defaults to "email"
* sending method.
*
* @return
* Boolean value denoting success or failure of the notification.
*/
function message_notify_send_message(Message $message, array $options = array(), $notifier_name = 'email') {
if (!$plugin = message_notify_get_notifier($notifier_name)) {
throw new MessageNotifyException(format_string('Could not send notification using the "@notifier" notifier.', array('@notifier' => $notifier_name)));
}
$plugin['options'] = drupal_array_merge_deep($plugin['options'], $options);
$class = ctools_plugin_load_class('message_notify', 'notifier', $notifier_name, 'class');
$notifier = new $class($plugin, $message);
if ($notifier->access()) {
return $notifier->send();
}
}
/**
* Add defaults values to the notifier plugins.
*
* - 'description': The description of the plugin.
* - 'options': Array with the following keys:
* - 'save on fail': Save the message is delivery failed. Defaults to
* TRUE.
* - 'save on success': Save the message is delivery successded. Defaults to
* TRUE.
* - 'language override': Override user's language, and use the
* Message's language. Defaults to FALSE.
* - 'rendered fields': Array keyed with the view mode(s), and the text
* field(s) to save the rendered output.
* The rendered field is used when it is needed to save the message
* exactly as it was sent (for example for message logs), as it might
* change and show differently when viewed later - as a result of on
* the fly token replacement, or even editing the message type.
*/
function message_notify_plugin_process(&$plugin, $info) {
$plugin += array(
'description' => '',
'options' => array(),
);
$plugin['options'] += array(
'save on fail' => TRUE,
'save on success' => TRUE,
'language override' => FALSE,
'rendered fields' => array(),
);
}
/**
* Implements hook_entity_info_alter().
*
* Add a the notifiers view modes.
*/
function message_notify_entity_info_alter(&$entity_info) {
foreach (message_notify_get_notifiers() as $plugin) {
$view_modes = $plugin['view_modes'];
// Set default values.
foreach ($view_modes as &$view_mode) {
$view_mode += array('custom settings' => TRUE);
}
$entity_info['message']['view modes'] += $view_modes;
}
}
/**
* Implements hook_mail().
*
* Set's the message subject and body as configured.
*/
function message_notify_mail($key, &$message, $params) {
$message['subject'] = $params['message_notify_email_subject'];
$message['body'][] = $params['message_notify_email_body'];
}
/**
* Implements hook_field_attach_create_bundle().
*
* We cannot easily set the the visibilty of extra fields, so we set the
* bundle settings upon creation of new message bundle.
*/
function message_notify_field_attach_create_bundle($entity_type, $bundle) {
if ($entity_type != 'message') {
return;
}
$bundle_settings = field_bundle_settings('message', $bundle);
$display = &$bundle_settings['extra_fields']['display'];
$visible = array('visible' => TRUE, 'weight' => 0);
$hidden = array('visible' => FALSE, 'weight' => 0);
$display['message__message_text__0'] = array(
'message_notify_email_subject' => $visible,
'message_notify_email_body' => $hidden,
);
$display['message__message_text__1'] = array(
'message_notify_email_subject' => $hidden,
'message_notify_email_body' => $visible,
);
field_bundle_settings('message', $bundle, $bundle_settings);
}
/**
* Implements hook_ctools_plugin_api().
*/
function message_notify_ctools_plugin_api($module, $api) {
if ($module == 'message_notify' && $api == 'notifier') {
return array('version' => 1);
}
}
/**
* Implements hook_ctools_plugin_type().
*/
function message_notify_ctools_plugin_type() {
$plugins['notifier'] = array(
'classes' => array('class'),
'process' => 'message_notify_plugin_process',
);
return $plugins;
}
/**
* Implements hook_ctools_plugin_directory().
*/
function message_notify_ctools_plugin_directory($module, $plugin) {
if ($module == 'message_notify') {
return 'plugins/' . $plugin;
}
}
/**
* Helper function to include CTools plugins and get a notifier plguin.
*
* @param $plugin_name
* The plugin that should be laoded.
*/
function message_notify_get_notifier($notifier_name) {
ctools_include('plugins');
return ctools_get_plugins('message_notify', 'notifier', $notifier_name);
}
/**
* Helper function to include CTools plugins and get all notifier plugins.
*/
function message_notify_get_notifiers() {
ctools_include('plugins');
return ctools_get_plugins('message_notify', 'notifier');
}
/**
* Helper function to return all notifiers as options for a select list.
*/
function message_notify_get_notifiers_as_options() {
$notifiers = message_notify_get_notifiers();
$options = array();
foreach ($notifiers as $notifier_name => $notifier) {
$options[$notifier_name] = check_plain($notifier['title']);
}
return $options;
}
/**
* Deprectaed: Send email.
*
* Wrapper function for message_notify_send_message(), using the "email"
* notifier.
*
* @deprecated
*/
function message_notify_send_mail(Message $message, array $options = array()) {
message_notify_send_message($message, $options, 'email');
}