public function actionStructSave($id) { $struct = new DishStructure(); $halfStruct = new DishStructure2(); $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'); $this->chosenProduct = CHtml::listData(Products::model()->findAllByPk($prod_id), 'product_id', 'name'); $this->chosenHalfstuff = CHtml::listData(Halfstaff::model()->findAllByPk($stuff_id), 'halfstuff_id', 'name'); if ($_POST) { $transaction = Yii::app()->db->beginTransaction(); try { if ($_POST['prod']) { foreach ($_POST['prod'] as $key => $val) { $tempStruct = DishStructure::model()->updateAll(array('amount' => $val), 'dish_id = :dish_id AND prod_id = :prod_id', array(':dish_id' => $id, ':prod_id' => $key)); } } if ($_POST['half']) { foreach ($_POST['half'] as $key => $val) { $tempStruct = DishStructure2::model()->updateAll(array('amount' => $val), 'dish_id = :dish_id AND halfstuff_id = :halfstuff_id', array(':dish_id' => $id, ':halfstuff_id' => $key)); } } $transaction->commit(); Yii::app()->user->setFlash($messageType, $message); $this->redirect(array('index')); } catch (exception $ex) { $transaction->rollBack(); Yii::app()->user->setFlash('error', "{$e->getMessage()}"); } } $this->render('structSave', array()); }
/** * 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)); } }