<?php
class Rednumber_Marketing_CRM_Backend_Forminator{ 
	private static $form ="forminator"; 
	function __construct(){
		add_filter('crm_marketing_data_table', array($this,'add_datas'));
		add_filter('crm_marketing_map_fields_form_'.self::$form, array($this,'add_map_fields'),10,2);
		add_filter('crm_marketing_list_add_ons',array($this,"add_add_on"));
		add_action('rednumber_crm_marketing_sync_'.self::$form,array($this,"add_sync"));
	}
	function add_sync(){
		?>
		<p><?php esc_html_e("Please save changes before SYNC","crm-marketing"); ?></p>
		<a href="#" class="button button-primary crm_marketing_sync"><?php esc_html_e("SYNC Entries","crm-marketing") ?></a>
		<?php
	}
	function add_add_on($datas){
		$datas[self::$form] = esc_html__("Forminator","crm-marketing");
		return $datas;
	}
	function add_datas($datas){
		global $wpdb;
		$args = array('post_type' => 'forminator_forms','numberposts' => -1);
		$forms = get_posts($args);

		foreach ( $forms as $form ) {
			
			$datas[] = array(
                    'id'          => $form->ID,
                    'title'       => $form->post_title,
                    'type'        => self::$form,
                    'label'       => "Forminator"
                    );
		}
		return $datas;
	}
	function add_map_fields($datas, $form_id){
		$lists = self::get_form_fields($form_id);
		return array_merge($datas,$lists);
	}
	public static function get_form_fields($form_id){
		global $wpdb;
		$shortcode = array();
		$forminator_form_meta = get_post_meta( $form_id, "forminator_form_meta", true );
		$tab = 0;
		$datas = $forminator_form_meta["fields"];
		foreach( $datas as $data ){
			$shortcode["{".$data["id"]."}"] =  $data["field_label"];
			$tab++;
		}
        return $shortcode;
	}
}
new Rednumber_Marketing_CRM_Backend_Forminator;