{extends "base.tpl.html"}
{block "title"}Eventum Installation{/block}

{block javascripts}
<script type="text/javascript">

function validateForm(f)
{
    if (Validation.isFieldWhitespace('hostname')) {
        Validation.selectField('hostname');
        alert('{t escape=js}Please enter the hostname for the server of this installation of Eventum.{/t}');
        return false;
    }
    if (Validation.isFieldWhitespace('relative_url')) {
        Validation.selectField('relative_url');
        alert('{t escape=js}Please enter the relative URL of this installation of Eventum.{/t}');
        return false;
    }
    if (Validation.isFieldWhitespace('db_hostname')) {
        Validation.selectField('db_hostname');
        alert('{t escape=js}Please enter the database hostname for this installation of Eventum.{/t}');
        return false;
    }
    if (Validation.isFieldWhitespace('db_name')) {
        Validation.selectField('db_name');
        alert('{t escape=js}Please enter the database name for this installation of Eventum.{/t}');
        return false;
    }
    if (Validation.isFieldWhitespace('db_username')) {
        Validation.selectField('db_username');
        alert('{t escape=js}Please enter the database username for this installation of Eventum.{/t}');
        return false;
    }
    if (Eventum.getField('alternate_user').is(':checked')) {
        if (Validation.isFieldWhitespace('eventum_user')) {
            Validation.selectField('eventum_user');
            alert('{t escape=js}Please enter the alternate username for this installation of Eventum.{/t}');
            return false;
        }
    }

    var field = Eventum.getField('setup[smtp][from]');
    if (Validation.isFieldWhitespace(field)) {
        Validation.selectField(field);
        alert('{t escape=js}Please enter the sender address that will be used for all outgoing notification emails.{/t}');
        return false;
    }
    if (!Validation.isEmail(field.val())) {
        Validation.selectField(field);
        alert('{t escape=js}Please enter a valid email address for the sender address.{/t}');
        return false;
    }
    field = Eventum.getField('setup[smtp][host]');
    if (Validation.isWhitespace(field)) {
        Validation.selectField(field);
        alert('{t escape=js}Please enter the SMTP server hostname.{/t}');
        return false;
    }
    field = Eventum.getField('setup[smtp][port]');
    if ((Validation.isFieldWhitespace(field)) || (!Validation.isNumberOnly(field.val()))) {
        Validation.selectField(field);
        alert('{t escape=js}Please enter the SMTP server port number.{/t}');
        return false;
    }
    var auth_field = Eventum.getField('setup[smtp][auth]');
    if (!Validation.hasOneChecked(auth_field)) {
        alert('{t escape=js}Please indicate whether the SMTP server requires authentication or not.{/t}');
        return false;
    }
    if (auth_field.val() == 1) {
      if (Validation.isFieldWhitespace('setup[smtp][username]')) {
          Validation.selectField('setup[smtp][username]');
          alert('{t escape=js}Please enter the SMTP server username.{/t}');
          return false;
      }
      if (Validation.isFieldWhitespace('setup[smtp][password]')) {
          Validation.selectField('setup[smtp][password]');
          alert('{t escape=js}Please enter the SMTP server password.{/t}');
          return false;
      }
    }
    return true;
}
function toggleAlternateUserFields()
{
    $('#alternate_user_row').toggle(Eventum.getField('alternate_user').is(':checked'));
}

function disableAuthFields(bool)
{
  var bgcolor = bool ? '#CCCCCC' : '#FFFFFF';
  var field = Eventum.getField('setup[smtp][username]')[0];
  field.disabled = bool;
  field.style.backgroundColor = bgcolor;
  field = Eventum.getField('setup[smtp][password]')[0];
  field.disabled = bool;
  field.style.backgroundColor = bgcolor;
}

$(function(){
    $("h3").on("click", function() {
        $(this).next("pre").toggleClass("uncollapsed");
    });
    $("pre").on("click", function() {
        if (!$(this).hasClass('uncollapsed')) {
            $(this).toggleClass("uncollapsed");
        }
    });
});

</script>
{/block}

{block "stylesheets"}
<style>
  #content > table {
    width: 50%;
  }

  h3 {
    text-decoration: underline;
  }

  h3:hover {
    cursor: pointer;
  }

  pre {
    position: relative;
    height: 50px;
    padding-bottom: 40px;
    overflow: hidden;
    white-space: normal;
    white-space: pre-wrap;
    transition: all .5s ease-in-out;
  }

  pre:after {
    position: absolute;
    content: "⇓";
    display: block;
    width: 100%;
    height: 30px;
    margin-top: -30px;
    text-align: center;
    line-height: 42px;
    left: 0;
    bottom: 0;
    font-size: 26px;
    background: -moz-linear-gradient(top, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 1) 60%, rgba(255, 255, 255, 1) 99%);
    background: -webkit-linear-gradient(top, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 1) 60%, rgba(255, 255, 255, 1) 99%);
    background: linear-gradient(to bottom, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 1) 60%, rgba(255, 255, 255, 1) 99%);
    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#00ffffff', endColorstr='#ffffff', GradientType=0);
    cursor: pointer;
  }

  pre.uncollapsed {
    height: 60vh;
    overflow-y: scroll;
  }

  pre.uncollapsed:after {
    display: none;
  }
</style>
{/block}

{block name="content"}
{if $result|default:'' != '' and $result|default:'' != 'success'}
<br />
<table width="400" bgcolor="#003366" border="0" cellspacing="0" cellpadding="1" align="center">
  <tr>
    <td>
      <table bgcolor="#FFFFFF" width="100%" cellspacing="1" cellpadding="2" border="0">
        <tr>
          <td width="100%">
            <span id="error-header">
              <i class="fa fa-warning" aria-hidden="true"></i>
              {t}An Error Was Found{/t}
            </span>
          </td>
        </tr>
        <tr>
          <td>
            <br />
            <b>{t}Details{/t}:
              {if $result == 'db_not_found'}
                {t}The provided database name could not be found. Review your information or specify that the database should be created in the form below.{/t}
              {elseif $result == 'db_user_not_found'}
                {t}The provided MySQL username could not be found. Review your information or specify that the username should be created in the form below.{/t}
                {elseif $result == 'db_create_access'}
                {t}The provided MySQL username doesn't have the appropriate permissions to create tables. Please contact your local system administrator for further assistance.{/t}
              {elseif $result == 'db_drop_access'}
                {t}The provided MySQL username doesn't have the appropriate permissions to drop tables. Please contact your local system administrator for further assistance.{/t}
              {elseif $result == 'db_phinx_failure'}
                {t}There was error initializing database.{/t}
              {else}
                {$result}
              {/if}
            </b>
            <br /><br />
          </td>
        </tr>
      </table>
    </td>
  </tr>
</table>
{/if}

{if $db_result|default:''}
  <br />
  <table width="400" bgcolor="#003366" border="0" cellspacing="0" cellpadding="1" align="center">
    <tr>
      <td>
        <table bgcolor="#FFFFFF" width="100%" cellspacing="1" cellpadding="2" border="0">
          <tr>
            <td colspan="2">
              <div>
                <h3>{t}Database setup log:{/t}</h3>
                <pre>
                <br/>{$db_result|escape:html}
                </pre>
              </div>
            </td>
          </tr>
        </table>
      </td>
    </tr>
  </table>
{/if}

{if $result|default:'' == 'success'}
<br />
<table width="400" bgcolor="#003366" border="0" cellspacing="0" cellpadding="1" align="center">
  <tr>
    <td>
      <table bgcolor="#FFFFFF" width="100%" cellspacing="1" cellpadding="2" border="0">
        <tr>
          <td width="100%"><span style="font-weight: bold; font-size: 160%;">Success!</span></td>
        </tr>
        <tr>
          <td>
            <br />
            <b>{t}Thank You, Eventum is now properly setup and ready to be used. Open the following URL to login on it for the first time:{/t}</b>
            <br />
            <a href="{if $smarty.post.is_ssl|default:'' == 'yes'}https://{else}http://{/if}{$smarty.post.hostname}{$smarty.post.relative_url}">{if $smarty.post.is_ssl|default:'' == 'yes'}https://{else}http://{/if}{$smarty.post.hostname}{$smarty.post.relative_url}</a>
            <br /><br />
            {t}Email Address{/t}: admin@example.com {t}(literally){/t}<br />
            {t}Password{/t}: admin<br />
            <br />
            <b>{t}NOTE: For security reasons it is highly recommended that the default password be changed as soon as possible.{/t}
            <br /><br />
            <hr size="1" noshade color="#000000">
            {t 1="setup"}Remember to protect your '%1' directory (like changing its permissions) to prevent anyone else from changing your existing Eventum configuration.{/t}
            <br /><br />
            {t}In order to check if your permissions are setup correctly visit the{/t} <a href="check_permissions.php">{t}Check Permissions{/t}</a> {t}page{/t}.
            {if not $is_imap_enabled}
            <br /><br />
            <hr size="1" noshade color="#000000">
            {t}WARNING: If you want to use the email integration features to download messages saved on a IMAP/POP3 server, you will need to enable the IMAP extension in your PHP.INI configuration file. See the PHP manual for more details.{/t}
            {/if}
            </b>
            <br /><br />
          </td>
        </tr>
      </table>
    </td>
  </tr>
</table>
{else}
<br />

<form name="install_form" id="install_form" method="post" onSubmit="return validateForm(this)" data-persist="garlic" data-destroy="false">
<input type="hidden" name="cat" value="install">

<table width="600" bgcolor="#000000" border="0" cellspacing="0" cellpadding="1" align="center">
  <tr>
    <td>
      <table bgcolor="#CCCCCC" width="100%" cellspacing="1" cellpadding="2" border="0">
        <tr>
          <td colspan="2" align="center">
            <h1>{t}Eventum Installation{/t}</h1>
            <hr size="1" noshade color="#000000">
          </td>
        </tr>
        <tr>
          <td colspan="2">
            <li>{t 1=$core.app_version}Eventum Version: %1{/t}
            <li>{t 1=$core.php_version}PHP Version: %1{/t}
            <hr size="1" noshade color="#000000">
          </td>
        </tr>
        <tr>
          <td width="180" align="right">
            <b>{t}Server Hostname{/t}: *</b>
          </td>
          <td>
            {assign var="tabindex" value="1"}
            <input type="text" name="hostname" value="{$smarty.post.hostname|default:$smarty.server.HTTP_HOST}" size="30" tabindex="{$tabindex}">
            {include file="error_icon.tpl.html" field="hostname"}
            <br/>
            <label><input type="checkbox" name="is_ssl" value="yes" tabindex="{$tabindex}" {if $is_secure}checked{/if}>{t}SSL Server{/t}</label>
          </td>
        </tr>
        <tr>
          <td width="180" align="right">
            <b>{t}Eventum Relative URL{/t}: *</b>
          </td>
          <td>
            <input type="text" name="relative_url" value="{$smarty.post.rel_url|default:$core.rel_url}" size="30" tabindex="{$tabindex}">
            {include file="error_icon.tpl.html" field="relative_url"}
          </td>
        </tr>
        <tr>
          <td width="180" align="right">
            <nobr>&nbsp;<b>{t}MySQL Server Hostname{/t}: *</b></nobr>
          </td>
          <td>
            <input type="text" name="db_hostname" size="30" tabindex="{$tabindex}" value="{$smarty.post.db_hostname|default:'localhost'}">
            {include file="error_icon.tpl.html" field="db_hostname"}
          </td>
        </tr>
        <tr>
          <td width="180" align="right">
            <b>{t}MySQL Database{/t}: *</b>
          </td>
          <td>
            <input type="text" name="db_name" size="30" tabindex="{$tabindex}" value="{$smarty.post.db_name|default:'eventum'}">
            {include file="error_icon.tpl.html" field="db_name"}
            <br/>
            <label><input type="checkbox" name="create_db" value="yes" tabindex="{$tabindex}" {if $smarty.post.create_db|default:'' == 'yes'}checked{/if}> Create Database</label>
          </td>
        </tr>
        <tr>
          <td colspan="2" align="center">
            <label><input type="checkbox" name="drop_tables" value="yes" tabindex="{$tabindex}" {if $smarty.post.drop_tables|default:'' == 'yes'}checked{/if}> {t}Drop Tables If They Already Exist{/t}</label>
          </td>
        </tr>
        <tr>
          <td width="180" align="right">
            <b>{t}MySQL Username{/t}: *</b>
          </td>
          <td>
            <input type="text" name="db_username" size="20" tabindex="{$tabindex}" value="{$smarty.post.db_username|default:''}">
            {include file="error_icon.tpl.html" field="db_username"}
          </td>
        </tr>
        <tr>
          <td colspan="2" align="center">
            <span><i>(<b>{t}Note{/t}:</b> {t}This user requires permission to create and drop tables in the specified database.{/t}<br />{t}This value is used only for these installation procedures, and is not saved if you provide a separate user below.{/t})</i></span>
          </td>
        </tr>
        <tr>
          <td width="180" align="right">
            <b>{t}MySQL Password{/t}:</b>
          </td>
          <td>
            <input type="password" name="db_password" size="20" tabindex="{$tabindex}" value="{$smarty.post.db_password|default:''}" autocomplete="off">
          </td>
        </tr>
        <tr>
          <td colspan="2" align="center">
            <label><input type="checkbox" name="alternate_user" value="yes" onClick="toggleAlternateUserFields();"  tabindex="{$tabindex}" {if $smarty.post.alternate_user|default:'' == 'yes'}checked{/if}>{t}Use a Separate MySQL User for Normal Eventum Use{/t}</label>
          </td>
        </tr>
        <tr id="alternate_user_row">
          <td colspan="2" align="center">
            <table>
              <tr>
                <td>
                  <table width="300" cellpadding="1" cellspacing="0" bgcolor="white" border="0">
                    <tr>
                      <td>
                        <table width="100%" border="0" cellspacing="0" cellpadding="1" bgcolor="#C0C0C0">
                          <tr>
                            <td colspan="2">
                              <b>{t}Enter the details below{/t}:</b>
                            </td>
                          </tr>
                          <tr>
                            <td align="right">
                              <nobr>&nbsp;<b>{t}Username{/t}: *</b>&nbsp;</nobr>
                            </td>
                            <td>
                              <nobr><input type="text" name="eventum_user" size="20" value="{$smarty.post.eventum_user|default:''}" tabindex="{$tabindex}">&nbsp;</nobr>
                              {include file="error_icon.tpl.html" field="eventum_user"}
                            </td>
                          </tr>
                          <tr>
                            <td align="right">
                              <nobr>&nbsp;<b>{t}Password{/t}:</b>&nbsp;</nobr>
                            </td>
                            <td>
                              <nobr><input type="password" name="eventum_password" size="20" value="{$smarty.post.eventum_password|default:''}" tabindex="{$tabindex}" autocomplete="off">&nbsp;</nobr>
                            </td>
                          </tr>
                          <tr>
                            <td colspan="2" align="center">
                              <label><input type="checkbox" name="create_user" value="yes" tabindex="{$tabindex}" {if $smarty.post.create_user|default:'' == 'yes'}checked{/if}>{t}Create User and Permissions{/t}</label>
                            </td>
                          </tr>
                        </table>
                      </td>
                    </tr>
                  </table>
                </td>
              </tr>
            </table>
          </td>
        </tr>


        <tr>
          <td colspan="2" align="right">
            &nbsp;
          </td>
        </tr>
        <tr>
          <td align="center" colspan="2">
            <h2><b>{t}Defaults{/t}</b></h2>
            <hr size="1" noshade color="#000000">
          </td>
        </tr>

        {if $default_timezone == 'UTC'}
        <tr>
          <td colspan="2">
            <b>{t}Note{/t}:</b><br>

              {t}You may help Eventum installer to figure out timezone if you configure PHP timezone parameters first:{/t}

              <pre>$ php -r 'echo "date.timezone = ", @date_default_timezone_get(), "\n";' | sudo tee -a /etc/php.ini
              </pre>

              {t escape=no}if <tt>php.ini</tt> path is different, get proper filename from <tt>php --ini</tt> command output.{/t}
          </td>
        </tr>
        {/if}

        <tr>
          <td width="100" align="right" valign="top">
            <b>{t}Timezone{/t}:</b>
          </td>
          <td width="80%">
            <select name="default_timezone" class="chosen-select" data-placeholder="{t}Choose a timezone...{/t}">
              {html_options values=$zones output=$zones selected=$smarty.post.default_timezone|default:$default_timezone}
            </select><br/>
            {include file="lookup_field.tpl.html" lookup_field_name="search" lookup_field_target="default_timezone"}
          </td>
        </tr>
        <tr>
          <td width="100" align="right">
            <b>{t}Week starts on{/t}:</b>
          </td>
          <td width="80%">
            <label><input type="radio" name="default_weekday" {if $smarty.post.default_weekday|default:$default_weekday != "1"}checked{/if} value="0"> {t}Sunday{/t}</label>&nbsp;&nbsp;
            <label><input type="radio" name="default_weekday" {if $smarty.post.default_weekday|default:$default_weekday == "1"}checked{/if} value="1"> {t}Monday{/t}</label>
          </td>
        </tr>
        </tr>



        <tr>
          <td colspan="2" align="right">
            &nbsp;
          </td>
        </tr>
        <tr>
          <td align="center" colspan="2">
            <h2><b>{t}SMTP Configuration{/t}</b></h2>
            <hr size="1" noshade color="#000000">
          </td>
        </tr>
        <tr>
          <td colspan="2" align="center">
            <b>{t}Note{/t}:</b> {t}The SMTP (outgoing mail) configuration is needed to make sure emails are properly sent when creating new users/projects.{/t}
            &nbsp;
            <hr size="1" noshade color="#000000">
          </td>
        </tr>
        <tr>
          <td width="100" align="right">
            <b>{t}Sender{/t}: *</b>
          </td>
          <td width="80%">
            <input type="text" name="setup[smtp][from]" size="30" value="{$smarty.request.setup.smtp.from|default:''|escape:"html"}" tabindex="{$tabindex}">
            {include file="error_icon.tpl.html" field="setup[smtp][from]"}
            <br/>
            <span><i>({t}must be a valid email address{/t})</i></span>
          </td>
        </tr>
        <tr>
          <td width="100" align="right">
            <b>{t}Hostname{/t}: *</b>
          </td>
          <td width="80%">
            <input type="text" name="setup[smtp][host]" size="30" value="{$smarty.request.setup.smtp.host|default:'localhost'|escape:"html"}" tabindex="{$tabindex}">
            {include file="error_icon.tpl.html" field="setup[smtp][host]"}
          </td>
        </tr>
        <tr>
          <td width="100" align="right">
            <b>{t}Port{/t}: *</b>
          </td>
          <td width="80%">
            <input type="text" name="setup[smtp][port]" size="5" value="{$smarty.request.setup.smtp.port|default:'25'}" tabindex="{$tabindex}">
            {include file="error_icon.tpl.html" field="setup[smtp][port]"}
          </td>
        </tr>
        <tr>
          <td width="100" align="right">
            <b>{t}Requires Authentication?{/t}&nbsp;</b>
          </td>
          <td width="80%">
            <label><input type="radio" name="setup[smtp][auth]" value="1" {if $smarty.request.setup.smtp.auth|default:'' == 1}checked{/if} onClick="disableAuthFields(false);" tabindex="{$tabindex}">
            {t}Yes{/t}</label>&nbsp;&nbsp;
            <label><input type="radio" name="setup[smtp][auth]" value="0" {if $smarty.request.setup.smtp.auth|default:'' != 1}checked{/if} onClick="disableAuthFields(true);" tabindex="{$tabindex}">
            {t}No{/t}</label>
          </td>
        </tr>
        <tr>
          <td width="100" align="right">
            <b>{t}Username{/t}:&nbsp;</b>
          </td>
          <td width="80%">
            <input type="text" name="setup[smtp][username]" size="20" value="{$smarty.request.setup.smtp.username|default:''|escape:"html"}" tabindex="{$tabindex}">
            {include file="error_icon.tpl.html" field="setup[smtp][username]"}
          </td>
        </tr>
        <tr>
          <td width="100" align="right">
            <b>{t}Password{/t}:&nbsp;</b>
          </td>
          <td width="80%">
            <input type="password" name="setup[smtp][password]" size="20" value="{$smarty.request.setup.smtp.password|default:''|escape:"html"}" tabindex="{$tabindex}" autocomplete="off">
            {include file="error_icon.tpl.html" field="setup[smtp][password]"}
          </td>
        </tr>
        <tr>
          <td colspan="2" bgcolor="#666666" align="right">
            <input style="font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: bold; font-size: 90%;" type="submit" value="{t}Start Installation{/t} &gt;&gt;" tabindex="{$tabindex}">
          </td>
        </tr>
      </table>
    </td>
  </tr>
  <tr>
    <td bgcolor="#FFFFFF">
      <b>* {t}Required Fields{/t}</b>
    </td>
  </tr>
</form>
</table>


<script type="text/javascript">
<!--
window.onload = setFocus;
function setFocus()
{
    document.install_form.hostname.focus();
    toggleAlternateUserFields();

    {if $smarty.request.setup.smtp.auth|default:'' != 1}
    disableAuthFields(true);
    {/if}

}
//-->
</script>

{/if}
{/block}
