[TASK] Adds fileReferenceUid to content rendering
[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 * The name of the table
36 *
37 * @var string
38 */
39 static protected $migrateFields = array(
40 'tt_content' => array(
41 'image' => array(
42 'paths' => 'image',
43 'titleTexts' => 'titleText',
44 'captions' => 'imagecaption',
45 'links' => 'image_link',
46 'alternativeTexts' => 'altText',
47 '__typeMatch' => array(
48 'typeField' => 'CType',
49 'types' => array('image', 'textpic'),
50 )
51 ),
52 'media' => array(
53 'paths' => 'media',
54 'captions' => 'imagecaption',
55 '__typeMatch' => array(
56 'typeField' => 'CType',
57 'types' => array('uploads'),
58 )
59 )
60 ),
61 'pages' => array(
62 'media' => array(
63 'paths' => 'media'
64 )
65 )
66 );
67
68 /**
69 * Modifies the DB row in the CONTENT cObj of tslib_content for supplying
70 * backwards compatibility for some file fields which have switched to using
71 * the new File API instead of the old uploads/ folder for storing files.
72 *
73 * This method is called by the render() method of \TYPO3\CMS\Frontend\ContentObject\ContentContentObject
74 *
75 * @param array $row typically an array, but can also be null (in extensions or e.g. FLUID viewhelpers)
76 * @param string $table the database table where the record is from
77 * @throws \RuntimeException
78 * @return void
79 */
80 static public function modifyDBRow(&$row, $table) {
81 if (isset($row['_MIGRATED']) && $row['_MIGRATED'] === TRUE) {
82 return;
83 }
84 if (array_key_exists($table, static::$migrateFields)) {
85 foreach (static::$migrateFields[$table] as $migrateFieldName => $oldFieldNames) {
86 if ($row !== NULL && isset($row[$migrateFieldName]) && self::fieldIsInType($migrateFieldName, $table, $row)) {
87 /** @var $fileRepository \TYPO3\CMS\Core\Resource\FileRepository */
88 $fileRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\FileRepository');
89 if ($table === 'pages' && isset($row['_LOCALIZED_UID']) && intval($row['sys_language_uid']) > 0) {
90 $table = 'pages_language_overlay';
91 }
92 $files = $fileRepository->findByRelation($table, $migrateFieldName, isset($row['_LOCALIZED_UID']) ? intval($row['_LOCALIZED_UID']) : intval($row['uid']));
93 $fileFieldContents = array(
94 'paths' => array(),
95 'titleTexts' => array(),
96 'captions' => array(),
97 'links' => array(),
98 'alternativeTexts' => array(),
99 $migrateFieldName . '_fileUids' => array(),
100 $migrateFieldName . '_fileReferenceUids' => array(),
101 );
102 $oldFieldNames[$migrateFieldName . '_fileUids'] = $migrateFieldName . '_fileUids';
103 $oldFieldNames[$migrateFieldName . '_fileReferenceUids'] = $migrateFieldName . '_fileReferenceUids';
104
105 foreach ($files as $file) {
106 /** @var $file \TYPO3\CMS\Core\Resource\FileReference */
107 $fileProperties = $file->getProperties();
108 $fileFieldContents['paths'][] = '../../' . $file->getPublicUrl();
109 $fileFieldContents['titleTexts'][] = $fileProperties['title'];
110 $fileFieldContents['captions'][] = $fileProperties['description'];
111 $fileFieldContents['links'][] = $fileProperties['link'];
112 $fileFieldContents['alternativeTexts'][] = $fileProperties['alternative'];
113 $fileFieldContents[$migrateFieldName . '_fileUids'][] = $file->getOriginalFile()->getUid();
114 $fileFieldContents[$migrateFieldName . '_fileReferenceUids'][] = $file->getUid();
115 }
116 foreach ($oldFieldNames as $oldFieldType => $oldFieldName) {
117 if ($oldFieldType === '__typeMatch') {
118 continue;
119 }
120 if ($oldFieldType === 'paths' || substr($oldFieldType, -9) == '_fileUids' || substr($oldFieldType, -18) == '_fileReferenceUids') {
121 // For paths and uids, make comma separated list
122 $fieldContents = implode(',', $fileFieldContents[$oldFieldType]);
123 } else {
124 // For all other fields, separate by newline
125 $fieldContents = implode(chr(10), $fileFieldContents[$oldFieldType]);
126 }
127 $row[$oldFieldName] = $fieldContents;
128 }
129 }
130 }
131 }
132 $row['_MIGRATED'] = TRUE;
133 }
134
135 /**
136 * Check if fieldis in type
137 *
138 * @param string $fieldName
139 * @param string $table
140 * @param array $row
141 * @return boolean
142 */
143 static protected function fieldIsInType($fieldName, $table, array $row) {
144 $fieldConfiguration = static::$migrateFields[$table][$fieldName];
145 if (empty($fieldConfiguration['__typeMatch'])) {
146 return TRUE;
147 } else {
148 return in_array($row[$fieldConfiguration['__typeMatch']['typeField']], $fieldConfiguration['__typeMatch']['types']);
149 }
150 }
151 }
152
153
154 ?>