Passed
Pull Request — develop (#890)
by Tito
03:54
created

JFormFieldRmedia::getInput()   F

Complexity

Conditions 38
Paths > 20000

Size

Total Lines 261
Code Lines 157

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 157
c 1
b 0
f 0
dl 0
loc 261
rs 0
cc 38
nc 453378048
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @package     Redcore
4
 * @subpackage  Fields
5
 *
6
 * @copyright   Copyright (C) 2008 - 2020 redWEB.dk. All rights reserved.
7
 * @license     GNU General Public License version 2 or later, see LICENSE.
8
 */
9
10
use Joomla\Utilities\ArrayHelper;
11
12
defined('JPATH_REDCORE') or die;
13
14
/**
15
 * Media field.
16
 *
17
 * @package     Redcore
18
 * @subpackage  Fields
19
 * @since       1.0
20
 */
21
class JFormFieldRmedia extends JFormField
22
{
23
	/**
24
	 * The form field type.
25
	 *
26
	 * @var  string
27
	 */
28
	protected $type = 'Rmedia';
29
30
	/**
31
	 * The initialised state of the document object.
32
	 *
33
	 * @var  boolean
34
	 */
35
	protected static $initialised = false;
36
37
	/**
38
	 * Method to get the field input markup for a media selector.
39
	 * Use attributes to identify specific created_by and asset_id fields
40
	 *
41
	 * @return  string  The field input markup.
42
	 *
43
	 * @todo    Create a layout and put all the output HTML there!
44
	 */
45
	protected function getInput()
46
	{
47
		$bootstrapVersion = RHtmlMedia::getFramework();
48
		$assetField       = $this->element['asset_field'] ? (string) $this->element['asset_field'] : 'asset_id';
49
		$authorField      = $this->element['created_by_field'] ? (string) $this->element['created_by_field'] : 'created_by';
50
		$asset            = $this->form->getValue($assetField) ? $this->form->getValue($assetField) : (string) $this->element['asset_id'];
51
52
		if ($asset == '')
53
		{
54
			$asset = JFactory::getApplication()->input->get('option');
55
		}
56
57
		$link = (string) $this->element['link'];
58
59
		$modalTitle = isset($this->element['modal_title']) ? JText::_($this->element['modal_title']) : JText::_('LIB_REDCORE_MEDIA_MANAGER');
60
61
		$modalId = 'modal-' . $this->id;
62
63
		if (!self::$initialised)
64
		{
65
			// Build the script.
66
			$script   = array();
67
			$script[] = '	function jInsertFieldValue(value, id) {';
68
			$script[] = '		var old_value = document.getElementById(id).value;';
69
			$script[] = '		if (value && old_value != value) {';
70
			$script[] = '			var elem = document.getElementById(id);';
71
			$script[] = '			elem.value = value;';
72
			$script[] = '			var changeEvent = document.createEvent("HTMLEvents");';
73
			$script[] = '			changeEvent.initEvent("change", true, true);';
74
			$script[] = '			elem.dispatchEvent(changeEvent);';
75
			$script[] = '			jMediaRefreshPreview(id);';
76
			$script[] = '		};';
77
			$script[] = '		jQuery("#' . $modalId . '").modal("hide");';
78
			$script[] = '	}';
79
80
			$script[] = '	function jMediaRefreshPreview(id) {';
81
			$script[] = '		var value = document.getElementById(id).value;';
82
			$script[] = '		var img = document.getElementById(id + "_preview");';
83
			$script[] = '		if (img) {';
84
			$script[] = '			if (value) {';
85
			$script[] = '				img.src = "' . JUri::root() . '" + value;';
86
			$script[] = '				document.getElementById(id + "_preview_empty").style.display = "none";';
87
			$script[] = '				document.getElementById(id + "_preview_img").style.display = "";';
88
			$script[] = '			} else { ';
89
			$script[] = '				img.src = ""';
90
			$script[] = '				document.getElementById(id + "_preview_empty").style.display = "";';
91
			$script[] = '				document.getElementById(id + "_preview_img").style.display = "none";';
92
			$script[] = '			} ';
93
			$script[] = '}}';
94
95
			$script[] = '	function jSetIframeHeight(iframe)';
96
			$script[] = '	{';
97
			$script[] = '		var newheight;';
98
			$script[] = '		if(iframe) {';
99
			$script[] = '			newheight = iframe.contentWindow.document.body.scrollHeight;';
100
			$script[] = '			iframe.height= (newheight) + "px";';
101
			$script[] = '			iframe.style.maxHeight = iframe.height';
102
			$script[] = '		}';
103
			$script[] = '	}';
104
105
			$script[] = "
106
				function closeModal(fieldId)
107
				{
108
					jQuery('#modal-' + fieldId).modal('hide');
109
				}
110
			";
111
112
			// Add the script to the document head.
113
			JFactory::getDocument()->addScriptDeclaration(implode("\n", $script));
114
115
			self::$initialised = true;
116
		}
117
118
		$html = array();
119
		$attr = '';
120
121
		// Initialize some field attributes.
122
		$attr .= $this->element['class'] ? ' class="' . (string) $this->element['class'] . '"' : '';
123
		$attr .= $this->element['size'] ? ' size="' . (int) $this->element['size'] . '"' : '';
124
125
		// Initialize JavaScript field attributes.
126
		$attr .= $this->element['onchange'] ? ' onchange="' . (string) $this->element['onchange'] . '"' : '';
127
128
		$inputClass = $bootstrapVersion == 'bootstrap2' ? 'input-prepend input-append' : 'input-group';
129
130
		// The text field.
131
		$html[] = '<div class="' . $inputClass . '">';
132
133
		// The Preview.
134
		$preview       = (string) $this->element['preview'];
135
		$showPreview   = true;
136
		$showAsTooltip = false;
137
138
		switch ($preview)
139
		{
140
			case 'no': // Deprecated parameter value
141
			case 'false':
142
			case 'none':
143
				$showPreview = false;
144
				break;
145
146
			case 'yes': // Deprecated parameter value
147
			case 'true':
148
			case 'show':
149
				break;
150
151
			case 'tooltip':
152
			default:
153
				$showAsTooltip = true;
154
				JHtml::_('rbootstrap.framework');
155
156
				$script = '
157
				jQuery(document).ready(function() {
158
				jQuery("#popover_' . $this->id . '").popover({
159
					content: function(){
160
						var id = jQuery(this).data("id");
161
						return jQuery("#"+id+\'_hidden_content\').html();
162
					},
163
					html: true,
164
					placement: "bottom",
165
					trigger: "hover focus",
166
					toggle: "popover",
167
					container: "body"
168
				});
169
			});
170
				';
171
172
			JFactory::getDocument()
173
				->addScriptDeclaration($script);
174
175
				break;
176
		}
177
178
		if ($showPreview)
179
		{
180
			if ($this->value && file_exists(JPATH_ROOT . '/' . $this->value))
181
			{
182
				$src = JUri::root() . $this->value;
183
			}
184
			else
185
			{
186
				$src = '';
187
			}
188
189
			$width  = isset($this->element['preview_width']) ? (int) $this->element['preview_width'] : 300;
190
			$height = isset($this->element['preview_height']) ? (int) $this->element['preview_height'] : 200;
191
192
			if ($showAsTooltip)
193
			{
194
				$width = $width > 245 ? 245 : $width;
195
			}
196
197
			$style  = '';
198
			$style .= ($width > 0) ? 'max-width:' . $width . 'px;' : '';
199
			$style .= ($height > 0) ? 'max-height:' . $height . 'px;' : '';
200
201
			$imgattr         = array(
202
				'id' => $this->id . '_preview',
203
				'class' => 'media-preview',
204
				'style' => $style,
205
			);
206
			$img             = JHtml::image($src, JText::_('JLIB_FORM_MEDIA_PREVIEW_ALT'), $imgattr);
207
			$previewImg      = '<div id="' . $this->id . '_preview_img"' . ($src ? '' : ' style="display:none"') . '>' . $img . '</div>';
208
			$previewImgEmpty = '<div id="' . $this->id . '_preview_empty"' . ($src ? ' style="display:none"' : '') . '>'
209
				. JText::_('JLIB_FORM_MEDIA_PREVIEW_EMPTY') . '</div>';
210
211
			if ($showAsTooltip)
212
			{
213
				$options = [
214
					'class' => 'hasPopoverPreview media-preview add-on input-group-addon',
215
					'id' => 'popover_' . $this->id,
216
					'data-id' => $this->id
217
				];
218
219
				if ($bootstrapVersion == 'bootstrap2')
220
				{
221
					$text = '<i class="icon-eye-open"></i>';
222
				}
223
				else
224
				{
225
					$text = '<i class="glyphicon glyphicon-eye-open"></i>';
226
				}
227
228
				$html[] = '<div ' . ArrayHelper::toString($options) . '>' . $text . '</div>';
229
				$html[] = '<div id="' . $this->id . '_hidden_content" class="rMediaHiddenContent" style="display:none">' . $previewImgEmpty . $previewImg . '</div>';
230
			}
231
			else
232
			{
233
				$html[] = '<div class="media-preview add-on input-group-addon" style="height:auto">';
234
				$html[] = ' ' . $previewImgEmpty;
235
				$html[] = ' ' . $previewImg;
236
				$html[] = '</div>';
237
			}
238
		}
239
240
		$inputClass = $bootstrapVersion == 'bootstrap2' ? 'input-small' : 'input-sm';
241
242
		$html[] = '	<input type="text" class="' . $inputClass . '" name="' . $this->name . '" id="' . $this->id . '" value="'
243
			. htmlspecialchars($this->value, ENT_COMPAT, 'UTF-8') . '" readonly="readonly"' . $attr . ' />';
244
245
		$directory = (string) $this->element['directory'];
246
247
		if ($this->value && file_exists(JPATH_ROOT . '/' . $this->value))
248
		{
249
			$folder = explode('/', $this->value);
250
			$folder = array_diff_assoc($folder, explode('/', JComponentHelper::getParams('com_media')->get('image_path', 'images')));
251
			array_pop($folder);
252
			$folder = implode('/', $folder);
253
		}
254
		elseif (file_exists(JPATH_ROOT . '/' . JComponentHelper::getParams('com_media')->get('image_path', 'images') . '/' . $directory))
255
		{
256
			$folder = $directory;
257
		}
258
		else
259
		{
260
			$folder = '';
261
		}
262
263
		$link = ($link ? $link : 'index.php?option=com_media&amp;view=images&amp;layout=modal&amp;tmpl=component&amp;asset='
264
				. $asset . '&amp;author=' . $this->form->getValue($authorField)) . '&amp;fieldid='
265
			. $this->id . '&amp;folder=' . $folder
266
			. '&amp;redcore=true';
267
268
		$hideModal = $bootstrapVersion == 'bootstrap2' ? 'modal hide' : 'modal';
269
		$style     = $bootstrapVersion == 'bootstrap2' ? 'width: 820px; height: 500px; margin-left: -410px; top: 50%; margin-top: -250px;' : '';
270
271
		// Create the modal object
272
		$modal = RModal::getInstance(
273
			array(
274
				'attribs' => array(
275
					'id'    => $modalId,
276
					'class' => $hideModal,
277
					'style' => $style,
278
					'tabindex' => '-1',
279
					'role' => 'dialog'
280
				),
281
				'params' => array(
282
					'showHeader'      => true,
283
					'showFooter'      => false,
284
					'showHeaderClose' => true,
285
					'title' => $modalTitle,
286
					'link' => $link,
287
					'events' => array (
288
						'onload' => 'jSetIframeHeight'
289
					)
290
				)
291
			),
292
			$modalId
0 ignored issues
show
Bug introduced by
$modalId of type string is incompatible with the type integer expected by parameter $id of RDomObject::getInstance(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

292
			/** @scrutinizer ignore-type */ $modalId
Loading history...
293
		);
294
295
		$html[] = RLayoutHelper::render(
296
			'fields.rmedia',
297
			array(
0 ignored issues
show
Bug introduced by
array('modal' => $modal, 'field' => $this) of type array<string,JFormFieldRmedia|RDomObject> is incompatible with the type object expected by parameter $displayData of RLayoutHelper::render(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

297
			/** @scrutinizer ignore-type */ array(
Loading history...
298
				'modal' => $modal,
299
				'field' => $this
300
			)
301
		);
302
303
		$html[] = '</div>';
304
305
		return implode("\n", $html);
306
	}
307
}
308