RedcoreModelTranslation::getItem()   C
last analyzed

Complexity

Conditions 16
Paths 144

Size

Total Lines 99
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 55
c 0
b 0
f 0
dl 0
loc 99
rs 5.1999
cc 16
nc 144
nop 1

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
 * Translation Model
14
 *
15
 * @package     Redcore.Frontend
16
 * @subpackage  Models
17
 * @since       1.0
18
 */
19
class RedcoreModelTranslation extends RModelAdmin
20
{
21
	/**
22
	 * Method to get a single record.
23
	 *
24
	 * @param   integer  $pk  The id of the primary key.
25
	 *
26
	 * @return  mixed  Object on success, false on failure.
27
	 */
28
	public function getItem($pk = null)
29
	{
30
		$app = JFactory::getApplication();
31
		$ids = $app->input->getString('id', '');
32
		$id = $app->input->getString('rctranslations_id', '');
33
		$table = RTranslationTable::setTranslationTableWithColumn($app->input->get('translationTableName', ''));
34
35
		if (empty($table))
36
		{
37
			// Translation table does not exist we are redirecting to manager
38
			$app->redirect('index.php?option=com_redcore&view=translations');
39
		}
40
41
		$db	= $this->getDbo();
42
		$query = $db->getQuery(true);
43
		$item = new stdClass;
44
45
		$ids = explode('###', $ids);
46
47
		$query->select('*')
48
			->from($db->qn($table->table));
49
50
		foreach ($table->primaryKeys as $key => $primaryKey)
51
		{
52
			$query->where($db->qn($primaryKey) . ' = ' . $db->q($ids[$key]));
53
		}
54
55
		$db->setQuery($query);
56
		$item->original = $db->loadObject();
57
58
		$query = $db->getQuery(true)
59
			->select('*')
60
			->from($db->qn(RTranslationTable::getTranslationsTableName($table->table, '')))
61
			->where('rctranslations_id = ' . $db->q($id));
62
63
		$db->setQuery($query);
64
		$item->translation = $db->loadObject();
65
66
		if (empty($item->translation))
67
		{
68
			$item->translation = new stdClass;
69
70
			foreach ($table->columns as $column)
71
			{
72
				$item->translation->{$column} = null;
73
			}
74
75
			foreach ($table->primaryKeys as $primaryKey)
76
			{
77
				if (!empty($item->original->{$primaryKey}))
78
				{
79
					$item->translation->{$primaryKey} = $item->original->{$primaryKey};
80
				}
81
			}
82
83
			$item->rctranslations_state = 1;
84
			$item->rctranslations_modified = '';
85
			$item->rctranslations_modified_by = '';
86
			$item->rctranslations_language = JFactory::getApplication()->input->getString('language', '');
87
			$item->id = 0;
88
		}
89
		else
90
		{
91
			if (!empty($item->translation->rctranslations_originals))
92
			{
93
				$registry = new JRegistry;
0 ignored issues
show
Bug introduced by
The type JRegistry was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
94
				$registry->loadString($item->translation->rctranslations_originals);
95
				$item->translation->rctranslations_originals = $registry->toArray();
96
			}
97
98
			$item->original->rctranslations_state = $item->rctranslations_state = $item->translation->rctranslations_state;
99
			$item->original->rctranslations_modified = $item->rctranslations_modified = $item->translation->rctranslations_modified;
100
			$item->original->rctranslations_modified_by = $item->rctranslations_modified_by = $item->translation->rctranslations_modified_by;
101
			$item->original->rctranslations_language = $item->rctranslations_language = $item->translation->rctranslations_language;
102
			$item->original->rctranslations_originals = $item->rctranslations_originals = $item->translation->rctranslations_originals;
103
			$item->id = $item->translation->rctranslations_id;
104
		}
105
106
		foreach ($table->allColumns as $column)
107
		{
108
			if ($column['column_type'] != RTranslationTable::COLUMN_TRANSLATE)
109
			{
110
				$item->translation->{$column['name']} = $item->original->{$column['name']};
111
			}
112
113
			if ($column['value_type'] == 'params'
114
				&& (empty($item->translation->{$column['name']}) || $item->translation->{$column['name']} == '{}'))
115
			{
116
				$item->translation->{$column['name']} = $item->original->{$column['name']};
117
			}
118
119
			if ($column['value_type'] == 'readonlytext'
120
				&& (empty($item->translation->{$column['name']}) || $item->translation->{$column['name']} == '{}'))
121
			{
122
				$item->translation->{$column['name']} = $item->original->{$column['name']};
123
			}
124
		}
125
126
		return $item;
127
	}
128
129
	/**
130
	 * Method to save the form data.
131
	 *
132
	 * @param   array  $data  The form data.
133
	 *
134
	 * @return  boolean  True on success.
135
	 */
136
	public function save($data)
137
	{
138
		$app = JFactory::getApplication();
139
		$dataArr = $app->input->get('jform', array(), 'array');
140
		$translationTable = RTranslationTable::setTranslationTableWithColumn($app->input->get('translationTableName', ''));
141
		$translation = $app->input->get('translation', array(), 'array');
142
143
		if ($original = $this->getItem())
144
		{
145
			$original = (array) $original->original;
146
		}
147
		else
148
		{
149
			$original = $app->input->get('original', array(), 'array');
150
		}
151
152
		foreach ($translation as $translationKey => $translationData)
153
		{
154
			$allLanguages = RTranslationHelper::getAllContentLanguageCodes();
155
156
			if (!in_array($translationKey, $allLanguages) && $translationKey != 'no-language')
157
			{
158
				continue;
159
			}
160
161
			$data = array_merge($dataArr[$translationKey], $translationData);
162
163
			$id = !empty($data['rctranslations_id']) ? (int) $data['rctranslations_id'] : 0;
164
165
			if ($translationKey == 'no-language')
166
			{
167
				$data['rctranslations_language'] = $dataArr[$translationKey]['rctranslations_language'];
168
			}
169
			else
170
			{
171
				$data['rctranslations_language'] = $translationKey;
172
			}
173
174
			/** @var RedcoreTableTranslation $table */
175
			$table = $this->getTable();
176
177
			if (empty($id))
178
			{
179
				$db	= $this->getDbo();
180
				$query = $db->getQuery(true)
181
					->select('rctranslations_id')
182
					->from($db->qn(RTranslationTable::getTranslationsTableName($translationTable->table, '')))
183
					->where('rctranslations_language = ' . $db->q($data['rctranslations_language']));
184
185
				foreach ($translationTable->primaryKeys as $primaryKey)
186
				{
187
					if (!empty($data[$primaryKey]))
188
					{
189
						$query->where($db->qn($primaryKey) . ' = ' . $db->q($data[$primaryKey]));
190
					}
191
				}
192
193
				$db->setQuery($query);
194
				$id = $db->loadResult();
195
			}
196
197
			// Check if the form is completely empty, and return an error if it is.
198
			$dataFilled = RTranslationHelper::validateEmptyTranslationData($translationData, $translationTable->primaryKeys);
199
200
			if (!$dataFilled)
201
			{
202
				$this->setError(JText::_('COM_REDCORE_TRANSLATIONS_SAVE_ERROR_EMPTY'));
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

202
				/** @scrutinizer ignore-deprecated */ $this->setError(JText::_('COM_REDCORE_TRANSLATIONS_SAVE_ERROR_EMPTY'));

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...
203
204
				if (!empty($id))
205
				{
206
					$table->delete($id);
207
				}
208
209
				continue;
210
			}
211
212
			foreach ($translationTable->allColumns as $field)
213
			{
214
				if ($field['value_type'] == 'params' && $field['column_type'] == RTranslationTable::COLUMN_TRANSLATE)
215
				{
216
					$fieldName = $field['name'];
217
					$paramsChanged = false;
218
219
					if (!empty($data[$fieldName]))
220
					{
221
						$registry = new JRegistry;
222
						$registry->loadString($original[$fieldName]);
223
						$originalParams = $registry->toArray();
224
225
						foreach ($data[$fieldName] as $paramKey => $paramValue)
226
						{
227
							if ((!isset($originalParams[$paramKey]) && $paramValue != '') || $originalParams[$paramKey] != $paramValue)
228
							{
229
								$paramsChanged = true;
230
231
								break;
232
							}
233
						}
234
235
						if ($paramsChanged)
236
						{
237
							$data[$fieldName] = json_encode($data[$fieldName]);
238
						}
239
						else
240
						{
241
							$data[$fieldName] = '';
242
						}
243
					}
244
				}
245
			}
246
247
			$dispatcher = RFactory::getDispatcher();
248
249
			foreach ($translationTable->primaryKeys as $primaryKey)
250
			{
251
				$original[$primaryKey] = $data[$primaryKey];
252
			}
253
254
			$isNew = true;
255
256
			// Load the row if saving an existing item.
257
			$table->load((int) $id);
258
259
			if ($table->rctranslations_modified)
260
			{
261
				$isNew = false;
262
			}
263
264
			$data['rctranslations_originals'] = RTranslationTable::createOriginalValueFromColumns($original, $translationTable->columns);
265
266
			// We run posthandler methods
267
			foreach ($translationTable->allColumns as $field)
268
			{
269
				$postHandler = $field['posthandler'];
270
				$fieldName = $field['name'];
271
272
				if (!empty($postHandler) && $field['translate'] == '1')
273
				{
274
					$postHandlerFunctions = explode(',', $postHandler);
275
276
					foreach ($postHandlerFunctions as $postHandlerFunction)
277
					{
278
						$postHandlerFunctionArray = explode('::', $postHandlerFunction);
279
280
						if (empty($postHandlerFunctionArray[1]))
281
						{
282
							$postHandlerFunctionArray[1] = $postHandlerFunctionArray[0];
283
							$postHandlerFunctionArray[0] = 'RTranslationContentHelper';
284
							$postHandlerFunction = 'RTranslationContentHelper::' . $postHandlerFunction;
0 ignored issues
show
Unused Code introduced by
The assignment to $postHandlerFunction is dead and can be removed.
Loading history...
285
						}
286
287
						if (method_exists($postHandlerFunctionArray[0], $postHandlerFunctionArray[1]))
288
						{
289
							call_user_func_array(
290
								array(
291
									$postHandlerFunctionArray[0],
292
									$postHandlerFunctionArray[1]),
293
									array($field, &$data[$fieldName], &$data, $translationTable)
294
							);
295
						}
296
					}
297
				}
298
			}
299
300
			// Bind the data.
301
			if (!$table->bind($data))
302
			{
303
				$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

303
				$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

303
				/** @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...
304
305
				return false;
306
			}
307
308
			// Prepare the row for saving
309
			$this->prepareTable($table);
310
311
			// Check the data.
312
			if (!$table->check())
313
			{
314
				$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

314
				$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

314
				/** @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...
315
316
				return false;
317
			}
318
319
			// Trigger the onContentBeforeSave event.
320
			$result = $dispatcher->trigger($this->event_before_save, array($this->option . '.' . $this->name, &$table, $isNew));
321
322
			if (in_array(false, $result, true))
323
			{
324
				$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

324
				/** @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

324
				$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...
325
326
				return false;
327
			}
328
329
			// Store the data.
330
			if (!$table->store())
331
			{
332
				$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

332
				$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

332
				/** @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...
333
334
				return false;
335
			}
336
337
			// Trigger the onContentAfterSave event.
338
			$dispatcher->trigger($this->event_after_save, array($this->option . '.' . $this->name, &$table, $isNew));
339
340
			$this->setState($this->getName() . '.id', $table->rctranslations_id);
0 ignored issues
show
Bug introduced by
The property rctranslations_id does not exist on RedcoreTableTranslation. Did you mean rctranslations_modified?
Loading history...
341
342
			// Clear the cache
343
			$this->cleanCache();
344
		}
345
346
		return true;
347
	}
348
349
	/**
350
	 * Method to get a form object.
351
	 *
352
	 * @param   array    $data      Data for the form.
353
	 * @param   boolean  $loadData  True if the form is to load its own data (default case), false if not.
354
	 *
355
	 * @return  mixed  A JForm object on success, false on failure
356
	 */
357
	public function getForm($data = array(), $loadData = true)
358
	{
359
		$option = JFactory::getApplication()->input->getString('component');
360
361
		JForm::addFormPath(JPATH_ADMINISTRATOR . '/components/' . $option);
362
363
		// Get the form.
364
		$form = $this->loadForm(
365
			'com_redcore.edit.translation.translation',
366
			'translation',
367
			array('control' => 'jform', 'load_data' => $loadData),
368
			true
369
		);
370
371
		if (empty($form))
372
		{
373
			return false;
374
		}
375
376
		return $form;
377
	}
378
}
379