PseudoConstant.php 8 KB
Newer Older
totten's avatar
totten committed
1 2 3
<?php
/*
 +--------------------------------------------------------------------+
totten's avatar
totten committed
4
 | CiviCRM version 5                                                  |
totten's avatar
totten committed
5
 +--------------------------------------------------------------------+
6
 | Copyright CiviCRM LLC (c) 2004-2019                                |
totten's avatar
totten committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 +--------------------------------------------------------------------+
 | This file is a part of CiviCRM.                                    |
 |                                                                    |
 | CiviCRM is free software; you can copy, modify, and distribute it  |
 | under the terms of the GNU Affero General Public License           |
 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
 |                                                                    |
 | CiviCRM is distributed in the hope that it will be useful, but     |
 | WITHOUT ANY WARRANTY; without even the implied warranty of         |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
 | See the GNU Affero General Public License for more details.        |
 |                                                                    |
 | You should have received a copy of the GNU Affero General Public   |
 | License and the CiviCRM Licensing Exception along                  |
 | with this program; if not, contact CiviCRM LLC                     |
 | at info[AT]civicrm[DOT]org. If you have questions about the        |
 | GNU Affero General Public License or the licensing of CiviCRM,     |
 | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
 +--------------------------------------------------------------------+
26
 */
totten's avatar
totten committed
27 28 29 30

/**
 *
 * @package CRM
31
 * @copyright CiviCRM LLC (c) 2004-2019
totten's avatar
totten committed
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.
totten's avatar
totten committed
37 38 39
 */
class CRM_Mailing_PseudoConstant extends CRM_Core_PseudoConstant {

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
  /**
   * Status options for A/B tests.
   * @var array
   */
  private static $abStatus;

  /**
   * Test criteria for A/B tests.
   * @var array
   */
  private static $abTestCriteria;

  /**
   * Winner criteria for A/B tests.
   * @var array
   */
  private static $abWinnerCriteria;

totten's avatar
totten committed
58
  /**
colemanw's avatar
colemanw committed
59
   * Mailing templates
totten's avatar
totten committed
60 61 62 63 64
   * @var array
   */
  private static $template;

  /**
colemanw's avatar
colemanw committed
65
   * Completed mailings
totten's avatar
totten committed
66 67 68 69 70
   * @var array
   */
  private static $completed;

  /**
colemanw's avatar
colemanw committed
71
   * Mailing components
totten's avatar
totten committed
72 73 74 75 76
   * @var array
   */
  private static $component;

  /**
colemanw's avatar
colemanw committed
77
   * Default component id's, indexed by component type
78
   * @var array
totten's avatar
totten committed
79 80 81
   */
  private static $defaultComponent;

82 83 84 85 86 87
  /**
   * Mailing Types
   * @var array
   */
  private static $mailingTypes;

88 89 90 91 92
  /**
   * @return array
   */
  public static function abStatus() {
    if (!is_array(self::$abStatus)) {
93
      self::$abStatus = [
94 95 96
        'Draft' => ts('Draft'),
        'Testing' => ts('Testing'),
        'Final' => ts('Final'),
97
      ];
98 99 100 101 102 103 104 105 106
    }
    return self::$abStatus;
  }

  /**
   * @return array
   */
  public static function abTestCriteria() {
    if (!is_array(self::$abTestCriteria)) {
107
      self::$abTestCriteria = [
108 109 110
        'subject' => ts('Test different "Subject" lines'),
        'from' => ts('Test different "From" lines'),
        'full_email' => ts('Test entirely different emails'),
111
      ];
112 113 114 115 116 117 118 119 120
    }
    return self::$abTestCriteria;
  }

  /**
   * @return array
   */
  public static function abWinnerCriteria() {
    if (!is_array(self::$abWinnerCriteria)) {
121
      self::$abWinnerCriteria = [
122 123 124
        'open' => ts('Open'),
        'unique_click' => ts('Total Unique Clicks'),
        'link_click' => ts('Total Clicks on a particular link'),
125
      ];
126 127 128 129
    }
    return self::$abWinnerCriteria;
  }

130 131 132 133 134
  /**
   * @return array
   */
  public static function mailingTypes() {
    if (!is_array(self::$mailingTypes)) {
135
      self::$mailingTypes = [
136 137 138
        'standalone' => ts('Standalone'),
        'experiment' => ts('Experimental'),
        'winner' => ts('Winner'),
139
      ];
140 141 142 143
    }
    return self::$mailingTypes;
  }

totten's avatar
totten committed
144
  /**
145
   * Get all the mailing components of a particular type.
totten's avatar
totten committed
146
   *
147 148
   * @param $type
   *   The type of component needed.
totten's avatar
totten committed
149
   *
150 151
   * @return array
   *   array reference of all mailing components
totten's avatar
totten committed
152 153 154 155 156 157
   */
  public static function &component($type = NULL) {
    $name = $type ? $type : 'ALL';

    if (!self::$component || !array_key_exists($name, self::$component)) {
      if (!self::$component) {
158
        self::$component = [];
totten's avatar
totten committed
159 160 161
      }
      if (!$type) {
        self::$component[$name] = NULL;
colemanw's avatar
colemanw committed
162
        CRM_Core_PseudoConstant::populate(self::$component[$name], 'CRM_Mailing_BAO_MailingComponent');
totten's avatar
totten committed
163 164 165
      }
      else {
        // we need to add an additional filter for $type
166
        self::$component[$name] = [];
totten's avatar
totten committed
167

colemanw's avatar
colemanw committed
168
        $object = new CRM_Mailing_BAO_MailingComponent();
totten's avatar
totten committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
        $object->component_type = $type;
        $object->selectAdd();
        $object->selectAdd("id, name");
        $object->orderBy('component_type, is_default, name');
        $object->is_active = 1;
        $object->find();
        while ($object->fetch()) {
          self::$component[$name][$object->id] = $object->name;
        }
      }
    }
    return self::$component[$name];
  }

  /**
184
   * Determine the default mailing component of a given type.
totten's avatar
totten committed
185
   *
186 187 188 189
   * @param $type
   *   The type of component needed.
   * @param $undefined
   *   The value to use if no default is defined.
totten's avatar
totten committed
190
   *
191
   * @return int
192
   *   The ID of the default mailing component.
totten's avatar
totten committed
193 194 195 196 197 198 199
   */
  public static function &defaultComponent($type, $undefined = NULL) {
    if (!self::$defaultComponent) {
      $queryDefaultComponents = "SELECT id, component_type
                FROM    civicrm_mailing_component
                WHERE   is_active = 1
                AND     is_default = 1
200
                GROUP BY component_type, id";
totten's avatar
totten committed
201 202 203

      $dao = CRM_Core_DAO::executeQuery($queryDefaultComponents);

204
      self::$defaultComponent = [];
totten's avatar
totten committed
205 206 207 208 209 210 211 212 213
      while ($dao->fetch()) {
        self::$defaultComponent[$dao->component_type] = $dao->id;
      }
    }
    $value = CRM_Utils_Array::value($type, self::$defaultComponent, $undefined);
    return $value;
  }

  /**
214
   * Get all the mailing templates.
totten's avatar
totten committed
215 216
   *
   *
217 218
   * @return array
   *   array reference of all mailing templates if any
totten's avatar
totten committed
219 220 221 222 223 224 225 226 227
   */
  public static function &template() {
    if (!self::$template) {
      CRM_Core_PseudoConstant::populate(self::$template, 'CRM_Mailing_DAO_Mailing', TRUE, 'name', 'is_template');
    }
    return self::$template;
  }

  /**
228
   * Get all the completed mailing.
totten's avatar
totten committed
229 230
   *
   *
Eileen McNaughton's avatar
Eileen McNaughton committed
231 232
   * @param null $mode
   *
233 234
   * @return array
   *   array reference of all mailing templates if any
totten's avatar
totten committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
   */
  public static function &completed($mode = NULL) {
    if (!self::$completed) {
      $mailingACL = CRM_Mailing_BAO_Mailing::mailingACL();
      $mailingACL .= $mode == 'sms' ? " AND sms_provider_id IS NOT NULL " : "";

      CRM_Core_PseudoConstant::populate(self::$completed,
        'CRM_Mailing_DAO_Mailing',
        FALSE,
        'name',
        'is_completed',
        $mailingACL
      );
    }
    return self::$completed;
  }

  /**
   * Labels for advanced search against mailing summary.
   *
   * @param $field
   *
   * @return unknown_type
   */
  public static function &yesNoOptions($field) {
    static $options;
    if (!$options) {
262 263
      $options = [
        '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
        ],
      ];
totten's avatar
totten committed
284 285 286 287 288 289
    }
    return $options[$field];
  }

  /**
   * Flush given pseudoconstant so it can be reread from db
290
   * next time it's requested.
totten's avatar
totten committed
291 292
   *
   *
Eileen McNaughton's avatar
Eileen McNaughton committed
293
   * @param bool|string $name pseudoconstant to be flushed
totten's avatar
totten committed
294
   */
295
  public static function flush($name = 'template') {
296
    if (isset(self::$$name)) {
297 298
      self::$$name = NULL;
    }
totten's avatar
totten committed
299
  }
300

totten's avatar
totten committed
301
}