<?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="foaf-import-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">
	<v:variable name="edit_svc" type="int" default="null" param-name="edit" />
	<v:variable name="del_svc" type="int" default="null" param-name="del" />
	<v:variable name="names" type="any" default="null" persist="temp" />
	<h3>External Web Services Settings</h3>
  <v:form type="simple" method="POST" xhtml_enctype="multipart/form-data" action="uiedit_ws.vspx?page=4">
	  <table class="listing" rules="groups">
	      <tr class="listing_header_row">
		  <th>Service</th>
		  <th>URI</th>
		    <th width="50px">Action</th>
	      </tr>
      <v:data-set name="svc" sql="select US_ID, US_SVC, US_IRI, US_KEY from WA_USER_SVC where US_U_ID = :self.u_id" scrollable="1" editable="1" nrows="0">
		  <v:before-data-bind>
if (not e.ve_is_post and self.del_svc is not null)
{
  delete from WA_USER_SVC where US_ID = self.del_svc and US_U_ID = self.u_id;
}
		  </v:before-data-bind>

		  <v:template name="svc_rep" type="repeat">
		      <v:template name="svc_brows" type="browse">
			  <v:after-data-bind>
if (not e.ve_is_post and control.te_rowset[0] = self.edit_svc)
{
  self.svc_name.ufl_value := control.te_rowset[1];
  self.svc_url.ufl_value := control.te_rowset[2];
  self.svc_keys.ufl_value := control.te_rowset[3];
}
	                  </v:after-data-bind>
			  <tr class="<?V case when self.edit_svc = control.te_rowset[0] then 'listing_row_selected' when mod(control.te_ctr, 2) then 'listing_row_odd' else 'listing_row_even' end ?>">
			      <td>
				  <v:url name="ur3"
				      value='--concat (&apos;<img src="images/icons/edit_16.png" hspace="3" border="0"/>&apos;,
				      get_keyword ((control.vc_parent as vspx_row_template).te_rowset[1], self.names))'
				      format="%s"
                       url="--sprintf ('uiedit_ws.vspx?page=4&amp;edit=%d', (control.vc_parent as vspx_row_template).te_rowset[0])"
				      xhtml_title="Edit"
				      xhtml_alt="Edit"
				      />
			      </td>
			      <td>
				  <v:label name="la2" value="--coalesce ((control.vc_parent as vspx_row_template).te_rowset[2], '')"/>
			      </td>
              <td nowrap="nowrap">
                <v:url name="ur4" value='<img src="images/icons/trash_16.png" hspace="3" border="0"/>Delete'
				      format="%s"
                       url="--sprintf ('uiedit_ws.vspx?page=4&amp;del=%d', (control.vc_parent as vspx_row_template).te_rowset[0])"
				      xhtml_title="Delete"
				      xhtml_alt="Delete"
                       xhtml_class="img_button"
				      />
			      </td>
			  </tr>
		      </v:template>
		  </v:template>

		  <v:template name="svc_footer" type="simple">
		      <tr>
			  <td colspan="3" class="listing_col_action">
			      <vm:ds-navigation data-set="svc" type="set" />
			  </td>
		      </tr>
		  </v:template>
	      </v:data-set>
	  </table>
	  <div class="fm">
	      <fieldset>
		  <label for="svc_name">Service</label>
        <br />
		  <v:select-list name="svc_name">
          <v:on-init>
            <![CDATA[
declare v any;
declare sel, val any;

              control.vsl_items := vector ('Amazon API', 'Amazon WishList', 'Ebay', 'Flickr', 'Google Associates', 'Google API');
              control.vsl_item_values := vector ('AmazonKey', 'AmazonID', 'EbayID', 'FlickrKey', 'GoogleAdsenseID', 'GoogleKey');

val := control.vsl_items;
sel := control.vsl_item_values;
v := make_array (length (sel)*2, 'any');
for (declare i, l int, i := 0, l := length (sel); i < l; i := i + 1)
  {
    v[i*2] := sel[i];
    v[(i*2) + 1] := val[i];
  }
self.names := v;
            ]]>
          </v:on-init>
          <v:before-render>
            <![CDATA[
declare sel, val any;
declare ex, newv, newn any;

ex := (select VECTOR_AGG (US_SVC) from WA_USER_SVC where US_U_ID = self.u_id);
val := control.vsl_items;
sel := control.vsl_item_values;
newv := vector ();
newn := vector ();
for (declare i, l int, i := 0, l := length (sel); i < l; i := i + 1)
  {
     if (position (sel[i], ex) = 0 or sel[i] = self.svc_name.ufl_value)
       {
         newn := vector_concat (newn, vector (val[i]));
         newv := vector_concat (newv, vector (sel[i]));
       }
  }
control.vsl_items := newn;
control.vsl_item_values := newv;
control.vs_set_selected ();
            ]]>
          </v:before-render>
		  </v:select-list>
		  <br/>
		  <label for="svc_url">URL</label>
        <br />
        <v:text name="svc_url" value="" xhtml_id="svc_url" xhtml_size="50" xhtml_class="_validate_ _url_ _canEmpty_" xhtml_onblur="javascript: validateField(this);" />
		  <br />
		  <label for="svc_keys">Keys</label>
        <br />
        <v:textarea name="svc_keys" value="" xhtml_id="svc_keys" xhtml_rows="5" xhtml_cols="50" default_value="key=\nsecret=\nsession="/>
		  <br />
	      <span class="fm_ctl_btn">
		  <v:button name="cancel2" value="Cancel" action="simple" enabled="--equ(isnull(self.edit_svc),0)">
		      <v:on-post>
self.svc_name.ufl_value := null;
self.svc_url.ufl_value := null;
self.svc_keys.ufl_value := null;
self.edit_svc := null;
self.save2.vc_data_bind (e);
control.vc_enabled := 0;
		      </v:on-post>
		  </v:button>
          <v:button name="save2" value="--case when self.edit_svc is null then 'Add' else 'Update' end" action="simple" xhtml_onclick="return validateInputs(this);">
            <v:on-post>
              <![CDATA[
declare v any;
if (not self.vc_is_valid)
  return;

v := self.svc_keys.ufl_value;
v := replace (v, '\r', '\n');
v := replace (v, '\n\n', '\n');
if ('key=\nsecret=\nsession=' = v)
  self.svc_keys.ufl_value := '';

if (length (self.svc_keys.ufl_value) + length (self.svc_keys.ufl_value) = 0)
  {
    self.vc_is_valid := 0;
    self.vc_error_message := 'URL and/or key must be given.';
    return;
  }

if (self.edit_svc is null)
  {
    insert into WA_USER_SVC (US_U_ID, US_SVC, US_IRI, US_KEY)
      values (self.u_id, self.svc_name.ufl_value, self.svc_url.ufl_value, self.svc_keys.ufl_value);
  }
else
  {
    update WA_USER_SVC set US_SVC = self.svc_name.ufl_value, US_IRI = self.svc_url.ufl_value, US_KEY = self.svc_keys.ufl_value
      where US_U_ID = self.u_id and US_ID = self.edit_svc;
  }

self.edit_svc := null;
self.svc_name.ufl_value := null;
self.svc_url.ufl_value := null;
self.svc_keys.ufl_value := null;
control.vc_data_bind (e);
self.svc.vc_data_bind (e);
              ]]>
            </v:on-post>
		  </v:button>
	      </span>
	  </fieldset>
      </div>
	</v:form>
</v:page>
