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

namespace SRF\Outline;

use SMW\Query\PrintRequest;
use SRF\Outline\OutlineTree;
use SMWDataItem as DataItem;

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

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

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

	/**
	 * @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 ) {
		return $this->tree( $outlineTree );
	}

	private function tree( $outline_tree, $level = 0 ) {
		$text = "";

		if ( !is_null( $outline_tree->items ) ) {
			$text .= "<ul>\n";
			foreach ( $outline_tree->items as $item ) {
				$text .= "<li>{$this->item($item)}</li>\n";
			}
			$text .= "</ul>\n";
		}

		if ( $level > 0 ) {
			$text .= "<ul>\n";
		}

		$num_levels = count( $this->params['outlineproperties'] );
		// set font size and weight depending on level we're at
		$font_level = $level;

		if ( $num_levels < 4 ) {
			$font_level += ( 4 - $num_levels );
		}

		if ( $font_level == 0 ) {
			$font_size = 'x-large';
		} elseif ( $font_level == 1 ) {
			$font_size = 'large';
		} elseif ( $font_level == 2 ) {
			$font_size = 'medium';
		} else {
			$font_size = 'small';
		}

		if ( $font_level == 3 ) {
			$font_weight = 'bold';
		} else {
			$font_weight = 'regular';
		}

		foreach ( $outline_tree->tree as $key => $node ) {
			$text .= "<p style=\"font-size: $font_size; font-weight: $font_weight;\">$key</p>\n";
			$text .= $this->tree( $node, $level + 1 );
		}

		if ( $level > 0 ) {
			$text .= "</ul>\n";
		}

		return $text;
	}

	private function item( $item ) {
		$first_col = true;
		$found_values = false; // has anything but the first column been printed?
		$result = "";

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

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

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

			$linker = $this->params['link'] === 'all' ? $this->linker : null;

			if ( $this->params['link'] === 'subject' && $printRequest->isMode( PrintRequest::PRINT_THIS ) ) {
				$linker = $this->linker;
			}

			while ( ( $dv = $resultArray->getNextDataValue() ) !== false ) {

				if ( !$first_col && !$found_values ) { // first values after first column
					$result .= ' (';
					$found_values = true;
				} elseif ( $found_values || !$first_value ) {
					// any value after '(' or non-first values on first column
					$result .= ', ';
				}

				if ( $first_value ) { // first value in any column, print header
					$first_value = false;
					if ( $this->params['showHeaders'] && ( '' != $printRequest->getLabel() ) ) {
						$result .= $printRequest->getText( SMW_OUTPUT_WIKI, $linker ) . ' ';
					}
				}

				$dataItem = $dv->getDataItem();

				if ( $linker === null && $dataItem->getDIType() === DataItem::TYPE_WIKIPAGE && ( $caption = $dv->getDisplayTitle() ) !== '' ) {
					$dv->setCaption( $caption );
				}

				$result .= $dv->getShortText( SMW_OUTPUT_WIKI, $linker );
			}

			$first_col = false;
		}

		if ( $found_values ) {
			$result .= ')';
		}

		return $result;
	}

}