Commit e41c6789 authored by jaapjansma's avatar jaapjansma
Browse files

enrich messages based on the e-mail address and not contact id

parent 823eecc5
......@@ -39,6 +39,13 @@ class UserProvider extends BasicUserProvider implements Configurable {
return $messages;
}
protected function getEmailStatementForMessage(UserMessage $message) {
if ($message->message['email']) {
return "SELECT contact_id FROM civicrm_email WHERE `email` = '" . \CRM_Utils_Type::escape($message->message['email'], 'String')."'";
}
return null;
}
/**
* @param $contact_id
*
......@@ -72,36 +79,37 @@ class UserProvider extends BasicUserProvider implements Configurable {
$status_ids = implode(", ", $configuration['membership_status_ids']);
$membership_type_ids = implode(", ", $configuration['membership_type_ids']);
$family_membership_type_ids = implode(", ", $configuration['family_membership_type_ids']);
$contact_ids = implode(", ", array_keys($messages));
$sql = "
SELECT
COUNT(membership_id) as `count`,
`contact_id`
FROM (
SELECT
`civicrm_contact`.`id` AS `contact_id`,
`civicrm_membership`.`id` AS `membership_id`
FROM `civicrm_contact`
LEFT JOIN `civicrm_membership` ON `civicrm_membership`.`contact_id` = `civicrm_contact`.`id`
AND `civicrm_membership`.`status_id` IN({$status_ids})
AND `civicrm_membership`.`membership_type_id` IN({$membership_type_ids})
WHERE `civicrm_contact`.`id` IN({$contact_ids})
UNION SELECT
`civicrm_contact`.`id` AS `contact_id`,
`civicrm_membership`.`id` AS `membership_id`
FROM `civicrm_contact`
LEFT JOIN `civicrm_membership` ON `civicrm_membership`.`contact_id` = `civicrm_contact`.`id`
AND `civicrm_membership`.`status_id` IN({$status_ids})
AND `civicrm_membership`.`membership_type_id` IN({$family_membership_type_ids})
WHERE `civicrm_contact`.`id` IN({$contact_ids})
{$familyContactTypStatement}
) `tbl`
GROUP BY
`contact_id`
";
$dao = \CRM_Core_DAO::executeQuery($sql);
while($dao->fetch()) {
$messages[$dao->contact_id]->setMembership($dao->count);
foreach($messages as $key => $message) {
$contactIdStatement = $this->getEmailStatementForMessage($message);
if ($contactIdStatement) {
$sql = "
SELECT
COUNT(membership_id) as `count`
FROM (
SELECT
`civicrm_contact`.`id` AS `contact_id`,
`civicrm_membership`.`id` AS `membership_id`
FROM `civicrm_contact`
LEFT JOIN `civicrm_membership` ON `civicrm_membership`.`contact_id` = `civicrm_contact`.`id`
AND `civicrm_membership`.`status_id` IN({$status_ids})
AND `civicrm_membership`.`membership_type_id` IN({$membership_type_ids})
WHERE `civicrm_contact`.`id` IN({$contactIdStatement})
UNION SELECT
`civicrm_contact`.`id` AS `contact_id`,
`civicrm_membership`.`id` AS `membership_id`
FROM `civicrm_contact`
LEFT JOIN `civicrm_membership` ON `civicrm_membership`.`contact_id` = `civicrm_contact`.`id`
AND `civicrm_membership`.`status_id` IN({$status_ids})
AND `civicrm_membership`.`membership_type_id` IN({$family_membership_type_ids})
WHERE `civicrm_contact`.`id` IN({$contactIdStatement})
{$familyContactTypStatement}
) `tbl`
";
$dao = \CRM_Core_DAO::executeQuery($sql);
if ($dao->fetch()) {
$messages[$key]->setMembership($dao->count);
}
}
}
return $messages;
}
......@@ -118,22 +126,28 @@ class UserProvider extends BasicUserProvider implements Configurable {
}
$relationship_type_id = implode(", ", $configuration['steering_committee_relationship_type_ids']);
$contact_ids = implode(", ", array_keys($messages));
$sql = "
SELECT COUNT(*) as count, contact_id_a as contact_id
foreach($messages as $key => $message) {
$contactIdStatement = $this->getEmailStatementForMessage($message);
if ($contactIdStatement) {
$sql = "
SELECT COUNT(*) as count
FROM civicrm_relationship r
INNER JOIN civicrm_value_relationship_minside minside ON minside.entity_id = r.id
WHERE is_active = '1'
AND (start_date IS NULL OR DATE(start_date) <= NOW())
AND (end_date IS NULL OR DATE(end_date) >= NOW())
AND relationship_type_id IN ({$relationship_type_id})
AND contact_id_a IN ({$contact_ids})
AND contact_id_a IN ({$contactIdStatement})
AND minside.access_to_memberlist = '1'
GROUP BY contact_id_a";
$dao = \CRM_Core_DAO::executeQuery($sql);
while($dao->fetch()) {
$messages[$dao->contact_id]->setSteeringCommitteeRole($dao->count);
";
$dao = \CRM_Core_DAO::executeQuery($sql);
if ($dao->fetch()) {
$messages[$key]->setSteeringCommitteeRole($dao->count);
}
}
}
return $messages;
}
......@@ -149,19 +163,24 @@ class UserProvider extends BasicUserProvider implements Configurable {
}
$relationship_type_id = implode(", ", $configuration['volunteer_role_relationship_type_ids']);
$contact_ids = implode(", ", array_keys($messages));
$sql = "
SELECT COUNT(*) as count, contact_id_a as contact_id
foreach($messages as $key => $message) {
$contactIdStatement = $this->getEmailStatementForMessage($message);
if ($contactIdStatement) {
$sql = "
SELECT COUNT(*) as count
FROM civicrm_relationship r
WHERE is_active = '1'
AND (start_date IS NULL OR DATE(start_date) <= NOW())
AND (end_date IS NULL OR DATE(end_date) >= NOW())
AND relationship_type_id IN ({$relationship_type_id})
AND contact_id_a IN ({$contact_ids})
GROUP BY contact_id_a";
$dao = \CRM_Core_DAO::executeQuery($sql);
while($dao->fetch()) {
$messages[$dao->contact_id]->setVolunteerRole($dao->count);
AND contact_id_a IN ({$contactIdStatement})
";
$dao = \CRM_Core_DAO::executeQuery($sql);
if ($dao->fetch()) {
$messages[$key]->setVolunteerRole($dao->count);
}
}
}
return $messages;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment