--- civicrm_subscribe.1/civicrm_subscribe.module 2007-12-16 07:37:26.000000000 -0800 +++ civicrm_subscribe/civicrm_subscribe.module 2008-03-21 18:12:44.000000000 -0700 @@ -36,15 +36,20 @@ 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); + print_r($groups[1]->id); + + $test["asd"] = 'asdf'; + $test[2] = 'df'; + $options[0] = '-- Select One --'; foreach ($groups as $group) { - $options[$group->id] = $group->title; + $options[$group["id"]] = $group["title"]; } asort($options); @@ -128,15 +133,17 @@ function civicrm_subscribe_form() { $groups = (arg(0) == 'user' && arg(1) == 'subscribe' && is_numeric($group_id = arg(2))) ? array($group_id) : array(); // 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)) { $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); } @@ -266,48 +273,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']); + $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); + } } - if (count($params)) { - crm_update_contact($contact, $params); + // 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); } } - 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)); - } + + 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 { @@ -318,7 +355,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); @@ -347,14 +384,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, @@ -362,7 +402,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( @@ -469,7 +509,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() { @@ -481,21 +521,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