<?xml version="1.0"?>
<!--
 -
 -  $Id$
 -
 -  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
 -  project.
 -
 -  Copyright (C) 1998-2018 OpenLink Software
 -
 -  This project is free software; you can redistribute it and/or modify it
 -  under the terms of the GNU General Public License as published by the
 -  Free Software Foundation; only version 2 of the License, dated June 1991.
 -
 -  This program is distributed in the hope that it will be useful, but
 -  WITHOUT ANY WARRANTY; without even the implied warranty of
 -  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 -  General Public License for more details.
 -
 -  You should have received a copy of the GNU General Public License along
 -  with this program; if not, write to the Free Software Foundation, Inc.,
 -  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 -
-->
<v:page name="blog-home-page"
        xmlns:vm="http://www.openlinksw.com/vspx/ods/"
        xmlns:v="http://www.openlinksw.com/vspx/"
        style="index.xsl"
        doctype="-//W3C//DTD XHTML 1.0 Transitional//EN"
        doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <vm:page>
    <v:on-init>
      <![CDATA[
        select WAI_INST, WAI_DESCRIPTION into self.inst, self.idesc from WA_INSTANCE where WAI_ID = self.iid;
      ]]>
    </v:on-init>
    <vm:header>
      <vm:title>Join Application</vm:title>
    </vm:header>
    <vm:pagewrapper>
      <vm:variable name="inst" type="db.dba.web_app" default="null" persist="0"/>
      <vm:variable name="iid" type="int" default="null" persist="0" param-name="wai_id"/>
      <vm:variable name="jid" type="varchar" default="null" persist="0" param-name="join_id"/>
      <vm:variable name="idesc" type="varchar" default="null" persist="0"/>
      <vm:variable name="chk" type="int" default="1" persist="0"/>
      <vm:navigation on="settings"/>
      <vm:navigation1 on="admin"/>
      <vm:navigation2 on="application"/>
      <vm:rawheader caption="Join Application"/>
      <vm:body>
        <vm:login redirect="login.vspx"/>
        <div class="box">
	  <v:form type="simple" method="POST" name="jform1">
	    <v:before-data-bind>
	      <![CDATA[
	      if (length (self.sid))
	        {
		  declare member_model int;
		  declare exit handler for not found {
		    self.chk := 0;
		    goto endf;
		  };
		  select WAI_MEMBER_MODEL into member_model from WA_INSTANCE where WAI_ID = self.iid;
		  if (self.jid is not null or member_model = 2)
		    {
		      update VSPX_SESSION set VS_EXPIRY = now () where VS_SID = self.jid and VS_REALM = 'wa';
		      self.chk := row_count ();
		    }
		  endf:;
		}
	      ]]>
	    </v:before-data-bind>
	    <v:template type="simple" name="no_inv" condition="self.chk = 0">
	      <h2>Invalid request</h2>
	      <div>
		The join request is not valid or expired.
	      </div>
	    </v:template>
	    <v:template type="simple" name="yes_inv" condition="self.chk = 1">
            <h2>Confirmation</h2>
            <p>Do you wish to join to <vm:label value="--self.idesc" /></p>
            <!--p>Select membership type
            <v:data-list name="membership_type_select"
                         sql="select WMT_NAME, WMT_ID from WA_MEMBER_TYPE, WA_INSTANCE where WAI_TYPE_NAME = WMT_APP and WAI_ID = self.iid"
                         key-column="WMT_ID"
                         value-column="WMT_NAME">
            </v:data-list>
            </p-->
            <v:button action="simple" value="Yes" name="join_request">
              <v:on-post>
                <![CDATA[
                  declare _wai_name, acc_type, app_type any;
                  declare exit handler for sqlstate '*', not found
                  {
                    self.vc_is_valid := 0;
		    declare _use_sys_errors, _sys_error, _error any;
		    if (isstring (__SQL_STATE))
		      _sys_error := WA_RETRIEVE_MESSAGE(concat(__SQL_STATE,' ',__SQL_MESSAGE));
		    else
		      _sys_error := '';
                    _error := 'Due to a transient problem in the system, your join request could not be
                        processed at the moment. The system administrators have been notified. Please
                        try again later';
                    _use_sys_errors := (select top 1 WS_SHOW_SYSTEM_ERRORS from WA_SETTINGS);
                    if (_use_sys_errors)
                      self.vc_error_message := _error || ' ' || _sys_error;
                    else
                      self.vc_error_message := _error;
                    rollback work;
                    return;
		  };

		  select WAI_NAME, WAI_TYPE_NAME into _wai_name, app_type from WA_INSTANCE where WAI_ID = self.iid;

		  acc_type := (select max(WMT_ID) from WA_MEMBER_TYPE where WMT_APP = app_type);

                  insert into WA_MEMBER
                    (WAM_USER, WAM_INST, WAM_MEMBER_TYPE, WAM_STATUS)
                  values
                    (self.u_id, _wai_name, acc_type, 3);
                  declare _msg, _result any;
                  _result := connection_get('join_result');
                  if (_result = 'approved')
                    _msg := 'Your join request approved.';
                  else if (_result = 'ownerwait')
                    _msg := 'Application owner notified about your join request. You will get e-mail message after approval.';
		  delete from VSPX_SESSION where VS_SID = self.jid and VS_REALM = 'wa';
		  delete from WA_INVITATIONS where WI_SID = self.jid;
                  http_request_status('HTTP/1.1 302 Found');
                  http_header(sprintf('Location: services.vspx?msg=%s&sid=%s&realm=%s\r\n', _msg, self.sid, self.realm));
                ]]>
              </v:on-post>
            </v:button>
            <v:button action="simple" value="No" name="not_join">
              <v:on-post>
                <![CDATA[
		  delete from VSPX_SESSION where VS_SID = self.jid and VS_REALM = 'wa';
		  update WA_INVITATIONS set WI_STATUS = 'rejected' where WI_SID = self.jid;
                  http_request_status ('HTTP/1.1 302 Found');
                  http_header (sprintf ('Location: admin.vspx?sid=%s&realm=%s\r\n', self.sid, self.realm));
                ]]>
              </v:on-post>
            </v:button>
	</v:template>
          </v:form>
        </div>
      </vm:body>
    </vm:pagewrapper>
  </vm:page>
</v:page>
