summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Page/ListBuilder/ListBuilder.php
blob: 98185608003e042ff234ab68ee57737b1e36f0a7 (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
<?php

namespace SMW\Page\ListBuilder;

use Html;
use SMW\DIProperty;
use SMW\RequestOptions;
use SMW\Store;
use SMWDataItem as DataItem;
use SMWPageLister as PageLister;

/**
 * @license GNU GPL v2+
 * @since 3.0
 *
 * @author mwjames
 */
class ListBuilder {

	/**
	 * @var Store
	 */
	private $store;

	/**
	 * @var string
	 */
	private $languageCode = 'en';

	/**
	 * @var integer
	 */
	private $listLimit = 0;

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

	/**
	 * @var boolean
	 */
	private $isUserDefined = false;

	/**
	 * @var boolean
	 */
	private $checkProperty = true;

	/**
	 * @var integer
	 */
	private $itemCount = 0;

	/**
	 * @since 3.0
	 *
	 * @param Store $store
	 */
	public function __construct( Store $store ) {
		$this->store = $store;
	}

	/**
	 * @since 3.0
	 *
	 * @param string $languageCode
	 */
	public function setLanguageCode( $languageCode ) {
		$this->languageCode = $languageCode;
	}

	/**
	 * @since 3.0
	 *
	 * @param boolean $isUserDefined
	 */
	public function isUserDefined( $isUserDefined ) {
		$this->isUserDefined = $isUserDefined;
	}

	/**
	 * @since 3.0
	 *
	 * @param integer $listLimit
	 */
	public function setListLimit( $listLimit ) {
		$this->listLimit = $listLimit;
	}

	/**
	 * @since 3.0
	 *
	 * @param string $listHeader
	 */
	public function setListHeader( $listHeader ) {
		$this->listHeader = $listHeader;
	}

	/**
	 * @since 3.0
	 *
	 * @param boolean $checkProperty
	 */
	public function checkProperty( $checkProperty ) {
		$this->checkProperty = $checkProperty;
	}

	/**
	 * @since 3.0
	 *
	 * @return integer
	 */
	public function getItemCount() {
		return $this->itemCount;
	}

	/**
	 * @since 3.0
	 *
	 * @param DIProperty $property
	 * @param DataItem $dataItem
	 * @param RequestOptions $requestOptions
	 *
	 * @return string
	 */
	public function createHtml( DIProperty $property, DataItem $dataItem, RequestOptions $requestOptions ) {

		$subjectList =  $this->store->getPropertySubjects(
			$property,
			$dataItem,
			$requestOptions
		);

		// May return an iterator
		if ( $subjectList instanceof \Iterator ) {
			$subjectList = iterator_to_array( $subjectList );
		}

		$more = false;

		// Pop the +1 look ahead from the list
		if ( is_array( $subjectList ) && count( $subjectList ) > $this->listLimit ) {
			array_pop( $subjectList );
			$more = true;
		}

		$result = '';
		$this->itemCount = is_array( $subjectList ) ? count( $subjectList ) : 0;

		$callback = null;
		$message = wfMessage( 'smw-propertylist-count', $this->itemCount )->text();

		if ( $more ) {
			$callback = function() use ( $property, $dataItem ) {
				return \Html::element(
					'a',
					[
						'href' => \SpecialPage::getSafeTitleFor( 'SearchByProperty' )->getLocalURL( [
							'property' => $property->getLabel(),
							'value' => $dataItem->getDBKey()
						] )
					],
					wfMessage( 'smw_browse_more' )->text()
				);
			};

			$message = wfMessage( 'smw-propertylist-count-more-available', $this->itemCount )->text();
		}

		if ( $this->itemCount > 0 ) {
			$titleText = htmlspecialchars( str_replace( '_', ' ', $dataItem->getDBKey() ) );
			$result .= "<div id=\"{$this->listHeader}\">" . "\n<p>";

			$result .= $message . "</p>";
			$property = $this->checkProperty ? $property : null;

			if ( $this->itemCount < 6 ) {
				$result .= PageLister::getShortList( 0, $this->itemCount, $subjectList, $property, $callback );
			} else {
				$result .= PageLister::getColumnList( 0, $this->itemCount, $subjectList, $property, $callback );
			}

			$result .= "\n</div>";
		}

		return $result;
	}

}