Users_EditRecordStructure_Model::getStructure()   F
last analyzed

Complexity

Conditions 35
Paths 1828

Size

Total Lines 73
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 1260

Importance

Changes 0
Metric Value
eloc 54
c 0
b 0
f 0
dl 0
loc 73
ccs 0
cts 48
cp 0
rs 0
cc 35
nc 1828
nop 0
crap 1260

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
 * The contents of this file are subject to the vtiger CRM Public License Version 1.1
4
 * ("License"); You may not use this file except in compliance with the License
5
 * The Original Code is:  vtiger CRM Open Source
6
 * The Initial Developer of the Original Code is vtiger.
7
 * Portions created by vtiger are Copyright (C) vtiger.
8
 * All Rights Reserved.
9
 * Contributor(s): YetiForce S.A.
10
 * ********************************************************************************** */
11
12
class Users_EditRecordStructure_Model extends Vtiger_EditRecordStructure_Model
13
{
14
	/** {@inheritdoc} */
15
	public function getStructure()
16
	{
17
		if (!empty($this->structuredValues)) {
18
			return $this->structuredValues;
19
		}
20
		$values = [];
21
		$currentUserModel = Users_Record_Model::getCurrentUserModel();
22
		$recordModel = $this->getRecord();
23
		$recordId = $recordModel->getId();
24
		$isOtherUser = $recordId !== App\User::getCurrentUserRealId();
25
		$fieldsDependency = \App\FieldsDependency::getByRecordModel($recordModel->isNew() ? 'Create' : 'Edit', $recordModel);
26
		$blockModelList = $this->getModule()->getBlocks();
27
		foreach ($blockModelList as $blockLabel => $blockModel) {
28
			$fieldModelList = $blockModel->getFields();
29
			if ($fieldModelList) {
30
				$values[$blockLabel] = [];
31
				foreach ($fieldModelList as $fieldName => $fieldModel) {
32
					$fieldModel->set('recordId', $recordId);
33
					if (empty($recordId) && (99 == $fieldModel->get('uitype') || 106 == $fieldModel->get('uitype'))) {
34
						$fieldModel->set('editable', true);
35
					}
36
					if (156 == $fieldModel->get('uitype') && true === $currentUserModel->isAdminUser() && $isOtherUser) {
37
						$fieldModel->set('editable', true);
38
						$fieldValue = false;
39
						if ('on' === $recordModel->get($fieldName)) {
40
							$fieldValue = true;
41
						}
42
						$recordModel->set($fieldName, $fieldValue);
43
					}
44
					if ('is_owner' === $fieldName) {
45
						$fieldModel->set('editable', false);
46
					} elseif ('reports_to_id' === $fieldName && !$currentUserModel->isAdminUser()) {
47
						continue;
48
					} elseif ('force_password_change' === $fieldName) {
49
						$fieldModel->set('editable', false);
50
					}
51
					if ($fieldModel->isEditable() && 'is_owner' !== $fieldName && (!$fieldsDependency['hide']['backend'] || !\in_array($fieldName, $fieldsDependency['hide']['backend']))) {
52
						if ('' !== $recordModel->get($fieldName)) {
53
							$fieldModel->set('fieldvalue', $recordModel->get($fieldName));
54
						} else {
55
							$defaultValue = $fieldModel->getDefaultFieldValue();
56
							if ('time_zone' === $fieldName && empty($defaultValue)) {
57
								$defaultValue = \App\Config::main('default_timezone');
58
							}
59
							if ('' !== $defaultValue && !$recordId) {
60
								$fieldModel->set('fieldvalue', $defaultValue);
61
							}
62
						}
63
						if ($fieldsDependency['hide']['frontend'] && \in_array($fieldName, $fieldsDependency['hide']['frontend'])) {
64
							$fieldModel->set('hideField', true);
65
						}
66
						if ($fieldsDependency['mandatory'] && \in_array($fieldName, $fieldsDependency['mandatory'])) {
67
							$fieldModel->set('isMandatory', true);
68
						}
69
						if (!$recordId && 99 == $fieldModel->get('uitype')) {
70
							$fieldModel->set('editable', true);
71
							$fieldModel->set('fieldvalue', '');
72
							$values[$blockLabel][$fieldName] = $fieldModel;
73
							if ($fieldModel->get('tabindex') > Vtiger_Field_Model::$tabIndexLastSeq) {
74
								Vtiger_Field_Model::$tabIndexLastSeq = $fieldModel->get('tabindex');
75
							}
76
						} elseif (99 != $fieldModel->get('uitype')) {
77
							$values[$blockLabel][$fieldName] = $fieldModel;
78
							if ($fieldModel->get('tabindex') > Vtiger_Field_Model::$tabIndexLastSeq) {
79
								Vtiger_Field_Model::$tabIndexLastSeq = $fieldModel->get('tabindex');
80
							}
81
						}
82
					}
83
				}
84
			}
85
		}
86
		++Vtiger_Field_Model::$tabIndexLastSeq;
87
		return $this->structuredValues = $values;
88
	}
89
}
90