[BUGFIX] Invalid localization overlay of cObject FILES
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / Service / FrontendContentAdapterService.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource\Service;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011-2013 Ingmar Schlecht <ingmar@typo3.org>
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 3 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 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26 /**
27 * Tslib content adapter to modify $row array ($cObj->data[]) for backwards compatibility
28 *
29 * @author Ingmar Schlecht <ingmar@typo3.org>
30 * @license http://www.gnu.org/copyleft/gpl.html
31 */
32 class FrontendContentAdapterService {
33
34 /**
35 * Array containing all keys that are allowed in the migrateFields array.
36 *
37 * @var array
38 */
39 static protected $availableMigrationFields = array(
40 'paths',
41 'titleTexts',
42 'captions',
43 'links',
44 'alternativeTexts'
45 );
46
47 /**
48 * The name of the table
49 *
50 * @var string
51 */
52 static protected $migrateFields = array(
53 'tt_content' => array(
54 'image' => array(
55 'paths' => 'image',
56 'titleTexts' => 'titleText',
57 'captions' => 'imagecaption',
58 'links' => 'image_link',
59 'alternativeTexts' => 'altText',
60 '__typeMatch' => array(
61 'typeField' => 'CType',
62 'types' => array('image', 'textpic'),
63 )
64 ),
65 'media' => array(
66 'paths' => 'media',
67 'captions' => 'imagecaption',
68 '__typeMatch' => array(
69 'typeField' => 'CType',
70 'types' => array('uploads'),
71 )
72 )
73 ),
74 'pages' => array(
75 'media' => array(
76 'paths' => 'media'
77 )
78 )
79 );
80
81 /**
82 * Modifies the DB row in the CONTENT cObj of tslib_content for supplying
83 * backwards compatibility for some file fields which have switched to using
84 * the new File API instead of the old uploads/ folder for storing files.
85 *
86 * This method is called by the render() method of \TYPO3\CMS\Frontend\ContentObject\ContentContentObject
87 *
88 * @param array $row typically an array, but can also be null (in extensions or e.g. FLUID viewhelpers)
89 * @param string $table the database table where the record is from
90 * @throws \RuntimeException
91 * @return void
92 */
93 static public function modifyDBRow(&$row, $table) {
94 if (isset($row['_MIGRATED']) && $row['_MIGRATED'] === TRUE) {
95 return;
96 }
97 if (array_key_exists($table, static::$migrateFields)) {
98 foreach (static::$migrateFields[$table] as $migrateFieldName => $oldFieldNames) {
99 if ($row !== NULL && isset($row[$migrateFieldName]) && self::fieldIsInType($migrateFieldName, $table, $row)) {
100 $files = self::getPageRepository()->getFileReferences($table, $migrateFieldName, $row);
101 $fileFieldContents = array(
102 'paths' => array(),
103 'titleTexts' => array(),
104 'captions' => array(),
105 'links' => array(),
106 'alternativeTexts' => array(),
107 $migrateFieldName . '_fileUids' => array(),
108 $migrateFieldName . '_fileReferenceUids' => array(),
109 );
110 $oldFieldNames[$migrateFieldName . '_fileUids'] = $migrateFieldName . '_fileUids';
111 $oldFieldNames[$migrateFieldName . '_fileReferenceUids'] = $migrateFieldName . '_fileReferenceUids';
112
113 foreach ($files as $file) {
114 /** @var $file \TYPO3\CMS\Core\Resource\FileReference */
115 $fileProperties = $file->getProperties();
116 $fileFieldContents['paths'][] = '../../' . $file->getPublicUrl();
117 $fileFieldContents['titleTexts'][] = $fileProperties['title'];
118 $fileFieldContents['captions'][] = $fileProperties['description'];
119 $fileFieldContents['links'][] = $fileProperties['link'];
120 $fileFieldContents['alternativeTexts'][] = $fileProperties['alternative'];
121 $fileFieldContents[$migrateFieldName . '_fileUids'][] = $file->getOriginalFile()->getUid();
122 $fileFieldContents[$migrateFieldName . '_fileReferenceUids'][] = $file->getUid();
123 }
124 foreach ($oldFieldNames as $oldFieldType => $oldFieldName) {
125 if ($oldFieldType === '__typeMatch') {
126 continue;
127 }
128 if ($oldFieldType === 'paths' || substr($oldFieldType, -9) == '_fileUids' || substr($oldFieldType, -18) == '_fileReferenceUids') {
129 // For paths and uids, make comma separated list
130 $fieldContents = implode(',', $fileFieldContents[$oldFieldType]);
131 } else {
132 // For all other fields, separate by newline
133 $fieldContents = implode(chr(10), $fileFieldContents[$oldFieldType]);
134 }
135 $row[$oldFieldName] = $fieldContents;
136 }
137 }
138 }
139 }
140 $row['_MIGRATED'] = TRUE;
141 }
142
143 /**
144 * Registers an additional record type for an existing migration configuration.
145 *
146 * For use in ext_localconf.php files.
147 *
148 * @param string $table Name of the table in the migration configuration
149 * @param string $field Name of the field in the migration configuration
150 * @param string $additionalType The additional type for which the migration should be applied
151 * @throws \RuntimeException
152 * @return void
153 */
154 static public function registerAdditionalTypeForMigration($table, $field, $additionalType) {
155
156 if (!isset(static::$migrateFields[$table][$field]['__typeMatch'])) {
157 throw new \RuntimeException('Additional types can only be added when there is already an existing type match configuration for the given table and field.', 1377600978);
158 }
159
160 self::$migrateFields[$table][$field]['__typeMatch']['types'][] = $additionalType;
161 }
162
163 /**
164 * Registers an additional field for migration.
165 *
166 * For use in ext_localconf.php files
167 *
168 * @param string $table Name of the table in the migration configuration
169 * @param string $field Name of the field in the migration configuration
170 * @param string $migrationField The file property that should be migrated, see $availableMigrateFields for available settings
171 * @param string $oldFieldName The name of the field in which the file property should be available
172 * @param string $typeField Optional field that switches the record type, will only have an effect if $types array is provided
173 * @param array $types The record types for which the migration should be active
174 * @throws \InvalidArgumentException
175 */
176 static public function registerFieldForMigration($table, $field, $migrationField, $oldFieldName, $typeField = NULL, array $types = array()) {
177
178 if (array_search($migrationField, static::$availableMigrationFields) === FALSE) {
179 throw new \InvalidArgumentException('The value for $migrationField is invalid. Valid values can be found in the $availableMigrationFields array.', 1377600978);
180 }
181
182 self::$migrateFields[$table][$field][$migrationField] = $oldFieldName;
183
184 if (isset($typeField) && (count($types) > 0)) {
185 self::$migrateFields[$table][$field]['__typeMatch']['types'] = $types;
186 self::$migrateFields[$table][$field]['__typeMatch']['typeField'] = (string)$typeField;
187 }
188 }
189
190 /**
191 * Check if fieldis in type
192 *
193 * @param string $fieldName
194 * @param string $table
195 * @param array $row
196 * @return boolean
197 */
198 static protected function fieldIsInType($fieldName, $table, array $row) {
199 $fieldConfiguration = static::$migrateFields[$table][$fieldName];
200 if (empty($fieldConfiguration['__typeMatch'])) {
201 return TRUE;
202 } else {
203 return in_array($row[$fieldConfiguration['__typeMatch']['typeField']], $fieldConfiguration['__typeMatch']['types']);
204 }
205 }
206
207 /**
208 * @return \TYPO3\CMS\Frontend\Page\PageRepository
209 */
210 static protected function getPageRepository() {
211 return $GLOBALS['TSFE']->sys_page;
212 }
213
214 }