[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/summary/action/ -> summaryaction.class.php (source)

   1  <?php
   2      lt_include( PLOG_CLASS_PATH."class/action/action.class.php" );
   3      lt_include( PLOG_CLASS_PATH."class/summary/view/summaryview.class.php" );
   4      lt_include( PLOG_CLASS_PATH."class/summary/view/summarycachedview.class.php" );    
   5      lt_include( PLOG_CLASS_PATH."class/net/http/httpvars.class.php" );
   6  
   7      define('HTTP_ACCEPT_LANGUAGE_DETECTION', 0);
   8  
   9      /**

  10       * Base action that all summary actions should extend

  11       */
  12      class SummaryAction extends Action
  13      {
  14          
  15          var $_config;
  16          var $_locale;
  17          var $_userInfo;
  18          
  19  		function SummaryAction( $actionInfo, $request )
  20          {
  21              $this->Action( $actionInfo, $request );
  22              $this->_config   =& Config::getConfig();
  23              $this->_locale   =& $this->_loadLocale();
  24              
  25              // load userinfo data if any

  26              $this->_userInfo = SessionManager::getUserInfoFromSession();
  27          }
  28          
  29          /**

  30           * Loads the current locale. It works so that it tries to fetch the parameter "lang" from the

  31           * request. If it's not available, then it will try to look for it in the session. If it is not

  32           * there either, it will try to guess the most prefered language according to what the User Agent 

  33           * included in the HTTP_ACCEPT_LANGUAGE string sent with the request. If none matches available 

  34           * languages we have to use the value of "default_locale" and display the default language.

  35           *

  36           * @private

  37           * @return Returns a reference to a Locale object

  38           */
  39          function &_loadLocale()
  40          {
  41              $requestLocale = $this->_request->getValue( "lang" );
  42              $localeCode = "";
  43              $serverVars = HttpVars::getServer();
  44  
  45              // check if there's something in the request... 

  46              // if not, check the session or at least try to 

  47              // guess the apropriate language from the http_accept_language string

  48              if( $requestLocale ) {
  49                  // check if it's a valid one

  50                  if( Locales::isValidLocale( $requestLocale )) {
  51                      $localeCode = $requestLocale;
  52                  }
  53              }
  54              else {
  55                  $sessionLocale = SessionManager::getSessionValue( "summaryLang" );                
  56                  if( $sessionLocale ) {
  57                      $localeCode = $sessionLocale;
  58                  }
  59                  elseif ( $this->_config->getValue( "use_http_accept_language_detection", HTTP_ACCEPT_LANGUAGE_DETECTION) == 1 )
  60                  {
  61                      $localeCode = $this->_matchHttpAcceptLanguages( $serverVars['HTTP_ACCEPT_LANGUAGE'] );
  62                  }
  63              }
  64              
  65              // check if the locale code is correct

  66              // and as a valid resort, use the default one if the locale ist not valid or 'false'

  67              if( $localeCode === false || !Locales::isValidLocale( $localeCode ) ) {
  68                  $localeCode = $this->_config->getValue( "default_locale" );
  69              }
  70              
  71              // now put whatever locale value back to the session

  72              SessionManager::setSessionValue( "summaryLang", $localeCode );
  73              
  74              // load the correct locale

  75              $locale =& Locales::getLocale( $localeCode );            
  76              
  77              return( $locale );
  78          }
  79          
  80          /**

  81           * Common things that should be performed by all SummaryAction classes

  82           */
  83  		function setCommonData( $copyFormValues = false )
  84          {
  85              parent::setCommonData( $copyFormValues );
  86              
  87              $this->_view->setValue( "locale", $this->_locale );
  88              $this->_view->setValue( "authuser", $this->_userInfo );
  89          }
  90          
  91          /**

  92           * Tries to match the prefered language out of the http_accept_language string 

  93           * with one of the available languages.

  94           *

  95           * @private

  96           * @param httpAcceptLanguage the value of $_SERVER['HTTP_ACCEPT_LANGUAGE']

  97           * @return Returns returns prefered language or false if no language matched.

  98           */
  99          function _matchHttpAcceptLanguages(&$httpAcceptLanguage)
 100          {
 101              $acceptedLanguages = explode( ',', $httpAcceptLanguage );
 102              $availableLanguages = Locales::getAvailableLocales();
 103              $primaryLanguageMatch = '';
 104  
 105              // we iterate through the array of languages sent by the UA and test every single 

 106              // one against the array of available languages

 107              foreach($acceptedLanguages as $acceptedLang)
 108              {   
 109                  // clean the string and strip it down to the language value (remove stuff like ";q=0.3")

 110                  $acceptedLang = substr( $acceptedLang, 0, strcspn($acceptedLang, ';') );
 111  
 112                  if (strlen($acceptedLang) > 2)
 113                  {
 114                      // cut to primary language

 115                      $primaryAcceptedLang = substr($acceptedLang, 0, 2);
 116                  }
 117                  else
 118                  {
 119                      $primaryAcceptedLang = $acceptedLang;
 120                  }
 121                  
 122                  // this is where we start to iterate through available languages

 123                  foreach($availableLanguages as $availableLang)
 124                  {     
 125                      if( stristr($availableLang, $acceptedLang) !== false )
 126                      {
 127                          // we have a exact language match

 128                          return $availableLang;
 129                      }
 130                      elseif ( stristr($availableLang, $primaryAcceptedLang) !== false && $primaryLanguageMatch == '')
 131                      {
 132                          // we found the first primary language match!

 133                          $primaryLanguageMatch = $availableLang;
 134                      }
 135                  }
 136              } // foreach

 137  
 138              if ($primaryLanguageMatch != '')
 139              {
 140                  return $primaryLanguageMatch;
 141              }
 142              else
 143              {
 144                  return false;
 145              }
 146          }
 147  
 148      }
 149      
 150  ?>


Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics