You are here

function scs_node_selection in Simplenews Content Selection 7

Same name and namespace in other branches
  1. 6.2 scs.pages.inc \scs_node_selection()
  2. 6 scs.pages.inc \scs_node_selection()

Page callback: Node selection page also known as the first way to use this module

1 call to scs_node_selection()
scs_form_alter in ./scs.module
Implements hook_form_alter()
1 string reference to 'scs_node_selection'
scs_menu in ./scs.module
Implements hook_menu()

File

./scs.module, line 172
Select Drupal content to create a newsletter

Code

function scs_node_selection($form, &$form_state) {

  // Prevent the user from using this page if there is no content type
  // enabled for selection
  $types = array_filter(variable_get('scs_content_types', array()));
  if (empty($types)) {
    drupal_set_message(t('You need to enable some content types for selection in order to use this module. You can enable them in !link.', array(
      '!link' => l(t('Simplenews Content Selection settings'), 'admin/config/services/simplenews/settings/scs'),
    )), 'error');
    return array();
  }
  if (!isset($form_state['scs'])) {
    $form = _scs_get_metadata_form($form);

    // This is the first step of our two-step form
    $form['step'] = array(
      '#type' => 'hidden',
      '#value' => 1,
    );

    // Now begins the real content selection
    // Filtering
    $form['filters'] = array(
      '#type' => 'fieldset',
      '#title' => t('Filters'),
      '#collapsible' => TRUE,
      '#collapsed' => empty($filters) ? TRUE : FALSE,
    );
    $form['filters']['nid'] = array(
      '#type' => 'textfield',
      '#title' => t('Nid'),
      '#description' => t('Filter on a single nid.'),
      '#default_value' => !empty($form_state['values']['nid']) ? $form_state['values']['nid'] : '',
    );
    $form['filters']['title'] = array(
      '#type' => 'textfield',
      '#title' => t('Title'),
      '#description' => t('Filter on title, matching the first characters.'),
      '#default_value' => !empty($form_state['values']['title']) ? $form_state['values']['title'] : '',
    );
    $types = array(
      t('None'),
    ) + drupal_map_assoc($types);
    $form['filters']['type'] = array(
      '#type' => 'select',
      '#title' => t('Type'),
      '#options' => $types,
      '#multiple' => TRUE,
      '#default_value' => !empty($form_state['values']['type']) ? $form_state['values']['type'] : array_keys($types),
    );
    $form['filters']['filter'] = array(
      '#type' => 'submit',
      '#value' => t('Filter'),
    );

    // Table header
    $header = array(
      'title' => array(
        'data' => t('Title'),
        'field' => 'title',
        'sort' => 'ASC',
      ),
      'type' => array(
        'data' => t('Type'),
        'field' => 'type',
      ),
      'created' => array(
        'data' => t('Created'),
        'field' => 'created',
      ),
    );

    // Rows
    $rows = array();
    $query = db_select('node', 'n')
      ->extend('TableSort')
      ->fields('n', array(
      'nid',
      'title',
      'type',
      'created',
    ))
      ->orderByHeader($header);
    if (!empty($form_state['input'])) {
      if (!empty($form_state['input']['nid'])) {
        $query
          ->condition('nid', $form_state['values']['nid']);
      }
      if (!empty($form_state['input']['title'])) {
        $query
          ->condition('title', $form_state['input']['nid'] . '%', 'LIKE');
      }
    }
    if (!empty($form_state['input']['type'])) {
      $query
        ->condition('type', $form_state['input']['type']);
    }
    else {
      $query
        ->condition('type', array_keys($types));
    }
    $results = $query
      ->execute();
    foreach ($results as $node) {
      $rows[$node->nid] = array(
        'title' => l($node->title, 'node/' . $node->nid . '/edit'),
        'type' => $node->type,
        'created' => format_date($node->created),
      );
    }
    $form['nodes'] = array(
      '#type' => 'tableselect',
      '#header' => $header,
      '#options' => $rows,
      '#empty' => t('No nodes found'),
    );

    // Submit button
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Sort nodes'),
    );
  }
  else {
    $form['step'] = array(
      '#type' => 'hidden',
      '#value' => 2,
    );
    if (isset($form_state['values']['scs_title']) && isset($form_state['values']['scs_toc'])) {
      $form['scs_title'] = array(
        '#type' => 'hidden',
        '#value' => $form_state['values']['scs_title'],
      );
      $form['scs_toc'] = array(
        '#type' => 'hidden',
        '#value' => $form_state['values']['scs_toc'],
      );
    }
    $nodes = array_filter($form_state['values']['nodes']);
    $form = array_merge($form, _scs_get_sorting_form($form, $nodes));
    unset($form_state['values']['nodes']);
    $form['#theme'] = 'scs_sortable_table';
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Create newsletter'),
    );
  }
  return $form;
}