-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDolibarrEvent.php
More file actions
252 lines (218 loc) · 7.81 KB
/
Copy pathDolibarrEvent.php
File metadata and controls
252 lines (218 loc) · 7.81 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
<?php
enum DolibarrEventType: string
{
case MEETING = 'AC_RDV';
case CALL = 'AC_TEL';
case EMAIL = 'AC_EMAIL';
case NOTE = 'AC_NOTE';
case TASK = 'AC_TODO';
case OTHER = 'AC_OTH';
case ACTION = 'AC_ACT';
}
/**
* Class DolibarrEvent
*
* @property int $id
* @property string $label
* @property string $type_code
* @property DateTime $datep
* @property DateTime|null $datef
* @property string $elementtype
* @property int $elementid
* @property int $userownerid
* @property int|null $socid
*/
class DolibarrEvent extends DolibarrObject
{
public static function createEventInProject(
string $title,
DateTime $start,
?DateTime $end,
DolibarrEventType $type,
int $projectId,
int $userId,
?int $thirdpartyId = null
): ?self {
return self::createEvent($title, $start, $end, $type->value, 'project', $projectId, $userId, $thirdpartyId);
}
public static function createEventInProposal(
string $title,
DateTime $start,
?DateTime $end,
DolibarrEventType $type,
int $proposalId,
int $userId,
?int $thirdpartyId = null
): ?self {
return self::createEvent($title, $start, $end, $type->value, 'propal', $proposalId, $userId, $thirdpartyId);
}
public static function createEventInContact(
string $title,
DateTime $start,
?DateTime $end,
string $typeCode,
int $contactId,
int $userId,
?int $thirdpartyId = null,
?string $description = null
): ?self {
return self::createEvent($title, $start, $end, $typeCode, 'contact', $contactId, $userId, $thirdpartyId, $description);
}
public static function findByContactAndMarker(int $contactId, string $marker, ?string $typeCode = null): ?self
{
$escapedMarker = self::escapeSqlLikeValue($marker);
$sqlFilters = urlencode("(t.note:like:'%{$escapedMarker}%')");
$endpoint = '/agendaevents?sortfield=t.rowid&sortorder=DESC&limit=20&sqlfilters=' . $sqlFilters;
$events = self::fetchFromDolibarr($endpoint, 1, 1);
if (!is_array($events)) {
return null;
}
foreach ($events as $event) {
if (!is_object($event)) {
continue;
}
if ($typeCode !== null && isset($event->type_code) && (string) $event->type_code !== $typeCode) {
continue;
}
$eventContactId = (int) ($event->elementid ?? $event->contactid ?? $event->fk_contact ?? 0);
$eventElementType = strtolower((string) ($event->elementtype ?? ''));
if ($eventContactId > 0 && $eventContactId !== $contactId && in_array($eventElementType, ['contact', 'socpeople'], true)) {
continue;
}
$text = implode("\n", [
(string) ($event->label ?? ''),
(string) ($event->note ?? ''),
(string) ($event->private_note ?? ''),
(string) ($event->note_private ?? ''),
(string) ($event->note_public ?? ''),
(string) ($event->description ?? ''),
]);
if (str_contains($text, $marker)) {
return new self($event);
}
}
return null;
}
private static function createEvent(
string $title,
DateTime $start,
?DateTime $end,
string $typeCode,
string $elementType, // 'project' ou 'propal' (etc.)
int $elementId,
int $userId,
?int $thirdpartyId = null,
?string $description = null
): ?self {
$apiKey = DOLIBARR_API_KEY;
$url = DOLIBARR_REST_URL . "/agendaevents";
$data = [
'label' => $title,
'type_code' => $typeCode,
'datep' => $start->getTimestamp(),
'datef' => $end ? $end->getTimestamp() : '',
'userownerid' => $userId,
'fulldayevent' => 0,
'punctual' => 1,
'percentage' => 100,
'priority' => 0,
'transparency' => '1',
];
// 🎯 Affectation selon le type de lien
if ($elementType === 'project') {
$data['fk_project'] = $elementId;
} elseif ($elementType === 'contact') {
$data['elementtype'] = $elementType;
$data['elementid'] = $elementId;
$data['contactid'] = $elementId;
$data['fk_contact'] = $elementId;
} else {
$data['elementtype'] = $elementType;
$data['elementid'] = $elementId;
}
if ($thirdpartyId !== null) {
$data['socid'] = $thirdpartyId;
$data['fk_soc'] = $thirdpartyId;
}
if ($description !== null && trim($description) !== '') {
$data['note'] = $description;
$data['private_note'] = $description;
$data['note_private'] = $description;
$data['note_public'] = $description;
}
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"DOLAPIKEY: $apiKey",
"Content-Type: application/json"
]);
curl_setopt($ch, CURLOPT_POST, true);
$response = curl_exec($ch);
if (curl_errno($ch)) {
error_log('Erreur cURL : ' . curl_error($ch));
curl_close($ch);
return null;
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode !== 200 && $httpCode !== 201) {
error_log("Erreur HTTP $httpCode lors de la création de l'événement : $response");
return null;
}
$eventData = json_decode($response);
if (is_int($eventData)) {
return new self((object) ['id' => $eventData]);
}
if (is_string($eventData) && ctype_digit($eventData)) {
return new self((object) ['id' => (int) $eventData]);
}
return $eventData ? new self($eventData) : null;
}
private static function escapeSqlLikeValue(string $value): string
{
return str_replace(
["\\", "'", "%", "_"],
["\\\\", "\\'", "\\%", "\\_"],
$value
);
}
// Ajoute ici des getters si besoin, comme getId(), getTitle() etc.
public static function initTestAction(): void
{
if (!isset($_GET['action']) || $_GET['action'] !== 'createEvent') {
return;
}
if (!self::isCurrentUserAdminWP()) {
echo "⛔ Accès interdit : vous devez être connecté en tant qu’administrateur WordPress.";
return;
}
$start = new DateTime('now');
$end = (clone $start)->modify('+1 hour');
$event = self::createEventInProject(
"Test automatique depuis WordPress vers Project",
$start,
$end,
DolibarrEventType::ACTION,
50, // ID de project fictive
1, // ID Dolibarr de l'utilisateur à utiliser (à adapter)
195 // Tiers (facultatif)
);
if ($event) {
echo "✅ Événement de test créé avec succès !<br/>";
$event->printData();
} else {
echo "❌ Erreur lors de la création de l’événement de test.";
}
}
private static function isCurrentUserAdminWP(): bool
{
if (!function_exists('is_user_logged_in')) {
require_once ABSPATH . 'wp-includes/pluggable.php';
}
return is_user_logged_in() && current_user_can('administrator');
}
}
add_action('init', function () {
DolibarrEvent::initTestAction();
});