[BUGFIX] SQL error in install tool if no root page available
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Updates / MediaFlexformUpdate.php
1 <?php
2 namespace TYPO3\CMS\Install\Updates;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2012-2013 Steffen Ritter <steffen.ritter@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 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 /**
31 * Migrates the old media FlexForm to the new
32 */
33 class MediaFlexformUpdate extends AbstractUpdate {
34
35 /**
36 * @var string
37 */
38 protected $title = 'FlexForm Data from Media Element';
39
40 /**
41 * Checks whether updates need to be performed
42 *
43 * @param string &$description The description for the update
44 * @param integer &$showUpdate 0=dont show update; 1=show update and next button; 2=only show description
45 * @return boolean
46 */
47 public function checkForUpdate(&$description, &$showUpdate = 0) {
48 $mediaElements = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('*', $GLOBALS['TYPO3_CONF_VARS']['SYS']['contentTable'], 'CType = "media" AND pi_flexform LIKE "%<sheet index=\\"sDEF\\">%"');
49 if ($mediaElements > 0) {
50 $description = 'You have media elements within your installation. As the structure of the flexform changed, your data needs to be migrated.';
51 $showUpdate = 1;
52 } else {
53 $description = 'You currently have no media elements within your installation. Therefore nothing to be migrated';
54 $showUpdate = 0;
55 }
56 return $showUpdate > 0;
57 }
58
59 /**
60 * Perform update
61 *
62 * @param array &$dbQueries Queries done in this update
63 * @param mixed &$customMessages Custom messages
64 * @return boolean Whether the updated was made or not
65 */
66 public function performUpdate(array &$dbQueries, &$customMessages) {
67 $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
68 'uid,pi_flexform',
69 $GLOBALS['TYPO3_CONF_VARS']['SYS']['contentTable'],
70 'CType = "media" AND pi_flexform LIKE "%<sheet index=\\"sDEF\\">%"'
71 );
72 /** @var $flexformTools \TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools */
73 $flexformTools = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Configuration\\FlexForm\\FlexFormTools');
74 foreach ($rows as $row) {
75 $flexFormXML = $row['pi_flexform'];
76 $data = \TYPO3\CMS\Core\Utility\GeneralUtility::xml2array($flexFormXML);
77 $sDEF = $data['data']['sDEF']['lDEF'];
78 unset($data['data']['sDEF']);
79 $type = $sDEF['mmType']['vDEF'];
80 $data['data']['sGeneral'] = array(
81 'lDEF' => array(
82 'mmType' => array('vDEF' => $type)
83 )
84 );
85 $width = $sDEF['mmWidth']['vDEF'];
86 if ($width) {
87 $data['data']['sGeneral']['lDEF']['mmWidth'] = array('vDEF' => intval($width));
88 }
89 $height = $sDEF['mmHeight']['vDEF'];
90 if ($height) {
91 $data['data']['sGeneral']['lDEF']['mmHeight'] = array('vDEF' => intval($height));
92 }
93 switch ($type) {
94 case 'video':
95 $data['data']['sVideo'] = array('lDEF' => array('mmFile' => array('vDEF' => $sDEF['mmFile']['vDEF'])));
96 break;
97 case 'audio':
98 $data['data']['sAudio'] = array('lDEF' => array('mmAudioFallback' => array('vDEF' => $sDEF['mmFile']['vDEF'])));
99 break;
100 default:
101 continue;
102 }
103 $newXML = $flexformTools->flexArray2Xml($data, TRUE);
104 $newXML = str_replace('encoding=""', 'encoding="utf-8"', $newXML);
105 $GLOBALS['TYPO3_DB']->exec_UPDATEquery(
106 $GLOBALS['TYPO3_CONF_VARS']['SYS']['contentTable'],
107 'uid = ' . $row['uid'],
108 array('pi_flexform' => $newXML)
109 );
110 }
111 return TRUE;
112 }
113
114 }
115
116 ?>