Passed
Push — developer ( a71332...bcab7f )
by Radosław
16:27
created

Vtiger_QuickExportData_Action::checkPermission()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 2
nc 2
nop 1
1
<?php
2
/**
3
 * Quick export data file.
4
 *
5
 * @package Action
6
 *
7
 * @copyright YetiForce S.A.
8
 * @license   YetiForce Public License 3.0 (licenses/LicenseEN.txt or yetiforce.com)
9
 * @author    Radosław Skrzypczak <[email protected]>
10
 */
11
12
/**
13
 * Quick export data class.
14
 */
15
class Vtiger_QuickExportData_Action extends Vtiger_Mass_Action
16
{
17
	/** @var string Module name */
18
	protected $moduelName;
19
	/** @var \App\Export\ExportRecords Export model instance */
20
	protected $exportModel;
21
22
	/** {@inheritdoc} */
23
	public function checkPermission(App\Request $request)
24
	{
25
		$this->moduelName = $request->getModule();
26
		$currentUserPriviligesModel = Users_Privileges_Model::getCurrentUserPrivilegesModel();
27
		if (!$currentUserPriviligesModel->hasModuleActionPermission($this->moduelName, 'QuickExportToExcel')) {
28
			throw new \App\Exceptions\NoPermitted('LBL_PERMISSION_DENIED', 406);
29
		}
30
	}
31
32
	/** {@inheritdoc} */
33
	public function process(App\Request $request)
34
	{
35
		$this->exportModel = \App\Export\ExportRecords::getInstance($this->moduelName, $request->getByType('export_type', \App\Purifier::ALNUM))
36
			->setLimit(\App\Config::performance('MAX_NUMBER_EXPORT_RECORDS'))
37
			->setFormat(\App\Export\ExportRecords::USER_FORMAT);
38
39
		$this->setDataFromRequest($request);
40
		$this->exportModel->sendHttpHeader();
41
		$this->exportModel->exportData();
42
	}
43
44
	/**
45
	 * Set condition data in export model.
46
	 *
47
	 * @param App\Request $request
48
	 *
49
	 * @return void
50
	 */
51
	public function setDataFromRequest(App\Request $request)
52
	{
53
		if ($request->has('xmlExportType')) {
54
			$this->exportModel->setTemplate($request->getByType('xmlExportType', 'Text'));
0 ignored issues
show
Bug introduced by
The method setTemplate() does not exist on App\Export\ExportRecords. It seems like you code against a sub-type of App\Export\ExportRecords such as Vtiger_ExportToXml_Model. ( Ignorable by Annotation )

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

54
			$this->exportModel->/** @scrutinizer ignore-call */ 
55
                       setTemplate($request->getByType('xmlExportType', 'Text'));
Loading history...
55
		}
56
		$queryGenerator = $this->exportModel->getQueryGenerator();
57
58
		$cvId = $request->getInteger('viewname');
59
		$queryGenerator->initForCustomViewById($cvId);
60
61
		$selectedIds = $request->getArray('selected_ids', \App\Purifier::ALNUM);
62
		if ($selectedIds && 'all' !== $selectedIds[0]) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $selectedIds 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...
63
			$queryGenerator->addCondition('id', $selectedIds, 'e');
64
		}
65
		$searchParams = \App\Condition::validSearchParams($this->moduelName, $request->getArray('search_params'));
66
		if ($searchParams) {
67
			$transformedSearchParams = $queryGenerator->parseBaseSearchParamsToCondition($searchParams);
68
			$queryGenerator->parseAdvFilter($transformedSearchParams);
69
		}
70
71
		$operator = $request->isEmpty('operator') ? '' : $request->getByType('operator');
72
		if ($operator && $searchValue = \App\Condition::validSearchValue($request->getByType('search_value', \App\Purifier::TEXT), $this->moduelName, $request->getByType('search_key', \App\Purifier::ALNUM), $operator)) {
73
			$queryGenerator->addCondition($request->getByType('search_key', \App\Purifier::ALNUM), $searchValue, $operator);
74
		}
75
		$queryGenerator->setStateCondition($request->getByType('entityState'));
76
77
		if ($excludedIds = $request->getArray('excluded_ids', \App\Purifier::INTEGER)) {
78
			$queryGenerator->addCondition('id', $excludedIds, 'n');
79
		}
80
81
		if (!$request->isEmpty('exportColumns', true) && $fields = $request->getArray('exportColumns', \App\Purifier::TEXT)) {
82
			$this->exportModel->setFields($fields);
83
		} else {
84
			$fields = \App\CustomView::getInstance($this->moduelName)->getColumnsListByCvid($cvId);
85
			array_walk($fields, function (&$fieldInfo) {
86
				['field_name' => $relatedFieldName, 'module_name' => $relatedModule, 'source_field_name' => $referenceField] = $fieldInfo;
87
				$fieldInfo = $referenceField ? "{$relatedFieldName}:{$relatedModule}:{$referenceField}" : $relatedFieldName;
88
			});
89
			$this->exportModel->setFields($fields);
90
		}
91
	}
92
}
93