Location: PHPKode > projects > EXtensible Production Suite (XPS) > xpsdev/core/controls/theme_control.php
<?php

/***********************************************************************************************************
*
* <p></p>
*
* @author heidtc < hide@address.com >
*
************************************************************************************************************/
Class Theme_Control
{
	private $Theme;
	private $Message_Control;
	private $Setting_Control;


	/***************************************************
	* <p>constructor for theme control.</p>
	*
	* @access   public
	* @author 	heidtc < hide@address.com >
	* @param	Object [$MC] The message object
	* @return	type description
	***************************************************/
	public function __construct($MC, $SC)
	{
		$this->Message_Control = $MC;
		$this->Setting_Control = $SC;

		require_once( MODELS . '/theme.php');
		$this->Theme = new Theme;
	}

	/***************************************************
	* <p>lists all themes</p>
	*
	* @access   public
	* @author 	heidtc < hide@address.com >
	* @return	type description
	***************************************************/
	public function list_all_themes($active_only=false)
	{
		$path = THEMES;
		$unallowed_directory_names = $this->Setting_Control->get_setting('unallowed_directory_names', 'core');
		$bad_file_names = explode('; ', $unallowed_directory_names);
		$theme_names = array();

		$dir_handle = opendir($path) or die($this->Message_Control->get_message('open_dir_fail'));
	 	while ($file = readdir($dir_handle))
	 	{
			if(!in_array($file, $bad_file_names))
	 		{
                if($active_only)
                {
                    if($this->is_theme_active($file)) array_push($theme_names, $file);
                }
                else
                {
                    array_push($theme_names, $file);
                }
	 	 	}
	 	}

	 	return $theme_names;
	}


	/***************************************************
	***************************************************/
	public function change_current_theme($theme_id, $params=array(), $DC)
	{
        $bool = true;
        $message = '';
        $error_count = 0;

        //If the user is trying to set his/her theme then execute the update query, else update settings.xml.
        if((isset($params['USER_ID'])) and ((!isset($params['IS_ADMIN']) or ($params['IS_ADMIN'] != 1))))
        {
			$bool = $DC->execute_sql('update_user_theme', 'core', array('USER_ID' => $params['USER_ID'], 'THEME' => $theme_id));
        }
        else
        {
            $bool = $this->Setting_Control->set_setting('current_theme', $theme_id, 'core');
        }

		if($bool)
		{
			$message .= $this->Message_Control->get_message('theme_change_complete','core');
            $_SESSION['theme'] = $params['default_theme'];
		}
		else
		{
			$message .= $this->Message_Control->get_message('theme_change_failed','core');
            $error_count ++;
		}
		$message .= $theme_id;
		return array('message' => $message, 'error_count' => $error_count);
	}

    public function activate_theme($theme_id)
    {
        if(file_exists(THEMES . '/' . $theme_id))
        {
            $fp = fopen(THEMES . '/' . $theme_id . '/_active', 'w');
            fwrite($fp, '_');
            fclose($fp);
            return true;
        }
        return false;

    }

    public function deactivate_theme($theme_id)
    {
        if(file_exists(THEMES . '/' . $theme_id . '/_active'))
        {
            unlink(THEMES . '/' . $theme_id . '/_active');
            return true;
        }
        return false;
    }

    public function is_theme_active($theme_id)
    {
        if(file_exists(THEMES . '/' . $theme_id . '/_active'))
        {
            return 'checked';
        }
        return '';
    }

    public function is_current_theme($theme_id)
    {
        //See if a normal user is logged in and if so then simply test the $_SESSION.
        if(isset($_SESSION['theme']) and ($_SESSION['is_admin'] != 1))
        {
            if($theme_id == $_SESSION['theme'])
            {
                return 'checked';
            }
        }
        else
        {
            if($theme_id == $this->Setting_Control->get_setting('current_theme'))
            {
                return 'checked';
            }
        }
        return '';
    }
}
Return current item: EXtensible Production Suite (XPS)