<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


if (!class_exists('esigForminatorFilters')):

    class esigForminatorFilters {

        protected static $instance = null;

        private function __construct() {
            add_filter("esig_document_title_filter", array($this, "fluent_document_title_filter"), 10, 2);
            add_filter("esig_strip_shortcodes_tagnames", array($this, "tag_list_filter"), 10, 1);
            add_filter("esig_document_clone_render_content",array($this,"replace_esigforminator_shortcode"),10,4);
        }

        public function replace_esigforminator_shortcode($content, $new_doc_id, $docType, $args){

            if (!shortcode_exists( 'esigforminator' ) )  return $content;

            if(!function_exists("esig_do_unique_shortcode")) return $content;

            global $forminatorDocID; 
            $forminatorDocID = $new_doc_id; 
            if(is_null($forminatorDocID) && !is_numeric($forminatorDocID)) return $content;
            $newContent=  esig_do_unique_shortcode($content,["esigforminator"]);
            return $newContent;
        }

        public function tag_list_filter($listArray) {
            $listArray[] = "forminator";
            return $listArray;
        }

        public function fluent_document_title_filter($docTitle, $docId) {
            
            $formIntegration = WP_E_Sig()->document->getFormIntegration($docId);
            if ($formIntegration != "forminator") {
                return $docTitle;
            }

            preg_match_all('/{{+(.*?)}}/', $docTitle, $matchesAll);

            if (empty($matchesAll[1])) {
                return $docTitle;
            }
            if (!is_array($matchesAll[1])) {
                return $docTitle;
            }

            $titleResult = $matchesAll[1];

            foreach ($titleResult as $result) {              
               
                $fieldId = ($result) ? str_replace('esig_forminator_field_id-', "", $result) : false;
                
                if ($fieldId) {                    
                    $forminatorValue = esigForminatorSetting::get_submission_value($docId);
                    if (is_array($forminatorValue)) 
                    {
                        $value = esigget($fieldId, $forminatorValue);
                        // Check if value exists and is an array before accessing 'value' key
                        if (is_array($value) && isset($value['value'])) {
                            $docTitle = str_replace("{{esig_forminator_field_id-" .  $fieldId . "}}", $value['value'], $docTitle);
                        } else {
                            // If field doesn't exist, replace with empty string
                            $docTitle = str_replace("{{esig_forminator_field_id-" .  $fieldId . "}}", '', $docTitle);
                        }
                    }
                    
                }
            }
            
            return $docTitle;
        }

        /**
         * Return an instance of this class.
         * @since     0.1
         * @return    object    A single instance of this class.
         */
        public static function instance() {

            // If the single instance hasn't been set, set it now.
            if (null == self::$instance) {
                self::$instance = new self;
            }

            return self::$instance;
        }

    }

    

    
endif;
