From 76adafe66c644e083de60c2348412c37f35db754 Mon Sep 17 00:00:00 2001 From: Marc Michalsky forumZFD Date: Thu, 11 Feb 2021 18:34:05 +0100 Subject: [PATCH] add search parameters to TwingleCampaign.get --- api/v3/TwingleCampaign/Get.php | 99 +++++++++++++++++++++++++++------- 1 file changed, 79 insertions(+), 20 deletions(-) diff --git a/api/v3/TwingleCampaign/Get.php b/api/v3/TwingleCampaign/Get.php index 4dd91a6..c573d59 100644 --- a/api/v3/TwingleCampaign/Get.php +++ b/api/v3/TwingleCampaign/Get.php @@ -26,10 +26,56 @@ function _civicrm_api3_twingle_campaign_Get_spec(array &$spec) { 'api.required' => 0, 'description' => E::ts('Twingle ID of the parent TwingleProject'), ]; + $spec['name'] = [ + 'name' => 'name', + 'title' => E::ts('Campaign Name'), + 'type' => CRM_Utils_Type::T_STRING, + 'api.required' => 0, + 'description' => E::ts('Name of the Campaign'), + ]; + $spec['title'] = [ + 'name' => 'title', + 'title' => E::ts('Campaign Title'), + 'type' => CRM_Utils_Type::T_STRING, + 'api.required' => 0, + 'description' => E::ts('Title of the Campaign'), + ]; + $spec['start_date'] = [ + 'name' => 'start_date', + 'title' => E::ts('Campaign Start Date'), + 'type' => CRM_Utils_Type::T_STRING, + 'api.required' => 0, + 'description' => E::ts('Date and Time that Campaign starts.'), + ]; + $spec['last_modified_id'] = [ + 'name' => 'last_modified_id', + 'title' => E::ts('Campaign Modified By'), + 'type' => CRM_Utils_Type::T_INT, + 'api.required' => 0, + 'description' => E::ts('FK ti civicrm_contact, who recently edited this campaign'), + 'FKClassName' => 'CRM_Contact_DAO_Contact', + ]; + $spec['last_modified_date'] = [ + 'name' => 'last_modified_date', + 'title' => E::ts('Campaign Modified Date'), + 'type' => CRM_Utils_Type::T_STRING, + 'api.required' => 0, + 'description' => E::ts('FK ti civicrm_contact, who recently edited this campaign'), + ]; + $spec['is_active'] = [ + 'name' => 'is_active', + 'title' => E::ts('Campaign is active'), + 'type' => CRM_Utils_Type::T_BOOLEAN, + 'api.required' => 0, + 'description' => E::ts('Is this Campaign enabled or disabled/cancelled?'), + ]; } /** - * TwingleCampaign.Get API + * # TwingleCampaign.Get API + * Gets TwingleCampaign campaigns by the provided parameters.
+ * If a TwingleProject **project_id** is provided, this API returns all its + * TwingleCampaign children campaigns. * * @param array $params * @@ -49,39 +95,53 @@ function civicrm_api3_twingle_campaign_Get(array $params): array { $returnValues = []; - // If an id is provided, get a single project - if ($params['id']) { - $campaigns = civicrm_api3('Campaign', - 'get', - ['id' => $params['id']] - ); - } + // Get campaign type id for TwingleCampaign + $twingle_campaign_campaign_type_id = + Cache::getInstance() + ->getCampaigns()['campaign_types']['twingle_campaign']['id']; + // If no id but a project_id is provided, get all TwingleCampaign children of // this TwingleProject - elseif ($params['project_id']) { - - // Get campaign type id for TwingleCampaign - $twingle_campaign_campaign_type_id = - Cache::getInstance() - ->getCampaigns()['campaign_types']['twingle_campaign']['id']; + if (array_key_exists('project_id', $params) && $params['project_id']) { + // Get TwingleProject $project = civicrm_api3('TwingleProject', 'get', ['project_id' => $params['project_id']] ); + // Remove 'parent_id' from $params + unset($params['project_id']); + + // Include parent TwingleProject id in $params + $params['parent_id'] = $project['id']; + + // Include campaign type ot TwingleCampaigns in $params + $params['campaign_type_id'] = $twingle_campaign_campaign_type_id; + + // Get TwingleCampaign children campaigns of the TwingleProject $campaigns = civicrm_api3('Campaign', 'get', - [ - 'parent_id' => $project['id'], - 'campaign_type_id' => $twingle_campaign_campaign_type_id - ] + $params + ); + } + // If no 'project_id' is provided, get all TwingleCampaigns by the provided + // $params + else { + + // Include campaign type ot TwingleCampaigns in $params + $params['campaign_type_id'] = $twingle_campaign_campaign_type_id; + + $campaigns = civicrm_api3('Campaign', + 'get', + $params ); } // Translate custom fields if (!empty($campaigns)) { - $custom_field_mapping_reverse = array_flip(Cache::getInstance()->getCustomFieldMapping()); + $custom_field_mapping_reverse = + array_flip(Cache::getInstance()->getCustomFieldMapping()); foreach ($campaigns['values'] as $campaign) { foreach ($campaign as $key => $value) { @@ -103,7 +163,6 @@ function civicrm_api3_twingle_campaign_Get(array $params): array { } return civicrm_api3_create_success($returnValues, $params, 'TwingleCampaign', 'Get'); - } return civicrm_api3_create_success($returnValues, $params, 'TwingleCampaign', 'Get');