<?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="m_mode" type="varchar" default="'M/browse'"/>

	<v:variable name="m_id" type="integer" default="null" />
	<v:variable name="m_name" type="varchar" default="null" />
	<v:form name="usr_personal_7" type="simple" method="POST" xhtml_enctype="multipart/form-data">
    <h3>Creator Of</h3>
    <vm:if test="self.m_mode = 'M/browse'">
      <?vsp
        if (0)
        {
      ?>
        <v:button name="m_dButton" action="simple" value="Delete">
          <v:on-post>
            <![CDATA[
              delete from DB.DBA.WA_USER_PROJECTS where WUP_ID = get_keyword ('m_id', self.vc_page.vc_event.ve_params) and WUP_U_ID = self.u_id;
              self.vc_data_bind(e);
            ]]>
          </v:on-post>
        </v:button>
      <?vsp
        }
      ?>
      <input type="hidden" name="m_id" id="m_id" value="" />
      <div style="padding: 0 0 0.5em 0;">
        <v:button name="m_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.m_mode := 'M/add';
              self.m_name := 'foaf:made';
              self.m_url.ufl_value := null;
              self.m_descr.ufl_value := null;

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

	    <v:data-set name="m_ds"
	                sql="select WUP_ID, WUP_NAME, WUP_URL, WUP_DESC from DB.DBA.WA_USER_PROJECTS where WUP_U_ID = :self.u_id"
		              scrollable="1"
		              editable="1"
                  nrows="0">

        <v:template name="m_ds_header" type="simple" name-to-remove="table" set-to-remove="bottom">
	  <table class="listing" rules="groups">
	      <tr class="listing_header_row">
      		    <th>Property</th>
		  <th>Description</th>
      		    <th width="1%" nowrap="nowrap">Action</th>
	      </tr>
          </table>
        </v:template>

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

          <v:template name="m_ds_empty" type="if-not-exists" name-to-remove="table" set-to-remove="both">
            <table>
              <tr>
                <td colspan="3">No 'Creator Of' Items</td>
              </tr>
            </table>
          </v:template>

          <v:template name="m_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('WUP_NAME')" format="%s"/>
			      </td>
                <td nowrap="nowrap" width="50%">
                  <v:label value="--(control.vc_parent as vspx_row_template).te_column_value('WUP_DESC')" format="%s"/>
                </td>
                <td nowrap="nowrap">
                  <v:button name="m_eButton" action="simple" style="url" value="''" xhtml_onclick="javascript: return confirm(\'Are you sure you want to delete this record?\');" xhtml_class="img_button">
                    <v:before-render>
                      <![CDATA[
                        control.ufl_value := '<img src="images/icons/edit_16.png" border="0" alt="Edit Offer" title="Edit Offer"/> Edit';
                      ]]>
                    </v:before-render>
                    <v:on-post>
                      <![CDATA[
                        self.m_mode := 'M/edit';
                        self.m_id := (control.vc_parent as vspx_row_template).te_column_value('WUP_ID');
                        select WUP_NAME,
                               WUP_URL,
                               WUP_DESC
                          into self.m_name,
                               self.m_url.ufl_value,
                               self.m_descr.ufl_value
                          from DB.DBA.WA_USER_PROJECTS
                         where WUP_ID = self.m_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?')) {$('m_id').value = <?V (control as vspx_row_template).te_column_value('WUP_ID') ?>; doPost('page_form', 'm_dButton');}">
                    <img class="button" src="/ods/images/icons/trash_16.png"/> Delete
                  </span>
			      </td>
			  </tr>
            </table>
		      </v:template>
		  </v:template>

		    <v:template name="m_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.m_mode <> 'M/browse'">
	  <div class="fm">
				<table>
				  <tr>
				    <th width="120px">
		          Property (*)
		        </th>
		        <td id="if_opt">
            <script type="text/javascript">
              <![CDATA[
                function p_init ()
                {
                  var fld = new OAT.Combolist([], "<?V self.m_name ?>");
                  fld.input.name = 'm_name';
                  fld.input.id = 'm_name';
                  fld.input.style.width = "300px";
                  $("if_opt").appendChild(fld.div);
                  fld.addOption("foaf:made");
                  fld.addOption("dc:creator");
                  fld.addOption("sioc:owner");
                }
                ODSInitArray.push ( function () { OAT.Loader.load(["combolist"], p_init); } );
              ]]>
            </script>
            </td>
          </tr>
				  <tr>
				    <th valign="top">
		          URI
		        </th>
		        <td>
          <v:text name="m_url" value="" xhtml_id="m_url" xhtml_style="width:316px" xhtml_class="_validate_ _url_ _canEmpty_" xhtml_onblur="javascript: validateField(this);" />
            </td>
          </tr>
				  <tr>
				    <th valign="top">
		          Description (*)
		        </th>
		        <td>
              <v:textarea name="m_descr" value="" xhtml_id="m_descr" xhtml_rows="4" xhtml_cols="80"/>
		  <br />
		  <label>Note: The fields designated with '*' will be fetched from the source document if empty</label>
            </td>
          </tr>
          <tr>
            <td></td>
            <td>
  		      <v:button name="user_c_personal_7" value="Cancel" action="simple">
		      <v:on-post>
                self.m_mode := 'M/browse';
                self.vc_data_bind (e);
		      </v:on-post>
		  </v:button>
  		      <v:button name="user_s_personal_7" value="--case when self.m_mode = 'M/add' then 'Add' else 'Update' end" action="simple" xhtml_onclick="return validateInputs(this);">
  		        <v:on-post>
  		          <![CDATA[
                  declare tmp, descr, iri, name, url varchar;
                  declare stat, msg, dta, mdta, qrs, params any;

if (not self.vc_is_valid)
  return;

                  params := e.ve_params;
                  self.m_name := get_keyword ('m_name', params);
                  url := self.m_url.ufl_value;
                    descr := self.m_descr.ufl_value;
                  name := self.m_name;
iri := url;
tmp := uuid ();
{
  declare exit handler for sqlstate '*'
  {
    exec (sprintf ('sparql clear graph <%s>', tmp), stat, msg);
    goto nexts;
  };
  qrs := vector (0,0,0);
  exec (sprintf ('sparql load "%s" into graph <%s>', url, tmp));

                    qrs[0] := sprintf ('sparql
                                        prefix doap: <http://usefulinc.com/ns/doap#>
                                        select ?P ?N ?D
                                          from <%s>
                                         where { ?P a doap:Project ; doap:name ?N ; doap:description ?D . }', tmp);
                    qrs[1] := sprintf ('sparql
                                        prefix foaf: <http://xmlns.com/foaf/0.1/>
                                        select ?P ?N ?D
                                          from <%s>
                                         where { ?P a foaf:Organization ; foaf:name ?N . optional { ?P foaf:dummy ?D . } }', tmp);
                    qrs[2] := sprintf ('sparql
                                        prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
      			     prefix dc: <http://purl.org/dc/elements/1.1/>
			     prefix foaf: <http://xmlns.com/foaf/0.1/>
                                    		select ?P ?N ?D ?TI
                                    		  from <%s>
                                			      where { ?P a ?TP .
			     	optional { ?P foaf:name ?N } .
				optional { ?P rdfs:label ?D . } .
				optional { ?P dc:title ?TI }
                                         				filter ( ?P = <%s> )
                                         			 }', tmp, url);
  foreach (any qr in qrs) do
    {
      exec (qr, stat, msg, vector (), 0, mdta, dta);
      if (length (dta) and length (dta[0]) > 3)
	{
	  iri := url;
	  name := coalesce (dta[0][1], dta[0][2], dta[0][3]);
	  descr := coalesce (name, descr);
	  goto _found;
	}
      else if (length (dta) and length (dta[0]) > 2)
	{
	  iri := dta[0][0];
	  name := dta[0][1];
	  descr := coalesce (dta[0][2], descr);
	  goto _found;
	}
    }
  _found:
  exec (sprintf ('sparql clear graph <%s>', tmp), stat, msg);
}
nexts:;

                  if (length (self.m_name))
                    name := self.m_name;

                    name := cast (name as varchar);
                    descr := cast (descr as varchar);
if (not length (name))
  {
    self.vc_error_message := 'The title of item made is not specified nor can be retrieved from source URI, please specify.';
                    self.vc_is_valid := 0;
    return;
  }
                  if (not length (descr))
                  {
                    self.vc_error_message := 'The description of item made is not specified nor can be retrieved from source URI, please specify.';
                    self.vc_is_valid := 0;
                    return;
                  }
                  if (self.m_mode = 'M/add')
  {
                    insert into DB.DBA.WA_USER_PROJECTS (WUP_U_ID, WUP_NAME, WUP_URL, WUP_DESC, WUP_IRI)
                      values (self.u_id, name, url, descr, iri);
                  } else {
                    update DB.DBA.WA_USER_PROJECTS
                       set WUP_NAME = name,
                           WUP_URL = url,
                           WUP_DESC = descr,
                           WUP_IRI = iri
                     where WUP_ID = self.m_id;
  }

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