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