forked from lemniscus/osdi-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosdi_client.php
142 lines (122 loc) · 4.82 KB
/
osdi_client.php
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
<?php
require_once 'osdi_client.civix.php';
// phpcs:disable
use Civi\Osdi\Logger;
use CRM_OSDI_ExtensionUtil as E;
// phpcs:enable
require_once __DIR__ . DIRECTORY_SEPARATOR
. 'vendor' . DIRECTORY_SEPARATOR
. 'autoload.php';
function osdi_client_civicrm_config(&$config) {
if (isset(Civi::$statics[__FUNCTION__])) {
return;
}
Civi::$statics[__FUNCTION__] = 1;
Civi::dispatcher()->addListener('civi.api.exception', function (\Civi\API\Event\ExceptionEvent $e) {
$signature = $e->getEntityName() . ':' . $e->getActionName();
if (str_contains(strtolower($signature), 'osdi')) {
Logger::logError('API Exception', ['exception' => $e->getException()]);
}
});
Civi::dispatcher()->addListener('civi.api.respond', function (\Civi\API\Event\RespondEvent $e) {
$signature = $e->getEntityName() . ':' . $e->getActionName();
if (str_contains(strtolower($signature), 'osdi')) {
$response = $e->getResponse();
if (is_array($response && ($response['is_error'] ?? FALSE))) {
Logger::logError('API Error', $response);
}
}
});
Civi::dispatcher()->addListener('&hook_civicrm_post::OsdiSyncProfile', ['\Civi\OsdiClient', 'postSaveOsdiSyncProfile']);
Civi::dispatcher()->addListener('&hook_civicrm_queueRun_osdiclient', ['\Civi\Osdi\Queue', 'runQueue']);
osdi_client_add_syncprofile_dependent_listeners();
_osdi_client_civix_civicrm_config($config);
}
function osdi_client_add_syncprofile_dependent_listeners(): void {
// only run this function once per Civi container build
if (Civi::dispatcher()->hasListeners(__FUNCTION__)) {
return;
}
// don't bother if we don't have/can't make an OsdiClient container
if (empty(Civi::settings()->get('osdiClient.defaultSyncProfile'))) {
if (!\Civi\OsdiClient::containerIsInitialized()) {
return;
}
}
Civi::dispatcher()->addListener(__FUNCTION__, 'dummy');
Civi::dispatcher()->addListener('civi.dao.preDelete', ['\Civi\Osdi\CrmEventDispatch', 'daoPreDelete']);
Civi::dispatcher()->addListener('civi.dao.preUpdate', ['\Civi\Osdi\CrmEventDispatch', 'daoPreUpdate']);
Civi::dispatcher()->addListener('&hook_civicrm_alterLocationMergeData', ['\Civi\Osdi\CrmEventDispatch', 'alterLocationMergeData']);
Civi::dispatcher()->addListener('&hook_civicrm_merge', ['\Civi\Osdi\CrmEventDispatch', 'merge']);
Civi::dispatcher()->addListener('&hook_civicrm_pre', ['\Civi\Osdi\CrmEventDispatch', 'pre']);
Civi::dispatcher()->addListener('&hook_civicrm_post', ['\Civi\Osdi\CrmEventDispatch', 'post']);
Civi::dispatcher()->addListener('&hook_civicrm_postCommit', ['\Civi\Osdi\CrmEventDispatch', 'postCommit']);
}
function osdi_client_civicrm_check(&$messages, $statusNames, $includeDisabled) {
if ($statusNames && !in_array('osdiClientZombieJob', $statusNames)) {
return;
}
if (!$includeDisabled) {
$disabled = \Civi\Api4\StatusPreference::get()
->setCheckPermissions(FALSE)
->addWhere('is_active', '=', FALSE)
->addWhere('domain_id', '=', 'current_domain')
->addWhere('name', '=', 'osdiClientZombieJob')
->execute()->count();
if ($disabled) {
return;
}
}
$jobStartTime = Civi::settings()->get('osdiClient.syncJobStartTime');
$jobProcessId = Civi::settings()->get('osdiClient.syncJobProcessId');
if (empty($jobStartTime) || empty(($jobProcessId))) {
return;
}
if (posix_getsid($jobProcessId) === FALSE) {
return;
}
if (time() - strtotime($jobStartTime) > 3600) {
$messages[] = new CRM_Utils_Check_Message(
'osdiClientZombieJob',
ts('An Action Network sync job has been running for over an hour. '
. 'This prevents new sync jobs from running. Process ID %1 began %2.',
[1 => $jobProcessId, 2 => $jobStartTime]),
ts('Long-Running Action Network Sync'),
\Psr\Log\LogLevel::WARNING,
'fa-hourglass'
);
}
}
/**
* Implements hook_civicrm_install().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_install
*/
function osdi_client_civicrm_install() {
_osdi_client_civix_civicrm_install();
}
/**
* Implements hook_civicrm_enable().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_enable
*/
function osdi_client_civicrm_enable() {
_osdi_client_civix_civicrm_enable();
}
// --- Functions below this ship commented out. Uncomment as required. ---
/**
* Implements hook_civicrm_navigationMenu().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_navigationMenu
*/
//function osdi_client_civicrm_navigationMenu(&$menu) {
// _osdi_client_civix_insert_navigation_menu($menu, 'Mailings', array(
// 'label' => E::ts('New subliminal message'),
// 'name' => 'mailing_subliminal_message',
// 'url' => 'civicrm/mailing/subliminal',
// 'permission' => 'access CiviMail',
// 'operator' => 'OR',
// 'separator' => 0,
// ));
// _osdi_client_civix_navigationMenu($menu);
//}