Passed
Pull Request — developer (#16883)
by
unknown
14:53
created

getFieldInstanceByName()   C

Complexity

Conditions 10
Paths 12

Size

Total Lines 75
Code Lines 63

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
eloc 63
dl 0
loc 75
ccs 0
cts 0
cp 0
rs 6.9406
c 0
b 0
f 0
cc 10
nc 12
nop 1
crap 110

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.0
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
/**
13
 * Settings module model class for groups.
14
 */
15
class Settings_Groups_Module_Model extends Settings_Vtiger_Module_Model
16
{
17
	/** {@inheritdoc} */
18
	public $baseTable = 'vtiger_groups';
19
20
	/** {@inheritdoc} */
21
	public $baseIndex = 'groupid';
22
23
	/** {@inheritdoc} */
24
	public $listFields = ['groupname' => 'Name', 'description' => 'Description', 'parentid' => 'FL_PARENT', 'modules' => 'LBL_MODULES', 'members' => 'LBL_GROUP_MEMBERS'];
25
26
	/** {@inheritdoc} */
27
	public $name = 'Groups';
28
29
	/** {@inheritdoc} */
30
	public function getListFields(): array
31
	{
32
		$fields = [];
33
		foreach (array_keys($this->listFields) as $fieldName) {
34
			$fields[$fieldName] = $this->getFieldInstanceByName($fieldName);
35
		}
36
		return $fields;
37
	}
38
39
	/**
40
	 * Function to get the url for default view of the module.
41
	 *
42
	 * @return string - url
43
	 */
44
	public function getDefaultUrl()
45
	{
46
		return 'index.php?module=Groups&parent=Settings&view=List';
47
	}
48
49
	/**
50
	 * Function to get the url for create view of the module.
51
	 *
52
	 * @return string - url
53
	 */
54
	public function getCreateRecordUrl()
55
	{
56
		return 'index.php?module=Groups&parent=Settings&view=Edit';
57
	}
58
59
	/** @var string[] Fields name for edit view */
60
	public $editFields = [
61
		'groupname',
62
		'description',
63
		'parentid',
64
		'modules',
65
		'members'
66
	];
67
68
	/**
69
	 * Editable fields.
70
	 *
71
	 * @return array
72
	 */
73
	public function getEditableFields(): array
74
	{
75
		return $this->editFields;
76
	}
77
78
	/**
79
	 * Get structure fields.
80
	 *
81
	 * @param Settings_Groups_Record_Model|null $recordModel
82
	 *
83
	 * @return array
84
	 */
85
	public function getEditViewStructure($recordModel = null): array
86
	{
87
		$structure = [];
88
		foreach ($this->editFields as $fieldName) {
89
			$fieldModel = $this->getFieldInstanceByName($fieldName);
90
			if ($recordModel && $recordModel->has($fieldName)) {
91
				$fieldModel->set('fieldvalue', $recordModel->get($fieldName));
92
			} else {
93
				$defaultValue = $fieldModel->get('defaultvalue');
94
				$fieldModel->set('fieldvalue', $defaultValue ?? '');
95
			}
96
			$structure[$fieldName] = $fieldModel;
97
		}
98
99
		return $structure;
100
	}
101
102
	/**
103
	 * Get fields instance by name.
104
	 *
105
	 * @param string $name
106
	 *
107
	 * @return Vtiger_Field_Model
108
	 */
109
	public function getFieldInstanceByName($name)
110
	{
111
		$params = [];
112
		switch ($name) {
113
			case 'groupname':
114
				$params = [
115
					'name' => $name,
116
					'label' => 'Name',
117
					'uitype' => 1,
118
					'typeofdata' => 'V~M',
119
					'maximumlength' => '100',
120
					'purifyType' => \App\Purifier::TEXT,
121
					'table' => $this->getBaseTable()
122
				];
123
				break;
124
			case 'description':
125
				$params = [
126
					'name' => $name,
127
					'label' => 'Description',
128
					'uitype' => 1,
129
					'typeofdata' => 'V~O',
130
					'maximumlength' => '65535',
131
					'purifyType' => \App\Purifier::TEXT,
132
					'table' => $this->getBaseTable()
133
				];
134
				break;
135
			case 'parentid':
136
				$params = [
137
					'name' => $name,
138
					'label' => 'FL_PARENT',
139
					'uitype' => 53,
140
					'typeofdata' => 'I~O',
141
					'maximumlength' => '4294967295',
142
					'purifyType' => \App\Purifier::INTEGER,
143
					'table' => $this->getBaseTable(),
144
					'picklistValues' => []
145
				];
146
				break;
147
			case 'modules':
148
				$params = [
149
					'name' => $name,
150
					'label' => 'LBL_MODULES',
151
					'uitype' => 33,
152
					'typeofdata' => 'V~M',
153
					'maximumlength' => '65535',
154
					'purifyType' => \App\Purifier::TEXT,
155
					'table' => '',
156
					'picklistValues' => []
157
				];
158
				foreach (\vtlib\Functions::getAllModules(true, true, 0) as $module) {
159
					$params['picklistValues'][$module['tabid']] = \App\Language::translate($module['name'], $module['name']);
160
				}
161
				break;
162
			case 'members':
163
				$params = [
164
					'name' => $name,
165
					'label' => 'LBL_GROUP_MEMBERS',
166
					'uitype' => 33,
167
					'typeofdata' => 'V~M',
168
					'maximumlength' => '65535',
169
					'purifyType' => \App\Purifier::TEXT,
170
					'picklistValues' => []
171
				];
172
				foreach (\App\PrivilegeUtil::getMembers() as $memberType) {
173
					foreach ($memberType as $memberId => $memberValues) {
174
						$params['picklistValues'][$memberId] = \App\Language::translate($memberValues['type'], $this->getName()) . ': ' .
175
						\App\Language::translate($memberValues['name'], $this->getName());
176
					}
177
				}
178
				break;
179
			default:
180
				break;
181
		}
182
183
		return $params ? \Vtiger_Field_Model::init($this->getName(true), $params, $name) : null;
184
	}
185
186
	/**
187
	 * Function to get the links.
188
	 *
189
	 * @return Vtiger_Link_Model[]
190
	 */
191
	public function getLinks(): array
192
	{
193
		return [Vtiger_Link_Model::getInstanceFromValues([
194
			'linktype' => 'LISTVIEWBASIC',
195
			'linklabel' => App\Language::translate('LBL_ADD_RECORD', $this->getName()),
196
			'linkurl' => $this->getCreateRecordUrl(),
197
			'linkicon' => 'yfi yfi-full-editing-view',
198
			'linkclass' => 'btn-primary',
199
			'showLabel' => 1
200
		])];
201
	}
202
203
	/**
204
	 * Function to get Alphabet Search Field.
205
	 */
206
	public function getAlphabetSearchField()
207
	{
208
		return '';
209
	}
210
211
	/**
212
	 * Gets value from request.
213
	 *
214
	 * @param string      $fieldName
215
	 * @param App\Request $request
216
	 *
217
	 * @return mixed
218
	 */
219
	public function getValueFromRequest(string $fieldName, App\Request $request)
220
	{
221
		switch ($fieldName) {
222
			case 'groupname':
223
				case 'description':
224
				$value = $request->getByType($fieldName, \App\Purifier::ALNUM);
225
				break;
226
			case 'parentid':
227
				$value = $request->getArray($fieldName, \App\Purifier::INTEGER);
228
				break;
229
			case 'members':
230
				$value = $request->getArray($fieldName, \App\Purifier::TEXT);
231
				break;
232
			case 'modules':
233
				$value = $request->getArray($fieldName, \App\Purifier::INTEGER);
234
				break;
235
			default: break;
236
		}
237
		return $value;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $value does not seem to be defined for all execution paths leading up to this point.
Loading history...
238
	}
239
}
240