/** * takes an associative array and creates a contact object * * the function extract all the params it needs to initialize the create a * contact object. the params array could contain additional unused name/value * pairs * * @param array $params (reference ) an assoc array of name/value pairs * @param array $ids the array that holds all the db ids * @param array $locationId * * @return object CRM_Core_BAO_Location object on success, null otherwise * @access public * @static */ function add(&$params, &$ids, $locationId) { if (!CRM_Core_BAO_Location::dataExists($params, $locationId, $ids)) { return null; } $location =& new CRM_Core_BAO_Location(); if (!isset($params['contact_id'])) { require_once 'CRM/Core/BAO/Domain.php'; $location->entity_table = CRM_Core_BAO_Domain::getTableName(); $location->entity_id = $params['domain_id']; } else { $location->entity_table = CRM_Contact_BAO_Contact::getTableName(); $location->entity_id = $params['contact_id']; } $location->location_type_id = CRM_Utils_Array::value('location_type_id', $params['location'][$locationId]); $location->name = CRM_Utils_Array::value('name', $params['location'][$locationId]); $location->is_primary = CRM_Utils_Array::value('is_primary', $params['location'][$locationId], false); // check if there exists another location has is_primary set, and if so reset that // if no location has is_primary, make this one is_primart if ($location->is_primary) { // reset all other locations with the same entity table entity id $sql = "UPDATE " . CRM_Core_BAO_Location::getTableName() . "\n SET is_primary = 0 WHERE \n entity_table = '{$location->entity_table}' AND\n entity_id = '{$location->entity_id}' "; CRM_Core_DAO::executeQuery($sql); } else { // make sure there is at once location with is_primary set $sql = "SELECT count( " . CRM_Core_BAO_Location::getTableName() . ".id )\n FROM " . CRM_Core_BAO_Location::getTableName() . " WHERE\n entity_table = '{$location->entity_table}' AND\n entity_id = '{$location->entity_id}' AND\n is_primary = 1"; $count = CRM_Core_DAO::singleValueQuery($sql); if ($count == 0) { $location->is_primary = true; } } $location->id = CRM_Utils_Array::value('id', $ids['location'][$locationId]); $location->save(); $params['location'][$locationId]['id'] = $location->id; $address_object = CRM_Core_BAO_Address::add($params, $ids, $locationId); $location->address = $address_object; // set this to true if this has been made the primary IM. // the rule is the first entered value is the primary object $isPrimaryPhone = $isPrimaryEmail = $isPrimaryIM = true; $location->phone = array(); $location->email = array(); $location->im = array(); for ($i = 1; $i <= CRM_CONTACT_FORM_LOCATION_BLOCKS; $i++) { $location->phone[$i] = CRM_Core_BAO_Phone::add($params, $ids, $locationId, $i, $isPrimaryPhone); $location->email[$i] = CRM_Core_BAO_Email::add($params, $ids, $locationId, $i, $isPrimaryEmail); $location->im[$i] = CRM_Core_BAO_IM::add($params, $ids, $locationId, $i, $isPrimaryIM); } return $location; }
/** * Register a subscription event. Create a new contact if one does not * already exist. * * @param int $domain_id The domain id of the new subscription * @param int $group_id The group id to subscribe to * @param string $email The email address of the (new) contact * @return int|null $se_id The id of the subscription event, null on failure * @access public * @static */ function &subscribe($domain_id, $group_id, $email) { /* First, find out if the contact already exists */ $params = array('email' => $email, 'domain_id' => $domain_id); require_once 'CRM/Core/BAO/UFGroup.php'; $contact_id = CRM_Core_BAO_UFGroup::findContact($params); CRM_Core_DAO::transaction('BEGIN'); if (is_a($contact_id, CRM_Core_Error)) { require_once 'CRM/Core/BAO/LocationType.php'; /* If the contact does not exist, create one. */ $formatted = array('contact_type' => 'Individual'); $value = array('email' => $email, 'location_type' => CRM_Core_BAO_LocationType::getDefaultID()); _crm_add_formatted_param($value, $formatted); $contact =& crm_create_contact_formatted($formatted, CRM_IMPORT_PARSER_DUPLICATE_SKIP); if (is_a($contact, CRM_Core_Error)) { return null; } $contact_id = $contact->id; } require_once 'CRM/Core/BAO/Email.php'; require_once 'CRM/Core/BAO/Location.php'; require_once 'CRM/Contact/BAO/Contact.php'; /* Get the primary email id from the contact to use as a hash input */ $dao =& new CRM_Core_DAO(); $emailTable = CRM_Core_BAO_Email::getTableName(); $locTable = CRM_Core_BAO_Location::getTableName(); $contactTable = CRM_Contact_BAO_Contact::getTableName(); $dao->query("SELECT {$emailTable}.id as email_id\n FROM {$emailTable}\n INNER JOIN {$locTable}\n ON {$emailTable}.location_id = {$locTable}.id\n WHERE {$emailTable}.is_primary = 1\n AND {$locTable}.is_primary = 1\n AND {$locTable}.entity_table = '{$contactTable}'\n AND {$locTable}.entity_id = " . CRM_Utils_Type::escape($contact_id, 'Integer')); $dao->fetch(); $se =& new CRM_Mailing_Event_BAO_Subscribe(); $se->group_id = $group_id; $se->contact_id = $contact_id; $se->time_stamp = date('YmdHis'); $se->hash = sha1("{$group_id}:{$contact_id}:{$dao->email_id}"); $se->save(); $contacts = array($contact_id); require_once 'CRM/Contact/BAO/GroupContact.php'; CRM_Contact_BAO_GroupContact::addContactsToGroup($contacts, $group_id, 'Email', 'Pending', $se->id); CRM_Core_DAO::transaction('COMMIT'); return $se; }
/** * Returns array of contacts who are members of the specified group. * * @param CRM_Contact $group A valid group object (passed by reference) * @param array $returnProperties Which properties * should be included in the returned Contact object(s). If NULL, * the default set of contact properties will be * included. group_contact properties (such as 'status', * ' in_date', etc.) are included automatically.Note:Do not inclue * Id releted properties. * @param text $status A valid status value ('Added', 'Pending', 'Removed'). * @param text $sort Associative array of * one or more "property_name"=>"sort direction" * pairs which will control order of Contact objects returned. * @param Int $offset Starting row index. * @param Int $row_count Maximum number of rows to returns. * * * @return $contactArray Array of contacts who are members of the specified group * * @access public */ static function getGroupContacts(&$group, $returnProperties = null, $status = 'Added', $sort = null, $offset = null, $row_count = null, $includeChildGroups = false) { $groupDAO =& new CRM_Contact_DAO_Group(); $groupDAO->id = $group->id; if (!$groupDAO->find(true)) { return CRM_Core_Error::createError("Could not locate group with id: {$id}"); } // make sure user has got permission to view this group require_once 'CRM/Contact/BAO/Group.php'; if (!CRM_Contact_BAO_Group::checkPermission($groupDAO->id, $groupDAO->title)) { return CRM_Core_Error::createError("You do not have permission to access group with id: {$id}"); } $query = ''; if (empty($returnProperties)) { $query = "SELECT contact_a.id as contact_id,\n civicrm_email.email as email"; } else { $query = "SELECT contact_a.id as contact_id , {$grpStatus} as status,"; $query .= implode(',', $returnProperties); } $params = array(); if ($includeChildGroups) { require_once 'CRM/Contact/BAO/GroupNesting.php'; $groupIds = CRM_Contact_BAO_GroupNesting::getDescendentGroupIds(array($group->id)); } else { $groupIds = array($group->id); } foreach ($groupIds as $groupId) { $params[] = array('group', 'IN', array($group->id => true), 0, 0); } require_once 'CRM/Core/BAO/Email.php'; require_once 'CRM/Contact/BAO/Contact.php'; $tables = array(CRM_Core_BAO_Email::getTableName() => true, CRM_Contact_BAO_Contact::getTableName() => true); $inner = array(); $whereTables = array(); $where = CRM_Contact_BAO_Query::getWhereClause($params, null, $tables, $whereTables); $permission = CRM_Core_Permission::whereClause(CRM_Core_Permission::VIEW, $tables, $whereTables); $from = CRM_Contact_BAO_Query::fromClause($tables, $inner); $query .= " {$from} WHERE {$permission} AND {$where} "; if ($sort != null) { $order = array(); foreach ($sort as $key => $direction) { $order[] = " {$key} {$direction} "; } $query .= " ORDER BY " . implode(',', $order); } if (!is_null($offset) && !is_null($row_count)) { $query .= " LIMIT {$offset}, {$row_count}"; } $dao =& new CRM_Contact_DAO_Contact(); $dao->query($query); // this is quite inefficient, we need to change the return // values in docs $contactArray = array(); while ($dao->fetch()) { $contactArray[] = clone $dao; } return $contactArray; }
/** * Get rows for the event browser * * @param int $mailing_id ID of the mailing * @param int $job_id optional ID of the job * @param boolean $is_distinct Group by queue id? * @param int $offset Offset * @param int $rowCount Number of rows * @param array $sort sort array * @return array Result set * @access public * @static */ function &getRows($mailing_id, $job_id = null, $is_distinct = false, $offset = null, $rowCount = null, $sort = null) { $dao =& new CRM_Core_Dao(); $bounce = CRM_Mailing_Event_BAO_Bounce::getTableName(); $bounceType = CRM_Mailing_DAO_BounceType::getTableName(); $queue = CRM_Mailing_Event_BAO_Queue::getTableName(); $mailing = CRM_Mailing_BAO_Mailing::getTableName(); $job = CRM_Mailing_BAO_Job::getTableName(); $contact = CRM_Contact_BAO_Contact::getTableName(); $email = CRM_Core_BAO_Email::getTableName(); $query = "\n SELECT {$contact}.display_name as display_name,\n {$contact}.id as contact_id,\n {$email}.email as email,\n {$bounce}.time_stamp as date,\n {$bounce}.bounce_reason as reason,\n {$bounceType}.name as bounce_type\n FROM {$contact}\n INNER JOIN {$queue}\n ON {$queue}.contact_id = {$contact}.id\n INNER JOIN {$email}\n ON {$queue}.email_id = {$email}.id\n INNER JOIN {$bounce}\n ON {$bounce}.event_queue_id = {$queue}.id\n LEFT JOIN {$bounceType}\n ON {$bounce}.bounce_type_id = {$bounceType}.id\n INNER JOIN {$job}\n ON {$queue}.job_id = {$job}.id\n INNER JOIN {$mailing}\n ON {$job}.mailing_id = {$mailing}.id\n WHERE {$mailing}.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer'); if (!empty($job_id)) { $query .= " AND {$job}.id = " . CRM_Utils_Type::escape($job_id, 'Integer'); } if ($is_distinct) { $query .= " GROUP BY {$queue}.id "; } $query .= " ORDER BY {$contact}.sort_name, {$bounce}.time_stamp "; if ($offset) { $query .= ' LIMIT ' . CRM_Utils_Type::escape($offset, 'Integer') . ', ' . CRM_Utils_Type::escape($rowCount, 'Integer'); } $dao->query($query); $results = array(); while ($dao->fetch()) { $url = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$dao->contact_id}"); $results[] = array('name' => "<a href=\"{$url}\">{$dao->display_name}</a>", 'email' => $dao->email, 'type' => empty($dao->bounce_type) ? ts('Unknown') : $dao->bounce_type, 'reason' => $dao->reason, 'date' => CRM_Utils_Date::customFormat($dao->date)); } return $results; }
/** * Get rows for the event browser * * @param int $mailing_id ID of the mailing * @param int $job_id optional ID of the job * @param boolean $is_distinct Group by queue id? * @param int $offset Offset * @param int $rowCount Number of rows * @param array $sort sort array * @return array Result set * @access public * @static */ public static function &getRows($mailing_id, $job_id = null, $is_distinct = false, $offset = null, $rowCount = null, $sort = null) { $dao =& new CRM_Core_Dao(); $unsub = self::getTableName(); $queue = CRM_Mailing_Event_BAO_Queue::getTableName(); $mailing = CRM_Mailing_BAO_Mailing::getTableName(); $job = CRM_Mailing_BAO_Job::getTableName(); $contact = CRM_Contact_BAO_Contact::getTableName(); $email = CRM_Core_BAO_Email::getTableName(); $query = "\n SELECT {$contact}.display_name as display_name,\n {$contact}.id as contact_id,\n {$email}.email as email,\n {$unsub}.time_stamp as date,\n {$unsub}.org_unsubscribe as org_unsubscribe\n FROM {$contact}\n INNER JOIN {$queue}\n ON {$queue}.contact_id = {$contact}.id\n INNER JOIN {$email}\n ON {$queue}.email_id = {$email}.id\n INNER JOIN {$unsub}\n ON {$unsub}.event_queue_id = {$queue}.id\n INNER JOIN {$job}\n ON {$queue}.job_id = {$job}.id\n INNER JOIN {$mailing}\n ON {$job}.mailing_id = {$mailing}.id\n AND {$job}.is_test = 0\n WHERE {$mailing}.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer'); if (!empty($job_id)) { $query .= " AND {$job}.id = " . CRM_Utils_Type::escape($job_id, 'Integer'); } if ($is_distinct) { $query .= " GROUP BY {$queue}.id "; } $query .= " ORDER BY {$contact}.sort_name, {$unsub}.time_stamp DESC "; if ($offset) { $query .= ' LIMIT ' . CRM_Utils_Type::escape($offset, 'Integer') . ', ' . CRM_Utils_Type::escape($rowCount, 'Integer'); } $dao->query($query); $results = array(); while ($dao->fetch()) { $url = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$dao->contact_id}"); $results[] = array('name' => "<a href=\"{$url}\">{$dao->display_name}</a>", 'email' => $dao->email, 'org' => $dao->org_unsubscribe ? ts('Yes') : ts('No'), 'date' => CRM_Utils_Date::customFormat($dao->date)); } return $results; }
/** * Get rows for the event browser * * @param int $mailing_id ID of the mailing * @param int $job_id optional ID of the job * @param boolean $is_distinct Group by queue id? * @param int $offset Offset * @param int $rowCount Number of rows * @param array $sort sort array * @return array Result set * @access public * @static */ public static function &getRows($mailing_id, $job_id = null, $is_distinct = false, $offset = null, $rowCount = null, $sort = null) { $dao =& new CRM_Core_Dao(); $forward = self::getTableName(); $queue = CRM_Mailing_Event_BAO_Queue::getTableName(); $mailing = CRM_Mailing_BAO_Mailing::getTableName(); $job = CRM_Mailing_BAO_Job::getTableName(); $contact = CRM_Contact_BAO_Contact::getTableName(); $email = CRM_Core_BAO_Email::getTableName(); $query = "\n SELECT {$contact}.display_name as from_name,\n {$contact}.id as from_id,\n {$email}.email as from_email,\n dest_contact.id as dest_id,\n dest_email.email as dest_email,\n {$forward}.time_stamp as date\n FROM {$contact}\n INNER JOIN {$queue}\n ON {$queue}.contact_id = {$contact}.id\n INNER JOIN {$email}\n ON {$queue}.email_id = {$email}.id\n INNER JOIN {$forward}\n ON {$forward}.event_queue_id = {$queue}.id\n INNER JOIN {$queue} as dest_queue\n ON {$forward}.dest_queue_id = dest_queue.id\n INNER JOIN {$contact} as dest_contact\n ON dest_queue.contact_id = dest_contact.id\n INNER JOIN {$email} as dest_email\n ON dest_queue.email_id = dest_email.id\n INNER JOIN {$job}\n ON {$queue}.job_id = {$job}.id\n INNER JOIN {$mailing}\n ON {$job}.mailing_id = {$mailing}.id\n AND {$job}.is_test = 0\n WHERE {$mailing}.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer'); if (!empty($job_id)) { $query .= " AND {$job}.id = " . CRM_Utils_Type::escape($job_id, 'Integer'); } if ($is_distinct) { $query .= " GROUP BY {$queue}.id "; } $query .= " ORDER BY {$contact}.sort_name, {$forward}.time_stamp DESC "; if ($offset || $rowCount) { //Added "||$rowCount" to avoid displaying all records on first page $query .= ' LIMIT ' . CRM_Utils_Type::escape($offset, 'Integer') . ', ' . CRM_Utils_Type::escape($rowCount, 'Integer'); } $dao->query($query); $results = array(); while ($dao->fetch()) { $from_url = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$dao->from_id}"); $dest_url = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$dao->dest_id}"); $results[] = array('from_name' => "<a href=\"{$from_url}\">{$dao->from_name}</a>", 'from_email' => $dao->from_email, 'dest_email' => "<a href=\"{$dest_url}\">{$dao->dest_email}</a>", 'date' => CRM_Utils_Date::customFormat($dao->date)); } return $results; }
/** * returns the column headers as an array of tuples: * (name, sortName (key to the sort array)) * * @param string $action the action being performed * @param enum $output what should the result set include (web/email/csv) * * @return array the column headers that need to be displayed * @access public */ function &getColumnHeaders($action = NULL, $output = NULL) { $mailing = CRM_Mailing_BAO_Mailing::getTableName(); $contact = CRM_Contact_BAO_Contact::getTableName(); $email = CRM_Core_BAO_Email::getTableName(); $job = CRM_Mailing_BAO_MailingJob::getTableName(); if (!isset($this->_columnHeaders)) { $this->_columnHeaders = array(array('name' => ts('Contact'), 'sort' => $contact . '.sort_name', 'direction' => CRM_Utils_Sort::ASCENDING), array('name' => ts('Email Address'), 'sort' => $email . '.email', 'direction' => CRM_Utils_Sort::DONTCARE)); switch ($this->_event_type) { case 'queue': $dateSort = $job . '.start_date'; break; case 'delivered': $dateSort = CRM_Mailing_Event_BAO_Delivered::getTableName() . '.time_stamp'; break; case 'opened': $dateSort = CRM_Mailing_Event_BAO_Opened::getTableName() . '.time_stamp'; break; case 'bounce': $dateSort = CRM_Mailing_Event_BAO_Bounce::getTableName() . '.time_stamp'; $this->_columnHeaders = array_merge($this->_columnHeaders, array(array('name' => ts('Bounce Type')), array('name' => ts('Bounce Reason')))); break; case 'forward': $dateSort = CRM_Mailing_Event_BAO_Forward::getTableName() . '.time_stamp'; $this->_columnHeaders = array_merge($this->_columnHeaders, array(array('name' => ts('Forwarded Email')))); break; case 'reply': $dateSort = CRM_Mailing_Event_BAO_Reply::getTableName() . '.time_stamp'; break; case 'unsubscribe': $dateSort = CRM_Mailing_Event_BAO_Unsubscribe::getTableName() . '.time_stamp'; $this->_columnHeaders = array_merge($this->_columnHeaders, array(array('name' => ts('Unsubscribe')))); break; case 'optout': $dateSort = CRM_Mailing_Event_BAO_Unsubscribe::getTableName() . '.time_stamp'; $this->_columnHeaders = array_merge($this->_columnHeaders, array(array('name' => ts('Opt-Out')))); break; case 'click': $dateSort = CRM_Mailing_Event_BAO_TrackableURLOpen::getTableName() . '.time_stamp'; $this->_columnHeaders = array_merge($this->_columnHeaders, array(array('name' => ts('URL')))); break; default: return 0; } $this->_columnHeaders = array_merge($this->_columnHeaders, array(array('name' => ts('Date'), 'sort' => $dateSort, 'direction' => CRM_Utils_Sort::DESCENDING))); } return $this->_columnHeaders; }
/** * Get all of the ACLs through ACL groups * * @param int $contact_id - ID of a contact to search for * @param int $group_id - ID of a group to search for * @return array - Array of assoc. arrays of ACL rules * @access public * @static */ function &getACLGroups($contact_id = null, $group_id = null) { $contact_id = CRM_Utils_Type::escape($contact_id, 'Integer'); $group_id = CRM_Utils_Type::escape($group_id, 'Integer'); $rule =& new CRM_Core_BAO_ACL(); $acl = CRM_Core_BAO_ACL::getTableName(); $aclGroup = CRM_Core_BAO_ACLGroup::getTableName(); $contact = CRM_Contact_BAO_Contact::getTableName(); $domain = CRM_Core_DAO_Domain::getTableName(); $c2g = CRM_Contact_BAO_GroupContact::getTableName(); $group = CRM_Contact_BAO_Group::getTableName(); $session =& CRM_Core_Session::singleton(); $domainId = $session->get('domainID'); $query = " SELECT {$acl}.* \n FROM {$acl}\n INNER JOIN {$aclGroup}\n ON {$acl}.entity_table = '{$aclGroup}'\n AND {$acl}.entity_id = {$aclGroup}.id"; if (!empty($group_id)) { $query .= " INNER JOIN {$c2g}\n ON {$aclGroup}.entity_id = {$c2g}.group_id\n WHERE {$aclGroup}.entity_table = '{$group}'\n AND {$aclGroup}.is_active = 1\n AND {$c2g}.group_id = {$group_id}"; if (!empty($contact_id)) { $query .= " AND {$c2g}.contact_id = {$contact_id}\n AND {$c2g}.status = 'Added'"; } } else { if (!empty($contact_id)) { $query .= " WHERE {$aclGroup}.entity_table = '{$contact}'\n AND {$aclGroup}.is_active = 1\n AND {$aclGroup}.entity_id = {$contact_id}"; } else { $query .= " WHERE {$aclGroup}.entity_table = '{$domain}'\n AND {$aclGroup}.is_active = 1\n AND {$aclGroup}.entity_id = {$domain_id}"; } } $results = array(); $rule->query($query); while ($rule->fetch()) { $results[] =& $rule->toArray(); } return $results; }
/** * Get rows for the event browser * * @param int $mailing_id ID of the mailing * @param int $job_id optional ID of the job * @param boolean $is_distinct Group by queue id? * @param int $offset Offset * @param int $rowCount Number of rows * @param array $sort sort array * * @return array Result set * @access public * @static */ public static function &getRows($mailing_id, $job_id = NULL, $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL) { $dao = new CRM_Core_Dao(); $bounce = self::getTableName(); $bounceType = CRM_Mailing_DAO_BounceType::getTableName(); $queue = CRM_Mailing_Event_BAO_Queue::getTableName(); $mailing = CRM_Mailing_BAO_Mailing::getTableName(); $job = CRM_Mailing_BAO_MailingJob::getTableName(); $contact = CRM_Contact_BAO_Contact::getTableName(); $email = CRM_Core_BAO_Email::getTableName(); $query = "\n SELECT {$contact}.display_name as display_name,\n {$contact}.id as contact_id,\n {$email}.email as email,\n {$bounce}.time_stamp as date,\n {$bounce}.bounce_reason as reason,\n {$bounceType}.name as bounce_type\n FROM {$contact}\n INNER JOIN {$queue}\n ON {$queue}.contact_id = {$contact}.id\n INNER JOIN {$email}\n ON {$queue}.email_id = {$email}.id\n INNER JOIN {$bounce}\n ON {$bounce}.event_queue_id = {$queue}.id\n LEFT JOIN {$bounceType}\n ON {$bounce}.bounce_type_id = {$bounceType}.id\n INNER JOIN {$job}\n ON {$queue}.job_id = {$job}.id\n AND {$job}.is_test = 0\n INNER JOIN {$mailing}\n ON {$job}.mailing_id = {$mailing}.id\n WHERE {$mailing}.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer'); if (!empty($job_id)) { $query .= " AND {$job}.id = " . CRM_Utils_Type::escape($job_id, 'Integer'); } if ($is_distinct) { $query .= " GROUP BY {$queue}.id "; } $orderBy = "sort_name ASC, {$bounce}.time_stamp DESC"; if ($sort) { if (is_string($sort)) { $sort = CRM_Utils_Type::escape($sort, 'String'); $orderBy = $sort; } else { $orderBy = trim($sort->orderBy()); } } $query .= " ORDER BY {$orderBy} "; if ($offset || $rowCount) { //Added "||$rowCount" to avoid displaying all records on first page $query .= ' LIMIT ' . CRM_Utils_Type::escape($offset, 'Integer') . ', ' . CRM_Utils_Type::escape($rowCount, 'Integer'); } $dao->query($query); $results = array(); while ($dao->fetch()) { $url = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$dao->contact_id}"); $results[] = array('name' => "<a href=\"{$url}\">{$dao->display_name}</a>", 'email' => $dao->email, 'type' => empty($dao->bounce_type) ? ts('Unknown') : $dao->bounce_type, 'reason' => $dao->reason, 'date' => CRM_Utils_Date::customFormat($dao->date)); } return $results; }
/** * Get rows for the event browser. * * @param int $mailing_id * ID of the mailing. * @param int $job_id * Optional ID of the job. * @param bool $is_distinct * Group by queue id?. * @param int $offset * Offset. * @param int $rowCount * Number of rows. * @param array $sort * Sort array. * * @param null $org_unsubscribe * @return array * Result set */ public static function &getRows($mailing_id, $job_id = NULL, $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL, $org_unsubscribe = NULL) { $dao = new CRM_Core_Dao(); $unsub = self::$_tableName; $queueObject = new CRM_Mailing_Event_BAO_Queue(); $queue = $queueObject->getTableName(); $mailingObject = new CRM_Mailing_BAO_Mailing(); $mailing = $mailingObject->getTableName(); $jobObject = new CRM_Mailing_BAO_MailingJob(); $job = $jobObject->getTableName(); $contactObject = new CRM_Contact_BAO_Contact(); $contact = $contactObject->getTableName(); $emailObject = new CRM_Core_BAO_Email(); $email = $emailObject->getTableName(); $query = "\n SELECT {$contact}.display_name as display_name,\n {$contact}.id as contact_id,\n {$email}.email as email,\n {$unsub}.time_stamp as date,\n {$unsub}.org_unsubscribe as org_unsubscribe\n FROM {$contact}\n INNER JOIN {$queue}\n ON {$queue}.contact_id = {$contact}.id\n INNER JOIN {$email}\n ON {$queue}.email_id = {$email}.id\n INNER JOIN {$unsub}\n ON {$unsub}.event_queue_id = {$queue}.id\n INNER JOIN {$job}\n ON {$queue}.job_id = {$job}.id\n INNER JOIN {$mailing}\n ON {$job}.mailing_id = {$mailing}.id\n AND {$job}.is_test = 0\n WHERE {$mailing}.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer'); if (!empty($job_id)) { $query .= " AND {$job}.id = " . CRM_Utils_Type::escape($job_id, 'Integer'); } if ($org_unsubscribe !== NULL) { $query .= " AND {$unsub}.org_unsubscribe = " . ($org_unsubscribe ? 0 : 1); } if ($is_distinct) { $query .= " GROUP BY {$queue}.id "; } $orderBy = "sort_name ASC, {$unsub}.time_stamp DESC"; if ($sort) { if (is_string($sort)) { $sort = CRM_Utils_Type::escape($sort, 'String'); $orderBy = $sort; } else { $orderBy = trim($sort->orderBy()); } } $query .= " ORDER BY {$orderBy} "; if ($offset || $rowCount) { //Added "||$rowCount" to avoid displaying all records on first page $query .= ' LIMIT ' . CRM_Utils_Type::escape($offset, 'Integer') . ', ' . CRM_Utils_Type::escape($rowCount, 'Integer'); } $dao->query($query); $results = array(); while ($dao->fetch()) { $url = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$dao->contact_id}"); $results[] = array('name' => "<a href=\"{$url}\">{$dao->display_name}</a>", 'email' => $dao->email, 'unsubOrOptout' => ts('Yes'), 'date' => CRM_Utils_Date::customFormat($dao->date)); } return $results; }
/** * Send the mailing * * @param object $mailer A Mail object to send the messages * @return void * @access public */ public function deliver(&$mailer, $testParams = null) { require_once 'CRM/Mailing/BAO/Mailing.php'; $mailing =& new CRM_Mailing_BAO_Mailing(); $mailing->id = $this->mailing_id; $mailing->find(true); $eq =& new CRM_Mailing_Event_BAO_Queue(); $eqTable = CRM_Mailing_Event_BAO_Queue::getTableName(); $emailTable = CRM_Core_BAO_Email::getTableName(); $contactTable = CRM_Contact_BAO_Contact::getTableName(); $edTable = CRM_Mailing_Event_BAO_Delivered::getTableName(); $ebTable = CRM_Mailing_Event_BAO_Bounce::getTableName(); $query = " SELECT {$eqTable}.id,\n {$emailTable}.email as email,\n {$eqTable}.contact_id,\n {$eqTable}.hash\n FROM {$eqTable}\n INNER JOIN {$emailTable}\n ON {$eqTable}.email_id = {$emailTable}.id\n LEFT JOIN {$edTable}\n ON {$eqTable}.id = {$edTable}.event_queue_id\n LEFT JOIN {$ebTable}\n ON {$eqTable}.id = {$ebTable}.event_queue_id\n WHERE {$eqTable}.job_id = " . $this->id . "\n AND {$edTable}.id IS null\n AND {$ebTable}.id IS null"; $eq->query($query); static $config = null; $mailsProcessed = 0; if ($config == null) { $config =& CRM_Core_Config::singleton(); } $job_date = CRM_Utils_Date::isoToMysql($this->scheduled_date); $fields = array(); if (!empty($testParams)) { $mailing->from_name = ts('CiviCRM Test Mailer (%1)', array(1 => $mailing->from_name)); $mailing->subject = ts('Test Mailing:') . ' ' . $mailing->subject; } CRM_Mailing_BAO_Mailing::tokenReplace($mailing); // get and format attachments require_once 'CRM/Core/BAO/File.php'; $attachments =& CRM_Core_BAO_File::getEntityFile('civicrm_mailing', $mailing->id); if (defined('CIVICRM_MAIL_SMARTY')) { require_once 'CRM/Core/Smarty/resources/String.php'; civicrm_smarty_register_string_resource(); } // make sure that there's no more than $config->mailerBatchLimit mails processed in a run while ($eq->fetch()) { // if ( ( $mailsProcessed % 100 ) == 0 ) { // CRM_Utils_System::xMemory( "$mailsProcessed: " ); // } if ($config->mailerBatchLimit > 0 && $mailsProcessed >= $config->mailerBatchLimit) { $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments); return false; } $mailsProcessed++; $fields[] = array('id' => $eq->id, 'hash' => $eq->hash, 'contact_id' => $eq->contact_id, 'email' => $eq->email); if (count($fields) == self::MAX_CONTACTS_TO_PROCESS) { $isDelivered = $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments); if (!$isDelivered) { return $isDelivered; } $fields = array(); } } $isDelivered = $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments); return $isDelivered; }
function _crm_update_contact($contact, $values, $overwrite = true) { // first check to make sure the location arrays sync up $param = array("contact_id" => $contact->id); $contact = crm_get_contact($param); $locMatch = _crm_location_match($contact, $values); if (!$locMatch) { return _crm_error('Cannot update contact location'); } // it is possible that an contact type object record does not exist // if the contact_type_object is null etc, if so we create one if ($contact->contact_type_object == null) { require_once str_replace('_', DIRECTORY_SEPARATOR, "CRM_Contact_BAO_" . $contact->contact_type) . ".php"; eval('$contact->contact_type_object =& new CRM_Contact_BAO_' . $contact->contact_type . '( );'); $contact->contact_type_object->contact_id = $contact->id; } $sortNameArray = array(); // fix sort_name and display_name if ($contact->contact_type == 'Individual') { if ($overwrite || !isset($contact->contact_type_object->first_name)) { $firstName = CRM_Utils_Array::value('first_name', $values); } else { $firstName = null; } if (!$firstName) { $firstName = isset($contact->contact_type_object->first_name) ? $contact->contact_type_object->first_name : ''; } if ($overwrite || !isset($contact->contact_type_object->middle_name)) { $middleName = CRM_Utils_Array::value('middle_name', $values); } else { $middleName = null; } if (!$middleName) { $middleName = isset($contact->contact_type_object->middle_name) ? $contact->contact_type_object->middle_name : ''; } if ($overwrite || !isset($contact->contact_type_object->last_name)) { $lastName = CRM_Utils_Array::value('last_name', $values); } else { $lastName = null; } if (!$lastName) { $lastName = isset($contact->contact_type_object->last_name) ? $contact->contact_type_object->last_name : ''; } if ($overwrite || !isset($contact->contact_type_object->prefix_id)) { $prefix = CRM_Utils_Array::value('prefix', $values); } else { $prefix = null; } if (!$prefix) { if (isset($contact->contact_type_object->prefix_id)) { $prefix =& new CRM_Core_DAO_IndividualPrefix(); $prefix->id = $contact->contact_type_object->prefix_id; $prefix->find(); $prefix->fetch(); $prefix = $prefix->name; } else { $prefix = ""; } } if ($overwrite || !isset($contact->contact_type_object->suffix_id)) { $suffix = CRM_Utils_Array::value('suffix', $values); } else { $suffix = null; } if (!$suffix) { if (isset($contact->contact_type_object->suffix_id)) { $suffix =& new CRM_Core_DAO_IndividualSuffix(); $suffix->id = $contact->contact_type_object->suffix_id; $suffix->find(); $suffix->fetch(); $suffix = $suffix->name; } else { $suffix = ""; } } if ($overwrite) { $gender = CRM_Utils_Array::value('gender', $values); } else { $gender = null; } if ($gender) { $genderDao =& new CRM_Core_DAO_Gender(); $genderDao->name = $gender; $genderDao->find(true); $values['gender_id'] = $genderDao->id; } if ($lastName != "" && $firstName != "") { $values['sort_name'] = "{$lastName}, {$firstName}"; } else { if ($lastName != "") { $values['sort_name'] = "{$lastName}"; } else { if ($firstName != "") { $values['sort_name'] = "{$firstName}"; } } } $values['display_name'] = "{$prefix} {$firstName} {$middleName} {$lastName} {$suffix} "; } else { if ($contact->contact_type == 'Household') { if ($overwrite || !isset($contact->contact_type_object->household_name)) { $householdName = CRM_Utils_Array::value('household_name', $values); } else { $householdName = null; } if (!$householdName) { $householdName = isset($contact->contact_type_object->household_name) ? $contact->contact_type_object->household_name : ''; } $values['sort_name'] = $householdName; } else { if ($overwrite || !isset($contact->contact_type_object->organization_name)) { $organizationName = CRM_Utils_Array::value('organization_name', $values); } else { $organizationName = null; } if (!$organizationName) { $organizationName = isset($contact->contact_type_object->organization_name) ? $contact->contact_type_object->organization_name : ''; } $values['sort_name'] = $organizationName; } } _crm_update_object($contact, $values); _crm_update_object($contact->contact_type_object, $values); if (!isset($contact->location)) { $contact->location = array(); } if (!array_key_exists(1, $contact->location) || empty($contact->location[1])) { $contact->location[1] =& new CRM_Core_BAO_Location(); } $primary_location = null; foreach ($contact->location as $key => $loc) { if ($loc->is_primary) { $primary_location = $key; break; } } if (is_array($values['location'])) { foreach ($values['location'] as $updateLocation) { $emptyBlock = $contactLocationBlock = null; /* Scan the location array for the correct block to update */ foreach ($contact->location as $key => $loc) { if ($loc->location_type_id == $updateLocation['location_type_id']) { $contactLocationBlock = $key; break; } else { if (!isset($loc->location_type_id)) { $emptyBlock = $key; } } } if ($contactLocationBlock == null) { if ($emptyBlock != null) { $contactLocationBlock = $emptyBlock; } else { /* no matching blocks and no empty blocks, make a new one */ $contact->location[] =& new CRM_Core_BAO_Location(); $contactLocationBlock = count($contact->location); } } $updateLocation['entity_id'] = $contact->id; $updateLocation['entity_table'] = CRM_Contact_BAO_Contact::getTableName(); /* If we're not overwriting, copy old data back before updating */ if (!$overwrite) { _crm_update_from_object($contact->location[$contactLocationBlock], $updateLocation, true); } /* Make sure we only have one primary location */ if ($primary_location == null && $updateLocation['is_primary']) { $primary_location = $contactLocationBlock; } else { if ($primary_location != $contactLocationBlock) { $updateLocation['is_primary'] = false; } } _crm_update_object($contact->location[$contactLocationBlock], $updateLocation); if (!isset($contact->location[$contactLocationBlock]->address)) { $contact->location[$contactLocationBlock]->address =& new CRM_Core_BAO_Address(); } $updateLocation['address']['location_id'] = $contact->location[$contactLocationBlock]->id; if ($updateLocation['address']['state_province']) { $state_province =& new CRM_Core_DAO_StateProvince(); $state_province->name = $updateLocation['address']['state_province']; if (!$state_province->find(true)) { $state_province->name = null; $state_province->abbreviation = $updateLocation['address']['state_province']; $state_province->find(true); } $updateLocation['address']['state_province_id'] = $state_province->id; } if ($updateLocation['address']['country']) { $country =& new CRM_Core_DAO_Country(); $country->name = $updateLocation['address']['country']; if (!$country->find(true)) { $country->name = null; $country->iso_code = $updateLocation['address']['country']; $country->find(true); } $updateLocation['address']['country_id'] = $country->id; } if (!$overwrite) { _crm_update_from_object($contact->location[$contactLocationBlock]->address, $updateLocation['address'], true); } _crm_update_object($contact->location[$contactLocationBlock]->address, $updateLocation['address']); $blocks = array('Email', 'IM'); foreach ($blocks as $block) { $name = strtolower($block); if (!is_array($updateLocation[$name])) { continue; } if (!isset($contact->location[$contactLocationBlock]->{$name})) { $contact->location[$contactLocationBlock]->{$name} = array(); } $primary = null; foreach ($contact->location[$contactLocationBlock]->{$name} as $key => $value) { if ($value->is_primary) { $primary = $key; break; } } $propertyBlock = 1; foreach ($updateLocation[$name] as $property) { if (!isset($contact->location[$contactLocationBlock]->{$name}[$propertyBlock])) { require_once str_replace('_', DIRECTORY_SEPARATOR, "CRM_Core_BAO_" . $block) . ".php"; eval('$contact->location[$contactLocationBlock]->{$name}[$propertyBlock] =& new CRM_Core_BAO_' . $block . '( );'); } $property['location_id'] = $contact->location[$contactLocationBlock]->id; if (!$overwrite) { _crm_update_from_object($contact->location[$contactLocationBlock]->{$name}[$propertyBlock], $property, true); } if ($primary == null && $property['is_primary']) { $primary = $propertyBlock; } else { if ($primary != $propertyBlock) { $property['is_primary'] = false; } } _crm_update_object($contact->location[$contactLocationBlock]->{$name}[$propertyBlock], $property); $propertyBlock++; } } /* handle multiple phones */ if (is_array($updateLocation['phone'])) { if (!isset($contact->location[$contactLocationBlock]->phone)) { $contact->location[$contactLocationBlock]->phone = array(); } $primary_phone = null; foreach ($contact->location[$contactLocationBlock]->phone as $key => $value) { if ($value->is_primary) { $primary_phone = $key; break; } } foreach ($updateLocation['phone'] as $phone) { /* scan through the contact record for matching phone type at this location */ $contactPhoneBlock = null; foreach ($contact->location[$contactLocationBlock]->phone as $key => $contactPhoneBlock) { if ($contactPhoneBlock->phone_type_id == $phone['phone_type_id']) { $contactPhoneBlock = $key; break; } } if ($contactPhoneBlock == null) { if (empty($contact->location[$contactLocationBlock]->phone)) { $contactPhoneBlock = 1; } else { $contactPhoneBlock = count($contact->location[$contactLocationBlock]->phone) + 1; } $contact->location[$contactLocationBlock]->phone[$contactPhoneBlock] =& new CRM_Core_BAO_Phone(); } $phone['location_id'] = $contact->location[$contactLocationBlock]->id; if (!$overwrite) { _crm_update_from_object($contact->location[$contactLocationBlock]->phone[$contactPhoneBlock], $phone, true); } if ($primary_phone == null && $phone['is_primary']) { $primary_phone = $contactPhoneBlock; } else { if ($primary_phone != $contactPhoneBlock) { $phone['is_primary'] = false; } } _crm_update_object($contact->location[$contactLocationBlock]->phone[$contactPhoneBlock], $phone); } } } } /* Custom data */ if (is_array($values['custom'])) { foreach ($values['custom'] as $customValue) { /* get the field for the data type */ $field = CRM_Core_BAO_CustomValue::typeToField($customValue['type']); if (!$field) { /* FIXME failure! */ continue; } /* adjust the value if it's boolean */ if ($customValue['type'] == 'Boolean') { $value = CRM_Utils_String::strtobool($customValue['value']); } else { $value = $customValue['value']; } /* look for a matching existing custom value */ $match = false; foreach ($contact->custom_values as $cv) { if ($cv->custom_field_id == $customValue['custom_field_id']) { /* match */ $match = true; if ($overwrite) { $cv->{$field} = $value; $cv->save(); break; } } } if (!$match) { /* no match, so create a new CustomValue */ $cvParams = array('entity_table' => 'civicrm_contact', 'entity_id' => $contact->id, 'value' => $value, 'type' => $customValue['type'], 'custom_field_id' => $customValue['custom_field_id']); CRM_Core_BAO_CustomValue::create($cvParams); } } } return $contact; }
/** * Get rows for the event browser * * @param int $mailing_id ID of the mailing * @param int $job_id optional ID of the job * @param int $offset Offset * @param int $rowCount Number of rows * @param array $sort sort array * @return array Result set * @access public * @static */ public static function &getRows($mailing_id, $job_id = null, $offset = null, $rowCount = null, $sort = null) { $dao = new CRM_Core_Dao(); $queue = self::getTableName(); $mailing = CRM_Mailing_BAO_Mailing::getTableName(); $job = CRM_Mailing_BAO_Job::getTableName(); $contact = CRM_Contact_BAO_Contact::getTableName(); $email = CRM_Core_BAO_Email::getTableName(); $orderBy = "sort_name ASC, {$job}.start_date DESC"; if ($sort) { if (is_string($sort)) { $orderBy = $sort; } else { $orderBy = trim($sort->orderBy()); } } $query = "\n SELECT {$contact}.display_name as display_name,\n {$contact}.id as contact_id,\n {$email}.email as email,\n {$job}.start_date as date\n FROM {$contact}\n INNER JOIN {$queue}\n ON {$queue}.contact_id = {$contact}.id\n INNER JOIN {$email}\n ON {$queue}.email_id = {$email}.id\n INNER JOIN {$job}\n ON {$queue}.job_id = {$job}.id\n INNER JOIN {$mailing}\n ON {$job}.mailing_id = {$mailing}.id\n AND {$job}.is_test = 0\n WHERE {$mailing}.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer'); if (!empty($job_id)) { $query .= " AND {$job}.id = " . CRM_Utils_Type::escape($job_id, 'Integer'); } $query .= " ORDER BY {$orderBy} "; if ($offset || $rowCount) { //Added "||$rowCount" to avoid displaying all records on first page $query .= ' LIMIT ' . CRM_Utils_Type::escape($offset, 'Integer') . ', ' . CRM_Utils_Type::escape($rowCount, 'Integer'); } $dao->query($query); $results = array(); while ($dao->fetch()) { $url = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$dao->contact_id}"); $results[] = array('name' => "<a href=\"{$url}\">{$dao->display_name}</a>", 'email' => $dao->email, 'date' => CRM_Utils_Date::customFormat($dao->date)); } return $results; }
/** * Generate an event queue for a retry job (ie the contacts who bounced) * * @param int $job_id The job marked retry * @return object A DAO loaded with email_id/contact_id results * @access public */ function retryRecipients($job_id) { $eq =& new CRM_Mailing_Event_BAO_Queue(); $job = CRM_Mailing_BAO_Job::getTableName(); $queue = CRM_Mailing_Event_BAO_Queue::getTableName(); $bounce = CRM_Mailing_Event_BAO_Bounce::getTableName(); $delivered = CRM_Mailing_Event_BAO_Delivered::getTableName(); $email = CRM_Core_BAO_Email::getTableName(); $contact = CRM_Contact_BAO_Contact::getTableName(); $query = "SELECT {$queue}.email_id as email_id, \n {$queue}.contact_id as contact_id\n FROM {$queue}\n INNER JOIN {$job}\n ON {$queue}.job_id = {$job}.id\n INNER JOIN {$bounce}\n ON {$bounce}.event_queue_id = {$queue}.id\n INNER JOIN {$contact}\n ON {$queue}.contact_id = {$contact}.id\n INNER JOIN {$email}\n ON {$queue}.email_id = {$email}.id\n LEFT JOIN {$queue} as queue_d\n ON queue_d.contact_id = {$queue}.contact_id\n LEFT JOIN {$delivered}\n ON {$delivered}.event_queue_id = queue_d.id\n LEFT JOIN {$bounce} as bounce_d\n ON bounce_d.event_queue_id = queue_d.id\n WHERE \n {$job}.mailing_id = {$this->id}\n AND {$job}.id <> {$job_id}\n AND {$contact}.do_not_email = 0\n AND {$contact}.is_opt_out = 0\n AND {$email}.on_hold = 0\n AND bounce_d.id IS NOT NULL\n GROUP BY {$queue}.email_id"; $eq->query($query); return $eq; // $results = array(); // while ($eq->fetch()) { // $results[] = array( // 'email_id' => $eq->email_id, // 'contact_id' => $eq->contact_id, // ); // } // return $results; }
/** * Send the mailing. * * @param object $mailer * A Mail object to send the messages. * * @param array $testParams * * @return void */ public function deliver(&$mailer, $testParams = NULL) { $mailing = new CRM_Mailing_BAO_Mailing(); $mailing->id = $this->mailing_id; $mailing->find(TRUE); $mailing->free(); $eq = new CRM_Mailing_Event_BAO_Queue(); $eqTable = CRM_Mailing_Event_BAO_Queue::getTableName(); $emailTable = CRM_Core_BAO_Email::getTableName(); $phoneTable = CRM_Core_DAO_Phone::getTableName(); $contactTable = CRM_Contact_BAO_Contact::getTableName(); $edTable = CRM_Mailing_Event_BAO_Delivered::getTableName(); $ebTable = CRM_Mailing_Event_BAO_Bounce::getTableName(); $query = " SELECT {$eqTable}.id,\n {$emailTable}.email as email,\n {$eqTable}.contact_id,\n {$eqTable}.hash,\n NULL as phone\n FROM {$eqTable}\n INNER JOIN {$emailTable}\n ON {$eqTable}.email_id = {$emailTable}.id\n INNER JOIN {$contactTable}\n ON {$contactTable}.id = {$emailTable}.contact_id\n LEFT JOIN {$edTable}\n ON {$eqTable}.id = {$edTable}.event_queue_id\n LEFT JOIN {$ebTable}\n ON {$eqTable}.id = {$ebTable}.event_queue_id\n WHERE {$eqTable}.job_id = " . $this->id . "\n AND {$edTable}.id IS null\n AND {$ebTable}.id IS null\n AND {$contactTable}.is_opt_out = 0"; if ($mailing->sms_provider_id) { $query = "\n SELECT {$eqTable}.id,\n {$phoneTable}.phone as phone,\n {$eqTable}.contact_id,\n {$eqTable}.hash,\n NULL as email\n FROM {$eqTable}\n INNER JOIN {$phoneTable}\n ON {$eqTable}.phone_id = {$phoneTable}.id\n INNER JOIN {$contactTable}\n ON {$contactTable}.id = {$phoneTable}.contact_id\n LEFT JOIN {$edTable}\n ON {$eqTable}.id = {$edTable}.event_queue_id\n LEFT JOIN {$ebTable}\n ON {$eqTable}.id = {$ebTable}.event_queue_id\n WHERE {$eqTable}.job_id = " . $this->id . "\n AND {$edTable}.id IS null\n AND {$ebTable}.id IS null\n AND ( {$contactTable}.is_opt_out = 0\n OR {$contactTable}.do_not_sms = 0 )"; } $eq->query($query); $config = NULL; if ($config == NULL) { $config = CRM_Core_Config::singleton(); } $job_date = CRM_Utils_Date::isoToMysql($this->scheduled_date); $fields = array(); if (!empty($testParams)) { $mailing->subject = ts('[CiviMail Draft]') . ' ' . $mailing->subject; } CRM_Mailing_BAO_Mailing::tokenReplace($mailing); // get and format attachments $attachments = CRM_Core_BAO_File::getEntityFile('civicrm_mailing', $mailing->id); if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY) { CRM_Core_Smarty::registerStringResource(); } // CRM-12376 // This handles the edge case scenario where all the mails // have been delivered in prior jobs $isDelivered = TRUE; // make sure that there's no more than $config->mailerBatchLimit mails processed in a run while ($eq->fetch()) { // if ( ( $mailsProcessed % 100 ) == 0 ) { // CRM_Utils_System::xMemory( "$mailsProcessed: " ); // } if ($config->mailerBatchLimit > 0 && self::$mailsProcessed >= $config->mailerBatchLimit) { if (!empty($fields)) { $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments); } $eq->free(); return FALSE; } self::$mailsProcessed++; $fields[] = array('id' => $eq->id, 'hash' => $eq->hash, 'contact_id' => $eq->contact_id, 'email' => $eq->email, 'phone' => $eq->phone); if (count($fields) == self::MAX_CONTACTS_TO_PROCESS) { $isDelivered = $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments); if (!$isDelivered) { $eq->free(); return $isDelivered; } $fields = array(); } } $eq->free(); if (!empty($fields)) { $isDelivered = $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments); } return $isDelivered; }
/** * Send the mailing * * @param object $mailer A Mail object to send the messages * @return void * @access public */ function deliver(&$mailer) { require_once 'CRM/Mailing/BAO/Mailing.php'; $mailing =& new CRM_Mailing_BAO_Mailing(); $mailing->id = $this->mailing_id; $mailing->find(true); $eq =& new CRM_Mailing_Event_BAO_Queue(); $eqTable = CRM_Mailing_Event_BAO_Queue::getTableName(); $emailTable = CRM_Core_BAO_Email::getTableName(); $contactTable = CRM_Contact_BAO_Contact::getTableName(); $edTable = CRM_Mailing_Event_BAO_Delivered::getTableName(); $ebTable = CRM_Mailing_Event_BAO_Bounce::getTableName(); $query = " SELECT {$eqTable}.id,\n {$emailTable}.email as email,\n {$eqTable}.contact_id,\n {$eqTable}.hash\n FROM {$eqTable}\n INNER JOIN {$emailTable}\n ON {$eqTable}.email_id = {$emailTable}.id\n LEFT JOIN {$edTable}\n ON {$eqTable}.id = {$edTable}.event_queue_id\n LEFT JOIN {$ebTable}\n ON {$eqTable}.id = {$ebTable}.event_queue_id\n WHERE {$eqTable}.job_id = " . $this->id . "\n AND {$edTable}.id IS null\n AND {$ebTable}.id IS null"; $eq->query($query); while ($eq->fetch()) { /* Compose the mailing */ $recipient = null; $message = $mailing->compose($this->id, $eq->id, $eq->hash, $eq->contact_id, $eq->email, $recipient); /* Send the mailing */ $body = $message->get(); $headers = $message->headers(); /* TODO: when we separate the content generator from the delivery * engine, maybe we should dump the messages into a table */ PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array('CRM_Mailing_BAO_Mailing', 'catchSMTP')); $result = $mailer->send($recipient, $headers, $body); CRM_Core_Error::setCallback(); $params = array('event_queue_id' => $eq->id, 'job_id' => $this->id, 'hash' => $eq->hash); if (is_a($result, PEAR_Error)) { /* Register the bounce event */ require_once 'CRM/Mailing/BAO/BouncePattern.php'; require_once 'CRM/Mailing/Event/BAO/Bounce.php'; $params = array_merge($params, CRM_Mailing_BAO_BouncePattern::match($result->getMessage())); CRM_Mailing_Event_BAO_Bounce::create($params); } else { /* Register the delivery event */ CRM_Mailing_Event_BAO_Delivered::create($params); } } }
/** * Get all of the ACLs through ACL groups * * @param int $contact_id - ID of a contact to search for * @param int $group_id - ID of a group to search for * * @return array - Array of assoc. arrays of ACL rules * @access public * @static */ public static function &getACLRoles($contact_id = NULL, $group_id = NULL) { $contact_id = CRM_Utils_Type::escape($contact_id, 'Integer'); if ($group_id) { $group_id = CRM_Utils_Type::escape($group_id, 'Integer'); } $rule = new CRM_ACL_BAO_ACL(); $acl = self::getTableName(); $aclRole = 'civicrm_acl_role'; $aclRoleJoin = CRM_ACL_DAO_EntityRole::getTableName(); $contact = CRM_Contact_BAO_Contact::getTableName(); $c2g = CRM_Contact_BAO_GroupContact::getTableName(); $group = CRM_Contact_BAO_Group::getTableName(); $query = " SELECT {$acl}.*\n FROM {$acl}\n INNER JOIN civicrm_option_group og\n ON og.name = 'acl_role'\n INNER JOIN civicrm_option_value ov\n ON {$acl}.entity_table = '{$aclRole}'\n AND ov.option_group_id = og.id\n AND {$acl}.entity_id = ov.value"; if (!empty($group_id)) { $query .= " INNER JOIN {$c2g}\n ON {$acl}.entity_id = {$c2g}.group_id\n WHERE {$acl}.entity_table = '{$group}'\n AND {$acl}.is_active = 1\n AND {$c2g}.group_id = {$group_id}"; if (!empty($contact_id)) { $query .= " AND {$c2g}.contact_id = {$contact_id}\n AND {$c2g}.status = 'Added'"; } } else { if (!empty($contact_id)) { $query .= " WHERE {$acl}.entity_table = '{$contact}'\n AND {$acl}.is_active = 1\n AND {$acl}.entity_id = {$contact_id}"; } } $results = array(); $rule->query($query); while ($rule->fetch()) { $results[$rule->id] =& $rule->toArray(); } return $results; }
/** * Returns array of contacts who are members of the specified group. * * @param CRM_Contact $group A valid group object (passed by reference) * @param array $returnProperties Which properties * should be included in the returned Contact object(s). If NULL, * the default set of contact properties will be * included. group_contact properties (such as 'status', * ' in_date', etc.) are included automatically.Note:Do not inclue * Id releted properties. * @param text $status A valid status value ('Added', 'Pending', 'Removed'). * @param text $sort Associative array of * one or more "property_name"=>"sort direction" * pairs which will control order of Contact objects returned. * @param Int $offset Starting row index. * @param Int $row_count Maximum number of rows to returns. * * * @return $contactArray Array of contacts who are members of the specified group * * @access public */ function getGroupContacts(&$group, $returnProperties = null, $status = 'Added', $sort = null, $offset = null, $row_count = null) { $query = "SELECT * FROM civicrm_group WHERE id = " . CRM_Utils_Type::escape($group->id, 'Integer'); $groupDAO =& new CRM_Contact_DAO_Group(); $groupDAO->id = $group->id; if (!$groupDAO->find(true)) { return CRM_Core_Error::createError("Could not locate group with id: {$id}"); } // make sure user has got permission to view this group if (!CRM_Contact_BAO_Group::checkPermission($groupDAO->id, $groupDAO->title)) { return CRM_Core_Error::createError("You do not have permission to access group with id: {$id}"); } $query = ''; if (empty($returnProperties)) { $query = "SELECT civicrm_contact.id as contact_id,\n civicrm_email.email as email"; //$query = "SELECT *,civicrm_contact.id as contact_id, (talk to lobo before re-enabling this) //civicrm_email.email as email"; } else { $query = "SELECT civicrm_contact.id as contact_id ,"; $query .= implode(',', $returnProperties); } $fv = array('group' => array($group->id => true)); if ($status) { $fv['group_contact_status'] = array($status => true); } else { $fv['group_contact_status'] = array('Added' => true, 'Removed' => true, 'Pending' => true); } $tables = array(CRM_Contact_BAO_GroupContact::getTableName() => true, CRM_Core_BAO_Email::getTableName() => true, CRM_Contact_BAO_Contact::getTableName() => true, CRM_Contact_BAO_Group::getTableName() => true); $inner = array(); $whereTables = array(); $where = CRM_Contact_BAO_Query::getWhereClause($fv, null, $tables, $whereTables); $permission = CRM_Core_Permission::whereClause(CRM_CORE_PERMISSION_VIEW, $tables, $whereTables); $from = CRM_Contact_BAO_Query::fromClause($tables, $inner); $query .= " {$from} WHERE {$permission} AND {$where} "; if ($sort != null) { $order = array(); foreach ($sort as $key => $direction) { $order[] = " {$key} {$direction} "; } $query .= " ORDER BY " . implode(',', $order); } if ($offset != null && $row_count != null) { $query .= " LIMIT {$offset}, {$row_count}"; } // CRM_Core_Error::debug( 'q', $query ); $dao =& new CRM_Contact_DAO_Contact(); $dao->query($query); // this is quite inefficient, we need to change the return // values in docs $contactArray = array(); while ($dao->fetch()) { $contactArray[] = clone $dao; } return $contactArray; }
/** * Get rows for the event browser * * @param int $mailing_id ID of the mailing * @param int $job_id optional ID of the job * @param int $offset Offset * @param int $rowCount Number of rows * @param array $sort sort array * @return array Result set * @access public * @static */ function &getRows($mailing_id, $job_id = null, $offset = null, $rowCount = null, $sort = null) { $dao =& new CRM_Core_Dao(); $queue = CRM_Mailing_Event_BAO_Queue::getTableName(); $mailing = CRM_Mailing_BAO_Mailing::getTableName(); $job = CRM_Mailing_BAO_Job::getTableName(); $contact = CRM_Contact_BAO_Contact::getTableName(); $email = CRM_Core_BAO_Email::getTableName(); $query = "\n SELECT {$contact}.display_name as display_name,\n {$contact}.id as contact_id,\n {$email}.email as email,\n {$job}.start_date as date\n FROM {$contact}\n INNER JOIN {$queue}\n ON {$queue}.contact_id = {$contact}.id\n INNER JOIN {$email}\n ON {$queue}.email_id = {$email}.id\n INNER JOIN {$job}\n ON {$queue}.job_id = {$job}.id\n INNER JOIN {$mailing}\n ON {$job}.mailing_id = {$mailing}.id\n WHERE {$mailing}.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer'); if (!empty($job_id)) { $query .= " AND {$job}.id = " . CRM_Utils_Type::escape($job_id, 'Integer'); } $query .= " ORDER BY {$contact}.sort_name, {$job}.start_date "; if ($offset) { $query .= ' LIMIT ' . CRM_Utils_Type::escape($offset, 'Integer') . ', ' . CRM_Utils_Type::escape($rowCount, 'Integer'); } $dao->query($query); $results = array(); while ($dao->fetch()) { $url = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$dao->contact_id}"); $results[] = array('name' => "<a href=\"{$url}\">{$dao->display_name}</a>", 'email' => $dao->email, 'date' => CRM_Utils_Date::customFormat($dao->date)); } return $results; }
/** * Takes a bunch of params that are needed to match certain criteria and * retrieves the relevant objects. Typically the valid params are only * contact_id. We'll tweak this function to be more full featured over a period * of time. This is the inverse function of create. It also stores all the retrieved * values in the default array * * @param array $params (reference ) an assoc array of name/value pairs * @param array $defaults (reference ) an assoc array to hold the name / value pairs * in a hierarchical manner * @param array $ids (reference) the array that holds all the db ids * * @return object CRM_Contact_BAO_Contact object * @access public * @static */ function retrieve(&$params, &$defaults, &$ids) { $contact = CRM_Contact_BAO_Contact::getValues($params, $defaults, $ids); unset($params['id']); require_once str_replace('_', DIRECTORY_SEPARATOR, "CRM_Contact_BAO_" . $contact->contact_type) . ".php"; eval('$contact->contact_type_object =& CRM_Contact_BAO_' . $contact->contact_type . '::getValues( $params, $defaults, $ids );'); $locParams = $params + array('entity_id' => $params['contact_id'], 'entity_table' => CRM_Contact_BAO_Contact::getTableName()); $contact->location =& CRM_Core_BAO_Location::getValues($locParams, $defaults, $ids, 3); $contact->notes =& CRM_Core_BAO_Note::getValues($params, $defaults, $ids); $contact->relationship =& CRM_Contact_BAO_Relationship::getValues($params, $defaults, $ids); $contact->groupContact =& CRM_Contact_BAO_GroupContact::getValues($params, $defaults, $ids); $activityParam = array('entity_id' => $params['contact_id']); $contact->activity =& CRM_Core_BAO_History::getValues($activityParam, $defaults, 'Activity'); $activityParam = array('contact_id' => $params['contact_id']); $defaults['openActivity'] = array('data' => CRM_Contact_BAO_Contact::getOpenActivities($activityParam, 0, 3), 'totalCount' => CRM_Contact_BAO_Contact::getNumOpenActivity($params['contact_id'])); return $contact; }