-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlabels.php
123 lines (97 loc) · 2.98 KB
/
labels.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
require 'inc.bootstrap.php';
do_logincheck();
$key = &$_GET['key'];
$id = @$_GET['id'];
// Fetch labels autocomplete
if ( isset($_GET['label']) ) {
$searched = strtolower(trim($_GET['label']));
$rsp = jira_get('label', array(), $error, $info);
$labels = array_values(array_filter($rsp->values, function(string $found) use ($searched) {
return strpos($found, $searched) === 0;
}));
natcasesort($labels);
header('Content-type: text/json; charset=utf-8');
echo json_encode(compact('labels'));
exit;
}
// Update labels
else if ( isset($_POST['old_labels'], $_POST['new_labels']) ) {
$old_labels = array_filter(explode(' ', $_POST['old_labels']));
$new_labels = array_filter(explode(' ', $_POST['new_labels']));
$all_labels = array_unique(array_merge($old_labels, $new_labels));
$update = array();
foreach ( $all_labels AS $label ) {
$in_old = in_array($label, $old_labels);
$in_new = in_array($label, $new_labels);
if ( $in_old && !$in_new ) {
$update['labels'][] = array('remove' => $label);
}
else if ( !$in_old && $in_new ) {
$update['labels'][] = array('add' => $label);
}
}
if ( $update ) {
$response = jira_put('issue/' . $key, compact('update'), $error, $info);
}
if ( !$update || !$error ) {
return do_redirect('issue', array('key' => $key));
}
echo '<pre>';
print_r($update);
var_dump($error);
print_r($response);
print_r($info);
exit;
}
$issue = jira_get('issue/' . $key);
$id = $issue->id;
$_title = "Labels $key";
include 'tpl.header.php';
echo '<h1><a href="issue.php?key=' . $key . '">' . $key . '</a> ' . html($issue->fields->summary) . '</h1>';
?>
<h2>Labels</h2>
<form autocomplete="off" method="post">
<input type="hidden" name="old_labels" value="<?= html(implode(' ', $issue->fields->labels)) ?>" />
<p><input id="ls" name="new_labels" value="<?= html(implode(' ', $issue->fields->labels)) ?> " size="60" autocapitalize="off" /></p>
<p>(<a id="fl" href="#">fetch</a>) <button>Save</button></p>
</form>
<p>Suggestions: <span id="ss"></span></p>
<script>
$('ss').on('click', 'a', function(e) {
e.preventDefault();
var label = this.data('label');
var ls = $('ls');
var curLabels = ls.value;
// Append
if ( curLabels.match(/ $/) ) {
curLabels += label + ' ';
}
// Replace
else {
curLabels = (' ' + curLabels).replace(/ \w+$/, ' ' + label).trim() + ' ';
}
ls.value = curLabels;
});
$('fl').on('click', function(e) {
e.preventDefault();
var labels = $('ls').value.trim().split(/ /g);
var label = labels[labels.length-1];
document.body.classList.add('loading');
$.get('?id=<?= $id ?>&label=' + label).on('done', function(e, rsp) {
var html = '';
r.each(rsp.labels, function(label) {
html += ' [<a data-label="' + label + '" href="#">' + label + '</a>] ';
});
$('ss').setHTML(html || 'no results');
document.body.classList.remove('loading');
});
});
</script>
<?php
// echo '<p>[' . implode('] [', $labels) . ']</p>';
// echo '<pre>';
// print_r($labels);
// var_dump($error);
// print_r($info);
include 'tpl.footer.php';