Passed
Push — developer ( 8f2935...c734cf )
by Radosław
22:43 queued 04:55
created

getDataForSave()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
/**
4
 * Record Model.
5
 *
6
 * @package Settings
7
 *
8
 * @copyright YetiForce S.A.
9
 * @license   YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com)
10
 * @author    Radosław Skrzypczak <[email protected]>
11
 * @author    Mariusz Krzaczkowski <[email protected]>
12
 */
13
class Settings_WebserviceUsers_ManageConsents_Service extends Settings_WebserviceUsers_Record_Model
14
{
15
	/** {@inheritdoc} */
16
	public $baseTable = 'w_#__manage_consents_user';
17
18
	/** {@inheritdoc} */
19
	public $baseIndex = 'id';
20
21
	/** {@inheritdoc} */
22
	public $editFields = [
23
		'server_id' => 'FL_SERVER',
24
		'status' => 'FL_STATUS',
25
		'type' => 'FL_TYPE',
26
		'language' => 'FL_LANGUAGE',
27
		'user_id' => 'FL_USER'
28
	];
29
30
	/** {@inheritdoc} */
31
	public $listFields = [
32
		'server_id' => 'FL_SERVER',
33
		'status' => 'FL_STATUS',
34
		'user_id' => 'FL_USER',
35
		'type' => 'FL_TYPE',
36
		'login_time' => 'FL_LOGIN_TIME',
37
		'language' => 'FL_LANGUAGE'
38
	];
39
40
	/**
41
	 * Function determines fields available in edition view.
42
	 *
43
	 * @param mixed $name
44
	 *
45
	 * @return string[]
46
	 */
47
	public function getFieldInstanceByName($name)
48
	{
49
		$moduleName = $this->getModule()->getName(true);
50
		$fieldsLabel = $this->getEditFields();
51
		$params = ['uitype' => 1, 'column' => $name, 'name' => $name, 'label' => $fieldsLabel[$name], 'displaytype' => 1, 'typeofdata' => 'V~M', 'presence' => 0, 'isEditableReadOnly' => false];
52
		switch ($name) {
53
			case 'status':
54
				$params['uitype'] = 16;
55
				$params['picklistValues'] = [1 => \App\Language::translate('FL_ACTIVE'), 0 => \App\Language::translate('FL_INACTIVE')];
56
				break;
57
			case 'server_id':
58
				$servers = Settings_WebserviceApps_Module_Model::getActiveServers($this->getModule()->typeApi);
59
				$params['uitype'] = 16;
60
				$params['picklistValues'] = [];
61
				foreach ($servers as $key => $value) {
62
					$params['picklistValues'][$key] = $value['name'];
63
				}
64
				break;
65
			case 'type':
66
				$params['uitype'] = 16;
67
				$params['picklistValues'] = [];
68
				foreach ($this->getTypeValues() as $key => $value) {
69
					$params['picklistValues'][$key] = \App\Language::translate($value, $moduleName);
70
				}
71
				break;
72
			case 'language':
73
				$params['typeofdata'] = 'V~O';
74
				$params['uitype'] = 32;
75
				$params['picklistValues'] = \App\Language::getAll();
76
				break;
77
			case 'user_id':
78
				$params['uitype'] = 16;
79
				$params['picklistValues'] = \App\Fields\Owner::getInstance($moduleName)->getAccessibleUsers('', 'owner');
80
				break;
81
			case 'token':
82
				$params['typeofdata'] = 'P~M';
83
				break;
84
			default:
85
				break;
86
		}
87
		return Settings_Vtiger_Field_Model::init($moduleName, $params);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Settings_Vtiger_F...t($moduleName, $params) returns the type Vtiger_Field_Model which is incompatible with the documented return type string[].
Loading history...
88
	}
89
90
	/** {@inheritdoc} */
91
	public function save()
92
	{
93
		$db = App\Db::getInstance('webservice');
94
		$table = $this->baseTable;
95
		$index = $this->baseIndex;
96
		$data = $this->getDataForSave();
97
		$success = true;
98
		if (empty($this->getId())) {
99
			$success = $db->createCommand()->insert($table, $data)->execute();
100
			if ($success) {
101
				$this->set('id', $db->getLastInsertID("{$table}_{$index}_seq"));
102
			}
103
		} elseif ($data) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
104
			$success = $db->createCommand()->update($table, $data, [$index => $this->getId()])->execute();
105
		}
106
		return (bool) $success;
107
	}
108
109
	/**
110
	 * Sets data from request.
111
	 *
112
	 * @param App\Request $request
113
	 */
114
	public function setDataFromRequest(App\Request $request)
115
	{
116
		foreach (array_keys($this->getEditFields()) as $field) {
117
			if ($request->has($field)) {
118
				switch ($field) {
119
					case 'server_id':
120
					case 'status':
121
					case 'type':
122
					case 'user_id':
123
						$value = $request->getInteger($field);
124
					break;
125
					case 'language':
126
						$value = $request->getByType($field, 'Text');
127
						break;
128
					default:
129
						throw new \App\Exceptions\Security("ERR_ILLEGAL_FIELD_VALUE||{$field}", 406);
130
						break;
131
				}
132
				$this->set($field, $this->getValueToSave($field, $value));
133
			}
134
		}
135
	}
136
137
	/** {@inheritdoc} */
138
	public function getFieldsForSave()
139
	{
140
		$fields = $this->getEditFields();
141
		$fields['token'] = '';
142
		return $fields = array_intersect_key($fields, $this->changes);
0 ignored issues
show
Unused Code introduced by
The assignment to $fields is dead and can be removed.
Loading history...
143
	}
144
145
	/** {@inheritdoc} */
146
	public function getDataForSave()
147
	{
148
		if ($this->isNew()) {
149
			$this->set('token', \App\Fields\Token::generateToken());
150
		}
151
		return array_intersect_key($this->getData(), $this->getFieldsForSave());
152
	}
153
154
	/**
155
	 * Function formats data for saving.
156
	 *
157
	 * @param string $key
158
	 * @param mixed  $value
159
	 *
160
	 * @return int|string
161
	 */
162
	public function getValueToSave($key, $value)
163
	{
164
		switch ($key) {
165
			case 'server_id':
166
			case 'status':
167
			case 'type':
168
			case 'user_id':
169
				$value = (int) $value;
170
				break;
171
			default:
172
				break;
173
		}
174
		return $value;
175
	}
176
177
	/**
178
	 * Function to get the Display Value, for the current field type with given DB Insert Value.
179
	 *
180
	 * @param string $name
181
	 *
182
	 * @return string
183
	 */
184
	public function getDisplayValue($name)
185
	{
186
		switch ($name) {
187
			case 'server_id':
188
				$servers = Settings_WebserviceApps_Record_Model::getInstanceById($this->get($name));
189
				$value = $servers ? $servers->getName() : '<span class="redColor">ERROR</span>';
190
				break;
191
			case 'status':
192
				$value = \App\Language::translate((empty($this->get($name)) ? 'FL_INACTIVE' : 'FL_ACTIVE'));
193
				break;
194
			case 'user_id':
195
				$value = \App\Fields\Owner::getLabel($this->get($name));
196
				break;
197
			case 'language':
198
				$value = $this->get($name) ? \App\Language::getLanguageLabel($this->get($name)) : '';
0 ignored issues
show
Bug introduced by
It seems like $this->get($name) can also be of type null; however, parameter $prefix of App\Language::getLanguageLabel() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

198
				$value = $this->get($name) ? \App\Language::getLanguageLabel(/** @scrutinizer ignore-type */ $this->get($name)) : '';
Loading history...
199
				break;
200
			case 'type':
201
				$label = \App\Language::translate($this->getTypeValues($this->get($name)), $this->getModule()->getName(true));
0 ignored issues
show
Bug introduced by
It seems like $this->getTypeValues($this->get($name)) can also be of type array<integer,string>; however, parameter $key of App\Language::translate() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

201
				$label = \App\Language::translate(/** @scrutinizer ignore-type */ $this->getTypeValues($this->get($name)), $this->getModule()->getName(true));
Loading history...
202
				$value = \App\TextParser::textTruncate($label);
203
				break;
204
			default:
205
				$value = $this->get($name);
206
				break;
207
		}
208
		return $value;
209
	}
210
211
	/**
212
	 * Function to get the list view actions for the record.
213
	 *
214
	 * @return Vtiger_Link_Model[] - Associate array of Vtiger_Link_Model instances
215
	 */
216
	public function getRecordLinks()
217
	{
218
		$links = [];
219
		$recordLinks = [
220
			[
221
				'linktype' => 'LISTVIEWRECORD',
222
				'linklabel' => 'FL_TOKEN',
223
				'linkicon' => 'fas fa-copy',
224
				'linkclass' => 'btn btn-sm btn-primary clipboard',
225
				'linkdata' => ['copy-attribute' => 'clipboard-text', 'clipboard-text' => \App\Purifier::encodeHtml($this->get('token'))]
226
			],
227
			[
228
				'linktype' => 'LISTVIEWRECORD',
229
				'linklabel' => 'LBL_EDIT_RECORD',
230
				'linkurl' => $this->getModule()->getEditViewUrl() . '&record=' . $this->getId(),
231
				'linkicon' => 'yfi yfi-full-editing-view',
232
				'linkclass' => 'btn btn-sm btn-primary',
233
				'modalView' => true,
234
			],
235
			[
236
				'linktype' => 'LISTVIEWRECORD',
237
				'linklabel' => 'LBL_DELETE_RECORD',
238
				'linkurl' => 'javascript:Settings_WebserviceUsers_List_Js.deleteById(' . $this->getId() . ');',
239
				'linkicon' => 'fas fa-trash-alt',
240
				'linkclass' => 'btn btn-sm btn-danger',
241
			],
242
		];
243
		foreach ($recordLinks as $recordLink) {
244
			$links[] = Vtiger_Link_Model::getInstanceFromValues($recordLink);
245
		}
246
		return $links;
247
	}
248
249
	/**
250
	 * Type field values.
251
	 *
252
	 * @param type $value
253
	 *
254
	 * @return string|string[]
255
	 */
256
	public function getTypeValues($value = false)
257
	{
258
		$data = [
259
			\Api\WebservicePremium\Privilege::USER_PERMISSIONS => 'PLL_USER_PERMISSIONS',
260
		];
261
		if ($value) {
262
			return $data[$value] ?: '';
263
		}
264
		return $data;
265
	}
266
}
267