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

contentExtensionEntry_relationship_fieldSearch   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 84
Duplicated Lines 19.05 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 0
dl 16
loc 84
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C view() 16 81 11

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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