/** * 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); $options_list = new UserOptions(); $options_text = new UserText(); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['Users']) || isset($_POST['UserText']) || isset($_POST['UserOptions'])) { if (isset($_POST['Users'])) { $model->attributes = $_POST['Users']; $model->save(); } if (isset($_POST['UserText'])) { $options_text->attributes = $_POST['UserText']; foreach ($options_text->Value as $key => $data) { if (UserText::model()->findByAttributes(array('UserId' => $id, 'AttributeId' => $key))) { $res = UserText::model()->findByAttributes(array('UserId' => $id, 'AttributeId' => $key)); if ($data) { $res->Value = $data; $res->save(); } else { $res->delete(); } } else { if ($data) { $options_text = new UserText(); $options_text->UserId = $id; $options_text->AttributeId = $key; $options_text->Value = $data; $options_text->save(); } } } } if (isset($_POST['UserOptions'])) { $options_list->attributes = $_POST['UserOptions']; $result = $options_list->AttributeValueId; foreach ($result as $key => $data) { if (Attributes::model()->findByPk($key)->Type == 'checkbox') { if (!empty($data)) { $existed = array(); if (UserOptions::model()->findAllByAttributes(array('UserId' => $id, 'AttributeId' => $key))) { $res = UserOptions::model()->findAllByAttributes(array('UserId' => $id, 'AttributeId' => $key)); foreach ($res as $val) { $existed[] = $val->AttributeValueId; } } $toInsert = array_diff($data, $existed); $toDelete = array_diff($existed, $data); if (!empty($toInsert)) { foreach ($toInsert as $insert) { $options_list = new UserOptions(); $options_list->UserId = $id; $options_list->AttributeId = $key; $options_list->AttributeValueId = $insert; $options_list->save(); } } if (!empty($toDelete)) { foreach ($toDelete as $delete) { UserOptions::model()->findByAttributes(array('UserId' => $id, 'AttributeId' => $key, 'AttributeValueId' => $delete))->delete(); } } } else { if (UserOptions::model()->findAllByAttributes(array('UserId' => $id, 'AttributeId' => $key))) { UserOptions::model()->deleteAllByAttributes(array('UserId' => $id, 'AttributeId' => $key)); } } } else { if (UserOptions::model()->findByAttributes(array('UserId' => $id, 'AttributeId' => $key))) { $res = UserOptions::model()->findByAttributes(array('UserId' => $id, 'AttributeId' => $key)); if ($data) { $res->AttributeValueId = $data; $res->save(); } else { $res->delete(); } } else { $options_list = new UserOptions(); $options_list->UserId = $id; $options_list->AttributeId = $key; $options_list->AttributeValueId = $data; $options_list->save(); } } } } $this->redirect(array('update', 'id' => $model->Objid)); } $this->render('update', array('model' => $model, 'options_list' => $options_list, 'options_text' => $options_text)); }
public function actionIndex() { $id = Yii::app()->user->getId(); $model = $this->loadModel($id); if (UserOptions::model()->findByAttributes(array('UserId' => $id))) { $options_list = UserOptions::model()->findByAttributes(array('UserId' => $id)); } else { $options_list = new UserOptions(); } $profileImage = UserImages::model()->findByAttributes(array("UserId" => $id, "MainImage" => 1)); $UserOption = UserOptions::model()->findByAttributes(array('UserId' => $id)); $birthday = explode('-', $model->DateOfBirth); $model->year = $birthday[0]; $model->month = $birthday[1]; $model->day = $birthday[2]; if (isset($_POST['Profile'])) { $model->attributes = $_POST['Profile']; $model->year = $_POST['Profile']['year']; $model->month = $_POST['Profile']['month']; $model->day = $_POST['Profile']['day']; $dob = (string) $model->year . "-" . $model->month . "-" . $model->day; $timestamp = strtotime($dob); $model->DateOfBirth = date('Y-m-d', $timestamp); $model->save(false); } if (isset($_POST['UserOptions'])) { $data = array(); $data['basic'] = $_POST['UserOptions']['AttributeValueId']; $jsonData = CJSON::encode($data); $options_list = UserOptions::model()->findByAttributes(array('UserId' => $id)); if ($options_list) { $options_list->UserId = $id; $options_list->AttributeValueId = $jsonData; } else { $options_list = new UserOptions(); $options_list->UserId = $id; $options_list->AttributeValueId = $jsonData; } $options_list->save(false); } $this->render('index', array('model' => $model, 'options_list' => $options_list, 'profileImage' => $profileImage)); }