fixed a link bug in installer category tree in main categories with no sub catagories
[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 // try to write the data to localconf
104 if (!$this->basicsObject->saveLocalconf()) {
105 $ajaxContent = $this->pObj->getViewObject()->renderErrors().$ajaxContent;
106 } else {
107 $modMessage = '';
108 // var_dump($modifiedFields);
109 if (count($modifiedFields) > 0) {
110 $modMessage = $this->pObj->getViewObject()->render(array('type' => 'list', 'value' => $modifiedFields));
111 }
112 $ajaxContent = $aboutBox.$this->pObj->getViewObject()->getLastMessage().$modMessage.$ajaxContent;
113 }
114
115 }
116 }
117
118 $returnValue = $this->pObj->getViewObject()->renderTag('h1', $titleSection).$formWrap[0].$ajaxContent.$formWrap[1];
119
120 } else {
121 // this is the default... In other words this is only executed on first call. All other requests are done via AJAX see above...
122
123 $catData = $this->basicsObject->getModuleCategoryData();
124 $tree = $this->renderCategoryTree($catData, $tree);
125
126 $installerWebPath = $this->basicsObject->getInstallerWebPath();
127
128 $this->pObj->getViewObject()->addJS(
129 'var minusSrc = "'.$installerWebPath.'imgs/icons/minus.gif";' .
130 'var plusSrc = "'.$installerWebPath.'imgs/icons/plus.gif";' .
131 'var expandSrc = "'.$installerWebPath.'imgs/icons/expandall.png";' .
132 'var collapseSrc = "'.$installerWebPath.'imgs/icons/collapseall.png";' .
133 'var labelExpand = "'.$this->get_LL('label_expandAll').'";' .
134 'var labelCollapse = "'.$this->get_LL('label_collapseAll').'";'
135 );
136
137 $returnValue = array('content' => $tree.'<div id="moduleContent"></div>');
138 }
139
140 return $returnValue;
141 }
142
143
144 private function addAboutBox($category = 'categoryMain') {
145 // create a about box
146 $categoryDescription = $this->get_LL('descr_'.$this->env[$category]);
147 if (!empty($categoryDescription)) {
148 $aboutBoxCode = array (
149 'type' => 'box',
150 'value' => array (
151 'class' => 'category_about',
152 'elements' => array (
153 array (
154 'type' => 'image',
155 'value' => array (
156 'path' => 'imgs/icons/'.$this->env[$category].'.png'
157 )
158 ),
159 array (
160 'type' => 'plain',
161 'value' => $this->get_LL('descr_'.$this->env[$category])
162 )
163 )
164 )
165 );
166
167 // add the about box
168 $this->pObj->getViewObject()->addContent('', $this->pObj->getViewObject()->render($aboutBoxCode));
169 }
170 }
171
172 private function renderCategoryDeliverables($deliverables) {
173 $result = '';
174
175 foreach ($deliverables as $deliverable => $names) {
176 $result .= $this->pObj->getViewObject()->renderTag('h2', $this->get_LL('label_deliverable_'.$deliverable));
177
178 // add form for saving options
179 if ($deliverable == 'options') {
180 $formWrap = array (
181 '<form action="index.php" method="post" id="optionsForm">'.
182 '<input type="hidden" name="categoryMain" value="'.$this->env['categoryMain'].'" />'.
183 '<input type="hidden" name="categorySub" value="'.$this->env['categorySub'].'" />',
184 '<input type="button" onclick="sendForm(\'optionsForm\');" value="'.$this->get_LL('label_save').'" /></form>'
185 );
186 } else {
187 $formWrap = array('', '');
188 }
189
190 $modifiedFields = array();
191
192 foreach ($names as $name => $mod) {
193 $modConfig = $GLOBALS['MCA'][$mod][$deliverable][$name];
194
195 if (!isset($modConfig['title'])) {
196 $modConfig['title'] = 'title_'.$name;
197 }
198 if (!isset($modConfig['description'])) {
199 $modConfig['description'] = 'description_'.$name;
200 }
201 if (!isset($modConfig['help'])) {
202 $modConfig['help'] = 'help_'.$name;
203 }
204
205 $this->pObj->getBasicsObject()->loadModule($mod);
206
207 $helpData = $this->pObj->getViewObject()->renderHelp($this->get_LL($modConfig['help']), $name);
208 $descr = $this->pObj->getViewObject()->renderTag('div', $this->get_LL($modConfig['description']));
209 $deliverableContent = '';
210
211 switch ($deliverable) {
212 case 'checks':
213 // execute check and print out result in plain
214 // $this->pObj->getViewObject()->clearLastMessage();
215 $checkResult = $this->basicsObject->executeMethod($modConfig['method']);
216 $deliverableContent = $this->get_LL('label_'.(($checkResult) ? 'true' : 'false'));
217 if (!$checkResult) {
218 $deliverableContent .= $this->pObj->getViewObject()->renderErrors();
219 } else {
220 $deliverableContent .= $this->pObj->getViewObject()->getLastMessage();
221 $this->pObj->getViewObject()->clearLastMessage();
222 }
223 break;
224 case 'options':
225 // options are rendered as input elements
226
227 // save data
228 if ($this->env['saveData'] && isset($this->env[$mod.':'.$name])) {
229 // get data from environment
230
231 if ($modConfig['elementType'] == 'checkbox') {
232 $this->env[$mod.':'.$name] = intval($this->env[$mod.':'.$name]);
233 }
234
235 $saveData = $this->env[$mod.':'.$name];
236
237 // add only if data has really changed
238 $localConfValue = $this->basicsObject->getLocalconfValue($modConfig['value'], $modConfig['default']);
239 if ($saveData != $localConfValue) {
240 $this->basicsObject->addToLocalconf($modConfig['value'], $saveData, $modConfig['valueType']);
241
242 $modifiedFields[] = array (
243 'type' => 'message',
244 'value' => array (
245 'label' => sprintf($this->get_LL('label_value_modified'), $this->get_LL($modConfig['title']), $saveData)
246 )
247 );
248 }
249 }
250
251 $options = $modConfig;
252 $inputConfig = array (
253 'elementType' => $modConfig['elementType'],
254 'options' => array_merge($modConfig, array (
255 'name' => $mod.':'.$name,
256 'value' => $this->basicsObject->getLocalconfValue($modConfig['value'], $modConfig['default']),
257 'description' => $modConfig['description'],
258 'help' => $modConfig['help']
259 ))
260 );
261
262 $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'));
263 $deliverableContent = $this->pObj->getViewObject()->renderFormelement($inputConfig);
264 break;
265 case 'methods':
266 // the result of methods is simply printed out
267 $target = $name.'_result';
268 if ($modConfig['autostart'] == true) {
269 $deliverableContent = '<div id="'.$target.'">'.$this->basicsObject->executeMethod($modConfig['method']).'</div>';
270 } else {
271 list($module, $method) = t3lib_div::trimExplode(':', $modConfig['method']);
272 $headerContent = '&nbsp;[<a href="#" onclick="executeMethod(\''.$module.'\', \''.$method.'\', {target:\''.$target.'\'}, displayMethodResult)">'.$this->get_LL('label_execute').'</a>]';
273 $deliverableContent = '<div id="'.$target.'">'.$deliverableContent.'</div>';
274 }
275 break;
276 }
277
278
279 if ($modConfig['elementType'] == 'checkbox') {
280 // draw checkboxes in front of the caption
281 $deliverableBox = $this->pObj->getViewObject()->renderTag('h3', $deliverableContent.$this->get_LL($modConfig['title']).' '.(($helpData) ? $helpData['button'] : '')).(($helpData) ? $helpData['container'] : '').$descr;
282 } elseif ($deliverable == 'methods') {
283 // draw "execute" link after caption and target div under caption
284 $deliverableBox = $this->pObj->getViewObject()->renderTag('h3', $this->get_LL($modConfig['title']).$headerContent.' '.(($helpData) ? $helpData['button'] : '')).(($helpData) ? $helpData['container'] : '').$descr.$deliverableContent;
285 } else {
286 // draw caption and content under it (default)
287 $deliverableBox = $this->pObj->getViewObject()->renderTag('h3', $this->get_LL($modConfig['title']).' '.(($helpData) ? $helpData['button'] : '')).(($helpData) ? $helpData['container'] : '').$descr.$deliverableContent;
288 }
289
290 $paramData = array (
291 'id' => 'container_'.$name,
292 'class' => 'deliverable-box'
293 );
294 if ($filterResults[$name]) {
295 $paramData['style'] = 'background-color:#99ff99';
296 }
297
298 $result .= $this->pObj->getViewObject()->renderTag('div', $deliverableBox, $paramData);
299 }
300 }
301 return $result;
302 }
303
304 /**
305 * Renders a collapsable tree from the collected category data.
306 *
307 * @param array $data: The collected category data (see: basicsObj->getModuleCategoryData())
308 * @return HTML of the tree as unordered list.
309 */
310 private function renderCategoryTree($data) {
311 // add the searchbox
312 $content = '<div class="categoryTreeContainer">
313 <input type="text" id="treeFilter" value="" /><br />
314 <span id="filterStatus"></span>
315 <a href="#" onclick="toggleAllLeafs()" id="collapseExpandToggle">
316 '.$this->get_LL('label_expandAll').'
317 </a>
318 ';
319
320 $content .= '<ul class="categoryTree">';
321
322 // first level
323 foreach ($data as $level1Key => $level1Value) {
324 if (is_array($level1Value)) {
325 $onlyRootDeliverables = ((count($level1Value) == 1 && isset($level1Value['root'])));
326
327 if (!$onlyRootDeliverables) {
328 $content .= '<li id="item_'.$level1Key.'" class="tree_item">';
329 $content .= '<a href="#" onclick="toggleElement(\''.$level1Key.'\'); return false;"><img id="img_'.$level1Key.'" src="'.$this->pObj->getBasicsObject()->getInstallerWebPath().'imgs/icons/plus.gif" /></a>';
330 } else {
331 $content .= '<li id="item_'.$level1Key.'" class="tree_item_nosub">';
332 }
333 $content .= $this->renderTreeElement($level1Key, NULL, $onlyRootDeliverables);
334
335 if (is_array($level1Value) && !$onlyRootDeliverables) {
336 $content .= '<ul id="'.$level1Key.'" class="subLeaf" style="display:none">';
337 foreach ($level1Value as $level2Key => $level2Value) {
338 $content .= '<li id="item_'.$level2Key.'" class="tree_item">'.$this->renderTreeElement($level1Key, $level2Key).'</li>';
339 }
340 $content .= '</ul>';
341 }
342 $content .= '</li>';
343 }
344 }
345 $content .= '</ul></div>';
346
347 return $content;
348 }
349
350 private function renderTreeElement($level1Key, $level2Key = NULL, $onlyRootDeliverables = false) {
351 if(is_null($level2Key)) {
352 $label = $this->get_LL('label_category_'.$level1Key);
353 } else {
354 $label = $this->get_LL('label_subcategory_'.$level2Key);
355 }
356
357 $result = '<a href="#" onclick="';
358 if (!$onlyRootDeliverables) {
359 $result .= 'openLeaf(\''.$level1Key.'\');';
360 }
361 $result .= 'loadModuleContent(\''.$level1Key.'\', \''.$level2Key.'\'); return false;">'.$label.'</a>';
362 return $result;
363 }
364
365
366 /**
367 * Searches in all registered modules for a given string and returns a list of categories that contain
368 * the searchword.
369 *
370 * @param unknown_type $searchString
371 * @return unknown
372 */
373 public function searchCategories() {
374 $searchString = $this->env['searchString'];
375
376 $result = array(
377 'searchString' => $searchString,
378 'resultCount' => 0,
379 'resultMessage' => '',
380 'catMain' => array(),
381 'catSub' => array()
382 );
383 $filterResults = array();
384
385 if (strlen($searchString) >= 2) {
386
387 $this->basicsObject->getModuleCategoryData();
388 $localLang = $this->pObj->getLocalLang();
389 foreach ($localLang[$this->pObj->getLanguage()] as $index => $label) {
390 if (stripos($label, $searchString) !== false) {
391 $deliverableData = $this->pObj->getLabelIndexItem($index);
392 if (!empty($deliverableData)) {
393 $result['catMain'][$deliverableData['mainCat']] = true;
394 $result['catSub'][$deliverableData['subCat']][] = $deliverableData;
395 $result['resultCount']++;
396 $filterResults[$deliverableData['deliverable']] = true;
397 }
398 }
399 }
400
401 $result['resultMessage'] = sprintf($this->get_LL('msg_searchResultMessage'), $result['resultCount'], $searchString);
402 }
403
404 $this->pObj->setFilterResults($filterResults);
405 return json_encode($result);
406 }
407
408 }
409
410 ?>