3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
13 * This api exposes CiviCRM Campaign records.
15 * @note Campaign component must be enabled.
17 * @package CiviCRM_APIv3
21 * Create/update Campaign.
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.
27 * @param array $params
31 function civicrm_api3_campaign_create($params) {
32 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__
), $params, 'Campaign');
36 * Adjust Metadata for Create action.
38 * The metadata is used for setting defaults, documentation & validation.
40 * @param array $params
41 * Array of parameters determined by getfields.
43 function _civicrm_api3_campaign_create_spec(&$params) {
44 $params['title']['api.required'] = 1;
45 $params['is_active']['api.default'] = 1;
49 * Returns array of campaigns matching a set of one or more properties.
51 * @param array $params
55 * Array of matching campaigns
57 function civicrm_api3_campaign_get($params) {
58 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__
), $params, TRUE, 'Campaign');
62 * Delete an existing campaign.
64 * This method is used to delete any existing campaign.
65 * Id of the campaign to be deleted is required field in $params array
67 * @param array $params
68 * array containing id of the group to be deleted
72 function civicrm_api3_campaign_delete($params) {
73 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__
), $params);
77 * Get campaign list parameters.
79 * @see _civicrm_api3_generic_getlist_params
81 * @param array $request
83 function _civicrm_api3_campaign_getlist_params(&$request) {
84 $fieldsToReturn = ['title', 'campaign_type_id', 'status_id', 'start_date', 'end_date'];
85 $request['params']['return'] = array_unique(array_merge($fieldsToReturn, $request['extra']));
86 if (empty($request['params']['id'])) {
87 $request['params']['options']['sort'] = 'start_date DESC, title';
88 $request['params'] +
= [
95 * Get campaign list output.
97 * @see _civicrm_api3_generic_getlist_output
99 * @param array $result
100 * @param array $request
104 function _civicrm_api3_campaign_getlist_output($result, $request) {
106 if (!empty($result['values'])) {
107 $config = CRM_Core_Config
::singleton();
108 foreach ($result['values'] as $row) {
110 'id' => $row[$request['id_field']],
111 'label' => $row[$request['label_field']],
113 CRM_Core_PseudoConstant
::getLabel('CRM_Campaign_BAO_Campaign', 'campaign_type_id', $row['campaign_type_id']),
116 if (!empty($row['status_id'])) {
117 $data['description'][0] .= ': ' . CRM_Core_PseudoConstant
::getLabel('CRM_Campaign_BAO_Campaign', 'status_id', $row['status_id']);
119 $dateString = CRM_Utils_Date
::customFormat($row['start_date'], $config->dateformatFull
) . ' -';
120 if (!empty($row['end_date'])) {
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);
125 $dateString .= ' ' . CRM_Utils_Date
::customFormat($row['end_date'], $config->dateformatFull
);
127 $data['description'][] = $dateString;