<?php

	//AÑADIR PAGINA AL MENU
//	add_action( 'admin_menu', 'add_a3p_options_to_menu'  );	
	add_action( 'admin_menu', 'add_ainia_create_concept_options_to_menu'  );	
	add_action( 'admin_menu', 'add_ainia_manage_concept_options_to_menu'  );	
	//add_action( 'admin_menu', 'add_ainia_create_source_options_to_menu'  );	
	//add_action( 'admin_menu', 'add_ainia_manage_source_options_to_menu'  );	
	function add_a3p_options_to_menu() 
	{	
		global $a3p_options_admin_page;	
		$a3p_options_admin_page=add_submenu_page('tools.php',__('iWatch Settings','ainia'), 'iWatch', 'edit_users', basename(__FILE__), 'show_a3p_options_page');		
		
	}	
	
	add_action( 'current_screen', 'init_a3p_options'  );	
	function init_a3p_options()
	{	
		global $a3p_options_admin_page;	
		
		$screen = get_current_screen(); 		
		if($a3p_options_admin_page != $screen->id)return;	
		
		save_a3p_options_page();
	}


	
	
	function save_a3p_options_page() 
	{		
		if ( !empty($_POST) && $_POST['_wpnonce-security-ainia']!="") 
		if(check_admin_referer('security-ainia','_wpnonce-security-ainia') )
		{	
			global $a3p_options_page_results_ok,$a3p_options_page_results_ko;
			$a3p_options_page_results_ok = array();
			$a3p_options_page_results_ko = array();
			
			if(!empty($_REQUEST['save']))
			{				
				$field_id = 'ainia_newsletter_email';
				update_option($field_id,$_REQUEST[$field_id]);		
				$field_id = 'ainia_rss_url';
				update_option($field_id,$_REQUEST[$field_id]);		
				$field_id = 'ainia_push_url';
				update_option($field_id,$_REQUEST[$field_id]);		
				$field_id = 'ainia_max_days';
				update_option($field_id,intval($_REQUEST[$field_id]));		
				$field_id = 'ainia_max_posts';
				update_option($field_id,intval($_REQUEST[$field_id]));		
				$field_id = 'ainia_max_rss';
				update_option($field_id,intval($_REQUEST[$field_id]));		
				$field_id = 'ainia_imported_status';
				update_option($field_id,$_REQUEST[$field_id]);		
				$field_id = 'ainia_colors';
				update_option($field_id,$_REQUEST[$field_id]);
				
				$a3p_options_page_results_ok[]=sprintf(__("Options saved",'ainia'),$cuantos);
			}		
			else if(!empty($_REQUEST['newsletter']))
			{
			   $cuantos = ainia_send_newsletter();								   
			   if(is_wp_error( $cuantos )) $a3p_options_page_results_ko[]= "Error: ".$cuantos->get_error_message();
			   else if($cuantos)$a3p_options_page_results_ok[]=sprintf(__("%d mails sent by newsletter",'ainia'),$cuantos);
			   else $a3p_options_page_results_ko[]=__("No mails sent by newsletter",'ainia');
			}
			else if(!empty($_REQUEST['import']))
			{
				$cuantos = ainia_import_crawler();					
				if(is_wp_error( $cuantos )) $a3p_options_page_results_ko[]= "Error: ".$cuantos->get_error_message();
				else if($cuantos)$a3p_options_page_results_ok[]=sprintf(__("%d posts imported from crawler",'ainia'),$cuantos);
				else $a3p_options_page_results_ko[]=__("Nothing imported from crawler",'ainia');
			}
		}
	}
		/*******************************************************************/
		function add_ainia_manage_concept_options_to_menu() 
	{	
		global $ainia_create_concept_options_admin_page;	
		$ainia_create_concept_options_admin_page=add_submenu_page($parent_slug, $page_title, $menu_title, $capability, $menu_slug);
		//$ainia_create_concept_options_admin_page=add_menu_page(__('iWatch Adminitration','ainia'), 'Admin-iWatch', 'edit_users', basename(__FILE__), 'show_ainia_options_page');		
		
	}
	function add_ainia_create_concept_options_to_menu() 
	{	
		global $ainia_create_concept_options_admin_page;	
		$ainia_create_concept_options_admin_page=add_menu_page(__('iWatch Adminitration','ainia'), 'Fuentes y conceptos', 'edit_users', 'create-concept-options','show_ainia_create_concept_page');		
		//											add_menu_page('My Custom Page', 'My Custom Page', 'manage_options', 'my-top-level-slug');
		add_submenu_page( 'create-concept-options', 'Crear concepto', 'Crear concepto','edit_users', 'ainia-create-concept-page','show_ainia_create_concept_page');
		add_submenu_page( 'create-concept-options', 'Gestionar conceptos', 'Gestionar conceptos', 'edit_users','ainia-manage-concept-page', 'show_ainia_manage_concept_page');
		add_submenu_page( 'create-concept-options', 'Crear fuente', 'Crear fuente', 'edit_users','ainia-create-source-page', 'show_ainia_create_source_page');
		add_submenu_page( 'create-concept-options', 'Gestionar fuentes', 'Gestionar fuente', 'edit_users','ainia-manage-source-page', 'show_ainia_manage_source_page');
		add_submenu_page( 'create-concept-options', 'Grupos de conceptos', 'Grupos de conceptos', 'edit_users','ainia-manage-groups', 'show_ainia_manage_groups');
		add_submenu_page( 'create-concept-options', 'Crear Complex Matchers', 'Crear Complex Matchers', 'edit_users','ainia-create-complex-matchers', 'show_ainia_create_complex_matchers');
		add_submenu_page( 'create-concept-options', 'Gestionar Complex Matchers', 'Gestionar Complex Matchers', 'edit_users','ainia-manage-complex-matchers', 'show_ainia_manage_complex_matchers');
		
		remove_submenu_page( 'create-concept-options', 'create-concept-options' );
	}
	
	function show_ainia_create_concept_page()
	{
		if ( !current_user_can( 'edit_users' ) )
			wp_die( __( 'You do not have sufficient permissions to access this page.', 'ainia' ) );
			?>
			<div class="wrap"  width="90%" height="100%">
							
				<h2><?php _e( 'iWatch Settings', 'ainia' ); ?></h2>
				<iframe src="http://production2.ainia.es:8080/crawlerUIecoembes/#!ConceptView/user=ecoembes&/&password=ZWNvZW1iZXM= " width="100%" height="750" align="center"> </iframe>
				
				
			</div>
				
			
			<?php
		}
			function show_ainia_manage_concept_page() 
		{		
			if ( !current_user_can( 'edit_users' ) ) 
			 wp_die( __( 'You do not have sufficient permissions to access this page.', 'ainia' ) );
			?>
			<div class="wrap"  width="90%" height="100%">
							
				<h2><?php _e( 'iWatch Settings', 'ainia' ); ?></h2>
				<iframe src="http://production2.ainia.es:8080/crawlerUIecoembes/#!ConceptOfLoginView/user=ecoembes&/&password=ZWNvZW1iZXM= " width="100%" height="750" align="center"> </iframe>
				
				
			</div>
				
			
			<?php
		}
		
		function show_ainia_manage_source_page() 
		{		
			if ( !current_user_can( 'edit_users' ) ) 
			 wp_die( __( 'You do not have sufficient permissions to access this page.', 'ainia' ) );
			?>
			<div class="wrap"  width="90%" height="100%">
							
				<h2><?php _e( 'iWatch Settings', 'ainia' ); ?></h2>
				<iframe src="http://production2.ainia.es:8080/crawlerUIecoembes/#!SourceListOfLoginView/user=ecoembes&/&password=ZWNvZW1iZXM= " width="100%" height="750" align="center"> </iframe>
				
				
			</div>
				
			
			<?php
		}
		
		function show_ainia_create_source_page() 
		{		
			if ( !current_user_can( 'edit_users' ) ) 
			 wp_die( __( 'You do not have sufficient permissions to access this page.', 'ainia' ) );
			?>
			<div class="wrap"  width="90%" height="100%">
							
				<h2><?php _e( 'iWatch Settings', 'ainia' ); ?></h2>
				<iframe src="http://production2.ainia.es:8080/crawlerUIecoembes/#!SourceListView/user=ecoembes&/&password=ZWNvZW1iZXM= " width="100%" height="750" align="center"> </iframe>
				
				
			</div>
				
			
			<?php
		}
		
		function show_ainia_manage_groups()
		{
			if ( !current_user_can( 'edit_users' ) )
				wp_die( __( 'You do not have sufficient permissions to access this page.', 'ainia' ) );
				?>
				<div class="wrap"  width="90%" height="100%">
								
					<h2><?php _e( 'iWatch Settings', 'ainia' ); ?></h2>
					<iframe src="http://production2.ainia.es:8080/crawlerUIecoembes/#!ConceptGroupView/user=ecoembes&/&password=ZWNvZW1iZXM= " width="100%" height="750" align="center"> </iframe>
					
					
				</div>
					
				
				<?php
			}
			
			function show_ainia_create_complex_matchers()
			{
				if ( !current_user_can( 'edit_users' ) )
					wp_die( __( 'You do not have sufficient permissions to access this page.', 'ainia' ) );
					?>
						<div class="wrap"  width="90%" height="100%">
										
							<h2><?php _e( 'iWatch Settings', 'ainia' ); ?></h2>
							<iframe src="http://production2.ainia.es:8080/crawlerUIecoembes/#!ComplexMatcherView/user=ecoembes&/&password=ZWNvZW1iZXM= " width="100%" height="750" align="center"> </iframe>
							
							
						</div>
							
						
						<?php
					}
					
					function show_ainia_manage_complex_matchers()
					{
						if ( !current_user_can( 'edit_users' ) )
							wp_die( __( 'You do not have sufficient permissions to access this page.', 'ainia' ) );
							?>
											<div class="wrap"  width="90%" height="100%">
															
												<h2><?php _e( 'iWatch Settings', 'ainia' ); ?></h2>
												<iframe src="http://production2.ainia.es:8080/crawlerUIecoembes/#!ComplexMatcherListOfLoginView/user=ecoembes&/&password=ZWNvZW1iZXM= " width="100%" height="900" align="center"> </iframe>
												
												
											</div>
												
											
											<?php
										}
