Passed
Push — developer ( b6ebe7...0bf5e9 )
by Mariusz
47:54 queued 29:05
created

Item::saveToDb()   B

Complexity

Conditions 11
Paths 99

Size

Total Lines 44
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 44
rs 7.3166
c 0
b 0
f 0
cc 11
nc 99
nop 0

How to fix   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
 * Picklist value item.
4
 *
5
 * @package App
6
 *
7
 * @copyright YetiForce S.A.
8
 * @license   YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com)
9
 * @author    Radosław Skrzypczak <[email protected]>
10
 */
11
12
namespace App\Fields\Picklist;
13
14
/**
15
 * Picklist value item class.
16
 */
17
class Item extends \App\Base
18
{
19
	/** @var int Item ID */
20
	protected $id;
21
	/** @var string Item name */
22
	protected $name;
23
	/** @var int Permission level */
24
	protected $presence = 1;
25
	/** @var string Description */
26
	protected $description;
27
	/** @var string Prefix for numbering */
28
	protected $prefix;
29
	/** @var string Icon */
30
	protected $icon;
31
	/** @var string Color */
32
	protected $color;
33
	/** @var int Sort ID */
34
	protected $sortorderid;
35
	/** @var int Item ID for role */
36
	protected $valueid;
37
	/** @var int Record state */
38
	protected $record_state;
39
	/** @var int Time counting */
40
	protected $time_counting;
41
	/** @var int State for the record */
42
	protected $close_state;
43
	/** @var \Settings_Picklist_Field_Model */
44
	protected $fieldModel;
45
	/** @var array Changes */
46
	protected $changes = [];
47
	/** @var string[] Role IDs */
48
	protected $roles;
49
50
	/**
51
	 * Get instance.
52
	 *
53
	 * @param \Vtiger_Field_Model $fieldModel
54
	 * @param int|null            $id
55
	 *
56
	 * @return self
57
	 */
58
	public static function getInstance(\Vtiger_Field_Model $fieldModel, ?int $id): self
59
	{
60
		$instance = new self();
61
		$instance->fieldModel = $fieldModel;
0 ignored issues
show
Documentation Bug introduced by
$fieldModel is of type Vtiger_Field_Model, but the property $fieldModel was declared to be of type Settings_Picklist_Field_Model. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
62
		if ($id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $id of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
63
			$data = \App\Fields\Picklist::getValues($fieldModel->getName())[$id];
64
			$instance->id = $id;
65
			$instance->name = $data['picklistValue'];
66
			$instance->presence = (int) $data['presence'];
67
			$instance->description = $data['description'] ?? null;
68
			$instance->prefix = $data['prefix'] ?? null;
69
			$instance->icon = $data['icon'] ?? null;
70
			$instance->color = $data['color'] ?? null;
71
			$instance->sortorderid = (int) $data['sortorderid'];
72
			$instance->valueid = isset($data['picklist_valueid']) ? (int) $data['picklist_valueid'] : null;
73
			$instance->record_state = isset($data['record_state']) ? (int) $data['record_state'] : null;
74
			$instance->time_counting = isset($data['time_counting']) ? (int) $data['time_counting'] : null;
75
			$instance->close_state = (int) (new \App\Db\Query())->from('u_#__picklist_close_state')->where(['valueid' => $instance->valueid, 'fieldid' => $fieldModel->getId()])->exists();
76
		}
77
78
		return $instance;
79
	}
80
81
	/**
82
	 * Function to get the Id.
83
	 *
84
	 * @return int
85
	 */
86
	public function getId(): int
87
	{
88
		return (int) $this->id;
89
	}
90
91
	/** {@inheritdoc} */
92
	public function set($key, $value)
93
	{
94
		$propertyExists = \property_exists($this, $key);
95
		if ($this->getId() && !\in_array($key, ['id']) && ($propertyExists && $this->{$key} !== $value && (null !== $this->{$key} || '' !== $value)) && !\array_key_exists($key, $this->changes)) {
96
			$this->changes[$key] = $this->get($key);
97
		}
98
		return $propertyExists ? $this->{$key} = $value : parent::set($key, $value);
99
	}
100
101
	/** {@inheritdoc} */
102
	public function get($key)
103
	{
104
		return \property_exists($this, $key) ? $this->{$key} : parent::get($key);
105
	}
106
107
	/**
108
	 * Save.
109
	 *
110
	 * @return bool
111
	 */
112
	public function save(): bool
113
	{
114
		try {
115
			$this->validate();
116
			$result = $this->saveToDb();
117
			if ($this->getPreviousValue('name')) {
118
				$this->rename();
119
			}
120
		} catch (\Throwable $e) {
121
			\App\Log::error($e->__toString());
122
			throw $e;
123
		}
124
		\App\Fields\Picklist::clearCache($this->fieldModel->getName(), $this->fieldModel->getModuleName());
125
		return $result;
126
	}
127
128
	/**
129
	 * Save data to database.
130
	 *
131
	 * @return bool
132
	 */
133
	public function saveToDb(): bool
134
	{
135
		$db = \App\Db::getInstance();
136
		$result = false;
137
		$fieldName = $this->fieldModel->getName();
138
		$primaryKey = \App\Fields\Picklist::getPickListId($fieldName);
139
		$baseTable = $this->getTableName();
140
141
		$dataForSave = $this->getValuesToSave();
142
		foreach (array_keys(\App\Fields\Picklist::COLUMN_DB_TYPES) as $column) {
143
			if (isset($dataForSave[$baseTable][$column])) {
144
				\App\Fields\Picklist::addColumn($column, $baseTable);
145
			}
146
		}
147
148
		$transaction = $db->beginTransaction();
149
		try {
150
			foreach ($dataForSave as $tableName => $tableData) {
151
				if (!$this->getId() && $baseTable === $tableName) {
152
					if ($this->fieldModel->isRoleBased()) {
153
						$vId = $db->getUniqueID('vtiger_picklistvalues');
154
						$tableData['picklist_valueid'] = $vId;
155
						$this->set('valueid', $vId);
156
					}
157
					$result = $db->createCommand()->insert($tableName, $tableData)->execute();
158
					$this->id = $db->getLastInsertID($tableName . '_' . $primaryKey . '_seq');
0 ignored issues
show
Documentation Bug introduced by
The property $id was declared of type integer, but $db->getLastInsertID($ta.... $primaryKey . '_seq') is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
159
				} elseif ($baseTable === $tableName) {
160
					$db->createCommand()->update($tableName, $tableData, [$primaryKey => $this->getId()])->execute();
161
				} elseif ('u_#__picklist_close_state' === $tableName) {
162
					$db->createCommand()->delete($tableName, ['fieldid' => $this->fieldModel->getId(), 'valueid' => $this->valueid])->execute();
163
					if ($this->close_state) {
164
						$db->createCommand()->insert($tableName, ['fieldid' => $this->fieldModel->getId(), 'valueid' => $this->valueid, 'value' => $this->name])->execute();
165
					}
166
				}
167
			}
168
			$this->updateRolePermissions();
169
170
			$transaction->commit();
171
		} catch (\Throwable $ex) {
172
			$transaction->rollBack();
173
			\App\Log::error($ex->__toString());
174
			throw $ex;
175
		}
176
		return (bool) $result;
177
	}
178
179
	/**
180
	 * Update role permissions.
181
	 *
182
	 * @return void
183
	 */
184
	public function updateRolePermissions()
185
	{
186
		if ($this->valueid && null !== $this->roles) {
187
			$dbCommand = \App\Db::getInstance()->createCommand();
188
			$dbCommand->delete('vtiger_role2picklist', ['picklistvalueid' => $this->valueid])->execute();
189
			if ($this->roles) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->roles of type string[] 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...
190
				$picklistId = \App\Fields\Picklist::getPicklistIdNr($this->fieldModel->getName());
191
				if ($insertValueList = array_map(fn ($roleid) => [$roleid, $this->valueid, $picklistId], $this->roles)) {
192
					$dbCommand->batchInsert('vtiger_role2picklist', ['roleid', 'picklistvalueid', 'picklistid'], $insertValueList)->execute();
193
				}
194
			}
195
		}
196
	}
197
198
	/**
199
	 * Get table name.
200
	 *
201
	 * @return string
202
	 */
203
	public function getTableName(): string
204
	{
205
		return \App\Fields\Picklist::getPickListTableName($this->fieldModel->getName());
206
	}
207
208
	/**
209
	 * Get next sequence number.
210
	 *
211
	 * @return int
212
	 */
213
	public function getNextSeq(): int
214
	{
215
		return (int) (new \App\Db\Query())->from($this->getTableName())->max('sortorderid') + 1;
216
	}
217
218
	/**
219
	 * Get writable fields.
220
	 *
221
	 * @return array
222
	 */
223
	public function getWritableFields(): array
224
	{
225
		return ['name', 'presence', 'sortorderid', 'presence', 'description', 'prefix', 'icon', 'record_state', 'time_counting', 'close_state'];
226
	}
227
228
	/**
229
	 * Get field model.
230
	 *
231
	 * @return \Settings_Picklist_Field_Model
232
	 */
233
	public function getFieldModel(): \Settings_Picklist_Field_Model
234
	{
235
		return $this->fieldModel;
236
	}
237
238
	/**
239
	 * Check if item is deletable.
240
	 *
241
	 * @return bool
242
	 */
243
	public function isDeletable(): bool
244
	{
245
		return 1 === $this->presence && $this->fieldModel->isEditable();
246
	}
247
248
	/**
249
	 * Rename item value in other data.
250
	 *
251
	 * @return void
252
	 */
253
	public function rename()
254
	{
255
		$newValue = $this->name;
256
		$previousValue = $this->getPreviousValue('name');
257
		$fieldName = $this->fieldModel->getName();
258
259
		$dbCommand = \App\Db::getInstance()->createCommand();
260
		$dataReader = (new \App\Db\Query())->select(['tablename', 'columnname', 'fieldid', 'tabid'])
261
			->from('vtiger_field')
262
			->where(['fieldname' => $fieldName, 'uitype' => [15, 16, 33]])
263
			->createCommand()->query();
264
		while ($row = $dataReader->read()) {
265
			$tableName = $row['tablename'];
266
			$columnName = $row['columnname'];
267
			$dbCommand->update($tableName, [$columnName => $newValue], [$columnName => $previousValue])
268
				->execute();
269
			$dbCommand->update('vtiger_field', ['defaultvalue' => $newValue], ['defaultvalue' => $previousValue, 'fieldid' => $row['fieldid']])
270
				->execute();
271
			$moduleName = \App\Module::getModuleName($row['tabid']);
272
273
			\App\Fields\Picklist::clearCache($fieldName, $moduleName);
274
			$eventHandler = new \App\EventHandler();
275
			$eventHandler->setParams([
276
				'fieldname' => $fieldName,
277
				'oldvalue' => $previousValue,
278
				'newvalue' => $newValue,
279
				'module' => $moduleName,
280
				'id' => $this->getId(),
281
			]);
282
			$eventHandler->trigger('PicklistAfterRename');
283
		}
284
		\App\Fields\Picklist::clearCache($fieldName, $this->fieldModel->getModuleName());
285
	}
286
287
	/**
288
	 * Delete item.
289
	 *
290
	 * @param int $replaceId Item ID
291
	 *
292
	 * @return void
293
	 */
294
	public function delete(int $replaceId)
295
	{
296
		$db = \App\Db::getInstance();
297
		$fieldName = $this->fieldModel->getName();
298
		$transaction = $db->beginTransaction();
299
		try {
300
			$dbCommand = $db->createCommand();
301
302
			$primaryKey = \App\Fields\Picklist::getPickListId($this->fieldModel->getName());
303
			$replaceValue = \App\Purifier::decodeHtml((new \App\Db\Query())->select([$this->fieldModel->getName()])
304
				->from($this->getTableName())
305
				->where([$primaryKey => $replaceId])
306
				->scalar());
307
308
			if ($this->fieldModel->isRoleBased()) {
309
				$dbCommand->delete('vtiger_role2picklist', ['picklistvalueid' => $this->valueid])->execute();
310
				$dbCommand->delete('u_#__picklist_close_state', ['valueid' => $this->valueid])->execute();
311
			}
312
			$dbCommand->delete($this->getTableName(), [$primaryKey => $this->getId()])->execute();
313
			$dependencyId = (new \App\Db\Query())->select(['s_#__picklist_dependency.id'])->from('s_#__picklist_dependency')->innerJoin('s_#__picklist_dependency_data', 's_#__picklist_dependency_data.id = s_#__picklist_dependency.id')
314
				->where(['source_field' => $this->fieldModel->getId(), 'source_id' => $this->getId()])->column();
315
			if ($dependencyId) {
316
				$dbCommand->delete('s_#__picklist_dependency_data', ['id' => $dependencyId, 'source_id' => $this->getId()])->execute();
317
			}
318
319
			$dataReader = (new \App\Db\Query())->select(['tablename', 'columnname', 'fieldid', 'tabid'])
320
				->from('vtiger_field')
321
				->where(['fieldname' => $fieldName, 'uitype' => [15, 16, 33]])
322
				->createCommand()->query();
323
			while ($row = $dataReader->read()) {
324
				$moduleName = \App\Module::getModuleName($row['tabid']);
325
				$tableName = $row['tablename'];
326
				$columnName = $row['columnname'];
327
				$dbCommand->update($tableName, [$columnName => $replaceValue], [$columnName => $this->name])
328
					->execute();
329
				$dbCommand->update('vtiger_field', ['defaultvalue' => $replaceValue], ['defaultvalue' => $this->name, 'fieldid' => $row['fieldid']])
330
					->execute();
331
332
				\App\Fields\Picklist::clearCache($fieldName, $moduleName);
333
				$eventHandler = new \App\EventHandler();
334
				$eventHandler->setParams([
335
					'fieldname' => $fieldName,
336
					'valuetodelete' => [$this->name],
337
					'replacevalue' => $replaceValue,
338
					'module' => $moduleName,
339
				]);
340
				$eventHandler->trigger('PicklistAfterDelete');
341
			}
342
			$dataReader->close();
343
344
			$transaction->commit();
345
		} catch (\Throwable $ex) {
346
			$transaction->rollBack();
347
			\App\Log::error($ex->__toString());
348
			throw $ex;
349
		}
350
351
		\App\Fields\Picklist::clearCache($fieldName, $this->fieldModel->getModuleName());
352
	}
353
354
	/**
355
	 * Function formats data for saving.
356
	 *
357
	 * @return array
358
	 */
359
	private function getValuesToSave(): array
360
	{
361
		$forSave = [];
362
		$tableName = $this->getTableName();
363
		if (!$this->getId()) {
364
			$forSave[$this->getTableName()] = [
365
				'sortorderid' => $this->getNextSeq(),
366
				'presence' => $this->presence,
367
			];
368
		}
369
		$fields = $this->getId() ? array_keys($this->changes) : $this->getWritableFields();
370
		foreach ($fields as $name) {
371
			$itemPropertyModel = $this->getFieldInstanceByName($name);
372
			if ($itemPropertyModel && isset($this->{$name}) && ($this->getId() || (!$this->getId() && '' !== $this->{$name}))) {
373
				$this->validateValue($name, $this->{$name});
374
				$forSave[$itemPropertyModel->getTableName()][$itemPropertyModel->getColumnName()] = $this->{$name};
375
			} elseif (isset($this->{$name}) && ($this->getId() || (!$this->getId() && '' !== $this->{$name}))) {
376
				$this->validateValue($name, $this->{$name});
377
				$forSave[$tableName][$name] = $this->{$name};
378
			}
379
		}
380
381
		return $forSave;
382
	}
383
384
	/**
385
	 * Get pervious value by field.
386
	 *
387
	 * @param string $fieldName
388
	 *
389
	 * @return mixed
390
	 */
391
	public function getPreviousValue(?string $fieldName = '')
392
	{
393
		return $fieldName ? ($this->changes[$fieldName] ?? null) : $this->changes;
394
	}
395
396
	/** {@inheritdoc} */
397
	public function getData()
398
	{
399
		$data = [];
400
		foreach (get_object_vars($this) as $name => $value) {
401
			if (\is_object($value) || 'value' === $name || 'changes' === $name || null === $value) {
402
				continue;
403
			}
404
			if (\is_array($value)) {
405
				$value = implode(',', $value);
406
			}
407
			$data[$name] = $value;
408
		}
409
410
		return $data;
411
	}
412
413
	/**
414
	 * Get fields for edit.
415
	 *
416
	 * @return array
417
	 */
418
	public function getEditFields(): array
419
	{
420
		$fields = [];
421
		$editFields = ['name'];
422
		$editFields[] = 'icon';
423
		if ($this->fieldModel->getModule()->isEntityModule()) {
424
			if (!$this->getId()) {
425
				$editFields[] = 'roles';
426
			}
427
			$editFields[] = 'description';
428
			$editFields[] = 'prefix';
429
			if ($this->fieldModel->getFieldParams()['isProcessStatusField'] ?? false) {
430
				if (\App\Db::getInstance()->getTableSchema($this->getTableName())->getColumn('time_counting')) {
431
					$editFields[] = 'time_counting';
432
				}
433
				$editFields[] = 'record_state';
434
			}
435
			if (15 === $this->fieldModel->getUIType()) {
436
				$editFields[] = 'close_state';
437
			}
438
		}
439
440
		foreach ($editFields as $fieldName) {
441
			$propertyModel = $this->getFieldInstanceByName($fieldName);
442
			if (null !== $this->get($fieldName)) {
443
				$propertyModel->set('fieldvalue', $this->get($fieldName));
444
			} elseif (($defaultValue = $propertyModel->get('defaultvalue')) !== null) {
445
				$propertyModel->set('fieldvalue', $defaultValue);
446
			}
447
			$fields[$fieldName] = $propertyModel;
448
		}
449
450
		return $fields;
451
	}
452
453
	/**
454
	 * Basic validation.
455
	 *
456
	 * @return array
457
	 */
458
	public function validate(): array
459
	{
460
		$response = [];
461
		if ($this->isDuplicateValue()) {
462
			$response[] = [
463
				'result' => false,
464
				'message' => \App\Language::translate('LBL_DUPLICATE', 'Settings:Picklist')
465
			];
466
		}
467
		return $response;
468
	}
469
470
	/**
471
	 * Check if picklist value exists.
472
	 *
473
	 * @return bool
474
	 */
475
	public function isDuplicateValue(): bool
476
	{
477
		$picklistValues = \App\Fields\Picklist::getValuesName($this->fieldModel->getName());
478
		if ($this->id) {
479
			unset($picklistValues[$this->id]);
480
		}
481
482
		return \in_array(strtolower($this->name), array_map('strtolower', $picklistValues));
483
	}
484
485
	/**
486
	 * Validate item data.
487
	 *
488
	 * @param string $fieldName
489
	 * @param mixed  $value
490
	 *
491
	 * @return void
492
	 */
493
	public function validateValue(string $fieldName, $value)
494
	{
495
		switch ($fieldName) {
496
			case 'name':
497
				$itemPropertyModel = $this->getFieldInstanceByName($fieldName);
498
				$itemPropertyModel->getUITypeModel()->validate($value, false);
499
				if (empty($value)) {
500
					throw new \App\Exceptions\IllegalValue("LBL_NOT_FILLED_MANDATORY_FIELDS||{$fieldName}", 512);
501
				}
502
				if (preg_match('/[\<\>\"\#]/', $value)) {
503
					throw new \App\Exceptions\IllegalValue("ERR_SPECIAL_CHARACTERS_NOT_ALLOWED||{$fieldName}||{$value}", 512);
504
				}
505
				if ($itemPropertyModel->getMaxValue() && \strlen($value) > $itemPropertyModel->getMaxValue()) {
506
					throw new \App\Exceptions\IllegalValue("ERR_EXCEEDED_NUMBER_CHARACTERS||{$fieldName}||{$value}", 406);
507
				}
508
				if ($this->isDuplicateValue($value, $this->getId())) {
0 ignored issues
show
Unused Code introduced by
The call to App\Fields\Picklist\Item::isDuplicateValue() has too many arguments starting with $value. ( Ignorable by Annotation )

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

508
				if ($this->/** @scrutinizer ignore-call */ isDuplicateValue($value, $this->getId())) {

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
509
					throw new \App\Exceptions\IllegalValue("ERR_DUPLICATES_VALUES_FOUND||{$fieldName}||{$value}", 513);
510
				}
511
				break;
512
			case 'color':
513
			case 'description':
514
			case 'prefix':
515
			case 'close_state':
516
			case 'icon':
517
				$itemPropertyModel = $this->getFieldInstanceByName($fieldName);
518
				$itemPropertyModel->getUITypeModel()->validate($value, false);
519
				break;
520
			case 'time_counting':
521
			case 'record_state':
522
			case 'sortorderid':
523
				if (!\App\Validator::integer($value)) {
524
					throw new \App\Exceptions\IllegalValue("ERR_NOT_ALLOWED_VALUE||{$fieldName}||{$value}", 406);
525
				}
526
				break;
527
			case 'presence':
528
				if (1 !== $value && 0 !== $value) {
529
					throw new \App\Exceptions\IllegalValue("ERR_NOT_ALLOWED_VALUE||{$fieldName}||{$value}", 406);
530
				}
531
				break;
532
			default:
533
				throw new \App\Exceptions\IllegalValue("ERR_NOT_ALLOWED_VALUE||{$fieldName}||{$value}", 406);
534
		}
535
	}
536
537
	/**
538
	 * Get fields instance by name.
539
	 *
540
	 * @param string $name
541
	 *
542
	 * @return Vtiger_Field_Model
0 ignored issues
show
Bug introduced by
The type App\Fields\Picklist\Vtiger_Field_Model was not found. Did you mean Vtiger_Field_Model? If so, make sure to prefix the type with \.
Loading history...
543
	 */
544
	public function getFieldInstanceByName($name)
545
	{
546
		$params = [];
547
		$qualifiedModuleName = 'Settings:Picklist';
548
		$tableName = $this->getTableName();
549
		switch ($name) {
550
			case 'name':
551
				$params = [
552
					'name' => $name,
553
					'column' => $this->fieldModel->getName(),
554
					'label' => 'LBL_ITEM_VALUE',
555
					'uitype' => 1,
556
					'typeofdata' => 'V~M',
557
					'maximumlength' => $this->fieldModel->getMaxValue(),
558
					'purifyType' => \App\Purifier::TEXT,
559
					'table' => $tableName,
560
					'validator' => [['name' => 'FieldLabel']]
561
				];
562
				if (1 !== $this->presence || !$this->fieldModel->isEditable()) {
563
					$params['isEditableReadOnly'] = true;
564
				}
565
				break;
566
			case 'description':
567
				$params = [
568
					'name' => $name,
569
					'column' => $name,
570
					'label' => 'LBL_DESCRIPTION',
571
					'uitype' => 300,
572
					'typeofdata' => 'V~O',
573
					'maximumlength' => '65535',
574
					'purifyType' => \App\Purifier::HTML,
575
					'tooltip' => 'LBL_DESCRIPTION_VALUE_LIST',
576
					'table' => $tableName
577
				];
578
				break;
579
			case 'prefix':
580
				$params = [
581
					'name' => $name,
582
					'column' => $name,
583
					'label' => 'LBL_PREFIX',
584
					'uitype' => 1,
585
					'typeofdata' => 'V~O',
586
					'maximumlength' => '25',
587
					'purifyType' => \App\Purifier::TEXT,
588
					'tooltip' => 'LBL_DESCRIPTION_PREFIXES',
589
					'table' => $tableName
590
				];
591
				break;
592
			case 'close_state':
593
				$params = [
594
					'name' => $name,
595
					'column' => $name,
596
					'label' => 'LBL_CLOSES_RECORD',
597
					'uitype' => 56,
598
					'typeofdata' => 'C~O',
599
					'maximumlength' => '5',
600
					'purifyType' => \App\Purifier::BOOL,
601
					'tooltip' => 'LBL_BLOCKED_RECORD_INFO',
602
					'table' => 'u_#__picklist_close_state'
603
				];
604
				break;
605
			case 'icon':
606
				$params = [
607
					'name' => $name,
608
					'column' => $name,
609
					'label' => 'LBL_ICON',
610
					'uitype' => 62,
611
					'typeofdata' => 'V~O',
612
					'maximumlength' => '255',
613
					'purifyType' => \App\Purifier::TEXT,
614
					'table' => $tableName
615
				];
616
				break;
617
			case 'time_counting':
618
				$params = [
619
					'name' => $name,
620
					'column' => $name,
621
					'label' => 'LBL_TIME_COUNTING',
622
					'uitype' => 16,
623
					'typeofdata' => 'V~M',
624
					'maximumlength' => '250',
625
					'purifyType' => \App\Purifier::INTEGER,
626
					'tooltip' => 'LBL_TIME_COUNTING_INFO',
627
					'defaultvalue' => 0,
628
					'picklistValues' => [
629
						0 => \App\Language::translate('LBL_NONE', '_Base'),
630
						\App\RecordStatus::TIME_COUNTING_REACTION => \App\Language::translate('LBL_TIME_COUNTING_REACTION', $qualifiedModuleName),
631
						\App\RecordStatus::TIME_COUNTING_RESOLVE => \App\Language::translate('LBL_TIME_COUNTING_RESOLVE', $qualifiedModuleName),
632
						\App\RecordStatus::TIME_COUNTING_IDLE => \App\Language::translate('LBL_TIME_COUNTING_IDLE', $qualifiedModuleName)
633
					],
634
					'table' => $tableName
635
				];
636
				break;
637
			case 'record_state':
638
				$params = [
639
					'name' => $name,
640
					'column' => $name,
641
					'label' => 'LBL_RECORD_STATE',
642
					'uitype' => 16,
643
					'typeofdata' => 'V~M',
644
					'maximumlength' => '250',
645
					'purifyType' => \App\Purifier::INTEGER,
646
					'tooltip' => 'LBL_RECORD_STATE_INFO',
647
					'defaultvalue' => \App\RecordStatus::RECORD_STATE_NO_CONCERN,
648
					'picklistValues' => [],
649
					'table' => $tableName
650
				];
651
				foreach (\App\RecordStatus::getLabels() as $key => $value) {
652
					$params['picklistValues'][$key] = \App\Language::translate($value, $qualifiedModuleName);
653
				}
654
				break;
655
			case 'roles':
656
				$params = [
657
					'name' => $name,
658
					'column' => $name,
659
					'label' => 'LBL_ASSIGN_TO_ROLE',
660
					'uitype' => 33,
661
					'typeofdata' => 'V~O',
662
					'maximumlength' => '500',
663
					'purifyType' => \App\Purifier::TEXT,
664
					'defaultvalue' => 'all',
665
					'picklistValues' => [
666
						'all' => \App\Language::translate('LBL_ALL_ROLES', $qualifiedModuleName)
667
					],
668
					'table' => $tableName
669
				];
670
				foreach (\Settings_Roles_Record_Model::getAll() as $key => $roleModel) {
671
					$params['picklistValues'][$key] = \App\Language::translate($roleModel->get('rolename'), 'Settings:Roles');
672
				}
673
				break;
674
			default:
675
				break;
676
		}
677
678
		return $params ? \Vtiger_Field_Model::init($qualifiedModuleName, $params, $name)->set('sourceFieldModel', $this->fieldModel) : null;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $params ? Vtiger_...his->fieldModel) : null also could return the type Vtiger_Field_Model which is incompatible with the documented return type App\Fields\Picklist\Vtiger_Field_Model.
Loading history...
679
	}
680
}
681