Index: civicrm_subscribe.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/civicrm_subscribe/civicrm_subscribe.module,v retrieving revision 1.1.2.16 diff -u -p -r1.1.2.16 civicrm_subscribe.module --- civicrm_subscribe.module 17 Apr 2008 03:45:03 -0000 1.1.2.16 +++ civicrm_subscribe.module 16 Oct 2008 22:54:59 -0000 @@ -24,11 +24,12 @@ function civicrm_subscribe_menu($may_cac ); $items[] = array( 'path' => 'admin/settings/civicrm_subscribe', - 'title' => t('Subscribe'), + 'title' => t('CiviCRM Subscribe'), 'description' => t('CiviCRM Subscribe Settings'), 'callback' => 'drupal_get_form', 'callback arguments' => 'civicrm_subscribe_admin_settings', 'access' => user_access('administer site configuration'), + 'type' => MENU_NORMAL_ITEM, ); } return $items; @@ -36,15 +37,17 @@ function civicrm_subscribe_menu($may_cac function civicrm_subscribe_admin_settings() { if (module_exists('civicrm')) { - civicrm_initialize(true); + civicrm_initialize(); + require_once 'api/v2/Group.php'; $options = array(); $params = array(); $return_properties = array('id', 'title'); - $groups = crm_get_groups($params, $return_properties); + $groups = civicrm_groups_get($params); - $options[0] = '-- Select One --'; - foreach ($groups as $group) { - $options[$group->id] = $group->title; + if (!$groups['is_error']) { + foreach ($groups as $group) { + $options[$group["id"]] = $group["title"]; + } } asort($options); @@ -54,7 +57,7 @@ function civicrm_subscribe_admin_setting '#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( @@ -133,6 +136,7 @@ function civicrm_subscribe_form($group_i } // make sure that the path is a valid group that allows subscriptions + require_once 'api/v2/Group.php'; $groups = _civicrm_subscribe_get_groups($groups); if (count($groups)) { $path = isset($group_id) ? 'user/subscribe/'. $group_id : 'user/subscribe'; @@ -152,11 +156,12 @@ function civicrm_subscribe_form($group_i if (!$email_only) { $group_name = 'this group'; if (module_exists('civicrm')) { - civicrm_initialize(true); + civicrm_initialize(); $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_groups_get($params); + $group_names[] = _civicrm_subscribe_get_group_name(current($ccrm_groups)); } $group_name = implode(', ', $group_names); } @@ -190,11 +195,14 @@ function civicrm_subscribe_block($op = ' switch ($op) { case 'list': - civicrm_initialize(true); - foreach (variable_get('civicrm_subscribe_groups', array()) as $group_id) { - $ccrm_groups =& crm_get_groups(array('id' => $group_id)); - $group_name = _civicrm_subscribe_get_group_name($ccrm_groups[0]); - $blocks[$group_id]['info'] = t('Civicrm %group_name Subscribe', array('%group_name' => $group_name)); + require_once 'api/v2/Group.php'; + civicrm_initialize(); + $groups = variable_get('civicrm_subscribe_groups', array()); + foreach ($groups as $group_id) { + $params = array('id' => $group_id); + $ccrm_groups =& civicrm_groups_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; @@ -211,10 +219,10 @@ function civicrm_subscribe_block($op = ' case 'save': if (db_result(db_query("SELECT COUNT(*) FROM {civicrm_subscribe_form} WHERE group_id = %d", $delta))) { - db_result(db_query("UPDATE {civicrm_subscribe_form} SET email_only = %d WHERE group_id = %d", $edit['email_only'], $delta)); + db_query("UPDATE {civicrm_subscribe_form} SET email_only = %d WHERE group_id = %d", $edit['email_only'], $delta); } else { - db_result(db_query("INSERT INTO {civicrm_subscribe_form} (email_only, group_id) VALUES (%d, %d)", $edit['email_only'], $delta)); + db_query("INSERT INTO {civicrm_subscribe_form} (email_only, group_id) VALUES (%d, %d)", $edit['email_only'], $delta); } break; @@ -317,48 +325,78 @@ function civicrm_subscribe_form_validate */ function civicrm_subscribe($email, $params = array(), $groups = array()) { if (module_exists('civicrm')) { - civicrm_initialize(true); - + civicrm_initialize(); + require_once('api/v2/Contact.php'); // if 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)) { - // update first and last name only if we don't already have a value - if (isset($contact->first_name) || !isset($params['first_name'])) { - unset($params['first_name']); - } - if (isset($contact->last_name) || !isset($params['last_name'])) { - unset($params['last_name']); - } - if (count($params)) { - crm_update_contact($contact, $params); - } - } - else { - $params['email'] = $email; - $params['do_not_email'] = true; - $contact =& crm_create_contact($params); - } - if ($contact && isset($contact->id)) { - // if this is a new record - 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)); + $params['email'] = $email; + $contact =& civicrm_contact_get($params); + // if there was an error it was because of no match: the error string say "0 contacts matching ..." + // multiple matches on all fields we will let go to an error + if ($contact['is_error'] && !strcmp('0', substr($contact['error_message'],0,1) ) ) { + + // check for an inexact match on just email match + $params2 = array('email'=>$params['email']); + $contact =& civicrm_contact_get($params2); + if (!$contact['is_error'] && isset($contact['contact_id'])) { + // update first and last name only if we don't already have a value + $ischanged = false; + if (!isset($contact['first_name']) && isset($params['first_name'])) { + $contact['first_name'] = $params['first_name']; + $ischanged = true; + } + if (!isset($contact['last_name']) && isset($params['last_name'])) { + $contact['last_name'] = $params['last_name']; + $ischanged = true; + } + if ($ischanged) { + $contact['dupe_check']= 0; + $result = civicrm_contact_add($contact); + } } + // else if the error was because there were no matches then create a new contact + elseif (!strcmp('0', substr($contact['error_message'],0,1)) ) { + // create new contact + $params['email'] = $email; + $params['do_not_email'] = true; + $params['contact_type'] = 'Individual'; + $contact =& civicrm_contact_add($params); + $params = array('id'=>$contact['contact_id']); + $contact =& civicrm_contact_get($params); + } + } + + if ($contact && isset($contact['contact_id'])) { + + // TODO: decide how to use do_not_email flag, if at all + //// if this is a new record or user had opted out of email + //if ($contact['do_not_email']) { + // // NOTE: previously this has value was read from the contact, but v2 CiviCRM API does not + // // expose this, so just create a random hash + // $hash = md5( uniqid( rand( ), true ) ); + // _civicrm_subscribe_insert($contact['contact_id'], $hash); + //} + + // create a new hash value + $hash = md5( uniqid( rand( ), true ) ); + _civicrm_subscribe_insert($contact['contact_id'], $hash); // 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)); + $params = array('id' => $group_id); + $ccrm_groups =& civicrm_groups_get($params); if ($ccrm_groups && count($ccrm_groups) == 1) { - $group = $ccrm_groups[0]; - $contacts = array($contact); - if ($error = crm_add_group_contacts($group, $contacts)) { + $group = current($ccrm_groups); + //$contacts = array($contact); + $params = array('group_id' => $group['id'], 'contact_id'=> $contact['contact_id']); + $error = civicrm_group_contact_add($params); + if ($error['is_error']) { $messages[] = _civicrm_subscribe_error($error); } else { @@ -369,7 +407,7 @@ function civicrm_subscribe($email, $para $group_name = implode(', ', $group_names); // 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); @@ -398,14 +436,17 @@ function _civicrm_subscribe_error($error function civicrm_subscribe_confirm_page() { if (module_exists('civicrm')) { + require_once('api/v2/Contact.php'); if ($ccid = db_result(db_query("SELECT ccid FROM {civicrm_subscribe} WHERE hash='%s'", arg(2)))) { - civicrm_initialize(true); - - $contact =& crm_get_contact(array('id' => $ccid)); + civicrm_initialize(); + $params = array('id' => $ccid); + $contact =& civicrm_contact_get($params); - if ($contact && isset($contact->id)) { + if (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)); + $contact['do_not_email'] = 0; + $contact['dupe_check']= 0; + $result = civicrm_contact_add($contact); $confirmed = 1; // if we need to create a Drupal user, @@ -413,7 +454,7 @@ function civicrm_subscribe_confirm_page( // make sure the user doesn't exist first // NOTE: this is slightly different than the reset password hash link, // don't allow this as a backdoor to logging in - $email = $contact->location[1]->email[1]->email; + $email = $contact['location'][1]['email'][1]['email']; if (!user_load(array('mail' => $email))) { // create the new Drupal user $userinfo = array( @@ -520,7 +561,7 @@ function _civicrm_subscribe_variable_get } 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() { @@ -532,21 +573,27 @@ function _civicrm_subscribe_variable_get * invalidates the CCRM email address for the user and sends a new confirmation */ function civicrm_subscribe_user($type, &$edit, &$edituser, $category = NULL) { - // when a user changes their own email address on the user edit form + // TODO: this feature may not be desired by an users, so it would be a good feature to add to the module settings + // when a user changes their own email address on the user edit form change do not email status to true 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)) { - // change the civicrm email, and set the flag to do not email while we confirm the new email address + require_once('api/v2/Contact.php'); + // find the existing civicrm contact record, assuming unique email + civicrm_initialize(); + $params = array('email' => $edituser->mail); + $contact =& civicrm_contact_get($params); + if (!isset($contact['is_error'])) { + // set the civicrm email, to do not email while we confirm the new email address $params = array( - 'email' => $edit['mail'], + // 'email' => $edit['mail'], // i think civicrm core has a hook to handle this 'do_not_email' => true, + 'dupe_check' => 0 ); - $contact =& crm_update_contact($contact, $params); + $contact =& civicrm_contact_add($contact, $params); // send the re-confirmation email + $hash = md5( uniqid( rand( ), true ) ); + _civicrm_subscribe_insert($contact['contact_id'], $hash); _civicrm_subscribe_send_reconfirm_email($edit['mail'], $contact->hash); // display message to user