<?php
include_once('RCCWP_ManagementPage.php');
include_once('RCCWP_CreateCustomWritePanelPage.php');
include_once('RCCWP_CreateCustomFieldPage.php');
include_once('RCCWP_CustomFieldPage.php');

class RCCWP_Menu
{
	function AttachManagementMenuItem()
	{
		$page_group = 'RCCWP_ManagementPage';
		$page_type = 'View';
		
		if (isset($_REQUEST['create-custom-write-panel']))
		{
			$page_group = 'RCCWP_CreateCustomWritePanelPage';
			$page_type = 'Main';
		}
		else if (isset($_REQUEST['create-custom-field']))
		{
			$page_group = 'RCCWP_CreateCustomFieldPage';
			$page_type = 'Main';
		}
		else if (isset($_REQUEST['continue-create-custom-field']))
		{
			$current_field = RCCWP_CustomField::GetCustomFieldTypes((int)$_REQUEST['custom-field-type']);
			if ($current_field->has_options == "true" || $current_field->has_properties == "true")
			{
				$page_group = 'RCCWP_CreateCustomFieldPage';
				$page_type = 'SetOptions';
			}
			else if ($current_field->has_options == "false")
			{
				RCCWP_CustomField::Create(
					$_POST['custom-write-panel-id'],
					$_POST['custom-field-name'],
					$_POST['custom-field-description'],
					$_POST['custom-field-order'],
					$_POST['custom-field-type'],
					$_POST['custom-field-options']);	
				
				$page_group = 'RCCWP_CustomWritePanelPage';
				$page_type = 'View';
			}
		}
		else if (isset($_REQUEST['finish-create-custom-field']))
		{
			$page_group = 'RCCWP_CustomWritePanelPage';
			$page_type = 'View';
		}
		else if (isset($_REQUEST['finish-create-custom-write-panel']))
		{
			$page_group = 'RCCWP_ManagementPage';
			$page_type = 'View';
		}
		else if (isset($_REQUEST['edit-custom-write-panel']))
		{
			$page_group = 'RCCWP_CustomWritePanelPage';
			$page_type = 'Edit';
		}
		else if (isset($_REQUEST['cancel-edit-custom-write-panel']))
		{
			$page_group = 'RCCWP_CustomWritePanelPage';
			$page_type = 'View';
		}
		else if (isset($_REQUEST['submit-edit-custom-write-panel']))
		{
			$page_group = 'RCCWP_CustomWritePanelPage';
			$page_type = 'View';
		}
		else if (isset($_REQUEST['cancel-edit-custom-field']))
		{
			$page_group = 'RCCWP_CustomWritePanelPage';
			$page_type = 'View';
		}
		else if (isset($_REQUEST['submit-edit-custom-field']))
		{
			$page_group = 'RCCWP_CustomWritePanelPage';
			$page_type = 'View';
		}
		else if (isset($_REQUEST['edit-custom-field']))
		{
			$page_group = 'RCCWP_CustomFieldPage';
			$page_type = 'Edit';
		}
		else if (isset($_REQUEST['view-custom-write-panel']))
		{
			$page_group = 'RCCWP_CustomWritePanelPage';
			$page_type = 'View';
		}
		else if (isset($_REQUEST['assign-custom-write-panel']))
		{
			$page_group = 'RCCWP_ManagementPage';
			$page_type = 'AssignCustomWritePanel';
		}
		
		add_management_page(__('Custom Write Panel'), __('Write Panels'), 'manage_options', __FILE__, array($page_group, $page_type));	
	}
	
	function AttachOptionsMenuItem()
	{
		include_once('RCCWP_OptionsPage.php');
		add_options_page(__('Fresh Post Options'), __('Fresh Post'), 'manage_options', 'RCCWP_OptionsPage.php', array('RCCWP_OptionsPage', 'Main'));	
	}
	
	function AttachCustomWritePanelMenuItems()
	{
		include_once('RCCWP_Options.php');
		$assignToRole = RCCWP_Options::Get('assign-to-role');

		$customWritePanels = RCCWP_Application::GetCustomWritePanels();

		foreach ($customWritePanels as $panel)
		{
			if ($assignToRole <> 1 || current_user_can($panel->capability_name))
			{
				if ($panel->type == "post"){
					add_submenu_page('edit.php', __($panel->name), __($panel->name), 'publish_posts', 'edit.php?filter-posts=1&custom-write-panel-id=' . $panel->id);
				}
				else {
					add_submenu_page('edit.php', __($panel->name), __($panel->name), 'publish_posts', 'edit-pages.php?filter-posts=1&custom-write-panel-id=' . $panel->id);
				}
			}
			
		}
		
		if (isset($_REQUEST['post']))
		{
			global $submenu;
			//print_r($submenu);
			global $CUSTOM_WRITE_PANEL;
			/*if (isset($CUSTOM_WRITE_PANEL))
				if ($CUSTOM_WRITE_PANEL->type == "post")
					$submenu['edit.php'][5] = array(__($CUSTOM_WRITE_PANEL->name), 'edit_posts', 'edit.php');
				else
					$submenu['edit.php'][10] = array(__($CUSTOM_WRITE_PANEL->name), 'edit_pages', 'edit-pages.php');
			*/
		}
		else
		{
			
				
			foreach ($customWritePanels as $panel)
			{
				if ($assignToRole <> 1 || current_user_can($panel->capability_name))
				{
					if ($panel->type == "post"){
						add_submenu_page('post-new.php', __($panel->name), __($panel->name), 'publish_posts', 'post-new.php?custom-write-panel-id=' . $panel->id);
						//add_submenu_page('edit.php', __($panel->name), __($panel->name), 'publish_posts', 'edit.php?filter-posts=1&custom-write-panel-id=' . $panel->id);
					}
					else {
						add_submenu_page('post-new.php', __($panel->name), __($panel->name), 'publish_posts', 'page-new.php?custom-write-panel-id=' . $panel->id);
						//add_submenu_page('edit.php', __($panel->name), __($panel->name), 'publish_posts', 'edit-pages.php?filter-posts=1&custom-write-panel-id=' . $panel->id);
					}
				}
				
			}
		}
		
		RCCWP_Menu::SetCurrentCustomWritePanelMenuItem();
		
	}

	function HighlightCustomPanel(){
		global $wpdb, $submenu_file, $post; 
		
		$result = $wpdb->get_results( " SELECT meta_value
						FROM $wpdb->postmeta
						WHERE post_id = '".$post->ID."' and meta_key = '_rc_cwp_write_panel_id'", ARRAY_A );
		$currPage = basename($_SERVER['SCRIPT_NAME']);
		if (count($result) > 0 && $currPage =="post.php" ){
			$id = $result[0]['meta_value'];
			$submenu_file = "edit.php?filter-posts=1&custom-write-panel-id=$id";
		}
		elseif (count($result) > 0 && $currPage == "page.php" ){
			$id = $result[0]['meta_value'];
			$submenu_file = "edit-pages.php?filter-posts=1&custom-write-panel-id=$id";
		}
		
		
	}

	function FilterPostsPagesList($where){
		global $wpdb;
		if (isset($_GET['filter-posts'])) {
			$panel_id = $_GET['custom-write-panel-id'];
			$where = $where . " AND 0 < (SELECT count($wpdb->postmeta.meta_value)
					FROM $wpdb->postmeta
					WHERE $wpdb->postmeta.post_id = $wpdb->posts.ID and $wpdb->postmeta.meta_key = '_rc_cwp_write_panel_id' and $wpdb->postmeta.meta_value = '$panel_id') ";
		}
		return $where;
		/*$i = 0;
		if (isset($_GET['filter-posts']) && (!empty($posts))) {
			$panel_id = $_GET['custom-write-panel-id'];
			foreach($posts as $my_post){
				$result = $wpdb->get_results( " SELECT meta_value
					FROM $wpdb->postmeta
					WHERE post_id = '$my_post->ID' and meta_key = '_rc_cwp_write_panel_id' and meta_value = '$panel_id'", ARRAY_A );
				if (count($result) == 0 )
					array_splice($posts, $i ,1);
				else
					$i++;
			}

		}
		return $posts;*/
	}
	
	function DetachWpWritePanelMenuItems()
	{
		include_once('RCCWP_Options.php');
		global $submenu;

		$options = RCCWP_Options::Get();
		
		if ($options['hide-write-post'] == '1')
			unset($submenu['post-new.php'][5]);
		
		if ($options['hide-write-page'] == '1')
			unset($submenu['post-new.php'][10]);	
	}
	
	function SetCurrentCustomWritePanelMenuItem()
	{
		// wp-admin/menu.php
		global $submenu_file;
		global $menu;
		
		include_once('RCCWP_Options.php');
		$options = RCCWP_Options::Get();
		
		if ($options['default-custom-write-panel'] != '')
		{
			include_once('RCCWP_CustomWritePanel.php');
			
			$customWritePanel = RCCWP_CustomWritePanel::Get((int)$options['default-custom-write-panel']);
			if ($options['assign-to-role'] <> 1 || current_user_can($customWritePanel->capability_name))
			{
				if ($customWritePanel->type == "post")
					$menu[5][2] = 'post-new.php?custom-write-panel-id=' . (int)$options['default-custom-write-panel'];
				else
					$menu[5][2] = 'page-new.php?custom-write-panel-id=' . (int)$options['default-custom-write-panel'];
			}
		}
		
		if ($_REQUEST['custom-write-panel-id'])
		{
			$customWritePanel = RCCWP_CustomWritePanel::Get((int)$_REQUEST['custom-write-panel-id']);
			if ($_REQUEST['filter-posts']){
				if ($customWritePanel->type == "post")
					$submenu_file = 'edit.php?filter-posts=1&custom-write-panel-id=' . (int)$_REQUEST['custom-write-panel-id'];
				else
					$submenu_file = 'edit-pages.php?filter-posts=1&custom-write-panel-id=' . (int)$_REQUEST['custom-write-panel-id'];
			}
			else{
				if ($customWritePanel->type == "post")
					$submenu_file = 'post-new.php?custom-write-panel-id=' . (int)$_REQUEST['custom-write-panel-id'];
				else
					$submenu_file = 'page-new.php?custom-write-panel-id=' . (int)$_REQUEST['custom-write-panel-id'];
			}
		}
		else if (isset($_REQUEST['post']))
		{
		}
	}
}
?>