Пример #1
0
 public function run()
 {
     /** @var Controller $controller */
     $controller = $this->controller;
     $data = ['controllerClassName' => $controller->className(), 'actionId' => $this->id, 'params' => Yii::$app->request->getQueryParams()];
     $exportRequest = new ExportRequest();
     $exportRequest->user_id = Yii::$app->user->id;
     $exportRequest->data = $data;
     if ($exportRequest->save() == false) {
         Yz::errorFlash(Json::encode($exportRequest->getFirstErrors()));
     }
     Yii::$app->session->setFlash(Yz::FLASH_INFO, Yii::t('admin/export', 'Export request was added to queue. System will notify you when it will be done'));
     return $controller->goBack();
 }
 /**
  * Finds the ExportRequest model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return ExportRequest the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = ExportRequest::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
 /**
  * @param ExportRequest $request
  * @return bool
  */
 protected function processExportRequest($request)
 {
     $controllerClassName = $request->data['controllerClassName'];
     $actionId = $request->data['actionId'];
     $requestParams = $request->data['params'];
     /** @var \backend\base\Controller $originalController */
     $originalController = new $controllerClassName('temp-controller', $this->module);
     /** @var ExportAction $action */
     $action = $originalController->createAction($actionId);
     if (!is_a($action, ExportAction::className())) {
         return false;
     }
     if (is_callable($action->searchModel)) {
         $searchModel = call_user_func($action->searchModel, $requestParams);
     } else {
         $searchModel = $action->searchModel;
     }
     /** @var DataProviderInterface $dataProvider */
     $dataProvider = call_user_func($action->dataProvider, $requestParams, $searchModel);
     /** @var array $gridColumns */
     $gridColumns = call_user_func($action->getGridColumns(), $searchModel, $dataProvider, $requestParams);
     try {
         $exportedContent = GridView::widget(['renderAllPages' => true, 'runInConsoleMode' => true, 'layout' => "{items}", 'tableOptions' => ['class' => ''], 'dataProvider' => $dataProvider, 'columns' => $gridColumns]);
     } catch (Exception $e) {
         $request->delete();
         SystemEvent::create('error', $request->user_id, Yii::t('admin/export', 'Requested data was not exported, aborting'));
         return false;
     }
     $fileContent = strtr(ExportAction::EXPORT_TEMPLATE, ['{name}' => $action->reportName, '{grid}' => $exportedContent]);
     $fileName = 'Export_' . $request->id . '_' . strtotime('%d%m%Y%H%i%s') . '.xls';
     $request->file = $fileName;
     $request->is_exported = 1;
     $request->save();
     FileHelper::createDirectory(Yii::getAlias(ExportRequest::FILE_PATH));
     file_put_contents(Yii::getAlias($request->fullFileName), $fileContent);
     SystemEvent::create('success', $request->user_id, Yii::t('admin/export', 'Data requested for exporting is ready'), ['/admin-export/export-requests/index']);
     return true;
 }