-
Notifications
You must be signed in to change notification settings - Fork 18
/
AsterLinkEntryPoint.php
158 lines (121 loc) · 4.68 KB
/
AsterLinkEntryPoint.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
require_once('modules/AsterLink/utils.php');
if (!isset($_SERVER['HTTP_X_ASTERLINK_TOKEN']) ||
!isset($_REQUEST['action']) ||
!($config = getConfig())
) {
http_response_code(400);
die();
}
if ($_SERVER['HTTP_X_ASTERLINK_TOKEN'] != $config['endpoint_token']) {
sleep(5);
http_response_code(403);
die();
}
$action = $_REQUEST['action'];
$response = null;
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $_SERVER['CONTENT_TYPE'] === 'application/json') {
$_POST = json_decode(file_get_contents('php://input'), true);
}
switch ($action) {
case 'get_ext_users':
$r = BeanFactory::getBean('Users')->get_list("", "asterlink_ext_c IS NOT NULL AND asterlink_ext_c != ''");
foreach($r['list'] as $user) {
$response[$user->asterlink_ext_c] = $user->id;
}
break;
case 'create_call_record':
$response = [];
$callBean = BeanFactory::newBean('Calls');
foreach ($_POST as $k => $v) {
$callBean->{$k} = $v;
}
$callBean->parent_type = 'Accounts'; // Fix SuiteCRM 8 "My Calls" dashlet explosion.
$callBean->save();
$response['id'] = $callBean->id;
http_response_code(201);
if (empty($config['relationships'])) {
break;
}
foreach ($config['relationships'] as $rel_config) {
$module_t = strtolower($rel_config['module']);
$fields = [];
foreach ($rel_config['phone_fields'] as $phone_field) {
$fields[] = "`$module_t`.`$phone_field` = '{$callBean->asterlink_cid_c}'";
}
$rel = BeanFactory::getBean($rel_config['module'])->get_list("", implode(' OR ', $fields), 0, 1);
if (!count($rel['list'])) {
continue;
}
$rel = $rel['list'][0];
$callBean->parent_type = $rel_config['module'];
$callBean->parent_id = $rel->id;
$callBean->save();
if (!isset($rel_config['is_parent']) || !$rel_config['is_parent']) {
$callBean->load_relationship($rel_config['name']);
$callBean->{$rel_config['name']}->add($rel);
} else {
$callBean->parent_type = $rel_config['module'];
$callBean->parent_id = $rel->id;
$callBean->save();
$relatedBean = BeanFactory::getBean($rel_config['module'], $rel->id);
$relatedBean->load_relationship($rel_config['name']);
$relatedBean->{$rel_config['name']}->add($callBean);
}
$response['relations'][$rel_config['module']] = [
'id' => $rel->id,
'name' => $rel->{$rel_config['name_field']},
'assigned_user_id' => $rel->assigned_user_id
];
if (isset($config['relate_once']) && $config['relate_once'])
break;
}
break;
case 'update_call_record':
$callBean = BeanFactory::getBean('Calls', $_POST['id']);
foreach ($_POST['data'] as $k => $v) {
$callBean->{$k} = $v;
}
$callBean->date_start = null; // prevent suitecrm from date fields ("save" function in modules/Calls/Call.php)
$callBean->save();
break;
case 'get_relations':
$response = [];
if (empty($config['relationships'])) {
break;
}
$callBean = BeanFactory::getBean('Calls', $_POST['id']);
foreach ($config['relationships'] as $rel_config) {
// parent module special
if (isset($rel_config['is_parent']) && $rel_config['is_parent']) {
if (!empty($callBean->parent_id) && $callBean->parent_type == $rel_config['module']) {
$response[$rel_config['module']] = [
'id' => $callBean->parent_id,
'name' => $callBean->parent_name,
];
}
continue;
}
$callBean->load_relationship($rel_config['name']);
$rels = $callBean->{$rel_config['name']}->getBeans();
if (empty($rels))
continue;
$rel = reset($rels);
$response[$rel_config['module']] = [
'id' => $rel->id,
'name' => $rel->{$rel_config['name_field']},
'assigned_user_id' => $rel->assigned_user_id
];
}
break;
default:
http_response_code(400);
break;
}
if (is_null($response)) {
http_response_code(204);
die();
}
header('Content-Type: application/json');
echo json_encode($response);