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