/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate() { $model = new PollChoice; // $this->performAjaxValidation($model); if (isset($_POST['PollChoice'])) { $model->attributes = $_POST['PollChoice']; if ($model->save()) { $this->redirect(array('index')); } else { $this->addFlashMessage($model->errors,'error'); } } $this->render('create', array( 'model' => $model, )); }
/** * Updates a particular model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id the ID of the model to be updated */ public function actionUpdate($id) { $model = $this->loadModel($id); $this->pageName = 'Редактирование'; $choices = $model->choices; //$this->performAjaxValidation($model); if (Yii::app()->request->isAjaxRequest) { if (isset($_POST['remove'])) { $deleteModel = PollChoice::model()->findByPk($_POST['id']); $deleteModel->delete(); } } if (isset($_POST['PollChoice']['update'])) { foreach ($_POST['PollChoice']['update'] as $key => $row) { $updateModel = PollChoice::model()->findByPk($key); if ($updateModel->validate()) { $updateModel->name = $row; $updateModel->save(false, false); } else { echo '<script>alert("record no valid please edit this record");</script>'; } } } if (isset($_POST['PollChoice']['create'])) { foreach ($_POST['PollChoice']['create'] as $key => $row) { $createModel = new PollChoice(); //$createModel->attributes = $_POST['filter']['create']; $createModel->poll_id = $model->id; $createModel->name = $row; if ($createModel->validate()) { //$createModel->value = $filter; $createModel->save(false, false); } else { echo '<script>alert("record no valid please edit this record");</script>'; } } } if (isset($_POST['Poll'])) { $model->attributes = $_POST['Poll']; $model->save(); //$this->redirect(array('view', 'id' => $model->id)); } $this->render('update', array('model' => $model, 'choices' => $choices)); }
/** * Save attribute choice * @param StoreAttribute $model */ protected function saveChoice($model) { $dontDelete = array(); if (!empty($_POST['choice'])) { $sort = 0; foreach ($_POST['choice'] as $key => $val) { if (isset($val[0]) && $val[0] != '') { $index = 0; $poll_choice = PollChoice::model()->findByAttributes(array('id' => $key, 'poll_id' => $model->id)); if (!$poll_choice) { $poll_choice = new PollChoice(); $poll_choice->poll_id = $model->id; } $poll_choice->sort = $sort; $poll_choice->save(false); foreach (Yii::app()->languageManager->languages as $lang) { $poll_choice = PollChoice::model()->language($lang->id)->findByAttributes(array('id' => $poll_choice->id)); $poll_choice->name = $val[$index]; $poll_choice->save(false); ++$index; } array_push($dontDelete, $poll_choice->id); $sort++; } } } if (sizeof($dontDelete)) { $cr = new CDbCriteria(); $cr->addNotInCondition('t.id', $dontDelete); $choiseToDelete = PollChoice::model()->findAllByAttributes(array('poll_id' => $model->id), $cr); } else { // Clear all attribute choise $choiseToDelete = PollChoice::model()->findAllByAttributes(array('poll_id' => $model->id)); } if (!empty($choiseToDelete)) { foreach ($choiseToDelete as $o) { $o->delete(); } } }