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

class LinkController extends Controller {
	public function addLink() {
		//var_dump($_POST);
		$link                = M('links');
		$data['description'] = $_POST['description'];
		$data['link']        = $_POST['link'];
		$data['approve']     = $_POST['audit'];
		$data['userid']      = $_POST['userid'];
		if ($lastid = $link->data($data)->add() != 0) {
			$this->success('成功添加');
		}
	}
	public function listLinks($type = null) {
		$link = M('links');
		if (null == $type) {
			return $array = $link->where('userid=' . $_SESSION['userid'])->select();
		} elseif ('audit' == $type) {
			return $array = $link->where('approve=0 userid=' . $_SESSION['userid'])->select();
		}

	}
	public function auditLink() {
		$link            = M('links');
		$data['approve'] = $_GET['audit'];
		$link->where('id=' . $_GET['id'])->save($data);
		echo $link->getlastsql();

	}
	public function editLink() {
		$link                = M('links');
		$data['description'] = $_POST['description'];
		$data['link']        = $_POST['link'];
		if (($i = $link->where('id=' . $_GET['id'])->save($data)) != 0) {
			echo json_encode($link->where('id=' . $_GET['id'])->find());
		}

	}
	public function delLink() {
		$link = M('links');
		if (($i = $link->where('id=' . $_GET['id'])->delete()) != 0) {
			$this->show("删除成功");
		}
	}
}
