You are here

scs.module in Simplenews Content Selection 6.2

Same filename and directory in other branches
  1. 8 scs.module
  2. 6 scs.module
  3. 7.2 scs.module
  4. 7 scs.module

Select Drupal content to create a newsletter

File

scs.module
View source
<?php

/**
 * @file
 * Select Drupal content to create a newsletter
 */

/**
 * Implements hook_menu()
 */
function scs_menu() {
  $items = array();
  $items['admin/settings/simplenews/scs'] = array(
    'title' => 'Simplenews Content Selection',
    'description' => 'Configure what node types could be used for SCS, ...',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'scs_admin_settings_form',
    ),
    'access arguments' => array(
      'administer scs',
    ),
    'file' => 'scs.admin.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  $items['admin/content/scs_node_selection/nodes'] = array(
    'title' => t('From nodes'),
    'description' => t('Select nodes to create a newsletter'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'scs_node_selection',
    ),
    'access arguments' => array(
      'scs create newsletters',
    ),
    'file' => 'scs.pages.inc',
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/content/scs_node_selection'] = array(
    'title' => 'Simplenews Content Selection Creator',
    'description' => 'Select nodes to create a newsletter',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'scs_node_selection',
    ),
    'access arguments' => array(
      'scs create newsletters',
    ),
    'file' => 'scs.pages.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  $items['admin/content/scs_sort_nodes'] = array(
    'title' => 'Simplenews Content Select Sorter',
    'description' => 'Sort the nodes like you want them in the newsletter',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'scs_sort_nodes',
    ),
    'access arguments' => array(
      'scs create newsletters',
    ),
    'file' => 'scs.pages.inc',
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implements hook_perm()
 */
function scs_perm() {
  $perms = array(
    'administer scs',
    'scs create newsletters',
  );
  if (variable_get('scs_types', '') != '') {
    $types = _scs_get_types();
    unset($types[0]);
    foreach ($types as $type => $typename) {
      $perms[] = sprintf('use %s type', $type);
    }
  }
  return $perms;
}

/**
 * Implements hook_theme()
 */
function scs_theme() {
  return array(
    'scs_node_selection' => array(
      'arguments' => array(
        'form' => NULL,
      ),
      'file' => 'scs.theme.inc',
    ),
    'scs_region_output' => array(
      'arguments' => array(
        'region_id' => NULL,
        'content' => NULL,
      ),
      'file' => 'scs.theme.inc',
      'pattern' => 'scs_region_output__',
    ),
    'scs_node_output' => array(
      'arguments' => array(
        'node' => NULL,
      ),
      'file' => 'scs.theme.inc',
      'pattern' => 'scs_node_output__',
    ),
    'scs_newsletter_output' => array(
      'arguments' => array(
        'options' => array(),
      ),
      'file' => 'scs.theme.inc',
    ),
    'scs_sort_nodes' => array(
      'arguments' => array(
        'form' => NULL,
      ),
      'template' => 'templates/scs_sort',
    ),
    'scs_node_titles' => array(
      'arguments' => array(
        'titles' => array(),
      ),
      'file' => 'scs.theme.inc',
    ),
  );
}

/**
 * Implements hook_form_alter()
 * Used to add an extra update operation to admin/content/node
 */
function scs_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'node_admin_content') {
    $form['admin']['options']['operation']['#options']['scs'] = t('Create newsletter');
    $form['admin']['options']['submit']['#submit'][] = 'scs_admin_content_node_submit';
  }
}

/**
 * Extra submit function on admin/content/node form
 */
function scs_admin_content_node_submit($form, &$form_state) {
  if ($form_state['values']['operation'] == 'scs') {
    $nodes = $form_state['values']['nodes'];
    $counter = 0;
    $newnodes = array();
    foreach ($nodes as $key => $selected) {
      if ($selected != 0) {
        $newnodes['nid_' . $counter] = $selected;
        $counter++;
      }
    }
    $query = http_build_query($newnodes, '', '&');
    drupal_goto('admin/content/scs_sort_nodes', $query);
  }
}

/**
 * Get nodes avaiable for selection
 */
function _scs_get_nodes($headers = array(), $filters = array()) {
  $nodes = array();
  $content_types = variable_get('scs_content_types', array());
  $content_types = array_filter($content_types);
  $content_types = implode('\', \'', $content_types);
  if (count($content_types) != 0) {

    //Filters
    if (!empty($filters)) {
      $filtersql = array();
      if (array_key_exists('nid', $filters)) {
        $filtersql[] = sprintf('nid=%d', $filters['nid']);
      }
      if (array_key_exists('title', $filters)) {
        $filtersql[] = "title LIKE '" . $filters['title'] . "%'";
      }
      if (array_key_exists('type', $filters)) {
        $content_types = array(
          $filters['type'],
        );
      }
      $prefix = '';
      if (!empty($filtersql)) {
        $prefix = ' AND ';
      }
      $filtersql = $prefix . implode(' AND ', $filtersql);
    }
    $p = db_placeholders($content_types, 'varchar');

    //Table sort
    if (!empty($headers)) {
      $tablesortsql = tablesort_sql($headers);
    }
    else {
      if (isset($_GET['sort']) && isset($_GET['order'])) {
        switch ($_GET['order']) {
          case t('Nid'):
            $field = 'nid';
            break;
          case t('Title'):
            $field = 'title';
            break;
          case t('Created'):
            $field = 'created';
            break;
        }
        $tablesortsql = sprintf('ORDER BY %s %s', $field, strtoupper($_GET['sort']));
      }
      else {
        $tablesortsql = 'ORDER BY nid ASC';
      }
    }
    $query = "SELECT nid, title, created FROM {node} WHERE ({node}.status <> 0) AND type IN ('{$content_types}'){$filtersql} {$tablesortsql}";
    $result = pager_query($query, 25, 0);
    $pager = theme('pager');
    while ($node = db_fetch_object($result)) {
      $node->created = date('Y-m-d H:i:s', $node->created);
      $nodes[] = $node;
    }
  }
  return array(
    'nodes' => $nodes,
    'pager' => $pager,
  );
}

/**
 * Newsletter creator function
 */
function _scs_create_newsletter($options) {
  global $user, $custom_theme;
  if ($theme = variable_get('theme_default', FALSE)) {
    $custom_theme = $theme;
    init_theme();
  }

  //Title
  if ($options['title'] == '') {
    $title = variable_get('scs_default_title', t('Please edit the title of this newsletter'));
  }

  // Load node information
  foreach ($options['nodes'] as $region => $nodes) {
    foreach ($nodes as $node) {
      $complete_nodes[$region][] = node_load(array(
        'nid' => $node,
      ));
    }
  }
  ksort($complete_nodes);

  // Create the body of the newsletter
  $themeoptions = array(
    'nodes' => $complete_nodes,
    'toc' => $options['toc'],
    'type' => $options['newsletter_type'],
  );
  $body = theme('scs_newsletter_output', $themeoptions);
  $newsletter = new StdClass();
  $newsletter->type = $options['content_type'];
  $newsletter->uid = $user->uid;
  $newsletter->title = $options['title'];
  $newsletter->body = $body;
  $newsletter->teaser = node_teaser($body);
  $newsletter->filter = variable_get('filter_default_format', 1);
  $newsletter->status = variable_get('scs_publish_default', 1);
  $newsletter->revision = 1;
  $newsletter->promote = 0;
  $newsletter->comment = 0;
  $newsletter->created = time();
  $newsletter->changed = time();
  $newsletter->simplenews['s_format'] = strtolower(variable_get('scs_format', 'plain'));
  $newsletter->priority = 0;
  $newsletter->receipt = 0;

  //Add a hook to make it possible to add default settings to the created newsletter, all selected options during the process are available
  foreach (module_implements('scs_create_newsletter') as $name) {
    $function = $name . '_scs_create_newsletter';
    $function($newsletter, $options);
  }
  node_save($newsletter);
  drupal_goto('node/' . $newsletter->nid . '/edit');
}

/**
 * Interal to get the possible content types to filter on
 */
function _scs_get_typefilter() {
  $types = variable_get('scs_content_types', array());
  $options = array();
  $options[0] = t('Select node type');
  foreach ($types as $key => $value) {
    if (!is_numeric($value)) {
      $options[$key] = $value;
    }
  }
  return $options;
}

/**
 * Internal to get newsletter types
 */
function _scs_get_types() {
  $options = array();
  $options[0] = t('No special type');
  $types = variable_get('scs_types', '');
  $types = explode("\n", $types);
  foreach ($types as $key => $type) {
    if ($type != '' && user_access('use ' . trim($type) . ' type')) {
      $options[strtolower(trim($type))] = trim($type);
    }
  }
  return $options;
}

Functions

Namesort descending Description
scs_admin_content_node_submit Extra submit function on admin/content/node form
scs_form_alter Implements hook_form_alter() Used to add an extra update operation to admin/content/node
scs_menu Implements hook_menu()
scs_perm Implements hook_perm()
scs_theme Implements hook_theme()
_scs_create_newsletter Newsletter creator function
_scs_get_nodes Get nodes avaiable for selection
_scs_get_typefilter Interal to get the possible content types to filter on
_scs_get_types Internal to get newsletter types