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

	<v:variable name="r_id" type="integer" default="null" />
	<v:variable name="r_properties" type="any" default="null" />
	<v:variable name="r_ontology" type="any" default="null" />
	<v:form name="usr_personal_6" type="simple" method="POST" xhtml_enctype="multipart/form-data">
    <h3>Favorites</h3>
    <vm:if test="self.r_mode = 'R/browse'">
      <?vsp
        if (0)
        {
      ?>
        <v:button name="r_dButton" action="simple" value="Delete">
          <v:on-post>
            <![CDATA[
              delete from DB.DBA.WA_USER_FAVORITES where WUF_ID = get_keyword ('r_id', self.vc_page.vc_event.ve_params) and WUF_U_ID = self.u_id;
              self.vc_data_bind(e);
            ]]>
          </v:on-post>
        </v:button>
      <?vsp
      }
      ?>
      <input type="hidden" name="r_id" id="r_id" value="" />
      <div style="padding: 0 0 0.5em 0;">
        <v:button name="r_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.r_mode := 'R/add';
              self.r_flag.ufl_value := null;
              self.r_label.ufl_value := null;
              self.r_uri.ufl_value := null;
              self.r_properties := vector ();

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

	    <v:data-set name="r_ds"
	                sql="select WUF_ID, WUF_LABEL, WUF_URI from DB.DBA.WA_USER_FAVORITES where WUF_U_ID = :self.u_id"
		              scrollable="1"
		              editable="1"
		              nrows="0">

        <v:template name="r_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="1%" nowrap="nowrap">Action</th>
      	    </tr>
          </table>
        </v:template>

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

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

          <v:template name="r_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('WUF_LABEL')" format="%s"/>
                </td>
                <td nowrap="nowrap" width="50%">
                  <v:label value="--(control.vc_parent as vspx_row_template).te_column_value('WUF_URI')" format="%s"/>
                </td>
                <td nowrap="nowrap">
                  <v:button name="r_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 Offer" title="Edit Offer"/> Edit';
                      ]]>
                    </v:before-render>
                    <v:on-post>
                      <![CDATA[
                        self.r_mode := 'R/edit';
                        self.r_id := (control.vc_parent as vspx_row_template).te_column_value('WUF_ID');
                        select WUF_FLAG,
                               WUF_LABEL,
                               WUF_URI,
                               deserialize (WUF_PROPERTIES)
                          into self.r_flag.ufl_value,
                               self.r_label.ufl_value,
                               self.r_uri.ufl_value,
                               self.r_properties
                          from DB.DBA.WA_USER_FAVORITES
                         where WUF_ID = self.r_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?')) {$('r_id').value = <?V (control as vspx_row_template).te_column_value('WUF_ID') ?>; doPost('page_form', 'r_dButton');}">
                    <img class="button" src="/ods/images/icons/trash_16.png"/> Delete
                  </span>
  			        </td>
  			      </tr>
            </table>
		      </v:template>
		    </v:template>

		    <v:template name="r_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.r_mode <> 'R/browse'">
      <v:before-data-bind>
        <![CDATA[
          self.r_ontology := vector (
                                     vector_concat (
                                                    ODS..jsonObject (),
                                                    vector (
                                                            'id', '0',
                                                            'ontology', 'http://rdfs.org/sioc/ns#',
                                                            'items', vector (
                                                                             vector_concat (
                                                                                            ODS..jsonObject (),
                                                                                            vector (
                                                                                                    'id', '0',
                                                                                                    'className', 'sioc:Item',
                                                                                                    'properties', self.r_properties
                                                                                                   )
                                                                                           )
                                                                             )
                                                           )
                                                   )
                                    );
        ]]>
      </v:before-data-bind>
  	  <div class="fm">
				<table>
        <tr>
				    <th width="100px">
		          Access
		        </th>
		        <td>
              <v:select-list name="r_flag" xhtml_id="r_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="r_label" value="" xhtml_id="r_label" xhtml_size="50" xhtml_class="_validate_" />
            </td>
          </tr>
				  <tr>
				    <th valign="top">
		          External URI
                  </th>
		        <td>
              <v:text name="r_uri" value="" xhtml_id="r_uri" xhtml_class="_validate_ _url_ _canEmpty_" xhtml_size="50" />
            </td>
                </tr>
				  <tr>
				    <th valign="top">
		          Properties
		        </th>
		        <td width="800px">
              <table id="r_tbl" class="listing">
              <tbody id="r_tbody">
                  <tr id="r_item_0_tr_0_properties">
                  <td></td>
                    <td></td>
                    <td valign="top"></td>
        </tr>
              </tbody>
      </table>
      <script type="text/javascript">
        <![CDATA[
          ODSInitArray.push ( function () {
            OAT.Loader.load(["ajax", "json", "combolist"], function(){
                      RDF.tablePrefix = 'r';
                      RDF.itemTypes = <?vsp http (ODS..obj2json (self.r_ontology)); ?>;
                      RDF.loadOntology(
                        'http://rdfs.org/sioc/ns#',
                        function(){
                          RDF.loadClassProperties(
                            RDF.getOntologyClass('sioc:Item'),
                            function(){
                              RDF.showPropertiesTable(RDF.itemTypes[0].items[0]);
                            }
                          );
                        }
                      )
            });
          });
        ]]>
      </script>
            </td>
          </tr>
          <tr>
            <td></td>
            <td>
      <br />
    		      <v:button name="user_c_personal_6" value="Cancel" action="simple">
          <v:on-post>
                  self.r_mode := 'R/browse';
                  self.vc_data_bind (e);
          </v:on-post>
        </v:button>
    		      <v:button name="user_s_personal_6" value="--case when self.r_mode = 'R/add' then 'Add' else 'Update' end" action="simple" xhtml_onclick="return validateInputs(this);">
          <v:on-post>
            <![CDATA[
                    declare params, properties any;

                    params := e.ve_params;
                    if (not length (self.r_label.ufl_value))
                    {
                      self.vc_error_message := 'The title can''t be empty.';
                      self.vc_is_valid := 0;
                      return;
                    }
                    properties := self.getProperties('r', params, '0', '0');
                    if (self.r_mode = 'R/add')
                    {
                      insert into DB.DBA.WA_USER_FAVORITES (WUF_U_ID, WUF_FLAG, WUF_TYPE, WUF_CLASS, WUF_LABEL, WUF_URI, WUF_PROPERTIES)
                        values (self.u_id, self.r_flag.ufl_value, 'http://rdfs.org/sioc/ns#', 'sioc:Item', self.r_label.ufl_value, self.r_uri.ufl_value, serialize (properties));
                    } else {
                      update DB.DBA.WA_USER_FAVORITES
                         set WUF_FLAG = self.r_flag.ufl_value,
                             WUF_LABEL = self.r_label.ufl_value,
                             WUF_URI = self.r_uri.ufl_value,
                             WUF_PROPERTIES = serialize (properties)
                       where WUF_ID = self.r_id;
                    }

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