You are here

function flag_lists_ops_form in Flag Lists 7

Same name and namespace in other branches
  1. 7.3 flag_lists.module \flag_lists_ops_form()

Add the Flag list select menu widget.

1 call to flag_lists_ops_form()
flag_lists_form_alter in ./flag_lists.module
Implementation of hook_form_alter().

File

./flag_lists.module, line 359
The Flag Lists module.

Code

function flag_lists_ops_form($form, &$form_state, $flo) {
  $form['#attached']['js'][] = drupal_get_path('module', 'flag_lists') . '/js/flag_lists_ops.js';
  $form['#attached']['css'][] = drupal_get_path('module', 'flag_lists') . '/css/flag_lists_ops.css';
  $form['#prefix'] = '<div class="flo-views-form">';
  $form['#suffix'] = '</div>';
  $form_state['flo_operation'] = $flo->options['flo']['operation'];

  // Force browser to reload the page if Back is hit.
  if (preg_match('/msie/i', $_SERVER['HTTP_USER_AGENT'])) {
    drupal_add_http_header('Cache-Control', 'no-cache');

    // works for IE6+
  }
  else {
    drupal_add_http_header('Cache-Control', 'no-store');

    // works for Firefox and other browsers
  }
  global $user;
  global $base_url;
  $account = user_load($user->uid);
  $items = array();
  if ($flags = flag_lists_get_user_flags(NULL, $account)) {

    // Build the list of flag lists for this node.
    foreach ($flags as $flag) {
      $items[(string) $flag->fid] = $flag->title;
    }
  }

  // Group items into a fieldset for easier theming.
  $form['flag_lists_' . $form_state['flo_operation']] = array(
    '#type' => 'fieldset',
    '#title' => t('List operations'),
    '#tree' => TRUE,
    '#attributes' => array(
      'class' => array(
        'flag-lists-ops-fieldset',
      ),
    ),
  );
  switch ($flo->options['flo']['operation']) {
    case 'unflag':
      $null_text = 'Remove from a list';
      $operation = 'unflag';
      $form['flag_lists_' . $form_state['flo_operation']]['list'] = array(
        '#type' => 'button',
        '#value' => t('Remove from list'),
        '#ajax' => array(
          'callback' => 'flag_lists_ops_form_ajax_callback',
          'wrapper' => 'flag-list-ops-container-' . $flo->options['flo']['operation'],
        ),
      );
      break;
    default:
      $null_text = 'Add to a list';
      $operation = 'flag';
      drupal_add_library('system', 'ui.dialog');
      $form['flag_lists_' . $form_state['flo_operation']]['list'] = array(
        '#type' => 'select',
        '#options' => array(
          '0' => t('- ' . $null_text . ' -'),
        ) + $items,
        '#default_value' => '0',
        '#ajax' => array(
          'callback' => 'flag_lists_ops_form_ajax_callback',
          'wrapper' => 'flag-list-ops-container-' . $flo->options['flo']['operation'],
        ),
        '#attributes' => array(
          'class' => array(
            'flag-lists-ops-dropdown',
          ),
        ),
      );

      // Add the necessary JS for creating a new list via AJAX
      $result = db_select('flag_lists_types')
        ->fields('flag_lists_types')
        ->execute();
      $list_types = array();
      foreach ($result as $row) {
        if (!empty($row->type)) {
          $list_types[] = $row->type;
        }
      }
      drupal_add_js(array(
        'flag_lists' => array(
          'types' => $list_types,
          'json_path' => $base_url . '/flag-lists/add/%/js',
        ),
      ), 'setting');
      break;
  }

  // Get the $ops from the originating form.
  if (!empty($form_state['values']['flag_lists_' . $form_state['flo_operation']]['list'])) {
    $list = $form_state['values']['flag_lists_' . $form_state['flo_operation']]['list'];
  }
  if (!empty($_REQUEST['flag_lists_ops'])) {
    $ops = $_REQUEST['flag_lists_ops'];
  }
  if (!empty($ops) && !empty($list) && $form_state['values']['flag_lists_' . $form_state['flo_operation']]['operation'] == 'unflag') {
    $hidden_deleted_values = '';
    foreach ($ops as $nid) {
      $hidden_deleted_values .= '<input type="hidden" class="flo-deleted-value" value="' . $nid . '" />';
    }
  }
  $form['flag_lists_' . $form_state['flo_operation']]['operation'] = array(
    '#type' => 'hidden',
    '#value' => $operation,
  );
  $form['flag_lists_' . $form_state['flo_operation']]['go'] = array(
    '#type' => 'submit',
    '#value' => t('Go'),
    '#attributes' => array(
      'class' => array(
        'flag-lists-ops-go',
      ),
    ),
  );
  unset($form['actions']['submit']);

  // Generate a status message for AJAX submission.
  $form['flag_lists_' . $form_state['flo_operation']]['status_message'] = array(
    '#markup' => '',
  );
  $form['flag_lists_' . $form_state['flo_operation']]['#prefix'] = '<div id="flag-list-ops-container-' . $flo->options['flo']['operation'] . '">';
  $form['flag_lists_' . $form_state['flo_operation']]['#suffix'] = !empty($hidden_deleted_values) ? $hidden_deleted_values : '' . '</div>';
  return $form;
}