<?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="conversation" 
	style="comp.xsl"
	xmlns:v="http://www.openlinksw.com/vspx/" 
	xmlns:vm="http://www.openlinksw.com/vspx/macro" 
	on-deadlock-retry="5" 
	on-error-redirect="error.vspx" 
	doctype="-//W3C//DTD XHTML 1.0 Transitional//EN">
  <v:variable name="vspx_user" type="varchar" default="'WikiGuest'" persist="1"/>
  <v:variable name="source_page1" type="varchar" default="NULL"/>
  <v:variable name="write_enable" type="int" default="0"/>
  <head>
    <link rel="stylesheet" href="common.css" type="text/css"/>
    <link rel="stylesheet" href="Skins/default/default.css" type="text/css"/>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
    <title>Discussion page</title>
  </head>
  <vm:empty-body onload="javascript:setPaths ('/ods/images/', '/ods/');">
    <script type="text/javascript" src="/ods/openid.js"> </script>
      <v:method name="getTitle" arglist="">
        <![CDATA[
          declare title any;

          if (isnull(self.v_pid)) {
            select ClusterName || '.' || LocalName into title from WV.WIKI.TOPIC natural join WV.WIKI.CLUSTERS where TopicId = self.v_fid;
          } else {
            select C_SUBJECT into title from WV.WIKI.COMMENT where C_ID = self.v_pid;
          }
          return concat('Re: ', coalesce(title, '~ no title ~'));
        ]]>
      </v:method>

      <v:method name="getComment" arglist="">
        <![CDATA[
          declare comment any;

          if (isnull(self.v_pid)) {
            select ClusterName || '.' || LocalName into comment from WV.WIKI.TOPIC natural join WV.WIKI.CLUSTERS where TopicId = self.v_fid;
          } else {
            select C_TEXT into comment from WV.WIKI.COMMENT where C_ID = self.v_pid;
          }
          return coalesce(comment, '');
        ]]>
      </v:method>

  	  <v:template type="simple" condition="not self.vc_is_valid">
  	    <div class="error">
  		    <p><v:error-summary/></p>
  	    </div>
  	  </v:template>
      <v:on-init>
        <![CDATA[
          self.sid := get_keyword('sid', self.vc_event.ve_params);
          self.realm := get_keyword('realm', self.vc_event.ve_params);
	  declare _uname int;
	  _uname := coalesce ((select VS_UID from DB.DBA.VSPX_SESSION where VS_SID = self.sid and VS_REALM = self.realm), 'WikiGuest');
	  self.v_aid := (select U_ID from DB.DBA.SYS_USERS where U_NAME = _uname);

	  declare _topicid, _clusterid int;
   	  _topicid := get_keyword ('fid', self.vc_event.ve_params);
	  _clusterid := (select ClusterId from WV.WIKI.TOPIC where TOPICID = _topicid);
 
	  if (_clusterid is not null and 
		(_uname = 'dav' or _uname = WV.WIKI.CLUSTERPARAM(_clusterid, 'creator', 'dav')))
	   self.write_enable := 1;
        ]]>
      </v:on-init>
      <vm:popup_page_wrapper>
        <vm:variables>
          <v:variable persist="0" name="v_mode" type="varchar" default="'show'"/>
          <v:variable persist="0" name="v_fid" param-name="fid" type="integer"/>
          <v:variable persist="0" name="v_did" param-name="did" type="integer"/>
          <v:variable persist="1" name="v_aid" type="integer"/>
          <v:variable persist="0" name="v_pid" type="integer"/>
        </vm:variables>
        <v:form name="F1" type="simple" method="GET">
          <input type="hidden" name="sid" value="&lt;?V get_keyword('sid', self.vc_event.ve_params) ?>"/>
          <input type="hidden" name="realm" value="&lt;?V get_keyword('realm', self.vc_event.ve_params) ?>"/>
          <v:text name="oid_sig" value="" type="hidden" xhtml_id="oid_sig" />
	  <vm:close-popup-link/>
          <div class="new-form-header">
            Discussion
          </div>
          <div class="new-form-body">
            <table cellspacing="0">
              <tr>
                <td>
                  <v:label value="">
                    <v:after-data-bind>
                      <![CDATA[
                        control.ufl_value := (select ClusterName || '.' || LocalName from WV.WIKI.TOPIC natural join WV.WIKI.CLUSTERS where TopicId = self.v_fid);
                      ]]>
                    </v:after-data-bind>
                  </v:label>
                </td>
              </tr>
              <tr>
                <td>
                  <br /><b>Comments</b><hr />
                  <v:tree name="comments_list" multi-branch="1" orientation="vertical" start-path="--self.v_fid" open-at="--'//*'" root="WV..CM_ROOT_NODE"	child-function="WV..CM_CHILD_NODE">
	                  <v:node-template name="node_tmpl">
	                    <div class="<?V case when control.tn_level = 0 then 'cm_node_top' else 'cm_node' end ?>">
                    		<?vsp
                      		{
                      		  declare id, comment, title, u_name, last_update, _home, openid_sig any;

                      		  id := cast (control.tn_value as integer);

                      		  declare exit handler for not found;
				  if (id >= 0)
				    {
					select C_SUBJECT, cast (C_TEXT as varchar), C_AUTHOR, WV.WIKI.DATEFORMAT(C_DATE), C_HOME, C_OPENID_SIG
	                      		    into title, comment, u_name, last_update, _home, openid_sig
	                      		    from WV.WIKI.COMMENT
		              		   where C_ID = id;

          declare parsed_message any;
          declare _print_body,d_name varchar;
          declare idx integer;

          parsed_message := WV.WIKI.COMMENT_GET_MESS_ATTACHMENTS (comment, 0);
          
          _print_body := parsed_message[0];
          comment:=_print_body;
          
          comment:=comment||'<br/>';
          idx := 1;
          while (idx < length (parsed_message))
          {
            d_name := parsed_message[idx];
              comment:=comment||sprintf ('Download attachment : <a target="_BLANK"  href="%swiki/resources/attachment.vsp?id=%d&part=%i&fn=%s">%s</a><br/>',
                                          WA_LINK(1,'/'),
                                          id,
                                          idx,
                                          d_name,
                                          d_name);
            if (d_name like '%.jpg' or d_name like '%.gif' or d_name like '%.png')
            {
              comment:=comment||sprintf ('<img alt="attachment" src="%swiki/resources/attachment.vsp?id=%d&part=%i&fn=%s" />',
                                         WA_LINK(1,'/'),
                                         id,
                                         idx,
                                         d_name);
              comment:=comment||'<br/>';
            }
             
            idx := idx + 1;
          }


                      	?>
			<div class="comment" id="msg_<?V id ?>">
			  <div class="comment-header">
			    <span class="comment-subj"><?V title ?></span> <br />
			  </div>
			  <div class="comment-msg">
                            <?vsp
                              http (comment);
                            ?>
	                  </div>
			  <div class="comment-footer">
		                        <span class="comment-user">
		                          Posted by <a href="<?V coalesce(_home, sioc..get_graph()) ?>"><?V WV.WIKI.UTF2WIDE (case when length(u_name) = 0 then _home else u_name end) ?></a> 
				        <?V case when length(openid_sig) > 0 then '(verified)' else '' end ?>
		                        </span>
		                        <span class="comment-date"> on <?V cast(last_update as varchar) ?></span>
	                  </div>
                          <div class="comment-actions">
			    <v:button action="simple" style="url" value="Delete" xhtml_title="Delete" xhtml_class="button2" enabled="--self.write_enable">
                              <v:on-post>
                                <![CDATA[
                        		      declare id int;
                        		      id := cast ((control.vc_parent as vspx_tree_node).tn_value as integer);

                                  delete from
                                    WV.WIKI.COMMENT
                                  where
                                    C_ID = id;

                                  self.v_mode := 'show';
                                  self.vc_data_bind(e);
                                ]]>
                              </v:on-post>
                            </v:button>
	                    <v:button action="simple" style="url" value="Reply" xhtml_title="Reply" xhtml_class="button2">
                              <v:on-post>
                                <![CDATA[
                                  self.v_pid := cast ((control.vc_parent as vspx_tree_node).tn_value as integer);;
                                  self.v_mode := 'reply';
                                  self.vc_data_bind(e);
                                ]]>
                              </v:on-post>
                            </v:button>
                          </div>
                       </div>
		                   <?vsp
					}
		                     }
		                   ?>
		                   <v:node />
	                    </div>
	                  </v:node-template>
                  </v:tree>
                </td>
              </tr>
              <tr>
                <td>
                  <hr />
                  <v:button action="simple" style="url" value="Post a new comment" xhtml_title="Reply" xhtml_class="button2">
                    <v:on-post>
                      <![CDATA[
                        self.v_pid := null;
                        self.v_mode := 'reply';
                        self.vc_data_bind(e);
                      ]]>
                    </v:on-post>
                  </v:button>
                  <hr />
                </td>
              </tr>
              <v:template type="simple" enabled="--case when (self.v_mode = 'reply') then 1 else 0 end">
                <tr>
                  <td>
                    <table cellspacing="0">

                      <tr>
                        <th valign="top">
                          <v:label name="l_name" value="Name"/>
                        </th>
                        <td>
                          <v:text name="f_name" value="--case when self.vspx_user = 'WikiGuest' then '' else WV.WIKI.USER_DETAILS (self.v_aid, 'name') end" xhtml_class="textbox" xhtml_size="70" xhtml_disabled = "--case when self.vspx_user = 'WikiGuest' then '@@hidden@@' else '1' end"   xhtml_id="name1"/>
                        </td>
                      </tr>
                      <tr>
                        <th valign="top">
                          <v:label name="l_mail" value="E-Mail"/>
                        </th>
                        <td>
                          <v:text 
                            name="f_mail" 
                            value="--case when self.vspx_user = 'WikiGuest' then '' else WV.WIKI.USER_DETAILS (self.v_aid, 'e-mail') end" 
                            xhtml_class="textbox" 
                            xhtml_size="70"  
                            xhtml_disabled = "--case when self.vspx_user = 'WikiGuest' then '@@hidden@@' else '1' end"
			    xhtml_id="email1"
                            />
                        </td>
                      </tr>
                      <tr>
                        <th valign="top">
                          <v:label for="openid_url" value="Homepage (OpenId)" />
                        </th>
                        <td>
                          <span id="img">
                            <img src="" width="16" height="16" hspace="1" style="display: none;" />
                          </span>
                          <v:text
                            name="openid_url"  
                            value="--case when self.vspx_user = 'WikiGuest' then '' else WV.WIKI.USER_DETAILS (self.v_aid, 'homepage') end" 
                            xhtml_disabled="--case when self.vspx_user = 'WikiGuest' then '@@hidden@@' else '1' end"
                            class="textbox" 
                            xhtml_size="50" 
                            xhtml_id="openid_url" />
                          <?vsp if (self.vspx_user = 'WikiGuest') { ?>
                          <input type="button" 
                            value="Verify" 
                            id="verify_button" 
                            onclick="javascript: onClickVerify(event);" 
                            class="textbox" 
                            />
                          <br /><span id="msg" />
                          <?vsp } ?>
                        </td>
                      </tr>
                      <tr>
                        <th valign="top">
                          Comment
                        </th>
                        <td>
			  <v:textarea name="f_text"  xhtml_rows="10" xhtml_cols="70"/>
			</td>
                      </tr>
                      <tr>
                        <td colspan="2" align="center">
                          <hr />
                          <v:button action="simple" value="Post" xhtml_title="Post" xhtml_class="button2">
                            <v:on-post>
                              <![CDATA[
                                declare name, mail, title, comment any;
                                declare rName, rMail, rTitle, rComment any;

                                title := self.getTitle();
                                comment := trim(self.f_text.ufl_value);
                                name := trim(self.f_name.ufl_value);
                                mail := trim(self.f_mail.ufl_value);

                                insert into WV.WIKI.COMMENT (C_TOPIC_ID, C_PARENT_ID, C_SUBJECT, C_TEXT, C_AUTHOR, C_EMAIL, C_DATE, C_HOME, C_OPENID_SIG)
                                  values (self.v_fid, self.v_pid, title, comment, name, mail, now ()
						, trim (self.openid_url.ufl_value), self.oid_sig.ufl_value);
				self.v_pid := identity_value();

                                self.v_mode := 'show';
                                self.vc_data_bind(e);
                              ]]>
                            </v:on-post>
                          </v:button>
                          <v:button action="simple" value="Cancel" xhtml_title="Cancel" xhtml_class="button2">
                            <v:on-post>
                              <![CDATA[
                                self.v_mode := 'show';
                                self.vc_data_bind(e);
                              ]]>
                            </v:on-post>
                          </v:button>
                        </td>
                      </tr>
                    </table>
                  </td>
                </tr>
              </v:template>
            </table>
          </div>
        </v:form>
      </vm:popup_page_wrapper>
  </vm:empty-body>
    </v:page>

