<?php
if (!defined('ABSPATH'))
	exit; // Exit if accessed directly
include_once __DIR__ . '/integration/integration.php';

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)
	{
		$email = $this->email;
		$form_id = $custom_form->id;
		$settings = yeepdf_forminator_get_form_settings($form_id);
		$template_id = !empty($settings['template_id'])
			? (int) $settings['template_id']
			: yeepdf_forminator_get_selected_template($form_id);
		if ($template_id > 0) {
			// File name: allow merge tags, fall back to an auto name.
			$name = isset($settings['file_name']) ? trim($settings['file_name']) : '';
			if ($name !== '') {
				$name = $this->replace_placeholders($name, $custom_form, $entry);
			}
			$name = sanitize_file_name($name);
			if ($name === '') {
				$name = sanitize_file_name("form-" . time());
			}

			// Password: allow merge tags too.
			$password = isset($settings['password']) ? $settings['password'] : '';
			if ($password !== '') {
				$password = $this->replace_placeholders($password, $custom_form, $entry);
			}

			$save_dropbox = !empty($settings['save_dropbox']);

			$data_send_settings = array(
				"id_template" => $template_id,
				"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, array(), $entry, $email);
			$data_send_settings_download = array(
				"id_template" => $template_id,
				"type" => "upload",
				"name" => $name,
				"datas" => $entry,
				"html" => $message,
				"password" => $password,
				"save_dropbox" => $save_dropbox,
			);
			$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;