| Total Complexity | 261 |
| Total Lines | 1653 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like RelationHandler 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 RelationHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class RelationHandler |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * $fetchAllFields if false getFromDB() fetches only uid, pid, thumbnail and label fields (as defined in TCA) |
||
| 39 | * |
||
| 40 | * @var bool |
||
| 41 | */ |
||
| 42 | protected $fetchAllFields = false; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * If set, values that are not ids in tables are normally discarded. By this options they will be preserved. |
||
| 46 | * |
||
| 47 | * @var bool |
||
| 48 | */ |
||
| 49 | public $registerNonTableValues = false; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Contains the table names as keys. The values are the id-values for each table. |
||
| 53 | * Should ONLY contain proper table names. |
||
| 54 | * |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | public $tableArray = []; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Contains items in a numeric array (table/id for each). Tablenames here might be "_NO_TABLE" |
||
| 61 | * |
||
| 62 | * @var array<int, array<string, mixed>> |
||
| 63 | */ |
||
| 64 | public $itemArray = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Array for NON-table elements |
||
| 68 | * |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | public $nonTableArray = []; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | public $additionalWhere = []; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Deleted-column is added to additionalWhere... if this is set... |
||
| 80 | * |
||
| 81 | * @var bool |
||
| 82 | */ |
||
| 83 | public $checkIfDeleted = true; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Will contain the first table name in the $tablelist (for positive ids) |
||
| 87 | * |
||
| 88 | * @var string |
||
| 89 | */ |
||
| 90 | protected $firstTable = ''; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * If TRUE, uid_local and uid_foreign are switched, and the current table |
||
| 94 | * is inserted as tablename - this means you display a foreign relation "from the opposite side" |
||
| 95 | * |
||
| 96 | * @var bool |
||
| 97 | */ |
||
| 98 | protected $MM_is_foreign = false; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Is empty by default; if MM_is_foreign is set and there is more than one table |
||
| 102 | * allowed (on the "local" side), then it contains the first table (as a fallback) |
||
| 103 | * @var string |
||
| 104 | */ |
||
| 105 | protected $MM_isMultiTableRelationship = ''; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Current table => Only needed for reverse relations |
||
| 109 | * |
||
| 110 | * @var string |
||
| 111 | */ |
||
| 112 | protected $currentTable; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * If a record should be undeleted |
||
| 116 | * (so do not use the $useDeleteClause on \TYPO3\CMS\Backend\Utility\BackendUtility) |
||
| 117 | * |
||
| 118 | * @var bool |
||
| 119 | */ |
||
| 120 | public $undeleteRecord; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Array of fields value pairs that should match while SELECT |
||
| 124 | * and will be written into MM table if $MM_insert_fields is not set |
||
| 125 | * |
||
| 126 | * @var array |
||
| 127 | */ |
||
| 128 | protected $MM_match_fields = []; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * This is set to TRUE if the MM table has a UID field. |
||
| 132 | * |
||
| 133 | * @var bool |
||
| 134 | */ |
||
| 135 | protected $MM_hasUidField; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Array of fields and value pairs used for insert in MM table |
||
| 139 | * |
||
| 140 | * @var array |
||
| 141 | */ |
||
| 142 | protected $MM_insert_fields = []; |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Extra MM table where |
||
| 146 | * |
||
| 147 | * @var string |
||
| 148 | */ |
||
| 149 | protected $MM_table_where = ''; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Usage of a MM field on the opposite relation. |
||
| 153 | * |
||
| 154 | * @var array |
||
| 155 | */ |
||
| 156 | protected $MM_oppositeUsage; |
||
| 157 | |||
| 158 | /** |
||
| 159 | * If false, reference index is not updated. |
||
| 160 | * |
||
| 161 | * @var bool |
||
| 162 | * @deprecated since v11, will be removed in v12 |
||
| 163 | */ |
||
| 164 | protected $updateReferenceIndex = true; |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @var ReferenceIndexUpdater|null |
||
| 168 | */ |
||
| 169 | protected $referenceIndexUpdater; |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @var bool |
||
| 173 | */ |
||
| 174 | protected $useLiveParentIds = true; |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @var bool |
||
| 178 | */ |
||
| 179 | protected $useLiveReferenceIds = true; |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @var int|null |
||
| 183 | */ |
||
| 184 | protected $workspaceId; |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @var bool |
||
| 188 | */ |
||
| 189 | protected $purged = false; |
||
| 190 | |||
| 191 | /** |
||
| 192 | * This array will be filled by getFromDB(). |
||
| 193 | * |
||
| 194 | * @var array |
||
| 195 | */ |
||
| 196 | public $results = []; |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Gets the current workspace id. |
||
| 200 | * |
||
| 201 | * @return int |
||
| 202 | */ |
||
| 203 | protected function getWorkspaceId(): int |
||
| 204 | { |
||
| 205 | $backendUser = $GLOBALS['BE_USER'] ?? null; |
||
| 206 | if (!isset($this->workspaceId)) { |
||
| 207 | $this->workspaceId = $backendUser instanceof BackendUserAuthentication ? (int)($backendUser->workspace) : 0; |
||
| 208 | } |
||
| 209 | return $this->workspaceId; |
||
|
|
|||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Sets the current workspace id. |
||
| 214 | * |
||
| 215 | * @param int $workspaceId |
||
| 216 | */ |
||
| 217 | public function setWorkspaceId($workspaceId): void |
||
| 218 | { |
||
| 219 | $this->workspaceId = (int)$workspaceId; |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Setter to carry the 'deferred' reference index updater registry around. |
||
| 224 | * |
||
| 225 | * @param ReferenceIndexUpdater $updater |
||
| 226 | * @internal Used internally within DataHandler only |
||
| 227 | */ |
||
| 228 | public function setReferenceIndexUpdater(ReferenceIndexUpdater $updater): void |
||
| 229 | { |
||
| 230 | $this->referenceIndexUpdater = $updater; |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Whether item array has been purged in this instance. |
||
| 235 | * |
||
| 236 | * @return bool |
||
| 237 | */ |
||
| 238 | public function isPurged() |
||
| 239 | { |
||
| 240 | return $this->purged; |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Initialization of the class. |
||
| 245 | * |
||
| 246 | * @param string $itemlist List of group/select items |
||
| 247 | * @param string $tablelist Comma list of tables, first table takes priority if no table is set for an entry in the list. |
||
| 248 | * @param string $MMtable Name of a MM table. |
||
| 249 | * @param int|string $MMuid Local UID for MM lookup. May be a string for newly created elements. |
||
| 250 | * @param string $currentTable Current table name |
||
| 251 | * @param array $conf TCA configuration for current field |
||
| 252 | */ |
||
| 253 | public function start($itemlist, $tablelist, $MMtable = '', $MMuid = 0, $currentTable = '', $conf = []) |
||
| 254 | { |
||
| 255 | $conf = (array)$conf; |
||
| 256 | // SECTION: MM reverse relations |
||
| 257 | $this->MM_is_foreign = (bool)($conf['MM_opposite_field'] ?? false); |
||
| 258 | $this->MM_table_where = $conf['MM_table_where'] ?? null; |
||
| 259 | $this->MM_hasUidField = $conf['MM_hasUidField'] ?? null; |
||
| 260 | $this->MM_match_fields = (isset($conf['MM_match_fields']) && is_array($conf['MM_match_fields'])) ? $conf['MM_match_fields'] : []; |
||
| 261 | $this->MM_insert_fields = (isset($conf['MM_insert_fields']) && is_array($conf['MM_insert_fields'])) ? $conf['MM_insert_fields'] : $this->MM_match_fields; |
||
| 262 | $this->currentTable = $currentTable; |
||
| 263 | if (!empty($conf['MM_oppositeUsage']) && is_array($conf['MM_oppositeUsage'])) { |
||
| 264 | $this->MM_oppositeUsage = $conf['MM_oppositeUsage']; |
||
| 265 | } |
||
| 266 | $mmOppositeTable = ''; |
||
| 267 | if ($this->MM_is_foreign) { |
||
| 268 | $allowedTableList = $conf['type'] === 'group' ? $conf['allowed'] : $conf['foreign_table']; |
||
| 269 | // Normally, $conf['allowed'] can contain a list of tables, |
||
| 270 | // but as we are looking at a MM relation from the foreign side, |
||
| 271 | // it only makes sense to allow one table in $conf['allowed']. |
||
| 272 | [$mmOppositeTable] = GeneralUtility::trimExplode(',', $allowedTableList); |
||
| 273 | // Only add the current table name if there is more than one allowed |
||
| 274 | // field. We must be sure this has been done at least once before accessing |
||
| 275 | // the "columns" part of TCA for a table. |
||
| 276 | $mmOppositeAllowed = (string)($GLOBALS['TCA'][$mmOppositeTable]['columns'][$conf['MM_opposite_field'] ?? '']['config']['allowed'] ?? ''); |
||
| 277 | if ($mmOppositeAllowed !== '') { |
||
| 278 | $mmOppositeAllowedTables = explode(',', $mmOppositeAllowed); |
||
| 279 | if ($mmOppositeAllowed === '*' || count($mmOppositeAllowedTables) > 1) { |
||
| 280 | $this->MM_isMultiTableRelationship = $mmOppositeAllowedTables[0]; |
||
| 281 | } |
||
| 282 | } |
||
| 283 | } |
||
| 284 | // SECTION: normal MM relations |
||
| 285 | // If the table list is "*" then all tables are used in the list: |
||
| 286 | if (trim($tablelist) === '*') { |
||
| 287 | $tablelist = implode(',', array_keys($GLOBALS['TCA'])); |
||
| 288 | } |
||
| 289 | // The tables are traversed and internal arrays are initialized: |
||
| 290 | $tempTableArray = GeneralUtility::trimExplode(',', $tablelist, true); |
||
| 291 | foreach ($tempTableArray as $val) { |
||
| 292 | $tName = trim($val); |
||
| 293 | $this->tableArray[$tName] = []; |
||
| 294 | $deleteField = $GLOBALS['TCA'][$tName]['ctrl']['delete'] ?? false; |
||
| 295 | if ($this->checkIfDeleted && $deleteField) { |
||
| 296 | $fieldN = $tName . '.' . $deleteField; |
||
| 297 | if (!isset($this->additionalWhere[$tName])) { |
||
| 298 | $this->additionalWhere[$tName] = ''; |
||
| 299 | } |
||
| 300 | $this->additionalWhere[$tName] .= ' AND ' . $fieldN . '=0'; |
||
| 301 | } |
||
| 302 | } |
||
| 303 | if (is_array($this->tableArray)) { |
||
| 304 | reset($this->tableArray); |
||
| 305 | } else { |
||
| 306 | // No tables |
||
| 307 | return; |
||
| 308 | } |
||
| 309 | // Set first and second tables: |
||
| 310 | // Is the first table |
||
| 311 | $this->firstTable = (string)key($this->tableArray); |
||
| 312 | next($this->tableArray); |
||
| 313 | // Now, populate the internal itemArray and tableArray arrays: |
||
| 314 | // If MM, then call this function to do that: |
||
| 315 | if ($MMtable) { |
||
| 316 | if ($MMuid) { |
||
| 317 | $this->readMM($MMtable, $MMuid, $mmOppositeTable); |
||
| 318 | $this->purgeItemArray(); |
||
| 319 | } else { |
||
| 320 | // Revert to readList() for new records in order to load possible default values from $itemlist |
||
| 321 | $this->readList($itemlist, $conf); |
||
| 322 | $this->purgeItemArray(); |
||
| 323 | } |
||
| 324 | } elseif ($MMuid && ($conf['foreign_field'] ?? false)) { |
||
| 325 | // If not MM but foreign_field, the read the records by the foreign_field |
||
| 326 | $this->readForeignField($MMuid, $conf); |
||
| 327 | } else { |
||
| 328 | // If not MM, then explode the itemlist by "," and traverse the list: |
||
| 329 | $this->readList($itemlist, $conf); |
||
| 330 | // Do automatic default_sortby, if any |
||
| 331 | if (isset($conf['foreign_default_sortby']) && $conf['foreign_default_sortby']) { |
||
| 332 | $this->sortList($conf['foreign_default_sortby']); |
||
| 333 | } |
||
| 334 | } |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Sets $fetchAllFields |
||
| 339 | * |
||
| 340 | * @param bool $allFields enables fetching of all fields in getFromDB() |
||
| 341 | */ |
||
| 342 | public function setFetchAllFields($allFields) |
||
| 343 | { |
||
| 344 | $this->fetchAllFields = (bool)$allFields; |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Sets whether the reference index shall be updated. |
||
| 349 | * |
||
| 350 | * @param bool $updateReferenceIndex Whether the reference index shall be updated |
||
| 351 | * @deprecated since v11, will be removed in v12 |
||
| 352 | */ |
||
| 353 | public function setUpdateReferenceIndex($updateReferenceIndex) |
||
| 354 | { |
||
| 355 | trigger_error( |
||
| 356 | 'Calling RelationHandler->setUpdateReferenceIndex() is deprecated. Use setReferenceIndexUpdater() instead.', |
||
| 357 | E_USER_DEPRECATED |
||
| 358 | ); |
||
| 359 | $this->updateReferenceIndex = (bool)$updateReferenceIndex; |
||
| 360 | } |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @param bool $useLiveParentIds |
||
| 364 | */ |
||
| 365 | public function setUseLiveParentIds($useLiveParentIds) |
||
| 366 | { |
||
| 367 | $this->useLiveParentIds = (bool)$useLiveParentIds; |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @param bool $useLiveReferenceIds |
||
| 372 | */ |
||
| 373 | public function setUseLiveReferenceIds($useLiveReferenceIds) |
||
| 374 | { |
||
| 375 | $this->useLiveReferenceIds = (bool)$useLiveReferenceIds; |
||
| 376 | } |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Explodes the item list and stores the parts in the internal arrays itemArray and tableArray from MM records. |
||
| 380 | * |
||
| 381 | * @param string $itemlist Item list |
||
| 382 | * @param array $configuration Parent field configuration |
||
| 383 | */ |
||
| 384 | protected function readList($itemlist, array $configuration) |
||
| 385 | { |
||
| 386 | if ((string)trim($itemlist) != '') { |
||
| 387 | // Changed to trimExplode 31/3 04; HMENU special type "list" didn't work |
||
| 388 | // if there were spaces in the list... I suppose this is better overall... |
||
| 389 | $tempItemArray = GeneralUtility::trimExplode(',', $itemlist); |
||
| 390 | // If the second table is set and the ID number is less than zero (later) |
||
| 391 | // then the record is regarded to come from the second table... |
||
| 392 | $secondTable = (string)(key($this->tableArray) ?? ''); |
||
| 393 | foreach ($tempItemArray as $key => $val) { |
||
| 394 | // Will be set to "true" if the entry was a real table/id |
||
| 395 | $isSet = false; |
||
| 396 | // Extract table name and id. This is in the formula [tablename]_[id] |
||
| 397 | // where table name MIGHT contain "_", hence the reversion of the string! |
||
| 398 | $val = strrev($val); |
||
| 399 | $parts = explode('_', $val, 2); |
||
| 400 | $theID = strrev($parts[0]); |
||
| 401 | // Check that the id IS an integer: |
||
| 402 | if (MathUtility::canBeInterpretedAsInteger($theID)) { |
||
| 403 | // Get the table name: If a part of the exploded string, use that. |
||
| 404 | // Otherwise if the id number is LESS than zero, use the second table, otherwise the first table |
||
| 405 | $theTable = trim($parts[1] ?? '') |
||
| 406 | ? strrev(trim($parts[1] ?? '')) |
||
| 407 | : ($secondTable && $theID < 0 ? $secondTable : $this->firstTable); |
||
| 408 | // If the ID is not blank and the table name is among the names in the inputted tableList |
||
| 409 | if ((string)$theID != '' && $theID && $theTable && isset($this->tableArray[$theTable])) { |
||
| 410 | // Get ID as the right value: |
||
| 411 | $theID = $secondTable ? abs((int)$theID) : (int)$theID; |
||
| 412 | // Register ID/table name in internal arrays: |
||
| 413 | $this->itemArray[$key]['id'] = $theID; |
||
| 414 | $this->itemArray[$key]['table'] = $theTable; |
||
| 415 | $this->tableArray[$theTable][] = $theID; |
||
| 416 | // Set update-flag |
||
| 417 | $isSet = true; |
||
| 418 | } |
||
| 419 | } |
||
| 420 | // If it turns out that the value from the list was NOT a valid reference to a table-record, |
||
| 421 | // then we might still set it as a NO_TABLE value: |
||
| 422 | if (!$isSet && $this->registerNonTableValues) { |
||
| 423 | $this->itemArray[$key]['id'] = $tempItemArray[$key]; |
||
| 424 | $this->itemArray[$key]['table'] = '_NO_TABLE'; |
||
| 425 | $this->nonTableArray[] = $tempItemArray[$key]; |
||
| 426 | } |
||
| 427 | } |
||
| 428 | |||
| 429 | // Skip if not dealing with IRRE in a CSV list on a workspace |
||
| 430 | if (!isset($configuration['type']) || $configuration['type'] !== 'inline' |
||
| 431 | || empty($configuration['foreign_table']) || !empty($configuration['foreign_field']) |
||
| 432 | || !empty($configuration['MM']) || count($this->tableArray) !== 1 || empty($this->tableArray[$configuration['foreign_table']]) |
||
| 433 | || $this->getWorkspaceId() === 0 || !BackendUtility::isTableWorkspaceEnabled($configuration['foreign_table']) |
||
| 434 | ) { |
||
| 435 | return; |
||
| 436 | } |
||
| 437 | |||
| 438 | // Fetch live record data |
||
| 439 | if ($this->useLiveReferenceIds) { |
||
| 440 | foreach ($this->itemArray as &$item) { |
||
| 441 | $item['id'] = $this->getLiveDefaultId($item['table'], $item['id']); |
||
| 442 | } |
||
| 443 | } else { |
||
| 444 | // Directly overlay workspace data |
||
| 445 | $this->itemArray = []; |
||
| 446 | $foreignTable = $configuration['foreign_table']; |
||
| 447 | $ids = $this->getResolver($foreignTable, $this->tableArray[$foreignTable])->get(); |
||
| 448 | foreach ($ids as $id) { |
||
| 449 | $this->itemArray[] = [ |
||
| 450 | 'id' => $id, |
||
| 451 | 'table' => $foreignTable, |
||
| 452 | ]; |
||
| 453 | } |
||
| 454 | } |
||
| 455 | } |
||
| 456 | } |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Does a sorting on $this->itemArray depending on a default sortby field. |
||
| 460 | * This is only used for automatic sorting of comma separated lists. |
||
| 461 | * This function is only relevant for data that is stored in comma separated lists! |
||
| 462 | * |
||
| 463 | * @param string $sortby The default_sortby field/command (e.g. 'price DESC') |
||
| 464 | */ |
||
| 465 | protected function sortList($sortby) |
||
| 466 | { |
||
| 467 | // Sort directly without fetching additional data |
||
| 468 | if ($sortby === 'uid') { |
||
| 469 | usort( |
||
| 470 | $this->itemArray, |
||
| 471 | function ($a, $b) { |
||
| 472 | return $a['id'] < $b['id'] ? -1 : 1; |
||
| 473 | } |
||
| 474 | ); |
||
| 475 | } elseif (count($this->tableArray) === 1) { |
||
| 476 | reset($this->tableArray); |
||
| 477 | $table = (string)key($this->tableArray); |
||
| 478 | $connection = $this->getConnectionForTableName($table); |
||
| 479 | $maxBindParameters = PlatformInformation::getMaxBindParameters($connection->getDatabasePlatform()); |
||
| 480 | |||
| 481 | foreach (array_chunk(current($this->tableArray), $maxBindParameters - 10, true) as $chunk) { |
||
| 482 | if (empty($chunk)) { |
||
| 483 | continue; |
||
| 484 | } |
||
| 485 | $this->itemArray = []; |
||
| 486 | $this->tableArray = []; |
||
| 487 | $queryBuilder = $connection->createQueryBuilder(); |
||
| 488 | $queryBuilder->getRestrictions()->removeAll(); |
||
| 489 | $queryBuilder->select('uid') |
||
| 490 | ->from($table) |
||
| 491 | ->where( |
||
| 492 | $queryBuilder->expr()->in( |
||
| 493 | 'uid', |
||
| 494 | $queryBuilder->createNamedParameter($chunk, Connection::PARAM_INT_ARRAY) |
||
| 495 | ) |
||
| 496 | ); |
||
| 497 | foreach (QueryHelper::parseOrderBy((string)$sortby) as $orderPair) { |
||
| 498 | [$fieldName, $order] = $orderPair; |
||
| 499 | $queryBuilder->addOrderBy($fieldName, $order); |
||
| 500 | } |
||
| 501 | $statement = $queryBuilder->execute(); |
||
| 502 | while ($row = $statement->fetch()) { |
||
| 503 | $this->itemArray[] = ['id' => $row['uid'], 'table' => $table]; |
||
| 504 | $this->tableArray[$table][] = $row['uid']; |
||
| 505 | } |
||
| 506 | } |
||
| 507 | } |
||
| 508 | } |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Reads the record tablename/id into the internal arrays itemArray and tableArray from MM records. |
||
| 512 | * |
||
| 513 | * @param string $tableName MM Tablename |
||
| 514 | * @param int|string $uid Local UID |
||
| 515 | * @param string $mmOppositeTable Opposite table name |
||
| 516 | */ |
||
| 517 | protected function readMM($tableName, $uid, $mmOppositeTable) |
||
| 518 | { |
||
| 519 | $key = 0; |
||
| 520 | $theTable = null; |
||
| 521 | $queryBuilder = $this->getConnectionForTableName($tableName) |
||
| 522 | ->createQueryBuilder(); |
||
| 523 | $queryBuilder->getRestrictions()->removeAll(); |
||
| 524 | $queryBuilder->select('*')->from($tableName); |
||
| 525 | // In case of a reverse relation |
||
| 526 | if ($this->MM_is_foreign) { |
||
| 527 | $uidLocal_field = 'uid_foreign'; |
||
| 528 | $uidForeign_field = 'uid_local'; |
||
| 529 | $sorting_field = 'sorting_foreign'; |
||
| 530 | if ($this->MM_isMultiTableRelationship) { |
||
| 531 | // Be backwards compatible! When allowing more than one table after |
||
| 532 | // having previously allowed only one table, this case applies. |
||
| 533 | if ($this->currentTable == $this->MM_isMultiTableRelationship) { |
||
| 534 | $expression = $queryBuilder->expr()->orX( |
||
| 535 | $queryBuilder->expr()->eq( |
||
| 536 | 'tablenames', |
||
| 537 | $queryBuilder->createNamedParameter($this->currentTable, \PDO::PARAM_STR) |
||
| 538 | ), |
||
| 539 | $queryBuilder->expr()->eq( |
||
| 540 | 'tablenames', |
||
| 541 | $queryBuilder->createNamedParameter('', \PDO::PARAM_STR) |
||
| 542 | ) |
||
| 543 | ); |
||
| 544 | } else { |
||
| 545 | $expression = $queryBuilder->expr()->eq( |
||
| 546 | 'tablenames', |
||
| 547 | $queryBuilder->createNamedParameter($this->currentTable, \PDO::PARAM_STR) |
||
| 548 | ); |
||
| 549 | } |
||
| 550 | $queryBuilder->andWhere($expression); |
||
| 551 | } |
||
| 552 | $theTable = $mmOppositeTable; |
||
| 553 | } else { |
||
| 554 | // Default |
||
| 555 | $uidLocal_field = 'uid_local'; |
||
| 556 | $uidForeign_field = 'uid_foreign'; |
||
| 557 | $sorting_field = 'sorting'; |
||
| 558 | } |
||
| 559 | if ($this->MM_table_where) { |
||
| 560 | $queryBuilder->andWhere( |
||
| 561 | QueryHelper::stripLogicalOperatorPrefix(str_replace('###THIS_UID###', (string)$uid, $this->MM_table_where)) |
||
| 562 | ); |
||
| 563 | } |
||
| 564 | foreach ($this->MM_match_fields as $field => $value) { |
||
| 565 | $queryBuilder->andWhere( |
||
| 566 | $queryBuilder->expr()->eq($field, $queryBuilder->createNamedParameter($value, \PDO::PARAM_STR)) |
||
| 567 | ); |
||
| 568 | } |
||
| 569 | $queryBuilder->andWhere( |
||
| 570 | $queryBuilder->expr()->eq( |
||
| 571 | $uidLocal_field, |
||
| 572 | $queryBuilder->createNamedParameter((int)$uid, \PDO::PARAM_INT) |
||
| 573 | ) |
||
| 574 | ); |
||
| 575 | $queryBuilder->orderBy($sorting_field); |
||
| 576 | $queryBuilder->addOrderBy($uidForeign_field); |
||
| 577 | $statement = $queryBuilder->execute(); |
||
| 578 | while ($row = $statement->fetch()) { |
||
| 579 | // Default |
||
| 580 | if (!$this->MM_is_foreign) { |
||
| 581 | // If tablesnames columns exists and contain a name, then this value is the table, else it's the firstTable... |
||
| 582 | $theTable = $row['tablenames'] ?: $this->firstTable; |
||
| 583 | } |
||
| 584 | if (($row[$uidForeign_field] || $theTable === 'pages') && $theTable && isset($this->tableArray[$theTable])) { |
||
| 585 | $this->itemArray[$key]['id'] = $row[$uidForeign_field]; |
||
| 586 | $this->itemArray[$key]['table'] = $theTable; |
||
| 587 | $this->tableArray[$theTable][] = $row[$uidForeign_field]; |
||
| 588 | } elseif ($this->registerNonTableValues) { |
||
| 589 | $this->itemArray[$key]['id'] = $row[$uidForeign_field]; |
||
| 590 | $this->itemArray[$key]['table'] = '_NO_TABLE'; |
||
| 591 | $this->nonTableArray[] = $row[$uidForeign_field]; |
||
| 592 | } |
||
| 593 | $key++; |
||
| 594 | } |
||
| 595 | } |
||
| 596 | |||
| 597 | /** |
||
| 598 | * Writes the internal itemArray to MM table: |
||
| 599 | * |
||
| 600 | * @param string $MM_tableName MM table name |
||
| 601 | * @param int $uid Local UID |
||
| 602 | * @param bool $prependTableName If set, then table names will always be written. |
||
| 603 | */ |
||
| 604 | public function writeMM($MM_tableName, $uid, $prependTableName = false) |
||
| 835 | } |
||
| 836 | } |
||
| 837 | |||
| 838 | /** |
||
| 839 | * Remaps MM table elements from one local uid to another |
||
| 840 | * Does NOT update the reference index for you, must be called subsequently to do that! |
||
| 841 | * |
||
| 842 | * @param string $MM_tableName MM table name |
||
| 843 | * @param int $uid Local, current UID |
||
| 844 | * @param int $newUid Local, new UID |
||
| 845 | * @param bool $prependTableName If set, then table names will always be written. |
||
| 846 | */ |
||
| 847 | public function remapMM($MM_tableName, $uid, $newUid, $prependTableName = false) |
||
| 890 | } |
||
| 891 | } |
||
| 892 | |||
| 893 | /** |
||
| 894 | * Reads items from a foreign_table, that has a foreign_field (uid of the parent record) and |
||
| 895 | * stores the parts in the internal array itemArray and tableArray. |
||
| 896 | * |
||
| 897 | * @param int|string $uid The uid of the parent record (this value is also on the foreign_table in the foreign_field) |
||
| 898 | * @param array $conf TCA configuration for current field |
||
| 899 | */ |
||
| 900 | protected function readForeignField($uid, $conf) |
||
| 1025 | } |
||
| 1026 | } |
||
| 1027 | } |
||
| 1028 | |||
| 1029 | /** |
||
| 1030 | * Write the sorting values to a foreign_table, that has a foreign_field (uid of the parent record) |
||
| 1031 | * |
||
| 1032 | * @param array $conf TCA configuration for current field |
||
| 1033 | * @param int $parentUid The uid of the parent record |
||
| 1034 | * @param int $updateToUid If this is larger than zero it will be used as foreign UID instead of the given $parentUid (on Copy) |
||
| 1035 | * @param bool $skipSorting Do not update the sorting columns, this could happen for imported values |
||
| 1036 | */ |
||
| 1037 | public function writeForeignField($conf, $parentUid, $updateToUid = 0, $skipSorting = false) |
||
| 1038 | { |
||
| 1039 | if ($this->useLiveParentIds) { |
||
| 1040 | $parentUid = $this->getLiveDefaultId($this->currentTable, $parentUid); |
||
| 1041 | if (!empty($updateToUid)) { |
||
| 1042 | $updateToUid = $this->getLiveDefaultId($this->currentTable, $updateToUid); |
||
| 1043 | } |
||
| 1044 | } |
||
| 1045 | |||
| 1046 | // Ensure all values are set. |
||
| 1047 | $conf += [ |
||
| 1048 | 'foreign_table' => '', |
||
| 1049 | 'foreign_field' => '', |
||
| 1050 | 'symmetric_field' => '', |
||
| 1051 | 'foreign_table_field' => '', |
||
| 1052 | 'foreign_match_fields' => [], |
||
| 1053 | ]; |
||
| 1054 | |||
| 1055 | $c = 0; |
||
| 1056 | $foreign_table = $conf['foreign_table']; |
||
| 1057 | $foreign_field = $conf['foreign_field']; |
||
| 1058 | $symmetric_field = $conf['symmetric_field'] ?? ''; |
||
| 1059 | $foreign_table_field = $conf['foreign_table_field']; |
||
| 1060 | $foreign_match_fields = $conf['foreign_match_fields']; |
||
| 1061 | // If there are table items and we have a proper $parentUid |
||
| 1062 | if (MathUtility::canBeInterpretedAsInteger($parentUid) && !empty($this->tableArray)) { |
||
| 1063 | // If updateToUid is not a positive integer, set it to '0', so it will be ignored |
||
| 1064 | if (!(MathUtility::canBeInterpretedAsInteger($updateToUid) && $updateToUid > 0)) { |
||
| 1065 | $updateToUid = 0; |
||
| 1066 | } |
||
| 1067 | $considerWorkspaces = BackendUtility::isTableWorkspaceEnabled($foreign_table); |
||
| 1068 | $fields = 'uid,pid,' . $foreign_field; |
||
| 1069 | // Consider the symmetric field if defined: |
||
| 1070 | if ($symmetric_field) { |
||
| 1071 | $fields .= ',' . $symmetric_field; |
||
| 1072 | } |
||
| 1073 | // Consider workspaces if defined and currently used: |
||
| 1074 | if ($considerWorkspaces) { |
||
| 1075 | $fields .= ',t3ver_wsid,t3ver_state,t3ver_oid'; |
||
| 1076 | } |
||
| 1077 | // Update all items |
||
| 1078 | foreach ($this->itemArray as $val) { |
||
| 1079 | $uid = $val['id']; |
||
| 1080 | $table = $val['table']; |
||
| 1081 | $row = []; |
||
| 1082 | // Fetch the current (not overwritten) relation record if we should handle symmetric relations |
||
| 1083 | if ($symmetric_field || $considerWorkspaces) { |
||
| 1084 | $row = BackendUtility::getRecord($table, $uid, $fields, '', true); |
||
| 1085 | if (empty($row)) { |
||
| 1086 | continue; |
||
| 1087 | } |
||
| 1088 | } |
||
| 1089 | $isOnSymmetricSide = false; |
||
| 1090 | if ($symmetric_field) { |
||
| 1091 | $isOnSymmetricSide = self::isOnSymmetricSide((string)$parentUid, $conf, $row); |
||
| 1092 | } |
||
| 1093 | $updateValues = $foreign_match_fields; |
||
| 1094 | // No update to the uid is requested, so this is the normal behaviour |
||
| 1095 | // just update the fields and care about sorting |
||
| 1096 | if (!$updateToUid) { |
||
| 1097 | // Always add the pointer to the parent uid |
||
| 1098 | if ($isOnSymmetricSide) { |
||
| 1099 | $updateValues[$symmetric_field] = $parentUid; |
||
| 1100 | } else { |
||
| 1101 | $updateValues[$foreign_field] = $parentUid; |
||
| 1102 | } |
||
| 1103 | // If it is configured in TCA also to store the parent table in the child record, just do it |
||
| 1104 | if ($foreign_table_field && $this->currentTable) { |
||
| 1105 | $updateValues[$foreign_table_field] = $this->currentTable; |
||
| 1106 | } |
||
| 1107 | // Update sorting columns if not to be skipped |
||
| 1108 | if (!$skipSorting) { |
||
| 1109 | // Get the correct sorting field |
||
| 1110 | // Specific manual sortby for data handled by this field |
||
| 1111 | $sortby = ''; |
||
| 1112 | if ($conf['foreign_sortby'] ?? false) { |
||
| 1113 | $sortby = $conf['foreign_sortby']; |
||
| 1114 | } elseif ($GLOBALS['TCA'][$foreign_table]['ctrl']['sortby'] ?? false) { |
||
| 1115 | // manual sortby for all table records |
||
| 1116 | $sortby = $GLOBALS['TCA'][$foreign_table]['ctrl']['sortby']; |
||
| 1117 | } |
||
| 1118 | // Apply sorting on the symmetric side |
||
| 1119 | // (it depends on who created the relation, so what uid is in the symmetric_field): |
||
| 1120 | if ($isOnSymmetricSide && isset($conf['symmetric_sortby']) && $conf['symmetric_sortby']) { |
||
| 1121 | $sortby = $conf['symmetric_sortby']; |
||
| 1122 | } else { |
||
| 1123 | $tempSortBy = []; |
||
| 1124 | foreach (QueryHelper::parseOrderBy($sortby) as $orderPair) { |
||
| 1125 | [$fieldName, $order] = $orderPair; |
||
| 1126 | if ($order !== null) { |
||
| 1127 | $tempSortBy[] = implode(' ', $orderPair); |
||
| 1128 | } else { |
||
| 1129 | $tempSortBy[] = $fieldName; |
||
| 1130 | } |
||
| 1131 | } |
||
| 1132 | $sortby = implode(',', $tempSortBy); |
||
| 1133 | } |
||
| 1134 | if ($sortby) { |
||
| 1135 | $updateValues[$sortby] = ++$c; |
||
| 1136 | } |
||
| 1137 | } |
||
| 1138 | } else { |
||
| 1139 | if ($isOnSymmetricSide) { |
||
| 1140 | $updateValues[$symmetric_field] = $updateToUid; |
||
| 1141 | } else { |
||
| 1142 | $updateValues[$foreign_field] = $updateToUid; |
||
| 1143 | } |
||
| 1144 | } |
||
| 1145 | // Update accordant fields in the database: |
||
| 1146 | if (!empty($updateValues)) { |
||
| 1147 | // Update tstamp if any foreign field value has changed |
||
| 1148 | if (!empty($GLOBALS['TCA'][$table]['ctrl']['tstamp'])) { |
||
| 1149 | $updateValues[$GLOBALS['TCA'][$table]['ctrl']['tstamp']] = $GLOBALS['EXEC_TIME']; |
||
| 1150 | } |
||
| 1151 | $this->getConnectionForTableName($table) |
||
| 1152 | ->update( |
||
| 1153 | $table, |
||
| 1154 | $updateValues, |
||
| 1155 | ['uid' => (int)$uid] |
||
| 1156 | ); |
||
| 1157 | $this->updateRefIndex($table, $uid); |
||
| 1158 | } |
||
| 1159 | } |
||
| 1160 | } |
||
| 1161 | } |
||
| 1162 | |||
| 1163 | /** |
||
| 1164 | * After initialization you can extract an array of the elements from the object. Use this function for that. |
||
| 1165 | * |
||
| 1166 | * @param bool $prependTableName If set, then table names will ALWAYS be prepended (unless its a _NO_TABLE value) |
||
| 1167 | * @return array A numeric array. |
||
| 1168 | */ |
||
| 1169 | public function getValueArray($prependTableName = false) |
||
| 1170 | { |
||
| 1171 | // INIT: |
||
| 1172 | $valueArray = []; |
||
| 1173 | $tableC = count($this->tableArray); |
||
| 1174 | // If there are tables in the table array: |
||
| 1175 | if ($tableC) { |
||
| 1176 | // If there are more than ONE table in the table array, then always prepend table names: |
||
| 1177 | $prep = $tableC > 1 || $prependTableName; |
||
| 1178 | // Traverse the array of items: |
||
| 1179 | foreach ($this->itemArray as $val) { |
||
| 1180 | $valueArray[] = ($prep && $val['table'] !== '_NO_TABLE' ? $val['table'] . '_' : '') . $val['id']; |
||
| 1181 | } |
||
| 1182 | } |
||
| 1183 | // Return the array |
||
| 1184 | return $valueArray; |
||
| 1185 | } |
||
| 1186 | |||
| 1187 | /** |
||
| 1188 | * Reads all records from internal tableArray into the internal ->results array |
||
| 1189 | * where keys are table names and for each table, records are stored with uids as their keys. |
||
| 1190 | * If $this->fetchAllFields is false you can save a little memory |
||
| 1191 | * since only uid,pid and a few other fields are selected. |
||
| 1192 | * |
||
| 1193 | * @return array |
||
| 1194 | */ |
||
| 1195 | public function getFromDB() |
||
| 1196 | { |
||
| 1197 | // Traverses the tables listed: |
||
| 1198 | foreach ($this->tableArray as $table => $ids) { |
||
| 1199 | if (is_array($ids) && !empty($ids)) { |
||
| 1200 | $connection = $this->getConnectionForTableName($table); |
||
| 1201 | $maxBindParameters = PlatformInformation::getMaxBindParameters($connection->getDatabasePlatform()); |
||
| 1202 | |||
| 1203 | foreach (array_chunk($ids, $maxBindParameters - 10, true) as $chunk) { |
||
| 1204 | if ($this->fetchAllFields) { |
||
| 1205 | $fields = '*'; |
||
| 1206 | } else { |
||
| 1207 | $fields = 'uid,pid'; |
||
| 1208 | if ($GLOBALS['TCA'][$table]['ctrl']['label']) { |
||
| 1209 | // Title |
||
| 1210 | $fields .= ',' . $GLOBALS['TCA'][$table]['ctrl']['label']; |
||
| 1211 | } |
||
| 1212 | if ($GLOBALS['TCA'][$table]['ctrl']['label_alt'] ?? false) { |
||
| 1213 | // Alternative Title-Fields |
||
| 1214 | $fields .= ',' . $GLOBALS['TCA'][$table]['ctrl']['label_alt']; |
||
| 1215 | } |
||
| 1216 | } |
||
| 1217 | $queryBuilder = $connection->createQueryBuilder(); |
||
| 1218 | $queryBuilder->getRestrictions()->removeAll(); |
||
| 1219 | $queryBuilder->select(...GeneralUtility::trimExplode(',', $fields, true)) |
||
| 1220 | ->from($table) |
||
| 1221 | ->where($queryBuilder->expr()->in( |
||
| 1222 | 'uid', |
||
| 1223 | $queryBuilder->createNamedParameter($chunk, Connection::PARAM_INT_ARRAY) |
||
| 1224 | )); |
||
| 1225 | if ($this->additionalWhere[$table] ?? false) { |
||
| 1226 | $queryBuilder->andWhere( |
||
| 1227 | QueryHelper::stripLogicalOperatorPrefix($this->additionalWhere[$table]) |
||
| 1228 | ); |
||
| 1229 | } |
||
| 1230 | $statement = $queryBuilder->execute(); |
||
| 1231 | while ($row = $statement->fetch()) { |
||
| 1232 | $this->results[$table][$row['uid']] = $row; |
||
| 1233 | } |
||
| 1234 | } |
||
| 1235 | } |
||
| 1236 | } |
||
| 1237 | return $this->results; |
||
| 1238 | } |
||
| 1239 | |||
| 1240 | /** |
||
| 1241 | * This method is typically called after getFromDB(). |
||
| 1242 | * $this->results holds a list of resolved and valid relations, |
||
| 1243 | * $this->itemArray hold a list of "selected" relations from the incoming selection array. |
||
| 1244 | * The difference is that "itemArray" may hold a single table/uid combination multiple times, |
||
| 1245 | * for instance in a type=group relation having multiple=true, while "results" hold each |
||
| 1246 | * resolved relation only once. |
||
| 1247 | * The methods creates a sanitized "itemArray" from resolved "results" list, normalized |
||
| 1248 | * the return array to always contain both table name and uid, and keep incoming |
||
| 1249 | * "itemArray" sort order and keeps "multiple" selections. |
||
| 1250 | * |
||
| 1251 | * @return array |
||
| 1252 | */ |
||
| 1253 | public function getResolvedItemArray(): array |
||
| 1254 | { |
||
| 1255 | $itemArray = []; |
||
| 1256 | foreach ($this->itemArray as $item) { |
||
| 1257 | if (isset($this->results[$item['table']][$item['id']])) { |
||
| 1258 | $itemArray[] = [ |
||
| 1259 | 'table' => $item['table'], |
||
| 1260 | 'uid' => $item['id'], |
||
| 1261 | ]; |
||
| 1262 | } |
||
| 1263 | } |
||
| 1264 | return $itemArray; |
||
| 1265 | } |
||
| 1266 | |||
| 1267 | /** |
||
| 1268 | * Counts the items in $this->itemArray and puts this value in an array by default. |
||
| 1269 | * |
||
| 1270 | * @param bool $returnAsArray Whether to put the count value in an array |
||
| 1271 | * @return mixed The plain count as integer or the same inside an array |
||
| 1272 | */ |
||
| 1273 | public function countItems($returnAsArray = true) |
||
| 1274 | { |
||
| 1275 | $count = count($this->itemArray); |
||
| 1276 | if ($returnAsArray) { |
||
| 1277 | $count = [$count]; |
||
| 1278 | } |
||
| 1279 | return $count; |
||
| 1280 | } |
||
| 1281 | |||
| 1282 | /** |
||
| 1283 | * Update Reference Index (sys_refindex) for a record. |
||
| 1284 | * Should be called any almost any update to a record which could affect references inside the record. |
||
| 1285 | * If used from within DataHandler, only registers a row for update for later processing. |
||
| 1286 | * |
||
| 1287 | * @param string $table Table name |
||
| 1288 | * @param int $uid Record uid |
||
| 1289 | * @return array Result from ReferenceIndex->updateRefIndexTable() updated directly, else empty array |
||
| 1290 | */ |
||
| 1291 | protected function updateRefIndex($table, $uid): array |
||
| 1292 | { |
||
| 1293 | if (!$this->updateReferenceIndex) { |
||
| 1294 | return []; |
||
| 1295 | } |
||
| 1296 | if ($this->referenceIndexUpdater) { |
||
| 1297 | // Add to update registry if given |
||
| 1298 | $this->referenceIndexUpdater->registerForUpdate((string)$table, (int)$uid, $this->getWorkspaceId()); |
||
| 1299 | $statisticsArray = []; |
||
| 1300 | } else { |
||
| 1301 | // @deprecated else branch can be dropped when setUpdateReferenceIndex() is dropped. |
||
| 1302 | // Update reference index directly if enabled |
||
| 1303 | $referenceIndex = GeneralUtility::makeInstance(ReferenceIndex::class); |
||
| 1304 | if (BackendUtility::isTableWorkspaceEnabled($table)) { |
||
| 1305 | $referenceIndex->setWorkspaceId($this->getWorkspaceId()); |
||
| 1306 | } |
||
| 1307 | $statisticsArray = $referenceIndex->updateRefIndexTable($table, $uid); |
||
| 1308 | } |
||
| 1309 | return $statisticsArray; |
||
| 1310 | } |
||
| 1311 | |||
| 1312 | /** |
||
| 1313 | * Converts elements in the local item array to use version ids instead of |
||
| 1314 | * live ids, if possible. The most common use case is, to call that prior |
||
| 1315 | * to processing with MM relations in a workspace context. For tha special |
||
| 1316 | * case, ids on both side of the MM relation must use version ids if |
||
| 1317 | * available. |
||
| 1318 | * |
||
| 1319 | * @return bool Whether items have been converted |
||
| 1320 | */ |
||
| 1321 | public function convertItemArray() |
||
| 1362 | } |
||
| 1363 | |||
| 1364 | /** |
||
| 1365 | * @param int|null $workspaceId |
||
| 1366 | * @return bool Whether items have been purged |
||
| 1367 | */ |
||
| 1368 | public function purgeItemArray($workspaceId = null) |
||
| 1369 | { |
||
| 1370 | if ($workspaceId === null) { |
||
| 1371 | $workspaceId = $this->getWorkspaceId(); |
||
| 1372 | } else { |
||
| 1373 | $workspaceId = (int)$workspaceId; |
||
| 1374 | } |
||
| 1375 | |||
| 1376 | // Ensure, only live relations are in the items Array |
||
| 1377 | if ($workspaceId === 0) { |
||
| 1378 | $purgeCallback = 'purgeVersionedIds'; |
||
| 1379 | } else { |
||
| 1380 | // Otherwise, ensure that live relations are purged if version exists |
||
| 1381 | $purgeCallback = 'purgeLiveVersionedIds'; |
||
| 1382 | } |
||
| 1383 | |||
| 1384 | $itemArrayHasBeenPurged = $this->purgeItemArrayHandler($purgeCallback); |
||
| 1385 | $this->purged = ($this->purged || $itemArrayHasBeenPurged); |
||
| 1386 | return $itemArrayHasBeenPurged; |
||
| 1387 | } |
||
| 1388 | |||
| 1389 | /** |
||
| 1390 | * Removes items having a delete placeholder from $this->itemArray |
||
| 1391 | * |
||
| 1392 | * @return bool Whether items have been purged |
||
| 1393 | */ |
||
| 1394 | public function processDeletePlaceholder() |
||
| 1401 | } |
||
| 1402 | |||
| 1403 | /** |
||
| 1404 | * Handles a purge callback on $this->itemArray |
||
| 1405 | * |
||
| 1406 | * @param string $purgeCallback |
||
| 1407 | * @return bool Whether items have been purged |
||
| 1408 | */ |
||
| 1409 | protected function purgeItemArrayHandler($purgeCallback) |
||
| 1410 | { |
||
| 1411 | $itemArrayHasBeenPurged = false; |
||
| 1412 | |||
| 1413 | foreach ($this->tableArray as $itemTableName => $itemIds) { |
||
| 1414 | if (empty($itemIds) || !BackendUtility::isTableWorkspaceEnabled($itemTableName)) { |
||
| 1415 | continue; |
||
| 1416 | } |
||
| 1417 | |||
| 1418 | $purgedItemIds = []; |
||
| 1419 | $callable =[$this, $purgeCallback]; |
||
| 1420 | if (is_callable($callable)) { |
||
| 1421 | $purgedItemIds = $callable($itemTableName, $itemIds); |
||
| 1422 | } |
||
| 1423 | |||
| 1424 | $removedItemIds = array_diff($itemIds, $purgedItemIds); |
||
| 1425 | foreach ($removedItemIds as $removedItemId) { |
||
| 1426 | $this->removeFromItemArray($itemTableName, $removedItemId); |
||
| 1427 | } |
||
| 1428 | $this->tableArray[$itemTableName] = $purgedItemIds; |
||
| 1429 | if (!empty($removedItemIds)) { |
||
| 1430 | $itemArrayHasBeenPurged = true; |
||
| 1431 | } |
||
| 1432 | } |
||
| 1433 | |||
| 1434 | return $itemArrayHasBeenPurged; |
||
| 1435 | } |
||
| 1436 | |||
| 1437 | /** |
||
| 1438 | * Purges ids that are versioned. |
||
| 1439 | * |
||
| 1440 | * @param string $tableName |
||
| 1441 | * @param array $ids |
||
| 1442 | * @return array |
||
| 1443 | */ |
||
| 1444 | protected function purgeVersionedIds($tableName, array $ids) |
||
| 1445 | { |
||
| 1446 | $ids = $this->sanitizeIds($ids); |
||
| 1447 | $ids = (array)array_combine($ids, $ids); |
||
| 1448 | $connection = $this->getConnectionForTableName($tableName); |
||
| 1449 | $maxBindParameters = PlatformInformation::getMaxBindParameters($connection->getDatabasePlatform()); |
||
| 1450 | |||
| 1451 | foreach (array_chunk($ids, $maxBindParameters - 10, true) as $chunk) { |
||
| 1452 | $queryBuilder = $connection->createQueryBuilder(); |
||
| 1453 | $queryBuilder->getRestrictions()->removeAll(); |
||
| 1454 | $result = $queryBuilder->select('uid', 't3ver_oid', 't3ver_state') |
||
| 1455 | ->from($tableName) |
||
| 1456 | ->where( |
||
| 1457 | $queryBuilder->expr()->in( |
||
| 1458 | 't3ver_oid', |
||
| 1459 | $queryBuilder->createNamedParameter($chunk, Connection::PARAM_INT_ARRAY) |
||
| 1460 | ), |
||
| 1461 | $queryBuilder->expr()->neq( |
||
| 1462 | 't3ver_wsid', |
||
| 1463 | $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT) |
||
| 1464 | ) |
||
| 1465 | ) |
||
| 1466 | ->orderBy('t3ver_state', 'DESC') |
||
| 1467 | ->execute(); |
||
| 1468 | |||
| 1469 | while ($version = $result->fetch()) { |
||
| 1470 | $versionId = $version['uid']; |
||
| 1471 | if (isset($ids[$versionId])) { |
||
| 1472 | unset($ids[$versionId]); |
||
| 1473 | } |
||
| 1474 | } |
||
| 1475 | } |
||
| 1476 | |||
| 1477 | return array_values($ids); |
||
| 1478 | } |
||
| 1479 | |||
| 1480 | /** |
||
| 1481 | * Purges ids that are live but have an accordant version. |
||
| 1482 | * |
||
| 1483 | * @param string $tableName |
||
| 1484 | * @param array $ids |
||
| 1485 | * @return array |
||
| 1486 | */ |
||
| 1487 | protected function purgeLiveVersionedIds($tableName, array $ids) |
||
| 1488 | { |
||
| 1489 | $ids = $this->sanitizeIds($ids); |
||
| 1490 | $ids = (array)array_combine($ids, $ids); |
||
| 1491 | $connection = $this->getConnectionForTableName($tableName); |
||
| 1492 | $maxBindParameters = PlatformInformation::getMaxBindParameters($connection->getDatabasePlatform()); |
||
| 1493 | |||
| 1494 | foreach (array_chunk($ids, $maxBindParameters - 10, true) as $chunk) { |
||
| 1495 | $queryBuilder = $connection->createQueryBuilder(); |
||
| 1496 | $queryBuilder->getRestrictions()->removeAll(); |
||
| 1497 | $result = $queryBuilder->select('uid', 't3ver_oid', 't3ver_state') |
||
| 1498 | ->from($tableName) |
||
| 1499 | ->where( |
||
| 1500 | $queryBuilder->expr()->in( |
||
| 1501 | 't3ver_oid', |
||
| 1502 | $queryBuilder->createNamedParameter($chunk, Connection::PARAM_INT_ARRAY) |
||
| 1503 | ), |
||
| 1504 | $queryBuilder->expr()->neq( |
||
| 1505 | 't3ver_wsid', |
||
| 1506 | $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT) |
||
| 1507 | ) |
||
| 1508 | ) |
||
| 1509 | ->orderBy('t3ver_state', 'DESC') |
||
| 1510 | ->execute(); |
||
| 1511 | |||
| 1512 | while ($version = $result->fetch()) { |
||
| 1513 | $versionId = $version['uid']; |
||
| 1514 | $liveId = $version['t3ver_oid']; |
||
| 1515 | if (isset($ids[$liveId]) && isset($ids[$versionId])) { |
||
| 1516 | unset($ids[$liveId]); |
||
| 1517 | } |
||
| 1518 | } |
||
| 1519 | } |
||
| 1520 | |||
| 1521 | return array_values($ids); |
||
| 1522 | } |
||
| 1523 | |||
| 1524 | /** |
||
| 1525 | * Purges ids that have a delete placeholder |
||
| 1526 | * |
||
| 1527 | * @param string $tableName |
||
| 1528 | * @param array $ids |
||
| 1529 | * @return array |
||
| 1530 | */ |
||
| 1531 | protected function purgeDeletePlaceholder($tableName, array $ids) |
||
| 1532 | { |
||
| 1533 | $ids = $this->sanitizeIds($ids); |
||
| 1534 | $ids = array_combine($ids, $ids) ?: []; |
||
| 1535 | $connection = $this->getConnectionForTableName($tableName); |
||
| 1536 | $maxBindParameters = PlatformInformation::getMaxBindParameters($connection->getDatabasePlatform()); |
||
| 1537 | |||
| 1538 | foreach (array_chunk($ids, $maxBindParameters - 10, true) as $chunk) { |
||
| 1539 | $queryBuilder = $connection->createQueryBuilder(); |
||
| 1540 | $queryBuilder->getRestrictions()->removeAll(); |
||
| 1541 | $result = $queryBuilder->select('uid', 't3ver_oid', 't3ver_state') |
||
| 1542 | ->from($tableName) |
||
| 1543 | ->where( |
||
| 1544 | $queryBuilder->expr()->in( |
||
| 1545 | 't3ver_oid', |
||
| 1546 | $queryBuilder->createNamedParameter($chunk, Connection::PARAM_INT_ARRAY) |
||
| 1547 | ), |
||
| 1548 | $queryBuilder->expr()->eq( |
||
| 1549 | 't3ver_wsid', |
||
| 1550 | $queryBuilder->createNamedParameter( |
||
| 1551 | $this->getWorkspaceId(), |
||
| 1552 | \PDO::PARAM_INT |
||
| 1553 | ) |
||
| 1554 | ), |
||
| 1555 | $queryBuilder->expr()->eq( |
||
| 1556 | 't3ver_state', |
||
| 1557 | $queryBuilder->createNamedParameter( |
||
| 1558 | (string)VersionState::cast(VersionState::DELETE_PLACEHOLDER), |
||
| 1559 | \PDO::PARAM_INT |
||
| 1560 | ) |
||
| 1561 | ) |
||
| 1562 | ) |
||
| 1563 | ->execute(); |
||
| 1564 | |||
| 1565 | while ($version = $result->fetch()) { |
||
| 1566 | $liveId = $version['t3ver_oid']; |
||
| 1567 | if (isset($ids[$liveId])) { |
||
| 1568 | unset($ids[$liveId]); |
||
| 1569 | } |
||
| 1570 | } |
||
| 1571 | } |
||
| 1572 | |||
| 1573 | return array_values($ids); |
||
| 1574 | } |
||
| 1575 | |||
| 1576 | protected function removeFromItemArray($tableName, $id) |
||
| 1577 | { |
||
| 1578 | foreach ($this->itemArray as $index => $item) { |
||
| 1579 | if ($item['table'] === $tableName && (string)$item['id'] === (string)$id) { |
||
| 1580 | unset($this->itemArray[$index]); |
||
| 1581 | return true; |
||
| 1582 | } |
||
| 1583 | } |
||
| 1584 | return false; |
||
| 1585 | } |
||
| 1586 | |||
| 1587 | /** |
||
| 1588 | * Checks, if we're looking from the "other" side, the symmetric side, to a symmetric relation. |
||
| 1589 | * |
||
| 1590 | * @param string $parentUid The uid of the parent record |
||
| 1591 | * @param array $parentConf The TCA configuration of the parent field embedding the child records |
||
| 1592 | * @param array $childRec The record row of the child record |
||
| 1593 | * @return bool Returns TRUE if looking from the symmetric ("other") side to the relation. |
||
| 1594 | */ |
||
| 1595 | protected static function isOnSymmetricSide($parentUid, $parentConf, $childRec) |
||
| 1596 | { |
||
| 1597 | return MathUtility::canBeInterpretedAsInteger($childRec['uid']) |
||
| 1598 | && $parentConf['symmetric_field'] |
||
| 1599 | && $parentUid == $childRec[$parentConf['symmetric_field']]; |
||
| 1600 | } |
||
| 1601 | |||
| 1602 | /** |
||
| 1603 | * Completes MM values to be written by values from the opposite relation. |
||
| 1604 | * This method used MM insert field or MM match fields if defined. |
||
| 1605 | * |
||
| 1606 | * @param string $tableName Name of the opposite table |
||
| 1607 | * @param array $referenceValues Values to be written |
||
| 1608 | * @return array Values to be written, possibly modified |
||
| 1609 | */ |
||
| 1610 | protected function completeOppositeUsageValues($tableName, array $referenceValues) |
||
| 1611 | { |
||
| 1612 | if (empty($this->MM_oppositeUsage[$tableName]) || count($this->MM_oppositeUsage[$tableName]) > 1) { |
||
| 1613 | return $referenceValues; |
||
| 1614 | } |
||
| 1615 | |||
| 1616 | $fieldName = $this->MM_oppositeUsage[$tableName][0]; |
||
| 1617 | if (empty($GLOBALS['TCA'][$tableName]['columns'][$fieldName]['config'])) { |
||
| 1618 | return $referenceValues; |
||
| 1619 | } |
||
| 1620 | |||
| 1621 | $configuration = $GLOBALS['TCA'][$tableName]['columns'][$fieldName]['config']; |
||
| 1622 | if (!empty($configuration['MM_insert_fields'])) { |
||
| 1623 | $referenceValues = array_merge($configuration['MM_insert_fields'], $referenceValues); |
||
| 1624 | } elseif (!empty($configuration['MM_match_fields'])) { |
||
| 1625 | $referenceValues = array_merge($configuration['MM_match_fields'], $referenceValues); |
||
| 1626 | } |
||
| 1627 | |||
| 1628 | return $referenceValues; |
||
| 1629 | } |
||
| 1630 | |||
| 1631 | /** |
||
| 1632 | * Gets the record uid of the live default record. If already |
||
| 1633 | * pointing to the live record, the submitted record uid is returned. |
||
| 1634 | * |
||
| 1635 | * @param string $tableName |
||
| 1636 | * @param int|string $id |
||
| 1637 | * @return int |
||
| 1638 | */ |
||
| 1639 | protected function getLiveDefaultId($tableName, $id) |
||
| 1646 | } |
||
| 1647 | |||
| 1648 | /** |
||
| 1649 | * Removes empty values (null, '0', 0, false). |
||
| 1650 | * |
||
| 1651 | * @param int[] $ids |
||
| 1652 | * @return array |
||
| 1653 | */ |
||
| 1654 | protected function sanitizeIds(array $ids): array |
||
| 1657 | } |
||
| 1658 | |||
| 1659 | /** |
||
| 1660 | * @param string $tableName |
||
| 1661 | * @param int[] $ids |
||
| 1662 | * @param array $sortingStatement |
||
| 1663 | * @return PlainDataResolver |
||
| 1664 | */ |
||
| 1665 | protected function getResolver($tableName, array $ids, array $sortingStatement = null) |
||
| 1678 | } |
||
| 1679 | |||
| 1680 | /** |
||
| 1681 | * @param string $tableName |
||
| 1682 | * @return Connection |
||
| 1683 | */ |
||
| 1684 | protected function getConnectionForTableName(string $tableName) |
||
| 1685 | { |
||
| 1686 | return GeneralUtility::makeInstance(ConnectionPool::class) |
||
| 1687 | ->getConnectionForTable($tableName); |
||
| 1688 | } |
||
| 1689 | } |
||
| 1690 |