Skip to content

Commit

Permalink
api fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nczirjak-acdh committed Oct 1, 2021
1 parent 40d8638 commit 82f351f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/Controller/ArcheApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ public function repo_getRootTable(string $lng = 'en')

if (count($this->modelData) == 0) {
$response->setContent('No data!');
$response->setStatusCode(200);
$response->setStatusCode(400);
}

$this->result = $this->helper->createView($this->modelData, 'rootTable', $lng);
Expand Down
27 changes: 27 additions & 0 deletions src/Controller/DisseminationServicesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,34 @@ public function generateView(string $identifier, string $dissemination, array $a
return $this->basicViewData = $this->helper->createView($vd, $dissemination, $identifier, $additionalData);
}

/**
* get the collection binaries
* @param string $repoid
* @return Response
*/
public function repo_dl_collection_binaries(string $repoid): Response
{
$GLOBALS['resTmpDir'] = "";
$response = new Response();
$response->headers->set('Content-Type', 'application/json');

//the binary files
$binaries = $this->generalFunctions->jsonDecodeData($_POST['jsonData']);
if (count($binaries) == 0) {
$response->setContent(json_encode(""));
return $response;
}

($_POST['username']) ? $username = $_POST['username'] : $username = '';
($_POST['password']) ? $password = $_POST['password'] : $password = '';

$fileLocation = $this->helper->collectionDownload($binaries, $repoid, $username, $password);
if (empty($fileLocation)) {
$response->setStatusCode(400);
}
$response->setContent(json_encode($fileLocation));
return $response;
}

public function get_collection_data_lazy(string $id): Response
{
Expand Down
83 changes: 45 additions & 38 deletions src/Helper/ArcheApiHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
*
* @author norbertczirjak
*/
class ArcheApiHelper
{
class ArcheApiHelper {

use \Drupal\acdh_repo_gui\Traits\ArcheUtilTrait;

private $data = array();
Expand All @@ -23,10 +23,9 @@ class ArcheApiHelper
private $properties;
private $requiredClasses = array();

public function createView(array $data = array(), string $apiType = '', string $lng = 'en'): array
{
public function createView(array $data = array(), string $apiType = '', string $lng = 'en'): array {
(!empty($lng)) ? $this->siteLang = strtolower($lng) : $this->siteLang = "en";
if (count((array)$data) == 0 && !empty($apiType)) {
if (count((array) $data) == 0 && !empty($apiType)) {
return array();
}

Expand Down Expand Up @@ -79,8 +78,7 @@ public function createView(array $data = array(), string $apiType = '', string $
/**
* Create the inverse result for the datatable
*/
private function formatInverseData()
{
private function formatInverseData() {
$this->result = array();
foreach ($this->data as $id => $obj) {
foreach ($obj as $o) {
Expand All @@ -96,8 +94,7 @@ private function formatInverseData()
/**
* Format the sql result to the getMembers api endpoint
*/
private function formatMembersData()
{
private function formatMembersData() {
$this->result = array();
foreach ($this->data as $obj) {
$this->result[] = array("<a id='archeHref' href='/browser/oeaw_detail/$obj->id'>$obj->title</a>");
Expand All @@ -107,8 +104,7 @@ private function formatMembersData()
/**
* Format the sql result to the Related Publications and Resources api endpoint
*/
private function formatRPRData()
{
private function formatRPRData() {
$this->result = array();
foreach ($this->data as $obj) {
$this->result[] = array(
Expand All @@ -123,10 +119,9 @@ private function formatRPRData()
* Create the reponse header
* @param array $data
*/
private function setupMetadataType(array $data = array())
{
private function setupMetadataType(array $data = array()) {
$this->creatMetadataObj($data);
if (count((array)$data['properties']) > 0) {
if (count((array) $data['properties']) > 0) {
$this->data = $data['properties'];
}

Expand All @@ -140,8 +135,7 @@ private function setupMetadataType(array $data = array())
/**
* format the collections and binaries count response
*/
private function formatCollsBinsCount()
{
private function formatCollsBinsCount() {
$this->result['$schema'] = "http://json-schema.org/draft-07/schema#";
$collections = "0";
$files = "0";
Expand All @@ -164,25 +158,42 @@ private function formatCollsBinsCount()
/**
* Format the data for the metadata api request
*/
private function formatMetadataView()
{
private function formatMetadataView() {
foreach ($this->data as $v) {
$prop = str_replace('https://vocabs.acdh.oeaw.ac.at/schema#', '', $v->property);

if (isset($v->label)) {
$prop = "";
if (is_array($v->property)) {
foreach ($v->property as $key => $value) {
if (strpos($value, 'https://vocabs.acdh.oeaw.ac.at/schema#') !== false) {
$prop = str_replace('https://vocabs.acdh.oeaw.ac.at/schema#', '', $value);
}
}
}else {
$prop = str_replace('https://vocabs.acdh.oeaw.ac.at/schema#', '', $v->property);
}

if (isset($v->label) && isset($v->label[$this->siteLang])) {
$this->result['properties'][$prop]['title'] = $v->label[$this->siteLang];
}
if (isset($v->comment)) {
if (isset($v->comment) && isset($v->comment[$this->siteLang])) {

$this->result['properties'][$prop]['description'] = $v->comment[$this->siteLang];
$this->result['properties'][$prop]['attrs']['placeholder'] = $v->comment[$this->siteLang];
}
if (isset($v->range)) {
$this->result['properties'][$prop]['items']['range'] = $v->range;
if (strpos($v->range, 'string') !== false) {
$range = "";
$rangeUrl = "";
foreach ($v->range as $key => $value) {
if (strpos($value, 'http://www.w3.org/2001/XMLSchema#') !== false) {
$range = str_replace('http://www.w3.org/2001/XMLSchema#', '', $value);
$rangeUrl = $value;
}
}
$this->result['properties'][$prop]['items']['range'] = $rangeUrl;
if (strpos($range, 'string') !== false) {
$this->result['properties'][$prop]['items']['type'] = "string";
$this->result['properties'][$prop]['type'] = "string";
}
if (strpos($v->range, 'array') !== false) {
if (strpos($range, 'array') !== false) {
$this->result['properties'][$prop]['items']['type'] = "array";
$this->result['properties'][$prop]['type'] = "array";
}
Expand All @@ -201,7 +212,7 @@ private function formatMetadataView()
$this->result['properties'][$prop]['recommendedClass'] = $v->recommendedClass;
}
}
if (count((array)$this->requiredClasses) > 0) {
if (count((array) $this->requiredClasses) > 0) {
$this->result['required'] = $this->requiredClasses;
}
}
Expand All @@ -212,8 +223,7 @@ private function formatMetadataView()
* @param array $data
* @return string
*/
private function checkCardinality(string $prop, object $obj)
{
private function checkCardinality(string $prop, object $obj) {
if (isset($obj->min)) {
$this->result['properties'][$prop]['minItems'] = (int) $obj->min;
if ($obj->min >= 1) {
Expand Down Expand Up @@ -247,8 +257,7 @@ private function checkCardinality(string $prop, object $obj)
* Create properties obj with values from the metadata api request
* @param array $data
*/
private function creatMetadataObj(array $data)
{
private function creatMetadataObj(array $data) {
$this->properties = new \stdClass();

if (isset($data['class'])) {
Expand All @@ -265,8 +274,7 @@ private function creatMetadataObj(array $data)
/**
* Format the basic APi views
*/
private function formatView()
{
private function formatView() {
$this->result = array();
foreach ($this->data as $k => $val) {
foreach ($val as $v) {
Expand All @@ -277,7 +285,7 @@ private function formatView()
if ($v->property == 'https://vocabs.acdh.oeaw.ac.at/schema#hasAlternativeTitle') {
$altTitle = $v->value;
}

$this->result[$k] = new \stdClass();
$this->result[$k]->title[$lang] = $title;
$this->result[$k]->uri = $this->repo->getBaseUrl() . $k;
Expand All @@ -292,8 +300,7 @@ private function formatView()
/**
* Format the checkIdentifier api call result
*/
private function formatCheckIdentifierData()
{
private function formatCheckIdentifierData() {
$this->result = array();
foreach ($this->data as $val) {
if ($val->property == 'https://vocabs.acdh.oeaw.ac.at/schema#hasAvailableDate') {
Expand All @@ -311,14 +318,13 @@ private function formatCheckIdentifierData()
/**
* create the GNDfile for the GND API
*/
private function createGNDFile()
{
private function createGNDFile() {
$host = str_replace('http://', 'https://', \Drupal::request()->getSchemeAndHttpHost() . '/browser/oeaw_detail/');
$fileLocation = \Drupal::request()->getSchemeAndHttpHost() . '/browser/sites/default/files/beacon.txt';

$this->result = array();

if (count((array)$this->data) > 0) {
if (count((array) $this->data) > 0) {
$resTxt = "";
foreach ($this->data as $val) {
$resTxt .= $val->gnd . "|" . $host . $val->repoid . " \n";
Expand All @@ -333,4 +339,5 @@ private function createGNDFile()
}
}
}

}

0 comments on commit 82f351f

Please sign in to comment.