Campaign.php 4.65 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
 +--------------------------------------------------------------------+
Seamus Lee's avatar
Seamus Lee committed
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 26 27 28
 +--------------------------------------------------------------------+
 | 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        |
 +--------------------------------------------------------------------+
 */

/**
29
 * This api exposes CiviCRM Campaign records.
colemanw's avatar
colemanw committed
30 31
 *
 * @note Campaign component must be enabled.
totten's avatar
totten committed
32 33 34 35 36
 *
 * @package CiviCRM_APIv3
 */

/**
37
 * Create/update Campaign.
totten's avatar
totten committed
38 39 40 41 42
 *
 * This API is used to create new campaign or update any of the existing
 * In case of updating existing campaign, id of that particular campaign must
 * be in $params array.
 *
43
 * @param array $params
totten's avatar
totten committed
44
 *
45
 * @return array
totten's avatar
totten committed
46 47
 */
function civicrm_api3_campaign_create($params) {
48
  return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Campaign');
totten's avatar
totten committed
49
}
totten's avatar
totten committed
50 51

/**
52 53 54
 * Adjust Metadata for Create action.
 *
 * The metadata is used for setting defaults, documentation & validation.
eileen's avatar
eileen committed
55
 *
56
 * @param array $params
colemanw's avatar
colemanw committed
57
 *   Array of parameters determined by getfields.
totten's avatar
totten committed
58 59 60
 */
function _civicrm_api3_campaign_create_spec(&$params) {
  $params['title']['api.required'] = 1;
61
  $params['is_active']['api.default'] = 1;
totten's avatar
totten committed
62 63 64
}

/**
65
 * Returns array of campaigns matching a set of one or more properties.
totten's avatar
totten committed
66
 *
67
 * @param array $params
Eileen McNaughton's avatar
Eileen McNaughton committed
68
 *   Array per getfields
totten's avatar
totten committed
69
 *
70
 * @return array
71
 *   Array of matching campaigns
totten's avatar
totten committed
72 73
 */
function civicrm_api3_campaign_get($params) {
74
  return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, TRUE, 'Campaign');
totten's avatar
totten committed
75 76 77
}

/**
Eileen McNaughton's avatar
Eileen McNaughton committed
78
 * Delete an existing campaign.
totten's avatar
totten committed
79
 *
80 81
 * This method is used to delete any existing campaign.
 * Id of the campaign to be deleted is required field in $params array
totten's avatar
totten committed
82
 *
83
 * @param array $params
Eileen McNaughton's avatar
Eileen McNaughton committed
84
 *   array containing id of the group to be deleted
totten's avatar
totten committed
85
 *
86
 * @return array
totten's avatar
totten committed
87 88 89 90
 */
function civicrm_api3_campaign_delete($params) {
  return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
}
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

/**
 * Get campaign list parameters.
 *
 * @see _civicrm_api3_generic_getlist_params
 *
 * @param array $request
 */
function _civicrm_api3_campaign_getlist_params(&$request) {
  $fieldsToReturn = ['title', 'campaign_type_id', 'start_date', 'end_date'];
  $request['params']['return'] = array_unique(array_merge($fieldsToReturn, $request['extra']));
  if (empty($request['params']['id'])) {
    $request['params'] += [
      'is_active' => 1,
    ];
  }
}

/**
 * Get campaign list output.
 *
 * @see _civicrm_api3_generic_getlist_output
 *
 * @param array $result
 * @param array $request
 *
 * @return array
 */
function _civicrm_api3_campaign_getlist_output($result, $request) {
  $output = [];
  if (!empty($result['values'])) {
    foreach ($result['values'] as $row) {
      $data = [
        'id' => $row[$request['id_field']],
        'label' => $row[$request['label_field']],
        'description' => [
          CRM_Core_Pseudoconstant::getLabel(
            'CRM_Campaign_BAO_Campaign',
            'campaign_type_id',
            $row['campaign_type_id']
          ),
        ],
      ];
      $config = CRM_Core_Config::singleton();
      $data['description'][0] .= ': ' . CRM_Utils_Date::customFormat($row['start_date'], $config->dateformatFull) . ' - ';
      if (!empty($row['end_date'])) {
        $data['description'][0] .= CRM_Utils_Date::customFormat($row['end_date'], $config->dateformatFull);
      }
      $output[] = $data;
    }
  }
  return $output;
}