Merge pull request #20837 from colemanw/customACLs
[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) {
e13640cc
CW
84 $fieldsToReturn = [
85 'title',
86 'campaign_type_id',
87 'status_id',
88 'start_date',
89 'end_date',
90 $request['id_field'],
91 $request['label_field'],
92 ];
8dbd6052
CW
93 $request['params']['return'] = array_unique(array_merge($fieldsToReturn, $request['extra']));
94 if (empty($request['params']['id'])) {
33ccf435 95 $request['params']['options']['sort'] = 'start_date DESC, title';
8dbd6052
CW
96 $request['params'] += [
97 'is_active' => 1,
98 ];
99 }
100}
101
102/**
103 * Get campaign list output.
104 *
105 * @see _civicrm_api3_generic_getlist_output
106 *
107 * @param array $result
108 * @param array $request
109 *
110 * @return array
111 */
112function _civicrm_api3_campaign_getlist_output($result, $request) {
113 $output = [];
114 if (!empty($result['values'])) {
b7b528bc 115 $config = CRM_Core_Config::singleton();
8dbd6052
CW
116 foreach ($result['values'] as $row) {
117 $data = [
118 'id' => $row[$request['id_field']],
119 'label' => $row[$request['label_field']],
120 'description' => [
4a413eb6 121 CRM_Core_PseudoConstant::getLabel('CRM_Campaign_BAO_Campaign', 'campaign_type_id', $row['campaign_type_id']),
8dbd6052
CW
122 ],
123 ];
b7b528bc 124 if (!empty($row['status_id'])) {
4a413eb6 125 $data['description'][0] .= ': ' . CRM_Core_PseudoConstant::getLabel('CRM_Campaign_BAO_Campaign', 'status_id', $row['status_id']);
b7b528bc
CW
126 }
127 $dateString = CRM_Utils_Date::customFormat($row['start_date'], $config->dateformatFull) . ' -';
8dbd6052 128 if (!empty($row['end_date'])) {
b7b528bc
CW
129 // Remove redundant years
130 if (substr($row['start_date'], 0, 4) == substr($row['end_date'], 0, 4)) {
131 $dateString = preg_replace('/[, ]*' . substr($row['start_date'], 0, 4) . '/', '', $dateString);
132 }
133 $dateString .= ' ' . CRM_Utils_Date::customFormat($row['end_date'], $config->dateformatFull);
8dbd6052 134 }
b7b528bc 135 $data['description'][] = $dateString;
8dbd6052
CW
136 $output[] = $data;
137 }
138 }
139 return $output;
b7b528bc 140}