3 * Module to render generic HTML tables for Site Admin
5 * Copyright 1999-2001 (c) VA Linux Systems
6 * Copyright 2010 (c) Franck Villaume - Capgemini
7 * Copyright 2012,2014 Franck Villaume - TrivialDev
9 * This file is part of FusionForge. FusionForge is free software;
10 * you can redistribute it and/or modify it under the terms of the
11 * GNU General Public License as published by the Free Software
12 * Foundation; either version 2 of the Licence, or (at your option)
15 * FusionForge is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * admin_table_add() - present a form for adding a record to the specified table
28 * @param string $table the table to act on
29 * @param string $unit the name of the "units" described by the table's records
30 * @param string $primary_key the primary key of the table
32 function admin_table_add($table, $unit, $primary_key) {
34 // This query may return no rows, but the field names are needed.
35 $result = db_query_params("SELECT * FROM $table WHERE $primary_key=0", array());
39 $cols = db_numfields($result);
41 printf(_('Create a new %s below:'), getUnitLabel($unit));
43 echo $HTML->openForm(array('name' => 'add', 'action' => getStringFromServer('PHP_SELF').'?function=postadd', 'method' => 'post'));
45 <input type="hidden" name="form_key" value="'.form_generate_key().'" />
48 for ($i = 0; $i < $cols; $i++) {
49 $fieldname = db_fieldname($result, $i);
50 if ($fieldname != $primary_key) {
51 $fields[] = $fieldname;
52 echo '<tr><td><strong>'.$fieldname.'</strong></td>';
53 echo '<td><input type="text" name="'.$fieldname.'" value="" required="required" /></td></tr>';
56 echo '</table><input type="submit" value="'._('Add').'" />
57 <input type="hidden" name="__fields__" value="'.implode(',',$fields).'" />';
58 echo html_e('input', array('type' => 'button', 'name' => _('Cancel'), 'value' => _('Cancel'), 'onclick' => 'window.location=\''.util_make_uri(getStringFromServer('PHP_SELF')).'\''));
59 echo $HTML->closeForm();
66 * admin_table_postadd() - update the database based on a submitted change
68 * @param string $table the table to act on
69 * @param string $unit the name of the "units" described by the table's records
71 function admin_table_postadd($table, $unit) {
73 if (!form_key_is_valid(getStringFromRequest('form_key'))) {
74 exit_form_double_submit('home');
77 $field_list = getStringFromRequest('__fields__');
78 $fields = explode(",", $field_list);
79 $values = array(); $v = array ();
80 $qpa = db_construct_qpa(array(), '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 echo $HTML->feedback(sprintf(_('%s successfully added.'), ucfirst(getUnitLabel($unit))));
94 echo $HTML->error_msg(_('Failed.').' '.db_error());
95 form_release_key(getStringFromRequest('form_key'));
100 * admin_table_confirmdelete() - present a form to confirm requested record deletion
102 * @param string $table the table to act on
103 * @param string $unit the name of the "units" described by the table's records
104 * @param string $primary_key the primary key of the table
105 * @param string $id the id of the record to act on
107 function admin_table_confirmdelete($table, $unit, $primary_key, $id) {
109 if ($unit == "processor") {
110 $result = db_numrows(db_query_params ('SELECT processor_id FROM frs_file WHERE processor_id = $1',
113 echo $HTML->warning_msg(sprintf(_('You cannot delete the processor %s since it is currently referenced in a file release.'), db_result(db_query_params ('select name from frs_processor where processor_id = $1',
114 array($id)), 0, 0)));
118 if ($unit == "license") {
119 $result = db_numrows(db_query_params ('SELECT license FROM groups WHERE license = $1',
122 echo $HTML->warning_msg(sprintf(_('You can\'t delete the license %s since it is currently referenced in a project.'), db_result(db_query_params ('select license_name from licenses where license_id = $1',
123 array($id)), 0, 0)));
127 if ($unit == "supported_language") {
128 $result = db_numrows(db_query_params("SELECT language FROM users WHERE language=$1", array($id)));
130 echo $HTML->warning_msg(sprintf(_('You cannot delete the language %s since it is currently referenced in a user profile.'), db_result(db_query_params ('select language from users where language = $1',
131 array($id)), 0, 0)));
136 if ($unit == "theme") {
137 $result = db_numrows(db_query_params("SELECT theme_id FROM users WHERE theme_id=$1", array($id)));
139 echo $HTML->warning_msg(sprintf(_('You cannot delete the theme %s since it is currently referenced in a user profile.'), db_result(db_query_params ('select language from users where language = $1',
140 array($id)), 0, 0)));
145 $result = db_query_params("SELECT * FROM $table WHERE $primary_key=$1", array($id));
148 $cols = db_numfields($result);
150 printf(_('Are you sure you want to delete this %s?'), getUnitLabel($unit));
153 for ($i = 0; $i < $cols; $i++) {
154 echo '<li><strong>'.db_fieldname($result,$i).'</strong> '.db_result($result,0,$i).'</li>';
157 echo $HTML->openForm(array('name' => 'delete', 'action' => getStringFromServer('PHP_SELF').'?function=delete&id='.$id, 'method' => 'post'));
158 echo '<input type="submit" value="'._('Delete').'" />';
159 echo html_e('input', array('type' => 'button', 'name' => _('Cancel'), 'value' => _('Cancel'), 'onclick' => 'window.location=\''.util_make_uri(getStringFromServer('PHP_SELF')).'\''));
160 echo $HTML->closeForm();
162 echo $HTML->error_msg(db_error());
167 * admin_table_delete() - delete a record from the database after confirmation
169 * @param string $table the table to act on
170 * @param string $unit the name of the "units" described by the table's records
171 * @param string $primary_key the primary key of the table
172 * @param string $id the id of the record to act on
174 function admin_table_delete($table, $unit, $primary_key, $id) {
176 if (db_query_params("DELETE FROM $table WHERE $primary_key=$1", array($id))) {
177 echo $HTML->feedback(sprintf(_('%s successfully deleted.'), ucfirst(getUnitLabel($unit))));
179 echo $HTML->error_msg(db_error());
184 * admin_table_edit() - present a form for editing a record in the specified table
186 * @param string $table the table to act on
187 * @param string $unit the name of the "units" described by the table's records
188 * @param string $primary_key the primary key of the table
189 * @param string $id the id of the record to act on
191 function admin_table_edit($table, $unit, $primary_key, $id) {
193 $result = db_query_params("SELECT * FROM $table WHERE $primary_key=$1", array($id));
196 $cols = db_numfields($result);
198 printf(_('Modify the %s below:'), getUnitLabel($unit));
200 echo $HTML->openForm(array('name' => 'edit' ,'action' => getStringFromServer('PHP_SELF').'?function=postedit&id='.$id, 'method' => 'post'));
203 for ($i = 0; $i < $cols; $i++) {
204 $fieldname = db_fieldname($result, $i);
205 $value = db_result($result, 0, $i);
207 echo '<tr><td><strong>'.$fieldname.'</strong></td>';
209 if ($fieldname == $primary_key) {
210 echo "<td>$value</td></tr>";
212 echo '<td><input type="text" name="'.$fieldname.'" value="'.$value.'" /></td></tr>';
215 echo '</table><input type="submit" value="'._('Submit').'" />';
216 echo html_e('input', array('type' => 'button', 'name' => _('Cancel'), 'value' => _('Cancel'), 'onclick' => 'window.location=\''.util_make_uri(getStringFromServer('PHP_SELF')).'\''));
217 echo $HTML->closeForm();
219 echo $HTML->error_msg(db_error());
224 * admin_table_postedit() - update the database to reflect submitted modifications to a record
226 * @param string $table the table to act on
227 * @param string $unit the name of the "units" described by the table's records
228 * @param string $primary_key the primary key of the table
229 * @param string $id the id of the record to act on
231 function admin_table_postedit($table, $unit, $primary_key, $id) {
235 $qpa = db_construct_qpa(array(), 'UPDATE ' . $table . ' SET ') ;
238 while (list($var, $val) = each($_POST)) {
240 $qpa = db_construct_qpa($qpa, ', ') ;
243 if ($var != $primary_key) {
244 $qpa = db_construct_qpa($qpa, "$var=$1", array ($val)) ;
247 $qpa = db_construct_qpa($qpa, 'WHERE '.$primary_key.'=$1',
250 if (db_query_qpa($qpa)) {
251 echo $HTML->feedback(sprintf(_('%s successfully modified.'), ucfirst(getUnitLabel($unit))));
253 echo $HTML->error_msg(db_error());
256 $field_list = getStringFromRequest('__fields__');
257 if (strlen($field_list)) {
258 $fields = explode(",", $field_list);
259 $values = array(); $v = array ();
260 $qpa = db_construct_qpa(array(), 'INSERT INTO ' . $table . ' (' . $field_list . ') VALUES (') ;
263 foreach ($fields as $field) {
266 $values[] = getStringFromPost($field);
269 $qpa = db_construct_qpa($qpa, implode (',', $v).')', $values) ;
271 if (db_query_qpa($qpa)) {
272 echo $HTML->feedback(sprintf(_('%s successfully added.'), ucfirst(getUnitLabel($unit))));
274 form_release_key(getStringFromRequest('form_key'));
275 echo $HTML->error_msg(db_error());
281 * admin_table_show() - display the specified table, sorted by the primary key, with links to add, edit, and delete
283 * @param string $table the table to act on
284 * @param string $unit the name of the "units" described by the table's records
285 * @param string $primary_key the primary key of the table
287 function admin_table_show($table, $unit, $primary_key) {
290 $result = db_query_params("SELECT * FROM $table ORDER BY $primary_key", array());
293 $rows = db_numrows($result);
294 $cols = db_numfields($result);
297 $cell_data[] = array(ucwords(getUnitLabel($unit)).' '.util_make_link(getStringFromServer('PHP_SELF').'?function=add', '['._('add new').']'),
298 'colspan' => ($cols+1));
299 echo '<table class="listing full">';
300 echo $HTML->multiTableRow(array(), $cell_data, TRUE);
304 for ($i = 0; $i < $cols; $i++) {
305 echo '<td><strong>'.db_fieldname($result,$i).'</strong></td>';
309 for ($j = 0; $j < $rows; $j++) {
311 $id = db_result($result,$j,0);
312 echo '<td>'.util_make_link(getStringFromServer('PHP_SELF').'?function=edit&id='.$id, '['._('Edit').']');
313 echo util_make_link(getStringFromServer('PHP_SELF').'?function=confirmdelete&id='.$id, '['._('Delete').']').'</td>';
314 for ($i = 0; $i < $cols; $i++) {
315 echo '<td>'. db_result($result, $j, $i) .'</td>';
321 echo $HTML->error_msg(db_error());
326 * getUnitLabel - returns the localized label of a unit
328 * @param string $unit unit name
329 * @return string name localized label
331 function getUnitLabel($unit) {
335 require_once '../env.inc.php';
336 require_once $gfcommon.'include/pre.php';
340 session_require_global_perm('forge_admin');
342 $HTML->header(array('title'=>sprintf(_('Edit the %ss Table'), ucwords(getUnitLabel($unit)))));
344 // $table, $unit and $primary_key are variables passed from the parent scripts
345 $id = getStringFromRequest('id');
347 switch (getStringFromRequest('function')) {
349 admin_table_add($table, $unit, $primary_key);
353 admin_table_postadd($table, $unit);
356 case 'confirmdelete' : {
357 admin_table_confirmdelete($table, $unit, $primary_key, $id);
361 admin_table_delete($table, $unit, $primary_key, $id);
365 admin_table_edit($table, $unit, $primary_key, $id);
369 admin_table_postedit($table, $unit, $primary_key, $id);
374 admin_table_show($table, $unit, $primary_key);
380 // c-file-style: "bsd"