| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 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 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * This api exposes CiviCRM Campaign records. |
| 14 | * |
| 15 | * @note Campaign component must be enabled. |
| 16 | * |
| 17 | * @package CiviCRM_APIv3 |
| 18 | */ |
| 19 | |
| 20 | /** |
| 21 | * Create/update Campaign. |
| 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 | * |
| 27 | * @param array $params |
| 28 | * |
| 29 | * @return array |
| 30 | */ |
| 31 | function civicrm_api3_campaign_create($params) { |
| 32 | return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Campaign'); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Adjust Metadata for Create action. |
| 37 | * |
| 38 | * The metadata is used for setting defaults, documentation & validation. |
| 39 | * |
| 40 | * @param array $params |
| 41 | * Array of parameters determined by getfields. |
| 42 | */ |
| 43 | function _civicrm_api3_campaign_create_spec(&$params) { |
| 44 | $params['title']['api.required'] = 1; |
| 45 | $params['is_active']['api.default'] = 1; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Returns array of campaigns matching a set of one or more properties. |
| 50 | * |
| 51 | * @param array $params |
| 52 | * Array per getfields |
| 53 | * |
| 54 | * @return array |
| 55 | * Array of matching campaigns |
| 56 | */ |
| 57 | function civicrm_api3_campaign_get($params) { |
| 58 | return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, TRUE, 'Campaign'); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Delete an existing campaign. |
| 63 | * |
| 64 | * This method is used to delete any existing campaign. |
| 65 | * Id of the campaign to be deleted is required field in $params array |
| 66 | * |
| 67 | * @param array $params |
| 68 | * array containing id of the group to be deleted |
| 69 | * |
| 70 | * @return array |
| 71 | */ |
| 72 | function civicrm_api3_campaign_delete($params) { |
| 73 | return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Get campaign list parameters. |
| 78 | * |
| 79 | * @see _civicrm_api3_generic_getlist_params |
| 80 | * |
| 81 | * @param array $request |
| 82 | */ |
| 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'] += [ |
| 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 | */ |
| 104 | function _civicrm_api3_campaign_getlist_output($result, $request) { |
| 105 | $output = []; |
| 106 | if (!empty($result['values'])) { |
| 107 | $config = CRM_Core_Config::singleton(); |
| 108 | foreach ($result['values'] as $row) { |
| 109 | $data = [ |
| 110 | 'id' => $row[$request['id_field']], |
| 111 | 'label' => $row[$request['label_field']], |
| 112 | 'description' => [ |
| 113 | CRM_Core_PseudoConstant::getLabel('CRM_Campaign_BAO_Campaign', 'campaign_type_id', $row['campaign_type_id']), |
| 114 | ], |
| 115 | ]; |
| 116 | if (!empty($row['status_id'])) { |
| 117 | $data['description'][0] .= ': ' . CRM_Core_PseudoConstant::getLabel('CRM_Campaign_BAO_Campaign', 'status_id', $row['status_id']); |
| 118 | } |
| 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); |
| 124 | } |
| 125 | $dateString .= ' ' . CRM_Utils_Date::customFormat($row['end_date'], $config->dateformatFull); |
| 126 | } |
| 127 | $data['description'][] = $dateString; |
| 128 | $output[] = $data; |
| 129 | } |
| 130 | } |
| 131 | return $output; |
| 132 | } |