<?php

if ( ! class_exists( 'GFForms' ) ) {
	die();
}

class Wiaas_Field_Order_Document extends GF_Field_FileUpload {

	public $type ='wiaas_order_document';

	public $wiaasDocTypeFilter = 'install_guide';

	public function get_form_editor_field_settings() {
		return array(
			'wiaas_doc_type_filter',
			'multiple_files_setting',
			'conditional_logic_field_setting',
			'error_message_setting',
			'label_setting',
			'label_placement_setting',
			'admin_label_setting',
			'rules_setting',
			'file_extensions_setting',
			'file_size_setting',
			'visibility_setting',
			'description_setting',
			'css_class_setting',
		);
	}

	public function get_input_type() {
		return 'fileupload';
	}

	public function get_form_editor_field_title() {
		return esc_attr__( 'Order Document', 'wiaas' );
	}

	public function add_button( $field_groups ) {
		$field_groups = Wiaas_Order_Fields::maybe_add_order_field_group($field_groups);

		return parent::add_button( $field_groups );
	}

	public function get_form_editor_button() {
		return array(
			'group' => 'wiaas_order_fields',
			'text'  => $this->get_form_editor_field_title(),
		);
	}

	public function get_download_url( $file, $force_download = false ) {

		$upload_root = GFFormsModel::get_upload_url( $this->formId );
		$upload_root = trailingslashit( $upload_root );

		// handle download for order documents not uploaded by gravity forms
		if ( strpos( $file, $upload_root ) === false ) {

			return admin_url() . '?gf-wiaas-order-doc=' . urlencode($file);
		}

		return parent::get_download_url( $file, true );
	}

	public function sanitize_settings() {
		parent::sanitize_settings();

		$this->wiaasDocTypeFilter = sanitize_key($this->wiaasDocTypeFilter);
	}
}


GF_Fields::register( new Wiaas_Field_Order_Document() );