Open Data Catalog v2.0.0
DataNodeLifeCycle.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Drupal\dkan_data;
4 
7 
12 
18  public function presave() {
19 
20  if (empty($this->getDataType())) {
21  $this->setDataType('dataset');
22  }
23 
24  switch ($this->getDataType()) {
25  case 'dataset':
26  $this->datasetPresave();
27  break;
28 
29  case 'distribution':
30  $this->distributionPresave();
31  break;
32  }
33  }
34 
38  private function datasetPresave() {
39  /* @var $entity \Drupal\node\Entity\Node */
40  $entity = $this->node;
41 
42  $metadata = $this->getMetaData();
43 
44  $title = isset($metadata->title) ? $metadata->title : $metadata->name;
45 
46  $entity->setTitle($title);
47 
48  // If there is no uuid add one.
49  if (!isset($metadata->identifier)) {
50  $metadata->identifier = $entity->uuid();
51  }
52  // If one exists in the uuid it should be the same in the table.
53  else {
54  $entity->set('uuid', $metadata->identifier);
55  }
56 
57  $referencer = \Drupal::service("dkan_data.referencer");
58  $metadata = $referencer->reference($this->getMetaData());
59  $this->setMetadata($metadata);
60 
61  // Check for possible orphan property references when updating a dataset.
62  if (isset($entity->original)) {
63  $orphanChecker = \Drupal::service("dkan_data.orphan_checker");
64  $orphanChecker->processReferencesInUpdatedDataset(
65  json_decode($entity->referenced_metadata),
66  $metadata
67  );
68  }
69  }
70 
74  private function distributionPresave() {
75  $metadata = $this->getMetaData();
76  $host = \Drupal::request()->getHost();
77  if (isset($metadata->data->downloadURL)) {
78  $newUrl = $metadata->data->downloadURL;
79  $parsedUrl = parse_url($newUrl);
80  if ($parsedUrl['host'] == $host) {
81  $parsedUrl['host'] = UrlHostTokenResolver::TOKEN;
82  $metadata->data->downloadURL = $this->unparseUrl($parsedUrl);
83  $this->setMetadata($metadata);
84  }
85  }
86 
87  }
88 
92  private function unparseUrl($parsedUrl) {
93  $url = '';
94  $urlParts = [
95  'scheme',
96  'host',
97  'port',
98  'user',
99  'pass',
100  'path',
101  'query',
102  'fragment',
103  ];
104 
105  foreach ($urlParts as $part) {
106  if (!isset($parsedUrl[$part])) {
107  continue;
108  }
109  $url .= ($part == "port") ? ':' : '';
110  $url .= ($part == "query") ? '?' : '';
111  $url .= ($part == "fragment") ? '#' : '';
112  $url .= $parsedUrl[$part];
113  $url .= ($part == "scheme") ? '://' : '';
114  }
115 
116  return $url;
117  }
118 
119 }
Drupal\dkan_common\AbstractDataNodeLifeCycle\setDataType
setDataType($type)
Definition: AbstractDataNodeLifeCycle.php:33
Drupal\dkan_common\UrlHostTokenResolver
Definition: UrlHostTokenResolver.php:8
Drupal\dkan_data
Definition: ConfigurationOverrider.php:3
Drupal\dkan_common\AbstractDataNodeLifeCycle\$node
$node
Definition: AbstractDataNodeLifeCycle.php:13
Drupal\dkan_common\AbstractDataNodeLifeCycle\getDataType
getDataType()
Definition: AbstractDataNodeLifeCycle.php:26
Drupal\dkan_common\UrlHostTokenResolver\TOKEN
const TOKEN
Definition: UrlHostTokenResolver.php:9
Drupal\dkan_common\AbstractDataNodeLifeCycle\getMetaData
getMetaData()
Definition: AbstractDataNodeLifeCycle.php:40
Drupal\dkan_common\AbstractDataNodeLifeCycle\setMetadata
setMetadata($metadata)
Definition: AbstractDataNodeLifeCycle.php:49
Drupal\dkan_common\AbstractDataNodeLifeCycle
Definition: AbstractDataNodeLifeCycle.php:12
Drupal\dkan_data\DataNodeLifeCycle
Definition: DataNodeLifeCycle.php:11
Drupal\dkan_data\DataNodeLifeCycle\presave
presave()
Definition: DataNodeLifeCycle.php:18