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

view()   F

Complexity

Conditions 28
Paths 816

Size

Total Lines 156
Code Lines 108

Duplication

Lines 18
Ratio 11.54 %

Importance

Changes 0
Metric Value
dl 18
loc 156
rs 2.1804
c 0
b 0
f 0
cc 28
eloc 108
nc 816
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 View Code Duplication
			if (class_exists('FLang')) {
37
				try {
38
					FLang::setMainLang(Lang::get());
39
					FLang::setLangCode(Lang::get(), '');
40
				} catch (Exception $ex) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
41
			}
42
			// _context[0] => entry values
43
			// _context[1] => fieldId
44
			if (!is_array($this->_context) || empty($this->_context) || $this->_context[0] === 'null') {
45
				$this->_Result->appendChild(new XMLElement('error', __('Parameters not found')));
46
				return;
47
			}
48
			else if (count($this->_context) < self::NUMBER_OF_URL_PARAMETERS) {
49
				$this->_Result->appendChild(new XMLElement('error', __('Not enough parameters')));
50
				return;
51
			}
52
			else if (count($this->_context) > self::NUMBER_OF_URL_PARAMETERS) {
53
				$this->_Result->appendChild(new XMLElement('error', __('Too many parameters')));
54
				return;
55
			}
56
			
57
			$entriesId = explode(',', MySQL::cleanValue($this->_context[0]));
58
			$entriesId = array_map(array('General', 'intval'), $entriesId);
59 View Code Duplication
			if (!is_array($entriesId) || empty($entriesId)) {
60
				$this->_Result->appendChild(new XMLElement('error', __('No entry no found')));
61
				return;
62
			}
63
			
64
			$parentFieldId = General::intval($this->_context[1]);
65
			if ($parentFieldId < 1) {
66
				$this->_Result->appendChild(new XMLElement('error', __('Parent field id not valid')));
67
				return;
68
			}
69
			
70
			$parentField = $this->fieldManager->fetch($parentFieldId);
71 View Code Duplication
			if (!$parentField || empty($parentField)) {
72
				$this->_Result->appendChild(new XMLElement('error', __('Parent field not found')));
73
				return;
74
			}
75
			
76 View Code Duplication
			if (!($parentField instanceof FieldRelationship)) {
77
				$this->_Result->appendChild(new XMLElement('error', __('Parent field is `%s`, not `relationship field`', array($parentField->get('type')))));
78
				return;
79
			}
80
			
81
			// Get entries one by one since they may belong to
82
			// different sections, which prevents us from
83
			// passing an array of entryId.
84
			foreach ($entriesId as $key => $entryId) {
85
				$entry = $this->entryManager->fetch($entryId);
86
				if (empty($entry)) {
87
					$li = new XMLElement('li', null, array(
88
						'data-entry-id' => $entryId
89
					));
90
					$header = new XMLElement('header', null, array('class' => 'frame-header no-content ignore-collapsible'));
91
					$title = new XMLElement('h4');
92
					$title->appendChild(new XMLElement('strong', __('Entry %s not found', array($entryId))));
93
					$header->appendChild($title);
94
					$options = new XMLElement('div', null, array('class' => 'destructor'));
95
					$options->appendChild(new XMLElement('a', __('Un-link'), array(
96
						'class' => 'unlink ignore-collapsible',
97
						'data-unlink' => $entryId,
98
					)));
99
					$header->appendChild($options);
100
					$li->appendChild($header);
101
					$this->_Result->appendChild($li);
102
				} else {
103
					$entry = $entry[0];
104
					$entrySection = $this->sectionManager->fetch($entry->get('section_id'));
105
					$entryVisibleFields = $entrySection->fetchVisibleColumns();
106
					$entryFields = $entrySection->fetchFields();
107
					$entrySectionHandle = $this->getSectionName($entry, 'handle');
108
					
109
					$li = new XMLElement('li', null, array(
110
						'data-entry-id' => $entryId,
111
						'data-section' => $entrySectionHandle,
112
						'data-section-id' => $entrySection->get('id'),
113
						'data-timestamp' => $entry->get('modification_date'),
114
					));
115
					if ($parentField->is('show_header')) {
116
						$header = new XMLElement('header', null, array(
117
							'class' => 'frame-header',
118
							'data-orderable-handle' => '',
119
							'data-collapsible-handle' => ''
120
						));
121
						$title = new XMLElement('h4', null, array('class' => 'ignore-collapsible'));
122
						if (!$parentField->get('mode_header')) {
123
							$title->appendChildArray($this->buildDefaultTitle($entry, $entryVisibleFields, $entryFields));
124
						}
125
						else {
126
							$title->setValue(ERFXSLTUTilities::processXSLT($parentField, $entry, $entrySectionHandle, $entryFields, 'mode_header'));
127
						}
128
						$header->appendChild($title);
129
						
130
						$options = new XMLElement('div', null, array('class' => 'destructor'));
131
						if ($parentField->is('allow_edit')) {
132
							$title->setAttribute('data-edit', $entryId);
133
							$options->appendChild(new XMLElement('a', __('Edit'), array(
134
								'class' => 'edit ignore-collapsible',
135
								'data-edit' => $entryId,
136
							)));
137
						}
138
						if ($parentField->is('allow_delete')) {
139
							$options->appendChild(new XMLElement('a', __('Delete'), array(
140
								'class' => 'delete ignore-collapsible',
141
								'data-delete' => $entryId,
142
							)));
143
						}
144
						if ($parentField->is('allow_link')) {
145
							$options->appendChild(new XMLElement('a', __('Replace'), array(
146
								'class' => 'unlink ignore-collapsible',
147
								'data-replace' => $entryId,
148
							)));
149
						}
150
						if ($parentField->is('allow_goto')) {
151
							$options->appendChild(new XMLElement('a', __('Go to'), array(
152
								'class' => 'goto ignore-collapsible',
153
								'data-goto' => $entryId,
154
							)));
155
						}
156
						if ($parentField->is('allow_delete') ||
157
							$parentField->is('allow_link') || $parentField->is('allow_unlink') ||
158
							$parentField->is('allow_search')) {
159
							$options->appendChild(new XMLElement('a', __('Un-link'), array(
160
								'class' => 'unlink ignore-collapsible',
161
								'data-unlink' => $entryId,
162
							)));
163
						}
164
						$header->appendChild($options);
165
						$li->appendChild($header);
166
					}
167
					
168
					$content = ERFXSLTUTilities::processXSLT($parentField, $entry, $entrySectionHandle, $entryFields, 'mode', isset($_REQUEST['debug']));
169
					
170
					if ($content) {
171
						$li->appendChild(new XMLElement('div', $content, array(
172
							'class' => 'content',
173
							'data-collapsible-content' => '',
174
						)));
175
					}
176
					else {
177
						if ($parentField->is('show_header')) {
178
							$header->setAttribute('class', $header->getAttribute('class') . ' no-content');
0 ignored issues
show
Bug introduced by
The variable $header does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
179
						}
180
						else {
181
							$content = new XMLElement('div', null, array('class' => 'content'));
182
							$content->appendChildArray($this->buildDefaultTitle($entry, $entryVisibleFields, $entryFields));
183
							$li->appendChild($content);
184
						}
185
					}
186
					
187
					$this->_Result->appendChild($li);
188
				}
189
			}
190
		}
191
		
192
		public function getSectionName($entry, $name = 'name') {
193
			$sectionId = $entry->get('section_id');
194
			return $this->sectionManager->fetch($sectionId)->get($name);
195
		}
196
		
197
		public function getEntryTitle($entry, $entryVisibleFields, $entryFields) {
198
			$data = $entry->getData();
199
			$field = empty($entryVisibleFields) ? $entryFields : $entryVisibleFields;
200
			if (is_array($field)) {
201
				$field = current($field);
202
			}
203
			
204
			if ($field == null) {
205
				return __('None');
206
			}
207
			
208
			return $field->prepareReadableValue($data[$field->get('id')], $entry->get('id'), true);
209
		}
210
		
211
		public function buildDefaultTitle($entry, $entryVisibleFields, $entryFields) {
212
			return array(
213
				new XMLElement('strong', $this->getEntryTitle($entry, $entryVisibleFields, $entryFields), array('class' => 'ignore-collapsible')),
214
				new XMLElement('span', $this->getSectionName($entry), array('class' => 'ignore-collapsible'))
215
			);
216
		}
217
	}