Skip to content

Commit

Permalink
Allow featured images to be included in CSV export
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Thomson committed May 19, 2019
1 parent c4b5c7e commit 2a3d4ae
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
3 changes: 3 additions & 0 deletions assets/css/rainlab.blog-export.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.export-behavior .export-columns {
max-height: 450px !important;
}
7 changes: 7 additions & 0 deletions controllers/Posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ public function update($recordId = null)
return $this->asExtension('FormController')->update($recordId);
}

public function export()
{
$this->addCss('/plugins/rainlab/blog/assets/css/rainlab.blog-export.css');

return $this->asExtension('ImportExportController')->export();
}

public function listExtendQuery($query)
{
if (!$this->user->hasAnyAccess(['rainlab.blog.access_other_posts'])) {
Expand Down
26 changes: 24 additions & 2 deletions models/PostExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,32 @@ class PostExport extends ExportModel
]
];

public $hasMany = [
'featured_images' => [
'System\Models\File',
'order' => 'sort_order',
'key' => 'attachment_id',
'conditions' => 'field = "featured_images" AND attachment_type = "RainLab\\\\Blog\\\\Models\\\\Post"'
]
];

This comment has been minimized.

Copy link
@LukeTowers

LukeTowers May 20, 2019

Contributor

How come we can't use an attachMany relationship here instead?

This comment has been minimized.

Copy link
@bennothommo

bennothommo May 20, 2019

Author Contributor

@LukeTowers Unless I'm mistaken, attachMany doesn't allow you to specify the model name and defaults to using the current model name, so it would be searching for RainLab\Blog\Models\PostExport as the attachment type.

This comment has been minimized.

Copy link
@LukeTowers

LukeTowers May 20, 2019

Contributor

Ah, gotcha. Good point, excellent workaround too. Out of curiosity, does the import support featured images too?

This comment has been minimized.

Copy link
@bennothommo

bennothommo May 20, 2019

Author Contributor

@LukeTowers just export at this stage, just to cover the issue raised. :)

This comment has been minimized.

Copy link
@LukeTowers

LukeTowers May 20, 2019

Contributor

Fair enough :) Good job on this one!


/**
* The accessors to append to the model's array form.
* @var array
*/
protected $appends = [
'author_email',
'categories'
'categories',
'featured_image_urls'
];

public function exportData($columns, $sessionKey = null)
{
$result = self::make()
->with([
'post_user',
'post_categories'
'post_categories',
'featured_images'
])
->get()
->toArray()
Expand All @@ -69,4 +80,15 @@ public function getCategoriesAttribute()

return $this->encodeArrayValue($this->post_categories->lists('name'));
}

public function getFeaturedImageUrlsAttribute()
{
if (!$this->featured_images) {
return '';
}

return $this->encodeArrayValue($this->featured_images->map(function ($image) {
return $image->getPath();
}));
}
}
1 change: 1 addition & 0 deletions models/postexport/columns.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ columns:
slug: rainlab.blog::lang.post.slug
categories: rainlab.blog::lang.post.categories
author_email: rainlab.blog::lang.post.author_email
featured_image_urls: rainlab.blog::lang.post.featured_images
created_at: rainlab.blog::lang.post.created_date
updated_at: rainlab.blog::lang.post.updated_date
published_at: rainlab.blog::lang.post.published_date

0 comments on commit 2a3d4ae

Please sign in to comment.