}
}
+ /**
+ * Build a join and where part for a query
+ *
+ * @param $contact_id
+ * @return array - the first key is join part of the query and the second key is the where part of the query
+ */
+ public function buildAcl($contact_id) {
+ $tables = array();
+ $whereTables = array();
+ $whereClause = CRM_ACL_BAO_ACL::whereClause(CRM_Core_Permission::VIEW, $tables, $whereTables, $contact_id);
+ if (strlen($whereClause)) {
+ $whereClause = " AND (".$whereClause.")";
+ }
+
+ $join = "";
+ foreach ($whereTables as $name => $value) {
+ if (!$value) {
+ continue;
+ }
+ if ($value != 1) {
+ // if there is already a join statement in value, use value itself
+ if (strpos($value, 'JOIN')) {
+ $join .= " $value ";
+ }
+ continue;
+ }
+ }
+
+ return array (
+ $join,
+ $whereClause
+ );
+ }
+
/**
* Generate an event queue for a test job.
*
* @return void
*/
public function getTestRecipients($testParams) {
+ $session = CRM_Core_Session::singleton();
+ $sender_id = $session->get('userID');
+ list($acl_join, $acl_where) = $this->buildAcl($sender_id);
+
if (array_key_exists($testParams['test_group'], CRM_Core_PseudoConstant::group())) {
$contacts = civicrm_api('contact', 'get', array(
'version' => 3,
civicrm_email.is_primary as is_primary,
civicrm_email.is_bulkmail as is_bulkmail
FROM civicrm_email
-INNER JOIN civicrm_contact ON civicrm_email.contact_id = civicrm_contact.id
+INNER JOIN civicrm_contact contact_a ON civicrm_email.contact_id = contact_a.id
+{$acl_join}
WHERE (civicrm_email.is_bulkmail = 1 OR civicrm_email.is_primary = 1)
-AND civicrm_contact.id = {$groupContact}
-AND civicrm_contact.do_not_email = 0
-AND civicrm_contact.is_deceased <> 1
+AND contact_a.id = {$groupContact}
+AND contact_a.do_not_email = 0
+AND contact_a.is_deceased <> 1
AND civicrm_email.on_hold = 0
-AND civicrm_contact.is_opt_out = 0
+AND contact_a.is_opt_out = 0
+{$acl_where}
GROUP BY civicrm_email.id
ORDER BY civicrm_email.is_bulkmail DESC
";
}
}
+ /**
+ * Build a join and where part for a query
+ *
+ * @param $contact_id
+ * @return array - the first key is join part of the query and the second key is the where part of the query
+ */
+ public function buildAcl($contact_id) {
+ $tables = array();
+ $whereTables = array();
+ $whereClause = CRM_ACL_BAO_ACL::whereClause(CRM_Core_Permission::VIEW, $tables, $whereTables, $contact_id);
+ if (strlen($whereClause)) {
+ $whereClause = " AND (".$whereClause.")";
+ }
+
+ $join = "";
+ foreach ($whereTables as $name => $value) {
+ if (!$value) {
+ continue;
+ }
+ if ($value != 1) {
+ // if there is already a join statement in value, use value itself
+ if (strpos($value, 'JOIN')) {
+ $join .= " $value ";
+ }
+ continue;
+ }
+ }
+
+ return array (
+ $join,
+ $whereClause
+ );
+ }
+
/**
* Send the mailing.
*
$edTable = CRM_Mailing_Event_BAO_Delivered::getTableName();
$ebTable = CRM_Mailing_Event_BAO_Bounce::getTableName();
+ list($acl_join, $acl_where) = $this->buildAcl($mailing->created_id);
+
$query = " SELECT $eqTable.id,
$emailTable.email as email,
$eqTable.contact_id,
FROM $eqTable
INNER JOIN $emailTable
ON $eqTable.email_id = $emailTable.id
- INNER JOIN $contactTable
- ON $contactTable.id = $emailTable.contact_id
+ INNER JOIN $contactTable contact_a
+ ON contact_a.id = $emailTable.contact_id
LEFT JOIN $edTable
ON $eqTable.id = $edTable.event_queue_id
LEFT JOIN $ebTable
ON $eqTable.id = $ebTable.event_queue_id
+ $acl_join
WHERE $eqTable.job_id = " . $this->id . "
AND $edTable.id IS null
AND $ebTable.id IS null
- AND $contactTable.is_opt_out = 0";
+ AND contact_a.is_opt_out = 0
+ $acl_where";
if ($mailing->sms_provider_id) {
$query = "
FROM $eqTable
INNER JOIN $phoneTable
ON $eqTable.phone_id = $phoneTable.id
- INNER JOIN $contactTable
- ON $contactTable.id = $phoneTable.contact_id
+ INNER JOIN $contactTable contact_a
+ ON contact_a.id = $phoneTable.contact_id
LEFT JOIN $edTable
ON $eqTable.id = $edTable.event_queue_id
LEFT JOIN $ebTable
ON $eqTable.id = $ebTable.event_queue_id
+ $acl_join
WHERE $eqTable.job_id = " . $this->id . "
AND $edTable.id IS null
AND $ebTable.id IS null
- AND ( $contactTable.is_opt_out = 0
- OR $contactTable.do_not_sms = 0 )";
+ AND ( contact_a.is_opt_out = 0
+ OR contact_a.do_not_sms = 0 )
+ $acl_where}";
}
$eq->query($query);