You are here

function views_bulk_operations_fields_action_form in Views Bulk Operations (VBO) 6

Same name and namespace in other branches
  1. 6.3 fields.action.inc \views_bulk_operations_fields_action_form()

Action form function.

1 call to views_bulk_operations_fields_action_form()
views_bulk_operations_fields_rules_action_form in ./views_bulk_operations.rules.inc

File

actions/fields.action.inc, line 121
Drupal action to set individual field values.

Code

function views_bulk_operations_fields_action_form($context) {
  module_load_include('inc', 'content', 'includes/content.node_form');

  // This action form uses static-time settings. If they were not set, pull the defaults now.
  if (!isset($context['settings'])) {
    $context['settings'] = views_bulk_operations_fields_action_views_bulk_operations_form_options();
  }
  $form['#settings'] = $context['settings'];
  $form['#theme'] = 'views_bulk_operations_fields_action_form';
  $form['#node'] = (object) array(
    'type' => NULL,
  );
  $form_state = array();
  $weight = -100;

  // Get the content types of the selected nodes if any. Otherwise, get all types.
  if (!empty($context['selection']) && isset($context['view'])) {
    $nids = array_map('_views_bulk_operations_get_oid', $context['selection'], array_fill(0, count($context['selection']), $context['view']->base_field));
    $placeholders = db_placeholders($nids);
    $result = db_query("SELECT DISTINCT type FROM {node} WHERE nid IN ({$placeholders}) AND NOT (type = '' OR type IS NULL)", $nids);
  }
  else {
    $result = db_query("SELECT type FROM {node_type}");
  }

  // For each type, get its fields.
  $fields = array();
  while ($type = db_result($result)) {
    $fields += _views_bulk_operations_fields_action_non_cck($type);
    $type_info = content_types($type);
    $fields += $type_info['fields'];
  }
  if (empty($fields)) {
    form_set_error('', t('The selected nodes do not have any editable fields. Please select other nodes and try again.'));
    return array();
  }

  // Iterate on fields, creating the input widget for each.
  foreach ($fields as $field) {

    // Skip fields for which the user has no permission.
    if (module_exists('content_permissions') && in_array('edit ' . $field['field_name'], module_invoke('content_permissions', 'perm')) && !user_access('edit ' . $field['field_name'])) {
      continue;
    }

    // Skip fields that are not selected in VBO settings.
    if (!empty($context['settings']['display_fields']) && !in_array($field['field_name'], $context['settings']['display_fields']) && !in_array(VBO_ACTION_FIELDS_ALL, $context['settings']['display_fields'])) {
      continue;
    }

    // Set the default value in the synthesized node.
    $form['#node']->{$field['field_name']} = empty($context[$field['field_name'] . '_check']) ? NULL : $context[$field['field_name']];

    // The field info and widget.
    if ($field['type'] == 'non_cck') {

      // is it our hacked definition?
      $form += (array) $field['form'];
      if (is_array($form['#node']->{$field['field_name']})) {
        foreach ($form['#node']->{$field['field_name']} as $v_key => $v_item) {
          $form[$field['field_name']][$v_key]['#default_value'] = $v_item;
        }
      }
      else {
        $form[$field['field_name']]['#default_value'] = $form['#node']->{$field['field_name']};
      }
    }
    else {

      // no, it's CCK
      $field['required'] = FALSE;
      $form += (array) content_field_form($form, $form_state, $field);
    }
    if (empty($form[$field['field_name']])) {
      continue;
    }

    // CCK needs this to properly process its fields.
    $form['#field_info'][$field['field_name']] = $field;

    // Adjust some settings on the field itself.
    $form[$field['field_name']]['#weight'] = $weight++;
    $form[$field['field_name']]['#prefix'] = '<div class="fields-action-togglable">' . @$form[$field['field_name']]['#prefix'];
    $form[$field['field_name']]['#suffix'] = @$form[$field['field_name']]['#suffix'] . '</div>';

    // Checkbox to enable/disable this field.
    $form[$field['field_name'] . '_check'] = array(
      '#type' => 'checkbox',
      '#attributes' => array(
        'class' => 'fields-action-toggler',
      ),
      '#default_value' => !empty($context[$field['field_name'] . '_check']),
    );

    // Checkbox to add/overwrite values in this field.
    if (!empty($field['multiple'])) {
      $form[$field['field_name'] . '_add'] = array(
        '#type' => 'checkbox',
        '#prefix' => '<div class="fields-action-togglable">',
        '#suffix' => '</div>',
        '#attributes' => array(
          'title' => t('Check this box to add new values to this multi-valued field, instead of overwriting existing values.'),
        ),
        '#default_value' => !empty($context[$field['field_name'] . '_add']),
      );
    }
    else {
      $form[$field['field_name'] . '_add'] = array(
        '#type' => 'value',
        '#value' => FALSE,
      );
    }

    // PHP code to program the value.
    if (user_access('Use PHP input for field settings (dangerous - grant with care)') && $context['settings']['php_code']) {
      if ($field['type'] == 'non_cck') {
        $children = element_children($field['form'][$field['field_name']]);
        if (empty($children)) {
          $sample = t("&lt;?php\nreturn value; // value for @column\n?&gt;", array(
            '@column' => $field['field_name'],
          ));
        }
        else {
          $columns = array();
          foreach ($children as $child) {
            if (!empty($field['form'][$field['field_name']][$child]['#ignore'])) {
              continue;
            }
            $columns[$child] = t("'@column' => value for @column", array(
              '@column' => $child,
            ));
          }
          $sample = t("&lt;?php\n" . "return array(\n" . "  @columns\n" . ");\n" . "?&gt;", array(
            '@columns' => implode(",\n  ", $columns),
          ));
        }
      }
      else {
        $db_info = content_database_info($field);
        $columns = array_keys($db_info['columns']);
        foreach ($columns as $key => $column) {
          $columns[$key] = t("'@column' => value for @column", array(
            '@column' => $column,
          ));
        }
        $sample = t($field['multiple'] ? "&lt;?php\n" . "return array(\n" . "  0 => array(\n    @columns\n  ),\n" . "  1 => array(\n    @columns\n  ),\n" . "  2 => ...\n" . ");\n" . "?&gt;" : "&lt;?php\n" . "return array(\n" . "  0 => array(\n    @columns\n  ),\n" . ");\n" . "?&gt;", array(
          '@columns' => implode(",\n    ", $columns),
        ));
      }
      $form[$field['field_name'] . '_code'] = array(
        '#type' => 'textarea',
        '#default_value' => '',
        '#rows' => 6,
        '#description' => t('Expected format: <pre>!sample</pre>', array(
          '!sample' => $sample,
        )),
        '#prefix' => '<div class="fields-action-togglable">',
        '#suffix' => '</div>',
      );
    }
  }

  // Special case for only one field: convert the checkbox into a value that's TRUE by default.
  if (count($form['#field_info']) == 1) {
    $field_name = key($form['#field_info']);
    $form[$field_name . '_check'] = array(
      '#type' => 'value',
      '#value' => TRUE,
    );
  }
  return $form;
}