4eeb23e28b68b916d559eb70ff596142d78bf5f3
[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 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 * @package TYPO3
35 * @subpackage t3lib
36 */
37 class StorageRepository extends \TYPO3\CMS\Core\Resource\AbstractRepository {
38
39 /**
40 * @var string
41 */
42 protected $objectType = 'TYPO3\\CMS\\Core\\Resource\\ResourceStorage';
43
44 /**
45 * @var string
46 */
47 protected $table = 'sys_file_storage';
48
49 /**
50 * @var string
51 */
52 protected $typeField = 'type';
53
54 /**
55 * Finds storages by type.
56 *
57 * @param string $storageType
58 * @return \TYPO3\CMS\Core\Resource\ResourceStorage[]
59 */
60 public function findByStorageType($storageType) {
61 $storageObjects = array();
62 $whereClause = 'deleted=0 AND hidden=0';
63 $whereClause .= ' AND ' . $this->typeField . ' = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($storageType, $this->table);
64 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', $this->table, $whereClause);
65 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
66 $storageObjects[] = $this->createDomainObject($row);
67 }
68 $GLOBALS['TYPO3_DB']->sql_free_result($res);
69 return $storageObjects;
70 }
71
72 /**
73 * Returns a list of mountpoints that are available in the VFS.
74 * In case no storage exists this automatically created a storage for fileadmin/
75 *
76 * @return \TYPO3\CMS\Core\Resource\ResourceStorage[]
77 */
78 public function findAll() {
79 $storageObjects = array();
80 $whereClause = 'deleted=0 AND hidden=0';
81 if ($this->type != '') {
82 $whereClause .= ' AND ' . $this->typeField . ' = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($this->type, $this->table);
83 }
84 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', $this->table, $whereClause);
85 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
86 $storageObjects[] = $this->createDomainObject($row);
87 }
88 $GLOBALS['TYPO3_DB']->sql_free_result($res);
89 if (count($storageObjects) === 0) {
90 $this->createLocalStorage('fileadmin/ (auto-created)', $GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir'], 'relative', 'This is the local fileadmin/ directory. This storage mount has been created automatically by TYPO3.');
91 $storageObjects = self::findAll();
92 }
93 return $storageObjects;
94 }
95
96 /**
97 * Create the initial local storage base e.g. for the fileadmin/ directory.
98 *
99 * @param string $name
100 * @param string $basePath
101 * @param string $pathType
102 * @param string $description
103 * @return integer uid of the inserted record
104 */
105 public function createLocalStorage($name, $basePath, $pathType, $description = '') {
106 $field_values = array(
107 'pid' => 0,
108 'tstamp' => $GLOBALS['EXEC_TIME'],
109 'crdate' => $GLOBALS['EXEC_TIME'],
110 'name' => $name,
111 'description' => $description,
112 'driver' => 'Local',
113 'configuration' => '<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
114 <T3FlexForms>
115 <data>
116 <sheet index="sDEF">
117 <language index="lDEF">
118 <field index="basePath">
119 <value index="vDEF">' . rtrim($basePath, '/') . '/</value>
120 </field>
121 <field index="pathType">
122 <value index="vDEF">' . $pathType . '</value>
123 </field>
124 </language>
125 </sheet>
126 </data>
127 </T3FlexForms>',
128 'is_online' => 1,
129 'is_browsable' => 1,
130 'is_public' => 1,
131 'is_writable' => 1
132 );
133 $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_file_storage', $field_values);
134 return (int) $GLOBALS['TYPO3_DB']->sql_insert_id();
135 }
136
137 /**
138 * Creates an object managed by this repository.
139 *
140 * @param array $databaseRow
141 * @return \TYPO3\CMS\Core\Resource\ResourceStorage
142 */
143 protected function createDomainObject(array $databaseRow) {
144 return $this->factory->getStorageObject($databaseRow['uid'], $databaseRow);
145 }
146
147 }
148
149
150 ?>