<?php
function rednao_smart_forms_load_form($title,$form_id,$returnComponent)
{
    require_once(SMART_FORMS_DIR.'additional_fields/smart-forms-additional-fields-list.php');
    //$options=get_transient("rednao_smart_forms_$form_id");
    $options=false;
    if($options==false)
    {
        global $wpdb;
		/** @noinspection PhpUndefinedMethodInspection */
		$result="";
		if($form_id!=-1)
        {
            $result = $wpdb->get_results($wpdb->prepare("select form_id,element_options,form_options,client_form_options from " . SMART_FORMS_TABLE_NAME . " where form_id=%d", $form_id));
            if (count($result) > 0)
            {
                $result = $result[0];
                $options = array('elements' => $result->element_options,
                    'options' => $result->form_options,
                    'form_id' => $result->form_id,
                    'client_form_options' => $result->client_form_options
                );

                set_transient("rednao_smart_forms_$form_id", $options);
            }
        }else
            $options=array(1);

    }


    $additionalFields=array();
    if(isset($_GET['sfpreview']))
        $additionalFields=apply_filters('smart_forms_af_names',$additionalFields);
    else{
        $formOptions=json_decode($options['options'],true);
        if(isset($formOptions["AdditionalFields"]))
        {
            foreach($formOptions["AdditionalFields"] as $field)
            {
                array_push($additionalFields,array("id"=>$field));
            }
        }
    }

    $generatorDependencies=array('isolated-slider','smart-forms-event-manager','smart-forms-generator-interface','smart-forms-form-elements');
    foreach($additionalFields as $field)
    {
        array_push($generatorDependencies,'smart-forms-af-'.$field['id']);
        do_action('smart_forms_af_'.$field['id']);
    }

    $formElementDependencies=array('isolated-slider','smart-forms-form-elements-container');
    $formElementDependencies=apply_filters('smart_forms_add_form_elements_dependencies',$formElementDependencies);

    wp_enqueue_script('jquery');
    wp_enqueue_script('smart-forms-event-manager',SMART_FORMS_DIR_URL.'js/formBuilder/eventmanager.js',array('isolated-slider'),SMART_FORMS_FILE_VERSION);
    wp_enqueue_script('smart-forms-generator-interface',SMART_FORMS_DIR_URL.'js/subscriber_interfaces/ismart-forms-generator.js',array('isolated-slider'),SMART_FORMS_FILE_VERSION);
    wp_enqueue_script('isolated-slider',plugin_dir_url(__FILE__).'js/rednao-isolated-jq.js',SMART_FORMS_FILE_VERSION);
    wp_enqueue_script('smart-forms-generator',plugin_dir_url(__FILE__).'js/form-generator.js',$generatorDependencies,SMART_FORMS_FILE_VERSION);
    wp_enqueue_script('smart-forms-form-elements-container',SMART_FORMS_DIR_URL.'js/formBuilder/container/Container.js',array('isolated-slider'),SMART_FORMS_FILE_VERSION);
    wp_enqueue_script('smart-forms-form-elements',plugin_dir_url(__FILE__).'js/formBuilder/formelements.js',$formElementDependencies,SMART_FORMS_FILE_VERSION);
    wp_enqueue_script('smart-forms-formula',SMART_FORMS_DIR_URL.'js/formBuilder/formula/formula.js',array('isolated-slider','smart-forms-event-manager'),SMART_FORMS_FILE_VERSION);
    wp_enqueue_script('smart-forms-formula-manager',SMART_FORMS_DIR_URL.'js/formBuilder/formula/formulamanager.js',array('isolated-slider','smart-forms-event-manager','smart-forms-formula'),SMART_FORMS_FILE_VERSION);
    wp_enqueue_script('smart-forms-elements-manipulators',SMART_FORMS_DIR_URL.'js/formBuilder/properties/manipulators.js',array('isolated-slider'),SMART_FORMS_FILE_VERSION);
	wp_enqueue_script('smart-forms-conditional-handlers',SMART_FORMS_DIR_URL.'js/bundle/conditionalHandlers_bundle.js',array('isolated-slider'),SMART_FORMS_FILE_VERSION);

	if($form_id==-1)
        wp_enqueue_script('smart-forms-preview-helper',plugin_dir_url(__FILE__).'js/formBuilder/previewHelper.js',array('smart-forms-generator'),SMART_FORMS_FILE_VERSION);

    do_action('smart_forms_loading_form',$options);
	do_action('smart_forms_pr_add_form_elements_extensions');
	require_once(SMART_FORMS_DIR.'translations/form-elements-translation.php');
	require_once(SMART_FORMS_DIR.'smart-forms-bootstrap.php');

    wp_enqueue_style('smart-forms-Slider',SMART_FORMS_DIR_URL.'css/smartFormsSlider/jquery-ui-1.10.2.custom.min.css',array(),SMART_FORMS_FILE_VERSION);
    wp_enqueue_style('smart-forms-custom-elements',plugin_dir_url(__FILE__).'css/formBuilder/custom.css',array(),SMART_FORMS_FILE_VERSION);


    $random=rand();
    if($form_id==-1)
        $random="sfpreviewcontainer";

	if(!defined ("SMART_DONATIONS_PLUGIN_URL"))
		define("SMART_DONATIONS_PLUGIN_URL","");

	if(!defined ("SMART_DONATIONS_SANDBOX"))
		define("SMART_DONATIONS_SANDBOX","");


    $user=wp_get_current_user();
    $userName=$user->user_login;
    $firstName='';
    $lastName='';
    $email='';
    if($userName==false)
        $userName='';
    else{
        $firstName=$user->user_firstname;
        $lastName=$user->user_lastname;
        $email=$user->user_email;
    }



    if($returnComponent==false)
    {
        echo "<script type=\"text/javascript\">var ajaxurl = '".admin_url('admin-ajax.php')."';</script>";
		echo "<div class='widget'>";
        if($options==null)
            return null;

        if($title)
            echo "<div class='widget-wrapper'><h3 class='widgettitle widget-title'>$title</h3>"

        ?>


        <div id="formContainer<?php echo $random?>" class='sfForm rednaoFormContainer bootstrap-wrapper SfFormElementContainer'></div>
        <script type="text/javascript">
			var smartFormsCurrentTime=new Date(".date('D M d Y H:i:s O').");
            var smartFormsUserName="<?php echo $userName?>";
            var smartFormsFirstName="<?php echo $firstName?>";
            var smartFormsLastName="<?php echo $lastName?>";
            var smartFormsEmail="<?php echo $email?>";
            var smartFormsPath="<?php echo plugin_dir_url(__FILE__)?>";
            var smartDonationsRootPath="<?php echo SMART_DONATIONS_PLUGIN_URL ?>";
            var smartDonationsSandbox="<?php echo SMART_DONATIONS_SANDBOX ?>";
			var smartFormsDesignMode=false;
			var smartFormsAdditionalFields<?php echo ($options['form_id']==null?0:$options['form_id'])?>=<?php echo json_encode($additionalFields)?>;
            if(!window.smartFormsItemsToLoad)
                window.smartFormsItemsToLoad=[];

            window.smartFormsItemsToLoad.push({'form_id':<?php echo $options['form_id']?>, 'elements':<?php echo $options['elements']?>,'client_form_options':<?php echo $options['client_form_options']?>,'container':'formContainer<?php echo $random?>'});

        </script>
        <?php
        if($title)
            echo "</div>";

		echo "</div>";

    }else{
        if($options==null)
            return "";
        $loadedForm="";
        if($form_id!=-1)
            $loadedForm="window.smartFormsItemsToLoad.push({ 'form_id':".$options['form_id'].",  'elements':".$options['elements'].",'client_form_options':".$options['client_form_options'].",'container':'formContainer$random'});";

        return "<script type='text/javascript'>var ajaxurl = '".admin_url('admin-ajax.php')."';</script><div id='formContainer$random' class='sfForm rednaoFormContainer SfFormElementContainer bootstrap-wrapper'></div>
            <script>
            	var smartFormsCurrentTime=new Date('".date('D M d Y H:i:s O')."');
            	var smartFormsUserName=\"".$userName."\";
            	var smartFormsFirstName=\"".$firstName."\";
            	var smartFormsLastName=\"".$lastName."\";
            	var smartFormsEmail=\"".$email."\";
                var smartFormsPath=\"".plugin_dir_url(__FILE__)."\";
                var smartDonationsRootPath=\"".SMART_DONATIONS_PLUGIN_URL."\";
                var smartDonationsSandbox=\"".SMART_DONATIONS_SANDBOX."\";
                var smartFormsAdditionalFields".($options['form_id']==null?0:$options['form_id'])."=".json_encode($additionalFields).";
                var smartFormsDesignMode=false;
                if(!window.smartFormsItemsToLoad)
                    window.smartFormsItemsToLoad=new Array();
                $loadedForm
            </script>
           ";
    }

	return null;
}

function rednao_smart_forms_load_preview(){
    $previewPage=get_page_by_title("Smart Forms Page Preview","OBJECT","smartforms_preview");
    if($previewPage==null)
    {
        $post = array(
            'post_content'   => '[sform]-1[/sform]',
            'post_name'      => 'Smart Forms Page Preview',
            'post_title'     => 'Smart Forms Page Preview',
            'post_status'    => 'draft',
            'post_type'      => 'smartforms_preview',
            'ping_status'    => 'closed',
            'comment_status' => 'closed'
        );
        $page_id = wp_insert_post( $post );
        wp_redirect( trailingslashit( site_url() ) . '?page_id='.$page_id.'&sfpreview=true');
    }else
        wp_redirect( trailingslashit( site_url() ) . '?page_id='.$previewPage->ID.'&sfpreview=true');

}