<?php
// setup page
define("PAGE_NAME", t("contact_page_name", "Contact Us"));
define("PAGE_DESCRIPTION", t("contact_meta_description", "Contact us"));
define("PAGE_KEYWORDS", t("contact_meta_keywords", "contact, us, questions, queries, file, hosting"));
define("TITLE_DESCRIPTION_LEFT", t("contact_title_page_description_left", ""));
define("TITLE_DESCRIPTION_RIGHT", t("contact_title_page_description_right", ""));

$queryTypes = array();
$queryTypes['contact_query_type_site_support'] = 'Site Support';
$queryTypes['contact_query_type_bug_report'] = 'Bug Report';
$queryTypes['contact_query_type_abuse_report'] = 'Abuse Report';
$queryTypes['contact_query_type_suggest_improvements'] = 'Suggest Improvement';
$queryTypes['contact_query_type_other'] = 'Other';

// success handling
if (isset($_REQUEST['s']))
{
    notification::setSuccess(t('contact_success', 'Thanks for submitting the contact form on our site. We\'ll review the query as soon as possible and get back to your within the next 48 hours.'));
}

// prepare variables
$full_name = '';
$email_address = '';
$query = '';
$query_type = '';

// send report if submitted
if ((int) $_REQUEST['submitme'])
{
    $full_name = trim($_REQUEST['full_name']);
    $email_address = trim($_REQUEST['email_address']);
    $query = trim($_REQUEST['query']);
	$query_type = trim($_REQUEST['query_type']);

    if (strlen($full_name) == 0)
    {
        notification::setError(t("contact_error_name", "Please enter your name."));
    }
    elseif (strlen($email_address) == 0)
    {
        notification::setError(t("contact_error_email", "Please enter your email."));
    }
    elseif (validation::validEmail($email_address) == false)
    {
        notification::setError(t("contact_error_email_invalid", "Please enter a valid email address."));
    }
	elseif (strlen($query_type) == 0)
    {
        notification::setError(t("contact_error_query_type", "Please select your type of query."));
    }
    elseif (strlen($query) == 0)
    {
        notification::setError(t("contact_error_signature", "Please enter your query."));
    }

    // check captcha
    if ((!notification::isErrors()) && (SITE_CONFIG_CONTACT_FORM_SHOW_CAPTCHA == 'yes'))
    {
        if (!isset($_REQUEST['g-recaptcha-response']))
        {
            notification::setError(t("invalid_captcha123", "Captcha confirmation text is invalid. 1"));
        }
        else
        {
            $resp = coreFunctions::captchaCheck($_REQUEST["g-recaptcha-response"]);
            if ($resp == false)
            {
                notification::setError(t("invalid_captcha", "Captcha confirmation text is invalid."));
            }
        }
    }

    // send email to admin
    if (notification::isErrors() == false)
    {
        // send email
		$loggedInUsername = $Auth->loggedIn()?$Auth->username:'Guest';
        $subject = t('contact_email_subject_v2', '"[[[QUERY_TYPE]]]" contact from [[[SITE_NAME]]] by "[[[LOGGED_IN_USERNAME]]]" user.', array('SITE_NAME' => SITE_CONFIG_SITE_NAME, 'QUERY_TYPE' => $query_type, 'LOGGED_IN_USERNAME' => $loggedInUsername));

        $replacements = array(
            'FULL_NAME' => $full_name,
            'EMAIL_ADDRESS' => $email_address,
			'QUERY_TYPE' => $query_type,
            'QUERY' => nl2br($query),
            'SITE_NAME' => SITE_CONFIG_SITE_NAME,
            'WEB_ROOT' => WEB_ROOT,
			'LOGGED_IN' => $Auth->loggedIn()?'Yes':'No',
			'LOGGED_IN_USERNAME' => $loggedInUsername,
            'USERS_IP' => coreFunctions::getUsersIPAddress()
        );
        $defaultContent = "There has been a contact form submission from [[[SITE_NAME]]] with the following details:<br/><br/>";
        $defaultContent .= "***************************************<br/>";
        $defaultContent .= "Full Name: [[[FULL_NAME]]]<br/>";
        $defaultContent .= "Email Address: [[[EMAIL_ADDRESS]]]<br/>";
		$defaultContent .= "Query Type: [[[QUERY_TYPE]]]<br/>";
        $defaultContent .= "<br/>[[[QUERY]]]<br/>";
        $defaultContent .= "***************************************<br/>";
        $defaultContent .= "Logged In: [[[LOGGED_IN]]]<br/>";
		$defaultContent .= "Username: [[[LOGGED_IN_USERNAME]]]<br/>";
		$defaultContent .= "Submitted IP: [[[USERS_IP]]]<br/>";
        $defaultContent .= "***************************************<br/><br/>";
        $htmlMsg = t('contact_email_content_v2', $defaultContent, $replacements);

        coreFunctions::sendHtmlEmail(SITE_CONFIG_SITE_CONTACT_FORM_EMAIL, $subject, $htmlMsg, SITE_CONFIG_DEFAULT_EMAIL_ADDRESS_FROM, strip_tags(str_replace("<br/>", "\n", $htmlMsg)), false, $full_name, true, $email_address);
        coreFunctions::redirect(WEB_ROOT . '/contact.' . SITE_CONFIG_PAGE_EXTENSION . '?s=1');
    }
}
else
{
    if ($Auth->loggedIn())
    {
        $full_name = $Auth->user->firstname . ' ' . $Auth->user->lastname;
        $email_address = $Auth->user->email;
    }
}
// include header
require_once(SITE_TEMPLATES_PATH . '/partial/_header.inc.php');
?>

<script>
function selectTypeItem()
{
	$('.supportWrapper').hide();
	if ($('#query_type').val() == 'Abuse Report')
	{
		if (confirm('<?php echo str_replace(array("\n", "\r", "\"", "'"), '', t('contact_abuse_report_popup_notice', 'Please submit all abuse reports via our dedicated abuse report page. Click OK to continue to it now.')); ?>'))
		{
			$('#query_type').val('Site Support');
			window.location = '<?php echo WEB_ROOT; ?>/report_file.html';
		}
		else
		{
			$('#query_type').val('Site Support');
		}
	}
}
</script>

   
        <?php
        if (notification::isErrors())
        {
            echo notification::outputErrors();
        }
        elseif (notification::isSuccess())
        {
            echo notification::outputSuccess();
        }
        ?>
        <div class="row">        
            <div class="col-md-6 form-element">
                <div class="form">
                    <form id="form-join" class="contactForm" method="post" action="<?php echo WEB_ROOT; ?>/contact.<?php echo SITE_CONFIG_PAGE_EXTENSION; ?>" id="form-join">
                        <div class="input-group"><span class="input-group-addon"><i class="fa fa-chevron-right"></i></span>
                            <input name="full_name" class="form-control" placeholder="<?php echo t('contact_full_name', 'Your full name'); ?>" type="text" value="<?php echo validation::safeOutputToScreen($full_name); ?>"/>
                        </div>
                        <div class="input-group"><span class="input-group-addon"><i class="fa fa-chevron-right"></i></span>
                            <input name="email_address" class="form-control" placeholder="<?php echo t('contact_email_address', 'Email address'); ?>" type="text" value="<?php echo validation::safeOutputToScreen($email_address); ?>"/>
                        </div>
        				<div class="input-group"><span class="input-group-addon"><i class="fa fa-chevron-right"></i></span>
                            <select name="query_type" id="query_type" class="form-control" onChange="selectTypeItem(); return false;">
        						<option value="">- <?php echo t('contact_select_subject', 'select subject'); ?> -</option>
        						<?php
        						foreach($queryTypes AS $k=>$queryType)
        						{
        							echo '<option value="'.validation::safeOutputToScreen($queryType).'"';
        							if($queryType == $query_type)
        							{
        								echo ' SELECTED';
        							}
        							echo '>'.validation::safeOutputToScreen(t($k, $queryType)).'</option>';
        						}
        						?>
        					</select>
                        </div>
                        <div class="input-group"> <span class="input-group-addon"><i class="fa fa-chevron-right"></i></span>
                            <textarea rows="5" class="form-control" id="query" placeholder="<?php echo t('contact_your_query', 'Your query'); ?>" name="query"><?php echo validation::safeOutputToScreen($query); ?></textarea>
                        </div>
                        <?php if (SITE_CONFIG_CONTACT_FORM_SHOW_CAPTCHA == 'yes'): ?>
                            <label for="recaptcha_response_field">
                                <span class="field-name"><?php echo t("confirm_text", "Confirmation Text"); ?></span>
                            </label>
                            <?php echo coreFunctions::outputCaptcha(); ?>
                        <?php endif; ?>
                        <div class="input-group form-buttons"><span class="input-group-btn">
                                <input name="submitme" type="hidden" value="1"/>
                                <button class="btn btn-default" tabindex="99" type="submit" name="submit"><i class="fa fa-check"></i> <?php echo t("contact_submit_form", "submit form"); ?></button>
                            </span> 
                        </div>
                    </form>
                </div>
            </div>
            <div class="col-md-6">
                <div class="col-md-12 form-description">
                 <p><?php echo t('contact_intro', 'Please use the following form to contact us with any queries. Abuse reports should be sent via our <a href="[[[ABUSE_URL]]]">abuse pages</a>.', array('ABUSE_URL' => WEB_ROOT . '/report_file.' . SITE_CONFIG_PAGE_EXTENSION)); ?></p>
                </div>
            </div>            
        </div>
                        

<?php
// include footer
require_once(SITE_TEMPLATES_PATH . '/partial/_footer.inc.php');
?>
