3b8c13a51a9e352651776e67525fe3117ed87f44
[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']));
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 if ($modConfig['autostart'] == true) {
286 $deliverableContent = '<div id="'.$target.'">'.$this->basicsObject->executeMethod($modConfig['method']).'</div>';
287 } else {
288 list($module, $method) = t3lib_div::trimExplode(':', $modConfig['method']);
289 $headerContent = '&nbsp;[<a href="#" onclick="executeMethod(\''.$module.'\', \''.$method.'\', {target:\''.$target.'\'}, displayMethodResult)">'.$this->get_LL('label_execute').'</a>]';
290 $deliverableContent = '<div id="'.$target.'">'.$deliverableContent.'</div>';
291 }
292 break;
293 }
294
295
296 if ($modConfig['elementType'] == 'checkbox') {
297 // draw checkboxes in front of the caption
298 $deliverableBox = $this->pObj->getViewObject()->renderTag('h3', $deliverableContent.$this->get_LL($modConfig['title']).' '.(($helpData) ? $helpData['button'] : '')).(($helpData) ? $helpData['container'] : '').$descr;
299 } elseif ($deliverable == 'methods') {
300 // draw "execute" link after caption and target div under caption
301 $deliverableBox = $this->pObj->getViewObject()->renderTag('h3', $this->get_LL($modConfig['title']).$headerContent.' '.(($helpData) ? $helpData['button'] : '')).(($helpData) ? $helpData['container'] : '').$descr.$deliverableContent;
302 } else {
303 // draw caption and content under it (default)
304 $deliverableBox = $this->pObj->getViewObject()->renderTag('h3', $this->get_LL($modConfig['title']).' '.(($helpData) ? $helpData['button'] : '')).(($helpData) ? $helpData['container'] : '').$descr.$deliverableContent;
305 }
306
307 $paramData = array (
308 'id' => 'container_'.$name,
309 'class' => 'deliverable-box'
310 );
311 if ($filterResults[$name]) {
312 $paramData['style'] = 'background-color:#99ff99';
313 }
314
315 $result .= $this->pObj->getViewObject()->renderTag('div', $deliverableBox, $paramData);
316 }
317
318 // add form for saving options
319 if ($deliverable == 'options') {
320 $result .= '<input type="button" class="submit bg" onclick="sendForm(\'optionsForm\');" value="'.$this->get_LL('label_save').'" /></form>';
321 }
322 }
323
324 return $result;
325 }
326
327 /**
328 * Renders a collapsable tree from the collected category data.
329 *
330 * @param array $data: The collected category data (see: basicsObj->getModuleCategoryData())
331 * @return HTML of the tree as unordered list.
332 */
333 private function renderCategoryTree($data) {
334 // add the searchbox
335 $content = '<div class="categoryTreeContainer">
336 <div id="treeOptions">
337 <div id="treeFilterBox">
338 <input type="text" id="treeFilter" value="" />
339 </div>
340 </div>
341 <span id="filterStatus"></span>
342 <a href="#" onclick="toggleAllLeafs()" id="collapseExpandToggle">
343 '.$this->get_LL('label_expandAll').'
344 </a>
345 ';
346
347 $content .= '<ul class="categoryTree">';
348
349 // first level
350 foreach ($data as $level1Key => $level1Value) {
351 if (is_array($level1Value)) {
352 $onlyRootDeliverables = ((count($level1Value) == 1 && isset($level1Value['root'])));
353
354 if (!$onlyRootDeliverables) {
355 $content .= '<li id="item_'.$level1Key.'" class="tree_item">';
356 $content .= '<a href="#" onclick="toggleElement(\''.$level1Key.'\'); return false;"><img id="img_'.$level1Key.'" src="'.$this->pObj->getBasicsObject()->getInstallerWebPath().'imgs/icons/plus.gif" /></a>';
357 } else {
358 $content .= '<li id="item_'.$level1Key.'" class="tree_item_nosub">';
359 }
360 $content .= $this->renderTreeElement($level1Key, NULL, $onlyRootDeliverables);
361
362 if (is_array($level1Value) && !$onlyRootDeliverables) {
363 $content .= '<ul id="'.$level1Key.'" class="subLeaf" style="display:none">';
364 foreach ($level1Value as $level2Key => $level2Value) {
365 $content .= '<li id="item_'.$level2Key.'" class="tree_item">'.$this->renderTreeElement($level1Key, $level2Key).'</li>';
366 }
367 $content .= '</ul>';
368 }
369 $content .= '</li>';
370 }
371 }
372 $content .= '</ul></div>';
373
374 return $content;
375 }
376
377 private function renderTreeElement($level1Key, $level2Key = NULL, $onlyRootDeliverables = false) {
378 if(is_null($level2Key)) {
379 $label = $this->get_LL('label_category_'.$level1Key);
380 } else {
381 $label = $this->get_LL('label_subcategory_'.$level2Key);
382 }
383
384 $result = '<a href="#" onclick="';
385 if (!$onlyRootDeliverables) {
386 $result .= 'openLeaf(\''.$level1Key.'\');';
387 }
388 $result .= 'loadModuleContent(\''.$level1Key.'\', \''.$level2Key.'\'); return false;">'.$label.'</a>';
389 return $result;
390 }
391
392
393 /**
394 * Searches in all registered modules for a given string and returns a list of categories that contain
395 * the searchword.
396 *
397 * @param unknown_type $searchString
398 * @return unknown
399 */
400 public function searchCategories() {
401 $searchString = $this->env['searchString'];
402
403 $result = array(
404 'searchString' => $searchString,
405 'resultCount' => 0,
406 'resultMessage' => '',
407 'catMain' => array(),
408 'catSub' => array()
409 );
410 $filterResults = array();
411
412 if (strlen($searchString) >= 2) {
413
414 $this->basicsObject->getModuleCategoryData();
415 $localLang = $this->pObj->getLocalLang();
416 foreach ($localLang[$this->pObj->getLanguage()] as $index => $label) {
417 if (stripos($label, $searchString) !== false) {
418 $deliverableData = $this->pObj->getLabelIndexItem($index);
419 if (!empty($deliverableData)) {
420 $result['catMain'][$deliverableData['mainCat']] = true;
421 $result['catSub'][$deliverableData['subCat']][] = $deliverableData;
422 $result['resultCount']++;
423 $filterResults[$deliverableData['deliverable']] = true;
424 }
425 }
426 }
427
428 $result['resultMessage'] = sprintf($this->get_LL('msg_searchResultMessage'), $result['resultCount'], $searchString);
429 }
430
431 $this->pObj->setFilterResults($filterResults);
432 return json_encode($result);
433 }
434
435 }
436
437 ?>