--- simplenews_realname/simplenews_realname.module 2012-10-21 15:01:23.000000000 -0400 +++ simplenews_realname.module 2013-02-21 12:40:00.000000000 -0500 @@ -50,8 +50,14 @@ /** * Implementation of hook_form_alter(). */ -function simplenews_realname_form_alter(&$form, $form_state, $form_id) { +function simplenews_realname_form_alter(&$form, &$form_state, $form_id) { global $user; + + // Have an empty email for the anonymous user + if (!$user->uid) { + $user->mail = ''; + } + // Add textfield for real name to subscription forms. if (strpos($form_id, 'simplenews_block_form') !== FALSE || $form_id == 'simplenews_subscription_manager_form') { $realname_field = array( @@ -82,46 +88,63 @@ } switch ($form_id) { // Subscriptions list. - case 'simplenews_subscription_admin': + case 'simplenews_admin_subscription': // Table header. Used as tablesort default. - $header = array( - array('data' => t('Email'), 'field' => 'ss.mail', 'sort' => 'asc'), - array('data' => t('Username'), 'field' => 'u.name'), - array('data' => t('Real name'), 'field' => 'sr.realname'), - array('data' => t('Status'), 'field' => 'ss.activated'), - t('Operations') - ); - // Data collection with filter and sorting applied. - $filter = simplenews_build_subscription_filter_query(); - $query = ' - SELECT DISTINCT ss.*, u.name, sr.realname - FROM {simplenews_subscriptions} ss - LEFT JOIN {users} u ON ss.uid = u.uid - LEFT JOIN {simplenews_realname} sr ON ss.mail = sr.mail - INNER JOIN {simplenews_snid_tid} s ON ss.snid = s.snid - '. $filter['where']; - // $count_query used to count distinct records only. - $count_query = preg_replace('/SELECT.*\s*FROM /', 'SELECT COUNT(DISTINCT ss.mail) FROM ', $query); - $query .= tablesort_sql($header); - $result = pager_query($query, 30, 0, $count_query); - // Clear the current state to be able to ordering the table fields. - unset($form['admin']['mail'], $form['admin']['name'], $form['admin']['status'], $form['admin']['operations']); - $snids = array(); - // Subscription table and table pager. - while ($subscription = db_fetch_object($result)) { - $snids[$subscription->snid] = ''; - $form['admin']['mail'][$subscription->snid] = array('#value' => $subscription->mail); - $form['admin']['name'][$subscription->snid] = array('#value' => isset($subscription->uid) ? l($subscription->name, 'user/'. $subscription->uid) : $subscription->name); - $form['admin']['realname'][$subscription->snid] = array('#value' => $subscription->realname); - $form['admin']['status'][$subscription->snid] = array('#value' => theme('simplenews_status', $subscription->activated, 'activated')); - $form['admin']['operations'][$subscription->snid] = array('#value' => l(t('edit'), 'admin/content/simplenews/users/edit/'. $subscription->snid, array(), drupal_get_destination())); - } - $form['admin']['snids'] = array('#type' => 'checkboxes', '#options' => $snids); + $header = array( + 'realname' => array('data' => t('Real name'), 'field' => 'sr.realname'), + 'mail' => array('data' => t('Email'), 'field' => 'sn.mail', 'sort' => 'asc'), + 'username' => array('data' => t('Username'), 'field' => 'u.name'), + 'status' => array('data' => t('Status'), 'field' => 'sn.activated'), + 'language' => array('data' => t('Language'), 'field' => 'sn.language'), + 'operations' => array('data' => t('Operations')), + ); + + $query = db_select('simplenews_subscriber', 'sn')->extend('PagerDefault')->extend('TableSort'); + simplenews_build_subscription_filter_query($query); + $query->leftJoin('users', 'u', 'sn.uid = u.uid'); + $query->leftJoin('simplenews_realname', 'sr', 'sn.mail = sr.mail'); + $query->innerJoin('simplenews_subscription', 'su', 'sn.snid = su.snid'); + $query->condition('su.status', SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED); + $query->addField('u', 'name', 'name'); + $result = $query + ->fields('sn', array('snid', 'activated', 'mail', 'uid', 'language')) + ->fields('sr', array('realname')) + ->limit(30) + ->orderByHeader($header) + ->execute(); + + $options = array(); + $destination = drupal_get_destination(); + + foreach ($result as $subscriber) { + $options[$subscriber->snid] = array( + 'realname' => check_plain($subscriber->realname), + 'mail' => $subscriber->mail, + 'username' => isset($subscriber->uid) ? l($subscriber->name, 'user/' . $subscriber->uid) : check_plain($subscriber->name), + 'status' => theme('simplenews_status', array('source' => 'activated', 'status' => $subscriber->activated)), + 'language' => check_plain($subscriber->language), + 'operations' => l(t('edit'), 'admin/people/simplenews/users/edit/' . $subscriber->snid, array(), $destination), + ); + } + + $form['admin']['subscribers'] = array( + '#type' => 'tableselect', + '#header' => $header, + '#options' => $options, + '#empty' => t('No subscribers available.'), + ); + + $form['admin']['pager'] = array( + // Calling theme('pager') directly so that it the first call after the + // pager query executed above. + '#markup' => theme('pager'), + ); + break; // Subscription settings. case 'simplenews_admin_settings_subscription': - $form['simplenews_subscription']['account']['simplenews_sync_account']['#description'] .= ' '. t('Note: If checked, when accounts are updated, the subscriber\'s real name with the same email address will be updated.'); - $form['simplenews_subscription']['subscription_mail']['#description'] .= ' '. t('The Simplenews RealName module is enabled, so you may use !subscriber_name (the real name of the subscriber).'); + $form['account']['simplenews_sync_account']['#description'] .= ' '. t('Note: If checked, when accounts are updated, the subscriber\'s real name with the same email address will be updated.'); + $form['subscription_mail']['simplenews_use_combined']['#description'] .= ' '. t('The Simplenews RealName module is enabled, so you may use !subscriber_name (the real name of the subscriber).'); break; // Mass subscribe. case 'simplenews_subscription_list_add': @@ -154,7 +177,7 @@ break; // Node edit form. case 'simplenews_node_form': - $form['body_field']['body']['#description'] .= ' '. t('The Simplenews RealName module is enabled, so you may use !subscriber_name (the real name of the subscriber) in your newsletters.'); + $form['body'][$form['#entity']->language][0]['#description'] .= ' '. t('The Simplenews RealName module is enabled, so you may use !subscriber_name (the real name of the subscriber) in your newsletters.'); break; // Delete multiple subscriptions. case 'simplenews_subscription_multiple_delete_confirm': @@ -286,11 +309,17 @@ foreach ($emails as $mail) { // Prevent mismatches from accidental capitals in mail address. $mail = strtolower(trim($mail)); + // Get subscription account information. - $account = (object) array('mail' => $mail); - $subscription = simplenews_get_subscription($account); + $subscriber = simplenews_subscriber_load_by_mail($mail); + // Clean up subscription account if user is not subscribed to any newsletter anymore. - if (valid_email_address($mail) && !db_result(db_query("SELECT COUNT(*) FROM {simplenews_snid_tid} t WHERE t.snid = %d", $subscription->snid))) { + $query = db_select('simplenews_subscription', 'sn') + ->condition('sn.snid', $subscriber->snid) + ->fields('sn', array('snid')) + ->execute(); + + if (valid_email_address($mail) && ($query->rowCount() < 1)) { _simplenews_realname_set_realname($mail, '', TRUE); } } @@ -312,18 +341,31 @@ function simplenews_realname_block_form_submit($form, &$form_state) { global $user; $action = $form_state['values']['action']; - $realname = $form_state['values']['realname']; $mail = $form_state['values']['mail']; switch ($action) { case 'subscribe': - _simplenews_realname_set_realname($mail, $realname); + _simplenews_realname_set_realname($mail, $form_state['values']['realname']); break; case 'unsubscribe': - $account = _simplenews_user_load($mail); + // Prevent mismatches from accidental capitals in mail address. + $mail = strtolower(trim($mail)); + + // Get account information. + $account = user_load_by_mail($mail); + // If email belongs to the current registered user, don't send confirmation. $confirm = $account->uid && $account->uid == $user->uid ? FALSE : TRUE; + + // Get subscription account information. + $subscriber = simplenews_subscriber_load_by_mail($mail); + // Clean up subscription account if user is not subscribed to any newsletter anymore. - if (!$confirm && !db_result(db_query("SELECT COUNT(*) FROM {simplenews_snid_tid} t WHERE t.snid = %d", $subscription->snid))) { + $query = db_select('simplenews_subscription', 'sn') + ->condition('sn.snid', $subscriber->snid) + ->fields('sn', array('snid')) + ->execute(); + + if (valid_email_address($mail) && ($query->rowCount() < 1)) { _simplenews_realname_set_realname($mail, '', TRUE); } break; @@ -347,19 +389,33 @@ */ function simplenews_realname_subscription_manager_form_submit($form, &$form_state) { $action = $form_state['values']['op']; - $realname = $form_state['values']['realname']; $mail = $form_state['values']['mail']; switch ($action) { case t('Subscribe'): - _simplenews_realname_set_realname($mail, $realname); + _simplenews_realname_set_realname($mail, $form_state['values']['realname']); break; case t('Unsubscribe'): + // Prevent mismatches from accidental capitals in mail address. + $mail = strtolower(trim($mail)); + + // Get account information. + $account = user_load_by_mail($mail); + // If email belongs to the current registered user, don't send confirmation. $confirm = $account->uid && $account->uid == $user->uid ? FALSE : TRUE; - // Clean up subscription account if user is not subscribed to any newsletter anymore. - if (!$confirm && !db_result(db_query("SELECT COUNT(*) FROM {simplenews_snid_tid} t WHERE t.snid = %d", $subscription->snid))) { - _simplenews_realname_set_realname($mail, '', TRUE); - } + + // Get subscription account information. + $subscriber = simplenews_subscriber_load_by_mail($mail); + + // Clean up subscription account if user is not subscribed to any newsletter anymore. + $query = db_select('simplenews_subscription', 'sn') + ->condition('sn.snid', $subscriber->snid) + ->fields('sn', array('snid')) + ->execute(); + + if (valid_email_address($mail) && ($query->rowCount() < 1)) { + _simplenews_realname_set_realname($mail, '', TRUE); + } break; case t('Update'): $checked_newsletters = array_filter($form_state['values']['newsletters']); @@ -367,7 +423,7 @@ _simplenews_realname_set_realname($mail, '', TRUE); } else { - _simplenews_realname_set_realname($mail, $realname); + _simplenews_realname_set_realname($mail, $form_state['values']['realname']); } break; } @@ -390,13 +446,20 @@ */ function simplenews_realname_confirm_removal_form_submit($form, &$form_state) { $mail = $form_state['values']['mail']; + // Prevent mismatches from accidental capitals in mail address. $mail = strtolower(trim($mail)); + // Get subscription account information. - $account = (object) array('mail' => $mail); - $subscription = simplenews_get_subscription($account); + $subscriber = simplenews_subscriber_load_by_mail($mail); + // Clean up subscription account if user is not subscribed to any newsletter anymore. - if (valid_email_address($mail) && !db_result(db_query("SELECT COUNT(*) FROM {simplenews_snid_tid} t WHERE t.snid = %d", $subscription->snid))) { + $query = db_select('simplenews_subscription', 'sn') + ->condition('sn.snid', $subscriber->snid) + ->fields('sn', array('snid')) + ->execute(); + + if (valid_email_address($mail) && ($query->rowCount() < 1)) { _simplenews_realname_set_realname($mail, '', TRUE); } } @@ -410,7 +473,9 @@ // Replace token in the message body. $variables = array('!subscriber_name' => $realname); if (is_array($message['body'])) { - $message['body']['body'] = strtr($message['body']['body'], $variables); + foreach ($message['body'] as $msg_key=>$msg_value) { + $message['body'][$msg_key] = strtr($msg_value, $variables); + } } else { $message['body'] = strtr($message['body'], $variables); @@ -473,7 +538,9 @@ */ function _simplenews_realname_set_realname($mail, $realname, $remove = FALSE) { if ($remove) { - db_query("DELETE FROM {simplenews_realname} WHERE mail = '%s'", $mail); + db_delete('simplenews_realname') + ->condition('mail', $mail) + ->execute(); } else { $update = _simplenews_realname_has_realname($mail) ? 'mail' : array(); @@ -493,7 +560,7 @@ $realname = $result; } else { - $account = user_load_multiple(array(), array('mail' => $mail)); + $account = user_load_by_mail($mail); // Maybe the subscriber is a registered user and has a real name. if ($account && $account->uid != 0) { // Use the name provided by the RealName module if possible.