| 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 | * @var array |
| 79 | */ |
| 80 | private static $defaultComponent; |
| 81 | |
| 82 | /** |
| 83 | * Mailing Types |
| 84 | * @var array |
| 85 | */ |
| 86 | private static $mailingTypes; |
| 87 | |
| 88 | /** |
| 89 | * @return array |
| 90 | */ |
| 91 | public static function abStatus() { |
| 92 | if (!is_array(self::$abStatus)) { |
| 93 | self::$abStatus = [ |
| 94 | 'Draft' => ts('Draft'), |
| 95 | 'Testing' => ts('Testing'), |
| 96 | 'Final' => ts('Final'), |
| 97 | ]; |
| 98 | } |
| 99 | return self::$abStatus; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * @return array |
| 104 | */ |
| 105 | public static function abTestCriteria() { |
| 106 | if (!is_array(self::$abTestCriteria)) { |
| 107 | self::$abTestCriteria = [ |
| 108 | 'subject' => ts('Test different "Subject" lines'), |
| 109 | 'from' => ts('Test different "From" lines'), |
| 110 | 'full_email' => ts('Test entirely different emails'), |
| 111 | ]; |
| 112 | } |
| 113 | return self::$abTestCriteria; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * @return array |
| 118 | */ |
| 119 | public static function abWinnerCriteria() { |
| 120 | if (!is_array(self::$abWinnerCriteria)) { |
| 121 | self::$abWinnerCriteria = [ |
| 122 | 'open' => ts('Open'), |
| 123 | 'unique_click' => ts('Total Unique Clicks'), |
| 124 | 'link_click' => ts('Total Clicks on a particular link'), |
| 125 | ]; |
| 126 | } |
| 127 | return self::$abWinnerCriteria; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * @return array |
| 132 | */ |
| 133 | public static function mailingTypes() { |
| 134 | if (!is_array(self::$mailingTypes)) { |
| 135 | self::$mailingTypes = [ |
| 136 | 'standalone' => ts('Standalone'), |
| 137 | 'experiment' => ts('Experimental'), |
| 138 | 'winner' => ts('Winner'), |
| 139 | ]; |
| 140 | } |
| 141 | return self::$mailingTypes; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Get all the mailing components of a particular type. |
| 146 | * |
| 147 | * @param $type |
| 148 | * The type of component needed. |
| 149 | * |
| 150 | * @return array |
| 151 | * array reference of all mailing components |
| 152 | */ |
| 153 | public static function &component($type = NULL) { |
| 154 | $name = $type ? $type : 'ALL'; |
| 155 | |
| 156 | if (!self::$component || !array_key_exists($name, self::$component)) { |
| 157 | if (!self::$component) { |
| 158 | self::$component = []; |
| 159 | } |
| 160 | if (!$type) { |
| 161 | self::$component[$name] = NULL; |
| 162 | CRM_Core_PseudoConstant::populate(self::$component[$name], 'CRM_Mailing_BAO_MailingComponent'); |
| 163 | } |
| 164 | else { |
| 165 | // we need to add an additional filter for $type |
| 166 | self::$component[$name] = []; |
| 167 | |
| 168 | $object = new CRM_Mailing_BAO_MailingComponent(); |
| 169 | $object->component_type = $type; |
| 170 | $object->selectAdd(); |
| 171 | $object->selectAdd("id, name"); |
| 172 | $object->orderBy('component_type, is_default, name'); |
| 173 | $object->is_active = 1; |
| 174 | $object->find(); |
| 175 | while ($object->fetch()) { |
| 176 | self::$component[$name][$object->id] = $object->name; |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | return self::$component[$name]; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Determine the default mailing component of a given type. |
| 185 | * |
| 186 | * @param $type |
| 187 | * The type of component needed. |
| 188 | * @param $undefined |
| 189 | * The value to use if no default is defined. |
| 190 | * |
| 191 | * @return int |
| 192 | * The ID of the default mailing component. |
| 193 | */ |
| 194 | public static function &defaultComponent($type, $undefined = NULL) { |
| 195 | if (!self::$defaultComponent) { |
| 196 | $queryDefaultComponents = "SELECT id, component_type |
| 197 | FROM civicrm_mailing_component |
| 198 | WHERE is_active = 1 |
| 199 | AND is_default = 1 |
| 200 | GROUP BY component_type, id"; |
| 201 | |
| 202 | $dao = CRM_Core_DAO::executeQuery($queryDefaultComponents); |
| 203 | |
| 204 | self::$defaultComponent = []; |
| 205 | while ($dao->fetch()) { |
| 206 | self::$defaultComponent[$dao->component_type] = $dao->id; |
| 207 | } |
| 208 | } |
| 209 | $value = CRM_Utils_Array::value($type, self::$defaultComponent, $undefined); |
| 210 | return $value; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Get all the mailing templates. |
| 215 | * |
| 216 | * |
| 217 | * @return array |
| 218 | * array reference of all mailing templates if any |
| 219 | */ |
| 220 | public static function &template() { |
| 221 | if (!self::$template) { |
| 222 | CRM_Core_PseudoConstant::populate(self::$template, 'CRM_Mailing_DAO_Mailing', TRUE, 'name', 'is_template'); |
| 223 | } |
| 224 | return self::$template; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Get all the completed mailing. |
| 229 | * |
| 230 | * |
| 231 | * @param null $mode |
| 232 | * |
| 233 | * @return array |
| 234 | * array reference of all mailing templates if any |
| 235 | */ |
| 236 | public static function &completed($mode = NULL) { |
| 237 | if (!self::$completed) { |
| 238 | $mailingACL = CRM_Mailing_BAO_Mailing::mailingACL(); |
| 239 | $mailingACL .= $mode == 'sms' ? " AND sms_provider_id IS NOT NULL " : ""; |
| 240 | |
| 241 | CRM_Core_PseudoConstant::populate(self::$completed, |
| 242 | 'CRM_Mailing_DAO_Mailing', |
| 243 | FALSE, |
| 244 | 'name', |
| 245 | 'is_completed', |
| 246 | $mailingACL |
| 247 | ); |
| 248 | } |
| 249 | return self::$completed; |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Labels for advanced search against mailing summary. |
| 254 | * |
| 255 | * @param $field |
| 256 | * |
| 257 | * @return unknown_type |
| 258 | */ |
| 259 | public static function &yesNoOptions($field) { |
| 260 | static $options; |
| 261 | if (!$options) { |
| 262 | $options = [ |
| 263 | 'bounce' => [ |
| 264 | 'N' => ts('Successful '), |
| 265 | 'Y' => ts('Bounced '), |
| 266 | ], |
| 267 | 'delivered' => [ |
| 268 | 'Y' => ts('Successful '), |
| 269 | 'N' => ts('Bounced '), |
| 270 | ], |
| 271 | 'open' => [ |
| 272 | 'Y' => ts('Opened '), |
| 273 | 'N' => ts('Unopened/Hidden '), |
| 274 | ], |
| 275 | 'click' => [ |
| 276 | 'Y' => ts('Clicked '), |
| 277 | 'N' => ts('Not Clicked '), |
| 278 | ], |
| 279 | 'reply' => [ |
| 280 | 'Y' => ts('Replied '), |
| 281 | 'N' => ts('No Reply '), |
| 282 | ], |
| 283 | ]; |
| 284 | } |
| 285 | return $options[$field]; |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Flush given pseudoconstant so it can be reread from db |
| 290 | * next time it's requested. |
| 291 | * |
| 292 | * |
| 293 | * @param bool|string $name pseudoconstant to be flushed |
| 294 | */ |
| 295 | public static function flush($name = 'template') { |
| 296 | if (isset(self::$$name)) { |
| 297 | self::$$name = NULL; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | } |