District Core Developer DocsDistrict Core Developer Docs
Developers
Boilerplate
Modules
Bitbucket
Developers
Boilerplate
Modules
Bitbucket
  • Modules

    • ABN
    • ActivityLog
    • AnalyticsApi
    • ApiConnector
    • BlockApi
    • CategoryApi
    • CloneApi
    • CommentApi
    • ContentApi
    • Core
    • Documents
    • EmbedApi
    • Event
    • ExportApi
    • FeatureApi
    • FormApi
    • GTM
    • GalleryApi
    • HelpApi
    • Hotspot
    • IdeaSurvey
    • ImportApi
    • InteractionsApi
    • Intercom
    • MailApi
    • MapApi
    • MapSurvey
    • MediaApi
    • MenuApi
    • MetaTagApi
    • NlpApi
    • NotificationApi
    • Page
    • ParentableContent
    • PaymentApi
    • PermissionsApi
    • Postcode
    • ReCaptcha
    • Redirects
    • Renderer
    • ReportApi
    • RestrictionApi
    • RevisionApi
    • SearchApi
    • Settings
    • ShareableApi
    • Slack
    • SlugApi
    • SubscribableApi
    • Survey
    • Team
    • TenantApi
    • TestApi
    • ThemeApi
    • Timeline
    • TranslationApi
    • Update
    • Users
    • VisualisationApi
    • WorkflowApi
    • Wysiwyg

ExportAPI Module

Export Api provides with you a feature for your models to be exported in excel format.

Exporting to excel

  1. Implement the Modules\ExportApi\Contracts\CanExportTabularData interface along with Modules\ExportApi\Concerns\HasTabularExportable trait on your eloquent model.

  2. Create a export class that extends Modules\ExportApi\Exports\BaseTabularExport. Here you can define your query, mappings and headings. See Modules\ContentApi\Exports\ContentExport for example.

  3. Define exportTabularClass variable on your model for the export class that you created.

protected string|array $exportTabularClass = YourExportClass::class
  1. That's it, now export is enabled and can be triggered. Below is an example.
use Modules\ExportApi\Services\ExportApiService;

/**
* ExportXlsx is the export type plugin class, Content is you model context and options for additional options.
*/
return ExportApiService::export(ExportXlsx::class, Content::class, [
            'fileName' => Str::plural($this->getContentBundle()),
            'contentBundle' => get_class(ContentBundleService::getBundleFromMachineName($this->getContentBundle())),
]);
  1. If you have multiple export classes for your model. Define it like this.
protected string|array $exportTabularClass = [
    'followers' => FollowersExport::class,
];

return ExportApiService::export(ExportXlsx::class, Content::class, [
            'fileName' => Str::plural($this->getContentBundle()),
            // Fallbacks to exportType => 'default' if not provided.
            'exportType' => 'followers'
]);

Exporting to excel using queues.

  1. For queueable exports all the steps are same as defined per above. The only difference is instead of extending Modules\ExportApi\Exports\BaseTabularExport. You need to extend Modules\ExportApi\Exports\BaseQueueableTabularExport. Please ensure you run lando artisan horizon on the background to process queue jobs. Once the queue is processed you will receive a email and database notification if requester is provided for that export and if you have configured the step 2 properly.

  2. In order to customize the mail message sent and the data to store in database for the notification purpose. Please configure the following on your export class.

use Modules\MailApi\Messages\TransactionalMailMessage;

// For mail notification.
public function getNotifiableMessage(): MailMessage
{
    // Example.
    return TransactionalMailMessage::new()
			->subject('Your subject')			
			->action('Download submissions export', $this->getDownloadableUrl())
			->line('NOTE: The export link will expire at '.$this->getExpirationDateTime().', after that time you will need to generate another export');
}

// For db notification.
public function getToArray(): array
{
    // Example.
    return [
		'context_model' => Survey::class,
		'context_id' => $this->survey->id,
		'title' => "Export complete for {$this->survey->title}",
		'description' => 'Export expires at '.$this->getExpirationDateTime(), // Automatically injected onto export class.
		'url' => $this->getDownloadableUrl() // Automatically injected onto export class.
	];

}
  1. The $this->getDownloadableUrl() and $this->getExpirationDateTime() is automatically injected onto export class and the export will expire after the specified time. The expiration date time will be a carbon instance.

Note: Exporting to excel is done via Laravel Excel. Please also note, that the resourceful routes are already declared in Modules\Core\Routing\ResourceRegistrar for any corresponding model. The only thing required is to declare a controller action with the name as export and appropriate permission in policy if not already set. If you are using authorizeResource middleware on the controller then the export action is also automatically mapped to export policy method where permissions can be applied.

Exporting to PDF/Image

Exporting to pdf/jpg of page is also possible through this module for instance, if you want to download insight on reports. This is all handled by javascript.

  1. Import PageExport.vue that is made available by this component which is living under Resources/Assets/js/Components and it provides a modal and a button. Pass in the props if further customization is needed.

  2. By default the export for both pdf and image is handled using an id of the element that is available on the page which is print-area. Feel free to customize this by passing on the exportWrapperId prop to the component. Also an element with an id of main should exist for exporting to image. But the id's should already exist on dashboard layout.


Edit this page
Prev
Event
Next
FeatureApi