Completed
Push — 7LTS_compatible ( d8e85d...3c5c36 )
by Tomas Norre
28:52 queued 26:53
created

processDatamap_preProcessFieldArray()   D

Complexity

Conditions 9
Paths 8

Size

Total Lines 32
Code Lines 15

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 32
rs 4.909
cc 9
eloc 15
nc 8
nop 4
1
<?php
2
3
namespace AOE\Languagevisibility\Hooks;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2014 AOE GmbH <[email protected]>
9
 *
10
 *  All rights reserved
11
 *
12
 *  This script is part of the TYPO3 project. The TYPO3 project is
13
 *  free software; you can redistribute it and/or modify
14
 *  it under the terms of the GNU General Public License as published by
15
 *  the Free Software Foundation; either version 3 of the License, or
16
 *  (at your option) any later version.
17
 *
18
 *  The GNU General Public License can be found at
19
 *  http://www.gnu.org/copyleft/gpl.html.
20
 *
21
 *  This script is distributed in the hope that it will be useful,
22
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 *  GNU General Public License for more details.
25
 *
26
 *  This copyright notice MUST APPEAR in all copies of the script!
27
 ***************************************************************/
28
use AOE\Languagevisibility\CacheManager;
29
use AOE\Languagevisibility\Services\BeServices;
30
use AOE\Languagevisibility\VisibilityService;
31
32
/**
33
 * Class tx_languagevisibility_hooks_t3lib_tcemain
34
 */
35
class T3libTcemain {
36
37
	/**
38
	 * @param string $table
39
	 * @param integer $id
40
	 * @param array $data
41
	 * @param integer $res (but only 0 and 1 is relevant so it's boolean technically)
42
	 * @param $tcemain
43
	 * @internal param object $this
44
	 * @return integer
45
	 */
46
	public function checkRecordUpdateAccess($table, $id, $data, $res, $tcemain) {
0 ignored issues
show
Unused Code introduced by
The parameter $data 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...
Unused Code introduced by
The parameter $tcemain 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...
47
		/** @var tx_languagevisibility_beservices $visibilityservice */
48
		$visibilityservice = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('tx_languagevisibility_beservices');
49
		if ($table == 'pages' && ! $visibilityservice->hasUserAccessToPageRecord($id, 'edit')) {
50
			$result = 0;
51
		} elseif (! $visibilityservice->hasUserAccessToEditRecord($table, $id)) {
52
			$result = 0;
53
		} else {
54
			$result = $res;
55
		}
56
		return $result;
57
	}
58
59
	/**
60
	 * This function is called my TYPO each time an element is saved in the backend
61
	 *
62
	 * @param array $incomingFieldArray
63
	 * @param string $table
64
	 * @param unknown_type $id
65
	 * @param unknown_type $reference
66
	 */
67
	public function processDatamap_preProcessFieldArray(&$incomingFieldArray, $table, $id, &$reference) {
0 ignored issues
show
Unused Code introduced by
The parameter $id 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...
Unused Code introduced by
The parameter $reference 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...
68
		$data = $incomingFieldArray;
69
70
		if (! is_array($data)) {
71
			return; /** some strange DB situation */
72
		}
73
74
		if (in_array($table, VisibilityService::getSupportedTables())) {
75
			/**
76
			 * NOTE: This code does not affect new records because the field 'tx_languagevisibility_visibility' is not set
77
			 */
78
			if (isset($incomingFieldArray['tx_languagevisibility_visibility']) && is_array($incomingFieldArray['tx_languagevisibility_visibility'])) {
79
80
				if ($table == 'pages') {
81
82
					$incomingFieldArray['tx_languagevisibility_inheritanceflag_original'] = (in_array('no+', $incomingFieldArray['tx_languagevisibility_visibility'])) ? '1' : '0';
83
				} elseif ($table == 'pages_language_overlay') {
84
					$incomingFieldArray['tx_languagevisibility_inheritanceflag_overlayed'] = (in_array('no+', $incomingFieldArray['tx_languagevisibility_visibility'])) ? '1' : '0';
85
				}
86
87
				$incomingFieldArray['tx_languagevisibility_visibility'] = serialize($incomingFieldArray['tx_languagevisibility_visibility']);
88
89
					// flush all caches
90
				CacheManager::getInstance()->flushAllCaches();
91
92
				// Flush TYPO3 Caching Framework caches
93
				\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Cache\\CacheManager')
94
					->getCache('tx_languagevisibility')
95
					->flush();
96
			}
97
		}
98
	}
99
100
	/**
101
	 * This method is used to initialize new Elements with the default
102
	 *
103
	 * @param unknown_type $status
104
	 * @param unknown_type $table
105
	 * @param unknown_type $id
106
	 * @param unknown_type $fieldArray
107
	 * @param unknown_type $reference
108
	 */
109
	public function processDatamap_afterDatabaseOperations($status, $table, $id, $fieldArray, &$reference) {
110
111
		if (in_array($table, VisibilityService::getSupportedTables())) {
112
			/**
113
			 * Now we set the default visibility for elements which did not get a defaultvisibility array.
114
			 * This can happen, if a user creates a new element AND the user has no access for the languagevisibility_field
115
			 */
116
			if ($status == 'new') {
117
				$row = array();
118
				$row['uid'] = $reference->substNEWwithIDs[$id];
119
120
				if ($fieldArray['pid'] == '-1') {
121
					$row = \TYPO3\CMS\Backend\Utility\BackendUtility::getWorkspaceVersionOfRecord($fieldArray['t3ver_wsid'], $table, $row['uid'], $fields = '*');
122
				}
123
124
				$newdata = array('tx_languagevisibility_visibility' => serialize(BeServices::getDefaultVisibilityArray()));
125
				$where = "tx_languagevisibility_visibility = '' AND uid=" . $row['uid'];
126
127
				$GLOBALS['TYPO3_DB']->exec_UPDATEquery($table, $where, $newdata);
128
			}
129
130
			CacheManager::getInstance()->flushAllCaches();
131
132
			// Flush TYPO3 Caching Framework caches
133
			\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Cache\\CacheManager')
134
				->getCache('tx_languagevisibility')
135
				->flush();
136
		}
137
	}
138
}
139