<?xml version="1.0"?>
<!--
 -
 -  $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="tags" 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">
  <vm:pagetitle>Tags page</vm:pagetitle>
  <vm:popup_pagewrapper>
        <vm:variables>
          <v:variable persist="0" name="v_fid" param-name="fid" type="integer"/>
          <v:variable persist="0" name="v_did" param-name="did" type="integer"/>
          <v:variable persist="0" name="v_aid" param-name="aid" type="integer"/>
          <v:variable persist="0" name="step" type="integer" default="0" />
        </vm:variables>
    <vm:pagebody>
          <div class="form-header">
            <v:label format="%s" value="--'Tags'" />
          </div>
          <table class="form-body" cellspacing="0">
            <v:template type="simple" enabled="--case when (self.step = 0) then 1 else 0 end">
              <tr>
            <th style="text-align: center; background-color: #B0CDE4;">
                  <input type="checkbox" name="cbx_all" checked="checked" value="Unselect All" onClick="selectAllCheckboxes(this.form, this, 'cb_')"/>
                </th>
            <th style="text-align: center; background-color: #B0CDE4; width: 100%;">Tag</th>
              </tr>
              <?vsp
                declare i integer;
                declare tags, joinTags, allTags any;

                joinTags := '';
                tags := ENEWS.WA.tags_item_rules(self.v_fid, self.v_aid);
                if (not is_empty_or_null(tags)) {
                  joinTags := concat(joinTags, ',', tags);
                  http('<tr><td colspan="2"><b>&nbsp;Rule''s tags</b></td></tr>');
                  tags := ENEWS.WA.tags2vector (tags);
  		            for (i := 0; i < length (tags); i := i + 1) {
              ?>
                <tr>
                  <td align="center">
                    <?vsp
                      http(sprintf('<input type="checkbox" name="cb_item" checked="checked" value="r_%s"/>', tags[i]));
                    ?>
                  </td>
                  <td>
                    &nbsp;<?V tags[i] ?>
                  </td>
                </tr>
              <?vsp
                  }
                }
                tags := ENEWS.WA.tags_item_content(self.v_fid);
                if (not is_empty_or_null(tags)) {
                  joinTags := concat(joinTags, ',', tags);;
                  http('<tr><td colspan="2"><b>&nbsp;Content tags</b></td></tr>');
                  tags := ENEWS.WA.tags2vector (tags);
  		            for (i := 0; i < length (tags); i := i + 1) {
              ?>
                <tr>
                  <td align="center">
                    <?vsp
                      http(sprintf('<input type="checkbox" name="cb_item" checked="checked" value="c_%s"/>', tags[i]));
                    ?>
                  </td>
                  <td>
                    &nbsp;<?V tags[i] ?>
                  </td>
                </tr>
              <?vsp
                  }
                }
                tags := ENEWS.WA.tags_item_domain(self.v_fid, self.v_did);
                if (not is_empty_or_null(tags)) {
                  joinTags := concat(joinTags, ',', tags);
                  http('<tr><td colspan="2"><b>&nbsp;Domain tags</b></td></tr>');
                  tags := ENEWS.WA.tags2vector (tags);
  		            for (i := 0; i < length (tags); i := i + 1) {
              ?>
                <tr>
                  <td align="center">
                    <?vsp
                      http(sprintf('<input type="checkbox" name="cb_item" checked="checked" value="d_%s"/>', tags[i]));
                    ?>
                  </td>
                  <td>
                    &nbsp;<?V tags[i] ?>
                  </td>
                </tr>
              <?vsp
                  }
                }
                joinTags := trim(joinTags, ',');
                joinTags := ENEWS.WA.tags2vector (joinTags);
                allTags := coalesce((select EFID_TAGS from ENEWS.WA.FEED_ITEM_DATA where EFID_ITEM_ID = self.v_fid and EFID_DOMAIN_ID is null and EFID_ACCOUNT_ID = self.v_aid), '');
                allTags := ENEWS.WA.tags2vector (allTags);
                tags := ENEWS.WA.vector_except(allTags, joinTags);
                if (length(tags)) {
                  http('<tr><td colspan="2"><b>&nbsp;User tags</b></td></tr>');
  		            for (i := 0; i < length (tags); i := i + 1) {
              ?>
                <tr>
                  <td align="center">
                    <?vsp
                      http(sprintf('<input type="checkbox" name="cb_item" checked="checked" value="u_%s"/>', tags[i]));
                    ?>
                  </td>
                  <td>
                    &nbsp;<?V tags[i] ?>
                  </td>
                </tr>
              <?vsp
                  }
                }
              ?>
            </v:template>
            <v:template type="simple" enabled="--case when (self.step = 0) then 0 else 1 end">
              <tr>
                <th class="form-body2-th" style="text-align: center; height: 60px;">
                  <v:label value="''" >
                    <v:after-data-bind>
                      <![CDATA[
                        if (self.step = 1)
                          control.ufl_value := 'Tags are changed successfully!';
                        if (self.step = 2)
                          control.ufl_value := 'Tags are removed successfully!';
                      ]]>
                    </v:after-data-bind>
                  </v:label>
                </th>
              </tr>
            </v:template>
          </table>
          <div class="form-footer">
            <v:button action="simple" value="Submit" enabled="--case when (self.step = 0) then 1 else 0 end" xhtml_class="form-button">
              <v:on-post>
                <![CDATA[
                  declare i integer;
                  declare vectorTags, tags any;

                  vectorTags := vector();
                  for (i := 0; i < length(e.ve_params); i := i + 2)
                    if (e.ve_params[i] = 'cb_item') {
                      vectorTags := vector_concat(vectorTags, vector(subseq(e.ve_params[i+1], 2)));
                    }

                  vectorTags := ENEWS.WA.vector_unique(vectorTags);
                  tags := '';
                  for (i := 0; i < length(vectorTags); i := i + 1)
                    tags := concat (tags, ',', vectorTags[i]);
                  tags := trim(tags, ',');

                  -- save tags
                  ENEWS.WA.tags_account_item(self.v_aid, self.v_fid, tags);

                  self.step := 1;
                  if (tags = '')
                    self.step := 2;
                  self.vc_data_bind(e);
                ]]>
              </v:on-post>
            </v:button>
            <v:button action="simple" value="Close" enabled="--case when (self.step = 0) then 0 else 1 end" xhtml_onclick=" javascript: if (opener != null) { opener.document.forms[0].submit(); opener.focus();} window.close(); " xhtml_class="form-button"/>
          </div>
    </vm:pagebody>
  </vm:popup_pagewrapper>
    </v:page>
