2
* This file is part of the KDE project
4
* (C) Copyright 2008 by Lorenzo Villani <lvillani@binaryhelix.net>
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.
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.
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.
29
#include <kexidb/tableschema.h>
31
#include <google/template.h>
33
#include <pion/net/HTTPResponseWriter.hpp>
35
#include "model/DataProvider.h"
36
#include "model/Database.h"
38
#include "TemplateProvider.h"
42
namespace KexiWebForms {
45
void Create::view(const QHash<QString, QString>& d, pion::net::HTTPResponseWriterPtr writer) {
46
m_dict = initTemplate("create.tpl");
48
/* Retrieve the requested table name */
49
QString requestedTable(d["kwebforms__table"]);
50
setValue("TABLENAME", requestedTable);
52
KexiDB::TableSchema* tableSchema = KexiWebForms::Model::Database::getSchema(requestedTable);
56
if (d["dataSent"] == "true") {
57
QString tableFields(d["tableFields"]);
58
QStringList fieldsList(QUrl::fromPercentEncoding(tableFields.toUtf8()).split("|:|"));
59
kDebug() << "Fields: " << fieldsList;
61
QHash<QString, QVariant> data;
62
foreach(const QString& field, fieldsList) {
63
KexiDB::Field* currentField = tableSchema->field(field);
65
data[field] = QVariant(d[field]);
68
if (KexiWebForms::Model::Database::updateRow(requestedTable, data, true)) {
69
m_dict->ShowSection("SUCCESS");
70
setValue("MESSAGE", "Row added successfully");
72
m_dict->ShowSection("ERROR");
73
setValue("MESSAGE", KexiWebForms::Model::gConnection->errorMsg());
78
QStringList fieldsList;
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()));
91
formData.append(QString("\t\t<td><input type=\"text\" name=\"%1\" value=\"%2\"/></td>\n")
92
.arg(f->name()).arg(f->defaultValue().toString()));
95
formData.append("\t\t<td>\n");
96
if (f->isPrimaryKey())
97
formData.append("<img src=\"/f/toolbox/primary-key.png\" alt=\"Primary Key\"/>");
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\"/>");
104
formData.append("\n</td>\n\t</tr>\n");
105
fieldsList << f->name();
108
setValue("TABLEFIELDS", fieldsList.join("|:|"));
109
setValue("FORMDATA", formData);
111
renderTemplate(m_dict, writer);