<?php
include('action.abs.php');
class celebrity extends action {
		var $db = null;
		function celebrity() {
			parent :: action();
			$this->db = APP :: ADP('db');
			$this->db->setTable(T_CELEBRITY);
		}

		function add() {
			if ($this->_isPost()) {
				$nick = trim(V('p:nick'));
				$url = trim(V('p:url'));
				$description = trim(V('p:description', ''));
				$orders = trim(V('p:orders', 0));
				$image = V('p:image', '');
				
				if (!$nick && !$url) {
					$this->_error('请填写昵称和URL', array('add'));
				}
				/*
				if (!ereg('^http://', $url)) {
					$url = 'http://' . $url;
				}*/
				if (!preg_match('|ta[/&]id[\-=]\d+$|',$url)) {
					$this->_error('请填写正确的推荐地址', array('add'));
				}
				if (!$image) {
					$wb = APP::N('weibo');
					$AdminUser = APP::N('adminUser', 'admin_id');
					$info = $AdminUser->getInfo();
					$wb->setToken($info['oauth_token'],$info['oauth_token_secret']);
					//$wb->setToken(WB_USER_OAUTH_TOKEN, WB_USER_OAUTH_TOKEN_SECRET);
					$info = $wb->getUserShow('', '', $nick);
					if (!isset($info['error'])) {
						$image = $info['profile_image_url'];
					} else {
						$image = '';
					}
				}
				
				$data = array(
							'nick' => mysql_escape_string($nick),
							'url' => mysql_escape_string($url),
							'description' => mysql_escape_string($description),
							'orders' => mysql_escape_string($orders),
							'operator' => (int)$this->_getUid(),
							'add_time' => time(),
							'image' => $image,
							'province' => $info['province'],
							'city' => $info['city'],
							'sina_uid' => $info['id']
						);

				$this->db->save($data);
				$this->_succ('已成功添加', array('celebrityList'));
			}
			$this->_display('celebrity/celebrity_add');
		}

		function celebrityList() {
			$page = V('g:page', 1);
			$each = V('g:each', 20);
			$keyword = V('g:keyword', '');
			$offset = ($page -1) * $each;
			if ($keyword) {
				$where = ' WHERE `nick` LIKE "%' . $keyword . '%"' . ' OR `url` LIKE "%' . $keyword. '%"';
				if (0 != (int)$keyword) {
					$where .=  ' OR sina_uid="' . $keyword . '"';
				}
			}
			$select_count = 'SELECT COUNT(*) FROM ' . $this->db->getTable(T_CELEBRITY) . $where;
			$count = $this->db->getOne($select_count);
			$pager = APP :: N('pager');
			$page_param = array('currentPage'=> $page, 'pageSize' => $each, 'recordCount' => $count, 'linkNumber' => 10);
			$pager->setParam($page_param);
			TPL :: assign('pager', $pager->makePage());

			$sql = 'SELECT a.*,b.name FROM ' . $this->db->getTable(T_CELEBRITY) . ' AS a LEFT JOIN ' . $this->db->getTable(T_ADMIN_USER) . ' AS b ON a.operator=b.id  ' . $where . ' ORDER BY a.orders ASC,a.id DESC LIMIT ' . $offset . ',' . $each;
			$rs = $this->db->query($sql);
			for ($i=0, $count=count($rs); $i < $count; $i++) {
				if (preg_match('/\d+$/', $rs[$i]['url'], $match)) {
					$rs[$i]['url'] = URL('ta', 'id=' . $match[0]);
				} else {
					$rs[$i]['url'] = URL('ta', 'id=' . $rs[$i]['sina_uid'], 'index.php');
				}
			}
			TPL :: assign('list', $rs);

			$this->_display('celebrity/celebrity_list');
		}

		function getInfo() {
			$nick = trim(V('r:nick'));
			$wb = APP::N('weibo');
			$AdminUser = APP::N('adminUser', 'admin_id');
			$info = $AdminUser->getInfo();
			$wb->setToken($info['oauth_token'],$info['oauth_token_secret']);
			//$wb->setToken(WB_USER_OAUTH_TOKEN, WB_USER_OAUTH_TOKEN_SECRET);
			$info = $wb->getUserShow('', '', $nick, false);
			if (!isset($info['error'])) {
				$url = URL('ta', 'id=' . $info['id'], 'index.php');
			} else {
				$url = '';
			}
			echo '{"url": "' . $url .'","description":"' . $info['description']. '"}';
		}

		function edit() {
				if ($this->_isPost()) {
					$id = (int)V('p:id');
					
					$orders = trim(V('p:orders', 0));
				
					$data = array(
								'orders' => mysql_escape_string($orders),
								'operator' => (int)$this->_getUid(),
						    );

					$this->db->save($data, $id);
					$this->_succ('已成功保存', array('celebrityList'));
				}
				$id = V('g:id');
				$rs = $this->db->get($id);
				if (preg_match('/\d+$/', $rs['url'], $match)) {
					$rs['url'] = URL('ta', 'id=' . $match[0]);
				} else {
					$rs['url'] = '';
				}
				TPL :: assign('info', $rs);
				$this->_display('celebrity/celebrity_add');
		}

		function delete() {
				$id = V('r:id');
				if (is_string($id)) {
					$id = trim($id, ', ');
					if (strpos(',', $id)) {
						$id = explode(',', $id);
					}
				}
				
				$this->db->delete($id);
				//$this->_succ('已成功删除', array('celebrityList'));
				$this->_redirect('celebrityList');
		}

}
