Commit | Line | Data |
---|---|---|
546a1ecc TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
3435af9a | 4 | | CiviCRM version 4.7 | |
546a1ecc | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
546a1ecc TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
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. | | |
13 | | | | |
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. | | |
18 | | | | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | use Civi\ActionSchedule\RecipientBuilder; | |
29 | ||
30 | /** | |
31 | * Class CRM_Member_ActionMapping | |
32 | * | |
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. | |
36 | */ | |
37 | class CRM_Member_ActionMapping extends \Civi\ActionSchedule\Mapping { | |
38 | ||
46f5566c TO |
39 | /** |
40 | * The value for civicrm_action_schedule.mapping_id which identifies the | |
41 | * "Membership Type" mapping. | |
42 | * | |
43 | * Note: This value is chosen to match legacy DB IDs. | |
44 | */ | |
45 | const MEMBERSHIP_TYPE_MAPPING_ID = 4; | |
46 | ||
47 | /** | |
48 | * Register CiviMember-related action mappings. | |
49 | * | |
50 | * @param \Civi\ActionSchedule\Event\MappingRegisterEvent $registrations | |
51 | */ | |
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', | |
63 | ))); | |
64 | } | |
65 | ||
546a1ecc TO |
66 | /** |
67 | * Generate a query to locate recipients who match the given | |
68 | * schedule. | |
69 | * | |
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. | |
ad37ac8e | 74 | * @param array $defaultParams |
75 | * | |
546a1ecc TO |
76 | * @return \CRM_Utils_SQL_Select |
77 | * @see RecipientBuilder | |
78 | */ | |
efc40454 | 79 | public function createQuery($schedule, $phase, $defaultParams) { |
546a1ecc TO |
80 | $selectedValues = (array) \CRM_Utils_Array::explodePadded($schedule->entity_value); |
81 | $selectedStatuses = (array) \CRM_Utils_Array::explodePadded($schedule->entity_status); | |
82 | ||
9d180e38 | 83 | $query = \CRM_Utils_SQL_Select::from("{$this->entity} e")->param($defaultParams); |
546a1ecc TO |
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); | |
89 | ||
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"); | |
94 | } | |
95 | elseif (in_array(1, $selectedStatuses)) { | |
96 | $query->where("e.contribution_recur_id IS NULL"); | |
97 | } | |
98 | ||
99 | if (!empty($selectedValues)) { | |
100 | $query->where("e.membership_type_id IN (@memberTypeValues)") | |
101 | ->param('memberTypeValues', $selectedValues); | |
102 | } | |
103 | else { | |
104 | $query->where("e.membership_type_id IS NULL"); | |
105 | } | |
106 | ||
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')); | |
111 | ||
112 | // Why is this only for civicrm_membership? | |
113 | if ($schedule->start_action_date && $schedule->is_repeat == FALSE) { | |
114 | $query['casUseReferenceDate'] = TRUE; | |
115 | } | |
116 | ||
117 | return $query; | |
118 | } | |
119 | ||
120 | /** | |
121 | * @return array | |
122 | */ | |
123 | protected function prepareMembershipPermissionsFilter() { | |
124 | $query = ' | |
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) | |
133 | ||
134 | '; | |
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; | |
140 | } | |
141 | elseif ($dao->slave_contact == $dao->contact_id_b && $dao->is_permission_b_a == 0) { | |
142 | $excludeIds[] = $dao->slave_contact; | |
143 | } | |
144 | } | |
145 | ||
146 | if (!empty($excludeIds)) { | |
147 | return \CRM_Utils_SQL_Select::fragment() | |
148 | ->where("!casContactIdField NOT IN (#excludeMemberIds)") | |
149 | ->param(array( | |
150 | '#excludeMemberIds' => $excludeIds, | |
151 | )); | |
152 | } | |
153 | return NULL; | |
154 | } | |
155 | ||
156 | } |