Merge pull request #7019 from jitendrapurohit/CRM-17395
[civicrm-core.git] / CRM / Member / ActionMapping.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
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
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.
74 * @return \CRM_Utils_SQL_Select
75 * @see RecipientBuilder
76 */
77 public function createQuery($schedule, $phase, $defaultParams) {
78 $selectedValues = (array) \CRM_Utils_Array::explodePadded($schedule->entity_value);
79 $selectedStatuses = (array) \CRM_Utils_Array::explodePadded($schedule->entity_status);
80
81 $query = \CRM_Utils_SQL_Select::from("{$this->entity} e")->param($defaultParams);;
82 $query['casAddlCheckFrom'] = 'civicrm_membership e';
83 $query['casContactIdField'] = 'e.contact_id';
84 $query['casEntityIdField'] = 'e.id';
85 $query['casContactTableAlias'] = NULL;
86 $query['casDateField'] = str_replace('membership_', 'e.', $schedule->start_action_date);
87
88 // FIXME: Numbers should be constants.
89 if (in_array(2, $selectedStatuses)) {
90 //auto-renew memberships
91 $query->where("e.contribution_recur_id IS NOT NULL");
92 }
93 elseif (in_array(1, $selectedStatuses)) {
94 $query->where("e.contribution_recur_id IS NULL");
95 }
96
97 if (!empty($selectedValues)) {
98 $query->where("e.membership_type_id IN (@memberTypeValues)")
99 ->param('memberTypeValues', $selectedValues);
100 }
101 else {
102 $query->where("e.membership_type_id IS NULL");
103 }
104
105 $query->where("( e.is_override IS NULL OR e.is_override = 0 )");
106 $query->merge($this->prepareMembershipPermissionsFilter());
107 $query->where("e.status_id IN (#memberStatus)")
108 ->param('memberStatus', \CRM_Member_PseudoConstant::membershipStatus(NULL, "(is_current_member = 1 OR name = 'Expired')", 'id'));
109
110 // Why is this only for civicrm_membership?
111 if ($schedule->start_action_date && $schedule->is_repeat == FALSE) {
112 $query['casUseReferenceDate'] = TRUE;
113 }
114
115 return $query;
116 }
117
118 /**
119 * @return array
120 */
121 protected function prepareMembershipPermissionsFilter() {
122 $query = '
123 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
124 FROM civicrm_membership m
125 LEFT JOIN civicrm_membership cm ON cm.id = m.owner_membership_id
126 LEFT JOIN civicrm_membership_type cmt ON cmt.id = m.membership_type_id
127 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 )
128 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 ) )
129 WHERE m.owner_membership_id IS NOT NULL AND
130 ( rel.is_permission_a_b = 0 OR rel.is_permission_b_a = 0)
131
132 ';
133 $excludeIds = array();
134 $dao = \CRM_Core_DAO::executeQuery($query, array());
135 while ($dao->fetch()) {
136 if ($dao->slave_contact == $dao->contact_id_a && $dao->is_permission_a_b == 0) {
137 $excludeIds[] = $dao->slave_contact;
138 }
139 elseif ($dao->slave_contact == $dao->contact_id_b && $dao->is_permission_b_a == 0) {
140 $excludeIds[] = $dao->slave_contact;
141 }
142 }
143
144 if (!empty($excludeIds)) {
145 return \CRM_Utils_SQL_Select::fragment()
146 ->where("!casContactIdField NOT IN (#excludeMemberIds)")
147 ->param(array(
148 '#excludeMemberIds' => $excludeIds,
149 ));
150 }
151 return NULL;
152 }
153
154 }