Completed
Push — master ( a848da...37f5b2 )
by Henry
05:28
created

modules/Dialog/Dialog.php (6 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Redaxscript\Modules\Dialog;
3
4
use Redaxscript\Head;
5
use Redaxscript\Html;
6
use Redaxscript\Module;
7
8
/**
9
 * shared module to replace alert, confirm and prompt
10
 *
11
 * @since 4.0.0
12
 *
13
 * @package Redaxscript
14
 * @category Modules
15
 * @author Henry Ruhs
16
 */
17
18
class Dialog extends Module\Module
19
{
20
	/**
21
	 * array of the module
22
	 *
23
	 * @var array
24
	 */
25
26
	protected static $_moduleArray =
27
	[
28
		'name' => 'Dialog',
29
		'alias' => 'Dialog',
30
		'author' => 'Redaxmedia',
31
		'description' => 'Shared module to replace alert, confirm and prompt',
32
		'version' => '4.3.0'
33
	];
34
35
	/**
36
	 * array of the option
37
	 *
38
	 * @var array
39
	 */
40
41
	protected $_optionArray =
42
	[
43
		'className' =>
44
		[
45
			'overlay' => 'rs-overlay-dialog',
46
			'component' => 'rs-component-dialog',
47
			'title' => 'rs-title-dialog',
48
			'box' => 'rs-box-dialog',
49
			'text' => 'rs-text-dialog',
50
			'field' => 'rs-js-input rs-field-default rs-field-text',
51
			'button' => 'rs-button-default',
52
			'buttonOk' => 'rs-js-ok',
53
			'buttonCancel' => 'rs-js-cancel'
54
		]
55
	];
56
57
	/**
58
	 * renderStart
59
	 *
60
	 * @since 4.0.0
61
	 */
62
63
	public function renderStart() : void
64
	{
65
		$firstParameter = $this->_registry->get('firstParameter');
66
		$secondParameter = $this->_registry->get('secondParameter');
67
		$thirdParameter = $this->_registry->get('thirdParameter');
68
69
		/* link */
70
71
		$link = Head\Link::getInstance();
72
		$link
73
			->init()
74
			->appendFile('modules/Dialog/dist/styles/dialog.min.css');
75
76
		/* script */
77
78
		$script = Head\Script::getInstance();
79
		$script
80
			->init('foot')
81
			->appendFile(
82
			[
83
				'modules/Dialog/assets/scripts/init.js',
84
				'modules/Dialog/dist/scripts/dialog.min.js'
85
			]);
86
87
		/* router */
88
89
		if ($firstParameter === 'module' && ($secondParameter === 'dialog' || $secondParameter === 'admin-dialog'))
90
		{
91
			$this->_registry->set('renderBreak', true);
92
			$dialog = $secondParameter === 'admin-dialog' ? new Admin\Dialog($this->_registry, $this->_request, $this->_language, $this->_config) : $this;
93
			$message = $this->_request->getStream('message');
94
			$title = $this->_request->getStream('title');
95
96
			/* run as needed */
97
98
			if ($thirdParameter === 'alert')
99
			{
100
				echo $dialog->alert($message, $title);
0 ignored issues
show
It seems like $message defined by $this->_request->getStream('message') on line 93 can also be of type array; however, Redaxscript\Modules\Dialog\Dialog::alert() does only seem to accept null|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
It seems like $title defined by $this->_request->getStream('title') on line 94 can also be of type array; however, Redaxscript\Modules\Dialog\Dialog::alert() does only seem to accept null|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
101
			}
102
			if ($thirdParameter === 'confirm')
103
			{
104
				echo $dialog->confirm($message, $title);
0 ignored issues
show
It seems like $message defined by $this->_request->getStream('message') on line 93 can also be of type array; however, Redaxscript\Modules\Dialog\Dialog::confirm() does only seem to accept null|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
It seems like $title defined by $this->_request->getStream('title') on line 94 can also be of type array; however, Redaxscript\Modules\Dialog\Dialog::confirm() does only seem to accept null|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
105
			}
106
			if ($thirdParameter === 'prompt')
107
			{
108
				echo $dialog->prompt($message, $title);
0 ignored issues
show
It seems like $message defined by $this->_request->getStream('message') on line 93 can also be of type array; however, Redaxscript\Modules\Dialog\Dialog::prompt() does only seem to accept null|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
It seems like $title defined by $this->_request->getStream('title') on line 94 can also be of type array; however, Redaxscript\Modules\Dialog\Dialog::prompt() does only seem to accept null|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
109
			}
110
		}
111
	}
112
113
	/**
114
	 * alert
115
	 *
116
	 * @since 4.0.0
117
	 *
118
	 * @param string $message message of the alert
119
	 * @param string $title title of the alert
120
	 *
121
	 * @return string
122
	 */
123
124
	public function alert(string $message = null, string $title = null) : string
125
	{
126
		return $this->_dialog('alert', $message, $title ? : $this->_language->get('_dialog')['alert']);
127
	}
128
129
	/**
130
	 * confirm
131
	 *
132
	 * @since 4.0.0
133
	 *
134
	 * @param string $message message of the confirm
135
	 * @param string $title title of the confirm
136
	 *
137
	 * @return string
138
	 */
139
140
	public function confirm(string $message = null, string $title = null) : string
141
	{
142
		return $this->_dialog('confirm', $message, $title ? : $this->_language->get('_dialog')['confirm']);
143
	}
144
145
	/**
146
	 * prompt
147
	 *
148
	 * @since 4.0.0
149
	 *
150
	 * @param string $message message of the prompt
151
	 * @param string $title title of the prompt
152
	 *
153
	 * @return string
154
	 */
155
156
	public function prompt(string $message = null, string $title = null) : string
157
	{
158
		return $this->_dialog('prompt', $message, $title ? : $this->_language->get('_dialog')['prompt']);
159
	}
160
161
	/**
162
	 * dialog
163
	 *
164
	 * @since 4.0.0
165
	 *
166
	 * @param string $type type of the dialog
167
	 * @param string $message message of the dialog
168
	 * @param string $title title of the dialog
169
	 *
170
	 * @return string
171
	 */
172
173
	protected function _dialog(string $type = null, string $message = null, string $title = null) : string
174
	{
175
		$output = null;
176
177
		/* html elements */
178
179
		$element = new Html\Element();
180
		$overlayElement = $element
181
			->copy()
182
			->init('div',
183
			[
184
				'class' => $this->_optionArray['className']['overlay']
185
			]);
186
		$dialogElement = $element
187
			->copy()
188
			->init('div',
189
			[
190
				'class' => $this->_optionArray['className']['component']
191
			]);
192
		$titleElement = $element
193
			->copy()
194
			->init('h3',
195
			[
196
				'class' => $this->_optionArray['className']['title']
197
			])
198
			->text($title);
199
		$boxElement = $element
200
			->copy()
201
			->init('div',
202
			[
203
				'class' => $this->_optionArray['className']['box']
204
			]);
205
		$textElement = $message ? $element
206
			->copy()
207
			->init('p',
208
			[
209
				'class' => $this->_optionArray['className']['text']
210
			])
211
			->text($message) : null;
212
		$fieldElement = $type === 'prompt' ? $element
213
			->copy()
214
			->init('input',
215
			[
216
				'class' => $this->_optionArray['className']['field']
217
			]) : null;
218
		$buttonElement = $element
219
			->copy()
220
			->init('button',
221
			[
222
				'class' => $this->_optionArray['className']['button']
223
			]);
224
		$buttonOkElement = $buttonElement
225
			->copy()
226
			->addClass($this->_optionArray['className']['buttonOk'])
227
			->text($this->_language->get('ok'));
228
		$buttonCancelElement = $type === 'confirm' || $type === 'prompt' ? $buttonElement
229
			->copy()
230
			->addClass($this->_optionArray['className']['buttonCancel'])
231
			->text($this->_language->get('cancel')) : null;
232
233
		/* collect output */
234
235
		$output = $overlayElement->html(
236
			$dialogElement->html(
237
				$titleElement .
238
				$boxElement->html(
239
					$textElement .
240
					$fieldElement .
241
					$buttonOkElement .
242
					$buttonCancelElement
243
				)
244
			)
245
		);
246
		return $output;
247
	}
248
}
249