[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/kernel/classes/ -> ezpreferences.php (source)

   1  <?php
   2  //
   3  // Definition of eZPreferences class
   4  //
   5  // Created on: <11-Aug-2003 13:23:55 bf>
   6  //
   7  // SOFTWARE NAME: eZ publish
   8  // SOFTWARE RELEASE: 3.9.0
   9  // BUILD VERSION: 17785
  10  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
  11  // SOFTWARE LICENSE: GNU General Public License v2.0
  12  // NOTICE: >
  13  //   This program is free software; you can redistribute it and/or
  14  //   modify it under the terms of version 2.0  of the GNU General
  15  //   Public License as published by the Free Software Foundation.
  16  //
  17  //   This program is distributed in the hope that it will be useful,
  18  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  19  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20  //   GNU General Public License for more details.
  21  //
  22  //   You should have received a copy of version 2.0 of the GNU General
  23  //   Public License along with this program; if not, write to the Free
  24  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25  //   MA 02110-1301, USA.
  26  //
  27  //
  28  
  29  /*! \file ezpreferences.php
  30  */
  31  
  32  /*!
  33    \class eZPreferences ezpreferences.php
  34    \brief Handles user/session preferences
  35  
  36    Preferences can be either pr user or pr session. eZPreferences will automatically
  37    set a session preference if the user is not logged in, if not a user preference will be set.
  38  
  39  */
  40  
  41  include_once ( 'kernel/classes/datatypes/ezuser/ezuser.php' );
  42  include_once ( "lib/ezdb/classes/ezdb.php" );
  43  
  44  define( 'EZ_PREFERENCES_SESSION_NAME', 'eZPreferences' );
  45  
  46  class eZPreferences
  47  {
  48      function eZPreferences()
  49      {
  50      }
  51  
  52      /*!
  53       \static
  54       Sets a preference value for a given user. If
  55       the user is anonymous the value is only stored in session.
  56  
  57       \param $name The name of the preference to store
  58       \param $value The value of the preference to store
  59       \param $storeUserID The user which should get the preference,
  60                           if \c false it will use the current user
  61       \return \c true if the preference was stored correctly or \c false if something went wrong
  62       \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
  63       the calls within a db transaction; thus within db->begin and db->commit.
  64      */
  65      function setValue( $name, $value, $storeUserID = false )
  66      {
  67          $db =& eZDB::instance();
  68          $name = $db->escapeString( $name );
  69          $value = $db->escapeString( $value );
  70  
  71          $isCurrentUser = true;
  72          if ( $storeUserID === false )
  73          {
  74              $user =& eZUser::currentUser();
  75          }
  76          else
  77          {
  78              $currentID = eZUser::currentUserID();
  79              if ( $currentID != $storeUserID )
  80                  $isCurrentUser = false;
  81  
  82              $user = eZUser::fetch( $storeUserID );
  83              if ( !is_object( $user ) )
  84              {
  85                  eZDebug::writeError( "Cannot set preference for user $storeUserID, the user does not exist" );
  86                  return false;
  87              }
  88          }
  89  
  90          // We must store the database changes if:
  91          // a - The current user is logged in (ie. not anonymous)
  92          // b - We have specified a specific user (not the current).
  93          //    in which case isLoggedIn() will fail.
  94          if ( $storeUserID !== false or $user->isLoggedIn() )
  95          {
  96              // Only store in DB if user is logged in or we have
  97              // a specific user ID defined
  98              $userID = $user->attribute( 'contentobject_id' );
  99              $existingRes = $db->arrayQuery( "SELECT * FROM ezpreferences WHERE user_id = $userID AND name='$name'" );
 100  
 101              if ( count( $existingRes ) > 0 )
 102              {
 103                  $prefID = $existingRes[0]['id'];
 104                  $query = "UPDATE ezpreferences SET value='$value' WHERE id = $prefID AND name='$name'";
 105                  $db->query( $query );
 106              }
 107              else
 108              {
 109                  $query = "INSERT INTO ezpreferences ( user_id, name, value ) VALUES ( $userID, '$name', '$value' )";
 110                  $db->query( $query );
 111              }
 112          }
 113  
 114          // We also store in session if this is the current user (anonymous or normal user)
 115          if ( $isCurrentUser )
 116          {
 117              eZPreferences::storeInSession( $name, $value );
 118          }
 119  
 120          return true;
 121      }
 122  
 123      /*!
 124       \static
 125       \param $user The user object to read preferences for, if \c false it will read using the current user.
 126       \return The preference value for the specified user.
 127               If no variable is found \c false is returned.
 128       \note The preferences variable will be stored in session after fetching
 129             if the specified user is the current user.
 130      */
 131      function value( $name, $user = false )
 132      {
 133          if ( get_class( $user ) != 'ezuser' )
 134              $user =& eZUser::currentUser();
 135  
 136          $value = false;
 137          // If the user object is not the currently logged in user we cannot use the session values
 138          $http =& eZHTTPTool::instance();
 139          $useCache = ( $user->ContentObjectID == $http->sessionVariable( 'eZUserLoggedInID' ) );
 140          if ( $useCache and eZPreferences::isStoredInSession( $name ) )
 141              return eZPreferences::storedSessionValue( $name );
 142  
 143          // If this the anonymous user we should return false, no need to check database.
 144          if ( $user->isAnonymous() )
 145              return false;
 146  
 147          $db =& eZDB::instance();
 148          $name = $db->escapeString( $name );
 149          $userID = $user->attribute( 'contentobject_id' );
 150          $existingRes = $db->arrayQuery( "SELECT value FROM ezpreferences WHERE user_id = $userID AND name = '$name'" );
 151  
 152          if ( count( $existingRes ) == 1 )
 153          {
 154              $value = $existingRes[0]['value'];
 155              if ( $useCache )
 156                  eZPreferences::storeInSession( $name, $value );
 157          }
 158          else
 159          {
 160              if ( $useCache )
 161                  eZPreferences::storeInSession( $name, false );
 162          }
 163          return $value;
 164      }
 165  
 166      /*!
 167       \static
 168       \param $user The user object to read preferences for, if \c false it will read using the current user.
 169       \return An array with all the preferences for the specified user.
 170               If the user is not logged in the empty array will be returned.
 171      */
 172      function values( $user = false )
 173      {
 174          if ( get_class( $user ) != 'ezuser' )
 175              $user =& eZUser::currentUser();
 176  
 177          if ( !$user->isAnonymous() )
 178          {
 179              // If the user object is not the currently logged in user we cannot use the session values
 180              $http =& eZHTTPTool::instance();
 181              $useCache = ( $user->ContentObjectID == $http->sessionVariable( 'eZUserLoggedInID' ) );
 182  
 183              $returnArray = array();
 184              $userID = $user->attribute( 'contentobject_id' );
 185              $db =& eZDB::instance();
 186              $values = $db->arrayQuery( "SELECT name,value FROM ezpreferences WHERE user_id=$userID ORDER BY id" );
 187              foreach ( $values as $item )
 188              {
 189                  if ( $useCache )
 190                      eZPreferences::storeInSession( $item['name'], $item['value'] );
 191                  $returnArray[$item['name']] = $item['value'];
 192              }
 193              return $returnArray;
 194          }
 195          else
 196          {
 197              // For the anonymous user we just return all values
 198              $http =& eZHTTPTool::instance();
 199              if ( $http->hasSessionVariable( EZ_PREFERENCES_SESSION_NAME ) )
 200                  return $http->sessionVariable( EZ_PREFERENCES_SESSION_NAME );
 201              return array();
 202          }
 203      }
 204  
 205      /*!
 206       \static
 207       Makes sure the stored session values are cleaned up.
 208      */
 209      function sessionCleanup()
 210      {
 211          $http =& eZHTTPTool::instance();
 212          $http->removeSessionVariable( EZ_PREFERENCES_SESSION_NAME );
 213      }
 214  
 215      /*!
 216       \static
 217       Makes sure the preferences named \a $name is stored in the session with the value \a $value.
 218      */
 219      function storeInSession( $name, $value )
 220      {
 221          $http =& eZHTTPTool::instance();
 222          $preferencesInSession = array();
 223          if ( $http->hasSessionVariable( EZ_PREFERENCES_SESSION_NAME ) )
 224               $preferencesInSession =& $http->sessionVariable( EZ_PREFERENCES_SESSION_NAME );
 225          $preferencesInSession[$name] = $value;
 226          $http->setSessionVariable( EZ_PREFERENCES_SESSION_NAME, $preferencesInSession );
 227      }
 228  
 229      /*!
 230       \static
 231       \return \c true if the preference named \a $name is stored in session.
 232      */
 233      function isStoredInSession( $name )
 234      {
 235          $http =& eZHTTPTool::instance();
 236          if ( !$http->hasSessionVariable( EZ_PREFERENCES_SESSION_NAME ) )
 237              return false;
 238          $preferencesInSession =& $http->sessionVariable( EZ_PREFERENCES_SESSION_NAME );
 239          return array_key_exists( $name, $preferencesInSession );
 240      }
 241  
 242      /*!
 243       \static
 244       \return the stored preferenced value found in the session or \c null if none were found.
 245      */
 246      function storedSessionValue( $name )
 247      {
 248          $http =& eZHTTPTool::instance();
 249          if ( !$http->hasSessionVariable( EZ_PREFERENCES_SESSION_NAME ) )
 250              return null;
 251          $preferencesInSession =& $http->sessionVariable( EZ_PREFERENCES_SESSION_NAME );
 252          if ( !array_key_exists( $name, $preferencesInSession ) )
 253              return null;
 254          return $preferencesInSession[$name];
 255      }
 256  
 257      /*!
 258       \static
 259       Removes all preferences for all users.
 260       \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
 261       the calls within a db transaction; thus within db->begin and db->commit.
 262      */
 263      function cleanup()
 264      {
 265          $db =& eZDB::instance();
 266          $db->query( "DELETE FROM ezpreferences" );
 267      }
 268  }
 269  
 270  
 271  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7