Completed
Push — dev ( 35c279...79fff2 )
by Nicolas
01:54
created

view()   D

Complexity

Conditions 21
Paths 42

Size

Total Lines 122
Code Lines 84

Duplication

Lines 20
Ratio 16.39 %
Metric Value
dl 20
loc 122
rs 4.6955
cc 21
eloc 84
nc 42
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 2014
4
	LICENCE: MIT http://deuxhuithuit.mit-license.org;
5
	*/
6
7
	if(!defined("__IN_SYMPHONY__")) die("<h2>Error</h2><p>You cannot directly access this file</p>");
8
9
	require_once(TOOLKIT . '/class.xmlpage.php');
10
	require_once(EXTENSIONS . '/entry_relationship_field/lib/class.cacheablefetch.php');
11
	require_once(EXTENSIONS . '/entry_relationship_field/lib/class.erfxsltutilities.php');
12
13
	class contentExtensionEntry_Relationship_FieldRender extends XMLPage {
14
		
15
		const NUMBER_OF_URL_PARAMETERS = 2;
16
17
		private $sectionManager;
18
		private $fieldManager;
19
		private $entryManager;
20
		
21
		public function __construct() {
22
			parent::__construct();
23
			$this->sectionManager = new CacheableFetch('SectionManager');
24
			$this->fieldManager = new CacheableFetch('FieldManager');
25
			$this->entryManager = new CacheableFetch('EntryManager');
26
			// fix jquery
27
			$this->_Result->setIncludeHeader(false);
28
			$this->addHeaderToPage('Content-Type', 'text/html');
29
		}
30
		
31
		/**
32
		 *
33
		 * Builds the content view
34
		 */
35
		public function view() {
36
			// _context[0] => entry values
37
			// _context[1] => fieldId
38
			if (!is_array($this->_context) || empty($this->_context)) {
39
				$this->_Result->appendChild(new XMLElement('error', __('Parameters not found')));
40
				return;
41
			}
42
			else if (count($this->_context) < self::NUMBER_OF_URL_PARAMETERS) {
43
				$this->_Result->appendChild(new XMLElement('error', __('Not enough parameters')));
44
				return;
45
			}
46
			else if (count($this->_context) > self::NUMBER_OF_URL_PARAMETERS) {
47
				$this->_Result->appendChild(new XMLElement('error', __('Too many parameters')));
48
				return;
49
			}
50
			
51
			$entriesId = explode(',', MySQL::cleanValue($this->_context[0]));
52
			$entriesId = array_map(array('General', 'intval'), $entriesId);
53 View Code Duplication
			if (!is_array($entriesId) || empty($entriesId)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
				$this->_Result->appendChild(new XMLElement('error', __('No entry no found')));
55
				return;
56
			}
57
			
58
			$parentFieldId = General::intval($this->_context[1]);
59
			if ($parentFieldId < 1) {
60
				$this->_Result->appendChild(new XMLElement('error', __('Parent field id not valid')));
61
				return;
62
			}
63
			
64
			$parentField = $this->fieldManager->fetch($parentFieldId);
65 View Code Duplication
			if (!$parentField || empty($parentField)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
				$this->_Result->appendChild(new XMLElement('error', __('Parent field not found')));
67
				return;
68
			}
69
			
70
			if ($parentField->get('type') != 'entry_relationship') {
71
				$this->_Result->appendChild(new XMLElement('error', __('Parent field is `%s`, not `entry_relationship`', array($parentField->get('type')))));
72
				return;
73
			}
74
			
75
			// Get entries one by one since they may belong to
76
			// different sections, which prevents us from
77
			// passing an array of entryId.
78
			foreach ($entriesId as $key => $entryId) {
79
				$entry = $this->entryManager->fetch($entryId);
80
				if (empty($entry)) {
81
					$li = new XMLElement('li', null, array(
82
						'data-entry-id' => $entryId
83
					));
84
					$header = new XMLElement('header', null, array('class' => 'frame-header no-content ignore-collapsible'));
85
					$title = new XMLElement('h4');
86
					$title->appendChild(new XMLElement('strong', __('Entry %s not found', array($entryId))));
87
					$header->appendChild($title);
88
					$options = new XMLElement('div', null, array('class' => 'destructor'));
89 View Code Duplication
					if ($parentField->is('allow_delete') || $parentField->is('allow_link')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
						$options->appendChild(new XMLElement('a', __('Un-link'), array(
91
							'class' => 'unlink ignore-collapsible',
92
							'data-unlink' => $entryId,
93
						)));
94
					}
95
					$header->appendChild($options);
96
					$li->appendChild($header);
97
					$this->_Result->appendChild($li);
98
				} else {
99
					$entry = $entry[0];
100
					$entrySection = $this->sectionManager->fetch($entry->get('section_id'));
101
					$entryVisibleFields = $entrySection->fetchVisibleColumns();
102
					$entryFields = $entrySection->fetchFields();
103
					$entrySectionHandle = $this->getSectionName($entry, 'handle');
104
					
105
					$li = new XMLElement('li', null, array(
106
						'data-entry-id' => $entryId,
107
						'data-section' => $entrySectionHandle,
108
						'data-section-id' => $entrySection->get('id'),
109
					));
110
					$header = new XMLElement('header', null, array('class' => 'frame-header'));
111
					$title = new XMLElement('h4', null, array('class' => 'ignore-collapsible'));
112
					$title->appendChild(new XMLElement('strong', $this->getEntryTitle($entry, $entryVisibleFields, $entryFields), array('class' => 'ignore-collapsible')));
113
					$title->appendChild(new XMLElement('span', $this->getSectionName($entry), array('class' => 'ignore-collapsible')));
114
					$header->appendChild($title);
115
					$options = new XMLElement('div', null, array('class' => 'destructor'));
116
					if ($parentField->is('allow_edit')) {
117
						$title->setAttribute('data-edit', $entryId);
118
						$options->appendChild(new XMLElement('a', __('Edit'), array(
119
							'class' => 'edit ignore-collapsible',
120
							'data-edit' => $entryId,
121
						)));
122
					}
123
					if ($parentField->is('allow_delete')) {
124
						$options->appendChild(new XMLElement('a', __('Delete'), array(
125
							'class' => 'delete ignore-collapsible',
126
							'data-delete' => $entryId,
127
						)));
128
					}
129
					if ($parentField->is('allow_link')) {
130
						$options->appendChild(new XMLElement('a', __('Replace'), array(
131
							'class' => 'unlink ignore-collapsible',
132
							'data-replace' => $entryId,
133
						)));
134
					}
135 View Code Duplication
					if ($parentField->is('allow_delete') || $parentField->is('allow_link')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
						$options->appendChild(new XMLElement('a', __('Un-link'), array(
137
							'class' => 'unlink ignore-collapsible',
138
							'data-unlink' => $entryId,
139
						)));
140
					}
141
					$header->appendChild($options);
142
					$li->appendChild($header);
143
					
144
					$content = ERFXSLTUTilities::entryToXml($parentField, $entry, $entrySectionHandle, $entryFields);
145
					
146
					if ($content) {
147
						$li->appendChild($content);
148
					}
149
					else {
150
						$header->setAttribute('class', $header->getAttribute('class') . ' no-content');
151
					}
152
					
153
					$this->_Result->appendChild($li);
154
				}
155
			}
156
		}
157
		
158
		public function getSectionName($entry, $name = 'name') {
159
			$sectionId = $entry->get('section_id');
160
			return $this->sectionManager->fetch($sectionId)->get($name);
161
		}
162
		
163
		public function getEntryTitle($entry, $entryVisibleFields, $entryFields) {
164
			$data = $entry->getData();
165
			$field = empty($entryVisibleFields) ? $entryFields : $entryVisibleFields;
166
			if (is_array($field)) {
167
				$field = current($field);
168
			}
169
			
170
			if ($field == null) {
171
				return __('None');
172
			}
173
			
174
			return $field->prepareReadableValue($data[$field->get('id')], $entry->get('id'), true);
175
		}
176
	}