From e1e5194c1cedae13be414ab85fab728e417710db Mon Sep 17 00:00:00 2001 From: Sacha Judd Date: Mon, 15 May 2017 17:17:10 +1200 Subject: [PATCH] NEW Add cover image to a document --- code/model/DMSDocument.php | 27 +++++++++++++++++++++++++-- lang/en.yml | 3 +++ templates/Includes/Document.ss | 10 ++++++---- tests/DMSDocumentTest.php | 12 ++++++++++++ tests/dmstest.yml | 1 + 5 files changed, 47 insertions(+), 6 deletions(-) diff --git a/code/model/DMSDocument.php b/code/model/DMSDocument.php index 001ce27..d2d54b0 100644 --- a/code/model/DMSDocument.php +++ b/code/model/DMSDocument.php @@ -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(); diff --git a/lang/en.yml b/lang/en.yml index 18d5e8e..78d9f4f 100644 --- a/lang/en.yml +++ b/lang/en.yml @@ -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 diff --git a/templates/Includes/Document.ss b/templates/Includes/Document.ss index 2428804..a3fe7cf 100644 --- a/templates/Includes/Document.ss +++ b/templates/Includes/Document.ss @@ -1,9 +1,11 @@ <% if not $isHidden %>
- <% if $Title %> -

">$Title

- <% else %> -

">$FilenameWithoutID

+

">$getTitle

+ + <% if $CoverImage %> +
+ $CoverImage.FitMax(100, 100) +
<% end_if %>

<% include DocumentDetails %>

diff --git a/tests/DMSDocumentTest.php b/tests/DMSDocumentTest.php index c1a16a3..d010e3f 100644 --- a/tests/DMSDocumentTest.php +++ b/tests/DMSDocumentTest.php @@ -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()); + } } diff --git a/tests/dmstest.yml b/tests/dmstest.yml index ab88bdd..69adc7e 100644 --- a/tests/dmstest.yml +++ b/tests/dmstest.yml @@ -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: