CRM-13244 - ActionSchedule - Extract entity-specific logic for tokens and dropdowns.
[civicrm-core.git] / Civi / ActionSchedule / Mapping.php
CommitLineData
50a23755
TO
1<?php
2namespace Civi\ActionSchedule;
3
4class Mapping {
5
6 private static $fields = array(
7 'id',
8 'entity',
9 'entity_label',
10 'entity_value',
11 'entity_value_label',
12 'entity_status',
13 'entity_status_label',
14 'entity_date_start',
15 'entity_date_end',
16 'entity_recipient',
17 );
18
19 public static function create($params) {
20 return new static($params);
21 }
22
23 public function __construct($params) {
24 foreach (self::$fields as $field) {
25 if (isset($params[$field])) {
26 $this->{$field} = $params[$field];
27 }
28 }
29 }
30
31 public $id;
32
33 /**
34 * The basic entity to query (table name).
35 *
36 * @var string
37 * Ex: 'civicrm_activity', 'civicrm_event'.
38 */
39 public $entity;
40
41 /**
42 * The basic entity to query (label).
43 *
44 * @var
45 * Ex: 'Activity', 'Event'
46 */
47 public $entity_label;
48
49 /**
50 * Level 1 filter -- the field/option-list to filter on.
51 *
52 * @var string
53 * Ex: 'activity_type', 'civicrm_event', 'event_template'.
54 */
55 public $entity_value;
56
57 /**
58 * Level 1 filter -- The field label.
59 *
60 * @var string
61 * Ex: 'Activity Type', 'Event Name', 'Event Template'.
62 */
63 public $entity_value_label;
64
65 /**
66 * Level 2 filter -- the field/option-list to filter on.
67 * @var string
68 * Ex: 'activity_status, 'civicrm_participant_status_type', 'auto_renew_options'.
69 */
70 public $entity_status;
71
72 /**
73 * Level 2 filter -- the field label.
74 * @var string
75 * Ex: 'Activity Status', 'Participant Status', 'Auto Rewnewal Options'.
76 */
77 public $entity_status_label;
78
79 /**
80 * Date filter -- the field name.
81 * @var string|NULL
82 * Ex: 'event_start_date'
83 */
84 public $entity_date_start;
85
86 /**
87 * Date filter -- the field name.
88 * @var string|NULL
89 * Ex: 'event_end_date'.
90 */
91 public $entity_date_end;
92
93 /**
94 * Contact selector -- The field/relationship/option-group name.
95 * @var string|NULL
96 * Ex: 'activity_contacts', 'event_contacts'.
97 */
98 public $entity_recipient;
99
100 /**
101 * Get a list of value options.
102 *
103 * @return array
104 * Array(string $value => string $label).
105 * Ex: array(123 => 'Phone Call', 456 => 'Meeting').
106 */
107 public function getValueLabels() {
108 return self::getValueLabelMap($this->entity_value);
109 }
110
111 /**
112 * Get a list of status options.
113 *
114 * @param string|int $value
115 * The list of status options may be contingent upon the selected filter value.
116 * This is the selected filter value.
117 * @return array
118 * Array(string $value => string $label).
119 * Ex: Array(123 => 'Completed', 456 => 'Scheduled').
120 */
121 public function getStatusLabels($value) {
122 if ($this->entity_status === 'auto_renew_options') {
123 if ($value && \CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $value, 'auto_renew')) {
124 return \CRM_Core_OptionGroup::values('auto_renew_options');
125 }
126 else {
127 return array();
128 }
129 }
130 return self::getValueLabelMap($this->entity_status);
131 }
132
133 /**
134 * @return array
135 * Array(string $fieldName => string $fieldLabel).
136 */
137 public function getDateFields() {
138 $dateFieldLabels = array();
139 if (!empty($this->entity_date_start)) {
140 $dateFieldLabels[$this->entity_date_start] = ucwords(str_replace('_', ' ', $this->entity_date_start));
141 }
142 if (!empty($this->entity_date_end)) {
143 $dateFieldLabels[$this->entity_date_end] = ucwords(str_replace('_', ' ', $this->entity_date_end));
144 }
145 return $dateFieldLabels;
146 }
147
148 /**
149 * @param bool|NULL $noThanksJustKidding
150 * This is ridiculous and should not exist.
151 * If true, don't do our main job.
152 * @return array
153 * array(string $value => string $label).
154 * Ex: array('assignee' => 'Activity Assignee').
155 */
156 public function getRecipientTypes($noThanksJustKidding = FALSE) {
157 $entityRecipientLabels = array();
158 switch ($this->entity_recipient) {
159 case 'activity_contacts':
160 $entityRecipientLabels = \CRM_Core_OptionGroup::values('activity_contacts');
161 break;
162
163 case 'event_contacts':
164 if (!$noThanksJustKidding) {
165 $entityRecipientLabels = \CRM_Core_OptionGroup::values('event_contacts', FALSE, FALSE, FALSE, NULL, 'label', TRUE, FALSE, 'name');
166 }
167 break;
168
169 default:
170 }
171 $entityRecipientLabels += array(
172 'manual' => ts('Choose Recipient(s)'),
173 'group' => ts('Select Group'),
174 );
175 return $entityRecipientLabels;
176 }
177
178 protected static function getValueLabelMap($name) {
179 static $valueLabelMap = NULL;
180 if ($valueLabelMap === NULL) {
181 $valueLabelMap['activity_type'] = \CRM_Core_PseudoConstant::activityType(TRUE, TRUE);
182 asort($valueLabelMap['activity_type']);
183
184 $valueLabelMap['activity_status'] = \CRM_Core_PseudoConstant::activityStatus();
185 $valueLabelMap['event_type'] = \CRM_Event_PseudoConstant::eventType();
186 $valueLabelMap['civicrm_event'] = \CRM_Event_PseudoConstant::event(NULL, FALSE, "( is_template IS NULL OR is_template != 1 )");
187 $valueLabelMap['civicrm_participant_status_type'] = \CRM_Event_PseudoConstant::participantStatus(NULL, NULL, 'label');
188 $valueLabelMap['event_template'] = \CRM_Event_PseudoConstant::eventTemplates();
189 $valueLabelMap['auto_renew_options'] = \CRM_Core_OptionGroup::values('auto_renew_options');
190 $valueLabelMap['contact_date_reminder_options'] = \CRM_Core_OptionGroup::values('contact_date_reminder_options');
191 $valueLabelMap['civicrm_membership_type'] = \CRM_Member_PseudoConstant::membershipType();
192
193 $allCustomFields = \CRM_Core_BAO_CustomField::getFields('');
194 $dateFields = array(
195 'birth_date' => ts('Birth Date'),
196 'created_date' => ts('Created Date'),
197 'modified_date' => ts('Modified Date'),
198 );
199 foreach ($allCustomFields as $fieldID => $field) {
200 if ($field['data_type'] == 'Date') {
201 $dateFields["custom_$fieldID"] = $field['label'];
202 }
203 }
204 $valueLabelMap['civicrm_contact'] = $dateFields;
205 }
206
207 return $valueLabelMap[$name];
208 }
209
210}