Merge pull request #4983 from colemanw/CRM-15842
[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 * Mailing templates
44 * @var array
45 */
46 private static $template;
47
48 /**
49 * Completed mailings
50 * @var array
51 */
52 private static $completed;
53
54 /**
55 * Mailing components
56 * @var array
57 */
58 private static $component;
59
60 /**
61 * Default component id's, indexed by component type
62 */
63 private static $defaultComponent;
64
65 /**
66 * Get all the mailing components of a particular type
67 *
68 * @param $type
69 * The type of component needed.
70 *
71 * @return array
72 * array reference of all mailing components
73 */
74 public static function &component($type = NULL) {
75 $name = $type ? $type : 'ALL';
76
77 if (!self::$component || !array_key_exists($name, self::$component)) {
78 if (!self::$component) {
79 self::$component = array();
80 }
81 if (!$type) {
82 self::$component[$name] = NULL;
83 CRM_Core_PseudoConstant::populate(self::$component[$name], 'CRM_Mailing_DAO_Component');
84 }
85 else {
86 // we need to add an additional filter for $type
87 self::$component[$name] = array();
88
89 $object = new CRM_Mailing_DAO_Component();
90 $object->component_type = $type;
91 $object->selectAdd();
92 $object->selectAdd("id, name");
93 $object->orderBy('component_type, is_default, name');
94 $object->is_active = 1;
95 $object->find();
96 while ($object->fetch()) {
97 self::$component[$name][$object->id] = $object->name;
98 }
99 }
100 }
101 return self::$component[$name];
102 }
103
104 /**
105 * Determine the default mailing component of a given type
106 *
107 * @param $type
108 * The type of component needed.
109 * @param $undefined
110 * The value to use if no default is defined.
111 *
112 * @return int
113 * The ID of the default mailing component.
114 */
115 public static function &defaultComponent($type, $undefined = NULL) {
116 if (!self::$defaultComponent) {
117 $queryDefaultComponents = "SELECT id, component_type
118 FROM civicrm_mailing_component
119 WHERE is_active = 1
120 AND is_default = 1
121 GROUP BY component_type";
122
123 $dao = CRM_Core_DAO::executeQuery($queryDefaultComponents);
124
125 self::$defaultComponent = array();
126 while ($dao->fetch()) {
127 self::$defaultComponent[$dao->component_type] = $dao->id;
128 }
129 }
130 $value = CRM_Utils_Array::value($type, self::$defaultComponent, $undefined);
131 return $value;
132 }
133
134 /**
135 * Get all the mailing templates
136 *
137 *
138 * @return array
139 * array reference of all mailing templates if any
140 */
141 public static function &template() {
142 if (!self::$template) {
143 CRM_Core_PseudoConstant::populate(self::$template, 'CRM_Mailing_DAO_Mailing', TRUE, 'name', 'is_template');
144 }
145 return self::$template;
146 }
147
148 /**
149 * Get all the completed mailing
150 *
151 *
152 * @param null $mode
153 *
154 * @return array
155 * array reference of all mailing templates if any
156 */
157 public static function &completed($mode = NULL) {
158 if (!self::$completed) {
159 $mailingACL = CRM_Mailing_BAO_Mailing::mailingACL();
160 $mailingACL .= $mode == 'sms' ? " AND sms_provider_id IS NOT NULL " : "";
161
162 CRM_Core_PseudoConstant::populate(self::$completed,
163 'CRM_Mailing_DAO_Mailing',
164 FALSE,
165 'name',
166 'is_completed',
167 $mailingACL
168 );
169 }
170 return self::$completed;
171 }
172
173 /**
174 * Labels for advanced search against mailing summary.
175 *
176 * @param $field
177 *
178 * @return unknown_type
179 */
180 public static function &yesNoOptions($field) {
181 static $options;
182 if (!$options) {
183 $options = array(
184 'bounce' => array(
185 'N' => ts('Successful '),
186 'Y' => ts('Bounced '),
187 ),
188 'delivered' => array(
189 'Y' => ts('Successful '),
190 'N' => ts('Bounced '),
191 ),
192 'open' => array(
193 'Y' => ts('Opened '),
194 'N' => ts('Unopened/Hidden '),
195 ),
196 'click' => array(
197 'Y' => ts('Clicked '),
198 'N' => ts('Not Clicked '),
199 ),
200 'reply' => array(
201 'Y' => ts('Replied '),
202 'N' => ts('No Reply '),
203 ),
204 );
205 }
206 return $options[$field];
207 }
208
209 /**
210 * Flush given pseudoconstant so it can be reread from db
211 * next time it's requested.
212 *
213 *
214 * @param bool|string $name pseudoconstant to be flushed
215 */
216 public static function flush($name = 'template') {
217 if (isset(self::$$name)) {
218 self::$$name = NULL;
219 }
220 }
221
222 }