TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
UserElementsController.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Rtehtmlarea\Controller;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 use Psr\Http\Message\ResponseInterface;
17 use Psr\Http\Message\ServerRequestInterface;
19 
24 {
28  public $content;
29 
33  public $modData;
34 
38  public $siteUrl;
39 
45  public $doc;
46 
50  public $editorNo;
51 
55  public function __construct()
56  {
57  $GLOBALS['LANG']->includeLLFile('EXT:rtehtmlarea/Resources/Private/Language/locallang_userelementscontroller.xlf');
58  $GLOBALS['LANG']->includeLLFile('EXT:rtehtmlarea/Resources/Private/Language/locallang_dialogs.xlf');
59 
60  $this->init();
61  }
62 
66  public function init()
67  {
68  $this->editorNo = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('editorNo');
69  $this->siteUrl = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
70  $this->doc = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Template\DocumentTemplate::class);
71  $this->doc->bodyTagAdditions = 'onload="Init();"';
72  $this->doc->form = '
73  <form action="" id="process" name="process" method="post">
74  <input type="hidden" name="processContent" value="" />
75  <input type="hidden" name="returnUrl" value="' . htmlspecialchars(\TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('REQUEST_URI')) . '" />
76  ';
77  $JScode = '
78  var plugin = window.parent.RTEarea["' . $this->editorNo . '"].editor.getPlugin("UserElements");
79  var HTMLArea = window.parent.HTMLArea;
80  var editor = plugin.editor;
81 
82  function Init() {
83  };
84  function insertHTML(content,noHide) {
85  plugin.restoreSelection();
86  editor.getSelection().insertHtml(content);
87  if(!noHide) plugin.close();
88  };
89  function wrapHTML(wrap1,wrap2,noHide) {
90  plugin.restoreSelection();
91  if(!editor.getSelection().isEmpty()) {
92  editor.getSelection().surroundHtml(wrap1,wrap2);
93  } else {
94  alert(' . GeneralUtility::quoteJSvalue($GLOBALS['LANG']->getLL('noTextSelection')) . ');
95  }
96  if(!noHide) plugin.close();
97  };
98  function processSelection(script) {
99  plugin.restoreSelection();
100  document.process.action = script;
101  document.process.processContent.value = editor.getSelection().getHtml();
102  document.process.submit();
103  };
104  function jumpToUrl(URL) {
105  var RTEtsConfigParams = "&RTEtsConfigParams=' . rawurlencode(\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('RTEtsConfigParams')) . '";
106  var editorNo = "&editorNo=' . rawurlencode($this->editorNo) . '";
107  theLocation = URL+RTEtsConfigParams+editorNo;
108  window.location.href = theLocation;
109  }
110  ';
111 
112  // unset the default jumpToUrl() function
113  unset($this->doc->JScodeArray['jumpToUrl']);
114 
115  $this->doc->JScode = GeneralUtility::wrapJS($JScode);
116  $this->modData = $GLOBALS['BE_USER']->getModuleData('user.php', 'ses');
117  if (\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('OC_key')) {
118  $parts = explode('|', \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('OC_key'));
119  $this->modData['openKeys'][$parts[1]] = $parts[0] == 'O' ? 1 : 0;
120  $GLOBALS['BE_USER']->pushModuleData('user.php', $this->modData);
121  }
122  }
123 
132  public function mainAction(ServerRequestInterface $request, ResponseInterface $response)
133  {
134  $content = $this->main_user($this->modData['openKeys']);
135  $response->getBody()->write($content);
136  return $response;
137  }
138 
139  /********************************
140  *
141  * Other functions
142  *
143  *********************************/
150  public function calcWH($imgInfo, $maxW = 380, $maxH = 500)
151  {
152  $IW = $imgInfo[0];
153  $IH = $imgInfo[1];
154  if ($IW > $maxW) {
155  $IH = ceil($IH / $IW * $maxW);
156  $IW = $maxW;
157  }
158  if ($IH > $maxH) {
159  $IW = ceil($IW / $IH * $maxH);
160  $IH = $maxH;
161  }
162  $imgInfo[3] = 'width="' . $IW . '" height="' . $IH . '"';
163  return $imgInfo;
164  }
165 
172  public function main_user($openKeys)
173  {
174  // Starting content:
175  $content = $this->doc->startPage(htmlspecialchars($GLOBALS['LANG']->getLL('Insert Custom Element')));
176  $RTEtsConfigParts = explode(':', \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('RTEtsConfigParams'));
177  $RTEsetup = $GLOBALS['BE_USER']->getTSConfig('RTE', \TYPO3\CMS\Backend\Utility\BackendUtility::getPagesTSconfig($RTEtsConfigParts[5]));
178  $thisConfig = \TYPO3\CMS\Backend\Utility\BackendUtility::RTEsetup($RTEsetup['properties'], $RTEtsConfigParts[0], $RTEtsConfigParts[2], $RTEtsConfigParts[4]);
179  if (is_array($thisConfig['userElements.'])) {
180  $categories = [];
181  foreach ($thisConfig['userElements.'] as $k => $value) {
182  $ki = (int)$k;
183  $v = $thisConfig['userElements.'][$ki . '.'];
184  if (substr($k, -1) == '.' && is_array($v)) {
185  $subcats = [];
186  $openK = $ki;
187  if ($openKeys[$openK]) {
188  $mArray = '';
189  if ($v['load'] === 'images_from_folder') {
190  $mArray = [];
191  if ($v['path'] && @is_dir((PATH_site . $v['path']))) {
192  $files = \TYPO3\CMS\Core\Utility\GeneralUtility::getFilesInDir(PATH_site . $v['path'], 'gif,jpg,jpeg,png', 0, '');
193  if (is_array($files)) {
194  $c = 0;
195  foreach ($files as $filename) {
196  $iInfo = @getimagesize((PATH_site . $v['path'] . $filename));
197  $iInfo = $this->calcWH($iInfo, 50, 100);
198  $ks = (string)(100 + $c);
199  $mArray[$ks] = $filename;
200  $mArray[$ks . '.'] = [
201  'content' => '<img src="' . $this->siteUrl . $v['path'] . $filename . '" />',
202  '_icon' => '<img src="' . $this->siteUrl . $v['path'] . $filename . '" ' . $iInfo[3] . ' />',
203  'description' => $GLOBALS['LANG']->getLL('filesize') . ': ' . str_replace('&nbsp;', ' ', \TYPO3\CMS\Core\Utility\GeneralUtility::formatSize(@filesize((PATH_site . $v['path'] . $filename)))) . ', ' . htmlspecialchars($GLOBALS['LANG']->getLL('pixels')) . ': ' . $iInfo[0] . 'x' . $iInfo[1]
204  ];
205  $c++;
206  }
207  }
208  }
209  }
210  if (is_array($mArray)) {
211  if ($v['merge']) {
213  $v = $mArray;
214  } else {
215  $v = $mArray;
216  }
217  }
218  foreach ($v as $k2 => $dummyValue) {
219  $k2i = (int)$k2;
220  if (substr($k2, -1) == '.' && is_array($v[$k2i . '.'])) {
221  $title = trim($v[$k2i]);
222  if (!$title) {
223  $title = '[' . htmlspecialchars($GLOBALS['LANG']->getLL('noTitle')) . ']';
224  } else {
225  $title = htmlspecialchars($GLOBALS['LANG']->sL($title));
226  }
227  $description = htmlspecialchars($GLOBALS['LANG']->sL($v[$k2i . '.']['description'])) . '<br />';
228  if (!$v[$k2i . '.']['dontInsertSiteUrl']) {
229  $v[$k2i . '.']['content'] = str_replace('###_URL###', $this->siteUrl, $v[$k2i . '.']['content']);
230  }
231  $logo = $v[$k2i . '.']['_icon'] ?: '';
232  $onClickEvent = '';
233  switch ((string)$v[$k2i . '.']['mode']) {
234  case 'wrap':
235  $wrap = explode('|', $v[$k2i . '.']['content']);
236  $onClickEvent = 'wrapHTML(' . GeneralUtility::quoteJSvalue($wrap[0]) . ',' . GeneralUtility::quoteJSvalue($wrap[1]) . ',false);';
237  break;
238  case 'processor':
239  $script = trim($v[$k2i . '.']['submitToScript']);
240  if (substr($script, 0, 4) != 'http') {
241  $script = $this->siteUrl . $script;
242  }
243  if ($script) {
244  $onClickEvent = 'processSelection(' . GeneralUtility::quoteJSvalue($script) . ');';
245  }
246  break;
247  case 'insert':
248 
249  default:
250  $onClickEvent = 'insertHTML(' . GeneralUtility::quoteJSvalue($v[$k2i . '.']['content']) . ');';
251  }
252  $A = ['<a href="#" onClick="' . $onClickEvent . 'return false;">', '</a>'];
253  $subcats[$k2i] = '<tr>
254  <td></td>
255  <td>' . $A[0] . $logo . $A[1] . '</td>
256  <td>' . $A[0] . '<strong>' . $title . '</strong><br />' . $description . $A[1] . '</td>
257  </tr>';
258  }
259  }
260  ksort($subcats);
261  }
262  $categories[$ki] = implode('', $subcats);
263  }
264  }
265  ksort($categories);
266  // Render menu of the items:
267  $lines = [];
268  foreach ($categories as $k => $v) {
269  $title = trim($thisConfig['userElements.'][$k]);
270  $openK = $k;
271  if (!$title) {
272  $title = '[' . htmlspecialchars($GLOBALS['LANG']->getLL('noTitle')) . ']';
273  } else {
274  $title = htmlspecialchars($GLOBALS['LANG']->sL($title));
275  }
276 
277  $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
278  $url = (string)$uriBuilder->buildUriFromRoute('rtehtmlarea_wizard_user_elements', ['OC_key' => ($openKeys[$openK] ? 'C|' : 'O|') . $openK]);
279 
280  $lines[] = '<tr><td colspan="3"><a href="#" title="' . htmlspecialchars($GLOBALS['LANG']->getLL('expand')) . '" onClick="jumpToUrl(' . GeneralUtility::quoteJSvalue($url) . ');return false;"><i class="fa fa-caret-square-o-' . ($openKeys[$openK] ? 'left' : 'right') . '" title="' . htmlspecialchars($GLOBALS['LANG']->getLL('expand')) . '"></i><strong>' . $title . '</strong></a></td></tr>';
281  $lines[] = $v;
282  }
283  $content .= '<table class="table table-striped table-hover">' . implode('', $lines) . '</table>';
284  }
285  $content .= $this->doc->endPage();
286  return $content;
287  }
288 }
mainAction(ServerRequestInterface $request, ResponseInterface $response)
static formatSize($sizeInBytes, $labels= '', $base=0)
static getPagesTSconfig($id, $rootLine=null, $returnPartArray=false)
static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
if(TYPO3_MODE=== 'BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
static RTEsetup($RTEprop, $table, $field, $type= '')
static makeInstance($className,...$constructorArguments)
static getFilesInDir($path, $extensionList= '', $prependPath=false, $order= '', $excludePattern= '')