Skip to content

Commit ffe44e6

Browse files
committed
Save users with custom fields attached
1 parent b0b3621 commit ffe44e6

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

lib/Redmine/Api/AbstractApi.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,24 @@ protected function retrieveAll($endpoint, array $params = array())
113113

114114
return $ret;
115115
}
116+
117+
/**
118+
* Attaches Custom Fields to a create/update query
119+
*
120+
* @param SimpleXMLElement $xml XML Element the custom fields are attached to
121+
* @param array $fields array of fields to attach, each field needs name, id and value set
122+
* @return $xml Element
123+
* @see http://www.redmine.org/projects/redmine/wiki/Rest_api#Working-with-custom-fields
124+
*/
125+
protected function attachCustomFieldXML($xml,$fields) {
126+
$fieldsXML = $xml->addChild("custom_fields");
127+
$fieldsXML->addAttribute('type','array');
128+
foreach ($fields as $field) {
129+
$fieldXML = $fieldsXML->addChild("custom_field");
130+
$fieldXML->addAttribute('name',$field['name']);
131+
$fieldXML->addAttribute('id',$field['id']);
132+
$fieldXML->addChild('value',$field['value']);
133+
}
134+
return $xml;
135+
}
116136
}

lib/Redmine/Api/User.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,11 @@ public function create(array $params = array())
113113

114114
$xml = new \SimpleXMLElement('<?xml version="1.0"?><user></user>');
115115
foreach ($params as $k => $v) {
116-
$xml->addChild($k, $v);
116+
if ($k == 'custom_fields') {
117+
$this->attachCustomFieldXML($xml,$v);
118+
} else {
119+
$xml->addChild($k, $v);
120+
}
117121
}
118122

119123
return $this->post('/users.xml', $xml->asXML());
@@ -142,7 +146,11 @@ public function update($id, array $params)
142146

143147
$xml = new \SimpleXMLElement('<?xml version="1.0"?><user></user>');
144148
foreach ($params as $k => $v) {
145-
$xml->addChild($k, $v);
149+
if ($k == 'custom_fields') {
150+
$this->attachCustomFieldXML($xml,$v);
151+
} else {
152+
$xml->addChild($k, $v);
153+
}
146154
}
147155

148156
return $this->put('/users/'.$id.'.xml', $xml->asXML());

0 commit comments

Comments
 (0)