<?php
$wp_include = "../wp-load.php";$i = 0;
while (!file_exists($wp_include) && $i++ < 10) {$wp_include = "../$wp_include";} require($wp_include);
if ( !is_user_logged_in()) 
	wp_die(__("You are not allowed to be here","cloudfw"));


require (TMP_ADMIN_PATH.'/classes/gapi.class.php');
require (TMP_ADMIN_PATH.'/inc/fn.admin.ga.php');
		
		global $_opt;
		$_opt = getCO(NULL, array(PFIX."_ga_id", PFIX."_ga_pass", PFIX."_ga_profile"));


		if (defined("GA_ID")&&defined("GA_PASS")):
		 
		 $ga_id = GA_ID;
		 $ga_pass = GA_PASS;
		 
		else:
		
		 $ga_id = $_opt[PFIX."_ga_id"];
		 $ga_pass = $_opt[PFIX."_ga_pass"];
		
		endif;
	
		
		try
		  {
			$ga = new gapi($ga_id,$ga_pass);
		  }
		  
		catch(Exception $e)
		  {
			 if ($e->getMessage()=="NOAUTH"): echo 'NOAUTH'; 

				update_option(PFIX."_ga_id", NULL);
				update_option(PFIX."_ga_pass", NULL);
				update_option(PFIX."_ga_profile", NULL);
			 
			 exit;
			 
			 
			 endif;
			}
                
		$ga->requestAccountData();
		
		$items = array();
		
		if ($ga->getResults()) {
		
				foreach($ga->getResults() as $result)
				{
				 // $accounts .= $result . ' (' . $result->getProfileId() . ")";
				  
				  $items[] = array(	 "item_value"	=> $result->getProfileId(),
									 "item_class"	=> "",
									 "item_html" 	=> '<span class="contrainer">'.$result.'<br/>('.$result->getProfileId().')</span>'
											 );
				  
				  $first_id ? NULL: $first_id = $result->getProfileId();
				}

						
				

		$profile_id = $_opt[PFIX."_ga_profile"];
		
		
		if (($profile_id=="")): update_option(PFIX."_ga_profile", $first_id); $profile_id =  $first_id; endif;
		
		echo '
		
			 <!-- ROW -->
                        <div class="grid oneof4"><span class="title">'.__('Your Analytics Profiles:','cloudfw').'</span> </div>
                        <div class="grid threeof4 last">',
						
						($profile_id) ? NULL : '<div style="margin:0 0 15px 0;">'.__('Please select a profile to see in cloudfw dashboard','cloudfw').'</div>'
						
						,'',
						
						
						admin_create_selectlist("ga_profile", // id
								   $items, // items
								   'EMPTY', //default_value
								   "radio",
								   $profile_id,
								   'custom_label ga_profiles'
								   )
						
						
						
						,'
						<br />
						<br />

						<a href="'.TMP_ADMIN.'/ajax/del.php?ga=true&nonce='.wp_create_nonce('cloudfw').'" class="small-button small-grey"><span>'.__('Connect To A Different Account','cloudfw').'</span></a>
						</div>
						<div class="clear"></div>
			 <!-- /ROW -->			
		
		
		';
		
		}else{
		// no profile	
		
			echo 'No Profile';
			
		};

?>