You are here

function entity_legal_document_form in Entity Legal 7

Same name and namespace in other branches
  1. 7.2 entity_legal.admin.inc \entity_legal_document_form()

Generates the profile type editing form.

1 string reference to 'entity_legal_document_form'
EntityLegalDocumentUIController::hook_forms in ./entity_legal.entity_admin.inc
Provides definitions for implementing hook_forms().

File

./entity_legal.admin.inc, line 10
Administration hooks and helpers for entity_legal module.

Code

function entity_legal_document_form($form, &$form_state, EntityLegalDocument $entity, $op = 'edit', $entity_type = NULL) {
  $form['label'] = array(
    '#title' => t('Administrative label'),
    '#type' => 'textfield',
    '#default_value' => isset($entity->label) ? $entity->label : '',
    '#required' => TRUE,
  );
  $form['name'] = array(
    '#type' => 'machine_name',
    '#title' => t('Machine-readable name'),
    '#required' => TRUE,
    '#default_value' => isset($entity->name) ? $entity->name : '',
    '#machine_name' => array(
      'exists' => 'entity_legal_document_name_exists',
      'source' => array(
        'label',
      ),
    ),
    '#access' => $op == 'add' || $op == 'clone',
    '#maxlength' => 32,
  );
  if (!in_array($op, array(
    'add',
    'clone',
  ))) {
    $versions = $entity
      ->getAllVersions();
    if ($op == 'edit' && empty($versions)) {
      drupal_set_message(t('No versions for this document have been found. !add_link to use this document.', array(
        '!add_link' => l(t('Add a version'), $_GET['q'] . '/add'),
      )), 'warning');
    }
    $header = array(
      'title' => t('Title'),
      'created' => t('Created'),
      'updated' => t('Updated'),
      'operations' => t('Operations'),
    );
    $options = array();
    foreach ($versions as $document_version_entity) {

      // Use the default uri if this version is the current published version.
      if ($entity
        ->getPublishedVersion() && $entity
        ->getPublishedVersion()
        ->identifier() == $document_version_entity
        ->identifier()) {
        $entity_uri = $entity
          ->uri();
        $path = $entity_uri['path'];
      }
      else {
        $path = 'legal/document/' . $entity
          ->identifier() . '/' . $document_version_entity
          ->identifier();
      }
      $options[$document_version_entity
        ->identifier()] = array(
        'title' => l($document_version_entity
          ->label(TRUE), $path),
        'created' => $document_version_entity
          ->getFormattedDate('created'),
        'updated' => $document_version_entity
          ->getFormattedDate('updated'),
        'operations' => l(t('Edit'), 'admin/structure/legal/manage/' . $entity
          ->identifier() . '/manage/' . $document_version_entity
          ->identifier()),
      );
    }

    // By default just show a simple overview for all entities.
    $form['versions'] = array(
      '#type' => 'fieldset',
      '#title' => t('Current version'),
      '#description' => t('The current version users must agree to. If requiring existing users to accept, those users will be prompted if they have not accepted this particular version in the past.'),
      '#tree' => FALSE,
    );
    $form['versions']['published_version'] = array(
      '#type' => 'tableselect',
      '#header' => $header,
      '#options' => $options,
      '#empty' => t('Create a document version to set up a default'),
      '#multiple' => FALSE,
      '#default_value' => isset($entity->published_version) ? $entity->published_version : '',
    );
  }
  $form['settings'] = array(
    '#type' => 'vertical_tabs',
    '#weight' => 27,
    '#tree' => TRUE,
  );
  $form['settings']['new_users'] = array(
    '#title' => t('New users'),
    '#description' => t('Visit the !permissions page to ensure that users can view the document.', array(
      '!permissions' => l(t('permissions'), 'admin/people/permissions'),
    )),
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#tree' => TRUE,
  );
  $form['settings']['new_users']['require'] = array(
    '#title' => t('Require new users to accept this agreement on signup'),
    '#type' => 'checkbox',
    '#default_value' => isset($entity->require_signup) ? $entity->require_signup : 0,
  );
  $form['settings']['new_users']['require_method'] = array(
    '#title' => t('Present to user as'),
    '#type' => 'select',
    '#options' => _entity_legal_document_methods('new_users'),
    '#default_value' => $entity
      ->getAcceptanceDeliveryMethod(TRUE),
    '#states' => array(
      'visible' => array(
        ':input[name="settings[new_users][require]"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  $form['settings']['existing_users'] = array(
    '#title' => t('Existing users'),
    '#description' => t('Visit the !permissions page to configure which existing users these settings apply to.', array(
      '!permissions' => l(t('permissions'), 'admin/people/permissions'),
    )),
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#tree' => TRUE,
  );
  $form['settings']['existing_users']['require'] = array(
    '#title' => t('Require existing users to accept this agreement'),
    '#type' => 'checkbox',
    '#default_value' => isset($entity->require_existing) ? $entity->require_existing : 0,
  );
  $form['settings']['existing_users']['require_method'] = array(
    '#title' => t('Present to user as'),
    '#type' => 'select',
    '#options' => _entity_legal_document_methods('existing_users'),
    '#default_value' => $entity
      ->getAcceptanceDeliveryMethod(),
    '#states' => array(
      'visible' => array(
        ':input[name="settings[existing_users][require]"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  _entity_legal_document_form_add_path_settings($form, $form_state, $entity, $op);
  $form['actions'] = array(
    '#type' => 'actions',
    '#weight' => 28,
  );
  $form['actions']['save'] = array(
    '#type' => 'submit',
    '#value' => $op == 'add' ? t('Next') : t('Save'),
    '#weight' => 1,
  );

  // Remove unnecessary Entity API validation and submit handlers.
  $form['#validate'] = array();
  $form['#submit'] = array(
    'entity_legal_document_form_submit',
  );
  return $form;
}