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 |
||
| 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() { |
||
| 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) {} |
||
|
|
|||
| 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'); |
||
| 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') { |
||
| 202 | |||
| 203 | public function getEntryTitle($entry, $entryVisibleFields, $entryFields) { |
||
| 216 | |||
| 217 | public function buildDefaultTitle($entry, $entryVisibleFields, $entryFields) { |
||
| 223 | } |