tethys/app/Library/Xml/DatasetExtension.php

355 lines
12 KiB
PHP
Raw Permalink Normal View History

2018-08-06 12:30:51 +00:00
<?php
namespace App\Library\Xml;
2018-08-30 08:04:25 +00:00
use App\Models\Title;
2019-01-22 17:24:18 +00:00
use App\Models\Description;
2018-09-10 13:09:10 +00:00
use App\Models\License;
use App\Models\Person;
use App\Models\File;
2019-03-29 17:29:20 +00:00
use App\Models\Coverage;
use App\Models\DatasetIdentifier;
2019-03-18 13:32:29 +00:00
use App\Models\Subject;
use App\Models\DatasetReference;
2018-08-30 08:04:25 +00:00
2018-08-06 12:30:51 +00:00
/**
* DatasetExtension short summary.
*
* DatasetExtension description.
*
* @version 1.0
* @author kaiarn
*/
trait DatasetExtension
{
2018-09-10 13:09:10 +00:00
protected $externalFields = array(
2018-08-06 12:30:51 +00:00
'TitleMain' => array(
2018-08-30 08:04:25 +00:00
'model' => Title::class,
'options' => array('type' => ['Main']),
'fetch' => 'eager'
),
'TitleAdditional' => array(
'model' => Title::class,
'options' => array('type' => ['Alternative', 'Sub', 'Translated', 'Other']),
2018-08-06 12:30:51 +00:00
'fetch' => 'eager'
),
'TitleAbstract' => array(
2019-01-22 17:24:18 +00:00
'model' => Description::class,
2022-02-04 16:07:30 +00:00
'options' => array('type' => ['Abstract', 'Translated']),
'fetch' => 'eager'
),
'TitleAbstractAdditional' => array(
'model' => Description::class,
2022-02-04 16:07:30 +00:00
'options' => array('type' => ['Methods', 'Technical_info', 'Series_information', 'Other']),
2018-08-06 12:30:51 +00:00
'fetch' => 'eager'
),
'Licence' => array(
2018-09-10 13:09:10 +00:00
'model' => License::class,
2018-08-06 12:30:51 +00:00
'through' => 'link_documents_licences',
'relation' => 'licenses',
'fetch' => 'eager'
),
'PersonAuthor' => array(
2018-09-10 13:09:10 +00:00
'model' => Person::class,
2018-08-06 12:30:51 +00:00
'through' => 'link_documents_persons',
'pivot' => array('role' => 'author', 'sort_order' => 'sort_order'),
2018-08-06 12:30:51 +00:00
//'sort_order' => array('sort_order' => 'ASC'), // <-- We need a sorted authors list.
//'sort_field' => 'SortOrder',
2019-04-17 14:01:38 +00:00
'relation' => 'persons',
2018-08-06 12:30:51 +00:00
'fetch' => 'eager'
),
'PersonContributor' => array(
2018-09-10 13:09:10 +00:00
'model' => Person::class,
2018-08-06 12:30:51 +00:00
'through' => 'link_documents_persons',
'pivot' => array('role' => 'contributor', 'contributor_type' => 'contributor_type', 'sort_order' => 'sort_order'),
2018-08-06 12:30:51 +00:00
// 'sort_order' => array('sort_order' => 'ASC'), // <-- We need a sorted authors list.
//'sort_field' => 'SortOrder',
2019-04-17 14:01:38 +00:00
'relation' => 'persons',
2018-08-06 12:30:51 +00:00
'fetch' => 'eager'
),
'Reference' => array(
'model' => DatasetReference::class,
'relation' => 'references',
'fetch' => 'eager'
),
'Identifier' => array(
'model' => DatasetIdentifier::class,
'relation' => 'identifier',
'fetch' => 'eager'
),
2019-03-18 13:32:29 +00:00
'Subject' => array(
'model' => Subject::class,
'through' => 'link_dataset_subjects',
2019-03-18 13:32:29 +00:00
'relation' => 'subjects',
'fetch' => 'eager'
),
2018-08-06 12:30:51 +00:00
'File' => array(
2018-09-10 13:09:10 +00:00
'model' => File::class,
2018-08-06 12:30:51 +00:00
'relation' => 'files',
'fetch' => 'eager'
),
2019-03-29 17:29:20 +00:00
// 'GeolocationBox' => array(
// 'model' => GeolocationBox::class,
// 'relation' => 'geolocation',
// 'fetch' => 'eager'
// ),
'Coverage' => array(
'model' => Coverage::class,
'relation' => 'coverage',
2018-12-17 16:10:17 +00:00
'fetch' => 'eager'
),
2018-08-06 12:30:51 +00:00
);
2018-09-10 13:09:10 +00:00
protected $internalFields = array();
2018-08-06 12:30:51 +00:00
2018-09-10 13:09:10 +00:00
protected $fields = array();
2018-08-06 12:30:51 +00:00
2019-01-24 15:56:39 +00:00
protected function initFields()
2018-08-06 12:30:51 +00:00
{
$fields = array(
"Id",
"PublisherName",
"PublishId",
2018-08-06 12:30:51 +00:00
"ContributingCorporation",
"CreatingCorporation",
"Language",
"PublishedDate", "PublishedYear",
"PublisherName", "PublisherPlace",
"PublicationState",
"EmbargoDate", "CreatedAt",
2018-08-06 12:30:51 +00:00
"ServerDateModified",
"ServerDatePublished",
"ServerDateDeleted",
"ServerState",
"Type",
"BelongsToBibliography",
"EmbargoDate"
);
foreach ($fields as $fieldname) {
$field = new Field($fieldname);
$this->addField($field);
}
2018-09-10 13:09:10 +00:00
foreach (array_keys($this->externalFields) as $fieldname) {
2018-08-06 12:30:51 +00:00
$field = new Field($fieldname);
$field->setMultiplicity('*');
$this->addField($field);
}
// Initialize available date fields and set up date validator
// if the particular field is present
$dateFields = array(
'EmbargoDate', 'CreatedAt', 'PublishedDate',
'ServerDatePublished', 'ServerDateDeleted', 'EmbargoDate'
2018-08-06 12:30:51 +00:00
);
foreach ($dateFields as $fieldName) {
$this->getField($fieldName)
->setValueModelClass('Carbon');
}
// $this->_fetchValues();
}
/**
* Get a list of all fields attached to the model. Filters all fieldnames
2018-09-10 13:09:10 +00:00
* that are defined to be inetrnal in $internalFields.
2018-08-06 12:30:51 +00:00
*
2018-09-10 13:09:10 +00:00
* @see Opus_Model_Abstract::internalFields
2018-08-06 12:30:51 +00:00
* @return array List of fields
*/
public function describe()
{
2018-09-10 13:09:10 +00:00
return array_diff(array_keys($this->fields), $this->internalFields);
2018-08-06 12:30:51 +00:00
}
public function addField(Field $field)
{
$fieldname = $field->getName();
2018-09-10 13:09:10 +00:00
if (isset($fieldname, $this->externalFields[$fieldname])) {
$options = $this->externalFields[$fieldname];
2018-08-06 12:30:51 +00:00
// set ValueModelClass if a through option is given
if (isset($options['model'])) {
$field->setValueModelClass($options['model']);
}
// set LinkModelClass if a through option is given
//if (isset($options['through']))
//{
// $field->setLinkModelClass($options['through']);
//}
}
2018-09-10 13:09:10 +00:00
$this->fields[$field->getName()] = $field;
2018-08-06 12:30:51 +00:00
$field->setOwningModelClass(get_class($this));
return $this;
}
2019-01-22 17:24:18 +00:00
// public function getField($name)
// {
// return $this->_getField($name);
// }
2018-08-06 12:30:51 +00:00
/**
* Return a reference to an actual field.
*
* @param string $name Name of the requested field.
* @return Field The requested field instance. If no such instance can be found, null is returned.
*/
2019-02-12 11:21:35 +00:00
public function getField($name)
2018-08-06 12:30:51 +00:00
{
2018-09-10 13:09:10 +00:00
if (isset($this->fields[$name])) {
return $this->fields[$name];
2018-08-06 12:30:51 +00:00
} else {
return null;
}
}
public function fetchValues()
{
2019-01-24 15:56:39 +00:00
$this->initFields();
2018-09-10 13:09:10 +00:00
foreach ($this->fields as $fieldname => $field) {
if (isset($this->externalFields[$fieldname]) === true) {
2018-08-06 12:30:51 +00:00
$fetchmode = 'lazy';
2018-09-10 13:09:10 +00:00
if (isset($this->externalFields[$fieldname]['fetch']) === true) {
$fetchmode = $this->externalFields[$fieldname]['fetch'];
2018-08-06 12:30:51 +00:00
}
if ($fetchmode === 'lazy') {
// Remember the field to be fetched later.
$this->_pending[] = $fieldname;
// Go to next field
continue;
} else {
// Immediately load external field if fetching mode is set to 'eager'
2019-01-08 13:06:21 +00:00
$this->loadExternal($fieldname);
2018-08-06 12:30:51 +00:00
}
} else {
// Field is not external an gets handled by simply reading
$property_name = self::convertFieldnameToColumn($fieldname);
//$test = $this->server_date_created;
$fieldval = $this->{$property_name};
// explicitly set null if the field represents a model except for dates
if (null !== $field->getValueModelClass()) {
if (true === empty($fieldval)) {
$fieldval = null;
} else {
$fieldval = new \Illuminate\Support\Carbon($fieldval);
2018-08-06 12:30:51 +00:00
}
}
$field->setValue($fieldval);
}
}
}
public static function convertFieldnameToColumn($fieldname)
{
return strtolower(preg_replace('/(?!^)[[:upper:]]/', '_\0', $fieldname));
}
2019-01-08 13:06:21 +00:00
protected function loadExternal($fieldname)
2018-08-06 12:30:51 +00:00
{
2018-09-10 13:09:10 +00:00
$field = $this->fields[$fieldname];
2018-08-06 12:30:51 +00:00
$modelclass = $field->getLinkModelClass();
if (!isset($modelclass)) {
// For handling a value model, see 'model' option.
$modelclass = $field->getValueModelClass();
}
$tableclass = new $modelclass();//::getTableGatewayClass();
// $table = Opus_Db_TableGateway::getInstance($tableclass);
$select = $tableclass->query();//->where("document_id", $this->id);;
// If any declared constraints, add them to query
2018-09-10 13:09:10 +00:00
if (isset($this->externalFields[$fieldname]['options'])) {
$options = $this->externalFields[$fieldname]['options'];
2018-08-06 12:30:51 +00:00
foreach ($options as $column => $value) {
2018-12-17 16:10:17 +00:00
// $searchString = ',';
// if (strpos($value, $searchString) !== false) {
// $arr = explode(",", $value);
if (is_array($value)) {
$arr = $value;
$select->whereIn($column, $arr);
} else {
$select = $select->where($column, $value);
}
2018-08-06 12:30:51 +00:00
}
}
// Get dependent rows
$result = array();
$datasetId = $this->id;
$rows = array();
2018-09-10 13:09:10 +00:00
if (isset($this->externalFields[$fieldname]['through'])) {
$relation = $this->externalFields[$fieldname]['relation'];
2018-08-06 12:30:51 +00:00
//$rows = $select->datasets
////->orderBy('name')
//->get();
//$licenses = $select->with('datasets')->get();
//$rows = $supplier->datasets;
$rows = $this->{$relation};
if (isset($this->externalFields[$fieldname]['pivot'])) {
$pivArray = $this->externalFields[$fieldname]['pivot'];
$pivotValue = $pivArray['role'];
//$through = $this->externalFields[$fieldname]['through'];
$rows = $this->{$relation}()->wherePivot('role', $pivotValue)->get();
//$rows = $this->{$relation}()->get();
//$rows = $this->belongsToMany($modelclass, $through, 'document_id')
//->wherePivot('role', $pivotValue)->get();
}
2018-08-06 12:30:51 +00:00
} else {
$rows = $select->whereHas('dataset', function ($q) use ($datasetId) {
$q->where('id', $datasetId);
})->orderBy('id')->get();
2018-08-06 12:30:51 +00:00
}
foreach ($rows as $row) {
// //$newModel = new $modelclass($row);
// $result[] = $row;//->value;
$attributes = array_keys($row->getAttributes());
if (isset($this->externalFields[$fieldname]['pivot'])) {
$pivotArray = $this->externalFields[$fieldname]['pivot'];
$arrayKeys = array_keys($pivotArray);
$extendedArrayKeys = array_map(function ($pivotAttribute) {
return "pivot_" . $pivotAttribute;
}, $arrayKeys);
$attributes = array_merge($attributes, $extendedArrayKeys);
}
2018-08-06 12:30:51 +00:00
$objArray = [];
foreach ($attributes as $property_name) {
$fieldName = self::convertColumnToFieldname($property_name);
$fieldval = "";
if (substr($property_name, 0, 6) === "pivot_") {
$str = ltrim($property_name, 'pivot_');
$fieldName = self::convertColumnToFieldname($str);
$fieldval = $row->pivot->{$str};
} elseif ($fieldName == "Type") {
$fieldval = ucfirst($row->{$property_name});
} else {
// $field =new Field($fieldName);
$fieldval = $row->{$property_name};
// $field->setValue($fieldval);
// $this->_mapField($field, $dom, $rootNode);
}
2018-08-06 12:30:51 +00:00
$objArray[$fieldName] = $fieldval;
}
$result[] = $objArray;
}
// Set the field value
$field->setValue($result);
}
//snakeToCamel
public static function convertColumnToFieldname($columnname)
{
//return lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $columnname))));
return str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $columnname)));
}
}