<?php

/**
 *--------------------------------------
 * 生成
 *--------------------------------------
 * @project		: uwa
 * @author		: cblee
 * @created		: 2012-10-22
 * @copyright	: (c)2012 AsThis
 *--------------------------------------
 */

class BuildCtrlr extends ManageCtrlr
{
	public function clear_cache()
	{
		$this->display();
	}
	public function clear_cache_do()
	{
		$type = ARequest::get('type');
		if(is_array($type))
		{
			foreach($type as $type)
			{
				M('Build')->clear_cache($type);
			}
		}
		else
		{
			M('Build')->clear_cache($type);
		}
		M('AdminLog')->add_log(ASession::get('m_userid'), L('CLEAR_CACHE'));
		$this->success(L('CLEAR_SUCCESS'), Url::U('build/clear_cache'));
	}

	public function update_archive()
	{
		$_ACL = M('ArchiveChannel')->get_myChannelList();
		$act = new ATree($_ACL, array('archive_channel_id', 'ac_parent_id', 'ac_sub_channel'));
		$_ACLStr = $act->get_leafStr(0, "<option value='\$archive_channel_id'>\$spacer\$ac_name</option>\r\n");
		$this->assign('_ACLStr', $_ACLStr);

		$nextUrl = F('~build/~next_url');
		$this->assign('nextUrl', $nextUrl);

		$this->display();
	}
	public function update_archive_do()
	{
		set_time_limit(99999999);
		$this->display('admin/build/progress');

		/* 频道ID */
		$archiveChannelId = ARequest::get('archive_channel_id') ? ARequest::get('archive_channel_id') : 0;
		$acId = F('~build/~acid_'.$archiveChannelId);
		if(empty($acId))
		{
			$_ACL = M('ArchiveChannel')->get_channelList(0, $archiveChannelId);
			$act = new ATree($_ACL, array('archive_channel_id', 'ac_parent_id', 'ac_sub_channel'), $archiveChannelId);
			$acId = implode(',', $act->get_leafid($archiveChannelId));

			F('~build/~acid_'.$archiveChannelId, $acId);
		}
		/* 动作 */
		$action = ARequest::get('action') ? ARequest::get('action') : 'update_url';

		$where = array();
		/* 限制频道ID */
		$where['archive_channel_id'] = array('IN', $acId);
		/* 限制状态 */
		$where['a_status'] = array('EQ', 1);
		/* 限制开始结束时间 */
		$startTime = ARequest::get('start_time');
		$startTime = (is_int($startTime) ? $startTime : (strtotime($startTime) ? strtotime($startTime) : 0));
		$endTime = ARequest::get('end_time');
		$endTime = (is_int($endTime) ? $endTime : (strtotime($endTime) ? strtotime($endTime) : time()));
		if(0 < $startTime)
		{
			$where['a_add_time'] = array('BETWEEN', $startTime.','.$endTime);
		}
		/* 限制开始结束档案ID */
		$startId = ARequest::get('start_id') ? ARequest::get('start_id') : 0;
		$endId = ARequest::get('end_id') ? ARequest::get('end_id') : 0;
		if(0 < $startId)
		{
			$where['archive_id'] = array('BETWEEN', $startId.','.$endId);
		}
		/* 档案条数 */
		$totalRows = ARequest::get('total_rows') ? ARequest::get('total_rows') : M('Archive')->where($where)->count();
		/* 任务分页参数 */
		$pageSize = ARequest::get('page_size') ? ARequest::get('page_size') : 50;
		$currentPage = ARequest::get('current_page') ? ARequest::get('current_page') : 1;
		$totalPage = ceil($totalRows / $pageSize);
		$limit = ($currentPage - 1) * $pageSize.','.$pageSize;

		/* 档案ID列表 */
		$_AL = M('Archive')->field('archive_id')->where($where)->limit($limit)->select();

		$_L_ID = array(); /* 日志记录ID */

		foreach($_AL as $a)
		{
			$_L_ID[] = $a['archive_id'];
			if('update_url' == $action)
			{
				/* 更新相应URL */
				M('Archive')->update_url($a['archive_id']);
			}
			elseif('update_html' == $action)
			{
				/* 生成相应html */
				ARequest::set('archive_id', $a['archive_id']);
				A('Admin.Archive')->update_html_do();
			}
		}

		$_L_ID = is_array($_L_ID) ? implode(',', $_L_ID) : $_L_ID;
		M('AdminLog')->add_log(ASession::get('m_userid'), L('UPDATE_ARCHIVE').'-'.L(strtoupper($action)).': ARCHIVE_ID['.$_L_ID.']');

		/* 进度信息及下页地址 */
		if($currentPage < $totalPage)
		{
			$progress = round($currentPage * $pageSize / $totalRows * 100, 1);
			$nextUrl = Url::U('build/update_archive_do?page_size='.$pageSize
				.'&action='.$action
				.'&archive_channel_id='.$archiveChannelId
				.'&start_time='.$startTime
				.'&end_time='.$endTime
				.'&start_id='.$startId
				.'&end_id='.$endId
				.'&total_rows='.$totalRows
				.'&current_page='.($currentPage + 1));
			F('~build/~next_url', $nextUrl);
			M('Build')->show_progress($progress.'% ['.$currentPage * $pageSize.'/'.$totalRows.']: '.L(strtoupper($action)), $progress);
			M('Build')->show_direction($nextUrl);
		}
		else
		{
			M('Build')->show_progress('100% ['.$totalRows.'/'.$totalRows.']: '.L(strtoupper($action)).L('UPDATE_COMPLETE'), 100);
			set_time_limit(30);
			F('~build/~next_url', null);
			F('~build/~acid_'.$archiveChannelId, null);
			M('Build')->show_direction(Url::U('build/update_archive'), true);
		}
	}

	public function update_channel()
	{
		$_ACL = M('ArchiveChannel')->get_myChannelList();
		$act = new ATree($_ACL, array('archive_channel_id', 'ac_parent_id', 'ac_sub_channel'));
		$_ACLStr = $act->get_leafStr(0, "<option value='\$archive_channel_id'>\$spacer\$ac_name</option>\r\n");
		$this->assign('_ACLStr', $_ACLStr);

		$nextUrl = F('~build/~next_url');
		$this->assign('nextUrl', $nextUrl);

		$this->display();
	}
	public function update_channel_do()
	{
		set_time_limit(99999999);
		$this->display('admin/build/progress');

		/* 频道ID */
		$archiveChannelId = ARequest::get('archive_channel_id') ? ARequest::get('archive_channel_id') : 0;

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

		$acId = F('~build/~acid_'.$archiveChannelId);
		if(empty($acId))
		{
			$_ACL = M('ArchiveChannel')->get_channelList(0, $archiveChannelId);
			$act = new ATree($_ACL, array('archive_channel_id', 'ac_parent_id', 'ac_sub_channel'), $archiveChannelId);
			$acId = implode(',', $act->get_leafid($archiveChannelId));

			F('~build/~acid_'.$archiveChannelId, $acId);
		}
		/* 动作 */
		$action = ARequest::get('action') ? ARequest::get('action') : 'update_url';

		if('update_url' == $action)
		{
			/* 频道条数 */
			$totalRows = count(explode(',', $acId));
			/* 任务分页参数 */
			$pageSize = ARequest::get('page_size') ? ARequest::get('page_size') : 50;
			$currentPage = ARequest::get('current_page') ? ARequest::get('current_page') : 1;
			$totalPage = ceil($totalRows / $pageSize);
			$limitMin = ($currentPage - 1) * $pageSize;
			$limitMax = $currentPage * $pageSize;
			/* 更新URL */
			foreach(explode(',', $acId) as $key => $acId)
			{
				if($key >= $limitMin and $key < $limitMax)
				{
					/* 更新相应URL */
					M('ArchiveChannel')->update_url($acId);
				}
				continue;
			}
			/* 进度信息及下页地址 */
			if($currentPage < $totalPage)
			{
				$progress = round($currentPage * $pageSize / $totalRows * 100, 1);
				$nextUrl = Url::U('build/update_channel_do?page_size='.$pageSize
					.'&action='.$action
					.'&archive_channel_id='.$archiveChannelId
					.'&total_rows='.$totalRows
					.'&current_page='.($currentPage + 1));
				F('~build/~next_url', $nextUrl);
				M('Build')->show_progress($progress.'% ['.$currentPage * $pageSize.'/'.$totalRows.']: '.L('CHANNEL_URL'), $progress);
				M('Build')->show_direction($nextUrl);
			}
			else
			{
				M('Build')->show_progress('100% ['.$totalRows.'/'.$totalRows.']: '.L('CHANNEL_URL').L('UPDATE_COMPLETE'), 100);
				set_time_limit(30);
				F('~build/~next_url', null);
				F('~build/~acid_'.$archiveChannelId, null);
				M('Build')->show_direction(Url::U('build/update_channel'), true);
			}
		}
		elseif('update_html' == $action)
		{
			/* 生成相应html */
			$acId = explode(',', $acId);
			$acCount = count($acId);
			$currentKey = ARequest::get('current_key') ? ARequest::get('current_key') : 0;
			foreach($acId as $key => $acId)
			{
				if($key != $currentKey)
				{
					continue;
				}
				$where = array();
				$where['__ARCHIVE__.a_status'] = array('EQ', 1);

				$_ACL = M('ArchiveChannel')->get_channelList(0, $acId);
				$act = new ATree($_ACL, array('archive_channel_id', 'ac_parent_id', 'ac_sub_channel'), $acId);
				$where['__ARCHIVE__.archive_channel_id'] = array('IN', implode(',', $act->get_leafid($acId)));

				$archiveRows = ARequest::get('archive_rows')
					? ARequest::get('archive_rows')
					: M('Archive')->where($where)->count();

				$_ACI = M('ArchiveChannel')->where('archive_channel_id=\''.$acId.'\'')->find();
				ARequest::set('archive_channel_id', $acId);
				$index = ARequest::get('index') ? ARequest::get('index') : 'yes';
				if(1 == $_ACI['ac_type'] and 'yes' == $index)
				{
					A('Admin.ArchiveChannel')->update_html_index_do();
					M('Build')->show_progress('100% [1/1]: '.$_ACI['ac_name'].L('CHANNEL_INDEX').L('UPDATE_COMPLETE'), 100);
				}

				$totalRows = ceil($archiveRows / $_ACI['ac_page_size']);
				$pageSize = ARequest::get('page_size') ? ARequest::get('page_size') : 50;
				$currentPage = ARequest::get('current_page') ? ARequest::get('current_page') : 1;
				$totalPage = ceil($totalRows / $pageSize);

				$limitMax = $currentPage * $pageSize < $totalPage
					? $currentPage * $pageSize
					: $totalRows;
				if(0 == $archiveRows)
				{
					$_GET[C('VAR.PAGE')] = 1;
					A('Admin.ArchiveChannel')->update_html_list_do();
				}
				for($i = ($currentPage - 1) * $pageSize + 1; $i <= $limitMax; $i++)
				{
					$_GET[C('VAR.PAGE')] = $i;
					A('Admin.ArchiveChannel')->update_html_list_do();
				}

				if($currentPage < $totalPage)
				{
					$progress = round($currentPage * $pageSize / $totalRows * 100, 1);
					M('Build')->show_progress($progress.'% ['.$currentPage * $pageSize.'/'.$totalRows.']: '.$_ACI['ac_name'].L('CHANNEL_LIST'), $progress);
					$nextUrl = Url::U('build/update_channel_do?page_size='.$pageSize
						.'&index=no'
						.'&archive_channel_id='.$archiveChannelId
						.'&action='.$action
						.'&archive_rows='.$archiveRows
						.'&current_key='.$key
						.'&current_page='.($currentPage + 1));
					F('~build/~next_url', $nextUrl);
					M('Build')->show_direction($nextUrl);
				}
				else
				{
					M('Build')->show_progress('100% ['.$totalRows.'/'.$totalRows.']: '.$_ACI['ac_name'].L('CHANNEL_LIST').L('UPDATE_COMPLETE'), 100);
					if($key < $acCount - 1)
					{
						$nextUrl = Url::U('build/update_channel_do?page_size='.$pageSize
							.'&archive_channel_id='.$archiveChannelId
							.'&action='.$action
							.'&current_key='.($key + 1));
						F('~build/~next_url', $nextUrl);
						M('Build')->show_direction($nextUrl);
					}
					else
					{
						set_time_limit(30);
						F('~build/~acid_'.$archiveChannelId, null);
						F('~build/~next_url', null);
						M('Build')->show_direction(Url::U('build/update_channel'), true);
					} /*e: $key >= $acCount - 1 */
				} /*e: $currentPage >= $totalPage */
			} /*e: foreach */
		} /*e: action */
	} /*e: update_list_html_do */

	public function update_index_do()
	{
		M('AdminLog')->add_log(ASession::get('m_userid'), L('UPDATE_INDEX'));

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

		$_o = M('Option')->get_option('core');
		if(!$_o['html_switch'])
		{
			M('Build')->show_progress(L('HTML_SWITCH_IS_OFF'), 100);
			set_time_limit(30);
			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);

		$this->assign('TASK', 'update_html_index');

		$_C = require(CFG_PATH.D_S.'comm.php');
		$this->te->tplTheme = $_C['TE']['TPL_THEME'];
		$this->build_html('index'.C('HTML.FILE_SUFFIX'), APP_PATH, 'home/index');
		$this->te->tplTheme = 'default';
		M('Build')->show_progress('100% [1/1]: '.L('HOME_INDEX').L('UPDATE_COMPLETE'), 100);
		set_time_limit(30);
	}

}

?>