Migrate 4 builtin fields to fields2: priority, resolution, severity, status.
[bugdar.git] / docs / migrate_to_fields2.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar
5 || # Copyright (c)2002-2015 Blue Static
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version 2 of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 chdir('../');
23 require_once './includes/init.php';
24 require_once BUGDAR_ROOT . '/includes/model_field.php';
25
26 $do = filter_input(INPUT_GET, 'do');
27
28 $simple_field_types = [
29 'priority',
30 'resolution',
31 'severity',
32 'status',
33 ];
34
35 if ($do === NULL) {
36 }
37
38 ///////////////////////////////////////////////////////////////////////////////
39
40 if (in_array($do, $simple_field_types)) {
41 // Get the userhelp for title and description.
42 $stmt = Bugdar::$db->Prepare("SELECT * FROM " . TABLE_PREFIX . "fieldhelp WHERE keystring = ?");
43 $stmt->Execute([$do]);
44 $fieldhelp = $stmt->FetchObject();
45
46 // Find the default value.
47 $stmt = Bugdar::$db->Prepare("SELECT value FROM " . TABLE_PREFIX . "setting WHERE varname = ?");
48 if ($do == 'resolution')
49 $stmt->Execute(['defaultresolve']); // Unclear why this ever was.
50 else
51 $stmt->Execute(['default' . $do]);
52 $default_setting = $stmt->FetchObject();
53
54 // Get all the values.
55 $stmt = Bugdar::$db->Query("SELECT * FROM " . TABLE_PREFIX . $do . " ORDER BY displayorder");
56 $values = [];
57 $default_value = NULL;
58 while ($value = $stmt->Fetch()) {
59 $values[] = $value[$do];
60 if ($value[$do . 'id'] == $default_setting->value)
61 $default_value = $value[$do];
62 }
63
64 // Create the new field.
65 $field = new bugdar\Field();
66 $field->title = $fieldhelp->title;
67 $field->description = $fieldhelp->body;
68 $field->type = bugdar\Field::TYPE_LIST;
69 $field->validator_pattern = implode("\n", $values);
70 $field->required = TRUE;
71 $field->default_value = $default_value;
72 $field->can_search = TRUE;
73 var_dump($field);
74 $field->Insert();
75
76 // Migrate the data from field IDs in the bug table to the values in the new
77 // all-values table.
78 $stmt = Bugdar::$db->Prepare("
79 INSERT INTO ". TABLE_PREFIX . "bugfield2
80 (bugid, title, value)
81 SELECT bug.bugid, '" . $field->title . "', field." . $do . "
82 FROM " . TABLE_PREFIX . "bug AS bug
83 LEFT JOIN " . TABLE_PREFIX . $do . " AS field
84 ON (bug." . $do . " = field." . $do . "id)
85 ");
86 $stmt->Execute();
87 }
88
89 ///////////////////////////////////////////////////////////////////////////////
90
91 if ($do === 'product') {
92 }
93
94 print Bugdar::$db->ConstructHTMLDebugBlock();