public function testRun() { $quote = DatabaseCompatibilityUtil::getQuote(); //Create 2 export items, and set one with a date over a week ago (8 days ago) for the modifiedDateTime $exportItem = new ExportItem(); $exportItem->isCompleted = 0; $exportItem->exportFileType = 'csv'; $exportItem->exportFileName = 'test'; $exportItem->modelClassName = 'Account'; $exportItem->serializedData = serialize(array('test', 'test2')); $this->assertTrue($exportItem->save()); $fileContent = new FileContent(); $fileContent->content = 'test'; $exportFileModel = new ExportFileModel(); $exportFileModel->fileContent = $fileContent; $exportFileModel->name = $exportItem->exportFileName . ".csv"; $exportFileModel->type = 'application/octet-stream'; $exportFileModel->size = strlen($fileContent->content); $this->assertTrue($exportFileModel->save()); $exportFileModel1Id = $exportFileModel->id; $exportItem->exportFileModel = $exportFileModel; $this->assertTrue($exportItem->save()); $modifiedDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time() - 60 * 60 * 24 * 8); $sql = "Update item set modifieddatetime = '" . $modifiedDateTime . "' where id = " . $exportItem->getClassId('Item'); ZurmoRedBean::exec($sql); // Second exportItem, that shouldn't be deleted. $exportItem2 = new ExportItem(); $exportItem2->isCompleted = 0; $exportItem2->exportFileType = 'csv'; $exportItem2->exportFileName = 'test'; $exportItem2->modelClassName = 'Account'; $exportItem2->serializedData = serialize(array('test', 'test2')); $this->assertTrue($exportItem2->save()); $fileContent2 = new FileContent(); $fileContent2->content = 'test'; $exportFileModel2 = new ExportFileModel(); $exportFileModel2->fileContent = $fileContent2; $exportFileModel2->name = $exportItem->exportFileName . ".csv"; $exportFileModel2->type = 'application/octet-stream'; $exportFileModel2->size = strlen($fileContent->content); $this->assertTrue($exportFileModel2->save()); $exportFileModel2Id = $exportFileModel2->id; $exportItem2->exportFileModel = $exportFileModel2; $this->assertTrue($exportItem2->save()); $job = new ExportCleanupJob(); $this->assertTrue($job->run()); $exportItems = ExportItem::getAll(); $this->assertEquals(1, count($exportItems)); $this->assertEquals($exportItem2->id, $exportItems[0]->id); }
/** * @param string $content * @param string $exportFileName * @return ExportFileModel * @throws FailedToSaveModelException */ protected function makeExportFileModelByContent($content, $exportFileName) { assert('is_string($exportFileName)'); $fileContent = new FileContent(); $fileContent->content = $content; $exportFileModel = new ExportFileModel(); $exportFileModel->fileContent = $fileContent; $exportFileModel->name = $exportFileName . ".csv"; $exportFileModel->type = 'application/octet-stream'; $exportFileModel->size = strlen($content); $saved = $exportFileModel->save(); if (!$saved) { throw new FailedToSaveModelException(); } return $exportFileModel; }
public function run() { $exportItems = ExportItem::getUncompletedItems(); if (count($exportItems) > 0) { foreach ($exportItems as $exportItem) { if (isset($exportItem->exportFileModel)) { //continue; } $unserializedData = unserialize($exportItem->serializedData); if ($unserializedData instanceof RedBeanModelDataProvider) { $formattedData = $unserializedData->getData(); } else { $formattedData = array(); foreach ($unserializedData as $idToExport) { $model = call_user_func(array($exportItem->modelClassName, 'getById'), intval($idToExport)); $formattedData[] = $model; } } if ($exportItem->exportFileType == 'csv') { $headerData = array(); foreach ($formattedData as $model) { if (ControllerSecurityUtil::doesCurrentUserHavePermissionOnSecurableItem($model, Permission::READ)) { $modelToExportAdapter = new ModelToExportAdapter($model); if (count($headerData) == 0) { $headerData = $modelToExportAdapter->getHeaderData(); } $data[] = $modelToExportAdapter->getData(); } } $output = ExportItemToCsvFileUtil::export($data, $headerData); $fileContent = new FileContent(); $fileContent->content = $output; $exportFileModel = new ExportFileModel(); $exportFileModel->fileContent = $fileContent; $exportFileModel->name = $exportItem->exportFileName . ".csv"; $exportFileModel->type = 'application/octet-stream'; $exportFileModel->size = strlen($output); $saved = $exportFileModel->save(); if ($saved) { $exportItem->isCompleted = 1; $exportItem->exportFileModel = $exportFileModel; $exportItem->save(); $message = new NotificationMessage(); $message->htmlContent = Zurmo::t('ExportModule', 'Export of {fileName} requested on {dateTime} is completed. <a href="{url}">Click here</a> to download file!', array('{fileName}' => $exportItem->exportFileName, '{url}' => Yii::app()->createUrl('export/default/download', array('id' => $exportItem->id)), '{dateTime}' => DateTimeUtil::convertDbFormattedDateTimeToLocaleFormattedDisplay($exportItem->createdDateTime, 'long'))); $rules = new ExportProcessCompletedNotificationRules(); NotificationsUtil::submit($message, $rules); } } } } else { return true; } return true; }