d443ca26cc1a5006ba83136162c404d3d185651a
[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\DatabaseConnection;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * Bootstrap for note module
23 */
24 class Bootstrap
25 {
26 /**
27 * Do not touch if you are not sure what you are doing!
28 * @var array
29 */
30 protected $extbaseConfiguration = array(
31 'vendorName' => 'TYPO3\\CMS',
32 'extensionName' => 'SysNote',
33 'pluginName' => 'Note',
34 );
35
36 /**
37 * @var array
38 */
39 protected $currentGetArguments;
40
41 /**
42 * @var DatabaseConnection
43 */
44 protected $databaseConnection;
45
46 /**
47 * @param DatabaseConnection $databaseConnection
48 */
49 public function __construct(DatabaseConnection $databaseConnection = null)
50 {
51 $this->databaseConnection = $databaseConnection ?: $GLOBALS['TYPO3_DB'];
52 }
53
54 /**
55 * Bootstrap extbase and execute controller
56 *
57 * @param string $controllerName Controller to execute
58 * @param string $actionName Action to run
59 * @param array $arguments Arguments to pass to the controller action
60 * @return string
61 */
62 public function run($controllerName, $actionName, array $arguments = array())
63 {
64 if (!$this->expectOutput($arguments)) {
65 return '';
66 }
67 $arguments['controller'] = ucfirst(trim($controllerName));
68 $arguments['action'] = lcfirst(trim($actionName));
69 $this->overrideGetArguments($arguments);
70 /** @var $extbaseBootstrap \TYPO3\CMS\Extbase\Core\Bootstrap */
71 $extbaseBootstrap = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Core\Bootstrap::class);
72 $content = $extbaseBootstrap->run('', $this->extbaseConfiguration);
73 $this->revertGetArguments();
74 return $content;
75 }
76
77 /**
78 * Check if the note plugin expects output. If there are no sys_note records on the given
79 * pages, the extbase bootstrap doesn't have to run the complete plugin.
80 * This mechanism should increase the performance of the hooked backend modules heavily.
81 *
82 * @param array $arguments Arguments for the extbase plugin
83 * @return bool
84 */
85 protected function expectOutput(array $arguments = array())
86 {
87 // no pids set
88 if (!isset($arguments['pids']) || empty($arguments['pids']) || empty($GLOBALS['BE_USER']->user['uid'])) {
89 return false;
90 }
91 $pidList = $this->databaseConnection->cleanIntList($arguments['pids']);
92 if (empty($pidList)) {
93 return false;
94 }
95 // check if there are records
96 return ($this->databaseConnection->exec_SELECTcountRows('*', 'sys_note', 'pid IN (' . $pidList . ')' . BackendUtility::deleteClause('sys_note')) > 0);
97 }
98
99 /**
100 * Modify $_GET to force specific controller, action and arguments in
101 * extbase bootstrap process
102 *
103 * Note: Overwriting $_GET was the most simple solution here until extbase
104 * provides a clean way to force a controller and action in backend mode.
105 *
106 * @param array $arguments The arguments to set
107 * @return void
108 */
109 protected function overrideGetArguments(array $arguments)
110 {
111 $this->currentGetArguments = $_GET;
112 $_GET['tx_sysnote_note'] = $arguments;
113 }
114
115 /**
116 * Revert previously backuped get arguments
117 *
118 * @return void
119 */
120 protected function revertGetArguments()
121 {
122 if (is_array($this->currentGetArguments)) {
123 $_GET = $this->currentGetArguments;
124 }
125 }
126 }