public function actionUpdate($resource_type, $id)
{
if (!in_array($resource_type, static::$resources)) {
$this->sendErrorResponse(404, "Unrecognised Resource type {$resource_type}");
}
if (!$id) {
$this->sendResponse(404, 'External Resource ID required');
}
$resource_model = $this->getResourceModel($resource_type);
$body = \Yii::app()->request->rawBody;
try {
$resource = $resource_model::fromXml(static::$version, $body, array('update_only' => $this->getUpdateOnly(), 'partial_record' => $this->getPartialRecord()));
if ($resource->errors) {
$this->sendErrorResponse(400, $resource->errors);
}
$resource->id = $id;
if (!($internal_id = $resource->save())) {
if ($resource->errors && !$resource->warn_errors) {
$this->sendErrorResponse(400, $resource->errors);
} else {
// no internal id indicates we didn't get a resource
$response = array('Message' => $resource_type . ' not created');
// map errors to warnings if this is the case
if ($resource->errors) {
$response['Warnings'] = $resource->errors;
}
// success in that we are happy for there to have been no action taken
$this->sendSuccessResponse(200, $response);
}
}
$response = array('Id' => $internal_id);
if ($resource->isNewResource) {
$status_code = 201;
$response['Message'] = $resource_type . ' created.';
} else {
$status_code = 200;
$response['Message'] = $resource_type . ' updated.';
}
if ($resource->warnings) {
$response['Warnings'] = $resource->warnings;
}
$this->sendSuccessResponse($status_code, $response);
} catch (\Exception $e) {
if (YII_DEBUG) {
$errors = $resource->errors;
$errors[] = $e->getMessage();
\OELog::log(print_r($errors));
} else {
$errors = array('Could not save resource');
}
$this->sendErrorResponse(500, $errors);
}
}