[TASK] Re-work/simplify copyright header in PHP files - Part 9
[Packages/TYPO3.CMS.git] / typo3 / sysext / workspaces / Classes / Hook / DataHandlerHook.php
1 <?php
2 namespace TYPO3\CMS\Workspaces\Hook;
3
4 /**
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Backend\Utility\BackendUtility;
18
19 /**
20 * Tcemain service
21 *
22 * @author Workspaces Team (http://forge.typo3.org/projects/show/typo3v4-workspaces)
23 */
24 class DataHandlerHook {
25
26 /**
27 * In case a sys_workspace_stage record is deleted we do a hard reset
28 * for all existing records in that stage to avoid that any of these end up
29 * as orphan records.
30 *
31 * @param string $command
32 * @param string $table
33 * @param string $id
34 * @param string $value
35 * @param \TYPO3\CMS\Core\DataHandling\DataHandler $tcemain
36 * @return void
37 */
38 public function processCmdmap_postProcess($command, $table, $id, $value, \TYPO3\CMS\Core\DataHandling\DataHandler $tcemain) {
39 if ($command === 'delete') {
40 if ($table === \TYPO3\CMS\Workspaces\Service\StagesService::TABLE_STAGE) {
41 $this->resetStageOfElements($id);
42 } elseif ($table === \TYPO3\CMS\Workspaces\Service\WorkspaceService::TABLE_WORKSPACE) {
43 $this->flushWorkspaceElements($id);
44 }
45 }
46 }
47
48 /**
49 * hook that is called AFTER all commands of the commandmap was
50 * executed
51 *
52 * @param \TYPO3\CMS\Core\DataHandling\DataHandler $tcemainObj reference to the main tcemain object
53 * @return void
54 */
55 public function processCmdmap_afterFinish(\TYPO3\CMS\Core\DataHandling\DataHandler $tcemainObj) {
56 $this->flushWorkspaceCacheEntriesByWorkspaceId($tcemainObj->BE_USER->workspace);
57 }
58
59 /**
60 * In case a sys_workspace_stage record is deleted we do a hard reset
61 * for all existing records in that stage to avoid that any of these end up
62 * as orphan records.
63 *
64 * @param integer $stageId Elements with this stage are resetted
65 * @return void
66 */
67 protected function resetStageOfElements($stageId) {
68 $fields = array('t3ver_stage' => \TYPO3\CMS\Workspaces\Service\StagesService::STAGE_EDIT_ID);
69 foreach ($this->getTcaTables() as $tcaTable) {
70 if (BackendUtility::isTableWorkspaceEnabled($tcaTable)) {
71 $where = 't3ver_stage = ' . (int)$stageId;
72 $where .= ' AND t3ver_wsid > 0 AND pid=-1';
73 $where .= BackendUtility::deleteClause($tcaTable);
74 $GLOBALS['TYPO3_DB']->exec_UPDATEquery($tcaTable, $where, $fields);
75 }
76 }
77 }
78
79 /**
80 * Flushes elements of a particular workspace to avoid orphan records.
81 *
82 * @param integer $workspaceId The workspace to be flushed
83 * @return void
84 */
85 protected function flushWorkspaceElements($workspaceId) {
86 $command = array();
87 foreach ($this->getTcaTables() as $tcaTable) {
88 if (BackendUtility::isTableWorkspaceEnabled($tcaTable)) {
89 $where = '1=1';
90 $where .= BackendUtility::getWorkspaceWhereClause($tcaTable, $workspaceId);
91 $where .= BackendUtility::deleteClause($tcaTable);
92 $records = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('uid', $tcaTable, $where, '', '', '', 'uid');
93 if (is_array($records)) {
94 foreach (array_keys($records) as $recordId) {
95 $command[$tcaTable][$recordId]['version']['action'] = 'flush';
96 }
97 }
98 }
99 }
100 if (count($command)) {
101 $tceMain = $this->getTceMain();
102 $tceMain->start(array(), $command);
103 $tceMain->process_cmdmap();
104 }
105 }
106
107 /**
108 * Gets all defined TCA tables.
109 *
110 * @return array
111 */
112 protected function getTcaTables() {
113 return array_keys($GLOBALS['TCA']);
114 }
115
116 /**
117 * @return \TYPO3\CMS\Core\DataHandling\DataHandler
118 */
119 protected function getTceMain() {
120 $tceMain = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\DataHandling\\DataHandler');
121 $tceMain->stripslashes_values = 0;
122 return $tceMain;
123 }
124
125 /**
126 * Flushes the workspace cache for current workspace and for the virtual "all workspaces" too.
127 *
128 * @param integer $workspaceId The workspace to be flushed in cache
129 * @return void
130 */
131 protected function flushWorkspaceCacheEntriesByWorkspaceId($workspaceId) {
132 $workspacesCache = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Cache\\CacheManager')->getCache('workspaces_cache');
133 $workspacesCache->flushByTag($workspaceId);
134 $workspacesCache->flushByTag(\TYPO3\CMS\Workspaces\Service\WorkspaceService::SELECT_ALL_WORKSPACES);
135 }
136
137 }