View source
<?php
module_load_include('inc', 'civicrm_entity_profile', 'includes/civicrm_entity_profile.profile');
function civicrm_entity_profile_menu() {
$items = array();
$items['civicrm_entity_profile_multiple_fields_remove_button/ajax'] = array(
'title' => 'Remove item callback',
'page callback' => 'civicrm_entity_profile_multiple_fields_remove_button_js',
'delivery callback' => 'ajax_deliver',
'access callback' => TRUE,
'theme callback' => 'ajax_base_page_theme',
'type' => MENU_CALLBACK,
);
return $items;
}
function civicrm_entity_profile_multiple_fields_remove_button_js() {
if (isset($_POST['ajax_html_ids'])) {
unset($_POST['ajax_html_ids']);
}
list($form, $form_state) = ajax_get_form();
drupal_process_form($form['#form_id'], $form, $form_state);
$button = $form_state['triggering_element'];
$element = drupal_array_get_nested_value($form, array_slice($button['#array_parents'], 0, -3));
$return = array(
'#type' => 'ajax',
'#commands' => array(
ajax_command_replace('#' . $element['#id'], drupal_render($element)),
),
);
$messages = theme('status_messages');
if ($messages) {
$return['#commands'][] = ajax_command_prepend('#' . $element['#id'], $messages);
}
return $return;
}
function civicrm_entity_profile_civicrm_entity_supported_info(&$civicrm_entity_info) {
$civicrm_entity_info['civicrm_uf_field'] = array(
'civicrm entity name' => 'uf_field',
'label property' => 'label',
'permissions' => array(
'view' => array(),
'edit' => array(),
'update' => array(),
'create' => array(),
'delete' => array(),
),
'theme' => array(
'template' => 'civicrm-uf-field',
'path' => drupal_get_path('module', 'civicrm_entity_profile') . '/templates',
),
'display suite' => array(
'link fields' => array(
array(
'link_field' => 'uf_group_id',
'target' => 'civicrm_uf_group',
),
),
'boolean fields' => array(
'is_active',
'is_view',
'is_required',
'in_selector',
'is_searchable',
'is_reserved',
'is_multi_summary',
),
'option fields' => array(
'location_type_id',
'phone_type_id',
'website_type_id',
'field_type',
),
),
);
$civicrm_entity_info['civicrm_uf_group'] = array(
'civicrm entity name' => 'uf_group',
'label property' => 'title',
'permissions' => array(
'view' => array(),
'edit' => array(),
'update' => array(),
'create' => array(),
'delete' => array(),
),
'theme' => array(
'template' => 'civicrm-uf-group',
'path' => drupal_get_path('module', 'civicrm_entity_profile') . '/templates',
),
'display suite' => array(
'link fields' => array(),
'boolean fields' => array(
'is_active',
'is_map',
'add_captcha',
'is_edit_link',
'is_uf_link',
'is_update_dupe',
'is_reserved',
'is_cms_user',
'is_proximity_search',
),
'date fields' => array(
'created_date',
),
),
);
$civicrm_entity_info['civicrm_uf_join'] = array(
'civicrm entity name' => 'uf_join',
'label property' => 'id',
'permissions' => array(
'view' => array(),
'edit' => array(),
'update' => array(),
'create' => array(),
'delete' => array(),
),
'theme' => array(
'template' => 'civicrm-uf-join',
'path' => drupal_get_path('module', 'civicrm_entity_profile') . '/templates',
),
'display suite' => array(
'link fields' => array(
array(
'link_field' => 'uf_group_id',
'target' => 'civicrm_uf_group',
),
),
'boolean fields' => array(
'is_active',
),
),
);
}
function civicrm_entity_profile_field_info() {
return array(
'civicrm_entity_profile_field' => array(
'label' => t('CiviCRM Entity Profile'),
'description' => t('This field provides a widget selecting profiles for Events and Contribution Pages'),
'settings' => array(
'groups' => array(),
),
'instance_settings' => array(),
'default_widget' => 'civicrm_entity_profile_field_default_widget',
'default_formatter' => 'civicrm_entity_profile_field_default_formatter',
),
);
}
function civicrm_entity_profile_form_field_ui_field_overview_form_alter(&$form, &$form_state) {
$supported_entities = array(
'civicrm_event',
'civicrm_contribution_page',
);
if (!in_array($form['#entity_type'], $supported_entities)) {
unset($form['fields']['_add_new_field']['type']['#options']['civicrm_entity_profile_field']);
foreach ($form['fields']['_add_existing_field']['field_name']['#options'] as $field_name => $description) {
if (strpos($description, 'CiviCRM Entity Profile') !== FALSE) {
unset($form['fields']['_add_existing_field']['field_name']['#options'][$field_name]);
}
}
}
}
function civicrm_entity_profile_field_instance_settings_form($field, $instance) {
if ($instance['entity_type'] == 'civicrm_event') {
$settings = $instance['settings'];
$form['registrant_type'] = array(
'#type' => 'select',
'#multiple' => FALSE,
'#title' => t('Registrant Type'),
'#default_value' => !empty($settings['registrant_type']) ? $settings['registrant_type'] : 'CiviEvent',
'#description' => t('Groups to include for this field '),
'#options' => array(
'CiviEvent' => 'Main Registrant',
'CiviEvent_Additional' => 'Additional Participant',
),
);
return $form;
}
}
function civicrm_entity_profile_field_is_empty($item, $field) {
if (empty($item['uf_group_id'])) {
if (empty($item['target_id'])) {
return TRUE;
}
}
return FALSE;
}
function civicrm_entity_profile_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) {
$supported_entities = array(
'civicrm_event',
'civicrm_contribution_page',
);
if (in_array($entity_type, $supported_entities)) {
foreach ($entities as $id => $entity) {
if ($entity_type == 'civicrm_event') {
$type = !empty($instances[$id]['settings']['registrant_type']) ? $instances[$id]['settings']['registrant_type'] : 'CiviEvent';
}
else {
$type = 'CiviContribute';
}
$uf_joins = _civicrm_entity_profile_get_ufjoins($entity_type, $id, $type);
if (!empty($uf_joins) && count($uf_joins)) {
foreach ($uf_joins as $delta => $uf_join) {
$items[$id][$delta]['target_id'] = $uf_join;
}
}
}
}
}
function civicrm_entity_profile_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) {
if (($entity_type == 'civicrm_event' || $entity_type == 'civicrm_contribution_page') && $instance['widget']['type'] == 'civicrm_entity_profile_field_default_widget') {
if (!empty($items)) {
if (!empty($entity->id)) {
foreach ($items as $delta => $item) {
_civicrm_entity_profile_process_field_item($entity_type, $entity, $instance, $item, $delta);
unset($items[$delta]);
}
}
}
}
}
function civicrm_entity_profile_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) {
if (($entity_type == 'civicrm_event' || $entity_type == 'civicrm_contribution_page') && $instance['widget']['type'] == 'civicrm_entity_profile_field_default_widget') {
if (!empty($items)) {
if (!empty($entity->id)) {
if ($entity_type == 'civicrm_event') {
$module = !empty($instance['settings']['registrant_type']) ? $instance['settings']['registrant_type'] : 'CiviEvent';
}
else {
$module = 'CiviContribute';
}
$uf_join_ids_pre = _civicrm_entity_profile_get_ufjoins($entity_type, $entity->id, $module);
foreach ($uf_join_ids_pre as $uf_join_id) {
$delete = 1;
foreach ($items as $delta => $item) {
if ($item['target_id'] == $uf_join_id) {
$delete = 0;
}
}
if ($delete) {
entity_delete($entity_type, $uf_join_id);
}
}
foreach ($items as $delta => $item) {
_civicrm_entity_profile_process_field_item($entity_type, $entity, $instance, $item, $delta);
unset($items[$delta]);
}
}
}
}
}
function _civicrm_entity_profile_process_field_item($entity_type, $entity, $instance, $item, $delta) {
if ($entity_type == 'civicrm_event') {
$module = !empty($instance['settings']['registrant_type']) ? $instance['settings']['registrant_type'] : 'CiviEvent';
}
else {
$module = 'CiviContribute';
}
if (!empty($item['target_id'])) {
if (!empty($item['uf_group_id'])) {
$uf_join = entity_load_single('civicrm_uf_join', $item['target_id']);
$uf_join->uf_group_id = $item['uf_group_id'];
$uf_join->weight = $delta + 1;
$uf_join->is_active = 1;
$uf_join_wrapper = entity_metadata_wrapper('civicrm_uf_join', $uf_join);
$uf_join_wrapper
->save();
}
else {
entity_delete('civicrm_uf_join', $item['target_id']);
}
}
else {
if (!empty($item['uf_group_id'])) {
$uf_join = new CivicrmEntity(array(
'is_active' => 1,
'module' => $module,
'entity_table' => $entity_type,
'entity_id' => $entity->id,
'weight' => $delta + 1,
'uf_group_id' => $item['uf_group_id'],
), 'civicrm_uf_join');
$uf_join_wrapper = entity_metadata_wrapper('civicrm_uf_join', $uf_join);
$uf_join_wrapper
->save();
}
}
}
function civicrm_entity_profile_field_widget_info() {
return array(
'civicrm_entity_profile_field_default_widget' => array(
'label' => t('Default'),
'field types' => array(
'civicrm_entity_profile_field',
),
'settings' => array(),
'behaviors' => array(
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
'default value' => FIELD_BEHAVIOR_DEFAULT,
),
),
);
}
function civicrm_entity_profile_field_widget_info_alter(&$info) {
$info['inline_entity_form']['field types'][] = 'civicrm_entity_profile_field';
$info['inline_entity_form_single']['field types'][] = 'civicrm_entity_profile_field';
}
function civicrm_entity_profile_inline_entity_form_settings_alter(&$settings, $field, $instance) {
if ($field['type'] == 'civicrm_entity_profile_field') {
$settings['entity_type'] = 'civicrm_uf_join';
$settings['column'] = 'target_id';
$settings['bundles'] = array(
'civicrm_uf_join',
);
}
}
function civicrm_entity_profile_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$orig_element = $element;
$options = _civicrm_entity_profile_get_profiles_for_options_list();
switch ($instance['widget']['type']) {
case 'civicrm_entity_profile_field_default_widget':
$uf_group_id = NULL;
$target_id = NULL;
if (!empty($items[$delta]['target_id'])) {
$target_id = $items[$delta]['target_id'];
$uf_join = entity_load_single('civicrm_uf_join', $target_id);
if (!empty($uf_join->uf_group_id)) {
$uf_group_id = $uf_join->uf_group_id;
}
}
$widget = array();
$widget = $orig_element + array(
'#type' => 'hidden',
'#value' => $target_id,
);
$widget['#weight'] = 0;
$element['target_id'] = $widget;
$widget = array();
$widget = $orig_element + array(
'#type' => 'select',
'#default_value' => $uf_group_id,
'#options' => $options,
);
$widget['#weight'] = 0;
$element['uf_group_id'] = $widget;
break;
}
$field_parents = isset($element['#field_parents']) ? $element['#field_parents'] : array();
$field_name = isset($element['#field_name']) ? $element['#field_name'] : NULL;
$parents = array_merge($field_parents, array(
$field_name,
$langcode,
$delta,
));
if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED) {
$element['remove_button'] = array(
'#delta' => $delta,
'#name' => implode('_', $parents) . '_remove_button',
'#type' => 'submit',
'#value' => t('Remove'),
'#validate' => array(),
'#attributes' => array(
'class' => array(
'multiple-fields-remove-button',
),
),
'#submit' => array(
'_civicrm_entity_profile_remove_button_submit_handler',
),
'#limit_validation_errors' => array(),
'#ajax' => array(
'path' => 'civicrm_entity_profile_multiple_fields_remove_button/ajax',
'effect' => 'fade',
),
'#weight' => 1000,
);
}
return $element;
}
function civicrm_entity_profile_field_formatter_info() {
return array(
'civicrm_entity_profile_field_default_formatter' => array(
'label' => t('List Profiles'),
'field types' => array(
'civicrm_entity_profile_field',
),
'settings' => array(
'link_to_profile' => 0,
),
),
);
}
function civicrm_entity_profile_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
if ($field['type'] == 'civicrm_entity_profile_field') {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$element = array();
$element['link_to_profile'] = array(
'#type' => 'checkbox',
'#title' => t('Output as links to civicrm_uf_group entities'),
'#default_value' => isset($settings['link_to_profile']) ? $settings['link_to_profile'] : 0,
'#return_value' => 1,
);
return $element;
}
}
function civicrm_entity_profile_field_formatter_settings_summary($field, $instance, $view_mode) {
if ($field['type'] == 'civicrm_entity_profile_field') {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$summary = '';
if ($display['type'] == 'civicrm_entity_profile_field_default_formatter') {
$summary = t('Link to profile: @data', array(
'@data' => !empty($settings['link_to_profile']) ? 'Yes' : 'No',
));
}
return $summary;
}
}
function civicrm_entity_profile_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
$settings = $display['settings'];
switch ($display['type']) {
case 'civicrm_entity_profile_field_default_formatter':
$output_as_link = !empty($settings['link_to_profile']) ? 1 : 0;
foreach ($items as $delta => $item) {
if (!empty($item['target_id'])) {
$uf_join = entity_load_single('civicrm_uf_join', $item['target_id']);
$uf_group = entity_load_single('civicrm_uf_group', $uf_join->uf_group_id);
if ($output_as_link) {
$value = l($uf_group->title, 'civicrm-uf-group/' . $uf_group->id);
}
else {
$value = $uf_group->title;
}
$element[$delta] = array(
'#markup' => $value,
);
}
}
break;
}
return $element;
}
function civicrm_entity_profile_civicrm_post($op, $objectName, $objectId, &$objectRef) {
if (($objectName == 'Event' || $objectName == 'ContributionPage') && ($op == 'create' || $op == 'delete' || $op == 'edit')) {
if ($objectName == 'Event') {
$entity_type = 'civicrm_event';
}
elseif ($objectName == 'ContributionPage') {
$entity_type = 'civicrm_contribution_page';
}
$query = db_select('field_config', 'fc');
$query
->join('field_config_instance', 'fci');
$civicrm_entity_profile_fields = $query
->fields('fc', array(
'id',
))
->condition('fc.type', 'civicrm_entity_profile_field')
->condition('fci.entity_type', $entity_type)
->execute();
if ($civicrm_entity_profile_fields
->rowCount()) {
cache_clear_all('field:' . $entity_type . ':' . $objectRef->id, 'cache_field');
}
}
}
function _civicrm_entity_profile_remove_button_submit_handler($form, &$form_state) {
$button = $form_state['triggering_element'];
$delta = $button['#delta'];
$inpt = 'input';
$address = array_slice($button['#array_parents'], 0, -2);
$parent_element = drupal_array_get_nested_value($form, $address);
$field_name = $parent_element['#field_name'];
$langcode = $parent_element['#language'];
$parents = $parent_element['#field_parents'];
$field_state = field_form_get_state($parents, $field_name, $langcode, $form_state);
for ($i = $delta; $i <= $field_state['items_count']; $i++) {
$old_element_address = array_merge($address, array(
$i + 1,
));
$new_element_address = array_merge($address, array(
$i,
));
$moving_element = drupal_array_get_nested_value($form, $old_element_address);
$moving_element_value = drupal_array_get_nested_value($form_state['values'], $old_element_address);
$moving_element_input = drupal_array_get_nested_value($form_state[$inpt], $old_element_address);
$moving_element['#parents'] = $new_element_address;
form_set_value($moving_element, $moving_element_value, $form_state);
drupal_array_set_nested_value($form_state[$inpt], $moving_element['#parents'], $moving_element_input);
if (isset($field_state['entity'][$i + 1])) {
$field_state['entity'][$i] = $field_state['entity'][$i + 1];
}
else {
unset($field_state['entity'][$i]);
}
}
if ($field_state['items_count'] > 0) {
$field_state['items_count']--;
}
$input = drupal_array_get_nested_value($form_state[$inpt], $address);
uasort($input, '_field_sort_items_helper');
$weight = -1 * $field_state['items_count'];
foreach ($input as $key => $item) {
if ($item) {
$input[$key]['_weight'] = $weight++;
}
}
drupal_array_set_nested_value($form_state[$inpt], $address, $input);
field_form_set_state($parents, $field_name, $langcode, $form_state, $field_state);
$form_state['rebuild'] = TRUE;
}
function civicrm_entity_profile_form_civicrm_entity_admin_settings_form_alter(&$form, &$form_state) {
$required_entities = array(
'civicrm_event' => 'Event',
'civicrm_uf_field' => 'UF Field',
'civicrm_uf_group' => 'UF Group',
'civicrm_uf_join' => 'UF Join',
);
foreach ($required_entities as $drupal_entity_type => $entity_type_label) {
if (empty($selected_entities[$drupal_entity_type])) {
$form['civicrm_entity_admin_enabled_entities'][$drupal_entity_type]['#disabled'] = TRUE;
$form['civicrm_entity_admin_enabled_entities'][$drupal_entity_type]['#default_value'] = 1;
}
}
$form['#validate'][] = 'civicrm_entity_profile_admin_settings_form_validate';
}
function civicrm_entity_profile_admin_settings_form_validate($form, &$form_state) {
$selected_entities = $form_state['values']['civicrm_entity_admin_enabled_entities'];
$required_entities = array(
'civicrm_event' => 'Event',
'civicrm_uf_field' => 'UF Field',
'civicrm_uf_group' => 'UF Group',
'civicrm_uf_join' => 'UF Join',
);
foreach ($required_entities as $drupal_entity_type => $entity_type_label) {
if (empty($selected_entities[$drupal_entity_type])) {
form_set_error('civicrm_entity_admin_enabled_entities][' . $drupal_entity_type, $entity_type_label . ' required by CiviCRM Entity Profile Field module');
}
}
}