You are here

function party_activity_form in Party 7

Form callback: create or edit a activity.

Parameters

$activity: The PartyActivity object to edit or for a create form fot an empty PartyActivity object with only a type defined.

2 string references to 'party_activity_form'
party_activity_form_wrapper in modules/party_activity/party_activity.admin.inc
Form callback wrapper: create or edit a activity.
party_activity_modal_form_wrapper in modules/party_activity/party_activity.admin.inc
Modal Form Wrapper.

File

modules/party_activity/party_activity.admin.inc, line 308
Party Activity editing UI

Code

function party_activity_form($form, &$form_state, $activity, $op = 'edit') {
  global $user;
  $form_state['party_activity'] = $activity;
  $form_state['op'] = $op;

  // Store supported URL arguments in the form state.
  $args = drupal_get_query_parameters();
  foreach ($args as $arg => $value) {
    $form_state['get_params'][$arg] = $value;
  }

  // Add default start and end date vales.
  if (!empty($form_state['get_params']['startDate'])) {
    $tz_offset = 0;
    if (!empty($form_state['get_params']['tz'])) {
      $tz_offset = $form_state['get_params']['tz'] * 60;
    }
    $start = $form_state['get_params']['startDate'];
    $end = $form_state['get_params']['endDate'];

    // Make sure timezone things behave properly. Fullcalendar will
    // always pass the tz_offset of the computer rather than the users
    // timezone. For each date, therefore, we convert to what time fullcalendar
    // thinks the user clicks and then apply the users timezone to convert
    // this to utc so that the date field behaves properly.
    $date_item =& $activity->activity_date[LANGUAGE_NONE][0];
    $valueInUserTZ = gmdate('Y-m-d H:i:s', $start - $tz_offset);
    $valueDate = new DateTime($valueInUserTZ, new DateTimeZone($user->timezone));
    $valueDate
      ->setTimezone(new DateTimeZone('UTC'));
    $date_item['value'] = $valueDate
      ->format('Y-m-d H:i:s');
    $value2InUserTZ = gmdate('Y-m-d H:i:s', $end - $tz_offset);
    $value2Date = new DateTime($value2InUserTZ, new DateTimeZone($user->timezone));
    $value2Date
      ->setTimezone(new DateTimeZone('UTC'));
    $date_item['value2'] = $value2Date
      ->format('Y-m-d H:i:s');
    $date_item['timezone_db'] = 'UTC';
  }

  // If the operation is delete show a confirmation form.
  if ($op == 'delete') {
    return party_activity_delete_form($form, $form_state, $activity);
  }

  // If we don't have a bundle add a select box.
  if (empty($activity->type)) {
    $types = party_activity_get_types();
    $options = array();
    foreach ($types as $type) {
      $options[$type->type] = $type->label;
    }
    asort($options);
    $info = entity_get_info('party_activity');
    $form['type'] = array(
      '#type' => 'select',
      '#title' => t('!label type', array(
        '!label' => $info['label'],
      )),
      '#description' => t('Select what type of !label you wish to create.', array(
        '!label' => strtolower($info['label']),
      )),
      '#options' => $options,
    );
    $form['actions'] = array(
      '#type' => 'container',
      '#attributes' => array(
        'class' => array(
          'form-actions',
        ),
      ),
      '#weight' => 400,
    );
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Continue'),
      '#submit' => array(
        'party_activity_bundle_select_form_submit',
      ),
      '#validate' => array(
        'party_activity_bundle_select_form_validate',
      ),
    );
    return $form;
  }

  // Add the default field elements.
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#default_value' => isset($activity->title) ? $activity->title : '',
    '#maxlength' => 255,
    '#required' => TRUE,
    '#weight' => -5,
  );

  // Add the field related form elements.
  field_attach_form('party_activity', $activity, $form, $form_state);
  $form['actions'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'class' => array(
        'form-actions',
      ),
    ),
    '#weight' => 400,
  );

  // 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(
      'party_activity_form_submit',
    ),
  );
  if (!empty($activity->id)) {
    $form['actions']['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete'),
      '#suffix' => l(t('Cancel'), 'admin/community/activities'),
      '#submit' => $submit + array(
        'party_activity_form_submit_delete',
      ),
      '#weight' => 45,
    );
  }

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