<?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
 -
-->
<v:page name="uiedit-knows"
        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">

  <v:variable name="k_mode" type="varchar" default="'K/browse'"/>

	<v:variable name="k_id" type="any" default="null" />

	<v:form name="usr_personal_11" type="simple" method="POST" xhtml_enctype="multipart/form-data">
    <h3>Social Network</h3>
    <vm:if test="self.k_mode = 'K/browse'">
      <?vsp
        if (0)
        {
      ?>
        <v:button name="k_dButton" action="simple" value="Delete">
          <v:on-post>
            <![CDATA[
              delete from DB.DBA.WA_USER_KNOWS where WUK_ID = get_keyword ('k_id', self.vc_page.vc_event.ve_params) and WUK_U_ID = self.u_id;
              self.vc_data_bind(e);
            ]]>
          </v:on-post>
        </v:button>
      <?vsp
        }
      ?>
      <input type="hidden" name="k_id" id="k_id" value="" />
      <div style="padding: 0 0 0.5em 0;">
        <v:button name="k_aButton" action="simple" style="url" value="''" xhtml_class="img_button">
          <v:before-render>
            <![CDATA[
              control.ufl_value := '<img src="images/icons/add_16.png" border="0" /> Add';
            ]]>
          </v:before-render>
          <v:on-post>
            <![CDATA[
              self.k_mode := 'K/add';
              self.k_flag.ufl_value := null;
              self.k_uri.ufl_value := null;
              self.k_label.ufl_value := null;

              self.vc_data_bind(e);
            ]]>
          </v:on-post>
        </v:button>
        <v:button name="k_iButton" action="simple" style="url" value="''" xhtml_class="img_button">
          <v:before-render>
            <![CDATA[
              control.ufl_value := '<img src="images/icons/go_16.png" border="0" /> Import';
            ]]>
          </v:before-render>
          <v:on-post>
            <![CDATA[
              self.k_mode := 'K/import';

              self.vc_data_bind(e);
            ]]>
          </v:on-post>
        </v:button>
      </div>

	    <v:data-set name="k_ds"
	                sql="select WUK_ID, WUK_URI, WUK_LABEL from DB.DBA.WA_USER_KNOWS where WUK_U_ID = :self.u_id"
		              scrollable="1"
		              editable="1"
		              nrows="0">

        <v:template name="k_ds_header" type="simple" name-to-remove="table" set-to-remove="bottom">
      	  <table class="listing" rules="groups">
      	    <tr class="listing_header_row">
      		    <th width="50%">Label</th>
      		    <th width="50%">URI</th>
      		    <th width="50px">Action</th>
      	    </tr>
          </table>
        </v:template>

        <v:template name="k_ds_repeat" type="repeat" name-to-remove="" set-to-remove="">

          <v:template name="k_ds_empty" type="if-not-exists" name-to-remove="table" set-to-remove="both">
            <table>
              <tr align="center">
                <td colspan="3">No Knows</td>
              </tr>
            </table>
          </v:template>

          <v:template name="k_ds_browse" type="browse" name-to-remove="table" set-to-remove="both">
            <table>
  			      <tr class="<?V case when mod(control.te_ctr, 2) then 'listing_row_odd' else 'listing_row_even' end ?>">
                <td nowrap="nowrap" width="50%">
                  <v:label value="--(control.vc_parent as vspx_row_template).te_column_value('WUK_LABEL')" format="%s" name="k_ds_browse_2_label"/>
                </td>
                <td nowrap="nowrap" width="50%">
                  <v:label value="--(control.vc_parent as vspx_row_template).te_column_value('WUK_URI')" format="%s" name="k_ds_browse_1_label"/>
                </td>
                <td nowrap="nowrap">
                  <v:button name="k_eButton" action="simple" style="url" value="''" xhtml_class="img_button">
                    <v:before-render>
                      <![CDATA[
                        control.ufl_value := '<img src="images/icons/edit_16.png" border="0" alt="Edit Like" title="Edit Like"/> Edit';
                      ]]>
                    </v:before-render>
                    <v:on-post>
                      <![CDATA[
                        self.k_mode := 'K/edit';
                        self.k_id := (control.vc_parent as vspx_row_template).te_column_value('WUK_ID');
                        select WUK_FLAG,
                               WUK_URI,
                               WUK_LABEL
                          into self.k_flag.ufl_value,
                               self.k_uri.ufl_value,
                               self.k_label.ufl_value
                          from DB.DBA.WA_USER_KNOWS
                         where WUK_ID = self.k_id;

                        self.vc_data_bind(e);
                      ]]>
                    </v:on-post>
                  </v:button>
                  <span class="button pointer" onclick="javascript: if (confirm('Are you sure you want to delete this record?')) {$('k_id').value = <?V (control as vspx_row_template).te_column_value('WUK_ID') ?>; doPost('page_form', 'k_dButton');}">
                    <img class="button" src="/ods/images/icons/trash_16.png"/> Delete
                  </span>
  			        </td>
  			      </tr>
            </table>
		      </v:template>
		    </v:template>

		    <v:template name="k_ds_footer" type="simple" name-to-remove="table" set-to-remove="top">
          <table>
          </table>
		    </v:template>
	    </v:data-set>

    </vm:if>

    <vm:if test="self.k_mode = 'K/import'">
      <script type="text/javascript">
        <![CDATA[
          function knowsData() {
            if (!validateField($('k_import')))
              return;

          	var S = '/ods/api/user.getKnowsData?sourceURI=' + encodeURIComponent($v('k_import'));
          	var x = function(data) {
          		var o = null;
          		try {
          			o = OAT.JSON.parse(data);
          		} catch (e) {
          			o = null;
          		}
          		if (o) {
	              for ( var i = 0; i < o.length; i++) {
                  TBL.createRow('k', null, {fld_1: {mode: 4, value: '1'}, fld_2: {value: o[i].uri, readOnly: true}, fld_3: {value: o[i].label, readOnly: true}});
  	            }
          		} else {
          			alert('No data founded');
          		}
          	}
          	OAT.AJAX.GET(S, '', x, {onstart : function() {OAT.Dom.show('k_import_image')}, onend : function() {OAT.Dom.hide('k_import_image')}});
          }
        ]]>
      </script>
  	  <div class="fm">
				<table>
				  <tr>
				    <th width="100px">
		          Source
		        </th>
            <td>
              <input type="text" name="k_import" id="k_import" value="" size="100" class="_validate_ _uri_" />
              <input type="button" value="Retrieve" onclick="javascript: knowsData(); return false;" class="button" />
              <img id="k_import_image" alt="Import knows URIs" src="/ods/images/oat/Ajax_throbber.gif" style="display: none" />
            </td>
          </tr>
          <tr>
            <th></th>
            <td>
              <table id="k_tbl" class="listing">
                <thead>
                  <tr class="listing_header_row">
                    <th>
                      Access
                    </th>
                    <th width="50%">
                      URI
                    </th>
                    <th width="50%">
                      Label
                    </th>
                    <th width="65px">
                      Action
                    </th>
                  </tr>
                </thead>
                <tr id="k_tr_no"><td colspan="3"><b>No retrieved items</b></td></tr>
              </table>
            </td>
          </tr>
          <tr>
            <th></th>
            <td>
              <br />
    		      <v:button name="user_ci_personal_11" value="Cancel" action="simple">
    		        <v:on-post>
                  self.k_mode := 'K/browse';
                  self.vc_data_bind (e);
    		        </v:on-post>
    		      </v:button>
    		      <v:button name="user_si_personal_11" value="Import" action="simple">
    		        <v:on-post>
    		          <![CDATA[
                    declare N integer;
                    declare uri, flag, label, suffix, params any;

                    params := self.vc_page.vc_event.ve_params;
                    for (N := 0; N < length (params); N := N + 2)
                    {
                      if (params [N] like 'k_fld_1_%')
                      {
                        suffix := replace (params [N], 'k_fld_1_', '');
                        flag :=  trim (params [N+1]);
                        uri :=  trim (get_keyword ('k_fld_2_'||suffix, params, ''));
                        label :=  trim (get_keyword ('k_fld_3_'||suffix, params, ''));
                        if ((uri <> '') and not exists (select 1 from DB.DBA.WA_USER_KNOWS where WUK_U_ID = self.u_id and WUK_URI = uri))
                        {
                          insert into DB.DBA.WA_USER_KNOWS (WUK_U_ID, WUK_FLAG, WUK_URI, WUK_LABEL)
                            values (self.u_id, flag, uri, label);
                        }
                      }
                    }

                    self.k_mode := 'K/browse';
                    self.vc_data_bind (e);
                  ]]>
                </v:on-post>
              </v:button>
            </td>
          </tr>
        </table>
  	  </div>
    </vm:if>

    <vm:if test="self.k_mode in ('K/add', 'K/edit')">
  	  <div class="fm">
				<table>
				  <tr>
				    <th width="100px">
		          Access
		        </th>
		        <td>
              <v:select-list name="k_flag" xhtml_id="k_flag">
                <v:item name="public" value="1" />
                <v:item name="acl" value="2" />
                <v:item name="private" value="3" />
              </v:select-list>
            </td>
          </tr>
				  <tr>
				    <th>
		          Label (*)
		        </th>
		        <td>
              <v:text name="k_label" value="" xhtml_id="k_label" xhtml_size="50" xhtml_class="_validate_" />
            </td>
          </tr>
				  <tr>
				    <th>
		          URI (*)
		        </th>
		        <td>
              <v:text name="k_uri" value="" xhtml_id="k_uri" xhtml_size="50" xhtml_class="_validate_ _uri_" />
            </td>
          </tr>
				  <tr>
				    <th>
		        </th>
		        <td>
		          <label>
              	<v:check-box name="k_ping" value="1"/>
              	Issue Semantic Pingback
              </label>
            </td>
          </tr>
          <tr>
            <td></td>
            <td>
              <br />
    		      <v:button name="user_c_personal_11" value="Cancel" action="simple">
    		        <v:on-post>
                  self.k_mode := 'K/browse';
                  self.vc_data_bind (e);
    		        </v:on-post>
    		      </v:button>
    		      <v:button name="user_s_personal_11" value="--case when self.k_mode = 'K/add' then 'Add' else 'Update' end" action="simple" xhtml_onclick="return validateInputs(this);">
    		        <v:on-post><![CDATA[
                  if (self.vc_is_valid = 0)
                    return;

                  if (self.k_mode = 'K/add')
                  {
                    if (exists (select 1 from DB.DBA.WA_USER_KNOWS where WUK_U_ID = self.u_id and WUK_URI = self.k_uri.ufl_value))
                    {
                      self.vc_error_message := 'A "Knows" with same URI already exists, please specify unique URI.';
                      self.vc_is_valid := 0;
                      return;
                    }
                    insert into DB.DBA.WA_USER_KNOWS (WUK_U_ID, WUK_FLAG, WUK_URI, WUK_LABEL)
                      values (self.u_id, self.k_flag.ufl_value, self.k_uri.ufl_value, self.k_label.ufl_value);
                  }
                  else
                  {
                    update DB.DBA.WA_USER_KNOWS
                       set WUK_FLAG = self.k_flag.ufl_value,
                           WUK_URI = self.k_uri.ufl_value,
                           WUK_LABEL = self.k_label.ufl_value
                     where WUK_ID = self.k_id;
                  }
          	      if (self.k_ping.ufl_selected)
                    SEMPING.DBA.CLI_PING (sioc..person_iri (sioc..user_iri (self.u_id)), self.k_uri.ufl_value);

                  self.k_mode := 'K/browse';
                  self.vc_data_bind (e);
                ]]></v:on-post>
              </v:button>
            </td>
          </tr>
        </table>
      </div>
    </vm:if>
	</v:form>
</v:page>
