newsletter_list.admin.inc in Newsletter 7.2
Admin page callbacks for the newsletter module.
File
modules/list/includes/newsletter_list.admin.incView source
<?php
/**
* @file
* Admin page callbacks for the newsletter module.
*/
/**
* Menu callback; Form to list subscriber lists on a table
* and provide certain actions.
*/
function newsletter_list($form, &$form_state) {
if (isset($form_state['storage']['confirm'])) {
$form['operation'] = array(
'#type' => 'hidden',
'#value' => 'delete',
);
$form['list'] = array(
'#type' => 'hidden',
'#value' => $form_state['values']['list'],
);
$lists = newsletter_list_load($form_state['values']['list']);
$output = t('Are you sure you want to delete the following newsletter lists?');
$output .= '<ul>';
foreach ($lists as $list) {
$output .= !empty($list) ? '<li>' . check_plain($list->title) . '</li>' : '';
}
$output .= '</ul>';
$output .= t('This action cannot be undone.');
return confirm_form($form, t('Delete the following?'), 'admin/config/media/newsletter/lists', filter_xss($output));
}
$form['options'] = array(
'#type' => 'fieldset',
'#title' => t('Options'),
'#attributes' => array(
'class' => array(
'container-inline',
),
),
);
$options['delete'] = t('Delete the selected lists');
$form['options']['operation'] = array(
'#type' => 'select',
'#title' => t('Operation'),
'#title_display' => 'invisible',
'#options' => $options,
'#default_value' => 'delete',
);
$form['options']['submit'] = array(
'#type' => 'submit',
'#value' => t('Update'),
);
$header = array(
'id' => array(
'data' => t('ID'),
'field' => 'nlid',
),
'title' => array(
'data' => t('Title'),
'field' => 'title',
),
'send_rate' => array(
'data' => t('Send Rate'),
'field' => 'send_rate',
),
'template' => array(
'data' => t('Template'),
'field' => 'ntid',
),
'created' => array(
'data' => t('Created'),
'field' => 'lists.created',
'sort' => 'desc',
),
'operations' => array(
'data' => t('Operations'),
),
);
$query = db_select('newsletter_list', 'lists')
->extend('PagerDefault')
->extend('TableSort');
$query
->join('field_data_field_newsletter_template', 'template_field', 'lists.nlid = template_field.entity_id');
$query
->join('newsletter_template', 'templates', 'template_field.field_newsletter_template_target_id = templates.ntid');
$query
->addField('templates', 'subject', 'template_title');
$query
->fields('lists');
$result = $query
->limit(50)
->orderByHeader($header)
->execute();
$options = array();
$destination = drupal_get_destination();
foreach ($result as $row) {
$operations = array(
'data' => array(
array(
'#type' => 'link',
'#title' => t('edit'),
'#href' => 'admin/config/media/newsletter/lists/edit/' . $row->nlid,
'#options' => array(
'query' => $destination,
),
),
array(
'#markup' => ' | ',
),
array(
'#type' => 'link',
'#title' => t('delete'),
'#href' => 'admin/config/media/newsletter/lists/delete/' . $row->nlid,
'#options' => array(
'query' => $destination,
),
),
),
);
$options[$row->nlid] = array(
'id' => (int) $row->nlid,
'title' => check_plain($row->title),
'send_rate' => check_plain($row->send_rate),
'template' => check_plain($row->template_title),
'created' => format_date($row->created, 'short'),
'operations' => $operations,
);
}
$form['list'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
'#empty' => t('No lists available.'),
);
$form['pager'] = array(
'#theme' => 'pager',
);
return $form;
}
function newsletter_list_submit($form, &$form_state) {
$operation = $form_state['values']['operation'];
$ids = array_filter($form_state['values']['list']);
if ($ids) {
switch ($operation) {
case 'delete':
if (!isset($form_state['storage']['confirm'])) {
$form_state['storage']['confirm'] = TRUE;
$form_state['rebuild'] = TRUE;
return;
}
foreach ($ids as $id) {
entity_get_controller('newsletter_list')
->delete($id);
}
drupal_set_message(t('Lists deleted successfully'), 'status');
break;
}
}
else {
drupal_set_message(t('Please Select an action to perform.'), 'warning');
}
}
/**
* Menu callback; add a subscribers list.
*/
function newsletter_list_add() {
$list = entity_get_controller('newsletter_list')
->create();
return drupal_get_form('newsletter_list_edit', $list);
}
/**
* Menu callback; delete a subscribers list.
*/
function newsletter_list_delete($form, &$form_state, $list) {
$form['id'] = array(
'#type' => 'hidden',
'#value' => $list->nlid,
);
$output = t('Are you sure you want to delete the following newsletter list?');
$output .= '<ul>';
$output .= '<li>' . check_plain($list->title) . '</li>';
$output .= '</ul>';
$output .= t('This action cannot be undone.');
return confirm_form($form, t('Delete the following?'), 'admin/config/media/newsletter/lists', filter_xss($output));
}
function newsletter_list_delete_submit($form, &$form_state) {
$id = $form_state['values']['id'];
$deleted = entity_get_controller('newsletter_list')
->delete($id);
isset($deleted) ? drupal_set_message(t('List deleted successfully'), 'status') : drupal_set_message(t('An unknown error occured,please try again'), 'error');
$form_state['redirect'] = isset($_GET['destination']) ? drupal_get_destination() : 'admin/config/newsletter/lists';
}
/**
* Menu callback; edit a subscribers list.
*/
function newsletter_list_edit($form, &$form_state, $list) {
if (isset($list->nlid)) {
$form['nlid'] = array(
'#type' => 'hidden',
'#value' => (int) $list->nlid,
);
}
$form['title'] = array(
'#type' => 'textfield',
'#default_value' => check_plain($list->title),
'#title' => 'Name',
'#weight' => -10,
'#required' => TRUE,
);
$form['send_rate'] = array(
'#type' => 'select',
'#options' => array(
'Custom' => t('Custom'),
'Manual' => t('Manual'),
'Daily' => t('Daily'),
'Weekly' => t('Weekly'),
'Monthly' => t('Monthly'),
),
'#default_value' => check_plain($list->send_rate),
'#title' => t('Send rate'),
);
$form['send_rate_custom'] = array(
'#attributes' => array(
'id' => array(
'send_rate_custom',
),
),
'#type' => 'textfield',
'#title' => t('Custom send rate'),
'#default_value' => is_numeric($list->send_rate) ? $list->send_rate : NULL,
'#maxlength' => 4,
'#size' => 4,
'#description' => t('Only fill this option if you selected "Custom" Send rate'),
'#field_prefix' => t('Once'),
'#field_suffix' => t('nodes of this list are published.'),
);
field_attach_form('newsletter_list', $list, $form, $form_state);
// Remove Basic Templates
$lang = $form['field_newsletter_template']['#language'];
unset($form['field_newsletter_template'][$lang]['#options'][1]);
unset($form['field_newsletter_template'][$lang]['#options'][2]);
unset($form['field_newsletter_template'][$lang]['#options'][3]);
$form['submit'] = array(
'#type' => 'submit',
'#value' => isset($list->nlid) ? t('Update') : t('Save'),
);
$form['cancel'] = array(
'#type' => 'link',
'#title' => t('Cancel'),
'#href' => 'admin/config/media/newsletter/lists',
);
$form['#list'] = $list;
return $form;
}
function newsletter_list_edit_validate($form, &$form_state) {
if ($form_state['values']['send_rate'] == 'Custom') {
if (empty($form_state['values']['send_rate_custom'])) {
form_set_error('send_rate_custom', t('You must set the custom send rate first.'));
}
elseif (!is_numeric($form_state['values']['send_rate_custom'])) {
form_set_error('send_rate_custom', t('Custom send rate must be a number.'));
}
}
// Ensure list title is unique
if ($form['#list']->title != $form_state['values']['title'] || !isset($form_state['values']['nlid'])) {
$existing = db_query('SELECT title FROM {newsletter_list} WHERE title = :title', array(
'title' => $form_state['values']['title'],
))
->fetchField();
if ($existing) {
form_set_error('title', t('This list name already exists'));
}
}
}
function newsletter_list_edit_submit($form, &$form_state) {
$list = (object) $form_state['values'];
$list->send_rate = $list->send_rate == 'Custom' ? $form_state['values']['send_rate_custom'] : $list->send_rate;
if (isset($form_state['values']['nlid'])) {
$list->id = $form_state['values']['nlid'];
}
field_attach_submit('newsletter_list', $list, $form, $form_state);
$updated = entity_get_controller('newsletter_list')
->save($list);
isset($updated) ? drupal_set_message(t('List updated successfully'), 'status') : drupal_set_message(t('An unknown error occured,please try again'), 'error');
$form_state['redirect'] = 'admin/config/media/newsletter/lists';
}
Functions
Name![]() |
Description |
---|---|
newsletter_list | Menu callback; Form to list subscriber lists on a table and provide certain actions. |
newsletter_list_add | Menu callback; add a subscribers list. |
newsletter_list_delete | Menu callback; delete a subscribers list. |
newsletter_list_delete_submit | |
newsletter_list_edit | Menu callback; edit a subscribers list. |
newsletter_list_edit_submit | |
newsletter_list_edit_validate | |
newsletter_list_submit |