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