Completed
Pull Request — dev (#67)
by
unknown
01:21
created

view()   C

Complexity

Conditions 12
Paths 168

Size

Total Lines 85

Duplication

Lines 16
Ratio 18.82 %

Importance

Changes 0
Metric Value
dl 16
loc 85
rs 5.4471
c 0
b 0
f 0
cc 12
nc 168
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 https://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
		$excludes = !isset($this->_context[1]) ? '' : General::sanitize($this->_context[1]);
24
25
		if ($sectionId < 1) {
26
			$this->_Result['status'] = Page::HTTP_STATUS_BAD_REQUEST;
27
			$this->_Result['error'] = __('No section id found');
28
			return;
29
		}
30
31
		$section = (new SectionManager)
32
			->select()
33
			->section($sectionId)
34
			->execute()
35
			->next();
36
37 View Code Duplication
		if (empty($section)) {
38
			$this->_Result['status'] = Page::HTTP_STATUS_NOT_FOUND;
39
			$this->_Result['error'] = __('Section not found');
40
			return;
41
		}
42
43
		$query = General::sanitize($_GET['query']);
44
		$entries = array();
45
		$filterableFields = $section->fetchFilterableFields();
46 View Code Duplication
		if (empty($filterableFields)) {
47
			$this->_Result['status'] = Page::HTTP_STATUS_BAD_REQUEST;
48
			$this->_Result['error'] = __('Section not filterable');
49
			return;
50
		}
51
52
		$primaryField = $section->fetchVisibleColumns();
53
		if (empty($primaryField)) {
54
			$primaryField = current($filterableFields);
55
			reset($filterableFields);
56
		} else {
57
			$primaryField = current($primaryField);
58
		}
59
60
		foreach ($filterableFields as $fId => $field) {
61
			$q = (new EntryManager)
62
				->select()
63
				->sort('system:id', 'asc')
64
				->section($sectionId)
65
				->includeAllFields();
66
67
			if (strlen($query) != 0) {
68
				$q->filter($field->get('id'), ['regexp:' . $query]);
69
			}
70
71
			if (strlen($excludes) != 0) {
72
				$q->filter('system:id', ['not:' . $excludes]);
73
			}
74
75
			$fEntries = $q
76
				->execute()
77
				->rows();
78
79
			if (!empty($fEntries)) {
80
				$entries = array_merge($entries, $fEntries);
81
			}
82
		}
83
84
		$entries = array_map(function ($entry) use ($primaryField) {
85
			return array(
86
				'value' => $entry->get('id') . ':' .
87
					$primaryField->prepareReadableValue(
88
						$entry->getData($primaryField->get('id')),
89
						$entry->get('id')
90
				),
91
			);
92
		}, $entries);
93
94
		$this->_Result['entries'] = $entries;
95
	}
96
}
97