Passed
Push — developer ( 6b5868...bed0f9 )
by Radosław
22:42 queued 03:39
created

Vtiger_RelatedList_View   A

Complexity

Total Complexity 42

Size/Duplication

Total Lines 193
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 42
eloc 132
c 2
b 0
f 0
dl 0
loc 193
ccs 0
cts 113
cp 0
rs 9.0399

4 Methods

Rating   Name   Duplication   Size   Complexity  
A checkPermission() 0 11 4
A loadView() 0 3 1
A getTemplateName() 0 3 1
F process() 0 149 36

How to fix   Complexity   

Complex Class

Complex classes like Vtiger_RelatedList_View often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Vtiger_RelatedList_View, and based on these observations, apply Extract Interface, too.

1
<?php
2
/* +***********************************************************************************
3
 * The contents of this file are subject to the vtiger CRM Public License Version 1.0
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 Vtiger_RelatedList_View extends Vtiger_Index_View
13
{
14
	/**
15
	 * Checking permissions.
16
	 *
17
	 * @param \App\Request $request
18
	 *
19
	 * @throws \App\Exceptions\NoPermittedToRecord
20
	 */
21
	public function checkPermission(App\Request $request)
22
	{
23
		if ($request->isEmpty('record', true)) {
24
			throw new \App\Exceptions\NoPermittedToRecord('ERR_NO_PERMISSIONS_FOR_THE_RECORD', 406);
25
		}
26
		if (!\App\Privilege::isPermitted($request->getModule(), 'DetailView', $request->getInteger('record'))) {
27
			throw new \App\Exceptions\NoPermittedToRecord('ERR_NO_PERMISSIONS_FOR_THE_RECORD', 406);
28
		}
29
		$currentUserPrivilegesModel = Users_Privileges_Model::getCurrentUserPrivilegesModel();
30
		if (!$currentUserPrivilegesModel->hasModulePermission($request->getByType('relatedModule', 2))) {
31
			throw new \App\Exceptions\NoPermitted('LBL_PERMISSION_DENIED', 406);
32
		}
33
	}
34
35
	/**
36
	 * Process.
37
	 *
38
	 * @param \App\Request $request
39
	 */
40
	public function process(App\Request $request)
41
	{
42
		$moduleName = $request->getModule();
43
		$relatedModuleName = $request->getByType('relatedModule', 2);
44
		$parentId = $request->getInteger('record');
45
		if ($request->isEmpty('relatedView', true)) {
46
			$relatedView = empty($_SESSION['relatedView'][$moduleName][$relatedModuleName]) ? 'List' : $_SESSION['relatedView'][$moduleName][$relatedModuleName];
47
		} else {
48
			$relatedView = $request->getByType('relatedView');
49
			$_SESSION['relatedView'][$moduleName][$relatedModuleName] = $relatedView;
50
		}
51
		$pageNumber = $request->isEmpty('page', true) ? 1 : $request->getInteger('page');
52
		$totalCount = $request->isEmpty('totalCount', true) ? 0 : $request->getInteger('totalCount');
53
		$pagingModel = new Vtiger_Paging_Model();
54
		$pagingModel->set('page', $pageNumber);
55
		if ($request->has('limit')) {
56
			$pagingModel->set('limit', $request->getInteger('limit'));
57
		}
58
		$cvId = $request->isEmpty('cvId', true) ? 0 : $request->getByType('cvId', 'Alnum');
59
		$parentRecordModel = Vtiger_Record_Model::getInstanceById($parentId, $moduleName);
60
		$relationListView = Vtiger_RelationListView_Model::getInstance($parentRecordModel, $relatedModuleName, $request->getInteger('relationId'), $cvId);
61
62
		$orderBy = $request->getArray('orderby', \App\Purifier::STANDARD, [], \App\Purifier::SQL);
63
		if (empty($orderBy)) {
64
			$moduleInstance = $relationListView->getRelatedModuleModel()->getEntityInstance();
65
			if ($moduleInstance->default_order_by && $moduleInstance->default_sort_order) {
66
				$orderBy = [];
67
				foreach ((array) $moduleInstance->default_order_by as $value) {
68
					$orderBy[$value] = $moduleInstance->default_sort_order;
69
				}
70
			}
71
		}
72
		if (!empty($orderBy)) {
73
			$relationListView->set('orderby', $orderBy);
74
		}
75
		if ($request->has('entityState')) {
76
			$relationListView->set('entityState', $request->getByType('entityState'));
77
		}
78
		$viewer = $this->getViewer($request);
79
		$operator = 's';
80
		if (!$request->isEmpty('operator', true)) {
81
			$operator = $request->getByType('operator');
82
			$relationListView->set('operator', $operator);
83
			$viewer->assign('OPERATOR', $operator);
84
		}
85
		if (!$request->isEmpty('search_key', true)) {
86
			$searchKey = $request->getByType('search_key', 'Alnum');
87
			$searchValue = App\Condition::validSearchValue($request->getByType('search_value', 'Text'), $relationListView->getQueryGenerator()->getModule(), $searchKey, $operator);
88
			$relationListView->set('search_key', $searchKey);
89
			$relationListView->set('search_value', $searchValue);
90
			$viewer->assign('ALPHABET_VALUE', $searchValue);
91
		}
92
		$searchParams = App\Condition::validSearchParams($relationListView->getQueryGenerator()->getModule(), $request->getArray('search_params'));
93
		if (empty($searchParams) || !\is_array($searchParams)) {
94
			$searchParamsRaw = $searchParams = [];
95
		}
96
		$queryGenerator = $relationListView->getQueryGenerator();
97
		$transformedSearchParams = $queryGenerator->parseBaseSearchParamsToCondition($searchParams);
98
		$relationListView->set('search_params', $transformedSearchParams);
99
		$relationListView->loadSearchLockedFields($request);
100
		//To make smarty to get the details easily accesible
101
		foreach ($request->getArray('search_params') as $fieldListGroup) {
102
			$searchParamsRaw[] = $fieldListGroup;
103
			foreach ($fieldListGroup as $fieldSearchInfo) {
104
				$fieldSearchInfo['searchValue'] = $fieldSearchInfo[2] ?? '';
105
				$fieldSearchInfo['fieldName'] = $fieldName = $fieldSearchInfo[0] ?? '';
106
				$fieldSearchInfo['specialOption'] = $fieldSearchInfo[3] ?? '';
107
				$searchParams[$fieldName] = $fieldSearchInfo;
108
			}
109
		}
110
		$showHeader = true;
111
		if ($request->has('showHeader')) {
112
			$showHeader = $request->getBoolean('showHeader');
113
		}
114
		if ($showHeader) {
115
			$links = $relationListView->getLinks();
116
			if (!($request->has('showViews') ? $request->getBoolean('showViews') : true)) {
117
				unset($links['RELATEDLIST_VIEWS']);
118
				$relatedView = 'List';
119
			}
120
			if (!($request->has('showMassActions') ? $request->getBoolean('showMassActions') : true)) {
121
				unset($links['RELATEDLIST_MASSACTIONS']);
122
			}
123
			$viewer->assign('RELATED_LIST_LINKS', $links);
124
		}
125
		if ('ListPreview' === $relatedView) {
126
			$relationListView->setFields(array_merge(['id'], $relationListView->getRelatedModuleModel()->getNameFields()));
127
		}
128
		if ($request->has('fields')) {
129
			$relationListView->setFields(array_merge(['id'], $request->getArray('fields', 'Alnum')));
130
		}
131
		if ($request->has('quickSearchEnabled')) {
132
			$relationListView->set('quickSearchEnabled', $request->getBoolean('quickSearchEnabled'));
133
		}
134
		$models = $relationListView->getEntries($pagingModel);
135
		$header = $relationListView->getHeaders();
136
		$relationModel = $relationListView->getRelationModel();
137
		if ($request->has('sortEnabled')) {
138
			$relationListView->set('advSortEnabled', $request->getBoolean('sortEnabled'));
139
		}
140
		$viewer->assign('VIEW_MODEL', $relationListView);
141
		$viewer->assign('RELATED_RECORDS', $models);
142
		$viewer->assign('PARENT_RECORD', $parentRecordModel);
143
		$viewer->assign('RELATED_VIEW', $relatedView);
144
		$viewer->assign('SHOW_SUMMATION_ROW', $request->has('showSummation') ? $request->getBoolean('showSummation') : true);
145
		$viewer->assign('SHOW_HEADER', $showHeader);
146
		$viewer->assign('SHOW_CREATOR_DETAIL', $relationModel->showCreatorDetail());
147
		$viewer->assign('SHOW_COMMENT', $relationModel->showComment());
148
		$viewer->assign('RELATED_HEADERS', $header);
149
		$viewer->assign('RELATED_MODULE', $relationModel->getRelationModuleModel());
150
		$viewer->assign('RELATED_ENTIRES_COUNT', \count($models));
151
		$viewer->assign('RELATION_FIELD', $relationModel->getRelationField());
152
		if (\App\Config::performance('LISTVIEW_COMPUTE_PAGE_COUNT')) {
153
			$totalCount = (int) $relationListView->getRelatedEntriesCount();
154
			$pagingModel->set('totalCount', $totalCount);
155
		} elseif (!empty($totalCount)) {
156
			$pagingModel->set('totalCount', $totalCount);
157
		}
158
		$viewer->assign('LISTVIEW_COUNT', $totalCount);
159
		$viewer->assign('TOTAL_ENTRIES', $totalCount);
160
		$viewer->assign('PAGE_COUNT', $pagingModel->getPageCount());
161
		$viewer->assign('PAGE_NUMBER', $pageNumber);
162
		$viewer->assign('START_PAGIN_FROM', $pagingModel->getStartPagingFrom());
163
		$viewer->assign('MODULE', $moduleName);
164
		$viewer->assign('PAGING_MODEL', $pagingModel);
165
		$viewer->assign('ORDER_BY', $orderBy);
166
		$viewer->assign('INVENTORY_FIELDS', $relationModel->getRelationInventoryFields());
167
		$isFavorites = false;
168
		if ($relationModel->isFavorites() && \App\Privilege::isPermitted($moduleName, 'FavoriteRecords')) {
169
			$favorites = $relationListView->getFavoriteRecords();
170
			$viewer->assign('FAVORITES', $favorites);
171
			$isFavorites = $relationModel->isFavorites();
172
		}
173
		$viewer->assign('IS_FAVORITES', $isFavorites);
174
		$viewer->assign('IS_EDITABLE', $relationModel->isEditable());
175
		$viewer->assign('USER_MODEL', Users_Record_Model::getCurrentUserModel());
176
		$viewer->assign('SEARCH_DETAILS', $searchParams);
177
		$viewer->assign('SEARCH_PARAMS', $searchParamsRaw);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $searchParamsRaw does not seem to be defined for all execution paths leading up to this point.
Loading history...
178
		$viewer->assign('VIEW', $request->getByType('view'));
179
		$viewer->assign('SHOW_RELATED_WIDGETS', \in_array($relationModel->getId(), App\Config::module($moduleName, 'showRelatedWidgetsByDefault', [])));
180
		$viewer->assign('LOCKED_EMPTY_FIELDS', $request->isEmpty('lockedEmptyFields', true) ? [] : $request->getArray('lockedEmptyFields'));
181
		if ($relationListView->isWidgetsList()) {
182
			$viewer->assign('IS_WIDGETS', true);
183
			$viewer->assign('HIERARCHY_VALUE', \Config\Modules\ModComments::$defaultSource);
0 ignored issues
show
Bug introduced by
The type Config\Modules\ModComments was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
184
			$viewer->assign('HIERARCHY', \App\ModuleHierarchy::getModuleLevel($relatedModuleName));
185
		} else {
186
			$viewer->assign('IS_WIDGETS', false);
187
		}
188
		return $this->loadView();
189
	}
190
191
	/**
192
	 * Load template.
193
	 */
194
	public function loadView()
195
	{
196
		return $this->viewer->view($this->getTemplateName(), $this->viewer->getTemplateVars('MODULE_NAME'), true);
197
	}
198
199
	/**
200
	 * Template name.
201
	 */
202
	public function getTemplateName()
203
	{
204
		return 'RelatedList.tpl';
205
	}
206
}
207