You are here

function notifications_custom_fields_form in Notifications 6

Same name and namespace in other branches
  1. 6.4 notifications_custom/notifications_custom.admin.inc \notifications_custom_fields_form()

Fields form

1 string reference to 'notifications_custom_fields_form'
notifications_custom_menu in notifications_custom/notifications_custom.module
Implementation of hook_menu().

File

notifications_custom/notifications_custom.admin.inc, line 167

Code

function notifications_custom_fields_form($form_state, $subscription) {
  $form['subscription'] = array(
    '#type' => 'value',
    '#value' => $subscription,
  );

  // Now the hard part, which are the fields. Only when subscriptions is created.
  $form['fields'] = array(
    '#title' => t('Fields'),
    '#type' => 'fieldset',
    '#tree' => 'true',
    '#theme' => 'notifications_custom_fields',
  );

  // Take the values from form state (if submitted) or from the subscription itself
  if (!empty($form_state['submitted'])) {
    if (!empty($form_state['values']['fields']['type'])) {
      foreach ($form_state['values']['fields']['type'] as $key => $type) {
        if (empty($form_state['values']['fields']['delete'][$key])) {

          // Add field to the list and mark as formatted so we can use this value for the form
          $fields[] = array(
            'type' => $type,
            'value' => $form_state['values']['fields']['value'][$key],
            'parsed' => TRUE,
          );
        }
      }
    }
    if (!empty($form_state['values']['newfield'])) {
      $fields[] = array(
        'type' => $form_state['values']['newfield'],
        'value' => '',
      );
    }
  }
  else {
    $fields = $subscription->fields;
  }

  // Build the form with current fields
  if ($fields) {
    foreach ($fields as $fid => $data) {
      $form['fields']['type'][$fid] = array(
        '#type' => 'hidden',
        '#value' => $data['type'],
      );
      $form['fields']['name'][$fid] = array(
        '#value' => notifications_subscription_fields($data['type'], 'name'),
      );
      $form['fields']['delete'][$fid] = array(
        '#type' => 'checkbox',
        '#default_value' => 0,
      );

      // Generate the field and pass the value only if not parsed yet
      if (empty($data['parsed'])) {
        $form['fields']['value'][$fid] = notifications_subscription_form_field($data['type'], $data['value']);
      }
      else {
        $form['fields']['value'][$fid] = notifications_subscription_form_field($data['type']);
        $form['fields']['value'][$fid]['#default_value'] = $data['value'];
      }
    }
  }
  else {
    $form['fields']['#description'] = t('You have to define at least one field for this subscription.');
  }
  $form['fields']['name']['new'] = array(
    '#type' => 'select',
    '#options' => notifications_subscription_fields(NULL, 'name'),
  );
  $form['fields']['delete']['new'] = array(
    '#value' => t('new'),
  );
  $form['fields']['value']['new'] = array(
    '#type' => 'submit',
    '#value' => t('Add new field'),
    '#submit' => array(
      'notifications_custom_fields_form_add_field',
    ),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save fields'),
  );
  return $form;
}