-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcivicrm_event_parent.module
401 lines (360 loc) · 16.1 KB
/
civicrm_event_parent.module
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
<?php
define('CIVICRM_PARENT_RID', 1); // relationship type id
define('CIVICRM_PARENT_DIR', 'b_a'); // relationship id
/**
* hook_civicrm_buildForm
*/
function civicrm_event_parent_civicrm_buildForm( $formName, &$form ) {
$reset = true;
$session = CRM_Core_Session::singleton( );
if ($formName == 'CRM_Event_Form_Registration_Confirm'){
$session->getStatus(true);
}
// event registration form
if ( $formName == 'CRM_Event_Form_Registration_Register' ) {
civicrm_event_parent_civicrm_buildForm_regRegister( $formName, $form );
}
// event registration for additional participant
if ( $formName == 'CRM_Event_Form_Registration_AdditionalParticipant' ) {
$session->getStatus(true);
civicrm_event_parent_civicrm_buildForm_regAdditional( $formName, $form );
}
// Profile create url
if ( is_a( $form, 'CRM_Profile_Form_Edit' ) ) {
$session = CRM_Core_Session::singleton( );
if($_GET['event_id'] && is_numeric($_GET['event_id'])){
$event_id = $_GET['event_id'];
$session->set('event_id', $event_id, 'profile');
$form->addElement( 'hidden', 'event_id', $_GET['event_id']);
}
elseif($session->get('event_id', 'profile')){
$event_id = $session->get('event_id', 'profile');
if($session->get('userID') && $session->get('ufID')){
$mainID = $session->get('userID');
drupal_goto( 'civicrm/event/register', array('query' => array(
'reset' => 1,
'id' => $event_id,
'cid' => 0,
'mainID' => $mainID,
)));
}
$form->addElement( 'hidden', 'event_id', $event_id);
}
else{
return;
}
$parent_gid = variable_get('civicrm_event_parent_gid', array());
require_once("api/v2/Event.php");
$params = array('id' => $event_id);
$result = civicrm_event_get($params);
if(!$result['is_error'] && $parent_gid[$event_id]){
list($rTypeID, $rTypeDir) = explode('-', $parent_gid[$event_id]['parent_relation']);
$rTypeID = CRM_Utils_Type::escape($rTypeID, 'Integer');
$relation = CRM_Core_DAO::executeQuery("SELECT * FROM civicrm_relationship_type WHERE id = ".$rTypeID);
$relation->fetch();
$relation_label = 'label_'.$rTypeDir;
drupal_set_title($result['title']." - ".t("!relation profile form", array('!relation' => $relation->{$relation_label})));
}
}
// Event administration form
if ( $formName == 'CRM_Event_Form_ManageEvent_Registration'){
$parent_gid = variable_get('civicrm_event_parent_gid', array());
$event_id = $form->getVar('_id');
// add relationship type select
// for now, only available for both ab / ba is Individual
$relations = CRM_Core_PseudoConstant::relationshipType();
$relationships[0] = t('- select -');
foreach($relations as $rid => $r){
if($r['contact_type_a'] != 'Individual' || $r['contact_type_b'] != 'Individual'){
unset($relations[$rid]);
}
else{
$relationships[$rid.'-a_b'] = $r['label_a_b'];
$relationships[$rid.'-b_a'] = $r['label_b_a'];
}
}
$sel_id = 'parent_relation';
$sel = $form->addElement( 'select', $sel_id, t( 'Parent Relationship' ), $relationships);
if(!empty($parent_gid[$event_id])){
$form->setDefaults( array('parent_relation' => $parent_gid[$event_id]['parent_relation']) );
}
$sel_html = $sel->toHtml();
$sel_html = '<tr class="crm-event-manage-registration-form-block-'.$sel_id.'"><td scope="row" class="label" width="20%"><label for="'.$sel_id.'">'.t('Parent Relationship').'</label></td><td>'.$sel_html.'<br /><span class="description">'.t('Relationship type for registeration agent of participant. When enable this feature with profile option above, event registration will have two form to complete. One is profile crate form, another is normal event register. When a participant go join the event, they will bring to first form of profile to fill in agent information. After that, the form will redirect to normal event registeration. And these two individual will connect with seleted relationship of this option.').'</span></td></tr>';
$sel_html = str_replace("\n", "", $sel_html);
$sel_html = str_replace("'", "\'", $sel_html);
drupal_add_js("jQuery(document).ready(function($){ var parent_relation_html = '{$sel_html}'; $(parent_relation_html).insertBefore('.crm-event-manage-registration-form-block-custom_pre_id'); });",'inline', 'footer');
// add profile form select
$sql = "SELECT id, title FROM civicrm_uf_group WHERE is_active = 1 AND group_type LIKE '%Individual%'";
$dao = CRM_Core_DAO::executeQuery( $sql);
$options[0] = t('- select -');
while($dao->fetch()){
$options[$dao->id] = $dao->title;
}
$sel_id = 'parent_gid';
// set default
if(!empty($parent_gid[$event_id])){
$form->setDefaults( array('parent_gid' => $parent_gid[$event_id]['parent_gid']) );
}
$sel = $form->addElement( 'select', $sel_id, t( 'Parent Profile' ), $options );
$sel_html = $sel->toHtml();
$sel_html = '<tr class="crm-event-manage-registration-form-block-'.$sel_id.'"><td scope="row" class="label" width="20%"><label for="'.$sel_id.'">'.t('Parent Profile').'</label></td><td>'.$sel_html.'<br /><span class="description">'.t('Pre-registeration profile for registration agent. Using this option will also need to config relationship type below. This form will only collect contact data and build relationships. People who enter this form will not count in participant.').'<br />'.t('You may need to visit this event registeration page by <strong>anonymous user</strong> and <strong>logged user</strong>, to make sure whole process fit your needs.').'</span></td></tr>';
$sel_html = str_replace("\n", "", $sel_html);
$sel_html = str_replace("'", "\'", $sel_html);
drupal_add_js("jQuery(document).ready(function($){ var parent_relation_html = '{$sel_html}'; $(parent_relation_html).insertBefore('.crm-event-manage-registration-form-block-custom_pre_id'); });",array('type' => 'inline', 'scope' => 'footer'));
}
}
function civicrm_event_parent_civicrm_buildForm_regRegister( $formName, &$form ) {
// refs #27403
$participantId = CRM_Utils_Request::retrieve( 'participantId', 'Integer', $form, false, null, $_REQUEST);
if (!empty($participantId)) {
return;
}
$cid = CRM_Utils_Request::retrieve( 'cid', 'Integer', $form, false, null, $_REQUEST);
$session = CRM_Core_Session::singleton( );
$contact_id = $session->get( 'userID' );
$uid = $session->get( 'ufID' );
if($contact_id){
$display_name = CRM_Core_DAO::singleValueQuery("SELECT display_name FROM civicrm_contact WHERE id = {$contact_id}");
}
$event_id = $form->getVar( '_eventId' );
$civicrm_event_parent_gid = variable_get('civicrm_event_parent_gid', array());
$mainID = CRM_Utils_Request::retrieve( 'mainID', 'Integer', $form, false, null, $_REQUEST );
list($rTypeID, $rTypeDir) = explode('-', $civicrm_event_parent_gid[$event_id]['parent_relation']);
$relation_label = 'label_'.$rTypeDir;
$_id = $_GET['id'];
if(!empty($civicrm_event_parent_gid[$event_id]['parent_gid']) && !empty($rTypeID)){
$relation = CRM_Core_DAO::executeQuery("SELECT * FROM civicrm_relationship_type WHERE id = ".$rTypeID);
$relation->fetch();
// handling redirection
if($cid === NULL && $contact_id && $uid){
drupal_goto('civicrm/event/register', array('query' => array(
'reset' => 1,
'cid' => 0,
'mainID' => $contact_id,
'id' => $event_id,
)));
}
if( !$mainID ){
// redirect to parent profile create form
if(!$uid){
drupal_goto('civicrm/profile/create',array('query' => array(
'gid' => $civicrm_event_parent_gid[$event_id]['parent_gid'],
'reset' => 1,
'event_id' => $event_id,
)));
}
}
// now the real registration process
else {
if($_GET['_qf_Confirm_display'] || $_GET['_qf_ThankYou_display']){
return;
}
$status_msg = array(
'!name' => $display_name,
'!relationship' => $relation->{$relation_label},
'!register_link' => url("civicrm/event/register", array("query" => array(
'reset' => 1,
'cid' => $contact_id,
'id' => $event_id,
))),
);
if($uid){
$status = t("Hello !name! After finish this form, we will create a relashionship between you and belows. If you are not below's !relationship, or you would like to register for yourself, please <a href='!register_link'>use alternative form to register this event</a>.", $status_msg);
}
else{
$status = t("Please continue enter this form. We suppose you are !relationship of belows, then we will create relationship between you and belows. If you would like to register for yourself, please <a href='!register_link'>use alternative form to register this event</a>.", $status_msg);
}
CRM_Core_Session::setStatus($status, FALSE);
if($rTypeDir == 'b_a'){
$title_label = 'label_a_b';
}
else{
$title_label = 'label_b_a';
}
drupal_set_title(drupal_get_title()." - ".t("!relation profile form", array('!relation' => $relation->{$title_label})));
if ( $rTypeID ) {
$form->set( 'mainID' , $mainID );
$form->set( 'rTypeID' , $rTypeID );
$form->set( 'rTypeDir', $rTypeDir );
}
// now set the various defaults
// basically name and email and number of children
$children = _civicrm_event_parent_get_children_info( $mainID, $rTypeID);
$form->set( 'parentChildren', $children );
if ( empty( $children ) ) {
$defaults = civicrm_event_parent_set_default($mainID, $form, 'parent');
$form->setDefaults( $defaults);
}
elseif($uid){
$defaults = civicrm_event_parent_set_default($children[0], $form, 'child');
$form->setDefaults( $defaults );
}
$session->resetScope('profile');
}
}
}
function civicrm_event_parent_civicrm_buildForm_regAdditional( $formName, &$form ) {
$event_id = $form->getVar( '_eventId' );
$civicrm_event_parent_gid = variable_get('civicrm_event_parent_gid', array());
if(!empty($civicrm_event_parent_gid[$event_id]['parent_gid'])){
$relation = CRM_Core_DAO::executeQuery("SELECT * FROM civicrm_relationship_type WHERE id = ".$civicrm_event_parent_gid[$event_id]['parent_gid']);
$relation->fetch();
$children = $form->get( 'parentChildren' );
list($rTypeID, $rTypeDir) = explode('-', $civicrm_event_parent_gid[$event_id]['parent_relation']);
$relation_label = 'label_'.$rTypeDir;
drupal_set_title(drupal_get_title()." - ".t("!relation profile form", array('!relation' => $relation->{$relation_label})));
if ( ! empty( $children ) ) {
$childNum = substr( $form->getVar( '_name' ), 12 );
$defaults = array( );
$defaults['first_name'] = $children[$childNum]['first_name'];
$defaults['last_name' ] = $children[$childNum]['last_name' ];
$defaults['email-5' ] = $children[$childNum]['email' ];
$form->setDefaults( $defaults );
}
}
}
function civicrm_event_parent_civicrm_postProcess( $class, &$form ) {
if ( is_a( $form, 'CRM_Event_Form_Registration_Register' ) || is_a($form, 'CRM_Event_Form_Registration_AdditionalParticipant')) {
if($form->_values['event']['is_monetary'] == 0){
return civicrm_event_parent_civicrm_postProcess_regConfirm( $class, $form );
}
}
if ( is_a( $form, 'CRM_Event_Form_Registration_Confirm' ) ) {
return civicrm_event_parent_civicrm_postProcess_regConfirm( $class, $form );
}
if ( is_a( $form, 'CRM_Profile_Form_Edit' ) ) {
return civicrm_event_parent_civicrm_postProcess_profileEdit( $class, $form );
}
if ( is_a( $form, 'CRM_Event_Form_ManageEvent_Registration') ){
return civicrm_event_parent_civicrm_postProcess_parentProfile( $class, $form );
}
}
function civicrm_event_parent_civicrm_postProcess_regConfirm( $class, &$form ) {
if ( $form->get( 'mainID' ) ) {
$mainID = $form->get( 'mainID' );
$rTypeID = $form->get( 'rTypeID' );
$rTypeDir = $form->get( 'rTypeDir' );
// get the participantIDS
$participantIDs = $form->getVar( '_participantIDS' );
if (!empty($participantIDs)) {
$pIDString = implode( ',', $participantIDs );
// get all the contactIDs for these participants
$sql = "
SELECT contact_id
FROM civicrm_participant
WHERE id IN ( $pIDString )
";
if ( $rTypeDir == 'a_b' ) {
$mainLabel = 'a';
$otherLabel = 'b';
}
else {
$mainLabel = 'b';
$otherLabel = 'a';
}
$dao = CRM_Core_DAO::executeQuery( $sql );
require_once 'api/v2/Relationship.php';
while ( $dao->fetch( ) ) {
// create the relationship
$params = array( "contact_id_{$mainLabel}" => $mainID,
"contact_id_{$otherLabel}" => $dao->contact_id,
'relationship_type_id' => $rTypeID,
'is_active' => 1,
);
civicrm_relationship_create( $params );
}
}
}
}
function civicrm_event_parent_civicrm_postProcess_profileEdit( $class, &$form ) {
// get the contactID of the newly created contact (or existing one)
// and redirect to event registration page
$mainID = $form->getVar( '_id' );
$event_id = CRM_Utils_Request::retrieve( 'event_id', 'Integer', $form, false, null, $_REQUEST);
if ( $mainID && $event_id) {
$url = CRM_Utils_System::url( 'civicrm/event/register', "reset=1&id={$event_id}&cid=0&mainID={$mainID}" );
CRM_Utils_System::redirect( $url );
}
}
function civicrm_event_parent_civicrm_postProcess_parentProfile( $class, &$form ) {
$params = $form->exportValues();
$event_id = $form->getVar( '_id' );
$civicrm_event_parent_gid = variable_get('civicrm_event_parent_gid', array());
if ( $event_id && $params['parent_gid'] && $params['parent_relation']){
$civicrm_event_parent_gid[$event_id] = array(
'parent_gid' => $params['parent_gid'],
'parent_relation' => $params['parent_relation'],
);
variable_set('civicrm_event_parent_gid', $civicrm_event_parent_gid);
}
else{
unset($civicrm_event_parent_gid[$event_id]);
variable_set('civicrm_event_parent_gid', $civicrm_event_parent_gid);
}
}
function civicrm_event_parent_set_default($cid, &$form, $type){
require_once("api/v2/Contact.php");
$fields = array_keys($form->_fields);
$params = array('contact_id' => $cid);
$retrieved = & civicrm_contact_get( $params );
$fields = array_keys($form->_fields);
$phone_types = array();
foreach($fields as $v){
if(strstr($v, 'phone')){
list($p, $t, $phone_type) = explode('-', $v);
$phone_types[$phone_type] = $v;
}
}
if ( !civicrm_error( $retrieved ) ) {
$contact = $retrieved[$cid];
$defaults = array();
if($type == 'child'){
foreach($contact as $k => $v){
if($k == 'phone') {
foreach($phone_types as $ptid => $form_name){
if($ptid == $contact['phone_type_id']){
$defaults[$form_name] = $v;
}
}
}
else{
foreach($fields as $f){
if(strpos($f, $k) === 0){
$defaults[$f] = $v;
}
}
}
}
$defaults['gender'] = $contact['gender_id'];
}
else{
$defaults['postal_code-1'] = $contact['postal_code'];
$defaults['state_province-1'] = $contact['state_province_id'];
$defaults['_city-1'] = $contact['city'];
$defaults['street_address-1'] = $contact['street_address'];
foreach($phone_types as $ptid => $form_name){
if($ptid == $contact['phone_type_id']){
$defaults[$form_name] = $contact['phone'];
}
}
}
return $defaults;
}
}
function _civicrm_event_parent_get_children_info( $contactID, $rid) {
$sql = "
SELECT r.contact_id_a as child_id
FROM civicrm_contact c
INNER JOIN civicrm_relationship r ON c.id = r.contact_id_b
WHERE c.id = %1
AND r.relationship_type_id = ".$rid."
AND r.is_active = 1
";
$params = array( 1 => array( $contactID, 'Integer' ) );
$dao = CRM_Core_DAO::executeQuery( $sql, $params );
$children = array( );
while ( $dao->fetch( ) ) {
$children[] = $dao->child_id;
}
return $children;
}