474db2e64ce7d1a1d129428778e2b61b30ef799c
[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 $row typically an array, but can also be null (in extensions or e.g. FLUID viewhelpers)
69 * @param $table the database table where the record is from
70 * @return void
71 */
72 static public function modifyDBRow(&$row, $table) {
73 if (array_key_exists($table, static::$migrateFields)) {
74 foreach (static::$migrateFields[$table] as $migrateFieldName => $oldFieldNames) {
75 if ($row !== NULL && isset($row[$migrateFieldName])) {
76 /** @var $fileRepository \TYPO3\CMS\Core\Resource\FileRepository */
77 $fileRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\FileRepository');
78 $files = $fileRepository->findByRelation($table, $migrateFieldName, $row['uid']);
79 $fileFieldContents = array(
80 'paths' => array(),
81 'titleTexts' => array(),
82 'captions' => array(),
83 'links' => array(),
84 'alternativeTexts' => array(),
85 $migrateFieldName . '_fileUids' => array()
86 );
87 $oldFieldNames[$migrateFieldName . '_fileUids'] = $migrateFieldName . '_fileUids';
88
89 foreach ($files as $file) {
90 /** @var $file \TYPO3\CMS\Core\Resource\FileReference */
91 $fileFieldContents['paths'][] = '../../' . $file->getPublicUrl();
92 $fileFieldContents['titleTexts'][] = $file->getProperty('title');
93 $fileFieldContents['captions'][] = $file->getProperty('description');
94 $fileFieldContents['links'][] = $file->getProperty('link');
95 $fileFieldContents['alternativeTexts'][] = $file->getProperty('alternative');
96 $fileFieldContents[$migrateFieldName . '_fileUids'][] = $file->getOriginalFile()->getUid();
97 }
98 foreach ($oldFieldNames as $oldFieldType => $oldFieldName) {
99 // For paths, make comma separated list
100 if ($oldFieldType === 'paths' || substr($oldFieldType, -9) == '_fileUids') {
101 $fieldContents = implode(',', $fileFieldContents[$oldFieldType]);
102 } else {
103 // For all other fields, separate by newline
104 $fieldContents = implode(chr(10), $fileFieldContents[$oldFieldType]);
105 }
106 if ($fieldContents) {
107 $row[$oldFieldName] = $fieldContents;
108 }
109 }
110 }
111 }
112 }
113 }
114
115 }
116
117
118 ?>