public function actionCreate() { $model = new Election(); if (isset($_POST['Election'])) { $model->attributes = $_POST['Election']; $model->user_id = Yii::app()->user->id; $model->status = 0; if ($model->validate()) { $transaction = Yii::app()->db->beginTransaction(); if ($model->save()) { $this->assignRoles($model); $transaction->commit(); Yii::app()->user->setFlash('success', Yii::t('aes', 'Election created')); $this->redirect(array('/election/view/' . $model->id)); } else { $transaction->rollback(); } } } $this->render('create', array('model' => $model)); }
if (!isset($user)) { $app->flash('error', 'You must be logged in to access that page.'); $app->redirect($app->urlFor('homepage')); } else { if ($user->isEC()) { $app->render('create-ballot.html'); } else { $app->flash('error', 'You do not have permission to access that page.'); $app->redirect($app->urlFor('homepage')); } } })->name('create.ballot'); $app->post('/dashboard/create-ballot', function () use($app, $db) { $user = $app->view->get('user'); $data = array(); $app->response->headers->set('Content-Type', 'application/json'); if (!isset($user) || !$user->isEC()) { $data['success'] = false; $data['message'] = 'You do not have permission to perform that action.'; } else { $postData = json_decode($app->request->getBody()); $election = new Election($postData); $election->save($db); if (isset($election->id)) { $data['success'] = true; $data['id'] = $election->id; $data['message'] = 'Successfully created ballot "' . $election->name . '"'; } } $app->response->setBody(json_encode($data)); });