<?php

/**
 *--------------------------------------
 * 单页
 *--------------------------------------
 * @project		: uwa
 * @author		: cblee
 * @created		: 2012-10-19
 * @copyright	: (c)2012 AsThis
 *--------------------------------------
 */

class SinglePageCtrlr extends ManageCtrlr
{
	public function list_single_page()
	{
		/* 获取分页信息 */
		$_GET[C('VAR.PAGE')] = ARequest::get(C('VAR.PAGE')) ? ARequest::get(C('VAR.PAGE')) : 1;
		$rowsNum = M('SinglePage')->count();
		$p = new APage($rowsNum, 20, Url::U('single_page/list_single_page?'.C('VAR.PAGE').'=_page_'));
		$this->assign('PAGE_LIST', $p->show());
		$limit = $p->get_limit();

		$_SPL = M('SinglePage')->get_singlePageList('', '`sp_display_order` ASC', $limit);
		$this->assign('_SPL', $_SPL);

		$this->display();
	}

	public function add_single_page()
	{
		$this->display();
	}
	public function add_single_page_do()
	{
		$data = ARequest::get();
		$data['sp_edit_time'] = time();

		$result = M('SinglePage')->add_single_page($data);
		if(!empty($result['error']))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('ADD').L('SINGLE_PAGE').': '.$result['error'], 0);
			$this->error($result['error'], Url::U('single_page/list_single_page'));
		}

		M('AdminLog')->add_log(ASession::get('m_userid'), L('ADD').L('SINGLE_PAGE').': SINGLE_PAGE_ID['.$result['data'].']');
		$this->success(L('ADD_SUCCESS'), Url::U('single_page/list_single_page'));
	}

	public function edit_single_page()
	{
		$singlePageId = ARequest::get('single_page_id');

		$_SPI = M('SinglePage')->get_singlePageInfo($singlePageId);
		if(empty($_SPI))
		{
			$this->error(L('ITEM_NOT_EXIST'), Url::U('single_page/list_single_page'));
		}
		$this->assign('_SPI', $_SPI);

		$this->display();
	}
	public function edit_single_page_do()
	{
		$data = ARequest::get();
		$data['sp_edit_time'] = time();

		$result = M('SinglePage')->edit_single_page($data);
		if(!empty($result['error']))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('SINGLE_PAGE').': SINGLE_PAGE_ID['.$data['single_page_id'].']'.$result['error'], 0);
			$this->error($result['error'], Url::U('single_page/list_single_page'));
		}
		/* 立即更新 */
		if(isset($data['update_now']) and 1 == $data['update_now'])
		{
			M('SinglePage')->update_url($data['single_page_id']);
			ARequest::set('single_page_id', $data['single_page_id']);
			$_o = M('Option')->get_option('core');
			if($_o['html_switch'])
			{
				$this->update_html_do();
			}
		}
		M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('SINGLE_PAGE').': SINGLE_PAGE_ID['.$data['single_page_id'].']');
		$this->success(L('EDIT_SUCCESS'), Url::U('single_page/list_single_page'));
	}

	/* 快速更新 */
	public function update_single_page_do()
	{
		$singlePageId = ARequest::get('single_page_id');
		$_L_ID = is_array($singlePageId) ? implode(',', $singlePageId) : $singlePageId; /* 日志记录ID */

		if(is_array($singlePageId) && empty($singlePageId))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('SINGLE_PAGE').': '.L('ITEM_NOT_EXIST'), 0);
			$this->error(L('ITEM_NOT_EXIST'), Url::U('single_page/list_single_page'));
		}

		$spDisplayOrder = ARequest::get('sp_display_order');
		$data = array();
		foreach($singlePageId as $k => $id)
		{
			$data['single_page_id'] = $id;
			$data['sp_display_order'] = $spDisplayOrder[$k];
			$result = M('SinglePage')->edit_single_page($data);
			if(!empty($result['error']))
			{
				M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('SINGLE_PAGE').': SINGLE_PAGE_ID['.$singlePageId.']'.$result['error'], 0);
				$this->error($result['error'], Url::U('single_page/list_single_page'));
			}
		}
		/* 记录日志 */
		M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('SINGLE_PAGE').': SINGLE_PAGE_ID['.$_L_ID.']');
		$this->success(L('EDIT_SUCCESS'), Url::U('single_page/list_single_page'));
	}

	public function delete_single_page_do()
	{
		$singlePageId = ARequest::get('single_page_id');
		$_L_ID = is_array($singlePageId) ? implode(',', $singlePageId) : $singlePageId; /* 日志记录ID */

		if(is_array($singlePageId))
		{
			foreach($singlePageId as $singlePageId)
			{
				$result = M('SinglePage')->delete_single_page($singlePageId);
				if(!empty($result['error']))
				{
					M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('SINGLE_PAGE').': SINGLE_PAGE_ID['.$singlePageId.']'.$result['error'], 0);
					$this->error($result['error'], Url::U('single_page/list_single_page'));
				}
			}
		}
		else
		{
			$result = M('SinglePage')->delete_single_page($singlePageId);
			if(!empty($result['error']))
			{
				M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('SINGLE_PAGE').': SINGLE_PAGE_ID['.$singlePageId.']'.$result['error'], 0);
				$this->error($result['error'], Url::U('single_page/list_single_page'));
			}
		}

		/* 记录日志 */
		M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('SINGLE_PAGE').': SINGLE_PAGE_ID['.$_L_ID.']');
		$this->success(L('DELETE_SUCCESS'), Url::U('single_page/list_single_page'));
	}

	public function update_url_do()
	{
		$singlePageId = ARequest::get('single_page_id');
		$_L_ID = is_array($singlePageId) ? implode(',', $singlePageId) : $singlePageId; /* 日志记录ID */

		if(is_array($singlePageId))
		{
			foreach($singlePageId as $singlePageId)
			{
				$result = M('SinglePage')->update_url($singlePageId);
				if(!empty($result['error']))
				{
					M('AdminLog')->add_log(ASession::get('m_userid'), L('UPDATE_SINGLE_PAGE_URL').': SINGLE_PAGE_ID['.$singlePageId.']'.$result['error'], 0);
					$this->error($result['error'], Url::U('single_page/list_single_page'));
				}
			}
		}
		else
		{
			$result = M('SinglePage')->update_url($singlePageId);
			if(!empty($result['error']))
			{
				M('AdminLog')->add_log(ASession::get('m_userid'), L('UPDATE_SINGLE_PAGE_URL').': SINGLE_PAGE_ID['.$singlePageId.']'.$result['error'], 0);
				$this->error($result['error'], Url::U('single_page/list_single_page'));
			}
		}

		/* 记录日志 */
		M('AdminLog')->add_log(ASession::get('m_userid'), L('UPDATE_SINGLE_PAGE_URL').': SINGLE_PAGE_ID['.$_L_ID.']');
		$this->success(L('UPDATE_SUCCESS'), Url::U('single_page/list_single_page'));
	}

	/* 生成单页 */
	public function update_html_do()
	{
		$_o = M('Option')->get_option('core');
		if(!$_o['html_switch'])
		{
			return;
		}

		/* 站点信息 */
		$_SITE = M('Option')->get_option('site');
		unset($_SITE['theme']);
		$this->assign('_SITE', $_SITE);
		/* 设置 */
		$_G = M('Option')->get_option(array(
			'upload',
			'interaction',
			'member'
		));
		$this->assign('_G', $_G);

		set_time_limit(99999999);
		$this->display('admin/build/progress');

		$singlePageId = ARequest::get('single_page_id');

		/* 记录日志 */
		$_L_ID = is_array($singlePageId) ? implode(',', $singlePageId) : $singlePageId; /* 日志记录ID */
		M('AdminLog')->add_log(ASession::get('m_userid'), L('UPDATE_SINGLE_PAGE_HTML').': SINGLE_PAGE_ID['.$_L_ID.']');

		if(!is_array($singlePageId))
		{
			$singlePageId = explode(',', $singlePageId);
		}
		sort($singlePageId);
		$rowNum = count($singlePageId);
		foreach($singlePageId as $key => $singlePageId)
		{
			$progress = round(($key + 1) / $rowNum * 100, 1);

			$_V = M('SinglePage')->get_singlePageInfo($singlePageId);
			if(empty($_V) or empty($_V['sp_html_naming']))
			{
				M('Build')->show_progress($progress.'% ['.($key + 1).'/'.$rowNum.']: '.L('SKIP'), $progress);
				continue;
			}
			$this->assign('_V', $_V);

			$this->assign('POSITION', '<a href="'.__APP__.'">'.L('HOME').'</a> &raquo; '.$_V['sp_title']);

			$_C = require(CFG_PATH.D_S.'comm.php');
			$this->te->tplTheme = $_C['TE']['TPL_THEME'];
			$this->build_html($_V['sp_html_naming'], APP_PATH, 'home/'.$_V['sp_tpl']);
			$this->te->tplTheme = 'default';

			M('Build')->show_progress($progress.'% ['.($key + 1).'/'.$rowNum.']: '.$_V['sp_title'].' '.L('UPDATE_COMPLETE'), $progress);
		}

		set_time_limit(30);
		M('Build')->show_direction(Url::U('single_page/list_single_page'));
	}
}


?>