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