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