You are here

function civicrm_entity_form in CiviCRM Entity 7.2

Same name and namespace in other branches
  1. 7 civicrm_entity.module \civicrm_entity_form()
1 string reference to 'civicrm_entity_form'
CivicrmEntityUIController::hook_forms in ./civicrm_entity_ui_controller.inc
Always use the same civicrm_entity_form

File

./civicrm_entity.default_form.inc, line 3

Code

function civicrm_entity_form($form, &$form_state, $entity, $op, $entity_type) {
  if ($op == 'edit' || $op == 'delete') {
    if (!is_object($entity)) {
      drupal_set_title(t('No ' . $entity_type . ' with id:' . $entity));
      return $form;
    }
    $form_state['entity'] = $entity;
    $form_state['entity_type'] = $entity_type;
    $form['#entity'] = $entity;
  }
  if ($op == 'create') {
    $entity = new CiviCRMEntity(array(), $entity_type);
    $entity->is_new = TRUE;
    $entity->type = $entity_type;
    $form_state['entity'] = $entity;
    $form_state['entity_type'] = $entity_type;
    $form['#entity'] = $entity;
  }

  // Add the field related form elements.
  if ($op == 'edit' || $op == 'create') {
    if ($op == 'edit') {
      $title = 'Edit ' . ucwords(str_replace("_", " ", $entity_type)) . ": " . entity_label($entity_type, $entity) . " (id:" . $entity->id . ")";
    }
    else {
      $title = 'Add ' . ucwords(str_replace("_", " ", $entity_type));
    }
    field_attach_form($entity_type, $entity, $form, $form_state);
    if ($op == 'edit') {
      if (is_object($entity)) {
        $entity->original = clone $entity;
      }
    }

    //get entity controller build content
    if ($op == 'edit') {
      $wrapper = entity_metadata_wrapper($entity_type, $entity);
    }
    else {
      $wrapper = entity_metadata_wrapper($entity_type);
    }
    foreach ($wrapper as $name => $child) {
      $info = $child
        ->info();
      if (strpos($info['name'], 'field_') === 0) {
        continue;
      }
      else {
        if (isset($info['widget']['widget'])) {
          $form[$name] = array(
            '#type' => $info['widget']['widget'],
            '#title' => $info['label'],
            '#description' => !empty($info['description']) ? $info['description'] : '',
          );

          // set api required fields to be required on form
          if (!empty($info['required'])) {
            $form[$name]['#required'] = TRUE;
          }
          if ($form[$name]['#type'] == 'civi_fk_reference') {
            switch ($info['widget']['subtype']) {
              case 'autocomplete':
                $form[$name]['#type'] = 'textfield';
                $form[$name]['#autocomplete_path'] = 'civicrm-entity/autocomplete/' . $info['widget']['entity'];
                break;
              case 'select':
                $form[$name]['#type'] = 'select';
                $form[$name]['#options'] = !empty($entity_form[$name]['#required']) ? _civicrm_entity_get_entities_list($info['widget']['entity']) : array(
                  NULL => 'None',
                ) + _civicrm_entity_get_entities_list($info['widget']['entity']);

                /*if ($info['widget']['entity'] == 'Event') {
                    array_shift($form[$name]['#options']);
                  }*/
                break;
              default:
                $form[$name]['#type'] = 'textfield';
                break;
            }
          }

          // Handle default value
          if ($op == 'edit') {
            $form[$name]['#default_value'] = $child
              ->value();
            if ($entity_type == 'civicrm_participant') {
              if ($name == 'status_id' && !empty($entity->participant_status_id)) {
                $form[$name]['#default_value'] = $entity->participant_status_id;
              }
              if ($name == 'role_id' && !empty($entity->participant_role_id)) {
                $form[$name]['#default_value'] = $entity->participant_role_id;
              }
              if ($name == 'source' && !empty($entity->participant_source)) {
                $form[$name]['#default_value'] = $entity->participant_source;
              }
              if ($name == 'register_date' && !empty($entity->participant_register_date)) {
                $form[$name]['#default_value'] = $entity->participant_register_date;
              }
              if ($name == 'registered_by_id' && !empty($entity->participant_registered_by_id)) {
                $form[$name]['#default_value'] = $entity->participant_registered_by_id;
              }
              if ($name == 'fee_amount' && !empty($entity->participant_fee_amount)) {
                $form[$name]['#default_value'] = $entity->participant_fee_amount;
              }
            }
          }
          elseif ($op == 'create') {
            if (isset($info['default_value'])) {
              $form[$name]['#default_value'] = $info['default_value'];
            }
            else {
              $form[$name]['#default_value'] = NULL;
            }
          }

          // set the #size and #maxlength FAPI properties if the #type is a textfield
          if ($form[$name]['#type'] == 'textfield') {
            if (!empty($info['size'])) {
              $form[$name]['#size'] = $info['size'];
            }
            if (!empty($info['maxlength'])) {
              $form[$name]['#maxlength'] = $info['maxlength'];
            }
          }

          //set rows and cols if available for textarea or text_format
          if ($form[$name]['#type'] == 'textarea' || $form[$name]['#type'] == 'text_format') {
            if ($form[$name]['#type'] == 'text_format') {
              $form[$name]['#format'] = 'full_html';
            }

            // set rows value if not empty
            if (!empty($info['rows'])) {
              $form[$name]['#rows'] = $info['rows'];
            }
            if (!empty($info['cols'])) {
              $form[$name]['#cols'] = $info['cols'];
            }
          }

          //set the options for select options, radios, and checkboxes
          if ($form[$name]['#type'] == 'select') {
            if (!empty($info['widget']['options'])) {
              $form[$name]['#options'] = $info['widget']['options'];
              if (!empty($info['required'])) {
                if (isset($form[$name]['#options'][''])) {
                  unset($form[$name]['#options']['']);
                }
              }
            }
          }
          elseif ($form[$name]['#type'] == 'radios') {
            $form[$name]['#options'] = $info['widget']['options'];
            if (!empty($info['required'])) {
              if (isset($form[$name]['#options'][''])) {
                unset($form[$name]['#options']['']);
              }
            }
          }
          elseif ($form[$name]['#type'] == 'checkboxes') {
            if ($op == 'edit') {
              $value = $child
                ->value();
              if (!empty($value)) {
                foreach ($info['widget']['options'] as $key => $label) {
                  if (in_array($key, $value)) {
                    $default_values[] = $key;
                  }
                }
                $form[$name]['#default_value'] = $default_values;
              }
              else {
                $form[$name]['#default_value'] = array(
                  '',
                );
              }
            }
            elseif ($op == 'create') {
              if (isset($info['default_value'])) {
                $form[$name]['#default_value'] = explode(CRM_Core_DAO::VALUE_SEPARATOR, $form[$name]['#default_value']);
              }
            }
            $form[$name]['#options'] = $info['widget']['options'];
            if (!empty($info['required'])) {
              if (isset($form[$name]['#options'][''])) {
                unset($form[$name]['#options']['']);
              }
            }
          }
          elseif ($form[$name]['#type'] == 'date_select') {
            if (module_exists('date_api')) {
              if (module_exists('date_popup')) {
                $form[$name]['#type'] = 'date_popup';
              }
              $form[$name]['#format'] = $info['widget']['format'];
              $form[$name]['#date_year_range'] = '-115:+10';
            }
            else {
              $form[$name]['#type'] = 'textfield';
            }
          }

          // contact special handling
          if ($entity_type == 'civicrm_contact') {

            //for some reason the is_deleted column of the contact record is coming to the entity

            // as contact_is_deleted ...special handling to have the form value set properly
            if ($name == 'is_deleted') {
              $form[$name]['#default_value'] = isset($entity->contact_is_deleted) ? $entity->contact_is_deleted : 0;
            }
            if ($name == 'contact_type') {
              $form[$name]['#ajax'] = array(
                'event' => 'change',
                'wrapper' => 'contact-subtype-wrapper',
                'callback' => '_civicrm_entity_form_contact_subtype_ajax_callback',
                'method' => 'replace',
              );
            }
            if ($name == 'contact_sub_type') {
              $contact_type = !empty($form['contact_type']['#default_value']) ? $form['contact_type']['#default_value'] : 'Individual';
              $form[$name]['#type'] = 'select';
              $form[$name]['#options'] = _civicrm_entity_form_contact_get_subtypes($contact_type);
              $form[$name]['#prefix'] = '<div id="contact-subtype-wrapper">';
              $form[$name]['#suffix'] = '</div>';
              $form[$name]['#validated'] = TRUE;
              unset($form[$name]['#size']);
            }
          }

          // event special handling
          if ($entity_type == 'civicrm_event') {
            if ($name == 'thankyou_text' || $name == 'thankyou_footer_text') {
              $form[$name]['#type'] = 'text_format';
              $form[$name]['format'] = 'full_html';
            }
            if ($name == 'event_type_id') {
              $form[$name]['#required'] = TRUE;
            }
          }

          // case special handling
          if ($entity_type == 'civicrm_case') {
            if ($name == 'creator_id') {
              unset($creator_id);
            }
          }

          // make sure to disable id field, and any custom field with is_view = 1
          if ($name == 'id' || !empty($info['is_view'])) {
            $form[$name]['#disabled'] = TRUE;
          }
        }

        // end if isset widget
      }

      // end else not a drupal field
    }

    // end foreach
    $form['actions'] = array(
      '#type' => 'container',
      '#attributes' => array(
        'class' => array(
          'form-actions',
        ),
      ),
      '#weight' => 400,
    );
    $form['#validate'] = array();
    $form['#submit'] = array();
    $form['#validate'][] = 'civicrm_entity_form_validate';
    $form['#submit'][] = 'civicrm_entity_form_submit';

    // 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'),
      '#submit' => $submit + array(),
    );
  }

  //end if op is add or edit
  if ($op == 'delete') {
    $form['delete_markup'] = array(
      '#type' => 'markup',
      '#markup' => '<div></strong>' . t('Are you sure you want to delete') . ' ' . $entity_type . " id: " . $entity->id . '</strong></div>',
    );
    $form['actions'] = array(
      '#type' => 'container',
      '#attributes' => array(
        'class' => array(
          'form-actions',
        ),
      ),
      '#weight' => 400,
    );
    $form['#validate'] = array();
    $form['#submit'] = array();
    $form['#validate'][] = 'civicrm_entity_delete_form_validate';
    $form['#submit'][] = 'civicrm_entity_delete_form_submit';

    // 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.
    $form['actions']['cancel'] = array(
      '#type' => 'submit',
      '#value' => t('Cancel'),
      '#submit' => array(
        'civicrm_entity_delete_form_cancel_submit',
      ),
    );
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Delete'),
      '#submit' => $form['#submit'],
    );
    $title = 'Delete ' . ucwords(str_replace("_", " ", $entity_type)) . ": " . entity_label($entity_type, $entity) . " (id:" . $entity->id . ")";
  }
  $title = str_replace("Civicrm", "CiviCRM", $title);
  drupal_set_title($title);
  return $form;
}