summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticResultFormats/formats/Gantt/GanttPrinter.php
blob: 9b59fdf0e77d127a1e548ae4dc05321b51003e9f (plain)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php
/**
 * SMW result printer for Gantt Diagrams using mermaidjs.
 * https://github.com/knsv/mermaid
 *
 * In order to use this printer you need to have
 * the Mermaid MediaWiki extension installed.
 * https://www.mediawiki.org/wiki/Extension:Mermaid
 *
 * @file Gantt.php
 * @ingroup SemanticResultFormats
 *
 * @licence GNU GPL v2+
 * @author Sebastian Schmid
 */

namespace SRF\Gantt;

use SMWOutputs;
use SMWQueryResult;
use SMWResultPrinter;
use SMWDITime;
use SMWDIBlob;
use Html;

class GanttPrinter extends SMWResultPrinter {

	protected $mGantt = null;
	protected $mErrors = [];

	public function getName() {
		return wfMessage( 'srf-printername-gantt' )->text();
	}

	public function getParamDefinitions( array $definitions ) {
		$params = parent::getParamDefinitions( $definitions );

		$params[] = [
			'type'    => 'string',
			'name'    => 'diagramtitle',
			'message' => 'srf-paramdesc-gantt-diagramtitle',
			'default' => ''
		];

		$params[] = [
			'type'    => 'string',
			'name'    => 'theme',
			'message' => 'srf-paramdesc-gantt-diagramtheme',
			'default' => 'default'
		];

		$params[] = [
			'type'    => 'string',
			'name'    => 'axisformat',
			'message' => 'srf-paramdesc-gantt-axisformat',
			'default' => '%m/%d/%Y'
		];

		$params[] = [
			'type'    => 'string',
			'name'    => 'statusmapping',
			'message' => 'srf-paramdesc-gantt-statusmapping',
			'default' => ''
		];

		$params[] = [
			'type'    => 'string',
			'name'    => 'prioritymapping',
			'message' => 'srf-paramdesc-gantt-prioritymapping',
			'default' => ''
		];

		$params[] = [
			'type'    => 'integer',
			'name'    => 'titletopmargin',
			'message' => 'srf-paramdesc-gantt-titletopmargin',
			'default' => 25
		];

		$params[] = [
			'type'    => 'integer',
			'name'    => 'barheight',
			'message' => 'srf-paramdesc-gantt-barheight',
			'default' => 20
		];

		$params[] = [
			'type'    => 'integer',
			'name'    => 'leftpadding',
			'message' => 'srf-paramdesc-gantt-leftpadding',
			'default' => 75
		];

		$params[] = [
			'type'    => 'integer',
			'name'    => 'bargap',
			'message' => 'srf-paramdesc-gantt-bargap',
			'default' => 4
		];

		return $params;
	}

	/**
	 * Handle (set) the result format parameters
	 *
	 * @see SMWResultPrinter::handleParameters()
	 */
	protected function handleParameters( array $params, $outputmode ) {

		//Set header params
		$this->params['title'] = trim( $params['diagramtitle'] );
		$this->params['axisformat'] = trim( $params['axisformat'] );
		$this->params['statusmapping'] =  $this->getValidatedMapping( $params[ 'statusmapping' ], 'statusmapping', [ 'active', 'done' ] );
		$this->params['prioritymapping'] = $this->getValidatedMapping( $params[ 'prioritymapping' ], 'prioritymapping', [ 'crit' ] );
		$this->params['theme'] = $this->getValidatedTheme($params['theme']);

		$this->mGantt = $this->getGantt();
	}

	/**
	 * Return serialised results in specified format.
	 * @param SMWQueryResult $queryResult
	 * @param $outputmode
	 * @return string
	 */
	protected function getResultText( SMWQueryResult $queryResult, $outputmode ) {

		// Show warning if Extension:Mermaid is not available
		if ( !class_exists( 'Mermaid' ) && !class_exists( 'Mermaid\\MermaidParserFunction' ) ) {
			$queryResult->addErrors( [wfMessage('')->text()] );
			return '';
		}

		// Load general Modules
		SMWOutputs::requireResource( 'ext.srf.gantt' );

		//Add Tasks & Sections
		while ( $row = $queryResult->getNext() ) {

			$status = [];
			$priority = [];
			$startDate = '';
			$endDate = '';
			$taskID = '';
			$taskTitle = '';
			$sections = [];

			// Loop through all field of a row
			foreach ( $row as $field ) {

				$fieldLabel = $field->getPrintRequest()->getLabel();

				//get values
				foreach ( $field->getContent() as $dataItem ) {

					switch ( $fieldLabel ) {
						case 'section':
							$sections[$dataItem->getTitle()->getPrefixedDBKey()] = $dataItem->getSortKey();
							break;
						case 'task':
							if ( $dataItem instanceof SMWDIBlob ) {
								$taskTitle = $dataItem->getString();
								$taskID = $field->getResultSubject()->getTitle()->getPrefixedDBKey();
							}
							break;
						case 'startdate':
							if ( $dataItem instanceof SMWDITime ) {
								$startDate = $dataItem->getMwTimestamp();
							}
							break;
						case 'enddate':
							if ( $dataItem instanceof SMWDITime ) {
								$endDate = $dataItem->getMwTimestamp();
							}
							break;
						case 'status':
							if ( $dataItem instanceof SMWDIBlob ) {
								$status[] = $dataItem->getString();
							}
							break;
						case 'priority':
							if ( $dataItem instanceof SMWDIBlob ) {
								$priority[] = $dataItem->getString();
							}
							break;
					}
				}
			}

			// Add section/Task
			// Title, TaskID, StartDate and EndDate are required
			if ( $taskID !== '' && $taskTitle !== '' && $startDate !== '' && $endDate !== '' ) {
				$this->mGantt->addTask( $taskID, $taskTitle, $status, $priority, $startDate, $endDate );

				// If no section was found, put task into a dummy section object
				// "gantt-no-section#21780240" is used to identify Tasks that with no section (dummy section)
				if ( count( $sections ) == 0 ) {
					$this->mGantt->addSection( 'gantt-no-section#21780240', '', $startDate, $endDate, $taskID );
				} else {
					foreach ( $sections as $sectionID => $sectionTitle ) {
						$this->mGantt->addSection( $sectionID, $sectionTitle, $startDate, $endDate, $taskID );
					}
				}
			}
		}

		// Improve unique id by adding a random number
		$id = uniqid( 'srf-gantt-' . rand( 1, 10000 ) );

		// Add gantt configurations
		$config = [
			'theme' => $this->params['theme'],
			'gantt' => [
				'leftPadding'    => intval( $this->params['leftpadding'] ),
				'titleTopMargin' => intval( $this->params['titletopmargin'] ),
				'barHeight'      => intval( $this->params['barheight'] ),
				'barGap'         => intval( $this->params['bargap'] )
			]
		];


		// Manage Output
		if ( !empty( $this->mErrors ) ) {
			return $queryResult->addErrors( $this->mErrors );
		} else {
			return Html::rawElement( 'div', [
				'id'           => $id,
				'class'        => 'srf-gantt',
				'data-mermaid' => html_entity_decode( json_encode( [
					'content' => $this->mGantt->getGanttOutput(),
					'config'  => $config
				]))
			], Html::rawElement( 'div', [ 'class' => 'mermaid-dots' ]));
		}
	}

	private function getGantt(){
		return new Gantt( $this->params );
	}

	/**
	 * Return valid theme as string
	 * @param String $theme
	 *
	 * @return string
	 */
	private function getValidatedTheme( $theme ) {
		$theme = trim( $theme );

		if ( !in_array( $this->params['theme'], [ 'default', 'neutral', 'dark', 'forest' ] ) ) {
			$this->mErrors[] = wfMessage( 'srf-error-gantt-theme' )->text();
		}

		return $theme;
	}


	private function getValidatedMapping( $params, $mappingType, array $mappingKeys){
		//Validate mapping
		$mapping = [];

		if ( !empty( $params ) ) {
			$paramMapping = explode( ';', trim( $params ) );

			foreach ( $paramMapping as $pm ) {
				$pmKeyVal = explode( '=>', $pm, 2);

				// if no key value pair
				if ( count( $pmKeyVal ) !== 2 ) {
					$this->mErrors[] = wfMessage( 'srf-error-gantt-mapping-assignment', $mappingType )->text();
				} else {
					$mapping[trim( $pmKeyVal[0] )] = trim( $pmKeyVal[1] );

					if(!in_array(trim( $pmKeyVal[1] ), $mappingKeys)){
						$this->mErrors[] = wfMessage( 'srf-error-gantt-mapping-keywords' )->text();
					}
				}
			}
			return $mapping;
		}
		return '';
	}
}