/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate() { $model = new MenuType(); if (isset($_POST['MenuType'])) { $model->attributes = $_POST['MenuType']; if ($model->validate()) { if ($model->save()) { Yii::app()->user->setFlash('success', Yii::t('menutype', 'Add menutype successfully.')); } else { Yii::app()->user->setFlash('error', Yii::t('menutype', 'Add menutype failed. Please try it later.')); } $this->redirect(array('/' . backend . '/menutype/admin')); } } $this->render(strtolower($this->action->Id), array('model' => $model)); }
/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionImport() { $model = new MenuType(); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['MenuType'])) { if (!empty($_FILES)) { $tempFile = $_FILES['MenuType']['tmp_name']['fileImport']; $fileTypes = array('xls', 'xlsx'); // File extensions $fileParts = pathinfo($_FILES['MenuType']['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; //$menuType_id= $sheetData[$baseRow]['A']; $name = $sheetData[$baseRow]['B']; $model2 = new MenuType(); //$model2->menuType_id= $menuType_id; $model2->name = $name; 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)); } }