/**
	 * Called, when Plugin activated.
	 * 
	 * Creates or updates the options to latest version
	 * 
	 * Creates folder for upload files and initialises options, if not present
	 */
	public function on_activate() 
	{	
		//	We need WC -> if WC is not active, do not allow to activate the plugin, because we cannot load the correct version (backward compatibility)
		if ( ! function_exists('is_plugin_active') ) 
		{
			require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
		}
		if( ! is_plugin_active( 'woocommerce/woocommerce.php' ) )
		{
			deactivate_plugins( WC_Email_Att::$plugin_base_name );
			wp_die( __('<p>The plugin <strong>WooCommerce Email Attachments</strong> needs the plugin WooCommerce to be able to be activated. Please activate this plugin first. Plugin could not be activated.</p>', WC_Email_Att::TEXT_DOMAIN ), __( 'Plugin Activation Error', WC_Email_Att::TEXT_DOMAIN ),  array( 'response'=> 200, 'back_link' => TRUE ) );
		}
		
		//	See Documentation WP register_post_type
		WC_Email_Att::instance()->handler_wp_register_cpt_email_att();
		flush_rewrite_rules();
		
		$this->options = WC_Email_Att::get_options_default();
		
			//	test for previous versions, that need update - Optionname changed with 3.0.0
		$need_update = $this->check_for_update();
		
		//	Checks and creates new Upload Folder with fallback
		if( ! WC_Email_Att_Func::create_folder( $this->options['upload_folder'] ) )
		{
			$this->options['upload_folder'] = '/wc_email_attachment_uploads';
			update_option( WC_Email_Att::OPTIONNAME, $this->options );
			if( ! WC_Email_Att_Func::create_folder( $this->options['upload_folder'] ) )
			{
				deactivate_plugins( WC_Email_Att::$plugin_base_name );
				wp_die( __( '<p>The plugin <strong>WooCommerce Email Attachments</strong> encountered an error on creating the new upload folder. Try again or check your permissons or the permissons of Wordpress with your administrator. Plugin could not be activated.</p>', WC_Email_Att::TEXT_DOMAIN ), __( 'Plugin Activation Error', WC_Email_Att::TEXT_DOMAIN ),  array( 'response'=> 200, 'back_link' => TRUE ) );
			}
		}
				
		if( ! empty( $need_update ) )
		{
			update_option( WC_Email_Att::OPTIONNAME_UPDATE, $need_update );
		}
	}
	/**
	 * Saves the options in own option entry
	 */
	public function save_settings_page_options()
	{
		$this->options['del_on_deactivate'] = (WC_Email_Att::$show_activation) ? isset( $_REQUEST[ self::PREFIX_JS_NAMES . 'del_on_deactivate' ] ) : false;
		$this->options['del_on_uninstall'] = (WC_Email_Att::$show_uninstall) ? isset( $_REQUEST[self::PREFIX_JS_NAMES . 'del_on_uninstall'] ) : true;
		
		$this->options['notification_headline'] = (isset( $_REQUEST[ self::PREFIX_JS_NAMES . 'notification_headline']) && ! empty( $_REQUEST[self::PREFIX_JS_NAMES . 'notification_headline' ] ) ) ? $_REQUEST[ self::PREFIX_JS_NAMES . 'notification_headline' ] : '';
		$this->options['notification_headline'] =  trim( stripslashes( $this->options['notification_headline'] ) );
		
		$this->options['notification_text'] = (isset( $_REQUEST[ self::PREFIX_JS_NAMES . 'notification_text']) && ! empty( $_REQUEST[self::PREFIX_JS_NAMES . 'notification_text'])) ? $_REQUEST[self::PREFIX_JS_NAMES . 'notification_text' ] : '';
		$this->options['notification_text'] = trim( stripslashes( $this->options['notification_text'] ) );
	
		$this->options['show_notes_always'] = isset( $_REQUEST[ self::PREFIX_JS_NAMES . 'show_notes_always' ] );
		
		$oldpath = $newpath = strtolower( $this->options['upload_folder'] );
		if( isset( $_REQUEST[ self::PREFIX_JS_NAMES . 'upload_folder' ] ) )
		{
			$newpath = strtolower( trim( strtolower( stripslashes( $_REQUEST[ self::PREFIX_JS_NAMES . 'upload_folder'] ) ) ) );
			$newpath = str_replace( '\\', '/', $newpath );
			$newpath = (strlen( $newpath ) == 0 ) ? $oldpath : untrailingslashit( $newpath );
			$newpath = '/' . ltrim( $newpath, '/' );
			
			if( $oldpath != $newpath )
			{
				if( WC_Email_Att_Func::create_folder( $newpath ) )
				{
					$this->options['upload_folder'] = $newpath;
				}
				WC_Email_Att_Func::remove_empty_folder( $oldpath, WC_Email_Att::$skip_files );
			}
		}
		foreach ( $this->emailsubjects as $email_key => $emailclass ) 
		{
			$opt_key = $this->inputfields_param[ $email_key ]['id'];
			$this->options[ $opt_key ] = $this->get_opt_emailaddress( self::PREFIX_JS_NAMES . 'head_', $email_key );
			
			$opt_key = $this->inputfields_param[ $email_key ]['att_id'];
			$this->options[ $opt_key ] = $this->get_opt_attachment_files( self::PREFIX_JS_NAMES . 'att_', $email_key );
		}
		
		$this->options = apply_filters( 'wc_eatt_save_settings_options', $this->options, $this->emailsubjects, $this->inputfields_param );
		
		update_option( WC_Email_Att::OPTIONNAME, $this->options );
		
		WC_Email_Att::instance()->options = $this->options;
		
		WC_Email_Att::$admin_message = 'Options saved';
	}