a8a26b10a5eac94285ac2005c01b53ca21c7dd78
[WebKit-https.git] / Websites / perf.webkit.org / public / admin / triggerables.php
1 <?php
2
3 require('../include/admin-header.php');
4
5 if ($db) {
6
7     if ($action == 'add') {
8         if ($db->insert_row('build_triggerables', 'triggerable', array('name' => $_POST['name']))) {
9             notice('Inserted the new triggerable.');
10             regenerate_manifest();
11         } else
12             notice('Could not add the triggerable.');
13     } else if ($action == 'update') {
14         if (update_field('build_triggerables', 'triggerable', 'name'))
15             regenerate_manifest();
16         else if (update_field('build_triggerables', 'triggerable', 'disabled', Database::to_database_boolean(array_get($_POST, 'disabled'))))
17             regenerate_manifest();
18         else
19             notice('Invalid parameters.');
20     } else if ($action == 'update-group-name') {
21         if (update_field('triggerable_repository_groups', 'repositorygroup', 'name'))
22             regenerate_manifest();
23     } else if ($action == 'update-group-description') {
24         if (update_field('triggerable_repository_groups', 'repositorygroup', 'description'))
25             regenerate_manifest();
26     } else if ($action == 'update-group-accept-roots') {
27         if (update_field('triggerable_repository_groups', 'repositorygroup', 'accepts_roots',
28             Database::to_database_boolean(array_get($_POST, 'accepts'))))
29             regenerate_manifest();
30     } else if ($action == 'update-repository') {
31         $association = array_get($_POST, 'association');
32         $triggerable_id = array_get($_POST, 'triggerable');
33         $repository_id = array_get($_POST, 'repository');
34
35         $should_delete = FALSE;
36         $accepted = $association == 'accepted';
37         $required = $association == 'required';
38         if ($accepted || $required) {
39             $db->begin_transaction();
40             $select = array('repository' => $repository_id, 'triggerable' => $triggerable_id);
41             $update = array('repository' => $repository_id, 'triggerable' => $triggerable_id, 'required' => Database::to_database_boolean($required));
42             if (!$db->update_row('triggerable_repositories', 'trigrepo', $select, $update, 'repository')) {
43                 notice("Failed to update the association of repository $repository_id with triggerable $triggerable_id.");
44                 $db->rollback_transaction();
45             } else
46                 $db->commit_transaction();
47         } else if ($association == 'not-accepted') {
48             $db->begin_transaction();
49             $result = $db->query_and_get_affected_rows("DELETE FROM triggerable_repositories WHERE trigrepo_triggerable = $1 AND trigrepo_repository = $2",
50                 array($triggerable_id, $repository_id));
51             if ($result > 1) {
52                 notice("Failed to update the association of repository $repository_id with triggerable $triggerable_id.");
53                 $db->rollback_transaction();
54             } else
55                 $db->commit_transaction();
56         }
57     } else if ($action == 'update-repositories') {
58         $group_id = intval($_POST['group']);
59
60         $db->begin_transaction();
61         $db->query_and_get_affected_rows("DELETE FROM triggerable_repositories WHERE trigrepo_group = $1", array($group_id));
62         $suceeded = insert_triggerable_repositories($db, $group_id, array_get($_POST, 'repositories'));
63         if ($suceeded) {
64             $db->commit_transaction();
65             notice('Updated the association.');
66             regenerate_manifest();
67         } else
68             $db->rollback_transaction();
69     } else if ($action == 'add-repository-group') {
70         $triggerable_id = intval($_POST['triggerable']);
71         $name = $_POST['name'];
72
73         $db->begin_transaction();
74         $group_id = $db->insert_row('triggerable_repository_groups', 'repositorygroup', array('name' => $name, 'triggerable' => $triggerable_id));
75         if (!$group_id)
76             notice('Failed to insert the specified repository group.');
77         else if (!insert_triggerable_repositories($db, $group_id, array_get($_POST, 'repositories')))
78             $db->rollback_transaction();
79         else {
80             $db->commit_transaction();
81             notice('Updated the association.');
82             regenerate_manifest();
83         }
84     }
85
86     $repository_rows = $db->fetch_table('repositories', 'repository_name');
87
88     $page = new AdministrativePage($db, 'build_triggerables', 'triggerable', array(
89         'name' => array('editing_mode' => 'string'),
90         'disabled' => array('editing_mode' => 'boolean', 'post_insertion' => TRUE),
91         'repositories' => array(
92             'label' => 'Repository Groups',
93             'subcolumns'=> array('ID', 'Name', 'Description', 'Accepts Roots', 'Repositories'),
94             'custom' => function ($triggerable_row) use (&$db, &$repository_rows) {
95                 return generate_repository_list($db, $triggerable_row['triggerable_id'], $repository_rows);
96             }),
97     ));
98
99     $page->render_table('name');
100     $page->render_form_to_add();
101 }
102
103 function insert_triggerable_repositories($db, $group_id, $repositories)
104 {
105     if (!$repositories)
106         return TRUE;
107     foreach ($repositories as $repository_id) {
108         if (!$db->insert_row('triggerable_repositories', 'trigrepo', array('group' => $group_id, 'repository' => $repository_id), NULL)) {
109             notice("Failed to associate repository $repository_id with repository group $group_id.");
110             return FALSE;
111         }
112     }
113     return TRUE;
114 }
115
116
117 function generate_repository_list($db, $triggerable_id, $repository_rows) {
118     $group_forms = array();
119
120     $repository_groups = $db->select_rows('triggerable_repository_groups', 'repositorygroup', array('triggerable' => $triggerable_id), 'name');
121     foreach ($repository_groups as $group_row) {
122         $group_id = $group_row['repositorygroup_id'];
123         $group_name = $group_row['repositorygroup_name'];
124         $group_description = $group_row['repositorygroup_description'];
125         $checked_if_accepts_roots = Database::is_true($group_row['repositorygroup_accepts_roots']) ? 'checked' : '';
126
127         $group_name_form = <<< END
128             <form method="POST">
129             <input type="hidden" name="action" value="update-group-name">
130             <input type="hidden" name="id" value="$group_id">
131             <input type="text" name="name" value="$group_name">
132             </form>
133 END;
134
135         $group_description_form = <<< END
136             <form method="POST">
137             <input type="hidden" name="action" value="update-group-description">
138             <input type="hidden" name="id" value="$group_id">
139             <input name="description" value="$group_description">
140             </form>
141 END;
142
143         $group_accepts_roots = <<< END
144             <form method="POST">
145             <input type="hidden" name="action" value="update-group-accept-roots">
146             <input type="hidden" name="id" value="$group_id">
147             <input type="checkbox" name="accepts" $checked_if_accepts_roots>
148             <button type="submit">Save</button>
149             </form>
150 END;
151
152         array_push($group_forms, array($group_id, $group_name_form, $group_description_form, $group_accepts_roots, generate_repository_form($db, $repository_rows, $group_id)));
153     }
154
155     $new_group_checkboxes = generate_repository_checkboxes($db, $repository_rows);
156     $new_group_form = <<< END
157         <form method="POST">
158         <input type="hidden" name="action" value="add-repository-group">
159         <input type="hidden" name="triggerable" value="$triggerable_id">
160         <input type="text" name="name" value="" required><br>
161         $new_group_checkboxes
162         <br><button type="submit">Add</button></form>
163 END;
164
165     array_push($group_forms, $new_group_form);
166
167     return $group_forms;
168 }
169
170 function generate_repository_form($db, $repository_rows, $group_id)
171 {
172     $checkboxes = generate_repository_checkboxes($db, $repository_rows, $group_id);
173     return <<< END
174         <form method="POST">
175         <input type="hidden" name="action" value="update-repositories">
176         <input type="hidden" name="group" value="$group_id">
177         $checkboxes
178         <br><button type="submit">Save</button></form>
179 END;
180 }
181
182 function generate_repository_checkboxes($db, $repository_rows, $group_id = NULL)
183 {
184     $repositories_in_group = array();
185     if ($group_id) {
186         $group_repository_rows = $db->select_rows('triggerable_repositories', 'trigrepo', array('group' => $group_id));
187         foreach ($group_repository_rows as $row)
188             $repositories_in_group[$row['trigrepo_repository']] = TRUE;
189     }
190
191     $form = '';
192     foreach ($repository_rows as $row) {
193         $id = $row['repository_id'];
194         $name = $row['repository_name'];
195         $checked = array_key_exists($id, $repositories_in_group) ? 'checked' : '';
196         $form .= "<label><input type=\"checkbox\" name=\"repositories[]\" value=\"$id\" $checked>$name</label>";
197     }
198     return $form;
199 }
200
201 require('../include/admin-footer.php');
202
203 ?>