Skip to content

Commit

Permalink
Adapt code to PHP 8.1: Check if objects exist before accessing and cr…
Browse files Browse the repository at this point in the history
…eate properties explicitly.
  • Loading branch information
oliver-stoehr committed Jul 11, 2024
1 parent abd4200 commit b425bc4
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 39 deletions.
117 changes: 84 additions & 33 deletions module/AvailabilityPlus/src/AvailabilityPlus/Resolver/Driver/DAIA.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace AvailabilityPlus\Resolver\Driver;

use stdClass;
use VuFind\Config\SearchSpecsReader;

class DAIA extends AvailabilityPlusResolver
Expand Down Expand Up @@ -86,8 +87,10 @@ public function parseLinks($data_org)
$record->service = $service_key;
switch($service_key) {
case 'openaccess':
$record->daia_action = (object)'';
if(!in_array($service_content->href, $urls) || !$this->resolverConfig->hide_url_duplicates) {
if (!isset($record->daia_action)) {
$record->daia_action = new stdClass;
}
$record->daia_action->level = 'FreeAccess link_external';
if (!empty($service_content->title)) $record->daia_action->title = $service_content->title;
$record->daia_action->label = 'FreeAccess';
Expand All @@ -106,8 +109,10 @@ public function parseLinks($data_org)
$this->parsed_data->document[0]->item[$key]->availabilityplus->daia_action_array[] = $record->daia_action;
}
case 'remote':
$record->daia_action = (object)'';
if(!in_array($service_content->href, $urls) || !$this->resolverConfig->hide_url_duplicates) {
if (!isset($record->daia_action)) {
$record->daia_action = new stdClass;
}
$record->daia_action->level = 'LicensedAccess link_external';
if (!empty($service_content->title)) $record->daia_action->title = $service_content->title;
$record->daia_action->label = 'LicensedAccess';
Expand All @@ -132,50 +137,40 @@ public function parseLinks($data_org)
}
case 'loan':
case 'presentation':
$record->storage = (object)'';
$record->daia_hint = (object)'';
$record->daia_action = (object)'';
if(empty($item_services['available']['openaccess']) && empty($item_services['available']['remote'])) {
if($service_key == 'loan') {
$record->score = 20;
} else {
$record->score = 30;
}
if(!empty($item->storage->href)){
$record->storage->level = 'link_external';
$record->storage->label = $item->storage->content;
$record->storage->url = $item->storage->href;
$this->setStorage($record, 'link_external', $item->storage->content, $item->storage->href);
} elseif(!empty($item->storage->id)){
$record->storage->level = 'link_external';
$record->storage->label = $item->storage->content;
$record->storage->url = $item->storage->id;
$this->setStorage($record, 'link_external', $item->storage->content, $item->storage->id);
} else {
$record->storage->label = 'unknown_location';
$this->setStorage($record, null, 'unknown_location', null);
}
if(!empty($item->label)) $record->callnumber = $item->label;
if(!empty($service_content->limitation[0]->id)) {
$limitation = str_replace(' ','', substr($service_content->limitation[0]->id, strpos($service_content->limitation[0]->id, "#") + 1));
$record->daia_hint->level = $limitation;
$record->daia_hint->label = $service_content->service.$limitation;
$this->setDaiaHint($record, $limitation, $service_content->service.$limitation, null);
$record->score += 5;
} elseif(!empty($service_content->limitation[0]->content)) {
$limitation = str_replace(' ','',$service_content->limitation[0]->content);
$record->daia_hint->level = $limitation;
$record->daia_hint->label = $service_content->service.$limitation;
$this->setDaiaHint($record, $limitation, $service_content->service.$limitation, null);
$record->score += 5;
} elseif(!empty($service_content->expected)) {
$record->daia_hint->level = "daia_orange";
$date = date_create($service_content->expected);
$record->daia_hint->label = 'on_loan_until';
$record->daia_hint->label_date = date_format($date,"d.m.Y");
$this->setDaiaHint($record, "daia_orange", 'on_loan_until', date_format($date,"d.m.Y"));
$record->score += 20;
} elseif(isset($service_content->queue)) {
$record->daia_hint->level = "daia_orange";
$record->daia_hint->label = 'on_loan';
$this->setDaiaHint($record, "daia_orange", 'on_loan', null);
$record->score += 20;
} else {
$record->daia_hint->level = "daia_green";
$record->daia_hint->label = $service_content->service;
$this->setDaiaHint($record, "daia_green", $service_content->service, null);
}
if (!isset($record->daia_action)) {
$record->daia_action = new stdClass;
}
if(!empty($service_content->href)) {
$record->daia_action->level = 'internal_link';
Expand All @@ -189,7 +184,9 @@ public function parseLinks($data_org)
$record->daia_action->label = $service_content->service.'_default_action'.$limitation;
}
if(isset($service_content->queue)) {
$record->queue = (object)'';
if (!isset($record->queue)) {
$record->queue = new stdClass;
}
$record->queue->length = $service_content->queue;
if($service_content->queue == 1) {
$record->queue->label .= 'Recall';
Expand All @@ -209,20 +206,14 @@ public function parseLinks($data_org)
break;
}
case 'fallback':
$record->storage = (object)'';
$record->daia_hint = (object)'';
$record->daia_action = (object)'';
if(empty($item_services['available']['openaccess']) && empty($item_services['available']['remote']) && empty($item_services['available']['loan']) && empty($item_services['available']['presentation'])) {
if(!empty($item->storage->id)){
$record->storage->level = 'link_external';
$record->storage->label = $item->storage->content;
$record->storage->url = $item->storage->id;
$this->setStorage($record, 'link_external', $item->storage->content, $item->storage->id);
} else {
$record->storage->label = 'unknown_location';
$this->setStorage($record, null, 'unknown_location', null);
}
if(!empty($item->label)) $record->callnumber = $item->label;
$record->daia_hint->level = 'daia_red';
$record->daia_hint->label = 'not_available';
$this->setDaiaHint($record, 'daia_red', 'not_available', null);
if(!empty($item->about)) {
$record->about = $item->about;
}
Expand Down Expand Up @@ -250,6 +241,66 @@ public function parseLinks($data_org)
return $response;
}

/**
* Add `level`, `label` and `url` parameters to the `storage` property on the given object.
* Creates a new object and `storage` property if not present.
* `level`, `label` and `url` are only added to the object when the variables are set and not null, otherwise
* they are ignored.
* @param $obj stdClass The object that the storage will be added to.
* @param $level string
* @param $label string
* @param $url string
* @return void
*/
private function setStorage($obj, $level, $label, $url): void
{
if (!isset($obj)) {
$obj = new stdClass;
}
if (!isset($obj->storage)) {
$obj->storage = new stdClass;
}
if (isset($level)) {
$obj->storage->level = $level;
}
if (isset($label)) {
$obj->storage->label = $label;
}
if (isset($url)) {
$obj->storage->url = $url;
}
}

/**
* Add `level`, `label` and `label_date` parameters to the `daia_hint` property on the given object.
* Creates a new object and `daia_hint` property if not present.
* `level`, `label` and `label_date` are only added to the object when the variables are set and not null, otherwise
* they are ignored.
* @param $obj stdClass The object that the storage will be added to.
* @param $level string
* @param $label string
* @param $label_date string
* @return void
*/
private function setDaiaHint($obj, $level, $label, $label_date): void
{
if (!isset($obj)) {
$obj = new stdClass;
}
if (!isset($obj->daia_hint)) {
$obj->daia_hint = new stdClass;
}
if (isset($level)) {
$obj->daia_hint->level = $level;
}
if (isset($label)) {
$obj->daia_hint->label = $label;
}
if (isset($label_date)) {
$obj->daia_hint->url = $label_date;
}
}

protected function applyCustomChanges() {

$specsReader = new SearchSpecsReader();
Expand Down
3 changes: 1 addition & 2 deletions themes/availabilityplus/templates/ajax/link-daia.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
$items = $data->document[0];

if(in_array($mode, array('break_on_first', 'break_on_first_next', 'always_break_on_first')) && $data->best_item) {
unset($items);
$items->item[0] = $data->best_item;
$items = (object) array('item' => array($data->best_item));
}

foreach($items->item as $key => $item) {
Expand Down
3 changes: 1 addition & 2 deletions themes/belugax_hcu/templates/ajax/link-daia.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ if ($mediatype == 'Journal' && $list) {
$items = $data->document[0];

if($mode == 'break_on_first' && $data->best_item) {
unset($items);
$items->item[0] = $data->best_item;
$items = (object) array('item' => array($data->best_item));
}

foreach($items->item as $key => $item) {
Expand Down
3 changes: 1 addition & 2 deletions themes/belugax_hcu_dev/templates/ajax/link-daia.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ if ($mediatype == 'Journal' && $list) {
$items = $data->document[0];

if($mode == 'break_on_first' && $data->best_item) {
$items = (object)'';
$items->item[0] = $data->best_item;
$items = (object) array('item' => array($data->best_item));
}

foreach($items->item as $key => $item) {
Expand Down

0 comments on commit b425bc4

Please sign in to comment.