View source
<?php
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;
}
function scs_perm() {
return array(
'administer scs',
'scs create newsletters',
);
}
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',
),
);
}
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';
}
}
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);
}
}
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;
}
function _scs_create_newsletter($title, $nodes) {
global $user;
if ($title == '') {
$title = t('Please edit the title of this 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');
}