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 print('<p class="feedback">');
167 printf(_('%1$s successfully deleted.'), ucfirst(getUnitLabel($unit)));
175 * admin_table_edit() - present a form for editing a record in the specified table
177 * @param $table - the table to act on
178 * @param $unit - the name of the "units" described by the table's records
179 * @param $primary_key - the primary key of the table
180 * @param $id - the id of the record to act on
182 function admin_table_edit($table, $unit, $primary_key, $id) {
183 $result = db_query_params("SELECT * FROM $table WHERE $primary_key=$1", array($id));
186 $cols = db_numfields($result);
188 printf(_('Modify the %1$s below:'), getUnitLabel($unit));
191 <form name="edit" action="'.getStringFromServer('PHP_SELF').'?function=postedit&id='.$id.'" method="post">
194 for ($i = 0; $i < $cols; $i++) {
195 $fieldname = db_fieldname($result, $i);
196 $value = db_result($result, 0, $i);
198 echo '<tr><td><strong>'.$fieldname.'</strong></td>';
200 if ($fieldname == $primary_key) {
201 echo "<td>$value</td></tr>";
203 echo '<td><input type="text" name="'.$fieldname.'" value="'.$value.'" /></td></tr>';
206 echo '</table><input type="submit" value="'._('Submit').'" /></form>
207 <form name="cancel" action="'.getStringFromServer('PHP_SELF').'" method="post">
208 <input type="submit" value="'._('Cancel').'" />
216 * admin_table_postedit() - update the database to reflect submitted modifications to a record
218 * @param $table - the table to act on
219 * @param $unit - the name of the "units" described by the table's records
220 * @param $primary_key - the primary key of the table
221 * @param $id - the id of the record to act on
223 function admin_table_postedit($table, $unit, $primary_key, $id) {
226 $qpa = db_construct_qpa (false, 'UPDATE ' . $table . ' SET ') ;
229 while (list($var, $val) = each($_POST)) {
231 $qpa = db_construct_qpa ($qpa, ', ') ;
234 if ($var != $primary_key) {
235 $qpa = db_construct_qpa ($qpa, "$var=$1", array ($val)) ;
238 $qpa = db_construct_qpa ($qpa, 'WHERE '.$primary_key.'=$1',
241 if (db_query_qpa($qpa)) {
242 printf(_('%1$s successfully modified.'), ucfirst(getUnitLabel($unit)));
251 $field_list = getStringFromRequest('__fields__');
252 $fields = split(",", $field_list);
253 $values = array(); $v = array ();
254 $qpa = db_construct_qpa (false, 'INSERT INTO ' . $table . ' (' . $field_list . ') VALUES (') ;
257 foreach ($fields as $field) {
260 $values[] = getStringFromPost($field);
263 $qpa = db_construct_qpa ($qpa, implode (',', $v), $values) ;
265 if (db_query_qpa($qpa)) {
266 printf(_('%1$s successfully added.'), ucfirst(getUnitLabel($unit)));
268 form_release_key(getStringFromRequest('form_key'));
275 * admin_table_show() - display the specified table, sorted by the primary key, with links to add, edit, and delete
277 * @param $table - the table to act on
278 * @param $unit - the name of the "units" described by the table's records
279 * @param $primary_key - the primary key of the table
281 function admin_table_show($table, $unit, $primary_key) {
284 $result = db_query_params("SELECT * FROM $table ORDER BY $primary_key", array());
287 $rows = db_numrows($result);
288 $cols = db_numfields($result);
291 $cell_data[]=array(ucwords(getUnitLabel($unit)).' <a href="'.getStringFromServer('PHP_SELF').'?function=add">['._('add new').']</a>',
292 'colspan="'.($cols+1).'"');
294 echo '<table border="0" width="100%">';
295 echo $HTML->multiTableRow('',$cell_data, TRUE);
298 <tr><td width="5%"></td>';
299 for ($i = 0; $i < $cols; $i++) {
300 echo '<td><strong>'.db_fieldname($result,$i).'</strong></td>';
304 for ($j = 0; $j < $rows; $j++) {
305 echo '<tr '. $HTML->boxGetAltRowStyle($j) . '>';
307 $id = db_result($result,$j,0);
308 echo '<td><a href="'.getStringFromServer('PHP_SELF').'?function=edit&id='.$id.'">['._('Edit').']</a>';
309 echo '<a href="'.getStringFromServer('PHP_SELF').'?function=confirmdelete&id='.$id.'">['._('Delete').']</a> </td>';
310 for ($i = 0; $i < $cols; $i++) {
311 echo '<td>'. db_result($result, $j, $i) .'</td>';
322 * getUnitLabel - returns the localized label of a unit
324 * @param string unit name
325 * @return name localized label
327 function getUnitLabel($unit) {
332 require_once('../env.inc.php');
333 require_once $gfwww.'include/pre.php';
334 session_require(array('group'=>'1','admin_flags'=>'A'));
336 $HTML->header(array('title'=>sprintf(_('Edit the %1$ss Table'), ucwords(getUnitLabel($unit)))));
338 echo '<h1>'.sprintf(_('Edit the %1$ss Table'), ucwords(getUnitLabel($unit))).'</h1>
339 <p>'.util_make_link ('/admin/',_('Site Admin Home')).'</p>';
341 // $table, $unit and $primary_key are variables passed from the parent scripts
342 $id = getStringFromRequest('id');
344 switch (getStringFromRequest('function')) {
346 admin_table_add($table, $unit, $primary_key);
350 admin_table_postadd($table, $unit, $primary_key);
353 case 'confirmdelete' : {
354 admin_table_confirmdelete($table, $unit, $primary_key, $id);
358 admin_table_delete($table, $unit, $primary_key, $id);
362 admin_table_edit($table, $unit, $primary_key, $id);
366 admin_table_postedit($table, $unit, $primary_key, $id);
371 echo admin_table_show($table, $unit, $primary_key);
373 $HTML->footer(array());
377 // c-file-style: "bsd"