<?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>
    <vm:header>
      <vm:title>Application Invitation</vm:title>
    </vm:header>
    <v:on-init>
      <![CDATA[
        select WAI_MEMBER_MODEL, WAI_IS_PUBLIC, WAI_MEMBERS_VISIBLE, WAI_INST, WAI_NAME, WAI_TYPE_NAME, WAI_DESCRIPTION
          into self.imodel, self.is_public, self.is_visible, self.inst, self.iname, self.wa_type, self.idesc
          from WA_INSTANCE
         where WAI_ID = self.iid;
        if (self.instance_descr is null)
          self.instance_descr := WA_GET_APP_NAME (coalesce(get_keyword('wa_name', self.vc_event.ve_params), (select WAI_TYPE_NAME from WA_INSTANCE where WAI_ID = get_keyword('wai_id', self.vc_event.ve_params)), ''));
        if (self.instance_descr = 'Blog')
          self.ihome := self.inst.wa_home_url();
        if (self.wa_type in ('oMail'))
        {
          declare tmp varchar;

          tmp := self.iname;
          self.iname := subseq(tmp, 0, strstr(tmp, '@'));
          self.wa_domain := subseq(tmp, strstr(tmp, '@'));
        }
      ]]>
    </v:on-init>
    <v:after-data-bind>
      <![CDATA[
        declare freeze varchar;
        freeze := (select WAI_IS_FROZEN from DB.DBA.WA_INSTANCE where WAI_ID = self.iid);
        if (freeze = 1 and not wa_user_is_dba (self.u_name, self.u_group))
        {
          http_request_status('HTTP/1.1 302 Found');
          http_header(sprintf('Location: services.vspx?sid=%s&realm=%s\r\n', self.sid, self.realm));
          return;
        }
      ]]>
    </v:after-data-bind>
    <vm:pagewrapper>
      <vm:variable name="inst" type="db.dba.web_app" default="null" persist="0" />
      <vm:variable name="wa_name" type="varchar" default="null" persist="0" param-name="wa_name"/>
      <vm:variable name="wa_type" type="varchar" default="null" persist="0"/>
      <vm:variable name="imodel" type="int" default="null" persist="0"/>
      <vm:variable name="is_public" type="int" default="null" persist="0"/>
      <vm:variable name="is_visible" type="int" default="null" persist="0"/>
      <vm:variable name="iid" type="int" default="null" persist="0" param-name="wai_id"/>
      <vm:variable name="iname" type="varchar" default="null" persist="0"/>
      <vm:variable name="idesc" type="varchar" default="null" persist="0"/>
      <vm:variable name="ihome" type="varchar" default="null" persist="0"/>
      <vm:variable name="instance_descr" type="varchar" default="null" persist="0" />
      <vm:variable name="wa_domain" type="varchar" default="null" persist="0"/>
      <vm:variable name="domains" type="varchar" default="null" persist="0"/>
      <vm:variable name="switch_adv" type="int" default="0" persist="0"/>
      <vm:variable name="page_type" type="varchar" default="'edit'" persist="0"/>
      <vm:variable name="member_filter" type="varchar" default="'%'" persist="0"/>
      <vm:navigation on="settings"/>
      <vm:navigation1 on="services"/>
      <vm:rawheader>
	<h1 class="page_title">Invite someone to join to <v:label render-only="1" value="--self.iname" name="rah1" /></h1>
      </vm:rawheader>
      <vm:body>
        <vm:login redirect="login.vspx"/>
        <?vsp
          if (self.wa_type not in ('oDrive', 'oMail', 'IM'))
          {
        ?>
      <input type="hidden" name="page" value="membership"/>
      <?vsp
      if (self.wa_type in ('oWiki')
        and self.imodel = 1)
       {
        ;
       }
      else
       {
      ?>
      <div>
      <?vsp
      {
        declare cnt, cnt1 int;
	select count(*) into cnt from WA_INVITATIONS where WI_U_ID = self.u_id and WI_INSTANCE = self.iname;
	select count(*) into cnt1 from WA_MEMBER where WAM_INST = self.iname and WAM_STATUS > 2;
	cnt := cnt + cnt1;
	if (cnt > 0)
	  {
      ?>
      <img src="images/icons/go_16.png" border="0" alt="Sent Invitations" /> <v:url name="app_inv_url" url="--sprintf ('app_sent_inv.vspx?wai_name=%U&wai_id=%d', self.iname, self.iid)" value="--sprintf ('You have %d pending Invitation(s)', cnt)" render-only="1"/>
      <?vsp
          }
      }
      ?>
      <img src="images/icons/go_16.png" border="0" alt="Members" /> <v:url name="app_inv1_url" url="--sprintf ('members.vspx?wai_id=%d', self.iid)" value="Members" render-only="1"/>
      </div>
      <v:form type="simple" method="POST" name="inv_form">
      <table class="ctl_grp">
	<tr>
	  <th>Full Name</th>
	</tr>
	<tr>
	  <td>
	    <v:text name="inv_name" value="" xhtml_size="100" error-glyph="*" fmt-function="wa_utf8_to_wide">
	      <v:validator   test="length" min="1" max="256" message="Full name cannot be empty " />
	    </v:text>
	  </td>
	</tr>
	<tr>
	  <th>To:</th>
	</tr>
	<tr>
	  <td><v:text name="inv_mail" value="" xhtml_size="100" error-glyph="*">
	      <v:validator test="length" min="1" max="256" message="E-mail address cannot be empty"/>
	      <v:validator test="regexp" regexp="[^@ ]+@([^\. ]+\.)+[^\. ]+" message="Invalid E-mail address" />
	    </v:text>
	    <br/>
	    <small>Specify recipient's email address.</small>
	    </td>
	</tr>
	<tr>
	  <th>Subject:</th>
	</tr>
	<tr>
	  <td><v:text name="subj1" value="--sprintf ('Invitation to join my %s at %s', WA_GET_APP_NAME (self.wa_type), self.banner)" xhtml_size="100" fmt-function="wa_utf8_to_wide"/></td>
	</tr>
	<tr>
	  <th>Customize Message:</th>
	</tr>
	<tr>
	  <td>
	    <v:textarea name="txt1" value="" xhtml_rows="10" xhtml_cols="100" fmt-function="wa_utf8_to_wide" xhtml_class="fm_ctl_em">
	      <v:after-data-bind><![CDATA[
		if (not e.ve_is_post)
		  {
		    declare _mail_body any;
		    declare inst web_app;
		    inst := (select WAI_INST from WA_INSTANCE where WAI_NAME = self.iname);
		    _mail_body := WA_GET_EMAIL_TEMPLATE('WS_INV_TEMPLATE');
		    _mail_body := WA_MAIL_TEMPLATES(_mail_body, inst, '', '%url%');
		    control.ufl_value := _mail_body;
		  }
		]]></v:after-data-bind>
	    </v:textarea>
	  </td>
	</tr>
	<tr>
	 <td>
	    <span class="fm_ctl_btn">
	    <v:button action="simple" name="bt1" value="Send Invitation">
	      <v:on-post><![CDATA[
		if (not self.vc_is_valid)
		  return;
		declare msg, sid, url any;

		msg := self.txt1.ufl_value;
		msg := replace (msg, '%user%', self.inv_name.ufl_value);
		sid := vspx_sid_generate ();

		insert into VSPX_SESSION (VS_SID, VS_REALM, VS_EXPIRY, VS_UID)
		values (sid, 'wa', now (), self.inv_mail.ufl_value);

		insert soft WA_INVITATIONS (WI_U_ID, WI_TO_MAIL, WI_INSTANCE, WI_SID, WI_STATUS)
		values (self.u_id, self.inv_mail.ufl_value, self.iname, sid, 'pending');

		if (row_count () = 0)
		  {
		    rollback work;
		    self.vc_is_valid := 0;
		    self.vc_error_message := 'There is already an invitation to this e-mail address.';
		    return;
		  }

		url := sprintf ('%s/join.vspx?wai_id=%d&join_id=%s', rtrim (wa_link (1), '/'), self.iid, sid);
		msg := replace (msg, '%url%', url);

		declare exit handler for sqlstate '*'
		{
		  rollback work;
		  self.vc_is_valid := 0;
		  if (__SQL_STATE like 'WA%')
		    self.vc_error_message := __SQL_MESSAGE;
		  else
		    {
		      self.vc_error_message :=
		       'Some of the e-mail addresses are not valid and mail cannot be sent.'||
		       'Please enter correct value and re-try.';
		    }
		  return;
		};
		commit work;
		WA_SEND_MAIL (self.u_e_mail, self.inv_mail.ufl_value, self.subj1.ufl_value, msg);
                self.ok_msg := 'The invitation mail was sent.';
                self.inv_name.ufl_value := '';
                self.inv_mail.ufl_value := '';
		]]></v:on-post>
	      </v:button>
	     </span>
	  </td>
	</tr>
      </table>
    </v:form>
    <?vsp
      }
    ?>
        <?vsp
          }
        ?>
      </vm:body>
    </vm:pagewrapper>
  </vm:page>
</v:page>
