eZ Publish  [4.0]
ezcontentbrowse.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZContentBrowse class
00004 //
00005 // Created on: <28-Apr-2003 11:04:47 sp>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program 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 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 /*! \file ezcontentbrowse.php
00032 */
00033 
00034 /*!
00035   \class eZContentBrowse ezcontentbrowse.php
00036   \brief Handles browsing of content in the node tree
00037 
00038   This class makes it easy to use the browse system to
00039   search for content objects or nodes. The class will take
00040   care of storing the necessary session variables and redirect
00041   to the browse page.
00042 
00043   Using it is simply to call the \link browse \endlink function with some parameters.
00044 
00045 \code
00046 eZContentBrowse::browse( array( 'action_name' => 'MyActionName' ), $module );
00047 \endcode
00048 
00049   It requires the module objects as the second parameter to redirect and the first
00050   define how the browse page should behave. Normally you just want to set \c action_name
00051   and define the behaviour of that action in settings/browse.ini.
00052 
00053 */
00054 
00055 //include_once( 'lib/ezutils/classes/ezhttptool.php' );
00056 
00057 class eZContentBrowse
00058 {
00059     /*!
00060      Initializes the object with the session data if they are found.
00061      If \a $params is supplied it used instead.
00062     */
00063     function eZContentBrowse( $params = false )
00064     {
00065         $http = eZHTTPTool::instance();
00066         if ( !$params && $http->hasSessionVariable( 'BrowseParameters' ) )
00067         {
00068             $this->Parameters =& $http->sessionVariable( 'BrowseParameters' );
00069         }
00070         else
00071         {
00072             $this->Parameters = $params;
00073         }
00074     }
00075 
00076     /*!
00077      \return an array with attribute names.
00078     */
00079     function attributes()
00080     {
00081         return array_keys( $this->Parameters );
00082     }
00083 
00084     /*!
00085      \return true if the attribute name \a $attributeName is among the browse parameters.
00086     */
00087     function hasAttribute( $attributeName )
00088     {
00089         return array_key_exists( $attributeName, $this->Parameters );
00090     }
00091 
00092     /*!
00093      \return the attribute value of the attribute named \a $attributeName or \c null if no such attribute.
00094     */
00095     function attribute( $attributeName )
00096     {
00097         if ( isset( $this->Parameters[$attributeName] ) )
00098         {
00099             return $this->Parameters[$attributeName];
00100         }
00101 
00102         eZDebug::writeError( "Attribute '$attributeName' does not exist", 'eZContentBrowse::attribute' );
00103         return null;
00104     }
00105 
00106     /*!
00107      \static
00108      Sets some session data taken from \a $parameters and start the browse module by redirecting to it using \a $module.
00109      Most data will be automatically derived from the \c action_name value taken from settings/browse.ini, other
00110      values will override default values.
00111     */
00112     static function browse( $parameters = array(), &$module )
00113     {
00114         $ini = eZINI::instance( 'browse.ini' );
00115 
00116         if ( !isset( $parameters['action_name'] ) )
00117             $parameters['action_name'] = $ini->variable( 'BrowseSettings', 'DefaultActionName' );
00118 
00119         if ( !isset( $parameters['type'] ) )
00120             $parameters['type'] = $parameters['action_name']; //$ini->variable( $parameters['action_name'], 'BrowseType' );
00121 
00122         if ( !isset( $parameters['selection'] ) )
00123         {
00124             if ( $ini->hasVariable( $parameters['type'], 'SelectionType' ) )
00125                 $parameters['selection'] = $ini->variable( $parameters['type'], 'SelectionType' );
00126             else
00127                 $parameters['selection'] = $ini->variable( 'BrowseSettings', 'DefaultSelectionType' );
00128         }
00129 
00130         if ( !isset( $parameters['return_type'] ) )
00131         {
00132             if ( $ini->hasVariable( $parameters['type'], 'ReturnType' ) )
00133                 $parameters['return_type'] = $ini->variable( $parameters['type'], 'ReturnType' );
00134             else
00135                 $parameters['return_type'] = $ini->variable( 'BrowseSettings', 'DefaultReturnType' );
00136         }
00137 
00138         if ( !isset( $parameters['browse_custom_action'] ) )
00139             $parameters['browse_custom_action'] = false;
00140 
00141         if ( !isset( $parameters['custom_action_data'] ) )
00142             $parameters['custom_action_data'] = false;
00143 
00144         if ( !isset( $parameters['description_template'] ) )
00145             $parameters['description_template'] = false;
00146 
00147         if ( !isset( $parameters['start_node'] ) )
00148             $parameters['start_node'] = $ini->variable( $parameters['type'], 'StartNode' );
00149 
00150         if ( !isset( $parameters['ignore_nodes_select'] ) )
00151             $parameters['ignore_nodes_select'] = array();
00152 
00153         if ( !isset( $parameters['ignore_nodes_select_subtree'] ) )
00154             $parameters['ignore_nodes_select_subtree'] = array();
00155 
00156         if ( !isset( $parameters['ignore_nodes_click'] ) )
00157             $parameters['ignore_nodes_click'] = array();
00158 
00159         if ( !isset( $parameters['class_array'] ) )
00160         {
00161             if ( $ini->hasVariable( $parameters['type'], 'Class' ) )
00162             {
00163                 $parameters['class_array'] = $ini->variable( $parameters['type'], 'Class' );
00164             }
00165             else
00166             {
00167                 $parameters['class_array'] = false;
00168             }
00169         }
00170 
00171         if ( isset( $parameters['keys'] ) )
00172         {
00173             $overrideStartNode = false;
00174             foreach ( $parameters['keys'] as $key => $keyValue )
00175             {
00176                 $variableName = 'StartNode_' . $key;
00177                 if ( !$ini->hasVariable( $parameters['type'], $variableName ) )
00178                     continue;
00179                 $keyData = $ini->variable( $parameters['type'], $variableName );
00180                 if ( is_array( $keyValue ) )
00181                 {
00182                     foreach ( $keyValue as $keySubValue )
00183                     {
00184                         if ( isset( $keyData[$keySubValue] ) )
00185                             $overrideStartNode = $keyData[$keySubValue];
00186                     }
00187                 }
00188                 else if ( isset( $keyData[$keyValue] ) )
00189                 {
00190                     $overrideStartNode = $keyData[$keyValue];
00191                 }
00192                 if ( $overrideStartNode )
00193                     break;
00194             }
00195             if ( $overrideStartNode )
00196                 $parameters['start_node'] = $overrideStartNode;
00197         }
00198 
00199         if ( !isset( $parameters['persistent_data'] ) )
00200             $parameters['persistent_data'] = false;
00201 
00202         if ( !isset( $parameters['permission'] ) )
00203             $parameters['permission'] = false;
00204 
00205         if ( !isset( $parameters['top_level_nodes'] ) )
00206         {
00207             $parameters['top_level_nodes'] = $ini->variable( 'BrowseSettings', 'DefaultTopLevelNodes' );
00208             if ( $ini->hasVariable( $parameters['type'], 'TopLevelNodes' ) )
00209                 $parameters['top_level_nodes'] = $ini->variable( $parameters['type'], 'TopLevelNodes' );
00210         }
00211 
00212         if ( !is_numeric( $parameters['start_node'] ) )
00213             $parameters['start_node'] = eZContentBrowse::nodeAliasID( $parameters['start_node'] );
00214 
00215         for ( $i =0; $i < count( $parameters['top_level_nodes'] ); $i++ )
00216         {
00217             if ( !is_numeric( $parameters['top_level_nodes'][$i] ) )
00218                 $parameters['top_level_nodes'][$i] = eZContentBrowse::nodeAliasID( $parameters['top_level_nodes'][$i] );
00219         }
00220 
00221         if ( !isset( $parameters['cancel_page'] ) )
00222             $parameters['cancel_page'] = false;
00223 
00224         if ( !isset( $parameters['from_page'] ) )
00225         {
00226             eZDebug::writeError( $parameters, 'eZContentBrowse::browse() $parameters[\'from_page\'] is not set' );
00227         }
00228 
00229         $http = eZHTTPTool::instance();
00230         $http->setSessionVariable( 'BrowseParameters', $parameters );
00231 
00232         if ( is_null( $module ) )
00233         {
00234             return "/content/browse/";
00235         }
00236         else
00237         {
00238             $module->redirectTo( "/content/browse/" );
00239             return "/content/browse/";
00240         }
00241     }
00242 
00243     /*!
00244      \static
00245      \return the node ID for the node alias \a $nodeName or \c false if no ID could be found.
00246     */
00247     static function nodeAliasID( $nodeName )
00248     {
00249         if ( is_numeric( $nodeName ) )
00250             return $nodeName;
00251         $browseINI = eZINI::instance( 'browse.ini' );
00252         $aliasList = $browseINI->variable( 'BrowseSettings', 'AliasList' );
00253         if ( isset( $aliasList[$nodeName] ) )
00254             return $aliasList[$nodeName];
00255         $contentINI = eZINI::instance( 'content.ini' );
00256         if ( $nodeName == 'content' )
00257             return $contentINI->variable( 'NodeSettings', 'RootNode' );
00258         else if ( $nodeName == 'users' )
00259             return $contentINI->variable( 'NodeSettings', 'UserRootNode' );
00260         else if ( $nodeName == 'media' )
00261             return $contentINI->variable( 'NodeSettings', 'MediaRootNode' );
00262         else if ( $nodeName == 'setup' )
00263             return $contentINI->variable( 'NodeSettings', 'SetupRootNode' );
00264         else
00265             return false;
00266     }
00267 
00268     /*!
00269      Sets the node ID where browsing starts.
00270     */
00271     function setStartNode( $nodeID )
00272     {
00273         $this->Parameters['start_node'] = $nodeID;
00274     }
00275 
00276     /*!
00277      \static
00278      \return the result of the previous browse operation or \c false if no result was found.
00279              It uses the action name \a $actionName to determine which result to look for.
00280     */
00281     static function result( $actionName, $asObject = false )
00282     {
00283         $ini = eZINI::instance( 'browse.ini' );
00284         $isNodeSelection = $ini->variable( $actionName, 'ReturnType' ) == 'NodeID';
00285         if ( $isNodeSelection )
00286             $postName = 'SelectedNodeIDArray';
00287         else
00288             $postName = 'SelectedObjectIDArray';
00289         $http = eZHTTPTool::instance();
00290         if ( $http->hasPostVariable( $postName ) && !$http->hasPostVariable( 'BrowseCancelButton' ) )
00291         {
00292             $postList = $http->postVariable( $postName );
00293             $list = array();
00294             foreach ( $postList as $value )
00295             {
00296                 if ( !is_numeric( $value ) )
00297                 {
00298                     eZDebug::writeError( "Non-numeric value ($value) found for POST variable $postName for browse action '$actionName', the value will be excluded",
00299                                          'eZContentBrowse::result' );
00300                     continue;
00301                 }
00302                 // Append the value as a real integer, avoids XSS problems.
00303                 $intValue = (int)$value;
00304                 if ( $value != $intValue )
00305                 {
00306                     eZDebug::writeError( "Non-integer value ($value) found for POST variable $postName for browse action '$actionName', the value will be excluded",
00307                                          'eZContentBrowse::result' );
00308                     continue;
00309                 }
00310                 $list[] = $intValue;
00311             }
00312             return array_unique( $list );
00313         }
00314         return false;
00315     }
00316 
00317     /// \privatesection
00318     /// The browse parameters.
00319     public $Parameters = false;
00320 }
00321 
00322 ?>