tethys/app/Library/Xml/DatasetExtension.php

317 lines
11 KiB
PHP
Raw 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;
2018-12-17 16:10:17 +00:00
use App\Models\GeolocationBox;
2019-03-29 17:29:20 +00:00
use App\Models\Coverage;
2019-03-18 13:32:29 +00:00
use App\Models\Subject;
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', '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,
'options' => array('type' => ['Abstract', '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' => 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'),
// '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'
),
2019-03-18 13:32:29 +00:00
'Subject' => array(
'model' => Subject::class,
'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",
"ContributingCorporation",
"CreatingCorporation",
"Language",
"PublishedDate", "PublishedYear",
"PublisherName", "PublisherPlace",
"PublicationState",
"ServerDateCreated",
"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(
2019-04-17 14:01:38 +00:00
'ServerDateCreated', 'PublishedDate',
2018-08-06 12:30:51 +00:00
'ServerDateModified', 'ServerDatePublished', 'ServerDateDeleted', 'EmbargoDate'
);
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 \Carbon\Carbon($fieldval);
}
}
$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->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);
})->get();
}
foreach ($rows as $row) {
// //$newModel = new $modelclass($row);
// $result[] = $row;//->value;
$attributes = array_keys($row->getAttributes());
$objArray = [];
foreach ($attributes as $property_name) {
$fieldName = self::convertColumnToFieldname($property_name);
$fieldval = "";
if ($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)));
}
}