Users_EditRecordStructure_Model   A
last analyzed

Complexity

Total Complexity 35

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 35
eloc 55
c 0
b 0
f 0
dl 0
loc 76
ccs 0
cts 48
cp 0
rs 9.6

1 Method

Rating   Name   Duplication   Size   Complexity  
F getStructure() 0 73 35
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