<?php
/**
 * EGroupware - Infolog iCal import
 *
 * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
 * @package calendar
 * @subpackage importexport
 * @link http://www.egroupware.org
 * @author Nathan Gray
 * @copyright Nathan Gray
 * @version $Id$
 */

/**
 * import ical for infolog
 * Not a lot of options for this, just uses the ical class
 */
class infolog_import_ical implements importexport_iface_import_plugin  {

	private static $plugin_options = array(
		'conditions',		/* => array containing condition arrays:
				'type' => exists, // exists
				'string' => '#kundennummer',
				'true' => array(
					'action' => update,
					'last' => true,
				),
				'false' => array(
					'action' => insert,
					'last' => true,
				),*/
	);

	/**
	 * actions wich could be done to data entries
	 */
	protected static $actions = array();

	/**
	 * conditions for actions
	 *
	 * @var array
	 */
	protected static $conditions = array();

	/**
	 * @var definition
	 */
	private $definition;

	/**
	 * @var bo
	 */
	private $bo;

	/**
	 * @var bool
	 */
	private $dry_run = false;

	/**
	 * List of import warnings
	 */
	protected $warnings = array();

	/**
	 * List of import errors
	 */
	protected $errors = array();

	/**
	 * List of actions, and how many times that action was taken
	 */
	protected $results = array();

	/**
	 * imports entries according to given definition object.
	 * @param resource $_stream
	 * @param string $_charset
	 * @param definition $_definition
	 */
	public function import( $_stream, importexport_definition $_definition ) {

		$this->definition = $_definition;

		// dry run?
		$this->dry_run = isset( $_definition->plugin_options['dry_run'] ) ? $_definition->plugin_options['dry_run'] :  false;

		$success = 0;

		// Failures
		$this->errors = array();

		@set_time_limit(0);     // try switching execution time limit off

		$infolog_ical = new infolog_ical();
		$infolog_ical->entry_callback = array($this, 'entry_callback');
		if (!$infolog_ical->importVTODO(stream_get_contents($_stream)))
		{
			$this->errors[] = lang('Error: importing the iCal');
		}
		else
		{
			$success++;
		}
		$this->results['imported'] = $success;

		return $success;
	}

	/**
	 * Do some modification on each entry
	 */
	public function entry_callback(&$event)
	{
		// Check & apply value overrides
		foreach((array)$this->definition->plugin_options['override_values'] as $field => $settings)
		{
			if($settings['value'])
			{
				$event[$field] = $settings['value'];
			}
		}
		return true;
	}

	/**
	 * returns translated name of plugin
	 *
	 * @return string name
	 */
	public static function get_name() {
		return lang('Infolog iCal import');
	}

	/**
	 * returns translated (user) description of plugin
	 *
	 * @return string descriprion
	 */
	public static function get_description() {
		return lang("Imports TODOs into Infolog from an iCal File.");
	}

	/**
	 * retruns file suffix(s) plugin can handle (e.g. csv)
	 *
	 * @return string suffix (comma seperated)
	 */
	public static function get_filesuffix() {
		return 'ics';
	}

	/**
	 * return etemplate components for options.
	 * @abstract We can't deal with etemplate objects here, as an uietemplate
	 * objects itself are scipt orientated and not "dialog objects"
	 *
	 * @return array (
	 * 		name 		=> string,
	 * 		content		=> array,
	 * 		sel_options => array,
	 * 		preserv		=> array,
	 * )
	 */
	public function get_options_etpl(importexport_definition &$definition=null)
	{
		// lets do it!
	}

	/**
	 * returns etemplate name for slectors of this plugin
	 *
	 * @return string etemplate name
	 */
	public function get_selectors_etpl() {
		// lets do it!
	}

	/**
	 * Returns warnings that were encountered during importing
	 * Maximum of one warning message per record, but you can append if you need to
	 *
	 * @return Array (
	 *       record_# => warning message
	 *       )
	 */
	public function get_warnings() {
		return $this->warnings;
	}

	/**
	 * Returns errors that were encountered during importing
	 * Maximum of one error message per record, but you can append if you need to
	 *
	 * @return Array (
	 *       record_# => error message
	 *       )
	 */
	public function get_errors() {
		return $this->errors;
	}

	/**
	 * Returns a list of actions taken, and the number of records for that action.
	 * Actions are things like 'insert', 'update', 'delete', and may be different for each plugin.
	 *
	 * @return Array (
	 *       action => record count
	 * )
	 */
	public function get_results() {
		return $this->results;
	}
}
