Index: simplenews.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/simplenews/simplenews.module,v retrieving revision 1.220.2.30 diff -u -r1.220.2.30 simplenews.module --- simplenews.module 19 May 2010 14:02:24 -0000 1.220.2.30 +++ simplenews.module 13 Jun 2010 17:12:53 -0000 @@ -176,7 +176,7 @@ 'title' => 'Subscriptions', 'type' => MENU_CALLBACK, 'page callback' => 'drupal_get_form', - 'page arguments' => array('simplenews_admin_users_form', 5), + 'page arguments' => array('simplenews_subscriptions_admin_form', 5), 'access arguments' => array('administer simplenews subscriptions'), 'file' => 'simplenews.subscription.inc', ); @@ -276,7 +276,7 @@ 'title' => 'Manage newsletter subscriptions', 'type' => MENU_CALLBACK, 'page callback' => 'drupal_get_form', - 'page arguments' => array('simplenews_subscription_manager_form'), + 'page arguments' => array('simplenews_subscriptions_account_form'), 'access arguments' => array('subscribe to newsletters'), 'file' => 'simplenews.subscription.inc', ); @@ -861,7 +861,7 @@ case 'form': if ($category == 'newsletter') { $subscription = simplenews_get_subscription($account); - $form = _simplenews_subscription_manager_form($subscription); + $form = __simplenews_subscriptions_account_form($subscription); $form['subscriptions']['#title'] = t('Current newsletter subscriptions'); unset($form['update'], $form['subscriptions']['mail']); return $form; @@ -950,6 +950,12 @@ switch ($op) { case 'list': $blocks = array(); + + //Special block for multisignup + $blocks[0] = array( + 'info' => t('Newsletter: Multiple Sign-up'), + ); + foreach (simplenews_get_newsletters(variable_get('simplenews_vid', '')) as $newsletter) { //TODO: 1. without form -> by role; 2. with form -> user caching with refresh on subscribe/unsubscribe (option as setting) or no caching // Only list a block if the newsletter is not Hidden. @@ -960,66 +966,93 @@ } return $blocks; case 'configure': - $form['simplenews_block_'. $delta]['simplenews_block_m_'. $delta] = array( - '#type' => 'textfield', - '#title' => t('Block message'), - '#size' => 60, - '#maxlength' => 128, - '#default_value' => variable_get('simplenews_block_m_'. $delta, t('Stay informed on our latest news!')), - ); - $form['simplenews_block_'. $delta]['simplenews_block_f_'. $delta] = array( - '#type' => 'radios', - '#title' => t('Subscription interface'), - '#options' => array('1' => t('Subscription form'), '0' => t('Link to form')), - '#description' => t("Note: this requires permission 'subscribe to newsletters'."), - '#default_value' => variable_get('simplenews_block_f_'. $delta, 1), - ); - $form['simplenews_block_'. $delta]['simplenews_block_l_'. $delta] = array( - '#type' => 'checkbox', - '#title' => t('Display link to previous issues'), - '#return_value' => 1, - '#default_value' => variable_get('simplenews_block_l_'. $delta, 1), - ); - $form['simplenews_block_'. $delta]['simplenews_block_i_status_'. $delta] = array( - '#type' => 'checkbox', - '#title' => t('Display previous issues'), - '#return_value' => 1, - '#default_value' => variable_get('simplenews_block_i_status_'. $delta, 0), - ); - $form['simplenews_block_'. $delta]['simplenews_block_i_'. $delta] = array( - '#type' => 'select', - '#title' => t('Number of issues to display'), - '#options' => drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)), - '#default_value' => variable_get('simplenews_block_i_'. $delta, 5), - ); - $form['simplenews_block_'. $delta]['simplenews_block_r_'. $delta] = array( - '#type' => 'checkbox', - '#title' => t('Display RSS-feed icon'), - '#return_value' => 1, - '#default_value' => variable_get('simplenews_block_r_'. $delta, 1), - ); - return $form; + //Special block for multisignup + if ($delta == 0) { + $form['simplenews_multi_signup_block_help'] = array( + '#type' => 'textarea', + '#title' => t('Help text'), + '#default_value' => variable_get('simplenews_multi_signup_block_help', t('Select the newsletter(s) to which you want to subscribe or unsubscribe.')), + '#description' => t("Use this option to change the help message displayed above the form."), + ); + return $form; + } + else { + $form['simplenews_block_'. $delta]['simplenews_block_m_'. $delta] = array( + '#type' => 'textfield', + '#title' => t('Block message'), + '#size' => 60, + '#maxlength' => 128, + '#default_value' => variable_get('simplenews_block_m_'. $delta, t('Stay informed on our latest news!')), + ); + $form['simplenews_block_'. $delta]['simplenews_block_f_'. $delta] = array( + '#type' => 'radios', + '#title' => t('Subscription interface'), + '#options' => array('1' => t('Subscription form'), '0' => t('Link to form')), + '#description' => t("Note: this requires permission 'subscribe to newsletters'."), + '#default_value' => variable_get('simplenews_block_f_'. $delta, 1), + ); + $form['simplenews_block_'. $delta]['simplenews_block_l_'. $delta] = array( + '#type' => 'checkbox', + '#title' => t('Display link to previous issues'), + '#return_value' => 1, + '#default_value' => variable_get('simplenews_block_l_'. $delta, 1), + ); + $form['simplenews_block_'. $delta]['simplenews_block_i_status_'. $delta] = array( + '#type' => 'checkbox', + '#title' => t('Display previous issues'), + '#return_value' => 1, + '#default_value' => variable_get('simplenews_block_i_status_'. $delta, 0), + ); + $form['simplenews_block_'. $delta]['simplenews_block_i_'. $delta] = array( + '#type' => 'select', + '#title' => t('Number of issues to display'), + '#options' => drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)), + '#default_value' => variable_get('simplenews_block_i_'. $delta, 5), + ); + $form['simplenews_block_'. $delta]['simplenews_block_r_'. $delta] = array( + '#type' => 'checkbox', + '#title' => t('Display RSS-feed icon'), + '#return_value' => 1, + '#default_value' => variable_get('simplenews_block_r_'. $delta, 1), + ); + return $form; + } case 'save': - variable_set('simplenews_block_m_'. $delta, $edit['simplenews_block_m_'. $delta]); - variable_set('simplenews_block_f_'. $delta, $edit['simplenews_block_f_'. $delta]); - variable_set('simplenews_block_l_'. $delta, $edit['simplenews_block_l_'. $delta]); - variable_set('simplenews_block_i_status_'. $delta, $edit['simplenews_block_i_status_'. $delta]); - variable_set('simplenews_block_i_'. $delta, $edit['simplenews_block_i_'. $delta]); - variable_set('simplenews_block_r_'. $delta, $edit['simplenews_block_r_'. $delta]); + //Special block for multisignup + if ($delta == 0) { + variable_set('simplenews_multi_signup_block_help', $edit['simplenews_multi_signup_block_help']); + } + else { + variable_set('simplenews_block_m_'. $delta, $edit['simplenews_block_m_'. $delta]); + variable_set('simplenews_block_f_'. $delta, $edit['simplenews_block_f_'. $delta]); + variable_set('simplenews_block_l_'. $delta, $edit['simplenews_block_l_'. $delta]); + variable_set('simplenews_block_i_status_'. $delta, $edit['simplenews_block_i_status_'. $delta]); + variable_set('simplenews_block_i_'. $delta, $edit['simplenews_block_i_'. $delta]); + variable_set('simplenews_block_r_'. $delta, $edit['simplenews_block_r_'. $delta]); + } break; case 'view': - global $language; - - $newsletters = simplenews_get_newsletters(variable_get('simplenews_vid', '')); - // Only display a block if $delta is a valid newsletter term id. - if (in_array($delta, array_keys($newsletters))) { - // $delta is validated, the block can be displayed. + //Special block for multisignup + if ($delta == 0 && user_access('subscribe to newsletters')) { $block = array( - 'subject' => check_plain($newsletters['$delta']), - 'content' => theme(array('simplenews_block__'. $delta, 'simplenews_block'), $delta), + 'content' => _simplenews_multi_signup_block(), ); return $block; } + else { + global $language; + + $newsletters = simplenews_get_newsletters(variable_get('simplenews_vid', '')); + // Only display a block if $delta is a valid newsletter term id. + if (in_array($delta, array_keys($newsletters))) { + // $delta is validated, the block can be displayed. + $block = array( + 'subject' => check_plain($newsletters['$delta']), + 'content' => theme(array('simplenews_block__'. $delta, 'simplenews_block'), $delta), + ); + return $block; + } + } break; } } @@ -1308,11 +1341,11 @@ } /** - * Build subscription manager form. + * Build edit own subscriptions form. * * @param object $subscription subscription object */ -function _simplenews_subscription_manager_form($subscription) { +function _simplenews_subscriptions_account_form($subscription) { $form = array(); $options = array(); $default_value = array(); @@ -1367,6 +1400,143 @@ '#weight' => 30, ); } + + $form['#validate'][] = 'simplenews_subscriptions_account_form_validate'; + $form['#submit'][] = 'simplenews_subscriptions_account_form_submit'; + $form['#redirect'] = ''; //Return to home page after (un)subscribe + + return $form; +} + +/** + * Build admin all subscriptions form. + * + * @param object $subscription subscription object + */ +function _simplenews_subscriptions_admin_form($subscription) { + $form = array(); + $options = array(); + $default_value = array(); + global $language; + + // Get newsletters for subscription form checkboxes. + // Newsletters with opt-in/out method 'hidden' will not be listed. + foreach(simplenews_get_newsletters(variable_get('simplenews_vid', '')) as $newsletter) { + $options[$newsletter->tid] = check_plain($newsletter->name); + $default_value[$newsletter->tid] = FALSE; + } + + $form['subscriptions'] = array( + '#type' => 'fieldset', + '#description' => t('Select the newsletter(s) to which you want to subscribe or unsubscribe.'), + ); + $form['subscriptions']['newsletters'] = array( + '#type' => 'checkboxes', + '#options' => $options, + '#default_value' => array_merge($default_value, (array)$subscription->tids), + ); + + // If current user is logged in, just display email. + // Anonymous users see an email box and will receive confirmations + if ($subscription->mail) { + $form['subscriptions']['#title'] = t('Subscriptions for %mail', array('%mail' => $subscription->mail)); + $form['subscriptions']['mail'] = array('#type' => 'value', '#value' => $subscription->mail); + $form['update'] = array( + '#type' => 'submit', + '#value' => t('Update'), + '#weight' => 20, + ); + } + else { + $form['subscriptions']['#title'] = t('Manage your newsletter subscriptions'); + $form['subscriptions']['mail'] = array( + '#type' => 'textfield', + '#title' => t('email'), + '#size' => 20, + '#maxlength' => 128, + '#weight' => 10, + '#required' => TRUE, + ); + $form['subscribe'] = array( + '#type' => 'submit', + '#value' => t('Subscribe'), + '#weight' => 20, + ); + $form['unsubscribe'] = array( + '#type' => 'submit', + '#value' => t('Unsubscribe'), + '#weight' => 30, + ); + } + + $form['#validate'][] = 'simplenews_subscriptions_admin_form_validate'; + $form['#submit'][] = 'simplenews_subscriptions_admin_form_submit'; + $form['#redirect'] = 'admin/content/simplenews/users'; + + return $form; +} + +/** + * Build edit own subscriptions form for the multisignup block. + * + * @param object $subscription subscription object + */ +function _simplenews_subscriptions_multisignup_block_form($subscription) { + $form = array(); + $options = array(); + $default_value = array(); + global $language; + + // Get newsletters for subscription form checkboxes. + // Newsletters with opt-in/out method 'hidden' will not be listed. + foreach(simplenews_get_newsletters(variable_get('simplenews_vid', '')) as $newsletter) { + $options[$newsletter->tid] = check_plain($newsletter->name); + $default_value[$newsletter->tid] = FALSE; + } + + $form['subscriptions']['newsletters'] = array( + '#type' => 'checkboxes', + '#options' => $options, + '#default_value' => array_merge($default_value, (array)$subscription->tids), + ); + + // If current user is logged in, just display email. + // Anonymous users see an email box and will receive confirmations + if ($subscription->mail) { + $form['subscriptions']['#title'] = t('Subscriptions for %mail', array('%mail' => $subscription->mail)); + $form['subscriptions']['mail'] = array('#type' => 'value', '#value' => $subscription->mail); + $form['update'] = array( + '#type' => 'submit', + '#value' => t('Update'), + '#weight' => 20, + ); + } + else { + $form['subscriptions']['#title'] = t('Manage your newsletter subscriptions'); + $form['subscriptions']['mail'] = array( + '#type' => 'textfield', + '#title' => t('email'), + '#size' => 20, + '#maxlength' => 128, + '#weight' => 10, + '#required' => TRUE, + ); + $form['subscribe'] = array( + '#type' => 'submit', + '#value' => t('Subscribe'), + '#weight' => 20, + ); + $form['unsubscribe'] = array( + '#type' => 'submit', + '#value' => t('Unsubscribe'), + '#weight' => 30, + ); + } + + $form['#validate'][] = 'simplenews_subscriptions_account_form_validate'; + $form['#submit'][] = 'simplenews_subscriptions_account_form_submit'; + $form['#redirect'] = ''; //Return to home page after (un)subscribe + return $form; } @@ -2701,6 +2871,10 @@ 'file' => 'simplenews.admin.inc', 'arguments' => array('form' => NULL), ), + 'simplenews_multi_signup_block' => array( + 'rendered_form' => NULL, + 'help' => NULL, + ), ); } @@ -2785,3 +2959,29 @@ $variables['unsubscribe_text'] = t('Unsubscribe from this newsletter', array(), $variables['language']->language); $variables['test_message'] = t('This is a test version of the newsletter.', array(), $variables['language']->language); } + +/** + * Build subscription block. + * + * @return string + */ +function _simplenews_multi_signup_block() { + $help = variable_get('simplenews_multi_signup_block_help', t('Select the newsletter(s) to which you want to subscribe or unsubscribe.')); + + module_load_include('inc', 'simplenews', 'simplenews.subscription'); + $rendered_form = drupal_get_form('simplenews_subscriptions_multisignup_block_form'); + $content = theme('simplenews_multi_signup_block', $rendered_form, check_plain($help)); + + return $content; +} + +/** + * Theme signup block. + * + * @param string $rendered_form + * @param string $help + * @return string + */ +function theme_simplenews_multi_signup_block($rendered_form, $help) { + return '
' . $help . '
' . $rendered_form; +} Index: simplenews.subscription.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/simplenews/simplenews.subscription.inc,v retrieving revision 1.13.2.3 diff -u -r1.13.2.3 simplenews.subscription.inc --- simplenews.subscription.inc 19 Apr 2010 08:49:34 -0000 1.13.2.3 +++ simplenews.subscription.inc 13 Jun 2010 17:07:42 -0000 @@ -11,10 +11,10 @@ /** * Menu callback: Generates the subscription form for users. * - * @see simplenews_subscription_manager_form_validate() - * @see simplenews_subscription_manager_form_submit() + * @see simplenews_subscriptions_account_form_validate() + * @see simplenews_subscriptions_account_form_submit() */ -function simplenews_subscription_manager_form(&$form_state, $snid = NULL) { +function simplenews_subscriptions_account_form(&$form_state, $snid = NULL) { global $user; if (isset($snid)) { @@ -26,20 +26,23 @@ $subscription = simplenews_get_subscription($user); } - // If non-admin is trying to edit someone else's subscription, access denied. - if ($user->uid && $user->uid != $subscription->uid && !user_access('administer simplenews subscriptions')) { + // If admin trying to edit someone else's subscriptions redirect to admin_all_subscriptions page + if (user_access('administer simplenews subscriptions') && $user->uid != $subscription->uid) { + drupal_goto('admin/content/simplenews/users/edit/' . $snid); + return; + } + + // If someone is trying to edit someone else's subscription, access denied. + if ($user->uid && $user->uid != $subscription->uid) { drupal_access_denied(); return; } - $form = _simplenews_subscription_manager_form($subscription); - $form['#validate'][] = 'simplenews_subscription_manager_form_validate'; - $form['#submit'][] = 'simplenews_subscription_manager_form_submit'; - $form['#redirect'] = ''; //Return to home page after (un)subscribe + $form = _simplenews_subscriptions_account_form($subscription); return $form; } -function simplenews_subscription_manager_form_validate($form, &$form_state) { +function simplenews_subscriptions_account_form_validate($form, &$form_state) { $valid_email = valid_email_address($form_state['values']['mail']); if (!$valid_email) { form_set_error('mail', t('The email address you supplied is not valid.')); @@ -52,7 +55,7 @@ } } -function simplenews_subscription_manager_form_submit($form, &$form_state) { +function simplenews_subscriptions_account_form_submit($form, &$form_state) { switch ($form_state['values']['op']) { case t('Update'): // We first subscribe, then unsubscribe. This prevents deletion of subscriptions @@ -214,9 +217,139 @@ /** * Menu callback: handle the edit subscription page and a subscription * page for anonymous users. + * + * @see simplenews_subscriptions_admin_form_validate() + * @see simplenews_subscriptions_admin_form_submit() */ -function simplenews_admin_users_form(&$form_state, $snid = NULL) { - $form = simplenews_subscription_manager_form($form_state, $snid); - $form['#redirect'] = 'admin/content/simplenews/users'; +function simplenews_subscriptions_admin_form(&$form_state, $snid) { + global $user; + + $account = new stdClass(); + $account->snid = $snid; + $subscription = simplenews_get_subscription($account); + + // If non-admin, access denied. + if (!user_access('administer simplenews subscriptions')) { + drupal_access_denied(); + return; + } + + $form = _simplenews_subscriptions_admin_form($subscription); return $form; } + +function simplenews_subscriptions_admin_form_validate($form, &$form_state) { + $valid_email = valid_email_address($form_state['values']['mail']); + if (!$valid_email) { + form_set_error('mail', t('The email address you supplied is not valid.')); + } + $checked_newsletters = array_filter($form_state['values']['newsletters']); + $account = new stdClass(); + $account->mail = $form_state['values']['mail']; + if (!count($checked_newsletters) && !simplenews_get_subscription($account)) { + form_set_error('newsletters', t('You must select at least one newsletter.')); + } +} + +function simplenews_subscriptions_admin_form_submit($form, &$form_state) { + switch ($form_state['values']['op']) { + case t('Update'): + // We first subscribe, then unsubscribe. This prevents deletion of subscriptions + // when unsubscribed from the + arsort($form_state['values']['newsletters'], SORT_NUMERIC); + foreach ($form_state['values']['newsletters'] as $tid => $checked) { + if ($checked) { + simplenews_subscribe_user($form_state['values']['mail'], $tid, FALSE, 'website'); + } + else { + simplenews_unsubscribe_user($form_state['values']['mail'], $tid, FALSE, 'website'); + } + } + drupal_set_message(t('The newsletter subscriptions for %mail have been updated.', array('%mail' => $form_state['values']['mail']))); + break; + case t('Subscribe'): + foreach ($form_state['values']['newsletters'] as $tid => $checked) { + if ($checked) { + simplenews_subscribe_user($form_state['values']['mail'], $tid, TRUE, 'website'); + } + } + drupal_set_message(t('A confirmation email will be sent shortly containing further instructions on how to complete your subscription.')); + break; + case t('Unsubscribe'): + foreach ($form_state['values']['newsletters'] as $tid => $checked) { + if ($checked) { + simplenews_unsubscribe_user($form_state['values']['mail'], $tid, TRUE, 'website'); + } + } + drupal_set_message(t('A confirmation email will be sent shortly containing further instructions on how to cancel your subscription.')); + break; + } +} + +/** + * Menu callback: Generates the subscription form for users for the multisignup block. + * + * @see simplenews_subscriptions_multisignup_block_form_validate() + * @see simplenews_subscriptions_multisignup_block_form_submit() + */ +function simplenews_subscriptions_multisignup_block_form(&$form_state) { + global $user; + + $subscription = simplenews_get_subscription($user); + + // If someone not authorized to edit their subscription, return empty form. + if (!user_access('subscribe to newsletters')) { + return; + } + + $form = _simplenews_subscriptions_multisignup_block_form($subscription); + return $form; +} + +function simplenews_subscriptions_multisignup_block_form_validate($form, &$form_state) { + $valid_email = valid_email_address($form_state['values']['mail']); + if (!$valid_email) { + form_set_error('mail', t('The email address you supplied is not valid.')); + } + $checked_newsletters = array_filter($form_state['values']['newsletters']); + $account = new stdClass(); + $account->mail = $form_state['values']['mail']; + if (!count($checked_newsletters) && !simplenews_get_subscription($account)) { + form_set_error('newsletters', t('You must select at least one newsletter.')); + } +} + +function simplenews_subscriptions_multisignup_block_form_submit($form, &$form_state) { + switch ($form_state['values']['op']) { + case t('Update'): + // We first subscribe, then unsubscribe. This prevents deletion of subscriptions + // when unsubscribed from the + arsort($form_state['values']['newsletters'], SORT_NUMERIC); + foreach ($form_state['values']['newsletters'] as $tid => $checked) { + if ($checked) { + simplenews_subscribe_user($form_state['values']['mail'], $tid, FALSE, 'website'); + } + else { + simplenews_unsubscribe_user($form_state['values']['mail'], $tid, FALSE, 'website'); + } + } + drupal_set_message(t('The newsletter subscriptions for %mail have been updated.', array('%mail' => $form_state['values']['mail']))); + break; + case t('Subscribe'): + foreach ($form_state['values']['newsletters'] as $tid => $checked) { + if ($checked) { + simplenews_subscribe_user($form_state['values']['mail'], $tid, TRUE, 'website'); + } + } + drupal_set_message(t('You will receive a confirmation email shortly containing further instructions on how to complete your subscription.')); + break; + case t('Unsubscribe'): + foreach ($form_state['values']['newsletters'] as $tid => $checked) { + if ($checked) { + simplenews_unsubscribe_user($form_state['values']['mail'], $tid, TRUE, 'website'); + } + } + drupal_set_message(t('You will receive a confirmation email shortly containing further instructions on how to cancel your subscription.')); + break; + } +}