Index: feedback.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/feedback/feedback.module,v
retrieving revision 1.75
diff -u -p -r1.75 feedback.module
--- feedback.module	21 Mar 2009 20:22:14 -0000	1.75
+++ feedback.module	22 May 2009 14:05:31 -0000
@@ -10,7 +10,7 @@
  * Implementation of hook_perm().
  */
 function feedback_perm() {
-  return array('access feedback form', 'view feedback messages');
+  return array('access feedback form', 'view feedback messages', 'administer feedback triggers');
 }
 
 /**
@@ -141,7 +141,11 @@ function feedback_form() {
 }
 
 function feedback_form_submit($form, &$form_state) {
-  feedback_add_entry($form_state['values']['message'], $form_state['values']['location']);
+  $entry = feedback_add_entry($form_state['values']['message'], $form_state['values']['location']);
+
+  // Fire feedback submit trigger.
+  module_invoke_all('feedback', 'submit', $entry);
+
   $message = t('Thanks for your feedback!');
   if ($form_state['values']['ajax']) {
     echo drupal_to_js(array('message' => $message));
@@ -226,6 +230,7 @@ function feedback_add_entry($message, $l
   global $user;
 
   db_query("INSERT INTO {feedback} (uid, message, location, location_masked, url, timestamp, useragent) VALUES (%d, '%s', '%s', '%s', '%s', %d, '%s')", $user->uid, trim($message), $location, feedback_mask_path($location), url($location, array('absolute' => TRUE)), time(), $_SERVER['HTTP_USER_AGENT']);
+  return feedback_load(array('fid' => db_last_insert_id('feedback', 'fid')));
 }
 
 /**
@@ -348,6 +353,197 @@ function feedback_admin_view_form_submit
   // Update status of entries in database.
   foreach ($update as $fid => $value) {
     db_query("UPDATE {feedback} SET status = %d WHERE fid = %d", $value, $fid);
+    if ($value == 1) {
+      // Fire feedback process trigger.
+      module_invoke_all('feedback', 'process', feedback_load(array('fid' => $fid)));
+    }
+  }
+}
+
+/**
+ * Implementation of hook_menu_alter
+ */
+function feedback_menu_alter(&$items) {
+  // By default, the trigger system uses the module name (i.e. administer
+  // feedback) as the access for the trigger configuration menu item.
+  // We want to change that.
+  $items['admin/build/trigger/feedback']['access arguments'] = array('administer feedback triggers');
+}
+
+/**
+ * Implementation of hook_hook_info().
+ */
+function feedback_hook_info() {
+  return array(
+    'feedback' => array(
+      'feedback' => array(
+        'submit' => array(
+          'runs when' => t('After feedback has been submitted'),
+        ),
+        'process' => array(
+          'runs when' => t('After feedback has been processed'),
+        ),
+      ),
+    ),
+  );
+}
+
+/**
+ * Implementation of hook_feedback().
+ *
+ * @param $op
+ *   What kind of action is being performed.
+ * @param $entry
+ *   The feedback the action is being performed on.
+ */
+function feedback_feedback($op, $entry) {
+  // We support a subset of operations.
+  if (!in_array($op, array('submit', 'process'))) {
+    return;
+  }
+  $aids = _trigger_get_hook_aids('feedback', $op);
+  $context = array(
+    'hook' => 'feedback',
+    'op' => $op,
+  );
+  actions_do(array_keys($aids), $entry, $context);
+}
+
+/**
+ * Implementation of hook_info_alter().
+ */
+function feedback_action_info_alter(&$info) {
+  foreach ($info as $type => $data) {
+    if (stripos($type, 'user_') === 0 || stripos($type, 'system_') === 0) {
+      if (isset($info[$type]['hooks']['feedback'])) {
+        $info[$type]['hooks']['feedback'] = array_merge($info[$type]['hooks']['feedback'], array('submit', 'process'));
+      }
+      else {
+        $info[$type]['hooks']['feedback'] = array('submit', 'process');
+      }
+    }
+  }
+}
+
+/**
+ * Implementation of hook_action_info().
+ */
+function feedback_action_info() {
+  return array(
+    'feedback_send_email_action' => array(
+      'description' => t('Sends current user an e-mail'),
+      'type' => 'feedback',
+      'configurable' => TRUE,
+      'hooks' => array('feedback' => array('submit', 'process')),
+    ),
+  );
+}
+
+/**
+ * Return a form definition to configure the send e-mail action.
+ *
+ * @param $context
+ *   Default values (if we are editing an existing action instance).
+ */
+function feedback_send_email_action_form($context) {
+  $context += array(
+    'recipients' => '',
+    'subject' => '',
+    'message' => '',
+  );
+
+  $form['recipients'] = array(
+    '#type' => 'textarea',
+    '#title' => t('Recipients'),
+    '#default_value' => $context['recipients'],
+    '#description' => t("Example: 'webmaster@example.com' or 'dev@example.com,support@example.com'. To specify multiple recipients, separate each e-mail address with a comma."),
+    '#required' => TRUE,
+  );
+  $form['subject'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Subject'),
+    '#default_value' => $context['subject'],
+    '#maxlength' => '254',
+    '#description' => t('The subject of the message.'),
+    '#required' => TRUE,
+  );
+  $form['message'] = array(
+    '#type' => 'textarea',
+    '#title' => t('Message'),
+    '#default_value' => $context['message'],
+    '#cols' => '80',
+    '#rows' => '20',
+    '#description' => t('The message that should be sent. You may include the following variables: !uid, !username, !usermail, !useragent (of the user who gave the feedback), !site_name, !status, !message, !url, !date.'),
+    '#required' => TRUE,
+  );
+  return $form;
+}
+
+/**
+ * Validate the send e-mail action form submission.
+ */
+function feedback_send_email_action_validate($form, &$form_state) {
+  if (empty($form_state['values']['recipients'])) {
+    form_set_error('recipients', t('You must enter one or more recipients.'));
+  }
+  else {
+    $recipients = explode(',', $form_state['values']['recipients']);
+    foreach ($recipients as $recipient) {
+      if (!valid_email_address(trim($recipient))) {
+        form_set_error('recipients', t('%recipient is an invalid e-mail address.', array('%recipient' => $recipient)));
+      }
+    }
   }
 }
 
+/**
+ * Process the send e-mail action form submission.
+ */
+function feedback_send_email_action_submit($form, $form_state) {
+  // Process the HTML form to store configuration. The keyed array that
+  // we return will be serialized to the database.
+  $params = array(
+    'recipients' => $form_state['values']['recipients'],
+    'subject'    => $form_state['values']['subject'],
+    'message'    => $form_state['values']['message'],
+  );
+  return $params;
+}
+
+/**
+ * Implementation of the send e-mail action.
+ */
+function feedback_send_email_action($object, $context) {
+  $context = $params['context'];
+  $account = user_load($object->uid);
+  $from = $account->mail;
+  $params = array('object' => $object, 'account' => $account, 'context' => $context);
+  // Send the e-mail to the recipients using the site default language.
+  drupal_mail('feedback', 'send_email_action', $context['recipients'], language_default(), $params, $from);
+}
+
+/**
+ * Implementation of hook_mail().
+ */
+function feedback_mail($key, &$message, $params) {
+  $language = $message['language'];
+  $object = $params['object'];
+  $account = $params['account'];
+  $context = $params['context'];
+  $variables = array(
+    '!site_name' => variable_get('site_name', 'Drupal'),
+    '!uid' => $account->uid,
+    '!username' => $account->name,
+    '!usermail' => $account->mail,
+    '!status' => $object->status ? t('open') : t('closed'),
+    '!message' => $object->message,
+    '!url' => url($object->location, array('absolute' => TRUE, 'language' => $language)),
+    '!useragent' => $object->useragent,
+    '!date' => format_date($object->timestamp, 'small', '', NULL, $language->language),
+  );
+  $subject = strtr($context['subject'], $variables);
+  $body = strtr($context['message'], $variables);
+  $message['subject'] .= str_replace(array("\r", "\n"), '', $subject);
+  $message['body'][] = drupal_html_to_text($body);
+}
+
