Merge pull request #7019 from jitendrapurohit/CRM-17395
[civicrm-core.git] / CRM / Contact / 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_Contact_ActionMapping
32 *
33 * This defines the scheduled-reminder functionality for contact
34 * entities. It is useful for, e.g., sending a reminder based on
35 * birth date, modification date, or other custom dates on
36 * the contact record.
37 */
38 class CRM_Contact_ActionMapping extends \Civi\ActionSchedule\Mapping {
39
40 /**
41 * The value for civicrm_action_schedule.mapping_id which identifies the
42 * "Contact" mapping.
43 *
44 * Note: This value is chosen to match legacy DB IDs.
45 */
46 const CONTACT_MAPPING_ID = 6;
47
48 /**
49 * Register Contact-related action mappings.
50 *
51 * @param \Civi\ActionSchedule\Event\MappingRegisterEvent $registrations
52 */
53 public static function onRegisterActionMappings(\Civi\ActionSchedule\Event\MappingRegisterEvent $registrations) {
54 $registrations->register(CRM_Contact_ActionMapping::create(array(
55 'id' => CRM_Contact_ActionMapping::CONTACT_MAPPING_ID,
56 'entity' => 'civicrm_contact',
57 'entity_label' => ts('Contact'),
58 'entity_value' => 'civicrm_contact',
59 'entity_value_label' => ts('Date Field'),
60 'entity_status' => 'contact_date_reminder_options',
61 'entity_status_label' => ts('Annual Options'),
62 'entity_date_start' => 'date_field',
63 )));
64 }
65
66 private $contactDateFields = array(
67 'birth_date',
68 'created_date',
69 'modified_date',
70 );
71
72 /**
73 * Determine whether a schedule based on this mapping is sufficiently
74 * complete.
75 *
76 * @param \CRM_Core_DAO_ActionSchedule $schedule
77 * @return array
78 * Array (string $code => string $message).
79 * List of error messages.
80 */
81 public function validateSchedule($schedule) {
82 $errors = array();
83 if (CRM_Utils_System::isNull($schedule->entity_value) || $schedule->entity_value === '0') {
84 $errors['entity'] = ts('Please select a specific date field.');
85 }
86 elseif (count(CRM_Utils_Array::explodePadded($schedule->entity_value)) > 1) {
87 $errors['entity'] = ts('You may only select one contact field per reminder');
88 }
89 elseif (CRM_Utils_System::isNull($schedule->entity_status) || $schedule->entity_status === '0') {
90 $errors['entity'] = ts('Please select whether the reminder is sent each year.');
91 }
92
93 return $errors;
94 }
95
96 /**
97 * Generate a query to locate recipients who match the given
98 * schedule.
99 *
100 * @param \CRM_Core_DAO_ActionSchedule $schedule
101 * The schedule as configured by the administrator.
102 * @param string $phase
103 * See, e.g., RecipientBuilder::PHASE_RELATION_FIRST.
104 * @return \CRM_Utils_SQL_Select
105 * @see RecipientBuilder
106 * @throws \CRM_Core_Exception
107 */
108 public function createQuery($schedule, $phase, $defaultParams) {
109 $selectedValues = (array) \CRM_Utils_Array::explodePadded($schedule->entity_value);
110 $selectedStatuses = (array) \CRM_Utils_Array::explodePadded($schedule->entity_status);
111
112 // FIXME: This assumes that $values only has one field, but UI shows multiselect.
113 // Properly supporting multiselect would require total rewrite of this function.
114 if (count($selectedValues) != 1 || !isset($selectedValues[0])) {
115 throw new \CRM_Core_Exception("Error: Scheduled reminders may only have one contact field.");
116 }
117 elseif (in_array($selectedValues[0], $this->contactDateFields)) {
118 $dateDBField = $selectedValues[0];
119 $query = \CRM_Utils_SQL_Select::from("{$this->entity} e")->param($defaultParams);
120 $query->param(array(
121 'casAddlCheckFrom' => 'civicrm_contact e',
122 'casContactIdField' => 'e.id',
123 'casEntityIdField' => 'e.id',
124 'casContactTableAlias' => 'e',
125 ));
126 $query->where('e.is_deleted = 0 AND e.is_deceased = 0');
127 }
128 else {
129 //custom field
130 $customFieldParams = array('id' => substr($selectedValues[0], 7));
131 $customGroup = $customField = array();
132 \CRM_Core_BAO_CustomField::retrieve($customFieldParams, $customField);
133 $dateDBField = $customField['column_name'];
134 $customGroupParams = array('id' => $customField['custom_group_id'], $customGroup);
135 \CRM_Core_BAO_CustomGroup::retrieve($customGroupParams, $customGroup);
136 $query = \CRM_Utils_SQL_Select::from("{$customGroup['table_name']} e")->param($defaultParams);
137 $query->param(array(
138 'casAddlCheckFrom' => "{$customGroup['table_name']} e",
139 'casContactIdField' => 'e.entity_id',
140 'casEntityIdField' => 'e.id',
141 'casContactTableAlias' => NULL,
142 ));
143 $query->where('1'); // possible to have no "where" in this case
144 }
145
146 $query['casDateField'] = 'e.' . $dateDBField;
147
148 if (in_array(2, $selectedStatuses)) {
149 $query['casAnniversaryMode'] = 1;
150 $query['casDateField'] = 'DATE_ADD(' . $query['casDateField'] . ', INTERVAL ROUND(DATEDIFF(DATE(' . $query['casNow'] . '), ' . $query['casDateField'] . ') / 365) YEAR)';
151 }
152
153 return $query;
154 }
155
156 }