[TASK] Doctrine: Migrate EXT:sys_note
[Packages/TYPO3.CMS.git] / typo3 / sysext / sys_note / Classes / Core / Bootstrap.php
1 <?php
2 namespace TYPO3\CMS\SysNote\Core;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Core\Database\ConnectionPool;
19 use TYPO3\CMS\Core\Database\Query\QueryBuilder;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21
22 /**
23 * Bootstrap for note module
24 */
25 class Bootstrap
26 {
27 /**
28 * Do not touch if you are not sure what you are doing!
29 * @var array
30 */
31 protected $extbaseConfiguration = array(
32 'vendorName' => 'TYPO3\\CMS',
33 'extensionName' => 'SysNote',
34 'pluginName' => 'Note',
35 );
36
37 /**
38 * @var array
39 */
40 protected $currentGetArguments;
41
42 /**
43 * Bootstrap extbase and execute controller
44 *
45 * @param string $controllerName Controller to execute
46 * @param string $actionName Action to run
47 * @param array $arguments Arguments to pass to the controller action
48 * @return string
49 */
50 public function run($controllerName, $actionName, array $arguments = array())
51 {
52 if (!$this->expectOutput($arguments)) {
53 return '';
54 }
55 $arguments['controller'] = ucfirst(trim($controllerName));
56 $arguments['action'] = lcfirst(trim($actionName));
57 $this->overrideGetArguments($arguments);
58 /** @var $extbaseBootstrap \TYPO3\CMS\Extbase\Core\Bootstrap */
59 $extbaseBootstrap = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Core\Bootstrap::class);
60 $content = $extbaseBootstrap->run('', $this->extbaseConfiguration);
61 $this->revertGetArguments();
62 return $content;
63 }
64
65 /**
66 * Check if the note plugin expects output. If there are no sys_note records on the given
67 * pages, the extbase bootstrap doesn't have to run the complete plugin.
68 * This mechanism should increase the performance of the hooked backend modules heavily.
69 *
70 * @param array $arguments Arguments for the extbase plugin
71 * @return bool
72 */
73 protected function expectOutput(array $arguments = array())
74 {
75 // no pids set
76 if (!isset($arguments['pids']) || empty($arguments['pids']) || empty($GLOBALS['BE_USER']->user['uid'])) {
77 return false;
78 }
79 $pidList = GeneralUtility::intExplode(',', $arguments['pids'], true);
80 if (empty($pidList)) {
81 return false;
82 }
83 /** @var QueryBuilder $queryBuilder */
84 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_note');
85 $count = $queryBuilder
86 ->count('uid')
87 ->from('sys_note')
88 ->where($queryBuilder->expr()->in('pid', $pidList))
89 ->execute()
90 ->fetchColumn();
91 return (bool)$count;
92 }
93
94 /**
95 * Modify $_GET to force specific controller, action and arguments in
96 * extbase bootstrap process
97 *
98 * Note: Overwriting $_GET was the most simple solution here until extbase
99 * provides a clean way to force a controller and action in backend mode.
100 *
101 * @param array $arguments The arguments to set
102 * @return void
103 */
104 protected function overrideGetArguments(array $arguments)
105 {
106 $this->currentGetArguments = $_GET;
107 $_GET['tx_sysnote_note'] = $arguments;
108 }
109
110 /**
111 * Revert previously backuped get arguments
112 *
113 * @return void
114 */
115 protected function revertGetArguments()
116 {
117 if (is_array($this->currentGetArguments)) {
118 $_GET = $this->currentGetArguments;
119 }
120 }
121 }