| Total Complexity | 42 |
| Total Lines | 409 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like tx_dlf_collection 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 tx_dlf_collection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class tx_dlf_collection extends tx_dlf_plugin { |
||
| 21 | |||
| 22 | public $scriptRelPath = 'plugins/collection/class.tx_dlf_collection.php'; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * This holds the hook objects |
||
| 26 | * |
||
| 27 | * @var array |
||
| 28 | * @access protected |
||
| 29 | */ |
||
| 30 | protected $hookObjects = array (); |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The main method of the PlugIn |
||
| 34 | * |
||
| 35 | * @access public |
||
| 36 | * |
||
| 37 | * @param string $content: The PlugIn content |
||
| 38 | * @param array $conf: The PlugIn configuration |
||
| 39 | * |
||
| 40 | * @return string The content that is displayed on the website |
||
| 41 | */ |
||
| 42 | public function main($content, $conf) { |
||
| 87 | |||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Builds a collection list |
||
| 92 | * |
||
| 93 | * @access protected |
||
| 94 | * |
||
| 95 | * @return string The list of collections ready to output |
||
| 96 | */ |
||
| 97 | protected function showCollectionList() { |
||
| 98 | |||
| 99 | $additionalWhere = ''; |
||
| 100 | |||
| 101 | $orderBy = 'tx_dlf_collections.label'; |
||
| 102 | |||
| 103 | // Handle collections set by configuration. |
||
| 104 | if ($this->conf['collections']) { |
||
| 105 | |||
| 106 | if (count(explode(',', $this->conf['collections'])) == 1 && empty($this->conf['dont_show_single'])) { |
||
| 107 | |||
| 108 | $this->showSingleCollection(intval(trim($this->conf['collections'], ' ,'))); |
||
| 109 | |||
| 110 | } |
||
| 111 | |||
| 112 | $additionalWhere .= ' AND tx_dlf_collections.uid IN ('.$GLOBALS['TYPO3_DB']->cleanIntList($this->conf['collections']).')'; |
||
| 113 | |||
| 114 | $orderBy = 'FIELD(tx_dlf_collections.uid, '.$GLOBALS['TYPO3_DB']->cleanIntList($this->conf['collections']).')'; |
||
| 115 | |||
| 116 | } |
||
| 117 | |||
| 118 | // Should user-defined collections be shown? |
||
| 119 | if (empty($this->conf['show_userdefined'])) { |
||
| 120 | |||
| 121 | $additionalWhere .= ' AND tx_dlf_collections.fe_cruser_id=0'; |
||
| 122 | |||
| 123 | } elseif ($this->conf['show_userdefined'] > 0) { |
||
| 124 | |||
| 125 | if (!empty($GLOBALS['TSFE']->fe_user->user['uid'])) { |
||
| 126 | |||
| 127 | $additionalWhere .= ' AND tx_dlf_collections.fe_cruser_id='.intval($GLOBALS['TSFE']->fe_user->user['uid']); |
||
| 128 | |||
| 129 | } else { |
||
| 130 | |||
| 131 | $additionalWhere .= ' AND NOT tx_dlf_collections.fe_cruser_id=0'; |
||
| 132 | |||
| 133 | } |
||
| 134 | |||
| 135 | } |
||
| 136 | |||
| 137 | // Get collections. |
||
| 138 | $result = $GLOBALS['TYPO3_DB']->exec_SELECT_mm_query( |
||
| 139 | 'tx_dlf_collections.uid AS uid,tx_dlf_collections.label AS label,tx_dlf_collections.thumbnail AS thumbnail,tx_dlf_collections.description AS description,tx_dlf_collections.priority AS priority,COUNT(tx_dlf_documents.uid) AS titles', |
||
| 140 | 'tx_dlf_documents', |
||
| 141 | 'tx_dlf_relations', |
||
| 142 | 'tx_dlf_collections', |
||
| 143 | 'AND tx_dlf_collections.pid='.intval($this->conf['pages']).' AND tx_dlf_documents.partof=0 AND tx_dlf_relations.ident='.$GLOBALS['TYPO3_DB']->fullQuoteStr('docs_colls', 'tx_dlf_relations').$additionalWhere.tx_dlf_helper::whereClause('tx_dlf_documents').tx_dlf_helper::whereClause('tx_dlf_collections'), |
||
| 144 | 'tx_dlf_collections.uid', |
||
| 145 | $orderBy, |
||
| 146 | '' |
||
| 147 | ); |
||
| 148 | |||
| 149 | $count = $GLOBALS['TYPO3_DB']->sql_num_rows($result); |
||
| 150 | |||
| 151 | $content = ''; |
||
| 152 | |||
| 153 | if ($count == 1 && empty($this->conf['dont_show_single'])) { |
||
| 154 | |||
| 155 | $resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result); |
||
| 156 | |||
| 157 | $this->showSingleCollection(intval($resArray['uid'])); |
||
| 158 | |||
| 159 | } elseif ($count > 0) { |
||
| 160 | |||
| 161 | // Get number of volumes per collection. |
||
| 162 | $resultVolumes = $GLOBALS['TYPO3_DB']->exec_SELECT_mm_query( |
||
| 163 | 'tx_dlf_collections.uid AS uid,COUNT(tx_dlf_documents.uid) AS volumes', |
||
| 164 | 'tx_dlf_documents', |
||
| 165 | 'tx_dlf_relations', |
||
| 166 | 'tx_dlf_collections', |
||
| 167 | 'AND tx_dlf_collections.pid='.intval($this->conf['pages']).' AND NOT tx_dlf_documents.uid IN (SELECT DISTINCT tx_dlf_documents.partof FROM tx_dlf_documents WHERE NOT tx_dlf_documents.partof=0'.tx_dlf_helper::whereClause('tx_dlf_documents').') AND tx_dlf_relations.ident='.$GLOBALS['TYPO3_DB']->fullQuoteStr('docs_colls', 'tx_dlf_relations').$additionalWhere.tx_dlf_helper::whereClause('tx_dlf_documents').tx_dlf_helper::whereClause('tx_dlf_collections'), |
||
| 168 | 'tx_dlf_collections.uid', |
||
| 169 | '', |
||
| 170 | '' |
||
| 171 | ); |
||
| 172 | |||
| 173 | $volumes = array (); |
||
| 174 | |||
| 175 | while ($resArrayVolumes = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($resultVolumes)) { |
||
| 176 | |||
| 177 | $volumes[$resArrayVolumes['uid']] = $resArrayVolumes['volumes']; |
||
| 178 | |||
| 179 | } |
||
| 180 | |||
| 181 | // Process results. |
||
| 182 | while ($resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) { |
||
| 183 | |||
| 184 | // Generate random but unique array key taking priority into account. |
||
| 185 | do { |
||
| 186 | |||
| 187 | $_key = ($resArray['priority'] * 1000) + mt_rand(0, 1000); |
||
| 188 | |||
| 189 | } while (!empty($markerArray[$_key])); |
||
| 190 | |||
| 191 | // Merge plugin variables with new set of values. |
||
| 192 | $additionalParams = array ('collection' => $resArray['uid']); |
||
| 193 | |||
| 194 | if (is_array($this->piVars)) { |
||
| 195 | |||
| 196 | $piVars = $this->piVars; |
||
| 197 | |||
| 198 | unset($piVars['DATA']); |
||
| 199 | |||
| 200 | $additionalParams = tx_dlf_helper::array_merge_recursive_overrule($piVars, $additionalParams); |
||
| 201 | |||
| 202 | } |
||
| 203 | |||
| 204 | // Build typolink configuration array. |
||
| 205 | $conf = array ( |
||
| 206 | 'useCacheHash' => 1, |
||
| 207 | 'parameter' => $GLOBALS['TSFE']->id, |
||
| 208 | 'additionalParams' => \TYPO3\CMS\Core\Utility\GeneralUtility::implodeArrayForUrl($this->prefixId, $additionalParams, '', TRUE, FALSE) |
||
| 209 | ); |
||
| 210 | |||
| 211 | // Link collection's title to list view. |
||
| 212 | $markerArray[$_key]['###TITLE###'] = $this->cObj->typoLink(htmlspecialchars($resArray['label']), $conf); |
||
| 213 | |||
| 214 | // Add feed link if applicable. |
||
| 215 | if (!empty($this->conf['targetFeed'])) { |
||
| 216 | |||
| 217 | $img = '<img src="'.\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::siteRelPath($this->extKey).'res/icons/txdlffeeds.png" alt="'.$this->pi_getLL('feedAlt', '', TRUE).'" title="'.$this->pi_getLL('feedTitle', '', TRUE).'" />'; |
||
| 218 | |||
| 219 | $markerArray[$_key]['###FEED###'] = $this->pi_linkTP($img, array ($this->prefixId => array ('collection' => $resArray['uid'])), FALSE, $this->conf['targetFeed']); |
||
| 220 | |||
| 221 | } else { |
||
| 222 | |||
| 223 | $markerArray[$_key]['###FEED###'] = ''; |
||
| 224 | |||
| 225 | } |
||
| 226 | |||
| 227 | // Add thumbnail. |
||
| 228 | if (!empty($resArray['thumbnail'])) { |
||
| 229 | |||
| 230 | $markerArray[$_key]['###THUMBNAIL###'] = '<img alt="" title="'.htmlspecialchars($resArray['label']).'" src="'.$resArray['thumbnail'].'" />'; |
||
| 231 | |||
| 232 | } else { |
||
| 233 | |||
| 234 | $markerArray[$_key]['###THUMBNAIL###'] = ''; |
||
| 235 | |||
| 236 | } |
||
| 237 | |||
| 238 | // Add description. |
||
| 239 | $markerArray[$_key]['###DESCRIPTION###'] = $this->pi_RTEcssText($resArray['description']); |
||
| 240 | |||
| 241 | // Build statistic's output. |
||
| 242 | $labelTitles = $this->pi_getLL(($resArray['titles'] > 1 ? 'titles' : 'title'), '', FALSE); |
||
| 243 | |||
| 244 | $markerArray[$_key]['###COUNT_TITLES###'] = htmlspecialchars($resArray['titles'].$labelTitles); |
||
| 245 | |||
| 246 | $labelVolumes = $this->pi_getLL(($volumes[$resArray['uid']] > 1 ? 'volumes' : 'volume'), '', FALSE); |
||
| 247 | |||
| 248 | $markerArray[$_key]['###COUNT_VOLUMES###'] = htmlspecialchars($volumes[$resArray['uid']].$labelVolumes); |
||
| 249 | |||
| 250 | } |
||
| 251 | |||
| 252 | // Randomize sorting? |
||
| 253 | if (!empty($this->conf['randomize'])) { |
||
| 254 | |||
| 255 | ksort($markerArray, SORT_NUMERIC); |
||
| 256 | |||
| 257 | // Don't cache the output. |
||
| 258 | $this->setCache(FALSE); |
||
| 259 | |||
| 260 | } |
||
| 261 | |||
| 262 | $entry = $this->cObj->getSubpart($this->template, '###ENTRY###'); |
||
| 263 | |||
| 264 | foreach ($markerArray as $marker) { |
||
| 265 | |||
| 266 | $content .= $this->cObj->substituteMarkerArray($entry, $marker); |
||
| 267 | |||
| 268 | } |
||
| 269 | |||
| 270 | // Hook for getting custom collection hierarchies/subentries (requested by SBB). |
||
| 271 | foreach ($this->hookObjects as $hookObj) { |
||
| 272 | |||
| 273 | if (method_exists($hookObj, 'showCollectionList_getCustomCollectionList')) { |
||
| 274 | |||
| 275 | $hookObj->showCollectionList_getCustomCollectionList($this, $this->conf['templateFile'], $content, $markerArray); |
||
| 276 | |||
| 277 | } |
||
| 278 | |||
| 279 | } |
||
| 280 | |||
| 281 | return $this->cObj->substituteSubpart($this->template, '###ENTRY###', $content, TRUE); |
||
| 282 | |||
| 283 | } |
||
| 284 | |||
| 285 | return $content; |
||
| 286 | |||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Builds a collection's list |
||
| 291 | * |
||
| 292 | * @access protected |
||
| 293 | * |
||
| 294 | * @param integer $id: The collection's UID |
||
| 295 | * |
||
| 296 | * @return void |
||
| 297 | */ |
||
| 298 | protected function showSingleCollection($id) { |
||
| 429 | |||
| 430 | } |
||
| 433 |