[BUGFIX] StorageRepository::findByStorageType() uses wrong typeField
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / StorageRepository.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011-2013 Andreas Wolf <andreas.wolf@ikt-werk.de>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 /**
30 * Repository for accessing the file mounts
31 *
32 * @author Andreas Wolf <andreas.wolf@ikt-werk.de>
33 * @author Ingmar Schlecht <ingmar@typo3.org>
34 */
35 class StorageRepository extends AbstractRepository {
36
37 /**
38 * @var string
39 */
40 protected $objectType = 'TYPO3\\CMS\\Core\\Resource\\ResourceStorage';
41
42 /**
43 * @var string
44 */
45 protected $table = 'sys_file_storage';
46
47 /**
48 * @var string
49 */
50 protected $typeField = 'driver';
51
52 /**
53 * @var \TYPO3\CMS\Core\Log\Logger
54 */
55 protected $logger;
56
57 public function __construct() {
58 parent::__construct();
59
60 /** @var $logManager \TYPO3\CMS\Core\Log\LogManager */
61 $logManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Core\Log\LogManager');
62 $this->logger = $logManager->getLogger(__CLASS__);
63 }
64
65 /**
66 * Finds storages by type.
67 *
68 * @param string $storageType
69 * @return ResourceStorage[]
70 */
71 public function findByStorageType($storageType) {
72 /** @var $driverRegistry Driver\DriverRegistry */
73 $driverRegistry = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Core\Resource\Driver\DriverRegistry');
74 $storageObjects = array();
75 $whereClause = $this->typeField . ' = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($storageType, $this->table);
76 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
77 '*',
78 $this->table,
79 $whereClause . $this->getWhereClauseForEnabledFields()
80 );
81 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
82 if ($driverRegistry->driverExists($row['driver'])) {
83 $storageObjects[] = $this->createDomainObject($row);
84 } else {
85 $this->logger->warning(
86 sprintf('Could not instantiate storage "%s" because of missing driver.', array($row['name'])),
87 $row
88 );
89 }
90 }
91 $GLOBALS['TYPO3_DB']->sql_free_result($res);
92 return $storageObjects;
93 }
94
95 /**
96 * Returns a list of mountpoints that are available in the VFS.
97 * In case no storage exists this automatically created a storage for fileadmin/
98 *
99 * @return ResourceStorage[]
100 */
101 public function findAll() {
102 // check if we have never created a storage before (no records, regardless of the enableFields),
103 // only fetch one record for that (is enough). If no record is found, create the fileadmin/ storage
104 $storageObjectsCount = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('uid', $this->table, '1=1');
105 if ($storageObjectsCount === 0) {
106 $this->createLocalStorage(
107 'fileadmin/ (auto-created)',
108 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir'],
109 'relative',
110 'This is the local fileadmin/ directory. This storage mount has been created automatically by TYPO3.'
111 );
112 }
113
114 $storageObjects = array();
115 $whereClause = NULL;
116 if ($this->type != '') {
117 $whereClause = $this->typeField . ' = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($this->type, $this->table);
118 }
119 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
120 '*',
121 $this->table,
122 ($whereClause ? $whereClause : '1=1') . $this->getWhereClauseForEnabledFields()
123 );
124
125 /** @var $driverRegistry Driver\DriverRegistry */
126 $driverRegistry = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Core\Resource\Driver\DriverRegistry');
127
128 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
129 if ($driverRegistry->driverExists($row['driver'])) {
130 $storageObjects[] = $this->createDomainObject($row);
131 } else {
132 $this->logger->warning(
133 sprintf('Could not instantiate storage "%s" because of missing driver.', array($row['name'])),
134 $row
135 );
136 }
137 }
138 $GLOBALS['TYPO3_DB']->sql_free_result($res);
139 return $storageObjects;
140 }
141
142 /**
143 * Create the initial local storage base e.g. for the fileadmin/ directory.
144 *
145 * @param string $name
146 * @param string $basePath
147 * @param string $pathType
148 * @param string $description
149 * @return integer uid of the inserted record
150 */
151 public function createLocalStorage($name, $basePath, $pathType, $description = '') {
152
153 // create the FlexForm for the driver configuration
154 $flexFormData = array(
155 'data' => array(
156 'sDEF' => array(
157 'lDEF' => array(
158 'basePath' => array('vDEF' => rtrim($basePath, '/') . '/'),
159 'pathType' => array('vDEF' => $pathType)
160 )
161 )
162 )
163 );
164
165 /** @var $flexObj \TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools */
166 $flexObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Configuration\\FlexForm\\FlexFormTools');
167 $flexFormXml = $flexObj->flexArray2Xml($flexFormData, TRUE);
168
169 // create the record
170 $field_values = array(
171 'pid' => 0,
172 'tstamp' => $GLOBALS['EXEC_TIME'],
173 'crdate' => $GLOBALS['EXEC_TIME'],
174 'name' => $name,
175 'description' => $description,
176 'driver' => 'Local',
177 'configuration' => $flexFormXml,
178 'is_online' => 1,
179 'is_browsable' => 1,
180 'is_public' => 1,
181 'is_writable' => 1
182 );
183 $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_file_storage', $field_values);
184 return (int) $GLOBALS['TYPO3_DB']->sql_insert_id();
185 }
186
187 /**
188 * Creates an object managed by this repository.
189 *
190 * @param array $databaseRow
191 * @return ResourceStorage
192 */
193 protected function createDomainObject(array $databaseRow) {
194 return $this->factory->getStorageObject($databaseRow['uid'], $databaseRow);
195 }
196
197 /**
198 * get the WHERE clause for the enabled fields of this TCA table
199 * depending on the context
200 *
201 * @return string the additional where clause, something like " AND deleted=0 AND hidden=0"
202 */
203 protected function getWhereClauseForEnabledFields() {
204 if (is_object($GLOBALS['TSFE'])) {
205 // frontend context
206 $whereClause = $GLOBALS['TSFE']->sys_page->enableFields($this->table);
207 $whereClause .= $GLOBALS['TSFE']->sys_page->deleteClause($this->table);
208 } else {
209 // backend context
210 $whereClause = \TYPO3\CMS\Backend\Utility\BackendUtility::BEenableFields($this->table);
211 $whereClause .= \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause($this->table);
212 }
213 return $whereClause;
214 }
215 }
216
217
218 ?>