funder_field-1.1.0-alpha1/src/Controller/FunderFieldJsonApiController.php
src/Controller/FunderFieldJsonApiController.php
<?php
namespace Drupal\funder_field\Controller;
use Drupal\Component\Utility\Xss;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
class FunderFieldJsonApiController
{
//When users type something into the Funder field, this will do auto complete
public function handleAutocomplete(Request $request)
{
$results = [];
$no_of_results = 0;
$max_funders_to_display = 20;
$input = $request->query->get('q');
if (!$input) {
return new JsonResponse($results);
}
$input = Xss::filter($input);
$client = \Drupal::httpClient();
$response2 = \Drupal::httpClient()
->get('https://api.crossref.org/funders?query=' .$input);
$json_string = (string) $response2->getBody();
$json = json_decode($json_string,TRUE);
$no_of_results =(string)$json["message"]["total-results"];
$max_funders_to_display_config = \Drupal::config('funder_field.settings')->get('funder_items_depth');
if (!is_null($max_funders_to_display_config))
$max_funders_to_display = $max_funders_to_display_config;
if($no_of_results > $max_funders_to_display) {
$no_of_results = $max_funders_to_display;
}
for ($x = 0; $x < $no_of_results; $x++) {
$name=(string)$json["message"]["items"][$x]["name"];
$id=(String)$json["message"]["items"][$x]["id"];
$results[] = [
'value' => $name . " ( id: " . $id . " )",
'label' => $name,
];
}
return new JsonResponse($results);
}
//When users type something into the Award field, this will do auto complete
public function handleAutocompleteAward(Request $request, $funder_id)
{
$results = [];
$no_of_results = 0;
$max_funders_to_display = 20;
$input = $request->query->get('q');
if (!$input || empty($funder_id)) {
return new JsonResponse($results);
}
$input = Xss::filter($input);
//https://api.crossref.org/funders/100000001/works?query=ACI-15476&rows=1000&offset=1000&select=funder
$time1 = date("Y-m-d H:i:s");
$offset = 0;
$limit = 4;
//$response2 = \Drupal::httpClient()->get('https://api.crossref.org/funders/'.$funder_id.'/works?query=funder,'.$input.'&rows=1000&select=funder&offset='.$offset);
\Drupal::logger('FunderField')->info('https://api.crossref.org/funders/'.$funder_id.'/works?query=award,number,'.$input.'&rows=500&select=funder&offset='.$offset);
$response2 = \Drupal::httpClient()->get('https://api.crossref.org/funders/'.$funder_id.'/works?query=award,number,'.$input.'&rows=500&select=funder&offset='.$offset);
//$response2 = \Drupal::httpClient()->get('https://api.crossref.org/funders/'.$funder_id.'/works?query='.$input.'&rows=1000&select=funder&cursor=*');
$time2 = date("Y-m-d H:i:s");
//ksm(strtotime($time2) - strtotime($time1));
$json_string = (string) $response2->getBody();
$count = 1;
while (substr_count($json_string, $input) <= 1) {
$offset += 500;
$json = json_decode($json_string,TRUE);
//$next_cursor = $json["message"]["next-cursor"];
$response2 = \Drupal::httpClient()->get('https://api.crossref.org/funders/'.$funder_id.'/works?query='.$input.'&rows=500&select=funder&offset='.$offset);
//$response2 = \Drupal::httpClient()->get('https://api.crossref.org/funders/'.$funder_id.'/works?query='.$input.'&rows=1000&select=funder&cursor='.$next_cursor);
$count++;
$json_string = (string) $response2->getBody();
if (substr_count($json_string, $input) <= 1) {
if ($count >= 4)
return new JsonResponse($results);
}
else
break;
}
$time3 = date("Y-m-d H:i:s");
//ksm(strtotime($time3) - strtotime($time2));
$json = json_decode($json_string,TRUE);
$time4 = date("Y-m-d H:i:s");
//ksm(strtotime($time4) - strtotime($time3));
$no_of_results = $no_of_returns =count($json["message"]["items"]);
$max_funders_to_display_config = \Drupal::config('funder_field.settings')->get('funder_items_depth');
if (!is_null($max_funders_to_display_config))
$max_funders_to_display = $max_funders_to_display_config;
if($no_of_results > $max_funders_to_display) {
$no_of_results = $max_funders_to_display;
}
$entry_count = 0;
for ($x = 0; $x < $no_of_returns; $x++) {
if (!array_key_exists("funder", $json["message"]["items"][$x]))
continue;
$funders = $json["message"]["items"][$x]["funder"];
$funder_count = count($funders);
for ($y = 0; $y < $funder_count; ++$y) {
if (!array_key_exists("award", $funders[$y]))
continue;
$awards = $funders[$y]["award"];
$no_awards = count($awards);
for ($k = 0; $k < $no_awards; ++$k) {
$values = array_column($results, 'value');
if (!in_array($awards[$k], $values) && str_contains($awards[$k], $input)) {
$results[] = [
'value' => $awards[$k],
'label' => $awards[$k],
];
++$entry_count;
if ($entry_count >= $no_of_results)
break;
}
}
if ($entry_count >= $no_of_results)
break;
}
if ($entry_count >= $no_of_results)
break;
}
$time5 = date("Y-m-d H:i:s");
//ksm(strtotime($time5) - strtotime($time4));
return new JsonResponse($results);
}
}
