diff --git a/CRM/TwingleCampaign/BAO/CustomField.php b/CRM/TwingleCampaign/BAO/CustomField.php index 9aaab9b..7af8028 100644 --- a/CRM/TwingleCampaign/BAO/CustomField.php +++ b/CRM/TwingleCampaign/BAO/CustomField.php @@ -1,7 +1,6 @@ getCampaigns()['custom_fields']; + + // Get json file with all custom fields for this extension + $json_file = file_get_contents(E::path() . + '/CRM/TwingleCampaign/resources/campaigns.json'); + $campaign_info = json_decode($json_file, TRUE); + + // Log an error and throw an exception if the file cannot get read + if (!$campaign_info) { + Civi::log()->error("Could not read json file"); + throw new Exception('Could not read json file'); + } // Recursive method call with all custom field names from the json file - foreach ($customFields as $customField) { + foreach ($campaign_info['custom_fields'] as $customField) { $result[] = self::fetch($customField['name']); } return $result; @@ -171,7 +179,12 @@ class CRM_TwingleCampaign_BAO_CustomField { 'name' => $name, ] ); - return new self(array_shift($custom_field['values'])); + if ($custom_field = array_shift($custom_field['values'])) { + return new self($custom_field); + } + else { + return NULL; + } } catch (CiviCRM_API3_Exception $e) { return NULL; } diff --git a/CRM/TwingleCampaign/BAO/CustomGroup.php b/CRM/TwingleCampaign/BAO/CustomGroup.php index e1a77ae..5bb1361 100644 --- a/CRM/TwingleCampaign/BAO/CustomGroup.php +++ b/CRM/TwingleCampaign/BAO/CustomGroup.php @@ -1,5 +1,7 @@ getOptionValues(); + + // Get json file with all custom fields for this extension + $json_file = file_get_contents(E::path() . + '/CRM/TwingleCampaign/resources/option_values.json'); + $option_values = json_decode($json_file, TRUE); + + // Log an error and throw an exception if the file cannot get read + if (!$option_values) { + Civi::log()->error("Could not read json file"); + throw new Exception('Could not read json file'); + } // Recursive method call with all custom field names from the json file - foreach ($optionValues as $optionValue) { + foreach ($option_values as $optionValue) { $result[] = self::fetch($optionValue['name']); } return $result; @@ -150,7 +159,12 @@ class CRM_TwingleCampaign_BAO_OptionValue { 'name' => $name, ] ); - return new self(array_shift($option_value['values'])); + if ($option_value = array_shift($option_value['values'])) { + return new self($option_value); + } + else { + return NULL; + } } catch (CiviCRM_API3_Exception $e) { return NULL; }