<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Classified admin Controller
 *
 * This class handles user account related functionality
 *
 * @package		Show
 * @subpackage	ShowCore
 * @author		webhelios
 * @link		http://webhelios.com
 */



class Grab_model_core extends CI_model {


	public function __construct()
	{
		parent::__construct();
		$this->load->model('admin/content_model');
		$this->load->model('admin/news_model');
	}
	

	public function init()
	{

		$this->load->helper('date');
		$datestring = "%H%i%s";
		$time = time();

		$query = $this->db->get_where('sources',array('status'=>1,'auto_grabbing'=>1));
		$info = "";

		foreach($query->result() as $source)
		{
			$schedules = $this->get_cron_execution_times($source);
			$time = date('h:00:a', time());
			$current_time = floor(time()/3600)*3600;

			if(in_array($current_time, $schedules))
			{
				$this->grabsingle($source->id,'cron');
				$info .= $source->source_name." ".date('Y-m-d h:i:a', time())."\n"; 
			}
		}

		$this->load->helper('file');
		if ( ! write_file('./assets/logs/cron_log.txt', $info,"a+"))
		{
		     #echo 'Unable to write the file';
		}
		else
		{
		     #echo 'File written!';
		}

		if(!isset($_SERVER['SERVER_SOFTWARE']) && (php_sapi_name() == 'cli' || (is_numeric($_SERVER['argc']) && $_SERVER['argc'] > 0)))
		{
			echo 'Cron executed';
			die;
		}

	}

	public function get_cron_execution_times($source)
	{
		$schedule_time = array();
		$schedule_time[0] = strtotime($source->start_on);
		$schedule_time[1] = $schedule_time[0] + $source->grab_duration*60*60;
		$schedule_time[2] = $schedule_time[1] + $source->grab_duration*60*60;
		$schedule_time[3] = $schedule_time[2] + $source->grab_duration*60*60;
		return $schedule_time;
	}

	public function grabsingle($source_id='',$action='cron')
	{
		$source = $this->content_model->get_source_data_by_id($source_id);


		$this->load->library('rssparser');                          // load library
		$this->rssparser->set_feed_url($source->rss_url);  			// get feed
		$this->rssparser->set_cache_life(30);                       // Set cache life time in minutes
		$rss = $this->rssparser->getFeed(20);

		foreach ($rss as $item) 
		{
			// echo '<pre>';
			// print_r($item);
			// die;
			$media_url = (isset($item['media']))?$item['media']:'';
			if($media_url!='')
			{
				$is_media_exists = checkRemoteFile($media_url);
				if($is_media_exists)
				{
					if(get_settings('global_settings','save_images_locally','No')=='Yes')
					{
						$url = image_from_url($item['media']);
						$image_url = $url;
					}
					else
						$image_url = $item['media'];
				}
				else
					$image_url = base_url('uploads/images/no-image.png');
			}
			else
			{
				$image_url = base_url('uploads/images/no-image.png');
			}

			$description = $item['description'];
			$description = get_excerpt($description); 

			$data = array();
			$data['source_id'] 		= $source_id;
			$data['category'] 		= $source->parent_category;
			$data['sub_category'] 	= $source->sub_category;
			$data['title'] 			= $item['title'];
			$data['description'] 	= $description;
			$data['link'] 			= $item['link'];
			$data['media'] 			= $image_url;
			$data['publish_time'] 	= strtotime($item['pubDate']);
			$data['author'] 		= $item['author'];
			$data['search_meta'] 	= $item['title'].' '.$description;

			$result = $this->news_model->check_if_unique_news($data);
			
			if($result<=0)
			{
				$data['grab_time'] 		= time();
				$data['grab_action'] 	= 'manual';
				$data['status'] 		= 1;
				
				$this->news_model->insert_news($data);
			}

		}

		$data = array();
		$data['last_grab_time'] = time();
		$this->db->update('sources',$data,array('id'=>$source_id));

		if($action!='cron')
		{
			$this->session->set_flashdata('msg','<div class="alert alert-success">'.lang_key('news_grabbed').'</div>');
			redirect(site_url('admin/content/allsources'));
		}
	}

}





/* End of file install.php */

/* Location: ./application/modules/show/controllers/show_core.php */