Completed
Push — master ( 6dc7d8...407c40 )
by Karsten
15:45
created

Gantt::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * File holding the Gantt class
4
 *
5
 * - Add sections and tasks and
6
 *   manage the relations between them
7
 * - Sort elements based on the sortkey
8
 * - Creates config for gantt diagram
9
 *
10
 * @author Sebastian Schmid
11
 * @file
12
 * @ingroup SemanticResultFormats
13
 */
14
15
namespace SRF\Gantt;
16
17
class Gantt {
18
19
	private $mTitle;
20
	private $mAxisFormat;
21
	private $mSections = [];
22
	private $mTasks = [];
23
	private $mPriorityMapping;
24
	private $mStatusMapping;
25
26
	public function __construct( $headerParam ) {
27
		$this->setTitle( $headerParam['title'] );
28
		$this->setAxisFormat( $headerParam['axisformat'] );
29
		$this->setStatusMapping( $headerParam['statusmapping'] );
30
		$this->setPriorityMapping( $headerParam['prioritymapping'] );
31
	}
32
33
	private function setPriorityMapping( $priorityMapping ) {
34
		$this->mPriorityMapping = $priorityMapping;
35
	}
36
37
	private function getPriorityMapping() {
38
		return $this->mPriorityMapping;
39
	}
40
41
	private function setStatusMapping( $statusMapping ) {
42
		$this->mStatusMapping = $statusMapping;
43
	}
44
45
	private function getStatusMapping() {
46
		return $this->mStatusMapping;
47
	}
48
49
	private function setAxisFormat( $axisFormat ) {
50
		$this->mAxisFormat = $axisFormat;
51
	}
52
53
	private function getAxisFormat() {
54
		return $this->mAxisFormat;
55
	}
56
57
	private function setTitle( $title ) {
58
		$this->mTitle = $title;
59
	}
60
61
	private function getTitle() {
62
		return $this->mTitle;
63
	}
64
65
	private function getSections() {
66
		return $this->mSections;
67
	}
68
69
	private function getTasks() {
70
		return $this->mTasks;
71
	}
72
73
	/**
74
	 * Adds a new Task to array
75
	 *
76
	 * @param string $taskID
77
	 * @param string $taskTitle
78
	 * @param array $status
79
	 * @param array $priority
80
	 * @param string $startDate
81
	 * @param string $endDate
82
	 *
83
	 */
84
85
	public function addTask( $taskID, $taskTitle, $status, $priority, $startDate, $endDate ) {
86
		$task = new GanttTask();
87
		$task->setID( $taskID );
88
		$task->setTitle( $taskTitle );
89
		$task->setTaskParam( $status, $this->getStatusMapping(), 'status' );
90
		$task->setTaskParam( $priority, $this->getPriorityMapping(), 'priority' );
91
		$task->setStartDate( $startDate );
92
		$task->setEndDate( $endDate );
93
		$this->mTasks[$taskID] = $task;
94
	}
95
96
97
	/**
98
	 * Creats a new Section with related tasks
99
	 *
100
	 * @param string $sectionID
101
	 * @param string $sectionTitle
102
	 * @param string $startDate
103
	 * @param string $endDate
104
	 * @param string $taskID
105
	 *
106
	 */
107
	public function addSection( $sectionID, $sectionTitle, $startDate, $endDate, $taskID ) {
108
109
		$sections = $this->getSections();
110
111
		if ( array_key_exists( $sectionID, $sections ) ) {
112
113
			if ( $sections[$sectionID]->getEarliestStartDate() > $startDate ) {
114
				$sections[$sectionID]->setEarliestStartDate( $startDate );
115
			}
116
			if ( $sections[$sectionID]->getLatestEndDate() < $endDate ) {
117
				$sections[$sectionID]->setLatestEndDate( $endDate );
118
			}
119
			$sections[$sectionID]->addTask( $taskID );
120
121
		} else {
122
			$this->createNewSection( $sectionID, $sectionTitle, $startDate, $endDate, $taskID );
123
		}
124
	}
125
126
	private function createNewSection( $sectionID, $sectionTitle, $startDate, $endDate, $taskID ) {
127
		$ganttSection = new GanttSection();
128
		//check if the id in the object is realy needed or is it enough to have it as array key
129
		$ganttSection->setID( $sectionID );
130
		$ganttSection->setTitle( $sectionTitle );
131
		$ganttSection->setEarliestStartDate( $startDate );
132
		$ganttSection->setLatestEndDate( $endDate );
133
		$ganttSection->addTask( $taskID );
134
135
		$this->mSections[$sectionID] = $ganttSection;
136
	}
137
138
	/**
139
	 * Creates output for mermaidjs
140
	 *
141
	 * @return string
142
	 */
143
	public function getGanttOutput() {
144
145
		$sections = $this->getSections();
146
		$tasks = $this->getTasks();
147
148
		/*
149
		 * Bring the "section" with no title to the first position.
150
		 * This "section" is the one that hold tasks without any section.
151
		 * If we don't display it at the beginning we have to put them into a dummy section
152
		 */
153
		foreach ( $sections as $key => $section ) {
154
			if ( $section->getTitle() === '' ) {
155
				$noSection = $section;
156
				unset( $sections[$key] );
157
			}
158
		}
159
		// push now the dummy task to the first place of the array
160
		if ( isset( $noSection ) ) {
161
			array_unshift( $sections, $noSection );
162
		}
163
164
		$title = $this->getTitle();
165
		$axisFormat = $this->getAxisFormat();
166
167
		$mermaidOut = "gantt\n";
168
		$mermaidOut .= "dateFormat YYYY-MM-DD\n";
169
		$mermaidOut .= ( !empty( $title ) ) ? "title $title\n" : '';
170
		$mermaidOut .= "axisFormat $axisFormat\n";
171
172
		// Output section and all related Issues
173
		foreach ( $sections as $section ) {
174
			if ( $section->getTitle() !== "" ) {
175
				$mermaidOut .= 'section ' . $section->getTitle() . "\n";
176
			}
177
178
			//loop through related section tasks
179
			foreach ( $section->getTasks() as $sectionTask ) {
180
181
						$status = $tasks[$sectionTask]->getStatus();
182
183
						// Get Date from timestamp
184
						$date = date_create();
185
						date_timestamp_set( $date, $tasks[$sectionTask]->getStartDate() );
186
						$startDate = date_format( $date, 'Y-m-d' ) . ', ';
187
						date_timestamp_set( $date, $tasks[$sectionTask]->getEndDate() );
188
						$endDate = date_format( $date, 'Y-m-d' );
189
190
						//get Priority
191
						$priority = $tasks[$sectionTask]->getPriority();
192
193
						$mermaidOut .= $tasks[$sectionTask]->getTitle() . "\t :" . $priority . $status . $startDate . $endDate .
194
									   "\n";
195
			}
196
		}
197
198
		//Hashtags mark a Comment in Mermaid, so we need to replace it with <esc>35</esc> to replace it again after rendering
199
		$mermaidOut = str_replace( '#', '<esc>35</esc>', $mermaidOut );
200
201
		return $mermaidOut;
202
	}
203
}