3 * Module to render generic HTML tables for Site Admin
5 * Copyright 1999-2001 (c) VA Linux Systems
9 * This file is part of GForge.
11 * GForge is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * GForge is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with GForge; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 * admin_table_add() - present a form for adding a record to the specified table
30 * @param $table - the table to act on
31 * @param $unit - the name of the "units" described by the table's records
32 * @param $primary_key - the primary key of the table
34 function admin_table_add($table, $unit, $primary_key) {
35 // This query may return no rows, but the field names are needed.
36 $result = db_query('SELECT * FROM '.$table.' WHERE '.$primary_key.'=0');
40 $cols = db_numfields($result);
42 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("SELECT processor_id FROM frs_file WHERE processor_id = $id"));
106 echo '<p>'.sprintf(_('You can\'t delete the processor %1$s since it\'s currently referenced in a file release.'), db_result(db_query("select name from frs_processor where processor_id = $id"), 0, 0)).'</p>';
110 if ($unit == "license") {
111 $result = db_numrows(db_query("SELECT license FROM groups WHERE license = $id"));
113 echo '<p>'.sprintf(_('You can\'t delete the license %1$s since it\'s currently referenced in a project.'), db_result(db_query("select license_name from licenses where license_id = $id"), 0, 0)).'</p>';
117 if ($unit == "supported_language") {
118 $result = db_numrows(db_query('SELECT language FROM users WHERE language='.$id));
120 echo '<p>'.sprintf(_('You can\'t delete the language %1$s since it\'s currently referenced in a user profile.'), db_result(db_query("select license_name from licenses where license_id = $id"), 0, 0)).'</p>';
125 $result = db_query("SELECT * FROM $table WHERE $primary_key=$id");
128 $cols = db_numfields($result);
130 printf(_('Are you sure you want to delete this %1$s?'), getUnitLabel($unit)).'<ul>';
131 for ($i = 0; $i < $cols; $i++) {
132 echo '<li><strong>'.db_fieldname($result,$i).'</strong> '.db_result($result,0,$i).'</li>';
135 <form name="delete" action="'.getStringFromServer('PHP_SELF').'?function=delete&id='.$id.'" method="post">
136 <input type="submit" value="'._('Delete').'" />
138 <form name="cancel" action="'.getStringFromServer('PHP_SELF').'" method="post">
139 <input type="submit" value="'._('Cancel').'" />
147 * admin_table_delete() - delete a record from the database after confirmation
149 * @param $table - the table to act on
150 * @param $unit - the name of the "units" described by the table's records
151 * @param $primary_key - the primary key of the table
152 * @param $id - the id of the record to act on
154 function admin_table_delete($table, $unit, $primary_key, $id) {
155 $sql = "DELETE FROM $table WHERE $primary_key=$id";
156 if (db_query($sql)) {
157 printf(_('%1$s successfully deleted.'), ucfirst(getUnitLabel($unit)));
164 * admin_table_edit() - present a form for editing a record in the specified table
166 * @param $table - the table to act on
167 * @param $unit - the name of the "units" described by the table's records
168 * @param $primary_key - the primary key of the table
169 * @param $id - the id of the record to act on
171 function admin_table_edit($table, $unit, $primary_key, $id) {
172 $result = db_query("SELECT * FROM $table WHERE $primary_key=$id");
175 $cols = db_numfields($result);
177 printf(_('Modify the %1$s below:'), getUnitLabel($unit)).'
178 <form name="edit" action="'.getStringFromServer('PHP_SELF').'?function=postedit&id='.$id.'" method="post">
181 for ($i = 0; $i < $cols; $i++) {
182 $fieldname = db_fieldname($result, $i);
183 $value = db_result($result, 0, $i);
185 echo '<tr><td><strong>'.$fieldname.'</strong></td>';
187 if ($fieldname == $primary_key) {
188 echo "<td>$value</td></tr>";
190 echo '<td><input type="text" name="'.$fieldname.'" value="'.$value.'" /></td></tr>';
193 echo '</table><input type="submit" value="'._('Submit').'" /></form>
194 <form name="cancel" action="'.getStringFromServer('PHP_SELF').'" method="post">
195 <input type="submit" value="'._('Cancel').'" />
203 * admin_table_postedit() - update the database to reflect submitted modifications to a record
205 * @param $table - the table to act on
206 * @param $unit - the name of the "units" described by the table's records
207 * @param $primary_key - the primary key of the table
208 * @param $id - the id of the record to act on
210 function admin_table_postedit($table, $unit, $primary_key, $id) {
211 global $HTTP_POST_VARS;
213 $sql = 'UPDATE '.$table.' SET ';
214 while (list($var, $val) = each($HTTP_POST_VARS)) {
215 if ($var != $primary_key) {
216 $sql .= "$var='". htmlspecialchars($val) ."', ";
219 $sql = ereg_replace(', $', ' ', $sql);
220 $sql .= "WHERE $primary_key=$id";
222 if (db_query($sql)) {
223 printf(_('%1$s successfully modified.'), ucfirst(getUnitLabel($unit)));
230 * admin_table_show() - display the specified table, sorted by the primary key, with links to add, edit, and delete
232 * @param $table - the table to act on
233 * @param $unit - the name of the "units" described by the table's records
234 * @param $primary_key - the primary key of the table
236 function admin_table_show($table, $unit, $primary_key) {
239 $result = db_query("SELECT * FROM $table ORDER BY $primary_key");
242 $rows = db_numrows($result);
243 $cols = db_numfields($result);
246 $cell_data[]=array(ucwords(getUnitLabel($unit)).' <a href="'.getStringFromServer('PHP_SELF').'?function=add">['._('add new').']</a>',
247 'colspan="'.($cols+1).'"');
249 echo '<table border="0" width="100%">';
250 echo $HTML->multiTableRow('',$cell_data, TRUE);
253 <tr><td width="5%"></td>';
254 for ($i = 0; $i < $cols; $i++) {
255 echo '<td><strong>'.db_fieldname($result,$i).'</strong></td>';
259 for ($j = 0; $j < $rows; $j++) {
260 echo '<tr '. $HTML->boxGetAltRowStyle($j) . '>';
262 $id = db_result($result,$j,0);
263 echo '<td><a href="'.getStringFromServer('PHP_SELF').'?function=edit&id='.$id.'">['._('Edit').']</a>';
264 echo '<a href="'.getStringFromServer('PHP_SELF').'?function=confirmdelete&id='.$id.'">['._('Delete').']</a> </td>';
265 for ($i = 0; $i < $cols; $i++) {
266 echo '<td>'. db_result($result, $j, $i) .'</td>';
277 * getUnitLabel - returns the localized label of a unit
279 * @param string unit name
280 * @return name localized label
282 function getUnitLabel($unit) {
284 $label = $Language->getText('admin_admin_table', 'unit_'.$unit);
289 require_once('../env.inc.php');
290 require_once('pre.php');
291 session_require(array('group'=>'1','admin_flags'=>'A'));
293 $HTML->header(array('title'=>sprintf(_('Edit the %1$ss Table'), ucwords(getUnitLabel($unit)))));
295 echo '<h3>'.sprintf(_('Edit the %1$ss Table'), ucwords(getUnitLabel($unit))).'</h3>
296 <p><a href="'.$GLOBALS['sys_urlprefix'].'/admin/">'._('Site Admin Home').'</a></p>
299 // $table, $unit and $primary_key are variables passed from the parent scripts
300 $id = getStringFromRequest('id');
302 switch (getStringFromRequest('function')) {
304 admin_table_add($table, $unit, $primary_key);
308 admin_table_postadd($table, $unit, $primary_key);
311 case 'confirmdelete' : {
312 admin_table_confirmdelete($table, $unit, $primary_key, $id);
316 admin_table_delete($table, $unit, $primary_key, $id);
320 admin_table_edit($table, $unit, $primary_key, $id);
324 admin_table_postedit($table, $unit, $primary_key, $id);
329 echo admin_table_show($table, $unit, $primary_key);
331 $HTML->footer(array());
335 // c-file-style: "bsd"