~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to kexi/webforms/view/default/Create.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2010-09-21 15:36:35 UTC
  • mfrom: (1.4.1 upstream) (60.2.11 maverick)
  • Revision ID: james.westby@ubuntu.com-20100921153635-6tejqkiro2u21ydi
Tags: 1:2.2.2-0ubuntu3
Add kubuntu_03_fix-crash-on-closing-sqlite-connection-2.2.2.diff and
kubuntu_04_support-large-memo-values-for-msaccess-2.2.2.diff as
recommended by upstream http://kexi-
project.org/wiki/wikiview/index.php@Kexi2.2_Patches.html#sqlite_stab
ility

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of the KDE project
 
3
 *
 
4
 * (C) Copyright 2008 by Lorenzo Villani <lvillani@binaryhelix.net>
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU Library General Public
 
8
 * License as published by the Free Software Foundation; either
 
9
 * version 2 of the License, or (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
 * Library General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU Library General Public License
 
17
 * along with this library; see the file COPYING.LIB.  If not, write to
 
18
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
19
 * Boston, MA 02110-1301, USA.
 
20
 */
 
21
 
 
22
#include <QMap>
 
23
#include <QPair>
 
24
#include <QHash>
 
25
#include <QString>
 
26
 
 
27
#include <KDebug>
 
28
 
 
29
#include <kexidb/tableschema.h>
 
30
 
 
31
#include <google/template.h>
 
32
 
 
33
#include <pion/net/HTTPResponseWriter.hpp>
 
34
 
 
35
#include "model/DataProvider.h"
 
36
#include "model/Database.h"
 
37
 
 
38
#include "TemplateProvider.h"
 
39
 
 
40
#include "Create.h"
 
41
 
 
42
namespace KexiWebForms {
 
43
namespace View {
 
44
 
 
45
void Create::view(const QHash<QString, QString>& d, pion::net::HTTPResponseWriterPtr writer) {
 
46
    m_dict = initTemplate("create.tpl");
 
47
 
 
48
    /* Retrieve the requested table name */
 
49
    QString requestedTable(d["kwebforms__table"]);
 
50
    setValue("TABLENAME", requestedTable);
 
51
 
 
52
    KexiDB::TableSchema* tableSchema = KexiWebForms::Model::Database::getSchema(requestedTable);
 
53
 
 
54
 
 
55
    /* send form data */
 
56
    if (d["dataSent"] == "true") {
 
57
        QString tableFields(d["tableFields"]);
 
58
        QStringList fieldsList(QUrl::fromPercentEncoding(tableFields.toUtf8()).split("|:|"));
 
59
        kDebug() << "Fields: " << fieldsList;
 
60
 
 
61
        QHash<QString, QVariant> data;
 
62
        foreach(const QString& field, fieldsList) {
 
63
            KexiDB::Field* currentField = tableSchema->field(field);
 
64
            if (currentField)
 
65
                data[field] = QVariant(d[field]);
 
66
        }
 
67
 
 
68
        if (KexiWebForms::Model::Database::updateRow(requestedTable, data, true)) {
 
69
            m_dict->ShowSection("SUCCESS");
 
70
            setValue("MESSAGE", "Row added successfully");
 
71
        } else {
 
72
            m_dict->ShowSection("ERROR");
 
73
            setValue("MESSAGE", KexiWebForms::Model::gConnection->errorMsg());
 
74
        }
 
75
    }
 
76
 
 
77
    QString formData;
 
78
    QStringList fieldsList;
 
79
 
 
80
    foreach(const KexiDB::Field* f, *tableSchema->fields()) {
 
81
        formData.append("\t<tr>\n");
 
82
        formData.append(QString("\t\t<td>%1</td>\n").arg(f->captionOrName()));
 
83
        if (f->type() == KexiDB::Field::LongText) {
 
84
            formData.append(QString("\t\t<td><textarea name=\"%1\">%2</textarea></td>\n")
 
85
                            .arg(f->name()).arg(f->defaultValue().toString()));
 
86
        } else if (f->type() == KexiDB::Field::Boolean) {
 
87
            // TODO: Create a checkbox instead
 
88
            formData.append(QString("\t\t<td><input type=\"text\" name=\"%1\" value=\"%2\"/></td>\n")
 
89
                            .arg(f->name()).arg(f->defaultValue().toString()));
 
90
        } else {
 
91
            formData.append(QString("\t\t<td><input type=\"text\" name=\"%1\" value=\"%2\"/></td>\n")
 
92
                            .arg(f->name()).arg(f->defaultValue().toString()));
 
93
        }
 
94
        // Field icons
 
95
        formData.append("\t\t<td>\n");
 
96
        if (f->isPrimaryKey())
 
97
            formData.append("<img src=\"/f/toolbox/primary-key.png\" alt=\"Primary Key\"/>");
 
98
 
 
99
        if (f->isNotEmpty() && f->isAutoIncrement()) {
 
100
            formData.append("<img src=\"/f/toolbox/auto-increment.png\" alt=\"Auto Increment\"/>");
 
101
        } else if (f->isNotNull()) {
 
102
            formData.append("<img src=\"/f/toolbox/emblem-required.png\" alt=\"Required\"/>");
 
103
        }
 
104
        formData.append("\n</td>\n\t</tr>\n");
 
105
        fieldsList << f->name();
 
106
    }
 
107
 
 
108
    setValue("TABLEFIELDS", fieldsList.join("|:|"));
 
109
    setValue("FORMDATA", formData);
 
110
 
 
111
    renderTemplate(m_dict, writer);
 
112
    delete m_dict;
 
113
}
 
114
}
 
115
}