[TASK] Cleanup cshmanual
[Packages/TYPO3.CMS.git] / typo3 / sysext / cshmanual / Classes / Controller / HelpModuleController.php
1 <?php
2 namespace TYPO3\CMS\Cshmanual\Controller;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 1999-2011 Kasper Skårhøj (kasperYYYY@typo3.com)
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 /**
31 * Script Class for rendering the Context Sensitive Help documents, either the single display in the small pop-up window or the full-table view in the larger window.
32 *
33 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
34 */
35 class HelpModuleController {
36
37 /**
38 * @todo Define visibility
39 */
40 public $allowedHTML = '<strong><em><b><i>';
41
42 /**
43 * For these vars, see init()
44 * If set access to fields and tables is checked. Should be done for TRUE database tables.
45 *
46 * @todo Define visibility
47 */
48 public $limitAccess;
49
50 /**
51 * The "table" key
52 *
53 * @todo Define visibility
54 */
55 public $table;
56
57 /**
58 * The "field" key
59 *
60 * @todo Define visibility
61 */
62 public $field;
63
64 /**
65 * Key used to point to the right CSH resource
66 * In simple cases, is equal to $table
67 *
68 * @var string
69 */
70 protected $mainKey;
71
72 /**
73 * Internal, static: GPvar
74 * Table/Field id
75 *
76 * @todo Define visibility
77 */
78 public $tfID;
79
80 /**
81 * Back (previous tfID)
82 *
83 * @todo Define visibility
84 */
85 public $back;
86
87 /**
88 * If set, then in TOC mode the FULL manual will be printed as well!
89 *
90 * @todo Define visibility
91 */
92 public $renderALL;
93
94 /**
95 * Content accumulation
96 *
97 * @todo Define visibility
98 */
99 public $content;
100
101 /**
102 * Glossary words
103 *
104 * @todo Define visibility
105 */
106 public $glossaryWords;
107
108 /**
109 * Initialize the class for various input etc.
110 *
111 * @return void
112 * @todo Define visibility
113 */
114 public function init() {
115 // Setting GPvars:
116 $this->tfID = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('tfID');
117 // Sanitizes the tfID using whitelisting.
118 if (!preg_match('/^[a-zA-Z0-9_\\-\\.\\*]*$/', $this->tfID)) {
119 $this->tfID = '';
120 }
121 $this->back = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('back');
122 $this->renderALL = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('renderALL');
123 // Set internal table/field to the parts of "tfID" incoming var.
124 $identifierParts = explode('.', $this->tfID);
125 // The table is the first item
126 $this->table = array_shift($identifierParts);
127 $this->mainKey = $this->table;
128 // The field is the second one
129 $this->field = array_shift($identifierParts);
130 // There may be extra parts for FlexForms
131 if (count($identifierParts) > 0) {
132 // There's at least one extra part
133 $extraIdentifierInformation = array();
134 $extraIdentifierInformation[] = array_shift($identifierParts);
135 // Load the TCA details of the table
136 \TYPO3\CMS\Core\Utility\GeneralUtility::loadTCA($this->table);
137 // If the ds_pointerField contains a comma, it means the choice of FlexForm DS
138 // is determined by 2 parameters. In this case we have an extra identifier part
139 if (strpos($GLOBALS['TCA'][$this->table]['columns'][$this->field]['config']['ds_pointerField'], ',') !== FALSE) {
140 $extraIdentifierInformation[] = array_shift($identifierParts);
141 }
142 // The remaining parts make up the FlexForm field name itself
143 // (reassembled with dots)
144 $flexFormField = implode('.', $identifierParts);
145 // Assemble a different main key and switch field to use FlexForm field name
146 $this->mainKey .= '.' . $this->field;
147 foreach ($extraIdentifierInformation as $extraKey) {
148 $this->mainKey .= '.' . $extraKey;
149 }
150 $this->field = $flexFormField;
151 }
152 // limitAccess is checked if the $this->table really IS a table (and if the user is NOT a translator who should see all!)
153 $showAllToUser = \TYPO3\CMS\Backend\Utility\BackendUtility::isModuleSetInTBE_MODULES('txllxmltranslateM1') && $GLOBALS['BE_USER']->check('modules', 'txllxmltranslateM1');
154 $this->limitAccess = isset($GLOBALS['TCA'][$this->table]) ? !$showAllToUser : FALSE;
155 $GLOBALS['LANG']->includeLLFile('EXT:lang/locallang_view_help.xlf', 1);
156 }
157
158 /**
159 * Main function, rendering the display
160 *
161 * @return void
162 * @todo Define visibility
163 */
164 public function main() {
165 // Start HTML output accumulation:
166 $GLOBALS['TBE_TEMPLATE']->divClass = 'typo3-view-help';
167 $this->content .= $GLOBALS['TBE_TEMPLATE']->startPage($GLOBALS['LANG']->getLL('title'));
168 if ($this->field == '*') {
169 // If ALL fields is supposed to be shown:
170 $this->createGlossaryIndex();
171 $this->content .= $this->render_Table($this->mainKey);
172 } elseif ($this->tfID) {
173 // ... otherwise show only single field:
174 $this->createGlossaryIndex();
175 $this->content .= $this->render_Single($this->mainKey, $this->field);
176 } else {
177 // Render Table Of Contents if nothing else:
178 $this->content .= $this->render_TOC();
179 }
180 // End page:
181 $this->content .= '<br/>';
182 $this->content .= $GLOBALS['TBE_TEMPLATE']->endPage();
183 }
184
185 /**
186 * Outputting the accumulated content to screen
187 *
188 * @return void
189 * @todo Define visibility
190 */
191 public function printContent() {
192 echo $this->content;
193 }
194
195 /************************************
196 * Rendering main modes
197 ************************************/
198
199 /**
200 * Creates Table Of Contents and possibly "Full Manual" mode if selected.
201 *
202 * @return string HTML content
203 * @todo Define visibility
204 */
205 public function render_TOC() {
206 // Initialize:
207 $CSHkeys = array_flip(array_keys($GLOBALS['TCA_DESCR']));
208 $TCAkeys = array_keys($GLOBALS['TCA']);
209 $outputSections = array();
210 $tocArray = array();
211 // TYPO3 Core Features:
212 $GLOBALS['LANG']->loadSingleTableDescription('xMOD_csh_corebe');
213 $this->render_TOC_el('xMOD_csh_corebe', 'core', $outputSections, $tocArray, $CSHkeys);
214 // Backend Modules:
215 $loadModules = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Module\\ModuleLoader');
216 $loadModules->load($GLOBALS['TBE_MODULES']);
217 foreach ($loadModules->modules as $mainMod => $info) {
218 $cshKey = '_MOD_' . $mainMod;
219 if ($CSHkeys[$cshKey]) {
220 $GLOBALS['LANG']->loadSingleTableDescription($cshKey);
221 $this->render_TOC_el($cshKey, 'modules', $outputSections, $tocArray, $CSHkeys);
222 }
223 if (is_array($info['sub'])) {
224 foreach ($info['sub'] as $subMod => $subInfo) {
225 $cshKey = '_MOD_' . $mainMod . '_' . $subMod;
226 if ($CSHkeys[$cshKey]) {
227 $GLOBALS['LANG']->loadSingleTableDescription($cshKey);
228 $this->render_TOC_el($cshKey, 'modules', $outputSections, $tocArray, $CSHkeys);
229 }
230 }
231 }
232 }
233 // Database Tables:
234 foreach ($TCAkeys as $table) {
235 // Load descriptions for table $table
236 $GLOBALS['LANG']->loadSingleTableDescription($table);
237 if (is_array($GLOBALS['TCA_DESCR'][$table]['columns']) && $GLOBALS['BE_USER']->check('tables_select', $table)) {
238 $this->render_TOC_el($table, 'tables', $outputSections, $tocArray, $CSHkeys);
239 }
240 }
241 // Extensions
242 foreach ($CSHkeys as $cshKey => $value) {
243 if (\TYPO3\CMS\Core\Utility\GeneralUtility::isFirstPartOfStr($cshKey, 'xEXT_') && !isset($GLOBALS['TCA'][$cshKey])) {
244 $GLOBALS['LANG']->loadSingleTableDescription($cshKey);
245 $this->render_TOC_el($cshKey, 'extensions', $outputSections, $tocArray, $CSHkeys);
246 }
247 }
248 // Glossary
249 foreach ($CSHkeys as $cshKey => $value) {
250 if (\TYPO3\CMS\Core\Utility\GeneralUtility::isFirstPartOfStr($cshKey, 'xGLOSSARY_') && !isset($GLOBALS['TCA'][$cshKey])) {
251 $GLOBALS['LANG']->loadSingleTableDescription($cshKey);
252 $this->render_TOC_el($cshKey, 'glossary', $outputSections, $tocArray, $CSHkeys);
253 }
254 }
255 // Other:
256 foreach ($CSHkeys as $cshKey => $value) {
257 if (!\TYPO3\CMS\Core\Utility\GeneralUtility::isFirstPartOfStr($cshKey, '_MOD_') && !isset($GLOBALS['TCA'][$cshKey])) {
258 $GLOBALS['LANG']->loadSingleTableDescription($cshKey);
259 $this->render_TOC_el($cshKey, 'other', $outputSections, $tocArray, $CSHkeys);
260 }
261 }
262 // COMPILE output:
263 $output = '';
264 $output .= '
265
266 <h1>' . $GLOBALS['LANG']->getLL('manual_title', 1) . '</h1>';
267 $output .= '
268
269 <h2>' . $GLOBALS['LANG']->getLL('introduction', 1) . '</h2>
270 <p>' . $GLOBALS['LANG']->getLL('description', 1) . '</p>';
271 $output .= '
272
273 <h2>' . $GLOBALS['LANG']->getLL('TOC', 1) . '</h2>' . $this->render_TOC_makeTocList($tocArray);
274 if (!$this->renderALL) {
275 $output .= '
276 <br/>
277 <p class="c-nav"><a href="mod.php?M=help_cshmanual&renderALL=1">' . $GLOBALS['LANG']->getLL('full_manual', 1) . '</a></p>';
278 }
279 if ($this->renderALL) {
280 $output .= '
281
282 <h2>' . $GLOBALS['LANG']->getLL('full_manual_chapters', 1) . '</h2>' . implode('
283
284
285 <!-- NEW SECTION: -->
286 ', $outputSections);
287 }
288 $output .= '<hr /><p class="manual-title">' . \TYPO3\CMS\Backend\Utility\BackendUtility::TYPO3_copyRightNotice() . '</p>';
289 return $output;
290 }
291
292 /**
293 * Creates a TOC list element and renders corresponding HELP content if "renderALL" mode is set.
294 *
295 * @param string $table CSH key / Table name
296 * @param string $tocCat TOC category keyword: "core", "modules", "tables", "other
297 * @param array $outputSections Array for accumulation of rendered HELP Content (in "renderALL" mode). Passed by reference!
298 * @param array $tocArray TOC array; Here TOC index elements are created. Passed by reference!
299 * @param array $CSHkeys CSH keys array. Every item rendered will be unset in this array so finally we can see what CSH keys are not processed yet. Passed by reference!
300 * @return void
301 * @todo Define visibility
302 */
303 public function render_TOC_el($table, $tocCat, &$outputSections, &$tocArray, &$CSHkeys) {
304 // Render full manual right here!
305 if ($this->renderALL) {
306 $outputSections[$table] = $this->render_Table($table);
307 if ($outputSections[$table]) {
308 $outputSections[$table] = '
309
310 <!-- New CSHkey/Table: ' . $table . ' -->
311 <p class="c-nav"><a name="ANCHOR_' . $table . '" href="#">' . $GLOBALS['LANG']->getLL('to_top', 1) . '</a></p>
312 <h2>' . $this->getTableFieldLabel($table) . '</h2>
313
314 ' . $outputSections[$table];
315 $tocArray[$tocCat][$table] = '<a href="#ANCHOR_' . $table . '">' . $this->getTableFieldLabel($table) . '</a>';
316 } else {
317 unset($outputSections[$table]);
318 }
319 } else {
320 // Only TOC:
321 $tocArray[$tocCat][$table] = '<p><a href="mod.php?M=help_cshmanual&tfID=' . rawurlencode(($table . '.*')) . '">' . $this->getTableFieldLabel($table) . '</a></p>';
322 }
323 // Unset CSH key:
324 unset($CSHkeys[$table]);
325 }
326
327 /**
328 * Renders the TOC index as a HTML bullet list from TOC array
329 *
330 * @param array $tocArray ToC Array.
331 * @return string HTML bullet list for index.
332 * @todo Define visibility
333 */
334 public function render_TOC_makeTocList($tocArray) {
335 // The Various manual sections:
336 $keys = explode(',', 'core,modules,tables,extensions,glossary,other');
337 // Create TOC bullet list:
338 $output = '';
339 foreach ($keys as $tocKey) {
340 if (is_array($tocArray[$tocKey])) {
341 $output .= '
342 <li>' . $GLOBALS['LANG']->getLL(('TOC_' . $tocKey), 1) . '
343 <ul>
344 <li>' . implode('</li>
345 <li>', $tocArray[$tocKey]) . '</li>
346 </ul>
347 </li>';
348 }
349 }
350 // Compile TOC:
351 $output = '
352
353 <!-- TOC: -->
354 <div class="c-toc">
355 <ul>
356 ' . $output . '
357 </ul>
358 </div>';
359 return $output;
360 }
361
362 /**
363 * Render CSH for a full cshKey/table
364 *
365 * @param string $key Full CSH key (may be different from table name)
366 * @param string $table CSH key / table name
367 * @return string HTML output
368 * @todo Define visibility
369 */
370 public function render_Table($key, $table = NULL) {
371 $output = '';
372 // Take default key if not explicitly specified
373 if ($table === NULL) {
374 $table = $key;
375 }
376 // Load table TCA
377 \TYPO3\CMS\Core\Utility\GeneralUtility::loadTCA($key);
378 // Load descriptions for table $table
379 $GLOBALS['LANG']->loadSingleTableDescription($key);
380 if (is_array($GLOBALS['TCA_DESCR'][$key]['columns']) && (!$this->limitAccess || $GLOBALS['BE_USER']->check('tables_select', $table))) {
381 // Initialize variables:
382 $parts = array();
383 // Reserved for header of table
384 $parts[0] = '';
385 // Traverse table columns as listed in TCA_DESCR
386 foreach ($GLOBALS['TCA_DESCR'][$key]['columns'] as $field => $_) {
387 $fieldValue = isset($GLOBALS['TCA'][$key]) && strcmp($field, '') ? $GLOBALS['TCA'][$key]['columns'][$field] : array();
388 if (is_array($fieldValue) && (!$this->limitAccess || !$fieldValue['exclude'] || $GLOBALS['BE_USER']->check('non_exclude_fields', $table . ':' . $field))) {
389 if (!$field) {
390 // Header
391 $parts[0] = $this->printItem($key, '', 1);
392 } else {
393 // Field
394 $parts[] = $this->printItem($key, $field, 1);
395 }
396 }
397 }
398 if (!$parts[0]) {
399 unset($parts[0]);
400 }
401 $output .= implode('<br />', $parts);
402 }
403 // Substitute glossary words:
404 $output = $this->substituteGlossaryWords($output);
405 // TOC link:
406 if (!$this->renderALL) {
407 $tocLink = '<p class="c-nav"><a href="mod.php?M=help_cshmanual">' . $GLOBALS['LANG']->getLL('goToToc', 1) . '</a></p>';
408 $output = $tocLink . '
409 <br/>' . $output . '
410 <br />' . $tocLink;
411 }
412 return $output;
413 }
414
415 /**
416 * Renders CSH for a single field.
417 *
418 * @param string $key CSH key / table name
419 * @param string $field Sub key / field name
420 * @return string HTML output
421 * @todo Define visibility
422 */
423 public function render_Single($key, $field) {
424 $output = '';
425 // Load the description field
426 $GLOBALS['LANG']->loadSingleTableDescription($key);
427 // Render single item
428 $output .= $this->printItem($key, $field);
429 // Substitute glossary words:
430 $output = $this->substituteGlossaryWords($output);
431 // Link to Full table description and TOC:
432 $getLLKey = $this->limitAccess ? 'fullDescription' : 'fullDescription_module';
433 $output .= '<br />
434 <p class="c-nav"><a href="mod.php?M=help_cshmanual&tfID=' . rawurlencode(($key . '.*')) . '">' . $GLOBALS['LANG']->getLL($getLLKey, 1) . '</a></p>
435 <p class="c-nav"><a href="mod.php?M=help_cshmanual">' . $GLOBALS['LANG']->getLL('goToToc', 1) . '</a></p>';
436 return $output;
437 }
438
439 /************************************
440 * Rendering CSH items
441 ************************************/
442
443 /**
444 * Make seeAlso links from $value
445 *
446 * @param string $value See-also input codes
447 * @param string $anchorTable If $anchorTable is set to a tablename, then references to this table will be made as anchors, not URLs.
448 * @return string See-also links HTML
449 * @todo Define visibility
450 */
451 public function make_seeAlso($value, $anchorTable = '') {
452 // Split references by comma or linebreak
453 $items = preg_split('/[,' . LF . ']/', $value);
454 $lines = array();
455 foreach ($items as $val) {
456 $val = trim($val);
457 if ($val) {
458 $iP = explode(':', $val);
459 $iPUrl = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode('|', $val);
460 // URL reference:
461 if (substr($iPUrl[1], 0, 4) == 'http') {
462 $lines[] = '<a href="' . htmlspecialchars($iPUrl[1]) . '" target="_blank"><em>' . htmlspecialchars($iPUrl[0]) . '</em></a>';
463 } elseif (substr($iPUrl[1], 0, 5) == 'FILE:') {
464 $fileName = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName(substr($iPUrl[1], 5), 1, 1);
465 if ($fileName && @is_file($fileName)) {
466 $fileName = '../' . substr($fileName, strlen(PATH_site));
467 $lines[] = '<a href="' . htmlspecialchars($fileName) . '" target="_blank"><em>' . htmlspecialchars($iPUrl[0]) . '</em></a>';
468 }
469 } else {
470 // "table" reference
471 \TYPO3\CMS\Core\Utility\GeneralUtility::loadTCA($iP[0]);
472 if (!isset($GLOBALS['TCA'][$iP[0]]) || (!$iP[1] || is_array($GLOBALS['TCA'][$iP[0]]['columns'][$iP[1]])) && (!$this->limitAccess || $GLOBALS['BE_USER']->check('tables_select', $iP[0]) && (!$iP[1] || !$GLOBALS['TCA'][$iP[0]]['columns'][$iP[1]]['exclude'] || $GLOBALS['BE_USER']->check('non_exclude_fields', $iP[0] . ':' . $iP[1])))) {
473 // Checking read access:
474 if (isset($GLOBALS['TCA_DESCR'][$iP[0]])) {
475 // Make see-also link:
476 $href = $this->renderALL || $anchorTable && $iP[0] == $anchorTable ? '#' . implode('.', $iP) : 'mod.php?M=help_cshmanual&tfID=' . rawurlencode(implode('.', $iP)) . '&back=' . $this->tfID;
477 $label = $this->getTableFieldLabel($iP[0], $iP[1], ' / ');
478 $lines[] = '<a href="' . htmlspecialchars($href) . '">' . htmlspecialchars($label) . '</a>';
479 }
480 }
481 }
482 }
483 }
484 return implode('<br />', $lines);
485 }
486
487 /**
488 * Will return an image tag with description in italics.
489 *
490 * @param string $images Image file reference (list of)
491 * @param string $descr Description string (divided for each image by line break)
492 * @return string Image HTML codes
493 * @todo Define visibility
494 */
495 public function printImage($images, $descr) {
496 $code = '';
497 // Splitting:
498 $imgArray = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $images, 1);
499 if (count($imgArray)) {
500 $descrArray = explode(LF, $descr, count($imgArray));
501 foreach ($imgArray as $k => $image) {
502 $descr = $descrArray[$k];
503 $absImagePath = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($image, 1, 1);
504 if ($absImagePath && @is_file($absImagePath)) {
505 $imgFile = substr($absImagePath, strlen(PATH_site));
506 $imgInfo = @getimagesize($absImagePath);
507 if (is_array($imgInfo)) {
508 $imgFile = '../' . $imgFile;
509 $code .= '<br /><img src="' . $imgFile . '" ' . $imgInfo[3] . ' class="c-inlineimg" alt="" /><br />
510 ';
511 $code .= '<p><em>' . htmlspecialchars($descr) . '</em></p>
512 ';
513 } else {
514 $code .= '<div style="background-color: red; border: 1px solid black; color: white;">NOT AN IMAGE: ' . $imgFile . '</div>';
515 }
516 } else {
517 $code .= '<div style="background-color: red; border: 1px solid black; color: white;">IMAGE FILE NOT FOUND: ' . $image . '</div>';
518 }
519 }
520 }
521 return $code;
522 }
523
524 /**
525 * Returns header HTML content
526 *
527 * @param string $str Header text
528 * @param integer $type Header type (1, 0)
529 * @return string The HTML for the header.
530 * @todo Define visibility
531 */
532 public function headerLine($str, $type = 0) {
533 switch ($type) {
534 case 1:
535 $str = '<h2 class="t3-row-header">' . htmlspecialchars($str) . '</h2>
536 ';
537 break;
538 case 0:
539 $str = '<h3 class="divider">' . htmlspecialchars($str) . '</h3>
540 ';
541 break;
542 }
543 return $str;
544 }
545
546 /**
547 * Returns prepared content
548 *
549 * @param string $str Content to format.
550 * @return string Formatted content.
551 * @todo Define visibility
552 */
553 public function prepareContent($str) {
554 return '<p>' . nl2br(trim(strip_tags($str, $this->allowedHTML))) . '</p>
555 ';
556 }
557
558 /**
559 * Prints a single $table/$field information piece
560 * If $anchors is set, then seeAlso references to the same table will be page-anchors, not links.
561 *
562 * @param string $key CSH key / table name
563 * @param string $field Sub key / field name
564 * @param boolean $anchors If anchors is to be shown.
565 * @return string HTML content
566 * @todo Define visibility
567 */
568 public function printItem($key, $field, $anchors = FALSE) {
569 $out = '';
570 // Load full table definition in $GLOBALS['TCA']
571 \TYPO3\CMS\Core\Utility\GeneralUtility::loadTCA($key);
572 if ($key && (!$field || is_array($GLOBALS['TCA_DESCR'][$key]['columns'][$field]))) {
573 // Make seeAlso references.
574 $seeAlsoRes = $this->make_seeAlso($GLOBALS['TCA_DESCR'][$key]['columns'][$field]['seeAlso'], $anchors ? $key : '');
575 // Making item:
576 $out = '<a name="' . $key . '.' . $field . '"></a>' . $this->headerLine($this->getTableFieldLabel($key, $field), 1) . $this->prepareContent($GLOBALS['TCA_DESCR'][$key]['columns'][$field]['description']) . ($GLOBALS['TCA_DESCR'][$key]['columns'][$field]['details'] ? $this->headerLine(($GLOBALS['LANG']->getLL('details') . ':')) . $this->prepareContent($GLOBALS['TCA_DESCR'][$key]['columns'][$field]['details']) : '') . ($GLOBALS['TCA_DESCR'][$key]['columns'][$field]['syntax'] ? $this->headerLine(($GLOBALS['LANG']->getLL('syntax') . ':')) . $this->prepareContent($GLOBALS['TCA_DESCR'][$key]['columns'][$field]['syntax']) : '') . ($GLOBALS['TCA_DESCR'][$key]['columns'][$field]['image'] ? $this->printImage($GLOBALS['TCA_DESCR'][$key]['columns'][$field]['image'], $GLOBALS['TCA_DESCR'][$key]['columns'][$field]['image_descr']) : '') . ($GLOBALS['TCA_DESCR'][$key]['columns'][$field]['seeAlso'] && $seeAlsoRes ? $this->headerLine(($GLOBALS['LANG']->getLL('seeAlso') . ':')) . '<p>' . $seeAlsoRes . '</p>' : '') . ($this->back ? '<br /><p><a href="' . htmlspecialchars(('mod.php?M=help_cshmanual&tfID=' . rawurlencode($this->back))) . '" class="typo3-goBack">' . htmlspecialchars($GLOBALS['LANG']->getLL('goBack')) . '</a></p>' : '') . '<br />';
577 }
578 return $out;
579 }
580
581 /**
582 * Returns labels for a given field in a given structure
583 *
584 * @param string $key CSH key / table name
585 * @param string $field Sub key / field name
586 * @return array Table and field labels in a numeric array
587 * @todo Define visibility
588 */
589 public function getTableFieldNames($key, $field) {
590 $GLOBALS['LANG']->loadSingleTableDescription($key);
591 // Define the label for the key
592 $keyName = $key;
593 if (is_array($GLOBALS['TCA_DESCR'][$key]['columns']['']) && isset($GLOBALS['TCA_DESCR'][$key]['columns']['']['alttitle'])) {
594 // If there's an alternative title, use it
595 $keyName = $GLOBALS['TCA_DESCR'][$key]['columns']['']['alttitle'];
596 } elseif (isset($GLOBALS['TCA'][$key])) {
597 // Otherwise, if it's a table, use its title
598 $keyName = $GLOBALS['TCA'][$key]['ctrl']['title'];
599 } else {
600 // If no title was found, make sure to remove any "_MOD_"
601 $keyName = preg_replace('/^_MOD_/', '', $key);
602 }
603 // Define the label for the field
604 $fieldName = $field;
605 if (is_array($GLOBALS['TCA_DESCR'][$key]['columns'][$field]) && isset($GLOBALS['TCA_DESCR'][$key]['columns'][$field]['alttitle'])) {
606 // If there's an alternative title, use it
607 $fieldName = $GLOBALS['TCA_DESCR'][$key]['columns'][$field]['alttitle'];
608 } elseif (isset($GLOBALS['TCA'][$key]) && isset($GLOBALS['TCA'][$key]['columns'][$field])) {
609 // Otherwise, if it's a table, use its title
610 $fieldName = $GLOBALS['TCA'][$key]['columns'][$field]['label'];
611 }
612 return array($keyName, $fieldName);
613 }
614
615 /**
616 * Returns composite label for table/field
617 *
618 * @param string $key CSH key / table name
619 * @param string $field Sub key / field name
620 * @param string $mergeToken Token to merge the two strings with
621 * @return string Labels joined with merge token
622 * @see getTableFieldNames()
623 * @todo Define visibility
624 */
625 public function getTableFieldLabel($key, $field = '', $mergeToken = ': ') {
626 $tableName = '';
627 $fieldName = '';
628 // Get table / field parts:
629 list($tableName, $fieldName) = $this->getTableFieldNames($key, $field);
630 // Create label:
631 $labelString = $GLOBALS['LANG']->sL($tableName) . ($field ? $mergeToken . rtrim(trim($GLOBALS['LANG']->sL($fieldName)), ':') : '');
632 return $labelString;
633 }
634
635 /******************************
636 * Glossary related
637 ******************************/
638
639 /**
640 * Creates glossary index in $this->glossaryWords
641 * Glossary is cached in cache_hash cache and so will be updated only when cache is cleared.
642 *
643 * @return void
644 * @todo Define visibility
645 */
646 public function createGlossaryIndex() {
647 // Create hash string and try to retrieve glossary array:
648 $hash = md5('typo3/mod.php?M=help_cshmanual:glossary');
649 list($this->glossaryWords, $this->substWords) = unserialize(\TYPO3\CMS\Backend\Utility\BackendUtility::getHash($hash));
650 // Generate glossary words if not found:
651 if (!is_array($this->glossaryWords)) {
652 // Initialize:
653 $this->glossaryWords = array();
654 $this->substWords = array();
655 $CSHkeys = array_flip(array_keys($GLOBALS['TCA_DESCR']));
656 // Glossary
657 foreach ($CSHkeys as $cshKey => $value) {
658 if (\TYPO3\CMS\Core\Utility\GeneralUtility::isFirstPartOfStr($cshKey, 'xGLOSSARY_') && !isset($GLOBALS['TCA'][$cshKey])) {
659 $GLOBALS['LANG']->loadSingleTableDescription($cshKey);
660 if (is_array($GLOBALS['TCA_DESCR'][$cshKey]['columns'])) {
661 // Traverse table columns as listed in TCA_DESCR
662 foreach ($GLOBALS['TCA_DESCR'][$cshKey]['columns'] as $field => $data) {
663 if ($field) {
664 $this->glossaryWords[$cshKey . '.' . $field] = array(
665 'title' => trim($data['alttitle'] ? $data['alttitle'] : $cshKey),
666 'description' => str_replace('%22', '%23%23%23', rawurlencode($data['description']))
667 );
668 }
669 }
670 }
671 }
672 }
673 // First, create unique list of words:
674 foreach ($this->glossaryWords as $key => $value) {
675 // Making word lowercase in order to filter out same words in different cases.
676 $word = strtolower($value['title']);
677 if ($word !== '') {
678 $this->substWords[$word] = $value;
679 $this->substWords[$word]['key'] = $key;
680 }
681 }
682 krsort($this->substWords);
683 \TYPO3\CMS\Backend\Utility\BackendUtility::storeHash($hash, serialize(array($this->glossaryWords, $this->substWords)), 'Glossary');
684 }
685 }
686
687 /**
688 * Processing of all non-HTML content in the output
689 * Will be done by a call-back to ->substituteGlossaryWords_htmlcleaner_callback()
690 *
691 * @param string $code Input HTML code
692 * @return string Output HTML code
693 * @todo Define visibility
694 */
695 public function substituteGlossaryWords($code) {
696 $htmlParser = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Core\Html\HtmlParser');
697 $htmlParser->pObj = $this;
698 $code = $htmlParser->HTMLcleaner($code, array(), 1);
699 return $code;
700 }
701
702 /**
703 * Substituting glossary words in the CSH
704 * (This is a call-back function from "class local_t3lib_parsehtml extends t3lib_parsehtml", see top of this script)
705 *
706 * @param string $code Input HTML string
707 * @return string HTML with substituted words in.
708 * @todo Define visibility
709 */
710 public function substituteGlossaryWords_htmlcleaner_callback($code) {
711 if (is_array($this->substWords) && count($this->substWords) && strlen(trim($code))) {
712 // Substitute words:
713 foreach ($this->substWords as $wordKey => $wordSet) {
714 // quoteMeta used so special chars (which should not occur though) in words will not break the regex. Seemed to work (- kasper)
715 $parts = preg_split('/( |[\\(])(' . quoteMeta($wordSet['title']) . ')([\\.\\!\\)\\?\\:\\,]+| )/i', ' ' . $code . ' ', 2, PREG_SPLIT_DELIM_CAPTURE);
716 if (count($parts) == 5) {
717 $parts[2] = '<a class="glossary-term" href="' . htmlspecialchars(('mod.php?M=help_cshmanual&tfID=' . rawurlencode($wordSet['key']) . '&back=' . $this->tfID)) . '" title="' . rawurlencode(htmlspecialchars(\TYPO3\CMS\Core\Utility\GeneralUtility::fixed_lgd_cs(rawurldecode($wordSet['description']), 80))) . '">' . htmlspecialchars($parts[2]) . '</a>';
718 $code = substr(implode('', $parts), 1, -1);
719 // Disable entry so it doesn't get used next time:
720 unset($this->substWords[$wordKey]);
721 }
722 }
723 $code = str_replace('###', '&quot;', rawurldecode($code));
724 }
725 return $code;
726 }
727
728 }
729
730 ?>