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
|
/*
* SPL - The SPL Programming Language
* Copyright (C) 2004, 2005 Clifford Wolf <clifford@clifford.at>
*
* This program 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; either version 2 of the License, or
* (at your option) any later version.
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* mod_wsf_edit_sql.spl: A WSF Edit Component for SQL databases
*/
/**
* A module which provides a generic component for editing records in SQL
* databases, based on [[wsf_edit:WsfEdit]].
*/
load "sql";
load "wsf_edit";
/**
* A WSF Component for editing records in SQL databases.
*
* E.g. there is a database handle 'db' (see [[sql:]]) for accessing a database
* with a table 'user'. In this table exist the fields 'firstname' and
* 'lastname' which should be editable using this comonent. Records are indexed
* using the field 'userid'. The field 'username' should be displayed in the
* form, but is not editable. Creating an editor for record 42 in this table
* could look like this:
*
* var editor = new WsfEditSql(42, db, "user", "userid",
* function() { return #file-as-template *edit_template; });
*
* #embedded-file edit_template END-OF-TEMPLATE
* <form id="$id" ${add_action(cgi.url)}>
* Editing user '${edit_data.username}': <br/>
* Realname: ${edit_input("", "60", "realname")} <br/>
* Firstname: ${edit_input("", "60", "firstname")}
* </form>
* END-OF-TEMPLATE
*
* This object is derived from [[wsf_edit:WsfEdit]].
*/
object WsfEditSql WsfEdit
{
/**
* The ID of the record in the databse.
* This is looked up in field [[.spl_key]].
*/
var sql_id;
/**
* The database handle.
*/
var sql_db;
/**
* The table name.
*/
var sql_table;
/**
* The field which is used to find the record to be edited.
* (see also [[.spl_id]])
*/
var sql_key;
/**
* The function which evaluates the template. When executed, this will
* be context-dispatched to this object (using the '*' operator).
*/
var get_html_core;
/**
* Overloaded [[wsf_edit:WsfEdit.edit_load()]].
*/
method edit_load()
{
var r = sql(sql_db, "select * from $sql_table where ${sql_key} = ${sql::sql_id} limit 1");
edit_data = r.[0];
}
/**
* Overloaded [[wsf_edit:WsfEdit.edit_save()]].
*/
method edit_save()
{
var query = "update $sql_table set";
var delim = "";
foreach i (edit_fields) {
if (declared cgi.param.["edit_${edit_fields.[i]}"]) {
query ~= "${delim} ${edit_fields.[i]} = ${sql::cgi.param.["edit_${edit_fields.[i]}"]}";
delim = ",";
}
}
if (delim ~== ",") {
query ~= " where ${sql_key} = ${sql::sql_id}";
sql(sql_db, query);
just_updated = 1;
} else {
just_updated = 0;
}
}
/**
* Overloaded [[wsf_edit:WsfEdit.get_html()]].
*
* Simply calls [[.get_html_core]] (context-dispatched to this object)
* to do the real work.
*/
method get_html()
{
edit_reset();
return *get_html_core();
}
/**
* The Constructor.
*
* The parameters are simply copyied to the variables described
* above.
*
* See the general discription of this object ([[WsfEditSql]]) for
* a usage example.
*/
method init(_sql_id, _sql_db, _sql_table, _sql_key, _get_html_core)
{
sql_id = _sql_id;
sql_db = _sql_db;
sql_table = _sql_table;
sql_key = _sql_key;
get_html_core = _get_html_core;
return *WsfEdit.init();
}
}
|