You are here

function redhen_contact_contact_user_form in RedHen CRM 7

Return a form for managing contact/user links.

_state

Parameters

$form:

Return value

array

1 string reference to 'redhen_contact_contact_user_form'
redhen_contact_menu in modules/redhen_contact/redhen_contact.module
Implements hook_menu().

File

modules/redhen_contact/includes/redhen_contact.forms.inc, line 318
Forms for creating, editing, and deleting contacts.

Code

function redhen_contact_contact_user_form($form, &$form_state, RedhenContact $contact) {
  $form_state['redhen_contact'] = $contact;
  $form['#attributes'] = array(
    'id' => 'redhen-contact-user-form',
  );
  $wrapper = entity_metadata_wrapper('redhen_contact', $contact);
  $user = $wrapper->user
    ->value();
  if ($user) {
    $user_uri = entity_uri('user', $user);
    $form['existing_user'] = array(
      '#type' => 'container',
      array(
        'message' => array(
          '#markup' => t('!user is currently linked with %contact.', array(
            '!user' => l($user->name, $user_uri['path']),
            '%contact' => $contact
              ->label(),
          )),
        ),
        'user' => user_view($user),
      ),
      array(
        'user_actions' => array(
          '#type' => 'actions',
          array(
            'unlink' => array(
              '#type' => 'submit',
              '#value' => t('Unlink'),
              '#submit' => array(
                'redhen_contact_unlink_user_submit',
              ),
            ),
            'delete' => array(
              '#type' => 'submit',
              '#value' => t('Delete'),
              '#submit' => array(
                'redhen_contact_unlink_user_submit',
              ),
            ),
          ),
        ),
      ),
    );
  }
  else {
    $form['link_user'] = array(
      '#title' => t('Link a Drupal user'),
      '#type' => 'select',
      '#options' => array(
        'existing' => t('Choose existing'),
        'new' => t('Create new'),
      ),
      '#description' => t('Link a new or existing Drupal user to this contact'),
      '#ajax' => array(
        'callback' => 'redhen_contact_select_user_callback',
        'wrapper' => 'redhen-contact-user-form',
      ),
      '#weight' => -100,
      '#required' => TRUE,
    );
    if (isset($form_state['values']['link_user']) && !empty($form_state['values']['link_user'])) {
      switch ($form_state['values']['link_user']) {
        case 'existing':

          // Get default value based on email:
          $desc = t('Start typing to select a Drupal user to link to this contact.');
          foreach ($contact
            ->allEmail() as $email) {
            if ($user = user_load_by_mail($email)) {
              $default = $user->name;
              $desc = t('A match has been found for user %name by matching on the contact email address. It can be overriden by entering a different value.', array(
                '%name' => $user->name,
              ));
              break;
            }
          }
          $form['existing'] = array(
            '#title' => t('Existing'),
            '#type' => 'textfield',
            '#description' => $desc,
            '#default_value' => $default,
            '#autocomplete_path' => 'user/autocomplete',
            '#required' => TRUE,
          );
          break;
        case 'new':

          // Use the core user registration form, with some adjustments.
          $form = user_register_form($form, $form_state);

          // Default value for email field:
          $form['account']['mail']['#default_value'] = $contact
            ->email();
          $form['account']['name']['#default_value'] = $contact
            ->label();
          $form['#submit'][] = 'redhen_contact_contact_user_form_submit';
      }
    }
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Link user'),
    );
  }
  return $form;
}