3 * Module to render generic HTML tables for Site Admin
5 * Copyright 1999-2001 (c) VA Linux Systems
7 * This file is part of GForge.
9 * GForge is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * GForge is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with GForge; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 * admin_table_add() - present a form for adding a record to the specified table
28 * @param $table - the table to act on
29 * @param $unit - the name of the "units" described by the table's records
30 * @param $primary_key - the primary key of the table
32 function admin_table_add($table, $unit, $primary_key) {
33 // This query may return no rows, but the field names are needed.
34 $result = db_query_params("SELECT * FROM $table WHERE $primary_key=0", array());
38 $cols = db_numfields($result);
40 printf(_('Create a new %1$s below:'), getUnitLabel($unit));
43 <form name="add" action="'.getStringFromServer('PHP_SELF').'?function=postadd" method="post">
44 <input type="hidden" name="form_key" value="'.form_generate_key().'" />
47 for ($i = 0; $i < $cols; $i++) {
48 $fieldname = db_fieldname($result, $i);
49 $fields[] = $fieldname;
51 echo '<tr><td><strong>'.$fieldname.'</strong></td>';
52 echo '<td><input type="text" name="'.$fieldname.'" value="" /></td></tr>';
54 echo '</table><input type="submit" value="'._('Add').'" />
55 <input type="hidden" name="__fields__" value="'.implode(',',$fields).'">
57 <form name="cancel" action="'.getStringFromServer('PHP_SELF').'" method="post">
58 <input type="submit" value="'._('Cancel').'" />
66 * admin_table_postadd() - update the database based on a submitted change
68 * @param $table - the table to act on
69 * @param $unit - the name of the "units" described by the table's records
70 * @param $primary_key - the primary key of the table
72 function admin_table_postadd($table, $unit, $primary_key) {
73 if (!form_key_is_valid(getStringFromRequest('form_key'))) {
74 exit_form_double_submit();
77 $field_list = getStringFromRequest('__fields__');
78 $fields = split(",", $field_list);
80 foreach ($fields as $field) {
81 $values[] = "'".getStringFromPost($field)."'";
84 $sql = "INSERT INTO $table (".$field_list.") VALUES (".implode(",", $values).")";
87 printf(_('%1$s successfully added.'), ucfirst(getUnitLabel($unit)));
89 form_release_key(getStringFromRequest('form_key'));
95 * admin_table_confirmdelete() - present a form to confirm requested record deletion
97 * @param $table - the table to act on
98 * @param $unit - the name of the "units" described by the table's records
99 * @param $primary_key - the primary key of the table
100 * @param $id - the id of the record to act on
102 function admin_table_confirmdelete($table, $unit, $primary_key, $id) {
103 if ($unit == "processor") {
104 $result = db_numrows(db_query_params ('SELECT processor_id FROM frs_file WHERE processor_id = $1',
107 echo '<p>'.sprintf(_('You can\'t delete the processor %1$s since it\'s currently referenced in a file release.'), db_result(db_query_params ('select name from frs_processor where processor_id = $1',
108 array($id)), 0, 0)).'</p>';
112 if ($unit == "license") {
113 $result = db_numrows(db_query_params ('SELECT license FROM groups WHERE license = $1',
116 echo '<p>'.sprintf(_('You can\'t delete the license %1$s since it\'s currently referenced in a project.'), db_result(db_query_params ('select license_name from licenses where license_id = $1',
117 array($id)), 0, 0)).'</p>';
121 if ($unit == "supported_language") {
122 $result = db_numrows(db_query_params("SELECT language FROM users WHERE language=$1", array($id)));
124 echo '<p>'.sprintf(_('You can\'t delete the language %1$s since it\'s currently referenced in a user profile.'), db_result(db_query_params ('select license_name from licenses where license_id = $1',
125 array($id)), 0, 0)).'</p>';
130 $result = db_query_params("SELECT * FROM $table WHERE $primary_key=$1", array($id));
133 $cols = db_numfields($result);
135 printf(_('Are you sure you want to delete this %1$s?'), getUnitLabel($unit)).'<ul>';
136 for ($i = 0; $i < $cols; $i++) {
137 echo '<li><strong>'.db_fieldname($result,$i).'</strong> '.db_result($result,0,$i).'</li>';
140 <form name="delete" action="'.getStringFromServer('PHP_SELF').'?function=delete&id='.$id.'" method="post">
141 <input type="submit" value="'._('Delete').'" />
143 <form name="cancel" action="'.getStringFromServer('PHP_SELF').'" method="post">
144 <input type="submit" value="'._('Cancel').'" />
152 * admin_table_delete() - delete a record from the database after confirmation
154 * @param $table - the table to act on
155 * @param $unit - the name of the "units" described by the table's records
156 * @param $primary_key - the primary key of the table
157 * @param $id - the id of the record to act on
159 function admin_table_delete($table, $unit, $primary_key, $id) {
160 if (db_query_params("DELETE FROM $table WHERE $primary_key=$1", array($id))) {
161 printf(_('%1$s successfully deleted.'), ucfirst(getUnitLabel($unit)));
168 * admin_table_edit() - present a form for editing a record in the specified table
170 * @param $table - the table to act on
171 * @param $unit - the name of the "units" described by the table's records
172 * @param $primary_key - the primary key of the table
173 * @param $id - the id of the record to act on
175 function admin_table_edit($table, $unit, $primary_key, $id) {
176 $result = db_query_params("SELECT * FROM $table WHERE $primary_key=$1", array($id));
179 $cols = db_numfields($result);
181 printf(_('Modify the %1$s below:'), getUnitLabel($unit));
184 <form name="edit" action="'.getStringFromServer('PHP_SELF').'?function=postedit&id='.$id.'" method="post">
187 for ($i = 0; $i < $cols; $i++) {
188 $fieldname = db_fieldname($result, $i);
189 $value = db_result($result, 0, $i);
191 echo '<tr><td><strong>'.$fieldname.'</strong></td>';
193 if ($fieldname == $primary_key) {
194 echo "<td>$value</td></tr>";
196 echo '<td><input type="text" name="'.$fieldname.'" value="'.$value.'" /></td></tr>';
199 echo '</table><input type="submit" value="'._('Submit').'" /></form>
200 <form name="cancel" action="'.getStringFromServer('PHP_SELF').'" method="post">
201 <input type="submit" value="'._('Cancel').'" />
209 * admin_table_postedit() - update the database to reflect submitted modifications to a record
211 * @param $table - the table to act on
212 * @param $unit - the name of the "units" described by the table's records
213 * @param $primary_key - the primary key of the table
214 * @param $id - the id of the record to act on
216 function admin_table_postedit($table, $unit, $primary_key, $id) {
217 global $HTTP_POST_VARS;
219 $sql = 'UPDATE '.$table.' SET ';
220 while (list($var, $val) = each($HTTP_POST_VARS)) {
221 if ($var != $primary_key) {
222 $sql .= "$var='". htmlspecialchars($val) ."', ";
225 $sql = ereg_replace(', $', ' ', $sql);
226 $sql .= "WHERE $primary_key=$id";
228 if (db_query($sql)) {
229 printf(_('%1$s successfully modified.'), ucfirst(getUnitLabel($unit)));
236 * admin_table_show() - display the specified table, sorted by the primary key, with links to add, edit, and delete
238 * @param $table - the table to act on
239 * @param $unit - the name of the "units" described by the table's records
240 * @param $primary_key - the primary key of the table
242 function admin_table_show($table, $unit, $primary_key) {
245 $result = db_query_params("SELECT * FROM $table ORDER BY $primary_key", array());
248 $rows = db_numrows($result);
249 $cols = db_numfields($result);
252 $cell_data[]=array(ucwords(getUnitLabel($unit)).' <a href="'.getStringFromServer('PHP_SELF').'?function=add">['._('add new').']</a>',
253 'colspan="'.($cols+1).'"');
255 echo '<table border="0" width="100%">';
256 echo $HTML->multiTableRow('',$cell_data, TRUE);
259 <tr><td width="5%"></td>';
260 for ($i = 0; $i < $cols; $i++) {
261 echo '<td><strong>'.db_fieldname($result,$i).'</strong></td>';
265 for ($j = 0; $j < $rows; $j++) {
266 echo '<tr '. $HTML->boxGetAltRowStyle($j) . '>';
268 $id = db_result($result,$j,0);
269 echo '<td><a href="'.getStringFromServer('PHP_SELF').'?function=edit&id='.$id.'">['._('Edit').']</a>';
270 echo '<a href="'.getStringFromServer('PHP_SELF').'?function=confirmdelete&id='.$id.'">['._('Delete').']</a> </td>';
271 for ($i = 0; $i < $cols; $i++) {
272 echo '<td>'. db_result($result, $j, $i) .'</td>';
283 * getUnitLabel - returns the localized label of a unit
285 * @param string unit name
286 * @return name localized label
288 function getUnitLabel($unit) {
293 require_once('../env.inc.php');
294 require_once $gfwww.'include/pre.php';
295 session_require(array('group'=>'1','admin_flags'=>'A'));
297 $HTML->header(array('title'=>sprintf(_('Edit the %1$ss Table'), ucwords(getUnitLabel($unit)))));
299 echo '<h3>'.sprintf(_('Edit the %1$ss Table'), ucwords(getUnitLabel($unit))).'</h3>
300 <p>'.util_make_link ('/admin/',_('Site Admin Home')).'</p>
303 // $table, $unit and $primary_key are variables passed from the parent scripts
304 $id = getStringFromRequest('id');
306 switch (getStringFromRequest('function')) {
308 admin_table_add($table, $unit, $primary_key);
312 admin_table_postadd($table, $unit, $primary_key);
315 case 'confirmdelete' : {
316 admin_table_confirmdelete($table, $unit, $primary_key, $id);
320 admin_table_delete($table, $unit, $primary_key, $id);
324 admin_table_edit($table, $unit, $primary_key, $id);
328 admin_table_postedit($table, $unit, $primary_key, $id);
333 echo admin_table_show($table, $unit, $primary_key);
335 $HTML->footer(array());
339 // c-file-style: "bsd"