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