[TASK][workspaces] Remove @package and @subpackage annotations
[Packages/TYPO3.CMS.git] / typo3 / sysext / workspaces / Classes / Hook / DataHandlerHook.php
1 <?php
2 namespace TYPO3\CMS\Workspaces\Hook;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2011 Workspaces Team (http://forge.typo3.org/projects/show/typo3v4-workspaces)
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 /**
30 * Tcemain service
31 *
32 * @author Workspaces Team (http://forge.typo3.org/projects/show/typo3v4-workspaces)
33 */
34 class DataHandlerHook {
35
36 /**
37 * In case a sys_workspace_stage record is deleted we do a hard reset
38 * for all existing records in that stage to avoid that any of these end up
39 * as orphan records.
40 *
41 * @param string $command
42 * @param string $table
43 * @param string $id
44 * @param string $value
45 * @param \TYPO3\CMS\Core\DataHandling\DataHandler $tcemain
46 * @return void
47 */
48 public function processCmdmap_postProcess($command, $table, $id, $value, \TYPO3\CMS\Core\DataHandling\DataHandler $tcemain) {
49 if ($command === 'delete') {
50 if ($table === \TYPO3\CMS\Workspaces\Service\StagesService::TABLE_STAGE) {
51 $this->resetStageOfElements($id);
52 } elseif ($table === \TYPO3\CMS\Workspaces\Service\WorkspaceService::TABLE_WORKSPACE) {
53 $this->flushWorkspaceElements($id);
54 }
55 }
56 }
57
58 /**
59 * hook that is called AFTER all commands of the commandmap was
60 * executed
61 *
62 * @param \TYPO3\CMS\Core\DataHandling\DataHandler $tcemainObj reference to the main tcemain object
63 * @return void
64 */
65 public function processCmdmap_afterFinish(\TYPO3\CMS\Core\DataHandling\DataHandler $tcemainObj) {
66 $this->flushWorkspaceCacheEntriesByWorkspaceId($tcemainObj->BE_USER->workspace);
67 }
68
69 /**
70 * In case a sys_workspace_stage record is deleted we do a hard reset
71 * for all existing records in that stage to avoid that any of these end up
72 * as orphan records.
73 *
74 * @param integer $stageId Elements with this stage are resetted
75 * @return void
76 */
77 protected function resetStageOfElements($stageId) {
78 $fields = array('t3ver_stage' => \TYPO3\CMS\Workspaces\Service\StagesService::STAGE_EDIT_ID);
79 foreach ($this->getTcaTables() as $tcaTable) {
80 if (\TYPO3\CMS\Backend\Utility\BackendUtility::isTableWorkspaceEnabled($tcaTable)) {
81 $where = 't3ver_stage = ' . intval($stageId);
82 $where .= ' AND t3ver_wsid > 0 AND pid=-1';
83 $where .= \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause($tcaTable);
84 $GLOBALS['TYPO3_DB']->exec_UPDATEquery($tcaTable, $where, $fields);
85 }
86 }
87 }
88
89 /**
90 * Flushes elements of a particular workspace to avoid orphan records.
91 *
92 * @param integer $workspaceId The workspace to be flushed
93 * @return void
94 */
95 protected function flushWorkspaceElements($workspaceId) {
96 $command = array();
97 foreach ($this->getTcaTables() as $tcaTable) {
98 if (\TYPO3\CMS\Backend\Utility\BackendUtility::isTableWorkspaceEnabled($tcaTable)) {
99 $where = '1=1';
100 $where .= \TYPO3\CMS\Backend\Utility\BackendUtility::getWorkspaceWhereClause($tcaTable, $workspaceId);
101 $where .= \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause($tcaTable);
102 $records = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('uid', $tcaTable, $where, '', '', '', 'uid');
103 if (is_array($records)) {
104 foreach (array_keys($records) as $recordId) {
105 $command[$tcaTable][$recordId]['version']['action'] = 'flush';
106 }
107 }
108 }
109 }
110 if (count($command)) {
111 $tceMain = $this->getTceMain();
112 $tceMain->start(array(), $command);
113 $tceMain->process_cmdmap();
114 }
115 }
116
117 /**
118 * Gets all defined TCA tables.
119 *
120 * @return array
121 */
122 protected function getTcaTables() {
123 return array_keys($GLOBALS['TCA']);
124 }
125
126 /**
127 * Gets a new instance of t3lib_TCEmain.
128 *
129 * @return \TYPO3\CMS\Core\DataHandling\DataHandler
130 */
131 protected function getTceMain() {
132 $tceMain = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\DataHandling\\DataHandler');
133 $tceMain->stripslashes_values = 0;
134 return $tceMain;
135 }
136
137 /**
138 * Flushes the workspace cache for current workspace and for the virtual "all workspaces" too.
139 *
140 * @param integer $workspaceId The workspace to be flushed in cache
141 * @return void
142 */
143 protected function flushWorkspaceCacheEntriesByWorkspaceId($workspaceId) {
144 $workspacesCache = $GLOBALS['typo3CacheManager']->getCache('workspaces_cache');
145 $workspacesCache->flushByTag($workspaceId);
146 $workspacesCache->flushByTag(\TYPO3\CMS\Workspaces\Service\WorkspaceService::SELECT_ALL_WORKSPACES);
147 }
148
149 }
150
151
152 ?>