<?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
 -
-->
<script type="text/javascript" src="/ods/users_select.js"></script>
<script type="text/javascript" src="/ods/oat/loader.js"></script>
<v:page name="webid_select"
        style="index.xsl"
        fast-render="1"
        xmlns:v="http://www.openlinksw.com/vspx/"
        xmlns:vm="http://www.openlinksw.com/vspx/ods/"
        doctype="-//W3C//DTD XHTML 1.0 Transitional//EN">

  <vm:page>
    <vm:header>
      <vm:title>Select WebID</vm:title>
      <link rel="stylesheet" type="text/css" href="default.css" />
      <style type="text/css">
        div.boxHeader {
          background-color: #EFEFEF;
          margin: 0 0 0.5em 0;
          padding: 2px;
          border: 1px solid #7F94A5;
          vertical-align: middle;
        }
      </style>
      <script type="text/javascript">
      	window.onkeyup = function (event) {
      		if (event.keyCode == 27) {
      			window.close ();
        }
      	}
      </script>
    </vm:header>

    <v:method name="sortChange" arglist="in columnName varchar">
      <![CDATA[
        if (columnName <> '')
        {
        self.ds.vc_reset();
          if (self.v_order = columnName)
          {
          self.v_direction := either(equ(self.v_direction, 'asc'), 'desc', 'asc');
        } else {
          self.v_direction := 'asc';
        self.v_order := columnName;
          }
        }
      ]]>
    </v:method>

    <v:method name="isMultiple" arglist="in dst any">
      <![CDATA[
        return case when isnull (strstr (self.v_dst, 'm')) then 0 else 1 end;
      ]]>
    </v:method>

    <v:before-data-bind>
      <![CDATA[
        if (isnull(self.v_filter))
          self.v_filter := self.v_mode;
      ]]>
    </v:before-data-bind>

    <vm:pagewrapper odsbar="no" >
      <vm:variables>
        <v:variable name="v_params" param-name="params" type="varchar"/>
        <v:variable name="v_mode" persist="0" param-name="mode" type="varchar" default="''"/>
        <v:variable name="v_value" persist="0" type="varchar" default="''"/>
        <v:variable name="v_filter" persist="0" type="varchar" default="null"/>
        <v:variable name="v_order" persist="0" type="varchar" default="'F2'" />
        <v:variable name="v_direction" persist="0" type="varchar" default="'asc'" />
        <v:variable name="v_nrows" persist="0" param-name="nrows" type="varchar" default="'10'" />
        <v:variable name="v_src" persist="0" param-name="src" type="varchar" default="''" />
        <v:variable name="v_dst" persist="0" param-name="dst" type="varchar" default="'sc'" />
        <v:variable name="v_s1" persist="0" param-name="s1" type="varchar" default="''" />
        <v:variable name="v_s2" persist="0" param-name="s2" type="varchar" default="''" />
      </vm:variables>
      <vm:body>
        <vm:login redirect="index.vspx"/>
        <input type="hidden" name="nrows" id="sid"   value="<?V get_keyword('nrows', self.vc_page.vc_event.ve_params, '10') ?>" />
        <input type="hidden" name="sid"   id="sid"   value="<?V get_keyword('sid', self.vc_page.vc_event.ve_params) ?>" />
        <input type="hidden" name="realm" id="realm" value="<?V get_keyword('realm', self.vc_page.vc_event.ve_params) ?> " />
        <input type="hidden" name="form"  id="form"  value="<?V get_keyword('form', self.vc_page.vc_event.ve_params, 'page_form') ?> " />
        <input type="hidden" name="mode"  id="mode"  value="<?V get_keyword('mode', self.vc_page.vc_event.ve_params, '') ?> " />
        <input type="hidden" name="src"   id="src"   value="<?V get_keyword('src', self.vc_page.vc_event.ve_params, '') ?>" />
        <input type="hidden" name="dst"   id="dst"   value="<?V get_keyword('dst', self.vc_page.vc_event.ve_params, 'sc') ?>" />
        <input type="hidden" name="s1"    id="s1"    value="<?V get_keyword('s1', self.vc_page.vc_event.ve_params, '') ?>" />
        <input type="hidden" name="s2"    id="s2"    value="<?V get_keyword('s2', self.vc_page.vc_event.ve_params, '') ?>" />
        <v:text name="params" type="hidden" value="--self.v_params"/>
        <div style="padding: 0.2em;">
          <div style="padding: 0 0 0.5em 0;">
            <span class="button pointer" onclick="javascript: if (opener != null) opener.focus(); window.close();"><img class="button" src="/ods/images/icons/close_16.png" border="0" alt="Close" title="Close" /> Close</span>
          </div>
          <vm:if test="self.v_mode = ''">
          <div class="boxHeader">
            Show
              <v:select-list name="filter" value="--self.v_filter">
              <v:item name="Persons & Groups" value="" />
              <v:item name="Persons" value="p" />
              <v:item name="Groups" value="g" />
            </v:select-list>
          </div>
          </vm:if>
          <v:data-source name="dsrc" expression-type="sql" nrows="10" initial-offset="0">
            <v:before-data-bind>
              <![CDATA[
                declare user_sql, group_sql varchar;

                user_sql := '';
                group_sql := '';
                if (self.v_filter = 'u')
                {
                  control.ds_sql := 'select ''Person'' F1, SIOC..person_iri (SIOC..user_iri (U_ID)) F2, '''' F3 from DB.DBA.SYS_USERS where U_IS_ROLE = 0 and U_DAV_ENABLE = 1 and U_ACCOUNT_DISABLED = 0';
                }
                else
                {
                if (self.v_filter <> 'p')
                {
                  group_sql := sprintf ('select ''Group'' F1, SIOC..acl_group_iri (%d, WACL_NAME) F2, WACL_DESCRIPTION F3 from WA_GROUPS_ACL where WACL_USER_ID = %d', self.u_id, self.u_id);
                }
                if (self.v_filter <> 'g')
                {
                    user_sql := sprintf ('select x.F1, x.F2, x.F3 from DB.DBA.wa_webid_users(user_id) (F1 varchar, F2 varchar, F3 varchar) x where x.user_id = %d', self.u_id);;
                }
                if (user_sql = '')
                {
                  control.ds_sql := group_sql;
                }
                else if (group_sql = '')
                {
                  control.ds_sql := user_sql;
                }
                else
                {
                  control.ds_sql := concat('select F1, F2, F3 from (', user_sql, ' union ' , group_sql, ') x ');
                }
                }
                self.sortChange(get_keyword('sortColumn', e.ve_params, ''));
                control.ds_sql := concat(control.ds_sql, ' order by ', self.v_order, ' ', self.v_direction);
                control.ds_nrows := cast (self.v_nrows as integer);
              ]]>
            </v:before-data-bind>
            <v:after-data-bind>
              control.ds_make_statistic ();
            </v:after-data-bind>
          </v:data-source>
          <v:data-set name="ds" data-source="self.dsrc" scrollable="1">
            <v:template name="ds_header" type="simple" name-to-remove="table" set-to-remove="bottom">
              <table class="ODS_grid">
                <thead>
                  <tr>
                  <vm:if test="self.isMultiple(self.v_dst)">
                      <th class="checkbox">
                      <input type="checkbox" name="cb_all" value="Select All" onclick="selectAllCheckboxes(this, 'cb_item'); updateChecked(this, 'cb_item'); "/>
                    </th>
                  </vm:if>
                  <vm:if test="self.v_mode = ''">
                      <th>Type</th>
                  </vm:if>
                    <?vsp http (DB.DBA.wa_show_column_header('WebID', 'F2', self.v_order, self.v_direction, 'width="50%"')); ?>
                    <?vsp http (DB.DBA.wa_show_column_header('Description', 'F3', self.v_order, self.v_direction, 'width="50%"')); ?>
                </tr>
                </thead>
              </table>
            </v:template>

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

              <v:template name="ds_empty" type="if-not-exists" name-to-remove="table" set-to-remove="both">
                <table>
                  <td colspan="<?V either(equ(self.v_mode,''),3,2)+self.isMultiple(self.v_dst) ?>">
                    No records
                  </td>
                </table>
              </v:template>

              <v:template name="ds_browse" type="browse" name-to-remove="table" set-to-remove="both">
                <table>
                  <tr onclick="rowSelected(this);" class="pointer">
                    <vm:if test="self.isMultiple(self.v_dst)">
                      <td class="checkbox">
                        <?vsp
                          declare S, name any;

                          name := (control as vspx_row_template).te_column_value('U_NAME');
                          S := case when isnull (strstr(self.v_s1, concat(name, ','))) then '' else 'checked="checked"' end;
                          http (sprintf ('<input type="checkbox" name="cb_item" value="%s" %s onclick="javascript: updateChecked(this, ''cb_item'', event); " />', name, S));
                        ?>
                      </td>
                    </vm:if>
                    <vm:if test="self.v_mode = ''">
                      <td nowrap="nowrap">
                        <v:label value="--(control.vc_parent as vspx_row_template).te_column_value('F1')" format="%s"/>
                      </td>
                    </vm:if>
                    <td nowrap="nowrap">
                      <input name="s1_item" type="hidden" value="<?V (control as vspx_row_template).te_column_value('F2') ?>" />
                      <v:label value="--(control.vc_parent as vspx_row_template).te_column_value('F2')" format="%s"/>
                    </td>
                    <td nowrap="nowrap">
                      <input name="s2_item" type="hidden" value="<?V (control as vspx_row_template).te_column_value('F3') ?>" />
                      <v:label value="--(control.vc_parent as vspx_row_template).te_column_value('F3')" format="%s"/>
                    </td>
                  </tr>
                </table>
              </v:template>

            </v:template>

            <v:template type="simple" name-to-remove="table" set-to-remove="top">
              <table>
                <tfoot>
                  <tr>
                    <td style="text-align: right;" colspan="<?V either(equ(self.v_mode,''),3,2)+self.isMultiple(self.v_dst) ?>">
                    <vm:ds-navigation data-set="ds"/>
                  </td>
                </tr>
                </tfoot>
              </table>
            </v:template>

          </v:data-set>
          <vm:if test="self.isMultiple(self.v_dst)">
            <div style="padding: 0 0 0.5em 0;">
              <hr />
              <a href="#" onclick="javascript: addChecked(document.forms['page_form'], 'cb_item', 'No users were selected for addition. No groups were selected for addition.');" class="navi-button">&amp;nbsp;Add selected</a>
            </div>
          </vm:if>
        </div>
      </vm:body>
    </vm:pagewrapper>
  </vm:page>
</v:page>
