You are here

function crm_core_user_sync_edit_relation_form_submit in CRM Core 8.2

Same name and namespace in other branches
  1. 7 modules/crm_core_user_sync/crm_core_user_sync.admin.inc \crm_core_user_sync_edit_relation_form_submit()

Submit callback on edit form.

File

modules/crm_core_user_sync/crm_core_user_sync.admin.inc, line 424

Code

function crm_core_user_sync_edit_relation_form_submit($form, &$form_state) {
  if ($form_state['values']['op'] == $form['actions']['save']['#value']) {
    $user = _crm_core_user_sync_get_entity_id_from_text($form_state['values']['user'], 'user');
    $contact = _crm_core_user_sync_get_entity_id_from_text($form_state['values']['contact'], 'crm_core_contact');
    if (!empty($form_state['values']['relation'])) {
      $relation = $form_state['values']['relation'];
      $relation->endpoints[LANGUAGE_NONE][0]['entity_id'] = $user->uid;
      $relation->endpoints[LANGUAGE_NONE][1]['entity_id'] = $contact->contact_id;
      $relation
        ->save();
    }
    else {
      $relation = Relation::create(array(
        'relation_type' => 'crm_core_user_sync',
        array(
          array(
            'entity_type' => 'user',
            'entity_id' => $user->uid,
          ),
          array(
            'entity_type' => 'crm_core_contact',
            'entity_id' => $contact->contact_id,
          ),
        ),
      ));
      $relation
        ->save();
    }
    $form_state['redirect'] = 'admin/config/crm-core/user-sync/contact-to-user-management';
    drupal_set_message('Relation has been saved.');
  }
  elseif ($form_state['values']['op'] == $form['actions']['delete']['#value']) {
    $relation = $form_state['values']['relation'];
    $form_state['redirect'] = 'admin/config/crm-core/user-sync/contact-to-user-management/' . $relation
      ->id() . '/delete';
  }
}