04ef531361f099ebfd9e9dae2939f6f1c18702c0
[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\DatabaseConnection;
20
21 /**
22 * Extended by other provider that fetch records from database
23 */
24 abstract class AbstractDatabaseRecordProvider
25 {
26 /**
27 * Fetch a record from database. Deleted records will NOT be fetched.
28 * Method is similar to BackendUtility::getRecord, but is more picky
29 * about input and result.
30 *
31 * @param string $tableName The table name to fetch record from
32 * @param int $uid Uid of record to fetch
33 * @return array Fetched record row
34 * @throws DatabaseRecordException|\InvalidArgumentException|\UnexpectedValueException|\RuntimeException
35 */
36 protected function getRecordFromDatabase($tableName, $uid)
37 {
38 if ($uid <= 0) {
39 throw new \InvalidArgumentException(
40 '$uid must be positive integer, ' . $uid . ' given',
41 1437656456
42 );
43 }
44 $database = $this->getDatabase();
45 $tableName = $database->quoteStr($tableName, $tableName);
46 $where = 'uid=' . (int)$uid . BackendUtility::deleteClause($tableName);
47 $row = $database->exec_SELECTgetSingleRow('*', $tableName, $where);
48 if ($row === null) {
49 // Indicates a program / usage error
50 throw new \RuntimeException(
51 'Database error fetching record from tablename ' . $tableName . ' with uid ' . $uid,
52 1437655862
53 );
54 }
55 if ($row === false) {
56 // Indicates a runtime error (eg. record was killed by other editor meanwhile) can be caught elsewhere
57 // and transformed to a message to the user or something
58 throw new DatabaseRecordException(
59 'Record with uid ' . $uid . ' from table ' . $tableName . ' not found',
60 1437656081
61 );
62 }
63 if (!is_array($row)) {
64 // Database connection behaves weird ...
65 throw new \UnexpectedValueException(
66 'Database exec_SELECTgetSingleRow() did not return error type or result',
67 1437656323
68 );
69 }
70 return $row;
71 }
72
73 /**
74 * @return DatabaseConnection
75 */
76 protected function getDatabase()
77 {
78 return $GLOBALS['TYPO3_DB'];
79 }
80 }