Added feature #15754: ExtDirect in Frontend (Thanks to Stefan Galinksi)
[Packages/TYPO3.CMS.git] / t3lib / extjs / class.t3lib_extjs_extdirectapi.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010 Sebastian Kurfürst <sebastian@typo3.org>
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 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * Ext Direct API Generator
30 *
31 * @author Sebastian Kurfürst <sebastian@typo3.org>
32 * @author Stefan Galinski <stefan.galinski@gmail.com>
33 * @package TYPO3
34 */
35 class t3lib_extjs_ExtDirectApi {
36 /**
37 * @var array
38 */
39 protected $settings = array();
40
41 /**
42 * Constructs this object.
43 */
44 public function __construct() {
45 if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect']) && is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'])) {
46 $this->settings = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'];
47 }
48 }
49
50 /**
51 * Parses the ExtDirect configuration array "$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect']"
52 * and feeds the given typo3ajax instance with the resulting information. The get parameter
53 * "namespace" will be used to filter the configuration.
54 *
55 * This method makes usage of the reflection mechanism to fetch the methods inside the
56 * defined classes together with their amount of parameters. This information are building
57 * the API and are required by ExtDirect. The result is cached to improve the overall
58 * performance.
59 *
60 * @param array $ajaxParams ajax parameters
61 * @param TYPO3AJAX $ajaxObj typo3ajax instance
62 * @return void
63 */
64 public function getAPI($ajaxParams, TYPO3AJAX $ajaxObj) {
65 $filterNamespace = t3lib_div::_GET('namespace');
66
67 // Check GET-parameter no_cache and extCache setting
68 $extCache = isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['extCache']) && (
69 $GLOBALS['TYPO3_CONF_VARS']['SYS']['extCache'] === 0 ||
70 $GLOBALS['TYPO3_CONF_VARS']['SYS']['extCache'] === '0'
71 );
72 $noCache = t3lib_div::_GET('no_cache') ? TRUE : $extCache;
73
74 // look up into the cache
75 $cacheIdentifier = 'ExtDirectApi';
76 $cacheHash = md5($cacheIdentifier . $filterNamespace .
77 serialize($this->settings) . TYPO3_MODE);
78
79 // with no_cache always generate the javascript content
80 $cacheContent = $noCache ? '' : t3lib_pageSelect::getHash($cacheHash);
81
82 // generate the javascript content if it wasn't found inside the cache and cache it!
83 if (!$cacheContent) {
84 $javascriptNamespaces = $this->generateAPI($filterNamespace);
85 if (!empty($javascriptNamespaces)) {
86 t3lib_pageSelect::storeHash(
87 $cacheHash,
88 serialize($javascriptNamespaces),
89 $cacheIdentifier
90 );
91 }
92 } else {
93 $javascriptNamespaces = unserialize($cacheContent);
94 }
95
96 // return the generated javascript API configuration
97 if (count($javascriptNamespaces)) {
98 $setup = '
99 if (typeof Ext.app.ExtDirectAPI !== "object") {
100 Ext.app.ExtDirectAPI = {};
101 }
102
103 if (typeof Object.extend !== "function") {
104 Object.extend = function(destination, source) {
105 for (var property in source) {
106 destination[property] = source[property];
107 }
108 return destination;
109 };
110 }
111 ';
112
113 $ajaxObj->setContent($javascriptNamespaces);
114 $ajaxObj->setContentFormat('javascript');
115 $ajaxObj->setJavascriptCallbackWrap(
116 $setup . 'Ext.app.ExtDirectAPI = Object.extend(Ext.app.ExtDirectAPI, |);'
117 );
118 } else {
119 if ($filterNamespace) {
120 // namespace error
121 $errorMessage = sprintf($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:ExtDirect.namespaceError'),
122 __CLASS__, $filterNamespace
123 );
124 }
125 else {
126 // no namespace given
127 $errorMessage = sprintf($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:ExtDirect.noNamespace'),
128 __CLASS__
129 );
130 }
131 // make js multiline message
132 $msg = t3lib_div::trimExplode(LF, str_replace('"', '\"', $errorMessage), TRUE);
133 $errorMessage = '';
134 foreach ($msg as $line) {
135 $errorMessage .= '"' . $line . '" + ' . LF;
136 }
137 $errorMessage = substr(trim($errorMessage), 0, -1);
138 //generate the javascript
139 $ajaxObj->setContentFormat('javascript');
140 $ajaxObj->setJavascriptCallbackWrap('
141 errorMessage = ' . $errorMessage . ';
142 if (typeof console === "object") {
143 console.log(errorMessage);
144 } else {
145 alert(errorMessage);
146 }
147 ');
148 }
149 }
150
151 /**
152 * Generates the API that is configured inside the ExtDirect configuration
153 * array "$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect']".
154 *
155 * @param string $filerNamespace namespace that should be loaded like TYPO3.Backend
156 * @return array javascript API configuration
157 */
158 protected function generateAPI($filterNamespace) {
159 $javascriptNamespaces = array();
160 if (is_array($this->settings)) {
161 foreach ($this->settings as $javascriptName => $className) {
162 $splittedJavascriptName = explode('.', $javascriptName);
163 $javascriptObjectName = array_pop($splittedJavascriptName);
164 $javascriptNamespace = implode('.', $splittedJavascriptName);
165
166 // only items inside the wanted namespace
167 if (strpos($javascriptNamespace, $filterNamespace) !== 0) {
168 continue;
169 }
170
171 if (!isset($javascriptNamespaces[$javascriptNamespace])) {
172 $javascriptNamespaces[$javascriptNamespace] = array(
173 'url' => $this->getRoutingUrl($javascriptNamespace),
174 'type' => 'remoting',
175 'actions' => array(),
176 'namespace' => $javascriptNamespace
177 );
178 }
179
180 $serverObject = t3lib_div::getUserObj($className, FALSE);
181 $javascriptNamespaces[$javascriptNamespace]['actions'][$javascriptObjectName] = array();
182 foreach (get_class_methods($serverObject) as $methodName) {
183 $reflectionMethod = new ReflectionMethod($serverObject, $methodName);
184 $numberOfParameters = $reflectionMethod->getNumberOfParameters();
185 $docHeader = $reflectionMethod->getDocComment();
186 $formHandler = (strpos($docHeader, '@formHandler') !== FALSE);
187
188 $javascriptNamespaces[$javascriptNamespace]['actions'][$javascriptObjectName][] = array(
189 'name' => $methodName,
190 'len' => $numberOfParameters,
191 'formHandler' => $formHandler
192 );
193 }
194 }
195 }
196
197 return $javascriptNamespaces;
198 }
199
200 /**
201 * Returns the convenient path for the routing Urls based on the TYPO3 mode.
202 *
203 * @param string $namespace
204 * @return string
205 */
206 public function getRoutingUrl($namespace) {
207 $url = '';
208 if (TYPO3_MODE === 'FE') {
209 $url = t3lib_div::locationHeaderUrl('?eID=ExtDirect&action=route&namespace=');
210 } else {
211 $url = t3lib_div::locationHeaderUrl('ajax.php?ajaxID=ExtDirect::route&namespace=');
212 }
213 $url .= rawurlencode($namespace);
214
215 return $url;
216 }
217 }
218
219 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_extjs_extdirectapi.php']) {
220 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_extjs_extdirectapi.php']);
221 }
222
223 ?>