-
Notifications
You must be signed in to change notification settings - Fork 2
/
ContactSegment.php
executable file
·426 lines (405 loc) · 16.3 KB
/
ContactSegment.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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
<?php
require_once 'CRM/Core/Form.php';
// TODO: validate against available roles per segment
/**
* Form controller class
*
* @see http://wiki.civicrm.org/confluence/display/CRMDOC43/QuickForm+Reference
*/
class CRM_Contactsegment_Form_ContactSegment extends CRM_Core_Form {
protected $_contactId = NULL;
protected $_contactSegmentId = NULL;
protected $_parentSegmentId = NULL;
protected $_parentLabel = NULL;
protected $_childLabel = NULL;
protected $_contactSegment = array();
function buildQuickForm() {
$this->addFormElements();
parent::buildQuickForm();
}
/**
* Method to get the segment labels
*
* @access private
*/
private function getSegmentLabels() {
$segmentSetting = civicrm_api3('SegmentSetting', 'Getsingle', array());
$this->_parentLabel = $segmentSetting['parent_label'];
$this->_childLabel = $segmentSetting['child_label'];
}
/**
* Overridden parent method to initiate form
*
* @access public
*/
function preProcess() {
if ($this->_action != CRM_Core_Action::ADD) {
$this->getContactSegment();
} else {
$exportValues = CRM_Utils_Request::exportValues();
if (isset($exportValues['cid'])) {
$this->_contactId = $exportValues['cid'];
} else {
if (isset($exportValues['contact_id'])) {
$this->_contactId = $exportValues['contact_id'];
}
}
}
$this->getSegmentLabels();
switch ($this->_action) {
case CRM_Core_Action::ADD:
$actionLabel = "Add";
break;
case CRM_Core_Action::CLOSE:
$this->closeContactSegmentAndReturn();
break;
case CRM_Core_Action::UPDATE:
$actionLabel = "Edit";
break;
}
$headerLabel = $this->_parentLabel . " or " . $this->_childLabel;
CRM_Utils_System::setTitle($actionLabel." ".$headerLabel);
$this->assign('actionLabel', $actionLabel);
$this->assign('headerLabel', $headerLabel);
$contactName = (string) civicrm_api3('Contact', 'Getvalue',
array('id' => $this->_contactId, 'return' => 'display_name'));
$this->assign('contactName', $contactName);
}
/**
* Overridden parent method to process form (calls parent method too)
*
* @access public
*/
function postProcess() {
$this->_contactSegmentId = $this->_submitValues['contact_segment_id'];
if ($this->_submitValues['contact_id']) {
$this->_contactId = $this->_submitValues['contact_id'];
}
$this->_contactSegmentId = $this->_submitValues['contact_segment_id'];
if ($this->_action != CRM_Core_Action::VIEW) {
$this->saveContactSegment($this->_submitValues);
}
$session = CRM_Core_Session::singleton();
$contactSegmentUrl= CRM_Utils_System::url('civicrm/contact/view', 'action=browse&selectedChild=contactSegments&reset=1&cid='.$this->_contactId, true);
$session->replaceUserContext($contactSegmentUrl);
parent::postProcess();
}
/**
* Overridden parent method to set default values
*
* @return array $defaults
* @access public
*/
function setDefaultValues() {
$defaults = array();
$defaults['contact_id'] = $this->_contactId;
$defaults['contact_segment_id'] = $this->_contactSegmentId;
if ($this->_action == CRM_Core_Action::ADD) {
list($defaults['start_date']) = CRM_Utils_Date::setDateDefaults(date('d-m-Y'));
} else {
$defaults['contact_segment_role'] = $this->_contactSegment['role_value'];
if ($this->_parentSegmentId) {
$defaults['segment_parent'] = $this->_parentSegmentId;
$defaults['segment_child'] = $this->_contactSegment['segment_id'];
} else {
$defaults['segment_parent'] = $this->_contactSegment['segment_id'];
}
if ($this->_contactSegment['start_date']) {
list($defaults['start_date']) = CRM_Utils_Date::setDateDefaults($this->_contactSegment['start_date']);
}
if (isset($this->_contactSegment['end_date']) && !empty($this->_contactSegment['end_date'])) {
list($defaults['end_date']) = CRM_Utils_Date::setDateDefaults($this->_contactSegment['end_date']);
}
}
return $defaults;
}
/**
* Method to add form elements
*
* @access protected
*/
protected function addFormElements() {
$roleList = CRM_Contactsegment_Utils::getRoleList();
$parentList = CRM_Contactsegment_Utils::getParentList();
if ($this->_parentSegmentId) {
$childList = array("- select -") + CRM_Contactsegment_Utils::getChildList($this->_parentSegmentId);
} else {
if (isset($this->_contactSegment['segment_id'])) {
$childList = array("- select -") + CRM_Contactsegment_Utils::getChildList($this->_contactSegment['segment_id']);
} else {
$sql = 'SELECT id FROM civicrm_segment where parent_id IS NULL AND is_active = %1 ORDER BY label ASC LIMIT 1';
$defaultParentId = CRM_Core_DAO::singleValueQuery($sql, array(1 => array(1, 'Integer')));
$childList = array("- select -") + CRM_Contactsegment_Utils::getChildList($defaultParentId);
}
}
$this->add('hidden', 'contact_id');
$this->add('hidden', 'contact_segment_id');
$this->add('select', 'contact_segment_role', ts('Role'), $roleList, true);
$this->add('select', 'segment_parent', ts($this->_parentLabel), $parentList, true);
$this->add('select', 'segment_child', ts($this->_childLabel), $childList);
$this->addDate('start_date', ts('Start Date'), true);
$this->addDate('end_date', ts('End Date'), false);
$this->addButtons(array(
array('type' => 'next', 'name' => ts('Save'), 'isDefault' => true,),
array('type' => 'cancel', 'name' => ts('Cancel'))));
}
/**
* Method to save the contact segment
*
* @param $formValues
* @access protected
*/
protected function saveContactSegment($formValues) {
$params = array();
if ($formValues['contact_segment_id']) {
$params['id'] = $formValues['contact_segment_id'];
}
$params['contact_id'] = $formValues['contact_id'];
$params['role_value'] = $formValues['contact_segment_role'];
if ($formValues['segment_child']) {
$params['segment_id'] = $formValues['segment_child'];
} else {
$params['segment_id'] = $formValues['segment_parent'];
}
$segmentLabel = civicrm_api3('Segment', 'Getvalue', array('id' => $params['segment_id'], 'return' => 'label'));
if ($formValues['start_date']) {
$params['start_date'] = $formValues['start_date'];
} elseif (isset($formValues['start_date']) && empty($formValues['start_date'])) {
$params['start_date'] = false;
}
if ($formValues['end_date']) {
$params['end_date'] = $formValues['end_date'];
} elseif (isset($formValues['end_date']) && empty($formValues['end_date'])) {
$params['end_date'] = false;
}
$contactSegment = civicrm_api3('ContactSegment', 'create', $params);
$this->_contactSegment = $contactSegment['values'];
$session = CRM_Core_Session::singleton();
$session->setStatus("Contact linked to ".$segmentLabel." as ".$formValues['contact_segment_role'],
"Contact Linked to ".$segmentLabel, "success");
}
/**
* Overridden parent method to set validation rules
*/
public function addRules() {
$this->addFormRule(array('CRM_Contactsegment_Form_ContactSegment', 'validateRoleAllowed'));
$this->addFormRule(array('CRM_Contactsegment_Form_ContactSegment', 'validateRoleUnique'));
$this->addFormRule(array('CRM_Contactsegment_Form_ContactSegment', 'validateExists'));
$this->addFormRule(array('CRM_Contactsegment_Form_ContactSegment', 'validateEndDate'));
}
/**
* Method to end contact segment and return
*
*/
protected function closeContactSegmentAndReturn() {
$startDate = new DateTime($this->_contactSegment['start_date']);
$nowDate = new DateTime();
if ($startDate > $nowDate) {
$endDate = $startDate->modify('+1 day');
$this->_contactSegment['end_date'] = $endDate->format('Y-m-d');
} else {
$this->_contactSegment['end_date'] = $nowDate->format('Y-m-d');
$this->_contactSegment['is_active'] = 0;
}
civicrm_api3('ContactSegment', 'Create', $this->_contactSegment);
$session = CRM_Core_Session::singleton();
$displayName = civicrm_api3('Contact', 'Getvalue',
array('id' => $this->_contactSegment['contact_id'], 'return' => 'display_name'));
$segment = civicrm_api3('Segment', 'Getsingle', array('id' => $this->_contactSegment['segment_id']));
if (!$segment['parent_id']) {
$statusMessage = $this->_parentLabel . " " . $segment['label'] . " with role " . $this->_contactSegment['role_value']
. " ended for contact " . $displayName;
$statusTitle = $this->_parentLabel . " ended";
} else {
$statusMessage = $this->_childLabel . " " . $segment['label'] . " with role " . $this->_contactSegment['role_value']
. " ended for contact " . $displayName;
$statusTitle = $this->_childLabel . " ended";
}
$session->setStatus($statusMessage, $statusTitle, "success");
$contactSegmentUrl= CRM_Utils_System::url('civicrm/contact/view', 'action=browse&selectedChild=contactSegments&reset=1&cid='.$this->_contactId, true);
CRM_Utils_System::redirect($contactSegmentUrl);
}
/**
* Method to get the current contact segment data
*
* @access protected
*/
protected function getContactSegment() {
if (empty($this->_submitValues)) {
$this->_contactSegmentId = CRM_Utils_Request::retrieve('csid', 'Integer');
$this->_contactId = CRM_Utils_Request::retrieve('cid', 'Integer');
} else {
$this->_contactSegmentId = $this->_submitValues['contact_segment_id'];
$this->_contactId = $this->_submitValues['contact_id'];
}
$this->_contactSegment = civicrm_api3('ContactSegment', 'Getsingle', array('id' => $this->_contactSegmentId));
$this->_parentSegmentId = civicrm_api3('Segment', 'Getvalue',
array('id' => $this->_contactSegment['segment_id'], 'return' => 'parent_id'));
}
/**
* Method to validate if role is allowed for segment
*
* @param array $fields
* @return array $errors or TRUE
* @access public
* @static
*/
static function validateRoleAllowed($fields) {
$errors = array();
$segmentSettings = civicrm_api3('SegmentSetting', 'Getsingle', array());
$roleName = CRM_Contactsegment_Utils::getRoleNameWithLabel($fields['contact_segment_role']);
if ($fields['contact_segment_role']) {
if ($fields['segment_child']) {
if (!isset($segmentSettings['child_roles'][$roleName])) {
$errors['contact_segment_role'] = ts('Role not allowed for '.$segmentSettings['child_label']);
$errors['segment_child'] = ts('Role not allowed for '.$segmentSettings['child_label']);
return $errors;
}
}
if ($fields['segment_parent']) {
if (!isset($segmentSettings['parent_roles'][$roleName])) {
$errors['contact_segment_role'] = ts('Role not allowed for '.$segmentSettings['parent_label']);
$errors['segment_parent'] = ts('Role not allowed for '.$segmentSettings['parent_label']);
return $errors;
}
}
}
return TRUE;
}
/**
* Method to validate if contact segment already exists taking overlapping dates into consideration
*
* @param array $fields
* @return array|bool $errors or TRUE
* @access public
* @static
*/
static function validateExists($fields)
{
// not required if end date and start date are the same (dummy record)
if ($fields['start_date'] != $fields['end_date']) {
$errors = array();
$segmentSettings = civicrm_api3('SegmentSetting', 'Getsingle', array());
// determine if we are dealing with parent or child
if (!$fields['segment_child']) {
$segmentId = $fields['segment_parent'];
$segmentErrorLabel = $segmentSettings['parent_label'];
$errorIndex = 'segment_parent';
} else {
$segmentId = $fields['segment_child'];
$segmentErrorLabel = $segmentSettings['child_label'];
$errorIndex = 'segment_child';
}
// retrieve all existing contact segments with the same segment, contact and role
$foundContactSegments = civicrm_api3('ContactSegment', 'get', array(
'contact_id' => $fields['contact_id'],
'role_value' => $fields['contact_segment_role'],
'segment_id' => $segmentId));
// foreach found contact segment, check if it overlaps
foreach ($foundContactSegments['values'] as $foundContactSegmentId => $foundContactSegment) {
if (CRM_Contactsegment_Form_ContactSegment::checkValidDates($fields, $foundContactSegment) == FALSE) {
$errors[$errorIndex] = ts('Contact is already linked to ' . $segmentErrorLabel . ', edit the existing link if required');
return $errors;
}
}
}
return TRUE;
}
/**
* Method to check if the entered start_date in $fields is allowed in combination with the
* $foundContactSegment
*
* @param $fields
* @param $foundContactSegment
* @return bool
*/
static function checkValidDates($fields, $foundContactSegment) {
// if start and end date are the same on foundContactSegment, ignore it as it is a dummy record
if (isset($foundContactSegment['start_date']) && isset($foundContactSegment['end_date'])) {
if ($foundContactSegment['start_date'] == $foundContactSegment['end_date']) {
return TRUE;
}
}
// if we have the same id, ignore (this can be at update time)
if (isset($fields['contact_segment_id']) && $fields['contact_segment_id'] != $foundContactSegment['id']) {
// if found has no end date, then error else check if overlap
if (!isset($foundContactSegment['end_date']) || empty($foundContactSegment['end_date'])) {
return FALSE;
} else {
if (CRM_Contactsegment_Utils::overlapDates($fields['start_date'], $foundContactSegment['end_date']) == TRUE) {
return FALSE;
}
}
}
if (empty($fields['contact_segment_id'])) {
// if found has no end date, then error else check if overlap
if (!isset($foundContactSegment['end_date']) || empty($foundContactSegment['end_date'])) {
return FALSE;
} else {
if (CRM_Contactsegment_Utils::overlapDates($fields['start_date'], $foundContactSegment['end_date']) == TRUE) {
return FALSE;
}
}
}
return TRUE;
}
/**
* Method to validate if end date is not earlier than or equal to start date
*
* @param array $fields
* @return array|bool $errors or TRUE
* @access public
* @static
*/
static function validateEndDate($fields) {
$errors = array();
if ($fields['end_date']) {
if ($fields['start_date']) {
$endDate = new DateTime($fields['end_date']);
$startDate = new DateTime($fields['start_date']);
if ($endDate < $startDate) {
$errors['end_date'] = ts('End Date has to be later than Start Date');
return $errors;
}
} else {
$errors['end_date'] = ts('End Date has to be later than Start Date');
return $errors;
}
}
return TRUE;
}
/**
* Method to validate if role is unique and already active
*
* @param array $fields
* @return array $errors or TRUE
* @access public
* @static
*/
static function validateRoleUnique($fields) {
$errors = array();
$checkParams = array(
'role' => $fields['contact_segment_role'],
'contact_id' => $fields['contact_id'],
'start_date' => $fields['start_date'],
'end_date' => $fields['end_date']);
if (!empty($fields['segment_child'])) {
if (CRM_Contactsegment_Utils::isSegmentRoleUnique($fields['contact_segment_role'], 'child') == TRUE) {
$checkParams['segment_id'] = $fields['segment_child'];
if (CRM_Contactsegment_Utils::activeCurrentContactSegmentForRole($checkParams) != FALSE) {
$errors['segment_child'] = ts('Only 1 active role allowed, there is already an active '.$fields['contact_segment_role']);
return $errors;
}
}
} else {
if (CRM_Contactsegment_Utils::isSegmentRoleUnique($fields['contact_segment_role'], 'parent') == TRUE) {
$checkParams['segment_id'] = $fields['segment_parent'];
if (CRM_Contactsegment_Utils::activeCurrentContactSegmentForRole($checkParams) != FALSE) {
$errors['segment_parent'] = ts('Only 1 active role allowed, there is already an active '.$fields['contact_segment_role']);
return $errors;
}
}
}
return TRUE;
}
}