Completed
Pull Request — master (#14)
by Jeroen De
16:10
created

setHideSubpageParentState()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace SBL;
4
5
use SMW\DIWikiPage;
6
use SMW\DataValueFactory;
7
use Title;
8
use Html;
9
use DummyLinker;
10
11
/**
12
 * @license GNU GPL v2+
13
 * @since 1.0
14
 *
15
 * @author mwjames
16
 */
17
class HtmlBreadcrumbLinksBuilder {
18
19
	/**
20
	 * @var ByPropertyHierarchicalLinksFinder
21
	 */
22
	private $byPropertyHierarchicalLinksFinder;
23
24
	/**
25
	 * @var BySubpageLinksFinder
26
	 */
27
	private $bySubpageLinksFinder;
28
29
	/**
30
	 * @var DummyLinker|null
31
	 */
32
	private $linker = null;
33
34
	/**
35
	 * @var string
36
	 */
37
	private $breadcrumbs = '';
38
39
	/**
40
	 * @var string
41
	 */
42
	private $breadcrumbTrailStyleClass = 'sbl-breadcrumb-trail-boxed';
43
44
	/**
45
	 * @var string
46
	 */
47
	private $breadcrumbDividerStyleClass = 'sbl-breadcrumb-arrow';
48
49
	/**
50
	 * @var boolean
51
	 */
52
	private $isRTL = false;
53
54
	/**
55
	 * @var boolean
56
	 */
57
	private $hasChildren = false;
58
59
	/**
60 6
	 * @var boolean
61 6
	 */
62 6
	private $hideSubpageParent = false;
63 6
64
	/**
65
	 * @since 1.0
66
	 *
67
	 * @param ByPropertyHierarchicalLinksFinder $byPropertyHierarchicalLinksFinder
68
	 * @param BySubpageLinksFinder $bySubpageLinksFinder
69
	 */
70 1
	public function __construct( ByPropertyHierarchicalLinksFinder $byPropertyHierarchicalLinksFinder, BySubpageLinksFinder $bySubpageLinksFinder ) {
71 1
		$this->byPropertyHierarchicalLinksFinder = $byPropertyHierarchicalLinksFinder;
72 1
		$this->bySubpageLinksFinder = $bySubpageLinksFinder;
73
	}
74
75
	/**
76
	 * @since 1.0
77
	 *
78
	 * @param DummyLinker $linker
79 1
	 */
80 1
	public function setLinker( DummyLinker $linker ) {
81 1
		$this->linker = $linker;
82
	}
83
84
	/**
85
	 * @since 1.0
86
	 *
87
	 * @param string $breadcrumbTrailStyleClass
88 1
	 */
89 1
	public function setBreadcrumbTrailStyleClass( $breadcrumbTrailStyleClass ) {
90 1
		$this->breadcrumbTrailStyleClass = $breadcrumbTrailStyleClass;
91
	}
92
93
	/**
94
	 * @since 1.0
95
	 *
96
	 * @param string $breadcrumbDividerStyleClass
97 1
	 */
98 1
	public function setBreadcrumbDividerStyleClass( $breadcrumbDividerStyleClass ) {
99 1
		$this->breadcrumbDividerStyleClass = $breadcrumbDividerStyleClass;
100
	}
101
102
	/**
103
	 * @since 1.3
104
	 *
105
	 * @param boolean $hideSubpageParent
106 4
	 */
107
	public function setHideSubpageParentState( $hideSubpageParent ) {
108 4
		$this->hideSubpageParent = $hideSubpageParent;
109 1
	}
110
111
	/**
112
	 * @since 1.0
113 3
	 *
114 3
	 * @param boolean $isRTL
115
	 */
116 3
	public function setRTLDirectionalityState( $isRTL ) {
117
		$this->isRTL = $isRTL;
118 3
	}
119 3
120
	/**
121 3
	 * @since  1.0
122 3
	 *
123
	 * @param Title $title
124 3
	 */
125
	public function buildBreadcrumbs( Title $title ) {
126 3
127 3
		if ( $title->isRedirect() ) {
128
			return;
129
		}
130
131
		// Ensure no subobject is used by replacing the fragment
132
		$title->setFragment( '' );
133
		$subject = DIWikiPage::newFromTitle( $title );
134 3
135
		$this->byPropertyHierarchicalLinksFinder->tryToFindLinksFor( $subject );
136 3
137 1
		$parents = $this->byPropertyHierarchicalLinksFinder->getParents();
138
		$children = $this->byPropertyHierarchicalLinksFinder->getChildren();
139
140 2
		$parents = $this->tryToUseSubpageHierarchyFallback(
141 2
			$subject,
142 2
			$parents
143 2
		);
144 2
145 2
		$this->formatToFlatList( $subject, $parents, $children );
146
	}
147
148 3
	/**
149
	 * @since 1.0
150 3
	 *
151 2
	 * @return string
152
	 */
153
	public function getHtml() {
154 1
155
		if ( $this->breadcrumbs === '' ) {
156 1
			return $this->breadcrumbs;
157
		}
158
159 3
		return Html::rawElement( 'div', array(
160
			'id'    => 'sbl-breadcrumbs',
161 3
			'class' => $this->breadcrumbTrailStyleClass,
162
			'dir'   => $this->isRTL ? 'rtl' : 'ltr' ),
163 3
			$this->breadcrumbs
164 2
		);
165 3
	}
166
167 3
	private function tryToUseSubpageHierarchyFallback( $subject, $parents ) {
168
169 3
		if ( $parents !== array() || !$this->bySubpageLinksFinder->canUseSubpageDiscoveryForFallback() ) {
170 2
			return $parents;
171 2
		}
172 3
173
		$this->bySubpageLinksFinder->tryToFindLinksFor( $subject );
174 2
175
		return $this->bySubpageLinksFinder->getParents();
176 2
	}
177
178 2
	private function formatToFlatList( DIWikiPage $subject, $parents, $children ) {
179
180 2
		$parent = '';
181
182
		foreach ( $parents as $breadcrumb ) {
183 2
			$parent .= $this->wrapHtml( 'parent', $this->getDvShortHtmlText( $breadcrumb, $this->linker ) ) .  $this->wrapHtml( 'right' );
184 2
		}
185 2
186
		$this->hasChildren = count( $children ) > 1;
187 2
188
		list( $child, $data ) = $this->findElementsForChildren( $children  );
189
190 3
		if ( $parent !== '' || $child !== '' ) {
191
			$this->breadcrumbs = $parent . $this->wrapHtml( 'location', $this->getDvShortHtmlText( $subject ) ) . $child . $this->addHtmlDataElement( $data );
192 3
		}
193 3
	}
194
195 3
	private function getDvShortHtmlText( $subject, $linker = null ) {
196
197
		$dataValue = DataValueFactory::getInstance()->newDataItemValue(
198
			$subject
199 1
		);
200 1
201 1
		$dataValue->setCaption(
202
			$this->hideSubpageParent ? $subject->getTitle()->getSubpageText() : false
203
		);
204 1
205 3
		return $dataValue->getShortHtmlText( $linker );
206
	}
207 3
208
	private function wrapHtml( $subClass, $html = '' ) {
209
		return Html::rawElement( 'span', array(
210 1
				'class' => $this->breadcrumbDividerStyleClass . '-' . $subClass,
211 1
				'style' => $subClass === 'child' && $this->hasChildren ? 'font-style:italic;' : ''
212
			),
213
			$html
214 2
		);
215
	}
216 2
217 1
	private function findElementsForChildren( array $children ) {
218
219
		$child = '';
220 1
		$data = '';
221 1
222 1
		foreach ( $children as $breadcrumb ) {
223
224 1
			// The first child is added as visible element while others
225
			// are added as data-element
226
			if ( $child !== '' ) {
227
				$this->hasChildren = true;
228
				$data .=  $this->addHtmlListElement( $this->getDvShortHtmlText( $breadcrumb, $this->linker ) );
229
				continue;
230
			}
231
232
			$child .=  $this->wrapHtml( 'left' ) . $this->wrapHtml( 'child', $this->getDvShortHtmlText( $breadcrumb, $this->linker ) );
233
		}
234
235
		return array( $child, $data );
236
	}
237
238
	private function addHtmlListElement( $html = '' ) {
239
		return Html::rawElement( 'li', array(), $html );
240
	}
241
242
	private function addHtmlDataElement( $data = '' ) {
243
244
		if ( $data === '' ) {
245
			return '';
246
		}
247
248
		return Html::rawElement( 'span', array(
249
			'class' => 'sbl-breadcrumb-children',
250
			'data-children' => $data ),
251
			''
252
		);
253
	}
254
255
}
256