123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383 |
- <?php
- /**
- * @file
- * Rules hooks for uc_roles.module.
- */
- /**
- * Implements hook_rules_data_info().
- *
- * An entity is defined in order to get role expiration tokens in the email.
- */
- function uc_roles_rules_data_info() {
- // CA entity for a role expiration object.
- $entities['uc_roles_expiration'] = array(
- 'label' => t('Ubercart role expiration'),
- 'wrap' => TRUE,
- 'token type' => 'uc_role',
- 'property info' => array(
- 'reid' => array(
- 'type' => 'integer',
- 'label' => t('Role expiration ID'),
- 'description' => t('Primary key for role expirations.'),
- ),
- 'uid' => array(
- 'type' => 'integer',
- 'label' => t('User ID'),
- 'description' => t('The user account ID.'),
- ),
- 'user' => array(
- 'type' => 'user',
- 'label' => t('User'),
- 'description' => t('The user account that has the role.'),
- 'getter callback' => 'uc_roles_get_expiration_properties',
- 'setter callback' => 'uc_roles_set_expiration_properties',
- ),
- 'rid' => array(
- 'type' => 'integer',
- 'label' => t('Role ID'),
- 'description' => t('The granted role.'),
- ),
- 'expiration' => array(
- 'type' => 'date',
- 'label' => t('Expiration time'),
- 'description' => t('The time the role will be removed from the user.'),
- ),
- 'notified' => array(
- 'type' => 'boolean',
- 'label' => t('Notified'),
- 'description' => t('Indicates the user has been warned that the role will be removed soon.'),
- ),
- ),
- );
- return $entities;
- }
- /**
- * Callback for getting role expiration properties.
- *
- * @see entity_metadata_node_entity_info_alter()
- */
- function uc_roles_get_expiration_properties($expiration, array $options, $name, $entity_type) {
- switch ($name) {
- case 'user':
- return $expiration->uid;
- }
- }
- /**
- * Callback for setting role expiration properties.
- *
- * @see entity_metadata_node_entity_info_alter()
- */
- function uc_roles_set_expiration_properties($expiration, $name, $value) {
- if ($name == 'user') {
- $expiration->uid = $value;
- }
- }
- /**
- * Implements hook_rules_action_info().
- */
- function uc_roles_rules_action_info() {
- // Renew a role expiration.
- $actions['uc_roles_order_renew'] = array(
- 'label' => t('Renew the roles on an order.'),
- 'group' => t('Renewal'),
- 'base' => 'uc_roles_action_order_renew',
- 'parameter' => array(
- 'order' => array(
- 'type' => 'uc_order',
- 'label' => t('Order'),
- ),
- 'message' => array(
- 'type' => 'boolean',
- 'label' => t('Display messages to alert users of any new or updated roles.'),
- ),
- ),
- );
- $email_args = array(
- 'expiration' => array(
- 'type' => 'uc_roles_expiration',
- 'label' => t('Role expiration'),
- ),
- 'from' => array(
- 'type' => 'text',
- 'label' => t('Sender'),
- ),
- 'addresses' => array(
- 'type' => 'text',
- 'label' => t('Recipients'),
- ),
- 'subject' => array(
- 'type' => 'text',
- 'label' => t('Subject'),
- ),
- 'message' => array(
- 'type' => 'text',
- 'label' => t('Message'),
- ),
- 'format' => array(
- 'type' => 'text',
- 'label' => t('Message format'),
- 'options list' => 'uc_roles_message_formats',
- ),
- );
- // Send an email to an order with a role expiration
- $actions['uc_roles_order_email'] = array(
- 'label' => t('Send an order email regarding roles.'),
- 'group' => t('Notification'),
- 'base' => 'uc_roles_action_order_email',
- 'parameter' => array(
- 'order' => array(
- 'type' => 'uc_order',
- 'label' => t('Order'),
- ),
- ) + $email_args,
- );
- // Send an email to a user with a role expiration
- $actions['uc_roles_user_email'] = array(
- 'label' => t('Send a user an email regarding roles.'),
- 'group' => t('Notification'),
- 'base' => 'uc_roles_action_user_email',
- 'parameter' => array(
- 'account' => array(
- 'type' => 'user',
- 'label' => t('User'),
- ),
- ) + $email_args,
- );
- return $actions;
- }
- /**
- * Options list callback for message formats.
- */
- function uc_roles_message_formats() {
- global $user;
- $options = array();
- $formats = filter_formats($user);
- foreach ($formats as $format) {
- $options[$format->format] = $format->name;
- }
- return $options;
- }
- /**
- * Implements hook_rules_event_info().
- */
- function uc_roles_rules_event_info() {
- $order = array(
- 'type' => 'uc_order',
- 'label' => t('Order'),
- );
- $account = array(
- 'type' => 'user',
- 'label' => t('User'),
- );
- $expiration = array(
- 'type' => 'uc_roles_expiration',
- 'label' => t('Role expiration'),
- );
- $events['uc_roles_notify_grant'] = array(
- 'label' => t('E-mail for granted roles'),
- 'group' => t('Notification'),
- 'variables' => array(
- 'order' => $order,
- 'expiration' => $expiration,
- ),
- );
- $events['uc_roles_notify_revoke'] = array(
- 'label' => t('E-mail for revoked roles'),
- 'group' => t('Notification'),
- 'variables' => array(
- 'account' => $account,
- 'expiration' => $expiration,
- ),
- );
- $events['uc_roles_notify_renew'] = array(
- 'label' => t('E-mail for renewed roles'),
- 'group' => t('Notification'),
- 'variables' => array(
- 'order' => $order,
- 'expiration' => $expiration,
- ),
- );
- $events['uc_roles_notify_reminder'] = array(
- 'label' => t('E-mail for role expiration reminders'),
- 'group' => t('Notification'),
- 'variables' => array(
- 'account' => $account,
- 'expiration' => $expiration,
- ),
- );
- return $events;
- }
- /**
- * Send an email with order and role replacement tokens.
- *
- * The recipients, subject, and message fields take order token replacements.
- *
- * @see uc_roles_action_order_email_form()
- */
- function uc_roles_action_order_email($order, $role_expiration, $from, $addresses, $subject, $message, $format) {
- $settings = array(
- 'from' => $from,
- 'addresses' => $addresses,
- 'subject' => $subject,
- 'message' => $message,
- 'format' => $format,
- );
- // Token replacements for the subject and body
- $settings['replacements'] = array(
- 'uc_order' => $order,
- 'uc_role' => $role_expiration,
- );
- // Replace tokens and parse recipients.
- $recipients = array();
- $addresses = token_replace($settings['addresses'], $settings['replacements']);
- foreach (explode("\n", $addresses) as $address) {
- $address = trim($address);
- // Remove blank lines
- if (!empty($address)) {
- $recipients[] = $address;
- }
- }
- // Send to each recipient.
- foreach ($recipients as $email) {
- $sent = drupal_mail('uc_order', 'action-mail', $email, uc_store_mail_recipient_language($email), $settings, $settings['from']);
- if (!$sent['result']) {
- watchdog('uc_roles', 'Attempt to e-mail @email concerning order @order_id failed.', array('@email' => $email, '@order_id' => $order->order_id), WATCHDOG_ERROR);
- }
- }
- }
- /**
- * Send an email with order and role replacement tokens.
- *
- * The recipients, subject, and message fields take order token replacements.
- *
- * @see uc_roles_action_user_email_form()
- */
- function uc_roles_action_user_email($account, $role_expiration, $from, $addresses, $subject, $message, $format) {
- $settings = array(
- 'from' => $from,
- 'addresses' => $addresses,
- 'subject' => $subject,
- 'message' => $message,
- 'format' => $format,
- );
- // Token replacements for the subject and body
- $settings['replacements'] = array(
- 'user' => $account,
- 'uc_role' => $role_expiration,
- );
- // Replace tokens and parse recipients.
- $recipients = array();
- $addresses = token_replace($settings['addresses'], $settings['replacements']);
- foreach (explode("\n", $addresses) as $address) {
- $address = trim($address);
- // Remove blank lines
- if (!empty($address)) {
- $recipients[] = $address;
- }
- }
- // Send to each recipient.
- foreach ($recipients as $email) {
- $sent = drupal_mail('uc_order', 'action-mail', $email, uc_store_mail_recipient_language($email), $settings, $settings['from']);
- if (!$sent['result']) {
- watchdog('uc_roles', 'Attempt to e-mail @email concerning role notification failed.', array('@email' => $email), WATCHDOG_ERROR);
- }
- }
- }
- /**
- * Renews an order's product roles.
- *
- * This function updates expiration time on all roles found on all products
- * on a given order. First, the order user is loaded, then the order's products
- * are scanned for role product features. If any are found, the expiration time
- * of the role is set using the feature settings to determine the new length of
- * time the new expiration will last. An order comment is saved, and the user
- * is notified in Drupal, as well as through the email address associated with
- * the order.
- *
- * @param $order
- * An Ubercart order object.
- * @param $message
- * If TRUE, messages will be displayed to the user about the renewal.
- */
- function uc_roles_action_order_renew($order, $message) {
- // Load the order's user and exit if not available.
- if (!($account = user_load($order->uid))) {
- return;
- }
- // Loop through all the products on the order.
- foreach ($order->products as $product) {
- // Look for any role promotion features assigned to the product.
- $roles = db_query("SELECT * FROM {uc_roles_products} WHERE nid = :nid", array(':nid' => $product->nid));
- foreach ($roles as $role) {
- // Product model matches, or was 'any'.
- if (!empty($role->model) && $role->model != $product->model) {
- continue;
- }
- $existing_role = db_query("SELECT * FROM {uc_roles_expirations} WHERE uid = :uid AND rid = :rid", array(':uid' => $account->uid, ':rid' => $role->rid))->fetchObject();
- // Determine the expiration timestamp for the role.
- $expiration = _uc_roles_product_get_expiration($role, $product->qty, isset($existing_role->expiration) ? $existing_role->expiration : NULL);
- // Leave an order comment.
- if (isset($existing_role->expiration)) {
- $op = 'renew';
- $comment = t('Customer user role %role renewed.', array('%role' => _uc_roles_get_name($role->rid)));
- // Renew the user's role.
- uc_roles_renew($account, $role->rid, $expiration, !$message);
- }
- else {
- $op = 'grant';
- $comment = t('Customer granted user role %role.', array('%role' => _uc_roles_get_name($role->rid)));
- // Grant the role to the user.
- uc_roles_grant($account, $role->rid, $expiration, TRUE, !$message);
- }
- // Get the new expiration (if applicable)
- $new_expiration = db_query("SELECT * FROM {uc_roles_expirations} WHERE uid = :uid AND rid = :rid", array(':uid' => $account->uid, ':rid' => $role->rid))->fetchObject();
- if (!$new_expiration) {
- $new_expiration = new stdClass();
- $new_expiration->uid = $account->uid;
- $new_expiration->rid = $role->rid;
- $new_expiration->expiration = NULL;
- }
- uc_order_comment_save($order->order_id, $account->uid, $comment);
- // Trigger role email.
- rules_invoke_event('uc_roles_notify_' . $op, $order, $new_expiration);
- }
- }
- }
|