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

/**
 * Video Player - by Duktee
 *
 * @package		Video Player
 * @version		Version 2.5
 * @author		Benjamin David
 * @copyright	Copyright (c) 2011 - Duktee
 * @license		http://duktee.com/addons/video-player/license
 * @link		http://duktee.com/addons/video-player/
 *
 */
 
class Videoplayer_ft extends EE_Fieldtype {

	var $info = array(
		'name'		=> 'Video Player',
		'version'	=> '2.5'
	);
	
	var $has_array_data = TRUE;
	
	/**
	 * Constructor
	 *
	 */
	function __construct()
	{
	
		parent::EE_Fieldtype();

		
		// load video player package
		
		$this->EE->load->add_package_path(PATH_THIRD . 'videoplayer/');
		$this->EE->lang->loadfile('videoplayer');
		$this->EE->load->library('videoplayer_lib');
		
		
		// prepare cache for head files
		
		if (! isset($this->EE->session->cache['videoplayer']['head_files']))
		{
			$this->EE->session->cache['videoplayer']['head_files'] = false;
		}

	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Install Function
	 *
	 * @access	public
	 * @return	array
	 */
	function install()
	{
		return array(
			'video_url'	=> '',
		);
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Display Field
	 *
	 * @access	public
	 * @param	array
	 * @return	array
	 */
	function display_field($data)
	{
		return $this->_display_field($data);
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Matrix Compatibility
	 *
	 * @access	public
	 * @param	array
	 * @return	array
	 */
	function display_cell($data)
	{
		return $this->_display_field($data, 'matrix');
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Common function for native and Matrix display field
	 *
	 * @access	private
	 * @param	array
	 * @param	string
	 * @return	string
	 */
	function _display_field($data, $type=false)
	{
		
		/* -------------------------------------------
		/* this is our Videoplayer endpoint when in CP
		*/
			$this->EE->videoplayer_lib->endpoint();
		/*
		/* -------------------------------------------*/
		
		$this->load_head_files();
		
		$ret = '<div class="videoplayer-field">';
		
			$ret .= '<div class="videoplayer-field-preview">';
			
			$video_url = urldecode($data);
			
			$video_opts = array();
			$video_opts['url'] = $video_url;			
			$video_opts['width'] = 376;
			$video_opts['height'] = 250;
			
			$video = $this->EE->videoplayer_lib->get_video($video_opts);
			
			if($video['id'])
			{
				$ret .= $video['embed'];
			}
			
			$ret .= '</div>';
			$ret .= '<div class="videoplayer-addremove"><p><a href="#" class="videoplayer-add">Add Video</a><a href="#" class="videoplayer-remove">Remove Video</a></p></div>';
			
			if($type == "matrix")
			{
				$ret .= form_hidden($this->cell_name, $data);
			}
			else
			{
				$ret .= form_hidden($this->field_name, $data);
			}

		$ret .= '</div>';
		
		return $ret;
	}
	
	// --------------------------------------------------------------------

	/**
	 * Display fieldtype global settings
	 *
	 * @access	public
	 * @return	string
	 */
	function display_global_settings()
	{
		$license_key = isset($this->settings['license_key']) ? $this->settings['license_key'] : '';


		// load the language file
		
		$this->EE->lang->loadfile('videoplayer');

		
		// load the table lib
		
		$this->EE->load->library('table');


		// use the default template
		
		$this->EE->table->set_template(array(
			'table_open'    => '<table class="mainTable padTable" border="0" cellspacing="0" cellpadding="0">',
			'row_start'     => '<tr class="even">',
			'row_alt_start' => '<tr class="odd">'
		));

		$this->EE->table->set_heading(array('data' => lang('preference'), 'style' => 'width: 30%'), lang('setting'));

		$this->EE->table->add_row(
			lang('license_key', 'license_key'),
			form_input('license_key', $license_key, 'id="license_key" size="40"')
		);

		return $this->EE->table->generate();
	}

	// --------------------------------------------------------------------
	
	/**
	 * Save fieldtype global settings
	 *
	 * @access	public
	 * @return	array
	 */
	function save_global_settings()
	{
		return array(
			'license_key' => isset($_POST['license_key']) ? $_POST['license_key'] : ''
		);
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * {video} - Rendering Tag & Tag Pair 
	 *
	 * @access	public
	 * @return	string
	 */
	function replace_tag($data, $params = array(), $tagdata = FALSE)
	{
		if(empty($data))
		{
			return "";
		}
	
		$params['url'] = $data;
		
		$this->video = $this->EE->videoplayer_lib->get_video($params);
		
		$video = $this->video;		

		if($tagdata)
		{
		
			// rendering {video}{/video} pair
			
			if($video['video_found'])
			{
			
				// date format
				
				$local_date = $video['date'];
				
				if (preg_match_all("#".LD."date format=[\"|'](.+?)[\"|']".RD."#", $tagdata, $matches))
				{
					foreach ($matches['1'] as $match)
					{
						$tagdata = preg_replace("#".LD."date format=.+?".RD."#", $this->EE->localize->decode_date($match, $local_date), $tagdata, 1);
					}
				}

			}
			
			$conditionals = $this->EE->functions->prep_conditionals($tagdata, $video);
			
			$out = $this->EE->functions->var_swap($conditionals, $video);
		}
		else
		{
			// rendering {video}
			
			if($video['video_found'])
			{
				$out = $video['embed'];				
			}
			else
			{
				$out = $video['error'];
			}
		}

		return $out;

	}
	
	// --------------------------------------------------------------------
	
	/**
	 * {video:single} Alias for replace_tag()
	 *
	 * @access	public
	 * @return	bool
	 */
	function replace_single($data, $params = array(), $tagdata = FALSE)
	{
		return $this->replace_tag($data, $params, $tagdata);
	}	
	
	// --------------------------------------------------------------------
	
	/**
	 * {video:details} Alias for replace_tag()
	 *
	 * @access	public
	 * @return	bool
	 */
	function replace_details($data, $params = array(), $tagdata = FALSE)
	{
		return $this->replace_tag($data, $params, $tagdata);
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * {video:pair} Alias for replace_tag()
	 *
	 * @access	public
	 * @return	bool
	 */
	function replace_pair($data, $params = array(), $tagdata = FALSE)
	{
		return $this->replace_tag($data, $params, $tagdata);
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Check if video exists
	 *
	 * @access	public
	 * @return	bool
	 */
	 
	function replace_video_exists($data, $params=array(), $tagdata=false)
	{
		$url = $data;
		
		return $this->EE->videoplayer_lib->video_exists($url);
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Load heading files once
	 *
	 * @access	private
	 * @return	void
	 */
	function load_head_files()
	{
		$version = $this->info['version'];

		if (!$this->EE->session->cache['videoplayer']['head_files'])
		{
		
			$js = "	var VideoPlayer = VideoPlayer ? VideoPlayer : new Object();
					VideoPlayer.box_ajax_url = '".$this->EE->videoplayer_lib->endpoint_url('box')."';
					VideoPlayer.search_ajax_url = '".$this->EE->videoplayer_lib->endpoint_url('search')."';
					VideoPlayer.video_ajax_url = '".$this->EE->videoplayer_lib->endpoint_url('video')."';
				";

			$this->EE->cp->add_to_head('<script type="text/javascript">' . $js . '</script>');
			$this->EE->cp->add_to_head('<link rel="stylesheet" type="text/css" href="'.$this->_theme_url().'videoplayer.css?'.$version.'" />');
			$this->EE->cp->add_to_head('<script charset="utf-8" type="text/javascript" src="'.$this->_theme_url().'videoplayer.js?'.$version.'"></script>');
			
			$this->EE->session->cache['videoplayer']['head_files'] = true;
		}
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Theme URL
	 *
	 * @access	private
	 * @return	string
	 */
	private function _theme_url()
	{
		$url = $this->EE->config->item('theme_folder_url')."third_party/videoplayer/";
		return $url;
	}		
	
	// --------------------------------------------------------------------

	/**
	 * No Video Tag (DEPRECATED)
	 *
	 * @access	public
	 * @return	bool
	 */
	function replace_no_video($data, $params = array(), $tagdata = FALSE)
	{
		return false;
	}
	
	// --------------------------------------------------------------------

}
// END Videoplayer_ft class

/* End of file ft.videoplayer.php */
/* Location: ./system/expressionengine/third_party/videoplayer/ft.videoplayer.php */