‪TYPO3CMS  ‪main
GeneratorFrontend.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
26 
33 {
34  public function ‪create(string $basePath = '', int $hidden = 1): void
35  {
36  $recordFinder = GeneralUtility::makeInstance(RecordFinder::class);
37  $kauderWelsch = GeneralUtility::makeInstance(KauderwelschService::class);
38 
39  // Create should not be called if demo frontend data exists already
40  if (count($recordFinder->findUidsOfFrontendPages())) {
41  throw new ‪Exception(
42  'Can not create a second styleguide frontend record tree',
43  1626357141
44  );
45  }
46 
47  // Add files
48  $this->‪addToFal([
49  'bus_lane.jpg',
50  'telephone_box.jpg',
51  'underground.jpg',
52  ], 'EXT:styleguide/Resources/Public/Images/Pictures/', 'styleguide_frontend');
53 
54  // Add entry page on top level
55  $newIdOfEntryPage = ‪StringUtility::getUniqueId('NEW');
56  $newIdOfUserFolder = ‪StringUtility::getUniqueId('NEW');
57  $newIdOfRootTsTemplate = ‪StringUtility::getUniqueId('NEW');
58  $newIdOfEntryContent = ‪StringUtility::getUniqueId('NEW');
59  $newIdOfCategory = ‪StringUtility::getUniqueId('NEW');
60  $newIdOfFrontendGroup = ‪StringUtility::getUniqueId('NEW');
61  $newIdOfFrontendUser = ‪StringUtility::getUniqueId('NEW');
62 
63  $data = [
64  'pages' => [
65  $newIdOfEntryPage => [
66  'title' => 'styleguide frontend demo',
67  'pid' => 0 - $this->‪getUidOfLastTopLevelPage(),
68  // Define page as styleguide frontend
69  'tx_styleguide_containsdemo' => 'tx_styleguide_frontend_root',
70  'is_siteroot' => 1,
71  'hidden' => $hidden,
72  ],
73  // Storage for for frontend users
74  $newIdOfUserFolder => [
75  'title' => 'frontend user',
76  'pid' => $newIdOfEntryPage,
77  'tx_styleguide_containsdemo' => 'tx_styleguide_frontend',
78  'hidden' => 0,
79  'doktype' => 254,
80  ],
81  ],
82  'sys_template' => [
83  $newIdOfRootTsTemplate => [
84  'title' => 'root styleguide frontend demo',
85  'root' => 1,
86  'clear' => 3,
87  'include_static_file' => 'EXT:styleguide/Configuration/TypoScript,EXT:seo/Configuration/TypoScript/XmlSitemap',
88  'constants' => '',
89  'config' => '',
90  'pid' => $newIdOfEntryPage,
91  ],
92  ],
93  'tt_content' => [
94  $newIdOfEntryContent => [
95  'header' => 'TYPO3 Styleguide Frontend',
96  'CType' => 'text',
97  'bodytext' => 'This is the generated frontend for the Styleguide Extension. This consists of all default content elements of the TYPO3 Core.',
98  'pid' => $newIdOfEntryPage,
99  'tx_styleguide_containsdemo' => 'tx_styleguide_frontend',
100  ],
101  ],
102  'sys_category' => [
103  $newIdOfCategory => [
104  'title' => 'Styleguide Demo Category',
105  'pid' => $newIdOfEntryPage,
106  ],
107  ],
108  'fe_groups' => [
109  $newIdOfFrontendGroup => [
110  'title' => 'Styleguide Frontend Demo',
111  'hidden' => 0,
112  'pid' => $newIdOfUserFolder,
113  'tx_styleguide_containsdemo' => 'tx_styleguide_frontend',
114  ],
115  ],
116  'fe_users' => [
117  $newIdOfFrontendUser => [
118  'username' => 'styleguide-frontend-demo',
119  'hidden' => 0,
120  'usergroups' => $newIdOfFrontendGroup,
121  // Password of demo frontend user: 'password'
122  'password' => '$argon2i$v=19$m=65536,t=16,p=1$VjFaWDFGMmh6RlNEWjY2Vw$Vp5lFrbe8/GNwIrlXnUm6m2d9JJPfkQudnD8sBQKG9A',
123  'pid' => $newIdOfUserFolder,
124  'tx_styleguide_containsdemo' => 'tx_styleguide_frontend',
125  ],
126  ],
127  ];
128 
129  $neighborPage = $newIdOfEntryPage;
130  $contentData = $this->‪getElementContent();
131 
132  foreach ($contentData as $type => $ce) {
133  $newIdOfPage = ‪StringUtility::getUniqueId('NEW');
134  $data['pages'][$newIdOfPage] = [
135  'title' => $type,
136  'tx_styleguide_containsdemo' => 'tx_styleguide_frontend',
137  'hidden' => 0,
138  'abstract' => $kauderWelsch->getLoremIpsum(),
139  'pid' => $neighborPage,
140  'categories' => $newIdOfCategory,
141  ];
142 
143  // Set keyword for menu_related_pages to show up
144  if (substr($type, 0, 5) === 'menu_') {
145  $data['pages'][$newIdOfPage]['keywords'] = 'Bacon';
146  }
147 
148  foreach ($ce as $content) {
149  $newIdOfContent = ‪StringUtility::getUniqueId('NEW');
150  $data['tt_content'][$newIdOfContent] = $content;
151  $data['tt_content'][$newIdOfContent]['CType'] = $type;
152  $data['tt_content'][$newIdOfContent]['pid'] = $newIdOfPage;
153  $data['tt_content'][$newIdOfContent]['categories'] = $newIdOfCategory;
154 
155  if ($type === 'menu_categorized_content') {
156  $data['tt_content'][$newIdOfContent]['selected_categories'] = $newIdOfCategory;
157  $data['tt_content'][$newIdOfContent]['category_field'] = 'categories';
158  }
159 
160  if ($type === 'menu_categorized_pages') {
161  $data['tt_content'][$newIdOfContent]['selected_categories'] = $newIdOfCategory;
162  }
163 
164  $data['tt_content'][$newIdOfContent]['tx_styleguide_containsdemo'] = 'tx_styleguide_frontend';
165  }
166  }
167 
168  $this->‪executeDataHandler($data);
169 
170  // Create site configuration for frontend
171  if (isset(‪$GLOBALS['TYPO3_REQUEST']) && empty($basePath)) {
172  $port = ‪$GLOBALS['TYPO3_REQUEST']->getUri()->getPort() ? ':' . ‪$GLOBALS['TYPO3_REQUEST']->getUri()->getPort() : '';
173  $domain = ‪$GLOBALS['TYPO3_REQUEST']->getUri()->getScheme() . '://' . ‪$GLOBALS['TYPO3_REQUEST']->getUri()->getHost() . $port . '/';
174  } else {
175  // On cli there is no TYPO3_REUQEST object
176  $domain = empty($basePath) ? '/' : $basePath;
177  }
178  $topPageUid = (int)$recordFinder->findUidsOfFrontendPages(['tx_styleguide_frontend_root'])[0];
179  $this->‪createSiteConfiguration($topPageUid, $domain, 'Styleguide frontend demo');
180 
182  $this->‪populateTtContentPages();
184  $this->‪populateFeUserAndGroup();
185  }
186 
187  public function delete(): void
188  {
189  $recordFinder = GeneralUtility::makeInstance(RecordFinder::class);
190  $commands = [];
191 
192  // Delete frontend pages - also deletes tt_content, sys_category and sys_file_references
193  $frontendPagesUids = $recordFinder->findUidsOfFrontendPages();
194  if (!empty($frontendPagesUids)) {
195  foreach ($frontendPagesUids as $page) {
196  $commands['pages'][(int)$page]['delete'] = 1;
197  }
198  }
199 
200  // Delete site configuration
201  try {
202  $rootUid = $recordFinder->findUidsOfFrontendPages(['tx_styleguide_frontend_root']);
203 
204  if (!empty($rootUid)) {
205  $site = GeneralUtility::makeInstance(SiteFinder::class)->getSiteByRootPageId((int)$rootUid[0]);
206  ‪$identifier = $site->getIdentifier();
207  GeneralUtility::makeInstance(SiteConfiguration::class)->delete(‪$identifier);
208  }
209  } catch (‪SiteNotFoundException $e) {
210  // Do not throw a thing if site config does not exist
211  }
212  // Delete records data
213  $this->‪executeDataHandler([], $commands);
214 
215  // Delete created files
216  $this->‪deleteFalFolder('styleguide_frontend');
217  }
218 
224  protected function ‪getElementContent(): array
225  {
227  $kauderWelsch = GeneralUtility::makeInstance(KauderwelschService::class);
228 
229  return [
230  'bullets' => [
231  [
232  'header' => 'A bullet list',
233  'bodytext' => "Item 1\nItem 2\nItem 3\n",
234  ],
235  [
236  'header' => 'Another bullet list',
237  'bodytext' => "Item 4\nItem 5\nItem 6\n",
238  ],
239  ],
240  'div' => [
241  [
242  'header' => $kauderWelsch->getLoremIpsum(),
243  ],
244  [
245  'header' => $kauderWelsch->getLoremIpsum(),
246  ],
247  ],
248  'header' => [
249  [
250  'header' => $kauderWelsch->getLoremIpsum(),
251  ],
252  [
253  'header' => $kauderWelsch->getLoremIpsum(),
254  'header_layout' => 2,
255  ],
256  [
257  'header' => $kauderWelsch->getLoremIpsum(),
258  'header_layout' => 3,
259  ],
260  ],
261  'text' => [
262  [
263  'header' => $kauderWelsch->getLoremIpsum(),
264  'subheader' => $kauderWelsch->getLoremIpsum(),
265  'bodytext' => $kauderWelsch->getLoremIpsumHtml() . ' ' . $kauderWelsch->getLoremIpsumHtml(),
266  ],
267  [
268  'header' => $kauderWelsch->getLoremIpsum(),
269  'header_layout' => 3,
270  'bodytext' => $kauderWelsch->getLoremIpsumHtml() . ' ' . $kauderWelsch->getLoremIpsumHtml(),
271  ],
272  ],
273  'textpic' => [ // @todo add images
274  [
275  'header' => $kauderWelsch->getLoremIpsum(),
276  'header_layout' => 5,
277  'subheader' => $kauderWelsch->getLoremIpsum(),
278  'bodytext' => $kauderWelsch->getLoremIpsumHtml() . ' ' . $kauderWelsch->getLoremIpsumHtml(),
279  ],
280  [
281  'header' => $kauderWelsch->getLoremIpsum(),
282  'header_layout' => 2,
283  'bodytext' => $kauderWelsch->getLoremIpsumHtml() . ' ' . $kauderWelsch->getLoremIpsumHtml(),
284  ],
285  ],
286  'textmedia' => [
287  [
288  'header' => $kauderWelsch->getLoremIpsum(),
289  'header_layout' => 5,
290  'subheader' => $kauderWelsch->getLoremIpsum(),
291  'bodytext' => $kauderWelsch->getLoremIpsumHtml() . ' ' . $kauderWelsch->getLoremIpsumHtml(),
292  ],
293  [
294  'header' => $kauderWelsch->getLoremIpsum(),
295  'header_layout' => 2,
296  'bodytext' => $kauderWelsch->getLoremIpsumHtml() . ' ' . $kauderWelsch->getLoremIpsumHtml(),
297  'imageorient' => 25,
298  ],
299  ],
300  'image' => [
301  [
302  'header' => $kauderWelsch->getLoremIpsum(),
303  'bodytext' => $kauderWelsch->getLoremIpsumHtml() . ' ' . $kauderWelsch->getLoremIpsumHtml(),
304  ],
305  ],
306  'html' => [
307  [
308  'header' => $kauderWelsch->getLoremIpsum(),
309  'bodytext' => $kauderWelsch->getLoremIpsumHtml() . ' ' . $kauderWelsch->getLoremIpsumHtml(),
310  ],
311  ],
312  'table' => [
313  [
314  'header' => $kauderWelsch->getLoremIpsum(),
315  'bodytext' => "row1 col1|row1 col2|row1 col3|row1 col4\nrow2 col1|row2 col2|row2 col3|row2 col4",
316  ],
317  [
318  'header' => $kauderWelsch->getLoremIpsum(),
319  'bodytext' => "row1 col1|row1 col2|row1 col3|row1 col4\nrow2 col1|row2 col2|row2 col3|row2 col4\nrow3 col1|row3 col2|row3 col3|row3 col4\nrow4 col1|row4 col2|row4 col3|row4 col4",
320  ],
321  ],
322  'felogin_login' => [
323  [
324  'header' => $kauderWelsch->getLoremIpsum(),
325  ],
326  ],
327  'form_formframework' => [
328  [
329  'header' => 'Advanced form - all fields',
330  'pi_flexform' => [
331  'data' => [
332  'sDEF' => [
333  'lDEF' => [
334  'settings.persistenceIdentifier' => [
335  'vDEF' => 'EXT:styleguide/Resources/Private/Forms/allfields.form.yaml',
336  ],
337  ],
338  ],
339  ],
340  ],
341  ],
342  [
343  'header' => 'Simple form',
344  'pi_flexform' => [
345  'data' => [
346  'sDEF' => [
347  'lDEF' => [
348  'settings.persistenceIdentifier' => [
349  'vDEF' => 'EXT:styleguide/Resources/Private/Forms/simpleform.form.yaml',
350  ],
351  ],
352  ],
353  ],
354  ],
355  ],
356  ],
357  'indexedsearch_pi2' => [
358  [
359  'header' => 'Indexed Search',
360  ],
361  ],
362  'shortcut' => [
363  [
364  'header' => 'Shortcut',
365  'records' => '', // UIDs 856,857,849
366  ],
367  ],
368  'uploads' => [
369  [
370  'header' => 'Uploads',
371  ],
372  ],
373  'menu_categorized_pages' => [
374  [
375  'header' => 'Menu categorized pages',
376  'records' => '', // UIDs 856,857,849
377  ],
378  ],
379  'menu_categorized_content' => [
380  [
381  'header' => 'Menu categorized content',
382  'records' => '', // UIDs 856,857,849
383  ],
384  ],
385  'menu_pages' => [
386  [
387  'header' => 'Menu pages',
388  'records' => '', // UIDs 856,857,849
389  ],
390  ],
391  'menu_subpages' => [
392  [
393  'header' => 'Menu subpages',
394  'records' => '', // UIDs 856,857,849
395  ],
396  ],
397  'menu_sitemap' => [
398  [
399  'header' => 'Menu sitemap',
400  'records' => '', // UIDs 856,857,849
401  ],
402  ],
403  'menu_section' => [
404  [
405  'header' => 'Menu section',
406  'records' => '', // UIDs 856,857,849
407  ],
408  ],
409  'menu_abstract' => [
410  [
411  'header' => 'Menu abstract',
412  'records' => '', // UIDs 856,857,849
413  ],
414  ],
415  'menu_recently_updated' => [
416  [
417  'header' => 'Menu recently updated',
418  'records' => '', // UIDs 856,857,849
419  ],
420  ],
421  'menu_related_pages' => [
422  [
423  'header' => 'Menu related pages',
424  'records' => '', // UIDs 856,857,849
425  ],
426  ],
427  'menu_section_pages' => [
428  [
429  'header' => 'Menu section pages',
430  'records' => '', // UIDs 856,857,849
431  ],
432  ],
433  'menu_sitemap_pages' => [
434  [
435  'header' => 'Menu sitemap pages',
436  'records' => '', // UIDs 856,857,849
437  ],
438  ],
439  ];
440  }
441 
445  protected function ‪populateSysFileReference(): void
446  {
448  $recordFinder = GeneralUtility::makeInstance(RecordFinder::class);
449  $files = $recordFinder->findDemoFileObjects('styleguide_frontend');
450 
451  $recordData = [];
452  foreach ($recordFinder->findTtContent() as $content) {
453  switch ($content['CType']) {
454  case 'textmedia':
455  $fieldname = 'assets';
456  break;
457  case 'uploads':
458  $fieldname = 'media';
459  break;
460  default:
461  $fieldname = 'image';
462  }
463 
464  foreach ($files as $image) {
465  $newId = ‪StringUtility::getUniqueId('NEW');
466  $recordData['sys_file_reference'][$newId] = [
467  'table_local' => 'sys_file',
468  'uid_local' => $image->getUid(),
469  'uid_foreign' => $content['uid'],
470  'tablenames' => 'tt_content',
471  'fieldname' => $fieldname,
472  'pid' => $content['pid'],
473  ];
474  }
475  }
476 
477  $this->‪executeDataHandler($recordData);
478  }
479 
483  protected function ‪populateTtContentPages(string $field = 'pages', int $count = 5): void
484  {
486  $recordFinder = GeneralUtility::makeInstance(RecordFinder::class);
487  $pages = $recordFinder->findUidsOfFrontendPages();
488  $contentElements = $recordFinder->findTtContent(['menu_pages', 'menu_subpages', 'menu_section', 'menu_abstract', 'menu_recently_updated', 'menu_section_pages', 'menu_sitemap_pages']);
489 
490  $recordData = [];
491  foreach ($contentElements as $content) {
492  $recordData['tt_content'][$content['uid']] = [
493  $field => implode(',', array_slice($pages, 0, $count)),
494  ];
495  }
496 
497  $this->‪executeDataHandler($recordData);
498  }
499 
505  protected function ‪populateTtContentRecords(string $field = 'records'): void
506  {
507  $recordFinder = GeneralUtility::makeInstance(RecordFinder::class);
508 
509  $shortcutToElement = $recordFinder->findTtContent(['text'])[0]['uid'];
510  $contentElements = $recordFinder->findTtContent(['shortcut']);
511 
512  $recordData = [];
513  foreach ($contentElements as $content) {
514  $recordData['tt_content'][$content['uid']] = [
515  $field => $shortcutToElement,
516  ];
517  }
518 
519  $this->‪executeDataHandler($recordData);
520  }
521 
522  private function ‪populateFeUserAndGroup(): void
523  {
525  $recordFinder = GeneralUtility::makeInstance(RecordFinder::class);
526 
527  $ceFeLogin = $recordFinder->findTtContent(['felogin_login']);
528  $storageFeLogin = $recordFinder->findUidsOfFrontendPages(['tx_styleguide_frontend_root', 'tx_styleguide_frontend'], [254])[0];
529  $feUsers = $recordFinder->findFeUsers();
530  $feGroups = $recordFinder->findFeUserGroups();
531  $feGroupUids = implode(',', array_column($feGroups, 'uid'));
532 
533  $recordData = [];
534  foreach ($feUsers as $login) {
535  $recordData['fe_users'][$login['uid']] = [
536  'usergroup' => $feGroupUids,
537  ];
538  }
539 
540  // Set storage pid for content element to 'frontend user'
541  foreach ($ceFeLogin as $ce) {
542  $recordData['tt_content'][$ce['uid']] = [
543  'pi_flexform' => [
544  'data' => [
545  'sDEF' => [
546  'lDEF' => [
547  'settings.pages' => [
548  'vDEF' => $storageFeLogin,
549  ],
550  ],
551  ],
552  ],
553  ],
554  ];
555  }
556 
557  $this->‪executeDataHandler($recordData);
558  }
559 }
‪TYPO3\CMS\Styleguide\TcaDataGenerator\AbstractGenerator
Definition: AbstractGenerator.php:40
‪TYPO3\CMS\Styleguide\TcaDataGenerator\GeneratorFrontend\populateFeUserAndGroup
‪populateFeUserAndGroup()
Definition: GeneratorFrontend.php:522
‪TYPO3\CMS\Styleguide\Service\KauderwelschService
Definition: KauderwelschService.php:26
‪TYPO3\CMS\Styleguide\TcaDataGenerator\AbstractGenerator\addToFal
‪addToFal(array $files, string $from, string $to)
Definition: AbstractGenerator.php:175
‪TYPO3\CMS\Core\Exception\SiteNotFoundException
Definition: SiteNotFoundException.php:25
‪TYPO3\CMS\Core\Site\SiteFinder
Definition: SiteFinder.php:31
‪TYPO3\CMS\Core\Configuration\SiteConfiguration
Definition: SiteConfiguration.php:47
‪TYPO3\CMS\Styleguide\TcaDataGenerator\GeneratorFrontend\populateTtContentRecords
‪populateTtContentRecords(string $field='records')
Definition: GeneratorFrontend.php:505
‪TYPO3\CMS\Styleguide\TcaDataGenerator\AbstractGenerator\createSiteConfiguration
‪createSiteConfiguration(int $topPageUid, string $base='http://localhost/', string $title='styleguide demo')
Definition: AbstractGenerator.php:44
‪TYPO3\CMS\Styleguide\TcaDataGenerator\AbstractGenerator\getUidOfLastTopLevelPage
‪int getUidOfLastTopLevelPage()
Definition: AbstractGenerator.php:151
‪TYPO3\CMS\Styleguide\TcaDataGenerator\GeneratorFrontend\create
‪create(string $basePath='', int $hidden=1)
Definition: GeneratorFrontend.php:34
‪TYPO3\CMS\Styleguide\TcaDataGenerator\GeneratorFrontend\getElementContent
‪array getElementContent()
Definition: GeneratorFrontend.php:224
‪TYPO3\CMS\Styleguide\TcaDataGenerator\GeneratorFrontend\populateSysFileReference
‪populateSysFileReference()
Definition: GeneratorFrontend.php:445
‪TYPO3\CMS\Styleguide\TcaDataGenerator\GeneratorFrontend\populateTtContentPages
‪populateTtContentPages(string $field='pages', int $count=5)
Definition: GeneratorFrontend.php:483
‪TYPO3\CMS\Styleguide\TcaDataGenerator
Definition: AbstractGenerator.php:18
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Styleguide\TcaDataGenerator\Exception
Definition: Exception.php:25
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Styleguide\TcaDataGenerator\GeneratorFrontend
Definition: GeneratorFrontend.php:33
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:24
‪TYPO3\CMS\Styleguide\TcaDataGenerator\AbstractGenerator\executeDataHandler
‪executeDataHandler(array $data=[], array $commands=[])
Definition: AbstractGenerator.php:212
‪TYPO3\CMS\Styleguide\TcaDataGenerator\AbstractGenerator\deleteFalFolder
‪deleteFalFolder(string $path)
Definition: AbstractGenerator.php:198
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static getUniqueId(string $prefix='')
Definition: StringUtility.php:57