<?php
class afeblogs
{
	private $db;
	function __construct() 
	{
		$this->db = $GLOBALS['wpdb'];
	}

	function afe_article_category($cat_id)
	{
		$api_key='0f98c411ea2016f0a1df9ef46573d676';
		$category='';

		if(!empty($api_key)){
			$url='http://articlestorze.com/api/category.php?api_key='.$api_key.'&cat_id='.$cat_id;
			$ch = curl_init($url);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
			$data = curl_exec($ch);
			curl_close($ch);
			$category=json_decode($data);
			
		}
		
		return $category;
	}

	function afe_search_result($afe_subcat,$afe_key)
	{
		global $wpdb;
		$afe_output = array();
		$afe_test = str_replace( ', ', ' ', $afe_subcat );
        $afe_split_string=explode(" ",$afe_test);
        $afe_key = str_replace( ' ', '+', $afe_key );
        
		$afe_api_key='0f98c411ea2016f0a1df9ef46573d676';
		if(!empty($afe_api_key) && !empty($afe_split_string))
		{
    	    foreach($afe_split_string as $afe_split_string1)
            {
    		    $afe_url='http://articlestorze.com/api/article_search.php?api_key='.$afe_api_key.'&keyword='.trim($afe_key).'&cat_id='.$afe_split_string1;
	  
        		$ch = curl_init($afe_url);
        		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        		$afe_data = curl_exec($ch);
        		 
        		curl_close($ch);
        		$afe_resp=json_decode($afe_data);
		        foreach((array)$afe_resp as $afe_resp11)
		        {
					$afe_output[]= $afe_resp11;					  
		        }
            }
            
            if(is_array($afe_output))
            {
    		    $afe_tmp_table = $wpdb->prefix . "afe_tmp_table";
        	    $wpdb->query('DELETE  FROM '.$afe_tmp_table.' WHERE type = "article"');
	            foreach($afe_output as $afe_resp1)
        		{
              	   $afe_tmp_data=array(
                                        'catg_id' => $afe_resp1->id, 
                                        'title' => $afe_resp1->title, 
                                        'link' => $afe_resp1->link, 
                                        'short_des' => $afe_resp1->short_desc, 
                                        'long_desc' => $afe_resp1->long_desc, 
                                        'type' => 'article' 
                                       );


                    $wpdb->insert( $afe_tmp_table, $afe_tmp_data);
	            }
	        }
		}
		return $afe_resp;
	}
	
	
	function afe_youtube_search_keyword($afe_vid_keyword)
	{
		global $wpdb;
        $video_key='AIzaSyDcQz6PgLYw66WBi1T64ZW4ey7d3rTPpt0';
        $client = new Google_Client_v1();
        $client->setDeveloperKey($video_key);
        $youtube = new Google_YouTubeService($client);
        try 
        {
            $searchResponse = $youtube->search->listSearch('id,snippet', array(
                'q' => $afe_vid_keyword,
                'maxResults' => 50,
				'order'=>'date'
	        ));
            $result_output=array();
            $videos = '';
            $channels = '';
        
			foreach ($searchResponse['items'] as $afe_key_resp) 
			{
				if ($afe_key_resp['id']['kind'] == "youtube#video") 
				{
					$afe_name = stripslashes(str_replace('"','`',$afe_key_resp['snippet']['title']));
					
					$afe_vid_id = $afe_key_resp['id']['videoId'];
					
					$afe_vid_desc = stripslashes(str_replace('"','`',$afe_key_resp['snippet']['description']));
					
					$afe_thum_img_vid_url = $afe_key_resp['snippet']['thumbnails']['default']['url'];
					$afe_link_array=array('link'=>$afe_vid_id,'thum_img_url'=>$afe_thum_img_vid_url);
					if($afe_vid_keyword)
					{
						if(count($result_output) < 50)
						{
							$result_output[]=array('title'=>$afe_name,'link'=>$afe_link_array,'short_desc'=>$afe_vid_desc);
						}
					}	
				}
			}
        } 
        catch (Google_ServiceException $e) 
        {
            $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
            htmlspecialchars($e->getMessage()));
        }
        catch (Google_Exception $e) 
        {
             $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',
             htmlspecialchars($e->getMessage()));
        }
        
        if(is_array($result_output))
        {
		    $afe_video_tmp_table = $wpdb->prefix . "afe_tmp_table";
            $wpdb->query('DELETE  FROM '.$afe_video_tmp_table.' WHERE type = "video"');
            foreach($result_output as $result_output1)
    		{
    		    $afe_link_data=serialize($result_output1["link"]);
          	    $afe_video_tmp_data=array(
                                    'catg_id' => '', 
                                    'title' => $result_output1["title"], 
                                    'link' => $afe_link_data, 
                                    'short_des' => $result_output1["short_desc"], 
                                    'long_desc' => '', 
                                    'type' => 'video' 
                                   );


                $wpdb->insert( $afe_video_tmp_table, $afe_video_tmp_data);
            }
        }
        return $afe_key_resp;
	}
	function afe_youtube_search_keyword1($afe_vid_keyword1)
	{
		global $wpdb;
        $video_key1='AIzaSyDcQz6PgLYw66WBi1T64ZW4ey7d3rTPpt0';
        $client1 = new Google_Client_v1();
        $client1->setDeveloperKey($video_key1);
        $youtube1 = new Google_YouTubeService($client1);
        try 
        {
            $searchResponse1 = $youtube1->search->listSearch('id,snippet', array(
                'q' => $afe_vid_keyword1,
                'maxResults' => 1,
				'order'=>'date'
	        ));
            $result_output1=array();
            $videos1 = '';
            $channels1 = '';
        
			foreach ($searchResponse1['items'] as $afe_key_resp1) 
			{
				if ($afe_key_resp1['id']['kind'] == "youtube#video") 
				{
					$afe_name1 = stripslashes(str_replace('"','`',$afe_key_resp1['snippet']['title']));
					
					$afe_vid_id1 = $afe_key_resp1['id']['videoId'];
					
					$afe_vid_desc1 = stripslashes(str_replace('"','`',$afe_key_resp1['snippet']['description']));
					
					$afe_thum_img_vid_url1 = $afe_key_resp1['snippet']['thumbnails']['default']['url'];
					$afe_link_array1=array('link'=>$afe_vid_id1,'thum_img_url'=>$afe_thum_img_vid_url1);
					if($afe_vid_keyword1)
					{
						if(count($result_output1) < 1)
						{
							$result_output1[]=array('link'=>$afe_link_array1);
						}
					}	
				}
			}
        } 
        catch (Google_ServiceException $e) 
        {
            $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
            htmlspecialchars($e->getMessage()));
        }
        catch (Google_Exception $e) 
        {
             $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',
             htmlspecialchars($e->getMessage()));
        }
        
        foreach($result_output1 as $result_output11)
		{
		    $option_name = 'vid_tmp_camp';
		    $afe_link_data1= $result_output11["link"];
		    $campaign_post_vid = $afe_link_data1['link'];
		    if ( get_option( $option_name ) !== false ) 
		    {
                update_option( $option_name, $campaign_post_vid );
             
            } 
            else 
            {
    		    add_option( $option_name, $campaign_post_vid );
            }
        }
        return $afe_key_resp1;
	}
	

}


?>