Completed
Push — 7LTS_compatible ( 1f867f...a6fb07 )
by Tomas Norre
02:14
created

FieldVisibility::getLLL()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace AOE\Languagevisibility;
4
5
/***************************************************************
6
 * Copyright notice
7
 *
8
 * (c) 2007 AOE media ([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
/**
29
 * Class FieldVisibility
30
 * @package AOE\Languagevisibility
31
 */
32
class FieldVisibility {
33
	private $isNewElement = FALSE;
34
	private $pageId = 0;
35
	private $modTSconfig = array();
36
	private $calcPerms = FALSE;
37
38
	/**
39
	 * @return void
40
	 */
41
	public function init() {
42
		$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...
43
	}
44
45
	/**
46
	 * @param $PA
47
	 * @param $fobj
48
	 * @return string
49
	 */
50
	public function user_fieldvisibility($PA) {
51
		$content = '';
52
		$this->init();
53
54
			//init some class attributes
55
		$this->pageId = $PA['row']['pid'];
56
		$uid = $PA['row']['uid'];
57
58
		if (substr($uid, 0, 3) == 'NEW') {
59
			$this->isNewElement = TRUE;
60
		}
61
		if ($PA['table'] == 'pages' && ! $this->isNewElement) {
62
			$this->pageId = $PA['row']['uid'];
63
		}
64
65
		$_modTSconfig = $GLOBALS['BE_USER']->getTSConfig('mod.languagevisibility', \TYPO3\CMS\Backend\Utility\BackendUtility::getPagesTSconfig($this->pageId));
66
		$this->modTSconfig = $_modTSconfig['properties'];
67
68
		$languageRep = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('\\AOE\\Languagevisibility\\LanguageRepository');
69
		$dao = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('tx_languagevisibility_daocommon');
70
71
		$elementfactory = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('\\AOE\\Languagevisibility\\ElementFactory', $dao);
72
73
		$value = $PA['row'][$PA['field']];
74
		$table = $PA['table'];
75
		$isOverlay = tx_languagevisibility_beservices::isOverlayRecord($PA['row'], $table);
76
77
		$visivilitySetting = @unserialize($value);
78
		if (! is_array($visivilitySetting) && $value != '') {
79
			$content .= 'Visibility Settings seems to be corrupt:' . $value;
80
		}
81
82
		if ($isOverlay) {
83
			$uid = tx_languagevisibility_beservices::getOriginalUidOfTranslation($PA['row'], $table);
84
			$table = tx_languagevisibility_beservices::getOriginalTableOfTranslation($table);
85
86
				//This element is an overlay therefore we need to render the visibility field just for the language of the overlay
87
			$overlayRecordsLanguage = $languageRep->getLanguageById($PA['row']['sys_language_uid']);
88
89
			try {
90
				$originalElement = $elementfactory->getElementForTable($table, $uid);
91
			} catch ( Exception $e ) {
0 ignored issues
show
Bug introduced by
The class AOE\Languagevisibility\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
92
				return '';
93
			}
94
95
			$infosStruct = $this->_getLanguageInfoStructurListForElementAndLanguageList($originalElement, array($overlayRecordsLanguage ), $PA['itemFormElName'], TRUE);
96
		} else {
97
				//This element is an original element (no overlay)
98
			try {
99
				$originalElement = $elementfactory->getElementForTable($table, $uid);
100
			} catch ( Exception $e ) {
0 ignored issues
show
Bug introduced by
The class AOE\Languagevisibility\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
101
				return 'sorry this element supports no visibility settings';
102
			}
103
104
			$content .= $originalElement->getInformativeDescription();
105
106
			if ($originalElement->isMonolithicTranslated()) {
107
				return $content;
108
			}
109
110
			$languageList = $languageRep->getLanguages();
111
			$infosStruct = $this->_getLanguageInfoStructurListForElementAndLanguageList($originalElement, $languageList, $PA['itemFormElName'], FALSE);
112
		}
113
114
		$content .= $this->renderLanguageInfos($infosStruct);
115
		return '<div id="fieldvisibility">' . $content . '<a href="#" onclick="resetSelectboxes()">reset</a></div>' . $this->_javascript();
116
	}
117
118
	/**
119
	 * This methid is used to generate an infostructur array, which will be
120
	 * renderd as a Form
121
	 *
122
	 * @param tx_languagevisibility_element $changeableElement
123
	 * @param array $languageList
124
	 * @param string $itemFormElName
125
	 * @param boolean $isOverlay
126
	 * @return unknown
127
	 */
128
	public function _getLanguageInfoStructurListForElementAndLanguageList($changeableElement, $languageList, $itemFormElName, $isOverlay) {
129
130
		$visibility = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('\\AOE\\Languagevisibility\\VisibilityService');
131
		$visibilityString = '';
132
		$infosStruct = array();
133
134
		foreach ( $languageList as $language ) {
135
136
			$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) );
137
138
				// if there is no access to language - and localsettings exist, then do not show select box
139
				// this is to not be able as an translator to override languagesetting
140
			$currentSetting = $changeableElement->getLocalVisibilitySetting($language->getUid());
141
			$currentOptionsForUserAndLanguage = tx_languagevisibility_beservices::getAvailableOptionsForLanguage($language, $isOverlay, $changeableElement);
142
			if ($currentSetting == '' || isset($currentOptionsForUserAndLanguage[$currentSetting])) {
143
144
				if ($isOverlay) {
145
					$defaultSelect = $changeableElement->getVisibilitySettingStoredInOverlayRecord($language->getUid());
146
147
					$visibilityValue = $changeableElement->getVisibilitySettingStoredInDefaultRecord($language->getUid());
148
					$visibilityString = $currentOptionsForUserAndLanguage[$visibilityValue];
149
				} else {
150
					$defaultSelect = $changeableElement->getVisibilitySettingStoredInDefaultRecord($language->getUid());
151
152
					$visibilityValue = $changeableElement->getVisibilitySettingStoredInOverlayRecord($language->getUid());
153
					$visibilityString = $currentOptionsForUserAndLanguage[$visibilityValue];
154
				}
155
156
				if ($this->isNewElement && $defaultSelect == '') {
157
					if ($this->modTSconfig['language.'][$language->getUid() . '.']['defaultVisibilityOnCreate'] != '') {
158
						$defaultSelect = $this->modTSconfig['language.'][$language->getUid() . '.']['defaultVisibilityOnCreate'];
159
					}
160
				}
161
				$selectBox = $this->getSelectBox($language->getUid(), $currentOptionsForUserAndLanguage, $defaultSelect, $itemFormElName);
162
			} else {
163
				$selectBox = '<input type="hidden" name="' . $itemFormElName . '[' . $language->getUid() . ']" value="' . $currentSetting . '" ></input>(' . $currentSetting . ')';
164
			}
165
166
			if ($isOverlay) {
167
				$infoitem['overlayVisibility'] = $selectBox;
168
				$infoitem['originalVisibility'] = $visibilityString;
169
			} else {
170
				$infoitem['overlayVisibility'] = $visibilityString;
171
				$infoitem['originalVisibility'] = $selectBox;
172
			}
173
174
			$infosStruct[] = $infoitem;
175
		}
176
177
		return $infosStruct;
178
	}
179
180
	/**
181
	 * Generates the selectbox for the languagevisibility settings of an item
182
	 *
183
	 * @param int $languageid
184
	 * @param array $select
185
	 * @param string $current current selected item
186
	 * @param string $name
187
	 * @return string
188
	 */
189
	protected function getSelectBox($languageid, $select, $current, $name) {
190
		$content = '';
191
		$addClassName = '';
192
		if (count($select) == 1) {
193
			$addClassName = ' oneitem';
194
		}
195
196
		$content .= '<select class="fieldvisibility_selects' . $addClassName . '" name="' . $name . '[' . $languageid . ']">';
197
		foreach ( $select as $skey => $svalue ) {
198
			if ($current == $skey) {
199
				$selected = ' selected="selected"';
200
			} else {
201
				$selected = '';
202
			}
203
			$content .= '<option class="' . $this->getCSSClassFromVisibilityKey($skey) . '" value="' . $skey . '"' . $selected . '>' . $svalue . '</option>';
204
		}
205
		$content .= '</select>';
206
		return $content;
207
208
	}
209
210
	/**
211
	 * This method is used to determine a css class for the diffrent visiblity modes
212
	 *
213
	 * @param string
214
	 * @return string
215
	 */
216
	protected function getCSSClassFromVisibilityKey($key) {
217
		$res = '';
218
		switch ($key) {
219
			case 'yes' :
220
			case 'no' :
221
			case 't' :
222
			case 'f' :
223
				$res = $key;
224
				break;
225
			case 'no+' :
226
				$res = 'no_inherited';
227
				break;
228
		}
229
230
		return $res;
231
	}
232
233
	protected function renderLanguageInfos($infosStruct) {
234
		$content = '<style type="text/css">
235
		.visibilitytable  {margin: 10px 0 0 0}
236
		.visibilitytable  .bgColor4 {background-color: #C9B88B}
237
		.visibilitytable  .bgColor {background-color: #FFEED4}
238
		.visibilitytable  .lastcell {background-color: #DEEAB5}
239
		.visibilitytable  .bgColor .lastcell {background-color: #E8EAB5}
240
		.visibilitytable  .bgColor4 .lastcell {border-bottom: 1px solid #333333; background-color: #C9B88B}
241
		.visibilitytable th {padding: 2px 5px 2px 2px; text-align: left; font-size: 12px;}
242
		.visibilitytable select {width: 100px}
243
		.visibilitytable select.oneitem {background-color: #999999}
244
		.visibilitytable select option {background-color: #83FF73}
245
		.visibilitytable select .yes {background-color: #E0FF81}
246
		.visibilitytable select .no {background-color: #FFCE81}
247
		.visibilitytable select .no_inherited {background-color: #FF8881}
248
		.visibilitytable select .t {background-color: #BFFFB7}
249
		.visibilitytable select .f {background-color: #BFFFB7}
250
		.visibilitytable td {padding: 0 5px 2px 2px}
251
		</' . 'style>';
252
253
		$content .= '<table style="border-collapse: collapse;" class="visibilitytable">';
254
		$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>';
255
256
		$i=0;
257
		foreach ( $infosStruct as $info ) {
258
			$i ++;
259
260
				// toggle row class
261
			$class = ($i % 2) ? ' class="bgColor"' : '';
262
			$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>';
263
		}
264
265
		$content .= '</table>';
266
		return $content;
267
	}
268
269
	/**
270
	 * @param $key
271
	 * @return mixed
272
	 */
273
	public function getLLL($key) {
274
		return $GLOBALS['LANG']->sl('LLL:EXT:languagevisibility/locallang_db.xml:' . $key);
275
	}
276
277
	/**
278
	 * Generated a little status icon
279
	 *
280
	 * @param boolean positive or negative state
281
	 * @param string $title
282
	 * @return html tag to include the state image
283
	 */
284
	protected function _getStatusImage($stat, $title = '') {
285
		if ($stat) {
286
			return '<img src="../typo3conf/ext/languagevisibility/res/ok.gif" title="' . htmlspecialchars($title) . '">';
287
		} else {
288
			return '<img src="../typo3conf/ext/languagevisibility/res/nok.gif" title="' . htmlspecialchars($title) . '">';
289
		}
290
	}
291
292
	protected function _link_edit($table, $id) {
293
		$params = '&table=' . $table . '&edit[' . $table . '][' . $id . ']=edit';
294
		$url = $GLOBALS['BACK_PATH'] . 'alt_doc.php?id=' . $id . $params;
295
		return '<a href="' . $url . '" target="blank">[edit]</a>';
296
	}
297
298
	/*******************************************
299
	 *
300
	 * Link functions (protected)
301
	 *
302
	 *******************************************/
303
304
	/**
305
	 * Returns an HTML link for editing
306
	 *
307
	 * @param	string		$label The label (or image)
308
	 * @param	string		$table The table, fx. 'tt_content'
309
	 * @param	integer		$uid The uid of the element to be edited
310
	 * @param	boolean		$forced By default the link is not shown if translatorMode is set, but with this boolean it can be forced anyway.
311
	 * @return	string		HTML anchor tag containing the label and the correct link
312
	 * @access protected
313
	 */
314
	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...
315
		if ($label) {
316
			if (($table == 'pages' && ($this->calcPerms & 2) || $table != 'pages' && ($this->calcPerms & 16))) {
317
318
				$params = '&edit[' . $table . '][' . $uid . ']=edit';
319
				$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...
320
				$url = "alt_doc.php?" . $retUrl . $params;
321
				$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')";
322
				return '<a style="text-decoration: none;" href="#" onclick="' . htmlspecialchars($onClick) . '">' . $label . '</a>';
323
324
			} else {
325
				return $label;
326
			}
327
		}
328
		return '';
329
	}
330
331
	/**
332
	 * @return string
333
	 */
334
	protected  function _javascript() {
335
336
		return '
337
<script type="text/javascript">
338
339
function resetSelectboxes() {
340
	var obj=getElementsByClassName("fieldvisibility_selects");
341
	for(i=0;i<obj.length;i++)
342
  {
343
    obj[i].selectedIndex=0;
344
  }
345
}
346
347
function getElementsByClassName(class_name)
348
{
349
  var all_obj,ret_obj=new Array(),j=0,teststr;
350
351
  if(document.all)all_obj=document.all;
352
  else if(document.getElementsByTagName && !document.all)
353
    all_obj=document.getElementsByTagName("*");
354
355
  for(i=0;i<all_obj.length;i++)
356
  {
357
    if(all_obj[i].className.indexOf(class_name)!=-1)
358
    {
359
      teststr=","+all_obj[i].className.split(" ").join(",")+",";
360
      if(teststr.indexOf(","+class_name+",")!=-1)
361
      {
362
        ret_obj[j]=all_obj[i];
363
        j++;
364
      }
365
    }
366
  }
367
  return ret_obj;
368
}
369
370
</script>';
371
	}
372
}
373