Passed
Push — developer ( 1116b3...4a3269 )
by Radosław
34:20 queued 19:24
created

Settings_Groups_Module_Model   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 216
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 25
eloc 110
c 0
b 0
f 0
dl 0
loc 216
rs 10
ccs 0
cts 4
cp 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getListFields() 0 7 2
A getDefaultUrl() 0 3 1
A getEditableFields() 0 3 1
A getEditViewStructure() 0 15 4
A getCreateRecordUrl() 0 3 1
A getValueFromRequest() 0 18 6
A getAlphabetSearchField() 0 3 1
A getLinks() 0 9 1
B getFieldInstanceByName() 0 69 8
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=Index';
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
				break;
173
			default:
174
				break;
175
		}
176
177
		return $params ? \Vtiger_Field_Model::init($this->getName(true), $params, $name) : null;
178
	}
179
180
	/**
181
	 * Function to get the links.
182
	 *
183
	 * @return Vtiger_Link_Model[]
184
	 */
185
	public function getLinks(): array
186
	{
187
		return [Vtiger_Link_Model::getInstanceFromValues([
188
			'linktype' => 'LISTVIEWBASIC',
189
			'linklabel' => App\Language::translate('LBL_ADD_RECORD', $this->getName()),
190
			'linkurl' => $this->getCreateRecordUrl(),
191
			'linkicon' => 'yfi yfi-full-editing-view',
192
			'linkclass' => 'btn-primary',
193
			'showLabel' => 1
194
		])];
195
	}
196
197
	/**
198
	 * Function to get Alphabet Search Field.
199
	 */
200
	public function getAlphabetSearchField()
201
	{
202
		return '';
203
	}
204
205
	/**
206
	 * Gets value from request.
207
	 *
208
	 * @param string      $fieldName
209
	 * @param App\Request $request
210
	 *
211
	 * @return mixed
212
	 */
213
	public function getValueFromRequest(string $fieldName, App\Request $request)
214
	{
215
		switch ($fieldName) {
216
			case 'groupname':
217
			case 'description':
218
				$value = $request->getByType($fieldName, \App\Purifier::TEXT);
219
				break;
220
			case 'parentid':
221
				$value = $request->getArray($fieldName, \App\Purifier::INTEGER);
222
				break;
223
			case 'members':
224
				$value = $request->getArray($fieldName, \App\Purifier::TEXT);
225
				break;
226
			case 'modules':
227
				$value = $request->getArray($fieldName, \App\Purifier::INTEGER);
228
				break;
229
		}
230
		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...
231
	}
232
}
233