* Installer: Added a "hide" button to hide the results of executed methods
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / modules / setup / class.tx_install_module_setup.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2006-2007 Thomas Hempel (thomas@work.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 * 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 *
30 * $Id$
31 *
32 * @author Thomas Hempel <thomas@work.de>
33 * @author Ingo Renner <ingo@typo3.org>
34 */
35 class tx_install_module_setup extends tx_install_module_base {
36
37 /**
38 * Main method of this module. This method is called by default at various places.
39 *
40 * @return plain HTML if AJAX request, otherwise array(content)
41 */
42 public function main() {
43 $returnValue = '';
44
45 // check if we should return AJAX content
46 if ($this->env['ajax'] == 1) {
47
48 $ajaxContent = '';
49 $titleSection = $this->get_LL('label_category_'.$this->env['categoryMain']);
50
51 // create a header box for main category if sub catagory is empty
52 if (empty($this->env['categorySub'])) {
53 $this->addAboutBox();
54
55 // get all deliverables for all modules in main category
56 $categoryDeliverables = $this->basicsObject->getCategoryModuleDeliverables($this->env['categoryMain']);
57 $subCategoryListElements = array();
58
59 // create a link to each subcategory (but not root)
60 foreach ($categoryDeliverables as $subCategory => $deliverables) {
61 if ($subCategory == 'root') continue;
62 $label = $this->get_LL('label_subcategory_'.$subCategory);
63 $subCategoryListElements[] = array (
64 'type' => 'plain',
65 'value' => '<a href="#" onclick="loadModuleContent(\''.$this->env['categoryMain'].'\', \''.$subCategory.'\'); return false;">'.$label.'</a>'
66 );
67 }
68
69 if (count($subCategoryListElements) > 0) {
70 // render a list with the links
71 $deliverableCode = $this->pObj->getViewObject()->render(array(
72 'type' => 'list',
73 'value' => $subCategoryListElements
74 ));
75
76 // add a new section to the output
77 $this->pObj->getViewObject()->addContent($this->get_LL('label_subsectionsfound'), $deliverableCode);
78 }
79
80 $categoryRootDeliverables = $this->basicsObject->getCategoryModuleDeliverables($this->env['categoryMain'], 'root');
81 if (is_array($categoryRootDeliverables)) {
82 $this->pObj->getViewObject()->addContent('', $this->renderCategoryDeliverables($categoryRootDeliverables));
83 }
84
85 $ajaxContent = $this->pObj->getViewObject()->getContent();
86
87 } else {
88 // sub category was selected...
89 $titleSection .= ' / '.$this->get_LL('label_subcategory_'.$this->env['categorySub']);
90 $moduleDeliverables = $this->pObj->getBasicsObject()->getCategoryModuleDeliverables($this->env['categoryMain'], $this->env['categorySub']);
91
92 $this->addAboutBox('categorySub');
93
94 if ($moduleDeliverables === false) {
95 // render errors if something went wrong before
96 $ajaxContent = $this->pObj->getViewObj()->renderErrors(true);
97 } else {
98 $filterResults = $this->pObj->getFilterResults();
99
100 // get content from deliverables
101 $ajaxContent = $this->renderCategoryDeliverables($moduleDeliverables);
102 }
103 }
104
105 // try to write the data to localconf
106 if (!$this->basicsObject->saveLocalconf()) {
107 $ajaxContent = $this->pObj->getViewObject()->renderErrors().$ajaxContent;
108 } else {
109 $modMessage = '';
110 if (count($modifiedFields) > 0) {
111 $modMessage = $this->pObj->getViewObject()->render(array('type' => 'list', 'value' => $modifiedFields));
112 }
113 $ajaxContent = $aboutBox.$this->pObj->getViewObject()->getLastMessage().$modMessage.$ajaxContent;
114 }
115
116 $returnValue = $this->pObj->getViewObject()->renderTag('h1', $titleSection).$ajaxContent;
117
118 } else {
119 // this is the default... In other words this is only executed on first call. All other requests are done via AJAX see above...
120
121 $catData = $this->basicsObject->getModuleCategoryData();
122 $tree = $this->renderCategoryTree($catData, $tree);
123
124 $installerWebPath = $this->basicsObject->getInstallerWebPath();
125
126 $this->pObj->getViewObject()->addJS(
127 'var minusSrc = "'.$installerWebPath.'imgs/icons/minus.gif";' .
128 'var plusSrc = "'.$installerWebPath.'imgs/icons/plus.gif";' .
129 'var expandSrc = "'.$installerWebPath.'imgs/icons/expandall.png";' .
130 'var collapseSrc = "'.$installerWebPath.'imgs/icons/collapseall.png";' .
131 'var labelExpand = "'.$this->get_LL('label_expandAll').'";' .
132 'var labelCollapse = "'.$this->get_LL('label_collapseAll').'";'
133 );
134
135 $returnValue = array('content' => $tree.'<div id="moduleContent"></div>');
136 }
137
138 return $returnValue;
139 }
140
141
142 /**
143 * Adds the about box on top of the page.
144 * The box is not displayed if no description was found.
145 *
146 * @param string $category: the category for which the about should be generated
147 */
148 private function addAboutBox($category = 'categoryMain') {
149 // create a about box
150 $categoryDescription = $this->get_LL('description_module_'.$this->env[$category]);
151
152 // if (!empty($categoryDescription)) {
153 $aboutBoxCode = array (
154 'type' => 'box',
155 'value' => array (
156 'class' => 'category_about',
157 'elements' => array (
158 array (
159 'type' => 'image',
160 'value' => array (
161 'path' => 'imgs/icons/'.$this->env[$category].'.png'
162 )
163 ),
164 array (
165 'type' => 'plain',
166 'value' => $categoryDescription
167 )
168 )
169 )
170 );
171
172 // add the about box
173 $this->pObj->getViewObject()->addContent('', $this->pObj->getViewObject()->render($aboutBoxCode));
174 // }
175 }
176
177
178 /**
179 * This one of the most important methods in setup. Here we render all pages and deliverables. It also
180 * looks for changes and pushes them to the localconf cache.
181 *
182 * @param array $deliverables: The deliverable configs
183 * @return HTML with the rendered deliverables
184 */
185 private function renderCategoryDeliverables($deliverables) {
186 $result = '';
187 $this->pObj->getViewObject()->clearLastMessage();
188
189 foreach ($deliverables as $deliverable => $names) {
190 if (!empty($this->env['categorySub'])) {
191 $result .= $this->pObj->getViewObject()->renderTag('h2', $this->get_LL('label_deliverable_'.$deliverable));
192 }
193
194 $modifiedFields = array();
195
196 if ($deliverable == 'options') {
197 $result .= '<form action="index.php" method="post" id="optionsForm">'.
198 '<input type="hidden" name="categoryMain" value="'.$this->env['categoryMain'].'" />'.
199 '<input type="hidden" name="categorySub" value="'.$this->env['categorySub'].'" />';
200 }
201
202 foreach ($names as $name => $mod) {
203 $modConfig = $GLOBALS['MCA'][$mod][$deliverable][$name];
204
205 if (!isset($modConfig['title'])) {
206 $modConfig['title'] = 'title_'.$name;
207 }
208 if (!isset($modConfig['description'])) {
209 $modConfig['description'] = 'description_'.$name;
210 }
211 if (!isset($modConfig['help'])) {
212 $modConfig['help'] = 'help_'.$name;
213 }
214
215 $this->pObj->getBasicsObject()->loadModule($mod);
216
217 $helpData = $this->pObj->getViewObject()->renderHelp($this->get_LL($modConfig['help']), $name);
218 $descr = $this->pObj->getViewObject()->renderTag('div', $this->get_LL($modConfig['description']), array('class' => 'description'));
219 $deliverableContent = '';
220
221 switch ($deliverable) {
222 case 'checks':
223 // execute check and print out result in plain
224 $checkResult = $this->basicsObject->executeMethod($modConfig['method']);
225 $messageConfig = array(
226 'type' => 'message',
227 'value' => array (
228 'severity' => (($checkResult) ? 'ok' : 'error'),
229 'label' => (($checkResult) ? $this->pObj->getViewObject()->getLastMessage() : $this->get_LL('label_false'))
230 ));
231 if (!$checkResult) {
232 $messageConfig['value']['message'] = $this->pObj->getViewObject()->renderErrors();
233 } else {
234 $messageConfig['value']['message'] = $this->pObj->getViewObject()->clearLastMessage();
235 }
236 $deliverableContent = $this->pObj->getViewObject()->render($messageConfig);
237 $this->pObj->getViewObject()->clearLastMessage();
238 break;
239 case 'options':
240 // options are rendered as input elements
241
242 // save data
243 if ($this->env['saveData'] && isset($this->env[$mod.':'.$name])) {
244 // get data from environment
245
246 if ($modConfig['elementType'] == 'checkbox') {
247 $this->env[$mod.':'.$name] = intval($this->env[$mod.':'.$name]);
248 }
249
250 $saveData = $this->env[$mod.':'.$name];
251
252 // add only if data has really changed
253 $localConfValue = $this->basicsObject->getLocalconfValue($modConfig['value'], $modConfig['default']);
254 if ($saveData != $localConfValue) {
255 $this->basicsObject->addToLocalconf($modConfig['value'], $saveData, $modConfig['valueType']);
256
257 $modifiedFields[] = array (
258 'type' => 'message',
259 'value' => array (
260 'label' => sprintf($this->get_LL('label_value_modified'), $this->get_LL($modConfig['title']), $saveData)
261 )
262 );
263 }
264 }
265
266 $options = $modConfig;
267 $inputConfig = array (
268 'elementType' => $modConfig['elementType'],
269 'options' => array_merge($modConfig, array (
270 'name' => $mod.':'.$name,
271 'value' => $this->basicsObject->getLocalconfValue($modConfig['value'], $modConfig['default']),
272 'description' => $modConfig['description'],
273 'help' => $modConfig['help'],
274 'class' => 'bg'
275 ))
276 );
277
278 $descr .= $this->pObj->getViewObject()->renderTag('span', $this->get_LL('label_path').' '.$this->basicsObject->getLocalconfPath($modConfig['value']).' - '.$this->get_LL('label_default').' '.$modConfig['default'], array('class' => 'description italic'));
279 $deliverableContent = $this->pObj->getViewObject()->renderFormelement($inputConfig);
280
281 break;
282 case 'methods':
283 // the result of methods is simply printed out
284 $target = $name.'_result';
285 $hideButton = '<span style="display:none" id="hideBtn_'.$target.'">&nbsp;[<a href="#" onclick="hideElement(\''.$target.'\');">'.$this->get_LL('label_hide').'</a>]</span>';
286
287 if ($modConfig['autostart'] == true) {
288 $headerContent = $hideButton;
289 $deliverableContent = '<div id="'.$target.'">'.$this->basicsObject->executeMethod($modConfig['method']).'</div>';
290 } else {
291 list($module, $method) = t3lib_div::trimExplode(':', $modConfig['method']);
292 $headerContent = '&nbsp;[<a href="#" onclick="executeMethod(\''.$module.'\', \''.$method.'\', {target:\''.$target.'\'}, displayMethodResult)">'.$this->get_LL('label_execute').'</a>]'.$hideButton;
293 $deliverableContent = '<div id="'.$target.'">'.$deliverableContent.'</div>';
294 }
295 break;
296 }
297
298
299 if ($modConfig['elementType'] == 'checkbox') {
300 // draw checkboxes in front of the caption
301 $deliverableBox = $this->pObj->getViewObject()->renderTag('h3', $deliverableContent.$this->get_LL($modConfig['title']).' '.(($helpData) ? $helpData['button'] : '')).(($helpData) ? $helpData['container'] : '').$descr;
302 } elseif ($deliverable == 'methods') {
303 // draw "execute" link after caption and target div under caption
304 $deliverableBox = $this->pObj->getViewObject()->renderTag('h3', $this->get_LL($modConfig['title']).$headerContent.' '.(($helpData) ? $helpData['button'] : '')).(($helpData) ? $helpData['container'] : '').$descr.$deliverableContent;
305 } else {
306 // draw caption and content under it (default)
307 $deliverableBox = $this->pObj->getViewObject()->renderTag('h3', $this->get_LL($modConfig['title']).' '.(($helpData) ? $helpData['button'] : '')).(($helpData) ? $helpData['container'] : '').$descr.$deliverableContent;
308 }
309
310 $paramData = array (
311 'id' => 'container_'.$name,
312 'class' => 'deliverable-box'
313 );
314 if ($filterResults[$name]) {
315 $paramData['style'] = 'background-color:#99ff99';
316 }
317
318 $result .= $this->pObj->getViewObject()->renderTag('div', $deliverableBox, $paramData);
319 }
320
321 // add form for saving options
322 if ($deliverable == 'options') {
323 $result .= '<input type="button" class="submit bg" onclick="sendForm(\'optionsForm\');" value="'.$this->get_LL('label_save').'" /></form>';
324 }
325 }
326
327 return $result;
328 }
329
330 /**
331 * Renders a collapsable tree from the collected category data.
332 *
333 * @param array $data: The collected category data (see: basicsObj->getModuleCategoryData())
334 * @return HTML of the tree as unordered list.
335 */
336 private function renderCategoryTree($data) {
337 // add the searchbox
338 $content = '<div class="categoryTreeContainer">
339 <div id="treeOptions">
340 <div id="treeFilterBox">
341 <input type="text" id="treeFilter" value="" />
342 </div>
343 </div>
344 <span id="filterStatus"></span>
345 <a href="#" onclick="toggleAllLeafs()" id="collapseExpandToggle">
346 '.$this->get_LL('label_expandAll').'
347 </a>
348 ';
349
350 $content .= '<ul class="categoryTree">';
351
352 // first level
353 foreach ($data as $level1Key => $level1Value) {
354 if (is_array($level1Value)) {
355 $onlyRootDeliverables = ((count($level1Value) == 1 && isset($level1Value['root'])));
356
357 if (!$onlyRootDeliverables) {
358 $content .= '<li id="item_'.$level1Key.'" class="tree_item">';
359 $content .= '<a href="#" onclick="toggleElement(\''.$level1Key.'\'); return false;"><img id="img_'.$level1Key.'" src="'.$this->pObj->getBasicsObject()->getInstallerWebPath().'imgs/icons/plus.gif" /></a>';
360 } else {
361 $content .= '<li id="item_'.$level1Key.'" class="tree_item_nosub">';
362 }
363 $content .= $this->renderTreeElement($level1Key, NULL, $onlyRootDeliverables);
364
365 if (is_array($level1Value) && !$onlyRootDeliverables) {
366 $content .= '<ul id="'.$level1Key.'" class="subLeaf" style="display:none">';
367 foreach ($level1Value as $level2Key => $level2Value) {
368 $content .= '<li id="item_'.$level2Key.'" class="tree_item">'.$this->renderTreeElement($level1Key, $level2Key).'</li>';
369 }
370 $content .= '</ul>';
371 }
372 $content .= '</li>';
373 }
374 }
375 $content .= '</ul></div>';
376
377 return $content;
378 }
379
380 private function renderTreeElement($level1Key, $level2Key = NULL, $onlyRootDeliverables = false) {
381 if(is_null($level2Key)) {
382 $label = $this->get_LL('label_category_'.$level1Key);
383 } else {
384 $label = $this->get_LL('label_subcategory_'.$level2Key);
385 }
386
387 $result = '<a href="#" onclick="';
388 if (!$onlyRootDeliverables) {
389 $result .= 'openLeaf(\''.$level1Key.'\');';
390 }
391 $result .= 'loadModuleContent(\''.$level1Key.'\', \''.$level2Key.'\'); return false;">'.$label.'</a>';
392 return $result;
393 }
394
395
396 /**
397 * Searches in all registered modules for a given string and returns a list of categories that contain
398 * the searchword.
399 *
400 * @param unknown_type $searchString
401 * @return unknown
402 */
403 public function searchCategories() {
404 $searchString = $this->env['searchString'];
405
406 $result = array(
407 'searchString' => $searchString,
408 'resultCount' => 0,
409 'resultMessage' => '',
410 'catMain' => array(),
411 'catSub' => array()
412 );
413 $filterResults = array();
414
415 if (strlen($searchString) >= 2) {
416
417 $this->basicsObject->getModuleCategoryData();
418 $localLang = $this->pObj->getLocalLang();
419 foreach ($localLang[$this->pObj->getLanguage()] as $index => $label) {
420 if (stripos($label, $searchString) !== false) {
421 $deliverableData = $this->pObj->getLabelIndexItem($index);
422 if (!empty($deliverableData)) {
423 $result['catMain'][$deliverableData['mainCat']] = true;
424 $result['catSub'][$deliverableData['subCat']][] = $deliverableData;
425 $result['resultCount']++;
426 $filterResults[$deliverableData['deliverable']] = true;
427 }
428 }
429 }
430
431 $result['resultMessage'] = sprintf($this->get_LL('msg_searchResultMessage'), $result['resultCount'], $searchString);
432 }
433
434 $this->pObj->setFilterResults($filterResults);
435 return json_encode($result);
436 }
437
438 }
439
440 ?>