<?php namespace Admin\Controller;
use Think\Controller;

class SettingsController extends Controller {
	function getSettings() {
		$settings = M('users');
		//var_dump($settings->where('id='.$_SESSION['userid'])->select());
		return $settings->where('id=' . $_SESSION['userid'])->find();
	}
	function updataSettings() {
		$settings                = M('users');
		$data['email']           = $_POST['email'];
		$data['nickname']        = $_POST['nickname'];
		$data['blogname']        = $_POST['blogname'];
		$data['blogdescription'] = $_POST['blogdescription'];
		$data['userurl']         = $_POST['userurl'];
		$data['theme']           = $_POST['themes'];
		if ($settings->where('id=' . $_SESSION['userid'])->save($data) != 0) {$this->success('更改成功');} else { $this->show($settings->getLastsql());}
	}

	function listThemes() {
		$arr = array();
		$dir = opendir("./Application/Home/View/");
		while (($file = readdir($dir)) !== false) {
			if ("." != $file && ".." != $file) {
				array_push($arr, $file);
			}

		}
		return $arr;

	}

	function setPassword() {
		$settings = M('users');
		$arr      = $settings->where('id=' . $_SESSION['userid'])->find();
		if (md5($_POST['oldpwd']) != $arr['password']) {
			$this->error('旧的密码不匹配');

		} else {
			$data['password'] = md5($_POST['newpwd']);
			if ($settings->where('id=' . $_SESSION['userid'])->save($data) != 0) {$this->success('更改成功');} else { $this->show($settings->getLastsql());}
		}
	}
}