Completed
Push — dev ( da0648...23d1c0 )
by Nicolas
01:15
created

view()   C

Complexity

Conditions 11
Paths 92

Size

Total Lines 81
Code Lines 58

Duplication

Lines 16
Ratio 19.75 %

Importance

Changes 0
Metric Value
dl 16
loc 81
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 58
nc 92
nop 0

How to fix   Long Method    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
 * Copyright: Deux Huit Huit 2017
4
 * LICENCE: MIT http://deuxhuithuit.mit-license.org;
5
 */
6
7
require_once(TOOLKIT . '/class.jsonpage.php');
8
9
class contentExtensionEntry_relationship_fieldSearch extends JSONPage
10
{
11
	public function view()
12
	{
13 View Code Duplication
		if (class_exists('FLang')) {
14
			try {
15
				FLang::setMainLang(Lang::get());
16
				FLang::setLangCode(Lang::get(), '');
17
			} catch (Exception $ex) { }
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
18
		}
19
		
20
		$section = General::sanitize($this->_context[0]);
21
		$sectionId = SectionManager::fetchIDFromHandle($section);
22
		$sectionId = General::intval($sectionId);
23
24
		if ($sectionId < 1) {
25
			$this->_Result['status'] = Page::HTTP_STATUS_BAD_REQUEST;
26
			$this->_Result['error'] = __('No section id found');
27
			return;
28
		}
29
30
		$section = SectionManager::fetch($sectionId);
31
32 View Code Duplication
		if (empty($section)) {
33
			$this->_Result['status'] = Page::HTTP_STATUS_NOT_FOUND;
34
			$this->_Result['error'] = __('Section not found');
35
			return;
36
		}
37
38
		$query = General::sanitize($_GET['query']);
39
		$entries = array();
40
		$filterableFields = $section->fetchFilterableFields();
41 View Code Duplication
		if (empty($filterableFields)) {
42
			$this->_Result['status'] = Page::HTTP_STATUS_BAD_REQUEST;
43
			$this->_Result['error'] = __('Section not filterable');
44
			return;
45
		}
46
47
		$primaryField = $section->fetchVisibleColumns();
48
		if (empty($primaryField)) {
49
			$primaryField = current($filterableFields);
50
			reset($filterableFields);
51
		} else {
52
			$primaryField = current($primaryField);
53
		}
54
55
		foreach ($filterableFields as $fId => $field) {
56
			EntryManager::setFetchSorting($fId, 'ASC');
57
			$fQuery = $query;
58
			$joins = '';
59
			$where = '';
60
			$opt = array_map(function ($op) {
61
				return trim($op['filter']);
62
			}, $field->fetchFilterableOperators());
63
			if (in_array('regexp:', $opt)) {
64
				$fQuery = 'regexp: ' . $fQuery;
65
			}
66
			$field->buildDSRetrievalSQL(array($fQuery), $joins, $where, false);
67
			$fEntries = EntryManager::fetch(null, $sectionId, 10, 0, $where, $joins, true, false);
68
			if (!empty($fEntries)) {
69
				$entries = array_merge($entries, $fEntries);
70
			}
71
		}
72
73
		EntryManager::setFetchSorting('system:id', 'ASC');
74
		if (!empty($entries)) {
75
			$entries = EntryManager::fetch(array_unique(array_map(function ($e) {
76
				return $e['id'];
77
			}, $entries)), $sectionId);
78
		}
79
80
		$entries = array_map(function ($entry) use ($primaryField) {
81
			return array(
82
				'value' => $entry->get('id') . ':' .
83
					$primaryField->prepareReadableValue(
84
						$entry->getData($primaryField->get('id')),
85
						$entry->get('id')
86
				),
87
			);
88
		}, $entries);
89
90
		$this->_Result['entries'] = $entries;
91
	}
92
}
93