Passed
Pull Request — developer (#16962)
by Arkadiusz
16:59
created

isSummaryFieldOptionDisabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 0
cts 3
cp 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/* +**********************************************************************************
3
 * The contents of this file are subject to the vtiger CRM Public License Version 1.1
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
class Settings_LayoutEditor_Field_Model extends Vtiger_Field_Model
13
{
14
	const WEBSERVICE_APPS_VISIBILITY = [
15
		0 => 'LBL_WSA_VISIBILITY_DEFAULT',
16
		1 => 'LBL_DISPLAY_TYPE_1',
17 21
		2 => 'LBL_DISPLAY_TYPE_2',
18
		3 => 'LBL_DISPLAY_TYPE_3',
19 21
		4 => 'LBL_DISPLAY_TYPE_4',
20 21
		9 => 'LBL_DISPLAY_TYPE_9',
21
		10 => 'LBL_DISPLAY_TYPE_10',
22 21
		6 => 'LBL_DISPLAY_TYPE_6',
23 21
	];
24 21
25 21
	/** @var array Translations of field types */
26 21
	public $fieldTypeLabel = [
27 21
		'string' => 'Text',
28 21
		'date' => 'Date',
29 21
		'integer' => 'Integer',
30
		'double' => 'Decimal',
31 21
		'percentage' => 'Percent',
32
		'phone' => 'Phone',
33 21
		'email' => 'Email',
34 21
		'time' => 'Time',
35
		'picklist' => 'Picklist',
36 21
		'url' => 'URL',
37
		'multipicklistTags' => 'MultipicklistTags',
38 21
		'text' => 'TextArea',
39
		'languages' => 'LBL_LANGUAGE',
40
		'multipicklist' => 'MultiSelectCombo',
41
		'country' => 'Country',
42
		'reference' => 'Related1M',
43
		'userCreator' => 'LBL_USER',
44 21
		'boolean' => 'Checkbox',
45 3
		'image' => 'Image',
46 3
		'datetime' => 'DateTime',
47 3
		'currency' => 'Currency',
48 3
		'skype' => 'Skype',
49 3
		'tree' => 'Tree',
50 3
		'multiReferenceValue' => 'MultiReferenceValue',
51
		'multiReference' => 'MultiReference',
52 3
		'rangeTime' => 'RangeTime',
53
		'categoryMultipicklist' => 'CategoryMultipicklist',
54
		'multiImage' => 'MultiImage',
55 3
		'twitter' => 'Twitter',
56
		'multiEmail' => 'MultiEmail',
57 3
		'smtp' => 'Smtp',
58
		'serverAccess' => 'ServerAccess',
59
		'multiDomain' => 'MultiDomain',
60
		'token' => 'Token',
61 21
		'multiAttachment' => 'MultiAttachment',
62 1
		'mapCoordinates' => 'MapCoordinates',
63 1
		'advPercentage' => 'AdvPercentage',
64 1
		'group' => 'Group'
65 1
	];
66 1
67
	/** @var array Webservice field data */
68 21
	protected $webserviceData;
69 21
70 21
	/**
71 21
	 * Get field label by data type.
72 21
	 */
73 21
	public function getFieldLabelByDataType()
74
	{
75
		if (isset($this->fieldTypeLabel[$this->getFieldDataType()])) {
76
			$label = $this->fieldTypeLabel[$this->getFieldDataType()];
77
			if (300 === $this->getUIType()) {
78
				$label = 'Editor';
79 21
			}
80
			return $label;
81
		}
82
	}
83
84
	/**
85
	 * Function to remove field.
86
	 */
87
	public function delete()
88
	{
89
		$db = \App\Db::getInstance();
90
		try {
91
			parent::delete();
92
93
			$fldModule = $this->getModuleName();
94
			$id = $this->getId();
95
			$fieldname = $this->getName();
96
			$tablename = $this->get('table');
97
			$columnName = $this->get('column');
98
			$tabId = $this->getModuleId();
99
			if ('vtiger_crmentity' !== $tablename) {
100
				$db->createCommand()->dropColumn($tablename, $columnName)->execute();
101
			}
102
			App\Db::getInstance('admin')->createCommand()->delete('a_#__mapped_fields', ['or', ['source' => (string) $id], ['target' => (string) $id]])->execute();
103
			//we have to remove the entries in customview and report related tables which have this field ($colName)
104
			$db->createCommand()->delete('vtiger_cvcolumnlist', ['field_name' => $fieldname, 'module_name' => $fldModule])->execute();
105
			$db->createCommand()->delete('vtiger_cvcolumnlist', [
106
				'source_field_name' => $fieldname,
107
				'cvid' => (new \App\Db\Query())->select(['cvid'])->from('vtiger_customview')->where(['entitytype' => $fldModule]),
108
			])->execute();
109
			$db->createCommand()->delete('u_#__cv_condition', ['field_name' => $fieldname, 'module_name' => $fldModule])->execute();
110
			//Deleting from convert lead mapping vtiger_table- Jaguar
111
			if ('Leads' === $fldModule) {
112
				$db->createCommand()->delete('vtiger_convertleadmapping', ['leadfid' => $id])->execute();
113
			} elseif ('Accounts' === $fldModule) {
114
				$mapDelId = ['Accounts' => 'accountfid'];
115
				$db->createCommand()->update('vtiger_convertleadmapping', [$mapDelId[$fldModule] => 0], [$mapDelId[$fldModule] => $id])->execute();
116
			}
117
			switch ($this->getFieldDataType()) {
118
				case 'picklist':
119
				case 'multipicklist':
120
						$query = (new \App\Db\Query())->from('vtiger_field')
121
							->where(['fieldname' => $fieldname])
122
							->andWhere(['in', 'uitype', [15, 16, 33]]);
123
						$dataReader = $query->createCommand()->query();
124
						if (!$dataReader->count()) {
125
							$db->createCommand()->dropTable('vtiger_' . $fieldname)->execute();
126
							//To Delete Sequence Table
127
							if ($db->isTableExists('vtiger_' . $fieldname . '_seq')) {
128
								$db->createCommand()->dropTable('vtiger_' . $fieldname . '_seq')->execute();
129
							}
130
							$db->createCommand()->delete('vtiger_picklist', ['name' => $fieldname])->execute();
131
						}
132
					break;
133
					case 'mapCoordinates':
134
						\App\Fields\MapCoordinates::reloadHandler();
135
						break;
136
				default:
137
					break;
138
			}
139
			$entityInfo = \App\Module::getEntityInfo($fldModule);
140
			foreach (['fieldnameArr' => 'fieldname', 'searchcolumnArr' => 'searchcolumn'] as $key => $name) {
141
				if (false !== ($fieldNameKey = array_search($fieldname, $entityInfo[$key]))) {
142
					unset($entityInfo[$key][$fieldNameKey]);
143
					$params = [
144
						'name' => $name,
145
						'tabid' => $tabId,
146
						'value' => $entityInfo[$key],
147
					];
148
					Settings_Search_Module_Model::save($params);
149
				}
150
			}
151
		} catch (\Throwable $ex) {
152
			\App\Log::error($ex->__toString());
153
			throw $ex;
154
		}
155
	}
156
157
	/**
158
	 * Function to Move the field.
159
	 *
160
	 * @param <Array> $fieldNewDetails
0 ignored issues
show
Documentation Bug introduced by
The doc comment <Array> at position 0 could not be parsed: Unknown type name '<' at position 0 in <Array>.
Loading history...
161
	 * @param <Array> $fieldOlderDetails
162
	 */
163
	public function move($fieldNewDetails, $fieldOlderDetails)
164
	{
165
		$db = \App\Db::getInstance();
166
		$newBlockId = $fieldNewDetails['blockId'];
167
		$olderBlockId = $fieldOlderDetails['blockId'];
168
169
		$newSequence = $fieldNewDetails['sequence'];
170
		$olderSequence = $fieldOlderDetails['sequence'];
171
172
		if ($olderBlockId == $newBlockId) {
173
			if ($newSequence > $olderSequence) {
174
				$db->createCommand()->update('vtiger_field', ['sequence' => new \yii\db\Expression('sequence - 1')], ['and', 'sequence > :olderSequence', 'sequence <= :newSequence', 'block = :olderBlockId'], [':olderSequence' => $olderSequence, ':newSequence' => $newSequence, ':olderBlockId' => $olderBlockId])->execute();
175
			} elseif ($newSequence < $olderSequence) {
176
				$db->createCommand()->update('vtiger_field', ['sequence' => new \yii\db\Expression('sequence + 1')], ['and', 'sequence < :olderSequence', 'sequence >= :newSequence', 'block = :olderBlockId'], [':olderSequence' => $olderSequence, ':newSequence' => $newSequence, ':olderBlockId' => $olderBlockId])->execute();
177
			}
178
			$db->createCommand()->update('vtiger_field', ['sequence' => $newSequence], ['fieldid' => $this->getId()])->execute();
179
		} else {
180
			$db->createCommand()->update('vtiger_field', ['sequence' => new \yii\db\Expression('sequence - 1')], ['and', 'sequence > :olderSequence', 'block = :olderBlockId'], [':olderSequence' => $olderSequence, ':olderBlockId' => $olderBlockId])->execute();
181
			$db->createCommand()->update('vtiger_field', ['sequence' => new \yii\db\Expression('sequence - 1')], ['and', 'sequence >= :newSequence', 'block = :newBlockId'], [':newSequence' => $newSequence, ':newBlockId' => $newBlockId])->execute();
182
183
			$db->createCommand()->update('vtiger_field', ['sequence' => $newSequence, 'block' => $newBlockId], ['fieldid' => $this->getId()])->execute();
184
		}
185
	}
186
187
	/**
188
	 * Function to activate field.
189
	 *
190
	 * @param int[] $fieldIdsList
191
	 * @param int   $blockId
192
	 */
193
	public static function makeFieldActive($fieldIdsList, $blockId)
194
	{
195
		$maxSequence = (new \App\Db\Query())->from('vtiger_field')
196
			->where(['block' => $blockId, 'presence' => [0, 2]])->max('sequence');
197
		foreach ($fieldIdsList as $fieldId) {
198
			++$maxSequence;
199
			$fieldInstance = self::getInstance($fieldId);
200
			$fieldInstance->set('sequence', $maxSequence);
201
			$fieldInstance->set('presence', 2);
202
			$fieldInstance->save();
203
		}
204
	}
205
206
	/**
207
	 * Function which specifies whether the field can have mandatory switch to happen.
208
	 *
209
	 * @return bool - true if we can make a field mandatory and non mandatory , false if we cant change previous state
210
	 */
211
	public function isMandatoryOptionDisabled(): bool
212
	{
213
		$focus = $this->getModule()->getEntityInstance();
214
		$compulsoryMandatoryFieldList = $focus->mandatory_fields ?? [];
215
216
		return \in_array($this->getName(), $compulsoryMandatoryFieldList) || \in_array($this->get('uitype'), ['4', '70']);
217
	}
218
219
	/**
220
	 * Function which will specify whether the active option is disabled.
221
	 *
222
	 * @return bool
223
	 */
224
	public function isActiveOptionDisabled(): bool
225
	{
226
		return 0 === (int) $this->get('presence') || 306 === (int) $this->get('uitype') || $this->isMandatoryOptionDisabled();
227
	}
228
229
	/**
230
	 * Function which will specify whether the quickcreate option is disabled.
231
	 *
232
	 * @return bool
233
	 */
234 21
	public function isQuickCreateOptionDisabled()
235
	{
236 21
		$moduleModel = $this->getModule();
237 21
		if (0 == $this->get('quickcreate') || 3 == $this->get('quickcreate') || !$moduleModel->isQuickCreateSupported()) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $this->get('quickcreate') of type mixed|null to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
238 21
			return true;
239 21
		}
240 21
		return false;
241
	}
242 21
243
	/**
244
	 * Function which will specify whether the mass edit option is disabled.
245
	 *
246
	 * @return bool
247
	 */
248
	public function isMassEditOptionDisabled()
249
	{
250
		if (0 == $this->get('masseditable') || 1 != $this->get('displaytype') || 3 == $this->get('masseditable')) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $this->get('masseditable') of type mixed|null to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
251
			return true;
252
		}
253
		return false;
254
	}
255
256
	/**
257
	 * Function which will specify whether the default value option is disabled.
258
	 *
259
	 * @return bool
260
	 */
261
	public function isDefaultValueOptionDisabled(): bool
262
	{
263
		if ($this->isMandatoryOptionDisabled() || $this->isReferenceField() || 'image' === $this->getFieldDataType() || 'multiImage' === $this->getFieldDataType()) {
264
			return true;
265
		}
266
		return false;
267
	}
268
269
	/**
270
	 * A function that will determine if the default value option is disabled for an WebserviceApps configuration.
271
	 *
272
	 * @return bool
273
	 */
274
	public function isDefaultValueForWebservice(): bool
275
	{
276
		return !(\in_array($this->get('uitype'), ['4', '70']) || 'image' === $this->getFieldDataType() || 'multiImage' === $this->getFieldDataType());
277
	}
278
279
	/**
280
	 * Function to check whether summary field option is disable or not.
281
	 *
282
	 * @return bool true/false
283
	 */
284
	public function isSummaryFieldOptionDisabled()
285
	{
286
		return 70 === $this->get('uitype');
287
	}
288
289
	/**
290
	 * Function to check field is editable or not.
291
	 *
292
	 * @param string $viewName
293
	 *
294
	 * @return bool true/false
295
	 */
296
	public function isEditable(string $viewName = 'Edit'): bool
297
	{
298
		if ('ModComments' === $this->getModuleName() && \in_array($this->getName(), ['commentcontent', 'userid', 'created_user_id', 'customer', 'reasontoedit', 'parents', 'assigned_user_id', 'creator', 'modifiedtime', 'related_to', 'createdtime', 'parent_comments'])) {
299
			return false;
300
		}
301
		return true;
302
	}
303
304
	/**
305
	 * Function to get instance.
306
	 *
307
	 * @param string $value  - fieldname or fieldid
308
	 * @param <type> $module - optional - module instance
0 ignored issues
show
Documentation Bug introduced by
The doc comment <type> at position 0 could not be parsed: Unknown type name '<' at position 0 in <type>.
Loading history...
309
	 *
310
	 * @return self
311
	 */
312
	public static function getInstance($value, $module = false)
313
	{
314
		$fieldObject = parent::getInstance($value, $module);
315
		$objectProperties = get_object_vars($fieldObject);
316
		$fieldModel = new self();
317
		foreach ($objectProperties as $properName => $propertyValue) {
318
			$fieldModel->{$properName} = $propertyValue;
319
		}
320
		return $fieldModel;
321
	}
322
323
	/**
324
	 * Function to get all fields list for all blocks.
325
	 *
326
	 * @param array List of block ids
327
	 * @param Vtiger_Module_Model $moduleInstance
328
	 * @param mixed               $blockId
329
	 *
330
	 * @return array List of Field models Settings_LayoutEditor_Field_Model
331
	 */
332
	public static function getInstanceFromBlockIdList($blockId, $moduleInstance = false)
333
	{
334
		if (!\is_array($blockId)) {
335
			$blockId = [$blockId];
336
		}
337
		$query = (new \App\Db\Query())->from('vtiger_field')->where(['block' => $blockId])->orderBy('sequence');
338
		$dataReader = $query->createCommand()->query();
339
		$fieldModelsList = [];
340
		while ($row = $dataReader->read()) {
341
			$fieldModel = new self();
342
			$fieldModel->initialize($row);
343
			if ($moduleInstance) {
344
				$fieldModel->setModule($moduleInstance);
345
			}
346
			$fieldModelsList[$row['fieldname']] = $fieldModel;
347
		}
348
		$dataReader->close();
349
350
		return $fieldModelsList;
351
	}
352
353
	/** {@inheritdoc} */
354
	public function getFieldInfo(): array
355
	{
356
		$fieldInfo = parent::getFieldInfo();
357
		$fieldInfo['isQuickCreateDisabled'] = $this->isQuickCreateOptionDisabled();
358
		$fieldInfo['isSummaryField'] = $this->isSummaryField();
359
		$fieldInfo['isSummaryFieldDisabled'] = $this->isSummaryFieldOptionDisabled();
360
		$fieldInfo['isMassEditDisabled'] = $this->isMassEditOptionDisabled();
361
		$fieldInfo['isDefaultValueDisabled'] = $this->isDefaultValueOptionDisabled();
362
		return $fieldInfo;
363
	}
364
365
	/**
366
	 * Get webservice data.
367
	 *
368
	 * @param int $webserviceApp
369
	 *
370
	 * @return array
371
	 */
372
	public function getWebserviceData(int $webserviceApp): array
373
	{
374
		if (isset($this->webserviceData)) {
375
			return $this->webserviceData;
376
		}
377
		return $this->webserviceData = (new \App\Db\Query())->from('w_#__fields_server')->where(['fieldid' => $this->getId(), 'serverid' => $webserviceApp])->one(\App\Db::getInstance('webservice')) ?: [];
378
	}
379
380
	/**
381
	 * Load webservice data.
382
	 *
383
	 * @param int $webserviceApp
384
	 *
385
	 * @return void
386
	 */
387
	public function loadWebserviceData(int $webserviceApp): void
388
	{
389
		$data = $this->getWebserviceData($webserviceApp);
390
		if (empty($data['is_default'])) {
391
			$this->set('defaultvalue', '');
392
		} else {
393
			$this->set('defaultvalue', $data['default_value']);
394
		}
395
		if (!empty($data['visibility'])) {
396
			$this->set('displaytype', $data['visibility']);
397
		}
398
	}
399
400
	/**
401
	 * Update webservice data.
402
	 *
403
	 * @param array $data
404
	 * @param int   $webserviceApp
405
	 *
406
	 * @return void
407
	 */
408
	public function updateWebserviceData(array $data, int $webserviceApp): void
409
	{
410
		$createCommand = \App\Db::getInstance('webservice')->createCommand();
411
		if ($this->getWebserviceData($webserviceApp)) {
412
			$createCommand->update('w_#__fields_server', $data, ['fieldid' => $this->getId(), 'serverid' => $webserviceApp])->execute();
413
		} else {
414
			$createCommand->insert('w_#__fields_server', \App\Utils::merge($data, ['fieldid' => $this->getId(), 'serverid' => $webserviceApp]))->execute();
415
		}
416
		\App\Cache::delete('WebserviceAppsFields', $webserviceApp);
417
	}
418
}
419