<?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
 -
-->
<!-- 
  <?vsp WV.WIKI.VSPHEADER (path,params,lines, 'Export');?>
-->

    <v:page 
	  name="main" 
	  style="comp.xsl"
	  on-error-redirect="../main/" 
	  xmlns:v="http://www.openlinksw.com/vspx/" 
	  on-deadlock-retry="3"
	  xmlns:vm="http://www.openlinksw.com/vspx/macro">
    <v:login name="wiki_login" realm="wa" mode="url">
      <v:template name='inl' type='if-no-login'/>
    </v:login>
	  
      <v:variable name="id" type="integer" default="NULL" param-name="id"/>
      <v:variable name="type" type="varchar" default="'html'" param-name="type"/>
	  <v:variable name="is_html" type="integer" default="0"/>
      <v:variable name="allow_read" type="varchar" default="'@@hidden@@'"/>

      <v:variable name="topic" type="WV.WIKI.TOPICINFO" default="NULL"/>
      <v:variable name="source_page" type="varchar"/>
      <v:variable name="home" type="varchar"/>
      <v:variable name="default_header" type="varchar"/>
      <v:variable name="default_footer" type="varchar"/>
	
    <v:on-init>
      <![CDATA[
        self.sid := get_keyword('sid', params);
        self.realm := get_keyword('realm', params, 'wiki');
	declare _topic WV.WIKI.TOPICINFO;
	_topic := WV.WIKI.TOPICINFO();
	_topic.ti_id := self.id;
	_topic.ti_find_metadata_by_id();	
    self.is_html := case when self.type = 'html' then 1 else 0 end;
	self.source_page := WV.WIKI.READONLYWIKIWORDLINK (_topic.ti_cluster_name,_topic.ti_local_name);
	self.topic := _topic;

	self.default_header := '<head>
    <title> <nop>%TOPIC% </title>
  </head>
  <body>';
	self.default_footer := '---
<small>Generated by oWiki export utility at %NOW%</small>
</body>';

      ]]>
    </v:on-init>
    <v:before-data-bind>
      <![CDATA[
        self.vspx_user := coalesce ((select vs_uid from  VSPX_SESSION where vs_sid = self.sid and vs_realm = self.realm), 'WikiGuest');
      ]]>
    </v:before-data-bind>
    <v:after-data-bind>
      <![CDATA[
        if (not self.vc_authenticated) {
          http_rewrite();
          http_request_status ('HTTP/1.1 302 Found');
          http_header (http_header_get() || sprintf('Location: %slogin.vspx?URL=%U\r\n', WV..ODS_LINK (self.vc_event.ve_lines), sprintf ('http://%s/wiki/resources/export.vspx?id=%d&type=%s', WV.WIKI.GET_HOST(), self.id, self.type)));
          return;
        }
        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 null)
	  self.allow_read := 'disable';
	self.home := coalesce ( (select U_HOME from DB.DBA.SYS_USERS where U_NAME = self.vspx_user), '/DAV/') || '/export/';
	self.home := WV.WIKI.FIX_PATH (self.home) || '/';
        self.login.vc_data_bind(e);
        self.topic_info.vc_data_bind(e);
      ]]>
    </v:after-data-bind>
     <vm:page title="Export">
      <vm:body>
	  <div class="wiki-error"><v:error-summary/></div>
          <div class="wiki-settings">
	  <v:form type="simple" method="POST">
	    <v:template name="topic_info" type="simple">
	      <table>
		<tr>	
		  <td colspan="2"><h2>Publish Cluster</h2></td>
		</tr>
	        <tr>
		  <th>Export DAV collection:</th>
		  <td><v:text name="collection" value="--self.home" error-glyph="*" xhtml_class="input-text"/></td>
		</tr>
		<tr>
		  <th>Header:</th>
		  <td>
<?vsp
                      if (self.is_html) {
	http_value ('<html>');
?>
                    <br/>
                    <v:textarea name="header" value="--WV.WIKI.CLUSTERPARAM(self.topic.ti_cluster_id, 'export-header-template', self.default_header)" xhtml_size="20" xhtml_class="textarea-box" xhtml_disabled="@@hidden@@"/>
                  </td>
 		</tr>
		<tr>
		  <th>Footer:</th>
                  <td>
                    <v:textarea name="footer" value="--WV.WIKI.CLUSTERPARAM(self.topic.ti_cluster_id, 'export-footer-template', self.default_footer)" xhtml_size="20" xhtml_class="textarea-box" xhtml_disabled="@@hidden@@"/>
                    <br/>
<?vsp
	http_value ('</html>');
      }
?>
</td>
		</tr>
		<tr>
		  <td colspan="2">
		    <v:button name="Export" value="Export" action="simple" xhtml_disabled="--self.allow_read">
                      <v:on-post>
                        <![CDATA[
			declare exit handler for sqlstate '*' {
		          self.vc_is_valid := 0;
		 	  self.vc_error_message := split_and_decode (__SQL_MESSAGE, 0,'\0\0\n')[0];
		 	  self.vc_error_message := __SQL_MESSAGE;
		          return;
                          };
		 	declare _passwd varchar;	
			_passwd := (select pwd_magic_calc(U_NAME, U_PASSWORD, 1) from DB.DBA.SYS_USERS where U_NAME = self.vspx_user);
			WV.WIKI.MKDIR (self.collection.ufl_value, self.vspx_user, 'WikiUser', _passwd);
			WV.WIKI.SETCLUSTERPARAM(self.topic.ti_cluster_id, 'export-header-template', self.header.ufl_value);
			WV.WIKI.SETCLUSTERPARAM(self.topic.ti_cluster_id, 'export-footer-template', self.footer.ufl_value);
			WV.WIKI.EXPORT_CLUSTER (self.topic.ti_cluster_name, 
				'virt://WS.WS.SYS_DAV_RES.RES_FULL_PATH.RES_CONTENT:/DAV/VAD/wiki/Root/export.xsl', 
				self.collection.ufl_value, 
				self.header.ufl_value, 
				self.footer.ufl_value,
				self.vspx_user, _passwd, 'WikiUser', self.type);
		      ]]>
		      </v:on-post>
		    </v:button>	
		    <vm:back-button/>
		  </td>
		</tr>
	      </table>
	    </v:template>
	  </v:form>
	  </div>
       </vm:body>
     </vm:page>
     </v:page>
<!--
<?vsp WV.WIKI.VSPFOOTER (path,params,lines);?>
-->
