You are here

function commerce_file_license_form in Commerce File 7

Form callback: create or edit a license.

Parameters

$entity: The object to edit through the form.

File

includes/commerce_file_license.forms.inc, line 17
Forms for creating / editing, deleting, issuing licenses

Code

function commerce_file_license_form($form, &$form_state, $entity, $op = 'edit') {
  $module_path = drupal_get_path('module', 'commerce_file');
  $field_names = _commerce_file_get_field_names();
  $license_id = $entity
    ->internalIdentifier();
  $have_line_items = FALSE;

  // we can always wrap since entity_ui_get_form() creates new entity if $op = 'add'
  $wrapper = commerce_file_license_wrapper($entity);

  // add help text
  if ($op == 'add' && ($help = variable_get('commerce_file_license_help_text', ''))) {
    $form['help'] = array(
      '#weight' => -100,
      '#markup' => '<p>' . filter_xss_admin($help) . '</p>',
    );
  }

  // Ensure this include file is loaded when the form is rebuilt from the cache.
  $form_state['build_info']['files']['form'] = $module_path . '/includes/commerce_file_license.forms.inc';

  // add license after build handler
  $form['#after_build'][] = 'commerce_file_license_form_after_build';

  // set formatted date if creating new
  if (empty($entity->created)) {
    $entity->is_new = TRUE;
    $entity->date = format_date(REQUEST_TIME, 'custom', 'Y-m-d H:i:s O');
  }

  // get any line items
  $line_items = array();
  if (!empty($wrapper->{$field_names['license_line_items']})) {
    $line_items = $wrapper->{$field_names['license_line_items']}
      ->value();
  }
  $have_line_items = !empty($line_items);
  $form_state['have_line_items'] = $have_line_items;

  // Add the user account and e-mail fields.
  $user_field_disabled = $have_line_items && !empty($entity->owner);
  $form['user'] = array(
    '#type' => 'fieldset',
    '#title' => t('Owner information'),
    '#collapsible' => TRUE,
    '#collapsed' => $user_field_disabled,
    '#weight' => -10,
  );
  $form['user']['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Owned by'),
    '#description' => t('Owner cannot be left blank.', array(
      '%anonymous' => variable_get('anonymous', t('Anonymous')),
    )),
    '#maxlength' => 60,
    '#autocomplete_path' => 'user/autocomplete',
    '#default_value' => isset($entity->owner) && isset($entity->owner->name) ? $entity->owner->name : '',
    '#required' => TRUE,
    '#weight' => -1,
    '#disabled' => $user_field_disabled,
  );

  // Add the field related form elements and allow extra_fields weights to be set
  $form_state[COMMERCE_FILE_LICENSE_ENTITY_NAME] = $entity;
  field_attach_form(COMMERCE_FILE_LICENSE_ENTITY_NAME, $entity, $form, $form_state);

  // file field pre-processing
  $file_field_name = $field_names['license_file'];
  $file_lang = isset($form[$file_field_name]['#language']) ? $form[$file_field_name]['#language'] : LANGUAGE_NONE;
  if (!empty($form[$file_field_name][$file_lang])) {
    foreach (element_children($form[$file_field_name][$file_lang]) as $delta) {
      if (is_int($delta)) {
        $file_element =& $form[$file_field_name][$file_lang][$delta];
        $file_element['data']['#collapsed'] = FALSE;

        /** @todo add a js display of current limits that changes with file settings changes **************/

        /*
        if (isset($file_element['data']['#attributes']['class'])) {
          $file_element['data']['#attributes']['class'] = array_merge($file_element['data']['#attributes']['class'], array('clearfix'));
        }
        else {
          $file_element['data']['#attributes']['class'] = array('clearfix');
        }


        $file_element['data']['_current_limits'] = array(
          '#markup' => '<div class="commerce-file-license-current-limits"><pre>'.print_r($entity->limits,1).'</pre></div>',
          '#weight' => -10,
        );
        */
        unset($file_element);
      }
    }

    //$form['#attached']['js'][] = drupal_get_path('module', 'commerce_file') . '/js/commerce_file.forms.js';
  }

  // line item field pre-processing
  $line_item_fieldname = $field_names['license_line_items'];
  if (isset($form[$line_item_fieldname])) {
    $line_item_description = t('The line item references are shown for display purposes only. Line items are automatically linked and un-linked through order management.');

    // always disable line item form
    $line_item_lang = isset($form[$line_item_fieldname]['#language']) ? $form[$line_item_fieldname]['#language'] : LANGUAGE_NONE;
    $form[$line_item_fieldname]['#disabled'] = TRUE;
    $form[$line_item_fieldname][$line_item_lang]['actions'] = array(
      '#markup' => $line_item_description,
    );

    // get view if we have line items
    $line_items_embedded_view = views_embed_view('commerce_license_line_items_list', 'default', $license_id);
    if (!empty($line_items_embedded_view)) {

      // hide line item form and show summary view display
      $form[$line_item_fieldname]['#access'] = FALSE;
      $form[$line_item_fieldname . '_summary'] = array(
        '#type' => 'item',
        '#title' => t('Related Orders'),
        '#description' => $line_item_description,
        '#markup' => $line_items_embedded_view,
        '#weight' => $form[$line_item_fieldname]['#weight'],
      );
    }
  }

  // setup vertical tabs
  $form['additional_settings'] = array(
    '#type' => 'vertical_tabs',
    '#weight' => 99,
  );

  // Add a section to update the status
  $status_original = isset($entity->status) ? $entity->status : commerce_file_license_status_default('name');
  $granted_original = isset($entity->granted) ? $entity->granted : NULL;
  $status_options = commerce_file_license_status_options_list();
  $form['license_status'] = array(
    '#type' => 'fieldset',
    '#title' => t('License status'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#group' => 'additional_settings',
    '#attached' => array(
      'js' => array(
        //$module_path . '/js/commerce_file_license.forms.js',
        array(
          'type' => 'setting',
          'data' => array(
            'status_titles' => $status_options,
          ),
        ),
      ),
    ),
    '#weight' => 20,
  );
  $form['license_status']['status'] = array(
    '#type' => 'select',
    '#title' => t('Status'),
    '#options' => $status_options,
    '#default_value' => $status_original,
  );
  $form['license_status']['status_original'] = array(
    '#type' => 'hidden',
    '#value' => $status_original,
    '#attributes' => array(
      'id' => 'edit-status-original',
    ),
  );
  $form['license_status']['granted'] = array(
    '#type' => 'textfield',
    '#title' => t('Granted on'),
    '#maxlength' => 25,
    '#default_value' => !empty($granted_original) ? format_date($granted_original, 'short') : '',
  );
  $form['license_status']['granted_original'] = array(
    '#type' => 'hidden',
    '#value' => !empty($granted_original) ? format_date($granted_original, 'short') : '',
    '#attributes' => array(
      'id' => 'edit-granted-original',
    ),
  );

  // Add History info
  $form['license_history'] = array(
    '#type' => 'fieldset',
    '#title' => t('License history', array(), array(
      'context' => 'a drupal commerce file license',
    )),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#group' => 'additional_settings',
    //'#attached' => array(

    //  'js' => array($module_path . '/js/commerce_file_license.forms.js'),

    //),
    '#weight' => 40,
  );

  // Created and changed date
  $form['license_history']['date'] = array(
    '#type' => 'textfield',
    '#title' => t('Created on'),
    '#description' => t('Format: %time. The date format is YYYY-MM-DD and %timezone is the time zone offset from UTC. Leave blank to use the time of form submission.', array(
      '%time' => !empty($entity->date) ? date_format(date_create($entity->date), 'Y-m-d H:i:s O') : format_date($entity->created, 'custom', 'Y-m-d H:i:s O'),
      '%timezone' => !empty($entity->date) ? date_format(date_create($entity->date), 'O') : format_date($entity->created, 'custom', 'O'),
    )),
    '#maxlength' => 25,
    '#default_value' => !empty($entity->created) ? format_date($entity->created, 'custom', 'Y-m-d H:i:s O') : '',
  );
  $form['license_history']['created'] = array(
    '#type' => 'hidden',
    '#value' => !empty($entity->created) ? format_date($entity->created, 'short') : '',
    '#attributes' => array(
      'id' => 'edit-created',
    ),
  );
  $form['license_history']['changed'] = array(
    '#type' => 'hidden',
    '#value' => !empty($entity->changed) ? format_date($entity->changed, 'short') : '',
    '#attributes' => array(
      'id' => 'edit-changed',
    ),
  );

  // actions
  $form['actions'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'class' => array(
        'form-actions',
      ),
    ),
    '#weight' => 100,
  );

  // 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 License'),
    '#weight' => 40,
  );

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