You are here

scs.module in Simplenews Content Selection 6

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

File

scs.module
View source
<?php

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

/*
 * Implementation of 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'] = 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,
  );
  return $items;
}

/*
 * Implementation of hook_perm
 */
function scs_perm() {
  return array(
    'administer scs',
    'scs create newsletters',
  );
}

/*
 * Implementation of hook_theme
 */
function scs_theme() {
  return array(
    'scs_node_selection' => array(
      'arguments' => array(
        'form' => NULL,
      ),
      'file' => 'scs.theme.inc',
    ),
    'scs_node_output' => array(
      'arguments' => array(
        'form' => NULL,
      ),
      'file' => 'scs.theme.inc',
    ),
  );
}

/*
 * Implementation of 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'];
    foreach ($nodes as $key => $selected) {
      if ($selected == 0) {
        unset($nodes[$key]);
      }
    }
    _scs_create_newsletter('', $nodes);
  }
}

/*
 * Get nodes avaiable for selection
 */
function _scs_get_nodes() {
  $nodes = array();
  $content_types = variable_get('scs_content_types', array());
  foreach ($content_types as $type => $selected) {
    if ($selected == '0') {
      unset($content_types[$type]);
    }
  }
  if (count($content_types) != 0) {
    $p = db_placeholders($content_types, 'varchar');
    $result = db_query("SELECT nid, title, created FROM {node} WHERE type IN ({$p})", $content_types);
    while ($node = db_fetch_object($result)) {
      $node->created = date('Y-m-d H:i:s', $node->created);
      $nodes[] = $node;
    }
  }
  return $nodes;
}

/*
 * Newsletter creator function
 */
function _scs_create_newsletter($title, $nodes) {
  global $user;

  //Title
  if ($title == '') {
    $title = t('Please edit the title of this newsletter');
  }

  //Create the body of the newsletter
  $body = '';
  foreach ($nodes as $node) {
    $node = node_load(array(
      'nid' => $node,
    ));
    $body .= theme('scs_node_output', $node);
  }
  $newsletter = new StdClass();
  $newsletter->type = 'simplenews';
  $newsletter->uid = $user->uid;
  $newsletter->title = $title;
  $newsletter->body = $body;
  $newsletter->teaser = node_teaser($body);
  $newsletter->filter = variable_get('filter_default_format', 1);
  $newsletter->status = 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;
  node_save($newsletter);
  drupal_goto('node/' . $newsletter->nid . '/edit');
}