Completed
Push — 7LTS_compatible ( b51d35...40d99a )
by Tomas Norre
32:08
created

Modfunc1   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 272
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 27
lcom 1
cbo 0
dl 0
loc 272
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A modMenu() 0 20 2
A main() 0 58 3
F renderL10nTable() 0 149 17
A _getStatusImage() 0 7 2
A getContentElementCount() 0 8 3
1
<?php
2
3
namespace AOE\Languagevisibility\BeModules;
4
/***************************************************************
5
 * Copyright notice
6
 *
7
 * (c) 2008  <>
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
 *
19
 * This script is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU General Public License for more details.
23
 *
24
 * This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
/**
28
 * Module extension (addition to function menu) 'Language Visibility Overview' for the 'testtt' extension.
29
 *
30
 * @author     <Daniel P�tzinger>
31
 * @package    TYPO3
32
 * @subpackage    tx_languagevisibility
33
 */
34
class Modfunc1 extends \TYPO3\CMS\Backend\Module\AbstractFunctionModule {
35
36
	/**
37
	 * Returns the menu array
38
	 *
39
	 * @return	array
40
	 */
41
	public function modMenu() {
42
		$menuArray = array(
43
			'depth' => array(
44
				0 => $GLOBALS['LANG']->getLL('depth_0'),
45
				1 => $GLOBALS['LANG']->getLL('depth_1'),
46
				2 => $GLOBALS['LANG']->getLL('depth_2'),
47
				3 => $GLOBALS['LANG']->getLL('depth_3')
48
			)
49
		);
50
51
			// Languages:
52
		$languageRep = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('tx_languagevisibility_languagerepository');
53
		$languageList = $languageRep->getLanguages();
54
		$menuArray['lang'] = array(0 => '[All]' );
55
		foreach ( $languageList as $language ) {
56
			$menuArray['lang'][$language->getUid()] = $language->getTitle();
57
		}
58
59
		return $menuArray;
60
	}
61
62
	/**
63
	 * MAIN function for page information of localization
64
	 *
65
	 * @return	string		Output HTML for the module.
66
	 */
67
	public function main() {
68
		$theOutput = '';
69
		if ($this->pObj->id) {
70
71
				// Depth selector:
72
			$h_func = \TYPO3\CMS\Backend\Utility\BackendUtility::getFuncMenu($this->pObj->id, 'SET[depth]', $this->pObj->MOD_SETTINGS['depth'], $this->pObj->MOD_MENU['depth'], 'index.php');
73
			$h_func .= \TYPO3\CMS\Backend\Utility\BackendUtility::getFuncMenu($this->pObj->id, 'SET[lang]', $this->pObj->MOD_SETTINGS['lang'], $this->pObj->MOD_MENU['lang'], 'index.php');
74
			$theOutput .= $h_func;
75
76
				// Add CSH:
77
			$theOutput .= \TYPO3\CMS\Backend\Utility\BackendUtility::cshItem('_MOD_web_info', 'lang', $GLOBALS['BACK_PATH'], '|<br/>');
78
79
				// Showing the tree:
80
				// Initialize starting point of page tree:
81
			$treeStartingPoint = intval($this->pObj->id);
82
			$treeStartingRecord = \TYPO3\CMS\Backend\Utility\BackendUtility::getRecordWSOL('pages', $treeStartingPoint);
83
			$depth = $this->pObj->MOD_SETTINGS['depth'];
84
85
				// Initialize tree object:
86
			$tree = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('\TYPO3\CMS\Backend\Tree\View\PageTreeView');
87
			$tree->init('AND ' . $GLOBALS['BE_USER']->getPagePermsClause(1));
88
			$tree->addField('l18n_cfg');
89
90
				// Creating top icon; the current page
91
			$HTML = \TYPO3\CMS\Backend\Utility\IconUtility::getIconImage('pages', $treeStartingRecord, $GLOBALS['BACK_PATH'], 'align="top"');
92
			$tree->tree[] = array('row' => $treeStartingRecord, 'HTML' => $HTML );
93
94
				// Create the tree from starting point:
95
			if ($depth) {
96
				$tree->getTree($treeStartingPoint, $depth, '');
97
			}
98
99
				// Add CSS needed:
100
			$css_content = '
101
				TABLE#langTable {
102
					margin-top: 10px;
103
				}
104
				TABLE#langTable TR TD {
105
					padding-left : 2px;
106
					padding-right : 2px;
107
					white-space: nowrap;
108
				}
109
				TD.c-notvisible { background-color: red; }
110
				TD.c-visible { background-color: #669966; }
111
				TD.c-translated { background-color: #A8E95C; }
112
				TD.c-nottranslated { background-color: #E9CD5C; }
113
				TD.c-leftLine {border-left: 2px solid black; }
114
				.bgColor5 { font-weight: bold; }
115
			';
116
			$marker = '/*###POSTCSSMARKER###*/';
117
			$this->pObj->content = str_replace($marker, $css_content . chr(10) . $marker, $this->pObj->content);
118
119
				// Render information table:
120
			$theOutput .= $this->renderL10nTable($tree);
121
		}
122
123
		return $theOutput;
124
	}
125
126
	/**
127
	 * Rendering the localization information table.
128
	 *
129
	 * @param	array		The Page tree data
130
	 * @return	string		HTML for the localization information table.
131
	 */
132
	public function renderL10nTable(&$tree) {
133
			// Title length:
134
		$titleLen = $GLOBALS['BE_USER']->uc['titleLen'];
135
136
			// Put together the tree:
137
		$output = '';
138
		$newOL_js = array();
139
		$langRecUids = array();
140
141
			// Init DAO
142
		$dao = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('tx_languagevisibility_daocommon');
143
		$elementfactory = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('tx_languagevisibility_elementFactory', $dao);
144
		$languageRep = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('tx_languagevisibility_languagerepository');
145
		$languageList = $languageRep->getLanguages();
146
		$visibility = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('tx_languagevisibility_visibilityService');
147
148
			//traverse tree:
149
		foreach ( $tree->tree as $data ) {
150
			$tCells = array();
151
152
			$element = $elementfactory->getElementForTable('pages', $data['row']['uid']);
153
154
				// first cell (tree):
155
				// Page icons / titles etc.
156
			$tCells[] = '<td' . ($data['row']['_CSSCLASS'] ? ' class="' . $data['row']['_CSSCLASS'] . '"' : '') . '>' . $data['HTML'] . htmlspecialchars(\TYPO3\CMS\Core\Utility\GeneralUtility::fixed_lgd_cs($data['row']['title'], $titleLen)) . (strcmp($data['row']['nav_title'], '') ? ' [Nav: <em>' . htmlspecialchars(\TYPO3\CMS\Core\Utility\GeneralUtility::fixed_lgd_cs($data['row']['nav_title'], $titleLen)) . '</em>]' : '') . '</td>';
157
				// language cells:
158
			foreach ( $languageList as $language ) {
159
				$info = '';
160
				$editUid = $data['row']['uid'];
161
				$params = '&edit[pages][' . $editUid . ']=edit';
162
				$langId = $language->getUid();
163
				if ($visibility->isVisible($language, $element)) {
164
					$isVisible = TRUE;
165
					$statusVis = 'c-visible';
166
				} else {
167
					$isVisible = FALSE;
168
					$statusVis = 'c-notvisible';
169
				}
170
				if ($element->hasTranslation($langId)) {
171
					$statusTrans = 'c-translated';
172
				} else {
173
					$statusTrans = 'c-nottranslated';
174
				}
175
176
				if ($language->getUid() == 0) {
177
						// Default
178
						// "View page" link is created:
179
					$viewPageLink = '<a href="#" onclick="' . htmlspecialchars(\TYPO3\CMS\Backend\Utility\BackendUtility::viewOnClick($data['row']['uid'], $GLOBALS['BACK_PATH'], '', '', '', '&L=###LANG_UID###')) . '">' . '<img' . \TYPO3\CMS\Backend\Utility\IconUtility::skinImg($GLOBALS['BACK_PATH'], 'gfx/zoom.gif', 'width="12" height="12"') . ' title="' . $GLOBALS['LANG']->getLL('lang_renderl10n_viewPage', '1') . '" border="0" alt="" />' . '</a>';
180
					$info .= '<a href="#" onclick="' . htmlspecialchars(\TYPO3\CMS\Backend\Utility\BackendUtility::editOnClick($params, $GLOBALS['BACK_PATH'])) . '">' . '<img' . \TYPO3\CMS\Backend\Utility\IconUtility::skinImg($GLOBALS['BACK_PATH'], 'gfx/edit2.gif', 'width="11" height="12"') . ' title="' . $GLOBALS['LANG']->getLL('lang_renderl10n_editDefaultLanguagePage', '1') . '" border="0" alt="" />' . '</a>';
181
					$info .= '<a href="#" onclick="' . htmlspecialchars('top.loadEditId(' . intval($data['row']['uid']) . ',"&SET[language]=0"); return FALSE;') . '">' . '<img' . \TYPO3\CMS\Backend\Utility\IconUtility::skinImg($GLOBALS['BACK_PATH'], 'gfx/edit_page.gif', 'width="12" height="12"') . ' title="' . $GLOBALS['LANG']->getLL('lang_renderl10n_editPage', '1') . '" border="0" alt="" />' . '</a>';
182
					$info .= str_replace('###LANG_UID###', '0', $viewPageLink);
183
					$info .= $data['row']['l18n_cfg'] & 1 ? '<span title="' . $GLOBALS['LANG']->sL('LLL:EXT:cms/locallang_tca.php:pages.l18n_cfg.I.1', '1') . '">D</span>' : '&nbsp;';
184
						// Put into cell:
185
					$tCells[] = '<td class="' . $statusTrans . ' c-leftLine">' . $info . '</td>';
186
					$tCells[] = '<td class="' . $statusTrans . '" title="' . $GLOBALS['LANG']->getLL('lang_renderl10n_CEcount', '1') . '" align="center">' . $this->getContentElementCount($data['row']['uid'], 0) . '</td>';
187
188
				} else {
189
						// Normal Language:
190
					if ($element->hasTranslation($langId)) {
191
						$viewPageLink = '<a href="#" onclick="' . htmlspecialchars(\TYPO3\CMS\Backend\Utility\BackendUtility::viewOnClick($data['row']['uid'], $GLOBALS['BACK_PATH'], '', '', '', '&L=###LANG_UID###')) . '">' . '<img' . \TYPO3\CMS\Backend\Utility\IconUtility::skinImg($GLOBALS['BACK_PATH'], 'gfx/zoom.gif', 'width="12" height="12"') . ' title="' . $GLOBALS['LANG']->getLL('lang_renderl10n_viewPage', '1') . '" border="0" alt="" />' . '</a>';
192
193
						$overLayRow = $element->getOverLayRecordForCertainLanguage($langId);
194
							// add uid of overlay to list of editable records:
195
						$langRecUids[$langId][] = $overLayRow['uid'];
196
						$icon = \TYPO3\CMS\Backend\Utility\IconUtility::getIconImage('pages_language_overlay', $overLayRow, $GLOBALS['BACK_PATH'], 'align="top" class="c-recIcon"');
197
198
						$info = $icon . htmlspecialchars(\TYPO3\CMS\Core\Utility\GeneralUtility::fixed_lgd_cs($overLayRow['title'], $titleLen)) . (strcmp($overLayRow['nav_title'], '') ? ' [Nav: <em>' . htmlspecialchars(\TYPO3\CMS\Core\Utility\GeneralUtility::fixed_lgd_cs($overLayRow['nav_title'], $titleLen)) . '</em>]' : '') . ($overLayRow['_COUNT'] > 1 ? '<div>' . $GLOBALS['LANG']->getLL('lang_renderl10n_badThingThereAre', '1') . '</div>' : '');
199
						$tCells[] = '<td class="' . $statusTrans . ' c-leftLine">' . $info . '</td>';
200
201
							// Edit whole record:
202
						$info = '';
203
						$editUid = $overLayRow['uid'];
204
						$params = '&edit[pages_language_overlay][' . $editUid . ']=edit';
205
						$info .= '<a href="#" onclick="' . htmlspecialchars(\TYPO3\CMS\Backend\Utility\BackendUtility::editOnClick($params, $GLOBALS['BACK_PATH'])) . '">' . '<img' . \TYPO3\CMS\Backend\Utility\IconUtility::skinImg($GLOBALS['BACK_PATH'], 'gfx/edit2.gif', 'width="11" height="12"') . ' title="' . $GLOBALS['LANG']->getLL('lang_renderl10n_editLanguageOverlayRecord', '1') . '" border="0" alt="" />' . '</a>';
206
207
						$info .= '<a href="#" onclick="' . htmlspecialchars('top.loadEditId(' . intval($data['row']['uid']) . ',"&SET[language]=' . $langId . '"); return FALSE;') . '">' . '<img' . \TYPO3\CMS\Backend\Utility\IconUtility::skinImg($GLOBALS['BACK_PATH'], 'gfx/edit_page.gif', 'width="12" height="12"') . ' title="' . $GLOBALS['LANG']->getLL('lang_renderl10n_editPageLang', '1') . '" border="0" alt="" />' . '</a>';
208
						$info .= str_replace('###LANG_UID###', $langId, $viewPageLink);
209
210
						$tCells[] = '<td class="' . $statusTrans . '">' . $info . '</td>';
211
						$tCells[] = '<td class="' . $statusTrans . '" title="' . $GLOBALS['LANG']->getLL('lang_renderl10n_CEcount', '1') . '" align="center">' . $this->getContentElementCount($data['row']['uid'], $langId) . '</td>';
212
					} else {
213
						$tCells[] = '<td class="' . $statusTrans . ' c-leftLine">&nbsp;</td>';
214
						$tCells[] = '<td class="' . $statusTrans . '">&nbsp;</td>';
215
						//add to JS
216
						$infoCell = '<input type="checkbox" name="newOL[' . $langId . '][' . $data['row']['uid'] . ']" value="1" />';
217
						$newOL_js[$langId] .= '
218
								+(document.webinfoForm[\'newOL[' . $langId . '][' . $data['row']['uid'] . ']\'].checked ? \'&edit[pages_language_overlay][' . $data['row']['uid'] . ']=new\' : \'\')
219
							';
220
						$tCells[] = '<td class="' . $statusTrans . '">' . $infoCell . '</td>';
221
222
					}
223
				}
224
				//last cell show status
225
				$tCells[] = '<td class="' . $statusVis . '">' . $this->_getStatusImage($isVisible) . '</td>';
226
			}
227
			$output .= '
228
			<tr class="bgColor5">
229
				' . implode('
230
				', $tCells) . '
231
			</tr>';
232
		}
233
234
			// first ROW:
235
		$firstRowCells = array();
236
		$firstRowCells[] = '<td>' . $GLOBALS['LANG']->getLL('lang_renderl10n_page', '1') . ':</td>';
237
		foreach ( $languageList as $language ) {
238
			$langId = $language->getUid();
239
			if ($this->pObj->MOD_SETTINGS['lang'] == 0 || ( int ) $this->pObj->MOD_SETTINGS['lang'] === ( int ) $langId) {
240
				$firstRowCells[] = '<td class="c-leftLine">' . $language->getTitle() . $language->getFlagImg() . '</td>';
241
				if ($langId == 0) {
242
					$firstRowCells[] = '<td></td>';
243
					$firstRowCells[] = '<td></td>';
244
				} else {
245
					// Title:
246
247
248
					// Edit language overlay records:
249
					if (is_array($langRecUids[$langId])) {
250
						$params = '&edit[pages_language_overlay][' . implode(',', $langRecUids[$langId]) . ']=edit&columnsOnly=title,nav_title,hidden';
251
						$firstRowCells[] = '<td><a href="#" onclick="' . htmlspecialchars(\TYPO3\CMS\Backend\Utility\BackendUtility::editOnClick($params, $GLOBALS['BACK_PATH'])) . '">
252
							<img' . \TYPO3\CMS\Backend\Utility\IconUtility::skinImg($GLOBALS['BACK_PATH'], 'gfx/edit2.gif', 'width="11" height="12"') . ' title="' . $GLOBALS['LANG']->getLL('lang_renderl10n_editLangOverlays', '1') . '" border="0" alt="" />
253
							</a></td>';
254
					} else {
255
						$firstRowCells[] = '<td>&nbsp;</td>';
256
					}
257
258
					// Create new overlay records:
259
					$params = "'" . $newOL_js[$langId] . "+'&columnsOnly=title,hidden,sys_language_uid&defVals[pages_language_overlay][sys_language_uid]=" . $langId;
260
					$firstRowCells[] = '<td><a href="#" onclick="' . htmlspecialchars(\TYPO3\CMS\Backend\Utility\BackendUtility::editOnClick($params, $GLOBALS['BACK_PATH'])) . '">
261
						<img' . \TYPO3\CMS\Backend\Utility\IconUtility::skinImg($GLOBALS['BACK_PATH'], 'gfx/new_el.gif', 'width="11" height="12"') . ' title="' . $GLOBALS['LANG']->getLL('lang_getlangsta_createNewTranslationHeaders', '1') . '" border="0" alt="" />
262
						</a></td>';
263
					$firstRowCells[] = '<td></td>';
264
				}
265
			}
266
		}
267
268
		$output = '
269
			<tr class="bgColor4">
270
				' . implode('
271
				', $firstRowCells) . '
272
			</tr>' . $output;
273
274
		$output = '
275
276
		<table border="0" cellspacing="0" cellpadding="0" id="langTable">' . $output . '
277
		</table>';
278
279
		return $output;
280
	}
281
282
	protected function _getStatusImage($stat) {
283
		if ($stat) {
284
			return '<img src="' . $GLOBALS['BACK_PATH'] . '../typo3conf/ext/languagevisibility/res/ok.gif">';
285
		} else {
286
			return '<img src="' . $GLOBALS['BACK_PATH'] . '../typo3conf/ext/languagevisibility/res/nok.gif">';
287
		}
288
	}
289
290
	/**
291
	 * Counting content elements for a single language on a page.
292
	 *
293
	 * @param	integer		Page id to select for.
294
	 * @param	integer		Sys language uid
295
	 * @return	integer		Number of content elements from the PID where the language is set to a certain value.
296
	 */
297
	public function getContentElementCount($pageId, $sysLang) {
298
		if ($sysLang == 0) {
299
			$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('count(*)', 'tt_content', 'pid=' . intval($pageId) . ' AND sys_language_uid=' . intval($sysLang) . \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause('tt_content') . \TYPO3\CMS\Backend\Utility\BackendUtility::versioningPlaceholderClause('tt_content'));
300
			list ( $count ) = $GLOBALS['TYPO3_DB']->sql_fetch_row($res);
301
		}
302
303
		return $count ? $count : '-';
0 ignored issues
show
Bug introduced by
The variable $count does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
304
	}
305
}
306
307
/**
308
 * Module extension (addition to function menu) 'Language Visibility Overview' for the 'testtt' extension.
309
 *
310
 * @author     <Daniel P?tzinger>
311
 * @package    TYPO3
312
 * @subpackage    tx_languagevisibility
313
 */
314
class sstx_languagevisibility_modfunc1 extends \TYPO3\CMS\Backend\Module\AbstractFunctionModule {
315
316
	/**
317
	 * Returns the module menu
318
	 *
319
	 * @return    Array with menuitems
320
	 */
321
	public function modMenu() {
322
		return Array("tx_languagevisibility_modfunc1_check" => "" );
323
	}
324
325
	/**
326
	 * Main method of the module
327
	 *
328
	 * @return    HTML
329
	 */
330
	public function main() {
331
		// Initializes the module. Done in this function because we may need to re-initialize if data is submitted!
332
		$theOutput = $this->pObj->doc->spacer(5);
333
		$theOutput .= $this->pObj->doc->section($GLOBALS['LANG']->getLL('title'), 'Dummy content here...', 0, 1);
334
335
		$menu = array();
336
		$menu[] = \TYPO3\CMS\Backend\Utility\BackendUtility::getFuncCheck($this->wizard->pObj->id, 'SET[tx_languagevisibility_modfunc1_check]', $this->wizard->pObj->MOD_SETTINGS['tx_languagevisibility_modfunc1_check']) . $GLOBALS['LANG']->getLL('checklabel');
337
		$theOutput .= $this->pObj->doc->spacer(5);
338
		$theOutput .= $this->pObj->doc->section('Menu', implode('' - '', $menu), 0, 1);
339
340
		return $theOutput;
341
	}
342
}
343
344
if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/languagevisibility/modfunc1/class.tx_languagevisibility_modfunc1.php']) {
345
	include_once ($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/languagevisibility/modfunc1/class.tx_languagevisibility_modfunc1.php']);
346
}
347