summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticResultFormats/src/Outline/TemplateBuilder.php
blob: e6982e29fca907a7d313988475ceaa393824f549 (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
<?php

namespace SRF\Outline;

use SMW\Query\PrintRequest;
use SRF\Outline\OutlineTree;

/**
 * @license GNU GPL v2+
 * @since 3.1
 *
 * @author mwjames
 */
class TemplateBuilder {

	/**
	 * @var []
	 */
	private $params = [];

	/**
	 * @var Linker
	 */
	private $linker;

	/**
	 * @var string
	 */
	private $template = '';

	/**
	 * @param array $params
	 */
	public function __construct( array $params ) {
		$this->params = $params;
	}

	/**
	 * @since 3.1
	 *
	 * @param Linker|null|false $linker
	 */
	public function setLinker( $linker ) {
		$this->linker = $linker;
	}

	/**
	 * @since 3.1
	 *
	 * @param OutlineTree $tree
	 *
	 * @return string
	 */
	public function build( OutlineTree $outlineTree ) {
		$this->tree( $outlineTree );

		return $this->template;
	}

	private function tree( $outlineTree, $level = 0 ) {

		if ( $outlineTree->items !== null ) {
			foreach ( $outlineTree->items as $i => $item ) {
				$this->template .= $this->item( $i, $item );
			}
		}

		foreach ( $outlineTree->tree as $key => $node ) {
			$property = $this->params['outlineproperties'][$level];
			$class = $this->params['template'] . '-section-' . strtolower( str_replace( ' ', '-', $property ) );

			$this->template .= "<div class='$class'>";
			$this->template .= $this->open( $this->params['template'] . "-header" );
			$this->template .= $this->parameter( $property, $key );
			$this->template .= $this->parameter( "#outlinelevel", $level );
			$this->template .= $this->parameter( "#itemcount",  $node->leafCount );
			$this->template .= $this->parameter( "#userparam", $this->params['userparam'] );
			$this->template .= $this->close();
			$this->template .= "<div class='" . $this->params['template'] . "-items'>";
			$this->tree( $node, $level + 1 );
			$this->template .= "</div>";
			$this->template .= "</div>";
		}
	}

	private function item( $i, $item ) {

		$first_col = true;
		$template = '';
		$linker = $this->params['link'] === 'all' ? $this->linker : null;
		$itemnumber = 0;

		foreach ( $item->row as $resultArray ) {

			$printRequest = $resultArray->getPrintRequest();
			$val = $printRequest->getText( SMW_OUTPUT_WIKI, null );

			if ( in_array( $val, $this->params['outlineproperties'] ) ) {
				continue;
			}

			while ( ( $dv = $resultArray->getNextDataValue() ) !== false ) {
				$template .= $this->open( $this->params['template'] . '-item' );
				$template .= $this->parameter( "#itemsection", $i );

				$template .= $this->parameter( "#itemnumber", $itemnumber );
				$template .= $this->parameter( "#userparam", $this->params['userparam'] );

				$template .= $this->itemText( $dv, $linker, $printRequest, $first_col );
				$template .= $this->close();

				$itemnumber++;
			}
		}

		return "<div class='" . $this->params['template'] . "-item'>" . $template . '</div>';
	}

	private function itemText( $dv, $linker, $printRequest, &$first_col ) {

		if ( $first_col && $printRequest->isMode( PrintRequest::PRINT_THIS ) ) {
			$first_col = false;

			if ( $linker === null && ( $caption = $dv->getDisplayTitle() ) !== '' ) {
				$dv->setCaption( $caption );
			}

			$text = $dv->getShortText(
				SMW_OUTPUT_WIKI,
				$this->params['link'] === 'subject' ? $this->linker : $linker
			);

			return $this->parameter( "#itemsubject", $text );
		}

		$text = $dv->getShortText(
			SMW_OUTPUT_WIKI,
			$linker
		);

		return $this->parameter( $printRequest->getLabel(), $text );
	}

	private function open( $v ) {
		return "{{" . $v;
	}

	private function parameter( $k, $v ) {
		return " |$k=$v";
	}

	private function close() {
		return "}}";
	}

}