diff --git a/includes/simplenews.admin.inc b/includes/simplenews.admin.inc index 7d0303f..fbc62fa 100644 --- a/includes/simplenews.admin.inc +++ b/includes/simplenews.admin.inc @@ -321,30 +321,45 @@ function simplenews_build_issue_filter_query(SelectQueryInterface $query) { * * @ingroup forms * @see simplenews_admin_newsletter_categories_submit() - * @see theme_simplenews_admin_newsletter_categories() + * @see theme_simplenews_admin_categories() */ function simplenews_admin_categories() { $form['#tree'] = TRUE; + if ($categories = simplenews_categories_load_multiple(array(), array('show_all' => TRUE))) { foreach ($categories as $category) { - $form[$category->tid]['#category'] = $category; - $form[$category->tid]['name'] = array('#markup' => check_plain(_simplenews_newsletter_name($category))); - $form[$category->tid]['count'] = array('#markup' => simplenews_count_subscriptions($category->tid)); - $form[$category->tid]['optinout'] = array('#markup' => ucfirst($category->opt_inout)); - - $form[$category->tid]['edit'] = array( + $tid = $category->tid; + $form[$tid]['#category'] = $category; + $form[$tid]['#disabled'] = $category->disabled; + $form[$tid]['name'] = array('#markup' => check_plain(_simplenews_newsletter_name($category))); + $form[$tid]['count'] = array('#markup' => simplenews_count_subscriptions($tid)); + $form[$tid]['optinout'] = array('#markup' => ucfirst($category->opt_inout)); + + $form[$tid]['edit'] = array( '#type' => 'dropbutton', '#links' => array( 'edit' => array( 'title' => t('Edit category'), - 'href' => "admin/config/services/simplenews/categories/$category->tid/edit", - ), - 'delete' => array( - 'title' => t('Delete'), - 'href' => "admin/config/services/simplenews/categories/$category->tid/delete", + 'href' => "admin/config/services/simplenews/categories/$tid/edit", ), ), ); + if ($category->disabled) { + $form[$tid]['edit']['#links']['enable'] = array( + 'title' => t('Enable'), + 'href' => "admin/config/services/simplenews/categories/$tid/enable", + ); + } + else { + $form[$tid]['edit']['#links']['disable'] = array( + 'title' => t('Disable'), + 'href' => "admin/config/services/simplenews/categories/$tid/disable", + ); + } + $form[$tid]['edit']['#links']['delete'] = array( + 'title' => t('Delete'), + 'href' => "admin/config/services/simplenews/categories/$tid/delete", + ); } } @@ -359,8 +374,8 @@ function simplenews_admin_categories() { function theme_simplenews_admin_categories($variables) { $form = $variables['form']; - $rows = array(); - + $enabled_rows = array(); + $disabled_rows = array(); foreach (element_children($form) as $key) { if (isset($form[$key]['name'])) { $category = &$form[$key]; @@ -374,25 +389,50 @@ function theme_simplenews_admin_categories($variables) { $row[] = backdrop_render($category['weight']); } $row[] = backdrop_render($category['edit']); - $rows[] = array( - 'data' => $row, - ); + + if ($category['#disabled']) { + $disabled_rows[] = array( + 'data' => $row, + 'class' => array('disabled'), + ); + } + else { + $enabled_rows[] = array( + 'data' => $row, + ); + } } } - $header = array(t('Newsletter category name')); + $header = array(t('Newsletter category')); $header[] = t('Subscribers'); $header[] = t('Opt In/Out status'); + $header[] = t('Operations'); if (isset($form['submit'])) { $header[] = t('Weight'); backdrop_add_tabledrag('newsletter-category', 'order', 'self', 'simplenews-category-weight'); } - $header[] = array( - 'data' => t('Operations'), - 'colspan' => '3', + + $empty_text = t('No newsletter categories. Add category.', array('@link' => url('admin/config/services/simplenews/add'))); + + $enabled_table = array( + 'header' => $header, + 'rows' => $enabled_rows, + 'empty' => $empty_text, + 'attributes' => array('id' => 'newsletter-category'), + ); + $disabled_table = array( + 'header' => $header, + 'rows' => $disabled_rows, + 'attributes' => array('id' => 'newsletter-category'), ); - return theme('table', array('header' => $header, 'rows' => $rows, 'empty' => t('No newsletter categories. Add category.', array('@link' => url('admin/config/services/simplenews/add'))), 'attributes' => array('id' => 'newsletter-category'))) . backdrop_render_children($form); + $output = theme('table', $enabled_table); + $output .= '

' . t('Disabled Newsletters') . '

'; + $output .= theme('table', $disabled_table); + $output .= backdrop_render_children($form); + + return $output; } /** @@ -416,6 +456,7 @@ function simplenews_admin_category_form($form, &$form_state, $edit = array()) { 'new_account' => 'none', 'opt_inout' => 'double', 'block' => 1, + 'disabled' => 0, 'format' => $config->get('format'), 'priority' => $config->get('priority'), 'receipt' => $config->get('receipt'), @@ -494,6 +535,12 @@ function simplenews_admin_category_form($form, &$form_state, $edit = array()) { '#description' => t('A subscription block will be provided for this newsletter category. Anonymous and authenticated users can subscribe and unsubscribe using this block.'), ); + // Add disabled state. + $form['subscription']['disabled'] = array( + '#type' => 'value', + '#value' => $edit['disabled'], + ); + $form['email'] = array( '#type' => 'fieldset', '#title' => t('Email settings'), @@ -728,6 +775,54 @@ function simplenews_admin_category_delete_submit($form, &$form_state) { return; } +/** + * Menu callback: Enable / Disable newsletter category. + */ +function simplenews_newsletter_change_status($category, $action) { + // Get the current status. + $success = FALSE; + $query = "SELECT disabled FROM {simplenews_category} WHERE tid = :tid"; + $substitutions = array(':tid' => $category->tid); + $disabled = db_query($query, $substitutions)->fetchField(); + + // Attempt to change the status. + switch ($action) { + case 'disable': + if (!$disabled) { + $query = db_update('simplenews_category'); + $query->fields(array('disabled' => TRUE)); + $query->condition('tid', $category->tid); + $success = $query->execute(); + } + break; + case 'enable': + if ($disabled) { + $query = db_update('simplenews_category'); + $query->fields(array('disabled' => FALSE)); + $query->condition('tid', $category->tid); + $success = $query->execute(); + } + break; + } + + // Alert the person requestig the change. + $name = check_plain($category->name); + $verb = $action . 'd'; + $replacements = array( + '@name' => $name, + '@verb' => $verb, + ); + if ($success) { + $message = t('The @name category has been @verb.', $replacements); + } + else { + $message = t('The @name category has already been @verb.', $replacements, 'warning'); + } + + backdrop_set_message($message); + backdrop_goto('admin/config/services/simplenews'); +} + /** * Menu callback: Mass subscribe to newsletters. * diff --git a/simplenews.install b/simplenews.install index 375209a..7194dd6 100644 --- a/simplenews.install +++ b/simplenews.install @@ -89,6 +89,13 @@ function simplenews_schema() { 'not null' => TRUE, 'default' => 0, ), + 'disabled' => array( + 'description' => 'Determines whether this category appears as an option on the newsletter issue.', + 'type' => 'int', + 'size' => 'tiny', + 'not null' => TRUE, + 'default' => 0, + ), ), 'primary key' => array('tid'), ); @@ -617,6 +624,21 @@ function simplenews_update_1004() { return t('Per-category newsletter status table added.'); } +/** + * Add the 'disabled' column to the simplenews database table. + */ +function simplenews_update_1005() { + $schema = array( + 'description' => 'Determines whether this category appears as an option for the newsletter issue.', + 'type' => 'int', + 'size' => 'tiny', + 'not null' => TRUE, + 'default' => 0, + ); + db_add_field('simplenews_category', 'disabled', $schema); + + return t('Added a disabled option for each simplenews category.'); +} /** * Create SimpleNews node type. diff --git a/simplenews.module b/simplenews.module index b2524ad..fe78dc5 100644 --- a/simplenews.module +++ b/simplenews.module @@ -119,19 +119,33 @@ function simplenews_menu() { 'type' => MENU_DEFAULT_LOCAL_TASK, ); $items['admin/config/services/simplenews/categories/%simplenews_category/edit'] = array( - 'title' => 'Newsletters', + 'title' => 'Edit Newsletters', 'page callback' => 'backdrop_get_form', 'page arguments' => array('simplenews_admin_category_form', 5), 'access arguments' => array('administer newsletters'), 'file' => 'includes/simplenews.admin.inc', ); $items['admin/config/services/simplenews/categories/%simplenews_category/delete'] = array( - 'title' => 'Newsletters', + 'title' => 'Delete Newsletters', 'page callback' => 'backdrop_get_form', 'page arguments' => array('simplenews_admin_category_delete', 5), 'access arguments' => array('administer newsletters'), 'file' => 'includes/simplenews.admin.inc', ); + $items['admin/config/services/simplenews/categories/%simplenews_category/enable'] = array( + 'title' => 'Enable Newsletters', + 'page callback' => 'simplenews_newsletter_change_status', + 'page arguments' => array(5, 6), + 'access arguments' => array('administer newsletters'), + 'file' => 'includes/simplenews.admin.inc', + ); + $items['admin/config/services/simplenews/categories/%simplenews_category/disable'] = array( + 'title' => 'Disable Newsletters', + 'page callback' => 'simplenews_newsletter_change_status', + 'page arguments' => array(5, 6), + 'access arguments' => array('administer newsletters'), + 'file' => 'includes/simplenews.admin.inc', + ); $items['admin/config/services/simplenews/add'] = array( 'title' => 'Add newsletter category', 'type' => MENU_LOCAL_ACTION, @@ -837,7 +851,6 @@ function simplenews_form_alter(&$form, &$form_state, $form_id) { * @todo */ function _simplenews_node_form(&$form, $form_state) { - // Display warning if the node is currently being sent. if (!empty($form['#node']->nid)) { $newsletter = simplenews_newsletter_load($form['#node']->nid); @@ -862,6 +875,39 @@ function _simplenews_node_form(&$form, $form_state) { } } +/** + * Implements hook_field_attach_form(). + */ +function simplenews_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) { + if ($entity_type == 'node') { + if (simplenews_check_node_types(array($entity->type))) { + $config = config('simplenews.settings'); + $field = $config->get('simplenews_category_field'); + if (isset($form[$field])) { + // Remove all disabled options. + foreach ($form[$field][LANGUAGE_NONE]['#options'] as $key => $label) { + if (is_numeric($key)) { + $query = "SELECT disabled FROM {simplenews_category} WHERE tid = :key"; + $disabled = db_query($query, array(':key' => $key))->fetchfield(); + if ($disabled) { + // If the selected newsletter is disabled, prevent it from being changed. + if (!empty($form[$field][LANGUAGE_NONE]['#default_value']) && + ($key == $form[$field][LANGUAGE_NONE]['#default_value'][0])) { + $form[$field]['#disabled'] = TRUE; + } + else { + // Otherwise, remove the option from the list. + unset($form[$field][LANGUAGE_NONE]['#options'][$key]); + } + } + } + } + } + } + } + +} + /** * Implements hook_entity_info_alter(). */ @@ -2045,6 +2091,7 @@ function simplenews_category_save($category) { 'new_account' => $category->new_account, 'opt_inout' => $category->opt_inout, 'block' => $category->block, + 'disabled' => $category->disabled, )) ->execute(); module_invoke_all('simplenews_category_update', $category);