TYPO3 API  SVNRelease
class.tx_opendocs.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 2008-2011 Benjamin Mack <mack@xnos.org>
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 require_once(PATH_typo3 . 'interfaces/interface.backend_toolbaritem.php');
00029 
00030     // load the language file
00031 $GLOBALS['LANG']->includeLLFile('EXT:opendocs/locallang_opendocs.xml');
00032 
00033 
00034 /**
00035  * Adding a list of all open documents of a user to the backend.php
00036  *
00037  * @author  Benjamin Mack <benni@typo3.org>
00038  * @author  Ingo Renner <ingo@typo3.org>
00039  * @package TYPO3
00040  * @subpackage  opendocs
00041  */
00042 class tx_opendocs implements backend_toolbarItem {
00043 
00044     /**
00045      * reference back to the backend object
00046      *
00047      * @var TYPO3backend
00048      */
00049     protected $backendReference;
00050 
00051     protected $openDocs;
00052     protected $recentDocs;
00053     protected $EXTKEY = 'opendocs';
00054 
00055 
00056     /**
00057      * constructor, loads the documents from the user control
00058      *
00059      * @param   TYPO3backend    TYPO3 backend object reference
00060      */
00061     public function __construct(TYPO3backend &$backendReference = null) {
00062         $this->backendReference = $backendReference;
00063         $this->loadDocsFromUserSession();
00064     }
00065 
00066     /**
00067      * checks whether the user has access to this toolbar item
00068      *
00069      * @return  boolean  true if user has access, false if not
00070      */
00071     public function checkAccess() {
00072         $conf = $GLOBALS['BE_USER']->getTSConfig('backendToolbarItem.tx_opendocs.disabled');
00073         return ($conf['value'] == 1 ? false : true);
00074     }
00075 
00076     /**
00077      * loads the opened and recently opened documents from the user
00078      *
00079      * @return  void
00080      */
00081     public function loadDocsFromUserSession() {
00082         list($this->openDocs, )  = $GLOBALS['BE_USER']->getModuleData('alt_doc.php', 'ses');
00083         $this->recentDocs        = $GLOBALS['BE_USER']->getModuleData('opendocs::recent');
00084     }
00085 
00086     /**
00087      * renders the toolbar item and the initial menu
00088      *
00089      * @return  string      the toolbar item including the initial menu content as HTML
00090      */
00091     public function render() {
00092         $this->addJavascriptToBackend();
00093         $this->addCssToBackend();
00094         $numDocs      = count($this->openDocs);
00095         $opendocsMenu = array();
00096         $title        = $GLOBALS['LANG']->getLL('toolbaritem', true);
00097 
00098             // toolbar item icon
00099         $opendocsMenu[] = '<a href="#" class="toolbar-item">';
00100         $opendocsMenu[] = '<input type="text" id="tx-opendocs-counter" disabled="disabled" value="' . $numDocs . '" />';
00101         $opendocsMenu[] = t3lib_iconWorks::getSpriteIcon('apps-toolbar-menu-opendocs', array('title' => $title)) . '</a>';
00102 
00103             // toolbar item menu and initial content
00104         $opendocsMenu[] = '<div class="toolbar-item-menu" style="display: none;">';
00105         $opendocsMenu[] = $this->renderMenu();
00106         $opendocsMenu[] = '</div>';
00107 
00108         return implode(LF, $opendocsMenu);
00109     }
00110 
00111     /**
00112      * renders the pure contents of the menu
00113      *
00114      * @return  string      the menu's content
00115      */
00116     public function renderMenu() {
00117         $openDocuments   = $this->openDocs;
00118         $recentDocuments = $this->recentDocs;
00119         $entries         = array();
00120         $content         = '';
00121 
00122         if (count($openDocuments)) {
00123             $entries[] = '<tr><th colspan="3">' . $GLOBALS['LANG']->getLL('open_docs', true) . '</th></tr>';
00124 
00125             $i = 0;
00126             foreach ($openDocuments as $md5sum => $openDocument) {
00127                 $i++;
00128                 $entries[] = $this->renderMenuEntry($openDocument, $md5sum, false, ($i == 1));
00129             }
00130         }
00131 
00132             // if there are "recent documents" in the list, add them
00133         if (count($recentDocuments)) {
00134             $entries[] = '<tr><th colspan="3">' . $GLOBALS['LANG']->getLL('recent_docs', true) . '</th></tr>';
00135 
00136             $i = 0;
00137             foreach ($recentDocuments as $md5sum => $recentDocument) {
00138                 $i++;
00139                 $entries[] = $this->renderMenuEntry($recentDocument, $md5sum, true, ($i == 1));
00140             }
00141         }
00142 
00143         if (count($entries)) {
00144             $content = '<table class="list" cellspacing="0" cellpadding="0" border="0">' . implode('', $entries) . '</table>';
00145         } else {
00146             $content = '<div class="no-docs">' . $GLOBALS['LANG']->getLL('no_docs', true) . '</div>';
00147         }
00148 
00149         return $content;
00150     }
00151 
00152     /**
00153      * returns the recent documents list as an array
00154      *
00155      * @return  array   all recent documents as list-items
00156      */
00157     public function renderMenuEntry($document, $md5sum, $isRecentDoc = false, $isFirstDoc = false) {
00158         $table  = $document[3]['table'];
00159         $uid    = $document[3]['uid'];
00160         $record = t3lib_BEfunc::getRecordWSOL($table, $uid);
00161         if (!is_array($record)) {
00162                 // record seems to be deleted
00163             return '';
00164         }
00165         $label  = htmlspecialchars(strip_tags(t3lib_div::htmlspecialchars_decode($document[0])));
00166         $icon   = t3lib_iconWorks::getSpriteIconForRecord($table, $record);
00167         $link   = $GLOBALS['BACK_PATH'] . 'alt_doc.php?' . $document[2];
00168 
00169         $firstRow = '';
00170         if ($isFirstDoc) {
00171             $firstRow = ' first-row';
00172         }
00173 
00174         if (!$isRecentDoc) {
00175             $title = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:rm.closeDoc', true);
00176 
00177                 // open document
00178             $closeIcon = t3lib_iconWorks::getSpriteIcon('actions-document-close');
00179 
00180             $entry = '
00181                 <tr class="opendoc' . $firstRow . '">
00182                     <td class="icon">' . $icon . '</td>
00183                     <td class="label"><a href="#" onclick="jump(unescape(\'' . htmlspecialchars($link) . '\'), \'web_list\', \'web\'); TYPO3BackendOpenDocs.toggleMenu(); return false;" target="content">' . $label . '</a></td>
00184                     <td class="close" onclick="return TYPO3BackendOpenDocs.closeDocument(\'' . $md5sum . '\');">' . $closeIcon . '</td>
00185                 </tr>';
00186         } else {
00187                 // recently used document
00188             $entry = '
00189                 <tr class="recentdoc' . $firstRow . '">
00190                     <td class="icon">' . $icon . '</td>
00191                     <td class="label" colspan="2"><a href="#" onclick="jump(unescape(\'' . htmlspecialchars($link) . '\'), \'web_list\', \'web\'); TYPO3BackendOpenDocs.toggleMenu(); return false;" target="content">' . $label . '</a></td>
00192                 </tr>';
00193         }
00194 
00195         return $entry;
00196     }
00197 
00198     /**
00199      * returns additional attributes for the list item in the toolbar
00200      *
00201      * @return  string      list item HTML attibutes
00202      */
00203     public function getAdditionalAttributes() {
00204         return ' id="tx-opendocs-menu"';
00205     }
00206 
00207     /**
00208      * adds the neccessary javascript to the backend
00209      *
00210      * @return  void
00211      */
00212     protected function addJavascriptToBackend() {
00213         $this->backendReference->addJavascriptFile(t3lib_extMgm::extRelPath($this->EXTKEY) . 'opendocs.js');
00214     }
00215 
00216     /**
00217      * adds the neccessary CSS to the backend
00218      *
00219      * @return  void
00220      */
00221     protected function addCssToBackend() {
00222         $this->backendReference->addCssFile('opendocs', t3lib_extMgm::extRelPath($this->EXTKEY) . 'opendocs.css');
00223     }
00224 
00225 
00226     /*******************
00227      ***    HOOKS    ***
00228      *******************/
00229 
00230     /**
00231      * called as a hook in t3lib_BEfunc::setUpdateSignal, calls a JS function to change
00232      * the number of opened documents
00233      *
00234      * @param   array       $params
00235      * @param   unknown_type        $ref
00236      * @return  string      list item HTML attibutes
00237      */
00238     public function updateNumberOfOpenDocsHook(&$params, $ref) {
00239         $params['JScode'] = '
00240             if (top && top.TYPO3BackendOpenDocs) {
00241                 top.TYPO3BackendOpenDocs.updateNumberOfDocs(' . count($this->openDocs) . ', true);
00242             }
00243         ';
00244     }
00245 
00246 
00247     /******************
00248      *** AJAX CALLS ***
00249      ******************/
00250 
00251     /**
00252      * closes a document in the session and
00253      *
00254      * @param   array       array of parameters from the AJAX interface, currently unused
00255      * @param   TYPO3AJAX   object of type TYPO3AJAX
00256      * @return  string      list item HTML attibutes
00257      */
00258     public function closeDocument($params = array(), TYPO3AJAX &$ajaxObj = null) {
00259         $md5sum = t3lib_div::_GP('md5sum');
00260 
00261         if ($md5sum && isset($this->openDocs[$md5sum])) {
00262 
00263                 // add the document to be closed to the recent documents
00264             $this->recentDocs = array_merge(
00265                 array($md5sum => $this->openDocs[$md5sum]),
00266                 $this->recentDocs
00267             );
00268 
00269                 // allow a maximum of 8 recent documents
00270             if (count($this->recentDocs) > 8) {
00271                 $this->recentDocs = array_slice($this->recentDocs, 0, 8);
00272             }
00273 
00274                 // remove it from the list of the open documents, and store the status
00275             unset($this->openDocs[$md5sum]);
00276             list(, $docDat) = $GLOBALS['BE_USER']->getModuleData('alt_doc.php', 'ses');
00277             $GLOBALS['BE_USER']->pushModuleData('alt_doc.php', array($this->openDocs, $docDat));
00278             $GLOBALS['BE_USER']->pushModuleData('opendocs::recent', $this->recentDocs);
00279         }
00280 
00281         $this->renderAjax($params, $ajaxObj);
00282     }
00283 
00284     /**
00285      * renders the menu so that it can be returned as response to an AJAX call
00286      *
00287      * @param   array       array of parameters from the AJAX interface, currently unused
00288      * @param   TYPO3AJAX   object of type TYPO3AJAX
00289      * @return  void
00290      */
00291     public function renderAjax($params = array(), TYPO3AJAX &$ajaxObj = null) {
00292         $menuContent = $this->renderMenu();
00293 
00294         $ajaxObj->addContent('opendocsMenu', $menuContent);
00295     }
00296 
00297 }
00298 
00299 
00300 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/opendocs/class.tx_opendocs.php'])) {
00301     include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/opendocs/class.tx_opendocs.php']);
00302 }
00303 ?>