<?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 feature extends ASK_CONTROLLER
{
	function get_permission_action()
	{
	
	}

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

		TPL::assign('menu_list', $this->model('admin_group')->get_menu_list($this->user_info['group_id'], 304));
	}

	public function index_action()
	{
		$this->list_action();
	}

	public function list_action()
	{
		$per_page = 20;
		
		$page_id = intval($_GET['page_id']);

		$page_id = ($page_id == 0) ? 1 : $page_id;
		
		$limit = ($page_id - 1) * $per_page . "," . $per_page;
		
		$feature_list = $this->model('feature')->get_feature_list(null, false, 'id DESC', $limit);
		
		$feature_count = $this->model('feature')->get_feature_list(null, true);
		
		ASK_APP::pagination()->initialize(array(
			'base_url' => get_setting('base_url') . '/?/admin/feature/list/',
			'total_rows' => $feature_count,
			'per_page' => $per_page,
			'last_link' => "Last Page",
			'first_link' => "Home",
			'next_link' => "Next »",
			'prev_link' => "« Previous",
			'anchor_class' => ' class="number"',
			'cur_tag_open' => '<a class="number current">',
			'cur_tag_close' => '</a>',
			'direct_page' => TRUE
		));
		
		$this->crumb("Manage Feature", "admin/feature/list/");
		
		TPL::assign('pagination', ASK_APP::pagination()->create_links());
		
		TPL::assign('list', $feature_list);
		
		TPL::output("admin/feature/list");
	}
	
	public function edit_action()
	{
		$feature = $this->model('feature')->get_feature_by_id(intval($_GET['feature_id']));
		
		$this->crumb('Edit Feature', "admin/feature/list/");
		
		TPL::assign('feature', $feature);
		
		TPL::output("admin/feature/edit");
	}
	
	public function topic_list_action()
	{
		$feature = $this->model('feature')->get_feature_by_id($_GET['feature_id']);
		
		$list = $this->model('feature')->get_topics_by_feature_id($_GET['feature_id']);
		
		$this->crumb('Management topics:' . $feature['title'] . '  Next topic', "admin/feature/list/");
		
		TPL::assign('feature', $feature);
		
		TPL::assign('list', $list);
		
		TPL::import_js(array(
			'admin/js/jquery.autocomplete.js',
		));
		
		TPL::output("admin/feature/topic_list");
	}
	
	public function add_topic_ajax_action()
	{
		$feature_id = intval($_GET['feature_id']);
		
		$topic_id = intval($_POST['topic_id']);
		
		if(!$feature_id)
		{
			H::ajax_json_output(ASK_APP::RSM(null, -1, 'Topic ID is empty.'));
		}
		
		if(!$topic_id)
		{
			if(!$topic_info = $this->model('topic')->get_topic_by_title($_POST['topic']))
			{
				H::ajax_json_output(ASK_APP::RSM(null, -1, 'Topic does not exist'));
			}
			
			$topic_id = $topic_info['topic_id'];
		}
		
		if($this->model('feature')->add_topic($feature_id, $topic_id))
		{
			$this->model('feature')->update_topic_count($feature_id);
			
			H::ajax_json_output(ASK_APP::RSM(null, 1, null));
		}
		else
		{
			H::ajax_json_output(ASK_APP::RSM(null, -1, 'Topic already exists'));
		}
	}
	
	public function delete_topic_ajax_action()
	{
		if($this->model('feature')->delete_topic($_GET['feature_id'], $_GET['topic_id']))
		{
			$this->model('feature')->update_topic_count(intval($_GET['feature_id']));
		}
		
		H::ajax_json_output(ASK_APP::RSM(null, 1, null));
	}
	
	public function delete_feature_ajax_action()
	{
		if($this->model('feature')->delete_feature($_GET['feature_id']))
		{
			H::ajax_json_output(ASK_APP::RSM(null, 1, ''));
		}
		else
		{
			H::ajax_json_output(ASK_APP::RSM(null, -1, 'Delete failed'));
		}
	}

	public function save_ajax_action()
	{
		define('IN_AJAX', TRUE);
	
		$feature_id = intval($_GET['feature_id']);
		$title = $_POST['title'];
			
		if (empty($title))
		{
			H::ajax_json_output(ASK_APP::RSM(null, "-1", "Feature title cannot be empty"));
		}
		
		if($feature_id)
		{
			$feature = $this->model('feature')->get_feature_by_id($feature_id);
		}
		
		if($_POST['url_token'])
		{
			if (!preg_match("/^(?!__)[a-zA-Z0-9_]+$/i", $_POST['url_token']))
			{
				H::ajax_json_output(ASK_APP::RSM(null, '-1', 'Feature name should be only alphabets or numbers'));
			}
			
			if (preg_match("/^[\d]+$/i", $_POST['url_token']) AND ($feature_id != $_POST['url_token']))
			{
				H::ajax_json_output(ASK_APP::RSM(null, '-1', 'Feature name cannot be only numbers'));
			}
		
			if ($this->model('feature')->check_url_token($_POST['url_token'], $feature_id))
			{
				H::ajax_json_output(ASK_APP::RSM(null, '-1', 'Feature name already exist, please use another name'));
			}
		}

		//Add a new topic
		if (! $feature_id)
		{		
			$feature_id = $this->model('feature')->add_feature($title);

			if ($_POST['add_nav_menu'])
			{
				$this->model('menu')->add_nav_menu($title, htmlspecialchars($_POST['description']), 'feature', $feature_id);
			}
		}
	
		$update_data = array(
			'title' => $title,
			'description' => htmlspecialchars($_POST['description']),
			'css' => htmlspecialchars($_POST['css']),
			'url_token' => $_POST['url_token'],
		);
	
		if ($_FILES['icon']['name'])
		{
			ASK_APP::upload()->initialize(array(
				'allowed_types' => 'jpg,jpeg,png,gif',
				'upload_path' => get_setting('upload_dir') . '/feature',
				'is_image' => TRUE
			))->do_upload('icon');
			
			
			if (ASK_APP::upload()->get_error())
			{
				switch (ASK_APP::upload()->get_error())
				{
					default:
						H::ajax_json_output(ASK_APP::RSM(null, '-1', 'Invalid code: ' . ASK_APP::upload()->get_error()));
					break;
					
					case 'upload_invalid_filetype':
						H::ajax_json_output(ASK_APP::RSM(null, '-1', 'Invalid file format'));
					break;	
				}
			}
			
			if (! $upload_data = ASK_APP::upload()->data())
			{
				H::ajax_json_output(ASK_APP::RSM(null, '-1', 'Failed to upload, please contact the administrator'));
			}

			foreach (ASK_APP::config()->get('image')->feature_thumbnail as $key => $val)
			{
				$thumb_file[$key] = $upload_data['file_path'] . $feature_id . "_" . $val['w'] . "_" . $val['h'] . '.jpg';
				
				ASK_APP::image()->initialize(array(
					'quality' => 90,
					'source_image' => $upload_data['full_path'],
					'new_image' => $thumb_file[$key],
					'width' => $val['w'],
					'height' => $val['h']
				))->resize();	
			}
			
			unlink($upload_data['full_path']);
			
			$update_data['icon'] = basename($thumb_file['min']);
		}
	
		$this->model('feature')->update_feature($feature_id, $update_data);
	
		H::ajax_json_output(ASK_APP::RSM(array(
			'url' => GET_INDEX_SCRIPT . 'admin/feature/list/'
		), 1, "Successfully modified"));
	
	}
}