/** * 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) { $products = new Products(); $stuff = new Halfstaff(); $prodList = $products->getUseProdList(); $prod_id = CHtml::listData(DishStructure::model()->findAll(array("condition" => "dish_id = {$id}")), 'struct_id', 'prod_id'); $stuff_id = CHtml::listData(DishStructure2::model()->findAll(array("condition" => "dish_id = {$id}")), 'struct2_id', 'halfstuff_id'); // $dish_id = CHtml::listData(DishStructure3::model()->findAll(array("condition"=>"dish_id = $id")),'struct3_id','dishes_id'); $chosenProd = Products::model()->with('Struct')->findAllByPk($prod_id, 'Struct.dish_id = :dish_id', array(':dish_id' => $id)); $stuffList = $stuff->getUseStuffList(); $chosenStuff = Halfstaff::model()->with('Struct')->findAllByPk($stuff_id, 'Struct.dish_id = :dish_id', array(':dish_id' => $id)); $model = $this->loadModel($id); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['Dishes'])) { $messageType = 'warning'; $message = "There are some errors "; $transaction = Yii::app()->db->beginTransaction(); try { $_POST['Dishes']['count'] = $this->changeToFloat($_POST['Dishes']['count']); if ($_POST['Dishes']['count'] == '' or $_POST['Dishes']['count'] == 0) { $_POST['Dishes']['count'] = 1; } $model->attributes = $_POST['Dishes']; $messageType = 'success'; $message = "<strong>Well done!</strong> You successfully update data "; if ($model->save()) { DishStructure::model()->deleteAll('dish_id=:dish_id', array(':dish_id' => $id)); DishStructure2::model()->deleteAll('dish_id=:dish_id', array(':dish_id' => $id)); if ($_POST['product_id'] != null) { $count = 0; $prodMes = "prod>"; for ($i = 0; $i < count($_POST['product_id']); $i++) { $ss = $this->changeToFloat($_POST['prod'][$i]); $struct = new DishStructure(); $struct->dish_id = $id; $struct->prod_id = $_POST['product_id'][$i]; $struct->amount = $ss; if ($struct->save()) { $messageType = 'success'; $prodMes .= $struct->prod_id . ":" . $struct->amount . ","; } $count++; } } if ($_POST['stuff_id'] != null) { $stuffMes = "stuff>"; for ($i = 0; $i < count($_POST['stuff_id']); $i++) { $ss = $this->changeToFloat($_POST['stuff'][$i]); $struct2 = new DishStructure2(); $struct2->dish_id = $id; $struct2->halfstuff_id = $_POST['stuff_id'][$i]; $struct2->amount = $ss; if ($struct2->save()) { $message = "<strong>Well done!</strong> You successfully create data "; $stuffMes .= $struct2->halfstuff_id . ":" . $struct2->amount . ","; } } } $this->logs('update', 'dishes', $model->dish_id, $model->name . "->" . $prodMes . "=>" . $stuffMes); $transaction->commit(); Yii::app()->user->setFlash($messageType, $message); $this->redirect(array('view', 'id' => $model->dish_id)); } } catch (Exception $e) { $transaction->rollBack(); Yii::app()->user->setFlash('error', "{$e->getMessage()}"); // $this->refresh(); } $model->attributes = $_POST['Dishes']; if ($model->save()) { } //$this->redirect(array('view','id'=>$model->dish_id)); } $this->render('update', array('model' => $model, 'prodList' => $prodList, 'chosenProd' => $chosenProd, 'stuffList' => $stuffList, 'chosenStuff' => $chosenStuff)); }
/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionImport() { $model = new DishStructure(); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['DishStructure'])) { if (!empty($_FILES)) { $tempFile = $_FILES['DishStructure']['tmp_name']['fileImport']; $fileTypes = array('xls', 'xlsx'); // File extensions $fileParts = pathinfo($_FILES['DishStructure']['name']['fileImport']); if (in_array(@$fileParts['extension'], $fileTypes)) { Yii::import('ext.heart.excel.EHeartExcel', true); EHeartExcel::init(); $inputFileType = PHPExcel_IOFactory::identify($tempFile); $objReader = PHPExcel_IOFactory::createReader($inputFileType); $objPHPExcel = $objReader->load($tempFile); $sheetData = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true); $baseRow = 2; $inserted = 0; $read_status = false; while (!empty($sheetData[$baseRow]['A'])) { $read_status = true; //$struct_id= $sheetData[$baseRow]['A']; $dish_id = $sheetData[$baseRow]['B']; $prod_id = $sheetData[$baseRow]['C']; $model2 = new DishStructure(); //$model2->struct_id= $struct_id; $model2->dish_id = $dish_id; $model2->prod_id = $prod_id; try { if ($model2->save()) { $inserted++; } } catch (Exception $e) { Yii::app()->user->setFlash('error', "{$e->getMessage()}"); //$this->refresh(); } $baseRow++; } Yii::app()->user->setFlash('success', $inserted . ' row inserted'); } else { Yii::app()->user->setFlash('warning', 'Wrong file type (xlsx, xls, and ods only)'); } } $this->render('admin', array('model' => $model)); } else { $this->render('admin', array('model' => $model)); } }