[TASK] Replace deprecated API calls and label references
[TYPO3CMS/Extensions/languagevisibility.git] / class.tx_languagevisibility_fieldvisibility.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2007 AOE media (dev@aoemedia.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 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * Class tx_languagevisibility_fieldvisibility
27 */
28 class tx_languagevisibility_fieldvisibility {
29 private $isNewElement = FALSE;
30 private $pageId = 0;
31 private $modTSconfig = array();
32
33 /**
34 * @return void
35 */
36 public function init() {
37 $this->calcPerms = $GLOBALS['BE_USER']->calcPerms($pageInfoArr);
38 }
39
40 /**
41 * @param $PA
42 * @param $fobj
43 * @return string
44 */
45 public function user_fieldvisibility($PA, $fobj) {
46 $this->init();
47
48 //init some class attributes
49 $this->pageId = $PA['row']['pid'];
50 $uid = $PA['row']['uid'];
51
52 if (substr($uid, 0, 3) == 'NEW') {
53 $this->isNewElement = TRUE;
54 }
55 if ($PA['table'] == 'pages' && ! $this->isNewElement) {
56 $this->pageId = $PA['row']['uid'];
57 }
58
59 $_modTSconfig = $GLOBALS['BE_USER']->getTSConfig('mod.languagevisibility', t3lib_BEfunc::getPagesTSconfig($this->pageId));
60 $this->modTSconfig = $_modTSconfig['properties'];
61
62 $languageRep = t3lib_div::makeInstance('tx_languagevisibility_languagerepository');
63 $dao = t3lib_div::makeInstance('tx_languagevisibility_daocommon');
64
65 $elementfactory = t3lib_div::makeInstance('tx_languagevisibility_elementFactory', $dao);
66
67 $value = $PA['row'][$PA['field']];
68 $table = $PA['table'];
69 $isOverlay = tx_languagevisibility_beservices::isOverlayRecord($PA['row'], $table);
70
71 $visivilitySetting = @unserialize($value);
72 if (! is_array($visivilitySetting) && $value != '') {
73 $content .= 'Visibility Settings seems to be corrupt:' . $value;
74 }
75
76 if ($isOverlay) {
77 $uid = tx_languagevisibility_beservices::getOriginalUidOfTranslation($PA['row'], $table);
78 $table = tx_languagevisibility_beservices::getOriginalTableOfTranslation($table);
79
80 //This element is an overlay therefore we need to render the visibility field just for the language of the overlay
81 $overlayRecordsLanguage = $languageRep->getLanguageById($PA['row']['sys_language_uid']);
82
83 try {
84 $originalElement = $elementfactory->getElementForTable($table, $uid);
85 } catch ( Exception $e ) {
86 return '';
87 }
88
89 $infosStruct = $this->_getLanguageInfoStructurListForElementAndLanguageList($originalElement, array($overlayRecordsLanguage ), $PA['itemFormElName'], TRUE);
90 } else {
91 //This element is an original element (no overlay)
92 try {
93 $originalElement = $elementfactory->getElementForTable($table, $uid);
94 } catch ( Exception $e ) {
95 return 'sorry this element supports no visibility settings';
96 }
97
98 $content .= $originalElement->getInformativeDescription();
99
100 if ($originalElement->isMonolithicTranslated()) {
101 return $content;
102 }
103
104 $languageList = $languageRep->getLanguages();
105 $infosStruct = $this->_getLanguageInfoStructurListForElementAndLanguageList($originalElement, $languageList, $PA['itemFormElName'], FALSE);
106 }
107
108 $content .= $this->renderLanguageInfos($infosStruct);
109 return '<div id="fieldvisibility">' . $content . '<a href="#" onclick="resetSelectboxes()">reset</a></div>' . $this->_javascript();
110 }
111
112 /**
113 * This methid is used to generate an infostructur array, which will be
114 * renderd as a Form
115 *
116 * @param tx_languagevisibility_element $changeableElement
117 * @param array $languageList
118 * @param string $itemFormElName
119 * @param boolean $isOverlay
120 * @return unknown
121 */
122 public function _getLanguageInfoStructurListForElementAndLanguageList($changeableElement, $languageList, $itemFormElName, $isOverlay) {
123
124 $visibility = t3lib_div::makeInstance('tx_languagevisibility_visibilityService');
125 $infosStruct = array();
126
127 foreach ( $languageList as $language ) {
128
129 $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) );
130
131 // if there is no access to language - and localsettings exist, then do not show select box
132 // this is to not be able as an translator to override languagesetting
133 $currentSetting = $changeableElement->getLocalVisibilitySetting($language->getUid());
134 $currentOptionsForUserAndLanguage = tx_languagevisibility_beservices::getAvailableOptionsForLanguage($language, $isOverlay, $changeableElement);
135 if ($currentSetting == '' || isset($currentOptionsForUserAndLanguage[$currentSetting])) {
136
137 if ($isOverlay) {
138 $defaultSelect = $changeableElement->getVisibilitySettingStoredInOverlayRecord($language->getUid());
139
140 $visibilityValue = $changeableElement->getVisibilitySettingStoredInDefaultRecord($language->getUid());
141 $visibilityString = $currentOptionsForUserAndLanguage[$visibilityValue];
142 } else {
143 $defaultSelect = $changeableElement->getVisibilitySettingStoredInDefaultRecord($language->getUid());
144
145 $visibilityValue = $changeableElement->getVisibilitySettingStoredInOverlayRecord($language->getUid());
146 $visibilityString = $currentOptionsForUserAndLanguage[$visibilityValue];
147 }
148
149 if ($this->isNewElement && $defaultSelect == '') {
150 if ($this->modTSconfig['language.'][$language->getUid() . '.']['defaultVisibilityOnCreate'] != '') {
151 $defaultSelect = $this->modTSconfig['language.'][$language->getUid() . '.']['defaultVisibilityOnCreate'];
152 }
153 }
154 $selectBox = $this->getSelectBox($language->getUid(), $currentOptionsForUserAndLanguage, $defaultSelect, $itemFormElName);
155 } else {
156 $selectBox = '<input type="hidden" name="' . $itemFormElName . '[' . $language->getUid() . ']" value="' . $currentSetting . '" ></input>(' . $currentSetting . ')';
157 }
158
159 if ($isOverlay) {
160 $infoitem['overlayVisibility'] = $selectBox;
161 $infoitem['originalVisibility'] = $visibilityString;
162 } else {
163 $infoitem['overlayVisibility'] = $visibilityString;
164 $infoitem['originalVisibility'] = $selectBox;
165 }
166
167 $infosStruct[] = $infoitem;
168 }
169
170 return $infosStruct;
171 }
172
173 /**
174 * Generates the selectbox for the languagevisibility settings of an item
175 *
176 * @param int $languageid
177 * @param array $select
178 * @param string $current current selected item
179 * @param string $name
180 * @return string
181 */
182 protected function getSelectBox($languageid, $select, $current, $name) {
183 if (count($select) == 1) {
184 $addClassName = ' oneitem';
185 }
186
187 $content .= '<select class="fieldvisibility_selects' . $addClassName . '" name="' . $name . '[' . $languageid . ']">';
188 foreach ( $select as $skey => $svalue ) {
189 if ($current == $skey) {
190 $selected = ' selected="selected"';
191 } else {
192 $selected = '';
193 }
194 $content .= '<option class="' . $this->getCSSClassFromVisibilityKey($skey) . '" value="' . $skey . '"' . $selected . '>' . $svalue . '</option>';
195 }
196 $content .= '</select>';
197 return $content;
198
199 }
200
201 /**
202 * This method is used to determine a css class for the diffrent visiblity modes
203 *
204 * @param string
205 * @return string
206 */
207 protected function getCSSClassFromVisibilityKey($key) {
208 switch ($key) {
209 case 'yes' :
210 case 'no' :
211 case 't' :
212 case 'f' :
213 $res = $key;
214 break;
215 case 'no+' :
216 $res = 'no_inherited';
217 break;
218 }
219
220 return $res;
221 }
222
223 protected function renderLanguageInfos($infosStruct) {
224 $content = '<style type="text/css">
225 .visibilitytable {margin: 10px 0 0 0}
226 .visibilitytable .bgColor4 {background-color: #C9B88B}
227 .visibilitytable .bgColor {background-color: #FFEED4}
228 .visibilitytable .lastcell {background-color: #DEEAB5}
229 .visibilitytable .bgColor .lastcell {background-color: #E8EAB5}
230 .visibilitytable .bgColor4 .lastcell {border-bottom: 1px solid #333333; background-color: #C9B88B}
231 .visibilitytable th {padding: 2px 5px 2px 2px; text-align: left; font-size: 12px;}
232 .visibilitytable select {width: 100px}
233 .visibilitytable select.oneitem {background-color: #999999}
234 .visibilitytable select option {background-color: #83FF73}
235 .visibilitytable select .yes {background-color: #E0FF81}
236 .visibilitytable select .no {background-color: #FFCE81}
237 .visibilitytable select .no_inherited {background-color: #FF8881}
238 .visibilitytable select .t {background-color: #BFFFB7}
239 .visibilitytable select .f {background-color: #BFFFB7}
240 .visibilitytable td {padding: 0 5px 2px 2px}
241 </' . 'style>';
242
243 $content .= '<table style="border-collapse: collapse;" class="visibilitytable">';
244 $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>';
245
246 foreach ( $infosStruct as $info ) {
247 $i ++;
248
249 // toggle row class
250 $class = ($i % 2) ? ' class="bgColor"' : '';
251 $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>';
252 }
253
254 $content .= '</table>';
255 return $content;
256 }
257
258 /**
259 * @param $key
260 * @return mixed
261 */
262 public function getLLL($key) {
263 return $GLOBALS['LANG']->sl('LLL:EXT:languagevisibility/locallang_db.xml:' . $key);
264 }
265
266 /**
267 * Generated a little status icon
268 *
269 * @param boolean positive or negative state
270 * @param string $title
271 * @return html tag to include the state image
272 */
273 protected function _getStatusImage($stat, $title = '') {
274 if ($stat) {
275 return '<img src="../typo3conf/ext/languagevisibility/res/ok.gif" title="' . htmlspecialchars($title) . '">';
276 } else {
277 return '<img src="../typo3conf/ext/languagevisibility/res/nok.gif" title="' . htmlspecialchars($title) . '">';
278 }
279 }
280
281 protected function _link_edit($table, $id) {
282 $params = '&table=' . $table . '&edit[' . $table . '][' . $id . ']=edit';
283 $url = $GLOBALS['BACK_PATH'] . 'alt_doc.php?id=' . $id . $params;
284 return '<a href="' . $url . '" target="blank">[edit]</a>';
285 }
286
287 /*******************************************
288 *
289 * Link functions (protected)
290 *
291 *******************************************/
292
293 /**
294 * Returns an HTML link for editing
295 *
296 * @param string $label: The label (or image)
297 * @param string $table: The table, fx. 'tt_content'
298 * @param integer $uid: The uid of the element to be edited
299 * @param boolean $forced: By default the link is not shown if translatorMode is set, but with this boolean it can be forced anyway.
300 * @return string HTML anchor tag containing the label and the correct link
301 * @access protected
302 */
303 public function link_edit($label, $table, $uid, $forced = FALSE) {
304 if ($label) {
305 if (($table == 'pages' && ($this->calcPerms & 2) || $table != 'pages' && ($this->calcPerms & 16))) {
306
307 $params = '&edit[' . $table . '][' . $uid . ']=edit';
308 $retUrl = 'returnUrl=' . ($requestUri == - 1 ? "'+T3_THIS_LOCATION+'" : rawurlencode($requestUri ? $requestUri : t3lib_div::getIndpEnv('REQUEST_URI')));
309 $url = "alt_doc.php?" . $retUrl . $params;
310 $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')";
311 return '<a style="text-decoration: none;" href="#" onclick="' . htmlspecialchars($onClick) . '">' . $label . '</a>';
312
313 } else {
314 return $label;
315 }
316 }
317 return '';
318 }
319
320 /**
321 * @return string
322 */
323 protected function _javascript() {
324
325 return '
326 <script type="text/javascript">
327
328 function resetSelectboxes() {
329 var obj=getElementsByClassName("fieldvisibility_selects");
330 for(i=0;i<obj.length;i++)
331 {
332 obj[i].selectedIndex=0;
333 }
334 }
335
336 function getElementsByClassName(class_name)
337 {
338 var all_obj,ret_obj=new Array(),j=0,teststr;
339
340 if(document.all)all_obj=document.all;
341 else if(document.getElementsByTagName && !document.all)
342 all_obj=document.getElementsByTagName("*");
343
344 for(i=0;i<all_obj.length;i++)
345 {
346 if(all_obj[i].className.indexOf(class_name)!=-1)
347 {
348 teststr=","+all_obj[i].className.split(" ").join(",")+",";
349 if(teststr.indexOf(","+class_name+",")!=-1)
350 {
351 ret_obj[j]=all_obj[i];
352 j++;
353 }
354 }
355 }
356 return ret_obj;
357 }
358
359 </script>';
360 }
361 }