Completed
Push — master ( 2867c5...18d669 )
by Nicolas
08:20 queued 04:11
created

getEntryTitle()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 8
nop 3
1
<?php
2
	/**
3
	 * Copyright: Deux Huit Huit 2014
4
	 * LICENCE: MIT https://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
			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
			if (!$parentField->get('elements')) {
81
				$parentField->set('elements', '*');
82
			}
83
			if (!$parentField->get('sections') && $parentField->get('linked_section_id')) {
84
				$parentField->set('sections', $parentField->get('linked_section_id'));
85
			}
86
			
87
			// Get entries one by one since they may belong to
88
			// different sections, which prevents us from
89
			// passing an array of entryId.
90
			foreach ($entriesId as $key => $entryId) {
91
				$entry = $this->entryManager->fetch($entryId);
92
				if (empty($entry)) {
93
					$li = new XMLElement('li', null, array(
94
						'data-entry-id' => $entryId
95
					));
96
					$header = new XMLElement('header', null, array('class' => 'frame-header no-content ignore-collapsible'));
97
					$title = new XMLElement('h4');
98
					$title->appendChild(new XMLElement('strong', __('Entry %s not found', array($entryId))));
99
					$header->appendChild($title);
100
					$options = new XMLElement('div', null, array('class' => 'destructor'));
101
					$options->appendChild(new XMLElement('a', __('Un-link'), array(
102
						'class' => 'unlink ignore-collapsible',
103
						'data-unlink' => $entryId,
104
					)));
105
					$header->appendChild($options);
106
					$li->appendChild($header);
107
					$this->_Result->appendChild($li);
108
				} else {
109
					$entry = $entry[0];
110
					$entrySection = $this->sectionManager->fetch($entry->get('section_id'));
111
					$entryVisibleFields = $entrySection->fetchVisibleColumns();
112
					$entryFields = $entrySection->fetchFields();
113
					$entrySectionHandle = $this->getSectionName($entry, 'handle');
114
					
115
					$li = new XMLElement('li', null, array(
116
						'data-entry-id' => $entryId,
117
						'data-section' => $entrySectionHandle,
118
						'data-section-id' => $entrySection->get('id'),
119
						'data-timestamp' => $entry->get('modification_date'),
120
					));
121
					if ($parentField->is('show_header')) {
122
						$header = new XMLElement('header', null, array(
123
							'class' => 'frame-header',
124
							'data-orderable-handle' => '',
125
							'data-collapsible-handle' => ''
126
						));
127
						$title = new XMLElement('h4', null, array('class' => 'ignore-collapsible'));
128
						if (!$parentField->get('mode_header')) {
129
							$title->appendChildArray($this->buildDefaultTitle($entry, $entryVisibleFields, $entryFields));
130
						}
131
						else {
132
							$title->setValue(ERFXSLTUTilities::processXSLT($parentField, $entry, $entrySectionHandle, $entryFields, 'mode_header'));
133
						}
134
						$header->appendChild($title);
135
						
136
						$options = new XMLElement('div', null, array('class' => 'destructor'));
137
						if ($parentField->is('allow_edit')) {
138
							$title->setAttribute('data-edit', $entryId);
139
							$options->appendChild(new XMLElement('a', __('Edit'), array(
140
								'class' => 'edit ignore-collapsible',
141
								'data-edit' => $entryId,
142
							)));
143
						}
144
						if ($parentField->is('allow_delete')) {
145
							$options->appendChild(new XMLElement('a', __('Delete'), array(
146
								'class' => 'delete ignore-collapsible',
147
								'data-delete' => $entryId,
148
							)));
149
						}
150
						if ($parentField->is('allow_link')) {
151
							$options->appendChild(new XMLElement('a', __('Replace'), array(
152
								'class' => 'unlink ignore-collapsible',
153
								'data-replace' => $entryId,
154
							)));
155
						}
156
						if ($parentField->is('allow_goto')) {
157
							$options->appendChild(new XMLElement('a', __('Go to'), array(
158
								'class' => 'goto ignore-collapsible',
159
								'data-goto' => $entryId,
160
							)));
161
						}
162
						if ($parentField->is('allow_delete') ||
163
							$parentField->is('allow_link') || $parentField->is('allow_unlink') ||
164
							$parentField->is('allow_search')) {
165
							$options->appendChild(new XMLElement('a', __('Un-link'), array(
166
								'class' => 'unlink ignore-collapsible',
167
								'data-unlink' => $entryId,
168
							)));
169
						}
170
						$header->appendChild($options);
171
						$li->appendChild($header);
172
					}
173
					
174
					$content = ERFXSLTUTilities::processXSLT($parentField, $entry, $entrySectionHandle, $entryFields, 'mode', isset($_REQUEST['debug']));
175
					
176
					if ($content) {
177
						$li->appendChild(new XMLElement('div', $content, array(
178
							'class' => 'content',
179
							'data-collapsible-content' => '',
180
						)));
181
					}
182
					else {
183
						if ($parentField->is('show_header')) {
184
							$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...
185
						}
186
						else {
187
							$content = new XMLElement('div', null, array('class' => 'content'));
188
							$content->appendChildArray($this->buildDefaultTitle($entry, $entryVisibleFields, $entryFields));
189
							$li->appendChild($content);
190
						}
191
					}
192
					
193
					$this->_Result->appendChild($li);
194
				}
195
			}
196
		}
197
		
198
		public function getSectionName($entry, $name = 'name') {
199
			$sectionId = $entry->get('section_id');
200
			return $this->sectionManager->fetch($sectionId)->get($name);
201
		}
202
		
203
		public function getEntryTitle($entry, $entryVisibleFields, $entryFields) {
204
			$data = $entry->getData();
205
			$field = empty($entryVisibleFields) ? $entryFields : $entryVisibleFields;
206
			if (is_array($field)) {
207
				$field = current($field);
208
			}
209
			
210
			if ($field == null) {
211
				return __('None');
212
			}
213
			
214
			return $field->prepareReadableValue($data[$field->get('id')], $entry->get('id'), true);
215
		}
216
		
217
		public function buildDefaultTitle($entry, $entryVisibleFields, $entryFields) {
218
			return array(
219
				new XMLElement('strong', $this->getEntryTitle($entry, $entryVisibleFields, $entryFields), array('class' => 'ignore-collapsible')),
220
				new XMLElement('span', $this->getSectionName($entry), array('class' => 'ignore-collapsible'))
221
			);
222
		}
223
	}