root/form/manage_field.php

Revision 277, 6.5 kB (checked in by kevin, 9 months ago)

First commit of form builder plugin

Line 
1 <?php
2
3 /*
4  * Elgg Forms
5  * Kevin Jardine
6  * Radagast Solutions
7  * http://radagast.biz
8  *
9  * The main function for managing fields.
10  *
11  */
12  
13 // Load Elgg framework
14 @require_once("../../includes.php");
15     
16 // Define context
17 define("context","form");
18
19 $redirect_url = '';
20
21 if (isloggedin() && run("users:flags:get", array("admin", $_SESSION['userid']))) {
22         
23     $action = optional_param('action','');
24     
25     // possible actions: add, change, edit, delete, move, add_existing, copy_existing, add_new
26     
27     switch($action) {
28         case "add":
29             $form_id = optional_param('form_id',0,PARAM_INT);
30             form_set_field_definition();
31             $messages[] = __gettext('This new field has been added to this form.');
32             $redirect_url = $CFG->wwwroot.'mod/form/manage_form.php?id='.$form_id;
33             break;
34         case "change":
35             $form_id = optional_param('form_id',0,PARAM_INT);
36             $type = optional_param('type','');
37             form_set_field_definition();
38             $messages[] = __gettext('Your changes to this field have been saved.');
39             if ($type == 'all' || $type == 'orphan') {
40                 $redirect_url = $CFG->wwwroot.'mod/form/list_fields.php?type='.$type;
41             } else {               
42                 $redirect_url = $CFG->wwwroot.'mod/form/manage_form.php?id='.$form_id;
43             }
44             break;
45         case "edit":
46             $map_id = optional_param('id',0,PARAM_INT);
47             $map = get_record('form_fields_map','ident',$map_id);
48             $field = form_get_field_definition($map->field_id);
49             $title = __gettext("Edit field");
50             $body = form_get_field_form($map->form_id,$field);
51             $redirect_url = '';
52             break;
53         case "edit_with_field_id":
54             $field_id = optional_param('id',0,PARAM_INT);
55             $title = __gettext("Edit field");
56             $field = form_get_field_definition($field_id);
57             $body = form_get_field_form(0,$field);
58             $redirect_url = '';
59             break;
60         case "remove":
61             $map_id = optional_param('id',0,PARAM_INT);
62             $map = get_record('form_fields_map','ident',$map_id);
63             delete_records('form_fields_map','ident',$map_id);
64             $messages[] = __gettext('This field has been removed from this form.');
65             $redirect_url = $CFG->wwwroot.'mod/form/manage_form.php?id='.$map->form_id;
66             break;
67         case "delete":
68             // unlike remove, delete actually deletes the form definition itself
69             // and the references for all forms
70             $field_id = optional_param('id',0,PARAM_INT);
71             delete_records('form_fields_map','field_id',$field_id);
72             delete_records('form_fields','ident',$field_id);
73             $type = optional_param('type','');
74             if ($type == 'orphan') {
75                 $messages[] = __gettext('This field definition has been deleted.');
76                 $redirect_url = $CFG->wwwroot.'mod/form/list_fields.php?type=orphan';
77             } else {
78                 $messages[] = __gettext('This field definition has been deleted and has been removed from all forms.');
79                 $redirect_url = $CFG->wwwroot.'mod/form/list_fields.php?type=all';
80             }
81                 
82             break;
83         case "move":
84             $map_id = optional_param('id',0,PARAM_INT);
85             $direction = optional_param('direction','');
86             $map = get_record('form_fields_map','ident',$map_id);
87             if ($direction == 'up') {
88                 form_field_moveup($map);
89             } else if ($direction == 'down') {
90                 form_field_movedown($map);
91             }
92             $messages[] = __gettext('This field has been moved.');
93             $redirect_url = $CFG->wwwroot.'mod/form/manage_form.php?id='.$map->form_id;
94             break;
95         case "add_existing":
96             $existing_field_name = optional_param('existing_field_name','');
97             $form_id = optional_param('form_id',0,PARAM_INT);
98             $field_id = get_field('form_fields','ident','internal_name',$existing_field_name);
99             $map = new stdClass;
100             $map->form_id = $form_id;
101             $map->field_id = $field_id;
102             $map->display_order = 100000;
103             $map->ident = insert_record('form_fields_map',$map);
104             form_reorder($form_id);
105             $messages[] = sprintf(__gettext('Field "%s" has been added.'), $existing_field_name);
106             $redirect_url = $CFG->wwwroot.'mod/form/manage_form.php?id='.$map->form_id;
107             break;
108         case "copy_existing":
109             $existing_field_name = optional_param('existing_field_name','');
110             $new_field_name = optional_param('new_field_name','');
111             $form_id = optional_param('form_id',0,PARAM_INT);
112             $field_id = get_field('form_fields','ident','internal_name',$existing_field_name);
113             $field = form_get_field_definition($field_id);
114             $field->internal_name = $new_field_name;
115             $field->ident = 0;
116             $messages[] = __gettext('Review the new field definition below and click on the "Create field" button'
117                 .' at the bottom to save it.');
118             $title = __gettext("Create field");
119             $body = form_get_field_form($form_id,$field,$form_name = '');
120             $redirect_url = '';
121             break;
122         case "add_new":
123             $new_field_name = optional_param('new_field_name','');
124             $form_id = optional_param('form_id',0,PARAM_INT);
125             $field_id = get_field('form_fields','ident','internal_name',$new_field_name);
126             $title = __gettext("Create field");
127             $body = form_get_field_form($form_id,null,$new_field_name);
128             $redirect_url = '';
129             break;
130     }
131 } else {
132     $body = __gettext("You must be logged in as a site administrator to view this page.");
133     $title = __gettext("Error");
134 }
135     
136 if ($redirect_url) {
137     // all done, so redirect to form page
138     $_SESSION['messages'] = $messages;
139     header("Location: $redirect_url");
140 } else {
141
142     // Output to the screen
143     $body = templates_draw(array(
144                     'context' => 'contentholder',
145                     'title' => $title,
146                     'body' => $body
147                 )
148                 );
149     
150     echo templates_page_draw( array(
151                     $title, $body
152                 )
153                 );
154 }
155 ?>
Note: See TracBrowser for help on using the browser.