Merge pull request #13343 from GinkgoFJG/case-issues-w-ufgroup-api
[civicrm-core.git] / CRM / Mailing / PseudoConstant.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33
34 /**
35 * This class holds all the Pseudo constants that are specific to Mass mailing. This avoids
36 * polluting the core class and isolates the mass mailer class.
37 */
38 class CRM_Mailing_PseudoConstant extends CRM_Core_PseudoConstant {
39
40 /**
41 * Status options for A/B tests.
42 * @var array
43 */
44 private static $abStatus;
45
46 /**
47 * Test criteria for A/B tests.
48 * @var array
49 */
50 private static $abTestCriteria;
51
52 /**
53 * Winner criteria for A/B tests.
54 * @var array
55 */
56 private static $abWinnerCriteria;
57
58 /**
59 * Mailing templates
60 * @var array
61 */
62 private static $template;
63
64 /**
65 * Completed mailings
66 * @var array
67 */
68 private static $completed;
69
70 /**
71 * Mailing components
72 * @var array
73 */
74 private static $component;
75
76 /**
77 * Default component id's, indexed by component type
78 */
79 private static $defaultComponent;
80
81 /**
82 * Mailing Types
83 * @var array
84 */
85 private static $mailingTypes;
86
87 /**
88 * @return array
89 */
90 public static function abStatus() {
91 if (!is_array(self::$abStatus)) {
92 self::$abStatus = array(
93 'Draft' => ts('Draft'),
94 'Testing' => ts('Testing'),
95 'Final' => ts('Final'),
96 );
97 }
98 return self::$abStatus;
99 }
100
101 /**
102 * @return array
103 */
104 public static function abTestCriteria() {
105 if (!is_array(self::$abTestCriteria)) {
106 self::$abTestCriteria = array(
107 'subject' => ts('Test different "Subject" lines'),
108 'from' => ts('Test different "From" lines'),
109 'full_email' => ts('Test entirely different emails'),
110 );
111 }
112 return self::$abTestCriteria;
113 }
114
115 /**
116 * @return array
117 */
118 public static function abWinnerCriteria() {
119 if (!is_array(self::$abWinnerCriteria)) {
120 self::$abWinnerCriteria = array(
121 'open' => ts('Open'),
122 'unique_click' => ts('Total Unique Clicks'),
123 'link_click' => ts('Total Clicks on a particular link'),
124 );
125 }
126 return self::$abWinnerCriteria;
127 }
128
129 /**
130 * @return array
131 */
132 public static function mailingTypes() {
133 if (!is_array(self::$mailingTypes)) {
134 self::$mailingTypes = array(
135 'standalone' => ts('Standalone'),
136 'experiment' => ts('Experimental'),
137 'winner' => ts('Winner'),
138 );
139 }
140 return self::$mailingTypes;
141 }
142
143 /**
144 * Get all the mailing components of a particular type.
145 *
146 * @param $type
147 * The type of component needed.
148 *
149 * @return array
150 * array reference of all mailing components
151 */
152 public static function &component($type = NULL) {
153 $name = $type ? $type : 'ALL';
154
155 if (!self::$component || !array_key_exists($name, self::$component)) {
156 if (!self::$component) {
157 self::$component = array();
158 }
159 if (!$type) {
160 self::$component[$name] = NULL;
161 CRM_Core_PseudoConstant::populate(self::$component[$name], 'CRM_Mailing_BAO_MailingComponent');
162 }
163 else {
164 // we need to add an additional filter for $type
165 self::$component[$name] = array();
166
167 $object = new CRM_Mailing_BAO_MailingComponent();
168 $object->component_type = $type;
169 $object->selectAdd();
170 $object->selectAdd("id, name");
171 $object->orderBy('component_type, is_default, name');
172 $object->is_active = 1;
173 $object->find();
174 while ($object->fetch()) {
175 self::$component[$name][$object->id] = $object->name;
176 }
177 }
178 }
179 return self::$component[$name];
180 }
181
182 /**
183 * Determine the default mailing component of a given type.
184 *
185 * @param $type
186 * The type of component needed.
187 * @param $undefined
188 * The value to use if no default is defined.
189 *
190 * @return int
191 * The ID of the default mailing component.
192 */
193 public static function &defaultComponent($type, $undefined = NULL) {
194 if (!self::$defaultComponent) {
195 $queryDefaultComponents = "SELECT id, component_type
196 FROM civicrm_mailing_component
197 WHERE is_active = 1
198 AND is_default = 1
199 GROUP BY component_type, id";
200
201 $dao = CRM_Core_DAO::executeQuery($queryDefaultComponents);
202
203 self::$defaultComponent = array();
204 while ($dao->fetch()) {
205 self::$defaultComponent[$dao->component_type] = $dao->id;
206 }
207 }
208 $value = CRM_Utils_Array::value($type, self::$defaultComponent, $undefined);
209 return $value;
210 }
211
212 /**
213 * Get all the mailing templates.
214 *
215 *
216 * @return array
217 * array reference of all mailing templates if any
218 */
219 public static function &template() {
220 if (!self::$template) {
221 CRM_Core_PseudoConstant::populate(self::$template, 'CRM_Mailing_DAO_Mailing', TRUE, 'name', 'is_template');
222 }
223 return self::$template;
224 }
225
226 /**
227 * Get all the completed mailing.
228 *
229 *
230 * @param null $mode
231 *
232 * @return array
233 * array reference of all mailing templates if any
234 */
235 public static function &completed($mode = NULL) {
236 if (!self::$completed) {
237 $mailingACL = CRM_Mailing_BAO_Mailing::mailingACL();
238 $mailingACL .= $mode == 'sms' ? " AND sms_provider_id IS NOT NULL " : "";
239
240 CRM_Core_PseudoConstant::populate(self::$completed,
241 'CRM_Mailing_DAO_Mailing',
242 FALSE,
243 'name',
244 'is_completed',
245 $mailingACL
246 );
247 }
248 return self::$completed;
249 }
250
251 /**
252 * Labels for advanced search against mailing summary.
253 *
254 * @param $field
255 *
256 * @return unknown_type
257 */
258 public static function &yesNoOptions($field) {
259 static $options;
260 if (!$options) {
261 $options = array(
262 'bounce' => array(
263 'N' => ts('Successful '),
264 'Y' => ts('Bounced '),
265 ),
266 'delivered' => array(
267 'Y' => ts('Successful '),
268 'N' => ts('Bounced '),
269 ),
270 'open' => array(
271 'Y' => ts('Opened '),
272 'N' => ts('Unopened/Hidden '),
273 ),
274 'click' => array(
275 'Y' => ts('Clicked '),
276 'N' => ts('Not Clicked '),
277 ),
278 'reply' => array(
279 'Y' => ts('Replied '),
280 'N' => ts('No Reply '),
281 ),
282 );
283 }
284 return $options[$field];
285 }
286
287 /**
288 * Flush given pseudoconstant so it can be reread from db
289 * next time it's requested.
290 *
291 *
292 * @param bool|string $name pseudoconstant to be flushed
293 */
294 public static function flush($name = 'template') {
295 if (isset(self::$$name)) {
296 self::$$name = NULL;
297 }
298 }
299
300 }