TYPO3 API  SVNRelease
MassActionHandler.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003  *  Copyright notice
00004  *
00005  *  (c) 1999-2011 Kasper Skårhøj (kasperYYYY@typo3.com)
00006  *  (c) 2010-2011 Workspaces Team (http://forge.typo3.org/projects/show/typo3v4-workspaces)
00007  *  All rights reserved
00008  *
00009  *  This script is part of the TYPO3 project. The TYPO3 project is
00010  *  free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  The GNU General Public License can be found at
00016  *  http://www.gnu.org/copyleft/gpl.html.
00017  *  A copy is found in the textfile GPL.txt and important notices to the license
00018  *  from the author is found in LICENSE.txt distributed with these scripts.
00019  *
00020  *
00021  *  This script is distributed in the hope that it will be useful,
00022  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00023  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00024  *  GNU General Public License for more details.
00025  *
00026  *  This copyright notice MUST APPEAR in all copies of the script!
00027  ***************************************************************/
00028 
00029 /**
00030  * Class encapsulates all actions which are triggered for all elements within the current workspace.
00031  *
00032  * @author Kasper Skårhøj (kasperYYYY@typo3.com)
00033  * @author Workspaces Team (http://forge.typo3.org/projects/show/typo3v4-workspaces)
00034  * @package Workspaces
00035  * @subpackage ExtDirect
00036  */
00037 class tx_Workspaces_ExtDirect_MassActionHandler extends tx_Workspaces_ExtDirect_AbstractHandler {
00038     const MAX_RECORDS_TO_PROCESS = 30;
00039 
00040     /**
00041      * Path to the locallang file
00042      * @var string
00043      */
00044     private $pathToLocallang = 'LLL:EXT:workspaces/Resources/Private/Language/locallang.xml';
00045 
00046     /**
00047      * Get list of available mass workspace actions.
00048      *
00049      * @param object $parameter
00050      * @return array $data
00051      */
00052     public function getMassStageActions($parameter) {
00053         $actions = array();
00054         $currentWorkspace = $this->getCurrentWorkspace();
00055 
00056             // in case we're working within "All Workspaces" we can't provide Mass Actions
00057         if ($currentWorkspace != tx_Workspaces_Service_Workspaces::SELECT_ALL_WORKSPACES) {
00058             $publishAccess = $GLOBALS['BE_USER']->workspacePublishAccess($currentWorkspace);
00059             if ($publishAccess && !($GLOBALS['BE_USER']->workspaceRec['publish_access'] & 1)) {
00060                 $actions[] = array('action' => 'publish', 'title' => $GLOBALS['LANG']->sL($this->pathToLocallang . ':label_doaction_publish')
00061                 );
00062                 if ($GLOBALS['BE_USER']->workspaceSwapAccess()) {
00063                     $actions[] = array('action' => 'swap', 'title' => $GLOBALS['LANG']->sL($this->pathToLocallang . ':label_doaction_swap')
00064                     );
00065                 }
00066             }
00067 
00068             if ($currentWorkspace !== tx_Workspaces_Service_Workspaces::LIVE_WORKSPACE_ID) {
00069                 $actions[] = array('action' => 'discard', 'title' => $GLOBALS['LANG']->sL($this->pathToLocallang . ':label_doaction_discard')
00070                 );
00071             }
00072         }
00073 
00074         $result = array(
00075             'total' => count($actions),
00076             'data' => $actions
00077         );
00078         return $result;
00079     }
00080 
00081     /**
00082      * Publishes the current workspace.
00083      *
00084      * @param stdclass $parameters
00085      * @return array
00086      */
00087     public function publishWorkspace(stdclass $parameters) {
00088         $result = array(
00089             'init' => false,
00090             'total' => 0,
00091             'processed' => 0,
00092             'error' => false
00093         );
00094 
00095         try {
00096             if ($parameters->init) {
00097                 $cnt = $this->initPublishData($this->getCurrentWorkspace(), $parameters->swap);
00098                 $result['total'] = $cnt;
00099             } else {
00100                 $result['processed'] = $this->processData($this->getCurrentWorkspace());
00101                 $result['total'] = $GLOBALS['BE_USER']->getSessionData('workspaceMassAction_total');
00102             }
00103         } catch (Exception $e) {
00104             $result['error'] = $e->getMessage();
00105         }
00106         return $result;
00107     }
00108 
00109     /**
00110      * Flushes the current workspace.
00111      *
00112      * @param stdclass $parameters
00113      * @return array
00114      */
00115     public function flushWorkspace(stdclass $parameters) {
00116         $result = array(
00117             'init' => false,
00118             'total' => 0,
00119             'processed' => 0,
00120             'error' => false
00121         );
00122 
00123         try {
00124             if ($parameters->init) {
00125                 $cnt = $this->initFlushData($this->getCurrentWorkspace());
00126                 $result['total'] = $cnt;
00127             } else {
00128                 $result['processed'] = $this->processData($this->getCurrentWorkspace());
00129                 $result['total'] = $GLOBALS['BE_USER']->getSessionData('workspaceMassAction_total');
00130             }
00131         } catch (Exception $e) {
00132             $result['error'] = $e->getMessage();
00133         }
00134         return $result;
00135     }
00136 
00137     /**
00138      * Initializes the command map to be used for publishing.
00139      *
00140      * @param integer $workspace
00141      * @param boolean $swap
00142      * @return integer
00143      */
00144     protected function initPublishData($workspace, $swap) {
00145         $workspaceService = t3lib_div::makeInstance('tx_Workspaces_Service_Workspaces');
00146             // workspace might be -98 a.k.a "All Workspaces but that's save here
00147         $publishData = $workspaceService->getCmdArrayForPublishWS($workspace, $swap);
00148         $recordCount = 0;
00149         foreach ($publishData as $table => $recs) {
00150             $recordCount += count($recs);
00151         }
00152         if ($recordCount > 0) {
00153             $GLOBALS['BE_USER']->setAndSaveSessionData('workspaceMassAction', $publishData);
00154             $GLOBALS['BE_USER']->setAndSaveSessionData('workspaceMassAction_total', $recordCount);
00155             $GLOBALS['BE_USER']->setAndSaveSessionData('workspaceMassAction_processed', 0);
00156         }
00157         return $recordCount;
00158     }
00159 
00160     /**
00161      * Initializes the command map to be used for flushing.
00162      *
00163      * @param integer $workspace
00164      * @return integer
00165      */
00166     protected function initFlushData($workspace) {
00167         $workspaceService = t3lib_div::makeInstance('tx_Workspaces_Service_Workspaces');
00168             // workspace might be -98 a.k.a "All Workspaces but that's save here
00169         $flushData = $workspaceService->getCmdArrayForFlushWS($workspace);
00170         $recordCount = 0;
00171         foreach ($flushData as $table => $recs) {
00172             $recordCount += count($recs);
00173         }
00174         if ($recordCount > 0) {
00175             $GLOBALS['BE_USER']->setAndSaveSessionData('workspaceMassAction', $flushData);
00176             $GLOBALS['BE_USER']->setAndSaveSessionData('workspaceMassAction_total', $recordCount);
00177             $GLOBALS['BE_USER']->setAndSaveSessionData('workspaceMassAction_processed', 0);
00178         }
00179         return $recordCount;
00180     }
00181 
00182     /**
00183      * Processes the data.
00184      *
00185      * @param integer $workspace
00186      * @return integer
00187      */
00188     protected function processData($workspace) {
00189         $processData = $GLOBALS['BE_USER']->getSessionData('workspaceMassAction');
00190         $recordsProcessed = $GLOBALS['BE_USER']->getSessionData('workspaceMassAction_processed');
00191         $limitedCmd = array();
00192         $numRecs = 0;
00193 
00194         foreach ($processData as $table => $recs) {
00195             foreach ($recs as $key => $value) {
00196                 $numRecs++;
00197                 $limitedCmd[$table][$key] = $value;
00198                 if ($numRecs == self::MAX_RECORDS_TO_PROCESS) {
00199                     break;
00200                 }
00201             }
00202             if ($numRecs == self::MAX_RECORDS_TO_PROCESS) {
00203                 break;
00204             }
00205         }
00206 
00207         if ($numRecs == 0) {
00208                 // All done
00209             $GLOBALS['BE_USER']->setAndSaveSessionData('workspaceMassAction', null);
00210             $GLOBALS['BE_USER']->setAndSaveSessionData('workspaceMassAction_total', 0);
00211         } else {
00212                 // Execute the commands:
00213             $tce = t3lib_div::makeInstance('t3lib_TCEmain');
00214             $tce->stripslashes_values = 0;
00215             $tce->start(array(), $limitedCmd);
00216             $tce->process_cmdmap();
00217 
00218             $errors = $tce->errorLog;
00219             if (count($errors) > 0) {
00220                 throw new Exception(implode(', ', $errors));
00221             } else {
00222                     // Unset processed records
00223                 foreach ($limitedCmd as $table => $recs) {
00224                     foreach ($recs as $key => $value) {
00225                         $recordsProcessed++;
00226                         unset($processData[$table][$key]);
00227                     }
00228                 }
00229                 $GLOBALS['BE_USER']->setAndSaveSessionData('workspaceMassAction', $processData);
00230                 $GLOBALS['BE_USER']->setAndSaveSessionData('workspaceMassAction_processed', $recordsProcessed);
00231             }
00232         }
00233 
00234         return $recordsProcessed;
00235     }
00236 }
00237 
00238 
00239 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/workspaces/Classes/ExtDirect/MassActionHandler.php'])) {
00240     include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/workspaces/Classes/ExtDirect/MassActionHandler.php']);
00241 }
00242 ?>