<?php
class SaveTheme extends XCube_ActionFilter
{
  function postFilter()
  {
    if ($this->mRoot->mContext->mUser->isInRole('Site.RegisteredUser')) {
      $xUser =& $this->mRoot->mContext->mXoopsUser;
      if ( isset($_SESSION['xoopsUserTheme']) && is_object($xUser) && $xUser->get('theme') != $_SESSION['xoopsUserTheme'] ) {
        $handler =& xoops_gethandler('user');
        $edituser =& $handler->get($xUser->get('uid'));
        $edituser->set('theme', $_SESSION['xoopsUserTheme']);
        $handler->insert($edituser, true);
      }
    } else {
      if ( isset($_SESSION['xoopsUserTheme']) ) {
        if ( isset($_COOKIE['MySelectTheme']) && $_COOKIE['MySelectTheme'] != $_SESSION['xoopsUserTheme'] ) {
          $patha = explode($_SERVER['HTTP_HOST'], XOOPS_URL);
          $path = $patha[1].'/';
          setcookie ('MySelectTheme', $_SESSION['xoopsUserTheme'], time() + 86400 * 14, $path);
        }
      } elseif ( isset($_COOKIE['MySelectTheme']) && !is_array($_COOKIE['MySelectTheme']) ) {
        if ( in_array($_COOKIE['MySelectTheme'], $this->mRoot->mContext->getXoopsConfig('theme_set_allowed')) ) {
          $this->mRoot->mContext->setThemeName($_COOKIE['MySelectTheme']);
          $_SESSION['xoopsUserTheme'] = $_COOKIE['MySelectTheme'];
        }
      }
    }
  }
}
?>