public function actionCreate() { switch ($_GET['model']) { // Get an instance of the respective model case 'category': $model = new Category(); break; case 'item': $model = new Item(); break; default: $this->_sendResponse(501, sprintf('Mode <b>create</b> is not implemented for model <b>%s</b>', $_GET['model'])); exit; } // Try to assign POST values to attributes foreach ($_POST as $var => $value) { // Does the model have this attribute? If not raise an error if ($model->hasAttribute($var)) { $model->{$var} = $value; } else { $this->_sendResponse(500, sprintf('Parameter <b>%s</b> is not allowed for model <b>%s</b>', $var, $_GET['model'])); } } // Try to save the model if ($model->save()) { $msg = '<div data-role="page" id="place" data-theme="b" data-add-back-btn="true"> <div data-role="header"> <h1>Share Your Place</h1> </div> <div data-role="content"> <p>Thank you. We will review and approve your place soon.</p> </div> </div>'; $this->_sendResponse(200, $msg); } else { // Errors occurred $msg = "<h1>Error</h1>"; $msg .= sprintf("Couldn't create model <b>%s</b>", $_GET['model']); $msg .= "<ul>"; foreach ($model->errors as $attribute => $attr_errors) { $msg .= "<li>Attribute: {$attribute}</li>"; $msg .= "<ul>"; foreach ($attr_errors as $attr_error) { $msg .= "<li>{$attr_error}</li>"; } $msg .= "</ul>"; } $msg .= "</ul>"; $this->_sendResponse(500, $msg); } }