forked from 3sd/io.3sd.chainedsms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchainsms.php
More file actions
280 lines (246 loc) · 9.18 KB
/
Copy pathchainsms.php
File metadata and controls
280 lines (246 loc) · 9.18 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
require_once 'chainsms.civix.php';
const SURVEY_SUBMENU_LABEL = 'SMS Survey';
/**
* Implementation of hook_civicrm_config
*/
function chainsms_civicrm_config(&$config) {
_chainsms_civix_civicrm_config($config);
}
/**
* Implementation of hook_civicrm_xmlMenu
*
* @param $files array(string)
*/
function chainsms_civicrm_xmlMenu(&$files) {
_chainsms_civix_civicrm_xmlMenu($files);
}
/**
* Implementation of hook_civicrm_install
*/
function chainsms_civicrm_install() {
// Insert blank arrays for users to fill in their own swear words.
CRM_Core_BAO_Setting::setItem(array(), 'org.thirdsectordesign.chainsms', 'safe_list');
CRM_Core_BAO_Setting::setItem(array(), 'org.thirdsectordesign.chainsms', 'swear_list');
// TODO create permission to view swear filter page.
// Create SMS Conversation Activity Type
$smsConversationActivityType = CRM_Core_OptionGroup::getValue('activity_type', 'SMS Conversation', 'name');
if (empty($smsConversationActivityType)) {
$createSMSConversationActivityApiParams = array(
'version' => 3,
'sequential' => 1,
'label' => 'SMS Conversation',
'description' => 'Translated SMS Surveys. Automatically added by the SMS Survey Extension.',
'weight' => 0,
'name' => 'SMSConversation',
'reserved' => 1,
);
civicrm_api('ActivityType', 'create', $createSMSConversationActivityApiParams);
}
// Create SMS Survey next message table map
CRM_Core_DAO::executeQuery('CREATE TABLE IF NOT EXISTS civicrm_chainsms_answer (
id int(11) NOT NULL AUTO_INCREMENT,
msg_template_id int(11) DEFAULT NULL,
answer varchar(255) DEFAULT NULL,
next_msg_template_id int(11) DEFAULT NULL,
PRIMARY KEY (id),
KEY index_question_id (msg_template_id),
KEY index_next_question_id (next_msg_template_id)
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=latin1');
return _chainsms_civix_civicrm_install();
}
/**
* Implementation of hook_civicrm_uninstall
*/
function chainsms_civicrm_uninstall() {
// Wipe the swear filters.
$dao = CRM_Core_DAO::executeQuery("DELETE FROM civicrm_setting WHERE group_name = 'org.thirdsectordesign.chainsms'");
// Remove the added table
$dao = CRM_Core_DAO::executeQuery("DROP TABLE IF EXISTS civicrm_chainsms_answer");
return _chainsms_civix_civicrm_uninstall();
}
/**
* Implementation of hook_civicrm_enable
*/
function chainsms_civicrm_enable() {
return _chainsms_civix_civicrm_enable();
}
/**
* Implementation of hook_civicrm_disable
*/
function chainsms_civicrm_disable() {
return _chainsms_civix_civicrm_disable();
}
/**
* Implementation of hook_civicrm_upgrade
*
* @param $op string, the type of operation being performed; 'check' or 'enqueue'
* @param $queue CRM_Queue_Queue, (for 'enqueue') the modifiable list of pending up upgrade tasks
*
* @return mixed based on op. for 'check', returns array(boolean) (TRUE if upgrades are pending)
* for 'enqueue', returns void
*/
function chainsms_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
return _chainsms_civix_civicrm_upgrade($op, $queue);
}
/**
* Implementation of hook_civicrm_managed
*
* Generate a list of entities to create/deactivate/delete when this module
* is installed, disabled, uninstalled.
*/
function chainsms_civicrm_managed(&$entities) {
return _chainsms_civix_civicrm_managed($entities);
}
/**
* Respond to inbound SMS, if applicable, through the chain system.
* Part of a hook_civicrm_post implementation.
*/
function chainsms_post_respond_to_inbound_sms($op, $objectName, $objectId, &$objectRef) {
$activity = civicrm_api('Activity', 'getsingle', array('version' => '3', 'id' => $objectId));
if (civicrm_error($activity)) {
CRM_Core_Error::debug_log_message("In chainsms_post_respond_to_inbound_sms: getsingle Activity id '$objectId' failed");
return;
}
$p = new CRM_Chainsms_Processor;
$p->inbound($activity);
}
/**
* Add body text to Mass SMS activities.
* Part of a hook_civicrm_post implementation.
*/
function chainsms_post_add_details_to_mass_sms($op, $objectName, $objectId, &$objectRef) {
$activity = civicrm_api('Activity', 'getsingle', array('version' => '3', 'id' => $objectId));
if (civicrm_error($activity)) {
CRM_Core_Error::debug_log_message("In chainsms_post_add_details_to_mass_sms: getsingle Activity id '$objectId' failed");
return;
}
$mailing = civicrm_api('Mailing', 'getsingle', array(
'version' => 3,
// Matching on both the mailing ID and subject just to make sure
'id' => $activity['source_record_id'],
'name' => $activity['subject'],
));
if (civicrm_error($mailing)) {
CRM_Core_Error::debug_log_message("In chainsms_post_add_details_to_mass_sms: getsingle Mailing id '{$activity['source_record_id']}' name '{$activity['subject']}' failed");
return;
}
$activityUpdateResult = civicrm_api('Activity', 'create', array(
'version' => 3,
// Update existing Activity, copying body text from the Mailing that sent this Mass SMS
'id' => $activity['id'],
'details' => $mailing['body_text'],
));
if (civicrm_error($activityUpdateResult)) {
CRM_Core_Error::debug_log_message("In chainsms_post_add_details_to_mass_sms: update Activity id {$activity['id']} adding details from Mailing id {$mailing['id']}'s body_text failed");
return;
}
}
function chainsms_civicrm_post($op, $objectName, $objectId, &$objectRef) {
//try and return as quickly as possible
if (
$objectName == 'Activity' &&
$objectRef->activity_type_id == CRM_Core_OptionGroup::getValue('activity_type', 'Inbound SMS', 'name')
) {
chainsms_post_respond_to_inbound_sms($op, $objectName, $objectId, $objectRef);
} elseif (
$objectName == 'Activity' && $op == 'create' &&
$objectRef->activity_type_id == CRM_Core_OptionGroup::getValue('activity_type', 'Mass SMS', 'name')
) {
chainsms_post_add_details_to_mass_sms($op, $objectName, $objectId, $objectRef);
}
}
/**
* civicrm_civicrm_navigationMenu
*
* implementation of civicrm_civicrm_navigationMenu
*/
function chainsms_civicrm_navigationMenu(&$params) {
$sAdministerMenuId = CRM_Core_DAO::getFieldValue('CRM_Core_BAO_Navigation', 'Administer', 'id', 'name');
$sSystemSettingsMenuId = CRM_Core_DAO::getFieldValue('CRM_Core_BAO_Navigation', 'System Settings', 'id', 'name');
// Get the maximum key of $params
$insertKey = ( max(array_keys($params)) ) + 2;
$params[$sAdministerMenuId]['child'][$sSystemSettingsMenuId]['child'][$insertKey] = array(
'attributes' => array(
'label' => 'SMS Survey Swear Filter',
'name' => 'SMSSurveySwearFilter',
'url' => 'civicrm/smssurvey/swearfilter',
'permission' => null,
'operator' => null,
'separator' => null,
'parentID' => $sSystemSettingsMenuId,
'navID' => $insertKey,
'active' => 1
)
);
// get the id of the Mailings Menu
$mailingsMenuId = CRM_Core_DAO::getFieldValue('CRM_Core_BAO_Navigation', 'Mailings', 'id', 'name');
// Create the SMS Survey submenu
// Get the maximum key of $params
$insertKey = max(array_keys($params[$mailingsMenuId]['child'])) + 1;
$subMenuId = $insertKey;
$params[$mailingsMenuId]['child'][$subMenuId] = array(
'attributes' => array(
'label' => SURVEY_SUBMENU_LABEL,
'name' => SURVEY_SUBMENU_LABEL,
'url' => null,
'permission' => null,
'operator' => null,
'separator' => null,
'parentID' => $mailingsMenuId,
'navID' => $subMenuId,
'active' => 1
)
);
// Populate the submenu
$chainSmsMessagesKey = max(array_keys($params[$mailingsMenuId]['child'])) + 1;
$params[$mailingsMenuId]['child'][$subMenuId]['child'][$chainSmsMessagesKey] = array(
'attributes' => array(
'label' => 'Chain SMS Messages',
'name' => 'SMSSurveyChains',
'url' => 'civicrm/smssurvey/chains',
'permission' => 'view all contacts',
'operator' => NULL,
'separator' => FALSE,
'parentID' => $subMenuId,
'navID' => $chainSmsMessagesKey,
'active' => 1
)
);
$smsSurveyTranslateKey = $chainSmsMessagesKey + 1;
$params[$mailingsMenuId]['child'][$subMenuId]['child'][$smsSurveyTranslateKey] = array(
'attributes' => array(
'label' => 'Translate Responses',
'name' => 'Translator',
'url' => 'civicrm/smssurvey/translate',
'permission' => 'view all contacts',
'operator' => NULL,
'separator' => FALSE,
'parentID' => $subMenuId,
'navID' => $smsSurveyTranslateKey,
'active' => 1
)
);
$smsSurveyCleanKey = $smsSurveyTranslateKey + 1;
$params[$mailingsMenuId]['child'][$subMenuId]['child'][$smsSurveyCleanKey] = array(
'attributes' => array(
'label' => 'Clean Translated Responses',
'name' => 'TranslationCleaner',
'url' => 'civicrm/smssurvey/translationcleaning',
'permission' => 'view all contacts',
'operator' => NULL,
'separator' => FALSE,
'parentID' => $subMenuId,
'navID' => $smsSurveyCleanKey,
'active' => 1
)
);
}
/**
* Implements hook_civicrm_permission().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_permission
*/
function chainsms_civicrm_permission(&$permissions) {
$permissions['view sms survey swear filter'] = 'ChainSMS: ' . ts('view sms survey swear filter');
}