-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCard.php
220 lines (166 loc) · 6.64 KB
/
Card.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
/**
* Created by PhpStorm.
* User: ExE
* Date: 27.01.17
* Time: 12:02
*/
require_once "APIObject.php";
class Card extends APIObject {
public $id;
public $title;
public $current_phase_id;
public $due_date;
public $duration;
public $started_at;
public $finished_at;
public $expiration_time;
public $index;
public $token;
public $pipe; // Pipe
public $expired;
public $late;
public $draft;
public $done;
public $can_show_pipe;
public $previous_phase; // Phase
public $next_phase; //Phase
public $assignees; // [User],
public $labels; // [Label],
public $current_phase_detail; // PhaseDetail,
public $other_phase_details; // [],
public $parent_card_phase_detail; // CardPhaseDetail
public $checklists; // [Checklist]
private $endpoint = "https://app.pipefy.com/";
/**
* @param null $data mixed
*/
function __construct($data = null) {
if ($data != null) {
$this->assign_results($data);
}
}
/**
* @param null $card_id mixed
* @return $this
*/
public function fetch($card_id = null) {
if ($card_id == null)
$card_id = $this->id;
$resp = $this->send_get($this->endpoint . "cards/" . $card_id . ".json", array("Content-Type: application/json"), null);
$this->assign_results($resp);
$this->parse_property("pipe", "Pipe");
$this->parse_property("previous_phase", "Phase");
$this->parse_property("next_phase", "Phase");
$this->parse_property("assignees", "User");
$this->parse_property("labels", "Label");
$this->parse_property("current_phase_detail", "PhaseDetail");
$this->parse_property("other_phase_details", "PhaseDetail");
$this->parse_property("parent_card_phase_detail", "CardPhaseDetail");
$this->parse_property("checklists", "Checklist");
return $this;
}
/**
* @param $title string
* @param $pipe_id int
* @param $field_values array
* @param $parent_card_id int If not null, the connected card will be created.
* @return $this
*/
public function create_card ($title, $pipe_id, $field_values, $parent_card_id) {
if ($parent_card_id == null) {
//creating common card
$card["card"]["title"] = $title;
$card["card"]["field_values"] = $field_values;
$resp = $this->send_post($this->endpoint . "cards/pipes/" . $pipe_id . "/create_card.json", array("Content-Type: application/json"), $card);
$this->assign_results($resp);
$this->parse_property("pipe", "Pipe");
$this->parse_property("previous_phase", "Phase");
$this->parse_property("next_phase", "Phase");
$this->parse_property("assignees", "User");
$this->parse_property("labels", "Label");
$this->parse_property("current_phase_detail", "PhaseDetail");
$this->parse_property("other_phase_details", "PhaseDetail");
$this->parse_property("parent_card_phase_detail", "CardPhaseDetail");
$this->parse_property("checklists", "Checklist");
return $this;
}
else {
//create connected card
$resp = $this->send_post($this->endpoint . "cards/" . $parent_card_id . "/create_connected_card.json?pipe_id=" . $pipe_id, null, null);
$this->assign_results($resp);
$card["card"]["title"] = $title;
$card["card"]["id"] = $this->id;
//change title
$this->send_put($this->endpoint . "cards/" . $this->id . ".json", array("Content-Type: application/json"), json_encode($card));
//set field values
foreach($field_values as $key => $field) {
$fieldData = array(
"card_id" => $this->id,
"field_id" => $field["field_id"],
"new_value" => $field["value"]
);
$this->send_post($this->endpoint . "card_field_values/persist.json", null, http_build_query($fieldData));
}
//sync card data
$this->fetch($this->id);
//moving to the first non-draft phase
$this->send_put($this->endpoint . "cards/".$this->id."/jump_to_phase/".$this->next_phase->id.".json", array("application/x-www-form-urlencoded"), array("source" => "connected_card"));
//sync card data
$this->fetch($this->id);
return $this;
}
}
public function jump_to_phase($phase_id) {
$this->send_put($this->endpoint . "cards/".$this->id."/jump_to_phase/".$phase_id.".json", array("application/x-www-form-urlencoded"), array());
return $this->fetch();
}
/**
* @return $this
*/
public function move_to_next_phase() {
$resp = $this->send_put($this->endpoint . "cards/" . $this->id . "/next_phase.json", null, null);
$this->assign_results($resp);
return $this;
}
/**
* @return $this
*/
public function move_to_previous_phase() {
$resp = $this->send_put($this->endpoint . "cards/" . $this->id . "/previous_phase.json", null, null);
$this->assign_results($resp);
return $this;
}
public function update_field_persist($fieldID, $newValue) {
$fieldData = array(
"card_id" => $this->id,
"field_id" => $fieldID,
"new_value" => $newValue
);
$this->send_post($this->endpoint . "card_field_values/persist.json", null, http_build_query($fieldData));
return $this;
}
public function update_field($fieldID, $newValue) {
$fieldData = array(
"card_phase_detail_id" => $this->current_phase_detail->id,
"field_id" => $fieldID,
"new_value" => $newValue
);
$this->send_post($this->endpoint . "card_field_values.json", null, http_build_query($fieldData));
return $this;
}
public function leave_comment($text) {
$comment = array();
$comment["comment"]["text"] = $text;
$comment["comment"]["card_phase_detail_id"] = $this->current_phase_detail->id;
$resp = $this->send_post($this->endpoint . "comments.json", null, http_build_query($comment));
return json_decode($resp);
}
public function create_checklist($checklist_name, $items, $no_layout = true) {
$checklist = (new Checklist())->create_checklist($checklist_name, $this->current_phase_detail->id, $no_layout);
foreach ($items as $item_name) {
$checklist->add_option($item_name, $no_layout);
}
return $checklist;
}
}