FieldVisibility::getCSSClassFromVisibilityKey()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 13
nc 6
nop 1
1
<?php
2
3
namespace AOE\Languagevisibility;
4
5
/***************************************************************
6
 * Copyright notice
7
 *
8
 * (c) 2016 AOE GmbH <[email protected]>
9
 * All rights reserved
10
 *
11
 * This script is part of the TYPO3 project. The TYPO3 project is
12
 * free software; you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation; either version 2 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * The GNU General Public License can be found at
18
 * http://www.gnu.org/copyleft/gpl.html.
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
use AOE\Languagevisibility\Services\BeServices;
29
use TYPO3\CMS\Core\Utility\GeneralUtility;
30
31
/**
32
 * Class FieldVisibility
33
 * @package AOE\Languagevisibility
34
 */
35
class FieldVisibility {
36
	private $isNewElement = FALSE;
37
	private $pageId = 0;
38
	private $modTSconfig = array();
39
	private $calcPerms = FALSE;
40
41
	/**
42
	 * @return void
43
	 */
44
	public function init() {
45
		$this->calcPerms = $GLOBALS['BE_USER']->calcPerms($pageInfoArr);
0 ignored issues
show
Bug introduced by
The variable $pageInfoArr does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
46
	}
47
48
	/**
49
	 * @param $PA
50
	 * @param $fobj
51
	 * @return string
52
	 */
53
	public function user_fieldvisibility($PA) {
54
		$content = '';
55
		$this->init();
56
57
			//init some class attributes
58
		$this->pageId = $PA['row']['pid'];
59
		$uid = $PA['row']['uid'];
60
61
		if (substr($uid, 0, 3) == 'NEW') {
62
			$this->isNewElement = TRUE;
63
		}
64
		if ($PA['table'] == 'pages' && ! $this->isNewElement) {
65
			$this->pageId = $PA['row']['uid'];
66
		}
67
68
		$_modTSconfig = $GLOBALS['BE_USER']->getTSConfig('mod.languagevisibility', \TYPO3\CMS\Backend\Utility\BackendUtility::getPagesTSconfig($this->pageId));
69
		$this->modTSconfig = $_modTSconfig['properties'];
70
71
		$languageRep = GeneralUtility::makeInstance('AOE\\Languagevisibility\\LanguageRepository');
72
		$dao = GeneralUtility::makeInstance('AOE\\Languagevisibility\\Dao\DaoCommon');
73
74
		$elementfactory = GeneralUtility::makeInstance('AOE\\Languagevisibility\\ElementFactory', $dao);
75
76
		$value = $PA['row'][$PA['field']];
77
		$table = $PA['table'];
78
		$isOverlay = BeServices::isOverlayRecord($PA['row'], $table);
79
80
		$visivilitySetting = @unserialize($value);
81
		if (! is_array($visivilitySetting) && $value != '') {
82
			$content .= 'Visibility Settings seems to be corrupt:' . $value;
83
		}
84
85
		if ($isOverlay) {
86
			$uid = BeServices::getOriginalUidOfTranslation($PA['row'], $table);
87
			$table = BeServices::getOriginalTableOfTranslation($table);
88
89
				//This element is an overlay therefore we need to render the visibility field just for the language of the overlay
90
			$overlayRecordsLanguage = $languageRep->getLanguageById($PA['row']['sys_language_uid']);
91
92
			try {
93
				$originalElement = $elementfactory->getElementForTable($table, $uid);
94
			} catch ( \Exception $e ) {
95
				return '';
96
			}
97
98
			$infosStruct = $this->_getLanguageInfoStructurListForElementAndLanguageList($originalElement, array($overlayRecordsLanguage ), $PA['itemFormElName'], TRUE);
99
		} else {
100
				//This element is an original element (no overlay)
101
			try {
102
				$originalElement = $elementfactory->getElementForTable($table, $uid);
103
			} catch ( \Exception $e ) {
104
				return 'sorry this element supports no visibility settings';
105
			}
106
107
			$content .= $originalElement->getInformativeDescription();
108
109
			if ($originalElement->isMonolithicTranslated()) {
110
				return $content;
111
			}
112
113
			$languageList = $languageRep->getLanguages();
114
			$infosStruct = $this->_getLanguageInfoStructurListForElementAndLanguageList($originalElement, $languageList, $PA['itemFormElName'], FALSE);
115
		}
116
117
		$content .= $this->renderLanguageInfos($infosStruct);
118
		return '<div id="fieldvisibility">' . $content . '<a href="#" onclick="resetSelectboxes()">reset</a></div>' . $this->_javascript();
119
	}
120
121
	/**
122
	 * This methid is used to generate an infostructur array, which will be
123
	 * renderd as a Form
124
	 *
125
	 * @param tx_languagevisibility_element $changeableElement
126
	 * @param array $languageList
127
	 * @param string $itemFormElName
128
	 * @param boolean $isOverlay
129
	 * @return unknown
130
	 */
131
	public function _getLanguageInfoStructurListForElementAndLanguageList($changeableElement, $languageList, $itemFormElName, $isOverlay) {
132
133
		$visibility = GeneralUtility::makeInstance('AOE\\Languagevisibility\\Services\\VisibilityService');
134
		$visibilityString = '';
135
		$infosStruct = array();
136
137
		foreach ( $languageList as $language ) {
138
139
			$infoitem = array('visible' => $visibility->isVisible($language, $changeableElement), 'languageTitle' => $language->getTitle($this->pageId), 'languageFlag' => $language->getFlagImg($this->pageId), 'hasTranslation' => $changeableElement->hasTranslation($language->getUid()), 'isTranslation' => $isOverlay, 'isVisible' => $visibility->isVisible($language, $changeableElement), 'visibilityDescription' => $visibility->getVisibilityDescription($language, $changeableElement) );
140
141
				// if there is no access to language - and localsettings exist, then do not show select box
142
				// this is to not be able as an translator to override languagesetting
143
			$currentSetting = $changeableElement->getLocalVisibilitySetting($language->getUid());
144
			$currentOptionsForUserAndLanguage = BeServices::getAvailableOptionsForLanguage($language, $isOverlay, $changeableElement);
145
			if ($currentSetting == '' || isset($currentOptionsForUserAndLanguage[$currentSetting])) {
146
147
				if ($isOverlay) {
148
					$defaultSelect = $changeableElement->getVisibilitySettingStoredInOverlayRecord($language->getUid());
149
150
					$visibilityValue = $changeableElement->getVisibilitySettingStoredInDefaultRecord($language->getUid());
151
					$visibilityString = $currentOptionsForUserAndLanguage[$visibilityValue];
152
				} else {
153
					$defaultSelect = $changeableElement->getVisibilitySettingStoredInDefaultRecord($language->getUid());
154
155
					$visibilityValue = $changeableElement->getVisibilitySettingStoredInOverlayRecord($language->getUid());
156
					$visibilityString = $currentOptionsForUserAndLanguage[$visibilityValue];
157
				}
158
159
				if ($this->isNewElement && $defaultSelect == '') {
160
					if ($this->modTSconfig['language.'][$language->getUid() . '.']['defaultVisibilityOnCreate'] != '') {
161
						$defaultSelect = $this->modTSconfig['language.'][$language->getUid() . '.']['defaultVisibilityOnCreate'];
162
					}
163
				}
164
				$selectBox = $this->getSelectBox($language->getUid(), $currentOptionsForUserAndLanguage, $defaultSelect, $itemFormElName);
165
			} else {
166
				$selectBox = '<input type="hidden" name="' . $itemFormElName . '[' . $language->getUid() . ']" value="' . $currentSetting . '" ></input>(' . $currentSetting . ')';
167
			}
168
169
			if ($isOverlay) {
170
				$infoitem['overlayVisibility'] = $selectBox;
171
				$infoitem['originalVisibility'] = $visibilityString;
172
			} else {
173
				$infoitem['overlayVisibility'] = $visibilityString;
174
				$infoitem['originalVisibility'] = $selectBox;
175
			}
176
177
			$infosStruct[] = $infoitem;
178
		}
179
180
		return $infosStruct;
181
	}
182
183
	/**
184
	 * Generates the selectbox for the languagevisibility settings of an item
185
	 *
186
	 * @param int $languageid
187
	 * @param array $select
188
	 * @param string $current current selected item
189
	 * @param string $name
190
	 * @return string
191
	 */
192
	protected function getSelectBox($languageid, $select, $current, $name) {
193
		$content = '';
194
		$addClassName = '';
195
		if (count($select) == 1) {
196
			$addClassName = ' oneitem';
197
		}
198
199
		$content .= '<select class="fieldvisibility_selects' . $addClassName . '" name="' . $name . '[' . $languageid . ']">';
200
		foreach ( $select as $skey => $svalue ) {
201
			if ($current == $skey) {
202
				$selected = ' selected="selected"';
203
			} else {
204
				$selected = '';
205
			}
206
			$content .= '<option class="' . $this->getCSSClassFromVisibilityKey($skey) . '" value="' . $skey . '"' . $selected . '>' . $svalue . '</option>';
207
		}
208
		$content .= '</select>';
209
		return $content;
210
211
	}
212
213
	/**
214
	 * This method is used to determine a css class for the diffrent visiblity modes
215
	 *
216
	 * @param string
217
	 * @return string
218
	 */
219
	protected function getCSSClassFromVisibilityKey($key) {
220
		$res = '';
221
		switch ($key) {
222
			case 'yes' :
223
			case 'no' :
224
			case 't' :
225
			case 'f' :
226
				$res = $key;
227
				break;
228
			case 'no+' :
229
				$res = 'no_inherited';
230
				break;
231
		}
232
233
		return $res;
234
	}
235
236
	protected function renderLanguageInfos($infosStruct) {
237
		$content = '<style type="text/css">
238
		.visibilitytable  {margin: 10px 0 0 0}
239
		.visibilitytable  .bgColor4 {background-color: #C9B88B}
240
		.visibilitytable  .bgColor {background-color: #FFEED4}
241
		.visibilitytable  .lastcell {background-color: #DEEAB5}
242
		.visibilitytable  .bgColor .lastcell {background-color: #E8EAB5}
243
		.visibilitytable  .bgColor4 .lastcell {border-bottom: 1px solid #333333; background-color: #C9B88B}
244
		.visibilitytable th {padding: 2px 5px 2px 2px; text-align: left; font-size: 12px;}
245
		.visibilitytable select {width: 100px}
246
		.visibilitytable select.oneitem {background-color: #999999}
247
		.visibilitytable select option {background-color: #83FF73}
248
		.visibilitytable select .yes {background-color: #E0FF81}
249
		.visibilitytable select .no {background-color: #FFCE81}
250
		.visibilitytable select .no_inherited {background-color: #FF8881}
251
		.visibilitytable select .t {background-color: #BFFFB7}
252
		.visibilitytable select .f {background-color: #BFFFB7}
253
		.visibilitytable td {padding: 0 5px 2px 2px}
254
		</' . 'style>';
255
256
		$content .= '<table style="border-collapse: collapse;" class="visibilitytable">';
257
		$content .= '<tr class="bgColor4">' . '<th >' . $this->getLLL('language') . '</th>' . '<th >' . $this->getLLL('visibility_in_default') . '</th>' . '<th >' . $this->getLLL('visibility_in_overlay') . '</th>' . '<th>' . $this->getLLL('hastranslation') . '</th>' . '<th>' . $this->getLLL('isshown') . '</th>' . '</tr>';
258
259
		$i=0;
260
		foreach ( $infosStruct as $info ) {
261
			$i ++;
262
263
				// toggle row class
264
			$class = ($i % 2) ? ' class="bgColor"' : '';
265
			$content .= '<tr' . $class . '>' . '<td>' . $info['languageFlag'] . $info['languageTitle'] . '</td>' . '<td>' . $info['originalVisibility'] . '</td>' . '<td>' . $info['overlayVisibility'] . '</td>' . '<td style="text-align: center">' . $this->_getStatusImage($info['hasTranslation'] || $info['isTranslation'], '') . '</td>' . '<td style="text-align: center"  class="lastcell">' . $this->_getStatusImage($info['isVisible'], $info['visibilityDescription']) . '</td>' . '</tr>';
266
		}
267
268
		$content .= '</table>';
269
		return $content;
270
	}
271
272
	/**
273
	 * @param $key
274
	 * @return mixed
275
	 */
276
	public function getLLL($key) {
277
		return $GLOBALS['LANG']->sl('LLL:EXT:languagevisibility/locallang_db.xml:' . $key);
278
	}
279
280
	/**
281
	 * Generated a little status icon
282
	 *
283
	 * @param boolean positive or negative state
284
	 * @param string $title
285
	 * @return html tag to include the state image
286
	 */
287
	protected function _getStatusImage($stat, $title = '') {
288
		if ($stat) {
289
			return '<img src="../typo3conf/ext/languagevisibility/Resources/Public/Icons/ok.gif" title="' . htmlspecialchars($title) . '">';
290
		} else {
291
			return '<img src="../typo3conf/ext/languagevisibility/Resources/Public/Icons/nok.gif" title="' . htmlspecialchars($title) . '">';
292
		}
293
	}
294
295
	protected function _link_edit($table, $id) {
296
		$params = '&table=' . $table . '&edit[' . $table . '][' . $id . ']=edit';
297
		$url = $GLOBALS['BACK_PATH'] . 'alt_doc.php?id=' . $id . $params;
298
		return '<a href="' . $url . '" target="blank">[edit]</a>';
299
	}
300
301
	/*******************************************
302
	 *
303
	 * Link functions (protected)
304
	 *
305
	 *******************************************/
306
307
	/**
308
	 * Returns an HTML link for editing
309
	 *
310
	 * @param	string		$label The label (or image)
311
	 * @param	string		$table The table, fx. 'tt_content'
312
	 * @param	integer		$uid The uid of the element to be edited
313
	 * @param	boolean		$forced By default the link is not shown if translatorMode is set, but with this boolean it can be forced anyway.
314
	 * @return	string		HTML anchor tag containing the label and the correct link
315
	 * @access protected
316
	 */
317
	public function link_edit($label, $table, $uid, $forced = FALSE) {
0 ignored issues
show
Unused Code introduced by
The parameter $forced is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
318
		if ($label) {
319
			if (($table == 'pages' && ($this->calcPerms & 2) || $table != 'pages' && ($this->calcPerms & 16))) {
320
321
				$params = '&edit[' . $table . '][' . $uid . ']=edit';
322
				$retUrl = 'returnUrl=' . ($requestUri == - 1 ? "'+T3_THIS_LOCATION+'" : rawurlencode($requestUri ? $requestUri : \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('REQUEST_URI')));
0 ignored issues
show
Bug introduced by
The variable $requestUri does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
323
				$url = "alt_doc.php?" . $retUrl . $params;
324
				$onClick = "window.open('" . $url . "','editpopup','scrollbars=no,status=no,toolbar=no,location=no,directories=no,resizable=no,menubar=no,width=700,height=500,top=10,left=10')";
325
				return '<a style="text-decoration: none;" href="#" onclick="' . htmlspecialchars($onClick) . '">' . $label . '</a>';
326
327
			} else {
328
				return $label;
329
			}
330
		}
331
		return '';
332
	}
333
334
	/**
335
	 * @return string
336
	 */
337
	protected  function _javascript() {
338
339
		return '
340
<script type="text/javascript">
341
342
function resetSelectboxes() {
343
	var obj=getElementsByClassName("fieldvisibility_selects");
344
	for(i=0;i<obj.length;i++)
345
  {
346
    obj[i].selectedIndex=0;
347
  }
348
}
349
350
function getElementsByClassName(class_name)
351
{
352
  var all_obj,ret_obj=new Array(),j=0,teststr;
353
354
  if(document.all)all_obj=document.all;
355
  else if(document.getElementsByTagName && !document.all)
356
    all_obj=document.getElementsByTagName("*");
357
358
  for(i=0;i<all_obj.length;i++)
359
  {
360
    if(all_obj[i].className.indexOf(class_name)!=-1)
361
    {
362
      teststr=","+all_obj[i].className.split(" ").join(",")+",";
363
      if(teststr.indexOf(","+class_name+",")!=-1)
364
      {
365
        ret_obj[j]=all_obj[i];
366
        j++;
367
      }
368
    }
369
  }
370
  return ret_obj;
371
}
372
373
</script>';
374
	}
375
}
376