28432ac9355c5327e7b01e04fda124cf3f672b91
[Packages/TYPO3.CMS.git] / t3lib / extjs / class.t3lib_extjs_extdirectapi.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010-2011 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 $ajaxObj->setContent(array());
66 }
67
68 // enable caching
69 $expireDate = date('r', $GLOBALS['EXEC_TIME'] + 3600 * 24 * 30);
70 header('Expires: ' . $expireDate);
71 header('Cache-control: public');
72 header('Pragma:');
73
74 // return the generated javascript API configuration
75 if (count($javascriptNamespaces)) {
76 $setup = '
77 if (typeof Ext.app.ExtDirectAPI !== "object") {
78 Ext.app.ExtDirectAPI = {};
79 }
80
81 if (typeof Object.extend !== "function") {
82 Object.extend = function(destination, source) {
83 for (var property in source) {
84 destination[property] = source[property];
85 }
86 return destination;
87 };
88 }
89 ';
90
91 $ajaxObj->setContent($javascriptNamespaces);
92 $ajaxObj->setContentFormat('javascript');
93 $ajaxObj->setJavascriptCallbackWrap(
94 $setup . 'Ext.app.ExtDirectAPI = Object.extend(Ext.app.ExtDirectAPI, |);'
95 );
96 } else {
97 if ($filterNamespace) {
98 // namespace error
99 $errorMessage = sprintf($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:ExtDirect.namespaceError'),
100 __CLASS__, $filterNamespace
101 );
102 }
103 else {
104 // no namespace given
105 $errorMessage = sprintf($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:ExtDirect.noNamespace'),
106 __CLASS__
107 );
108 }
109 // make js multiline message
110 $msg = t3lib_div::trimExplode(LF, str_replace('"', '\"', $errorMessage), TRUE);
111 $errorMessage = '';
112 foreach ($msg as $line) {
113 $errorMessage .= '"' . $line . '" + ' . LF;
114 }
115 $errorMessage = substr(trim($errorMessage), 0, -1);
116 //generate the javascript
117 $ajaxObj->setContentFormat('javascript');
118 $ajaxObj->setJavascriptCallbackWrap('
119 errorMessage = ' . $errorMessage . ';
120 if (typeof console === "object") {
121 console.log(errorMessage);
122 } else {
123 alert(errorMessage);
124 }
125 ');
126 }
127 }
128
129 /**
130 * Generates the API that is configured inside the ExtDirect configuration
131 * array "$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect']".
132 *
133 * @param string $filerNamespace namespace that should be loaded like TYPO3.Backend
134 * @return array javascript API configuration
135 */
136 protected function generateAPI($filterNamespace) {
137 $javascriptNamespaces = array();
138 if (is_array($this->settings)) {
139 foreach ($this->settings as $javascriptName => $className) {
140 $splittedJavascriptName = explode('.', $javascriptName);
141 $javascriptObjectName = array_pop($splittedJavascriptName);
142 $javascriptNamespace = implode('.', $splittedJavascriptName);
143
144 // only items inside the wanted namespace
145 if (strpos($javascriptNamespace, $filterNamespace) !== 0) {
146 continue;
147 }
148
149 if (!isset($javascriptNamespaces[$javascriptNamespace])) {
150 $javascriptNamespaces[$javascriptNamespace] = array(
151 'url' => $this->getRoutingUrl($javascriptNamespace),
152 'type' => 'remoting',
153 'actions' => array(),
154 'namespace' => $javascriptNamespace
155 );
156 }
157
158 $serverObject = t3lib_div::getUserObj($className, FALSE);
159 $javascriptNamespaces[$javascriptNamespace]['actions'][$javascriptObjectName] = array();
160 foreach (get_class_methods($serverObject) as $methodName) {
161 $reflectionMethod = new ReflectionMethod($serverObject, $methodName);
162 $numberOfParameters = $reflectionMethod->getNumberOfParameters();
163 $docHeader = $reflectionMethod->getDocComment();
164 $formHandler = (strpos($docHeader, '@formHandler') !== FALSE);
165
166 $javascriptNamespaces[$javascriptNamespace]['actions'][$javascriptObjectName][] = array(
167 'name' => $methodName,
168 'len' => $numberOfParameters,
169 'formHandler' => $formHandler
170 );
171 }
172 }
173 }
174
175 return $javascriptNamespaces;
176 }
177
178 /**
179 * Returns the convenient path for the routing Urls based on the TYPO3 mode.
180 *
181 * @param string $namespace
182 * @return string
183 */
184 public function getRoutingUrl($namespace) {
185 $url = '';
186 if (TYPO3_MODE === 'FE') {
187 $url = t3lib_div::locationHeaderUrl('?eID=ExtDirect&action=route&namespace=');
188 } else {
189 $url = t3lib_div::locationHeaderUrl('ajax.php?ajaxID=ExtDirect::route&namespace=');
190 }
191 $url .= rawurlencode($namespace);
192
193 return $url;
194 }
195 }
196
197 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/extjs/class.t3lib_extjs_extdirectapi.php'])) {
198 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/extjs/class.t3lib_extjs_extdirectapi.php']);
199 }
200
201 ?>