Extbase:
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Utility / Plugin.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * Utilities to manage the plugins of an extension
27 *
28 * @package Extbase
29 * @subpackage extbase
30 * @version $ID:$
31 */
32 class Tx_Extbase_Utility_Plugin {
33
34 /**
35 * Add an Extbase PlugIn to TypoScript
36 *
37 * When adding a frontend plugin you will have to add both an entry to the TCA definition
38 * of tt_content table AND to the TypoScript template which must initiate the rendering.
39 * Since the static template with uid 43 is the "content.default" and practically always
40 * used for rendering the content elements it's very useful to have this function automatically
41 * adding the necessary TypoScript for calling your plugin. It will also work for the
42 * extension "css_styled_content"
43 * FOR USE IN ext_tables.php FILES
44 * Usage: 2
45 *
46 * @param string $extensionName is the extension name in UpperCamelCase
47 * @param string $pluginName must be a unique id for your plugin in UpperCamelCase (the string length of the extension key added to the length of the plugin name should be less than 32!)
48 * @param string $pluginTitle is a speaking title of the plugin that will be displayed in the drop down menu in the backend
49 * @param string $controllerActions is an array of allowed combinations of controller and action stored in an array (controller name as key and a comma separated list of action names as value, the first controller and its first action is chosen as default)
50 * @param string $nonCachableControllerActions is an optional array of controller name and action names which should not be cached (array as defined in $controllerActions)
51 * @param string $defaultControllerAction is an optional array controller name (as array key) and action name (as array value) that should be called as default
52 * @return void
53 */
54 public static function registerPlugin($extensionName, $pluginName, $pluginTitle, array $controllerActions, array $nonCachableControllerActions = array()) {
55 if (empty($pluginName)) {
56 throw new InvalidArgumentException('The plugin name must not be empty', 1239891987);
57 }
58 if (empty($extensionName) || preg_match('/[A-Z][A-Za-z0-9]/', $extensionName) === 0) {
59 throw new InvalidArgumentException('The extension name was invalid (must not be empty and must match /[A-Z]][A-Za-z0-9]/)', 1239891989);
60 }
61 $pluginSignature = strtolower($extensionName) . '_' . strtolower($pluginName);
62
63 $controllerCounter = 1;
64 $hasMultipleActionsCounter = 0;
65 $controllers = '';
66 foreach ($controllerActions as $controller => $actionsList) {
67 $controllers .= '
68 ' . $controllerCounter . '.controller = ' . $controller . '
69 ' . $controllerCounter . '.actions = ' . $actionsList;
70 $controllerCounter++;
71 if (strpos($actionsList, ',') !== FALSE) {
72 $hasMultipleActionsCounter++;
73 }
74 }
75
76 $switchableControllerActions = '';
77 if ($controllerCounter > 1 || $hasMultipleActionsCounter > 0) {
78 $switchableControllerActions = '
79 switchableControllerActions {' . $controllers . '
80 }';
81 }
82
83 reset($controllerActions);
84 $defaultController = key($controllerActions);
85 $controller = '
86 controller = ' . $defaultController;
87 $defaultAction = array_shift(t3lib_div::trimExplode(',', current($controllerActions)));
88 $action = '
89 action = ' . $defaultAction;
90
91 $nonCachableActions = array();
92 if (!empty($nonCachableControllerActions[$defaultController])) {
93 $nonCachableActions = t3lib_div::trimExplode(',', $nonCachableControllerActions[$defaultController]);
94 }
95 $cachableActions = array_diff(t3lib_div::trimExplode(',', $controllerActions[$defaultController]), $nonCachableActions);
96
97 $contentObjectType = in_array($defaultAction, $nonCachableActions) ? 'USER_INT' : 'USER';
98
99 $conditions = '';
100 foreach ($controllerActions as $controllerName => $actionsList) {
101 if (!empty($nonCachableControllerActions[$controllerName])) {
102 $nonCachableActions = t3lib_div::trimExplode(',', $nonCachableControllerActions[$controllerName]);
103 $cachableActions = array_diff(t3lib_div::trimExplode(',', $controllerActions[$controllerName]), $nonCachableActions);
104 if (($contentObjectType == 'USER' && count($nonCachableActions) > 0)
105 || ($contentObjectType == 'USER_INT' && count($cachableActions) > 0)) {
106
107 $conditions .= '
108 [globalString: GP = tx_' . $pluginSignature . '|controller = ' . $controllerName . '] && [globalString: GP = tx_' . $pluginSignature . '|action = /' . implode('|', $contentObjectType === 'USER' ? $nonCachableActions : $cachableActions) . '/]
109 tt_content.list.20.' . $pluginSignature . ' = ' . ($contentObjectType === 'USER' ? 'USER_INT' : 'USER') . '
110 [global]
111 ';
112 }
113 }
114 }
115
116 $pluginContent = trim('
117 tt_content.list.20.' . $pluginSignature . ' = ' . $contentObjectType . '
118 tt_content.list.20.' . $pluginSignature . ' {
119 userFunc = tx_extbase_dispatcher->dispatch
120 pluginName = ' . $pluginName . '
121 extensionName = ' . $extensionName .
122 $controller .
123 $action .
124 $switchableControllerActions . '
125 }
126 ' . $conditions);
127
128 t3lib_extMgm::addTypoScript($extensionName, 'setup', '
129 # Setting ' . $extensionName . ' plugin TypoScript
130 ' . $pluginContent, 43);
131
132 t3lib_extMgm::addPlugin(array($pluginTitle, $pluginSignature), 'list_type');
133 }
134
135
136 }
137 ?>