1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
<?php
declare( strict_types = 1 );
namespace ModernTimeline;
use ModernTimeline\ResultFacade\PropertyValueCollection;
use ModernTimeline\ResultFacade\Subject;
use ModernTimeline\ResultFacade\SubjectCollection;
use ModernTimeline\SlidePresenter\SlidePresenter;
use SMWDITime;
class JsonBuilder {
private $slidePresenter;
public function __construct( SlidePresenter $slidePresenter ) {
$this->slidePresenter = $slidePresenter;
}
public function buildTimelineJson( SubjectCollection $pages ): array {
$events = [];
foreach ( $pages->getSubjects() as $subject ) {
[ $startDate, $endDate ] = $this->getDates( $subject );
if ( $startDate !== null ) {
$events[] = new Event( $subject, $startDate, $endDate );
}
}
return $this->eventsToTimelineJson( $events );
}
private function getDates( Subject $subject ): array {
$startDate = null;
$endDate = null;
foreach ( $this->getPropertyValueCollectionsWithDates( $subject ) as $propertyValues ) {
$dataItem = $propertyValues->getDataItems()[0];
if ( $dataItem instanceof SMWDITime ) {
if ( $startDate === null ) {
$startDate = $dataItem;
}
else if ( $endDate === null ) {
$endDate = $dataItem;
}
else {
break;
}
}
}
return [ $startDate, $endDate ];
}
/**
* @param Event[] $events
* @return array
*/
public function eventsToTimelineJson( array $events ): array {
$jsonEvents = [];
foreach ( $events as $event ) {
$jsonEvents[] = $this->buildEvent( $event );
}
return [ 'events' => $jsonEvents ];
}
private function buildEvent( Event $event ): array {
$jsonEvent = [
'text' => [
'headline' => $this->newHeadline( $event->getSubject()->getWikiPage()->getTitle() ),
'text' => $this->slidePresenter->getText( $event->getSubject() )
],
'start_date' => $this->timeToJson( $event->getStartDate() )
];
if ( $event->getEndDate() !== null ) {
$jsonEvent['end_date'] = $this->timeToJson( $event->getEndDate() );
}
return $jsonEvent;
}
/**
* @return PropertyValueCollection[]
*/
private function getPropertyValueCollectionsWithDates( Subject $subject ) {
return array_filter(
$subject->getPropertyValueCollections(),
function( PropertyValueCollection $pvc ) {
return $pvc->getPrintRequest()->getTypeID() === '_dat'
&& $pvc->getDataItems() !== [];
}
);
}
private function newHeadline( \Title $title ): string {
return \Html::element(
'a',
[ 'href' => $title->getFullURL() ],
$title->getText()
);
// return DataValueFactory::getInstance()->newDataValueByItem( $subject->getWikiPage() )->getLongHTMLText( smwfGetLinker() );
}
private function timeToJson( SMWDITime $time ): array {
return [
'year' => $time->getYear(),
'month' => $time->getMonth(),
'day' => $time->getDay(),
'hour' => $time->getHour(),
'minute' => $time->getMinute(),
'second' => (int)$time->getSecond(),
];
}
}
|