1 <?php |
1 <?php |
2 namespace CorpusParole\Models; |
2 namespace CorpusParole\Models; |
3 |
3 |
|
4 use CorpusParole\Libraries\CorpusParoleException; |
4 use CorpusParole\Libraries\RdfModel\RdfModelResource; |
5 use CorpusParole\Libraries\RdfModel\RdfModelResource; |
5 use CorpusParole\Libraries\Utils; |
6 use CorpusParole\Libraries\Utils; |
6 use JsonSerializable; |
7 use JsonSerializable; |
7 use Log; |
8 use Log; |
8 |
9 |
9 /** |
10 /** |
10 */ |
11 */ |
11 class GeoResource extends RdfModelResource implements JsonSerializable { |
12 class GeoResource extends RdfModelResource implements JsonSerializable { |
12 |
13 |
13 public function __construct($uri, $graph) { |
14 public function __construct($uri, $graph, $providedCHO) { |
14 parent::__construct($uri, $graph); |
15 parent::__construct($uri, $graph); |
|
16 $this->providedCHO = $providedCHO; |
|
17 $this->readOnly = true; |
|
18 $this->changePending = false; |
|
19 $this->needDelete = false; |
15 } |
20 } |
|
21 |
|
22 private $providedCHO = null; |
|
23 private $readOnly = true; |
|
24 private $changePending = false; |
|
25 private $needDelete = false; |
16 |
26 |
17 private $refLocs = null; |
27 private $refLocs = null; |
18 private $notes = null; |
28 private $notes = null; |
|
29 private $latitude = false; |
|
30 private $longitude = false; |
|
31 |
|
32 public function getDeltaList() { |
|
33 if($this->changePending) { |
|
34 throw new CorpusParoleException('GetDeltaList called when changes are pending'); |
|
35 } |
|
36 return parent::getDeltaList(); |
|
37 } |
19 |
38 |
20 public function clearMemoizationCache() { |
39 public function clearMemoizationCache() { |
21 $this->refLocs = null; |
40 $this->refLocs = null; |
22 $this->notes = null; |
41 $this->notes = null; |
|
42 $this->latitude = false; |
|
43 $this->longitude = false; |
23 } |
44 } |
24 |
45 |
25 public function getRefLocs() { |
46 public function getRefLocs() { |
26 if(is_null($this->refLocs)) { |
47 if(is_null($this->refLocs)) { |
27 $refLocs = $this->allResources("<http://www.w3.org/2002/07/owl#sameAs>"); |
48 $refLocs = $this->allResources("<http://www.w3.org/2002/07/owl#sameAs>"); |
28 $this->refLocs = array_map(function($refLoc) { return $refLoc->getUri();}, $refLocs); |
49 $this->refLocs = array_map(function($refLoc) { return $refLoc->getUri();}, $refLocs); |
29 } |
50 } |
30 return $this->refLocs; |
51 return $this->refLocs; |
31 } |
52 } |
32 |
53 |
|
54 public function setRefLocs($refLocs) { |
|
55 if(!$this->changePending) { |
|
56 throw new CorpusParoleException('Can call setRefLocs only when changes are pending'); |
|
57 } |
|
58 |
|
59 $this->delete("<http://www.w3.org/2002/07/owl#sameAs>"); |
|
60 |
|
61 foreach($refLocs as $refLocUri) { |
|
62 $this->addResource("http://www.w3.org/2002/07/owl#sameAs", $refLocUri); |
|
63 } |
|
64 |
|
65 $this->clearMemoizationCache(); |
|
66 |
|
67 } |
|
68 |
33 public function getNotes() { |
69 public function getNotes() { |
34 if(is_null($this->notes)) { |
70 if(is_null($this->notes)) { |
35 $this->notes = $this->all('<http://www.w3.org/2004/02/skos/core#note>'); |
71 $this->notes = $this->all('<http://www.w3.org/2004/02/skos/core#note>'); |
36 } |
72 } |
37 return $this->notes; |
73 return $this->notes; |
38 } |
74 } |
|
75 |
|
76 public function getLatitude() { |
|
77 if($this->latitude === false) { |
|
78 try { |
|
79 $this->latitude = $this->getLiteral('<http://www.w3.org/2003/01/geo/wgs84_pos#lat>'); |
|
80 } catch(\Exception $e) { |
|
81 $this->latitude = null; |
|
82 } |
|
83 } |
|
84 return $this->latitude; |
|
85 } |
|
86 |
|
87 public function getLatitudeValue() { |
|
88 $lat = $this->getLatitude(); |
|
89 return is_null($lat)?null:$lat->getValue(); |
|
90 } |
|
91 |
|
92 public function getLongitude() { |
|
93 if($this->longitude === false) { |
|
94 try { |
|
95 $this->longitude = $this->getLiteral('<http://www.w3.org/2003/01/geo/wgs84_pos#long>'); |
|
96 } catch(\Exception $e) { |
|
97 $this->longitude = null; |
|
98 } |
|
99 } |
|
100 return $this->longitude; |
|
101 } |
|
102 |
|
103 public function getLongitudeValue() { |
|
104 $long = $this->getLongitude(); |
|
105 return is_null($long)?null:$long->getValue(); |
|
106 } |
|
107 |
|
108 |
39 |
109 |
40 public function jsonSerialize() { |
110 public function jsonSerialize() { |
41 $notes = array_map( |
111 $notes = array_map( |
42 function($note) { return Utils::processLiteralResourceOrString($note); }, |
112 function($note) { return Utils::processLiteralResourceOrString($note); }, |
43 $this->getNotes() |
113 $this->getNotes() |
44 ); |
114 ); |
45 return [ |
115 return [ |
46 'ref-locs' => $this->getRefLocs(), |
116 'ref-locs' => $this->getRefLocs(), |
47 'notes' => $notes |
117 'notes' => $notes, |
|
118 'latitude' => $this->getLatitudeValue(), |
|
119 'longitude' => $this->getLongitudeValue(), |
48 ]; |
120 ]; |
49 } |
121 } |
50 |
122 |
|
123 public function setReadOnly($ro) { |
|
124 if($this->readOnly and !$ro) { |
|
125 $this->changePending = true; |
|
126 } |
|
127 $this->readOnly = $ro; |
|
128 } |
|
129 |
|
130 public function setNeedDelete($nd) { |
|
131 $this->needDelete = $nd; |
|
132 } |
|
133 |
|
134 public function rollback() { |
|
135 $this->changePending = false; |
|
136 } |
|
137 |
|
138 public function commit() { |
|
139 |
|
140 // Do nothing if there is no change pending |
|
141 if(!$this->changePending) { |
|
142 return; |
|
143 } |
|
144 |
|
145 $delta = $this->startDelta(); |
|
146 |
|
147 //delete the previous blank node |
|
148 if($this->needDelete) { |
|
149 $delta->addDeleteWhere( |
|
150 "?s ?p ?o. ". |
|
151 "{ ". |
|
152 " ?_ <http://purl.org/dc/terms/spatial> ?s. ". |
|
153 "} ". |
|
154 "UNION { ". |
|
155 " ?s <http://purl.org/dc/terms/spatial> ?o ". |
|
156 "}" |
|
157 ); |
|
158 } |
|
159 |
|
160 // add the node |
|
161 $geoinfoNodeAdded = $delta->getAddedGraph()->newBNode("http://www.europeana.eu/schemas/edm/Place"); |
|
162 $delta->getAddedGraph()->add($this->providedCHO, "http://purl.org/dc/terms/spatial", $geoinfoNodeAdded); |
|
163 |
|
164 foreach($this->propertyUris() as $prop) { |
|
165 if($prop == "http://www.w3.org/1999/02/22-rdf-syntax-ns#type") { |
|
166 continue; |
|
167 } |
|
168 foreach($this->all("<$prop>") as $propVal) { |
|
169 $delta->getAddedGraph()->add($geoinfoNodeAdded, $prop, $propVal); |
|
170 } |
|
171 } |
|
172 |
|
173 $this->changePending = false; |
|
174 |
|
175 } |
|
176 |
|
177 |
51 } |
178 } |