We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 61 |
| Total Lines | 352 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like TableOfContents often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TableOfContents, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class TableOfContents extends \Kitodo\Dlf\Common\AbstractPlugin |
||
| 29 | { |
||
| 30 | public $scriptRelPath = 'Classes/Plugin/TableOfContents.php'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * This holds the active entries according to the currently selected page |
||
| 34 | * |
||
| 35 | * @var array |
||
| 36 | * @access protected |
||
| 37 | */ |
||
| 38 | protected $activeEntries = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * This builds an array for one menu entry |
||
| 42 | * |
||
| 43 | * @access protected |
||
| 44 | * |
||
| 45 | * @param array $entry: The entry's array from \Kitodo\Dlf\Common\Document->getLogicalStructure |
||
| 46 | * @param bool $recursive: Whether to include the child entries |
||
| 47 | * |
||
| 48 | * @return array HMENU array for menu entry |
||
| 49 | */ |
||
| 50 | protected function getMenuEntry(array $entry, $recursive = false) |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * The main method of the PlugIn |
||
| 125 | * |
||
| 126 | * @access public |
||
| 127 | * |
||
| 128 | * @param string $content: The PlugIn content |
||
| 129 | * @param array $conf: The PlugIn configuration |
||
| 130 | * |
||
| 131 | * @return string The content that is displayed on the website |
||
| 132 | */ |
||
| 133 | public function main($content, $conf) |
||
| 134 | { |
||
| 135 | $this->init($conf); |
||
| 136 | // Check for typoscript configuration to prevent fatal error. |
||
| 137 | if (empty($this->conf['menuConf.']) && empty($this->conf['previewConf.'])) { |
||
| 138 | $this->logger->warning('Incomplete plugin configuration'); |
||
| 139 | return $content; |
||
| 140 | } |
||
| 141 | // Load template file. |
||
| 142 | $this->getTemplate(); |
||
| 143 | $TSconfig = []; |
||
| 144 | $TSconfig['special'] = 'userfunction'; |
||
| 145 | if (empty($this->conf['previewConf.'])) { |
||
| 146 | $TSconfig['special.']['userFunc'] = TableOfContents::class . '->makeMenuArray'; |
||
| 147 | $TSconfig = Helper::mergeRecursiveWithOverrule($this->conf['menuConf.'], $TSconfig); |
||
| 148 | } else { |
||
| 149 | $TSconfig['special.']['userFunc'] = TableOfContents::class . '->makeMenuFor3DObjects'; |
||
| 150 | $TSconfig = Helper::mergeRecursiveWithOverrule($this->conf['previewConf.'], $TSconfig); |
||
| 151 | } |
||
| 152 | $TSconfig = Helper::mergeRecursiveWithOverrule($this->conf['menuConf.'], $TSconfig); |
||
| 153 | $markerArray['###TOCMENU###'] = $this->cObj->cObjGetSingle('HMENU', $TSconfig); |
||
| 154 | $content .= $this->templateService->substituteMarkerArray($this->template, $markerArray); |
||
| 155 | return $this->pi_wrapInBaseClass($content); |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * This builds a menu array for HMENU |
||
| 160 | * |
||
| 161 | * @access public |
||
| 162 | * |
||
| 163 | * @param string $content: The PlugIn content |
||
| 164 | * @param array $conf: The PlugIn configuration |
||
| 165 | * |
||
| 166 | * @return array HMENU array |
||
| 167 | */ |
||
| 168 | public function makeMenuArray($content, $conf) |
||
| 169 | { |
||
| 170 | $this->init($conf); |
||
| 171 | // Load current document. |
||
| 172 | $this->loadDocument(); |
||
| 173 | if ($this->doc === null) { |
||
| 174 | // Quit without doing anything if required variables are not set. |
||
| 175 | return []; |
||
| 176 | } else { |
||
| 177 | if (!empty($this->piVars['logicalPage'])) { |
||
| 178 | $this->piVars['page'] = $this->doc->getPhysicalPage($this->piVars['logicalPage']); |
||
|
|
|||
| 179 | // The logical page parameter should not appear again |
||
| 180 | unset($this->piVars['logicalPage']); |
||
| 181 | } |
||
| 182 | // Set default values for page if not set. |
||
| 183 | // $this->piVars['page'] may be integer or string (physical structure @ID) |
||
| 184 | if ( |
||
| 185 | (int) $this->piVars['page'] > 0 |
||
| 186 | || empty($this->piVars['page']) |
||
| 187 | ) { |
||
| 188 | $this->piVars['page'] = MathUtility::forceIntegerInRange((int) $this->piVars['page'], 1, $this->doc->numPages, 1); |
||
| 189 | } else { |
||
| 190 | $this->piVars['page'] = array_search($this->piVars['page'], $this->doc->physicalStructure); |
||
| 191 | } |
||
| 192 | $this->piVars['double'] = MathUtility::forceIntegerInRange($this->piVars['double'], 0, 1, 0); |
||
| 193 | } |
||
| 194 | $menuArray = []; |
||
| 195 | // Does the document have physical elements or is it an external file? |
||
| 196 | if ( |
||
| 197 | !empty($this->doc->physicalStructure) |
||
| 198 | || !MathUtility::canBeInterpretedAsInteger($this->doc->uid) |
||
| 199 | ) { |
||
| 200 | // Get all logical units the current page or track is a part of. |
||
| 201 | if ( |
||
| 202 | !empty($this->piVars['page']) |
||
| 203 | && !empty($this->doc->physicalStructure) |
||
| 204 | ) { |
||
| 205 | $this->activeEntries = array_merge((array) $this->doc->smLinks['p2l'][$this->doc->physicalStructure[0]], (array) $this->doc->smLinks['p2l'][$this->doc->physicalStructure[$this->piVars['page']]]); |
||
| 206 | if ( |
||
| 207 | !empty($this->piVars['double']) |
||
| 208 | && $this->piVars['page'] < $this->doc->numPages |
||
| 209 | ) { |
||
| 210 | $this->activeEntries = array_merge($this->activeEntries, (array) $this->doc->smLinks['p2l'][$this->doc->physicalStructure[$this->piVars['page'] + 1]]); |
||
| 211 | } |
||
| 212 | } |
||
| 213 | // Go through table of contents and create all menu entries. |
||
| 214 | foreach ($this->doc->tableOfContents as $entry) { |
||
| 215 | $menuArray[] = $this->getMenuEntry($entry, true); |
||
| 216 | } |
||
| 217 | } else { |
||
| 218 | // Go through table of contents and create top-level menu entries. |
||
| 219 | foreach ($this->doc->tableOfContents as $entry) { |
||
| 220 | $menuArray[] = $this->getMenuEntry($entry, false); |
||
| 221 | } |
||
| 222 | // Build table of contents from database. |
||
| 223 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
| 224 | ->getQueryBuilderForTable('tx_dlf_documents'); |
||
| 225 | |||
| 226 | $excludeOtherWhere = ''; |
||
| 227 | if ($this->conf['excludeOther']) { |
||
| 228 | $excludeOtherWhere = 'tx_dlf_documents.pid=' . intval($this->conf['pages']); |
||
| 229 | } |
||
| 230 | // Check if there are any metadata to suggest. |
||
| 231 | $result = $queryBuilder |
||
| 232 | ->select( |
||
| 233 | 'tx_dlf_documents.uid AS uid', |
||
| 234 | 'tx_dlf_documents.title AS title', |
||
| 235 | 'tx_dlf_documents.volume AS volume', |
||
| 236 | 'tx_dlf_documents.mets_label AS mets_label', |
||
| 237 | 'tx_dlf_documents.mets_orderlabel AS mets_orderlabel', |
||
| 238 | 'tx_dlf_structures_join.index_name AS type' |
||
| 239 | ) |
||
| 240 | ->innerJoin( |
||
| 241 | 'tx_dlf_documents', |
||
| 242 | 'tx_dlf_structures', |
||
| 243 | 'tx_dlf_structures_join', |
||
| 244 | $queryBuilder->expr()->eq( |
||
| 245 | 'tx_dlf_structures_join.uid', |
||
| 246 | 'tx_dlf_documents.structure' |
||
| 247 | ) |
||
| 248 | ) |
||
| 249 | ->from('tx_dlf_documents') |
||
| 250 | ->where( |
||
| 251 | $queryBuilder->expr()->eq('tx_dlf_documents.partof', intval($this->doc->uid)), |
||
| 252 | $queryBuilder->expr()->eq('tx_dlf_structures_join.pid', intval($this->doc->pid)), |
||
| 253 | $excludeOtherWhere |
||
| 254 | ) |
||
| 255 | ->addOrderBy('tx_dlf_documents.volume_sorting') |
||
| 256 | ->addOrderBy('tx_dlf_documents.mets_orderlabel') |
||
| 257 | ->execute(); |
||
| 258 | |||
| 259 | $allResults = $result->fetchAll(); |
||
| 260 | |||
| 261 | if (count($allResults) > 0) { |
||
| 262 | $menuArray[0]['ITEM_STATE'] = 'CURIFSUB'; |
||
| 263 | $menuArray[0]['_SUB_MENU'] = []; |
||
| 264 | foreach ($allResults as $resArray) { |
||
| 265 | $entry = [ |
||
| 266 | 'label' => !empty($resArray['mets_label']) ? $resArray['mets_label'] : $resArray['title'], |
||
| 267 | 'type' => $resArray['type'], |
||
| 268 | 'volume' => $resArray['volume'], |
||
| 269 | 'orderlabel' => $resArray['mets_orderlabel'], |
||
| 270 | 'pagination' => '', |
||
| 271 | 'targetUid' => $resArray['uid'] |
||
| 272 | ]; |
||
| 273 | $menuArray[0]['_SUB_MENU'][] = $this->getMenuEntry($entry, false); |
||
| 274 | } |
||
| 275 | } |
||
| 276 | } |
||
| 277 | return $menuArray; |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * This builds a menu for list of 3D objects |
||
| 282 | * |
||
| 283 | * @access public |
||
| 284 | * |
||
| 285 | * @param string $content: The PlugIn content |
||
| 286 | * @param array $conf: The PlugIn configuration |
||
| 287 | * |
||
| 288 | * @return array HMENU array |
||
| 289 | */ |
||
| 290 | public function makeMenuFor3DObjects($content, $conf) |
||
| 325 | } |
||
| 326 | |||
| 327 | protected function getMenuEntryWithImage(array $entry, $recursive = false) |
||
| 382 |