<?php
/*
+--------------------------------------------------------------------------
|   TanyaCMS - Question and Answer Sofware
|   -----------------------------------------------------------------------
|   by fbschool dev team
|   (c) 2011 - 2012 FBSCHOOL
|   http://www.fbschool.com/tanyacms
|   -----------------------------------------------------------------------
|   This is NOT a freeware, use is subject to license terms
|   Support: 13455n@gmail.com
+--------------------------------------------------------------------------
*/


if (!defined('IN_TANYACMS'))
{
	die;
}

class tool extends ASK_CONTROLLER
{
	function get_permission_action()
	{
	
	}

	public function setup()
	{
		$this->model('admin_session')->init($this->get_permission_action());
	}

	/**
	 * Clear System Cache
	 */
	public function cache_clean_action()
	{
		$this->crumb("Update Cache", "admin/tool/cache_clean/");
		TPL::assign('menu_list', $this->model('admin_group')->get_menu_list($this->user_info['group_id'], 501));
		TPL::output('admin/tool/cache_clean');
	}

	public function cache_clean_process_action()
	{
		$this->model('cache')->clean();
		ASK_APP::cache()->clean();
		
		TPL::assign('message', 'Successful removal of Web Caching');
		TPL::assign('menu_list', $this->model('admin_group')->get_menu_list($this->user_info['group_id'], 501));
		TPL::output('admin/tool/cache_clean');
	}

	public function database_action()
	{
		$this->crumb("Database Maintenance", "admin/tool/database/");
		
		TPL::assign('backup_dir', $this->model('database')->get_backup_dir());
		TPL::assign('list', $this->model('database')->get_file_list());
		TPL::assign('menu_list', $this->model('admin_group')->get_menu_list($this->user_info['group_id'], 503));
		TPL::output('admin/tool/database');
	}

	public function database_backup_process_action()
	{
		if ($this->is_post())
		{
			foreach ($_POST as $key => $val)
			{
				$gets[] = $key . '-' . $val;
			}
			
			HTTP::redirect('?/admin/tool/database_backup_process/' . implode('__', $gets));
			exit();
		}
		
		$sqlstr = '';
		$size = intval($_GET['size']);
		$page = intval($_GET['page']);
		$tid = intval($_GET['tid']);
		$start = intval($_GET['start']);
		
		$tables = $this->model('database')->get_table_list();
		
		if (! $tid && $page == 0)
		{
			foreach ($tables as $table)
			{
				$sqlstr .= $this->model('database')->dump_struct($table);
			}
		}
		
		for ($finish = true; ($tid < count($tables)) && (strlen($sqlstr) + 300 < $size * 1024) && $finish; $tid ++)
		{
			$dump_query = $this->model('database')->dump_record($tables[$tid], $size, $start, $finish, strlen($sqlstr));
			
			$sqlstr .= $dump_query['sqlstr'];
			$start = $dump_query['start'];
			$finish = $dump_query['finish'];
			
			if ($finish)
			{
				$start = 0;
			}
		}
		
		! $finish && $tid --;
		
		if (trim($sqlstr))
		{
			$page ++;
			
			$retval = $this->model('database')->save_sql_file($_GET['filename'], $sqlstr, $page);
			
			if ($retval)
			{
				H::redirect_msg('Being backed up, the successful export sub-volume ' . $page . '...', '?/admin/tool/database_backup_process/size-' . rawurlencode($size) . '__page-' . rawurlencode($page) . '__tid-' . rawurlencode($tid) . '__start-' . rawurlencode($start) . '__filename-' . rawurlencode($_GET['filename']));
			}
			else
			{
				die('Unable to save file, please check system have write permissions to the directory');
			}
		}
		else
		{
			//Tip the backup is complete, display the file list
			$this->crumb("Database Backup", "admin/tool/database/");
			
			for ($i = 1; $i <= $page; $i ++)
			{
				$backup_list[] = '/system/backup_' . get_setting('backup_dir') . '/' . $_GET['filename'] . '-' . $i . '.sql';
			}
			
			TPL::assign('list', $backup_list);
			TPL::assign('menu_list', $this->model('admin_group')->get_menu_list($this->user_info['group_id'], 503));
			TPL::output('admin/tool/database_backup_success');
		}
	}

	public function database_revert_process_action()
	{
		$filename = $_GET['filename'];
		
		$page = intval($_GET['page']);
		
		@set_time_limit(0);
		
		if (! $filename)
		{
			HTTP::redirect('?/admin/tool/database/');
		}
		
		$file_list = $this->model('db_manage')->get_file_list();
		
		$page = ! $page ? 1 : $page;
		
		if ($page <= $file_list[$filename]['page'])
		{
			$retval = $this->model('db_manage')->revert($filename, $page);
			
			H::redirect_msg('Being restored, successfully imported sub-volume ' . $page . '...', '?/admin/tool/database_revert_process/filename-' . rawurlencode($_GET['filename']) . '__page-' . rawurlencode(++ $page));
		}
		else
		{
			H::redirect_msg('Successfully imported ' . count($file_list) . ' Sub-volumes, all backup files to restore successfully.');
		}
	}

	public function reputation_rebuild_action()
	{
		$this->crumb("Update user reputation", "admin/tool/reputation_rebuild/");
		TPL::assign('menu_list', $this->model('admin_group')->get_menu_list($this->user_info['group_id'], 504));
		TPL::output('admin/tool/reputation_rebuild');
	}

	public function rebuild_reputation_process_action()
	{
		$this->crumb("Update user reputation", "admin/tool/reputation_rebuild/");
		
		if ($this->is_post())
		{
			$per_page = intval($_POST['per_page']);
			
			HTTP::redirect('?/admin/tool/rebuild_reputation_process/per_page-' . $per_page);
		}
		
		if (!$_GET['page'])
		{
			$_GET['page'] = 1;
		}
		
		$per_page = intval($_GET['per_page']);
		$page = intval($_GET['page']);
		$interval = intval($_GET['interval']);
		
		$user_count = $this->model('account')->get_user_count();
		
		if ($page * $per_page < $user_count) //Unprocessed, continue processing
		{
			$this->model('reputation')->calculate((($page * $per_page) - $per_page), $per_page);
			
			$current = ($page * $per_page) > $user_count ? $user_count : ($page * $per_page);
			
			$interval = ($interval == 0) ? 3 : $interval;
			
			$url = '?/admin/tool/rebuild_reputation_process/page-' . ($page + 1) . '__per_page-' . $per_page . '__interval-' . $interval;
			
			H::redirect_msg('Total of ' . $user_count . ' Users, current updated to ' . $current . ' Users', $url, $interval);
		}
		else //Tip processed
		{
			TPL::assign('msg', 'Reputation data update is complete, a total update ' . $user_count . ' Reputation user data.');
			TPL::assign('menu_list', $this->model('admin_group')->get_menu_list($this->user_info['group_id'], 504));
			TPL::output('admin/tool/reputation_rebuild');
		}
	}

	public function bbcode_to_markdown_action()
	{
		if ($this->is_post())
		{
			H::redirect_msg('Content is coping with the 1 page, please wait...', '?/admin/tool/bbcode_to_markdown/model-question', 2);
		}
		
		if ($model = $_GET['model'])
		{
			$per_page = 100;
			
			$page = $_GET['page'];
			
			if (!$_GET['interval'])
			{
				$_GET['interval'] = 3;
			}
			
			$done = false;
				
			switch ($model)
			{
				case 'question' :
					
					$model_name = 'Question';
					
					if ($question_list = $this->model('question')->search_questions_list(false, array('page' => $page, 'per_page' => $per_page)))
					{
						foreach ($question_list as $key => $val)
						{
							$this->model('question')->update_question_field($val['question_id'], array(
								'question_detail' => FORMAT::bbcode_2_markdown($val['question_detail'])
							));
						}
						
						$page++;
					}
					else
					{
						$page = 1;
						$model = 'answer';
						$model_name = 'Reply';
					}
					
					break;
				case 'answer' :
					
					$model_name = 'Reply';

					if ($answer_list = $this->model('answer')->get_answer_list(null, calc_page_limit($page, $per_page), 'answer_id ASC'))
					{
						foreach ($answer_list as $key => $val)
						{
							$this->model('answer')->update_answer_by_id($val['answer_id'], array(
								'answer_content' => FORMAT::bbcode_2_markdown($val['answer_content'])
							));
						}
					
						$page++;
					}
					else
					{
						$page = 1;
						$model = 'topic';
						$model_name = 'Topic';
					}
					
					break;
					
				case 'topic' :
					
					$model_name = 'Topic';

					if ($topic_list = $this->model('topic')->get_topic_list(null, calc_page_limit($page, $per_page), false, 'topic_id ASC'))
					{
						foreach ($topic_list as $key => $val)
						{
							$this->model('topic')->update('topic', array(
								'topic_description' => FORMAT::bbcode_2_markdown($val['topic_description'])
							), 'topic_id = ' . intval($val['topic_id']));
						}
					
						$page++;
					}
					else
					{
						$done = true;
					}
					
					break;
			}
			
			if ($done)
			{
				TPL::assign('message', 'BBcode Conversion completed');
			}
			else
			{
				$url = '?/admin/tool/bbcode_to_markdown/model-' . $model . '__page-' . $page . '__per_page-' . $per_page . '__interval-' . $_GET['interval'];
				
				H::redirect_msg('Processing' . $model_name . 'Content first ' . $page . ' Page, please wait...', $url, $_GET['interval']);
			}
		}
		
		$this->crumb('Convert BBcode');
		TPL::assign('menu_list', $this->model('admin_group')->get_menu_list($this->user_info['group_id'], 505));
		TPL::output('admin/tool/bbcode_to_markdown');
	}

	public function search_index_action()
	{
		$this->crumb("Update search index", "admin/tool/search_index/");
		TPL::assign('menu_list', $this->model('admin_group')->get_menu_list($this->user_info['group_id'], 502));
		TPL::output('admin/tool/search_index');
	}

	public function update_search_index_action()
	{
		if ($this->is_post())
		{
			$per_page = intval($_POST['per_page']);
			
			H::redirect_msg('Is preparing to update the index, please wait...', '?/admin/tool/update_search_index/model-question__per_page-' . $per_page);
		}
		
		if ($_GET['model'])
		{
			$per_page = intval($_GET['per_page']);
			
			$_GET['page'] = (!$_GET['page']) ? 1 : intval($_GET['page']);
			
			$_GET['interval'] = (!$_GET['interval']) ? 3 : intval($_GET['interval']);
			
			$done = false;
				
			switch ($_GET['model'])
			{
				case 'question' :
					
					$model_name = 'Question';
					
					//if ($question_list = $this->model('question')->search_questions_list(false, array('page' => $_GET['page'], 'per_page' => $per_page, 'sort_key' => 'question_id', 'order' => 'ASC')))
					if ($question_list = $this->model('question')->query_all("SELECT question_id, question_content FROM " . get_table('question') . " ORDER BY question_id ASC LIMIT " . calc_page_limit($_GET['page'], $per_page)))
					{
						foreach ($question_list as $key => $val)
						{
							$this->model('search_index')->push_index('question', $val['question_content'], $val['question_id']);
						}
					}
					else
					{
						$done = true;
					}
					
				break;
			}
			
			if ($done)
			{
				TPL::assign('message', 'Search index is updated completed');
			}
			else
			{
				H::redirect_msg('Processing' . $model_name . 'Index page ' . $_GET['page'] . ' Page, please wait...', '?/admin/tool/update_search_index/model-' . $_GET['model'] . '__page-' . ++$_GET['page'] . '__per_page-' . $per_page . '__interval-' . $_GET['interval'], $_GET['interval']);
			}
		}
		
		$this->crumb('Update search index');
		
		TPL::assign('menu_list', $this->model('admin_group')->get_menu_list($this->user_info['group_id'], 502));
		
		TPL::output('admin/tool/search_index');
	}
}
