From f48584151a2e96fb98071de9e5c3133e4358fcca Mon Sep 17 00:00:00 2001 From: Matthew Wire <mjw@mjwconsult.co.uk> Date: Fri, 5 Feb 2021 23:16:20 +0000 Subject: [PATCH] Code cleanup --- .../Page/Civipostcode.php | 67 +++++------ .../Page/GetAddressIo.php | 19 +++- CRM/Civicrmpostcodelookup/Utils.php | 106 ------------------ 3 files changed, 41 insertions(+), 151 deletions(-) delete mode 100755 CRM/Civicrmpostcodelookup/Utils.php diff --git a/CRM/Civicrmpostcodelookup/Page/Civipostcode.php b/CRM/Civicrmpostcodelookup/Page/Civipostcode.php index 91e3e24..092a9b4 100755 --- a/CRM/Civicrmpostcodelookup/Page/Civipostcode.php +++ b/CRM/Civicrmpostcodelookup/Page/Civipostcode.php @@ -46,12 +46,8 @@ class CRM_Civicrmpostcodelookup_Page_Civipostcode extends CRM_Civicrmpostcodeloo $querystring = self::getCivipostcodeCredentials(1); $querystring = $querystring . "&postcode=" . urlencode($postcode) . "&property=" . $number; - ############### - #File Handling - ############### - - ##Open the JSON Document## - $filetoparse = fopen("$querystring","r") or die("Error reading JSON data."); + // Open the JSON document + $filetoparse = fopen("$querystring", "r") or die("Error reading JSON data."); $data = stream_get_contents($filetoparse); $simpleJSONData = json_decode($data); @@ -59,15 +55,12 @@ class CRM_Civicrmpostcodelookup_Page_Civipostcode extends CRM_Civicrmpostcodeloo if ($simpleJSONData->is_error == 1) { $addresslist[0]['value'] = ''; $addresslist[0]['label'] = $simpleJSONData->error; - } else { + } + else { $addresslist = self::getAddressList($simpleJSONData, $postcode); } } - - // highlight search results - //$addresslist = CRM_Civicrmpostcodelookup_Utils::apply_highlight($addresslist, $postcode); - - ##Close the JSON source## + // Close the JSON source fclose($filetoparse); echo json_encode($addresslist); @@ -82,15 +75,16 @@ class CRM_Civicrmpostcodelookup_Page_Civipostcode extends CRM_Civicrmpostcodeloo $addressLineArray = self::formatAddressLines($addressItem, TRUE); $addressLineArray = array_filter($addressLineArray); - $addressRow["id"] = (string) $addressItem->id; - $addressRow["value"] = $postcode; - $addressRow["label"] = @implode(', ', $addressLineArray); + $addressRow['id'] = (string) $addressItem->id; + $addressRow['value'] = $postcode; + $addressRow['label'] = @implode(', ', $addressLineArray); + $addressRow['lineArray'] = $addressLineArray; array_push($addressList, $addressRow); } if (empty($addressList)) { - $addressRow["id"] = ''; - $addressRow["value"] = ''; + $addressRow['id'] = ''; + $addressRow['value'] = ''; $addressRow["label"] = 'Postcode Not Found'; array_push($addressList, $addressRow); } @@ -102,42 +96,37 @@ class CRM_Civicrmpostcodelookup_Page_Civipostcode extends CRM_Civicrmpostcodeloo * Function to get address details based on the Civipostcode address id */ public static function getaddress() { - $moniker = CRM_Utils_Request::retrieve('id', 'String'); - if (empty($moniker)) { + $selectedId = CRM_Utils_Request::retrieve('id', 'String'); + if (empty($selectedId)) { exit; } - $address = self::getAddressByMoniker($moniker); - $response = [ - 'address' => $address - ]; - - echo json_encode($response); - exit; - } - - private static function getAddressByMoniker($moniker) { $querystring = self::getCivipostcodeCredentials(2); - $querystring = $querystring . "&id=" . urlencode($moniker); - - ############### - #File Handling - ############### + $querystring = $querystring . "&id=" . urlencode($selectedId); - ##Open the JSON Document## + // Open the JSON Document $filetoparse = fopen("$querystring","r") or die("Error reading JSON data."); $data = stream_get_contents($filetoparse); $simpleJSONData = json_decode($data); $addressObj = $simpleJSONData->results[0]; - $address = self::formatAddressLines($addressObj); - - ##Close the JSON source## + // Close the JSON source fclose($filetoparse); - return $address; + $response = [ + 'address' => $address + ]; + + echo json_encode($response); + exit; } + /** + * @param Object $addressObj + * @param bool $forList + * + * @return array|void + */ private static function formatAddressLines($addressObj, $forList = FALSE) { if (empty($addressObj)) { return; diff --git a/CRM/Civicrmpostcodelookup/Page/GetAddressIo.php b/CRM/Civicrmpostcodelookup/Page/GetAddressIo.php index d7d004b..855a6cf 100644 --- a/CRM/Civicrmpostcodelookup/Page/GetAddressIo.php +++ b/CRM/Civicrmpostcodelookup/Page/GetAddressIo.php @@ -9,6 +9,7 @@ class CRM_Civicrmpostcodelookup_Page_GetAddressIo extends CRM_Civicrmpostcodeloo } return parent::isValidPostcode($postcode); } + /* * Function to get address list based on a Post code */ @@ -195,23 +196,29 @@ class CRM_Civicrmpostcodelookup_Page_GetAddressIo extends CRM_Civicrmpostcodeloo $addressLineArray = self::formatAddressLines($addressId, $addressItem); $addressLineArray['postcode'] = $postcode; - $addressRow["id"] = $addressId; - $addressRow["value"] = $postcode; - $addressRow["label"] = @implode(', ', $addressLineArray); + $addressRow['id'] = $addressId; + $addressRow['value'] = $postcode; + $addressRow['label'] = @implode(', ', $addressLineArray); $addressRow['lineArray'] = $addressLineArray; array_push($addressList, $addressRow); } if (empty($addressList)) { - $addressRow["id"] = ''; - $addressRow["value"] = ''; - $addressRow["label"] = 'Postcode Not Found'; + $addressRow['id'] = ''; + $addressRow['value'] = ''; + $addressRow['label'] = 'Postcode Not Found'; array_push($addressList, $addressRow); } return $addressList; } + /** + * @param string $addressId + * @param array $addressItem + * + * @return array|void + */ private static function formatAddressLines($addressId, $addressItem) { if (empty($addressItem)) { return; diff --git a/CRM/Civicrmpostcodelookup/Utils.php b/CRM/Civicrmpostcodelookup/Utils.php deleted file mode 100755 index bbc1929..0000000 --- a/CRM/Civicrmpostcodelookup/Utils.php +++ /dev/null @@ -1,106 +0,0 @@ -<?php - -require_once 'CRM/Core/Page.php'; - -class CRM_Civicrmpostcodelookup_Utils { - /** - * mb_stripos all occurences - * based on http://www.php.net/manual/en/function.strpos.php#87061 - * - * Find all occurrences of a needle in a haystack - * - * @param string $haystack - * @param string $needle - * @return array or false - */ - public static function mb_stripos_all($haystack, $needle) { - - $s = 0; - $i = 0; - - while(is_integer($i)) { - - $i = mb_stripos($haystack, $needle, $s); - - if(is_integer($i)) { - $aStrPos[] = $i; - $s = $i + mb_strlen($needle); - } - } - - if(isset($aStrPos)) { - return $aStrPos; - } else { - return false; - } - } - - /** - * Apply highlight to row label - * - * @param string $a_json json data - * @param array $parts strings to search - * @return array - */ - public static function apply_highlight($a_json, $parts) { - - $p = count($parts); - $rows = count($a_json); - - for($row = 0; $row < $rows; $row++) { - - $label = $a_json[$row]["label"]; - $a_label_match = []; - - for($i = 0; $i < $p; $i++) { - - $part_len = mb_strlen($parts[$i]); - $a_match_start = self::mb_stripos_all($label, $parts[$i]); - - foreach($a_match_start as $part_pos) { - - $overlap = false; - foreach($a_label_match as $pos => $len) { - if($part_pos - $pos >= 0 && $part_pos - $pos < $len) { - $overlap = true; - break; - } - } - if(!$overlap) { - $a_label_match[$part_pos] = $part_len; - } - - } - - } - - if(count($a_label_match) > 0) { - ksort($a_label_match); - - $label_highlight = ''; - $start = 0; - $label_len = mb_strlen($label); - - foreach($a_label_match as $pos => $len) { - if($pos - $start > 0) { - $no_highlight = mb_substr($label, $start, $pos - $start); - $label_highlight .= $no_highlight; - } - $highlight = '<span class="hl_results">' . mb_substr($label, $pos, $len) . '</span>'; - $label_highlight .= $highlight; - $start = $pos + $len; - } - - if($label_len - $start > 0) { - $no_highlight = mb_substr($label, $start); - $label_highlight .= $no_highlight; - } - - $a_json[$row]["label"] = $label_highlight; - } - - } - return $a_json; - } - -} -- GitLab