Index: multiple_email.drush.inc
===================================================================
RCS file: multiple_email.drush.inc
diff -N multiple_email.drush.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ multiple_email.drush.inc	15 Feb 2011 01:01:49 -0000
@@ -0,0 +1,448 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ *  Provide drush commands to administer emails for Multiple Email module.
+ */
+
+/**
+ * Implementation of hook_drush_command().
+ */
+function multiple_email_drush_command() {
+  $items = array();
+
+  $items['multiple-email-list'] = array(
+    'description' => 'List all addresses of a given user account.',
+    'drupal dependencies' => array('multiple_email'),
+    'arguments' => array(
+      'user' => 'UID, username, or primary address of the target user.',
+    ),
+    'aliases' => array('mail-list'),
+  );
+  $items['multiple-email-add'] = array(
+    'description' => 'Add a new address to a given user account.',
+    'drupal dependencies' => array('multiple_email'),
+    'arguments' => array(
+      'user' => 'UID, username, or primary address of the target user.',
+    ),
+    'options' => array(
+      '--make-primary' => 'Make the address primary.',
+    ),
+    'aliases' => array('mail-add'),
+  );
+  $items['multiple-email-update'] = array(
+    'description' => 'Update properties of a selected email for a specified user.',
+    'drupal dependencies' => array('multiple_email'),
+    'arguments' => array(
+      'user' => 'UID, username, or primary address of the target user.',
+      'address' => '(optional with --oldest or --newest) Existing address or Email ID to update.',
+    ),
+    'options' => array(
+      '--oldest'       => 'Update the oldest address.',
+      '--newest'       => 'Update the newest address.',
+      '--make-primary' => 'Make the address the primary email of the user.',
+      '--confirmed'    => "(default: yes) 'yes' to confirm the address, 'no' to unconfirm.",
+      '--edit'         => 'Replace a given email address without changing other attributes.',
+    ),
+    'aliases' => array('mail-update'),
+  );
+  $items['multiple-email-delete'] = array(
+    'description' => 'Delete the specified email address of a given user.',
+    'drupal dependencies' => array('multiple_email'),
+    'arguments' => array(
+      'user' => 'UID, username, or primary address of the target user.',
+      'address' => '(optional) Existing address or Email ID to update. Will fall back to providing a choice of existing options.',
+    ),
+    'aliases' => array('mail-delete'),
+  );
+  $items['multiple-email-resend'] = array(
+    'description' => 'Resend the confirmation email for a specific address.',
+    'drupal dependencies' => array('multiple_email'),
+    'arguments' => array(
+      'user' => 'UID, username, or primary address of the target user.',
+      'address' => '(optional) Existing address or Email ID to update. Will fall back to providing a choice of existing options.',
+    ),
+    'aliases' => array('mail-resend'),
+  );
+  return $items;
+}
+
+/**
+ * Implementation of hook_drush_help().
+ */
+function multiple_email_drush_help($section) {
+  switch ($section) {
+    case 'meta:multiple_email:title':
+      return dt('Multiple Email commands');
+    case 'meta:multiple_email:summary':
+      return dt('Review, add, and configure email address settings.');
+    case 'drush:multiple-email-list':
+      return dt("List all email addresses for the specified user account.");
+    case 'drush:multiple-email-add':
+      return dt("Add a new email address to the specified user account.");
+    case 'drush:user-email-update':
+      return dt('Update an email address entry, such as changing the address or making it primary. If no address is specified and the oldest/newest option is not set, will present the administrator with options.');
+    case 'drush:user-email-delete':
+      return dt('Delete the selected email address of the specified user.');
+    case 'drush:user-email-update':
+      return dt('Resend the confirmation email for the selected address of the specified user.');
+  }
+}
+
+/**
+ * Implementation of drush_hook_COMMAND_validate for multiple-email-list.
+ */
+function drush_multiple_email_list_validate($user = NULL, $address = NULL) {
+  if (!$user) {
+    return drush_set_error('DRUSH_ERROR_USER', dt('You must specify a user.'));
+  }
+}
+
+/**
+ * Implementation of drush_hook_COMMAND_validate for multiple-email-add.
+ */
+function drush_multiple_email_add_validate($user = NULL, $address = NULL) {
+  if (!$user) {
+    return drush_set_error('DRUSH_ERROR_USER', dt('You must specify a user.'));
+  }
+  if (!$address) {
+    return drush_set_error('DRUSH_ERROR_EMAIL', dt('You must provide a valid email address for the account.'));
+  }
+  if ($address && !valid_email_address($address)) {
+    return drush_set_error('DRUSH_ERROR_ADDRESS', dt('The email address <!mail> is invalid.', array('!mail' => $address)));
+  }
+  if (multiple_email_find_address($address)) {
+    return drush_set_error(dt('Entered address is already registered on this site.'));
+  }  
+}
+
+/**
+ * Implementation of drush_hook_COMMAND_validate for multiple-email-update.
+ */
+function drush_multiple_email_update_validate($user = NULL, $address = NULL) {
+  if (!$user) {
+    return drush_set_error('DRUSH_ERROR_USER', dt('You must specify a user.'));
+  }
+  if ($address && !valid_email_address($address)) {
+    return drush_set_error('DRUSH_ERROR_ADDRESS', dt('The email address <!mail> is invalid.', array('!mail' => $address)));
+  }
+  if ($new_address = drush_get_option('edit', FALSE)) {
+    _drush_multiple_email_address_validate($new_address);
+  }
+  
+}
+
+/**
+ * Implementation of drush_hook_COMMAND_validate for multiple-email-delete.
+ */
+function drush_multiple_email_delete_validate($user = NULL, $address = NULL) {
+  if (!$user) {
+    return drush_set_error('DRUSH_ERROR_USER', dt('You must specify a user.'));
+  }
+  if ($address && !valid_email_address($address)) {
+    return drush_set_error('DRUSH_ERROR_ADDRESS', dt('The email address <!mail> is invalid.', array('!mail' => $address)));
+  }
+}
+
+/**
+ * Implementation of drush_hook_COMMAND_validate for multiple-email-resend.
+ */
+function drush_multiple_email_resend_validate($user = NULL, $address = NULL) {
+  if (!$user) {
+    return drush_set_error('DRUSH_ERROR_USER', dt('You must specify a user.'));
+  }
+  if ($address && !valid_email_address($address)) {
+    return drush_set_error('DRUSH_ERROR_ADDRESS', dt('The email address <!mail> is invalid.', array('!mail' => $address)));
+  }
+}
+
+/**
+ * Implementation of drush_hook_COMMAND for multiple-email-list.
+ */
+function drush_multiple_email_list($user) {
+  $uid = _drush_user_get_uid($user);
+  if (!$uid) {
+    return;  
+  }
+
+  $raw = multiple_email_load_addresses($uid);
+  $items[] = array('Email Address', 'Confirmed', 'Registered');
+
+  array_shift($raw);
+  foreach ($raw as &$address) {
+    $items[] = _multiple_email_drush_address_row($address);
+  }
+  drush_print_table($items, TRUE);
+}
+
+/**
+ * Implementation of drush_hook_COMMAND for multiple-email-add.
+ */
+function drush_multiple_email_add($user, $address) {
+  $uid = _drush_user_get_uid($user);
+  if (!$uid) {
+    return;  
+  }
+  $confirmed = _multiple_email_drush_get_confirmed_option();
+
+  $msg = dt('Added <!mail> to user <!uid>.', array('!mail' => $address, '!uid' => $uid));
+  if (drush_get_context('DRUSH_SIMULATE')) {
+    drush_log($msg, 'success');
+    if ($confirmed && drush_get_option('make-primary', FALSE)) {
+      drush_print(dt('If this had not been simulated, !mail would have been made primary.', array('!mail' => $address)));
+    }
+    return;
+  }
+
+  multiple_email_register_email($uid, $address, $confirmed);
+  drush_log($msg, 'success');
+
+  if ($confirmed) {
+    drush_print('The address has been marked as confirmed, and can be made primary.');
+  }
+
+  if ($confirmed && drush_get_option('make-primary', FALSE)) {
+    _multiple_email_drush_make_primary($uid, $address);
+  }
+  elseif (!$confirmed && drush_get_option('make-primary', FALSE)) {
+    drush_print("To make an address primary it must be confirmed. Use the '--confirmed=yes' flag to create a confirmed address");
+  }
+}
+
+/**
+ * Implementation of drush_hook_COMMAND for multiple-email-update.
+ */
+function drush_multiple_email_update($user, $address = NULL) {
+  $uid = _drush_user_get_uid($user);
+  if (!$uid) {
+    return;  
+  }
+
+  if (!$address) {
+    $eid = _multiple_email_identify_address_by_options($uid);
+    if (!$eid) {
+      $eid = _multiple_email_drush_choice($uid);
+      if (!$eid) {
+        return;
+      } 
+    }
+    $email = multiple_email_load($eid);
+    $address = $email->email;
+  }
+  else {
+    $email = multiple_email_find_address($address);
+  }
+
+  if (!drush_get_context('DRUSH_SIMULATE')) {
+    $confirmed = _multiple_email_drush_get_confirmed_option();
+    if ($email->confirmed != $confirmed) {
+      multiple_email_confirm_email($address, $confirmed);
+      if ($confirmed) {
+        drush_log(dt('The address <!mail> has been confirmed!', array('!mail' => $address)), 'success');
+      }
+      else {
+        drush_log(dt('The address <!mail> has been marked as unconfirmed.', array('!mail' => $address)), 'success');
+      }
+    }
+    $make_primary = drush_get_option('make-primary', FALSE);
+    if ($make_primary && !$address->primary_address) {
+      _multiple_email_drush_make_primary($uid, $address);
+    }
+    $new_address = drush_get_option('edit', FALSE);
+    if ($new_address) {
+      // Does not use proper workflow with multiple_email_register_email().
+      db_query("UPDATE {multiple_email} SET email = '%s' WHERE eid = %d", $new_address, $address->eid);
+      drush_log(dt('The e-mail address has been changed to <!mail>', array('!mail' => $new_address)), 'success');
+    }
+  }
+  else {
+    // Provide feedback for simulated operations.
+    drush_log(dt('Modified <!mail>', array('!mail' => $address)), 'success');
+  }
+}
+
+/**
+ * Implementation of drush_hook_COMMAND for multiple-email-delete.
+ */
+function drush_multiple_email_delete($user, $address = NULL) {
+  $uid = _drush_user_get_uid($user);
+  if (!$uid) {
+    return;  
+  }
+
+  if (!$address) {
+    $eid = _multiple_email_drush_choice($uid);
+    if (!$eid) {
+      return;
+    }
+    $email = multiple_email_load($eid);
+    $address = $email->email;
+  }
+  else {
+    $email = multiple_email_find_address($address);
+  }
+
+  if ($email->uid != $uid) {
+    return drush_set_error('DRUSH_ERROR_ADDRESS_OWNER',
+      dt('The selected address does not belong to the specified user.'));
+  }
+  if ($email->primary_address) {
+    return drush_set_error('DRUSH_ERROR_ADDRESS_PRIMARY',
+      dt('The selected address is currently the primary email of this account and cannot be deleted. Please use `drush multiple-email-add` or `drush multiple-email-update` with the "--make-primary" flag to set a different address as primary.'));
+  }
+  if (!drush_get_context('DRUSH_SIMULATE')) {
+    multiple_email_delete_email($email->eid);
+  }
+  drush_log(dt('Address <!mail> deleted from user <!uid>.',
+    array('!mail' => $address, '!uid' => $uid)), 'success');
+}
+
+/**
+ * Implementation of drush_hook_COMMAND for multiple-email-resend.
+ */
+function drush_multiple_email_resend($user, $address = NULL) {
+  $uid = _drush_user_get_uid($user);
+  if (!$uid) {
+    return;  
+  }
+
+  if (!$address) {
+    $eid = _multiple_email_drush_choice($uid, FALSE);
+    if (!$eid) {
+      return;
+    }
+    $email = multiple_email_load($eid);
+    $address = $email->email;
+  }
+  else {
+    $email = multiple_email_find_address($address);
+  }
+
+  if ($email->uid != $uid) {
+    return drush_set_error('DRUSH_ERROR_ADDRESS_OWNER',
+      dt('The selected address does not belong to the specified user.'));
+  }
+  if ($email->confirmed) {
+    return drush_set_error('DRUSH_ERROR_ADDRESS_CONFIRMED',
+      dt('The selected address is already confirmed. There is no need to resend a confirmation email.'));
+  }
+  if (!drush_get_context('DRUSH_SIMULATE')) {
+    $account = user_load($uid);
+    multiple_email_send_confirmation($account, $email);
+  }
+  drush_log(dt('User <!uid> has been sent a new confirmation email for address <!mail>',
+    array('!mail' => $address, '!uid' => $uid)), 'success'); 
+}
+
+/**
+ * Reprocesses email address for administrative review.
+ *
+ * Takes the result of multiple_email_get_address() and makes an array of
+ * commonly useful information suitable for output as a drush table row.
+ *
+ * @param $address
+ *   Email address object as queried from database.
+ *
+ */
+function _multiple_email_drush_address_row($address) {
+  $item = array(
+    'email' => $address->email,
+    'confirmed' => $address->confirmed ? t('Yes') : t('No'),
+    'date' => $address->time_registered,  
+  );
+  if ($address->primary_address) {
+    $item['email'] = '*' . $item['email'];
+  }
+  $item['date'] = date('Y-m-d', $item['date']);
+  return $item;
+}
+
+/**
+ * Make the specified address primary for the specified user.
+ *
+ * @param $uid
+ *  User ID
+ * @param $address
+ *  Email address to make primary.
+ */
+function _multiple_email_drush_make_primary($uid, $address) {
+  multiple_email_make_primary((object)array('email' => $address, 'uid' => $uid));
+  drush_print(dt('<!mail> will be the primary address for user <!uid>',
+    array('!mail' => $address, '!uid' => $uid)));
+}
+
+/**
+ * Get the standardized 'confirmed' option.
+ *
+ * Leaving the option empty, or answering with 1, Yes, yes, Y, y will result in
+ * a positive result for 'confirmed'.
+ *
+ * @return
+ *  Boolean. Whether or not confirmed is TRUE.
+ */
+function _multiple_email_drush_get_confirmed_option() {
+  $confirmed = drush_get_option('confirmed', 1);
+  return in_array($confirmed, array(1, 'Yes', 'yes', 'y', 'Y'));
+}
+
+/**
+ * Select which of the user's email addresses should be used for an operation.
+ *
+ * @param $uid
+ *  User ID
+ * @param $confirmed
+ *  Whether the address is confirmed.
+ *
+ * @return
+ *  The Email Address ID of the selected address, or FALSE if none was available or the operation canceled.
+ */
+function _multiple_email_drush_choice($uid, $confirmed = 'all') {
+  $sql = "SELECT eid, email FROM {multiple_email} WHERE uid = %d";
+  if ($confirmed != 'all') {
+    $sql .= " AND confirmed = %d";
+    $results = db_query($sql, $uid, (int) $confirmed);
+  }
+  else {
+    $results = db_query($sql, $uid);
+  }
+
+  $options = array();
+  while ($item = db_fetch_object($results)) {
+    $options[$item->eid] = $item->email;
+  }
+  
+  if (empty($options)) {
+    return drush_set_error('DRUSH_ERROR_EMAIL', dt('No valid email address available for this operation.'));
+  }
+  return drush_choice($options);
+}
+
+/**
+ * Identify the address by criteria set in command options.
+ *
+ * @param $uid
+ *  User ID.
+ *
+ * @return
+ *  Email Address ID if successful, otherwise FALSE.
+ */
+function _multiple_email_identify_address_by_options($uid) {
+  $newest = drush_get_option('newest', FALSE);
+  $oldest = drush_get_option('oldest', FALSE);
+  if ($oldest == $newest) {
+    if ($oldest) {
+      drush_log(dt('You cannot use both --newest and --oldest.'), 'warning');
+    }
+    return FALSE;
+  }
+  $sql = "SELECT eid FROM {multiple_email} WHERE uid = %d ORDER BY time_registered";
+  if ($newest) {
+    $sql .= ' DESC';    
+  }
+  else {
+    $sql .= ' ASC';
+  }
+  return db_result(db_query($sql, $uid));
+}
+
