php8
[web.mtrack] / admin / customfield.php
1 <?php # vim:ts=2:sw=2:et:
2 /* For licensing and copyright terms, see the file named LICENSE */
3 include '../../inc/common.php';
4
5 MTrackACL::requireAnyRights('Enumerations', 'modify');
6
7 $C = MTrackTicket_CustomFields::getInstance();
8
9 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
10
11   $name = $_POST['name'];
12   $type = $_POST['type'];
13   $group = $_POST['group'];
14   $label = $_POST['label'];
15   $options = $_POST['options'];
16   $default = $_POST['default'];
17   $order = (int)$_POST['order'];
18
19   if (!isset($C->field_types[$type])) {
20     throw new Exception("invalid type $type");
21   }
22
23   $name = MTrackTicket_CustomField::canonName($name);
24   if (!preg_match("/^x_[a-z_]+$/", $name)) {
25     throw new Exception("invalid field name $name");
26   }
27
28   $field = $C->fieldByName($name, true);
29
30   if (isset($_POST['delete'])) {
31     $C->deleteField($field);
32   } else {
33     $field->group = $group;
34     $field->label = $label;
35     $field->type = $type;
36     $field->order = $order;
37     $field->options = $options;
38     $field->default = $default;
39   }
40
41   $C->save();
42   MTrackConfig::save();
43
44   header("Location: ${ABSWEB}admin/customfield.php");
45   exit;
46 }
47
48 mtrack_head("Administration - Custom Fields");
49 echo "<h1>Custom Fields</h1>";
50
51
52 $field = null;
53 if (isset($_GET['add'])) {
54   $field = new MTrackTicket_CustomField;
55   $field->type = 'text';
56   $field->name = 'x_fieldname';
57   $field->label = 'The Label';
58   $field->group = 'Custom Fields';
59 } else if (isset($_GET['field'])) {
60   $field = $C->fieldByName($_GET['field']);
61   if ($field === null) {
62     throw new Exception("No such field " . $_GET['field']);
63   }
64 }
65
66 if ($field) {
67   $type = mtrack_select_box('type', $C->field_types, $field->type);
68   $name = htmlentities($field->name, ENT_QUOTES, 'utf-8');
69   $label = htmlentities($field->label, ENT_QUOTES, 'utf-8');
70   $group = htmlentities($field->group, ENT_QUOTES, 'utf-8');
71   $options = htmlentities($field->options, ENT_QUOTES, 'utf-8');
72   $default = htmlentities($field->default, ENT_QUOTES, 'utf-8');
73   $order = $field->order;
74 ?>
75 <form method='post' id='editfield'>
76   <fieldset>
77   <legend>Edit Custom Field</legend>
78   <table>
79     <tr>
80       <td><label for='name'>Name</label></td>
81       <td><input type='text' name='name' value='<?php echo $name ?>'><br>
82         <em>The field name to use in the database.  Must have a prefix
83           of 'x_' and must only contain characters a-z or underscore.
84           You cannot rename a field; once it is created, it stays
85           in the database.  You can use the label field below if you
86           want to change the presentation.</em></td>
87     </tr>
88     <tr>
89       <td><label for='type'>Type</label></td>
90       <td><?php echo $type ?></td>
91     </tr>
92     <tr>
93       <td><label for='label'>Label</label></td>
94       <td><input type='text' name='label' value='<?php echo $label ?>'><br>
95         <em>The label to display on the ticket screen</em></td>
96     </tr>
97     <tr>
98       <td><label for='group'>Group</label></td>
99       <td><input type='text' name='group' value='<?php echo $group ?>'><br>
100         <em>Fields with the same group are grouped together on the ticket
101           editing screen</em></td>
102     </tr>
103     <tr>
104       <td><label for='default'>Default</label></td>
105       <td><input type='text' name='default' value='<?php echo $default ?>'><br>
106         <em>Enter the default value for this field</em></td>
107     </tr>
108
109     <tr>
110       <td><label for='options'>Options</label></td>
111       <td><input type='text' name='options' value='<?php echo $options ?>'><br>
112         <em>For Select and Multi-Select types, enter a list of possible
113           choices here, separated by a pipe character |</em></td>
114     </tr>
115     <tr>
116       <td><label for='order'>Sort Order</label></td>
117       <td>
118         <input type='text' name='order' value='<?php echo $order ?>'><br>
119         <em>Lower means show first.  If two or more fields have same 'order',
120           then they are ordered by name</em>
121       </td>
122     </tr>
123   </table>
124   <button type='submit'>Save</button>
125   <button type='submit' name='cancel'>Cancel</button>
126   <button type='submit' name='delete' id='delete-field'>Delete</button>
127   </fieldset>
128 </form>
129 <div id="confirmDeleteDialog" style="display:none"
130     title="Are you sure?">
131   <p>
132     Deleting the field will hide it from the user interface; it will
133     not remove it from the database.
134   </p>
135   <p>
136     If you add the field back later on, the data previously entered
137     will be visible again.
138   </p>
139 </div>
140
141 <script>
142 $(document).ready(function () {
143 var delete_button = $('#delete-field');
144 var edit_form = $('#editfield');
145
146 $('#confirmDeleteDialog').dialog({
147   autoOpen: false,
148   bgiframe: true,
149   resizable: false,
150   modal: true,
151   buttons: {
152     'Delete': function() {
153       $(this).dialog('close');
154       delete_ok = true;
155       edit_form.append('<input type="hidden" name="delete" value="delete">');
156       delete_button.remove();
157       edit_form.submit();
158     },
159     'Keep': function() {
160       $(this).dialog('close');
161     }
162   }
163 });
164 $('#delete-field').click(
165   function() {
166     $('#confirmDeleteDialog').dialog('open');
167     return false;
168   }
169 );
170 });
171 </script>
172 <?php
173 } else {
174
175 $grouped = $C->getGroupedFields();
176
177 foreach ($grouped as $groupname => $group) {
178   $groupname = htmlentities($groupname, ENT_QUOTES, 'utf-8');
179   echo "<b>Group: $groupname</b><br>\n<table>\n";
180   foreach ($group as $field) {
181     $type = $field->type;
182     $label = htmlentities($field->label, ENT_QUOTES, 'utf-8');
183     $name = $field->name;
184     $name = "<a href=\"{$ABSWEB}admin/customfield.php?field=$name\">$name</a>";
185     echo "<tr><td>$name</td><td>$type</td><td>$label</td></tr>\n";
186   }
187   echo "</table>\n";
188 }
189
190 ?>
191 <form method='get'>
192   <input type='hidden' name='add' value='1'>
193   <button type='submit'>Add New Field</button>
194 </form>
195 <?php
196 }
197
198 mtrack_foot();
199