Completed
Push — master ( 6ac621...f01326 )
by
unknown
26:53
created

injectObjectManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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