<!--
 -
 -  $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>Tag meanings</vm:title>
    </vm:header>
    <vm:pagewrapper>
      <vm:variable name="edit_tag" type="varchar" default="null" persist="0" param-name="edit"/>
      <vm:variable name="tm_data" type="any" default="null" persist="0" />
      <vm:variable name="trs_id" type="int" default="null" persist="0" param-name="trs_id"/>

      <vm:navigation on="settings"/>
      <vm:subnavigation-new on="site"/>

      <vm:navigation1 on="admin"/>
      <vm:navigation2 on="application"/>
      <vm:rawheader caption="Tag meanings"/>
      <vm:body>
        <vm:login redirect="index.vspx"/>
        <div id="container">
          <div id="main">
	    <div class="box">
	      <h1>Meanings for <v:label name="t_name" value="--self.edit_tag" /></h1>
		<v:button name="ibtn" action="simple" value="Import meanings">
		  <v:on-post><![CDATA[
		    declare x, y any;
		    if (not length (trim(self.edit_tag)))
		      {
			self.vc_is_valid := 0;
			self.vc_error_message :=
			'The tag name cannot be empty.';
			return;
		      }
		    x := get_tag_meanings_from_moat (trim(self.edit_tag));
 		    y := make_array (length (x), 'any');
		    for (declare i, l int, i:=0, l := length (x); i < l; i := i + 1)
		      {
		        y[i] := vector (x[i]);
		      }
                    self.tm_data := y;
		    self.tm.vc_data_bind (e);
		    ]]></v:on-post>
		</v:button><br/>
		<table class="listing">
		  <tr class="listing_header_row">
		    <th>Meaning URL</th>
		    <th>Action</th>
		  </tr>
		<v:data-set
		  data="--self.tm_data"
		  meta="--vector()"
		  name="tm"
		  nrows="1000"
		  scrollable="1"
		  edit="1">
		  <v:before-data-bind>
		    if (self.tm_data is null)
		      self.tm_data := (select vector_agg (vector (mu_url)) from moat.DBA.moat_user_meanings where
		         mu_tag = self.edit_tag and mu_trs_id = self.trs_id);
		  </v:before-data-bind>
		  <v:template type="repeat" name="tm_rep">
		    <v:template type="browse" name="tm_brws">
		      <tr>
			<td>
			  <v:text name="mu1" value="--(control.vc_parent as vspx_row_template).te_rowset[0]"  xhtml_size="150" cvt-function="wa_trim" error-glyph="*">
			    <v:validator test="regexp" regexp=".+" message="Tag name cannot be empty"/>
			  </v:text>
			</td>
			<td>
			  <v:button name="bt3" value="Update" action="simple">
			    <v:on-post><![CDATA[
			      declare r vspx_row_template;
			      declare o, dt, inx any;
			      declare expr varchar;
			      if (not self.vc_is_valid)
			        return;
			      r := control.vc_parent;
			      o := r.te_ctr + self.tm.ds_rows_offs;
			      dt := self.tm_data;
			      dt[o][0] := trim((r.vc_find_control ('mu1') as vspx_field).ufl_value);
			      ]]></v:on-post>
			  </v:button>
			  <v:button name="bt4" value="Delete" action="simple">
			    <v:on-post><![CDATA[
			      declare r vspx_row_template;
			      declare dt, f, t, l, o, lef, rig any;
			      dt := self.tm_data;
			      r := control.vc_parent;
			      l := length (dt);
			      o := r.te_ctr + self.tm.ds_rows_offs;
			      lef := null; rig := null;
			      if (o > 0)
			      lef := subseq (dt, 0, o);
			      if (o < l-1)
			      rig := subseq (dt, o+1, l);
			      self.tm_data := vector_concat (lef, rig);
			      self.tm.vc_data_bind (e);
			      ]]></v:on-post>
			  </v:button>
			</td>
		      </tr>
		    </v:template>
		  <v:template name="tm_add" type="add">
		    <v:form type="update" name="tm_af" method="POST">
		      <tr>
			<td>
			  <v:text name="mu2" value="" xhtml_size="150" cvt-function="wa_trim" error-glyph="*">
			    <v:validator test="regexp" regexp=".+" message="Ruleset name cannot be empty"/>
			  </v:text>
			</td>
			<td>
			  <v:button name="bt5" value="Add" action="simple">
			    <v:on-post><![CDATA[
			      declare vec any;

			      if (not self.vc_is_valid)
			        return;

			      vec := self.tm_data;
			      vec := vector_concat (vec, vector (vector (self.mu2.ufl_value)));
			      self.tm_data := vec;
			      self.mu2.ufl_value := '';
			      self.tm.vc_data_bind (e);
			      ]]></v:on-post>
			  </v:button>
			</td>
		      </tr>
		    </v:form>
		  </v:template>
		</v:template>
		</v:data-set>
	      </table>
	      <br/>
	      <v:button action="simple" name="bt2" value="Cancel">
		<v:on-post><![CDATA[
		  self.vc_redirect (sprintf ('edit_rule.vspx?rid=%d', self.trs_id));
		  ]]></v:on-post>
	      </v:button>
	      <!--v:button name="dbtn" action="simple" value="Delete" enabled="-#-equ(isnull(self.edit_tag), 0)">
		<v:on-post><![CDATA[
		  delete from moat.DBA.moat_user_meanings where mu_tag = self.edit_tag and mu_trs_id = self.trs_id;
		  self.vc_redirect ('tags.vspx');
		  ]]></v:on-post>
	      </v:button-->
	      <v:button action="simple" name="bt1" value="Save">
		<v:on-post><![CDATA[
		  declare t_name varchar;
		  declare cnt int;
		  t_name := trim (self.edit_tag);
		  if ((self.edit_tag is null or t_name <> self.edit_tag) and
		     exists (select 1 from moat.DBA.moat_user_meanings where mu_tag = t_name and mu_trs_id = self.trs_id))
		    {
		      self.vc_is_valid := 0;
		      self.vc_error_message :=
		       'A tag with this name already exists, please specify another name or edit the existing one.';
                      return;
		    }
		  if (not length (t_name))
                    {
		      self.vc_is_valid := 0;
		      self.vc_error_message :=
		       'The tag name cannot be empty.';
                      return;
		    }
		  delete from moat.DBA.moat_user_meanings where mu_tag = self.edit_tag and mu_trs_id = self.trs_id;
		  foreach (any t in self.tm_data) do
		    {
		      insert replacing moat.DBA.moat_user_meanings (mu_tag, mu_trs_id, mu_url)
		        values (t_name, self.trs_id, t[0]);
		    }
		  self.vc_redirect (sprintf ('edit_rule.vspx?rid=%d', self.trs_id));
		  ]]></v:on-post>
	      </v:button>
            </div>
          </div>
        </div>
      </vm:body>
    </vm:pagewrapper>
  </vm:page>
</v:page>
