1 | <?php |
||
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) { |
|
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) { |
||
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) { |
|
224 | /** |
||
225 | * persist all changes |
||
226 | */ |
||
227 | 1 | private function persistAll() { |
|
232 | /** |
||
233 | * remove overwriteprotection |
||
234 | * |
||
235 | * @param integer $id |
||
236 | * @param string $table |
||
237 | */ |
||
238 | 1 | private function removeOverwriteprotection($id, $table) { |
|
245 | } |