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);
79 $values = array(); $v = array ();
80 $qpa = db_construct_qpa (false, 'INSERT INTO ' . $table . ' (' . $field_list . ') VALUES (') ;
83 foreach ($fields as $field) {
86 $values[] = getStringFromPost($field);
89 $qpa = db_construct_qpa ($qpa, implode (',', $v), $values) ;
91 if (db_query_qpa($qpa)) {
92 printf(_('%1$s successfully added.'), ucfirst(getUnitLabel($unit)));
94 form_release_key(getStringFromRequest('form_key'));
100 * admin_table_confirmdelete() - present a form to confirm requested record deletion
102 * @param $table - the table to act on
103 * @param $unit - the name of the "units" described by the table's records
104 * @param $primary_key - the primary key of the table
105 * @param $id - the id of the record to act on
107 function admin_table_confirmdelete($table, $unit, $primary_key, $id) {
108 if ($unit == "processor") {
109 $result = db_numrows(db_query_params ('SELECT processor_id FROM frs_file WHERE processor_id = $1',
112 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',
113 array($id)), 0, 0)).'</p>';
117 if ($unit == "license") {
118 $result = db_numrows(db_query_params ('SELECT license FROM groups WHERE license = $1',
121 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',
122 array($id)), 0, 0)).'</p>';
126 if ($unit == "supported_language") {
127 $result = db_numrows(db_query_params("SELECT language FROM users WHERE language=$1", array($id)));
129 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',
130 array($id)), 0, 0)).'</p>';
135 $result = db_query_params("SELECT * FROM $table WHERE $primary_key=$1", array($id));
138 $cols = db_numfields($result);
140 printf(_('Are you sure you want to delete this %1$s?'), getUnitLabel($unit)).'<ul>';
141 for ($i = 0; $i < $cols; $i++) {
142 echo '<li><strong>'.db_fieldname($result,$i).'</strong> '.db_result($result,0,$i).'</li>';
145 <form name="delete" action="'.getStringFromServer('PHP_SELF').'?function=delete&id='.$id.'" method="post">
146 <input type="submit" value="'._('Delete').'" />
148 <form name="cancel" action="'.getStringFromServer('PHP_SELF').'" method="post">
149 <input type="submit" value="'._('Cancel').'" />
157 * admin_table_delete() - delete a record from the database after confirmation
159 * @param $table - the table to act on
160 * @param $unit - the name of the "units" described by the table's records
161 * @param $primary_key - the primary key of the table
162 * @param $id - the id of the record to act on
164 function admin_table_delete($table, $unit, $primary_key, $id) {
165 if (db_query_params("DELETE FROM $table WHERE $primary_key=$1", array($id))) {
166 printf(_('%1$s successfully deleted.'), ucfirst(getUnitLabel($unit)));
173 * admin_table_edit() - present a form for editing a record in the specified table
175 * @param $table - the table to act on
176 * @param $unit - the name of the "units" described by the table's records
177 * @param $primary_key - the primary key of the table
178 * @param $id - the id of the record to act on
180 function admin_table_edit($table, $unit, $primary_key, $id) {
181 $result = db_query_params("SELECT * FROM $table WHERE $primary_key=$1", array($id));
184 $cols = db_numfields($result);
186 printf(_('Modify the %1$s below:'), getUnitLabel($unit));
189 <form name="edit" action="'.getStringFromServer('PHP_SELF').'?function=postedit&id='.$id.'" method="post">
192 for ($i = 0; $i < $cols; $i++) {
193 $fieldname = db_fieldname($result, $i);
194 $value = db_result($result, 0, $i);
196 echo '<tr><td><strong>'.$fieldname.'</strong></td>';
198 if ($fieldname == $primary_key) {
199 echo "<td>$value</td></tr>";
201 echo '<td><input type="text" name="'.$fieldname.'" value="'.$value.'" /></td></tr>';
204 echo '</table><input type="submit" value="'._('Submit').'" /></form>
205 <form name="cancel" action="'.getStringFromServer('PHP_SELF').'" method="post">
206 <input type="submit" value="'._('Cancel').'" />
214 * admin_table_postedit() - update the database to reflect submitted modifications to a record
216 * @param $table - the table to act on
217 * @param $unit - the name of the "units" described by the table's records
218 * @param $primary_key - the primary key of the table
219 * @param $id - the id of the record to act on
221 function admin_table_postedit($table, $unit, $primary_key, $id) {
224 $qpa = db_construct_qpa (false, 'UPDATE ' . $table . ' SET ') ;
227 while (list($var, $val) = each($_POST)) {
229 $qpa = db_construct_qpa ($qpa, ', ') ;
232 if ($var != $primary_key) {
233 $qpa = db_construct_qpa ($qpa, "$var=$1", array ($val)) ;
236 $qpa = db_construct_qpa ($qpa, 'WHERE '.$primary_key.'=$1',
239 if (db_query_qpa($qpa)) {
240 printf(_('%1$s successfully modified.'), ucfirst(getUnitLabel($unit)));
249 $field_list = getStringFromRequest('__fields__');
250 $fields = split(",", $field_list);
251 $values = array(); $v = array ();
252 $qpa = db_construct_qpa (false, 'INSERT INTO ' . $table . ' (' . $field_list . ') VALUES (') ;
255 foreach ($fields as $field) {
258 $values[] = getStringFromPost($field);
261 $qpa = db_construct_qpa ($qpa, implode (',', $v), $values) ;
263 if (db_query_qpa($qpa)) {
264 printf(_('%1$s successfully added.'), ucfirst(getUnitLabel($unit)));
266 form_release_key(getStringFromRequest('form_key'));
273 * admin_table_show() - display the specified table, sorted by the primary key, with links to add, edit, and delete
275 * @param $table - the table to act on
276 * @param $unit - the name of the "units" described by the table's records
277 * @param $primary_key - the primary key of the table
279 function admin_table_show($table, $unit, $primary_key) {
282 $result = db_query_params("SELECT * FROM $table ORDER BY $primary_key", array());
285 $rows = db_numrows($result);
286 $cols = db_numfields($result);
289 $cell_data[]=array(ucwords(getUnitLabel($unit)).' <a href="'.getStringFromServer('PHP_SELF').'?function=add">['._('add new').']</a>',
290 'colspan="'.($cols+1).'"');
292 echo '<table border="0" width="100%">';
293 echo $HTML->multiTableRow('',$cell_data, TRUE);
296 <tr><td width="5%"></td>';
297 for ($i = 0; $i < $cols; $i++) {
298 echo '<td><strong>'.db_fieldname($result,$i).'</strong></td>';
302 for ($j = 0; $j < $rows; $j++) {
303 echo '<tr '. $HTML->boxGetAltRowStyle($j) . '>';
305 $id = db_result($result,$j,0);
306 echo '<td><a href="'.getStringFromServer('PHP_SELF').'?function=edit&id='.$id.'">['._('Edit').']</a>';
307 echo '<a href="'.getStringFromServer('PHP_SELF').'?function=confirmdelete&id='.$id.'">['._('Delete').']</a> </td>';
308 for ($i = 0; $i < $cols; $i++) {
309 echo '<td>'. db_result($result, $j, $i) .'</td>';
320 * getUnitLabel - returns the localized label of a unit
322 * @param string unit name
323 * @return name localized label
325 function getUnitLabel($unit) {
330 require_once('../env.inc.php');
331 require_once $gfwww.'include/pre.php';
332 session_require(array('group'=>'1','admin_flags'=>'A'));
334 $HTML->header(array('title'=>sprintf(_('Edit the %1$ss Table'), ucwords(getUnitLabel($unit)))));
336 echo '<h3>'.sprintf(_('Edit the %1$ss Table'), ucwords(getUnitLabel($unit))).'</h3>
337 <p>'.util_make_link ('/admin/',_('Site Admin Home')).'</p>
340 // $table, $unit and $primary_key are variables passed from the parent scripts
341 $id = getStringFromRequest('id');
343 switch (getStringFromRequest('function')) {
345 admin_table_add($table, $unit, $primary_key);
349 admin_table_postadd($table, $unit, $primary_key);
352 case 'confirmdelete' : {
353 admin_table_confirmdelete($table, $unit, $primary_key, $id);
357 admin_table_delete($table, $unit, $primary_key, $id);
361 admin_table_edit($table, $unit, $primary_key, $id);
365 admin_table_postedit($table, $unit, $primary_key, $id);
370 echo admin_table_show($table, $unit, $primary_key);
372 $HTML->footer(array());
376 // c-file-style: "bsd"