Skip to content

Commit 7110d59

Browse files
committed
Merge pull request kbsali#87 from alxshr/master
Fix array attributes parsing in project save (create / update)
2 parents 3f4663f + a3bce11 commit 7110d59

31 files changed

+224
-199
lines changed

lib/Redmine/Api/AbstractApi.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ protected function retrieveAll($endpoint, array $params = array())
137137
$params['limit'] = $_limit;
138138
$params['offset'] = $offset;
139139

140-
$newDataSet = (array) $this->get($endpoint . '?' . http_build_query($params));
140+
$newDataSet = (array) $this->get($endpoint.'?'.http_build_query($params));
141141
$ret = array_merge_recursive($ret, $newDataSet);
142142

143143
$offset += $_limit;
@@ -171,8 +171,21 @@ protected function attachCustomFieldXML(SimpleXMLElement $xml, array $fields)
171171
if (isset($field['name'])) {
172172
$_field->addAttribute('name', $field['name']);
173173
}
174+
if (isset($field['field_format'])) {
175+
$_field->addAttribute('field_format', $field['field_format']);
176+
}
177+
174178
$_field->addAttribute('id', $field['id']);
175-
$_field->addChild('value', $field['value']);
179+
if (is_array($field['value'])) {
180+
$_field->addAttribute('multiple', 'true');
181+
$_values = $_field->addChild('value');
182+
$_values->addAttribute('type', 'array');
183+
foreach ($field['value'] as $val) {
184+
$_value = $_values->addChild('value', $val);
185+
}
186+
} else {
187+
$_field->addChild('value', $field['value']);
188+
}
176189
}
177190

178191
return $xml;

lib/Redmine/Api/Attachment.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ public function show($id)
3131
*/
3232
public function upload($attachment)
3333
{
34-
return $this->post('/uploads.json', $attachment);
34+
return $this->post('/uploads.json', $attachment);
3535
}
3636
}

lib/Redmine/Api/Group.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function create(array $params = array())
5959
'user_ids' => null,
6060
);
6161
$params = array_filter(array_merge($defaults, $params));
62-
if(
62+
if (
6363
!isset($params['name'])
6464
) {
6565
throw new \Exception('Missing mandatory parameters');

lib/Redmine/Api/Issue.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ private function buildXML(array $params = array())
7777
foreach ($v as $upload) {
7878
$upload_item = $uploads_item->addChild('upload', '');
7979
foreach ($upload as $upload_k => $upload_v) {
80-
$upload_item->addChild($upload_k, $upload_v);
80+
$upload_item->addChild($upload_k, $upload_v);
8181
}
8282
}
8383
} elseif ('description' === $k && strpos($v, '\n') !== false) {
@@ -203,7 +203,7 @@ public function setIssueStatus($id, $status)
203203
$statusId = $this->client->api('issue_status')->getIdByName($status);
204204

205205
return $this->update($id, array(
206-
'status_id' => $statusId
206+
'status_id' => $statusId,
207207
));
208208
}
209209

@@ -215,7 +215,7 @@ public function setIssueStatus($id, $status)
215215
public function addNoteToIssue($id, $note)
216216
{
217217
return $this->update($id, array(
218-
'notes' => $note
218+
'notes' => $note,
219219
));
220220
}
221221

@@ -269,8 +269,8 @@ public function attach($id, array $attachment)
269269
$request['issue'] = array(
270270
'id' => $id,
271271
'uploads' => array(
272-
'upload' => $attachment
273-
)
272+
'upload' => $attachment,
273+
),
274274
);
275275

276276
return $this->put('/issues/'.$id.'.json', json_encode($request));

lib/Redmine/Api/IssueCategory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function create($project, array $params = array())
9191
'assigned_to_id' => null,
9292
);
9393
$params = array_filter(array_merge($defaults, $params));
94-
if(
94+
if (
9595
!isset($params['name'])
9696
) {
9797
throw new \Exception('Missing mandatory parameters');

lib/Redmine/Api/Membership.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function create($project, array $params = array())
4343
'role_ids' => null,
4444
);
4545
$params = array_filter(array_merge($defaults, $params));
46-
if((!isset($params['user_ids']) && !isset($params['user_id']))
46+
if ((!isset($params['user_ids']) && !isset($params['user_id']))
4747
|| !isset($params['role_ids'])
4848
) {
4949
throw new \Exception('Missing mandatory parameters');
@@ -65,7 +65,7 @@ public function create($project, array $params = array())
6565
public function update($id, array $params = array())
6666
{
6767
$defaults = array(
68-
'role_ids' => null
68+
'role_ids' => null,
6969
);
7070
$params = array_filter(array_merge($defaults, $params));
7171
if (!isset($params['role_ids'])) {

lib/Redmine/Api/News.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,4 @@ public function all($project = null, array $params = array())
2525

2626
return $this->news;
2727
}
28-
2928
}

lib/Redmine/Api/Project.php

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -94,25 +94,14 @@ public function create(array $params = array())
9494
array($this, 'isNotNull')
9595
);
9696

97-
if(
97+
if (
9898
!isset($params['name'])
9999
|| !isset($params['identifier'])
100100
) {
101101
throw new \Exception('Missing mandatory parameters');
102102
}
103103

104-
$xml = new SimpleXMLElement('<?xml version="1.0"?><project></project>');
105-
foreach ($params as $k => $v) {
106-
if ('tracker_ids' == $k && is_array($v)) {
107-
$trackers = $xml->addChild('tracker_ids', '');
108-
$trackers->addAttribute('type', 'array');
109-
foreach ($v as $id) {
110-
$trackers->addChild('tracker', $id);
111-
}
112-
} else {
113-
$xml->addChild($k, $v);
114-
}
115-
}
104+
$xml = $this->prepareParamsXml($params);
116105

117106
return $this->post('/projects.xml', $xml->asXML());
118107
}
@@ -135,12 +124,40 @@ public function update($id, array $params)
135124
);
136125
$params = array_filter(array_merge($defaults, $params));
137126

127+
$xml = $this->prepareParamsXml($params);
128+
129+
return $this->put('/projects/'.$id.'.xml', $xml->asXML());
130+
}
131+
132+
/**
133+
*
134+
* @param array $params
135+
* @return \Redmine\Api\SimpleXMLElement
136+
*/
137+
protected function prepareParamsXml($params)
138+
{
139+
$array_params = array(
140+
'tracker_ids', 'issue_custom_field_ids',
141+
);
142+
$array_params_elements = array(
143+
'tracker_ids' => 'tracker',
144+
'issue_custom_field_ids' => 'issue_custom_field',
145+
);
146+
138147
$xml = new SimpleXMLElement('<?xml version="1.0"?><project></project>');
139148
foreach ($params as $k => $v) {
140-
$xml->addChild($k, $v);
149+
if (in_array($k, $array_params) && is_array($v)) {
150+
$array = $xml->addChild($k, '');
151+
$array->addAttribute('type', 'array');
152+
foreach ($v as $id) {
153+
$array->addChild($array_params_elements[$k], $id);
154+
}
155+
} else {
156+
$xml->addChild($k, $v);
157+
}
141158
}
142159

143-
return $this->put('/projects/'.$id.'.xml', $xml->asXML());
160+
return $xml;
144161
}
145162

146163
/**

lib/Redmine/Api/Query.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,4 @@ public function all(array $params = array())
2525

2626
return $this->query;
2727
}
28-
2928
}

lib/Redmine/Api/TimeEntry.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function create(array $params = array())
5555
'comments' => null,
5656
);
5757
$params = array_filter(array_merge($defaults, $params));
58-
if(
58+
if (
5959
(!isset($params['issue_id']) && !isset($params['project_id']))
6060
|| !isset($params['hours'])
6161
) {

0 commit comments

Comments
 (0)