TYPO3 API  SVNRelease
class.sc_mod_web_perm_ajax.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 2007-2011 mehrwert (typo3@mehrwert.de)
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 *
00017 *  This script is distributed in the hope that it will be useful,
00018 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 *  GNU General Public License for more details.
00021 *
00022 *  This copyright notice MUST APPEAR in all copies of the script!
00023 ***************************************************************/
00024 /**
00025  * [CLASS/FUNCTION INDEX of SCRIPT]
00026  *
00027  *
00028  *
00029  *   66: class SC_mod_web_perm_ajax
00030  *
00031  *              SECTION: Init method for this class
00032  *   97:     public function __construct()
00033  *
00034  *              SECTION: Main dispatcher method
00035  *  143:     public function dispatch($params = array(), TYPO3AJAX &$ajaxObj = null)
00036  *
00037  *              SECTION: Helpers for this script
00038  *  259:     private function renderUserSelector($page, $ownerUid, $username = '')
00039  *  302:     private function renderGroupSelector($page, $groupUid, $groupname = '')
00040  *  350:     private function renderOwnername($page, $ownerUid, $username)
00041  *  363:     private function renderGroupname($page, $groupUid, $groupname)
00042  *  375:     private function renderToggleEditLock($page, $editlockstate)
00043  *  389:     private function renderPermissions($int, $pageId = 0, $who = 'user')
00044  *
00045  * TOTAL FUNCTIONS: 8
00046  * (This index is automatically created/updated by the extension "extdeveval")
00047  *
00048  */
00049 
00050 $GLOBALS['LANG']->includeLLFile('EXT:lang/locallang_mod_web_perm.xml');
00051 
00052 /**
00053  * This class extends the permissions module in the TYPO3 Backend to provide
00054  * convenient methods of editing of page permissions (including page ownership
00055  * (user and group)) via new TYPO3AJAX facility
00056  *
00057  * @author      Andreas Kundoch <typo3@mehrwert.de>
00058  * @version     $Id: class.sc_mod_web_perm_ajax.php 10120 2011-01-18 20:03:36Z ohader $
00059  * @package     TYPO3
00060  * @subpackage  core
00061  * @license     GPL
00062  * @since       TYPO3_4-2
00063  */
00064 class SC_mod_web_perm_ajax {
00065 
00066     protected $conf = array();  // The local configuration array
00067     protected $backPath = '../../../';  // TYPO3 Back Path
00068 
00069     /********************************************
00070      *
00071      * Init method for this class
00072      *
00073      ********************************************/
00074 
00075     /**
00076      * The constructor of this class
00077      *
00078      * @return  Void
00079      */
00080     public function __construct() {
00081 
00082             // Configuration, variable assignment
00083         $this->conf['page']          = t3lib_div::_POST('page');
00084         $this->conf['who']           = t3lib_div::_POST('who');
00085         $this->conf['mode']          = t3lib_div::_POST('mode');
00086         $this->conf['bits']          = intval(t3lib_div::_POST('bits'));
00087         $this->conf['permissions']   = intval(t3lib_div::_POST('permissions'));
00088         $this->conf['action']        = t3lib_div::_POST('action');
00089         $this->conf['ownerUid']      = intval(t3lib_div::_POST('ownerUid'));
00090         $this->conf['username']      = t3lib_div::_POST('username');
00091         $this->conf['groupUid']      = intval(t3lib_div::_POST('groupUid'));
00092         $this->conf['groupname']     = t3lib_div::_POST('groupname');
00093         $this->conf['editLockState'] = intval(t3lib_div::_POST('editLockState'));
00094 
00095             // User: Replace some parts of the posted values
00096         $this->conf['new_owner_uid'] = intval(t3lib_div::_POST('newOwnerUid'));
00097         $temp_owner_data = t3lib_BEfunc::getUserNames(
00098             'username, uid',
00099             ' AND uid = ' . $this->conf['new_owner_uid']
00100         );
00101         $this->conf['new_owner_username'] = htmlspecialchars(
00102             $temp_owner_data[$this->conf['new_owner_uid']]['username']
00103         );
00104 
00105             // Group: Replace some parts of the posted values
00106         $this->conf['new_group_uid'] = intval(t3lib_div::_POST('newGroupUid'));
00107         $temp_group_data             = t3lib_BEfunc::getGroupNames(
00108             'title,uid',
00109             ' AND uid = ' . $this->conf['new_group_uid']
00110         );
00111         $this->conf['new_group_username'] = htmlspecialchars(
00112             $temp_group_data[$this->conf['new_group_uid']]['title']
00113         );
00114 
00115     }
00116 
00117     /********************************************
00118      *
00119      * Main dispatcher method
00120      *
00121      ********************************************/
00122 
00123     /**
00124      * The main dispatcher function. Collect data and prepare HTML output.
00125      *
00126      * @param   array       $params: array of parameters from the AJAX interface, currently unused
00127      * @param   TYPO3AJAX       $ajaxObj: object of type TYPO3AJAX
00128      * @return  Void
00129      */
00130     public function dispatch($params = array(), TYPO3AJAX &$ajaxObj = null) {
00131         $content = '';
00132 
00133             // Basic test for required value
00134         if ($this->conf['page'] > 0) {
00135 
00136                 // Init TCE for execution of update
00137             $tce = t3lib_div::makeInstance('t3lib_TCEmain');
00138             $tce->stripslashes_values = 1;
00139 
00140                 // Determine the scripts to execute
00141             switch ($this->conf['action']) {
00142 
00143                     // Return the select to change the owner (BE user) of the page
00144                 case 'show_change_owner_selector':
00145                     $content = $this->renderUserSelector($this->conf['page'], $this->conf['ownerUid'], $this->conf['username']);
00146                     break;
00147 
00148                     // Change the owner and return the new owner HTML snippet
00149                 case 'change_owner':
00150                     if (is_int($this->conf['new_owner_uid'])) {
00151                             // Prepare data to change
00152                         $data = array();
00153                         $data['pages'][$this->conf['page']]['perms_userid'] = $this->conf['new_owner_uid'];
00154 
00155                             // Execute TCE Update
00156                         $tce->start($data, array());
00157                         $tce->process_datamap();
00158                         $content = $this->renderOwnername($this->conf['page'], $this->conf['new_owner_uid'], $this->conf['new_owner_username']);
00159                     } else {
00160                         $ajaxObj->setError('An error occured: No page owner uid specified.');
00161                     }
00162                     break;
00163 
00164                     // Return the select to change the group (BE group) of the page
00165                 case 'show_change_group_selector':
00166                     $content = $this->renderGroupSelector($this->conf['page'], $this->conf['groupUid'], $this->conf['groupname']);
00167                     break;
00168 
00169                     // Change the group and return the new group HTML snippet
00170                 case 'change_group':
00171                     if (is_int($this->conf['new_group_uid'])) {
00172 
00173                             // Prepare data to change
00174                         $data = array();
00175                         $data['pages'][$this->conf['page']]['perms_groupid'] = $this->conf['new_group_uid'];
00176 
00177                             // Execute TCE Update
00178                         $tce->start($data, array());
00179                         $tce->process_datamap();
00180 
00181                         $content = $this->renderGroupname($this->conf['page'], $this->conf['new_group_uid'], $this->conf['new_group_username']);
00182                     } else {
00183                         $ajaxObj->setError('An error occured: No page group uid specified.');
00184                     }
00185                     break;
00186 
00187                     // Change the group and return the new group HTML snippet
00188                 case 'toggle_edit_lock':
00189 
00190                         // Prepare data to change
00191                     $data = array();
00192                     $data['pages'][$this->conf['page']]['editlock'] = ($this->conf['editLockState'] === 1 ? 0 : 1);
00193 
00194                         // Execute TCE Update
00195                     $tce->start($data, array());
00196                     $tce->process_datamap();
00197 
00198                     $content = $this->renderToggleEditLock($this->conf['page'], $data['pages'][$this->conf['page']]['editlock']);
00199                     break;
00200 
00201                     // The script defaults to change permissions
00202                 default:
00203                     if ($this->conf['mode'] == 'delete') {
00204                         $this->conf['permissions'] = intval($this->conf['permissions'] - $this->conf['bits']);
00205                     } else {
00206                         $this->conf['permissions'] = intval($this->conf['permissions'] + $this->conf['bits']);
00207                     }
00208 
00209                         // Prepare data to change
00210                     $data = array();
00211                     $data['pages'][$this->conf['page']]['perms_'.$this->conf['who']] = $this->conf['permissions'];
00212 
00213                         // Execute TCE Update
00214                     $tce->start($data, array());
00215                     $tce->process_datamap();
00216 
00217                     $content = $this->renderPermissions($this->conf['permissions'], $this->conf['page'], $this->conf['who']);
00218             }
00219         } else {
00220             $ajaxObj->setError('This script cannot be called directly.');
00221         }
00222         $ajaxObj->addContent($this->conf['page'].'_'.$this->conf['who'], $content);
00223     }
00224 
00225     /********************************************
00226      *
00227      * Helpers for this script
00228      *
00229      ********************************************/
00230 
00231     /**
00232      * Generate the user selector element
00233      *
00234      * @param   Integer     $page: The page id to change the user for
00235      * @param   Integer     $ownerUid: The page owner uid
00236      * @param   String      $username: The username to display
00237      * @return  String      The html select element
00238      */
00239     protected function renderUserSelector($page, $ownerUid, $username = '') {
00240 
00241             // Get usernames
00242         $beUsers = t3lib_BEfunc::getUserNames();
00243 
00244             // Init groupArray
00245         $groups = array();
00246 
00247         if (!$GLOBALS['BE_USER']->isAdmin()) {
00248             $beUsers = t3lib_BEfunc::blindUserNames($beUsers, $groups, 1);
00249         }
00250 
00251             // Owner selector:
00252         $options = '';
00253 
00254             // Loop through the users
00255         foreach ($beUsers as $uid => $row) {
00256             $selected = ($uid == $ownerUid  ? ' selected="selected"' : '');
00257             $options .= '<option value="'.$uid.'"'.$selected.'>'.htmlspecialchars($row['username']).'</option>';
00258         }
00259 
00260         $elementId = 'o_'.$page;
00261         $options = '<option value="0"></option>'.$options;
00262         $selector = '<select name="new_page_owner" id="new_page_owner">'.$options.'</select>';
00263         $saveButton = '<a onclick="WebPermissions.changeOwner('.$page.', '.$ownerUid.', \''.$elementId.'\');" title="Change owner">' . t3lib_iconWorks::getSpriteIcon('actions-document-save') . '</a>';
00264         $cancelButton = '<a onclick="WebPermissions.restoreOwner('.$page.', '.$ownerUid.', \''.($username == '' ? '<span class=not_set>[not set]</span>' : htmlspecialchars($username)).'\', \''.$elementId.'\');" title="Cancel">' . t3lib_iconWorks::getSpriteIcon('actions-document-close') . '</a>';
00265         $ret = $selector.$saveButton.$cancelButton;
00266         return $ret;
00267     }
00268 
00269     /**
00270      * Generate the group selector element
00271      *
00272      * @param   Integer     $page: The page id to change the user for
00273      * @param   Integer     $groupUid: The page group uid
00274      * @param   String      $username: The username to display
00275      * @return  String      The html select element
00276      */
00277     protected function renderGroupSelector($page, $groupUid, $groupname = '') {
00278 
00279             // Get usernames
00280         $beGroups = t3lib_BEfunc::getListGroupNames('title,uid');
00281         $beGroupKeys = array_keys($beGroups);
00282         $beGroupsO = $beGroups = t3lib_BEfunc::getGroupNames();
00283         if (!$GLOBALS['BE_USER']->isAdmin()) {
00284             $beGroups = t3lib_BEfunc::blindGroupNames($beGroupsO, $beGroupKeys, 1);
00285         }
00286 
00287             // Group selector:
00288         $options = '';
00289 
00290             // flag: is set if the page-groupid equals one from the group-list
00291         $userset = 0;
00292 
00293             // Loop through the groups
00294         foreach ($beGroups as $uid => $row) {
00295             if ($uid == $groupUid) {
00296                 $userset = 1;
00297                 $selected = ' selected="selected"';
00298             } else {
00299                 $selected = '';
00300             }
00301             $options .= '<option value="'.$uid.'"'.$selected.'>'.htmlspecialchars($row['title']).'</option>';
00302         }
00303 
00304             // If the group was not set AND there is a group for the page
00305         if (!$userset && $groupUid) {
00306             $options = '<option value="'.$groupUid.'" selected="selected">'.htmlspecialchars($beGroupsO[$groupUid]['title']).'</option>'.$options;
00307         }
00308 
00309         $elementId = 'g_'.$page;
00310         $options = '<option value="0"></option>'.$options;
00311         $selector = '<select name="new_page_group" id="new_page_group">'.$options.'</select>';
00312         $saveButton = '<a onclick="WebPermissions.changeGroup('.$page.', '.$groupUid.', \''.$elementId.'\');" title="Change group">' . t3lib_iconWorks::getSpriteIcon('actions-document-save') . '</a>';
00313         $cancelButton = '<a onclick="WebPermissions.restoreGroup('.$page.', '.$groupUid.', \''.($groupname == '' ? '<span class=not_set>[not set]</span>' : htmlspecialchars($groupname)).'\', \''.$elementId.'\');" title="Cancel">' . t3lib_iconWorks::getSpriteIcon('actions-document-close') . '</a>';
00314         $ret = $selector.$saveButton.$cancelButton;
00315         return $ret;
00316     }
00317 
00318 
00319     /**
00320      * Print the string with the new owner of a page record
00321      *
00322      * @param   Integer     $page: The TYPO3 page id
00323      * @param   Integer     $ownerUid: The new page user uid
00324      * @param   String      $username: The TYPO3 BE username (used to display in the element)
00325      * @param   Boolean     $validUser: Must be set to FALSE, if the user has no name or is deleted
00326      * @return  String      The new group wrapped in HTML
00327      */
00328     public function renderOwnername($page, $ownerUid, $username, $validUser = true) {
00329         $elementId = 'o_'.$page;
00330         $ret = '<span id="' . $elementId . '"><a class="ug_selector" onclick="WebPermissions.showChangeOwnerSelector(' . $page . ', ' . $ownerUid . ', \'' . $elementId.'\', \'' . htmlspecialchars($username) . '\');">' . ($validUser ? ($username == '' ? ('<span class=not_set>['. $GLOBALS['LANG']->getLL('notSet') .']</span>') : htmlspecialchars(t3lib_div::fixed_lgd_cs($username, 20))) :  ('<span class=not_set title="' . htmlspecialchars(t3lib_div::fixed_lgd_cs($username, 20)) . '">[' . $GLOBALS['LANG']->getLL('deleted') . ']</span>')) . '</a></span>';
00331         return $ret;
00332     }
00333 
00334 
00335     /**
00336      * Print the string with the new group of a page record
00337      *
00338      * @param   Integer     $page: The TYPO3 page id
00339      * @param   Integer     $groupUid: The new page group uid
00340      * @param   String      $groupname: The TYPO3 BE groupname (used to display in the element)
00341      * @param   Boolean     $validGroup: Must be set to FALSE, if the group has no name or is deleted
00342      * @return  String      The new group wrapped in HTML
00343      */
00344     public function renderGroupname($page, $groupUid, $groupname, $validGroup = true) {
00345         $elementId = 'g_'.$page;
00346         $ret = '<span id="'.$elementId . '"><a class="ug_selector" onclick="WebPermissions.showChangeGroupSelector(' . $page . ', ' . $groupUid . ', \'' . $elementId . '\', \'' . htmlspecialchars($groupname) . '\');">'. ($validGroup ? ($groupname == '' ? ('<span class=not_set>['. $GLOBALS['LANG']->getLL('notSet') .']</span>') : htmlspecialchars(t3lib_div::fixed_lgd_cs($groupname, 20))) : ('<span class=not_set title="' . htmlspecialchars(t3lib_div::fixed_lgd_cs($groupname, 20)) . '">[' . $GLOBALS['LANG']->getLL('deleted') . ']</span>')) . '</a></span>';
00347         return $ret;
00348     }
00349 
00350 
00351     /**
00352      * Print the string with the new edit lock state of a page record
00353      *
00354      * @param   Integer     $page: The TYPO3 page id
00355      * @param   String      $editlockstate: The state of the TYPO3 page (locked, unlocked)
00356      * @return  String      The new edit lock string wrapped in HTML
00357      */
00358     protected function renderToggleEditLock($page, $editLockState) {
00359         if ($editLockState === 1) {
00360             $ret = '<a class="editlock" onclick="WebPermissions.toggleEditLock('.$page.', 1);" title="The page and all content is locked for editing by all non-Admin users.">' . t3lib_iconWorks::getSpriteIcon('status-warning-lock') . '</a>';
00361         } else {
00362             $ret = '<a class="editlock" onclick="WebPermissions.toggleEditLock('.$page.', 0);" title="Enable the &raquo;Admin-only&laquo; edit lock for this page">[+]</a>';
00363         }
00364         return $ret;
00365     }
00366 
00367 
00368     /**
00369      * Print a set of permissions. Also used in index.php
00370      *
00371      * @param   integer     Permission integer (bits)
00372      * @param   Integer     $page: The TYPO3 page id
00373      * @param   String      $who: The scope (user, group or everybody)
00374      * @return  string      HTML marked up x/* indications.
00375      */
00376     public function renderPermissions($int, $pageId = 0, $who = 'user') {
00377         global $LANG;
00378         $str = '';
00379 
00380         $permissions = array(1,16,2,4,8);
00381         foreach ($permissions as $permission) {
00382             if ($int&$permission) {
00383                 $str .= t3lib_iconWorks::getSpriteIcon('status-status-permission-granted',array('tag'=>'a','title'=>$LANG->getLL($permission,1), 'onclick'=> 'WebPermissions.setPermissions('.$pageId.', '.$permission.', \'delete\', \''.$who.'\', '.$int.');'));
00384             } else {
00385                 $str .= t3lib_iconWorks::getSpriteIcon('status-status-permission-denied',array('tag'=>'a','title'=>$LANG->getLL($permission,1),'onclick'=>'WebPermissions.setPermissions('.$pageId.', '.$permission.', \'add\', \''.$who.'\', '.$int.');'));
00386             }
00387         }
00388         return '<span id="'.$pageId.'_'.$who.'">'.$str.'</span>';
00389     }
00390 
00391 }
00392 
00393 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['typo3/mod/web/perm/class.sc_mod_web_perm_ajax.php'])) {
00394     include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['typo3/mod/web/perm/class.sc_mod_web_perm_ajax.php']);
00395 }
00396 
00397 ?>