[TASK] Move LanguageService to EXT:core
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / FormDataProvider / AbstractDatabaseRecordProvider.php
1 <?php
2 namespace TYPO3\CMS\Backend\Form\FormDataProvider;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Backend\Form\Exception\DatabaseRecordException;
18 use TYPO3\CMS\Backend\Utility\BackendUtility;
19 use TYPO3\CMS\Core\Database\ConnectionPool;
20 use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Styleguide\TcaDataGenerator\TableHandler\General;
23
24 /**
25 * Extended by other provider that fetch records from database
26 */
27 abstract class AbstractDatabaseRecordProvider
28 {
29 /**
30 * Fetch a record from database. Deleted records will NOT be fetched.
31 * Method is similar to BackendUtility::getRecord, but is more picky
32 * about input and result.
33 *
34 * @param string $tableName The table name to fetch record from
35 * @param int $uid Uid of record to fetch
36 * @return array Fetched record row
37 * @throws DatabaseRecordException|\InvalidArgumentException|\UnexpectedValueException|\RuntimeException
38 */
39 protected function getRecordFromDatabase($tableName, $uid)
40 {
41 if ($uid <= 0) {
42 throw new \InvalidArgumentException(
43 '$uid must be positive integer, ' . $uid . ' given',
44 1437656456
45 );
46 }
47 $row = $this->getDatabaseRow($tableName, $uid);
48 if (empty($row)) {
49 // Indicates a runtime error (eg. record was killed by other editor meanwhile) can be caught elsewhere
50 // and transformed to a message to the user or something
51 throw new DatabaseRecordException(
52 'Record with uid ' . $uid . ' from table ' . $tableName . ' not found',
53 1437656081,
54 null,
55 $tableName,
56 (int)$uid
57 );
58 }
59 return $row;
60 }
61
62 /**
63 * Retrieve the requested row from the database
64 *
65 * @param string $tableName
66 * @param int $uid
67 * @return array
68 */
69 protected function getDatabaseRow(string $tableName, int $uid): array
70 {
71 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($tableName);
72 $queryBuilder->getRestrictions()
73 ->removeAll()
74 ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
75
76 $row = $queryBuilder->select('*')
77 ->from($tableName)
78 ->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)))
79 ->execute()
80 ->fetch();
81
82 return $row ?: [];
83 }
84 }