forked from krd-consulting/ncn-civi-zoom
-
Notifications
You must be signed in to change notification settings - Fork 15
/
ImportParticipant.php
180 lines (162 loc) · 6.3 KB
/
ImportParticipant.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
<?php
use CRM_NcnCiviZoom_ExtensionUtil as E;
/**
* Form controller class
*
* @see https://wiki.civicrm.org/confluence/display/CRMDOC/QuickForm+Reference
*/
class CRM_NcnCiviZoom_Form_ImportParticipant extends CRM_Core_Form {
public $_id = NULL;
public function preProcess() {
CRM_Utils_System::setTitle(ts("Import Participant"));
$this->_id = CRM_Utils_Request::retrieve('id', 'Int', CRM_Core_DAO::$_nullObject);
$session = CRM_Core_Session::singleton();
$session->pushUserContext(CRM_Utils_System::url('civicrm/zoom/importparticipant',"reset=1"));
parent::preProcess();
}
public function buildQuickForm() {
$this->_id = CRM_Utils_Request::retrieve('id', 'Int', CRM_Core_DAO::$_nullObject);
$zoomRegistrant = CRM_NcnCiviZoom_Utils::getZoomRegistrantDetailsById($this->_id);
CRM_Core_Error::debug_var('buildQuickForm zoomRegistrant', $zoomRegistrant);
$getContactsQuery = "
SELECT
c.id,
c.display_name as d_name
FROM civicrm_contact c
LEFT JOIN civicrm_email e ON e.contact_id = c.id
WHERE
c.is_deleted = 0 and
e.email = %1";
$qParams = array(
1 => array($zoomRegistrant['email'], 'String'),
);
CRM_Core_Error::debug_var('buildQuickForm getContactsQuery', $getContactsQuery);
CRM_Core_Error::debug_var('buildQuickForm qParams', $qParams);
$dao = CRM_Core_DAO::executeQuery($getContactsQuery, $qParams);
$selectOptions = array('' => '-- select --');
while ($dao->fetch()) {
$selectOptions[$dao->id] = $dao->d_name;
}
$this->add(
'select',
'change_contact_id',
'Select Contact',
$selectOptions,
TRUE,
array('class' => 'medium', 'multiple' => FALSE, 'id' => 'change_contact_id')
);
$defaults = array();
if(count($selectOptions) == 2){
$defaults['change_contact_id'] = key( array_slice( $selectOptions, -1, 1, TRUE ) );
}
CRM_Core_Error::debug_var('buildQuickForm default', $default);
$event_title = CRM_Core_DAO::singleValueQuery('SELECT title FROM civicrm_event WHERE id = '.$zoomRegistrant['event_id']);
$this->assign('current_event', $event_title);
$this->assign('id', $this->_id);
$this->assign('event_id', $zoomRegistrant['event_id']);
// export form elements
$this->assign('elementNames', $this->getRenderableElementNames());
$this->addButtons(array(
array(
'type' => 'submit',
'name' => ts('Submit'),
'isDefault' => TRUE,
),
));
$this->setDefaults($defaults);
parent::buildQuickForm();
}
public function postProcess() {
$values = $this->exportValues();
$zoomRegistrant = CRM_NcnCiviZoom_Utils::getZoomRegistrantDetailsById($this->_id);
try {
$default_role_options = civicrm_api3('Event', 'getoptions', [
'field' => "default_role_id",
]);
} catch (Exception $e) {
CRM_Core_Error::debug_log_message('Error while calling api in '.__CLASS__.'::'.__FUNCTION__);
CRM_Core_Error::debug_log_message('Api Entity: Event , Action: getoptions');
}
try {
$eventDetails = civicrm_api3('Event', 'get', [
'sequential' => 1,
'id' => $zoomRegistrant['event_id'],
]);
} catch (Exception $e) {
CRM_Core_Error::debug_log_message('Error while calling api in '.__CLASS__.'::'.__FUNCTION__);
CRM_Core_Error::debug_log_message('Api Entity: Event , Action: get');
}
$eventId = $zoomRegistrant['event_id'];
$contactId = $values['change_contact_id'];
$createParticipantApiParams = array(
'event_id' => $eventId,
'contact_id' => $contactId,
'status_id' => 'Registered',
);
if(!empty($eventDetails['values'][0]['default_role_id'])){
$createParticipantApiParams['role_id'] = $eventDetails['values'][0]['default_role_id'];
}
$result['is_error'] = TRUE;
try {
$result = civicrm_api3('Participant', 'create', $createParticipantApiParams);
} catch (Exception $e) {
CRM_Core_Error::debug_log_message('Error while calling api in '.__CLASS__.'::'.__FUNCTION__);
CRM_Core_Error::debug_log_message('Api Entity: Participant , Action: create');
CRM_Core_Error::debug_var('Api Params', $createParticipantApiParams);
CRM_Core_Error::debug_var('Api Error details', $e);
}
if (!$result['is_error']) {
CRM_Core_Session::setStatus(ts('Participant added successfully.'), ts('Added'), 'success');
// Retrieving the Zoom Join Link
$pId = $result['id'];
$zoom_join_link = '';
$settings = CRM_NcnCiviZoom_Utils::getZoomSettings();
$locationTypeId = $settings['import_email_location_type'];
if(empty($locationTypeId)){
$locationTypeId = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_location_type WHERE is_default = 1");
}
$queryParams = array(
1 => array($contactId, 'Integer'),
2 => array($locationTypeId, 'Integer'),
);
$query = "SELECT email FROM civicrm_email WHERE (contact_id = %1 AND location_type_id = %2) LIMIT 1";
$emailId = CRM_Core_DAO::singleValueQuery($query, $queryParams);
if(!empty($emailId)){
$zoomRegistrantsList = CRM_CivirulesActions_Participant_AddToZoom::getZoomRegistrants($eventId);
foreach ($zoomRegistrantsList as $key => $registrant) {
if($registrant['email'] == $emailId){
$zoom_join_link = $registrant['join_url'];
break;
}
}
}
// Update the zoom join link against the participant
if(!empty($zoom_join_link)){
CRM_NcnCiviZoom_Utils::updateZoomParticipantJoinLink($pId, $zoom_join_link);
}
} else {
CRM_Core_Session::setStatus(ts('Unable add the participant.'), ts('Error'), 'error');
}
parent::postProcess();
}
/**
* Get the fields/elements defined in this form.
*
* @return array (string)
*/
public function getRenderableElementNames() {
// The _elements list includes some items which should not be
// auto-rendered in the loop -- such as "qfKey" and "buttons". These
// items don't have labels. We'll identify renderable by filtering on
// the 'label'.
$elementNames = array();
foreach ($this->_elements as $element) {
/** @var HTML_QuickForm_Element $element */
$label = $element->getLabel();
if (!empty($label)) {
$elementNames[] = $element->getName();
}
}
return $elementNames;
}
}