Completed
Push — master ( 3ebbf6...991fd5 )
by
unknown
08:12
created

getLanguageService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/***************************************************************
3
 *  Copyright notice
4
 *
5
 *  (c) 2009 AOE GmbH ([email protected])
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
use \TYPO3\CMS\Core\Utility\GeneralUtility;
26
use \TYPO3\CMS\Lang\LanguageService;
27
use \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;
28
29
/**
30
 * @package aoe_dbsequenzer
31
 */
32
class Tx_AoeDbsequenzer_OverwriteProtectionService {
33
	/**
34
	 * @var string
35
	 */
36
	const OVERWRITE_PROTECTION_TILL = 'tx_aoe_dbsquenzer_protectoverwrite_till';
37
	/**
38
	 * @var string
39
	 */
40
	const OVERWRITE_PROTECTION_MODE = 'tx_aoe_dbsquenzer_protectoverwrite_mode';
41
	/**
42
	 * array of configured tables that should call the sequenzer
43
	 *
44
	 * @var array
45
	 */
46
	private $supportedTables;
47
	/**
48
	 * @var Tx_AoeDbsequenzer_Domain_Repository_OverwriteprotectionRepository
49
	 */
50
	private $overwriteprotectionRepository;
51
	/**
52
	 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
53
	 */
54
	private $objectManager;
55
56
	/**
57
	 * @param array $conf
58
	 */
59 6
	public function __construct($conf = NULL) {
60 6
		if (is_null ( $conf )) {
61
			$conf = unserialize ( $GLOBALS ['TYPO3_CONF_VARS'] ['EXT'] ['extConf'] ['aoe_dbsequenzer'] );
62
		}
63 6
		$explodedValues = explode ( ',', $conf ['tables'] );
64 6
		$this->supportedTables = array_map ( 'trim', $explodedValues );
65 6
		$this->objectManager = GeneralUtility::makeInstance ('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
66 6
	}
67
68
	/**
69
	 * Injects ObjectManager instance
70
	 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
71
	 */
72 6
	public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
73
	{
74 6
		$this->objectManager = $objectManager;
75 6
	}
76
77
	/**
78
	 * @return Tx_AoeDbsequenzer_Domain_Repository_OverwriteprotectionRepository
79
	 */
80 2
	public function getOverwriteprotectionRepository() {
81 2
		if (! isset ( $this->overwriteprotectionRepository )) {
82
			$this->overwriteprotectionRepository = $this->objectManager->get ( 'Tx_AoeDbsequenzer_Domain_Repository_OverwriteprotectionRepository' );
83
		}
84 2
		return $this->overwriteprotectionRepository;
85
	}
86
87
	/**
88
	 * Hook for deletes in Typo3 Backend. It also delete all overwrite protection
89
	 * @param string $command
90
	 * @param string $table
91
	 * @param integer $id
92
	 */
93 3
	public function processCmdmap_postProcess($command, $table, $id) {
94 3
		if (FALSE === $this->needsOverWriteProtection ( $table )) {
95 1
			return;
96
		}
97 2
		if ($command !== 'delete') {
98 1
			return;
99
		}
100 1
		$this->removeOverwriteprotection( $id, $table );
101 1
	}
102
	/**
103
	 * Hook for updates in Typo3 backend
104
	 * @param array $incomingFieldArray
105
	 * @param string $table
106
	 * @param integer $id
107
	 * @param \TYPO3\CMS\Core\DataHandling\DataHandler $tcemain
108
	 */
109 2
	public function processDatamap_preProcessFieldArray(&$incomingFieldArray, $table, $id, \TYPO3\CMS\Core\DataHandling\DataHandler &$tcemain) {
110 2
		if (FALSE === $this->needsOverWriteProtection ( $table )) {
111
			return;
112
		}
113
114
        // check, if overwrite-protection-fields are set:
115
        // If they are NOT set, it means, that any other extension maybe called the process_datamap!
116 2
        if(false === array_key_exists(self::OVERWRITE_PROTECTION_TILL, $incomingFieldArray) ||
117 1
           false === array_key_exists(self::OVERWRITE_PROTECTION_MODE, $incomingFieldArray)
118 2
        ) {
119 2
            return;
120
        }
121
122
		if (FALSE === $this->hasOverWriteProtection ( $incomingFieldArray )) {
123
			$this->removeOverwriteprotection( $id, $table );
124
		} else {
125
			$protection = strtotime ( $incomingFieldArray [self::OVERWRITE_PROTECTION_TILL] );
126
			$mode = $incomingFieldArray [self::OVERWRITE_PROTECTION_MODE];
127
128
			$result = $this->getOverwriteprotectionRepository ()->findByProtectedUidAndTableName ( $id, $table );
129
			if ($result->count() === 0) {
130
				/* @var $overwriteprotection Tx_AoeDbsequenzer_Domain_Model_Overwriteprotection */
131
				$overwriteprotection = $this->objectManager->get ( 'Tx_AoeDbsequenzer_Domain_Model_Overwriteprotection' );
132
				$overwriteprotection->setProtectedMode ( $mode );
133
				$overwriteprotection->setPid ( $tcemain->getPID ( $table, $id ) );
134
				$overwriteprotection->setProtectedTablename ( $table );
135
				$overwriteprotection->setProtectedUid ( $id );
136
				$overwriteprotection->setProtectedTime ( $protection );
137
				$this->getOverwriteprotectionRepository ()->add ( $overwriteprotection );
138
			} else {
139
				foreach ( $result as $overwriteprotection ) {
140
					/* @var $overwriteprotection Tx_AoeDbsequenzer_Domain_Model_Overwriteprotection */
141
					$overwriteprotection->setProtectedMode ( $mode );
142
					$overwriteprotection->setProtectedTime ( $protection );
143
					$this->getOverwriteprotectionRepository ()->update($overwriteprotection);
144
				}
145
			}
146
			$this->persistAll();
147
		}
148
		unset ( $incomingFieldArray [self::OVERWRITE_PROTECTION_TILL] );
149
		unset ( $incomingFieldArray [self::OVERWRITE_PROTECTION_MODE] );
150
	}
151
152
	/**
153
	 * Render Form Field in typo3 backend
154
	 * @param array $PA
155
	 */
156 1
	public function renderInput(array $PA) {
157 1
		$content = file_get_contents ( dirname ( __FILE__ ) . '/../Resources/Private/Templates/formField.php' );
158 1
		$content = str_replace ( '###UID###', $PA ['row'] ['uid'], $content );
159 1
		$content = str_replace ( '###TABLE###', $PA ['table'], $content );
160 1
		$content = str_replace ( '###ID###', uniqid (), $content );
161 1
		$result = $this->getOverwriteprotectionRepository ()->findByProtectedUidAndTableName ( $PA ['row'] ['uid'], $PA ['table'] );
162 1
		$value = '';
163 1
		$overwriteMode = '';
164 1
		$conflictMode = '';
165
166 1
		foreach ( $result as $overwriteprotection ) {
167
			/* @var $overwriteprotection Tx_AoeDbsequenzer_Domain_Model_Overwriteprotection */
168
			$value = $overwriteprotection->getProtectedTime ();
169
			$value = date ( 'H:i d-m-Y', $value );
170
			if ($overwriteprotection->getProtectedMode () === 0) {
171
				$conflictMode = 'selected="selected"';
172
			} else {
173
				$overwriteMode = 'selected="selected"';
174
			}
175 1
		}
176 1
		$content = str_replace ( '###VALUE###', $value, $content );
177 1
		$content = str_replace ( '###OVERWIRTE_MODE###', $overwriteMode, $content );
178 1
		$content = str_replace ( '###CONFLICT_MODE###', $conflictMode, $content );
179 1
		$content = str_replace ( '###LABEL_MODE###', $this->getLanguageService()->sL('LLL:EXT:aoe_dbsequenzer/Resources/Private/Language/locallang_db.xml:protected_mode'), $content );
180 1
		$content = str_replace ( '###LABEL_MODE_CONFLICT###', $this->getLanguageService()->sL('LLL:EXT:aoe_dbsequenzer/Resources/Private/Language/locallang_db.xml:mode_conflict'), $content );
181 1
		$content = str_replace ( '###LABEL_MODE_OVERWIRTE###', $this->getLanguageService()->sL('LLL:EXT:aoe_dbsequenzer/Resources/Private/Language/locallang_db.xml:mode_overwrite'), $content );
182 1
		return $content;
183
	}
184
185
	/**
186
	 * @param Tx_AoeDbsequenzer_Domain_Repository_OverwriteprotectionRepository $overwriteprotectionRepository
187
	 */
188 4
	public function setOverwriteprotectionRepository(Tx_AoeDbsequenzer_Domain_Repository_OverwriteprotectionRepository $overwriteprotectionRepository) {
189 4
		$this->overwriteprotectionRepository = $overwriteprotectionRepository;
190 4
	}
191
192
	/**
193
	 * @return LanguageService
194
	 */
195 1
	private function getLanguageService() {
196 1
		return $GLOBALS['LANG'];
197
	}
198
199
	/**
200
	 * @param array $fields_values
201
	 * @return boolean
202
	 */
203
	private function hasOverWriteProtection(array $fields_values) {
204
		if (isset ( $fields_values [self::OVERWRITE_PROTECTION_TILL] )) {
205
			$value = trim ( $fields_values [self::OVERWRITE_PROTECTION_TILL] );
206
			if (FALSE === empty ( $value ) && FALSE !== strtotime ( $value )) {
207
				return true;
208
			}
209
		}
210
		return false;
211
	}
212
	/**
213
	 * If a table is configured to use the sequenzer
214
	 *
215
	 * @param string $tableName
216
	 * @return boolean
217
	 */
218 5
	private function needsOverWriteProtection($tableName) {
219 5
		if ($tableName !== 'tx_aoedbsequenzer_domain_model_overwriteprotection' && in_array ( $tableName, $this->supportedTables )) {
220 4
			return true;
221
		}
222 1
		return false;
223
	}
224
	/**
225
	 * persist all changes
226
	 */
227 1
	private function persistAll() {
228
		/* @var $persistenceManager PersistenceManager */
229 1
		$persistenceManager = $this->objectManager->get ( 'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\PersistenceManager' );
230 1
		$persistenceManager->persistAll ();
231 1
	}
232
	/**
233
	 * remove overwriteprotection
234
	 *
235
	 * @param integer $id
236
	 * @param string $table
237
	 */
238 1
	private function removeOverwriteprotection($id, $table) {
239 1
		$result = $this->getOverwriteprotectionRepository ()->findByProtectedUidAndTableName ( $id, $table );
240 1
		foreach ( $result as $overwriteprotection ) {
241
			$this->getOverwriteprotectionRepository ()->remove ( $overwriteprotection );
242 1
		}
243 1
		$this->persistAll();
244
	}
245
}