Completed
Push — master ( 552173...c8702c )
by Tobias
12:00 queued 43s
created

Modal::placeMe()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 31
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 25
cts 25
cp 1
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 24
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * Contains the component class for rendering a modal.
4
 *
5
 * @copyright (C) 2018, Tobias Oetterer, Paderborn University
6
 * @license       https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License, version 3 (or later)
7
 *
8
 * This file is part of the MediaWiki extension BootstrapComponents.
9
 * The BootstrapComponents extension is free software: you can redistribute it
10
 * and/or modify it under the terms of the GNU General Public License as published
11
 * by the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * The BootstrapComponents extension is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
21
 *
22
 * @file
23
 * @ingroup       BootstrapComponents
24
 * @author        Tobias Oetterer
25
 */
26
27
namespace BootstrapComponents\Components;
28
29
use BootstrapComponents\AbstractComponent;
30
use BootstrapComponents\ApplicationFactory;
31
use BootstrapComponents\ModalBuilder;
32
use \Html;
33
34
/**
35
 * Class Modal
36
 *
37
 * Class for component 'modal'
38
 *
39
 * @see   https://github.com/oetterer/BootstrapComponents/blob/master/docs/components.md#Modal
40
 * @since 1.0
41
 */
42
class Modal extends AbstractComponent {
43
	/**
44
	 * @inheritdoc
45
	 *
46
	 * @param string $input
47
	 */
48 6
	public function placeMe( $input ) {
49 6
		list ( $outerClass, $style ) = $this->processCss( [], [] );
50
51 6
		list ( $returnCode, $text ) = $this->generateTrigger();
52 6
		if ( !$returnCode ) {
53 2
			return $text;
54
		}
55
56 5
		$safeInput = $this->prepareInput(
57 5
			$this->getParserRequest(),
58
			true
59 5
		);
60
61 5
		$modal = ApplicationFactory::getInstance()->getModalBuilder(
62 5
			$this->getId(),
63 5
			$text,
64 5
			$safeInput,
65 5
			$this->getParserOutputHelper()
66 5
		);
67 5
		return $modal->setOuterClass(
68 5
			$this->arrayToString( $outerClass, ' ' )
69 5
		)->setOuterStyle(
70 5
			$this->arrayToString( $style, ';' )
71 5
		)->setDialogClass(
72 5
			$this->calculateInnerClass()
73 5
		)->setHeader(
74 5
			(string)$this->getValueFor( 'heading' )
75 5
		)->setFooter(
76 5
			(string)$this->getValueFor( 'footer' )
77 5
		)->parse();
78
	}
79
80
	/**
81
	 * Calculates the css class string from the attributes array for the "inner" section (div around body and heading).
82
	 *
83
	 * @return false|string
84
	 */
85 5
	private function calculateInnerClass() {
86
87 5
		$class = [];
88
89 5
		if ( $size = $this->getValueFor( 'size' ) ) {
90 2
			$class[] = 'modal-' . $size;
91 2
		}
92 5
		return $this->arrayToString( $class, ' ' );
93
	}
94
95
	/**
96
	 * Spawns the button for the modal trigger.
97
	 *
98
	 * @param string $text
99
	 *
100
	 * @return string
101
	 */
102 3
	private function generateButton( $text ) {
103 3
		return Html::rawElement(
104 3
			'button',
105
			[
106 3
				'type'        => 'button',
107 3
				'class'       => 'modal-trigger btn btn-' . $this->getValueFor( 'color', 'default' ),
108 3
				'data-toggle' => 'modal',
109 3
				'data-target' => '#' . $this->getId(),
110 3
			],
111
			$text
112 3
		);
113
	}
114
115
	/**
116
	 * Generate the trigger element (button or image).
117
	 *
118
	 * @return array    (bool)return code, (string) message/trigger,
119
	 */
120 6
	private function generateTrigger() {
121 6
		$text = (string)$this->getValueFor( 'text' );
122 6
		if ( empty( $text ) ) {
123 2
			return [ false, $this->getParserOutputHelper()->renderErrorMessage( 'bootstrap-components-modal-text-missing' ) ];
124
		}
125 5
		if ( preg_match( '~Special:Upload~', $text ) ) {
126 1
			return [ false, $text ];
127
		}
128 5
		if ( !preg_match( '~^(.*)<a.+href=[^>]+>(.+)</a>(.*)$~', $text, $matches )
129 5
			&& !preg_match( '~(^.*<img.*src=.+>.*)$~', $text, $matches )
130 5
		) {
131 3
			return [ true, $this->generateButton( $text ) ];
132
		}
133 3
		array_shift( $matches );
134 3
		return [ true, ModalBuilder::wrapTriggerElement( implode( '', $matches ), $this->getId() ) ];
135
	}
136
}