This repository has been archived by the owner on Sep 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.php
121 lines (107 loc) · 4.46 KB
/
index.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
<?php
require_once 'vendor/limonade.php';
require_once 'scrumio.classes.php';
require_once 'init.php';
require_once 'helpers.php';
function configure() {
option('OAUTH_REDIRECT_URI', 'http://'.$_SERVER['HTTP_HOST'].url_for('authorize'));
option('OAUTH_URL', htmlentities(OAUTH_ENDPOINT.'?response_type=code&client_id='.CLIENT_ID.'&redirect_uri='.rawurlencode(option('OAUTH_REDIRECT_URI'))));
}
dispatch('/', 'scrumboard');
dispatch('/show/:id', 'scrumboard');
function scrumboard() {
global $api;
// If we have an access token, show the scrumboard
if (isset($_SESSION['access_token']) && $_SESSION['access_token'] && isset($_SESSION['story_app']) && $_SESSION['story_app']) {
// Grab sprints and find current sprint
// $filters = array(array('key' => SPRINT_STATE_ID, 'values' => array('Active')));
try {
$sprints = $api->item->getItems(SPRINT_APP_ID, array(
'limit' => 5,
'sort_by' => 'created_on',
'sort_desc' => 1
));
}
catch (PodioError $e) {
die("There was an error. The API responded with the error type <b>{$e->body['error']}</b> and the message <b>{$e->body['error_description']}</b>. The URL was <b>{$e->url}</b><br><a href='".url_for('logout')."'>Log out</a>");
}
foreach ($sprints['items'] as $item) {
if (params('id') == $item['item_id']) {
$current_sprint = $item;
break;
}
else {
foreach ($item['fields'] as $field) {
if ($field['field_id'] == SPRINT_STATE_ID) {
if ($field['values'][0]['value'] == 'Active') {
$current_sprint = $item;
}
}
}
}
}
$sprint = new ScrumioSprint($current_sprint);
return html('index.html.php', NULL, array('sprint' => $sprint, 'sprints' => $sprints['items']));
}
else {
// No access token, show the "login" screen
return html('login.html.php', NULL, array('oauth_url' => option('OAUTH_URL')));
}
}
dispatch('/authorize', 'authorize');
function authorize() {
global $api;
$story_app = NULL;
// Successful authorization. Store the access token in the session
if (!isset($_GET['error'])) {
try {
$api->authenticate('authorization_code', array('code' => $_GET['code'], 'redirect_uri' => option('OAUTH_REDIRECT_URI')));
$_SESSION['access_token'] = $api->oauth->access_token;
$_SESSION['refresh_token'] = $api->oauth->refresh_token;
$story_app = $api->app->get(STORY_APP_ID);
}
catch (PodioError $e) {
die("There was an error. The API responded with the error type <b>{$e->body['error']}</b> and the message <b>{$e->body['error_description']}</b><br><a href='".url_for('/')."'>Go back</a>");
}
}
if ($story_app) {
$_SESSION['story_app'] = $story_app;
$_SESSION['space'] = $api->space->get($_SESSION['story_app']['space_id']);
redirect_to('');
}
else {
// Something went wrong. Display appropriate error message.
unset($_SESSION['access_token']);
unset($_SESSION['refresh_token']);
$error_description = !empty($_GET['error_description']) ? htmlentities($_GET['error_description']) : 'You do not have access to the ScrumIO apps. Try logging in as a different user.';
return html('login.html.php', NULL, array('oauth_url' => option('OAUTH_URL'), 'error_description' => $error_description));
}
}
dispatch_put('/item/:item_id', 'update_time_left');
function update_time_left() {
global $api;
$item_id = params('item_id');
$state = $_POST['state'];
$data = array(array('value' => $state));
$api->item->updateFieldValue($item_id, ITEM_STATE_ID, $data);
// Set time_left to '0' when moving to one of the 'done' states
if (in_array($state, array(STATE_DEV_DONE, STATE_QA_DONE, STATE_PO_DONE))) {
$api->item->updateFieldValue($item_id, ITEM_TIMELEFT_ID, array(array('value' => 0)), 1);
}
// Reset time left when moving to Not Started
elseif ($state == STATE_NOT_STARTED) {
$item = $api->item->getBasic($item_id);
$item = new ScrumioItem($item);
$api->item->updateFieldValue($item_id, ITEM_TIMELEFT_ID, array(array('value' => $item->estimate*60*60)), 1);
}
return txt('ok');
}
dispatch('/logout', 'logout');
function logout() {
unset($_SESSION['access_token']);
unset($_SESSION['refresh_token']);
unset($_SESSION['space']);
unset($_SESSION['story_app']);
redirect_to('');
}
run();