Passed
Pull Request — developer (#17138)
by
unknown
18:27
created

Vtiger_Delete_Action::process()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * Delete record action file.
4
 *
5
 * @package Action
6
 *
7
 * @copyright YetiForce S.A.
8
 * @license   YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com)
9
 */
10
11
/**
12
 * Delete record action class.
13
 */
14
class Vtiger_Delete_Action extends \App\Controller\Action
15
{
16
	/**
17
	 * Record model instance.
18
	 *
19
	 * @var Vtiger_Record_Model
20
	 */
21
	protected $record;
22
23
	/** {@inheritdoc} */
24
	public function checkPermission(App\Request $request)
25
	{
26
		if ($request->isEmpty('record', true)) {
27
			throw new \App\Exceptions\NoPermittedToRecord('ERR_NO_PERMISSIONS_FOR_THE_RECORD', 406);
28
		}
29
		$this->record = Vtiger_Record_Model::getInstanceById($request->getInteger('record'), $request->getModule());
30
		if (!$this->record->privilegeToDelete()) {
31
			throw new \App\Exceptions\NoPermittedToRecord('ERR_NO_PERMISSIONS_FOR_THE_RECORD', 406);
32
		}
33
	}
34
35
	/** {@inheritdoc} */
36
	public function process(App\Request $request)
37
	{
38
		$result = $this->performDelete($request);
39
40
		$response = new Vtiger_Response();
41
		$response->setEmitType(Vtiger_Response::$EMIT_JSON);
42
		$response->setResult($result);
43
		$response->emit();
44
	}
45
46
	protected function performDelete(App\Request $request): array
47
	{
48
		$result = [];
49
		$skipHandlers = $request->getArray('skipHandlers', \App\Purifier::ALNUM, [], \App\Purifier::INTEGER);
50
		$eventHandler = $this->record->getEventHandler();
51
		foreach ($eventHandler->getHandlers(\App\EventHandler::PRE_DELETE) as $handler) {
52
			$handlerId = $handler['eventhandler_id'];
53
			$response = $eventHandler->triggerHandler($handler);
54
			if (!($response['result'] ?? null) && (!isset($response['hash'], $skipHandlers[$handlerId]) || $skipHandlers[$handlerId] !== $response['hash'])) {
55
				if ($result && 'confirm' === ($response['type'] ?? '')) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $result 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...
56
					break;
57
				}
58
				$result[$handlerId] = $response;
59
			}
60
		}
61
		if (!$result) {
0 ignored issues
show
introduced by
$result is of type array, thus it always evaluated to false.
Loading history...
62
			$this->record->delete();
63
			if ('List' === $request->getByType('sourceView')) {
64
				$result = ['notify' => ['type' => 'success', 'text' => \App\Language::translate('LBL_RECORD_HAS_BEEN_DELETED')]];
65
			} else {
66
				$result = ['url' => $this->record->getModule()->getListViewUrl()];
67
			}
68
		}
69
		return $result;
70
	}
71
}
72