Merge pull request #19307 from eileenmcnaughton/534
[civicrm-core.git] / api / v3 / Campaign.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
a30c801b 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
a30c801b
TO
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 |
6a488035
TO
9 +--------------------------------------------------------------------+
10 */
11
12/**
244bbdd8 13 * This api exposes CiviCRM Campaign records.
b081365f
CW
14 *
15 * @note Campaign component must be enabled.
6a488035
TO
16 *
17 * @package CiviCRM_APIv3
6a488035
TO
18 */
19
6a488035 20/**
244bbdd8 21 * Create/update Campaign.
6a488035
TO
22 *
23 * This API is used to create new campaign or update any of the existing
24 * In case of updating existing campaign, id of that particular campaign must
25 * be in $params array.
26 *
cf470720 27 * @param array $params
6a488035 28 *
a6c01b45 29 * @return array
6a488035
TO
30 */
31function civicrm_api3_campaign_create($params) {
bf38705f 32 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Campaign');
6a488035 33}
11e09c59
TO
34
35/**
0aa0303c
EM
36 * Adjust Metadata for Create action.
37 *
38 * The metadata is used for setting defaults, documentation & validation.
1c88e578 39 *
cf470720 40 * @param array $params
b081365f 41 * Array of parameters determined by getfields.
6a488035
TO
42 */
43function _civicrm_api3_campaign_create_spec(&$params) {
44 $params['title']['api.required'] = 1;
bab4f15e 45 $params['is_active']['api.default'] = 1;
6a488035
TO
46}
47
48/**
244bbdd8 49 * Returns array of campaigns matching a set of one or more properties.
6a488035 50 *
cf470720 51 * @param array $params
c23f45d3 52 * Array per getfields
6a488035 53 *
a6c01b45 54 * @return array
72b3a70c 55 * Array of matching campaigns
6a488035
TO
56 */
57function civicrm_api3_campaign_get($params) {
bf38705f 58 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, TRUE, 'Campaign');
6a488035
TO
59}
60
61/**
35823763 62 * Delete an existing campaign.
6a488035 63 *
244bbdd8
CW
64 * This method is used to delete any existing campaign.
65 * Id of the campaign to be deleted is required field in $params array
6a488035 66 *
cf470720 67 * @param array $params
c23f45d3 68 * array containing id of the group to be deleted
6a488035 69 *
a6c01b45 70 * @return array
6a488035
TO
71 */
72function civicrm_api3_campaign_delete($params) {
73 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
74}
8dbd6052
CW
75
76/**
77 * Get campaign list parameters.
78 *
79 * @see _civicrm_api3_generic_getlist_params
80 *
81 * @param array $request
82 */
83function _civicrm_api3_campaign_getlist_params(&$request) {
b7b528bc 84 $fieldsToReturn = ['title', 'campaign_type_id', 'status_id', 'start_date', 'end_date'];
8dbd6052
CW
85 $request['params']['return'] = array_unique(array_merge($fieldsToReturn, $request['extra']));
86 if (empty($request['params']['id'])) {
33ccf435 87 $request['params']['options']['sort'] = 'start_date DESC, title';
8dbd6052
CW
88 $request['params'] += [
89 'is_active' => 1,
90 ];
91 }
92}
93
94/**
95 * Get campaign list output.
96 *
97 * @see _civicrm_api3_generic_getlist_output
98 *
99 * @param array $result
100 * @param array $request
101 *
102 * @return array
103 */
104function _civicrm_api3_campaign_getlist_output($result, $request) {
105 $output = [];
106 if (!empty($result['values'])) {
b7b528bc 107 $config = CRM_Core_Config::singleton();
8dbd6052
CW
108 foreach ($result['values'] as $row) {
109 $data = [
110 'id' => $row[$request['id_field']],
111 'label' => $row[$request['label_field']],
112 'description' => [
4a413eb6 113 CRM_Core_PseudoConstant::getLabel('CRM_Campaign_BAO_Campaign', 'campaign_type_id', $row['campaign_type_id']),
8dbd6052
CW
114 ],
115 ];
b7b528bc 116 if (!empty($row['status_id'])) {
4a413eb6 117 $data['description'][0] .= ': ' . CRM_Core_PseudoConstant::getLabel('CRM_Campaign_BAO_Campaign', 'status_id', $row['status_id']);
b7b528bc
CW
118 }
119 $dateString = CRM_Utils_Date::customFormat($row['start_date'], $config->dateformatFull) . ' -';
8dbd6052 120 if (!empty($row['end_date'])) {
b7b528bc
CW
121 // Remove redundant years
122 if (substr($row['start_date'], 0, 4) == substr($row['end_date'], 0, 4)) {
123 $dateString = preg_replace('/[, ]*' . substr($row['start_date'], 0, 4) . '/', '', $dateString);
124 }
125 $dateString .= ' ' . CRM_Utils_Date::customFormat($row['end_date'], $config->dateformatFull);
8dbd6052 126 }
b7b528bc 127 $data['description'][] = $dateString;
8dbd6052
CW
128 $output[] = $data;
129 }
130 }
131 return $output;
b7b528bc 132}