Completed
Push — master ( 952e89...59c62b )
by Jeroen De
03:27
created

TimelinePresenter::getSlidePresenter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace ModernTimeline;
6
7
use ModernTimeline\ResultFacade\ResultSimplifier;
8
use ModernTimeline\SlidePresenter\SimpleSlidePresenter;
9
use ModernTimeline\SlidePresenter\SlidePresenter;
10
use ModernTimeline\SlidePresenter\TemplateSlidePresenter;
11
use ParamProcessor\ProcessedParam;
12
use SMWOutputs;
13
use SMWQueryResult;
14
15
class TimelinePresenter {
16
17
	private $id;
18
	private $parameters;
19
20
	/**
21
	 * @param ProcessedParam[] $parameters
22
	 */
23
	public function __construct( array $parameters ) {
24
		$this->parameters = $parameters;
25
		$this->id = $this->newTimelineId();
26
	}
27
28
	private function newTimelineId(): string {
29
		static $timelineNumber = 0;
30
		return 'modern_timeline_' . ++$timelineNumber;
31
	}
32
33
	public function getResult( SMWQueryResult $result ): string {
34
		SMWOutputs::requireResource( 'ext.modern.timeline' );
35
36
		SMWOutputs::requireHeadItem(
37
			$this->id,
38
			$this->createJs( $this->createJsonString( $result ) )
39
		);
40
41
		return $this->createDiv();
42
	}
43
44
	private function createJsonString( SMWQueryResult $result ) {
45
		$preJson = ( new JsonBuilder( $this->getSlidePresenter() ) )->buildTimelineJson(
46
			( new ResultSimplifier() )->newSubjectCollection( $result )
47
		);
48
49
		$preJson['options'] = TimelineOptions::processedParamsToJson( $this->parameters );
50
51
		return json_encode( $preJson );
52
	}
53
54
	private function getSlidePresenter(): SlidePresenter {
55
		if ( $this->getTemplateName() === '' ) {
56
			return new SimpleSlidePresenter();
57
		}
58
59
		return new TemplateSlidePresenter( $this->getTemplateName() );
60
	}
61
62
	private function getTemplateName(): string {
63
		return $this->parameters['template']->getValue();
64
	}
65
66
	private function createJs( string $json ): string {
67
		return \Html::rawElement(
68
			'script',
69
			[
70
				'type' => 'text/javascript'
71
			],
72
			"if (!window.hasOwnProperty('modernTimeline')) {window.modernTimeline = {};}"
73
			. "\n window.modernTimeline.{$this->id} = $json;"
74
		);
75
	}
76
77
	private function createDiv(): string {
78
		$width = $this->parameters[TimelineOptions::PARAM_WIDTH]->getValue();
79
		$height = $this->parameters[TimelineOptions::PARAM_HEIGHT]->getValue();
80
81
		return \Html::rawElement(
82
			'div',
83
			[
84
				'id' => $this->id,
85
				'style' => "width: $width; height: $height",
86
				'class' => 'modern_timeline_outer_div'
87
			],
88
			\Html::element(
89
				'div',
90
				[
91
					'class' => 'modern_timeline_inner_div',
92
					'style' => 'width: 100%; height: calc(100% - 10px); background-color: rgba(0, 0, 0, 0.05); margin-top: 5px; margin-bottom: 5px;'
93
				]
94
			)
95
		);
96
	}
97
98
}
99