RedcoreModelConfig::getInstalledExtensions()   F
last analyzed

Complexity

Conditions 17
Paths 288

Size

Total Lines 91
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 44
c 0
b 0
f 0
dl 0
loc 91
rs 3.2833
cc 17
nc 288
nop 4

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.Backend
4
 * @subpackage  Models
5
 *
6
 * @copyright   Copyright (C) 2008 - 2021 redWEB.dk. All rights reserved.
7
 * @license     GNU General Public License version 2 or later, see LICENSE.
8
 */
9
10
defined('_JEXEC') or die;
11
12
/**
13
 * Config Model
14
 *
15
 * @package     Redcore.Backend
16
 * @subpackage  Models
17
 * @since       1.0
18
 */
19
class RedcoreModelConfig extends RModelAdmin
20
{
21
	/**
22
	 * Method to get a form object.
23
	 *
24
	 * @param   array    $data      Data for the form.
25
	 * @param   boolean  $loadData  True if the form is to load its own data (default case), false if not.
26
	 *
27
	 * @return  mixed  A JForm object on success, false on failure
28
	 */
29
	public function getForm($data = array(), $loadData = true)
30
	{
31
		$option = JFactory::getApplication()->input->getString('component');
32
33
		// Add the search path for the admin component config.xml file.
34
		JForm::addFormPath(JPATH_ADMINISTRATOR . '/components/' . $option);
35
36
		// Get the form.
37
		/** @var RForm $form */
38
		$form = $this->loadForm(
39
			'com_redcore.config',
40
			'config',
41
			array('control' => 'jform', 'load_data' => $loadData),
42
			false,
43
			'/config'
44
		);
45
46
		if (empty($form))
47
		{
48
			/** @var RForm $form */
49
			$form = $this->loadForm(
50
				'com_redcore.translations',
51
				'config',
52
				array('control' => 'jform', 'load_data' => $loadData),
53
				false,
54
				'/config'
55
			);
56
		}
57
		else
58
		{
59
			$form->loadFile('translations', false, '/config');
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type string expected by parameter $reset of JForm::loadFile(). ( Ignorable by Annotation )

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

59
			$form->loadFile('translations', /** @scrutinizer ignore-type */ false, '/config');
Loading history...
60
		}
61
62
		if (empty($form))
63
		{
64
			return false;
65
		}
66
67
		return $form;
68
	}
69
70
	/**
71
	 * Get the associated JTable
72
	 *
73
	 * @param   string  $name    Table name
74
	 * @param   string  $prefix  Table prefix
75
	 * @param   array   $config  Configuration array
76
	 *
77
	 * @return  JTable
78
	 */
79
	public function getTable($name = null, $prefix = '', $config = array())
80
	{
81
		$name = !empty($name) ? $name : 'Extension';
82
		$prefix = !empty($prefix) ? $prefix : 'JTable';
83
84
		return parent::getTable($name, $prefix, $config);
85
	}
86
87
	/**
88
	 * Get the component information.
89
	 *
90
	 * @param   string  $option  Option name
91
	 *
92
	 * @return  object
93
	 */
94
	public function getComponent($option)
95
	{
96
		$this->loadExtensionLanguage($option, $option);
97
		$this->loadExtensionLanguage($option, $option . '.sys');
98
		$component = JComponentHelper::getComponent($option);
99
		$component->option = $option;
0 ignored issues
show
Documentation Bug introduced by
The property $option was declared of type integer, but $option is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
100
		$component->xml = RComponentHelper::getComponentManifestFile($option);
101
102
		return $component;
103
	}
104
105
	/**
106
	 * Load specific language file.
107
	 *
108
	 * @param   string  $option         Option name
109
	 * @param   string  $extensionFile  Extension File Name
110
	 *
111
	 * @return  object
112
	 */
113
	public function loadExtensionLanguage($option, $extensionFile)
114
	{
115
		// Load common and local language files.
116
		$lang = JFactory::getLanguage();
117
118
		// Load language file
119
		$lang->load($extensionFile, JPATH_BASE, null, false, false)
120
		|| $lang->load($extensionFile, JPATH_BASE . "/components/$option", null, false, false)
121
		|| $lang->load($extensionFile, JPATH_BASE, $lang->getDefault(), false, false)
122
		|| $lang->load($extensionFile, JPATH_BASE . "/components/$option", $lang->getDefault(), false, false);
123
	}
124
125
	/**
126
	 * Method to save the configuration data.
127
	 *
128
	 * @param   array  $data  An array containing all global config data.
129
	 *
130
	 * @return  bool   True on success, false on failure.
131
	 */
132
	public function save($data)
133
	{
134
		$dispatcher = RFactory::getDispatcher();
135
		$table = JTable::getInstance('Extension');
136
		$option = JFactory::getApplication()->input->getString('component');
137
		$isNew = true;
138
139
		// Save the rules.
140
		if (isset($data['params']) && isset($data['params']['rules']))
141
		{
142
			$rules = new JAccessRules($data['params']['rules']);
143
			$asset = JTable::getInstance('asset');
144
145
			if (!$asset->loadByName($option))
0 ignored issues
show
Bug introduced by
The method loadByName() does not exist on JTable. It seems like you code against a sub-type of JTable such as JTableAsset. ( Ignorable by Annotation )

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

145
			if (!$asset->/** @scrutinizer ignore-call */ loadByName($option))
Loading history...
146
			{
147
				$root = JTable::getInstance('asset');
148
				$root->loadByName('root.1');
149
				$asset->name = $option;
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
150
				$asset->title = $option;
0 ignored issues
show
Bug Best Practice introduced by
The property title does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
151
				$asset->setLocation($root->id, 'last-child');
0 ignored issues
show
Bug introduced by
The method setLocation() does not exist on JTable. It seems like you code against a sub-type of JTable such as JTableNested. ( Ignorable by Annotation )

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

151
				$asset->/** @scrutinizer ignore-call */ 
152
            setLocation($root->id, 'last-child');
Loading history...
152
			}
153
154
			$asset->rules = (string) $rules;
0 ignored issues
show
Bug Best Practice introduced by
The property rules does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
155
156
			if (!$asset->check() || !$asset->store())
157
			{
158
				$this->setError($asset->getError());
0 ignored issues
show
Deprecated Code introduced by
The function JObject::setError() has been deprecated: 12.3 JError has been deprecated ( Ignorable by Annotation )

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

158
				/** @scrutinizer ignore-deprecated */ $this->setError($asset->getError());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Deprecated Code introduced by
The function JObject::getError() has been deprecated: 12.3 JError has been deprecated ( Ignorable by Annotation )

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

158
				$this->setError(/** @scrutinizer ignore-deprecated */ $asset->getError());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
159
160
				return false;
161
			}
162
163
			// We don't need this anymore
164
			unset($data['params']['rules']);
165
		}
166
167
		// Load the previous Data
168
		if (!$table->load($data['id']))
169
		{
170
			$this->setError($table->getError());
0 ignored issues
show
Deprecated Code introduced by
The function JObject::setError() has been deprecated: 12.3 JError has been deprecated ( Ignorable by Annotation )

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

170
			/** @scrutinizer ignore-deprecated */ $this->setError($table->getError());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Deprecated Code introduced by
The function JObject::getError() has been deprecated: 12.3 JError has been deprecated ( Ignorable by Annotation )

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

170
			$this->setError(/** @scrutinizer ignore-deprecated */ $table->getError());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
171
172
			return false;
173
		}
174
175
		unset($data['id']);
176
177
		// Bind the data.
178
		if (!$table->bind($data))
179
		{
180
			$this->setError($table->getError());
0 ignored issues
show
Deprecated Code introduced by
The function JObject::setError() has been deprecated: 12.3 JError has been deprecated ( Ignorable by Annotation )

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

180
			/** @scrutinizer ignore-deprecated */ $this->setError($table->getError());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Deprecated Code introduced by
The function JObject::getError() has been deprecated: 12.3 JError has been deprecated ( Ignorable by Annotation )

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

180
			$this->setError(/** @scrutinizer ignore-deprecated */ $table->getError());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
181
182
			return false;
183
		}
184
185
		// Check the data.
186
		if (!$table->check())
187
		{
188
			$this->setError($table->getError());
0 ignored issues
show
Deprecated Code introduced by
The function JObject::getError() has been deprecated: 12.3 JError has been deprecated ( Ignorable by Annotation )

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

188
			$this->setError(/** @scrutinizer ignore-deprecated */ $table->getError());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Deprecated Code introduced by
The function JObject::setError() has been deprecated: 12.3 JError has been deprecated ( Ignorable by Annotation )

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

188
			/** @scrutinizer ignore-deprecated */ $this->setError($table->getError());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
189
190
			return false;
191
		}
192
193
		// Trigger the onConfigurationBeforeSave event.
194
		$result = $dispatcher->trigger($this->event_before_save, array($this->option . '.' . $this->name, $table, $isNew));
195
196
		if (in_array(false, $result, true))
197
		{
198
			$this->setError($table->getError());
0 ignored issues
show
Deprecated Code introduced by
The function JObject::setError() has been deprecated: 12.3 JError has been deprecated ( Ignorable by Annotation )

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

198
			/** @scrutinizer ignore-deprecated */ $this->setError($table->getError());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Deprecated Code introduced by
The function JObject::getError() has been deprecated: 12.3 JError has been deprecated ( Ignorable by Annotation )

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

198
			$this->setError(/** @scrutinizer ignore-deprecated */ $table->getError());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
199
200
			return false;
201
		}
202
203
		// Store the data.
204
		if (!$table->store())
205
		{
206
			$this->setError($table->getError());
0 ignored issues
show
Deprecated Code introduced by
The function JObject::setError() has been deprecated: 12.3 JError has been deprecated ( Ignorable by Annotation )

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

206
			/** @scrutinizer ignore-deprecated */ $this->setError($table->getError());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Deprecated Code introduced by
The function JObject::getError() has been deprecated: 12.3 JError has been deprecated ( Ignorable by Annotation )

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

206
			$this->setError(/** @scrutinizer ignore-deprecated */ $table->getError());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
207
208
			return false;
209
		}
210
211
		// Clean the component cache.
212
		$this->cleanCache('_system');
213
214
		// Trigger the onConfigurationAfterSave event.
215
		$dispatcher->trigger($this->event_after_save, array($this->option . '.' . $this->name, $table, $isNew));
216
217
		return true;
218
	}
219
220
	/**
221
	 * Method to get the data that should be injected in the form.
222
	 *
223
	 * @return  array  The default data is an empty array.
224
	 */
225
	protected function loadFormData()
226
	{
227
		// Check the session for previously entered form data.
228
		$data = JFactory::getApplication()->getUserState(
229
			$this->context . '.data',
230
			array()
231
		);
232
233
		if (empty($data))
234
		{
235
			$option = JFactory::getApplication()->input->getString('component');
236
237
			if ($option)
238
			{
239
				$table = JTable::getInstance('Extension');
240
241
				if ($table->load(array('element' => $option, 'type' => 'component')))
242
				{
243
					$data = $this->getItem($table->extension_id);
0 ignored issues
show
Bug introduced by
The property extension_id does not seem to exist on JTable.
Loading history...
244
245
					$data = $data->params;
246
				}
247
			}
248
		}
249
250
		return $data;
251
	}
252
253
	/**
254
	 * Gets Installed extensions
255
	 *
256
	 * @param   string   $extensionType      Extension type
257
	 * @param   array    $extensionElements  Extension element search type
258
	 * @param   array    $extensionFolder    Folder user when searching for plugin
259
	 * @param   boolean  $loadLanguage       Load language file for that extension
260
	 *
261
	 * @return  array  List of objects
262
	 */
263
	public function getInstalledExtensions(
264
		$extensionType = 'module',
265
		$extensionElements = array('%redcore%'),
266
		$extensionFolder = array('redcore'),
267
		$loadLanguage = true)
268
	{
269
		$db = $this->getDbo();
270
		$query = $db->getQuery(true)
271
			->select('e.name, e.type, e.element')
272
			->from('#__extensions AS e')
273
			->where('e.type = ' . $db->q($extensionType))
274
			->where('e.client_id = 0')
275
			->order('e.name');
276
277
		$folders = is_array($extensionFolder) ? $extensionFolder : array($extensionFolder);
0 ignored issues
show
introduced by
The condition is_array($extensionFolder) is always true.
Loading history...
278
		$isRedCore = false;
279
280
		foreach ($folders as $key => $folder)
281
		{
282
			$folders[$key] = $db->q($folder);
283
284
			if ($folder == 'redcore')
285
			{
286
				$isRedCore = true;
287
			}
288
		}
289
290
		if ($isRedCore)
291
		{
292
			$folders[] = $db->q('redpayment');
293
		}
294
295
		if ($extensionType == 'module')
296
		{
297
			$query->leftJoin('#__modules as m ON m.module = e.element')
298
				->select('m.published as enabled, m.module as moduleName')
299
				->group('e.element');
300
		}
301
		else
302
		{
303
			$query->select('e.enabled, e.folder');
304
		}
305
306
		$elements = array();
307
308
		foreach ($extensionElements as $group => $extensionElement)
309
		{
310
			if ($extensionType == 'plugin')
311
			{
312
				$elements[] = '(e.element LIKE ' . $db->q($extensionElement) . ' AND e.folder = ' . $db->quote($group) . ')';
0 ignored issues
show
Bug introduced by
Are you sure $db->quote($group) of type array|string can be used in concatenation? ( Ignorable by Annotation )

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

312
				$elements[] = '(e.element LIKE ' . $db->q($extensionElement) . ' AND e.folder = ' . /** @scrutinizer ignore-type */ $db->quote($group) . ')';
Loading history...
313
			}
314
			else
315
			{
316
				$elements[] = 'e.element LIKE ' . $db->q($extensionElement);
317
			}
318
		}
319
320
		$extensionsSearch = implode(' OR ', $elements);
321
322
		if (!empty($elements) && $extensionType == 'plugin')
323
		{
324
			$extensionsSearch .= ' OR ';
325
		}
326
327
		$query->where('(' . $extensionsSearch . ($extensionType == 'plugin' ? ' e.folder IN (' . implode(',', $folders) . ')' : '') . ')');
328
		$db->setQuery($query);
329
330
		$extensions = $db->loadObjectList();
331
332
		if ($loadLanguage && !empty($extensions))
333
		{
334
			// Load common and local language files.
335
			$lang = JFactory::getLanguage();
336
337
			foreach ($extensions as $extension)
338
			{
339
				if ($extensionType == 'plugin')
340
				{
341
					$extensionName = strtolower('plg_' . $extension->folder . '_' . $extension->element);
342
					$lang->load(strtolower($extensionName), JPATH_ADMINISTRATOR, null, false, true)
343
					|| $lang->load(strtolower($extensionName), JPATH_PLUGINS . '/' . $extension->folder . '/' . $extension->element, null, false, true);
344
				}
345
				else
346
				{
347
					$lang->load($extension->moduleName, JPATH_SITE, null, false, true) ||
348
					$lang->load($extension->moduleName,  JPATH_SITE . "/modules/" . $extension->moduleName, null, false, true);
349
				}
350
			}
351
		}
352
353
		return $extensions;
354
	}
355
}
356