<?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="users" decor="template/popup.vspx" style="template/template.xsl" fast-render="1" xmlns:v="http://www.openlinksw.com/vspx/" xmlns:vm="http://www.openlinksw.com/vspx/macro" doctype="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

  <v:method name="sortChange" arglist="in columnName varchar">
    <![CDATA[
      if (columnName = '')
        return;
      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[
      declare pos integer;

      pos := strstr(self.v_dst, 'm');
      if (not isnull(pos))
        return 1;
      return 0;
    ]]>
  </v:method>

  <vm:pagetitle>Select Contacts</vm:pagetitle>

  <vm:popup_page_wrapper>
    <vm:variables>
      <v:variable name="v_params" param-name="params" type="varchar"/>
      <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="'P_NAME'" />
      <v:variable name="v_direction" persist="0" type="varchar" default="'asc'" />
      <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:pagebody>
      <?vsp
        declare params any;

        params := self.vc_page.vc_event.ve_params;

        http (sprintf ('<input type="hidden" name="sid"   id="sid"   value="%s"/>', get_keyword('sid', params)));
        http (sprintf ('<input type="hidden" name="realm" id="realm" value="%s"/>', get_keyword('realm', params)));
        http (sprintf ('<input type="hidden" name="form"  id="form"  value="%s"/>', get_keyword('form', params, 'page_form')));
        http (sprintf ('<input type="hidden" name="src"   id="src"   value="%s"/>', get_keyword('src', params, '')));
        http (sprintf ('<input type="hidden" name="dst"   id="dst"   value="%s"/>', get_keyword('dst', params, 'sc')));
        http (sprintf ('<input type="hidden" name="s1"    id="s1"    value="%s"/>', get_keyword('s1', params, '')));
        http (sprintf ('<input type="hidden" name="s2"    id="s2"    value="%s"/>', get_keyword('s2', params, '')));
      ?>
      <v:text name="params" type="hidden" value="--self.v_params"/>
      <div class="boxHeader">
        Show
        <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_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.vc_data_bind (e);
            ]]>
          </v:on-post>
        </v:button>
      </div>
      <v:data-source name="dsrc" expression-type="sql" nrows="0" initial-offset="0">
        <v:before-data-bind>
          <![CDATA[
            control.ds_nrows := CAL.WA.settings_rows (self.settings);

            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, '%%', '%');

            control.ds_sql := sprintf ('select distinct a.P_NAME, a.P_MAIL from AB.WA.PERSONS a, DB.DBA.WA_MEMBER b, DB.DBA.WA_INSTANCE c where a.P_DOMAIN_ID = c.WAI_ID and c.WAI_TYPE_NAME = ''AddressBook'' and c.WAI_NAME = b.WAM_INST and B.WAM_MEMBER_TYPE = 1 and b.WAM_USER = %d and DB.DBA.is_empty_or_null (a.P_MAIL) <> 1 and (upper (a.P_NAME) like upper(''%s''))', self.account_id, self.v_mask);
            self.sortChange (get_keyword('sortColumn', e.ve_params, ''));
            control.ds_sql := concat (control.ds_sql, ' order by ', self.v_order, ' ', self.v_direction);
          ]]>
        </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>
                <v:template type="simple" enabled="--equ(self.isMultiple(self.v_dst), 1)">
                  <th class="checkbox">
                    <?vsp
                      http('<input type="checkbox" name="cb_all" value="Select All" onclick="selectAllCheckboxes(this.form, this); updateChecked(this, ''cb_item''); "/>');
                    ?>
                  </th>
                </v:template>
                <?vsp http (DB.DBA.wa_show_column_header('Name', 'P_NAME', self.v_order, self.v_direction, 'width="50%"')); ?>
                <?vsp http (DB.DBA.wa_show_column_header('Mail', 'P_MAIL', self.v_order, self.v_direction, 'width="50%"')); ?>
                <v:template type="simple" enabled="--neq(self.isMultiple(self.v_dst), 1)">
                  <th class="action">Action</th>
                </v:template>
              </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.isMultiple(self.v_dst),1),4,3) ?>">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">
                <v:template type="simple" enabled="--equ(self.isMultiple(self.v_dst), 1)">
                  <td class="checkbox">
                    <?vsp
                      declare S, name any;

                      name := (control.vc_parent as vspx_row_template).te_column_value('P_MAIL');
                      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>
                </v:template>
                <td nowrap="nowrap">
                  <v:label value="--(control.vc_parent as vspx_row_template).te_column_value('P_NAME')" format="%s"/>
                </td>
                <td nowrap="nowrap">
                  <v:label value="--(control.vc_parent as vspx_row_template).te_column_value('P_MAIL')" format="%s"/>
                </td>
                <v:template type="simple" enabled="--neq(self.isMultiple(self.v_dst), 1)">
                  <td nowrap="nowrap">
                    <v:text name="s1" type="hidden" value="--((control.vc_parent).vc_parent as vspx_row_template).te_column_value('P_NAME')"/>
                    <v:text name="s2" type="hidden" value="--((control.vc_parent).vc_parent as vspx_row_template).te_column_value('P_MAIL')"/>
                    <v:button name="b1" action="simple" value="select" xhtml_alt="Select" xhtml_onclick="javascript: rowSelect(this); return false;" xhtml_class="button"/>
                  </td>
                </v:template>
              </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.isMultiple(self.v_dst),1),4,3) ?>">
                  <vm:ds-navigation data-set="ds"/>
                </td>
              </tr>
            </tfoot>
          </table>
        </v:template>

      </v:data-set>
      <v:template type="simple" enabled="--equ(self.isMultiple(self.v_dst), 1)">
        <div style="padding: 0 0 0.5em 0;">
          <hr />
          <span onclick="javascript: addChecked(document.forms['F1'], 'cb_item', 'No users were selected for addition.');" class="button"> Add selected</span>
        </div>
      </v:template>
    </vm:pagebody>
  </vm:popup_page_wrapper>
</v:page>
