<?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="users_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 user/group</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 (self.v_mode = 'g')
        {
          self.v_filter := 'g';
        }
        else if (self.v_mode = 'u')
        {
          self.v_filter := 'u';
        }
        else if (self.v_mode like 's_%')
        {
          self.v_filter := 'u';
        }
      ]]>
    </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_filter" persist="0" type="varchar" default="''"/>
      <v:variable name="v_value" persist="0" type="varchar" default="''"/>
      <v:variable name="v_mask" persist="0" type="varchar" default="''"/>
      <v:variable name="v_how" persist="0" type="varchar" default="'1'"/>
      <v:variable name="v_order" persist="0" type="varchar" default="'U_NAME'" />
      <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"/>
        <?vsp http(sprintf('<input type="hidden" name="nrows" id="nrows" value="%s"/>', get_keyword('nrows', self.vc_page.vc_event.ve_params, '10'))); ?>
      <?vsp http(sprintf('<input type="hidden" name="sid"   id="sid"   value="%s"/>', get_keyword('sid', self.vc_page.vc_event.ve_params))); ?>
      <?vsp http(sprintf('<input type="hidden" name="realm" id="realm" value="%s"/>', get_keyword('realm', self.vc_page.vc_event.ve_params))); ?>
        <?vsp http(sprintf('<input type="hidden" name="form"  id="form"  value="%s"/>', get_keyword('form', self.vc_page.vc_event.ve_params, 'page_form'))); ?>
      <?vsp http(sprintf('<input type="hidden" name="src"   id="src"   value="%s"/>', get_keyword('src', self.vc_page.vc_event.ve_params, ''))); ?>
      <?vsp http(sprintf('<input type="hidden" name="dst"   id="dst"   value="%s"/>', get_keyword('dst', self.vc_page.vc_event.ve_params, 'sc'))); ?>
      <?vsp http(sprintf('<input type="hidden" name="s1"    id="s1"    value="%s"/>', get_keyword('s1', self.vc_page.vc_event.ve_params, ''))); ?>
      <?vsp http(sprintf('<input type="hidden" name="s2"    id="s2"    value="%s"/>', 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>
        <div class="boxHeader">
            Show
          <v:select-list name="filter" value="--self.v_filter" enabled="-- case when (self.v_mode = '') then 1 else 0 end">
            <v:item name="Users & Groups" value="" />
              <v:item name="Users" value="u" />
            <v:item name="Groups" value="g" />
          </v:select-list>
            <v:label value="--case self.v_mode when 'u' then ' users' when 'g' then ' groups' else '' end" enabled="-- case when (self.v_mode = '') then 0 else 1 end"/>
          <v:select-list name="how" value="--self.v_how">
            <v:item name="Starting with" value="1" />
            <v:item name="Containing" value="2" />
            <v:item name="Ending with" value="3" />
            <v:item name="Exact match" value="4" />
          </v:select-list>
          <v:text name="value" value="--self.v_value" xhtml_size="15"/>
          <v:button action="simple" value="Filter" xhtml_class="button">
            <v:on-post>
              <![CDATA[
                  self.v_filter := get_keyword('filter', e.ve_params, self.v_filter);
                self.v_how := self.how.ufl_value;
                self.v_value := trim(self.value.ufl_value);

                  self.ds.vc_reset();
                self.vc_data_bind (e);
              ]]>
            </v:on-post>
          </v:button>
          <v:button name="reset" action="simple" value="Clear" xhtml_class="button">
            <v:on-post>
              <![CDATA[
                self.v_how := '1';
                self.v_value := '';

                  self.ds.vc_reset();
                self.vc_data_bind (e);
              ]]>
            </v:on-post>
          </v:button>
            <label>
            <v:check-box auto-submit="1" name="show_all_cbx" xhtml_id="show_all_cbx" initial-checked="--0" value="--1"/>
              Show all
            </label>
        </div>
          <v:data-source name="dsrc" expression-type="sql" nrows="0" initial-offset="0">
          <v:before-data-bind>
            <![CDATA[
              declare user_sql, group_sql varchar;


              self.v_mask := case self.v_how
                               when '1' then self.v_value || '%'
                               when '2' then '%' || self.v_value || '%'
                               when '3' then '%' || self.v_value
                               else self.v_value end;
              if (self.v_mask = '')
                self.v_mask := '%';
              self.v_mask := replace(self.v_mask, '%%', '%');

              user_sql := '';
              group_sql := '';
              if (self.v_filter <> 'u')
              {
                  if (DB.DBA.wa_user_is_dba (connection_get ('vspx_user'), null) = 1)
                  {
                    group_sql := sprintf('select ''Group'' U_TYPE, a.U_NAME, coalesce(a.U_FULL_NAME, a.U_NAME) U_FULL_NAME from DB.DBA.SYS_USERS a where (a.U_IS_ROLE=1) and (upper(a.U_NAME) like upper(\'%s\') and (U_ID<>5) and (U_ID<>6))', self.v_mask);
                  } else {
                    group_sql := sprintf('select ''Group'' U_TYPE, a.U_NAME, coalesce(a.U_FULL_NAME, a.U_NAME) U_FULL_NAME from DB.DBA.SYS_USERS a, DB.DBA.WA_GROUPS b, DB.DBA.SYS_USERS c where (a.U_ID=b.WAG_GROUP_ID) and (a.U_IS_ROLE=1) and (b.WAG_USER_ID=c.U_ID) and (c.U_NAME=\'%s\') and (upper(a.U_NAME) like upper(\'%s\') and (a.U_ID<>5) and (a.U_ID<>6))', self.u_name, self.v_mask);
                  }
              }
              if (self.v_filter <> 'g')
              {
                  if (self.v_mode = 's_bmk')
                  {
                    user_sql := sprintf('select ''User'' U_TYPE, b.U_NAME, coalesce(b.U_FULL_NAME, \'\') U_FULL_NAME from BMK..GRANTS_VIEW a, DB.DBA.SYS_USERS b where (b.U_ACCOUNT_DISABLED=0) and upper(b.U_NAME) like upper(\'%s\') and a.GW_ID = %d and a.U_ID = b.U_ID', self.v_mask, self.u_id);
                  } else {
                user_sql := sprintf('select ''User'' U_TYPE, U_NAME, coalesce(U_FULL_NAME, \'\') U_FULL_NAME from WS.WS.SYS_DAV_USER where (U_ACCOUNT_DISABLED=0) and (upper(U_NAME) like upper(\'%s\') and (U_ID<>5) and (U_ID<>6))', self.v_mask);
              }
                }
                if (user_sql = '')
                {
                control.ds_sql := group_sql;
                }
                else if (group_sql = '')
                {
                control.ds_sql := user_sql;
                }
                else
                {
                  control.ds_sql := concat('select U_TYPE, U_NAME, U_FULL_NAME 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);
                if (get_keyword('show_all_cbx', self.vc_page.vc_event.ve_params, 0))
                {
                  self.ds.vc_reset();
                  control.ds_nrows := 1000;
                  control.ds_rows_fetched := 0;
                  control.ds_rows_offs := 0;
                }
                else
                {
                  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('Name', 'U_NAME', self.v_order, self.v_direction, 'width="50%"')); ?>
                    <?vsp http (DB.DBA.wa_show_column_header('Description', 'U_FULL_NAME', 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>
                <tr align="center">
                    <td colspan="<?V either(equ(self.v_mode,''),3,2)+self.isMultiple(self.v_dst) ?>">
                      No records
                    </td>
                </tr>
              </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('U_TYPE')" format="%s"/>
                    </td>
                    </vm:if>
                    <td width="50%" nowrap="nowrap">
                      <input name="s1_item" type="hidden" value="<?V (control as vspx_row_template).te_column_value('U_NAME') ?>" />
                    <v:label value="--(control.vc_parent as vspx_row_template).te_column_value('U_NAME')" format="%s"/>
                  </td>
                    <td width="50%" nowrap="nowrap">
                      <input name="s2_item" type="hidden" value="<?V (control as vspx_row_template).te_column_value('U_FULL_NAME') ?>" />
                    <v:label value="--(control.vc_parent as vspx_row_template).te_column_value('U_FULL_NAME')" 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: return 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>
