MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// No direct access
defined('_JEXEC') or die;
try {
    //set initial api request values
    $objExecuteRequest->setKey('api_url', $arr_credentials['api_base_url']);
    //object is defined in mod_majestic_forms.php
    //load forms model
    $objForm = new cls_forms($objExecuteRequest);
    //generate the form
    $objForm = loadMajesticForm($objForm, $formid, array(), $arr_config = array('cache_form' => $cache_form, 'cache_form_ttl' => $cache_form_ttl, 'cache_path' => $cache_path));
    //set data to be populated into form where applicable, in this case only the post data is being used
    if ($_POST) {
        $arr_form_data = $_POST;
        //submit form data to the api
        $objForm->submitForm($arr_form_data);
    } else {
        //this array could be used to prepoulate form elements
        $arr_form_data = array();
    }
    //end if
    //generate form html
    $form_html = $objForm->generateOutput($arr_form_data);
    if ($form_css_enabled == 1) {
        //add some basic styling
        $form_html .= '
/**
 * Deals with presenting and submitting forms
 */
function majestic_form()
{
    $api_url = get_option('m3_api_base_url');
    $api_key = get_option('m3_api_key');
    $api_user = get_option('m3_api_username');
    $api_pword = get_option('m3_api_password');
    $formid = get_option('m3_form_id');
    $form_css_enabled = get_option('m3_form_default_css');
    $cache_form = get_option('m3_cache_form');
    $cache_form_ttl = get_option('m3_cache_form_ttl');
    //set cache path
    if (!is_dir(getcwd() . '/wp-content/mjforms_tmp')) {
        if (is_writable(getcwd() . '/wp-content/mjforms_tmp/t.txt')) {
            mkdir(getcwd() . '/wp-content/mjforms_tmp', 0700);
        }
        //end if
    }
    //end if
    $cache_path = getcwd() . '/wp-content/mjforms_tmp/formcache' . $formid . '.json';
    $arr_credentials = array('api_base_url' => $api_url . '/api', 'api_key' => $api_key, 'api_username' => $api_user, 'api_password' => $api_pword);
    //load request object and set variables
    $objExecuteRequest = new cls_execute_request();
    //set variables load from credentials config file
    $objExecuteRequest->setKeyFromArray($arr_credentials);
    //set api url
    $objExecuteRequest->setKey('api_url', $arr_credentials['api_base_url']);
    try {
        //set initial api request values
        $objExecuteRequest->setKey('api_url', $arr_credentials['api_base_url']);
        //object is defined in mod_majestic_forms.php
        //load forms model
        $objForm = new cls_forms($objExecuteRequest);
        //generate the form
        $objForm = loadMajesticForm($objForm, $formid, array(), $arr_config = array('cache_form' => $cache_form, 'cache_form_ttl' => $cache_form_ttl, 'cache_path' => $cache_path));
        //set data to be populated into form where applicable, in this case only the post data is being used
        if ($_POST) {
            $arr_form_data = $_POST;
            //submit form data to the api
            $objForm->submitForm($arr_form_data);
        } else {
            //this array could be used to prepoulate form elements
            $arr_form_data = array();
        }
        //end if
        //generate form html
        $form_html = $objForm->generateOutput($arr_form_data);
        if ($form_css_enabled == 1) {
            //add some basic styling
            $form_html .= '
					<style>
						#' . $objForm->form_css_id . ' {
							padding: 5px;
							display: inline-block;
							margin-bottom: 25px;
							width: 500px;
						}

						#' . $objForm->form_css_id . ' div {
							margin: 5px 0px 5px 0px;
						}

						#' . $objForm->form_css_id . ' label.form-label  {
							display: inline-block;
							width: 150px;
						}
					</style>';
        }
        //end if
        //present the form
        echo $form_html;
    } catch (Exception $e) {
        echo '<fieldset><legend>Error:</legend>';
        echo '<pre>' . print_r($e, TRUE) . '</pre>';
        echo '</fieldset>';
    }
    //end catch
}