<?php

// This will add the theme options panel if the theme has this support

/*
*	TO DO :
*	Separate theme page css from HTML class css
*
*/

class Zn_Theme_Setup {

	var $theme_pages = array();

	function __construct(){

		global $pagenow;

		add_action('admin_menu', array(&$this, 'zn_add_admin_pages'));
		
		$this->theme_data = ZN()->theme_data;

		// LOAD THE BACKEND FILES
		add_action( 'admin_enqueue_scripts', array(&$this, 'zn_print_scripts') );

		// CHECK IF THEME WAS ACTIVATED
		if ( ( is_admin() && isset( $_GET['activated'] ) && $pagenow == 'themes.php' ) || ( is_admin() && $pagenow == 'customize.php' ) ) {
			add_action( 'zn_framework_init', array( &$this, 'do_theme_activation' ) );
		}

		add_action( 'zn_framework_init', array( &$this, 'zn_check_theme_version' ) );
		add_action( 'admin_init', array(&$this, 'zn_permalink_settings_init') );
		add_action( 'admin_init', array(&$this, 'zn_permalink_settings_save') );
	}

	/*--------------------------------------------------------------------------------------------------
		Save the permalinks options
	--------------------------------------------------------------------------------------------------*/
	function zn_permalink_settings_save() {
		if ( ! is_admin() )
			return;

		// We need to save the options ourselves; settings api does not trigger save for the permalinks page
		if ( isset( $_POST['zn_permalinks'] ) /*|| isset( $_POST['zn_portfolio_item_slug_input'] ) */ ) {
			$permalinks = $_POST['zn_permalinks'];
			update_option( 'zn_permalinks', $permalinks );
			flush_rewrite_rules();
		}
	}

	/*--------------------------------------------------------------------------------------------------
		Add options for the portfolio and Documentation
	--------------------------------------------------------------------------------------------------*/
	static public function permalink_callback( $field ) {

		$permalinks = get_option( 'zn_permalinks' );

		?>
			<input name="zn_permalinks[<?php echo $field['id']; ?>]" type="text" class="regular-text code" value="<?php if ( isset( $permalinks[$field['id']] ) ) echo esc_attr( $permalinks[$field['id']] ); ?>" placeholder="<?php echo $field['id']; ?>" />
		<?php
	}

	function zn_permalink_settings_init() {

		$post_types = array();
		$taxonomies = array();
		$this->zn_allowed_post_types = apply_filters( 'zn_allowed_post_types', $post_types );
		$this->zn_allowed_taxonomies = apply_filters( 'zn_allowed_taxonomies', $taxonomies );

		foreach ( $this->zn_allowed_post_types as $id => $name) {

			$post_type_section_id = 'zn-'.$id.'-permalink';
			
			// SECTION : UNIQUE ID, NAME, CALLBACK, SETTINGS PAGE
			add_settings_section( $post_type_section_id, $name.' Slugs', '', 'permalink' );

			$this->add_settings_field( $id, $name, $post_type_section_id );

			if ( !empty( $this->zn_allowed_taxonomies[$id] ) ) {

				$current_taxonomies = $this->zn_allowed_taxonomies[$id];
				foreach ( $current_taxonomies as $key => $taxonomy) {
					$this->add_settings_field( $taxonomy['id'], $taxonomy['name'], $post_type_section_id );
				}

			}

		}
	}

	function add_settings_field( $id, $name, $section ) {

		// Add Slug option
		add_settings_field(
			$id,      	// id
			$name .' item slug', 	// setting title
			array('Zn_Theme_Setup','permalink_callback'),  // display callback
			'permalink',                 				// settings page
			$section,                 				// settings section
			array(
				'id'	=> $id
			)
		);
	}

	/**
	 * Checks if theme is updated
	 * TODO : Check this code agains the install functions
	 * @access public
	 */
	function zn_check_theme_version(){

		// Check if theme was updated
		$theme_version_option_name = 'zn_'.ZN()->theme_data['theme_id'].'_version';
		//delete_option( $theme_version_option_name );
		$old_theme_version = get_option( $theme_version_option_name );
		$new_theme_version = ZN()->theme_data['version'];
		if ( empty( $old_theme_version ) ) { $old_theme_version = '1.0.0'; }

		if ( version_compare( $new_theme_version, $old_theme_version, '>' ) ){
			// Do something when the theme is updated
			update_option( $theme_version_option_name, $new_theme_version );

			// Let the theme hook here :)
			do_action( 'zn_theme_updated', $old_theme_version, $new_theme_version );
		}
	}

	function get_theme_options_pages(){

		if ( !file_exists(THEME_BASE.'/template_helpers/options/theme-pages.php') ) { return array(); }
		include( THEME_BASE.'/template_helpers/options/theme-pages.php');
		return apply_filters( 'zn_theme_pages', $admin_pages );
	}

	function get_theme_options(){
		include(THEME_BASE.'/template_helpers/options/theme-options.php');
		return apply_filters( 'zn_theme_options', $admin_options );
	}


	function zn_add_admin_pages(){

		$this->data['theme_pages'] = $this->get_theme_options_pages();

		$i = 0;

		foreach ( $this->data['theme_pages'] as $key => $value ) {
				
				if ( $i == 0 ){
					$this->theme_pages[] = add_menu_page(
						$value['title'],
						ZN()->theme_data['name'] .' Options', 
						'manage_options',
						'zn_tp_'.$key, 
						array(&$this, 'zn_render_page')
					);

					$main_page = 'zn_tp_'.$key;

				}

				/* CREATE THE SUBPAGES */
				$this->theme_pages[] = add_submenu_page (
						$main_page,
						$value['title'],
						$value['title'],
						'manage_options',
						'zn_tp_'.$key,
						array(&$this, 'zn_render_page')
					);
			$i++;

		}
	}

	function zn_print_scripts( $hook ){

		/* Set default theme pages where the js and css should be loaded */
		$this->theme_pages[] = 'post.php';
		$this->theme_pages[] = 'post-new.php';
		$this->theme_pages[] = 'widgets.php';
		$this->theme_pages   = apply_filters( 'zn_theme_pages', $this->theme_pages );

		if ( !in_array( $hook, $this->theme_pages ) ) {
			return;
		}

		// LOAD CUSTOM SCRIPTS
		wp_enqueue_script( 'zn_theme_ajax_callback', FW_URL .'/assets/js/zn_theme_ajax_callback.js', 'jquery','',true );
		add_action('admin_print_styles', array( &$this, 'admin_css' ) );

		ZN()->load_html_scripts();
	}

	function admin_css(){
		echo '<!-- ICON FONTS CSS -->';
		echo '<style type="text/css">';
			echo ZN()->icon_manager->set_css( '' );
		echo '</style>';
	}

	function zn_render_page() {

		// Get the curent slug
		$slug = $_GET['page'];
		$slug = str_replace( 'zn_tp_', '', $slug );
		$this->data['slug'] = $slug;

		
		$this->data['theme_options'] = $this->get_theme_options();
		ZN()->html()->zn_set_data( $this->data );
		
		echo ZN()->html()->zn_page_start();

		echo ZN()->html()->zn_render_page_options();

		echo ZN()->html()->zn_page_end();
	}

	/*--------------------------------------------------------------------------------------------------
		This function will add all STD values in DB if the theme options files is not present
	--------------------------------------------------------------------------------------------------*/
	function do_theme_activation() {

		$options_field = ZN()->theme_data['options_prefix'];
		$saved_values = array();

		//delete_option(  $options_field );

		// IF THE OPTIONS FIELD IS NOT PRESENT
		if ( false === get_option( $options_field ) )
		{

			$file =  THEME_BASE."/template_helpers/dummy_content/theme_options.txt";
			if( ! is_file( $file ) ) {
				include(THEME_BASE.'/template_helpers/options/theme-options.php');

				foreach ( $admin_options as $key => $option ) {

					if( !empty( $option['std'] ) )
					{
						$saved_values[$option['parent']][$option['id']] = $option['std'];
					}

				}
			}
			else{
				$data = file_get_contents( $file );
				$saved_values = json_decode( $data, true );
			}
			
			update_option( $options_field , $saved_values );
			generate_options_css( $saved_values );
			ZN()->pagebuilder->refresh_pb_data();

		}
		do_action('zn_theme_installed');
	}

	function import_theme_options(){

		$file =  THEME_BASE."/template_helpers/dummy_content/theme_options.txt";
		if( ! is_file( $file ) ) {
			return false;
		}
		$data = json_decode( $data, true );
	}


}


?>