TYPO3 API  SVNRelease
ActionHandler.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 2010-2011 Workspaces Team (http://forge.typo3.org/projects/show/typo3v4-workspaces)
00006 *  All rights reserved
00007 *
00008 *  This script is part of the TYPO3 project. The TYPO3 project is
00009 *  free software; you can redistribute it and/or modify
00010 *  it under the terms of the GNU General Public License as published by
00011 *  the Free Software Foundation; either version 2 of the License, or
00012 *  (at your option) any later version.
00013 *
00014 *  The GNU General Public License can be found at
00015 *  http://www.gnu.org/copyleft/gpl.html.
00016 *  A copy is found in the textfile GPL.txt and important notices to the license
00017 *  from the author is found in LICENSE.txt distributed with these scripts.
00018 *
00019 *
00020 *  This script is distributed in the hope that it will be useful,
00021 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 *  GNU General Public License for more details.
00024 *
00025 *  This copyright notice MUST APPEAR in all copies of the script!
00026 ***************************************************************/
00027 
00028 /**
00029  * @author Workspaces Team (http://forge.typo3.org/projects/show/typo3v4-workspaces)
00030  * @package Workspaces
00031  * @subpackage ExtDirect
00032  */
00033 class tx_Workspaces_ExtDirect_ActionHandler extends tx_Workspaces_ExtDirect_AbstractHandler {
00034     /**
00035      * @var Tx_Workspaces_Service_Stages
00036      */
00037     protected $stageService;
00038 
00039     /**
00040      * Creates this object.
00041      */
00042     public function __construct() {
00043         $this->stageService = t3lib_div::makeInstance('Tx_Workspaces_Service_Stages');
00044     }
00045 
00046     /**
00047      * Generates a workspace preview link.
00048      *
00049      * @param integer $uid The ID of the record to be linked
00050      * @return string the full domain including the protocol http:// or https://, but without the trailing '/'
00051      */
00052     public function generateWorkspacePreviewLink($uid) {
00053         $ttlHours = intval($GLOBALS['BE_USER']->getTSConfigVal('options.workspaces.previewLinkTTLHours'));
00054         $ttlHours = ($ttlHours ? $ttlHours : 24*2) * 3600;
00055         $linkParams = array(
00056             'ADMCMD_prev'   => t3lib_BEfunc::compilePreviewKeyword('', $GLOBALS['BE_USER']->user['uid'], $ttlHours, $this->getCurrentWorkspace()),
00057             'id'            => $uid
00058         );
00059         return t3lib_BEfunc::getViewDomain($uid) . '/index.php?' . t3lib_div::implodeArrayForUrl('', $linkParams);
00060     }
00061 
00062     /**
00063      * Swaps a sisngle record.
00064      *
00065      * @param string $table
00066      * @param integer $t3ver_oid
00067      * @param integer $orig_uid
00068      * @return void
00069      *
00070      * @todo What about reporting errors back to the ExtJS interface? /olly/
00071      */
00072     public function swapSingleRecord($table, $t3ver_oid, $orig_uid) {
00073         $cmd[$table][$t3ver_oid]['version'] = array(
00074             'action' => 'swap',
00075             'swapWith' => $orig_uid,
00076             'swapIntoWS' => 1
00077         );
00078 
00079         $tce = t3lib_div::makeInstance ('t3lib_TCEmain');
00080         $tce->start(array(), $cmd);
00081         $tce->process_cmdmap();
00082     }
00083 
00084     /**
00085      * Deletes a single record.
00086      *
00087      * @param string $table
00088      * @param integer $uid
00089      * @return void
00090      *
00091      * @todo What about reporting errors back to the ExtJS interface? /olly/
00092      */
00093     public function deleteSingleRecord($table, $uid) {
00094         $cmd[$table][$uid]['version'] = array(
00095             'action' => 'clearWSID'
00096         );
00097 
00098         $tce = t3lib_div::makeInstance ('t3lib_TCEmain');
00099         $tce->start(array(), $cmd);
00100         $tce->process_cmdmap();
00101     }
00102 
00103     /**
00104      * Generates a view link for a page.
00105      *
00106      * @param string $table
00107      * @param string $uid
00108      * @return void
00109      */
00110     public function viewSingleRecord($table, $uid) {
00111         return tx_Workspaces_Service_Workspaces::viewSingleRecord($table, $uid);
00112     }
00113 
00114 
00115     /**
00116      * Saves the selected columns to be shown to the preferences of the current backend user.
00117      *
00118      * @param object $model
00119      * @return void
00120      */
00121     public function saveColumnModel($model) {
00122         $data = array();
00123         foreach ($model as $column) {
00124             $data[$column->column] = array(
00125                 'position'  => $column->position,
00126                 'hidden'   => $column->hidden
00127             );
00128         }
00129         $GLOBALS['BE_USER']->uc['moduleData']['Workspaces']['columns'] = $data;
00130         $GLOBALS['BE_USER']->writeUC();
00131     }
00132 
00133     public function loadColumnModel() {
00134         if(is_array($GLOBALS['BE_USER']->uc['moduleData']['Workspaces']['columns'])) {
00135             return $GLOBALS['BE_USER']->uc['moduleData']['Workspaces']['columns'];
00136         } else {
00137             return array();
00138         }
00139     }
00140 
00141     /**
00142      * Gets the dialog window to be displayed before a record can be sent to the next stage.
00143      *
00144      *  @param integer $uid
00145      * @param string $table
00146      * @param integer $t3ver_oid
00147      * @return array
00148      */
00149     public function sendToNextStageWindow($uid, $table, $t3ver_oid) {
00150         $elementRecord = t3lib_BEfunc::getRecord($table, $uid);
00151 
00152         if(is_array($elementRecord)) {
00153             $stageId = $elementRecord['t3ver_stage'];
00154 
00155             if ($this->getStageService()->isValid($stageId)) {
00156                 $nextStage = $this->getStageService()->getNextStage($stageId);
00157                 $result = $this->getSentToStageWindow($nextStage['uid']);
00158                 $result['affects'] = array(
00159                     'table' => $table,
00160                     'nextStage' => $nextStage['uid'],
00161                     't3ver_oid' => $t3ver_oid,
00162                     'uid' => $uid,
00163                 );
00164             } else {
00165                 $result = $this->getErrorResponse('error.stageId.invalid', 1291111644);
00166             }
00167         } else {
00168             $result = $this->getErrorResponse('error.sendToNextStage.noRecordFound', 1287264776);
00169         }
00170 
00171         return $result;
00172     }
00173 
00174     /**
00175      * Gets the dialog window to be displayed before a record can be sent to the previous stage.
00176      *
00177      * @param integer $uid
00178      * @param string $table
00179      * @return array
00180      */
00181     public function sendToPrevStageWindow($uid, $table) {
00182         $elementRecord = t3lib_BEfunc::getRecord($table, $uid);
00183 
00184         if(is_array($elementRecord)) {
00185             $stageId = $elementRecord['t3ver_stage'];
00186 
00187             if ($this->getStageService()->isValid($stageId)) {
00188                 if ($stageId !== Tx_Workspaces_Service_Stages::STAGE_EDIT_ID) {
00189                     $prevStage = $this->getStageService()->getPrevStage($stageId);
00190 
00191                     $result = $this->getSentToStageWindow($prevStage['uid']);
00192                     $result['affects'] = array(
00193                         'table' => $table,
00194                         'uid' => $uid,
00195                         'nextStage' => $prevStage['uid'],
00196                     );
00197                 } else {
00198                         // element is already in edit stage, there is no prev stage - return an error message
00199                     $result = $this->getErrorResponse('error.sendToPrevStage.noPreviousStage', 1287264746);
00200                 }
00201             } else {
00202                 $result = $this->getErrorResponse('error.stageId.invalid', 1291111644);
00203             }
00204         } else {
00205             $result = $this->getErrorResponse('error.sendToNextStage.noRecordFound', 1287264765);
00206         }
00207 
00208         return $result;
00209     }
00210 
00211     /**
00212      * Gets the dialog window to be displayed before a record can be sent to a specific stage.
00213      *
00214      * @param integer $nextStageId
00215      * @return array
00216      */
00217     public function sendToSpecificStageWindow($nextStageId) {
00218         $result = $this->getSentToStageWindow($nextStageId);
00219         $result['affects'] = array(
00220             'nextStage' => $nextStageId,
00221         );
00222 
00223         return $result;
00224     }
00225 
00226     /**
00227      * Gets a merged variant of recipient defined by uid and custom ones.
00228      *
00229      * @param array list of recipients
00230      * @param string given user string of additional recipients
00231      * @return array
00232      */
00233     public function getRecipientList(array $uidOfRecipients, $additionalRecipients) {
00234         $finalRecipients = array();
00235 
00236         $recipients = array();
00237         foreach ($uidOfRecipients as $userUid) {
00238             $beUserRecord = t3lib_befunc::getRecord('be_users',intval($userUid));
00239             if(is_array($beUserRecord) && $beUserRecord['email'] != '') {
00240                 $recipients[] = $beUserRecord['email'];
00241             }
00242         }
00243 
00244         if ($additionalRecipients != '') {
00245             $additionalRecipients = t3lib_div::trimExplode("\n", $additionalRecipients, TRUE);
00246         } else {
00247             $additionalRecipients = array();
00248         }
00249 
00250         $allRecipients = array_unique(
00251             array_merge($recipients, $additionalRecipients)
00252         );
00253 
00254         foreach ($allRecipients as $recipient) {
00255             if (t3lib_div::validEmail($recipient)) {
00256                 $finalRecipients[] = $recipient;
00257             }
00258         }
00259 
00260         return $finalRecipients;
00261     }
00262 
00263     /**
00264      * Gets an object with this structure:
00265      *
00266      *  affects: object
00267      *      table
00268      *      t3ver_oid
00269      *      nextStage
00270      *      uid
00271      *  receipients: array with uids
00272      *  additional: string
00273      *  comments: string
00274      *
00275      * @param stdObject $parameters
00276      * @return array
00277      */
00278     public function sendToNextStageExecute(stdClass $parameters) {
00279         $cmdArray = array();
00280         $recipients = array();
00281 
00282         $setStageId = $parameters->affects->nextStage;
00283         $comments = $parameters->comments;
00284         $table = $parameters->affects->table;
00285         $uid = $parameters->affects->uid;
00286         $t3ver_oid = $parameters->affects->t3ver_oid;
00287         $recipients = $this->getRecipientList($parameters->receipients, $parameters->additional);
00288 
00289         if ($setStageId == Tx_Workspaces_Service_Stages::STAGE_PUBLISH_EXECUTE_ID) {
00290             $cmdArray[$table][$t3ver_oid]['version']['action'] = 'swap';
00291             $cmdArray[$table][$t3ver_oid]['version']['swapWith'] = $uid;
00292             $cmdArray[$table][$t3ver_oid]['version']['comment'] = $comments;
00293             $cmdArray[$table][$t3ver_oid]['version']['notificationAlternativeRecipients'] = $recipients;
00294         } else {
00295             $cmdArray[$table][$uid]['version']['action'] = 'setStage';
00296             $cmdArray[$table][$uid]['version']['stageId'] = $setStageId;
00297             $cmdArray[$table][$uid]['version']['comment'] = $comments;
00298             $cmdArray[$table][$uid]['version']['notificationAlternativeRecipients'] = $recipients;
00299         }
00300 
00301         $tce = t3lib_div::makeInstance('t3lib_TCEmain');
00302         $tce->start(array(), $cmdArray);
00303         $tce->process_cmdmap();
00304 
00305         $result = array(
00306             'success' => TRUE,
00307         );
00308 
00309         return $result;
00310     }
00311 
00312     /**
00313      * Gets an object with this structure:
00314      *
00315      *  affects: object
00316      *      table
00317      *      t3ver_oid
00318      *      nextStage
00319      *  receipients: array with uids
00320      *  additional: string
00321      *  comments: string
00322      *
00323      * @param stdObject $parameters
00324      * @return array
00325      */
00326     public function sendToPrevStageExecute(stdClass $parameters) {
00327         $cmdArray = array();
00328         $recipients = array();
00329 
00330         $setStageId = $parameters->affects->nextStage;
00331         $comments = $parameters->comments;
00332         $table = $parameters->affects->table;
00333         $uid = $parameters->affects->uid;
00334         $recipients = $this->getRecipientList($parameters->receipients, $parameters->additional);
00335 
00336         $cmdArray[$table][$uid]['version']['action'] = 'setStage';
00337         $cmdArray[$table][$uid]['version']['stageId'] = $setStageId;
00338         $cmdArray[$table][$uid]['version']['comment'] = $comments;
00339         $cmdArray[$table][$uid]['version']['notificationAlternativeRecipients'] = $recipients;
00340 
00341         $tce = t3lib_div::makeInstance('t3lib_TCEmain');
00342         $tce->start(array(), $cmdArray);
00343         $tce->process_cmdmap();
00344 
00345         $result = array(
00346             'success' => TRUE,
00347         );
00348 
00349         return $result;
00350     }
00351 
00352     /**
00353      * Gets an object with this structure:
00354      *
00355      *  affects: object
00356      *      elements: array
00357      *          0: object
00358      *              table
00359      *              t3ver_oid
00360      *              uid
00361      *          1: object
00362      *              table
00363      *              t3ver_oid
00364      *              uid
00365      *      nextStage
00366      *  receipients: array with uids
00367      *  additional: string
00368      *  comments: string
00369      *
00370      * @param stdObject $parameters
00371      * @return array
00372      */
00373     public function sendToSpecificStageExecute(stdClass $parameters) {
00374         $cmdArray = array();
00375 
00376         $setStageId = $parameters->affects->nextStage;
00377         $comments = $parameters->comments;
00378         $elements = $parameters->affects->elements;
00379         $recipients = $this->getRecipientList($parameters->receipients, $parameters->additional);
00380 
00381         foreach($elements as $key=>$element) {
00382             if ($setStageId == Tx_Workspaces_Service_Stages::STAGE_PUBLISH_EXECUTE_ID) {
00383                 $cmdArray[$element->table][$element->t3ver_oid]['version']['action'] = 'swap';
00384                 $cmdArray[$element->table][$element->t3ver_oid]['version']['swapWith'] = $element->uid;
00385                 $cmdArray[$element->table][$element->t3ver_oid]['version']['comment'] = $comments;
00386                 $cmdArray[$element->table][$element->t3ver_oid]['version']['notificationAlternativeRecipients'] = $recipients;
00387             } else {
00388                 $cmdArray[$element->table][$element->uid]['version']['action'] = 'setStage';
00389                 $cmdArray[$element->table][$element->uid]['version']['stageId'] = $setStageId;
00390                 $cmdArray[$element->table][$element->uid]['version']['comment'] = $comments;
00391                 $cmdArray[$element->table][$element->uid]['version']['notificationAlternativeRecipients'] = $recipients;
00392             }
00393         }
00394 
00395         $tce = t3lib_div::makeInstance('t3lib_TCEmain');
00396         $tce->start(array(), $cmdArray);
00397         $tce->process_cmdmap();
00398 
00399         $result = array(
00400             'success' => TRUE,
00401         );
00402 
00403         return $result;
00404     }
00405 
00406     /**
00407      * Gets the dialog window to be displayed before a record can be sent to a stage.
00408      *
00409      * @param  $nextStageId
00410      * @return array
00411      */
00412     protected function getSentToStageWindow($nextStageId) {
00413         $stageTitle = $this->getStageService()->getStageTitle($nextStageId);
00414         $result = array(
00415             'title' => $GLOBALS['LANG']->sL('LLL:EXT:workspaces/Resources/Private/Language/locallang.xml:actionSendToStage'),
00416             'items' => array(
00417                 array(
00418                     'xtype' => 'panel',
00419                     'bodyStyle' => 'margin-bottom: 7px; border: none;',
00420                     'html' => $GLOBALS['LANG']->sL('LLL:EXT:workspaces/Resources/Private/Language/locallang.xml:window.sendToNextStageWindow.itemsWillBeSentTo') . $stageTitle,
00421                 ),
00422                 array(
00423                     'fieldLabel' => $GLOBALS['LANG']->sL('LLL:EXT:workspaces/Resources/Private/Language/locallang.xml:window.sendToNextStageWindow.sendMailTo'),
00424                     'xtype' => 'checkboxgroup',
00425                     'itemCls' => 'x-check-group-alt',
00426                     'columns' => 1,
00427                     'style' => 'max-height: 200px',
00428                     'autoScroll' => true,
00429                     'items' => array(
00430                         $this->getReceipientsOfStage($nextStageId)
00431                     )
00432                 ),
00433                 array(
00434                     'fieldLabel' => $GLOBALS['LANG']->sL('LLL:EXT:workspaces/Resources/Private/Language/locallang.xml:window.sendToNextStageWindow.additionalRecipients'),
00435                     'name' => 'additional',
00436                     'xtype' => 'textarea',
00437                     'width' => 250,
00438                 ),
00439                 array(
00440                     'fieldLabel' => $GLOBALS['LANG']->sL('LLL:EXT:workspaces/Resources/Private/Language/locallang.xml:window.sendToNextStageWindow.comments'),
00441                     'name' => 'comments',
00442                     'xtype' => 'textarea',
00443                     'width' => 250,
00444                     'value' => $this->getDefaultCommentOfStage($nextStageId),
00445                 ),
00446             )
00447         );
00448 
00449         return $result;
00450     }
00451 
00452     /**
00453      * Gets all assigned recipients of a particular stage.
00454      *
00455      * @param integer $stage
00456      * @return array
00457      */
00458     protected function getReceipientsOfStage($stage) {
00459         $result = array();
00460 
00461         $recipients = $this->getStageService()->getResponsibleBeUser($stage);
00462 
00463         foreach ($recipients as $id => $user) {
00464             if (t3lib_div::validEmail($user['email'])) {
00465                 $name = $user['realName'] ? $user['realName'] : $user['username'];
00466                 $result[] = array(
00467                     'boxLabel' => sprintf('%s (%s)', $name, $user['email']),
00468                     'name' => 'receipients-' . $id,
00469                     'checked' => TRUE,
00470                 );
00471             }
00472         }
00473 
00474         return $result;
00475     }
00476 
00477     /**
00478      * Gets the default comment of a particular stage.
00479      *
00480      * @param integer $stage
00481      * @return string
00482      */
00483     protected function getDefaultCommentOfStage($stage) {
00484         $result = $this->getStageService()->getPropertyOfCurrentWorkspaceStage($stage, 'default_mailcomment');
00485 
00486         return $result;
00487     }
00488 
00489     /**
00490      * Gets an instance of the Stage service.
00491      *
00492      * @return Tx_Workspaces_Service_Stages
00493      */
00494     protected function getStageService() {
00495         if (!isset($this->stageService)) {
00496             $this->stageService = t3lib_div::makeInstance('Tx_Workspaces_Service_Stages');
00497         }
00498         return $this->stageService;
00499     }
00500 }
00501 
00502 
00503 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/workspaces/Classes/ExtDirect/ActionHandler.php'])) {
00504     include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/workspaces/Classes/ExtDirect/ActionHandler.php']);
00505 }
00506 ?>