<?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="sfolders" decor="template/template.vspx" style="template/template.xsl" fast-render="1" button-anchors="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="sortColumn" arglist="in titleName varchar, in columnName varchar">
    <![CDATA[
      declare altStr, directionStr, imageStr varchar;

      if (self.sf_order = columnName and self.sf_direction = 'desc') {
        directionStr := 'Ascending';
        imageStr := '&nbsp;<img src="image/d.gif" border="0" alt="Down"/>';
      } else if (self.sf_order = columnName and self.sf_direction = 'asc') {
        directionStr := 'Descending';
        imageStr := '&nbsp;<img src="image/u.gif" border="0" alt="Up"/>';
      } else {
        directionStr := 'Ascending';
        imageStr := '&nbsp;&nbsp;';
      }
      altStr := sprintf('Sort Rows on %s in %s Order', titleName, directionStr);
      http(sprintf('<a href="#" onClick="javascript: myPost(''F1'', ''sortColumn'', ''%s''); return false;" alt="%s" title="%s">%s%s</a>', columnName, altStr, altStr, titleName, imageStr));
    ]]>
  </v:method>
  <v:method name="sortChange" arglist="in columnName varchar">
    <![CDATA[
      if (columnName = '')
        return;
      self.ds.vc_reset();
      if (self.sf_order = columnName) {
        self.sf_direction := either(equ(self.sf_direction, 'asc'), 'desc', 'asc');
      } else {
        self.sf_direction := 'asc';
      }
      self.sf_order := columnName;
    ]]>
  </v:method>
  <vm:pagetitle>Smart Folders</vm:pagetitle>
  <vm:pagewrapper>
    <vm:variables>
      <v:variable name="sf_order" persist="1" type="varchar" default="'ESFO_NAME'" />
      <v:variable name="sf_direction" persist="1" type="varchar" default="'asc'" />

      <v:variable name="v_mode" type="varchar" param-name="mode" default="''"/>

      <v:variable name="v_id" param-name="id" persist="0" type="varchar" default="''"/>
      <v:variable name="v_return" param-name="return" persist="0" type="varchar" default="''"/>
      <v:variable name="v_name" type="varchar" default="''"/>
      <v:variable name="v_keywords" type="varchar" default="''"/>
      <v:variable name="v_expression" type="varchar" default="''"/>
      <v:variable name="v_tags" type="varchar" default="''"/>
      <v:variable name="v_read" type="varchar" default="''"/>
      <v:variable name="v_flag" type="varchar" default="''"/>
      <v:variable name="v_after" type="varchar" default="''"/>
      <v:variable name="v_before" type="varchar" default="''"/>
    </vm:variables>
    <vm:pagebody>
      <v:before-render>
        <![CDATA[
          self.F1.uf_action := ENEWS.WA.utf2wide (ENEWS.WA.page_url (self.domain_id, 'sfolders.vspx'));
        ]]>
      </v:before-render>
      <v:template type="simple" enabled="--either(equ(self.v_mode, ''), 1, 0)">
      <v:data-source name="dsrc" expression-type="sql" nrows="0" initial-offset="0">
        <v:before-data-bind>
          <![CDATA[
            self.sortChange(get_keyword('sortColumn', e.ve_params, ''));
            control.ds_nrows := ENEWS.WA.settings_rows (self.settings);
            control.ds_sql := sprintf('select ESFO_ID, ESFO_NAME from ENEWS.WA.SFOLDER where ESFO_DOMAIN_ID = %d', self.domain_id);
            control.ds_sql := concat(control.ds_sql, ' order by ', self.sf_order, ' ', self.sf_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">
        <div style="padding: 0 0 0.5em 0;">
          <v:button action="simple" value="create" xhtml_class="button" xhtml_alt="Create Smart folder">
            <v:on-post>
              <![CDATA[
                  self.v_id := 0;
                  self.v_mode := 'create';
                  self.vc_data_bind(e);
              ]]>
            </v:on-post>
          </v:button>
            <v:button value="delete" action="simple" xhtml_onclick="return confirmAction(\'Are you sure that you want to delete this smart folder?\');" xhtml_class="button" xhtml_alt="Delete Smart folder">
              <v:on-post>
                <![CDATA[
                  ENEWS.WA.sfolder_delete(self.domain_id, cast((control.vc_parent as vspx_row_template).te_column_value('ESFO_ID') as integer));
          		    self.vc_data_bind(e);
                ]]>
              </v:on-post>
            </v:button>
        </div>

        <v:template name="ds_header" type="simple" name-to-remove="table" set-to-remove="bottom">
            <table id="sfolders" class="FM_grid" cellspacing="0">
            <thead class="sortHeader">
              <tr>
                <th class="checkbox" width="1%">
                  <input type="checkbox" name="cb_all" value="Select All" onClick="selectAllCheckboxes(this.form, this, 'cb_item')" title="Select All" />
                </th>
                <th>
                  <?vsp self.sortColumn('Title', 'ESFO_NAME'); ?>
                </th>
                  <th width="1%">
                  <v:label value="Action" format="%s"/>
                </th>
              </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 id="list" cellspacing="0">
              <tr align="center">
                  <td colspan="3">No Smart folders</td>
              </tr>
            </table>
          </v:template>

          <v:template name="ds_browse" type="browse" name-to-remove="table" set-to-remove="both">
            <table id="list" cellspacing="0">
              <tr>
                <td align="center">
                  <?vsp
                    http (sprintf ('<input type="checkbox" name="cb_item" value="%s" title="%V" />', control.te_rowset[0], control.te_rowset[1]));
                  ?>
                </td>
                <td>
                  <v:label value="--(control.vc_parent as vspx_row_template).te_column_value('ESFO_NAME')" format="%s"/>
                </td>
                <td nowrap="nowrap">
                  <v:button value="edit" action="simple" xhtml_class="button" xhtml_alt="edit Smart folder">
                    <v:on-post>
                      <![CDATA[
                          self.v_id := (control.vc_parent as vspx_row_template).te_column_value('ESFO_ID');
                          self.v_mode := 'update';
                          self.vc_data_bind(e);
                      ]]>
                    </v:on-post>
                  </v:button>
                </td>
              </tr>
            </table>
          </v:template>

        </v:template>

        <v:template name="ds_footer" type="simple" name-to-remove="table" set-to-remove="top">
            <table>
            <tr>
                <td align="right" colspan="3">
                <vm:ds-navigation data-set="ds"/>
              </td>
            </tr>
          </table>
        </v:template>
      </v:data-set>
      <script type="text/javascript">
        <![CDATA[
          coloriseTable('sfolders');
        ]]>
      </script>
      </v:template>
      <v:template type="simple" enabled="--either(equ(self.v_mode, ''), 0, 1)">
        <v:before-data-bind>
          <![CDATA[
            declare params any;
     		    declare exit handler for not found goto _end;

            params := self.vc_page.vc_event.ve_params;
            if (isnull (get_keyword('f_name', params)) and (self.v_mode = 'update'))
            {
              declare data, keywords varchar;

              select ESFO_NAME,
                     ESFO_DATA
                into self.v_name,
                     data
                from ENEWS.WA.SFOLDER
               where ESFO_DOMAIN_ID = self.domain_id
                 and ESFO_ID = self.v_id;

              self.v_name := ENEWS.WA.utf2wide(self.v_name);
              self.v_keywords := ENEWS.WA.xml_get('keywords', data);
              self.v_expression := ENEWS.WA.xml_get('expression', data);
              self.v_tags := ENEWS.WA.xml_get('tags', data);
              self.v_read := ENEWS.WA.xml_get('read', data);
              self.v_flag := ENEWS.WA.xml_get('flag', data);
              self.v_after := ENEWS.WA.xml_get('afterDate', data);
              self.v_before := ENEWS.WA.xml_get('beforeDate', data);
    	    		return;
    		    }

  		    _end:
            self.v_name := get_keyword('f_name', params, '');
            self.v_keywords := trim (get_keyword('f_keywords', params, ''));
            self.v_expression := trim (get_keyword('f_expression', params, ''));
            self.v_tags := trim (get_keyword('f_tags', params, ''));
            self.v_read := trim (get_keyword('f_read', params, ''));
            self.v_flag := trim (get_keyword('f_flag', params, ''));
            self.v_after := trim (get_keyword('f_after', params, ''));
            self.v_before := trim (get_keyword('f_before', params, ''));
      			self.v_mode := get_keyword('f_mode', params, 'create');
      			if (self.v_return = 'yes')
      			{
              self.v_return := 'search.vspx?mode=advanced';
              if (not is_empty_or_null(self.v_keywords))
                self.v_return := concat(self.v_return, sprintf('&f_keywords=%U', self.v_keywords));
              if (not is_empty_or_null(self.v_tags))
                self.v_return := concat(self.v_return, sprintf('&f_tags=%U', self.v_tags));
              if (not is_empty_or_null(self.v_read))
                self.v_return := concat(self.v_return, sprintf('&f_read=%U', self.v_read));
              if (not is_empty_or_null(self.v_flag))
                self.v_return := concat(self.v_return, sprintf('&f_flag=%U', self.v_flag));
              if (not is_empty_or_null(self.v_after))
                self.v_return := concat(self.v_return, sprintf('&f_after=%U', self.v_after));
              if (not is_empty_or_null(self.v_before))
                self.v_return := concat(self.v_return, sprintf('&f_before=%U', self.v_before));
      			}
          ]]>
        </v:before-data-bind>
        <v:text name="f_mode" type="hidden" value="--self.v_mode"/>
        <div class="form-header">
          <?V concat(initcap(self.v_mode), ' Smart folder') ?>
        </div>
        <div class="new-form-body">
          <table cellspacing="0">
            <tr>
              <th>
                <v:label for="f_name" value="Smart folder name" />
              </th>
              <td>
                <v:text name="f_name" null-value="--''" value="--self.v_name" xhtml_class="textbox" xhtml_size="70%" />
              </td>
            </tr>
            <tr>
              <th>
                <v:label for="f_keywords" value="Space delimited keyword list" />
              </th>
              <td>
                <v:text name="f_keywords" value="--self.v_keywords" xhtml_class="textbox" xhtml_size="70%"/>
              </td>
            </tr>
            <tr>
              <th>
                <v:label for="" value="or expression" />
              </th>
              <td>
        			  <v:textarea name="f_expression" value="--self.v_expression" xhtml_rows="2" xhtml_cols="70"/>
              </td>
            </tr>
            <tr>
              <th>
                <v:label for="f_tags" value="Tags" />
              </th>
              <td>
                <v:text name="f_tags" value="--self.v_tags" xhtml_class="textbox" xhtml_size="70%"/>
              </td>
            </tr>
            <tr>
              <th>
                <v:label for="f_read" value="Show marked feeds"/>
              </th>
              <td>
                <v:select-list name="f_read" value="--self.v_read">
                  <v:item name="all" value=""/>
                  <v:item name="read" value="r+"/>
                  <v:item name="unread" value="r-"/>
                </v:select-list>
              </td>
            </tr>
            <tr>
              <th>
                <v:label for="f_flag" value="Show flagged feeds"/>
              </th>
              <td>
                <v:select-list name="f_flag" value="--self.v_flag">
                  <v:item name="all" value=""/>
                  <v:item name="flagged" value="f+"/>
                  <v:item name="unflagged" value="f-"/>
                </v:select-list>
              </td>
            </tr>
            <tr>
              <th>
                <v:label for="f_after" value="Date after"/>
              </th>
              <td>
                <v:text name="f_after" xhtml_id="f_after" null-value="--''" value="--self.v_after" xhtml_onclick="javascript: datePopup (\'f_after\');" xhtml_size="10" />
                <a href="#" name="f_after_select" id="f_after_select" onclick="datePopup ('f_after'); return false;" class="noapp">
                  <img border="0" title="pick" alt="pick" src="image/pick_calendar.gif" />
                </a>
              </td>
            </tr>
            <tr>
              <th>
                <v:label for="f_before" value="Date before"/>
              </th>
              <td>
                <v:text name="f_before" xhtml_id="f_before" null-value="--''" value="--self.v_before" xhtml_onclick="javascript: datePopup (\'f_before\');" xhtml_size="10" />
                <a href="#" name="f_before_select" id="f_before_select" onclick="datePopup ('f_before'); return false;" class="noapp">
                  <img border="0" title="pick" alt="pick" src="image/pick_calendar.gif" />
                </a>
              </td>
            </tr>
          </table>
        </div>
        <div class="form-footer">
          <v:button action="simple" value="Save" xhtml_class="form-button">
            <v:on-post>
              <![CDATA[
                declare tmp any;

                declare exit handler for SQLSTATE '*'
                {
                  if (__SQL_STATE = 'TEST')
                  {
                    self.vc_error_message := ENEWS.WA.test_clear(__SQL_MESSAGE);
                 		self.vc_is_valid := 0;
              		  return;
              		}
                  resignal;
                };

                self.v_name := trim (self.f_name.ufl_value);
                self.v_keywords := trim (self.f_keywords.ufl_value);
                self.v_expression := trim (self.f_expression.ufl_value);
                self.v_tags := trim (self.f_tags.ufl_value);
                self.v_read := trim (self.f_read.ufl_value);
                self.v_flag := trim (self.f_flag.ufl_value);
                self.v_after := trim (self.f_after.ufl_value);
                self.v_before := trim (self.f_before.ufl_value);

                ENEWS.WA.test (self.v_name, vector ('name', 'Smart folder name', 'class', 'folder', 'type', 'varchar', 'minLength', 1, 'maxLength', 60));
                tmp := coalesce((select ESFO_ID from ENEWS.WA.SFOLDER where ESFO_DOMAIN_ID = self.domain_id and ESFO_NAME = self.v_name), '');
                if (((self.v_mode = 'create') and (tmp <> '')) or ((self.v_mode = 'update') and (tmp <> self.v_id) and (tmp <> '')))
                  signal('TEST', 'Smart folder name already exists. Please, enter new ''Smart folder name''!<>');
                ENEWS.WA.test (self.v_keywords, vector ('name', 'Keywords', 'class', 'free-text'));
                ENEWS.WA.test (self.v_expression, vector ('name', 'Expression', 'class', 'free-text-expression'));
                ENEWS.WA.test (self.v_tags, vector ('name', ' Tags', 'class', 'tags'));
                ENEWS.WA.test (self.v_after, vector ('name', ' Date after', 'class', 'date', 'type', 'date'));
                ENEWS.WA.test (self.v_before, vector ('name', ' Date before', 'class', 'date', 'type', 'date'));

                tmp := ENEWS.WA.ft2vector (self.v_keywords);
                tmp := ENEWS.WA.vector_unique(tmp, 2);
                self.v_keywords := ENEWS.WA.vector2str(tmp);

                tmp := ENEWS.WA.tags2vector (self.v_tags);
                tmp := ENEWS.WA.vector_unique(tmp, 2);
                self.v_tags := ENEWS.WA.vector2tags(tmp);

                declare data varchar;

                ENEWS.WA.xml_set('keywords', data, self.v_keywords);
                ENEWS.WA.xml_set('expression', data, self.v_expression);
                ENEWS.WA.xml_set('tags', data, self.v_tags);
                ENEWS.WA.xml_set('read', data, self.v_read);
                ENEWS.WA.xml_set('flag', data, self.v_flag);
                ENEWS.WA.xml_set('afterDate', data, self.v_after);
                ENEWS.WA.xml_set('beforeDate', data, self.v_before);
                data := ENEWS.WA.xml2string(data);

                if (self.v_mode = 'create')
                {
                  self.v_id := ENEWS.WA.sfolder_create(self.domain_id, self.v_name, data);
                } else {
                  ENEWS.WA.sfolder_update(self.domain_id, self.v_id, self.v_name, data);
                }
                self.v_id := 0;
                self.v_mode := '';
                self.vc_data_bind(e);
              ]]>
            </v:on-post>
          </v:button>
          <v:button action="simple" value="Cancel" xhtml_class="form-button">
            <v:on-post>
              <![CDATA[
                if (self.v_return <> '')
                {
                  self.vc_redirect(self.v_return);
                } else {
                  self.v_id := 0;
                  self.v_mode := '';
                  self.vc_data_bind(e);
                }
              ]]>
            </v:on-post>
          </v:button>
        </div>
      </v:template>
    </vm:pagebody>
  </vm:pagewrapper>
</v:page>
