6cb7c58c02f8883235b770f3514a17ce15fc44fc
[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 . serialize($this->settings));
77 // with no_cache always generate the javascript content
78 $cacheContent = $noCache ? '' : t3lib_pageSelect::getHash($cacheHash);
79
80 // generate the javascript content if it wasn't found inside the cache and cache it!
81 if (!$cacheContent) {
82 $javascriptNamespaces = $this->generateAPI($filterNamespace);
83 if (!empty($javascriptNamespaces)) {
84 t3lib_pageSelect::storeHash(
85 $cacheHash,
86 serialize($javascriptNamespaces),
87 $cacheIdentifier
88 );
89 }
90 } else {
91 $javascriptNamespaces = unserialize($cacheContent);
92 }
93
94 // return the generated javascript API configuration
95 if (count($javascriptNamespaces)) {
96 $setup = '
97 if (typeof Ext.app.ExtDirectAPI !== "object") {
98 Ext.app.ExtDirectAPI = {};
99 }
100
101 if (typeof Object.extend !== "function") {
102 Object.extend = function(destination, source) {
103 for (var property in source) {
104 destination[property] = source[property];
105 }
106 return destination;
107 };
108 }
109 ';
110
111 $ajaxObj->setContent($javascriptNamespaces);
112 $ajaxObj->setContentFormat('javascript');
113 $ajaxObj->setJavascriptCallbackWrap(
114 $setup . 'Ext.app.ExtDirectAPI = Object.extend(Ext.app.ExtDirectAPI, |);'
115 );
116 } else {
117 if ($filterNamespace) {
118 // namespace error
119 $errorMessage = sprintf($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:ExtDirect.namespaceError'),
120 __CLASS__, $filterNamespace
121 );
122 }
123 else {
124 // no namespace given
125 $errorMessage = sprintf($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:ExtDirect.noNamespace'),
126 __CLASS__
127 );
128 }
129 // make js multiline message
130 $msg = t3lib_div::trimExplode(LF, str_replace('"', '\"', $errorMessage), TRUE);
131 $errorMessage = '';
132 foreach ($msg as $line) {
133 $errorMessage .= '"' . $line . '" + ' . LF;
134 }
135 $errorMessage = substr(trim($errorMessage), 0, -1);
136 //generate the javascript
137 $ajaxObj->setContentFormat('javascript');
138 $ajaxObj->setJavascriptCallbackWrap('
139 errorMessage = ' . $errorMessage . ';
140 if (typeof console === "object") {
141 console.log(errorMessage);
142 } else {
143 alert(errorMessage);
144 }
145 ');
146 }
147 }
148
149 /**
150 * Generates the API that is configured inside the ExtDirect configuration
151 * array "$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect']".
152 *
153 * @param string $filerNamespace namespace that should be loaded like TYPO3.Backend
154 * @return array javascript API configuration
155 */
156 protected function generateAPI($filterNamespace) {
157 $javascriptNamespaces = array();
158 if (is_array($this->settings)) {
159 foreach ($this->settings as $javascriptName => $className) {
160 $splittedJavascriptName = explode('.', $javascriptName);
161 $javascriptObjectName = array_pop($splittedJavascriptName);
162 $javascriptNamespace = implode('.', $splittedJavascriptName);
163
164 // only items inside the wanted namespace
165 if (strpos($javascriptNamespace, $filterNamespace) !== 0) {
166 continue;
167 }
168
169 if (!isset($javascriptNamespaces[$javascriptNamespace])) {
170 $javascriptNamespaces[$javascriptNamespace] = array(
171 'url' => t3lib_div::locationHeaderUrl('ajax.php?ajaxID=ExtDirect::route&namespace=') . rawurlencode($javascriptNamespace),
172 'type' => 'remoting',
173 'actions' => array(),
174 'namespace' => $javascriptNamespace
175 );
176 }
177
178 $serverObject = t3lib_div::getUserObj($className, FALSE);
179 $javascriptNamespaces[$javascriptNamespace]['actions'][$javascriptObjectName] = array();
180 foreach (get_class_methods($serverObject) as $methodName) {
181 $reflectionMethod = new ReflectionMethod($serverObject, $methodName);
182 $numberOfParameters = $reflectionMethod->getNumberOfParameters();
183 $docHeader = $reflectionMethod->getDocComment();
184 $formHandler = (strpos($docHeader, '@formHandler') !== FALSE);
185
186 $javascriptNamespaces[$javascriptNamespace]['actions'][$javascriptObjectName][] = array(
187 'name' => $methodName,
188 'len' => $numberOfParameters,
189 'formHandler' => $formHandler
190 );
191 }
192 }
193 }
194
195 return $javascriptNamespaces;
196 }
197 }
198
199 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_extjs_extdirectapi.php']) {
200 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_extjs_extdirectapi.php']);
201 }
202
203 ?>