Permalink
Browse files

Messages must be defined beforehand so that they can (potentially) be…

… configured by users.
  • Loading branch information...
1 parent 28fc31b commit ae848c9767ea2aada70738bb7b80b62f75ccec68 @decafdennis committed Jan 17, 2011
View
@@ -116,3 +116,47 @@ function hook_emailkit_dispatcher_info() {
function hook_dispatcher_send($dispatcher_name, $message, $destination) {
return FALSE;
}
+
+/**
+ * Returns the message types defined by this module.
+ *
+ * All types of messages sent by a module must be defined by the implementation of this hook, so that users can enable/disable various features for each messages type.
+ *
+ * This is a module-level hook. Implementation is optional.
+ *
+ * @return An array of message type information keyed by message id. Message type information is a subarray with the following attributes:
+ * #label: The human-readable name of the message type. Capitalize and wrap in t(). Required.
+ * #file: The file that needs to be included when invoking message type hooks. Optional.
+ * #base: The base for message type hooks. Optional. Defaults to the message id.
+ */
+function hook_emailkit_message_info() {
+ $info = array();
+
+ $info['my_module_node'] = array(
+ '#label' => t('Content'),
+ '#file' => 'my_module.node.inc',
+ );
+
+ return $info;
+}
+
+/**
+ * Returns a structured message array for the given message id.
+ *
+ * This is a message type-level hook. Implementation is required.
+ *
+ * @param $message_id The message id.
+ * @param ... Any additional arguments that were passed into emailkit_message_get().
+ *
+ * @return A structured message array.
+ */
+function hook_message($message_id, $node) {
+ return array(
+ '#subject' => $node->title,
+ '#body' => array(
+ 'node' => array(
+ '#value' => node_view($node),
+ ),
+ ),
+ );
+}
View
@@ -9,7 +9,7 @@
* @param $reset A flag indicating whether to reset the cache and call the info hooks again. Defaults to FALSE.
*/
function _emailkit_entity_info($domain, $identifier = NULL, $reset = FALSE) {
- assert('in_array($domain, array(\'destination\', \'dispatcher\')) /* should be a recognized domain */');
+ assert('in_array($domain, array(\'destination\', \'dispatcher\', \'message\')) /* should be a recognized domain */');
static $info = NULL;
View
@@ -254,6 +254,65 @@ function _emailkit_dispatcher_info_defaults() {
}
/**
+ * Returns information about all available message types or just about the given message type.
+ *
+ * @param $id The id of the message type to return information about, or NULL to return information about all message types.
+ *
+ * @return If an id was given, a structured message type array or NULL if no such message type exists. Otherwise, an array of message type information keyed by message id.
+ */
+function emailkit_message_info($id = NULL, $reset = FALSE) {
+ module_load_include('inc', 'emailkit', 'emailkit');
+
+ return _emailkit_entity_info('message', $id, $reset);
+}
+
+/**
+ * Private function used by _emailkit_entity_info that provides default values for message types.
+ */
+function _emailkit_message_info_defaults() {
+ return array(
+ '#label' => t('(Unlabeled)'),
+ );
+}
+
+/**
+ * Returns a structured message array for the given message id.
+ *
+ * @param $message_id The message id.
+ * @param ... Any additional arguments that will be passed into hook_message().
+ *
+ * @return A structured message array.
+ */
+function emailkit_message($message_id) {
+ if (!isset($message_id)) {
+ return NULL;
+ }
+
+ // Get information about the message type
+ $info = emailkit_message_info($message_id);
+ if (!isset($info)) {
+ return NULL;
+ }
+
+ module_load_include('inc', 'emailkit', 'emailkit');
+
+ // Get the message
+ $function = _emailkit_entity_info_hook($info, 'message');
+ $args = func_get_args();
+ $message = call_user_func_array($function, $args);
+
+ // If we have a message, adapt a few of its properties
+ if (isset($message)) {
+ // Set #message_id if it hasn't been set already
+ if (!isset($form['#message_id'])) {
+ $message['#message_id'] = $message_id;
+ }
+ }
+
+ return $message;
+}
+
+/**
* Sends the given message to the given destination.
*
* @param $message A structured message array.
@@ -318,8 +377,6 @@ function emailkit_destination_select_send($message, $element_value) {
return emailkit_send($message, $destination);
}
-
-
/**
* Private helper function that will include the appropriate file and pass on the message.
*/
@@ -299,14 +299,7 @@ function emailkit_list_user_load_by_address($address, $null = FALSE) {
* @return A flag indicating whether the message was sent successfully.
*/
function emailkit_list_confirmation_send($address, $lids) {
- $message = array(
- '#subject' => t('Confirm your subscriptions'),
- '#body' => array(
- 'link' => array(
- '#value' => emailkit_list_confirmation_url($address, $lids),
- ),
- ),
- );
+ $message = emailkit_message('emailkit_list_confirmation', $address, $lids);
$destination = array(
'#type' => 'emailkit_default',
@@ -321,6 +314,20 @@ function emailkit_list_confirmation_send($address, $lids) {
}
/**
+ * Implementation of hook_message() for the emailkit_list_confirmation message.
+ */
+function emailkit_list_confirmation_message($message_id, $address, $lids) {
+ return array(
+ '#subject' => t('Confirm your subscriptions'),
+ '#body' => array(
+ 'link' => array(
+ '#value' => emailkit_list_confirmation_url($address, $lids),
+ ),
+ ),
+ );
+}
+
+/**
* Generates an absolute URL that can be used to confirm subscription to the given lists.
*
* @param $address A subscriber's e-mail address.
@@ -224,6 +224,20 @@ function emailkit_list_emailkit_dispatcher_info() {
}
/**
+ * Implementation of emailkit_emailkit_message_info().
+ */
+function emailkit_list_emailkit_message_info() {
+ $info = array();
+
+ $info['emailkit_list_confirmation'] = array(
+ '#label' => t('E-mail list subscription confirmation'),
+ '#file' => 'emailkit_list.inc',
+ );
+
+ return $info;
+}
+
+/**
* Loads the list with the given identifier from the database.
*
* @return A list, or NULL if it cannot be found.
@@ -0,0 +1,17 @@
+<?php
+// Developed by Dennis Stevense for Digital Deployment
+
+
+/**
+ * Implementation of hook_message() for the emailkit_node message.
+ */
+function emailkit_node_message($message_id, $node) {
+ return array(
+ '#subject' => $node->title,
+ '#body' => array(
+ 'node' => array(
+ '#value' => $node->teaser,
+ ),
+ ),
+ );
+}
@@ -41,3 +41,17 @@ function emailkit_node_access($node) {
return user_access('e-mail content') || ($node->uid == $user->uid && user_access('e-mail own content'));
}
+
+/**
+ * Implementation of emailkit_emailkit_message_info().
+ */
+function emailkit_node_emailkit_message_info() {
+ $info = array();
+
+ $info['emailkit_node'] = array(
+ '#label' => t('Node'),
+ '#file' => 'emailkit_node.inc',
+ );
+
+ return $info;
+}
@@ -36,15 +36,7 @@ function emailkit_node_email_form(&$form_state, $node) {
* Handles submission of the form for the E-mail tab on a node.
*/
function emailkit_node_email_form_submit($form, &$form_state) {
- $node = $form['#node'];
- $message = array(
- '#subject' => $node->title,
- '#body' => array(
- 'node' => array(
- '#value' => $node->teaser,
- ),
- ),
- );
+ $message = emailkit_message('emailkit_node', $form['#node']);
if (emailkit_destination_select_send($message, $form_state['values']['dest'])) {
drupal_set_message(t('E-mail was sent successfully.'));

0 comments on commit ae848c9

Please sign in to comment.