--- civicrm_subscribe.module.orig 2007-12-24 04:16:04.000000000 +1300 +++ civicrm_subscribe.module 2009-12-15 13:54:47.789261085 +1300 @@ -1,5 +1,6 @@ id] = $group->title; - } + if (!$groups['is_error']) { + foreach ($groups as $group) { + $options[$group["id"]] = $group["title"]; + } + } asort($options); $form['civicrm_subscribe_groups'] = array( @@ -49,10 +53,10 @@ '#options' => $options, '#multiple' => 1, '#default_value' => variable_get('civicrm_subscribe_groups', 0), - '#description' => t("Select the CiviCRM group's users can subscribe to.") + '#description' => t("Select the CiviCRM groups users can subscribe to.") ); } - $form['civicrm_subscribe_drupal_account'] = array( + /* $form['civicrm_subscribe_drupal_account'] = array( '#type' => 'select', '#title' => t('Account Handling'), '#options' => array(0 => t('CiviCRM Only'), 1 => t('Drupal and CiviCRM')), @@ -113,7 +117,7 @@ '#title' => t('Signup Form Complete (Page Text)'), '#default_value' => _civicrm_subscribe_variable_get_page_reconfirm_submitted(), '#description' => t('Enter a short message that is displayed after the user changes their email address.'), - ); + );*/ return system_settings_form($form); } @@ -127,20 +131,23 @@ if (count($groups)) { $group_name = 'this group'; if (module_exists('civicrm')) { - civicrm_initialize(true); + civicrm_initialize(); + require_once('api/v2/Group.php'); + $group_names = array(); foreach ($groups as $group_id) { - $ccrm_groups =& crm_get_groups(array('id' => $group_id)); - $group_names[] = _civicrm_subscribe_get_group_name($ccrm_groups[0]); + $params = array('id' => $group_id); + $ccrm_groups =& civicrm_group_get($params); + $group_names[] = _civicrm_subscribe_get_group_name(current($ccrm_groups)); } $group_name = implode(', ', $group_names); } $help = t('Enter your email address to subscribe to %group_name.', array('%group_name' => $group_name)); return array( - '#action' => url('user/subscribe', 'destination='. $_GET['q']), - '#submit' => array('civicrm_subscribe_form_submit' => array()), - '#validate' => array('civicrm_subscribe_form_validate' => array()), + '#action' => url('user/subscribe', array('query' => 'destination='. $_GET['q'])), + '#submit' => array('civicrm_subscribe_form_submit'), + '#validate' => array('civicrm_subscribe_form_validate'), 'groups' => array( '#type' => 'hidden', @@ -149,18 +156,21 @@ 'help' => array( '#value' => $help, ), - 'first_name' => array( + /*'first_name' => array( '#type' => 'textfield', '#title' => t('First Name'), + '#size' => 40, ), 'last_name' => array( '#type' => 'textfield', '#title' => t('Last Name'), - ), + '#size' => 40, + ),*/ 'email' => array( '#type' => 'textfield', '#title' => t('Email Address'), '#required' => 1, + '#size'=>30, ), 'submit' => array( '#type' => 'submit', @@ -208,11 +218,12 @@ } function civicrm_subscribe_form_submit($form, &$form_state) { + civicrm_initialize(); // create civicrm paramaters to save - $params = array( + /*$params = array( 'first_name' => $form_state['values']['first_name'], 'last_name' => $form_state['values']['last_name'], - ); + );*/ // when posting the form, get the groups from the hidden field // $groups = explode(',', $form_values['groups']); @@ -225,14 +236,35 @@ $groups = explode(',', $_POST['groups']); } + /* -- old method of civisubscribing (deprecated) -- if ($message = civicrm_subscribe($form_state['values']['email'], $params, $groups)) { drupal_set_message($message); + } */ + + require_once('api/v2/Group.php'); + require_once('api/Mailer.php'); + + foreach($groups as $group_id) { + $params = array('id' => $group_id); + $ccrm_groups =& civicrm_group_get($params); + $ccrm_group = array_shift($ccrm_groups); + $group_names[] = _civicrm_subscribe_get_group_name($ccrm_group); + + $contact_added = crm_mailer_event_subscribe($form_state['values']['email'], $group_id); + } + $group_name = implode(', ', $group_names); + + if ($contact_added == TRUE) { + drupal_set_message(_civicrm_subscribe_variable_get_page_submitted($group_name)); + } + else { + drupal_set_message("Subscription error."); } } function civicrm_subscribe_form_validate($form, &$form_state) { if (user_validate_mail($form_state['values']['email'])) { - form_set_error('email', t('Your Email address is invalid.')); + form_set_error('email', t('Your email address is invalid.')); } } @@ -240,7 +272,7 @@ * Subscribe the user to a CiviCRM group * This function is intended to be used by other modules, for example: * - * if (functions_exists('civicrm_subscribe')) { + * if (function_exists('civicrm_subscribe')) { * drupal_set_message(civicrm_subscribe($email)); * } * @@ -261,67 +293,86 @@ */ function civicrm_subscribe($email, $params = array(), $groups = array()) { if (module_exists('civicrm')) { - civicrm_initialize(true); + civicrm_initialize(); + require_once('api/v2/Group.php'); + require_once('api/v2/Contact.php'); - // if groups specified, then get the default group(s) + // if no groups specified, then get the default group(s) if (count($groups) == 0) { $groups = _civicrm_subscribe_get_groups(); } // get (or create) the contact - $contact =& crm_get_contact(array('email' => $email)); - if ($contact && isset($contact->id)) { + $params['email'] = $email; + $contacts =& civicrm_contact_search($params); + $contact = array_shift($contacts); + if ($contact && isset($contact['contact_id'])) { // update first and last name only if we don't already have a value - if (isset($contact->first_name) || !isset($params['first_name'])) { + if (isset($contact['first_name']) || !isset($params['first_name'])) { unset($params['first_name']); } - if (isset($contact->last_name) || !isset($params['last_name'])) { + if (isset($contact['last_name']) || !isset($params['last_name'])) { unset($params['last_name']); } if (count($params)) { - crm_update_contact($contact, $params); + $params['contact_id'] = $contact['contact_id']; + $params['contact_type'] = $contact['contact_type']; + $contact =& civicrm_contact_add($params); } } else { $params['email'] = $email; $params['do_not_email'] = true; - $contact =& crm_create_contact($params); + $params['contact_type'] = 'Individual'; + $contact =& civicrm_contact_add($params); } - if ($contact && isset($contact->id)) { + if ($contact && isset($contact['contact_id'])) { // if this is a new record - if ($contact->do_not_email) { + if ($contact['do_not_email']) { // NOTE: we can not search on the CCRM hash value, so it must be saved in our own table - _civicrm_subscribe_insert($contact->id, $contact->hash); - $contact =& crm_get_contact(array('id' => $contact->id)); + // create a new hash value as CiviCRM v2+ no longer exposes this + $hash = md5( uniqid( rand( ), true ) ); + _civicrm_subscribe_insert($contact['contact_id'], $hash); + $params = array('contact_id' => $contact['contact_id']); + $contact =& civicrm_contact_get($params); } // loop through valid groups and add the user + require_once('api/v2/GroupContact.php'); $group_names = array(); foreach ($groups as $group_id) { - $ccrm_groups =& crm_get_groups(array('id' => $group_id)); - if ($ccrm_groups && count($ccrm_groups) == 1) { - $group = $ccrm_groups[0]; - $contacts = array($contact); - if ($error = crm_add_group_contacts($group, $contacts)) { - $messages[] = _civicrm_subscribe_error($error); + $params = array('id' => $group_id); + $ccrm_groups =& civicrm_group_get($params); + if ($ccrm_groups && count($ccrm_groups) == 1) { + $ccrm_group = array_shift($ccrm_groups); + // $contacts = array($contact); + $params = array('group_id' => $group_id, 'contact_id' => $contact['contact_id']); + $group_added = civicrm_group_contact_add($params); + if ($group_added['is_error'] == 1) { + _civicrm_subscribe_error(); } else { - $group_names[] = _civicrm_subscribe_get_group_name($group); + $group_names[] = _civicrm_subscribe_get_group_name($ccrm_group); } } } - $group_name = implode(', ', $group_names); - + $group_name = implode(', ', $group_names); + if ($group_added['added'] > 0) { // send the confirmation email - _civicrm_subscribe_send_confirm_email($email, $contact->hash, $group_name); + _civicrm_subscribe_send_confirm_email($email, $hash, $group_name); // display message to user $messages[] = _civicrm_subscribe_variable_get_page_submitted($group_name); + } } else { $messages[] = t('subscription error'); } - return implode(". ", $messages); + if ($messages) { + return implode(". ", $messages); } + else { + return t("You are already subscribed to %group_name", array('%group_name' => $group_name)); + } } return _civicrm_subscribe_invalid_group(); } @@ -332,27 +383,34 @@ } } -function _civicrm_subscribe_error($error) { +function _civicrm_subscribe_error($error = 0) { + if($error != 0) { foreach ($error->_errors as $err) { $msgs[] = $err['code'] .': '. $err['message']; } $params = array('@msgs' => implode('; ', $msgs)); - return t('subscription error: @msgs', $params); + return t('subscription error: @msgs', $params); } + else { + return t('subscription error'); + } } function civicrm_subscribe_confirm_page() { if (module_exists('civicrm')) { if ($ccid = db_result(db_query("SELECT ccid FROM {civicrm_subscribe} WHERE hash='%s'", arg(2)))) { - civicrm_initialize(true); + civicrm_initialize(); + require_once('api/v2/Contact.php'); - $contact =& crm_get_contact(array('id' => $ccid)); + $params = array('contact_id' => $ccid); + $contact =& civicrm_contact_get($params); - if ($contact && isset($contact->id)) { + if ($contact && isset($contact['contact_id'])) { // enable emailing now that this contact's email has been confirmed - $contact = crm_update_contact($contact, array('do_not_email' => 0)); + $params = array('contact_id' => $contact['contact_id'], 'contact_type' => $contact['contact_type'], 'do_not_email' => 0); + $contact = civicrm_contact_add($params); $confirmed = 1; - // if we need to create a Drupal user, + // if we need to create a Drupal user, if (variable_get('civicrm_subscribe_drupal_account', 0)) { // make sure the user doesn't exist first // NOTE: this is slightly different than the reset password hash link, @@ -396,35 +454,41 @@ } function _civicrm_subscribe_send_confirm_email($email, $hash, $group_name = 'this group') { - // get the body of the email to send - $body = _civicrm_subscribe_variable_get_email_body(); - - // replace common values - // @TODO: use token.module? - $search = array('%confirm_link', '%group_name', '%email'); - $confirm_link = url('user/confirm/'. $hash, NULL, NULL, TRUE); - $replace = array($confirm_link, $group_name, $email); - $body = str_replace($search, $replace, $body); - - // send the email - $subject = _civicrm_subscribe_variable_get_email_subject(); - drupal_mail('civicrm-subscribe-confirm', $email, $subject, $body); + //drupal_mail('civicrm-subscribe-confirm', $email, $subject, $body); + drupal_mail('civicrm_subscribe', 'subscribe', $email, language_default(), array('hash' => $hash, 'group_name' => $group_name, 'email' => $email)); } function _civicrm_subscribe_send_reconfirm_email($email, $hash) { - // get the body of the email to send - $body = _civicrm_subscribe_variable_get_email_reconfirm_body(); + //drupal_mail('civicrm-subscribe-confirm', $email, $subject, $body); + drupal_mail('civicrm_subscribe', 'confirm', $email, language_default(), array('hash' => $hash, 'group_name' => $group_name, 'email' => $email)); +} - // replace common values - // @TODO: use token.module? - $search = array('%confirm_link', '%email'); - $confirm_link = url('user/confirm/'. $hash, NULL, NULL, TRUE); - $replace = array($confirm_link, $email); - $body = str_replace($search, $replace, $body); - - // send the email - $subject = _civicrm_subscribe_variable_get_email_reconfirm_subject(); - drupal_mail('civicrm-subscribe-confirm', $email, $subject, $body); +function civicrm_subscribe_mail($key, &$message, $params) { + switch($key) { + case 'subscribe': + $message['subject'] = _civicrm_subscribe_variable_get_email_subject(); + $message['body'] = _civicrm_subscribe_variable_get_email_body(); + + // replace common values + // @TODO: use token.module? + $search = array('%confirm_link', '%group_name', '%email'); + $confirm_link = url('user/confirm/'. $params['hash'], array('query' => NULL, 'fragment' => NULL, 'absolute' => TRUE)); + $replace = array($confirm_link, $params['group_name'], $params['email']); + $message['body'] = str_replace($search, $replace, $message['body']); + break; + + case 'confirm': + $message['subject'] = _civicrm_subscribe_variable_get_email_reconfirm_subject(); + $message['body'] = _civicrm_subscribe_variable_get_email_reconfirm_body(); + + // replace common values + // @TODO: use token.module? + $search = array('%confirm_link', '%email'); + $confirm_link = url('user/confirm/'. $params['hash'], array('query' => NULL, 'fragment' => NULL, 'absolute' => TRUE)); + $replace = array($confirm_link, $params['email']); + $message['body'] = str_replace($search, $replace, $message['body']); + break; + } } function _civicrm_subscribe_variable_get_email_body() { @@ -450,7 +514,7 @@ } function _civicrm_subscribe_variable_get_page_submitted($group_name = NULL) { - $submitted = variable_get('civicrm_subscribe_page_submitted', t('Thank You for subscribing to %group_name. You will receive a confirmation email shortly.')); + $submitted = variable_get('civicrm_subscribe_page_submitted', t('Thank you for subscribing to %group_name. You will receive a confirmation email shortly.')); if ($group_name) { $submitted = str_replace('%group_name', $group_name, $submitted); @@ -460,11 +524,11 @@ } function _civicrm_subscribe_variable_get_page_reconfirm_submitted() { - return variable_get('civicrm_subscribe_page_reconfirm_submitted', t('Thank You for updating your email with us. You will receive a confirmation email shortly.')); + return variable_get('civicrm_subscribe_page_reconfirm_submitted', t('Thank you for updating your email with us. You will receive a confirmation email shortly.')); } function _civicrm_subscribe_get_group_name($group) { - return isset($group->title) ? $group->title : $groups->name; + return isset($group['title']) ? $group['title'] : $group['name']; } function _civicrm_subscribe_variable_get_page_confirmed() { @@ -480,21 +544,76 @@ global $user; if (module_exists('civicrm') && $type == 'submit' && $category == 'account' && $edituser->mail != $edit['mail'] && $user->uid == $edituser->uid) { // find the existing civicrm contact record - civicrm_initialize(true); - $contact =& crm_get_contact(array('email' => $edituser->mail)); - if (isset($contact->id)) { + civicrm_initialize(); + require_once('api/v2/Contact.php'); + + $params = array('email' => $edituser->mail); + $contacts =& civicrm_contact_search($params); + $contact = array_shift($contacts); + if (isset($contact['contact_id'])) { // change the civicrm email, and set the flag to do not email while we confirm the new email address $params = array( + 'contact_id' => $contact['contact_id'], + 'contact_type' => $contact['contact_type'], 'email' => $edit['mail'], 'do_not_email' => true, ); - $contact =& crm_update_contact($contact, $params); + $contact =& civicrm_contact_add($params); + $hash = md5( uniqid( rand( ), true ) ); + _civicrm_subscribe_insert($contact['contact_id'], $hash); // send the re-confirmation email - _civicrm_subscribe_send_reconfirm_email($edit['mail'], $contact->hash); + _civicrm_subscribe_send_reconfirm_email($edit['mail'], $hash); // display message to user drupal_set_message(_civicrm_subscribe_variable_get_page_reconfirm_submitted()); } } } +function civicrm_subscribe_block($op = 'list', $delta = 0, $edit = array()) { + if (!module_exists('civicrm')) { + return; + } + civicrm_initialize(); + require_once('api/v2/Group.php'); + + switch ($op) { + case 'list': + $groups = variable_get('civicrm_subscribe_groups', array()); + foreach ($groups as $group_id) { + $params = array('id' => $group_id); + $ccrm_groups =& civicrm_group_get($params); + $group_name = _civicrm_subscribe_get_group_name(array_pop($ccrm_groups)); + $blocks[$group_id]['info'] = t('CiviCRM Subscribe: @group_name', array('@group_name' => $group_name)); + } + return $blocks; + + case 'configure': + $email_only = db_result(db_query("SELECT email_only FROM {civicrm_subscribe_form} WHERE group_id = %d", $delta)); + $form['email_only'] = array( + '#type' => 'select', + '#title' => t('Form'), + '#options' => array(1 => t('Email Only'), 0 => t('Email and Name')), + '#default_value' => $email_only, + '#description' => t('Select which fields to display.'), + ); + return $form; + + case 'save': + if (db_result(db_query("SELECT COUNT(*) FROM {civicrm_subscribe_form} WHERE group_id = %d", $delta))) { + db_query("UPDATE {civicrm_subscribe_form} SET email_only = %d WHERE group_id = %d", $edit['email_only'], $delta); + } + else { + db_query("INSERT INTO {civicrm_subscribe_form} (email_only, group_id) VALUES (%d, %d)", $edit['email_only'], $delta); + } + break; + + case 'view': + + $email_only = db_result(db_query("SELECT email_only FROM {civicrm_subscribe_form} WHERE group_id = '%s'", $delta)); + $block['subject'] = t('Subscribe'); + $block['content'] = drupal_get_form('civicrm_subscribe_form', $delta, $email_only); + return $block; + } +} +