You are here

function crm_core_relationship_form in CRM Core 7

Same name and namespace in other branches
  1. 8.3 modules/crm_core_relationship_ui/crm_core_relationship_ui.pages.inc \crm_core_relationship_form()
  2. 8 modules/crm_core_relationship_ui/crm_core_relationship_ui.pages.inc \crm_core_relationship_form()
  3. 8.2 modules/crm_core_relationship_ui/crm_core_relationship_ui.pages.inc \crm_core_relationship_form()

Form builder for CRM Core Activity forms.

File

modules/crm_core_relationship_ui/crm_core_relationship_ui.pages.inc, line 51

Code

function crm_core_relationship_form($form, &$form_state, $relation) {

  // Ensure this include file is loaded when the form is rebuilt from the cache.
  $form_state['build_info']['file'] = drupal_get_path('module', 'crm_core_ui') . '/pages/contact_activity.pages.inc';
  $form_state['entity_type'] = 'relation';
  $form_state['relation'] = $form_state['crm_core_relationship'];
  unset($form_state['crm_core_relationship']);
  if (isset($relation->crm_core_contact)) {

    // get the contact id of who is trying to add the activity
    $form['contact_id'] = array(
      '#type' => 'value',
      '#value' => arg(2),
    );
  }
  module_load_include('inc', 'crm_core_relationship_ui');
  $form = array();
  $form['relationship_type'] = array(
    '#type' => 'value',
    '#value' => $relation,
  );
  $form['reverse'] = array(
    '#type' => 'value',
    '#value' => $relation->reverse,
  );
  $form['relationship_type_label'] = array(
    '#type' => 'item',
    '#title' => t('Relationship type'),
    '#markup' => $relation->label,
  );
  foreach (array(
    0,
    1,
  ) as $reverse_field) {
    $contact_type_names = array();
    foreach (crm_core_relationship_load_contact_types($relation, $reverse_field) as $contact_type) {
      $contact_type_names[] = t($contact_type->name);
    }
    $contact_type_names = implode(', ', $contact_type_names);
    if (isset($relation->crm_core_contact)) {
      $form[$reverse_field ? 'destination_contact' : 'source_contact'] = array(
        '#title' => $reverse_field ? t('Destination contact name') : t('Source contact name'),
        '#description' => t('Following contact types allowed: !types.', array(
          '!types' => $contact_type_names,
        )),
        '#type' => 'textfield',
        '#default_value' => !($reverse_field ^ $relation->reverse) ? crm_core_contact_title($relation->crm_core_contact) . " [cid:{$relation->crm_core_contact->contact_id}]" : '',
        '#disabled' => !($reverse_field ^ $relation->reverse),
        '#required' => TRUE,
        '#autocomplete_path' => 'crm-core/contact/' . $relation->crm_core_contact->contact_id . '/relationships/add/' . $relation->relation_type . '/' . $reverse_field . '/autocomplete',
      );
    }
    else {
      $form[$reverse_field ? 'destination_contact' : 'source_contact'] = array(
        '#title' => $reverse_field ? t('Destination contact name') : t('Source contact name'),
        '#description' => t('Following contact types allowed: !types.', array(
          '!types' => $contact_type_names,
        )),
        '#type' => 'textfield',
        '#default_value' => '',
        '#required' => TRUE,
        '#autocomplete_path' => 'crm-core/relationships/' . $relation->relation_type . '/' . $reverse_field . '/autocomplete',
      );
    }
  }
  $form['uid'] = array(
    '#type' => 'value',
    '#value' => $relation->uid,
  );
  field_attach_form('relation', $relation, $form, $form_state);
  unset($form['endpoints']);
  $form['actions'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'class' => array(
        'form-actions',
      ),
    ),
    '#weight' => 40,
  );

  // We add the form's #submit array to this button along with the actual submit
  // handler to preserve any submit handlers added by a form callback_wrapper.
  $submit = array();
  if (!empty($form['#submit'])) {
    $submit += $form['#submit'];
  }
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save Relationship'),
    '#submit' => $submit + array(
      'crm_core_relationship_ui_add_relationship_form_submit',
    ),
  );

  // Show Delete button if we edit activity.
  if ($form_state['op'] == 'edit') {
    $form['actions']['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete'),
      '#submit' => array(
        'crm_core_relationship_ui_add_relationship_form_delete',
      ),
    );
  }

  // We append the validate handler to #validate in case a form callback_wrapper
  // is used to add validate handlers earlier.
  $form['#validate'][] = 'crm_core_relationship_ui_add_relationship_form_validate';

  // Add after build to set proper page Title as it
  // got set by entity_ui_main_form_defaults
  // @see entity_ui_get_form().
  $form['#after_build'][] = 'crm_core_relationship_form_after_build';
  return $form;
}