public function prepare() { $this->files = array(); $templatePath = $this->templatePath; if (!empty($_FILES)) { $tempFile = $_FILES['CrudCode']['tmp_name']['file']; $fileTypes = array('xls', 'xlsx'); // File extensions $fileParts = pathinfo($_FILES['CrudCode']['name']['file']); 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; //die(); while (!empty($sheetData[$baseRow]['A'])) { $read_status = true; $this->modelType = $sheetData[$baseRow]['B']; $this->model = $sheetData[$baseRow]['C']; $this->controller = $sheetData[$baseRow]['D']; $this->modelParent = $sheetData[$baseRow]['E']; $this->controllerParent = $sheetData[$baseRow]['F']; $class = @Yii::import($this->model, true); $table = CActiveRecord::model($class)->tableSchema; $this->_modelClass = $class; $this->_table = $table; $controllerTemplateFile = $templatePath . DIRECTORY_SEPARATOR . 'controller.php'; $this->files[] = new CCodeFile($this->controllerFile, $this->render($controllerTemplateFile)); $files = scandir($templatePath); foreach ($files as $file) { if (is_file($templatePath . '/' . $file) && CFileHelper::getExtension($file) === 'php' && $file !== 'controller.php') { $this->files[] = new CCodeFile($this->viewPath . DIRECTORY_SEPARATOR . $file, $this->render($templatePath . '/' . $file)); } } $baseRow++; } if ($this->files != array()) { $this->save(); } } } else { $controllerTemplateFile = $templatePath . DIRECTORY_SEPARATOR . 'controller.php'; $this->files[] = new CCodeFile($this->controllerFile, $this->render($controllerTemplateFile)); $files = scandir($templatePath); foreach ($files as $file) { if (is_file($templatePath . '/' . $file) && CFileHelper::getExtension($file) === 'php' && $file !== 'controller.php') { $this->files[] = new CCodeFile($this->viewPath . DIRECTORY_SEPARATOR . $file, $this->render($templatePath . '/' . $file)); } } } }
/** * Register autoloader. */ public static function init() { if (!self::$_isInitialized) { $lib = Yii::getPathOfAlias(self::$libPathPHPExcel) . '.php'; if (!file_exists($lib)) { Yii::log("PHP Excel lib not found({$lib}). Export disabled !", CLogger::LEVEL_WARNING, 'EHeartExcel'); } else { spl_autoload_unregister(array('YiiBase', 'autoload')); Yii::import(self::$libPathPHPExcel, true); spl_autoload_register(array('YiiBase', 'autoload')); self::$_isInitialized = true; } } }
public function init() { if (isset($_GET[$this->grid_mode_var])) { $this->grid_mode = $_GET[$this->grid_mode_var]; } if (isset($_GET['exportType'])) { $this->exportType = $_GET['exportType']; } if ($this->grid_mode == 'export') { $this->title = $this->title ? $this->title : Yii::app()->getController()->getPageTitle(); $this->initColumns(); } if ($this->exportType == "PDF") { Yii::import('ext.heart.pdf.EHeartPDF', true); EHeartPDF::init(); // create new PDF document $this->pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); $this->pdf->SetCreator(PDF_CREATOR); $this->pdf->SetAuthor('YiiHeart'); } if ($this->exportType == "WORD") { Yii::import('ext.heart.opentbs.EHeartOpenTBS', true); EHeartOpenTBS::init(); $this->TBS = new clsTinyButStrong(); // new instance of TBS $this->TBS->Plugin(TBS_INSTALL, OPENTBS_PLUGIN); // load the OpenTBS plugin $templatePath = Yii::getPathOfAlias('ext.heart.opentbs'); $template = $templatePath . DIRECTORY_SEPARATOR . 'template.docx'; $this->TBS->LoadTemplate($template); } else { if ($this->grid_mode == 'export') { Yii::import('ext.heart.excel.EHeartExcel', true); EHeartExcel::init(); $this->objPHPExcel = new PHPExcel(); // Creating a workbook $this->objPHPExcel->getProperties()->setCreator($this->creator); $this->objPHPExcel->getProperties()->setTitle($this->title); $this->objPHPExcel->getProperties()->setSubject($this->subject); $this->objPHPExcel->getProperties()->setDescription($this->description); $this->objPHPExcel->getProperties()->setCategory($this->category); } else { parent::init(); } } }
/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionImport() { $model = new Realize(); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['Realize'])) { if (!empty($_FILES)) { $tempFile = $_FILES['Realize']['tmp_name']['fileImport']; $fileTypes = array('xls', 'xlsx'); // File extensions $fileParts = pathinfo($_FILES['Realize']['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; //$realize_id= $sheetData[$baseRow]['A']; $faktura_id = $sheetData[$baseRow]['B']; $prod_id = $sheetData[$baseRow]['C']; $price = $sheetData[$baseRow]['D']; $count = $sheetData[$baseRow]['E']; $model2 = new Realize(); //$model2->realize_id= $realize_id; $model2->faktura_id = $faktura_id; $model2->prod_id = $prod_id; $model2->price = $price; $model2->count = $count; 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)); } }
public function actionTemplate() { Yii::import('ext.heart.excel.EHeartExcel', true); EHeartExcel::init(); $objPHPExcel = new PHPExcel(); $objPHPExcel->getActiveSheet()->setCellValue('A1', 'No')->setCellValue('B1', 'ModelType')->setCellValue('C1', 'Model')->setCellValue('D1', 'Controller')->setCellValue('E1', 'ModelParent')->setCellValue('F1', 'ControllerParent'); $modelPath = Yii::getPathOfAlias('application.models'); $files = scandir($modelPath); $row = 2; foreach ($files as $file) { if (is_file($modelPath . '/' . $file) && CFileHelper::getExtension($file) === 'php' && !in_array($file, array('ContactForm.php', 'LoginForm.php', 'Admin.php', 'User.php'))) { $file_arr = explode(".", $file); $filename = $file_arr[0]; $objPHPExcel->getActiveSheet()->setCellValue('A' . $row, $row - 1)->setCellValue('B' . $row, "1")->setCellValue('C' . $row, $filename)->setCellValue('D' . $row, 'test/' . $filename)->setCellValue('E' . $row, '-')->setCellValue('F' . $row, '-'); $row++; } } // Redirect output to a client’s web browser (Excel2007) header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment;filename="models.xlsx"'); header('Cache-Control: max-age=0'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter->save('php://output'); exit; }