<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $yeepdf_settings_forminator;
class Yeepdf_Creator_Forminator_Backend {
	private $processing = false;
	private $pdf_lists = array();
	private $data;
	private $email;
	private static $add_on ="pdf"; 
	private static $form ="forminator";
	function __construct(){
		add_filter( "forminator_custom_form_mail_attachment", array($this,"forminator_custom_form_mail_attachment"),10,4 );
		add_filter("yeepdf_shortcodes",array($this,"add_shortcode"));
		add_action("yeepdf_head_settings",array($this,"add_head_settings"));
		add_action("save_post_yeepdf",array( $this, 'save_metabox' ), 10, 2 );
		add_filter( "forminator_custom_form_mail_data", array($this,"forminator_custom_form_mail_data"));
		add_action( "forminator_custom_form_mail_before_send_mail", array($this,"forminator_custom_form_mail_before_send_mail") );
	}
		function add_head_settings($post){
		global $wpdb;
		if( isset($_GET["post"] ) ) {
			$post_id= sanitize_text_field($_GET["post"]);
		}else{
			$post_id= $post->ID;
		}
		$data = get_post_meta( $post_id,'_pdfcreator_forminator',true);
        ?>
        <div class="yeepdf-testting-order">
            <select name="pdfcreator_forminator" class="builder_pdf_woo_testing pdfcreator_forminator">
			<option value='-1'>--- <?php esc_html_e("Forminator","pdf-for-wpforms") ?> ---</option>
                <?php
                $args = array(
					'numberposts' => -1,
					'post_type'   => 'forminator_forms'
					);
				$wpforms = get_posts( $args );
				if( $wpforms  ){
					foreach ( $wpforms as $post ) {
						$form_id = $post->ID;
						$form_title = $post->post_title;
						?>
							<option <?php selected($data,$form_id) ?> value="<?php echo esc_attr($form_id) ?>"><?php echo esc_html($form_title) ?></option>
						<?php
					}
				}else{
					?>
					<option value='0'><?php esc_html_e("Create a form","pdf-for-wpforms") ?></option>
					<?php
				}
                ?>
            </select>
        </div>
        <?php
    }
    function save_metabox($post_id, $post){
        if( isset($_POST['pdfcreator_forminator'])) {
            $id = sanitize_text_field($_POST['pdfcreator_forminator']);
            update_post_meta($post_id,'_pdfcreator_forminator',$id);
        }
    }
	function add_shortcode($shortcode) {
		global $wpdb;
		$inner_shortcode = array();
		if( isset($_GET["post"] ) ) {
			$post_id= sanitize_text_field($_GET["post"]);
			$form_id = get_post_meta( $post_id,'_pdfcreator_forminator',true);	
			if($form_id){
				$forminator_form_meta = get_post_meta( $form_id, "forminator_form_meta", true );
				$datas = $forminator_form_meta["fields"];
				foreach( $datas as $data ){
					$inner_shortcode["{".$data["id"]."}"] =  $data["field_label"];
				}
			}
		}
		$shortcode["Forminator"] = $inner_shortcode; 
		return $shortcode;
	}
	function forminator_custom_form_mail_attachment($attachment, $custom_form, $entry, $pdfs){
		$data = $this->data;
		$email = $this->email;
		$form_id = $custom_form->id;
		$datas = Rednumber_Marketing_CRM_Database::get_datas(self::$form,self::$add_on,$form_id);
		if( is_array($datas) && count($datas) > 0 ){
			foreach( $datas as $data ){ 
				foreach($data as $k => $vl ){
					$$k = $vl;
				}
				if( $template > 0) {
		        	if( $name == ""){
		        		$name= "form-".time();
		        	}else{
						$name = $this->replace_placeholders( $name, $custom_form, $entry );
		        		$name = apply_filters( 'forminator_custom_form_mail_admin_message', $name, $custom_form, $data, $entry, $email );
		        	}
		        	$name = $name."-".time(); 
		        	$name = sanitize_file_name($name);
					$data_send_settings = array(
						"id_template"=> $template,
						"type"=> "html",
						"name"=> $name,
						"datas" =>$entry,
						"return_html" =>true,
					);
					$message =Yeepdf_Create_PDF::pdf_creator_preview($data_send_settings);
					$message = $this->replace_placeholders( $message, $custom_form, $entry );
					$message = apply_filters( 'forminator_custom_form_mail_admin_message', $message, $custom_form, $data, $entry, $email );
					$data_send_settings_download = array(
						"id_template"=> $template,
						"type"=> "upload",
						"name"=> $name,
						"datas" =>$entry,
						"html" =>$message,
						"password" =>$pass,
						"save_dropbox" =>false,
					);
					$data_send_settings_download = apply_filters("pdf_before_render_datas",$data_send_settings_download);
					$folder_uploads =Yeepdf_Create_PDF::pdf_creator_preview($data_send_settings_download);
					$attachment[] = $folder_uploads["path"];
					update_option( "yeepdf_download_forminator", $folder_uploads["url"]);
				}
			}
		}
		return $attachment;
	}
	function forminator_custom_form_mail_data($data){
		$this->data = $data;
		return $data;
	}
	function forminator_custom_form_mail_before_send_mail($email){
		$this->email = $email;
	}
	private function replace_placeholders( $text, $module, $entry, $full_mode = false ) {
		$text = forminator_replace_form_data( $text );
		$text = forminator_replace_variables( $text, $module->id, $entry );
		$text = forminator_replace_custom_form_data( $text, $module, $entry, array() );
		return $text;
	}
}
$yeepdf_settings_forminator = new Yeepdf_Creator_Forminator_Backend;