예제 #1
0
 public function testGetSerializedDataForExport()
 {
     $super = User::getByUsername('super');
     Yii::app()->user->userModel = $super;
     $account = new Account(false);
     $searchForm = new AccountsSearchForm($account);
     $dataProvider = ExportTestHelper::makeRedBeanDataProvider($searchForm, 'Account', 0, Yii::app()->user->userModel->id);
     $serializedData = ExportUtil::getSerializedDataForExport($dataProvider);
     $this->assertEquals($dataProvider, unserialize($serializedData));
 }
예제 #2
0
 /**
  * @depends testExportByModelIds
  */
 public function testExportReportWithMultiplePagesOfData()
 {
     $super = User::getByUsername('super');
     Yii::app()->user->userModel = $super;
     $numberOfUserNotifications = Notification::getCountByTypeAndUser('ExportProcessCompleted', Yii::app()->user->userModel);
     $report = new Report();
     $report->setType(Report::TYPE_ROWS_AND_COLUMNS);
     $report->setModuleClassName('AccountsModule');
     $report->setFiltersStructure('');
     $displayAttribute = new DisplayAttributeForReportForm('AccountsModule', 'Account', Report::TYPE_ROWS_AND_COLUMNS);
     $displayAttribute->setModelAliasUsingTableAliasName('model1');
     $displayAttribute->attributeIndexOrDerivedType = 'name';
     $report->addDisplayAttribute($displayAttribute);
     $dataProvider = new RowsAndColumnsReportDataProvider($report);
     $exportItem = new ExportItem();
     $exportItem->isCompleted = 0;
     $exportItem->exportFileType = 'csv';
     $exportItem->exportFileName = 'rowAndColumnsTest2';
     $exportItem->modelClassName = 'SavedReport';
     $exportItem->serializedData = ExportUtil::getSerializedDataForExport($dataProvider);
     $this->assertTrue($exportItem->save());
     $id = $exportItem->id;
     $exportItem->forget();
     unset($exportItem);
     //Delete queued jobs from test exportItems created above
     Yii::app()->jobQueue->deleteAll();
     ExportModule::$asynchronousPageSize = 2;
     $job = new ExportJob();
     $this->assertEquals(0, count(Yii::app()->jobQueue->getAll()));
     $this->assertTrue($job->run());
     $this->assertEquals(0, count(Yii::app()->jobQueue->getAll()));
     $exportItem = ExportItem::getById($id);
     $fileModel = $exportItem->exportFileModel;
     $this->assertEquals(1, $exportItem->isCompleted);
     $this->assertEquals(0, $exportItem->processOffset);
     $this->assertEquals('csv', $exportItem->exportFileType);
     $this->assertEquals('rowAndColumnsTest2', $exportItem->exportFileName);
     $this->assertTrue($fileModel instanceof ExportFileModel);
     $accounts = Account::getAll();
     $headerData = array('Name');
     $data = array();
     foreach ($accounts as $account) {
         $data[] = array($account->name);
     }
     $output = ExportItemToCsvFileUtil::export($data, $headerData, 'rowAndColumnsTest2.csv', false);
     $this->assertEquals($output, $fileModel->fileContent->content);
     // Check if user got notification message, and if its type is ExportProcessCompleted
     $this->assertEquals($numberOfUserNotifications + 1, Notification::getCountByTypeAndUser('ExportProcessCompleted', Yii::app()->user->userModel));
     //Matrix report should not paginate
     $report = new Report();
     $report->setType(Report::TYPE_MATRIX);
     $report->setModuleClassName('AccountsModule');
     $report->setFiltersStructure('');
     $displayAttribute = new DisplayAttributeForReportForm('AccountsModule', 'Account', Report::TYPE_MATRIX);
     $displayAttribute->setModelAliasUsingTableAliasName('model1');
     $displayAttribute->attributeIndexOrDerivedType = 'Count';
     $report->addDisplayAttribute($displayAttribute);
     $groupBy = new GroupByForReportForm('AccountsModule', 'Account', Report::TYPE_MATRIX);
     $groupBy->attributeIndexOrDerivedType = 'name';
     $groupBy->axis = 'y';
     $report->addGroupBy($groupBy);
     $groupBy = new GroupByForReportForm('AccountsModule', 'Account', Report::TYPE_MATRIX);
     $groupBy->attributeIndexOrDerivedType = 'officePhone';
     $report->addGroupBy($groupBy);
     $dataProvider = new MatrixReportDataProvider($report);
     $exportItem = new ExportItem();
     $exportItem->isCompleted = 0;
     $exportItem->exportFileType = 'csv';
     $exportItem->exportFileName = 'matrixTest1';
     $exportItem->modelClassName = 'SavedReport';
     $exportItem->serializedData = ExportUtil::getSerializedDataForExport($dataProvider);
     $this->assertTrue($exportItem->save());
     $id = $exportItem->id;
     $exportItem->forget();
     unset($exportItem);
     //Delete queued jobs from test exportItems created above
     Yii::app()->jobQueue->deleteAll();
     ExportModule::$asynchronousPageSize = 2;
     $job = new ExportJob();
     $this->assertTrue($job->run());
     $exportItem = ExportItem::getById($id);
     $fileModel = $exportItem->exportFileModel;
     $this->assertEquals(1, $exportItem->isCompleted);
     $this->assertEquals(0, $exportItem->processOffset);
     $this->assertEquals('csv', $exportItem->exportFileType);
     $this->assertEquals('matrixTest1', $exportItem->exportFileName);
     $this->assertTrue($fileModel instanceof ExportFileModel);
     $fileContent = $fileModel->fileContent->content;
     $this->assertContains('Test Account', $fileContent);
     $this->assertContains('Test Account 2', $fileContent);
     $this->assertContains('Test Account 3', $fileContent);
     $this->assertContains('Test Account 4', $fileContent);
     // Check if user got notification message, and if its type is ExportProcessCompleted
     $this->assertEquals($numberOfUserNotifications + 2, Notification::getCountByTypeAndUser('ExportProcessCompleted', Yii::app()->user->userModel));
 }
 public function actionExport($id, $stickySearchKey = null)
 {
     assert('$stickySearchKey == null || is_string($stickySearchKey)');
     $savedReport = SavedReport::getById((int) $id);
     ControllerSecurityUtil::resolveCanCurrentUserAccessModule($savedReport->moduleClassName);
     ControllerSecurityUtil::resolveAccessCanCurrentUserReadModel($savedReport);
     $report = SavedReportToReportAdapter::makeReportBySavedReport($savedReport);
     $dataProvider = $this->getDataProviderForExport($report, $report->getId(), false);
     $totalItems = intval($dataProvider->calculateTotalItemCount());
     $data = array();
     if ($totalItems > 0) {
         if ($totalItems <= ExportModule::$asynchronousThreshold) {
             // Output csv file directly to user browser
             if ($dataProvider) {
                 $reportToExportAdapter = ReportToExportAdapterFactory::createReportToExportAdapter($report, $dataProvider);
                 $headerData = $reportToExportAdapter->getHeaderData();
                 $data = $reportToExportAdapter->getData();
             }
             // Output data
             if (count($data)) {
                 $fileName = $this->getModule()->getName() . ".csv";
                 ExportItemToCsvFileUtil::export($data, $headerData, $fileName, true);
             } else {
                 Yii::app()->user->setFlash('notification', Zurmo::t('ZurmoModule', 'There is no data to export.'));
             }
         } else {
             if ($dataProvider) {
                 $serializedData = ExportUtil::getSerializedDataForExport($dataProvider);
             }
             // Create background job
             $exportItem = new ExportItem();
             $exportItem->isCompleted = 0;
             $exportItem->exportFileType = 'csv';
             $exportItem->exportFileName = $this->getModule()->getName();
             $exportItem->modelClassName = 'SavedReport';
             $exportItem->serializedData = $serializedData;
             $exportItem->save();
             $exportItem->forget();
             Yii::app()->user->setFlash('notification', Zurmo::t('ZurmoModule', 'A large amount of data has been requested for export.  You will receive ' . 'a notification with the download link when the export is complete.'));
         }
     } else {
         Yii::app()->user->setFlash('notification', Zurmo::t('ZurmoModule', 'There is no data to export.'));
     }
     $this->redirect(array($this->getId() . '/index'));
 }
 protected function export($stickySearchKey = null, $modelClassName = null, $exportFileName = null)
 {
     assert('$stickySearchKey == null || is_string($stickySearchKey)');
     assert('$modelClassName == null || is_string($modelClassName)');
     assert('$exportFileName == null || is_string($exportFileName)');
     if ($modelClassName == null) {
         $modelClassName = $this->getModelName();
     }
     $searchFormClassName = static::getSearchFormClassName();
     $pageSize = null;
     $model = new $modelClassName(false);
     if ($searchFormClassName != null) {
         $searchForm = new $searchFormClassName($model);
     } else {
         throw new NotSupportedException();
     }
     $stateMetadataAdapterClassName = $this->resolveStateMetadataAdapterClassNameForExport();
     $dataProvider = $this->getDataProviderByResolvingSelectAllFromGet($searchForm, $pageSize, Yii::app()->user->userModel->id, $stateMetadataAdapterClassName, $stickySearchKey);
     if (!$dataProvider) {
         $idsToExport = array_filter(explode(",", trim($_GET['selectedIds'], " ,")));
         // Not Coding Standard
     }
     $totalItems = static::getSelectedRecordCountByResolvingSelectAllFromGet($dataProvider, false);
     $data = array();
     if ($totalItems > 0) {
         if ($totalItems <= ExportModule::$asynchronousThreshold) {
             // Output csv file directly to user browser
             if ($dataProvider) {
                 $modelsToExport = ExportUtil::getDataForExport($dataProvider);
                 if (count($modelsToExport) > 0) {
                     $modelToExportAdapter = new ModelToExportAdapter($modelsToExport[0]);
                     $headerData = $modelToExportAdapter->getHeaderData();
                 }
                 foreach ($modelsToExport as $model) {
                     if (ControllerSecurityUtil::doesCurrentUserHavePermissionOnSecurableItem($model, Permission::READ)) {
                         $modelToExportAdapter = new ModelToExportAdapter($model);
                         $data[] = $modelToExportAdapter->getData();
                     }
                 }
             } else {
                 $headerData = array();
                 foreach ($idsToExport as $idToExport) {
                     $model = $modelClassName::getById(intval($idToExport));
                     if (ControllerSecurityUtil::doesCurrentUserHavePermissionOnSecurableItem($model, Permission::READ)) {
                         $modelToExportAdapter = new ModelToExportAdapter($model);
                         $data[] = $modelToExportAdapter->getData();
                         if (count($headerData) == 0) {
                             $headerData = $modelToExportAdapter->getHeaderData();
                         }
                     }
                 }
             }
             // Output data
             if (count($data)) {
                 if ($exportFileName == null) {
                     $fileName = $this->getModule()->getName() . ".csv";
                 } else {
                     $fileName = $exportFileName . ".csv";
                 }
                 $output = ExportItemToCsvFileUtil::export($data, $headerData, $fileName, true);
             } else {
                 Yii::app()->user->setFlash('notification', Zurmo::t('ZurmoModule', 'There is no data to export.'));
             }
         } else {
             if ($dataProvider) {
                 $serializedData = ExportUtil::getSerializedDataForExport($dataProvider);
             } else {
                 $serializedData = serialize($idsToExport);
             }
             // Create background job
             $exportItem = new ExportItem();
             $exportItem->isCompleted = false;
             $exportItem->exportFileType = 'csv';
             $exportItem->exportFileName = $this->getModule()->getName();
             $exportItem->modelClassName = $modelClassName;
             $exportItem->serializedData = $serializedData;
             $exportItem->isJobRunning = false;
             $exportItem->cancelExport = false;
             $exportItem->save();
             $exportItem->forget();
             Yii::app()->user->setFlash('notification', Zurmo::t('ZurmoModule', 'A large amount of data has been requested for export.  You will receive ' . 'a notification with the download link when the export is complete.'));
         }
     } else {
         Yii::app()->user->setFlash('notification', Zurmo::t('ZurmoModule', 'There is no data to export.'));
     }
     $this->redirect(array($this->getId() . '/index'));
 }