| 1 |
<?php
|
| 2 |
|
| 3 |
|
| 4 |
/*
|
| 5 |
function connect_action_civicrm_insert(&$parent, &$participant) {
|
| 6 |
// duh.
|
| 7 |
if (!module_exists('civicrm')) {
|
| 8 |
return false;
|
| 9 |
}
|
| 10 |
|
| 11 |
civicrm_initialize(true);
|
| 12 |
$required = array(); // holds params to test for existing contact
|
| 13 |
$crm_params = array(); // holds all params to create/update contact
|
| 14 |
$translate = variable_get('connect_civicrm_map_'.connect_nid(), array());
|
| 15 |
$crm_test = variable_get('connect_civicrm_test_'.connect_nid(), array());
|
| 16 |
|
| 17 |
// TODO: include data types in the map to insert non-text info
|
| 18 |
|
| 19 |
// grab data from CCK node
|
| 20 |
foreach ($participant as $field=>$value) {
|
| 21 |
$field_keys = connect_get_field_keys($field);
|
| 22 |
foreach ($field_keys as $key) {
|
| 23 |
if (!empty($value[0][$key]) && isset($translate[$field][$key])) {
|
| 24 |
$crm_field = $translate[$field][$key];
|
| 25 |
$crm_params[$crm_field] = $value[0][$key];
|
| 26 |
|
| 27 |
// keep fields to test for existence
|
| 28 |
if ( in_array( $crm_field, $crm_test) ) {
|
| 29 |
$required[$crm_field] = $value[0][$key];
|
| 30 |
}
|
| 31 |
}
|
| 32 |
}
|
| 33 |
}
|
| 34 |
|
| 35 |
// update or create contact
|
| 36 |
if ( !empty($crm_params) && !empty($required) ) {
|
| 37 |
$get_contact = crm_get_contact($required);
|
| 38 |
if ( get_class($get_contact) == 'CRM_Contact_BAO_Contact' ) {
|
| 39 |
$contact = crm_update_contact($get_contact, $crm_params);
|
| 40 |
} else {
|
| 41 |
$contact = crm_create_contact($crm_params, 'Individual');
|
| 42 |
}
|
| 43 |
|
| 44 |
// group membership
|
| 45 |
if (get_class($contact) == 'CRM_Contact_BAO_Contact') {
|
| 46 |
|
| 47 |
// save contact ID in cck record
|
| 48 |
$participant->field_civicrm_id[0]['value'] = $contact->id;
|
| 49 |
node_save($participant);
|
| 50 |
|
| 51 |
// determine group name
|
| 52 |
$group = false;
|
| 53 |
if (isset($parent->og_groups)) { // if there's an OG, use that name
|
| 54 |
$og = node_load($parent->og_groups[0]);
|
| 55 |
$group = $og->title;
|
| 56 |
} else { // otherwise use name of parent node
|
| 57 |
$group = $parent->title;
|
| 58 |
}
|
| 59 |
|
| 60 |
// find or create group
|
| 61 |
$groups = crm_get_groups(array('title' => $group));
|
| 62 |
if ( isset($groups[0]) ) {
|
| 63 |
$group_obj = $groups[0];
|
| 64 |
} else {
|
| 65 |
$group_obj = crm_create_group( array('title' => $group, 'is_active' => '1') );
|
| 66 |
}
|
| 67 |
|
| 68 |
// add contact to group
|
| 69 |
$to_add[] = $contact;
|
| 70 |
$result = crm_add_group_contacts($group_obj, $to_add, $status = 'Added', $method = 'Admin');
|
| 71 |
if ( $result !== null ) {
|
| 72 |
watchdog('debug', 'connect: user not added to group: '.$participant->nid);
|
| 73 |
}
|
| 74 |
} else {
|
| 75 |
watchdog('debug', 'connect: CiviCRM record not created: '.$participant->nid);
|
| 76 |
}
|
| 77 |
} else {
|
| 78 |
watchdog('debug', 'connect: CiviCRM params empty: '.$participant->nid);
|
| 79 |
}
|
| 80 |
}
|
| 81 |
|
| 82 |
function connect_action_civicrm_update(&$participant) {
|
| 83 |
if (module_exists('civicrm') && isset($participant->field_civicrm_id[0]['value'])) {
|
| 84 |
civicrm_initialize(true);
|
| 85 |
|
| 86 |
$crm_params = array(); // holds all params to create/update contact
|
| 87 |
$translate = variable_get('connect_civicrm_map_'.$participant->field_parent_id[0]['value'], array());
|
| 88 |
|
| 89 |
// TODO: include data types in the map to insert non-text info
|
| 90 |
|
| 91 |
// grab data from CCK node
|
| 92 |
foreach ($participant as $field=>$value) {
|
| 93 |
$field_keys = connect_get_field_keys($field);
|
| 94 |
foreach ($field_keys as $key) {
|
| 95 |
if (!empty($value[0][$key]) && isset($translate[$field][$key])) {
|
| 96 |
$crm_field = $translate[$field][$key];
|
| 97 |
$crm_params[$crm_field] = $value[0][$key];
|
| 98 |
}
|
| 99 |
}
|
| 100 |
}
|
| 101 |
|
| 102 |
// update contact
|
| 103 |
$crm_id = $participant->field_civicrm_id[0]['value'];
|
| 104 |
if ( !empty($crm_params) ) {
|
| 105 |
$get_contact = crm_get_contact(array('contact_id' => $crm_id));
|
| 106 |
if (is_a($get_contact, 'CRM_Contact_BAO_Contact')) {
|
| 107 |
$contact = crm_update_contact($get_contact, $crm_params);
|
| 108 |
return true;
|
| 109 |
}
|
| 110 |
}
|
| 111 |
}
|
| 112 |
// errors fall through
|
| 113 |
watchdog('error','Connect: civicrm update failed: ' . $participant->nid);
|
| 114 |
}
|
| 115 |
*/
|
| 116 |
|
| 117 |
/*
|
| 118 |
function connect_civicrm_map_form() {
|
| 119 |
//add a form element that can map cck fields -> civicrm fields
|
| 120 |
$form['connect_civicrm_mapping']['intro'] = array(
|
| 121 |
'#type' => 'markup',
|
| 122 |
'#value' => '<p>' . t('Select the CiviCRM fields that correspond to the participant data.') . '</p>',
|
| 123 |
);
|
| 124 |
|
| 125 |
//only get the fields for this node's participant type!
|
| 126 |
$nid = arg(1);
|
| 127 |
$node = node_load($nid);
|
| 128 |
$cck_info = _content_type_info();
|
| 129 |
$cck_fields = $cck_info['content types'][$node->field_participant_type[0]['value']]['fields'];
|
| 130 |
$civicrm_fields = connect_civicrm_properties_options();
|
| 131 |
$cck2crm_map = variable_get('connect_civicrm_map_'.$nid, array());
|
| 132 |
|
| 133 |
// map cck -> civicrm
|
| 134 |
foreach ($cck_fields as $cck_field) {
|
| 135 |
$cck_name = $cck_field['field_name'];
|
| 136 |
$cck_keys = connect_get_field_keys($cck_name);
|
| 137 |
foreach ($cck_keys as $key) {
|
| 138 |
$default = isset($cck2crm_map[$cck_name][$key]) ? $cck2crm_map[$cck_name][$key] : '';
|
| 139 |
$form['connect_civicrm_mapping']['connect_civicrm_map_'.$cck_name.'+'.$key] = array (
|
| 140 |
'#type' => 'select',
|
| 141 |
'#title' => t($cck_field['widget']['label']. ' ('.$key.')' ),
|
| 142 |
'#options' => $civicrm_fields,
|
| 143 |
'#default_value' => $default,
|
| 144 |
'#prefix' => '<div class="container-inline">',
|
| 145 |
'#suffix' => '</div>',
|
| 146 |
);
|
| 147 |
// keep extant mapping for options in the next form item
|
| 148 |
if ($default) {
|
| 149 |
$test_options[$default] = $civicrm_fields[$default];
|
| 150 |
}
|
| 151 |
}
|
| 152 |
|
| 153 |
// define civicrm fields to test for existing contacts
|
| 154 |
if (!empty($test_options)){
|
| 155 |
$form['connect_civicrm_mapping']['intro2'] = array(
|
| 156 |
'#type' => 'markup',
|
| 157 |
'#value' => '<div><p style="padding-top:1em">' . t('You must select at least one field that will be used by CiviCRM to determine if a contact already exists. Good choices include email address or first and last name.') . '</p></div>',
|
| 158 |
'#weight' => '20',
|
| 159 |
);
|
| 160 |
|
| 161 |
$form['connect_civicrm_mapping']['connect_civicrm_test'] = array (
|
| 162 |
'#type' => 'checkboxes',
|
| 163 |
//'#required' => TRUE,
|
| 164 |
'#title' => t('Choose the CiviCRM elements used to uniquely identify contacts.'),
|
| 165 |
'#options' => $test_options,
|
| 166 |
'#default_value' => variable_get('connect_civicrm_test_'.$nid, array()),
|
| 167 |
'#weight' => '21',
|
| 168 |
);
|
| 169 |
}
|
| 170 |
|
| 171 |
$form['submit'] = array(
|
| 172 |
'#type' => 'submit',
|
| 173 |
'#value' => t('Submit'),
|
| 174 |
'#weight' => '22',
|
| 175 |
);
|
| 176 |
}
|
| 177 |
return $form;
|
| 178 |
}
|
| 179 |
|
| 180 |
// TODO
|
| 181 |
function connect_civicrm_map_form_validate($form_id, $form_values) {
|
| 182 |
|
| 183 |
// validate against cck and civicrm fields
|
| 184 |
$civicrm_fields = connect_civicrm_properties_options();
|
| 185 |
$cck_data = content_fields(NULL, $node->participant_type[0]['value']);
|
| 186 |
foreach ($cck_data as $cck) {
|
| 187 |
$cck_fields[] = $cck['field_name'];
|
| 188 |
}
|
| 189 |
|
| 190 |
$form_keys = array_keys($form_values);
|
| 191 |
foreach ($form_keys as $form_key) {
|
| 192 |
if ( strpos( $form_key, 'connect_civicrm_' ) !== false ) {
|
| 193 |
if ( ! empty($form_values[$form_key]) ) {
|
| 194 |
$cck_field = str_replace( 'connect_civicrm_', '', $form_key );
|
| 195 |
if ( !in_array( $cck_field, $cck_fields) || !in_array($form_values[$form_key], $civicrm_fields) ) {
|
| 196 |
// error
|
| 197 |
}
|
| 198 |
}
|
| 199 |
}
|
| 200 |
}
|
| 201 |
return true;
|
| 202 |
}
|
| 203 |
|
| 204 |
function connect_civicrm_map_form_submit($form_id, $form_values) {
|
| 205 |
$nid = arg(1);
|
| 206 |
|
| 207 |
// save data mapping
|
| 208 |
$data = array();
|
| 209 |
$form_keys = array_keys($form_values);
|
| 210 |
foreach ($form_keys as $form_key) {
|
| 211 |
if ( strpos( $form_key, 'connect_civicrm_map_' ) !== false ) {
|
| 212 |
if ( ! empty($form_values[$form_key]) ) {
|
| 213 |
// determine name, key
|
| 214 |
$cck_field = str_replace( 'connect_civicrm_map_', '', $form_key );
|
| 215 |
$cck_keyname = strrchr( $cck_field, '+');
|
| 216 |
$cck_fieldname = str_replace( $cck_keyname, '', $cck_field );
|
| 217 |
$cck_keyname = str_replace( '+', '', $cck_keyname );
|
| 218 |
$data[$cck_fieldname][$cck_keyname] = $form_values[$form_key];
|
| 219 |
}
|
| 220 |
}
|
| 221 |
}
|
| 222 |
variable_set('connect_civicrm_map_'.$nid, $data);
|
| 223 |
|
| 224 |
// save civicrm contact test info
|
| 225 |
if (isset($form_values['connect_civicrm_test'])) {
|
| 226 |
$data = array();
|
| 227 |
$civicrm_fields = connect_civicrm_properties_options();
|
| 228 |
$civicrm_names = array_flip($civicrm_fields);
|
| 229 |
foreach ($form_values['connect_civicrm_test'] as $key=>$value) {
|
| 230 |
if ($key === $value) {
|
| 231 |
$data[] = $value;
|
| 232 |
}
|
| 233 |
}
|
| 234 |
variable_set('connect_civicrm_test_'.$nid, $data);
|
| 235 |
}
|
| 236 |
|
| 237 |
drupal_set_message(t('The CiviCRM data mapping has been updated.'));
|
| 238 |
}
|
| 239 |
*/
|
| 240 |
|
| 241 |
|
| 242 |
/**
|
| 243 |
* allows designated users to use the parent node form as a data entry mechanism
|
| 244 |
*/
|
| 245 |
/*
|
| 246 |
function connect_action_data_entry($op='', &$parent=NULL, &$child=NULL, $target='child') {
|
| 247 |
return;
|
| 248 |
drupal_set_message(t('Participant entered. Enter another?'));
|
| 249 |
connect_session_status_set( $node->nid, '');
|
| 250 |
// fall through to display form
|
| 251 |
}
|
| 252 |
*/
|