3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 namespace Civi\ActionSchedule
;
32 * @package Civi\ActionSchedule
34 * This is the initial implementation of MappingInterface; it was
35 * constructed by cutting out swaths from CRM_Core_BAO_ActionSchedule.
36 * New implementers should consider implementing MappingInterface on
39 * Background: The original designers of ActionMappings intended that
40 * one could create and configure new mappings through the database.
41 * To, e.g., define the filtering options for CiviEvent, you
42 * would insert a record in "civicrm_action_mapping" with fields like
43 * "entity" (a table name, eg "civicrm_event"), "entity_value" (an
44 * option-group name, eg "event_types").
46 * Unfortunately, the metadata in "civicrm_action_mapping" proved
47 * inadequate and was not updated to cope. Instead, a number
48 * of work-arounds for specific entities were hard-coded into
49 * the core action-scheduling code. Ultimately, to add a new
50 * mapping type, one needed to run around and patch a dozen
53 * The new MappingInterface makes no pretense of database-driven
54 * configuration. The dozen places have been consolidated and
55 * replaced with functions in MappingInterface.
57 * This "Mapping" implementation is a refactoring of the old
58 * hard-coded bits. Internally, it uses the concepts from
59 * "civicrm_action_mapping". The resulting code is more
60 * convoluted than a clean implementation of MappingInterface, but
61 * it strictly matches the old behavior (based on logging/comparing
62 * the queries produced through ActionScheduleTest).
64 abstract class Mapping
implements MappingInterface
{
66 private static $fields = array(
73 'entity_status_label',
81 * @param array $params
85 public static function create($params) {
86 return new static($params);
92 * @param array $params
94 public function __construct($params) {
95 foreach (self
::$fields as $field) {
96 if (isset($params[$field])) {
97 $this->{$field} = $params[$field];
105 * The basic entity to query (table name).
108 * Ex: 'civicrm_activity', 'civicrm_event'.
113 * The basic entity to query (label).
116 * Ex: 'Activity', 'Event'
118 private $entity_label;
121 * Level 1 filter -- the field/option-list to filter on.
124 * Ex: 'activity_type', 'civicrm_event', 'event_template'.
126 private $entity_value;
129 * Level 1 filter -- The field label.
132 * Ex: 'Activity Type', 'Event Name', 'Event Template'.
134 private $entity_value_label;
137 * Level 2 filter -- the field/option-list to filter on.
139 * Ex: 'activity_status, 'civicrm_participant_status_type', 'auto_renew_options'.
141 private $entity_status;
144 * Level 2 filter -- the field label.
146 * Ex: 'Activity Status', 'Participant Status', 'Auto Rewnewal Options'.
148 private $entity_status_label;
151 * Date filter -- the field name.
153 * Ex: 'event_start_date'
155 private $entity_date_start;
158 * Date filter -- the field name.
160 * Ex: 'event_end_date'.
162 private $entity_date_end;
167 public function getId() {
174 public function getEntity() {
175 return $this->entity
;
179 * Get a printable label for this mapping type.
183 public function getLabel() {
184 return $this->entity_label
;
188 * Get a printable label to use a header on the 'value' filter.
192 public function getValueHeader() {
193 return $this->entity_value_label
;
197 * Get a printable label to use a header on the 'status' filter.
201 public function getStatusHeader() {
202 return $this->entity_status_label
;
206 * Get a list of value options.
209 * Array(string $value => string $label).
210 * Ex: array(123 => 'Phone Call', 456 => 'Meeting').
212 public function getValueLabels() {
213 return self
::getValueLabelMap($this->entity_value
);
217 * Get a list of status options.
219 * @param string|int $value
220 * The list of status options may be contingent upon the selected filter value.
221 * This is the selected filter value.
223 * Array(string $value => string $label).
224 * Ex: Array(123 => 'Completed', 456 => 'Scheduled').
226 public function getStatusLabels($value) {
227 if ($this->entity_status
=== 'auto_renew_options') {
228 if ($value && \CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType', $value, 'auto_renew')) {
229 return \CRM_Core_OptionGroup
::values('auto_renew_options');
235 return self
::getValueLabelMap($this->entity_status
);
239 * Get a list of available date fields.
242 * Array(string $fieldName => string $fieldLabel).
244 public function getDateFields() {
245 $dateFieldLabels = array();
246 if (!empty($this->entity_date_start
)) {
247 $dateFieldLabels[$this->entity_date_start
] = ucwords(str_replace('_', ' ', $this->entity_date_start
));
249 if (!empty($this->entity_date_end
)) {
250 $dateFieldLabels[$this->entity_date_end
] = ucwords(str_replace('_', ' ', $this->entity_date_end
));
252 return $dateFieldLabels;
256 * Get a list of recipient types.
258 * Note: A single schedule may filter on *zero* or *one* recipient types.
259 * When an admin chooses a value, it's stored in $schedule->recipient.
262 * array(string $value => string $label).
263 * Ex: array('assignee' => 'Activity Assignee').
265 public function getRecipientTypes() {
270 * Get a list of recipients which match the given type.
272 * Note: A single schedule may filter on *multiple* recipients.
273 * When an admin chooses value(s), it's stored in $schedule->recipient_listing.
275 * @param string $recipientType
276 * Ex: 'participant_role'.
278 * Array(mixed $name => string $label).
279 * Ex: array(1 => 'Attendee', 2 => 'Volunteer').
280 * @see getRecipientTypes
282 public function getRecipientListing($recipientType) {
286 protected static function getValueLabelMap($name) {
287 static $valueLabelMap = NULL;
288 if ($valueLabelMap === NULL) {
289 $valueLabelMap['activity_type'] = \CRM_Core_PseudoConstant
::activityType(TRUE, TRUE);
290 asort($valueLabelMap['activity_type']);
292 $valueLabelMap['activity_status'] = \CRM_Core_PseudoConstant
::activityStatus();
293 $valueLabelMap['event_type'] = \CRM_Event_PseudoConstant
::eventType();
294 $valueLabelMap['civicrm_event'] = \CRM_Event_PseudoConstant
::event(NULL, FALSE, "( is_template IS NULL OR is_template != 1 )");
295 $valueLabelMap['civicrm_participant_status_type'] = \CRM_Event_PseudoConstant
::participantStatus(NULL, NULL, 'label');
296 $valueLabelMap['event_template'] = \CRM_Event_PseudoConstant
::eventTemplates();
297 $valueLabelMap['auto_renew_options'] = \CRM_Core_OptionGroup
::values('auto_renew_options');
298 $valueLabelMap['contact_date_reminder_options'] = \CRM_Core_OptionGroup
::values('contact_date_reminder_options');
299 $valueLabelMap['civicrm_membership_type'] = \CRM_Member_PseudoConstant
::membershipType();
301 $allCustomFields = \CRM_Core_BAO_CustomField
::getFields('');
303 'birth_date' => ts('Birth Date'),
304 'created_date' => ts('Created Date'),
305 'modified_date' => ts('Modified Date'),
307 foreach ($allCustomFields as $fieldID => $field) {
308 if ($field['data_type'] == 'Date') {
309 $dateFields["custom_$fieldID"] = $field['label'];
312 $valueLabelMap['civicrm_contact'] = $dateFields;
315 return $valueLabelMap[$name];
319 * Determine whether a schedule based on this mapping is sufficiently
322 * @param \CRM_Core_DAO_ActionSchedule $schedule
324 * Array (string $code => string $message).
325 * List of error messages.
327 public function validateSchedule($schedule) {
332 * Generate a query to locate contacts who match the given
335 * @param \CRM_Core_DAO_ActionSchedule $schedule
336 * @param string $phase
337 * See, e.g., RecipientBuilder::PHASE_RELATION_FIRST.
338 * @param array $defaultParams
339 * @return \CRM_Utils_SQL_Select
341 public abstract function createQuery($schedule, $phase, $defaultParams);