本文整理汇总了PHP中CRM_Mailing_Event_BAO_Subscribe::save方法的典型用法代码示例。如果您正苦于以下问题:PHP CRM_Mailing_Event_BAO_Subscribe::save方法的具体用法?PHP CRM_Mailing_Event_BAO_Subscribe::save怎么用?PHP CRM_Mailing_Event_BAO_Subscribe::save使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CRM_Mailing_Event_BAO_Subscribe
的用法示例。
在下文中一共展示了CRM_Mailing_Event_BAO_Subscribe::save方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: array
/**
* Register a subscription event. Create a new contact if one does not
* already exist.
*
* @param int $group_id
* The group id to subscribe to.
* @param string $email
* The email address of the (new) contact.
* @param int $contactId
* Currently used during event registration/contribution.
* Specifically to avoid linking group to wrong duplicate contact
* during event registration.
* @param string $context
*
* @return int|null
* $se_id The id of the subscription event, null on failure
*/
public static function &subscribe($group_id, $email, $contactId = NULL, $context = NULL)
{
// CRM-1797 - allow subscription only to public groups
$params = array('id' => (int) $group_id);
$defaults = array();
$contact_id = NULL;
$success = NULL;
$bao = CRM_Contact_BAO_Group::retrieve($params, $defaults);
if ($bao && substr($bao->visibility, 0, 6) != 'Public' && $context != 'profile') {
return $success;
}
$strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower';
$email = $strtolower($email);
// process the query only if no contactId
if ($contactId) {
$contact_id = $contactId;
} else {
/* First, find out if the contact already exists */
$query = "\n SELECT DISTINCT contact_a.id as contact_id\n FROM civicrm_contact contact_a\nLEFT JOIN civicrm_email ON contact_a.id = civicrm_email.contact_id\n WHERE civicrm_email.email = %1 AND contact_a.is_deleted = 0";
$params = array(1 => array($email, 'String'));
$dao = CRM_Core_DAO::executeQuery($query, $params);
$id = array();
// lets just use the first contact id we got
if ($dao->fetch()) {
$contact_id = $dao->contact_id;
}
$dao->free();
}
$transaction = new CRM_Core_Transaction();
if (!$contact_id) {
require_once 'CRM/Utils/DeprecatedUtils.php';
/* If the contact does not exist, create one. */
$formatted = array('contact_type' => 'Individual', 'version' => 3);
$locationType = CRM_Core_BAO_LocationType::getDefault();
$value = array('email' => $email, 'location_type_id' => $locationType->id);
_civicrm_api3_deprecated_add_formatted_param($value, $formatted);
$formatted['onDuplicate'] = CRM_Import_Parser::DUPLICATE_SKIP;
$formatted['fixAddress'] = TRUE;
require_once 'api/api.php';
$contact = civicrm_api('contact', 'create', $formatted);
if (civicrm_error($contact)) {
return $success;
}
$contact_id = $contact['id'];
} elseif (!is_numeric($contact_id) && (int) $contact_id > 0) {
// make sure contact_id is numeric
return $success;
}
/* Get the primary email id from the contact to use as a hash input */
$dao = new CRM_Core_DAO();
$query = "\nSELECT civicrm_email.id as email_id\n FROM civicrm_email\n WHERE civicrm_email.email = %1\n AND civicrm_email.contact_id = %2";
$params = array(1 => array($email, 'String'), 2 => array($contact_id, 'Integer'));
$dao = CRM_Core_DAO::executeQuery($query, $params);
if (!$dao->fetch()) {
CRM_Core_Error::fatal('Please file an issue with the backtrace');
return $success;
}
$se = new CRM_Mailing_Event_BAO_Subscribe();
$se->group_id = $group_id;
$se->contact_id = $contact_id;
$se->time_stamp = date('YmdHis');
$se->hash = substr(sha1("{$group_id}:{$contact_id}:{$dao->email_id}:" . time()), 0, 16);
$se->save();
$contacts = array($contact_id);
CRM_Contact_BAO_GroupContact::addContactsToGroup($contacts, $group_id, 'Email', 'Pending', $se->id);
$transaction->commit();
return $se;
}