[BUGFIX] Enable translation of file references
[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 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 * @package TYPO3
30 * @author Ingmar Schlecht <ingmar@typo3.org>
31 * @license http://www.gnu.org/copyleft/gpl.html
32 */
33 class FrontendContentAdapterService {
34
35 /**
36 * The name of the table
37 *
38 * @var string
39 */
40 static protected $migrateFields = array(
41 'tt_content' => array(
42 'image' => array(
43 'paths' => 'image',
44 'titleTexts' => 'titleText',
45 'captions' => 'imagecaption',
46 'links' => 'image_link',
47 'alternativeTexts' => 'altText',
48 ),
49 'media' => array(
50 'paths' => 'media',
51 'captions' => 'imagecaption'
52 )
53 ),
54 'pages' => array(
55 'media' => array(
56 'paths' => 'media'
57 )
58 )
59 );
60
61 /**
62 * Modifies the DB row in the CONTENT cObj of tslib_content for supplying
63 * backwards compatibility for some file fields which have switched to using
64 * the new File API instead of the old uploads/ folder for storing files.
65 *
66 * This method is called by the render() method of tslib_content_Content.
67 *
68 * @param array $row typically an array, but can also be null (in extensions or e.g. FLUID viewhelpers)
69 * @param string $table the database table where the record is from
70 * @throws \RuntimeException
71 * @return void
72 */
73 static public function modifyDBRow(&$row, $table) {
74 if (array_key_exists($table, static::$migrateFields)) {
75 foreach (static::$migrateFields[$table] as $migrateFieldName => $oldFieldNames) {
76 if ($row !== NULL && isset($row[$migrateFieldName])) {
77 /** @var $fileRepository \TYPO3\CMS\Core\Resource\FileRepository */
78 $fileRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\FileRepository');
79 $files = $fileRepository->findByRelation($table, $migrateFieldName, $row['uid']);
80 $fileFieldContents = array(
81 'paths' => array(),
82 'titleTexts' => array(),
83 'captions' => array(),
84 'links' => array(),
85 'alternativeTexts' => array(),
86 $migrateFieldName . '_fileUids' => array()
87 );
88 $oldFieldNames[$migrateFieldName . '_fileUids'] = $migrateFieldName . '_fileUids';
89
90 foreach ($files as $file) {
91 /** @var $file \TYPO3\CMS\Core\Resource\FileReference */
92 $fileFieldContents['paths'][] = '../../' . $file->getPublicUrl();
93 $fileFieldContents['titleTexts'][] = $file->getProperty('title');
94 $fileFieldContents['captions'][] = $file->getProperty('description');
95 $fileFieldContents['links'][] = $file->getProperty('link');
96 $fileFieldContents['alternativeTexts'][] = $file->getProperty('alternative');
97 $fileFieldContents[$migrateFieldName . '_fileUids'][] = $file->getOriginalFile()->getUid();
98 }
99 foreach ($oldFieldNames as $oldFieldType => $oldFieldName) {
100 // For paths, make comma separated list
101 if ($oldFieldType === 'paths' || substr($oldFieldType, -9) == '_fileUids') {
102 $fieldContents = implode(',', $fileFieldContents[$oldFieldType]);
103 } else {
104 // For all other fields, separate by newline
105 $fieldContents = implode(chr(10), $fileFieldContents[$oldFieldType]);
106 }
107 if ($fieldContents) {
108 $row[$oldFieldName] = $fieldContents;
109 }
110 }
111 }
112 }
113 }
114 }
115
116 }
117
118
119 ?>