<?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="folders" decor="template/template.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">
  <vm:pagetitle>Folders</vm:pagetitle>
  <vm:pagewrapper>
    <vm:variables>
      <v:variable name="v_id" param-name="id" type="integer" default="0"/>
      <v:variable name="v_parent_id" type="varchar" default="'0'"/>
      <v:variable name="v_name" type="varchar" default="''"/>
      <v:variable name="v_mode" param-name="mode" type="varchar" default="''" persist="0"/>
      <v:variable persist="session" name="ft_state" type="any" default="null" />
      <v:variable persist="session" name="ft_bookmark" type="varchar" default="null" />
    </vm:variables>
    <vm:pagebody>
      <v:before-render>
        <![CDATA[
          self.F1.uf_action := ENEWS.WA.utf2wide (ENEWS.WA.page_url (self.domain_id, 'folders.vspx'));
        ]]>
      </v:before-render>
      <v:on-post>
        <![CDATA[
          if (e.ve_button is not null) {
            declare btn vspx_button;
            btn := null;
            if (e.ve_button.vc_name = 'ft_toggle') {
              btn := e.ve_button;
            } else if (e.ve_button.vc_name = 'ft_leaf_url') {
              btn := e.ve_button;
            } else if (e.ve_button.vc_name = 'ft_leaf_image') {
              btn := e.ve_button.vc_parent.vc_find_control ('ft_leaf_url');
            }
            if (btn is not null)
              self.ft_bookmark := 'btn_' || btn.vc_get_name ();
          }
        ]]>
      </v:on-post>
      <v:template type="simple" enabled="--either(equ(self.v_mode, ''), 1, 0)">
        <div style="padding: 0 0 0.5em 0;">
          <v:button value="create" action="simple" xhtml_class="button" xhtml_alt="Create folder">
            <v:on-post>
              <![CDATA[
                self.v_id := 0;
                self.v_mode := 'create';
                self.vc_data_bind(e);
              ]]>
            </v:on-post>
          </v:button>
        </div>

        <div class="tree_main">
          <v:tree name="ft" multi-branch="1" orientation="vertical" root="ENEWS.WA.folder_root" child-function="ENEWS.WA.folder_child" start-path="--cast(self.domain_id as varchar)">
            <v:before-render>
              <![CDATA[
                self.ft_state := control.vc_get_state ();
              ]]>
            </v:before-render>
            <v:before-data-bind>
              <![CDATA[
                if (self.ft_state is not null and not e.ve_is_post)
                  control.vc_set_control_state (self.ft_state);
              ]]>
            </v:before-data-bind>
            <v:node-template>
              <?vsp
                if (control.tn_level  = 0) {
                  http ('<div style="white-space: nowrap;">');
                } else {
                  http ('<div style="margin-left:12px; white-space: nowrap;">');
                }
              ?>
                <table cellspacing="0">
                  <tr>
                    <td nowrap="nowrap">
                      <v:button name="ft_toggle"
                                action="simple"
                                style="image"
                                value="--case when ((control.vc_parent as vspx_tree_node).tn_open) then 'image/folder_open_16.png' else 'image/folder_16.png' end"
                                xhtml_alt="--case when ((control.vc_parent as vspx_tree_node).tn_open) then 'Close blogs' else 'Open blogs' end"
                                xhtml_class="nolink"
                                enabled="--case when ((control.vc_parent as vspx_tree_node).tn_is_leaf) then 0 else 1 end"/>
                      <?vsp
                        declare S varchar;

                        S := (control as vspx_tree_node).tn_value;
                        if ((control as vspx_tree_node).tn_is_leaf) {
                          http (sprintf('<img src="image/folder_empty_16.png"/>&nbsp;<span style="font-size: 1em; font-weight: bold;">%s</span>', S));
                        } else {
                          http (sprintf('<a href="#" onclick="javascript: clickNode2(this); return false;" alt="%s" title="%s" class="nolink3">%s</a>', S, S, S));
                        }
                      ?>
                    </td>
                    <td width="100%" valign="center">
                      <div style="height: 1px; margin: 0 1em 0 1em; border-style: dotted; border: solid #935000; border-width: 0px 0px 1px 0px;"/>
                    </td>
                    <td nowrap="nowrap">
                      <v:button action="simple" value="edit" xhtml_class="button" xhtml_alt="edit folder">
                        <v:on-post>
                          <![CDATA[
                            self.v_id := cast(xpath_eval('./@id', (control.vc_parent as vspx_tree_node).tn_element, 1) as integer);
                            self.v_mode := 'update';
                            self.vc_data_bind (e);
                          ]]>
                        </v:on-post>
                      </v:button>
                      <v:button value="delete" action="simple" xhtml_onclick="return confirm(\'Are you sure that you want to delete this folder?\');" xhtml_class="button" xhtml_alt="Delete folder">
                        <v:on-post>
                          <![CDATA[
                            self.v_id := cast(xpath_eval('./@id', (control.vc_parent as vspx_tree_node).tn_element, 1) as integer);
                            ENEWS.WA.folder_delete(self.domain_id, self.v_id);
                            self.vc_data_bind(e);
                          ]]>
                        </v:on-post>
                      </v:button>
                    </td>
                  </tr>
                </table>
                <v:node/>
              <?vsp
                http (sprintf ('</div>'));
              ?>
            </v:node-template>
          </v:tree>
        </div>
        <v:template type="simple" enabled="--case when (length(ENEWS.WA.folder_root(self.domain_id)) > 0) then 1 else 0 end">
          <div style="margin-top: 0.5em; height: auto" class="tree_main">
            <table cellspacing="0">
              <tr>
                <td width="100%" align="right">
                  <b>All&amp;nbsp;</b>
                </td>
                <td nowrap="nowrap">
                  <v:button value="delete" action="simple" xhtml_onclick="return confirm(\'Are you sure that you want to delete all folders?\');" xhtml_class="button" xhtml_alt="Delete all folders">
                    <v:on-post>
                      <![CDATA[
                        ENEWS.WA.folder_delete_all(self.domain_id);
                        self.vc_data_bind(e);
                      ]]>
                    </v:on-post>
                  </v:button>
                </td>
              </tr>
            </table>
          </div>
        </v:template>
      </v:template>
      <v:template type="simple" enabled="--either(equ(self.v_mode, ''), 0, 1)">
        <v:before-data-bind>
          <![CDATA[
             declare exit handler for not found goto _end;

            if (isnull(get_keyword('f_name', self.vc_page.vc_event.ve_params))) {
              select EFO_PARENT_ID,
                     EFO_NAME
                into self.v_parent_id,
                     self.v_name
                from ENEWS.WA.FOLDER
               where EFO_DOMAIN_ID = self.domain_id
                 and EFO_ID = self.v_id;

              self.v_mode := 'update';
              return;
            }

          _end:
            self.v_parent_id := cast(get_keyword('f_parent_id', self.vc_page.vc_event.ve_params, '0') as integer);
            self.v_name := get_keyword('f_name', self.vc_page.vc_event.ve_params, '');
          ]]>
        </v:before-data-bind>
        <div class="form-header">
          <v:label format="%s" value="--concat(initcap(self.v_mode), ' folder')"/>
        </div>
        <div class="new-form-body">
          <table cellspacing="0">
            <tr>
              <th>
                <v:label for="f_parent" value="Parent folder"/>
              </th>
              <td>
                <v:data-list name="f_parent_id" value="--self.v_parent_id" sql="select 0 as EFO_ID, 'Select parent folder ...' as EFO_PATH2, '' as EFO_PATH from WS.WS.SYS_DAV_USER where U_NAME = 'dav' union all select EFO_ID, ENEWS.WA.folder_path2(EFO_DOMAIN_ID, EFO_ID) as EFO_PATH2, ENEWS.WA.folder_path(EFO_DOMAIN_ID, EFO_ID) as EFO_PATH from ENEWS.WA.FOLDER where EFO_DOMAIN_ID = self.domain_id order by EFO_PATH" key-column="EFO_ID" value-column="EFO_PATH2" xhtml_class="select"/>
              </td>
            </tr>
            <tr>
              <th>
                <v:label for="f_name" value="Folder name"/>
              </th>
              <td>
                <v:text name="f_name" null-value="--''" value="--ENEWS.WA.utf2wide(self.v_name)" xhtml_size="70%"/>
              </td>
            </tr>
          </table>
        </div>
        <div class="form-footer">
          <v:button action="simple" value="Save" xhtml_class="form-button">
            <v:on-post>
              <![CDATA[
                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 := ENEWS.WA.test(self.f_name.ufl_value, vector('name', 'Folder name', 'class', 'folder', 'type', 'varchar', 'minLength', 1, 'maxLength', 60));
                self.v_parent_id := cast(self.f_parent_id.ufl_value as integer);
                if (self.v_parent_id = 0)
                  self.v_parent_id := null;

                if (ENEWS.WA.folder_check_unique(self.domain_id, self.v_parent_id, self.v_name, self.v_id))
                  signal('TEST', 'Please, enter other folder name. This name already exists!<>');

                if ((self.v_mode <> 'create') and ENEWS.WA.folder_check_parent(self.domain_id, self.v_id, self.v_parent_id))
                  signal('TEST', 'Please, enter other parent folder. This folder is superior of the selected parent folder!<>');

                if (self.v_mode = 'create') {
                  insert into ENEWS.WA.FOLDER(EFO_DOMAIN_ID, EFO_PARENT_ID, EFO_NAME) values(self.domain_id, self.v_parent_id, self.v_name);
                } else {
                  update ENEWS.WA.FOLDER
                     set EFO_PARENT_ID = self.v_parent_id,
                         EFO_NAME = self.v_name
                   where EFO_ID = self.v_id;
                }
                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[
                self.v_mode := '';
                self.vc_data_bind(e);
              ]]>
            </v:on-post>
          </v:button>
        </div>
      </v:template>
      <?vsp
        if (self.ft_bookmark is not null) {
          http ('\n<script type="text/javascript">\n');
          http (sprintf ('if (!OAT.Browser.isOpera) location.hash = "%s";\n', self.ft_bookmark));
          http ('</script>\n');
        }
      ?>
    </vm:pagebody>
  </vm:pagewrapper>
</v:page>
