@@ -16,27 +16,27 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class FieldType extends Enumeration |
| 18 | 18 | { |
| 19 | - public const TEXT = 'text'; |
|
| 19 | + public const TEXT = 'text'; |
|
| 20 | 20 | |
| 21 | - public const NUMBER = 'number'; |
|
| 21 | + public const NUMBER = 'number'; |
|
| 22 | 22 | |
| 23 | - public const EMAIL = 'email'; |
|
| 23 | + public const EMAIL = 'email'; |
|
| 24 | 24 | |
| 25 | - public const DATE = 'date'; |
|
| 25 | + public const DATE = 'date'; |
|
| 26 | 26 | |
| 27 | - public const DATETIME = 'datetime'; |
|
| 27 | + public const DATETIME = 'datetime'; |
|
| 28 | 28 | |
| 29 | - public const TEXTAREA = 'textarea'; |
|
| 29 | + public const TEXTAREA = 'textarea'; |
|
| 30 | 30 | |
| 31 | - public const SELECT = 'select'; |
|
| 31 | + public const SELECT = 'select'; |
|
| 32 | 32 | |
| 33 | - public const RADIO = 'radio'; |
|
| 33 | + public const RADIO = 'radio'; |
|
| 34 | 34 | |
| 35 | - public const CHECKBOX = 'check'; |
|
| 35 | + public const CHECKBOX = 'check'; |
|
| 36 | 36 | |
| 37 | - public const FILE = 'file'; |
|
| 37 | + public const FILE = 'file'; |
|
| 38 | 38 | |
| 39 | - public const MULTISELECT = 'multiselect'; |
|
| 39 | + public const MULTISELECT = 'multiselect'; |
|
| 40 | 40 | |
| 41 | - public const TREE = 'tree'; |
|
| 41 | + public const TREE = 'tree'; |
|
| 42 | 42 | } |
@@ -18,765 +18,765 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class FieldService extends AbstractTca |
| 20 | 20 | { |
| 21 | - /** |
|
| 22 | - * @var string |
|
| 23 | - */ |
|
| 24 | - protected $fieldName; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * @var string |
|
| 28 | - */ |
|
| 29 | - protected $compositeField; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * @var string |
|
| 33 | - */ |
|
| 34 | - protected $tableName; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * @var array |
|
| 38 | - */ |
|
| 39 | - protected $tca; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @param string $fieldName |
|
| 43 | - * @param array $tca |
|
| 44 | - * @param string $tableName |
|
| 45 | - * @param string $compositeField |
|
| 46 | - * @return \Fab\Vidi\Tca\FieldService |
|
| 47 | - */ |
|
| 48 | - public function __construct($fieldName, array $tca, $tableName, $compositeField = '') |
|
| 49 | - { |
|
| 50 | - $this->fieldName = $fieldName; |
|
| 51 | - $this->tca = $tca; |
|
| 52 | - $this->tableName = $tableName; |
|
| 53 | - $this->compositeField = $compositeField; |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Tells whether the field is considered as system field, e.g. uid, crdate, tstamp, etc... |
|
| 58 | - * |
|
| 59 | - * @return bool |
|
| 60 | - */ |
|
| 61 | - public function isSystem() |
|
| 62 | - { |
|
| 63 | - return in_array($this->fieldName, Tca::getSystemFields()); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Tells the opposition of isSystem() |
|
| 68 | - * |
|
| 69 | - * @return bool |
|
| 70 | - */ |
|
| 71 | - public function isNotSystem() |
|
| 72 | - { |
|
| 73 | - return !$this->isSystem(); |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Returns the configuration for a $field |
|
| 78 | - * |
|
| 79 | - * @throws \Exception |
|
| 80 | - * @return array |
|
| 81 | - */ |
|
| 82 | - public function getConfiguration() |
|
| 83 | - { |
|
| 84 | - return empty($this->tca['config']) ? [] : $this->tca['config']; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Returns a key of the configuration. |
|
| 89 | - * If the key can not to be found, returns null. |
|
| 90 | - * |
|
| 91 | - * @param string $key |
|
| 92 | - * @return mixed |
|
| 93 | - */ |
|
| 94 | - public function get($key) |
|
| 95 | - { |
|
| 96 | - $configuration = $this->getConfiguration(); |
|
| 97 | - return empty($configuration[$key]) ? null : $configuration[$key]; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Returns the foreign field of a given field (opposite relational field). |
|
| 102 | - * If no relation exists, returns null. |
|
| 103 | - * |
|
| 104 | - * @return string|null |
|
| 105 | - */ |
|
| 106 | - public function getForeignField() |
|
| 107 | - { |
|
| 108 | - $result = null; |
|
| 109 | - $configuration = $this->getConfiguration(); |
|
| 110 | - |
|
| 111 | - if (!empty($configuration['foreign_field'])) { |
|
| 112 | - $result = $configuration['foreign_field']; |
|
| 113 | - } elseif ($this->hasRelationManyToMany()) { |
|
| 114 | - $foreignTable = $this->getForeignTable(); |
|
| 115 | - $manyToManyTable = $this->getManyToManyTable(); |
|
| 116 | - |
|
| 117 | - // Load TCA service of foreign field. |
|
| 118 | - $tcaForeignTableService = Tca::table($foreignTable); |
|
| 119 | - |
|
| 120 | - // Look into the MM relations checking for the opposite field |
|
| 121 | - foreach ($tcaForeignTableService->getFields() as $fieldName) { |
|
| 122 | - if ($manyToManyTable == $tcaForeignTableService->field($fieldName)->getManyToManyTable()) { |
|
| 123 | - $result = $fieldName; |
|
| 124 | - break; |
|
| 125 | - } |
|
| 126 | - } |
|
| 127 | - } |
|
| 128 | - return $result; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * Returns the foreign table of a given field (opposite relational table). |
|
| 133 | - * If no relation exists, returns null. |
|
| 134 | - * |
|
| 135 | - * @return string|null |
|
| 136 | - */ |
|
| 137 | - public function getForeignTable() |
|
| 138 | - { |
|
| 139 | - $result = null; |
|
| 140 | - $configuration = $this->getConfiguration(); |
|
| 141 | - |
|
| 142 | - if (!empty($configuration['foreign_table'])) { |
|
| 143 | - $result = $configuration['foreign_table']; |
|
| 144 | - } elseif ($this->isGroup()) { |
|
| 145 | - $fieldParts = explode('.', $this->compositeField, 2); |
|
| 146 | - $result = $fieldParts[1]; |
|
| 147 | - } |
|
| 148 | - return $result; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * Returns the foreign clause. |
|
| 153 | - * If no foreign order exists, returns empty string. |
|
| 154 | - * |
|
| 155 | - * @return string |
|
| 156 | - */ |
|
| 157 | - public function getForeignClause() |
|
| 158 | - { |
|
| 159 | - $result = ''; |
|
| 160 | - $configuration = $this->getConfiguration(); |
|
| 161 | - |
|
| 162 | - if (!empty($configuration['foreign_table_where'])) { |
|
| 163 | - $parts = explode('ORDER BY', $configuration['foreign_table_where']); |
|
| 164 | - if (!empty($parts[0])) { |
|
| 165 | - $result = $parts[0]; |
|
| 166 | - } |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - // Substitute some variables |
|
| 170 | - return $this->substituteKnownMarkers($result); |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * Substitute some known markers from the where clause in the Frontend Context. |
|
| 175 | - * |
|
| 176 | - * @param string $clause |
|
| 177 | - * @return string |
|
| 178 | - */ |
|
| 179 | - protected function substituteKnownMarkers($clause) |
|
| 180 | - { |
|
| 181 | - if ($clause && $this->isFrontendMode()) { |
|
| 182 | - $searches = array( |
|
| 183 | - '###CURRENT_PID###', |
|
| 184 | - '###REC_FIELD_sys_language_uid###' |
|
| 185 | - ); |
|
| 186 | - |
|
| 187 | - $replaces = array( |
|
| 188 | - $this->getFrontendObject()->id, |
|
| 189 | - GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('language', 'id'), |
|
| 190 | - ); |
|
| 191 | - |
|
| 192 | - $clause = str_replace($searches, $replaces, $clause); |
|
| 193 | - } |
|
| 194 | - return $clause; |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * Returns the foreign order of the current field. |
|
| 199 | - * If no foreign order exists, returns empty string. |
|
| 200 | - * |
|
| 201 | - * @return string |
|
| 202 | - */ |
|
| 203 | - public function getForeignOrder() |
|
| 204 | - { |
|
| 205 | - $result = ''; |
|
| 206 | - $configuration = $this->getConfiguration(); |
|
| 207 | - |
|
| 208 | - if (!empty($configuration['foreign_table_where'])) { |
|
| 209 | - $parts = explode('ORDER BY', $configuration['foreign_table_where']); |
|
| 210 | - if (!empty($parts[1])) { |
|
| 211 | - $result = $parts[1]; |
|
| 212 | - } |
|
| 213 | - } |
|
| 214 | - return $result; |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * Returns the MM table of a field. |
|
| 219 | - * If no relation exists, returns null. |
|
| 220 | - * |
|
| 221 | - * @return string|null |
|
| 222 | - */ |
|
| 223 | - public function getManyToManyTable() |
|
| 224 | - { |
|
| 225 | - $configuration = $this->getConfiguration(); |
|
| 226 | - return empty($configuration['MM']) ? null : $configuration['MM']; |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - /** |
|
| 230 | - * Returns a possible additional table name used in MM relations. |
|
| 231 | - * If no table name exists, returns null. |
|
| 232 | - * |
|
| 233 | - * @return string|null |
|
| 234 | - */ |
|
| 235 | - public function getAdditionalTableNameCondition() |
|
| 236 | - { |
|
| 237 | - $result = null; |
|
| 238 | - $configuration = $this->getConfiguration(); |
|
| 239 | - |
|
| 240 | - if (!empty($configuration['MM_match_fields']['tablenames'])) { |
|
| 241 | - $result = $configuration['MM_match_fields']['tablenames']; |
|
| 242 | - } elseif ($this->isGroup()) { |
|
| 243 | - // @todo check if $this->fieldName could be simply used as $result |
|
| 244 | - $fieldParts = explode('.', $this->compositeField, 2); |
|
| 245 | - $result = $fieldParts[1]; |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - return $result; |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - /** |
|
| 252 | - * Returns a possible additional conditions for MM tables such as "tablenames", "fieldname", etc... |
|
| 253 | - * |
|
| 254 | - * @return array |
|
| 255 | - */ |
|
| 256 | - public function getAdditionalMMCondition() |
|
| 257 | - { |
|
| 258 | - $additionalMMConditions = []; |
|
| 259 | - $configuration = $this->getConfiguration(); |
|
| 260 | - |
|
| 261 | - if (!empty($configuration['MM_match_fields'])) { |
|
| 262 | - $additionalMMConditions = $configuration['MM_match_fields']; |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - // Add in any case a table name for "group" |
|
| 266 | - if ($this->isGroup()) { |
|
| 267 | - // @todo check if $this->fieldName could be simply used as $result |
|
| 268 | - $fieldParts = explode('.', $this->compositeField, 2); |
|
| 269 | - $additionalMMConditions = array( |
|
| 270 | - 'tablenames' => $fieldParts[1], |
|
| 271 | - ); |
|
| 272 | - } |
|
| 273 | - return $additionalMMConditions; |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - /** |
|
| 277 | - * Returns whether the field name is the opposite in MM relation. |
|
| 278 | - * |
|
| 279 | - * @return bool |
|
| 280 | - */ |
|
| 281 | - public function isOppositeRelation() |
|
| 282 | - { |
|
| 283 | - $configuration = $this->getConfiguration(); |
|
| 284 | - return isset($configuration['MM_opposite_field']); |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - /** |
|
| 288 | - * Returns the configuration for a $field. |
|
| 289 | - * |
|
| 290 | - * @throws \Exception |
|
| 291 | - * @return string |
|
| 292 | - */ |
|
| 293 | - public function getType() |
|
| 294 | - { |
|
| 295 | - if ($this->isSystem()) { |
|
| 296 | - $fieldType = FieldType::NUMBER; |
|
| 297 | - } else { |
|
| 298 | - $configuration = $this->getConfiguration(); |
|
| 299 | - |
|
| 300 | - if (empty($configuration['type'])) { |
|
| 301 | - throw new \Exception(sprintf('No field type found for "%s" in table "%s"', $this->fieldName, $this->tableName), 1385556627); |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - $fieldType = $configuration['type']; |
|
| 305 | - |
|
| 306 | - if ($configuration['type'] === FieldType::SELECT && !empty($configuration['size']) && $configuration['size'] > 1) { |
|
| 307 | - $fieldType = FieldType::MULTISELECT; |
|
| 308 | - } elseif (!empty($configuration['foreign_table']) |
|
| 309 | - && ($configuration['foreign_table'] == 'sys_file_reference' || $configuration['foreign_table'] == 'sys_file') |
|
| 310 | - ) { |
|
| 311 | - $fieldType = FieldType::FILE; |
|
| 312 | - } elseif (!empty($configuration['eval'])) { |
|
| 313 | - $parts = GeneralUtility::trimExplode(',', $configuration['eval']); |
|
| 314 | - if (in_array('datetime', $parts)) { |
|
| 315 | - $fieldType = FieldType::DATETIME; |
|
| 316 | - } elseif (in_array('date', $parts)) { |
|
| 317 | - $fieldType = FieldType::DATE; |
|
| 318 | - } elseif (in_array('email', $parts)) { |
|
| 319 | - $fieldType = FieldType::EMAIL; |
|
| 320 | - } elseif (in_array('int', $parts) || in_array('double2', $parts)) { |
|
| 321 | - $fieldType = FieldType::NUMBER; |
|
| 322 | - } |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - // Do some legacy conversion |
|
| 326 | - if ($fieldType === 'input') { |
|
| 327 | - $fieldType = FieldType::TEXT; |
|
| 328 | - } elseif ($fieldType === 'text') { |
|
| 329 | - $fieldType = FieldType::TEXTAREA; |
|
| 330 | - } |
|
| 331 | - } |
|
| 332 | - return $fieldType; |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - /** |
|
| 336 | - * Return the default value. |
|
| 337 | - * |
|
| 338 | - * @return bool |
|
| 339 | - */ |
|
| 340 | - public function getDefaultValue() |
|
| 341 | - { |
|
| 342 | - $configuration = $this->getConfiguration(); |
|
| 343 | - return isset($configuration['default']) ? $configuration['default'] : null; |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - /** |
|
| 347 | - * Get the translation of a label given a column. |
|
| 348 | - * |
|
| 349 | - * @return string |
|
| 350 | - */ |
|
| 351 | - public function getLabel() |
|
| 352 | - { |
|
| 353 | - $label = ''; |
|
| 354 | - if ($this->hasLabel()) { |
|
| 355 | - try { |
|
| 356 | - $label = LocalizationUtility::translate($this->tca['label'], ''); |
|
| 357 | - } catch (\InvalidArgumentException $e) { |
|
| 358 | - } |
|
| 359 | - if (empty($label)) { |
|
| 360 | - $label = $this->tca['label']; |
|
| 361 | - } |
|
| 362 | - } |
|
| 363 | - return $label; |
|
| 364 | - } |
|
| 365 | - |
|
| 366 | - /** |
|
| 367 | - * Get the translation of a label given a column. |
|
| 368 | - * |
|
| 369 | - * @param string $itemValue the item value to search for. |
|
| 370 | - * @return string |
|
| 371 | - */ |
|
| 372 | - public function getLabelForItem($itemValue) |
|
| 373 | - { |
|
| 374 | - // Early return whether there is nothing to be translated as label. |
|
| 375 | - if (is_null($itemValue)) { |
|
| 376 | - return ''; |
|
| 377 | - } elseif (is_string($itemValue) && $itemValue === '') { |
|
| 378 | - return $itemValue; |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - $configuration = $this->getConfiguration(); |
|
| 382 | - if (!empty($configuration['items']) && is_array($configuration['items'])) { |
|
| 383 | - foreach ($configuration['items'] as $item) { |
|
| 384 | - if ($item[1] == $itemValue) { |
|
| 385 | - try { |
|
| 386 | - $label = LocalizationUtility::translate($item[0], ''); |
|
| 387 | - } catch (\InvalidArgumentException $e) { |
|
| 388 | - } |
|
| 389 | - if (empty($label)) { |
|
| 390 | - $label = $item[0]; |
|
| 391 | - } |
|
| 392 | - break; |
|
| 393 | - } |
|
| 394 | - } |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - // Try fetching a label from a possible itemsProcFunc |
|
| 398 | - if (!isset($label) && is_scalar($itemValue)) { |
|
| 399 | - $items = $this->fetchItemsFromUserFunction(); |
|
| 400 | - if (!empty($items[$itemValue])) { |
|
| 401 | - $label = $items[$itemValue]; |
|
| 402 | - } |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - // Returns a label if it has been found, otherwise returns the item value as fallback. |
|
| 406 | - return isset($label) ? $label : $itemValue; |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - /** |
|
| 410 | - * Retrieve items from User Function. |
|
| 411 | - * |
|
| 412 | - * @return array |
|
| 413 | - */ |
|
| 414 | - protected function fetchItemsFromUserFunction() |
|
| 415 | - { |
|
| 416 | - $values = []; |
|
| 417 | - |
|
| 418 | - $configuration = $this->getConfiguration(); |
|
| 419 | - if (!empty($configuration['itemsProcFunc'])) { |
|
| 420 | - $parts = explode('php:', $configuration['itemsProcFunc']); |
|
| 421 | - if (!empty($parts[1])) { |
|
| 422 | - list($class, $method) = explode('->', $parts[1]); |
|
| 423 | - |
|
| 424 | - $parameters['items'] = []; |
|
| 425 | - $object = GeneralUtility::makeInstance($class); |
|
| 426 | - $object->$method($parameters); |
|
| 427 | - |
|
| 428 | - foreach ($parameters['items'] as $items) { |
|
| 429 | - $values[$items[1]] = $items[0]; |
|
| 430 | - } |
|
| 431 | - } |
|
| 432 | - } |
|
| 433 | - return $values; |
|
| 434 | - } |
|
| 435 | - |
|
| 436 | - /** |
|
| 437 | - * Get a possible icon given a field name an an item. |
|
| 438 | - * |
|
| 439 | - * @param string $itemValue the item value to search for. |
|
| 440 | - * @return string |
|
| 441 | - */ |
|
| 442 | - public function getIconForItem($itemValue) |
|
| 443 | - { |
|
| 444 | - $result = ''; |
|
| 445 | - $configuration = $this->getConfiguration(); |
|
| 446 | - if (!empty($configuration['items']) && is_array($configuration['items'])) { |
|
| 447 | - foreach ($configuration['items'] as $item) { |
|
| 448 | - if ($item[1] == $itemValue) { |
|
| 449 | - $result = empty($item[2]) ? '' : $item[2]; |
|
| 450 | - break; |
|
| 451 | - } |
|
| 452 | - } |
|
| 453 | - } |
|
| 454 | - return $result; |
|
| 455 | - } |
|
| 456 | - |
|
| 457 | - /** |
|
| 458 | - * Returns whether the field has a label. |
|
| 459 | - * |
|
| 460 | - * @return bool |
|
| 461 | - */ |
|
| 462 | - public function hasLabel() |
|
| 463 | - { |
|
| 464 | - return empty($this->tca['label']) ? false : true; |
|
| 465 | - } |
|
| 466 | - |
|
| 467 | - /** |
|
| 468 | - * Tell whether the current BE User has access to this field. |
|
| 469 | - * |
|
| 470 | - * @return bool |
|
| 471 | - */ |
|
| 472 | - public function hasAccess() |
|
| 473 | - { |
|
| 474 | - $hasAccess = true; |
|
| 475 | - if ($this->isBackendMode() |
|
| 476 | - && Tca::table($this->tableName)->hasAccess() |
|
| 477 | - && isset($this->tca['exclude']) |
|
| 478 | - && $this->tca['exclude'] |
|
| 479 | - ) { |
|
| 480 | - $hasAccess = $this->getBackendUser()->check('non_exclude_fields', $this->tableName . ':' . $this->fieldName); |
|
| 481 | - } |
|
| 482 | - return $hasAccess; |
|
| 483 | - } |
|
| 484 | - |
|
| 485 | - /** |
|
| 486 | - * Returns whether the field is numerical. |
|
| 487 | - * |
|
| 488 | - * @return bool |
|
| 489 | - */ |
|
| 490 | - public function isNumerical() |
|
| 491 | - { |
|
| 492 | - $result = $this->isSystem(); |
|
| 493 | - if ($result === false) { |
|
| 494 | - $configuration = $this->getConfiguration(); |
|
| 495 | - $parts = []; |
|
| 496 | - if (!empty($configuration['eval'])) { |
|
| 497 | - $parts = GeneralUtility::trimExplode(',', $configuration['eval']); |
|
| 498 | - } |
|
| 499 | - $result = in_array('int', $parts) || in_array('float', $parts); |
|
| 500 | - } |
|
| 501 | - return $result; |
|
| 502 | - } |
|
| 503 | - |
|
| 504 | - /** |
|
| 505 | - * Returns whether the field is of type text area. |
|
| 506 | - * |
|
| 507 | - * @return bool |
|
| 508 | - */ |
|
| 509 | - public function isTextArea() |
|
| 510 | - { |
|
| 511 | - return $this->getType() === FieldType::TEXTAREA; |
|
| 512 | - } |
|
| 513 | - /** |
|
| 514 | - * Returns whether the field is of type text area. |
|
| 515 | - * |
|
| 516 | - * @return bool |
|
| 517 | - */ |
|
| 518 | - public function isText() |
|
| 519 | - { |
|
| 520 | - return $this->getType() === FieldType::TEXT; |
|
| 521 | - } |
|
| 522 | - |
|
| 523 | - /** |
|
| 524 | - * Returns whether the field is displayed as a tree. |
|
| 525 | - * |
|
| 526 | - * @return bool |
|
| 527 | - */ |
|
| 528 | - public function isRenderModeTree() |
|
| 529 | - { |
|
| 530 | - $configuration = $this->getConfiguration(); |
|
| 531 | - return isset($configuration['renderMode']) && $configuration['renderMode'] == FieldType::TREE; |
|
| 532 | - } |
|
| 533 | - |
|
| 534 | - /** |
|
| 535 | - * Returns whether the field is of type select. |
|
| 536 | - * |
|
| 537 | - * @return bool |
|
| 538 | - */ |
|
| 539 | - public function isSelect() |
|
| 540 | - { |
|
| 541 | - return $this->getType() === FieldType::SELECT; |
|
| 542 | - } |
|
| 543 | - |
|
| 544 | - /** |
|
| 545 | - * Returns whether the field is of type select. |
|
| 546 | - * |
|
| 547 | - * @return bool |
|
| 548 | - */ |
|
| 549 | - public function isMultipleSelect() |
|
| 550 | - { |
|
| 551 | - return $this->getType() === FieldType::MULTISELECT; |
|
| 552 | - } |
|
| 553 | - |
|
| 554 | - /** |
|
| 555 | - * Returns whether the field is of type select. |
|
| 556 | - * |
|
| 557 | - * @return bool |
|
| 558 | - */ |
|
| 559 | - public function isCheckBox() |
|
| 560 | - { |
|
| 561 | - return $this->getType() === FieldType::CHECKBOX; |
|
| 562 | - } |
|
| 563 | - |
|
| 564 | - /** |
|
| 565 | - * Returns whether the field is of type db. |
|
| 566 | - * |
|
| 567 | - * @return bool |
|
| 568 | - */ |
|
| 569 | - public function isGroup() |
|
| 570 | - { |
|
| 571 | - return $this->getType() === 'group'; |
|
| 572 | - } |
|
| 573 | - |
|
| 574 | - /** |
|
| 575 | - * Returns whether the field is language aware. |
|
| 576 | - * |
|
| 577 | - * @return bool |
|
| 578 | - */ |
|
| 579 | - public function isLocalized() |
|
| 580 | - { |
|
| 581 | - $isLocalized = false; |
|
| 582 | - if (isset($this->tca['l10n_mode'])) { |
|
| 583 | - if ($this->tca['l10n_mode'] == 'prefixLangTitle' || $this->tca['l10n_mode'] == 'mergeIfNotBlank') { |
|
| 584 | - $isLocalized = true; |
|
| 585 | - } |
|
| 586 | - } |
|
| 587 | - return $isLocalized; |
|
| 588 | - } |
|
| 589 | - |
|
| 590 | - /** |
|
| 591 | - * Returns whether the field is required. |
|
| 592 | - * |
|
| 593 | - * @return bool |
|
| 594 | - */ |
|
| 595 | - public function isRequired() |
|
| 596 | - { |
|
| 597 | - $configuration = $this->getConfiguration(); |
|
| 598 | - |
|
| 599 | - $isRequired = false; |
|
| 600 | - if (isset($configuration['minitems'])) { |
|
| 601 | - // is required of a select? |
|
| 602 | - $isRequired = $configuration['minitems'] == 1 ? true : false; |
|
| 603 | - } elseif (isset($configuration['eval'])) { |
|
| 604 | - $parts = GeneralUtility::trimExplode(',', $configuration['eval'], true); |
|
| 605 | - $isRequired = in_array('required', $parts); |
|
| 606 | - } |
|
| 607 | - return $isRequired; |
|
| 608 | - } |
|
| 609 | - |
|
| 610 | - /** |
|
| 611 | - * Returns an array containing the configuration of a column. |
|
| 612 | - * |
|
| 613 | - * @return array |
|
| 614 | - */ |
|
| 615 | - public function getField() |
|
| 616 | - { |
|
| 617 | - return $this->tca; |
|
| 618 | - } |
|
| 619 | - |
|
| 620 | - /** |
|
| 621 | - * Returns the relation type |
|
| 622 | - * |
|
| 623 | - * @return string |
|
| 624 | - */ |
|
| 625 | - public function relationDataType() |
|
| 626 | - { |
|
| 627 | - $configuration = $this->getConfiguration(); |
|
| 628 | - return empty($configuration['foreign_table']) ? '' : $configuration['foreign_table']; |
|
| 629 | - } |
|
| 630 | - |
|
| 631 | - /** |
|
| 632 | - * Returns whether the field has relation (one to many, many to many) |
|
| 633 | - * |
|
| 634 | - * @return bool |
|
| 635 | - */ |
|
| 636 | - public function hasRelation() |
|
| 637 | - { |
|
| 638 | - return null !== $this->getForeignTable(); |
|
| 639 | - } |
|
| 640 | - |
|
| 641 | - /** |
|
| 642 | - * Returns whether the field has no relation (one to many, many to many) |
|
| 643 | - * |
|
| 644 | - * @return bool |
|
| 645 | - */ |
|
| 646 | - public function hasNoRelation() |
|
| 647 | - { |
|
| 648 | - return !$this->hasRelation(); |
|
| 649 | - } |
|
| 650 | - |
|
| 651 | - /** |
|
| 652 | - * Returns whether the field has a "many" objects connected including "many-to-many" or "one-to-many". |
|
| 653 | - * |
|
| 654 | - * @return bool |
|
| 655 | - */ |
|
| 656 | - public function hasMany() |
|
| 657 | - { |
|
| 658 | - $configuration = $this->getConfiguration(); |
|
| 659 | - return $this->hasRelation() && ($configuration['maxitems'] > 1 || isset($configuration['foreign_table_field'])); |
|
| 660 | - } |
|
| 661 | - |
|
| 662 | - /** |
|
| 663 | - * Returns whether the field has relation "one" object connected including of "one-to-one" or "many-to-one". |
|
| 664 | - * |
|
| 665 | - * @return bool |
|
| 666 | - */ |
|
| 667 | - public function hasOne() |
|
| 668 | - { |
|
| 669 | - $configuration = $this->getConfiguration(); |
|
| 670 | - return !isset($configuration['MM']) && $this->hasRelation() && ($configuration['maxitems'] == 1 || !isset($configuration['maxitems'])); |
|
| 671 | - } |
|
| 672 | - |
|
| 673 | - /** |
|
| 674 | - * Returns whether the field has many-to-one relation. |
|
| 675 | - * |
|
| 676 | - * @return bool |
|
| 677 | - */ |
|
| 678 | - public function hasRelationManyToOne() |
|
| 679 | - { |
|
| 680 | - $result = false; |
|
| 681 | - |
|
| 682 | - $foreignField = $this->getForeignField(); |
|
| 683 | - if (!empty($foreignField)) { |
|
| 684 | - // Load TCA service of the foreign field. |
|
| 685 | - $foreignTable = $this->getForeignTable(); |
|
| 686 | - $result = $this->hasOne() && Tca::table($foreignTable)->field($foreignField)->hasMany(); |
|
| 687 | - } |
|
| 688 | - return $result; |
|
| 689 | - } |
|
| 690 | - |
|
| 691 | - /** |
|
| 692 | - * Returns whether the field has one-to-many relation. |
|
| 693 | - * |
|
| 694 | - * @return bool |
|
| 695 | - */ |
|
| 696 | - public function hasRelationOneToMany() |
|
| 697 | - { |
|
| 698 | - $result = false; |
|
| 699 | - |
|
| 700 | - $foreignField = $this->getForeignField(); |
|
| 701 | - if (!empty($foreignField)) { |
|
| 702 | - // Load TCA service of the foreign field. |
|
| 703 | - $foreignTable = $this->getForeignTable(); |
|
| 704 | - $result = $this->hasMany() && Tca::table($foreignTable)->field($foreignField)->hasOne(); |
|
| 705 | - } |
|
| 706 | - return $result; |
|
| 707 | - } |
|
| 708 | - |
|
| 709 | - /** |
|
| 710 | - * Returns whether the field has one-to-one relation. |
|
| 711 | - * |
|
| 712 | - * @return bool |
|
| 713 | - */ |
|
| 714 | - public function hasRelationOneToOne() |
|
| 715 | - { |
|
| 716 | - $result = false; |
|
| 717 | - |
|
| 718 | - $foreignField = $this->getForeignField(); |
|
| 719 | - if (!empty($foreignField)) { |
|
| 720 | - // Load TCA service of foreign field. |
|
| 721 | - $foreignTable = $this->getForeignTable(); |
|
| 722 | - $result = $this->hasOne() && Tca::table($foreignTable)->field($foreignField)->hasOne(); |
|
| 723 | - } |
|
| 724 | - return $result; |
|
| 725 | - } |
|
| 726 | - |
|
| 727 | - /** |
|
| 728 | - * Returns whether the field has many to many relation. |
|
| 729 | - * |
|
| 730 | - * @return bool |
|
| 731 | - */ |
|
| 732 | - public function hasRelationManyToMany() |
|
| 733 | - { |
|
| 734 | - $configuration = $this->getConfiguration(); |
|
| 735 | - return $this->hasRelation() && (isset($configuration['MM']) || isset($configuration['foreign_table_field'])); |
|
| 736 | - } |
|
| 737 | - |
|
| 738 | - /** |
|
| 739 | - * Returns whether the field has many to many relation using comma separated values (legacy). |
|
| 740 | - * |
|
| 741 | - * @return bool |
|
| 742 | - */ |
|
| 743 | - public function hasRelationWithCommaSeparatedValues() |
|
| 744 | - { |
|
| 745 | - $configuration = $this->getConfiguration(); |
|
| 746 | - return $this->hasRelation() && !isset($configuration['MM']) && !isset($configuration['foreign_field']) && $configuration['maxitems'] > 1; |
|
| 747 | - } |
|
| 748 | - |
|
| 749 | - /** |
|
| 750 | - * @return array |
|
| 751 | - */ |
|
| 752 | - public function getTca() |
|
| 753 | - { |
|
| 754 | - return $this->tca['columns']; |
|
| 755 | - } |
|
| 756 | - |
|
| 757 | - /** |
|
| 758 | - * @return string |
|
| 759 | - */ |
|
| 760 | - public function getCompositeField() |
|
| 761 | - { |
|
| 762 | - return $this->compositeField; |
|
| 763 | - } |
|
| 764 | - |
|
| 765 | - /** |
|
| 766 | - * @param string $compositeField |
|
| 767 | - */ |
|
| 768 | - public function setCompositeField($compositeField) |
|
| 769 | - { |
|
| 770 | - $this->compositeField = $compositeField; |
|
| 771 | - } |
|
| 772 | - |
|
| 773 | - /** |
|
| 774 | - * Returns an instance of the Frontend object. |
|
| 775 | - * |
|
| 776 | - * @return TypoScriptFrontendController |
|
| 777 | - */ |
|
| 778 | - protected function getFrontendObject() |
|
| 779 | - { |
|
| 780 | - return $GLOBALS['TSFE']; |
|
| 781 | - } |
|
| 21 | + /** |
|
| 22 | + * @var string |
|
| 23 | + */ |
|
| 24 | + protected $fieldName; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * @var string |
|
| 28 | + */ |
|
| 29 | + protected $compositeField; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @var string |
|
| 33 | + */ |
|
| 34 | + protected $tableName; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * @var array |
|
| 38 | + */ |
|
| 39 | + protected $tca; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @param string $fieldName |
|
| 43 | + * @param array $tca |
|
| 44 | + * @param string $tableName |
|
| 45 | + * @param string $compositeField |
|
| 46 | + * @return \Fab\Vidi\Tca\FieldService |
|
| 47 | + */ |
|
| 48 | + public function __construct($fieldName, array $tca, $tableName, $compositeField = '') |
|
| 49 | + { |
|
| 50 | + $this->fieldName = $fieldName; |
|
| 51 | + $this->tca = $tca; |
|
| 52 | + $this->tableName = $tableName; |
|
| 53 | + $this->compositeField = $compositeField; |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Tells whether the field is considered as system field, e.g. uid, crdate, tstamp, etc... |
|
| 58 | + * |
|
| 59 | + * @return bool |
|
| 60 | + */ |
|
| 61 | + public function isSystem() |
|
| 62 | + { |
|
| 63 | + return in_array($this->fieldName, Tca::getSystemFields()); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Tells the opposition of isSystem() |
|
| 68 | + * |
|
| 69 | + * @return bool |
|
| 70 | + */ |
|
| 71 | + public function isNotSystem() |
|
| 72 | + { |
|
| 73 | + return !$this->isSystem(); |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Returns the configuration for a $field |
|
| 78 | + * |
|
| 79 | + * @throws \Exception |
|
| 80 | + * @return array |
|
| 81 | + */ |
|
| 82 | + public function getConfiguration() |
|
| 83 | + { |
|
| 84 | + return empty($this->tca['config']) ? [] : $this->tca['config']; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Returns a key of the configuration. |
|
| 89 | + * If the key can not to be found, returns null. |
|
| 90 | + * |
|
| 91 | + * @param string $key |
|
| 92 | + * @return mixed |
|
| 93 | + */ |
|
| 94 | + public function get($key) |
|
| 95 | + { |
|
| 96 | + $configuration = $this->getConfiguration(); |
|
| 97 | + return empty($configuration[$key]) ? null : $configuration[$key]; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Returns the foreign field of a given field (opposite relational field). |
|
| 102 | + * If no relation exists, returns null. |
|
| 103 | + * |
|
| 104 | + * @return string|null |
|
| 105 | + */ |
|
| 106 | + public function getForeignField() |
|
| 107 | + { |
|
| 108 | + $result = null; |
|
| 109 | + $configuration = $this->getConfiguration(); |
|
| 110 | + |
|
| 111 | + if (!empty($configuration['foreign_field'])) { |
|
| 112 | + $result = $configuration['foreign_field']; |
|
| 113 | + } elseif ($this->hasRelationManyToMany()) { |
|
| 114 | + $foreignTable = $this->getForeignTable(); |
|
| 115 | + $manyToManyTable = $this->getManyToManyTable(); |
|
| 116 | + |
|
| 117 | + // Load TCA service of foreign field. |
|
| 118 | + $tcaForeignTableService = Tca::table($foreignTable); |
|
| 119 | + |
|
| 120 | + // Look into the MM relations checking for the opposite field |
|
| 121 | + foreach ($tcaForeignTableService->getFields() as $fieldName) { |
|
| 122 | + if ($manyToManyTable == $tcaForeignTableService->field($fieldName)->getManyToManyTable()) { |
|
| 123 | + $result = $fieldName; |
|
| 124 | + break; |
|
| 125 | + } |
|
| 126 | + } |
|
| 127 | + } |
|
| 128 | + return $result; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * Returns the foreign table of a given field (opposite relational table). |
|
| 133 | + * If no relation exists, returns null. |
|
| 134 | + * |
|
| 135 | + * @return string|null |
|
| 136 | + */ |
|
| 137 | + public function getForeignTable() |
|
| 138 | + { |
|
| 139 | + $result = null; |
|
| 140 | + $configuration = $this->getConfiguration(); |
|
| 141 | + |
|
| 142 | + if (!empty($configuration['foreign_table'])) { |
|
| 143 | + $result = $configuration['foreign_table']; |
|
| 144 | + } elseif ($this->isGroup()) { |
|
| 145 | + $fieldParts = explode('.', $this->compositeField, 2); |
|
| 146 | + $result = $fieldParts[1]; |
|
| 147 | + } |
|
| 148 | + return $result; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * Returns the foreign clause. |
|
| 153 | + * If no foreign order exists, returns empty string. |
|
| 154 | + * |
|
| 155 | + * @return string |
|
| 156 | + */ |
|
| 157 | + public function getForeignClause() |
|
| 158 | + { |
|
| 159 | + $result = ''; |
|
| 160 | + $configuration = $this->getConfiguration(); |
|
| 161 | + |
|
| 162 | + if (!empty($configuration['foreign_table_where'])) { |
|
| 163 | + $parts = explode('ORDER BY', $configuration['foreign_table_where']); |
|
| 164 | + if (!empty($parts[0])) { |
|
| 165 | + $result = $parts[0]; |
|
| 166 | + } |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + // Substitute some variables |
|
| 170 | + return $this->substituteKnownMarkers($result); |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * Substitute some known markers from the where clause in the Frontend Context. |
|
| 175 | + * |
|
| 176 | + * @param string $clause |
|
| 177 | + * @return string |
|
| 178 | + */ |
|
| 179 | + protected function substituteKnownMarkers($clause) |
|
| 180 | + { |
|
| 181 | + if ($clause && $this->isFrontendMode()) { |
|
| 182 | + $searches = array( |
|
| 183 | + '###CURRENT_PID###', |
|
| 184 | + '###REC_FIELD_sys_language_uid###' |
|
| 185 | + ); |
|
| 186 | + |
|
| 187 | + $replaces = array( |
|
| 188 | + $this->getFrontendObject()->id, |
|
| 189 | + GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('language', 'id'), |
|
| 190 | + ); |
|
| 191 | + |
|
| 192 | + $clause = str_replace($searches, $replaces, $clause); |
|
| 193 | + } |
|
| 194 | + return $clause; |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * Returns the foreign order of the current field. |
|
| 199 | + * If no foreign order exists, returns empty string. |
|
| 200 | + * |
|
| 201 | + * @return string |
|
| 202 | + */ |
|
| 203 | + public function getForeignOrder() |
|
| 204 | + { |
|
| 205 | + $result = ''; |
|
| 206 | + $configuration = $this->getConfiguration(); |
|
| 207 | + |
|
| 208 | + if (!empty($configuration['foreign_table_where'])) { |
|
| 209 | + $parts = explode('ORDER BY', $configuration['foreign_table_where']); |
|
| 210 | + if (!empty($parts[1])) { |
|
| 211 | + $result = $parts[1]; |
|
| 212 | + } |
|
| 213 | + } |
|
| 214 | + return $result; |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * Returns the MM table of a field. |
|
| 219 | + * If no relation exists, returns null. |
|
| 220 | + * |
|
| 221 | + * @return string|null |
|
| 222 | + */ |
|
| 223 | + public function getManyToManyTable() |
|
| 224 | + { |
|
| 225 | + $configuration = $this->getConfiguration(); |
|
| 226 | + return empty($configuration['MM']) ? null : $configuration['MM']; |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + /** |
|
| 230 | + * Returns a possible additional table name used in MM relations. |
|
| 231 | + * If no table name exists, returns null. |
|
| 232 | + * |
|
| 233 | + * @return string|null |
|
| 234 | + */ |
|
| 235 | + public function getAdditionalTableNameCondition() |
|
| 236 | + { |
|
| 237 | + $result = null; |
|
| 238 | + $configuration = $this->getConfiguration(); |
|
| 239 | + |
|
| 240 | + if (!empty($configuration['MM_match_fields']['tablenames'])) { |
|
| 241 | + $result = $configuration['MM_match_fields']['tablenames']; |
|
| 242 | + } elseif ($this->isGroup()) { |
|
| 243 | + // @todo check if $this->fieldName could be simply used as $result |
|
| 244 | + $fieldParts = explode('.', $this->compositeField, 2); |
|
| 245 | + $result = $fieldParts[1]; |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + return $result; |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + /** |
|
| 252 | + * Returns a possible additional conditions for MM tables such as "tablenames", "fieldname", etc... |
|
| 253 | + * |
|
| 254 | + * @return array |
|
| 255 | + */ |
|
| 256 | + public function getAdditionalMMCondition() |
|
| 257 | + { |
|
| 258 | + $additionalMMConditions = []; |
|
| 259 | + $configuration = $this->getConfiguration(); |
|
| 260 | + |
|
| 261 | + if (!empty($configuration['MM_match_fields'])) { |
|
| 262 | + $additionalMMConditions = $configuration['MM_match_fields']; |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + // Add in any case a table name for "group" |
|
| 266 | + if ($this->isGroup()) { |
|
| 267 | + // @todo check if $this->fieldName could be simply used as $result |
|
| 268 | + $fieldParts = explode('.', $this->compositeField, 2); |
|
| 269 | + $additionalMMConditions = array( |
|
| 270 | + 'tablenames' => $fieldParts[1], |
|
| 271 | + ); |
|
| 272 | + } |
|
| 273 | + return $additionalMMConditions; |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + /** |
|
| 277 | + * Returns whether the field name is the opposite in MM relation. |
|
| 278 | + * |
|
| 279 | + * @return bool |
|
| 280 | + */ |
|
| 281 | + public function isOppositeRelation() |
|
| 282 | + { |
|
| 283 | + $configuration = $this->getConfiguration(); |
|
| 284 | + return isset($configuration['MM_opposite_field']); |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + /** |
|
| 288 | + * Returns the configuration for a $field. |
|
| 289 | + * |
|
| 290 | + * @throws \Exception |
|
| 291 | + * @return string |
|
| 292 | + */ |
|
| 293 | + public function getType() |
|
| 294 | + { |
|
| 295 | + if ($this->isSystem()) { |
|
| 296 | + $fieldType = FieldType::NUMBER; |
|
| 297 | + } else { |
|
| 298 | + $configuration = $this->getConfiguration(); |
|
| 299 | + |
|
| 300 | + if (empty($configuration['type'])) { |
|
| 301 | + throw new \Exception(sprintf('No field type found for "%s" in table "%s"', $this->fieldName, $this->tableName), 1385556627); |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + $fieldType = $configuration['type']; |
|
| 305 | + |
|
| 306 | + if ($configuration['type'] === FieldType::SELECT && !empty($configuration['size']) && $configuration['size'] > 1) { |
|
| 307 | + $fieldType = FieldType::MULTISELECT; |
|
| 308 | + } elseif (!empty($configuration['foreign_table']) |
|
| 309 | + && ($configuration['foreign_table'] == 'sys_file_reference' || $configuration['foreign_table'] == 'sys_file') |
|
| 310 | + ) { |
|
| 311 | + $fieldType = FieldType::FILE; |
|
| 312 | + } elseif (!empty($configuration['eval'])) { |
|
| 313 | + $parts = GeneralUtility::trimExplode(',', $configuration['eval']); |
|
| 314 | + if (in_array('datetime', $parts)) { |
|
| 315 | + $fieldType = FieldType::DATETIME; |
|
| 316 | + } elseif (in_array('date', $parts)) { |
|
| 317 | + $fieldType = FieldType::DATE; |
|
| 318 | + } elseif (in_array('email', $parts)) { |
|
| 319 | + $fieldType = FieldType::EMAIL; |
|
| 320 | + } elseif (in_array('int', $parts) || in_array('double2', $parts)) { |
|
| 321 | + $fieldType = FieldType::NUMBER; |
|
| 322 | + } |
|
| 323 | + } |
|
| 324 | + |
|
| 325 | + // Do some legacy conversion |
|
| 326 | + if ($fieldType === 'input') { |
|
| 327 | + $fieldType = FieldType::TEXT; |
|
| 328 | + } elseif ($fieldType === 'text') { |
|
| 329 | + $fieldType = FieldType::TEXTAREA; |
|
| 330 | + } |
|
| 331 | + } |
|
| 332 | + return $fieldType; |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + /** |
|
| 336 | + * Return the default value. |
|
| 337 | + * |
|
| 338 | + * @return bool |
|
| 339 | + */ |
|
| 340 | + public function getDefaultValue() |
|
| 341 | + { |
|
| 342 | + $configuration = $this->getConfiguration(); |
|
| 343 | + return isset($configuration['default']) ? $configuration['default'] : null; |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + /** |
|
| 347 | + * Get the translation of a label given a column. |
|
| 348 | + * |
|
| 349 | + * @return string |
|
| 350 | + */ |
|
| 351 | + public function getLabel() |
|
| 352 | + { |
|
| 353 | + $label = ''; |
|
| 354 | + if ($this->hasLabel()) { |
|
| 355 | + try { |
|
| 356 | + $label = LocalizationUtility::translate($this->tca['label'], ''); |
|
| 357 | + } catch (\InvalidArgumentException $e) { |
|
| 358 | + } |
|
| 359 | + if (empty($label)) { |
|
| 360 | + $label = $this->tca['label']; |
|
| 361 | + } |
|
| 362 | + } |
|
| 363 | + return $label; |
|
| 364 | + } |
|
| 365 | + |
|
| 366 | + /** |
|
| 367 | + * Get the translation of a label given a column. |
|
| 368 | + * |
|
| 369 | + * @param string $itemValue the item value to search for. |
|
| 370 | + * @return string |
|
| 371 | + */ |
|
| 372 | + public function getLabelForItem($itemValue) |
|
| 373 | + { |
|
| 374 | + // Early return whether there is nothing to be translated as label. |
|
| 375 | + if (is_null($itemValue)) { |
|
| 376 | + return ''; |
|
| 377 | + } elseif (is_string($itemValue) && $itemValue === '') { |
|
| 378 | + return $itemValue; |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + $configuration = $this->getConfiguration(); |
|
| 382 | + if (!empty($configuration['items']) && is_array($configuration['items'])) { |
|
| 383 | + foreach ($configuration['items'] as $item) { |
|
| 384 | + if ($item[1] == $itemValue) { |
|
| 385 | + try { |
|
| 386 | + $label = LocalizationUtility::translate($item[0], ''); |
|
| 387 | + } catch (\InvalidArgumentException $e) { |
|
| 388 | + } |
|
| 389 | + if (empty($label)) { |
|
| 390 | + $label = $item[0]; |
|
| 391 | + } |
|
| 392 | + break; |
|
| 393 | + } |
|
| 394 | + } |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + // Try fetching a label from a possible itemsProcFunc |
|
| 398 | + if (!isset($label) && is_scalar($itemValue)) { |
|
| 399 | + $items = $this->fetchItemsFromUserFunction(); |
|
| 400 | + if (!empty($items[$itemValue])) { |
|
| 401 | + $label = $items[$itemValue]; |
|
| 402 | + } |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + // Returns a label if it has been found, otherwise returns the item value as fallback. |
|
| 406 | + return isset($label) ? $label : $itemValue; |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + /** |
|
| 410 | + * Retrieve items from User Function. |
|
| 411 | + * |
|
| 412 | + * @return array |
|
| 413 | + */ |
|
| 414 | + protected function fetchItemsFromUserFunction() |
|
| 415 | + { |
|
| 416 | + $values = []; |
|
| 417 | + |
|
| 418 | + $configuration = $this->getConfiguration(); |
|
| 419 | + if (!empty($configuration['itemsProcFunc'])) { |
|
| 420 | + $parts = explode('php:', $configuration['itemsProcFunc']); |
|
| 421 | + if (!empty($parts[1])) { |
|
| 422 | + list($class, $method) = explode('->', $parts[1]); |
|
| 423 | + |
|
| 424 | + $parameters['items'] = []; |
|
| 425 | + $object = GeneralUtility::makeInstance($class); |
|
| 426 | + $object->$method($parameters); |
|
| 427 | + |
|
| 428 | + foreach ($parameters['items'] as $items) { |
|
| 429 | + $values[$items[1]] = $items[0]; |
|
| 430 | + } |
|
| 431 | + } |
|
| 432 | + } |
|
| 433 | + return $values; |
|
| 434 | + } |
|
| 435 | + |
|
| 436 | + /** |
|
| 437 | + * Get a possible icon given a field name an an item. |
|
| 438 | + * |
|
| 439 | + * @param string $itemValue the item value to search for. |
|
| 440 | + * @return string |
|
| 441 | + */ |
|
| 442 | + public function getIconForItem($itemValue) |
|
| 443 | + { |
|
| 444 | + $result = ''; |
|
| 445 | + $configuration = $this->getConfiguration(); |
|
| 446 | + if (!empty($configuration['items']) && is_array($configuration['items'])) { |
|
| 447 | + foreach ($configuration['items'] as $item) { |
|
| 448 | + if ($item[1] == $itemValue) { |
|
| 449 | + $result = empty($item[2]) ? '' : $item[2]; |
|
| 450 | + break; |
|
| 451 | + } |
|
| 452 | + } |
|
| 453 | + } |
|
| 454 | + return $result; |
|
| 455 | + } |
|
| 456 | + |
|
| 457 | + /** |
|
| 458 | + * Returns whether the field has a label. |
|
| 459 | + * |
|
| 460 | + * @return bool |
|
| 461 | + */ |
|
| 462 | + public function hasLabel() |
|
| 463 | + { |
|
| 464 | + return empty($this->tca['label']) ? false : true; |
|
| 465 | + } |
|
| 466 | + |
|
| 467 | + /** |
|
| 468 | + * Tell whether the current BE User has access to this field. |
|
| 469 | + * |
|
| 470 | + * @return bool |
|
| 471 | + */ |
|
| 472 | + public function hasAccess() |
|
| 473 | + { |
|
| 474 | + $hasAccess = true; |
|
| 475 | + if ($this->isBackendMode() |
|
| 476 | + && Tca::table($this->tableName)->hasAccess() |
|
| 477 | + && isset($this->tca['exclude']) |
|
| 478 | + && $this->tca['exclude'] |
|
| 479 | + ) { |
|
| 480 | + $hasAccess = $this->getBackendUser()->check('non_exclude_fields', $this->tableName . ':' . $this->fieldName); |
|
| 481 | + } |
|
| 482 | + return $hasAccess; |
|
| 483 | + } |
|
| 484 | + |
|
| 485 | + /** |
|
| 486 | + * Returns whether the field is numerical. |
|
| 487 | + * |
|
| 488 | + * @return bool |
|
| 489 | + */ |
|
| 490 | + public function isNumerical() |
|
| 491 | + { |
|
| 492 | + $result = $this->isSystem(); |
|
| 493 | + if ($result === false) { |
|
| 494 | + $configuration = $this->getConfiguration(); |
|
| 495 | + $parts = []; |
|
| 496 | + if (!empty($configuration['eval'])) { |
|
| 497 | + $parts = GeneralUtility::trimExplode(',', $configuration['eval']); |
|
| 498 | + } |
|
| 499 | + $result = in_array('int', $parts) || in_array('float', $parts); |
|
| 500 | + } |
|
| 501 | + return $result; |
|
| 502 | + } |
|
| 503 | + |
|
| 504 | + /** |
|
| 505 | + * Returns whether the field is of type text area. |
|
| 506 | + * |
|
| 507 | + * @return bool |
|
| 508 | + */ |
|
| 509 | + public function isTextArea() |
|
| 510 | + { |
|
| 511 | + return $this->getType() === FieldType::TEXTAREA; |
|
| 512 | + } |
|
| 513 | + /** |
|
| 514 | + * Returns whether the field is of type text area. |
|
| 515 | + * |
|
| 516 | + * @return bool |
|
| 517 | + */ |
|
| 518 | + public function isText() |
|
| 519 | + { |
|
| 520 | + return $this->getType() === FieldType::TEXT; |
|
| 521 | + } |
|
| 522 | + |
|
| 523 | + /** |
|
| 524 | + * Returns whether the field is displayed as a tree. |
|
| 525 | + * |
|
| 526 | + * @return bool |
|
| 527 | + */ |
|
| 528 | + public function isRenderModeTree() |
|
| 529 | + { |
|
| 530 | + $configuration = $this->getConfiguration(); |
|
| 531 | + return isset($configuration['renderMode']) && $configuration['renderMode'] == FieldType::TREE; |
|
| 532 | + } |
|
| 533 | + |
|
| 534 | + /** |
|
| 535 | + * Returns whether the field is of type select. |
|
| 536 | + * |
|
| 537 | + * @return bool |
|
| 538 | + */ |
|
| 539 | + public function isSelect() |
|
| 540 | + { |
|
| 541 | + return $this->getType() === FieldType::SELECT; |
|
| 542 | + } |
|
| 543 | + |
|
| 544 | + /** |
|
| 545 | + * Returns whether the field is of type select. |
|
| 546 | + * |
|
| 547 | + * @return bool |
|
| 548 | + */ |
|
| 549 | + public function isMultipleSelect() |
|
| 550 | + { |
|
| 551 | + return $this->getType() === FieldType::MULTISELECT; |
|
| 552 | + } |
|
| 553 | + |
|
| 554 | + /** |
|
| 555 | + * Returns whether the field is of type select. |
|
| 556 | + * |
|
| 557 | + * @return bool |
|
| 558 | + */ |
|
| 559 | + public function isCheckBox() |
|
| 560 | + { |
|
| 561 | + return $this->getType() === FieldType::CHECKBOX; |
|
| 562 | + } |
|
| 563 | + |
|
| 564 | + /** |
|
| 565 | + * Returns whether the field is of type db. |
|
| 566 | + * |
|
| 567 | + * @return bool |
|
| 568 | + */ |
|
| 569 | + public function isGroup() |
|
| 570 | + { |
|
| 571 | + return $this->getType() === 'group'; |
|
| 572 | + } |
|
| 573 | + |
|
| 574 | + /** |
|
| 575 | + * Returns whether the field is language aware. |
|
| 576 | + * |
|
| 577 | + * @return bool |
|
| 578 | + */ |
|
| 579 | + public function isLocalized() |
|
| 580 | + { |
|
| 581 | + $isLocalized = false; |
|
| 582 | + if (isset($this->tca['l10n_mode'])) { |
|
| 583 | + if ($this->tca['l10n_mode'] == 'prefixLangTitle' || $this->tca['l10n_mode'] == 'mergeIfNotBlank') { |
|
| 584 | + $isLocalized = true; |
|
| 585 | + } |
|
| 586 | + } |
|
| 587 | + return $isLocalized; |
|
| 588 | + } |
|
| 589 | + |
|
| 590 | + /** |
|
| 591 | + * Returns whether the field is required. |
|
| 592 | + * |
|
| 593 | + * @return bool |
|
| 594 | + */ |
|
| 595 | + public function isRequired() |
|
| 596 | + { |
|
| 597 | + $configuration = $this->getConfiguration(); |
|
| 598 | + |
|
| 599 | + $isRequired = false; |
|
| 600 | + if (isset($configuration['minitems'])) { |
|
| 601 | + // is required of a select? |
|
| 602 | + $isRequired = $configuration['minitems'] == 1 ? true : false; |
|
| 603 | + } elseif (isset($configuration['eval'])) { |
|
| 604 | + $parts = GeneralUtility::trimExplode(',', $configuration['eval'], true); |
|
| 605 | + $isRequired = in_array('required', $parts); |
|
| 606 | + } |
|
| 607 | + return $isRequired; |
|
| 608 | + } |
|
| 609 | + |
|
| 610 | + /** |
|
| 611 | + * Returns an array containing the configuration of a column. |
|
| 612 | + * |
|
| 613 | + * @return array |
|
| 614 | + */ |
|
| 615 | + public function getField() |
|
| 616 | + { |
|
| 617 | + return $this->tca; |
|
| 618 | + } |
|
| 619 | + |
|
| 620 | + /** |
|
| 621 | + * Returns the relation type |
|
| 622 | + * |
|
| 623 | + * @return string |
|
| 624 | + */ |
|
| 625 | + public function relationDataType() |
|
| 626 | + { |
|
| 627 | + $configuration = $this->getConfiguration(); |
|
| 628 | + return empty($configuration['foreign_table']) ? '' : $configuration['foreign_table']; |
|
| 629 | + } |
|
| 630 | + |
|
| 631 | + /** |
|
| 632 | + * Returns whether the field has relation (one to many, many to many) |
|
| 633 | + * |
|
| 634 | + * @return bool |
|
| 635 | + */ |
|
| 636 | + public function hasRelation() |
|
| 637 | + { |
|
| 638 | + return null !== $this->getForeignTable(); |
|
| 639 | + } |
|
| 640 | + |
|
| 641 | + /** |
|
| 642 | + * Returns whether the field has no relation (one to many, many to many) |
|
| 643 | + * |
|
| 644 | + * @return bool |
|
| 645 | + */ |
|
| 646 | + public function hasNoRelation() |
|
| 647 | + { |
|
| 648 | + return !$this->hasRelation(); |
|
| 649 | + } |
|
| 650 | + |
|
| 651 | + /** |
|
| 652 | + * Returns whether the field has a "many" objects connected including "many-to-many" or "one-to-many". |
|
| 653 | + * |
|
| 654 | + * @return bool |
|
| 655 | + */ |
|
| 656 | + public function hasMany() |
|
| 657 | + { |
|
| 658 | + $configuration = $this->getConfiguration(); |
|
| 659 | + return $this->hasRelation() && ($configuration['maxitems'] > 1 || isset($configuration['foreign_table_field'])); |
|
| 660 | + } |
|
| 661 | + |
|
| 662 | + /** |
|
| 663 | + * Returns whether the field has relation "one" object connected including of "one-to-one" or "many-to-one". |
|
| 664 | + * |
|
| 665 | + * @return bool |
|
| 666 | + */ |
|
| 667 | + public function hasOne() |
|
| 668 | + { |
|
| 669 | + $configuration = $this->getConfiguration(); |
|
| 670 | + return !isset($configuration['MM']) && $this->hasRelation() && ($configuration['maxitems'] == 1 || !isset($configuration['maxitems'])); |
|
| 671 | + } |
|
| 672 | + |
|
| 673 | + /** |
|
| 674 | + * Returns whether the field has many-to-one relation. |
|
| 675 | + * |
|
| 676 | + * @return bool |
|
| 677 | + */ |
|
| 678 | + public function hasRelationManyToOne() |
|
| 679 | + { |
|
| 680 | + $result = false; |
|
| 681 | + |
|
| 682 | + $foreignField = $this->getForeignField(); |
|
| 683 | + if (!empty($foreignField)) { |
|
| 684 | + // Load TCA service of the foreign field. |
|
| 685 | + $foreignTable = $this->getForeignTable(); |
|
| 686 | + $result = $this->hasOne() && Tca::table($foreignTable)->field($foreignField)->hasMany(); |
|
| 687 | + } |
|
| 688 | + return $result; |
|
| 689 | + } |
|
| 690 | + |
|
| 691 | + /** |
|
| 692 | + * Returns whether the field has one-to-many relation. |
|
| 693 | + * |
|
| 694 | + * @return bool |
|
| 695 | + */ |
|
| 696 | + public function hasRelationOneToMany() |
|
| 697 | + { |
|
| 698 | + $result = false; |
|
| 699 | + |
|
| 700 | + $foreignField = $this->getForeignField(); |
|
| 701 | + if (!empty($foreignField)) { |
|
| 702 | + // Load TCA service of the foreign field. |
|
| 703 | + $foreignTable = $this->getForeignTable(); |
|
| 704 | + $result = $this->hasMany() && Tca::table($foreignTable)->field($foreignField)->hasOne(); |
|
| 705 | + } |
|
| 706 | + return $result; |
|
| 707 | + } |
|
| 708 | + |
|
| 709 | + /** |
|
| 710 | + * Returns whether the field has one-to-one relation. |
|
| 711 | + * |
|
| 712 | + * @return bool |
|
| 713 | + */ |
|
| 714 | + public function hasRelationOneToOne() |
|
| 715 | + { |
|
| 716 | + $result = false; |
|
| 717 | + |
|
| 718 | + $foreignField = $this->getForeignField(); |
|
| 719 | + if (!empty($foreignField)) { |
|
| 720 | + // Load TCA service of foreign field. |
|
| 721 | + $foreignTable = $this->getForeignTable(); |
|
| 722 | + $result = $this->hasOne() && Tca::table($foreignTable)->field($foreignField)->hasOne(); |
|
| 723 | + } |
|
| 724 | + return $result; |
|
| 725 | + } |
|
| 726 | + |
|
| 727 | + /** |
|
| 728 | + * Returns whether the field has many to many relation. |
|
| 729 | + * |
|
| 730 | + * @return bool |
|
| 731 | + */ |
|
| 732 | + public function hasRelationManyToMany() |
|
| 733 | + { |
|
| 734 | + $configuration = $this->getConfiguration(); |
|
| 735 | + return $this->hasRelation() && (isset($configuration['MM']) || isset($configuration['foreign_table_field'])); |
|
| 736 | + } |
|
| 737 | + |
|
| 738 | + /** |
|
| 739 | + * Returns whether the field has many to many relation using comma separated values (legacy). |
|
| 740 | + * |
|
| 741 | + * @return bool |
|
| 742 | + */ |
|
| 743 | + public function hasRelationWithCommaSeparatedValues() |
|
| 744 | + { |
|
| 745 | + $configuration = $this->getConfiguration(); |
|
| 746 | + return $this->hasRelation() && !isset($configuration['MM']) && !isset($configuration['foreign_field']) && $configuration['maxitems'] > 1; |
|
| 747 | + } |
|
| 748 | + |
|
| 749 | + /** |
|
| 750 | + * @return array |
|
| 751 | + */ |
|
| 752 | + public function getTca() |
|
| 753 | + { |
|
| 754 | + return $this->tca['columns']; |
|
| 755 | + } |
|
| 756 | + |
|
| 757 | + /** |
|
| 758 | + * @return string |
|
| 759 | + */ |
|
| 760 | + public function getCompositeField() |
|
| 761 | + { |
|
| 762 | + return $this->compositeField; |
|
| 763 | + } |
|
| 764 | + |
|
| 765 | + /** |
|
| 766 | + * @param string $compositeField |
|
| 767 | + */ |
|
| 768 | + public function setCompositeField($compositeField) |
|
| 769 | + { |
|
| 770 | + $this->compositeField = $compositeField; |
|
| 771 | + } |
|
| 772 | + |
|
| 773 | + /** |
|
| 774 | + * Returns an instance of the Frontend object. |
|
| 775 | + * |
|
| 776 | + * @return TypoScriptFrontendController |
|
| 777 | + */ |
|
| 778 | + protected function getFrontendObject() |
|
| 779 | + { |
|
| 780 | + return $GLOBALS['TSFE']; |
|
| 781 | + } |
|
| 782 | 782 | } |
@@ -23,695 +23,695 @@ |
||
| 23 | 23 | */ |
| 24 | 24 | class GridService extends AbstractTca |
| 25 | 25 | { |
| 26 | - /** |
|
| 27 | - * @var array |
|
| 28 | - */ |
|
| 29 | - protected $tca; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * @var string |
|
| 33 | - */ |
|
| 34 | - protected $tableName; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * All fields available in the Grid. |
|
| 38 | - * |
|
| 39 | - * @var array |
|
| 40 | - */ |
|
| 41 | - protected $fields; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * All fields regardless whether they have been excluded or not. |
|
| 45 | - * |
|
| 46 | - * @var array |
|
| 47 | - */ |
|
| 48 | - protected $allFields; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @var array |
|
| 52 | - */ |
|
| 53 | - protected $instances; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * @var array |
|
| 57 | - */ |
|
| 58 | - protected $facets; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * __construct |
|
| 62 | - * |
|
| 63 | - * @param string $tableName |
|
| 64 | - */ |
|
| 65 | - public function __construct($tableName) |
|
| 66 | - { |
|
| 67 | - $this->tableName = $tableName; |
|
| 68 | - |
|
| 69 | - if (empty($GLOBALS['TCA'][$this->tableName])) { |
|
| 70 | - throw new InvalidKeyInArrayException('No TCA existence for table name: ' . $this->tableName, 1356945108); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - $this->tca = $GLOBALS['TCA'][$this->tableName]['grid'] ?? []; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Returns an array containing column names. |
|
| 78 | - * |
|
| 79 | - * @return array |
|
| 80 | - */ |
|
| 81 | - public function getFieldNames(): array |
|
| 82 | - { |
|
| 83 | - $fields = $this->getFields(); |
|
| 84 | - return array_keys($fields) ?: []; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Returns an array containing column names. |
|
| 89 | - * |
|
| 90 | - * @return array |
|
| 91 | - */ |
|
| 92 | - public function getAllFieldNames(): array |
|
| 93 | - { |
|
| 94 | - $allFields = $this->getAllFields(); |
|
| 95 | - return array_keys($allFields); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Get the label key. |
|
| 100 | - * |
|
| 101 | - * @param string $fieldNameAndPath |
|
| 102 | - * @return string |
|
| 103 | - */ |
|
| 104 | - public function getLabelKey($fieldNameAndPath): string |
|
| 105 | - { |
|
| 106 | - $field = $this->getField($fieldNameAndPath); |
|
| 107 | - |
|
| 108 | - // First option is to get the label from the Grid TCA. |
|
| 109 | - $rawLabel = ''; |
|
| 110 | - if (isset($field['label'])) { |
|
| 111 | - $rawLabel = $field['label']; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - // Second option is to fetch the label from the Column Renderer object. |
|
| 115 | - if (!$rawLabel && $this->hasRenderers($fieldNameAndPath)) { |
|
| 116 | - $renderers = $this->getRenderers($fieldNameAndPath); |
|
| 117 | - /** @var $renderer ColumnRendererInterface */ |
|
| 118 | - foreach ($renderers as $renderer) { |
|
| 119 | - if (isset($renderer['label'])) { |
|
| 120 | - $rawLabel = $renderer['label']; |
|
| 121 | - break; |
|
| 122 | - } |
|
| 123 | - } |
|
| 124 | - } |
|
| 125 | - return $rawLabel; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Get the translation of a label given a column name. |
|
| 130 | - * |
|
| 131 | - * @param string $fieldNameAndPath |
|
| 132 | - * @return string |
|
| 133 | - */ |
|
| 134 | - public function getLabel($fieldNameAndPath) |
|
| 135 | - { |
|
| 136 | - $label = ''; |
|
| 137 | - if ($this->hasLabel($fieldNameAndPath)) { |
|
| 138 | - $labelKey = $this->getLabelKey($fieldNameAndPath); |
|
| 139 | - try { |
|
| 140 | - $label = $this->getLanguageService()->sL($labelKey); |
|
| 141 | - } catch (\InvalidArgumentException $e) { |
|
| 142 | - } |
|
| 143 | - if (empty($label)) { |
|
| 144 | - $label = $labelKey; |
|
| 145 | - } |
|
| 146 | - } else { |
|
| 147 | - // Important to notice the label can contains a path, e.g. metadata.categories and must be resolved. |
|
| 148 | - $dataType = $this->getFieldPathResolver()->getDataType($fieldNameAndPath, $this->tableName); |
|
| 149 | - $fieldName = $this->getFieldPathResolver()->stripFieldPath($fieldNameAndPath, $this->tableName); |
|
| 150 | - $table = Tca::table($dataType); |
|
| 151 | - |
|
| 152 | - if ($table->hasField($fieldName) && $table->field($fieldName)->hasLabel()) { |
|
| 153 | - $label = $table->field($fieldName)->getLabel(); |
|
| 154 | - } |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - return $label; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * Returns the field name given its position. |
|
| 162 | - * |
|
| 163 | - * @param string $position the position of the field in the grid |
|
| 164 | - * @return string |
|
| 165 | - */ |
|
| 166 | - public function getFieldNameByPosition($position): string |
|
| 167 | - { |
|
| 168 | - $fields = array_keys($this->getFields()); |
|
| 169 | - if (empty($fields[$position])) { |
|
| 170 | - throw new InvalidKeyInArrayException('No field exist for position: ' . $position, 1356945119); |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - return (string)$fields[$position]; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * Returns a field name. |
|
| 178 | - * |
|
| 179 | - * @param string $fieldName |
|
| 180 | - * @return array |
|
| 181 | - */ |
|
| 182 | - public function getField($fieldName): array |
|
| 183 | - { |
|
| 184 | - $fields = $this->getFields(); |
|
| 185 | - return $fields[$fieldName] ?: []; |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * Returns an array containing column names for the Grid. |
|
| 190 | - * |
|
| 191 | - * @return array |
|
| 192 | - */ |
|
| 193 | - public function getFields(): array |
|
| 194 | - { |
|
| 195 | - // Cache this operation since it can take some time. |
|
| 196 | - if ($this->fields === null) { |
|
| 197 | - // Fetch all available fields first. |
|
| 198 | - $fields = $this->getAllFields(); |
|
| 199 | - |
|
| 200 | - if ($this->isBackendMode()) { |
|
| 201 | - // Then remove the not allowed. |
|
| 202 | - $fields = $this->filterByIncludedFields($fields); |
|
| 203 | - $fields = $this->filterByBackendUser($fields); |
|
| 204 | - $fields = $this->filterByExcludedFields($fields); |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - $this->fields = $fields; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - return $this->fields; |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - /** |
|
| 214 | - * Remove fields according to Grid configuration. |
|
| 215 | - * |
|
| 216 | - * @param $fields |
|
| 217 | - * @return array |
|
| 218 | - */ |
|
| 219 | - protected function filterByIncludedFields($fields): array |
|
| 220 | - { |
|
| 221 | - $filteredFields = $fields; |
|
| 222 | - $includedFields = $this->getIncludedFields(); |
|
| 223 | - if (count($includedFields) > 0) { |
|
| 224 | - $filteredFields = []; |
|
| 225 | - foreach ($fields as $fieldNameAndPath => $configuration) { |
|
| 226 | - if (in_array($fieldNameAndPath, $includedFields, true) || !Tca::table($this->tableName)->hasField($fieldNameAndPath)) { |
|
| 227 | - $filteredFields[$fieldNameAndPath] = $configuration; |
|
| 228 | - } |
|
| 229 | - } |
|
| 230 | - } |
|
| 231 | - return $filteredFields; |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - /** |
|
| 235 | - * Remove fields according to BE User permission. |
|
| 236 | - * |
|
| 237 | - * @param $fields |
|
| 238 | - * @return array |
|
| 239 | - */ |
|
| 240 | - protected function filterByBackendUser($fields): array |
|
| 241 | - { |
|
| 242 | - if (!$this->getBackendUser()->isAdmin()) { |
|
| 243 | - foreach ($fields as $fieldName => $field) { |
|
| 244 | - if (Tca::table($this->tableName)->hasField($fieldName) && !Tca::table($this->tableName)->field($fieldName)->hasAccess()) { |
|
| 245 | - unset($fields[$fieldName]); |
|
| 246 | - } |
|
| 247 | - } |
|
| 248 | - } |
|
| 249 | - return $fields; |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - /** |
|
| 253 | - * Remove fields according to Grid configuration. |
|
| 254 | - * |
|
| 255 | - * @param $fields |
|
| 256 | - * @return array |
|
| 257 | - */ |
|
| 258 | - protected function filterByExcludedFields($fields): array |
|
| 259 | - { |
|
| 260 | - // Unset excluded fields. |
|
| 261 | - foreach ($this->getExcludedFields() as $excludedField) { |
|
| 262 | - if (isset($fields[$excludedField])) { |
|
| 263 | - unset($fields[$excludedField]); |
|
| 264 | - } |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - return $fields; |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - /** |
|
| 271 | - * Returns an array containing column names for the Grid. |
|
| 272 | - * |
|
| 273 | - * @return array |
|
| 274 | - */ |
|
| 275 | - public function getAllFields(): array |
|
| 276 | - { |
|
| 277 | - // Cache this operation since it can take some time. |
|
| 278 | - if ($this->allFields === null) { |
|
| 279 | - $fields = isset($this->tca['columns']) && is_array($this->tca['columns']) ? $this->tca['columns'] : []; |
|
| 280 | - $gridFieldNames = array_keys($fields); |
|
| 281 | - |
|
| 282 | - // Fetch all fields of the TCA and merge it back to the fields configured for Grid. |
|
| 283 | - $tableFieldNames = Tca::table($this->tableName)->getFields(); |
|
| 284 | - |
|
| 285 | - // Just remove system fields from the Grid. |
|
| 286 | - foreach ($tableFieldNames as $key => $fieldName) { |
|
| 287 | - if (in_array($fieldName, Tca::getSystemFields())) { |
|
| 288 | - unset($tableFieldNames[$key]); |
|
| 289 | - } |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - $additionalFields = array_diff($tableFieldNames, $gridFieldNames); |
|
| 293 | - |
|
| 294 | - if (!empty($additionalFields)) { |
|
| 295 | - // Pop out last element of the key |
|
| 296 | - // Idea is to place new un-configured columns in between. By default, they will be hidden. |
|
| 297 | - end($fields); |
|
| 298 | - $lastColumnKey = key($fields); |
|
| 299 | - $lastColumn = array_pop($fields); |
|
| 300 | - |
|
| 301 | - // Feed up the grid fields with un configured elements |
|
| 302 | - foreach ($additionalFields as $additionalField) { |
|
| 303 | - $fields[$additionalField] = array( |
|
| 304 | - 'visible' => false |
|
| 305 | - ); |
|
| 306 | - |
|
| 307 | - // Try to guess the format of the field. |
|
| 308 | - $fieldType = Tca::table($this->tableName)->field($additionalField)->getType(); |
|
| 309 | - if ($fieldType === FieldType::DATE) { |
|
| 310 | - $fields[$additionalField]['format'] = 'Fab\Vidi\Formatter\Date'; |
|
| 311 | - } elseif ($fieldType === FieldType::DATETIME) { |
|
| 312 | - $fields[$additionalField]['format'] = 'Fab\Vidi\Formatter\Datetime'; |
|
| 313 | - } |
|
| 314 | - } |
|
| 315 | - $fields[$lastColumnKey] = $lastColumn; |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - $this->allFields = $fields; |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - return $this->allFields; |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - /** |
|
| 325 | - * Tell whether the field exists in the grid or not. |
|
| 326 | - * |
|
| 327 | - * @param string $fieldName |
|
| 328 | - * @return bool |
|
| 329 | - */ |
|
| 330 | - public function hasField($fieldName): bool |
|
| 331 | - { |
|
| 332 | - $fields = $this->getFields(); |
|
| 333 | - return isset($fields[$fieldName]); |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - /** |
|
| 337 | - * Tell whether the facet exists in the grid or not. |
|
| 338 | - * |
|
| 339 | - * @param string $facetName |
|
| 340 | - * @return bool |
|
| 341 | - */ |
|
| 342 | - public function hasFacet($facetName): bool |
|
| 343 | - { |
|
| 344 | - $facets = $this->getFacets(); |
|
| 345 | - return isset($facets[$facetName]); |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - /** |
|
| 349 | - * Returns an array containing facets fields. |
|
| 350 | - * |
|
| 351 | - * @return FacetInterface[] |
|
| 352 | - */ |
|
| 353 | - public function getFacets(): array |
|
| 354 | - { |
|
| 355 | - if ($this->facets === null) { |
|
| 356 | - $this->facets = []; |
|
| 357 | - |
|
| 358 | - if (is_array($this->tca['facets'])) { |
|
| 359 | - foreach ($this->tca['facets'] as $key => $facetNameOrArray) { |
|
| 360 | - if (is_array($facetNameOrArray)) { |
|
| 361 | - $name = $facetNameOrArray['name'] ?? ''; |
|
| 362 | - |
|
| 363 | - $label = isset($facetNameOrArray['label']) |
|
| 364 | - ? $this->getLanguageService()->sL($facetNameOrArray['label']) |
|
| 365 | - : ''; |
|
| 366 | - |
|
| 367 | - $suggestions = $facetNameOrArray['suggestions'] ?? []; |
|
| 368 | - $configuration = $facetNameOrArray['configuration'] ?? []; |
|
| 369 | - |
|
| 370 | - /** @var FacetInterface $facetObject */ |
|
| 371 | - $facetObject = GeneralUtility::makeInstance($key, $name, $label, $suggestions, $configuration); |
|
| 372 | - $this->facets[$facetObject->getName()] = $facetObject; |
|
| 373 | - } else { |
|
| 374 | - $this->facets[$facetNameOrArray] = $this->instantiateStandardFacet($facetNameOrArray); |
|
| 375 | - } |
|
| 376 | - } |
|
| 377 | - } |
|
| 378 | - } |
|
| 379 | - return $this->facets; |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - /** |
|
| 383 | - * Returns the "sortable" value of the column. |
|
| 384 | - * |
|
| 385 | - * @param string $fieldName |
|
| 386 | - * @return int|string |
|
| 387 | - */ |
|
| 388 | - public function isSortable($fieldName) |
|
| 389 | - { |
|
| 390 | - $defaultValue = true; |
|
| 391 | - $hasSortableField = Tca::table($this->tableName)->hasSortableField(); |
|
| 392 | - if ($hasSortableField) { |
|
| 393 | - $isSortable = false; |
|
| 394 | - } else { |
|
| 395 | - $isSortable = $this->get($fieldName, 'sortable', $defaultValue); |
|
| 396 | - } |
|
| 397 | - return $isSortable; |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - /** |
|
| 401 | - * Returns the "canBeHidden" value of the column. |
|
| 402 | - * |
|
| 403 | - * @param string $fieldName |
|
| 404 | - * @return bool |
|
| 405 | - */ |
|
| 406 | - public function canBeHidden($fieldName): bool |
|
| 407 | - { |
|
| 408 | - $defaultValue = true; |
|
| 409 | - return $this->get($fieldName, 'canBeHidden', $defaultValue); |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - /** |
|
| 413 | - * Returns the "width" value of the column. |
|
| 414 | - * |
|
| 415 | - * @param string $fieldName |
|
| 416 | - * @return int|string |
|
| 417 | - */ |
|
| 418 | - public function getWidth($fieldName) |
|
| 419 | - { |
|
| 420 | - $defaultValue = 'auto'; |
|
| 421 | - return $this->get($fieldName, 'width', $defaultValue); |
|
| 422 | - } |
|
| 423 | - |
|
| 424 | - /** |
|
| 425 | - * Returns the "visible" value of the column. |
|
| 426 | - * |
|
| 427 | - * @param string $fieldName |
|
| 428 | - * @return bool |
|
| 429 | - */ |
|
| 430 | - public function isVisible($fieldName): bool |
|
| 431 | - { |
|
| 432 | - $defaultValue = true; |
|
| 433 | - return $this->get($fieldName, 'visible', $defaultValue); |
|
| 434 | - } |
|
| 435 | - |
|
| 436 | - /** |
|
| 437 | - * Returns the "editable" value of the column. |
|
| 438 | - * |
|
| 439 | - * @param string $columnName |
|
| 440 | - * @return bool |
|
| 441 | - */ |
|
| 442 | - public function isEditable($columnName): bool |
|
| 443 | - { |
|
| 444 | - $defaultValue = false; |
|
| 445 | - return $this->get($columnName, 'editable', $defaultValue); |
|
| 446 | - } |
|
| 447 | - |
|
| 448 | - /** |
|
| 449 | - * Returns the "localized" value of the column. |
|
| 450 | - * |
|
| 451 | - * @param string $columnName |
|
| 452 | - * @return bool |
|
| 453 | - */ |
|
| 454 | - public function isLocalized($columnName): bool |
|
| 455 | - { |
|
| 456 | - $defaultValue = true; |
|
| 457 | - return $this->get($columnName, 'localized', $defaultValue); |
|
| 458 | - } |
|
| 459 | - |
|
| 460 | - /** |
|
| 461 | - * |
|
| 462 | - * Returns the "html" value of the column. |
|
| 463 | - * |
|
| 464 | - * @param string $fieldName |
|
| 465 | - * @return string |
|
| 466 | - */ |
|
| 467 | - public function getHeader($fieldName): string |
|
| 468 | - { |
|
| 469 | - $defaultValue = ''; |
|
| 470 | - return $this->get($fieldName, 'html', $defaultValue); |
|
| 471 | - } |
|
| 472 | - |
|
| 473 | - /** |
|
| 474 | - * Fetch a possible from a Grid Renderer. If no value is found, returns null |
|
| 475 | - * |
|
| 476 | - * @param string $fieldName |
|
| 477 | - * @param string $key |
|
| 478 | - * @param mixed $defaultValue |
|
| 479 | - * @return null|mixed |
|
| 480 | - */ |
|
| 481 | - public function get($fieldName, $key, $defaultValue = null) |
|
| 482 | - { |
|
| 483 | - $value = $defaultValue; |
|
| 484 | - |
|
| 485 | - $field = $this->getField($fieldName); |
|
| 486 | - if (isset($field[$key])) { |
|
| 487 | - $value = $field[$key]; |
|
| 488 | - } elseif ($this->hasRenderers($fieldName)) { |
|
| 489 | - $renderers = $this->getRenderers($fieldName); |
|
| 490 | - foreach ($renderers as $rendererConfiguration) { |
|
| 491 | - if (isset($rendererConfiguration[$key])) { |
|
| 492 | - $value = $rendererConfiguration[$key]; |
|
| 493 | - } |
|
| 494 | - } |
|
| 495 | - } |
|
| 496 | - return $value; |
|
| 497 | - } |
|
| 498 | - |
|
| 499 | - /** |
|
| 500 | - * Returns whether the column has a renderer. |
|
| 501 | - * |
|
| 502 | - * @param string $fieldName |
|
| 503 | - * @return bool |
|
| 504 | - */ |
|
| 505 | - public function hasRenderers($fieldName): bool |
|
| 506 | - { |
|
| 507 | - $field = $this->getField($fieldName); |
|
| 508 | - return empty($field['renderer']) && empty($field['renderers']) ? false : true; |
|
| 509 | - } |
|
| 510 | - |
|
| 511 | - /** |
|
| 512 | - * Returns a renderer. |
|
| 513 | - * |
|
| 514 | - * @param string $fieldName |
|
| 515 | - * @return array |
|
| 516 | - */ |
|
| 517 | - public function getRenderers($fieldName): array |
|
| 518 | - { |
|
| 519 | - $field = $this->getField($fieldName); |
|
| 520 | - $renderers = []; |
|
| 521 | - if (!empty($field['renderer'])) { |
|
| 522 | - $renderers = $this->convertRendererToArray($field['renderer'], $field); |
|
| 523 | - } elseif (!empty($field['renderers']) && is_array($field['renderers'])) { |
|
| 524 | - foreach ($field['renderers'] as $renderer) { |
|
| 525 | - $rendererNameAndConfiguration = $this->convertRendererToArray($renderer, $field); |
|
| 526 | - $renderers = array_merge($renderers, $rendererNameAndConfiguration); |
|
| 527 | - } |
|
| 528 | - } |
|
| 529 | - |
|
| 530 | - return $renderers; |
|
| 531 | - } |
|
| 532 | - |
|
| 533 | - /** |
|
| 534 | - * @param string $renderer |
|
| 535 | - * @return array |
|
| 536 | - */ |
|
| 537 | - protected function convertRendererToArray($renderer, array $field): array |
|
| 538 | - { |
|
| 539 | - $result = []; |
|
| 540 | - if (is_string($renderer)) { |
|
| 541 | - $configuration = empty($field['rendererConfiguration']) |
|
| 542 | - ? [] |
|
| 543 | - : $field['rendererConfiguration']; |
|
| 544 | - |
|
| 545 | - /** @var ColumnRendererInterface $rendererObject */ |
|
| 546 | - $rendererObject = GeneralUtility::makeInstance($renderer); |
|
| 547 | - |
|
| 548 | - $result[$renderer] = array_merge($rendererObject->getConfiguration(), $configuration); |
|
| 549 | - // TODO: throw alert message because this is not compatible anymore as of TYPO3 8.7.7 |
|
| 550 | - } elseif ($renderer instanceof ColumnRendererInterface) { |
|
| 551 | - /** @var ColumnRendererInterface $renderer */ |
|
| 552 | - $result[get_class($renderer)] = $renderer->getConfiguration(); |
|
| 553 | - } |
|
| 554 | - return $result; |
|
| 555 | - } |
|
| 556 | - |
|
| 557 | - /** |
|
| 558 | - * Returns the class names applied to a cell |
|
| 559 | - * |
|
| 560 | - * @param string $fieldName |
|
| 561 | - * @return bool |
|
| 562 | - */ |
|
| 563 | - public function getClass($fieldName): bool |
|
| 564 | - { |
|
| 565 | - $field = $this->getField($fieldName); |
|
| 566 | - return isset($field['class']) ? $field['class'] : ''; |
|
| 567 | - } |
|
| 568 | - |
|
| 569 | - /** |
|
| 570 | - * Returns whether the column has a label. |
|
| 571 | - * |
|
| 572 | - * @param string $fieldNameAndPath |
|
| 573 | - * @return bool |
|
| 574 | - */ |
|
| 575 | - public function hasLabel($fieldNameAndPath): bool |
|
| 576 | - { |
|
| 577 | - $field = $this->getField($fieldNameAndPath); |
|
| 578 | - |
|
| 579 | - $hasLabel = empty($field['label']) ? false : true; |
|
| 580 | - |
|
| 581 | - if (!$hasLabel && $this->hasRenderers($fieldNameAndPath)) { |
|
| 582 | - $renderers = $this->getRenderers($fieldNameAndPath); |
|
| 583 | - /** @var $renderer ColumnRendererInterface */ |
|
| 584 | - foreach ($renderers as $renderer) { |
|
| 585 | - if (isset($renderer['label'])) { |
|
| 586 | - $hasLabel = true; |
|
| 587 | - break; |
|
| 588 | - } |
|
| 589 | - } |
|
| 590 | - } |
|
| 591 | - return $hasLabel; |
|
| 592 | - } |
|
| 593 | - |
|
| 594 | - /** |
|
| 595 | - * @return array |
|
| 596 | - */ |
|
| 597 | - public function getTca(): array |
|
| 598 | - { |
|
| 599 | - return $this->tca; |
|
| 600 | - } |
|
| 601 | - |
|
| 602 | - /** |
|
| 603 | - * @return array |
|
| 604 | - */ |
|
| 605 | - public function getIncludedFields(): array |
|
| 606 | - { |
|
| 607 | - return empty($this->tca['included_fields']) ? [] : GeneralUtility::trimExplode(',', $this->tca['included_fields'], true); |
|
| 608 | - } |
|
| 609 | - |
|
| 610 | - /** |
|
| 611 | - * Return excluded fields from configuration + preferences. |
|
| 612 | - * |
|
| 613 | - * @return array |
|
| 614 | - */ |
|
| 615 | - public function getExcludedFields(): array |
|
| 616 | - { |
|
| 617 | - $configurationFields = $this->getExcludedFieldsFromConfiguration(); |
|
| 618 | - $preferencesFields = $this->getExcludedFieldsFromPreferences(); |
|
| 619 | - |
|
| 620 | - return array_merge($configurationFields, $preferencesFields); |
|
| 621 | - } |
|
| 622 | - |
|
| 623 | - /** |
|
| 624 | - * Fetch excluded fields from configuration. |
|
| 625 | - * |
|
| 626 | - * @return array |
|
| 627 | - */ |
|
| 628 | - protected function getExcludedFieldsFromConfiguration(): array |
|
| 629 | - { |
|
| 630 | - $excludedFields = []; |
|
| 631 | - if (!empty($this->tca['excluded_fields'])) { |
|
| 632 | - $excludedFields = GeneralUtility::trimExplode(',', $this->tca['excluded_fields'], true); |
|
| 633 | - } elseif (!empty($this->tca['export']['excluded_fields'])) { // only for export for legacy reason. |
|
| 634 | - $excludedFields = GeneralUtility::trimExplode(',', $this->tca['export']['excluded_fields'], true); |
|
| 635 | - } |
|
| 636 | - return $excludedFields; |
|
| 637 | - } |
|
| 638 | - |
|
| 639 | - /** |
|
| 640 | - * Fetch excluded fields from preferences. |
|
| 641 | - * |
|
| 642 | - * @return array |
|
| 643 | - */ |
|
| 644 | - protected function getExcludedFieldsFromPreferences(): array |
|
| 645 | - { |
|
| 646 | - $excludedFields = $this->getModulePreferences()->get(ConfigurablePart::EXCLUDED_FIELDS, $this->tableName); |
|
| 647 | - return is_array($excludedFields) ? $excludedFields : []; |
|
| 648 | - } |
|
| 649 | - |
|
| 650 | - /** |
|
| 651 | - * @return bool |
|
| 652 | - */ |
|
| 653 | - public function areFilesIncludedInExport(): bool |
|
| 654 | - { |
|
| 655 | - $isIncluded = true; |
|
| 656 | - |
|
| 657 | - if (isset($this->tca['export']['include_files'])) { |
|
| 658 | - $isIncluded = (bool)$this->tca['export']['include_files']; |
|
| 659 | - } |
|
| 660 | - return $isIncluded; |
|
| 661 | - } |
|
| 662 | - |
|
| 663 | - /** |
|
| 664 | - * Returns a "facet" service instance. |
|
| 665 | - * |
|
| 666 | - * @param string|FacetInterface $facetName |
|
| 667 | - * @return StandardFacet |
|
| 668 | - */ |
|
| 669 | - protected function instantiateStandardFacet($facetName): StandardFacet |
|
| 670 | - { |
|
| 671 | - $label = $this->getLabel($facetName); |
|
| 672 | - |
|
| 673 | - /** @var StandardFacet $facetName */ |
|
| 674 | - $facet = GeneralUtility::makeInstance(StandardFacet::class, $facetName, $label); |
|
| 675 | - |
|
| 676 | - if (!$facet instanceof StandardFacet) { |
|
| 677 | - throw new \RuntimeException('I could not instantiate a facet for facet name "' . $facetName . '""', 1445856345); |
|
| 678 | - } |
|
| 679 | - return $facet; |
|
| 680 | - } |
|
| 681 | - |
|
| 682 | - /** |
|
| 683 | - * Returns a "facet" service instance. |
|
| 684 | - * |
|
| 685 | - * @param string|FacetInterface $facetName |
|
| 686 | - * @return FacetInterface |
|
| 687 | - */ |
|
| 688 | - public function facet($facetName = ''): FacetInterface |
|
| 689 | - { |
|
| 690 | - $facets = $this->getFacets(); |
|
| 691 | - return $facets[$facetName]; |
|
| 692 | - } |
|
| 693 | - |
|
| 694 | - /** |
|
| 695 | - * @return FieldPathResolver|object |
|
| 696 | - */ |
|
| 697 | - protected function getFieldPathResolver() |
|
| 698 | - { |
|
| 699 | - return GeneralUtility::makeInstance(FieldPathResolver::class); |
|
| 700 | - } |
|
| 701 | - |
|
| 702 | - /** |
|
| 703 | - * @return ModulePreferences|object |
|
| 704 | - */ |
|
| 705 | - protected function getModulePreferences() |
|
| 706 | - { |
|
| 707 | - return GeneralUtility::makeInstance(ModulePreferences::class); |
|
| 708 | - } |
|
| 709 | - |
|
| 710 | - /** |
|
| 711 | - * @return LanguageService|object |
|
| 712 | - */ |
|
| 713 | - protected function getLanguageService() |
|
| 714 | - { |
|
| 715 | - return GeneralUtility::makeInstance(LanguageService::class); |
|
| 716 | - } |
|
| 26 | + /** |
|
| 27 | + * @var array |
|
| 28 | + */ |
|
| 29 | + protected $tca; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @var string |
|
| 33 | + */ |
|
| 34 | + protected $tableName; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * All fields available in the Grid. |
|
| 38 | + * |
|
| 39 | + * @var array |
|
| 40 | + */ |
|
| 41 | + protected $fields; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * All fields regardless whether they have been excluded or not. |
|
| 45 | + * |
|
| 46 | + * @var array |
|
| 47 | + */ |
|
| 48 | + protected $allFields; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @var array |
|
| 52 | + */ |
|
| 53 | + protected $instances; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * @var array |
|
| 57 | + */ |
|
| 58 | + protected $facets; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * __construct |
|
| 62 | + * |
|
| 63 | + * @param string $tableName |
|
| 64 | + */ |
|
| 65 | + public function __construct($tableName) |
|
| 66 | + { |
|
| 67 | + $this->tableName = $tableName; |
|
| 68 | + |
|
| 69 | + if (empty($GLOBALS['TCA'][$this->tableName])) { |
|
| 70 | + throw new InvalidKeyInArrayException('No TCA existence for table name: ' . $this->tableName, 1356945108); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + $this->tca = $GLOBALS['TCA'][$this->tableName]['grid'] ?? []; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Returns an array containing column names. |
|
| 78 | + * |
|
| 79 | + * @return array |
|
| 80 | + */ |
|
| 81 | + public function getFieldNames(): array |
|
| 82 | + { |
|
| 83 | + $fields = $this->getFields(); |
|
| 84 | + return array_keys($fields) ?: []; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Returns an array containing column names. |
|
| 89 | + * |
|
| 90 | + * @return array |
|
| 91 | + */ |
|
| 92 | + public function getAllFieldNames(): array |
|
| 93 | + { |
|
| 94 | + $allFields = $this->getAllFields(); |
|
| 95 | + return array_keys($allFields); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Get the label key. |
|
| 100 | + * |
|
| 101 | + * @param string $fieldNameAndPath |
|
| 102 | + * @return string |
|
| 103 | + */ |
|
| 104 | + public function getLabelKey($fieldNameAndPath): string |
|
| 105 | + { |
|
| 106 | + $field = $this->getField($fieldNameAndPath); |
|
| 107 | + |
|
| 108 | + // First option is to get the label from the Grid TCA. |
|
| 109 | + $rawLabel = ''; |
|
| 110 | + if (isset($field['label'])) { |
|
| 111 | + $rawLabel = $field['label']; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + // Second option is to fetch the label from the Column Renderer object. |
|
| 115 | + if (!$rawLabel && $this->hasRenderers($fieldNameAndPath)) { |
|
| 116 | + $renderers = $this->getRenderers($fieldNameAndPath); |
|
| 117 | + /** @var $renderer ColumnRendererInterface */ |
|
| 118 | + foreach ($renderers as $renderer) { |
|
| 119 | + if (isset($renderer['label'])) { |
|
| 120 | + $rawLabel = $renderer['label']; |
|
| 121 | + break; |
|
| 122 | + } |
|
| 123 | + } |
|
| 124 | + } |
|
| 125 | + return $rawLabel; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Get the translation of a label given a column name. |
|
| 130 | + * |
|
| 131 | + * @param string $fieldNameAndPath |
|
| 132 | + * @return string |
|
| 133 | + */ |
|
| 134 | + public function getLabel($fieldNameAndPath) |
|
| 135 | + { |
|
| 136 | + $label = ''; |
|
| 137 | + if ($this->hasLabel($fieldNameAndPath)) { |
|
| 138 | + $labelKey = $this->getLabelKey($fieldNameAndPath); |
|
| 139 | + try { |
|
| 140 | + $label = $this->getLanguageService()->sL($labelKey); |
|
| 141 | + } catch (\InvalidArgumentException $e) { |
|
| 142 | + } |
|
| 143 | + if (empty($label)) { |
|
| 144 | + $label = $labelKey; |
|
| 145 | + } |
|
| 146 | + } else { |
|
| 147 | + // Important to notice the label can contains a path, e.g. metadata.categories and must be resolved. |
|
| 148 | + $dataType = $this->getFieldPathResolver()->getDataType($fieldNameAndPath, $this->tableName); |
|
| 149 | + $fieldName = $this->getFieldPathResolver()->stripFieldPath($fieldNameAndPath, $this->tableName); |
|
| 150 | + $table = Tca::table($dataType); |
|
| 151 | + |
|
| 152 | + if ($table->hasField($fieldName) && $table->field($fieldName)->hasLabel()) { |
|
| 153 | + $label = $table->field($fieldName)->getLabel(); |
|
| 154 | + } |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + return $label; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * Returns the field name given its position. |
|
| 162 | + * |
|
| 163 | + * @param string $position the position of the field in the grid |
|
| 164 | + * @return string |
|
| 165 | + */ |
|
| 166 | + public function getFieldNameByPosition($position): string |
|
| 167 | + { |
|
| 168 | + $fields = array_keys($this->getFields()); |
|
| 169 | + if (empty($fields[$position])) { |
|
| 170 | + throw new InvalidKeyInArrayException('No field exist for position: ' . $position, 1356945119); |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + return (string)$fields[$position]; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * Returns a field name. |
|
| 178 | + * |
|
| 179 | + * @param string $fieldName |
|
| 180 | + * @return array |
|
| 181 | + */ |
|
| 182 | + public function getField($fieldName): array |
|
| 183 | + { |
|
| 184 | + $fields = $this->getFields(); |
|
| 185 | + return $fields[$fieldName] ?: []; |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * Returns an array containing column names for the Grid. |
|
| 190 | + * |
|
| 191 | + * @return array |
|
| 192 | + */ |
|
| 193 | + public function getFields(): array |
|
| 194 | + { |
|
| 195 | + // Cache this operation since it can take some time. |
|
| 196 | + if ($this->fields === null) { |
|
| 197 | + // Fetch all available fields first. |
|
| 198 | + $fields = $this->getAllFields(); |
|
| 199 | + |
|
| 200 | + if ($this->isBackendMode()) { |
|
| 201 | + // Then remove the not allowed. |
|
| 202 | + $fields = $this->filterByIncludedFields($fields); |
|
| 203 | + $fields = $this->filterByBackendUser($fields); |
|
| 204 | + $fields = $this->filterByExcludedFields($fields); |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + $this->fields = $fields; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + return $this->fields; |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + /** |
|
| 214 | + * Remove fields according to Grid configuration. |
|
| 215 | + * |
|
| 216 | + * @param $fields |
|
| 217 | + * @return array |
|
| 218 | + */ |
|
| 219 | + protected function filterByIncludedFields($fields): array |
|
| 220 | + { |
|
| 221 | + $filteredFields = $fields; |
|
| 222 | + $includedFields = $this->getIncludedFields(); |
|
| 223 | + if (count($includedFields) > 0) { |
|
| 224 | + $filteredFields = []; |
|
| 225 | + foreach ($fields as $fieldNameAndPath => $configuration) { |
|
| 226 | + if (in_array($fieldNameAndPath, $includedFields, true) || !Tca::table($this->tableName)->hasField($fieldNameAndPath)) { |
|
| 227 | + $filteredFields[$fieldNameAndPath] = $configuration; |
|
| 228 | + } |
|
| 229 | + } |
|
| 230 | + } |
|
| 231 | + return $filteredFields; |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + /** |
|
| 235 | + * Remove fields according to BE User permission. |
|
| 236 | + * |
|
| 237 | + * @param $fields |
|
| 238 | + * @return array |
|
| 239 | + */ |
|
| 240 | + protected function filterByBackendUser($fields): array |
|
| 241 | + { |
|
| 242 | + if (!$this->getBackendUser()->isAdmin()) { |
|
| 243 | + foreach ($fields as $fieldName => $field) { |
|
| 244 | + if (Tca::table($this->tableName)->hasField($fieldName) && !Tca::table($this->tableName)->field($fieldName)->hasAccess()) { |
|
| 245 | + unset($fields[$fieldName]); |
|
| 246 | + } |
|
| 247 | + } |
|
| 248 | + } |
|
| 249 | + return $fields; |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + /** |
|
| 253 | + * Remove fields according to Grid configuration. |
|
| 254 | + * |
|
| 255 | + * @param $fields |
|
| 256 | + * @return array |
|
| 257 | + */ |
|
| 258 | + protected function filterByExcludedFields($fields): array |
|
| 259 | + { |
|
| 260 | + // Unset excluded fields. |
|
| 261 | + foreach ($this->getExcludedFields() as $excludedField) { |
|
| 262 | + if (isset($fields[$excludedField])) { |
|
| 263 | + unset($fields[$excludedField]); |
|
| 264 | + } |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + return $fields; |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + /** |
|
| 271 | + * Returns an array containing column names for the Grid. |
|
| 272 | + * |
|
| 273 | + * @return array |
|
| 274 | + */ |
|
| 275 | + public function getAllFields(): array |
|
| 276 | + { |
|
| 277 | + // Cache this operation since it can take some time. |
|
| 278 | + if ($this->allFields === null) { |
|
| 279 | + $fields = isset($this->tca['columns']) && is_array($this->tca['columns']) ? $this->tca['columns'] : []; |
|
| 280 | + $gridFieldNames = array_keys($fields); |
|
| 281 | + |
|
| 282 | + // Fetch all fields of the TCA and merge it back to the fields configured for Grid. |
|
| 283 | + $tableFieldNames = Tca::table($this->tableName)->getFields(); |
|
| 284 | + |
|
| 285 | + // Just remove system fields from the Grid. |
|
| 286 | + foreach ($tableFieldNames as $key => $fieldName) { |
|
| 287 | + if (in_array($fieldName, Tca::getSystemFields())) { |
|
| 288 | + unset($tableFieldNames[$key]); |
|
| 289 | + } |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + $additionalFields = array_diff($tableFieldNames, $gridFieldNames); |
|
| 293 | + |
|
| 294 | + if (!empty($additionalFields)) { |
|
| 295 | + // Pop out last element of the key |
|
| 296 | + // Idea is to place new un-configured columns in between. By default, they will be hidden. |
|
| 297 | + end($fields); |
|
| 298 | + $lastColumnKey = key($fields); |
|
| 299 | + $lastColumn = array_pop($fields); |
|
| 300 | + |
|
| 301 | + // Feed up the grid fields with un configured elements |
|
| 302 | + foreach ($additionalFields as $additionalField) { |
|
| 303 | + $fields[$additionalField] = array( |
|
| 304 | + 'visible' => false |
|
| 305 | + ); |
|
| 306 | + |
|
| 307 | + // Try to guess the format of the field. |
|
| 308 | + $fieldType = Tca::table($this->tableName)->field($additionalField)->getType(); |
|
| 309 | + if ($fieldType === FieldType::DATE) { |
|
| 310 | + $fields[$additionalField]['format'] = 'Fab\Vidi\Formatter\Date'; |
|
| 311 | + } elseif ($fieldType === FieldType::DATETIME) { |
|
| 312 | + $fields[$additionalField]['format'] = 'Fab\Vidi\Formatter\Datetime'; |
|
| 313 | + } |
|
| 314 | + } |
|
| 315 | + $fields[$lastColumnKey] = $lastColumn; |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + $this->allFields = $fields; |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + return $this->allFields; |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + /** |
|
| 325 | + * Tell whether the field exists in the grid or not. |
|
| 326 | + * |
|
| 327 | + * @param string $fieldName |
|
| 328 | + * @return bool |
|
| 329 | + */ |
|
| 330 | + public function hasField($fieldName): bool |
|
| 331 | + { |
|
| 332 | + $fields = $this->getFields(); |
|
| 333 | + return isset($fields[$fieldName]); |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + /** |
|
| 337 | + * Tell whether the facet exists in the grid or not. |
|
| 338 | + * |
|
| 339 | + * @param string $facetName |
|
| 340 | + * @return bool |
|
| 341 | + */ |
|
| 342 | + public function hasFacet($facetName): bool |
|
| 343 | + { |
|
| 344 | + $facets = $this->getFacets(); |
|
| 345 | + return isset($facets[$facetName]); |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + /** |
|
| 349 | + * Returns an array containing facets fields. |
|
| 350 | + * |
|
| 351 | + * @return FacetInterface[] |
|
| 352 | + */ |
|
| 353 | + public function getFacets(): array |
|
| 354 | + { |
|
| 355 | + if ($this->facets === null) { |
|
| 356 | + $this->facets = []; |
|
| 357 | + |
|
| 358 | + if (is_array($this->tca['facets'])) { |
|
| 359 | + foreach ($this->tca['facets'] as $key => $facetNameOrArray) { |
|
| 360 | + if (is_array($facetNameOrArray)) { |
|
| 361 | + $name = $facetNameOrArray['name'] ?? ''; |
|
| 362 | + |
|
| 363 | + $label = isset($facetNameOrArray['label']) |
|
| 364 | + ? $this->getLanguageService()->sL($facetNameOrArray['label']) |
|
| 365 | + : ''; |
|
| 366 | + |
|
| 367 | + $suggestions = $facetNameOrArray['suggestions'] ?? []; |
|
| 368 | + $configuration = $facetNameOrArray['configuration'] ?? []; |
|
| 369 | + |
|
| 370 | + /** @var FacetInterface $facetObject */ |
|
| 371 | + $facetObject = GeneralUtility::makeInstance($key, $name, $label, $suggestions, $configuration); |
|
| 372 | + $this->facets[$facetObject->getName()] = $facetObject; |
|
| 373 | + } else { |
|
| 374 | + $this->facets[$facetNameOrArray] = $this->instantiateStandardFacet($facetNameOrArray); |
|
| 375 | + } |
|
| 376 | + } |
|
| 377 | + } |
|
| 378 | + } |
|
| 379 | + return $this->facets; |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + /** |
|
| 383 | + * Returns the "sortable" value of the column. |
|
| 384 | + * |
|
| 385 | + * @param string $fieldName |
|
| 386 | + * @return int|string |
|
| 387 | + */ |
|
| 388 | + public function isSortable($fieldName) |
|
| 389 | + { |
|
| 390 | + $defaultValue = true; |
|
| 391 | + $hasSortableField = Tca::table($this->tableName)->hasSortableField(); |
|
| 392 | + if ($hasSortableField) { |
|
| 393 | + $isSortable = false; |
|
| 394 | + } else { |
|
| 395 | + $isSortable = $this->get($fieldName, 'sortable', $defaultValue); |
|
| 396 | + } |
|
| 397 | + return $isSortable; |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + /** |
|
| 401 | + * Returns the "canBeHidden" value of the column. |
|
| 402 | + * |
|
| 403 | + * @param string $fieldName |
|
| 404 | + * @return bool |
|
| 405 | + */ |
|
| 406 | + public function canBeHidden($fieldName): bool |
|
| 407 | + { |
|
| 408 | + $defaultValue = true; |
|
| 409 | + return $this->get($fieldName, 'canBeHidden', $defaultValue); |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + /** |
|
| 413 | + * Returns the "width" value of the column. |
|
| 414 | + * |
|
| 415 | + * @param string $fieldName |
|
| 416 | + * @return int|string |
|
| 417 | + */ |
|
| 418 | + public function getWidth($fieldName) |
|
| 419 | + { |
|
| 420 | + $defaultValue = 'auto'; |
|
| 421 | + return $this->get($fieldName, 'width', $defaultValue); |
|
| 422 | + } |
|
| 423 | + |
|
| 424 | + /** |
|
| 425 | + * Returns the "visible" value of the column. |
|
| 426 | + * |
|
| 427 | + * @param string $fieldName |
|
| 428 | + * @return bool |
|
| 429 | + */ |
|
| 430 | + public function isVisible($fieldName): bool |
|
| 431 | + { |
|
| 432 | + $defaultValue = true; |
|
| 433 | + return $this->get($fieldName, 'visible', $defaultValue); |
|
| 434 | + } |
|
| 435 | + |
|
| 436 | + /** |
|
| 437 | + * Returns the "editable" value of the column. |
|
| 438 | + * |
|
| 439 | + * @param string $columnName |
|
| 440 | + * @return bool |
|
| 441 | + */ |
|
| 442 | + public function isEditable($columnName): bool |
|
| 443 | + { |
|
| 444 | + $defaultValue = false; |
|
| 445 | + return $this->get($columnName, 'editable', $defaultValue); |
|
| 446 | + } |
|
| 447 | + |
|
| 448 | + /** |
|
| 449 | + * Returns the "localized" value of the column. |
|
| 450 | + * |
|
| 451 | + * @param string $columnName |
|
| 452 | + * @return bool |
|
| 453 | + */ |
|
| 454 | + public function isLocalized($columnName): bool |
|
| 455 | + { |
|
| 456 | + $defaultValue = true; |
|
| 457 | + return $this->get($columnName, 'localized', $defaultValue); |
|
| 458 | + } |
|
| 459 | + |
|
| 460 | + /** |
|
| 461 | + * |
|
| 462 | + * Returns the "html" value of the column. |
|
| 463 | + * |
|
| 464 | + * @param string $fieldName |
|
| 465 | + * @return string |
|
| 466 | + */ |
|
| 467 | + public function getHeader($fieldName): string |
|
| 468 | + { |
|
| 469 | + $defaultValue = ''; |
|
| 470 | + return $this->get($fieldName, 'html', $defaultValue); |
|
| 471 | + } |
|
| 472 | + |
|
| 473 | + /** |
|
| 474 | + * Fetch a possible from a Grid Renderer. If no value is found, returns null |
|
| 475 | + * |
|
| 476 | + * @param string $fieldName |
|
| 477 | + * @param string $key |
|
| 478 | + * @param mixed $defaultValue |
|
| 479 | + * @return null|mixed |
|
| 480 | + */ |
|
| 481 | + public function get($fieldName, $key, $defaultValue = null) |
|
| 482 | + { |
|
| 483 | + $value = $defaultValue; |
|
| 484 | + |
|
| 485 | + $field = $this->getField($fieldName); |
|
| 486 | + if (isset($field[$key])) { |
|
| 487 | + $value = $field[$key]; |
|
| 488 | + } elseif ($this->hasRenderers($fieldName)) { |
|
| 489 | + $renderers = $this->getRenderers($fieldName); |
|
| 490 | + foreach ($renderers as $rendererConfiguration) { |
|
| 491 | + if (isset($rendererConfiguration[$key])) { |
|
| 492 | + $value = $rendererConfiguration[$key]; |
|
| 493 | + } |
|
| 494 | + } |
|
| 495 | + } |
|
| 496 | + return $value; |
|
| 497 | + } |
|
| 498 | + |
|
| 499 | + /** |
|
| 500 | + * Returns whether the column has a renderer. |
|
| 501 | + * |
|
| 502 | + * @param string $fieldName |
|
| 503 | + * @return bool |
|
| 504 | + */ |
|
| 505 | + public function hasRenderers($fieldName): bool |
|
| 506 | + { |
|
| 507 | + $field = $this->getField($fieldName); |
|
| 508 | + return empty($field['renderer']) && empty($field['renderers']) ? false : true; |
|
| 509 | + } |
|
| 510 | + |
|
| 511 | + /** |
|
| 512 | + * Returns a renderer. |
|
| 513 | + * |
|
| 514 | + * @param string $fieldName |
|
| 515 | + * @return array |
|
| 516 | + */ |
|
| 517 | + public function getRenderers($fieldName): array |
|
| 518 | + { |
|
| 519 | + $field = $this->getField($fieldName); |
|
| 520 | + $renderers = []; |
|
| 521 | + if (!empty($field['renderer'])) { |
|
| 522 | + $renderers = $this->convertRendererToArray($field['renderer'], $field); |
|
| 523 | + } elseif (!empty($field['renderers']) && is_array($field['renderers'])) { |
|
| 524 | + foreach ($field['renderers'] as $renderer) { |
|
| 525 | + $rendererNameAndConfiguration = $this->convertRendererToArray($renderer, $field); |
|
| 526 | + $renderers = array_merge($renderers, $rendererNameAndConfiguration); |
|
| 527 | + } |
|
| 528 | + } |
|
| 529 | + |
|
| 530 | + return $renderers; |
|
| 531 | + } |
|
| 532 | + |
|
| 533 | + /** |
|
| 534 | + * @param string $renderer |
|
| 535 | + * @return array |
|
| 536 | + */ |
|
| 537 | + protected function convertRendererToArray($renderer, array $field): array |
|
| 538 | + { |
|
| 539 | + $result = []; |
|
| 540 | + if (is_string($renderer)) { |
|
| 541 | + $configuration = empty($field['rendererConfiguration']) |
|
| 542 | + ? [] |
|
| 543 | + : $field['rendererConfiguration']; |
|
| 544 | + |
|
| 545 | + /** @var ColumnRendererInterface $rendererObject */ |
|
| 546 | + $rendererObject = GeneralUtility::makeInstance($renderer); |
|
| 547 | + |
|
| 548 | + $result[$renderer] = array_merge($rendererObject->getConfiguration(), $configuration); |
|
| 549 | + // TODO: throw alert message because this is not compatible anymore as of TYPO3 8.7.7 |
|
| 550 | + } elseif ($renderer instanceof ColumnRendererInterface) { |
|
| 551 | + /** @var ColumnRendererInterface $renderer */ |
|
| 552 | + $result[get_class($renderer)] = $renderer->getConfiguration(); |
|
| 553 | + } |
|
| 554 | + return $result; |
|
| 555 | + } |
|
| 556 | + |
|
| 557 | + /** |
|
| 558 | + * Returns the class names applied to a cell |
|
| 559 | + * |
|
| 560 | + * @param string $fieldName |
|
| 561 | + * @return bool |
|
| 562 | + */ |
|
| 563 | + public function getClass($fieldName): bool |
|
| 564 | + { |
|
| 565 | + $field = $this->getField($fieldName); |
|
| 566 | + return isset($field['class']) ? $field['class'] : ''; |
|
| 567 | + } |
|
| 568 | + |
|
| 569 | + /** |
|
| 570 | + * Returns whether the column has a label. |
|
| 571 | + * |
|
| 572 | + * @param string $fieldNameAndPath |
|
| 573 | + * @return bool |
|
| 574 | + */ |
|
| 575 | + public function hasLabel($fieldNameAndPath): bool |
|
| 576 | + { |
|
| 577 | + $field = $this->getField($fieldNameAndPath); |
|
| 578 | + |
|
| 579 | + $hasLabel = empty($field['label']) ? false : true; |
|
| 580 | + |
|
| 581 | + if (!$hasLabel && $this->hasRenderers($fieldNameAndPath)) { |
|
| 582 | + $renderers = $this->getRenderers($fieldNameAndPath); |
|
| 583 | + /** @var $renderer ColumnRendererInterface */ |
|
| 584 | + foreach ($renderers as $renderer) { |
|
| 585 | + if (isset($renderer['label'])) { |
|
| 586 | + $hasLabel = true; |
|
| 587 | + break; |
|
| 588 | + } |
|
| 589 | + } |
|
| 590 | + } |
|
| 591 | + return $hasLabel; |
|
| 592 | + } |
|
| 593 | + |
|
| 594 | + /** |
|
| 595 | + * @return array |
|
| 596 | + */ |
|
| 597 | + public function getTca(): array |
|
| 598 | + { |
|
| 599 | + return $this->tca; |
|
| 600 | + } |
|
| 601 | + |
|
| 602 | + /** |
|
| 603 | + * @return array |
|
| 604 | + */ |
|
| 605 | + public function getIncludedFields(): array |
|
| 606 | + { |
|
| 607 | + return empty($this->tca['included_fields']) ? [] : GeneralUtility::trimExplode(',', $this->tca['included_fields'], true); |
|
| 608 | + } |
|
| 609 | + |
|
| 610 | + /** |
|
| 611 | + * Return excluded fields from configuration + preferences. |
|
| 612 | + * |
|
| 613 | + * @return array |
|
| 614 | + */ |
|
| 615 | + public function getExcludedFields(): array |
|
| 616 | + { |
|
| 617 | + $configurationFields = $this->getExcludedFieldsFromConfiguration(); |
|
| 618 | + $preferencesFields = $this->getExcludedFieldsFromPreferences(); |
|
| 619 | + |
|
| 620 | + return array_merge($configurationFields, $preferencesFields); |
|
| 621 | + } |
|
| 622 | + |
|
| 623 | + /** |
|
| 624 | + * Fetch excluded fields from configuration. |
|
| 625 | + * |
|
| 626 | + * @return array |
|
| 627 | + */ |
|
| 628 | + protected function getExcludedFieldsFromConfiguration(): array |
|
| 629 | + { |
|
| 630 | + $excludedFields = []; |
|
| 631 | + if (!empty($this->tca['excluded_fields'])) { |
|
| 632 | + $excludedFields = GeneralUtility::trimExplode(',', $this->tca['excluded_fields'], true); |
|
| 633 | + } elseif (!empty($this->tca['export']['excluded_fields'])) { // only for export for legacy reason. |
|
| 634 | + $excludedFields = GeneralUtility::trimExplode(',', $this->tca['export']['excluded_fields'], true); |
|
| 635 | + } |
|
| 636 | + return $excludedFields; |
|
| 637 | + } |
|
| 638 | + |
|
| 639 | + /** |
|
| 640 | + * Fetch excluded fields from preferences. |
|
| 641 | + * |
|
| 642 | + * @return array |
|
| 643 | + */ |
|
| 644 | + protected function getExcludedFieldsFromPreferences(): array |
|
| 645 | + { |
|
| 646 | + $excludedFields = $this->getModulePreferences()->get(ConfigurablePart::EXCLUDED_FIELDS, $this->tableName); |
|
| 647 | + return is_array($excludedFields) ? $excludedFields : []; |
|
| 648 | + } |
|
| 649 | + |
|
| 650 | + /** |
|
| 651 | + * @return bool |
|
| 652 | + */ |
|
| 653 | + public function areFilesIncludedInExport(): bool |
|
| 654 | + { |
|
| 655 | + $isIncluded = true; |
|
| 656 | + |
|
| 657 | + if (isset($this->tca['export']['include_files'])) { |
|
| 658 | + $isIncluded = (bool)$this->tca['export']['include_files']; |
|
| 659 | + } |
|
| 660 | + return $isIncluded; |
|
| 661 | + } |
|
| 662 | + |
|
| 663 | + /** |
|
| 664 | + * Returns a "facet" service instance. |
|
| 665 | + * |
|
| 666 | + * @param string|FacetInterface $facetName |
|
| 667 | + * @return StandardFacet |
|
| 668 | + */ |
|
| 669 | + protected function instantiateStandardFacet($facetName): StandardFacet |
|
| 670 | + { |
|
| 671 | + $label = $this->getLabel($facetName); |
|
| 672 | + |
|
| 673 | + /** @var StandardFacet $facetName */ |
|
| 674 | + $facet = GeneralUtility::makeInstance(StandardFacet::class, $facetName, $label); |
|
| 675 | + |
|
| 676 | + if (!$facet instanceof StandardFacet) { |
|
| 677 | + throw new \RuntimeException('I could not instantiate a facet for facet name "' . $facetName . '""', 1445856345); |
|
| 678 | + } |
|
| 679 | + return $facet; |
|
| 680 | + } |
|
| 681 | + |
|
| 682 | + /** |
|
| 683 | + * Returns a "facet" service instance. |
|
| 684 | + * |
|
| 685 | + * @param string|FacetInterface $facetName |
|
| 686 | + * @return FacetInterface |
|
| 687 | + */ |
|
| 688 | + public function facet($facetName = ''): FacetInterface |
|
| 689 | + { |
|
| 690 | + $facets = $this->getFacets(); |
|
| 691 | + return $facets[$facetName]; |
|
| 692 | + } |
|
| 693 | + |
|
| 694 | + /** |
|
| 695 | + * @return FieldPathResolver|object |
|
| 696 | + */ |
|
| 697 | + protected function getFieldPathResolver() |
|
| 698 | + { |
|
| 699 | + return GeneralUtility::makeInstance(FieldPathResolver::class); |
|
| 700 | + } |
|
| 701 | + |
|
| 702 | + /** |
|
| 703 | + * @return ModulePreferences|object |
|
| 704 | + */ |
|
| 705 | + protected function getModulePreferences() |
|
| 706 | + { |
|
| 707 | + return GeneralUtility::makeInstance(ModulePreferences::class); |
|
| 708 | + } |
|
| 709 | + |
|
| 710 | + /** |
|
| 711 | + * @return LanguageService|object |
|
| 712 | + */ |
|
| 713 | + protected function getLanguageService() |
|
| 714 | + { |
|
| 715 | + return GeneralUtility::makeInstance(LanguageService::class); |
|
| 716 | + } |
|
| 717 | 717 | } |
@@ -20,47 +20,47 @@ |
||
| 20 | 20 | */ |
| 21 | 21 | abstract class AbstractComponentView implements ViewComponentInterface |
| 22 | 22 | { |
| 23 | - /** |
|
| 24 | - * Get the Vidi Module Loader. |
|
| 25 | - * |
|
| 26 | - * @return ModuleLoader|object |
|
| 27 | - */ |
|
| 28 | - protected function getModuleLoader() |
|
| 29 | - { |
|
| 30 | - return GeneralUtility::makeInstance(ModuleLoader::class); |
|
| 31 | - } |
|
| 23 | + /** |
|
| 24 | + * Get the Vidi Module Loader. |
|
| 25 | + * |
|
| 26 | + * @return ModuleLoader|object |
|
| 27 | + */ |
|
| 28 | + protected function getModuleLoader() |
|
| 29 | + { |
|
| 30 | + return GeneralUtility::makeInstance(ModuleLoader::class); |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * Returns an instance of the current Backend User. |
|
| 35 | - * |
|
| 36 | - * @return BackendUserAuthentication |
|
| 37 | - */ |
|
| 38 | - protected function getBackendUser() |
|
| 39 | - { |
|
| 40 | - return $GLOBALS['BE_USER']; |
|
| 41 | - } |
|
| 33 | + /** |
|
| 34 | + * Returns an instance of the current Backend User. |
|
| 35 | + * |
|
| 36 | + * @return BackendUserAuthentication |
|
| 37 | + */ |
|
| 38 | + protected function getBackendUser() |
|
| 39 | + { |
|
| 40 | + return $GLOBALS['BE_USER']; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * @return LanguageService|object |
|
| 45 | - */ |
|
| 46 | - protected function getLanguageService() |
|
| 47 | - { |
|
| 48 | - return GeneralUtility::makeInstance(LanguageService::class); |
|
| 49 | - } |
|
| 43 | + /** |
|
| 44 | + * @return LanguageService|object |
|
| 45 | + */ |
|
| 46 | + protected function getLanguageService() |
|
| 47 | + { |
|
| 48 | + return GeneralUtility::makeInstance(LanguageService::class); |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * @return IconFactory|object |
|
| 53 | - */ |
|
| 54 | - protected function getIconFactory() |
|
| 55 | - { |
|
| 56 | - return GeneralUtility::makeInstance(IconFactory::class); |
|
| 57 | - } |
|
| 51 | + /** |
|
| 52 | + * @return IconFactory|object |
|
| 53 | + */ |
|
| 54 | + protected function getIconFactory() |
|
| 55 | + { |
|
| 56 | + return GeneralUtility::makeInstance(IconFactory::class); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * @return LinkButton|object |
|
| 61 | - */ |
|
| 62 | - protected function makeLinkButton() |
|
| 63 | - { |
|
| 64 | - return GeneralUtility::makeInstance(LinkButton::class); |
|
| 65 | - } |
|
| 59 | + /** |
|
| 60 | + * @return LinkButton|object |
|
| 61 | + */ |
|
| 62 | + protected function makeLinkButton() |
|
| 63 | + { |
|
| 64 | + return GeneralUtility::makeInstance(LinkButton::class); |
|
| 65 | + } |
|
| 66 | 66 | } |
@@ -14,10 +14,10 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | interface ViewComponentInterface |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * Renders something to be printed out to the browser. |
|
| 19 | - * |
|
| 20 | - * @return string |
|
| 21 | - */ |
|
| 22 | - public function render(); |
|
| 17 | + /** |
|
| 18 | + * Renders something to be printed out to the browser. |
|
| 19 | + * |
|
| 20 | + * @return string |
|
| 21 | + */ |
|
| 22 | + public function render(); |
|
| 23 | 23 | } |
@@ -18,15 +18,15 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class CheckboxSystem extends AbstractComponentView |
| 20 | 20 | { |
| 21 | - /** |
|
| 22 | - * Returns a checkbox for the grids. |
|
| 23 | - * |
|
| 24 | - * @param Content $object |
|
| 25 | - * @param int $offset |
|
| 26 | - * @return string |
|
| 27 | - */ |
|
| 28 | - public function render(Content $object = null, $offset = 0) |
|
| 29 | - { |
|
| 30 | - return ''; |
|
| 31 | - } |
|
| 21 | + /** |
|
| 22 | + * Returns a checkbox for the grids. |
|
| 23 | + * |
|
| 24 | + * @param Content $object |
|
| 25 | + * @param int $offset |
|
| 26 | + * @return string |
|
| 27 | + */ |
|
| 28 | + public function render(Content $object = null, $offset = 0) |
|
| 29 | + { |
|
| 30 | + return ''; |
|
| 31 | + } |
|
| 32 | 32 | } |
@@ -18,14 +18,14 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class ButtonsSystem extends AbstractComponentView |
| 20 | 20 | { |
| 21 | - /** |
|
| 22 | - * Rendering buttons in the grids given a Content object. |
|
| 23 | - * |
|
| 24 | - * @param Content $object |
|
| 25 | - * @return string |
|
| 26 | - */ |
|
| 27 | - public function render(Content $object = null) |
|
| 28 | - { |
|
| 29 | - return ''; |
|
| 30 | - } |
|
| 21 | + /** |
|
| 22 | + * Rendering buttons in the grids given a Content object. |
|
| 23 | + * |
|
| 24 | + * @param Content $object |
|
| 25 | + * @return string |
|
| 26 | + */ |
|
| 27 | + public function render(Content $object = null) |
|
| 28 | + { |
|
| 29 | + return ''; |
|
| 30 | + } |
|
| 31 | 31 | } |
@@ -17,36 +17,36 @@ discard block |
||
| 17 | 17 | */ |
| 18 | 18 | class RelationsCheck extends AbstractComponentView |
| 19 | 19 | { |
| 20 | - /** |
|
| 21 | - * @var array |
|
| 22 | - */ |
|
| 23 | - protected $invalidFields = []; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * Renders a button for uploading assets. |
|
| 27 | - * |
|
| 28 | - * @return string |
|
| 29 | - */ |
|
| 30 | - public function render() |
|
| 31 | - { |
|
| 32 | - $result = ''; |
|
| 33 | - |
|
| 34 | - // Check whether storage is configured or not. |
|
| 35 | - if (!$this->isTcaValid()) { |
|
| 36 | - $result .= $this->formatMessageTcaIsNotValid(); |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - return $result; |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Format a message whenever the storage is offline. |
|
| 44 | - * |
|
| 45 | - * @return string |
|
| 46 | - */ |
|
| 47 | - protected function formatMessageTcaIsNotValid() |
|
| 48 | - { |
|
| 49 | - $result = <<< EOF |
|
| 20 | + /** |
|
| 21 | + * @var array |
|
| 22 | + */ |
|
| 23 | + protected $invalidFields = []; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * Renders a button for uploading assets. |
|
| 27 | + * |
|
| 28 | + * @return string |
|
| 29 | + */ |
|
| 30 | + public function render() |
|
| 31 | + { |
|
| 32 | + $result = ''; |
|
| 33 | + |
|
| 34 | + // Check whether storage is configured or not. |
|
| 35 | + if (!$this->isTcaValid()) { |
|
| 36 | + $result .= $this->formatMessageTcaIsNotValid(); |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + return $result; |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Format a message whenever the storage is offline. |
|
| 44 | + * |
|
| 45 | + * @return string |
|
| 46 | + */ |
|
| 47 | + protected function formatMessageTcaIsNotValid() |
|
| 48 | + { |
|
| 49 | + $result = <<< EOF |
|
| 50 | 50 | <div class="-warning alert alert-warning"> |
| 51 | 51 | <div class="alert-title"> |
| 52 | 52 | Grid may have trouble to render because of wrong / missing TCA. |
@@ -61,19 +61,19 @@ discard block |
||
| 61 | 61 | </div> |
| 62 | 62 | </div> |
| 63 | 63 | EOF; |
| 64 | - return $result; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Check relations of current data type in the Grid. |
|
| 69 | - * |
|
| 70 | - * @return string |
|
| 71 | - */ |
|
| 72 | - protected function formatMessageHelperText() |
|
| 73 | - { |
|
| 74 | - $helperText = ''; |
|
| 75 | - foreach ($this->invalidFields as $invalidField) { |
|
| 76 | - $helperText .= <<<EOF |
|
| 64 | + return $result; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Check relations of current data type in the Grid. |
|
| 69 | + * |
|
| 70 | + * @return string |
|
| 71 | + */ |
|
| 72 | + protected function formatMessageHelperText() |
|
| 73 | + { |
|
| 74 | + $helperText = ''; |
|
| 75 | + foreach ($this->invalidFields as $invalidField) { |
|
| 76 | + $helperText .= <<<EOF |
|
| 77 | 77 | <br /> |
| 78 | 78 | In file EXT:my_ext/Configuration/TCA/{$this->getModuleLoader()->getDataType()}.php |
| 79 | 79 | <pre> |
@@ -107,38 +107,38 @@ discard block |
||
| 107 | 107 | |
| 108 | 108 | </pre> |
| 109 | 109 | EOF; |
| 110 | - } |
|
| 111 | - return $helperText; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Check relations of current data type in the Grid. |
|
| 116 | - * |
|
| 117 | - * @return boolean |
|
| 118 | - */ |
|
| 119 | - protected function isTcaValid() |
|
| 120 | - { |
|
| 121 | - $dataType = $this->getModuleLoader()->getDataType(); |
|
| 122 | - $table = Tca::table($dataType); |
|
| 123 | - |
|
| 124 | - foreach (Tca::grid($dataType)->getFields() as $fieldName => $configuration) { |
|
| 125 | - if ($table->hasField($fieldName) && $table->field($fieldName)->hasMany()) { |
|
| 126 | - if ($table->field($fieldName)->hasRelationManyToMany()) { |
|
| 127 | - $foreignTable = $table->field($fieldName)->getForeignTable(); |
|
| 128 | - $manyToManyTable = $table->field($fieldName)->getManyToManyTable(); |
|
| 129 | - $foreignField = $table->field($fieldName)->getForeignField(); |
|
| 130 | - |
|
| 131 | - if (!$foreignField) { |
|
| 132 | - $this->invalidFields[] = $fieldName; |
|
| 133 | - } elseif (!$foreignTable) { |
|
| 134 | - $this->invalidFields[] = $fieldName; |
|
| 135 | - } elseif (!$manyToManyTable) { |
|
| 136 | - $this->invalidFields[] = $fieldName; |
|
| 137 | - } |
|
| 138 | - } |
|
| 139 | - } |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - return empty($this->invalidFields); |
|
| 143 | - } |
|
| 110 | + } |
|
| 111 | + return $helperText; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Check relations of current data type in the Grid. |
|
| 116 | + * |
|
| 117 | + * @return boolean |
|
| 118 | + */ |
|
| 119 | + protected function isTcaValid() |
|
| 120 | + { |
|
| 121 | + $dataType = $this->getModuleLoader()->getDataType(); |
|
| 122 | + $table = Tca::table($dataType); |
|
| 123 | + |
|
| 124 | + foreach (Tca::grid($dataType)->getFields() as $fieldName => $configuration) { |
|
| 125 | + if ($table->hasField($fieldName) && $table->field($fieldName)->hasMany()) { |
|
| 126 | + if ($table->field($fieldName)->hasRelationManyToMany()) { |
|
| 127 | + $foreignTable = $table->field($fieldName)->getForeignTable(); |
|
| 128 | + $manyToManyTable = $table->field($fieldName)->getManyToManyTable(); |
|
| 129 | + $foreignField = $table->field($fieldName)->getForeignField(); |
|
| 130 | + |
|
| 131 | + if (!$foreignField) { |
|
| 132 | + $this->invalidFields[] = $fieldName; |
|
| 133 | + } elseif (!$foreignTable) { |
|
| 134 | + $this->invalidFields[] = $fieldName; |
|
| 135 | + } elseif (!$manyToManyTable) { |
|
| 136 | + $this->invalidFields[] = $fieldName; |
|
| 137 | + } |
|
| 138 | + } |
|
| 139 | + } |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + return empty($this->invalidFields); |
|
| 143 | + } |
|
| 144 | 144 | } |
@@ -18,34 +18,34 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class EditUri extends AbstractComponentView |
| 20 | 20 | { |
| 21 | - /** |
|
| 22 | - * Renders a "edit" button to be placed in the grid. |
|
| 23 | - * |
|
| 24 | - * @param Content $object |
|
| 25 | - * @return string |
|
| 26 | - */ |
|
| 27 | - public function render(Content $object = null) |
|
| 28 | - { |
|
| 29 | - $uri = BackendUtility::getModuleUrl( |
|
| 30 | - 'record_edit', |
|
| 31 | - array( |
|
| 32 | - $this->getEditParameterName($object) => 'edit', |
|
| 33 | - 'returnUrl' => $this->getModuleLoader()->getModuleUrl() |
|
| 34 | - ) |
|
| 35 | - ); |
|
| 36 | - return $uri; |
|
| 37 | - } |
|
| 21 | + /** |
|
| 22 | + * Renders a "edit" button to be placed in the grid. |
|
| 23 | + * |
|
| 24 | + * @param Content $object |
|
| 25 | + * @return string |
|
| 26 | + */ |
|
| 27 | + public function render(Content $object = null) |
|
| 28 | + { |
|
| 29 | + $uri = BackendUtility::getModuleUrl( |
|
| 30 | + 'record_edit', |
|
| 31 | + array( |
|
| 32 | + $this->getEditParameterName($object) => 'edit', |
|
| 33 | + 'returnUrl' => $this->getModuleLoader()->getModuleUrl() |
|
| 34 | + ) |
|
| 35 | + ); |
|
| 36 | + return $uri; |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * @param Content $object |
|
| 41 | - * @return string |
|
| 42 | - */ |
|
| 43 | - protected function getEditParameterName(Content $object) |
|
| 44 | - { |
|
| 45 | - return sprintf( |
|
| 46 | - 'edit[%s][%s]', |
|
| 47 | - $object->getDataType(), |
|
| 48 | - $object->getUid() |
|
| 49 | - ); |
|
| 50 | - } |
|
| 39 | + /** |
|
| 40 | + * @param Content $object |
|
| 41 | + * @return string |
|
| 42 | + */ |
|
| 43 | + protected function getEditParameterName(Content $object) |
|
| 44 | + { |
|
| 45 | + return sprintf( |
|
| 46 | + 'edit[%s][%s]', |
|
| 47 | + $object->getDataType(), |
|
| 48 | + $object->getUid() |
|
| 49 | + ); |
|
| 50 | + } |
|
| 51 | 51 | } |