680a11936d6c3f4b6192a4bda3939cf11cd4a1aa
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Updates / RootTemplateUpdate.php
1 <?php
2 namespace TYPO3\CMS\Install\Updates;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2012-2013 Oliver Hader <oliver.hader@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 text file 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 RootTemplateUpdate extends AbstractUpdate implements InstallerProcessInterface {
34
35 /**
36 * @var string
37 */
38 protected $title = 'Integrate TypoScript Root Template';
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 $pages = $this->findRootLevelPages();
49
50 if ($pages !== NULL && $this->findRootTemplates(array_keys($pages)) !== NULL) {
51 $description = 'There is already at least one TypoScript root template available.';
52 $showUpdate = 0;
53 } else {
54 $description = 'There is no TypoScript root template! However, one is required for Extbase to behave correctly.';
55 $showUpdate = 1;
56 }
57
58 return $showUpdate > 0;
59 }
60
61 /**
62 * Performs updates and creates one page and Typoscript root template.
63 *
64 * @param array &$dbQueries Queries done in this update
65 * @param mixed &$customMessages Custom messages
66 * @return boolean Whether the updated was made or not
67 */
68 public function performUpdate(array &$dbQueries, &$customMessages) {
69 $now = time();
70 $result = FALSE;
71
72 $status = $GLOBALS['TYPO3_DB']->exec_INSERTquery(
73 'pages',
74 array(
75 'pid' => 0,
76 'title' => 'Home',
77 'is_siteroot' => 1,
78 'crdate' => $now,
79 'tstamp' => $now,
80 )
81 );
82
83 $dbQueries[] = str_replace(chr(10), ' ', $GLOBALS['TYPO3_DB']->debug_lastBuiltQuery);
84
85 if ($status) {
86 $pageId = $GLOBALS['TYPO3_DB']->sql_insert_id();
87
88 $status = $GLOBALS['TYPO3_DB']->exec_INSERTquery(
89 'sys_template',
90 array(
91 'pid' => $pageId,
92 'title' => 'Default Root Template',
93 'root' => 1,
94 'clear' => 1,
95 'crdate' => $now,
96 'tstamp' => $now,
97 )
98 );
99
100 $dbQueries[] = str_replace(chr(10), ' ', $GLOBALS['TYPO3_DB']->debug_lastBuiltQuery);
101
102 if ($status) {
103 $result = TRUE;
104 }
105 }
106
107 return $result;
108 }
109
110 /**
111 * Finds pages on the root level (pid 0).
112 *
113 * @return array|NULL
114 */
115 protected function findRootLevelPages() {
116 $pages = NULL;
117
118 $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
119 'uid,pid',
120 'pages',
121 'pid=0 AND deleted=0',
122 '',
123 '',
124 '',
125 'uid'
126 );
127
128 if (is_array($rows) && !empty($rows)) {
129 $pages = $rows;
130 }
131
132 return $pages;
133 }
134
135 /**
136 * Finds root templates in the given pages.
137 *
138 * @param array $pageUids
139 * @return array|NULL
140 */
141 protected function findRootTemplates(array $pageUids) {
142 $templates = NULL;
143
144 $pageUids = array_map('intval', $pageUids);
145 $pageUidList = implode(', ', $pageUids);
146
147 $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
148 'uid,pid',
149 'sys_template',
150 'deleted=0 AND root=1 AND pid IN (' . $pageUidList . ')',
151 '',
152 '',
153 '',
154 'uid'
155 );
156
157 if (is_array($rows)) {
158 $templates = $rows;
159 }
160
161 return $templates;
162 }
163 }