/** * Put action * * This function translates a PUT request into an edit or add action. Only if the model * state is unique and the item exists an edit action will be executed, if the resources * doesn't exist an add action will be executed. * * If the resource already exists it will be completely replaced based on the data * available in the request. * * If the model state is not unique the function will return false and set the * status code to 400 BAD REQUEST. * * @param KCommandContext A command context object * @return KDatabaseRow(set) A row(set) object containing the modified data */ protected function _actionPut(KCommandContext $context) { $result = false; if ($this->getModel()->getState()->isUnique()) { $row = $this->getModel()->getItem(); $action = 'add'; if (!$row->isNew()) { //Reset the row data $row->reset(); $action = 'edit'; } //Set the row data based on the unique state information $state = $this->getModel()->getState()->getData(true); $row->setData($state); $result = parent::execute($action, $context); } else { $context->status = KHttpResponse::BAD_REQUEST; } return $result; }
/** * Push the request data into the model state * * @TODO this is because KControllerBread have $data = null, while KControllerAbstract requires it to be KCommandContext */ public function execute($action, $data = null) { if (!is_a($data, 'KCommandContext')) { $data = new KCommandContext(); } return parent::execute($action, $data); }