File: filterManager.cpp

package info (click to toggle)
postbooks 4.10.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 112,660 kB
  • ctags: 22,890
  • sloc: cpp: 310,358; sh: 607; xml: 214; python: 140; awk: 104; makefile: 50
file content (171 lines) | stat: -rw-r--r-- 5,509 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * This file is part of the xTuple ERP: PostBooks Edition, a free and
 * open source Enterprise Resource Planning software suite,
 * Copyright (c) 1999-2014 by OpenMFG LLC, d/b/a xTuple.
 * It is licensed to you under the Common Public Attribution License
 * version 1.0, the full text of which (including xTuple-specific Exhibits)
 * is available at www.xtuple.com/CPAL.  By using this software, you agree
 * to be bound by its terms.
 */

#include "filterManager.h"
#include "parameterwidget.h"
#include "shortcuts.h"
#include <xtreewidget.h>
#include <xsqlquery.h>
#include <QMessageBox>
#include <QSqlError>
#include <QValidator>
#include <QVariant>
#include <QDialog>
#include <QtDebug>

filterManager::filterManager(QWidget* parent, const char* name)
    : QDialog(parent)
{
  if(name)
    setObjectName(name);

  setupUi(this);

  _filterSet->addColumn(tr("Filter Set Name"), -1, Qt::AlignLeft, true, "filter_name" );
  _filterSet->addColumn(tr("Shared"), _ynColumn, Qt::AlignLeft, true, "shared");

  connect(_filterSet, SIGNAL(valid(bool)), this, SLOT( handleButtons(bool) ));
  connect(_share, SIGNAL(clicked()), this, SLOT( shareFilter() ));
  connect(_unshare, SIGNAL(clicked()), this, SLOT( unshareFilter() ));
  connect(_delete, SIGNAL(clicked()), this, SLOT( deleteFilter() ) );
  connect(this, SIGNAL(filterDeleted()), parent, SLOT(setSavedFilters()) );

  shortcuts::setStandardKeys(this);
}

void filterManager::set(const ParameterList &pParams)
{
  QVariant param;
  bool     valid;

  param = pParams.value("screen", &valid);
  if (valid)
  {
    _screen = param.toString();
    populate();
  }
}

void filterManager::populate()
{
  if (parent())
  {
    if (parent()->parent())
    {
      XSqlQuery qry;
      qry.prepare("select filter_id, filter_name, "
                  " case when filter_username is null then "
                  "  true "
                  " else false end as shared "
                  "from filter "
                  "where COALESCE(filter_username,getEffectiveXtUser())=getEffectiveXtUser() "
                  " and filter_screen=:screen "
                  "order by filter_name;");
      qry.bindValue(":screen", parent()->parent()->objectName());
      qry.exec();

      _filterSet->populate(qry, false);
    }
  }
}
void filterManager::unshareFilter()
{
	XSqlQuery qry;
	if ( QMessageBox::question( this, tr("Unshare Filter"),
                                tr("<p>This will un-share the selected filter and assign it to you.\n"
                                   " Are you sure this is what you want to do?"),
                                QMessageBox::No | QMessageBox::Default,
                                QMessageBox::Yes) == QMessageBox::No )
      return;
	else
	{
		qry.prepare("UPDATE filter SET filter_username=getEffectiveXtUser() WHERE (filter_id=:filter_id);");
		qry.bindValue(":filter_id", _filterSet->id());
		qry.exec();
	}

	populate();

}
void filterManager::shareFilter()
{
  XSqlQuery qry;

  // See if there is already a shared filter by this name
  qry.prepare("SELECT shr.filter_id AS filter_id "
              "FROM filter prv "
              " JOIN filter shr ON ((prv.filter_name=shr.filter_name) "
              "                 AND (prv.filter_screen=shr.filter_screen) "
              "                 AND (shr.filter_id != prv.filter_id)) "
              "WHERE ((shr.filter_username IS NULL) "
              " AND (prv.filter_id=:filter_id)); ");
  qry.bindValue(":filter_id", _filterSet->id());
  qry.exec();
  if (qry.first())
  {
    if ( QMessageBox::question( this, tr("Shared Filter Exists"),
                                tr("<p>This will replace a shared filter with the same name.\n"
                                   " Are you sure this is what you want to do?"),
                                QMessageBox::No | QMessageBox::Default,
                                QMessageBox::Yes) == QMessageBox::No )
      return;
    else
    {
      int filterid = qry.value("filter_id").toInt();
      qry.prepare("UPDATE filter shr SET filter_value=prv.filter_value "
                  "FROM filter prv "
                  "WHERE ((shr.filter_id=:shr_filter_id) "
                  " AND (prv.filter_id=:prv_filter_id)); "
                  "DELETE FROM filter WHERE (filter_id=:prv_filter_id);");
      qry.bindValue(":shr_filter_id", filterid);
      qry.bindValue(":prv_filter_id", _filterSet->id());
      qry.exec();

      emit filterDeleted();
    }
  }
  else
  {
    qry.prepare("UPDATE filter SET filter_username=NULL WHERE (filter_id=:filter_id);");
    qry.bindValue(":filter_id", _filterSet->id());
    qry.exec();
  }

  populate();
}

void filterManager::deleteFilter()
{
  QString query = "delete from filter where filter_id=:filter_id";
  XSqlQuery qry;

  qry.prepare(query);
  qry.bindValue(":filter_id", _filterSet->id());
  qry.exec();

  this->populate();

  emit filterDeleted();
}

void filterManager::handleButtons(bool valid)
{
  _share->setEnabled(valid &&
                     !_filterSet->currentItem()->rawValue("shared").toBool() &&
                     _x_privileges->check("AllowSharedFilterEdit"));

  _delete->setEnabled(valid &&
                     (!_filterSet->currentItem()->rawValue("shared").toBool() ||
                      _x_privileges->check("AllowSharedFilterEdit")));

  _unshare->setEnabled(valid &&
                       _filterSet->currentItem()->rawValue("shared").toBool() &&
                       _x_privileges->check("AllowSharedFilterEdit"));
}