<?xml version="1.0" encoding="UTF-8"?>
<!--
 -
 -  $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
 -
-->
<!-- $Id$ -->
    <v:page 
	  name="main" 
	  style="comp.xsl"
	  on-error-redirect="../main/" 
	  xmlns:v="http://www.openlinksw.com/vspx/" 
	  xmlns:vm="http://www.openlinksw.com/vspx/macro">
	  
      <v:variable name="id" type="integer" default="NULL" param-name="id"/>
      <v:variable name="cluster_key" type="varchar" param-name="cluster"/>

      <v:variable name="topic" type="WV.WIKI.TOPICINFO" default="NULL"/>
      <v:variable name="pubtags" type="any" default="NULL"/>
      <v:variable name="privtags" type="any" default="NULL"/>
      <v:variable name="uid" type="int" default="NULL" persist="1"/>
      <v:variable name="nobody_uid" type="int" default="NULL" persist="1"/>
      <v:variable name="allow_change_str" type="varchar" default="'@@hidden@@'" persist="1"/>
      <v:variable name="allow_change" type="int" default="0"/>
      <v:variable name="source_page" type="varchar"/>
  <v:on-init>
    <![CDATA[
	declare _topic WV.WIKI.TOPICINFO;
	_topic := WV.WIKI.TOPICINFO();
	_topic.ti_id := self.id;
	_topic.ti_find_metadata_by_id();	
	self.source_page := WV.WIKI.READONLYWIKIWORDLINK (_topic.ti_cluster_name,_topic.ti_local_name);
	self.topic := _topic;
    ]]>
  </v:on-init>
  <v:after-data-bind>
    <![CDATA[
	if (self.vspx_user = 'WikiGuest')
	  {
	    self.allow_change := 0;
	    self.allow_change_str := 'disabled';
	  }
	self.t1.vc_data_bind(e);

        declare pwd varchar;
	pwd := (select pwd_magic_calc (U_NAME, U_PASSWORD, 1) from DB.DBA.SYS_USERS where U_NAME = self.vspx_user);
	if (DAV_HIDE_ERROR (DAV_AUTHENTICATE (self.topic.ti_res_id, 'R', '_1_', self.vspx_user, pwd)) is not null)
	  self.allow_change := 1;
	-- self.ds.vc_data_bind(e);
	-- self.ds2.vc_data_bind(e);
        self.t1.vc_data_bind(e);
    ]]>
  </v:after-data-bind>
  <v:before-data-bind>
    <![CDATA[
        self.login.vc_data_bind(e);
	self.uid := (select U_ID from DB.DBA.SYS_USERS where U_NAME = self.vspx_user);
	self.nobody_uid := (select U_ID from DB.DBA.SYS_USERS where U_NAME = 'nobody');
	self.topic.ti_curuser_username := self.vspx_user;
        declare _tags any;
	_tags := self.topic.ti_get_tags ();
	if (_tags is not null)
	  {
	    self.pubtags := xpath_eval ('//tagset[@type="public"]/tag/@name', _tags, 0);
	    self.privtags := xpath_eval ('//tagset[@type="private"]/tag/@name', _tags, 0);
	  }	    
    ]]>
  </v:before-data-bind>
  <v:method name="set_tags" arglist="in uid int, in tags_str varchar">
    <![CDATA[
         declare _res int;
	 _res := WV.WIKI.GETLOCK (self.topic.ti_full_path(), self.vspx_user);
	 -- dbg_obj_print ('lock: ', _res);
	 connection_get ('vspx_user', self.vspx_user);
	 DAV_TAG_SET (self.topic.ti_res_id, 'R', uid, tags_str);
      WV.WIKI.RELEASELOCK (self.topic.ti_full_path(), self.vspx_user);
      ]]>
      </v:method>
  <v:method name="delete_tag" arglist="inout e vspx_event, in tagname varchar">
    <![CDATA[
	 self.vc_redirect(sprintf ('rename.vspx?topic=%ld', self.topic.ti_id));
      ]]>
      </v:method>
      <vm:page title="Tags">
      <vm:body>
      <div class="wiki-error">
        <v:error-summary/>
      </div>
	    <v:label name="topic_name" value="--self.topic.ti_cluster_name || '.' || self.topic.ti_local_name"/>
	      <table class="wikitable" width="70%">
	        <tr bgcolor="#DFFFDF">
		 <th width="50%" align="left">Public tags:</th>
		 <th width="50%" align="left">Private tags:</th>
		</tr>
		<tr>
		 <td width="50%">
	         <?vsp
		   foreach (any tag in self.pubtags) do {
		      http (sprintf('<a href="tag.vspx?id=%ld&tag=%s">%s</a>', self.id, tag, tag));
		      http (sprintf('<a href="remove_tag.vspx?id=%ld&tag=%s"><img src="images/del_16.png" border="0"/></a>', self.id, tag));
		   }
		 ?>
		 </td>
		 <td>
		   <?vsp
		   foreach (any tag in self.privtags) do {
		      http (sprintf('<a href="tag.vspx?privatep=1&id=%ld&tag=%s">%s</a>', self.id, tag, tag));
		      http (sprintf('<a href="remove_tag.vspx?privatep=1&id=%ld&tag=%s"><img src="images/del_16.png" border="0"/></a>', self.id, tag));
		   }
		     ?>
		 </td>
	 	</tr>
		<tr>
		<v:template name="t1" type="simple">
		  <v:form name="back_buttons" type="simple" method="POST">
		  <td colspan="2">
		    Add Tags (comma separated):
		    <v:text name="tags_f" value="" error-glyph="*" xhtml_size="100%"/><br/>
		    <v:label name="public_tagsp_l" value="Public" enabled="--self.allow_change"/>
		    <v:check-box name="public_tagsp" value="1" initial-checked="1" enabled="--self.allow_change"/>
		    <v:button name="add" value="Add" action="simple" xhtml_disabled="--self.allow_change_str">
                  <v:on-post>
                    <![CDATA[
		        declare exit handler for sqlstate '42WV9' {
			  self.vc_error_message := regexp_match('[^\n]*',__SQL_MESSAGE);
			  self.vc_is_valid := 0;
			}
			;
		        declare _target_tags any;
			declare _target_uid int;
			if (not self.allow_change or not self.public_tagsp.ufl_selected)
			  {
			    _target_tags := self.privtags;
			    _target_uid := self.uid;
			  }
			else
			  {
			    _target_tags := self.pubtags;
			    _target_uid := self.nobody_uid;
			  }
			declare _new_tags any;
			declare _val, _matched_val varchar;
			_val := self.tags_f.ufl_value;
			if (_val is null)
			  WV.WIKI.APPSIGNAL (11001, 'Empty tag is not allowed', vector ());
		 	_matched_val := regexp_match ('([A-Za-z][A-Z a-z_0-9]*)([ ]*,[ ]*[A-Za-z][A-Z a-z_0-9]+[ ]*)*', _val);
			-- dbg_obj_print (_val, _matched_val);
			if (_matched_val is null or (length (_matched_val) <> length (_val)))
			  WV.WIKI.APPSIGNAL (11001, '"&Val;" is not valid tags string', vector ('Val', _val) );
			_new_tags := WV.WIKI.VECTOR_DROP_NOISE_WORDS(
			  WV.WIKI.VECTOR_CONCAT_UNIQ (
			    _target_tags, 
			    WV.WIKI.TRIM_EX(split_and_decode (WV.WIKI.TRIM_EX(cast (self.tags_f.ufl_value as varchar)), 0, '\0\0,'))));
			if (_new_tags <> _target_tags)
			  {
			    self.set_tags(_target_uid, WV.WIKI.STRJOIN(',',_new_tags));
			    self.vc_redirect (sprintf ('tags.vspx?id=%ld', self.topic.ti_id));
			  }
                    ]]>
                  </v:on-post>
		    </v:button>
		    <vm:back-button/>
		  </td>
	    	 </v:form>
		</v:template>
		</tr>
	      </table>
	</vm:body>
	  </vm:page>
     </v:page>
