3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
28 use Civi\ActionSchedule\RecipientBuilder
;
31 * Class CRM_Member_ActionMapping
33 * This defines the scheduled-reminder functionality for CiviMember
34 * memberships. It allows one to target reminders based on join date
35 * or end date, with additional filtering based on membership-type.
37 class CRM_Member_ActionMapping
extends \Civi\ActionSchedule\Mapping
{
40 * The value for civicrm_action_schedule.mapping_id which identifies the
41 * "Membership Type" mapping.
43 * Note: This value is chosen to match legacy DB IDs.
45 const MEMBERSHIP_TYPE_MAPPING_ID
= 4;
48 * Register CiviMember-related action mappings.
50 * @param \Civi\ActionSchedule\Event\MappingRegisterEvent $registrations
52 public static function onRegisterActionMappings(\Civi\ActionSchedule\Event\MappingRegisterEvent
$registrations) {
53 $registrations->register(CRM_Member_ActionMapping
::create(array(
54 'id' => CRM_Member_ActionMapping
::MEMBERSHIP_TYPE_MAPPING_ID
,
55 'entity' => 'civicrm_membership',
56 'entity_label' => ts('Membership'),
57 'entity_value' => 'civicrm_membership_type',
58 'entity_value_label' => ts('Membership Type'),
59 'entity_status' => 'auto_renew_options',
60 'entity_status_label' => ts('Auto Renew Options'),
61 'entity_date_start' => 'membership_join_date',
62 'entity_date_end' => 'membership_end_date',
67 * Generate a query to locate recipients who match the given
70 * @param \CRM_Core_DAO_ActionSchedule $schedule
71 * The schedule as configured by the administrator.
72 * @param string $phase
73 * See, e.g., RecipientBuilder::PHASE_RELATION_FIRST.
74 * @param array $defaultParams
76 * @return \CRM_Utils_SQL_Select
77 * @see RecipientBuilder
79 public function createQuery($schedule, $phase, $defaultParams) {
80 $selectedValues = (array) \CRM_Utils_Array
::explodePadded($schedule->entity_value
);
81 $selectedStatuses = (array) \CRM_Utils_Array
::explodePadded($schedule->entity_status
);
83 $query = \CRM_Utils_SQL_Select
::from("{$this->entity} e")->param($defaultParams);;
84 $query['casAddlCheckFrom'] = 'civicrm_membership e';
85 $query['casContactIdField'] = 'e.contact_id';
86 $query['casEntityIdField'] = 'e.id';
87 $query['casContactTableAlias'] = NULL;
88 $query['casDateField'] = str_replace('membership_', 'e.', $schedule->start_action_date
);
90 // FIXME: Numbers should be constants.
91 if (in_array(2, $selectedStatuses)) {
92 //auto-renew memberships
93 $query->where("e.contribution_recur_id IS NOT NULL");
95 elseif (in_array(1, $selectedStatuses)) {
96 $query->where("e.contribution_recur_id IS NULL");
99 if (!empty($selectedValues)) {
100 $query->where("e.membership_type_id IN (@memberTypeValues)")
101 ->param('memberTypeValues', $selectedValues);
104 $query->where("e.membership_type_id IS NULL");
107 $query->where("( e.is_override IS NULL OR e.is_override = 0 )");
108 $query->merge($this->prepareMembershipPermissionsFilter());
109 $query->where("e.status_id IN (#memberStatus)")
110 ->param('memberStatus', \CRM_Member_PseudoConstant
::membershipStatus(NULL, "(is_current_member = 1 OR name = 'Expired')", 'id'));
112 // Why is this only for civicrm_membership?
113 if ($schedule->start_action_date
&& $schedule->is_repeat
== FALSE) {
114 $query['casUseReferenceDate'] = TRUE;
123 protected function prepareMembershipPermissionsFilter() {
125 SELECT cm.id AS owner_id, cm.contact_id AS owner_contact, m.id AS slave_id, m.contact_id AS slave_contact, cmt.relationship_type_id AS relation_type, rel.contact_id_a, rel.contact_id_b, rel.is_permission_a_b, rel.is_permission_b_a
126 FROM civicrm_membership m
127 LEFT JOIN civicrm_membership cm ON cm.id = m.owner_membership_id
128 LEFT JOIN civicrm_membership_type cmt ON cmt.id = m.membership_type_id
129 LEFT JOIN civicrm_relationship rel ON ( ( rel.contact_id_a = m.contact_id AND rel.contact_id_b = cm.contact_id AND rel.relationship_type_id = cmt.relationship_type_id )
130 OR ( rel.contact_id_a = cm.contact_id AND rel.contact_id_b = m.contact_id AND rel.relationship_type_id = cmt.relationship_type_id ) )
131 WHERE m.owner_membership_id IS NOT NULL AND
132 ( rel.is_permission_a_b = 0 OR rel.is_permission_b_a = 0)
135 $excludeIds = array();
136 $dao = \CRM_Core_DAO
::executeQuery($query, array());
137 while ($dao->fetch()) {
138 if ($dao->slave_contact
== $dao->contact_id_a
&& $dao->is_permission_a_b
== 0) {
139 $excludeIds[] = $dao->slave_contact
;
141 elseif ($dao->slave_contact
== $dao->contact_id_b
&& $dao->is_permission_b_a
== 0) {
142 $excludeIds[] = $dao->slave_contact
;
146 if (!empty($excludeIds)) {
147 return \CRM_Utils_SQL_Select
::fragment()
148 ->where("!casContactIdField NOT IN (#excludeMemberIds)")
150 '#excludeMemberIds' => $excludeIds,