<?php
/*
+--------------------------------------------------------------------------
|   TanyaCMS - Question and Answer Sofware
|   -----------------------------------------------------------------------
|   by fbschool dev team
|   (c) 2011 - 2012 FBSCHOOL
|   http://www.fbschool.com/tanyacms
|   -----------------------------------------------------------------------
|   This is NOT a freeware, use is subject to license terms
|   Support: 13455n@gmail.com
+--------------------------------------------------------------------------
*/


if (!defined('IN_TANYACMS'))
{
	die;
}

class setting_class extends ASK_MODEL
{
	function get_setting($where = null)
	{
		if ($system_setting = $this->fetch_all('system_setting', $where))
		{
			foreach ($system_setting as $key => $val)
			{
				$setting[$val['varname']] = unserialize($val['value']);				
			}
			
			return $setting;
		}
		else
		{
			return false;
		}
	}
	
	/**
	 * Check the filter system identification parameters
	 * @param unknown_type $input
	 */
	function check_vars($input)
	{
		if (empty($input))
		{
			return false;
		}
		
		$r_vars = array();
		
		foreach ($input as $key => $val)
		{
			if (in_array($key, array_keys(ASK_APP::setting())))
			{
				$r_vars[$key] = $val;
			}
		}
		
		return $r_vars;
	}

	/**
	 * Save setting parameters
	 * @param unknown_type $vars
	 */
	function set_vars($vars)
	{
		if (empty($vars))
		{
			return false;
		}
		
		foreach ($vars as $key => $val)
		{
			$this->update('system_setting', array(
				'value' => serialize($val)
			), "varname = '" . $this->quote($key) . "'");
		}
		
		return true;
	}

	public function get_ui_styles()
	{
		$exclude_dir = array(
			'.', 
			'..', 
			'.svn'
		);
		
		$dirs = array();
		
		if ($handle = opendir(ROOT_PATH . 'views'))
		{
			while (false !== ($file = readdir($handle)))
			{
				if (! in_array($file, $exclude_dir) && is_dir('./views/' . $file))
				{
					$dirs[] = $file;
				}
			}
			
			closedir($handle);
		}
		
		$ui_style = array();
		
		foreach ($dirs as $key => $val)
		{
			$ui_style[] = array(
				'id' => $val, 
				'title' => $val
			);
		}
		
		return $ui_style;
	}
}
