-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhase.php
More file actions
179 lines (132 loc) · 4.14 KB
/
Phase.php
File metadata and controls
179 lines (132 loc) · 4.14 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/**
* Created by PhpStorm.
* User: ExE
* Date: 27.01.17
* Time: 12:01
*/
require_once "APIObject.php";
class Phase extends APIObject {
public $id;
public $name;
public $description;
public $done;
public $draft;
public $pipe_id;
public $index;
public $created_at;
public $updated_at;
public $can_edit;
public $cards; // [Card]
public $fields; // [Field],
public $connected_pipes; // [Pipe],
public $jump_targets; // [Phase ?]
/**
* @param null $data
*/
function __construct($data = null) {
if ($data != null) {
$this->assign_results($data);
}
}
/**
* @param null $phase_id int
* @return $this
*/
public function fetch($phase_id = null) {
if ($phase_id == null)
$phase_id = $this->id;
$resp = $this->send_get("https://app.pipefy.com/phases/$phase_id.json", null, null);
$this->assign_results($resp);
$this->parse_property("cards", "Card");
$this->parse_property("fields", "Field");
$this->parse_property("connected_pipes", "Pipe");
return $this;
}
/**
* @param $field_id int
* @return Field
*/
public function get_field_by_id($field_id) {
foreach ($this->fields as $key => $field) {
if ($field->id == $field_id)
return $field;
}
return false;
}
/**
* @param $field_label string
* @return Field
*/
public function get_field_by_label($field_label) {
foreach ($this->fields as $key => $field) {
if ($field->label == $field_label)
return $field;
}
return false;
}
public function move_card($from_index, $dest_index, $before) {
$cards_count = count($this->cards);
if ($from_index < 0 || $from_index >= $cards_count || $dest_index < 0 || $dest_index >= $cards_count)
return false;
$card_to_move = $this->cards[$from_index];
$putData = array();
if ($before) {
if ($dest_index != 0) {
$putData["previous_id"] = $this->cards[$dest_index - 1]->id;
}
$putData["next_id"] = $this->cards[$dest_index]->id;
}
else {
if ($dest_index != $cards_count - 1) {
$putData["next_id"] = $this->cards[$dest_index + 1]->id;
}
$putData["previous_id"] = $this->cards[$dest_index]->id;
}
$this->send_put(sprintf("https://app.pipefy.com/cards/%d/move", $card_to_move->id), null, $putData);
//move card in array
$new_cards = array();
for ($i = 0; $i < $cards_count; $i++) {
if ($i == $dest_index) {
$new_cards[] = $card_to_move;
}
else {
if ($i != $from_index)
$new_cards[] = $this->cards[$i];
}
}
$this->cards = $new_cards;
return $this;
}
public function move_card_by_id($card_id, $prev_card_id, $next_card_id, $fetch_updated = true) {
$putData = array();
if ($prev_card_id != null)
$putData["previous_id"] = $prev_card_id;
if ($next_card_id != null)
$putData["next_id"] = $next_card_id;
$this->send_put(sprintf("https://app.pipefy.com/cards/%d/move", $card_id), null, $putData);
if ($fetch_updated)
return $this->fetch();
else
return $this;
}
public function sort_cards_by_due_date() {
foreach ($this->cards as $card) {
$card->fetch();
}
usort($this->cards, function ($a, $b) {
$ta = strtotime($a->due_date);
$tb = strtotime($b->due_date);
if ($ta == $tb) {
return 0;
}
return ($ta < $tb) ? -1 : 1;
});
for ($i = 1; $i < count($this->cards); $i++) {
$card_id = $this->cards[$i]->id;
$prev_card_id = $this->cards[$i - 1]->id;
$this->move_card_by_id($card_id, $prev_card_id, null, false);
}
return $this->fetch();
}
}