Passed
Push — developer ( 4e3135...f5c82a )
by Radosław
30:25 queued 12:59
created

Settings_MailServers_Module_Model::getDefaultUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * Mail serwers module model class.
5
 *
6
 * @copyright YetiForce S.A.
7
 * @license   YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com)
8
 * @author    Radosław Skrzypczak <[email protected]>
9
 */
10
class Settings_MailServers_Module_Model extends Settings_Vtiger_Module_Model
11
{
12
	/** {@inheritdoc} */
13
	public $name = 'MailServers';
14
	/** {@inheritdoc} */
15
	public $baseTable = 's_#__mail_servers';
16
	/** {@inheritdoc} */
17
	public $baseIndex = 'id';
18
19
	/** {@inheritdoc} */
20
	public $listFields = ['name' => 'FL_SUBJECT', 'status' => 'FL_ACTIVE',  'imap_host' => 'FL_IMAP_HOST'];
21
22
	/**
23
	 * Function to get the url for Create view of the module.
24
	 *
25
	 * @return string - url
26
	 */
27
	public function getCreateRecordUrl()
28
	{
29
		return 'index.php?module=' . $this->getName() . '&parent=Settings&view=Edit';
30
	}
31
32
	/**
33
	 * Function to get the url for edit view of the module.
34
	 *
35
	 * @return string - url
36
	 */
37
	public function getEditViewUrl()
38
	{
39
		return 'index.php?module=' . $this->getName() . '&parent=Settings&view=Edit';
40
	}
41
42
	/**
43
	 * Function to get the url for default view of the module.
44
	 *
45
	 * @return string URL
46
	 */
47
	public function getDefaultUrl()
48
	{
49
		return 'index.php?module=' . $this->getName() . '&parent=Settings&view=List';
50
	}
51
52
	/**
53
	 * Function verifies if it is possible to sort by given field in list view.
54
	 *
55
	 * @param string $fieldName
56
	 *
57
	 * @return bool
58
	 */
59
	public function isSortByName($fieldName)
60
	{
61
		return \in_array($fieldName, ['name', 'status', 'imap_host']);
62
	}
63
64
	/** {@inheritdoc} */
65
	public function getListFields(): array
66
	{
67
		if (!isset($this->listFieldModels)) {
68
			$fields = $this->listFields;
69
			$fieldObjects = [];
70
			foreach ($fields as $fieldName => $fieldLabel) {
71
				$fieldObject = new \App\Base(['name' => $fieldName, 'label' => $fieldLabel]);
72
				if (!$this->isSortByName($fieldName)) {
73
					$fieldObject->set('sort', true);
74
				}
75
				$fieldObjects[$fieldName] = $fieldObject;
76
			}
77
			$this->listFieldModels = $fieldObjects;
0 ignored issues
show
Bug Best Practice introduced by
The property listFieldModels does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
78
		}
79
		return $this->listFieldModels;
80
	}
81
82
	/** @var string[] Fields name for edit view */
83
	public $editFields = [
84
		'name', 'auth_method', 'oauth_provider', 'client_id', 'client_secret', 'redirect_uri_id', 'status', 'visible', 'validate_cert', 'imap_encrypt', 'imap_host', 'imap_port', 'smtp_encrypt', 'smtp_host', 'smtp_port', 'spellcheck', 'ip_check', 'identities_level'
85
	];
86
87
	/**
88
	 * Editable fields.
89
	 *
90
	 * @return array
91
	 */
92
	public function getEditableFields(): array
93
	{
94
		return $this->editFields;
95
	}
96
97
	/**
98
	 * Get structure fields.
99
	 *
100
	 * @param Settings_AutomaticAssignment_Record_Model|null $recordModel
101
	 *
102
	 * @return array
103
	 */
104
	public function getEditViewStructure($recordModel = null): array
105
	{
106
		$structure = [];
107
		foreach ($this->editFields as $fieldName) {
108
			$fieldModel = $this->getFieldInstanceByName($fieldName);
109
			if ($recordModel && $recordModel->has($fieldName)) {
110
				$fieldModel->set('fieldvalue', $recordModel->get($fieldName));
111
			} else {
112
				$defaultValue = $fieldModel->get('defaultvalue');
113
				$fieldModel->set('fieldvalue', $defaultValue ?? '');
114
			}
115
			$block = $fieldModel->get('blockLabel') ?: '';
116
			$structure[$block][$fieldName] = $fieldModel;
117
		}
118
119
		return $structure;
120
	}
121
122
	/**
123
	 * Get block icon.
124
	 *
125
	 * @param string $name
126
	 *
127
	 * @return string
128
	 */
129
	public function getBlockIcon($name): string
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed. ( Ignorable by Annotation )

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

129
	public function getBlockIcon(/** @scrutinizer ignore-unused */ $name): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
130
	{
131
		return '';
132
	}
133
134
	/**
135
	 * Get fields instance by name.
136
	 *
137
	 * @param string $name
138
	 *
139
	 * @return Vtiger_Field_Model
140
	 */
141
	public function getFieldInstanceByName($name)
142
	{
143
		$params = [];
144
		switch ($name) {
145
			case 'name':
146
				$params = [
147
					'name' => $name,
148
					'label' => 'FL_SUBJECT',
149
					'uitype' => 1,
150
					'typeofdata' => 'V~M',
151
					'maximumlength' => '50',
152
					'purifyType' => \App\Purifier::TEXT,
153
					'blockLabel' => 'BL_BASE',
154
					'table' => $this->getBaseTable()
155
				];
156
				break;
157
			case 'auth_method':
158
				$params = [
159
					'name' => $name,
160
					'label' => 'FL_AUTH_METHOD',
161
					'uitype' => 16,
162
					'typeofdata' => 'V~M',
163
					'maximumlength' => '50',
164
					'purifyType' => \App\Purifier::ALNUM,
165
					'blockLabel' => 'BL_BASE',
166
					'table' => $this->getBaseTable(),
167
					'defaultvalue' => 'basic',
168
					'picklistValues' => [
169
						'basic' => \App\Language::translate('LBL_BASIC_AUTH', $this->getName(true)),
170
						'oauth2' => \App\Language::translate('LBL_OAUTH2', $this->getName(true))
171
					]
172
				];
173
				break;
174
			case 'status':
175
				$params = [
176
					'name' => $name,
177
					'label' => 'FL_ACTIVE',
178
					'uitype' => 56,
179
					'typeofdata' => 'C~O',
180
					'maximumlength' => '1',
181
					'purifyType' => \App\Purifier::BOOL,
182
					'blockLabel' => 'BL_BASE',
183
					'table' => $this->getBaseTable()
184
				];
185
				break;
186
			case 'imap_host':
187
				$params = [
188
					'name' => $name,
189
					'label' => 'FL_IMAP_HOST',
190
					'uitype' => 17,
191
					'typeofdata' => 'C~O',
192
					'maximumlength' => '128',
193
					'tooltip' => 'LBL_IMAP_HOST_DESC',
194
					'purifyType' => \App\Purifier::URL,
195
					'blockLabel' => 'BL_IMAP',
196
					'table' => $this->getBaseTable()
197
				];
198
				break;
199
			case 'smtp_host':
200
				$params = [
201
					'name' => $name,
202
					'label' => 'FL_SMTP_HOST',
203
					'uitype' => 17,
204
					'typeofdata' => 'V~O',
205
					'maximumlength' => '128',
206
					'tooltip' => 'LBL_SMTP_HOST_DESC',
207
					'purifyType' => \App\Purifier::URL,
208
					'blockLabel' => 'BL_SMTP',
209
					'table' => $this->getBaseTable()
210
				];
211
				break;
212
			case 'imap_port':
213
				$params = [
214
					'name' => $name,
215
					'label' => 'FL_IMAP_PORT',
216
					'uitype' => 7,
217
					'typeofdata' => 'I~O',
218
					'maximumlength' => '0,65535',
219
					'purifyType' => \App\Purifier::INTEGER,
220
					'blockLabel' => 'BL_IMAP',
221
					'table' => $this->getBaseTable()
222
				];
223
				break;
224
			case 'smtp_port':
225
				$params = [
226
					'name' => $name,
227
					'label' => 'FL_SMTP_PORT',
228
					'uitype' => 7,
229
					'typeofdata' => 'I~O',
230
					'maximumlength' => '0,65535',
231
					'purifyType' => \App\Purifier::INTEGER,
232
					'blockLabel' => 'BL_SMTP',
233
					'table' => $this->getBaseTable()
234
				];
235
				break;
236
			case 'imap_encrypt':
237
				$params = [
238
					'name' => $name,
239
					'label' => 'FL_IMAP_ENCRYPT',
240
					'uitype' => 16,
241
					'typeofdata' => 'V~O',
242
					'maximumlength' => '5',
243
					'purifyType' => \App\Purifier::STANDARD,
244
					'blockLabel' => 'BL_IMAP',
245
					'defaultvalue' => '',
246
					'table' => $this->getBaseTable()
247
				];
248
				$params['picklistValues'] = [
249
					'ssl' => \App\Language::translate('ssl', $this->getName(true)),
250
					'tls' => \App\Language::translate('tls', $this->getName(true))
251
				];
252
				break;
253
			case 'smtp_encrypt':
254
				$params = [
255
					'name' => $name,
256
					'label' => 'FL_SMTP_ENCRYPT',
257
					'uitype' => 16,
258
					'typeofdata' => 'V~O',
259
					'maximumlength' => '5',
260
					'purifyType' => \App\Purifier::STANDARD,
261
					'blockLabel' => 'BL_SMTP',
262
					'defaultvalue' => '',
263
					'table' => $this->getBaseTable()
264
				];
265
				$params['picklistValues'] = [
266
					'ssl' => \App\Language::translate('ssl', $this->getName(true)),
267
					'tls' => \App\Language::translate('tls', $this->getName(true))
268
				];
269
				break;
270
			case 'session_lifetime':
271
				$params = [
272
					'name' => $name,
273
					'label' => 'FL_SESSION_LIFETIME',
274
					'uitype' => 7,
275
					'typeofdata' => 'I~M',
276
					'maximumlength' => '0,65535',
277
					'purifyType' => \App\Purifier::BOOL,
278
					'blockLabel' => 'BL_OTHER',
279
					'table' => $this->getBaseTable()
280
				];
281
				break;
282
			case 'validate_cert':
283
				$params = [
284
					'name' => $name,
285
					'label' => 'FL_VALIDATE_CERT',
286
					'uitype' => 56,
287
					'typeofdata' => 'C~O',
288
					'maximumlength' => '1',
289
					'purifyType' => \App\Purifier::BOOL,
290
					'blockLabel' => 'BL_BASE',
291
					'table' => $this->getBaseTable()
292
				];
293
				break;
294
			case 'spellcheck':
295
				$params = [
296
					'name' => $name,
297
					'label' => 'FL_SPELL_CHECK',
298
					'uitype' => 56,
299
					'typeofdata' => 'C~O',
300
					'maximumlength' => '1',
301
					'purifyType' => \App\Purifier::BOOL,
302
					'blockLabel' => 'BL_OTHER',
303
					'table' => $this->getBaseTable()
304
				];
305
				break;
306
			case 'ip_check':
307
				$params = [
308
					'name' => $name,
309
					'label' => 'FL_IP_CHECK',
310
					'uitype' => 56,
311
					'typeofdata' => 'C~O',
312
					'maximumlength' => '1',
313
					'purifyType' => \App\Purifier::BOOL,
314
					'blockLabel' => 'BL_OTHER',
315
					'table' => $this->getBaseTable()
316
				];
317
				break;
318
			case 'identities_level':
319
				$params = [
320
					'name' => $name,
321
					'label' => 'FL_IDENTITIES_LEVEL',
322
					'uitype' => 16,
323
					'typeofdata' => 'V~M',
324
					'maximumlength' => '1',
325
					'purifyType' => \App\Purifier::INTEGER,
326
					'blockLabel' => 'BL_OTHER',
327
					'defaultvalue' => 0,
328
					'table' => $this->getBaseTable()
329
				];
330
				$params['picklistValues'] = [
331
					0 => \App\Language::translate('identities_level_0', $this->getName(true)),
332
					1 => \App\Language::translate('identities_level_1', $this->getName(true)),
333
					2 => \App\Language::translate('identities_level_2', $this->getName(true)),
334
					3 => \App\Language::translate('identities_level_3', $this->getName(true)),
335
					4 => \App\Language::translate('identities_level_4', $this->getName(true))
336
				];
337
				break;
338
			case 'visible':
339
				$params = [
340
					'name' => $name,
341
					'label' => 'FL_VISIBLE',
342
					'uitype' => 56,
343
					'typeofdata' => 'C~O',
344
					'maximumlength' => '1',
345
					'purifyType' => \App\Purifier::BOOL,
346
					'blockLabel' => 'BL_BASE',
347
					'table' => $this->getBaseTable()
348
				];
349
				break;
350
			case 'oauth_provider':
351
				$params = [
352
					'name' => $name,
353
					'label' => 'FL_OAUTH_PROVIDER',
354
					'uitype' => 16,
355
					'typeofdata' => 'V~M',
356
					'maximumlength' => '50',
357
					'purifyType' => \App\Purifier::INTEGER,
358
					'blockLabel' => 'BL_BASE',
359
					'defaultvalue' => '',
360
					'table' => $this->getBaseTable(),
361
					'picklistValues' => array_map(fn ($provider) => \App\Language::translate($provider->getLabel(), $this->getName(true)), \App\Integrations\OAuth::getProviders())
362
				];
363
				break;
364
			case 'redirect_uri_id':
365
				$params = [
366
					'name' => $name,
367
					'label' => 'FL_REDIRECT_URI_ID',
368
					'uitype' => 16,
369
					'typeofdata' => 'I~M',
370
					'maximumlength' => '2147483647',
371
					'purifyType' => \App\Purifier::INTEGER,
372
					'blockLabel' => 'BL_BASE',
373
					'tooltip' => 'LBL_REDIRECT_URI_ID_DESC',
374
					'defaultvalue' => '',
375
					'table' => $this->getBaseTable(),
376
					'picklistValues' => array_map(fn ($service) => $service['name'], \App\Integrations\Services::getByType(\App\Integrations\Services::OAUTH))
377
				];
378
				break;
379
			case 'client_id':
380
				$params = [
381
					'name' => $name,
382
					'label' => 'FL_CLIENT_ID',
383
					'uitype' => 1,
384
					'typeofdata' => 'V~M',
385
					'maximumlength' => '255',
386
					'purifyType' => \App\Purifier::TEXT,
387
					'blockLabel' => 'BL_BASE',
388
					'table' => $this->getBaseTable()
389
				];
390
				break;
391
			case 'client_secret':
392
				$params = [
393
					'name' => $name,
394
					'label' => 'FL_CLIENT_SECRET',
395
					'uitype' => 99,
396
					'typeofdata' => 'V~M',
397
					'maximumlength' => '255',
398
					'purifyType' => 'raw',
399
					'blockLabel' => 'BL_BASE',
400
					'fromOutsideList' => true,
401
					'table' => $this->getBaseTable()
402
				];
403
				break;
404
			default:
405
				break;
406
		}
407
408
		return $params ? \Vtiger_Field_Model::init($this->getName(true), $params, $name) : null;
409
	}
410
}
411