/* * SPL - The SPL Programming Language * Copyright (C) 2004, 2005 Clifford Wolf * * 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 *
* Editing user '${edit_data.username}':
* Realname: ${edit_input("", "60", "realname")}
* Firstname: ${edit_input("", "60", "firstname")} *
* 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(); } }