NEW Add cover image to a document

This commit is contained in:
Sacha Judd 2017-05-15 17:17:10 +12:00
parent 6b80f32832
commit e1e5194c1c
5 changed files with 47 additions and 6 deletions

View File

@ -47,6 +47,10 @@ class DMSDocument extends DataObject implements DMSDocumentInterface
'Sets' => 'DMSDocumentSet'
);
private static $has_one = array(
'CoverImage' => 'Image'
);
private static $many_many = array(
'RelatedDocuments' => 'DMSDocument',
'Tags' => 'DMSTag',
@ -906,8 +910,14 @@ class DMSDocument extends DataObject implements DMSDocumentInterface
$fieldsTop = $this->getFieldsForFile($relationList->count());
$fields->add($fieldsTop);
$fields->add(new TextField('Title', 'Title'));
$fields->add(new TextareaField('Description', 'Description'));
$fields->add(TextField::create('Title', _t('DMSDocument.TITLE', 'Title')));
$fields->add(TextareaField::create('Description', _t('DMSDocument.DESCRIPTION', 'Description')));
$coverImageField = UploadField::create('CoverImage', _t('DMSDocument.COVERIMAGE', 'Cover Image'));
$coverImageField->getValidator()->setAllowedExtensions(array('jpg', 'jpeg', 'png', 'gif'));
$coverImageField->setConfig('allowedMaxFileNumber', 1);
$fields->add($coverImageField);
$downloadBehaviorSource = array(
'open' => _t('DMSDocument.OPENINBROWSER', 'Open in browser'),
@ -1112,6 +1122,19 @@ class DMSDocument extends DataObject implements DMSDocumentInterface
return CompositeField::create($fields);
}
/**
* Return a title to use on the frontend, preferably the "title", otherwise the filename without it's numeric ID
*
* @return string
*/
public function getTitle()
{
if ($this->getField('Title')) {
return $this->getField('Title');
}
return $this->FilenameWithoutID;
}
public function onBeforeWrite()
{
parent::onBeforeWrite();

View File

@ -9,6 +9,8 @@ en:
TYPE: 'File type'
URL: URL
DMSDocument:
COVERIMAGE: Cover Image
DESCRIPTION: Description
EDIT: Edit
EDITDOCUMENT: Edit this document
PLURALNAME: Documents
@ -18,6 +20,7 @@ en:
Versions: Versions
DOWNLOAD: "Download {title}"
LASTCHANGED: "Last changed: {date}"
TITLE: Title
DMSDocumentSet:
ADDDOCUMENTBUTTON: Add Document
ADDDOCUMENTSBUTTON: Add Documents

View File

@ -1,9 +1,11 @@
<% if not $isHidden %>
<div class="document $Extension">
<% if $Title %>
<h4><a href="$Link" title="<%t DMSDocument.DOWNLOAD "Download {title}" title=$Title %>">$Title</a></h4>
<% else %>
<h4><a href="$Link" title="<%t DMSDocument.DOWNLOAD "Download {title}" title=$FilenameWithoutID %>">$FilenameWithoutID</a></h4>
<h4><a href="$Link" title="<%t DMSDocument.DOWNLOAD "Download {title}" title=$getTitle %>">$getTitle</a></h4>
<% if $CoverImage %>
<div class="article-thumbnail">
$CoverImage.FitMax(100, 100)
</div>
<% end_if %>
<p class="details"><% include DocumentDetails %></p>

View File

@ -247,4 +247,16 @@ class DMSDocumentTest extends SapphireTest
$this->assertCount(3, $result, 'Document 1 is related to 3 Pages');
$this->assertSame(array('s1', 's2', 's3'), $result->column('URLSegment'));
}
/**
* Test that the title is returned if it is set, otherwise the filename without ID
*/
public function testGetTitleOrFilenameWithoutId()
{
$d1 = $this->objFromFixture('DMSDocument', 'd1');
$this->assertSame('test-file-file-doesnt-exist-1', $d1->getTitle());
$d2 = $this->objFromFixture('DMSDocument', 'd2');
$this->assertSame('File That Doesn\'t Exist (Title)', $d2->getTitle());
}
}

View File

@ -78,6 +78,7 @@ DMSDocument:
d2:
Filename: test-file-file-doesnt-exist-2
Folder: 5
Title: File That Doesn't Exist (Title)
Tags: =>DMSTag.t5, =>DMSTag.t6
Sets: =>DMSDocumentSet.ds1, =>DMSDocumentSet.ds2, =>DMSDocumentSet.ds3, =>DMSDocumentSet.ds4
document_with_relations: