[TASK] Re-work/simplify copyright header in PHP files - Part 2
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Collection / RecordCollectionRepository.php
1 <?php
2 namespace TYPO3\CMS\Core\Collection;
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 * Implements the repository for record collections.
18 *
19 * @author Steffen Ritter <typo3@steffen-ritter.net>
20 */
21 class RecordCollectionRepository {
22
23 const TYPE_Static = 'static';
24 /**
25 * Name of the table the collection records are stored to
26 *
27 * @var string
28 */
29 protected $table = 'sys_collection';
30
31 /**
32 * @var string
33 */
34 protected $typeField = 'type';
35
36 /**
37 * @var string
38 */
39 protected $tableField = 'table_name';
40
41 /**
42 * Finds a record collection by uid.
43 *
44 * @param integer $uid The uid to be looked up
45 * @return NULL|\TYPO3\CMS\Core\Collection\AbstractRecordCollection
46 */
47 public function findByUid($uid) {
48 $result = NULL;
49 $data = $this->getDatabaseConnection()->exec_SELECTgetSingleRow('*', $this->table, 'uid=' . (int)$uid . \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause($this->table));
50 if ($data !== NULL) {
51 $result = $this->createDomainObject($data);
52 }
53 return $result;
54 }
55
56 /**
57 * Finds all record collections.
58 *
59 * @return NULL|\TYPO3\CMS\Core\Collection\AbstractRecordCollection[]
60 */
61 public function findAll() {
62 return $this->queryMultipleRecords();
63 }
64
65 /**
66 * Finds record collections by table name.
67 *
68 * @param string $tableName Name of the table to be looked up
69 * @return \TYPO3\CMS\Core\Collection\AbstractRecordCollection[]
70 */
71 public function findByTableName($tableName) {
72 $conditions = array(
73 $this->tableField . '=' . $this->getDatabaseConnection()->fullQuoteStr($tableName, $this->table)
74 );
75 return $this->queryMultipleRecords($conditions);
76 }
77
78 /**
79 * Finds record collection by type.
80 *
81 * @param string $type Type to be looked up
82 * @return NULL|\TYPO3\CMS\Core\Collection\AbstractRecordCollection[]
83 */
84 public function findByType($type) {
85 $conditions = array(
86 $this->typeField . '=' . $this->getDatabaseConnection()->fullQuoteStr($type, $this->table)
87 );
88 return $this->queryMultipleRecords($conditions);
89 }
90
91 /**
92 * Finds record collections by type and table name.
93 *
94 * @param string $type Type to be looked up
95 * @param string $tableName Name of the table to be looked up
96 * @return NULL|\TYPO3\CMS\Core\Collection\AbstractRecordCollection[]
97 */
98 public function findByTypeAndTableName($type, $tableName) {
99 $conditions = array(
100 $this->typeField . '=' . $this->getDatabaseConnection()->fullQuoteStr($type, $this->table),
101 $this->tableField . '=' . $this->getDatabaseConnection()->fullQuoteStr($tableName, $this->table)
102 );
103 return $this->queryMultipleRecords($conditions);
104 }
105
106 /**
107 * Deletes a record collection by uid.
108 *
109 * @param integer $uid uid to be deleted
110 * @return void
111 */
112 public function deleteByUid($uid) {
113 $this->getDatabaseConnection()->exec_UPDATEquery($this->table, 'uid=' . (int)$uid, array('deleted' => 1, 'tstamp' => $GLOBALS['EXEC_TIME']));
114 }
115
116 /**
117 * Queries for multiple records for the given conditions.
118 *
119 * @param array $conditions Conditions concatenated with AND for query
120 * @return NULL|\TYPO3\CMS\Core\Collection\AbstractRecordCollection[]
121 */
122 protected function queryMultipleRecords(array $conditions = array()) {
123 $result = NULL;
124 if (count($conditions) > 0) {
125 $conditionsWhereClause = implode(' AND ', $conditions);
126 } else {
127 $conditionsWhereClause = '1=1';
128 }
129 $data = $this->getDatabaseConnection()->exec_SELECTgetRows('*', $this->table, $conditionsWhereClause . \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause($this->table));
130 if ($data !== NULL) {
131 $result = $this->createMultipleDomainObjects($data);
132 }
133 return $result;
134 }
135
136 /**
137 * Creates a record collection domain object.
138 *
139 * @param array $record Database record to be reconstituted
140 * @return \TYPO3\CMS\Core\Collection\AbstractRecordCollection
141 */
142 protected function createDomainObject(array $record) {
143 switch ($record['type']) {
144 case self::TYPE_Static:
145 $collection = \TYPO3\CMS\Core\Collection\StaticRecordCollection::create($record);
146 break;
147 default:
148 throw new \RuntimeException('Unknown record collection type "' . $record['type'], 1328646798);
149 }
150 return $collection;
151 }
152
153 /**
154 * Creates multiple record collection domain objects.
155 *
156 * @param array $data Array of multiple database records to be reconstituted
157 * @return \TYPO3\CMS\Core\Collection\AbstractRecordCollection[]
158 */
159 protected function createMultipleDomainObjects(array $data) {
160 $collections = array();
161 foreach ($data as $collection) {
162 $collections[] = $this->createDomainObject($collection);
163 }
164 return $collections;
165 }
166
167 /**
168 * Gets the database object.
169 *
170 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
171 */
172 protected function getDatabaseConnection() {
173 return $GLOBALS['TYPO3_DB'];
174 }
175
176 }